2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, 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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: head/sys/netinet/sctputil.c 255190 2013-09-03 19:31:59Z tuexen $");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctputil.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_sysctl.h>
44 #if defined(__Userspace__) || defined(__FreeBSD__)
45 #include <netinet6/sctp6_var.h>
48 #include <netinet/sctp_header.h>
49 #include <netinet/sctp_output.h>
50 #include <netinet/sctp_uio.h>
51 #include <netinet/sctp_timer.h>
52 #include <netinet/sctp_indata.h>/* for sctp_deliver_data() */
53 #include <netinet/sctp_auth.h>
54 #include <netinet/sctp_asconf.h>
55 #include <netinet/sctp_bsd_addr.h>
56 #if defined(__Userspace__)
57 #include <netinet/sctp_constants.h>
59 #if defined(__FreeBSD__)
60 #include <netinet/udp.h>
61 #include <netinet/udp_var.h>
65 #if defined(__APPLE__)
66 #define APPLE_FILE_NO 8
69 #if defined(__Windows__)
70 #if !defined(SCTP_LOCAL_TRACE_BUF)
71 #include "eventrace_netinet.h"
72 #include "sctputil.tmh" /* this is the file that will be auto generated */
76 #define KTR_SCTP KTR_SUBSYS
80 extern struct sctp_cc_functions sctp_cc_functions[];
81 extern struct sctp_ss_functions sctp_ss_functions[];
84 sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
86 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
87 struct sctp_cwnd_log sctp_clog;
89 sctp_clog.x.sb.stcb = stcb;
90 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
92 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
94 sctp_clog.x.sb.stcb_sbcc = 0;
95 sctp_clog.x.sb.incr = incr;
96 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
99 sctp_clog.x.misc.log1,
100 sctp_clog.x.misc.log2,
101 sctp_clog.x.misc.log3,
102 sctp_clog.x.misc.log4);
107 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
109 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
110 struct sctp_cwnd_log sctp_clog;
112 sctp_clog.x.close.inp = (void *)inp;
113 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
115 sctp_clog.x.close.stcb = (void *)stcb;
116 sctp_clog.x.close.state = (uint16_t)stcb->asoc.state;
118 sctp_clog.x.close.stcb = 0;
119 sctp_clog.x.close.state = 0;
121 sctp_clog.x.close.loc = loc;
122 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
123 SCTP_LOG_EVENT_CLOSE,
125 sctp_clog.x.misc.log1,
126 sctp_clog.x.misc.log2,
127 sctp_clog.x.misc.log3,
128 sctp_clog.x.misc.log4);
133 rto_logging(struct sctp_nets *net, int from)
135 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
136 struct sctp_cwnd_log sctp_clog;
138 memset(&sctp_clog, 0, sizeof(sctp_clog));
139 sctp_clog.x.rto.net = (void *) net;
140 sctp_clog.x.rto.rtt = net->rtt / 1000;
141 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
144 sctp_clog.x.misc.log1,
145 sctp_clog.x.misc.log2,
146 sctp_clog.x.misc.log3,
147 sctp_clog.x.misc.log4);
152 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
154 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
155 struct sctp_cwnd_log sctp_clog;
157 sctp_clog.x.strlog.stcb = stcb;
158 sctp_clog.x.strlog.n_tsn = tsn;
159 sctp_clog.x.strlog.n_sseq = sseq;
160 sctp_clog.x.strlog.e_tsn = 0;
161 sctp_clog.x.strlog.e_sseq = 0;
162 sctp_clog.x.strlog.strm = stream;
163 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
166 sctp_clog.x.misc.log1,
167 sctp_clog.x.misc.log2,
168 sctp_clog.x.misc.log3,
169 sctp_clog.x.misc.log4);
174 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
176 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
177 struct sctp_cwnd_log sctp_clog;
179 sctp_clog.x.nagle.stcb = (void *)stcb;
180 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
181 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
182 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
183 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
184 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
185 SCTP_LOG_EVENT_NAGLE,
187 sctp_clog.x.misc.log1,
188 sctp_clog.x.misc.log2,
189 sctp_clog.x.misc.log3,
190 sctp_clog.x.misc.log4);
195 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
197 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
198 struct sctp_cwnd_log sctp_clog;
200 sctp_clog.x.sack.cumack = cumack;
201 sctp_clog.x.sack.oldcumack = old_cumack;
202 sctp_clog.x.sack.tsn = tsn;
203 sctp_clog.x.sack.numGaps = gaps;
204 sctp_clog.x.sack.numDups = dups;
205 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
208 sctp_clog.x.misc.log1,
209 sctp_clog.x.misc.log2,
210 sctp_clog.x.misc.log3,
211 sctp_clog.x.misc.log4);
216 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
218 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
219 struct sctp_cwnd_log sctp_clog;
221 memset(&sctp_clog, 0, sizeof(sctp_clog));
222 sctp_clog.x.map.base = map;
223 sctp_clog.x.map.cum = cum;
224 sctp_clog.x.map.high = high;
225 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
228 sctp_clog.x.misc.log1,
229 sctp_clog.x.misc.log2,
230 sctp_clog.x.misc.log3,
231 sctp_clog.x.misc.log4);
236 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
238 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
239 struct sctp_cwnd_log sctp_clog;
241 memset(&sctp_clog, 0, sizeof(sctp_clog));
242 sctp_clog.x.fr.largest_tsn = biggest_tsn;
243 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
244 sctp_clog.x.fr.tsn = tsn;
245 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
248 sctp_clog.x.misc.log1,
249 sctp_clog.x.misc.log2,
250 sctp_clog.x.misc.log3,
251 sctp_clog.x.misc.log4);
256 sctp_log_mb(struct mbuf *m, int from)
258 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
259 struct sctp_cwnd_log sctp_clog;
261 sctp_clog.x.mb.mp = m;
262 sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m));
263 sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m));
264 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
265 if (SCTP_BUF_IS_EXTENDED(m)) {
266 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
267 #if defined(__APPLE__)
268 /* APPLE does not use a ref_cnt, but a forward/backward ref queue */
270 sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m));
273 sctp_clog.x.mb.ext = 0;
274 sctp_clog.x.mb.refcnt = 0;
276 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
279 sctp_clog.x.misc.log1,
280 sctp_clog.x.misc.log2,
281 sctp_clog.x.misc.log3,
282 sctp_clog.x.misc.log4);
287 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
289 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
290 struct sctp_cwnd_log sctp_clog;
292 if (control == NULL) {
293 SCTP_PRINTF("Gak log of NULL?\n");
296 sctp_clog.x.strlog.stcb = control->stcb;
297 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
298 sctp_clog.x.strlog.n_sseq = control->sinfo_ssn;
299 sctp_clog.x.strlog.strm = control->sinfo_stream;
300 if (poschk != NULL) {
301 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
302 sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn;
304 sctp_clog.x.strlog.e_tsn = 0;
305 sctp_clog.x.strlog.e_sseq = 0;
307 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
310 sctp_clog.x.misc.log1,
311 sctp_clog.x.misc.log2,
312 sctp_clog.x.misc.log3,
313 sctp_clog.x.misc.log4);
318 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
320 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
321 struct sctp_cwnd_log sctp_clog;
323 sctp_clog.x.cwnd.net = net;
324 if (stcb->asoc.send_queue_cnt > 255)
325 sctp_clog.x.cwnd.cnt_in_send = 255;
327 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
328 if (stcb->asoc.stream_queue_cnt > 255)
329 sctp_clog.x.cwnd.cnt_in_str = 255;
331 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
334 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
335 sctp_clog.x.cwnd.inflight = net->flight_size;
336 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
337 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
338 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
340 if (SCTP_CWNDLOG_PRESEND == from) {
341 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
343 sctp_clog.x.cwnd.cwnd_augment = augment;
344 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
347 sctp_clog.x.misc.log1,
348 sctp_clog.x.misc.log2,
349 sctp_clog.x.misc.log3,
350 sctp_clog.x.misc.log4);
356 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
358 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
359 struct sctp_cwnd_log sctp_clog;
361 memset(&sctp_clog, 0, sizeof(sctp_clog));
363 sctp_clog.x.lock.sock = (void *) inp->sctp_socket;
366 sctp_clog.x.lock.sock = (void *) NULL;
368 sctp_clog.x.lock.inp = (void *) inp;
369 #if (defined(__FreeBSD__) && __FreeBSD_version >= 503000) || (defined(__APPLE__))
371 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
373 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
376 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
377 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
379 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
380 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
382 #if (defined(__FreeBSD__) && __FreeBSD_version <= 602000)
383 sctp_clog.x.lock.info_lock = mtx_owned(&SCTP_BASE_INFO(ipi_ep_mtx));
385 sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
387 if (inp && (inp->sctp_socket)) {
388 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
389 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
390 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
392 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
393 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
394 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
397 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
400 sctp_clog.x.misc.log1,
401 sctp_clog.x.misc.log2,
402 sctp_clog.x.misc.log3,
403 sctp_clog.x.misc.log4);
409 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
411 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
412 struct sctp_cwnd_log sctp_clog;
414 memset(&sctp_clog, 0, sizeof(sctp_clog));
415 sctp_clog.x.cwnd.net = net;
416 sctp_clog.x.cwnd.cwnd_new_value = error;
417 sctp_clog.x.cwnd.inflight = net->flight_size;
418 sctp_clog.x.cwnd.cwnd_augment = burst;
419 if (stcb->asoc.send_queue_cnt > 255)
420 sctp_clog.x.cwnd.cnt_in_send = 255;
422 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
423 if (stcb->asoc.stream_queue_cnt > 255)
424 sctp_clog.x.cwnd.cnt_in_str = 255;
426 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
427 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
428 SCTP_LOG_EVENT_MAXBURST,
430 sctp_clog.x.misc.log1,
431 sctp_clog.x.misc.log2,
432 sctp_clog.x.misc.log3,
433 sctp_clog.x.misc.log4);
438 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
440 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
441 struct sctp_cwnd_log sctp_clog;
443 sctp_clog.x.rwnd.rwnd = peers_rwnd;
444 sctp_clog.x.rwnd.send_size = snd_size;
445 sctp_clog.x.rwnd.overhead = overhead;
446 sctp_clog.x.rwnd.new_rwnd = 0;
447 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
450 sctp_clog.x.misc.log1,
451 sctp_clog.x.misc.log2,
452 sctp_clog.x.misc.log3,
453 sctp_clog.x.misc.log4);
458 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
460 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
461 struct sctp_cwnd_log sctp_clog;
463 sctp_clog.x.rwnd.rwnd = peers_rwnd;
464 sctp_clog.x.rwnd.send_size = flight_size;
465 sctp_clog.x.rwnd.overhead = overhead;
466 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
467 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
470 sctp_clog.x.misc.log1,
471 sctp_clog.x.misc.log2,
472 sctp_clog.x.misc.log3,
473 sctp_clog.x.misc.log4);
478 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
480 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
481 struct sctp_cwnd_log sctp_clog;
483 sctp_clog.x.mbcnt.total_queue_size = total_oq;
484 sctp_clog.x.mbcnt.size_change = book;
485 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
486 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
487 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
488 SCTP_LOG_EVENT_MBCNT,
490 sctp_clog.x.misc.log1,
491 sctp_clog.x.misc.log2,
492 sctp_clog.x.misc.log3,
493 sctp_clog.x.misc.log4);
498 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
500 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
501 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
509 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
511 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
512 struct sctp_cwnd_log sctp_clog;
514 sctp_clog.x.wake.stcb = (void *)stcb;
515 sctp_clog.x.wake.wake_cnt = wake_cnt;
516 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
517 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
518 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
520 if (stcb->asoc.stream_queue_cnt < 0xff)
521 sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
523 sctp_clog.x.wake.stream_qcnt = 0xff;
525 if (stcb->asoc.chunks_on_out_queue < 0xff)
526 sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
528 sctp_clog.x.wake.chunks_on_oque = 0xff;
530 sctp_clog.x.wake.sctpflags = 0;
531 /* set in the defered mode stuff */
532 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
533 sctp_clog.x.wake.sctpflags |= 1;
534 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
535 sctp_clog.x.wake.sctpflags |= 2;
536 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
537 sctp_clog.x.wake.sctpflags |= 4;
538 /* what about the sb */
539 if (stcb->sctp_socket) {
540 struct socket *so = stcb->sctp_socket;
542 sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff));
544 sctp_clog.x.wake.sbflags = 0xff;
546 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
549 sctp_clog.x.misc.log1,
550 sctp_clog.x.misc.log2,
551 sctp_clog.x.misc.log3,
552 sctp_clog.x.misc.log4);
557 sctp_log_block(uint8_t from, struct sctp_association *asoc, int sendlen)
559 #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF)
560 struct sctp_cwnd_log sctp_clog;
562 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
563 sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
564 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
565 sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
566 sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
567 sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight/1024);
568 sctp_clog.x.blk.sndlen = sendlen;
569 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
570 SCTP_LOG_EVENT_BLOCK,
572 sctp_clog.x.misc.log1,
573 sctp_clog.x.misc.log2,
574 sctp_clog.x.misc.log3,
575 sctp_clog.x.misc.log4);
580 sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
582 /* May need to fix this if ktrdump does not work */
586 #ifdef SCTP_AUDITING_ENABLED
587 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
588 static int sctp_audit_indx = 0;
592 sctp_print_audit_report(void)
598 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
599 if ((sctp_audit_data[i][0] == 0xe0) &&
600 (sctp_audit_data[i][1] == 0x01)) {
603 } else if (sctp_audit_data[i][0] == 0xf0) {
606 } else if ((sctp_audit_data[i][0] == 0xc0) &&
607 (sctp_audit_data[i][1] == 0x01)) {
611 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
612 (uint32_t) sctp_audit_data[i][1]);
617 for (i = 0; i < sctp_audit_indx; i++) {
618 if ((sctp_audit_data[i][0] == 0xe0) &&
619 (sctp_audit_data[i][1] == 0x01)) {
622 } else if (sctp_audit_data[i][0] == 0xf0) {
625 } else if ((sctp_audit_data[i][0] == 0xc0) &&
626 (sctp_audit_data[i][1] == 0x01)) {
630 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
631 (uint32_t) sctp_audit_data[i][1]);
640 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
641 struct sctp_nets *net)
643 int resend_cnt, tot_out, rep, tot_book_cnt;
644 struct sctp_nets *lnet;
645 struct sctp_tmit_chunk *chk;
647 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
648 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
650 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
654 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
655 sctp_audit_data[sctp_audit_indx][1] = 0x01;
657 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
663 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
664 sctp_audit_data[sctp_audit_indx][1] = 0x02;
666 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
671 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
672 sctp_audit_data[sctp_audit_indx][1] =
673 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
675 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
680 resend_cnt = tot_out = 0;
681 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
682 if (chk->sent == SCTP_DATAGRAM_RESEND) {
684 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
685 tot_out += chk->book_size;
689 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
690 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
691 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
693 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
696 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
697 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
699 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
700 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
701 sctp_audit_data[sctp_audit_indx][1] =
702 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
704 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
708 if (tot_out != stcb->asoc.total_flight) {
709 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
710 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
712 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
716 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
717 (int)stcb->asoc.total_flight);
718 stcb->asoc.total_flight = tot_out;
720 if (tot_book_cnt != stcb->asoc.total_flight_count) {
721 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
722 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
724 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
728 SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
730 stcb->asoc.total_flight_count = tot_book_cnt;
733 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
734 tot_out += lnet->flight_size;
736 if (tot_out != stcb->asoc.total_flight) {
737 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
738 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
740 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
744 SCTP_PRINTF("real flight:%d net total was %d\n",
745 stcb->asoc.total_flight, tot_out);
746 /* now corrective action */
747 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
750 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
751 if ((chk->whoTo == lnet) &&
752 (chk->sent < SCTP_DATAGRAM_RESEND)) {
753 tot_out += chk->book_size;
756 if (lnet->flight_size != tot_out) {
757 SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
758 (void *)lnet, lnet->flight_size,
760 lnet->flight_size = tot_out;
765 sctp_print_audit_report();
770 sctp_audit_log(uint8_t ev, uint8_t fd)
773 sctp_audit_data[sctp_audit_indx][0] = ev;
774 sctp_audit_data[sctp_audit_indx][1] = fd;
776 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
784 * sctp_stop_timers_for_shutdown() should be called
785 * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
786 * state to make sure that all timers are stopped.
789 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
791 struct sctp_association *asoc;
792 struct sctp_nets *net;
796 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
797 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
798 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
799 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
800 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
801 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
802 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
803 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
808 * a list of sizes based on typical mtu's, used only if next hop size not
811 static uint32_t sctp_mtu_sizes[] = {
833 * Return the largest MTU smaller than val. If there is no
834 * entry, just return val.
837 sctp_get_prev_mtu(uint32_t val)
841 if (val <= sctp_mtu_sizes[0]) {
844 for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
845 if (val <= sctp_mtu_sizes[i]) {
849 return (sctp_mtu_sizes[i - 1]);
853 * Return the smallest MTU larger than val. If there is no
854 * entry, just return val.
857 sctp_get_next_mtu(uint32_t val)
859 /* select another MTU that is just bigger than this one */
862 for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
863 if (val < sctp_mtu_sizes[i]) {
864 return (sctp_mtu_sizes[i]);
871 sctp_fill_random_store(struct sctp_pcb *m)
874 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
875 * our counter. The result becomes our good random numbers and we
876 * then setup to give these out. Note that we do no locking to
877 * protect this. This is ok, since if competing folks call this we
878 * will get more gobbled gook in the random store which is what we
879 * want. There is a danger that two guys will use the same random
880 * numbers, but thats ok too since that is random as well :->
883 (void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
884 sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
885 sizeof(m->random_counter), (uint8_t *)m->random_store);
890 sctp_select_initial_TSN(struct sctp_pcb *inp)
893 * A true implementation should use random selection process to get
894 * the initial stream sequence number, using RFC1750 as a good
899 int store_at, new_store;
901 if (inp->initial_sequence_debug != 0) {
904 ret = inp->initial_sequence_debug;
905 inp->initial_sequence_debug++;
909 store_at = inp->store_at;
910 new_store = store_at + sizeof(uint32_t);
911 if (new_store >= (SCTP_SIGNATURE_SIZE-3)) {
914 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
917 if (new_store == 0) {
918 /* Refill the random store */
919 sctp_fill_random_store(inp);
921 p = &inp->random_store[store_at];
928 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
934 (void)SCTP_GETTIME_TIMEVAL(&now);
937 x = sctp_select_initial_TSN(&inp->sctp_ep);
942 if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
950 sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
951 uint32_t override_tag, uint32_t vrf_id)
953 struct sctp_association *asoc;
955 * Anything set to zero is taken care of by the allocation routine's
960 * Up front select what scoping to apply on addresses I tell my peer
961 * Not sure what to do with these right now, we will need to come up
962 * with a way to set them. We may need to pass them through from the
963 * caller in the sctp_aloc_assoc() function.
968 /* init all variables to a known value. */
969 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
970 asoc->max_burst = inp->sctp_ep.max_burst;
971 asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
972 asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
973 asoc->cookie_life = inp->sctp_ep.def_cookie_life;
974 asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
975 asoc->ecn_allowed = inp->sctp_ecn_enable;
976 asoc->sctp_nr_sack_on_off = (uint8_t)SCTP_BASE_SYSCTL(sctp_nr_sack_on_off);
977 asoc->sctp_cmt_pf = (uint8_t)0;
978 asoc->sctp_frag_point = inp->sctp_frag_point;
979 asoc->sctp_features = inp->sctp_features;
980 asoc->default_dscp = inp->sctp_ep.default_dscp;
982 if (inp->sctp_ep.default_flowlabel) {
983 asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
985 if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
986 asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
987 asoc->default_flowlabel &= 0x000fffff;
988 asoc->default_flowlabel |= 0x80000000;
990 asoc->default_flowlabel = 0;
994 asoc->sb_send_resv = 0;
996 asoc->my_vtag = override_tag;
998 asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1000 /* Get the nonce tags */
1001 asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1002 asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1003 asoc->vrf_id = vrf_id;
1005 #ifdef SCTP_ASOCLOG_OF_TSNS
1006 asoc->tsn_in_at = 0;
1007 asoc->tsn_out_at = 0;
1008 asoc->tsn_in_wrapped = 0;
1009 asoc->tsn_out_wrapped = 0;
1010 asoc->cumack_log_at = 0;
1011 asoc->cumack_log_atsnt = 0;
1013 #ifdef SCTP_FS_SPEC_LOG
1017 asoc->assoc_up_sent = 0;
1018 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1019 sctp_select_initial_TSN(&inp->sctp_ep);
1020 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1021 /* we are optimisitic here */
1022 asoc->peer_supports_pktdrop = 1;
1023 asoc->peer_supports_nat = 0;
1024 asoc->sent_queue_retran_cnt = 0;
1027 asoc->last_net_cmt_send_started = NULL;
1029 /* This will need to be adjusted */
1030 asoc->last_acked_seq = asoc->init_seq_number - 1;
1031 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1032 asoc->asconf_seq_in = asoc->last_acked_seq;
1034 /* here we are different, we hold the next one we expect */
1035 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1037 asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1038 asoc->initial_rto = inp->sctp_ep.initial_rto;
1040 asoc->max_init_times = inp->sctp_ep.max_init_times;
1041 asoc->max_send_times = inp->sctp_ep.max_send_times;
1042 asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1043 asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1044 asoc->free_chunk_cnt = 0;
1046 asoc->iam_blocking = 0;
1047 asoc->context = inp->sctp_context;
1048 asoc->local_strreset_support = inp->local_strreset_support;
1049 asoc->def_send = inp->def_send;
1050 asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1051 asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1052 asoc->pr_sctp_cnt = 0;
1053 asoc->total_output_queue_size = 0;
1055 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1056 asoc->scope.ipv6_addr_legal = 1;
1057 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1058 asoc->scope.ipv4_addr_legal = 1;
1060 asoc->scope.ipv4_addr_legal = 0;
1062 #if defined(__Userspace__)
1063 asoc->scope.conn_addr_legal = 0;
1066 asoc->scope.ipv6_addr_legal = 0;
1067 #if defined(__Userspace__)
1068 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
1069 asoc->scope.conn_addr_legal = 1;
1070 asoc->scope.ipv4_addr_legal = 0;
1072 asoc->scope.conn_addr_legal = 0;
1073 asoc->scope.ipv4_addr_legal = 1;
1076 asoc->scope.ipv4_addr_legal = 1;
1080 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1081 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1083 asoc->smallest_mtu = inp->sctp_frag_point;
1084 asoc->minrto = inp->sctp_ep.sctp_minrto;
1085 asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1087 asoc->locked_on_sending = NULL;
1088 asoc->stream_locked_on = 0;
1089 asoc->ecn_echo_cnt_onq = 0;
1090 asoc->stream_locked = 0;
1092 asoc->send_sack = 1;
1094 LIST_INIT(&asoc->sctp_restricted_addrs);
1096 TAILQ_INIT(&asoc->nets);
1097 TAILQ_INIT(&asoc->pending_reply_queue);
1098 TAILQ_INIT(&asoc->asconf_ack_sent);
1099 /* Setup to fill the hb random cache at first HB */
1100 asoc->hb_random_idx = 4;
1102 asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1104 stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1105 stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1107 stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1108 stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1111 * Now the stream parameters, here we allocate space for all streams
1112 * that we request by default.
1114 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1115 inp->sctp_ep.pre_open_stream_count;
1116 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1117 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1119 if (asoc->strmout == NULL) {
1120 /* big trouble no memory */
1121 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1124 for (i = 0; i < asoc->streamoutcnt; i++) {
1126 * inbound side must be set to 0xffff, also NOTE when we get
1127 * the INIT-ACK back (for INIT sender) we MUST reduce the
1128 * count (streamoutcnt) but first check if we sent to any of
1129 * the upper streams that were dropped (if some were). Those
1130 * that were dropped must be notified to the upper layer as
1133 asoc->strmout[i].next_sequence_send = 0x0;
1134 TAILQ_INIT(&asoc->strmout[i].outqueue);
1135 asoc->strmout[i].chunks_on_queues = 0;
1136 asoc->strmout[i].stream_no = i;
1137 asoc->strmout[i].last_msg_incomplete = 0;
1138 asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL);
1140 asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1142 /* Now the mapping array */
1143 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1144 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1146 if (asoc->mapping_array == NULL) {
1147 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1148 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1151 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1152 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1154 if (asoc->nr_mapping_array == NULL) {
1155 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1156 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1157 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1160 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1162 /* Now the init of the other outqueues */
1163 TAILQ_INIT(&asoc->free_chunks);
1164 TAILQ_INIT(&asoc->control_send_queue);
1165 TAILQ_INIT(&asoc->asconf_send_queue);
1166 TAILQ_INIT(&asoc->send_queue);
1167 TAILQ_INIT(&asoc->sent_queue);
1168 TAILQ_INIT(&asoc->reasmqueue);
1169 TAILQ_INIT(&asoc->resetHead);
1170 asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1171 TAILQ_INIT(&asoc->asconf_queue);
1172 /* authentication fields */
1173 asoc->authinfo.random = NULL;
1174 asoc->authinfo.active_keyid = 0;
1175 asoc->authinfo.assoc_key = NULL;
1176 asoc->authinfo.assoc_keyid = 0;
1177 asoc->authinfo.recv_key = NULL;
1178 asoc->authinfo.recv_keyid = 0;
1179 LIST_INIT(&asoc->shared_keys);
1180 asoc->marked_retrans = 0;
1181 asoc->port = inp->sctp_ep.port;
1185 asoc->timoshutdown = 0;
1186 asoc->timoheartbeat = 0;
1187 asoc->timocookie = 0;
1188 asoc->timoshutdownack = 0;
1189 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1190 asoc->discontinuity_time = asoc->start_time;
1191 /* sa_ignore MEMLEAK {memory is put in the assoc mapping array and freed later when
1192 * the association is freed.
1198 sctp_print_mapping_array(struct sctp_association *asoc)
1200 unsigned int i, limit;
1202 SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1203 asoc->mapping_array_size,
1204 asoc->mapping_array_base_tsn,
1205 asoc->cumulative_tsn,
1206 asoc->highest_tsn_inside_map,
1207 asoc->highest_tsn_inside_nr_map);
1208 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1209 if (asoc->mapping_array[limit - 1] != 0) {
1213 SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1214 for (i = 0; i < limit; i++) {
1215 SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1219 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1220 if (asoc->nr_mapping_array[limit - 1]) {
1224 SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1225 for (i = 0; i < limit; i++) {
1226 SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ': '\n');
1233 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1235 /* mapping array needs to grow */
1236 uint8_t *new_array1, *new_array2;
1239 new_size = asoc->mapping_array_size + ((needed+7)/8 + SCTP_MAPPING_ARRAY_INCR);
1240 SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1241 SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1242 if ((new_array1 == NULL) || (new_array2 == NULL)) {
1243 /* can't get more, forget it */
1244 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1246 SCTP_FREE(new_array1, SCTP_M_MAP);
1249 SCTP_FREE(new_array2, SCTP_M_MAP);
1253 memset(new_array1, 0, new_size);
1254 memset(new_array2, 0, new_size);
1255 memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1256 memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1257 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1258 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1259 asoc->mapping_array = new_array1;
1260 asoc->nr_mapping_array = new_array2;
1261 asoc->mapping_array_size = new_size;
1267 sctp_iterator_work(struct sctp_iterator *it)
1269 int iteration_count = 0;
1272 struct sctp_inpcb *tinp;
1274 SCTP_INP_INFO_RLOCK();
1275 SCTP_ITERATOR_LOCK();
1277 SCTP_INP_RLOCK(it->inp);
1278 SCTP_INP_DECR_REF(it->inp);
1280 if (it->inp == NULL) {
1281 /* iterator is complete */
1283 SCTP_ITERATOR_UNLOCK();
1284 SCTP_INP_INFO_RUNLOCK();
1285 if (it->function_atend != NULL) {
1286 (*it->function_atend) (it->pointer, it->val);
1288 SCTP_FREE(it, SCTP_M_ITER);
1295 SCTP_INP_RLOCK(it->inp);
1297 while (((it->pcb_flags) &&
1298 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1299 ((it->pcb_features) &&
1300 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1301 /* endpoint flags or features don't match, so keep looking */
1302 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1303 SCTP_INP_RUNLOCK(it->inp);
1304 goto done_with_iterator;
1307 it->inp = LIST_NEXT(it->inp, sctp_list);
1308 SCTP_INP_RUNLOCK(tinp);
1309 if (it->inp == NULL) {
1310 goto done_with_iterator;
1312 SCTP_INP_RLOCK(it->inp);
1314 /* now go through each assoc which is in the desired state */
1315 if (it->done_current_ep == 0) {
1316 if (it->function_inp != NULL)
1317 inp_skip = (*it->function_inp)(it->inp, it->pointer, it->val);
1318 it->done_current_ep = 1;
1320 if (it->stcb == NULL) {
1321 /* run the per instance function */
1322 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1324 if ((inp_skip) || it->stcb == NULL) {
1325 if (it->function_inp_end != NULL) {
1326 inp_skip = (*it->function_inp_end)(it->inp,
1330 SCTP_INP_RUNLOCK(it->inp);
1334 SCTP_TCB_LOCK(it->stcb);
1335 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1336 /* not in the right state... keep looking */
1337 SCTP_TCB_UNLOCK(it->stcb);
1340 /* see if we have limited out the iterator loop */
1342 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1343 /* Pause to let others grab the lock */
1344 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1345 SCTP_TCB_UNLOCK(it->stcb);
1346 SCTP_INP_INCR_REF(it->inp);
1347 SCTP_INP_RUNLOCK(it->inp);
1348 SCTP_ITERATOR_UNLOCK();
1349 SCTP_INP_INFO_RUNLOCK();
1350 SCTP_INP_INFO_RLOCK();
1351 SCTP_ITERATOR_LOCK();
1352 if (sctp_it_ctl.iterator_flags) {
1353 /* We won't be staying here */
1354 SCTP_INP_DECR_REF(it->inp);
1355 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1356 #if !defined(__FreeBSD__)
1357 if (sctp_it_ctl.iterator_flags &
1358 SCTP_ITERATOR_MUST_EXIT) {
1359 goto done_with_iterator;
1362 if (sctp_it_ctl.iterator_flags &
1363 SCTP_ITERATOR_STOP_CUR_IT) {
1364 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1365 goto done_with_iterator;
1367 if (sctp_it_ctl.iterator_flags &
1368 SCTP_ITERATOR_STOP_CUR_INP) {
1369 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1372 /* If we reach here huh? */
1373 SCTP_PRINTF("Unknown it ctl flag %x\n",
1374 sctp_it_ctl.iterator_flags);
1375 sctp_it_ctl.iterator_flags = 0;
1377 SCTP_INP_RLOCK(it->inp);
1378 SCTP_INP_DECR_REF(it->inp);
1379 SCTP_TCB_LOCK(it->stcb);
1380 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1381 iteration_count = 0;
1384 /* run function on this one */
1385 (*it->function_assoc)(it->inp, it->stcb, it->pointer, it->val);
1388 * we lie here, it really needs to have its own type but
1389 * first I must verify that this won't effect things :-0
1391 if (it->no_chunk_output == 0)
1392 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1394 SCTP_TCB_UNLOCK(it->stcb);
1396 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1397 if (it->stcb == NULL) {
1398 /* Run last function */
1399 if (it->function_inp_end != NULL) {
1400 inp_skip = (*it->function_inp_end)(it->inp,
1406 SCTP_INP_RUNLOCK(it->inp);
1408 /* done with all assocs on this endpoint, move on to next endpoint */
1409 it->done_current_ep = 0;
1410 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1413 it->inp = LIST_NEXT(it->inp, sctp_list);
1415 if (it->inp == NULL) {
1416 goto done_with_iterator;
1418 goto select_a_new_ep;
1422 sctp_iterator_worker(void)
1424 struct sctp_iterator *it, *nit;
1426 /* This function is called with the WQ lock in place */
1428 sctp_it_ctl.iterator_running = 1;
1429 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
1430 sctp_it_ctl.cur_it = it;
1431 /* now lets work on this one */
1432 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1433 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1434 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1435 CURVNET_SET(it->vn);
1437 sctp_iterator_work(it);
1438 sctp_it_ctl.cur_it = NULL;
1439 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1442 SCTP_IPI_ITERATOR_WQ_LOCK();
1443 #if !defined(__FreeBSD__)
1444 if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
1448 /*sa_ignore FREED_MEMORY*/
1450 sctp_it_ctl.iterator_running = 0;
1456 sctp_handle_addr_wq(void)
1458 /* deal with the ADDR wq from the rtsock calls */
1459 struct sctp_laddr *wi, *nwi;
1460 struct sctp_asconf_iterator *asc;
1462 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1463 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1465 /* Try later, no memory */
1466 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1467 (struct sctp_inpcb *)NULL,
1468 (struct sctp_tcb *)NULL,
1469 (struct sctp_nets *)NULL);
1472 LIST_INIT(&asc->list_of_work);
1475 SCTP_WQ_ADDR_LOCK();
1476 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
1477 LIST_REMOVE(wi, sctp_nxt_addr);
1478 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1481 SCTP_WQ_ADDR_UNLOCK();
1483 if (asc->cnt == 0) {
1484 SCTP_FREE(asc, SCTP_M_ASC_IT);
1486 (void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
1487 sctp_asconf_iterator_stcb,
1488 NULL, /* No ep end for boundall */
1489 SCTP_PCB_FLAGS_BOUNDALL,
1490 SCTP_PCB_ANY_FEATURES,
1491 SCTP_ASOC_ANY_STATE,
1493 sctp_asconf_iterator_end, NULL, 0);
1498 sctp_timeout_handler(void *t)
1500 struct sctp_inpcb *inp;
1501 struct sctp_tcb *stcb;
1502 struct sctp_nets *net;
1503 struct sctp_timer *tmr;
1504 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1507 int did_output, type;
1509 tmr = (struct sctp_timer *)t;
1510 inp = (struct sctp_inpcb *)tmr->ep;
1511 stcb = (struct sctp_tcb *)tmr->tcb;
1512 net = (struct sctp_nets *)tmr->net;
1513 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1514 CURVNET_SET((struct vnet *)tmr->vnet);
1518 #ifdef SCTP_AUDITING_ENABLED
1519 sctp_audit_log(0xF0, (uint8_t) tmr->type);
1520 sctp_auditing(3, inp, stcb, net);
1523 /* sanity checks... */
1524 if (tmr->self != (void *)tmr) {
1526 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1529 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1534 tmr->stopped_from = 0xa001;
1535 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1537 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1540 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1545 tmr->stopped_from = 0xa002;
1546 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1547 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1552 /* if this is an iterator timeout, get the struct and clear inp */
1553 tmr->stopped_from = 0xa003;
1556 SCTP_INP_INCR_REF(inp);
1557 if ((inp->sctp_socket == NULL) &&
1558 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1559 (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1560 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1561 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1562 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1563 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1564 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1565 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1566 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1568 SCTP_INP_DECR_REF(inp);
1569 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1575 tmr->stopped_from = 0xa004;
1577 atomic_add_int(&stcb->asoc.refcnt, 1);
1578 if (stcb->asoc.state == 0) {
1579 atomic_add_int(&stcb->asoc.refcnt, -1);
1581 SCTP_INP_DECR_REF(inp);
1583 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1589 tmr->stopped_from = 0xa005;
1590 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1591 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1593 SCTP_INP_DECR_REF(inp);
1596 atomic_add_int(&stcb->asoc.refcnt, -1);
1598 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1603 tmr->stopped_from = 0xa006;
1606 SCTP_TCB_LOCK(stcb);
1607 atomic_add_int(&stcb->asoc.refcnt, -1);
1608 if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1609 ((stcb->asoc.state == 0) ||
1610 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1611 SCTP_TCB_UNLOCK(stcb);
1613 SCTP_INP_DECR_REF(inp);
1615 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1621 /* record in stopped what t-o occured */
1622 tmr->stopped_from = tmr->type;
1624 /* mark as being serviced now */
1625 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1627 * Callout has been rescheduled.
1631 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1633 * Not active, so no action.
1637 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1639 /* call the handler for the appropriate timer type */
1640 switch (tmr->type) {
1641 case SCTP_TIMER_TYPE_ZERO_COPY:
1645 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1646 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1649 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1653 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1654 SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1657 case SCTP_TIMER_TYPE_ADDR_WQ:
1658 sctp_handle_addr_wq();
1660 case SCTP_TIMER_TYPE_SEND:
1661 if ((stcb == NULL) || (inp == NULL)) {
1664 SCTP_STAT_INCR(sctps_timodata);
1665 stcb->asoc.timodata++;
1666 stcb->asoc.num_send_timers_up--;
1667 if (stcb->asoc.num_send_timers_up < 0) {
1668 stcb->asoc.num_send_timers_up = 0;
1670 SCTP_TCB_LOCK_ASSERT(stcb);
1671 if (sctp_t3rxt_timer(inp, stcb, net)) {
1672 /* no need to unlock on tcb its gone */
1676 SCTP_TCB_LOCK_ASSERT(stcb);
1677 #ifdef SCTP_AUDITING_ENABLED
1678 sctp_auditing(4, inp, stcb, net);
1680 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1681 if ((stcb->asoc.num_send_timers_up == 0) &&
1682 (stcb->asoc.sent_queue_cnt > 0)) {
1683 struct sctp_tmit_chunk *chk;
1686 * safeguard. If there on some on the sent queue
1687 * somewhere but no timers running something is
1688 * wrong... so we start a timer on the first chunk
1689 * on the send queue on whatever net it is sent to.
1691 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1692 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1696 case SCTP_TIMER_TYPE_INIT:
1697 if ((stcb == NULL) || (inp == NULL)) {
1700 SCTP_STAT_INCR(sctps_timoinit);
1701 stcb->asoc.timoinit++;
1702 if (sctp_t1init_timer(inp, stcb, net)) {
1703 /* no need to unlock on tcb its gone */
1706 /* We do output but not here */
1709 case SCTP_TIMER_TYPE_RECV:
1710 if ((stcb == NULL) || (inp == NULL)) {
1713 SCTP_STAT_INCR(sctps_timosack);
1714 stcb->asoc.timosack++;
1715 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1716 #ifdef SCTP_AUDITING_ENABLED
1717 sctp_auditing(4, inp, stcb, net);
1719 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1721 case SCTP_TIMER_TYPE_SHUTDOWN:
1722 if ((stcb == NULL) || (inp == NULL)) {
1725 if (sctp_shutdown_timer(inp, stcb, net)) {
1726 /* no need to unlock on tcb its gone */
1729 SCTP_STAT_INCR(sctps_timoshutdown);
1730 stcb->asoc.timoshutdown++;
1731 #ifdef SCTP_AUDITING_ENABLED
1732 sctp_auditing(4, inp, stcb, net);
1734 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1736 case SCTP_TIMER_TYPE_HEARTBEAT:
1737 if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1740 SCTP_STAT_INCR(sctps_timoheartbeat);
1741 stcb->asoc.timoheartbeat++;
1742 if (sctp_heartbeat_timer(inp, stcb, net)) {
1743 /* no need to unlock on tcb its gone */
1746 #ifdef SCTP_AUDITING_ENABLED
1747 sctp_auditing(4, inp, stcb, net);
1749 if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1750 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1751 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1754 case SCTP_TIMER_TYPE_COOKIE:
1755 if ((stcb == NULL) || (inp == NULL)) {
1759 if (sctp_cookie_timer(inp, stcb, net)) {
1760 /* no need to unlock on tcb its gone */
1763 SCTP_STAT_INCR(sctps_timocookie);
1764 stcb->asoc.timocookie++;
1765 #ifdef SCTP_AUDITING_ENABLED
1766 sctp_auditing(4, inp, stcb, net);
1769 * We consider T3 and Cookie timer pretty much the same with
1770 * respect to where from in chunk_output.
1772 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1774 case SCTP_TIMER_TYPE_NEWCOOKIE:
1781 SCTP_STAT_INCR(sctps_timosecret);
1782 (void)SCTP_GETTIME_TIMEVAL(&tv);
1783 SCTP_INP_WLOCK(inp);
1784 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1785 inp->sctp_ep.last_secret_number =
1786 inp->sctp_ep.current_secret_number;
1787 inp->sctp_ep.current_secret_number++;
1788 if (inp->sctp_ep.current_secret_number >=
1789 SCTP_HOW_MANY_SECRETS) {
1790 inp->sctp_ep.current_secret_number = 0;
1792 secret = (int)inp->sctp_ep.current_secret_number;
1793 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1794 inp->sctp_ep.secret_key[secret][i] =
1795 sctp_select_initial_TSN(&inp->sctp_ep);
1797 SCTP_INP_WUNLOCK(inp);
1798 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1802 case SCTP_TIMER_TYPE_PATHMTURAISE:
1803 if ((stcb == NULL) || (inp == NULL)) {
1806 SCTP_STAT_INCR(sctps_timopathmtu);
1807 sctp_pathmtu_timer(inp, stcb, net);
1810 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1811 if ((stcb == NULL) || (inp == NULL)) {
1814 if (sctp_shutdownack_timer(inp, stcb, net)) {
1815 /* no need to unlock on tcb its gone */
1818 SCTP_STAT_INCR(sctps_timoshutdownack);
1819 stcb->asoc.timoshutdownack++;
1820 #ifdef SCTP_AUDITING_ENABLED
1821 sctp_auditing(4, inp, stcb, net);
1823 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1825 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1826 if ((stcb == NULL) || (inp == NULL)) {
1829 SCTP_STAT_INCR(sctps_timoshutdownguard);
1830 sctp_abort_an_association(inp, stcb, NULL, SCTP_SO_NOT_LOCKED);
1831 /* no need to unlock on tcb its gone */
1834 case SCTP_TIMER_TYPE_STRRESET:
1835 if ((stcb == NULL) || (inp == NULL)) {
1838 if (sctp_strreset_timer(inp, stcb, net)) {
1839 /* no need to unlock on tcb its gone */
1842 SCTP_STAT_INCR(sctps_timostrmrst);
1843 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1845 case SCTP_TIMER_TYPE_ASCONF:
1846 if ((stcb == NULL) || (inp == NULL)) {
1849 if (sctp_asconf_timer(inp, stcb, net)) {
1850 /* no need to unlock on tcb its gone */
1853 SCTP_STAT_INCR(sctps_timoasconf);
1854 #ifdef SCTP_AUDITING_ENABLED
1855 sctp_auditing(4, inp, stcb, net);
1857 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1859 case SCTP_TIMER_TYPE_PRIM_DELETED:
1860 if ((stcb == NULL) || (inp == NULL)) {
1863 sctp_delete_prim_timer(inp, stcb, net);
1864 SCTP_STAT_INCR(sctps_timodelprim);
1867 case SCTP_TIMER_TYPE_AUTOCLOSE:
1868 if ((stcb == NULL) || (inp == NULL)) {
1871 SCTP_STAT_INCR(sctps_timoautoclose);
1872 sctp_autoclose_timer(inp, stcb, net);
1873 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1876 case SCTP_TIMER_TYPE_ASOCKILL:
1877 if ((stcb == NULL) || (inp == NULL)) {
1880 SCTP_STAT_INCR(sctps_timoassockill);
1881 /* Can we free it yet? */
1882 SCTP_INP_DECR_REF(inp);
1883 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL+SCTP_LOC_1);
1884 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1885 so = SCTP_INP_SO(inp);
1886 atomic_add_int(&stcb->asoc.refcnt, 1);
1887 SCTP_TCB_UNLOCK(stcb);
1888 SCTP_SOCKET_LOCK(so, 1);
1889 SCTP_TCB_LOCK(stcb);
1890 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1892 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL+SCTP_LOC_2);
1893 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1894 SCTP_SOCKET_UNLOCK(so, 1);
1897 * free asoc, always unlocks (or destroy's) so prevent
1898 * duplicate unlock or unlock of a free mtx :-0
1902 case SCTP_TIMER_TYPE_INPKILL:
1903 SCTP_STAT_INCR(sctps_timoinpkill);
1908 * special case, take away our increment since WE are the
1911 SCTP_INP_DECR_REF(inp);
1912 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL+SCTP_LOC_3);
1913 #if defined(__APPLE__)
1914 SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1);
1916 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1917 SCTP_CALLED_FROM_INPKILL_TIMER);
1918 #if defined(__APPLE__)
1919 SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1);
1924 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1928 #ifdef SCTP_AUDITING_ENABLED
1929 sctp_audit_log(0xF1, (uint8_t) tmr->type);
1931 sctp_auditing(5, inp, stcb, net);
1933 if ((did_output) && stcb) {
1935 * Now we need to clean up the control chunk chain if an
1936 * ECNE is on it. It must be marked as UNSENT again so next
1937 * call will continue to send it until such time that we get
1938 * a CWR, to remove it. It is, however, less likely that we
1939 * will find a ecn echo on the chain though.
1941 sctp_fix_ecn_echo(&stcb->asoc);
1945 SCTP_TCB_UNLOCK(stcb);
1950 SCTP_INP_DECR_REF(inp);
1954 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1956 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
1962 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1963 struct sctp_nets *net)
1966 struct sctp_timer *tmr;
1968 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1973 SCTP_TCB_LOCK_ASSERT(stcb);
1976 case SCTP_TIMER_TYPE_ZERO_COPY:
1977 tmr = &inp->sctp_ep.zero_copy_timer;
1978 to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1980 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1981 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1982 to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1984 case SCTP_TIMER_TYPE_ADDR_WQ:
1985 /* Only 1 tick away :-) */
1986 tmr = &SCTP_BASE_INFO(addr_wq_timer);
1987 to_ticks = SCTP_ADDRESS_TICK_DELAY;
1989 case SCTP_TIMER_TYPE_SEND:
1990 /* Here we use the RTO timer */
1994 if ((stcb == NULL) || (net == NULL)) {
1997 tmr = &net->rxt_timer;
1998 if (net->RTO == 0) {
1999 rto_val = stcb->asoc.initial_rto;
2003 to_ticks = MSEC_TO_TICKS(rto_val);
2006 case SCTP_TIMER_TYPE_INIT:
2008 * Here we use the INIT timer default usually about 1
2011 if ((stcb == NULL) || (net == NULL)) {
2014 tmr = &net->rxt_timer;
2015 if (net->RTO == 0) {
2016 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2018 to_ticks = MSEC_TO_TICKS(net->RTO);
2021 case SCTP_TIMER_TYPE_RECV:
2023 * Here we use the Delayed-Ack timer value from the inp
2024 * ususually about 200ms.
2029 tmr = &stcb->asoc.dack_timer;
2030 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
2032 case SCTP_TIMER_TYPE_SHUTDOWN:
2033 /* Here we use the RTO of the destination. */
2034 if ((stcb == NULL) || (net == NULL)) {
2037 if (net->RTO == 0) {
2038 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2040 to_ticks = MSEC_TO_TICKS(net->RTO);
2042 tmr = &net->rxt_timer;
2044 case SCTP_TIMER_TYPE_HEARTBEAT:
2046 * the net is used here so that we can add in the RTO. Even
2047 * though we use a different timer. We also add the HB timer
2048 * PLUS a random jitter.
2050 if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2056 if ((net->dest_state & SCTP_ADDR_NOHB) &&
2057 !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2060 if (net->RTO == 0) {
2061 to_ticks = stcb->asoc.initial_rto;
2063 to_ticks = net->RTO;
2065 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2066 jitter = rndval % to_ticks;
2067 if (jitter >= (to_ticks >> 1)) {
2068 to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2070 to_ticks = to_ticks - jitter;
2072 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2073 !(net->dest_state & SCTP_ADDR_PF)) {
2074 to_ticks += net->heart_beat_delay;
2077 * Now we must convert the to_ticks that are now in
2080 to_ticks = MSEC_TO_TICKS(to_ticks);
2081 tmr = &net->hb_timer;
2084 case SCTP_TIMER_TYPE_COOKIE:
2086 * Here we can use the RTO timer from the network since one
2087 * RTT was compelete. If a retran happened then we will be
2088 * using the RTO initial value.
2090 if ((stcb == NULL) || (net == NULL)) {
2093 if (net->RTO == 0) {
2094 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2096 to_ticks = MSEC_TO_TICKS(net->RTO);
2098 tmr = &net->rxt_timer;
2100 case SCTP_TIMER_TYPE_NEWCOOKIE:
2102 * nothing needed but the endpoint here ususually about 60
2108 tmr = &inp->sctp_ep.signature_change;
2109 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2111 case SCTP_TIMER_TYPE_ASOCKILL:
2115 tmr = &stcb->asoc.strreset_timer;
2116 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2118 case SCTP_TIMER_TYPE_INPKILL:
2120 * The inp is setup to die. We re-use the signature_chage
2121 * timer since that has stopped and we are in the GONE
2127 tmr = &inp->sctp_ep.signature_change;
2128 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2130 case SCTP_TIMER_TYPE_PATHMTURAISE:
2132 * Here we use the value found in the EP for PMTU ususually
2135 if ((stcb == NULL) || (inp == NULL)) {
2141 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2144 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2145 tmr = &net->pmtu_timer;
2147 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2148 /* Here we use the RTO of the destination */
2149 if ((stcb == NULL) || (net == NULL)) {
2152 if (net->RTO == 0) {
2153 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2155 to_ticks = MSEC_TO_TICKS(net->RTO);
2157 tmr = &net->rxt_timer;
2159 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2161 * Here we use the endpoints shutdown guard timer usually
2164 if ((inp == NULL) || (stcb == NULL)) {
2167 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2168 tmr = &stcb->asoc.shut_guard_timer;
2170 case SCTP_TIMER_TYPE_STRRESET:
2172 * Here the timer comes from the stcb but its value is from
2175 if ((stcb == NULL) || (net == NULL)) {
2178 if (net->RTO == 0) {
2179 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2181 to_ticks = MSEC_TO_TICKS(net->RTO);
2183 tmr = &stcb->asoc.strreset_timer;
2185 case SCTP_TIMER_TYPE_ASCONF:
2187 * Here the timer comes from the stcb but its value is from
2190 if ((stcb == NULL) || (net == NULL)) {
2193 if (net->RTO == 0) {
2194 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2196 to_ticks = MSEC_TO_TICKS(net->RTO);
2198 tmr = &stcb->asoc.asconf_timer;
2200 case SCTP_TIMER_TYPE_PRIM_DELETED:
2201 if ((stcb == NULL) || (net != NULL)) {
2204 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2205 tmr = &stcb->asoc.delete_prim_timer;
2207 case SCTP_TIMER_TYPE_AUTOCLOSE:
2211 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2213 * Really an error since stcb is NOT set to
2218 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2219 tmr = &stcb->asoc.autoclose_timer;
2222 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2223 __FUNCTION__, t_type);
2227 if ((to_ticks <= 0) || (tmr == NULL)) {
2228 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2229 __FUNCTION__, t_type, to_ticks, (void *)tmr);
2232 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2234 * we do NOT allow you to have it already running. if it is
2235 * we leave the current one up unchanged
2239 /* At this point we can proceed */
2240 if (t_type == SCTP_TIMER_TYPE_SEND) {
2241 stcb->asoc.num_send_timers_up++;
2243 tmr->stopped_from = 0;
2245 tmr->ep = (void *)inp;
2246 tmr->tcb = (void *)stcb;
2247 tmr->net = (void *)net;
2248 tmr->self = (void *)tmr;
2249 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
2250 tmr->vnet = (void *)curvnet;
2253 tmr->ticks = sctp_get_tick_count();
2255 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2260 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2261 struct sctp_nets *net, uint32_t from)
2263 struct sctp_timer *tmr;
2265 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2271 SCTP_TCB_LOCK_ASSERT(stcb);
2274 case SCTP_TIMER_TYPE_ZERO_COPY:
2275 tmr = &inp->sctp_ep.zero_copy_timer;
2277 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2278 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2280 case SCTP_TIMER_TYPE_ADDR_WQ:
2281 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2283 case SCTP_TIMER_TYPE_SEND:
2284 if ((stcb == NULL) || (net == NULL)) {
2287 tmr = &net->rxt_timer;
2289 case SCTP_TIMER_TYPE_INIT:
2290 if ((stcb == NULL) || (net == NULL)) {
2293 tmr = &net->rxt_timer;
2295 case SCTP_TIMER_TYPE_RECV:
2299 tmr = &stcb->asoc.dack_timer;
2301 case SCTP_TIMER_TYPE_SHUTDOWN:
2302 if ((stcb == NULL) || (net == NULL)) {
2305 tmr = &net->rxt_timer;
2307 case SCTP_TIMER_TYPE_HEARTBEAT:
2308 if ((stcb == NULL) || (net == NULL)) {
2311 tmr = &net->hb_timer;
2313 case SCTP_TIMER_TYPE_COOKIE:
2314 if ((stcb == NULL) || (net == NULL)) {
2317 tmr = &net->rxt_timer;
2319 case SCTP_TIMER_TYPE_NEWCOOKIE:
2320 /* nothing needed but the endpoint here */
2321 tmr = &inp->sctp_ep.signature_change;
2323 * We re-use the newcookie timer for the INP kill timer. We
2324 * must assure that we do not kill it by accident.
2327 case SCTP_TIMER_TYPE_ASOCKILL:
2329 * Stop the asoc kill timer.
2334 tmr = &stcb->asoc.strreset_timer;
2337 case SCTP_TIMER_TYPE_INPKILL:
2339 * The inp is setup to die. We re-use the signature_chage
2340 * timer since that has stopped and we are in the GONE
2343 tmr = &inp->sctp_ep.signature_change;
2345 case SCTP_TIMER_TYPE_PATHMTURAISE:
2346 if ((stcb == NULL) || (net == NULL)) {
2349 tmr = &net->pmtu_timer;
2351 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2352 if ((stcb == NULL) || (net == NULL)) {
2355 tmr = &net->rxt_timer;
2357 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2361 tmr = &stcb->asoc.shut_guard_timer;
2363 case SCTP_TIMER_TYPE_STRRESET:
2367 tmr = &stcb->asoc.strreset_timer;
2369 case SCTP_TIMER_TYPE_ASCONF:
2373 tmr = &stcb->asoc.asconf_timer;
2375 case SCTP_TIMER_TYPE_PRIM_DELETED:
2379 tmr = &stcb->asoc.delete_prim_timer;
2381 case SCTP_TIMER_TYPE_AUTOCLOSE:
2385 tmr = &stcb->asoc.autoclose_timer;
2388 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2389 __FUNCTION__, t_type);
2395 if ((tmr->type != t_type) && tmr->type) {
2397 * Ok we have a timer that is under joint use. Cookie timer
2398 * per chance with the SEND timer. We therefore are NOT
2399 * running the timer that the caller wants stopped. So just
2404 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2405 stcb->asoc.num_send_timers_up--;
2406 if (stcb->asoc.num_send_timers_up < 0) {
2407 stcb->asoc.num_send_timers_up = 0;
2411 tmr->stopped_from = from;
2412 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2417 sctp_calculate_len(struct mbuf *m)
2424 tlen += SCTP_BUF_LEN(at);
2425 at = SCTP_BUF_NEXT(at);
2431 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2432 struct sctp_association *asoc, uint32_t mtu)
2435 * Reset the P-MTU size on this association, this involves changing
2436 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2437 * allow the DF flag to be cleared.
2439 struct sctp_tmit_chunk *chk;
2440 unsigned int eff_mtu, ovh;
2442 asoc->smallest_mtu = mtu;
2443 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2444 ovh = SCTP_MIN_OVERHEAD;
2446 ovh = SCTP_MIN_V4_OVERHEAD;
2448 eff_mtu = mtu - ovh;
2449 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2450 if (chk->send_size > eff_mtu) {
2451 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2454 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2455 if (chk->send_size > eff_mtu) {
2456 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2463 * given an association and starting time of the current RTT period return
2464 * RTO in number of msecs net should point to the current network
2468 sctp_calculate_rto(struct sctp_tcb *stcb,
2469 struct sctp_association *asoc,
2470 struct sctp_nets *net,
2471 struct timeval *told,
2472 int safe, int rtt_from_sack)
2475 * given an association and the starting time of the current RTT
2476 * period (in value1/value2) return RTO in number of msecs.
2478 int32_t rtt; /* RTT in ms */
2480 int first_measure = 0;
2481 struct timeval now, then, *old;
2483 /* Copy it out for sparc64 */
2484 if (safe == sctp_align_unsafe_makecopy) {
2486 memcpy(&then, told, sizeof(struct timeval));
2487 } else if (safe == sctp_align_safe_nocopy) {
2491 SCTP_PRINTF("Huh, bad rto calc call\n");
2494 /************************/
2495 /* 1. calculate new RTT */
2496 /************************/
2497 /* get the current time */
2498 if (stcb->asoc.use_precise_time) {
2499 (void)SCTP_GETPTIME_TIMEVAL(&now);
2501 (void)SCTP_GETTIME_TIMEVAL(&now);
2503 timevalsub(&now, old);
2504 /* store the current RTT in us */
2505 net->rtt = (uint64_t)1000000 * (uint64_t)now.tv_sec +
2506 (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)) {
2510 /* Tell the CC module that a new update has just occurred from a sack */
2511 (*asoc->cc_functions.sctp_rtt_calculated)(stcb, net, &now);
2513 /* Do we need to determine the lan? We do this only
2514 * on sacks i.e. RTT being determined from data not
2515 * non-data (HB/INIT->INITACK).
2517 if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2518 (net->lan_type == SCTP_LAN_UNKNOWN)) {
2519 if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2520 net->lan_type = SCTP_LAN_INTERNET;
2522 net->lan_type = SCTP_LAN_LOCAL;
2526 /***************************/
2527 /* 2. update RTTVAR & SRTT */
2528 /***************************/
2530 * Compute the scaled average lastsa and the
2531 * scaled variance lastsv as described in van Jacobson
2532 * Paper "Congestion Avoidance and Control", Annex A.
2534 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2535 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2537 if (net->RTO_measured) {
2538 rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2543 rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2545 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2546 rto_logging(net, SCTP_LOG_RTTVAR);
2549 /* First RTO measurment */
2550 net->RTO_measured = 1;
2552 net->lastsa = rtt << SCTP_RTT_SHIFT;
2553 net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2554 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2555 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2558 if (net->lastsv == 0) {
2559 net->lastsv = SCTP_CLOCK_GRANULARITY;
2561 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2562 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2563 (stcb->asoc.sat_network_lockout == 0)) {
2564 stcb->asoc.sat_network = 1;
2565 } else if ((!first_measure) && stcb->asoc.sat_network) {
2566 stcb->asoc.sat_network = 0;
2567 stcb->asoc.sat_network_lockout = 1;
2569 /* bound it, per C6/C7 in Section 5.3.1 */
2570 if (new_rto < stcb->asoc.minrto) {
2571 new_rto = stcb->asoc.minrto;
2573 if (new_rto > stcb->asoc.maxrto) {
2574 new_rto = stcb->asoc.maxrto;
2576 /* we are now returning the RTO */
2581 * return a pointer to a contiguous piece of data from the given mbuf chain
2582 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2583 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2584 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2587 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2593 if ((off < 0) || (len <= 0))
2596 /* find the desired start location */
2597 while ((m != NULL) && (off > 0)) {
2598 if (off < SCTP_BUF_LEN(m))
2600 off -= SCTP_BUF_LEN(m);
2601 m = SCTP_BUF_NEXT(m);
2606 /* is the current mbuf large enough (eg. contiguous)? */
2607 if ((SCTP_BUF_LEN(m) - off) >= len) {
2608 return (mtod(m, caddr_t) + off);
2610 /* else, it spans more than one mbuf, so save a temp copy... */
2611 while ((m != NULL) && (len > 0)) {
2612 count = min(SCTP_BUF_LEN(m) - off, len);
2613 bcopy(mtod(m, caddr_t) + off, ptr, count);
2617 m = SCTP_BUF_NEXT(m);
2619 if ((m == NULL) && (len > 0))
2622 return ((caddr_t)in_ptr);
2628 struct sctp_paramhdr *
2629 sctp_get_next_param(struct mbuf *m,
2631 struct sctp_paramhdr *pull,
2634 /* This just provides a typed signature to Peter's Pull routine */
2635 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2641 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2644 * add padlen bytes of 0 filled padding to the end of the mbuf. If
2645 * padlen is > 3 this routine will fail.
2651 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
2654 if (padlen <= M_TRAILINGSPACE(m)) {
2656 * The easy way. We hope the majority of the time we hit
2659 dp = (uint8_t *) (mtod(m, caddr_t) + SCTP_BUF_LEN(m));
2660 SCTP_BUF_LEN(m) += padlen;
2662 /* Hard way we must grow the mbuf */
2665 tmp = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2667 /* Out of space GAK! we are in big trouble. */
2668 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
2671 /* setup and insert in middle */
2672 SCTP_BUF_LEN(tmp) = padlen;
2673 SCTP_BUF_NEXT(tmp) = NULL;
2674 SCTP_BUF_NEXT(m) = tmp;
2675 dp = mtod(tmp, uint8_t *);
2677 /* zero out the pad */
2678 for (i = 0; i < padlen; i++) {
2686 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2688 /* find the last mbuf in chain and pad it */
2692 return (sctp_add_pad_tombuf(last_mbuf, padval));
2694 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2695 if (SCTP_BUF_NEXT(m_at) == NULL) {
2696 return (sctp_add_pad_tombuf(m_at, padval));
2700 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
2705 sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2706 uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2707 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2712 struct mbuf *m_notify;
2713 struct sctp_assoc_change *sac;
2714 struct sctp_queued_to_read *control;
2715 size_t notif_len, abort_len;
2717 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2721 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2722 notif_len = sizeof(struct sctp_assoc_change);
2723 if (abort != NULL) {
2724 abort_len = htons(abort->ch.chunk_length);
2728 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2729 notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2730 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2731 notif_len += abort_len;
2733 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2734 if (m_notify == NULL) {
2735 /* Retry with smaller value. */
2736 notif_len = sizeof(struct sctp_assoc_change);
2737 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2738 if (m_notify == NULL) {
2742 SCTP_BUF_NEXT(m_notify) = NULL;
2743 sac = mtod(m_notify, struct sctp_assoc_change *);
2744 sac->sac_type = SCTP_ASSOC_CHANGE;
2746 sac->sac_length = sizeof(struct sctp_assoc_change);
2747 sac->sac_state = state;
2748 sac->sac_error = error;
2749 /* XXX verify these stream counts */
2750 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2751 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2752 sac->sac_assoc_id = sctp_get_associd(stcb);
2753 if (notif_len > sizeof(struct sctp_assoc_change)) {
2754 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2756 if (stcb->asoc.peer_supports_prsctp) {
2757 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2759 if (stcb->asoc.peer_supports_auth) {
2760 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2762 if (stcb->asoc.peer_supports_asconf) {
2763 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2765 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2766 if (stcb->asoc.peer_supports_strreset) {
2767 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2769 sac->sac_length += i;
2770 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2771 memcpy(sac->sac_info, abort, abort_len);
2772 sac->sac_length += abort_len;
2775 SCTP_BUF_LEN(m_notify) = sac->sac_length;
2776 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2777 0, 0, stcb->asoc.context, 0, 0, 0,
2779 if (control != NULL) {
2780 control->length = SCTP_BUF_LEN(m_notify);
2781 /* not that we need this */
2782 control->tail_mbuf = m_notify;
2783 control->spec_flags = M_NOTIFICATION;
2784 sctp_add_to_readq(stcb->sctp_ep, stcb,
2786 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2789 sctp_m_freem(m_notify);
2793 * For 1-to-1 style sockets, we send up and error when an ABORT
2797 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2798 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2799 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2800 SOCK_LOCK(stcb->sctp_socket);
2802 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
2803 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2804 stcb->sctp_socket->so_error = ECONNREFUSED;
2806 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2807 stcb->sctp_socket->so_error = ECONNRESET;
2810 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2811 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2812 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2813 stcb->sctp_socket->so_error = ETIMEDOUT;
2815 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2816 stcb->sctp_socket->so_error = ECONNABORTED;
2820 /* Wake ANY sleepers */
2821 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2822 so = SCTP_INP_SO(stcb->sctp_ep);
2824 atomic_add_int(&stcb->asoc.refcnt, 1);
2825 SCTP_TCB_UNLOCK(stcb);
2826 SCTP_SOCKET_LOCK(so, 1);
2827 SCTP_TCB_LOCK(stcb);
2828 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2829 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2830 SCTP_SOCKET_UNLOCK(so, 1);
2835 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2836 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2837 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2838 #if defined(__APPLE__)
2839 socantrcvmore(stcb->sctp_socket);
2841 socantrcvmore_locked(stcb->sctp_socket);
2844 sorwakeup(stcb->sctp_socket);
2845 sowwakeup(stcb->sctp_socket);
2846 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2848 SCTP_SOCKET_UNLOCK(so, 1);
2854 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2855 struct sockaddr *sa, uint32_t error)
2857 struct mbuf *m_notify;
2858 struct sctp_paddr_change *spc;
2859 struct sctp_queued_to_read *control;
2861 if ((stcb == NULL) ||
2862 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2863 /* event not enabled */
2866 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2867 if (m_notify == NULL)
2869 SCTP_BUF_LEN(m_notify) = 0;
2870 spc = mtod(m_notify, struct sctp_paddr_change *);
2871 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2873 spc->spc_length = sizeof(struct sctp_paddr_change);
2874 switch (sa->sa_family) {
2877 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2883 #ifdef SCTP_EMBEDDED_V6_SCOPE
2884 struct sockaddr_in6 *sin6;
2885 #endif /* SCTP_EMBEDDED_V6_SCOPE */
2886 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2888 #ifdef SCTP_EMBEDDED_V6_SCOPE
2889 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2890 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2891 if (sin6->sin6_scope_id == 0) {
2892 /* recover scope_id for user */
2894 (void)sa6_recoverscope(sin6);
2896 (void)in6_recoverscope(sin6, &sin6->sin6_addr,
2900 /* clear embedded scope_id for user */
2901 in6_clearscope(&sin6->sin6_addr);
2904 #endif /* SCTP_EMBEDDED_V6_SCOPE */
2908 #if defined(__Userspace__)
2910 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_conn));
2917 spc->spc_state = state;
2918 spc->spc_error = error;
2919 spc->spc_assoc_id = sctp_get_associd(stcb);
2921 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2922 SCTP_BUF_NEXT(m_notify) = NULL;
2924 /* append to socket */
2925 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2926 0, 0, stcb->asoc.context, 0, 0, 0,
2928 if (control == NULL) {
2930 sctp_m_freem(m_notify);
2933 control->length = SCTP_BUF_LEN(m_notify);
2934 control->spec_flags = M_NOTIFICATION;
2935 /* not that we need this */
2936 control->tail_mbuf = m_notify;
2937 sctp_add_to_readq(stcb->sctp_ep, stcb,
2939 &stcb->sctp_socket->so_rcv, 1,
2940 SCTP_READ_LOCK_NOT_HELD,
2941 SCTP_SO_NOT_LOCKED);
2946 sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2947 struct sctp_tmit_chunk *chk, int so_locked
2948 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2953 struct mbuf *m_notify;
2954 struct sctp_send_failed *ssf;
2955 struct sctp_send_failed_event *ssfe;
2956 struct sctp_queued_to_read *control;
2959 if ((stcb == NULL) ||
2960 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
2961 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2962 /* event not enabled */
2966 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
2967 length = sizeof(struct sctp_send_failed_event);
2969 length = sizeof(struct sctp_send_failed);
2971 m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
2972 if (m_notify == NULL)
2975 length += chk->send_size;
2976 length -= sizeof(struct sctp_data_chunk);
2977 SCTP_BUF_LEN(m_notify) = 0;
2978 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
2979 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2980 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
2982 ssfe->ssfe_flags = SCTP_DATA_SENT;
2984 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
2986 ssfe->ssfe_length = length;
2987 ssfe->ssfe_error = error;
2988 /* not exactly what the user sent in, but should be close :) */
2989 bzero(&ssfe->ssfe_info, sizeof(ssfe->ssfe_info));
2990 ssfe->ssfe_info.snd_sid = chk->rec.data.stream_number;
2991 ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
2992 ssfe->ssfe_info.snd_ppid = chk->rec.data.payloadtype;
2993 ssfe->ssfe_info.snd_context = chk->rec.data.context;
2994 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
2995 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
2996 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
2998 ssf = mtod(m_notify, struct sctp_send_failed *);
2999 ssf->ssf_type = SCTP_SEND_FAILED;
3001 ssf->ssf_flags = SCTP_DATA_SENT;
3003 ssf->ssf_flags = SCTP_DATA_UNSENT;
3005 ssf->ssf_length = length;
3006 ssf->ssf_error = error;
3007 /* not exactly what the user sent in, but should be close :) */
3008 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
3009 ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
3010 ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
3011 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
3012 ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
3013 ssf->ssf_info.sinfo_context = chk->rec.data.context;
3014 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3015 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3016 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
3020 * trim off the sctp chunk header(it should
3023 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3024 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3025 sctp_mbuf_crush(chk->data);
3026 chk->send_size -= sizeof(struct sctp_data_chunk);
3029 SCTP_BUF_NEXT(m_notify) = chk->data;
3030 /* Steal off the mbuf */
3033 * For this case, we check the actual socket buffer, since the assoc
3034 * is going away we don't want to overfill the socket buffer for a
3037 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3038 sctp_m_freem(m_notify);
3041 /* append to socket */
3042 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3043 0, 0, stcb->asoc.context, 0, 0, 0,
3045 if (control == NULL) {
3047 sctp_m_freem(m_notify);
3050 control->spec_flags = M_NOTIFICATION;
3051 sctp_add_to_readq(stcb->sctp_ep, stcb,
3053 &stcb->sctp_socket->so_rcv, 1,
3054 SCTP_READ_LOCK_NOT_HELD,
3060 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3061 struct sctp_stream_queue_pending *sp, int so_locked
3062 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3067 struct mbuf *m_notify;
3068 struct sctp_send_failed *ssf;
3069 struct sctp_send_failed_event *ssfe;
3070 struct sctp_queued_to_read *control;
3073 if ((stcb == NULL) ||
3074 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3075 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3076 /* event not enabled */
3079 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3080 length = sizeof(struct sctp_send_failed_event);
3082 length = sizeof(struct sctp_send_failed);
3084 m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
3085 if (m_notify == NULL) {
3089 length += sp->length;
3090 SCTP_BUF_LEN(m_notify) = 0;
3091 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3092 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3093 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3094 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3095 ssfe->ssfe_length = length;
3096 ssfe->ssfe_error = error;
3097 /* not exactly what the user sent in, but should be close :) */
3098 bzero(&ssfe->ssfe_info, sizeof(ssfe->ssfe_info));
3099 ssfe->ssfe_info.snd_sid = sp->stream;
3100 if (sp->some_taken) {
3101 ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
3103 ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
3105 ssfe->ssfe_info.snd_ppid = sp->ppid;
3106 ssfe->ssfe_info.snd_context = sp->context;
3107 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3108 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3109 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
3111 ssf = mtod(m_notify, struct sctp_send_failed *);
3112 ssf->ssf_type = SCTP_SEND_FAILED;
3113 ssf->ssf_flags = SCTP_DATA_UNSENT;
3114 ssf->ssf_length = length;
3115 ssf->ssf_error = error;
3116 /* not exactly what the user sent in, but should be close :) */
3117 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
3118 ssf->ssf_info.sinfo_stream = sp->stream;
3119 ssf->ssf_info.sinfo_ssn = 0;
3120 if (sp->some_taken) {
3121 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3123 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3125 ssf->ssf_info.sinfo_ppid = sp->ppid;
3126 ssf->ssf_info.sinfo_context = sp->context;
3127 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3128 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3129 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
3131 SCTP_BUF_NEXT(m_notify) = sp->data;
3133 /* Steal off the mbuf */
3136 * For this case, we check the actual socket buffer, since the assoc
3137 * is going away we don't want to overfill the socket buffer for a
3140 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3141 sctp_m_freem(m_notify);
3144 /* append to socket */
3145 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3146 0, 0, stcb->asoc.context, 0, 0, 0,
3148 if (control == NULL) {
3150 sctp_m_freem(m_notify);
3153 control->spec_flags = M_NOTIFICATION;
3154 sctp_add_to_readq(stcb->sctp_ep, stcb,
3156 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3162 sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3164 struct mbuf *m_notify;
3165 struct sctp_adaptation_event *sai;
3166 struct sctp_queued_to_read *control;
3168 if ((stcb == NULL) ||
3169 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3170 /* event not enabled */
3174 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3175 if (m_notify == NULL)
3178 SCTP_BUF_LEN(m_notify) = 0;
3179 sai = mtod(m_notify, struct sctp_adaptation_event *);
3180 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3182 sai->sai_length = sizeof(struct sctp_adaptation_event);
3183 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3184 sai->sai_assoc_id = sctp_get_associd(stcb);
3186 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3187 SCTP_BUF_NEXT(m_notify) = NULL;
3189 /* append to socket */
3190 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3191 0, 0, stcb->asoc.context, 0, 0, 0,
3193 if (control == NULL) {
3195 sctp_m_freem(m_notify);
3198 control->length = SCTP_BUF_LEN(m_notify);
3199 control->spec_flags = M_NOTIFICATION;
3200 /* not that we need this */
3201 control->tail_mbuf = m_notify;
3202 sctp_add_to_readq(stcb->sctp_ep, stcb,
3204 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3207 /* This always must be called with the read-queue LOCKED in the INP */
3209 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3210 uint32_t val, int so_locked
3211 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3216 struct mbuf *m_notify;
3217 struct sctp_pdapi_event *pdapi;
3218 struct sctp_queued_to_read *control;
3221 if ((stcb == NULL) ||
3222 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3223 /* event not enabled */
3226 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3230 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3231 if (m_notify == NULL)
3234 SCTP_BUF_LEN(m_notify) = 0;
3235 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3236 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3237 pdapi->pdapi_flags = 0;
3238 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3239 pdapi->pdapi_indication = error;
3240 pdapi->pdapi_stream = (val >> 16);
3241 pdapi->pdapi_seq = (val & 0x0000ffff);
3242 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3244 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3245 SCTP_BUF_NEXT(m_notify) = NULL;
3246 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3247 0, 0, stcb->asoc.context, 0, 0, 0,
3249 if (control == NULL) {
3251 sctp_m_freem(m_notify);
3254 control->spec_flags = M_NOTIFICATION;
3255 control->length = SCTP_BUF_LEN(m_notify);
3256 /* not that we need this */
3257 control->tail_mbuf = m_notify;
3258 control->held_length = 0;
3259 control->length = 0;
3260 sb = &stcb->sctp_socket->so_rcv;
3261 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3262 sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3264 sctp_sballoc(stcb, sb, m_notify);
3265 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3266 sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
3268 atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
3269 control->end_added = 1;
3270 if (stcb->asoc.control_pdapi)
3271 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3273 /* we really should not see this case */
3274 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3276 if (stcb->sctp_ep && stcb->sctp_socket) {
3277 /* This should always be the case */
3278 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3281 so = SCTP_INP_SO(stcb->sctp_ep);
3283 atomic_add_int(&stcb->asoc.refcnt, 1);
3284 SCTP_TCB_UNLOCK(stcb);
3285 SCTP_SOCKET_LOCK(so, 1);
3286 SCTP_TCB_LOCK(stcb);
3287 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3288 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3289 SCTP_SOCKET_UNLOCK(so, 1);
3294 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3295 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3297 SCTP_SOCKET_UNLOCK(so, 1);
3304 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3306 struct mbuf *m_notify;
3307 struct sctp_shutdown_event *sse;
3308 struct sctp_queued_to_read *control;
3311 * For TCP model AND UDP connected sockets we will send an error up
3312 * when an SHUTDOWN completes
3314 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3315 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3316 /* mark socket closed for read/write and wakeup! */
3317 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3320 so = SCTP_INP_SO(stcb->sctp_ep);
3321 atomic_add_int(&stcb->asoc.refcnt, 1);
3322 SCTP_TCB_UNLOCK(stcb);
3323 SCTP_SOCKET_LOCK(so, 1);
3324 SCTP_TCB_LOCK(stcb);
3325 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3326 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3327 SCTP_SOCKET_UNLOCK(so, 1);
3331 socantsendmore(stcb->sctp_socket);
3332 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3333 SCTP_SOCKET_UNLOCK(so, 1);
3336 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3337 /* event not enabled */
3341 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3342 if (m_notify == NULL)
3345 sse = mtod(m_notify, struct sctp_shutdown_event *);
3346 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3348 sse->sse_length = sizeof(struct sctp_shutdown_event);
3349 sse->sse_assoc_id = sctp_get_associd(stcb);
3351 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3352 SCTP_BUF_NEXT(m_notify) = NULL;
3354 /* append to socket */
3355 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3356 0, 0, stcb->asoc.context, 0, 0, 0,
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);
3373 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3375 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3380 struct mbuf *m_notify;
3381 struct sctp_sender_dry_event *event;
3382 struct sctp_queued_to_read *control;
3384 if ((stcb == NULL) ||
3385 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3386 /* event not enabled */
3390 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3391 if (m_notify == NULL) {
3395 SCTP_BUF_LEN(m_notify) = 0;
3396 event = mtod(m_notify, struct sctp_sender_dry_event *);
3397 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3398 event->sender_dry_flags = 0;
3399 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3400 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3402 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3403 SCTP_BUF_NEXT(m_notify) = NULL;
3405 /* append to socket */
3406 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3407 0, 0, stcb->asoc.context, 0, 0, 0,
3409 if (control == NULL) {
3411 sctp_m_freem(m_notify);
3414 control->length = SCTP_BUF_LEN(m_notify);
3415 control->spec_flags = M_NOTIFICATION;
3416 /* not that we need this */
3417 control->tail_mbuf = m_notify;
3418 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3419 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3424 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3426 struct mbuf *m_notify;
3427 struct sctp_queued_to_read *control;
3428 struct sctp_stream_change_event *stradd;
3431 if ((stcb == NULL) ||
3432 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3433 /* event not enabled */
3436 if ((stcb->asoc.peer_req_out) && flag) {
3437 /* Peer made the request, don't tell the local user */
3438 stcb->asoc.peer_req_out = 0;
3441 stcb->asoc.peer_req_out = 0;
3442 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3443 if (m_notify == NULL)
3446 SCTP_BUF_LEN(m_notify) = 0;
3447 len = sizeof(struct sctp_stream_change_event);
3448 if (len > M_TRAILINGSPACE(m_notify)) {
3449 /* never enough room */
3450 sctp_m_freem(m_notify);
3453 stradd = mtod(m_notify, struct sctp_stream_change_event *);
3454 stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3455 stradd->strchange_flags = flag;
3456 stradd->strchange_length = len;
3457 stradd->strchange_assoc_id = sctp_get_associd(stcb);
3458 stradd->strchange_instrms = numberin;
3459 stradd->strchange_outstrms = numberout;
3460 SCTP_BUF_LEN(m_notify) = len;
3461 SCTP_BUF_NEXT(m_notify) = NULL;
3462 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3464 sctp_m_freem(m_notify);
3467 /* append to socket */
3468 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3469 0, 0, stcb->asoc.context, 0, 0, 0,
3471 if (control == NULL) {
3473 sctp_m_freem(m_notify);
3476 control->spec_flags = M_NOTIFICATION;
3477 control->length = SCTP_BUF_LEN(m_notify);
3478 /* not that we need this */
3479 control->tail_mbuf = m_notify;
3480 sctp_add_to_readq(stcb->sctp_ep, stcb,
3482 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3486 sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3488 struct mbuf *m_notify;
3489 struct sctp_queued_to_read *control;
3490 struct sctp_assoc_reset_event *strasoc;
3493 if ((stcb == NULL) ||
3494 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3495 /* event not enabled */
3498 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3499 if (m_notify == NULL)
3502 SCTP_BUF_LEN(m_notify) = 0;
3503 len = sizeof(struct sctp_assoc_reset_event);
3504 if (len > M_TRAILINGSPACE(m_notify)) {
3505 /* never enough room */
3506 sctp_m_freem(m_notify);
3509 strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3510 strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3511 strasoc->assocreset_flags = flag;
3512 strasoc->assocreset_length = len;
3513 strasoc->assocreset_assoc_id= sctp_get_associd(stcb);
3514 strasoc->assocreset_local_tsn = sending_tsn;
3515 strasoc->assocreset_remote_tsn = recv_tsn;
3516 SCTP_BUF_LEN(m_notify) = len;
3517 SCTP_BUF_NEXT(m_notify) = NULL;
3518 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3520 sctp_m_freem(m_notify);
3523 /* append to socket */
3524 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3525 0, 0, stcb->asoc.context, 0, 0, 0,
3527 if (control == NULL) {
3529 sctp_m_freem(m_notify);
3532 control->spec_flags = M_NOTIFICATION;
3533 control->length = SCTP_BUF_LEN(m_notify);
3534 /* not that we need this */
3535 control->tail_mbuf = m_notify;
3536 sctp_add_to_readq(stcb->sctp_ep, stcb,
3538 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3544 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3545 int number_entries, uint16_t * list, int flag)
3547 struct mbuf *m_notify;
3548 struct sctp_queued_to_read *control;
3549 struct sctp_stream_reset_event *strreset;
3552 if ((stcb == NULL) ||
3553 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3554 /* event not enabled */
3558 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3559 if (m_notify == NULL)
3562 SCTP_BUF_LEN(m_notify) = 0;
3563 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3564 if (len > M_TRAILINGSPACE(m_notify)) {
3565 /* never enough room */
3566 sctp_m_freem(m_notify);
3569 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3570 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3571 strreset->strreset_flags = flag;
3572 strreset->strreset_length = len;
3573 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3574 if (number_entries) {
3577 for (i = 0; i < number_entries; i++) {
3578 strreset->strreset_stream_list[i] = ntohs(list[i]);
3581 SCTP_BUF_LEN(m_notify) = len;
3582 SCTP_BUF_NEXT(m_notify) = NULL;
3583 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3585 sctp_m_freem(m_notify);
3588 /* append to socket */
3589 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3590 0, 0, stcb->asoc.context, 0, 0, 0,
3592 if (control == NULL) {
3594 sctp_m_freem(m_notify);
3597 control->spec_flags = M_NOTIFICATION;
3598 control->length = SCTP_BUF_LEN(m_notify);
3599 /* not that we need this */
3600 control->tail_mbuf = m_notify;
3601 sctp_add_to_readq(stcb->sctp_ep, stcb,
3603 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3608 sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3610 struct mbuf *m_notify;
3611 struct sctp_remote_error *sre;
3612 struct sctp_queued_to_read *control;
3613 size_t notif_len, chunk_len;
3615 if ((stcb == NULL) ||
3616 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3619 if (chunk != NULL) {
3620 chunk_len = htons(chunk->ch.chunk_length);
3624 notif_len = sizeof(struct sctp_remote_error) + chunk_len;
3625 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3626 if (m_notify == NULL) {
3627 /* Retry with smaller value. */
3628 notif_len = sizeof(struct sctp_remote_error);
3629 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3630 if (m_notify == NULL) {
3634 SCTP_BUF_NEXT(m_notify) = NULL;
3635 sre = mtod(m_notify, struct sctp_remote_error *);
3636 sre->sre_type = SCTP_REMOTE_ERROR;
3638 sre->sre_length = sizeof(struct sctp_remote_error);
3639 sre->sre_error = error;
3640 sre->sre_assoc_id = sctp_get_associd(stcb);
3641 if (notif_len > sizeof(struct sctp_remote_error)) {
3642 memcpy(sre->sre_data, chunk, chunk_len);
3643 sre->sre_length += chunk_len;
3645 SCTP_BUF_LEN(m_notify) = sre->sre_length;
3646 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3647 0, 0, stcb->asoc.context, 0, 0, 0,
3649 if (control != NULL) {
3650 control->length = SCTP_BUF_LEN(m_notify);
3651 /* not that we need this */
3652 control->tail_mbuf = m_notify;
3653 control->spec_flags = M_NOTIFICATION;
3654 sctp_add_to_readq(stcb->sctp_ep, stcb,
3656 &stcb->sctp_socket->so_rcv, 1,
3657 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3659 sctp_m_freem(m_notify);
3665 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3666 uint32_t error, void *data, int so_locked
3667 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3672 if ((stcb == NULL) ||
3673 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3674 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3675 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3676 /* If the socket is gone we are out of here */
3679 #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__)
3680 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3682 if (stcb->sctp_socket->so_state & SS_CANTRCVMORE) {
3686 #if defined(__APPLE__)
3688 sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
3690 sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
3693 if ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3694 (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED)) {
3695 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3696 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3697 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3698 /* Don't report these in front states */
3702 switch (notification) {
3703 case SCTP_NOTIFY_ASSOC_UP:
3704 if (stcb->asoc.assoc_up_sent == 0) {
3705 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3706 stcb->asoc.assoc_up_sent = 1;
3708 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3709 sctp_notify_adaptation_layer(stcb);
3711 if (stcb->asoc.peer_supports_auth == 0) {
3712 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3716 case SCTP_NOTIFY_ASSOC_DOWN:
3717 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3718 #if defined(__Userspace__)
3719 if (stcb->sctp_ep->recv_callback) {
3720 if (stcb->sctp_socket) {
3721 union sctp_sockstore addr;
3722 struct sctp_rcvinfo rcv;
3724 memset(&addr, 0, sizeof(union sctp_sockstore));
3725 memset(&rcv, 0, sizeof(struct sctp_rcvinfo));
3726 atomic_add_int(&stcb->asoc.refcnt, 1);
3727 SCTP_TCB_UNLOCK(stcb);
3728 stcb->sctp_ep->recv_callback(stcb->sctp_socket, addr, NULL, 0, rcv, 0, stcb->sctp_ep->ulp_info);
3729 SCTP_TCB_LOCK(stcb);
3730 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3735 case SCTP_NOTIFY_INTERFACE_DOWN:
3737 struct sctp_nets *net;
3739 net = (struct sctp_nets *)data;
3740 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3741 (struct sockaddr *)&net->ro._l_addr, error);
3744 case SCTP_NOTIFY_INTERFACE_UP:
3746 struct sctp_nets *net;
3748 net = (struct sctp_nets *)data;
3749 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3750 (struct sockaddr *)&net->ro._l_addr, error);
3753 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3755 struct sctp_nets *net;
3757 net = (struct sctp_nets *)data;
3758 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3759 (struct sockaddr *)&net->ro._l_addr, error);
3762 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3763 sctp_notify_send_failed2(stcb, error,
3764 (struct sctp_stream_queue_pending *)data, so_locked);
3766 case SCTP_NOTIFY_SENT_DG_FAIL:
3767 sctp_notify_send_failed(stcb, 1, error,
3768 (struct sctp_tmit_chunk *)data, so_locked);
3770 case SCTP_NOTIFY_UNSENT_DG_FAIL:
3771 sctp_notify_send_failed(stcb, 0, error,
3772 (struct sctp_tmit_chunk *)data, so_locked);
3774 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3777 val = *((uint32_t *)data);
3779 sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3782 case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3783 if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3784 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3785 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3787 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3790 case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3791 if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3792 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3793 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3795 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3798 case SCTP_NOTIFY_ASSOC_RESTART:
3799 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3800 if (stcb->asoc.peer_supports_auth == 0) {
3801 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3805 case SCTP_NOTIFY_STR_RESET_SEND:
3806 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN);
3808 case SCTP_NOTIFY_STR_RESET_RECV:
3809 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING);
3811 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3812 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3813 (SCTP_STREAM_RESET_OUTGOING_SSN|SCTP_STREAM_RESET_FAILED));
3815 case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3816 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3817 (SCTP_STREAM_RESET_OUTGOING_SSN|SCTP_STREAM_RESET_DENIED));
3819 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3820 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3821 (SCTP_STREAM_RESET_INCOMING|SCTP_STREAM_RESET_FAILED));
3823 case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3824 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3825 (SCTP_STREAM_RESET_INCOMING|SCTP_STREAM_RESET_DENIED));
3827 case SCTP_NOTIFY_ASCONF_ADD_IP:
3828 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3831 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3832 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3835 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3836 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3839 case SCTP_NOTIFY_PEER_SHUTDOWN:
3840 sctp_notify_shutdown_event(stcb);
3842 case SCTP_NOTIFY_AUTH_NEW_KEY:
3843 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3844 (uint16_t)(uintptr_t)data,
3847 case SCTP_NOTIFY_AUTH_FREE_KEY:
3848 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3849 (uint16_t)(uintptr_t)data,
3852 case SCTP_NOTIFY_NO_PEER_AUTH:
3853 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3854 (uint16_t)(uintptr_t)data,
3857 case SCTP_NOTIFY_SENDER_DRY:
3858 sctp_notify_sender_dry_event(stcb, so_locked);
3860 case SCTP_NOTIFY_REMOTE_ERROR:
3861 sctp_notify_remote_error(stcb, error, data);
3864 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3865 __FUNCTION__, notification, notification);
3871 sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3872 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3877 struct sctp_association *asoc;
3878 struct sctp_stream_out *outs;
3879 struct sctp_tmit_chunk *chk, *nchk;
3880 struct sctp_stream_queue_pending *sp, *nsp;
3887 if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3888 /* already being freed */
3891 #if defined(__APPLE__)
3893 sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
3895 sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
3898 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3899 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3900 (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3903 /* now through all the gunk freeing chunks */
3904 if (holds_lock == 0) {
3905 SCTP_TCB_SEND_LOCK(stcb);
3907 /* sent queue SHOULD be empty */
3908 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3909 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3910 asoc->sent_queue_cnt--;
3911 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3912 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3913 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3916 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3920 if (chk->data != NULL) {
3921 sctp_free_bufspace(stcb, asoc, chk, 1);
3922 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
3923 error, chk, so_locked);
3925 sctp_m_freem(chk->data);
3929 sctp_free_a_chunk(stcb, chk, so_locked);
3930 /*sa_ignore FREED_MEMORY*/
3932 /* pending send queue SHOULD be empty */
3933 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3934 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3935 asoc->send_queue_cnt--;
3936 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3937 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3940 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3943 if (chk->data != NULL) {
3944 sctp_free_bufspace(stcb, asoc, chk, 1);
3945 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
3946 error, chk, so_locked);
3948 sctp_m_freem(chk->data);
3952 sctp_free_a_chunk(stcb, chk, so_locked);
3953 /*sa_ignore FREED_MEMORY*/
3955 for (i = 0; i < asoc->streamoutcnt; i++) {
3956 /* For each stream */
3957 outs = &asoc->strmout[i];
3958 /* clean up any sends there */
3959 asoc->locked_on_sending = NULL;
3960 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3961 asoc->stream_queue_cnt--;
3962 TAILQ_REMOVE(&outs->outqueue, sp, next);
3963 sctp_free_spbufspace(stcb, asoc, sp);
3965 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3966 error, (void *)sp, so_locked);
3968 sctp_m_freem(sp->data);
3970 sp->tail_mbuf = NULL;
3975 sctp_free_remote_addr(sp->net);
3978 /* Free the chunk */
3979 sctp_free_a_strmoq(stcb, sp, so_locked);
3980 /*sa_ignore FREED_MEMORY*/
3984 if (holds_lock == 0) {
3985 SCTP_TCB_SEND_UNLOCK(stcb);
3990 sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3991 struct sctp_abort_chunk *abort, int so_locked
3992 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4000 #if defined(__APPLE__)
4002 sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
4004 sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
4007 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4008 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4009 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4010 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
4012 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4013 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4014 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4017 /* Tell them we lost the asoc */
4018 sctp_report_all_outbound(stcb, error, 1, so_locked);
4020 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4022 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4027 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4028 struct mbuf *m, int iphlen,
4029 struct sockaddr *src, struct sockaddr *dst,
4030 struct sctphdr *sh, struct mbuf *op_err,
4031 #if defined(__FreeBSD__)
4032 uint8_t use_mflowid, uint32_t mflowid,
4034 uint32_t vrf_id, uint16_t port)
4037 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4043 /* We have a TCB to abort, send notification too */
4044 vtag = stcb->asoc.peer_vtag;
4045 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4046 /* get the assoc vrf id and table id */
4047 vrf_id = stcb->asoc.vrf_id;
4048 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
4050 sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4051 #if defined(__FreeBSD__)
4052 use_mflowid, mflowid,
4056 /* Ok, now lets free it */
4057 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4058 so = SCTP_INP_SO(inp);
4059 atomic_add_int(&stcb->asoc.refcnt, 1);
4060 SCTP_TCB_UNLOCK(stcb);
4061 SCTP_SOCKET_LOCK(so, 1);
4062 SCTP_TCB_LOCK(stcb);
4063 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4065 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4066 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
4067 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4068 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4070 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL+SCTP_LOC_4);
4071 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4072 SCTP_SOCKET_UNLOCK(so, 1);
4076 #ifdef SCTP_ASOCLOG_OF_TSNS
4078 sctp_print_out_track_log(struct sctp_tcb *stcb)
4082 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4083 SCTP_PRINTF("IN bound TSN log-aaa\n");
4084 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4085 SCTP_PRINTF("None rcvd\n");
4088 if (stcb->asoc.tsn_in_wrapped) {
4089 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4090 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4091 stcb->asoc.in_tsnlog[i].tsn,
4092 stcb->asoc.in_tsnlog[i].strm,
4093 stcb->asoc.in_tsnlog[i].seq,
4094 stcb->asoc.in_tsnlog[i].flgs,
4095 stcb->asoc.in_tsnlog[i].sz);
4098 if (stcb->asoc.tsn_in_at) {
4099 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4100 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4101 stcb->asoc.in_tsnlog[i].tsn,
4102 stcb->asoc.in_tsnlog[i].strm,
4103 stcb->asoc.in_tsnlog[i].seq,
4104 stcb->asoc.in_tsnlog[i].flgs,
4105 stcb->asoc.in_tsnlog[i].sz);
4109 SCTP_PRINTF("OUT bound TSN log-aaa\n");
4110 if ((stcb->asoc.tsn_out_at == 0) &&
4111 (stcb->asoc.tsn_out_wrapped == 0)) {
4112 SCTP_PRINTF("None sent\n");
4114 if (stcb->asoc.tsn_out_wrapped) {
4115 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4116 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4117 stcb->asoc.out_tsnlog[i].tsn,
4118 stcb->asoc.out_tsnlog[i].strm,
4119 stcb->asoc.out_tsnlog[i].seq,
4120 stcb->asoc.out_tsnlog[i].flgs,
4121 stcb->asoc.out_tsnlog[i].sz);
4124 if (stcb->asoc.tsn_out_at) {
4125 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4126 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4127 stcb->asoc.out_tsnlog[i].tsn,
4128 stcb->asoc.out_tsnlog[i].strm,
4129 stcb->asoc.out_tsnlog[i].seq,
4130 stcb->asoc.out_tsnlog[i].flgs,
4131 stcb->asoc.out_tsnlog[i].sz);
4139 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4140 struct mbuf *op_err,
4142 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4147 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4151 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4152 so = SCTP_INP_SO(inp);
4154 #if defined(__APPLE__)
4156 sctp_lock_assert(SCTP_INP_SO(inp));
4158 sctp_unlock_assert(SCTP_INP_SO(inp));
4162 /* Got to have a TCB */
4163 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4164 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4165 #if defined(__APPLE__)
4167 SCTP_SOCKET_LOCK(so, 1);
4170 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4171 SCTP_CALLED_DIRECTLY_NOCMPSET);
4172 #if defined(__APPLE__)
4174 SCTP_SOCKET_UNLOCK(so, 1);
4181 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
4183 /* notify the ulp */
4184 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4185 sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4187 /* notify the peer */
4188 sctp_send_abort_tcb(stcb, op_err, so_locked);
4189 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4190 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
4191 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4192 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4194 /* now free the asoc */
4195 #ifdef SCTP_ASOCLOG_OF_TSNS
4196 sctp_print_out_track_log(stcb);
4198 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4200 atomic_add_int(&stcb->asoc.refcnt, 1);
4201 SCTP_TCB_UNLOCK(stcb);
4202 SCTP_SOCKET_LOCK(so, 1);
4203 SCTP_TCB_LOCK(stcb);
4204 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4207 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL+SCTP_LOC_5);
4208 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4210 SCTP_SOCKET_UNLOCK(so, 1);
4216 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4217 struct sockaddr *src, struct sockaddr *dst,
4218 struct sctphdr *sh, struct sctp_inpcb *inp,
4219 #if defined(__FreeBSD__)
4220 uint8_t use_mflowid, uint32_t mflowid,
4222 uint32_t vrf_id, uint16_t port)
4224 struct sctp_chunkhdr *ch, chunk_buf;
4225 unsigned int chk_length;
4226 int contains_init_chunk;
4228 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4229 /* Generate a TO address for future reference */
4230 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4231 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4232 #if defined(__APPLE__)
4233 SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1);
4235 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4236 SCTP_CALLED_DIRECTLY_NOCMPSET);
4237 #if defined(__APPLE__)
4238 SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1);
4242 contains_init_chunk = 0;
4243 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4244 sizeof(*ch), (uint8_t *) & chunk_buf);
4245 while (ch != NULL) {
4246 chk_length = ntohs(ch->chunk_length);
4247 if (chk_length < sizeof(*ch)) {
4248 /* break to abort land */
4251 switch (ch->chunk_type) {
4253 contains_init_chunk = 1;
4255 case SCTP_COOKIE_ECHO:
4256 /* We hit here only if the assoc is being freed */
4258 case SCTP_PACKET_DROPPED:
4259 /* we don't respond to pkt-dropped */
4261 case SCTP_ABORT_ASSOCIATION:
4262 /* we don't respond with an ABORT to an ABORT */
4264 case SCTP_SHUTDOWN_COMPLETE:
4266 * we ignore it since we are not waiting for it and
4270 case SCTP_SHUTDOWN_ACK:
4271 sctp_send_shutdown_complete2(src, dst, sh,
4272 #if defined(__FreeBSD__)
4273 use_mflowid, mflowid,
4280 offset += SCTP_SIZE32(chk_length);
4281 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4282 sizeof(*ch), (uint8_t *) & chunk_buf);
4284 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4285 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4286 (contains_init_chunk == 0))) {
4287 sctp_send_abort(m, iphlen, src, dst, sh, 0, NULL,
4288 #if defined(__FreeBSD__)
4289 use_mflowid, mflowid,
4296 * check the inbound datagram to make sure there is not an abort inside it,
4297 * if there is return 1, else return 0.
4300 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
4302 struct sctp_chunkhdr *ch;
4303 struct sctp_init_chunk *init_chk, chunk_buf;
4305 unsigned int chk_length;
4307 offset = iphlen + sizeof(struct sctphdr);
4308 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4309 (uint8_t *) & chunk_buf);
4310 while (ch != NULL) {
4311 chk_length = ntohs(ch->chunk_length);
4312 if (chk_length < sizeof(*ch)) {
4313 /* packet is probably corrupt */
4316 /* we seem to be ok, is it an abort? */
4317 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4318 /* yep, tell them */
4321 if (ch->chunk_type == SCTP_INITIATION) {
4322 /* need to update the Vtag */
4323 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4324 offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4325 if (init_chk != NULL) {
4326 *vtagfill = ntohl(init_chk->init.initiate_tag);
4329 /* Nope, move to the next chunk */
4330 offset += SCTP_SIZE32(chk_length);
4331 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4332 sizeof(*ch), (uint8_t *) & chunk_buf);
4338 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4339 * set (i.e. it's 0) so, create this function to compare link local scopes
4343 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4345 #if defined(__Userspace__)
4346 /*__Userspace__ Returning 1 here always */
4348 #if defined(SCTP_EMBEDDED_V6_SCOPE)
4349 struct sockaddr_in6 a, b;
4355 if (a.sin6_scope_id == 0)
4357 if (sa6_recoverscope(&a)) {
4359 if (in6_recoverscope(&a, &a.sin6_addr, NULL)) {
4360 #endif /* SCTP_KAME */
4361 /* can't get scope, so can't match */
4364 if (b.sin6_scope_id == 0)
4366 if (sa6_recoverscope(&b)) {
4368 if (in6_recoverscope(&b, &b.sin6_addr, NULL)) {
4369 #endif /* SCTP_KAME */
4370 /* can't get scope, so can't match */
4373 if (a.sin6_scope_id != b.sin6_scope_id)
4376 if (addr1->sin6_scope_id != addr2->sin6_scope_id)
4378 #endif /* SCTP_EMBEDDED_V6_SCOPE */
4383 #if defined(SCTP_EMBEDDED_V6_SCOPE)
4385 * returns a sockaddr_in6 with embedded scope recovered and removed
4387 struct sockaddr_in6 *
4388 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4390 /* check and strip embedded scope junk */
4391 if (addr->sin6_family == AF_INET6) {
4392 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4393 if (addr->sin6_scope_id == 0) {
4396 if (!sa6_recoverscope(store)) {
4398 if (!in6_recoverscope(store, &store->sin6_addr,
4400 #endif /* SCTP_KAME */
4401 /* use the recovered scope */
4405 /* else, return the original "to" addr */
4406 in6_clearscope(&addr->sin6_addr);
4412 #endif /* SCTP_EMBEDDED_V6_SCOPE */
4416 * are the two addresses the same? currently a "scopeless" check returns: 1
4420 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4424 if (sa1 == NULL || sa2 == NULL)
4427 /* must be the same family */
4428 if (sa1->sa_family != sa2->sa_family)
4431 switch (sa1->sa_family) {
4435 /* IPv6 addresses */
4436 struct sockaddr_in6 *sin6_1, *sin6_2;
4438 sin6_1 = (struct sockaddr_in6 *)sa1;
4439 sin6_2 = (struct sockaddr_in6 *)sa2;
4440 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4447 /* IPv4 addresses */
4448 struct sockaddr_in *sin_1, *sin_2;
4450 sin_1 = (struct sockaddr_in *)sa1;
4451 sin_2 = (struct sockaddr_in *)sa2;
4452 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4455 #if defined(__Userspace__)
4458 struct sockaddr_conn *sconn_1, *sconn_2;
4460 sconn_1 = (struct sockaddr_conn *)sa1;
4461 sconn_2 = (struct sockaddr_conn *)sa2;
4462 return (sconn_1->sconn_addr == sconn_2->sconn_addr);
4466 /* we don't do these... */
4472 sctp_print_address(struct sockaddr *sa)
4475 #if defined(__FreeBSD__) && __FreeBSD_version >= 700000
4476 char ip6buf[INET6_ADDRSTRLEN];
4480 switch (sa->sa_family) {
4484 struct sockaddr_in6 *sin6;
4486 sin6 = (struct sockaddr_in6 *)sa;
4487 #if defined(__Userspace__)
4488 SCTP_PRINTF("IPv6 address: %x:%x:%x:%x:%x:%x:%x:%x:port:%d scope:%u\n",
4489 ntohs(sin6->sin6_addr.s6_addr16[0]),
4490 ntohs(sin6->sin6_addr.s6_addr16[1]),
4491 ntohs(sin6->sin6_addr.s6_addr16[2]),
4492 ntohs(sin6->sin6_addr.s6_addr16[3]),
4493 ntohs(sin6->sin6_addr.s6_addr16[4]),
4494 ntohs(sin6->sin6_addr.s6_addr16[5]),
4495 ntohs(sin6->sin6_addr.s6_addr16[6]),
4496 ntohs(sin6->sin6_addr.s6_addr16[7]),
4497 ntohs(sin6->sin6_port),
4498 sin6->sin6_scope_id);
4500 #if defined(__FreeBSD__) && __FreeBSD_version >= 700000
4501 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4502 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4503 ntohs(sin6->sin6_port),
4504 sin6->sin6_scope_id);
4506 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4507 ip6_sprintf(&sin6->sin6_addr),
4508 ntohs(sin6->sin6_port),
4509 sin6->sin6_scope_id);
4518 struct sockaddr_in *sin;
4521 sin = (struct sockaddr_in *)sa;
4522 p = (unsigned char *)&sin->sin_addr;
4523 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4524 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4528 #if defined(__Userspace__)
4531 struct sockaddr_conn *sconn;
4533 sconn = (struct sockaddr_conn *)sa;
4534 SCTP_PRINTF("AF_CONN address: %p\n", sconn->sconn_addr);
4545 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4546 struct sctp_inpcb *new_inp,
4547 struct sctp_tcb *stcb,
4551 * go through our old INP and pull off any control structures that
4552 * belong to stcb and move then to the new inp.
4554 struct socket *old_so, *new_so;
4555 struct sctp_queued_to_read *control, *nctl;
4556 struct sctp_readhead tmp_queue;
4560 old_so = old_inp->sctp_socket;
4561 new_so = new_inp->sctp_socket;
4562 TAILQ_INIT(&tmp_queue);
4563 #if defined(__FreeBSD__) && __FreeBSD_version < 700000
4564 SOCKBUF_LOCK(&(old_so->so_rcv));
4566 #if defined(__FreeBSD__) || defined(__APPLE__)
4567 error = sblock(&old_so->so_rcv, waitflags);
4569 #if defined(__FreeBSD__) && __FreeBSD_version < 700000
4570 SOCKBUF_UNLOCK(&(old_so->so_rcv));
4573 /* Gak, can't get sblock, we have a problem.
4574 * data will be left stranded.. and we
4575 * don't dare look at it since the
4576 * other thread may be reading something.
4577 * Oh well, its a screwed up app that does
4578 * a peeloff OR a accept while reading
4579 * from the main socket... actually its
4580 * only the peeloff() case, since I think
4581 * read will fail on a listening socket..
4585 /* lock the socket buffers */
4586 SCTP_INP_READ_LOCK(old_inp);
4587 TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4588 /* Pull off all for out target stcb */
4589 if (control->stcb == stcb) {
4590 /* remove it we want it */
4591 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4592 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4595 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4596 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE,SCTP_BUF_LEN(m));
4598 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4599 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4600 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
4602 m = SCTP_BUF_NEXT(m);
4606 SCTP_INP_READ_UNLOCK(old_inp);
4607 /* Remove the sb-lock on the old socket */
4608 #if defined(__FreeBSD__) && __FreeBSD_version < 700000
4609 SOCKBUF_LOCK(&(old_so->so_rcv));
4611 #if defined(__APPLE__)
4612 sbunlock(&old_so->so_rcv, 1);
4615 #if defined(__FreeBSD__)
4616 sbunlock(&old_so->so_rcv);
4618 #if defined(__FreeBSD__) && __FreeBSD_version < 700000
4619 SOCKBUF_UNLOCK(&(old_so->so_rcv));
4621 /* Now we move them over to the new socket buffer */
4622 SCTP_INP_READ_LOCK(new_inp);
4623 TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4624 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4627 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4628 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4630 sctp_sballoc(stcb, &new_so->so_rcv, m);
4631 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4632 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
4634 m = SCTP_BUF_NEXT(m);
4637 SCTP_INP_READ_UNLOCK(new_inp);
4641 sctp_add_to_readq(struct sctp_inpcb *inp,
4642 struct sctp_tcb *stcb,
4643 struct sctp_queued_to_read *control,
4646 int inp_read_lock_held,
4648 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4654 * Here we must place the control on the end of the socket read
4655 * queue AND increment sb_cc so that select will work properly on
4658 struct mbuf *m, *prev = NULL;
4663 panic("Gak, inp NULL on add_to_readq");
4667 #if defined(__APPLE__)
4669 sctp_lock_assert(SCTP_INP_SO(inp));
4671 sctp_unlock_assert(SCTP_INP_SO(inp));
4674 if (inp_read_lock_held == 0)
4675 SCTP_INP_READ_LOCK(inp);
4676 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4677 sctp_free_remote_addr(control->whoFrom);
4678 if (control->data) {
4679 sctp_m_freem(control->data);
4680 control->data = NULL;
4682 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4683 if (inp_read_lock_held == 0)
4684 SCTP_INP_READ_UNLOCK(inp);
4687 if (!(control->spec_flags & M_NOTIFICATION)) {
4688 atomic_add_int(&inp->total_recvs, 1);
4689 if (!control->do_not_ref_stcb) {
4690 atomic_add_int(&stcb->total_recvs, 1);
4694 control->held_length = 0;
4695 control->length = 0;
4697 if (SCTP_BUF_LEN(m) == 0) {
4698 /* Skip mbufs with NO length */
4701 control->data = sctp_m_free(m);
4704 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4705 m = SCTP_BUF_NEXT(prev);
4708 control->tail_mbuf = prev;
4713 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4714 sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4716 sctp_sballoc(stcb, sb, m);
4717 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4718 sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
4720 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4721 m = SCTP_BUF_NEXT(m);
4724 control->tail_mbuf = prev;
4726 /* Everything got collapsed out?? */
4727 sctp_free_remote_addr(control->whoFrom);
4728 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4729 if (inp_read_lock_held == 0)
4730 SCTP_INP_READ_UNLOCK(inp);
4734 control->end_added = 1;
4736 #if defined(__Userspace__)
4737 if (inp->recv_callback) {
4738 if (inp_read_lock_held == 0)
4739 SCTP_INP_READ_UNLOCK(inp);
4740 if (control->end_added == 1) {
4744 struct sctp_rcvinfo rcv;
4745 union sctp_sockstore addr;
4748 if ((buffer = malloc(control->length)) == NULL) {
4751 so = stcb->sctp_socket;
4752 for (m = control->data; m; m = SCTP_BUF_NEXT(m)) {
4753 sctp_sbfree(control, control->stcb, &so->so_rcv, m);
4755 atomic_add_int(&stcb->asoc.refcnt, 1);
4756 SCTP_TCB_UNLOCK(stcb);
4757 m_copydata(control->data, 0, control->length, buffer);
4758 memset(&rcv, 0, sizeof(struct sctp_rcvinfo));
4759 rcv.rcv_sid = control->sinfo_stream;
4760 rcv.rcv_ssn = control->sinfo_ssn;
4761 rcv.rcv_flags = control->sinfo_flags;
4762 rcv.rcv_ppid = control->sinfo_ppid;
4763 rcv.rcv_tsn = control->sinfo_tsn;
4764 rcv.rcv_cumtsn = control->sinfo_cumtsn;
4765 rcv.rcv_context = control->sinfo_context;
4766 rcv.rcv_assoc_id = control->sinfo_assoc_id;
4767 memset(&addr, 0, sizeof(union sctp_sockstore));
4768 switch (control->whoFrom->ro._l_addr.sa.sa_family) {
4771 addr.sin = control->whoFrom->ro._l_addr.sin;
4776 addr.sin6 = control->whoFrom->ro._l_addr.sin6;
4779 #if defined(__Userspace__)
4781 addr.sconn = control->whoFrom->ro._l_addr.sconn;
4785 addr.sa = control->whoFrom->ro._l_addr.sa;
4789 if (control->spec_flags & M_NOTIFICATION) {
4790 flags |= MSG_NOTIFICATION;
4792 inp->recv_callback(so, addr, buffer, control->length, rcv, flags, inp->ulp_info);
4793 SCTP_TCB_LOCK(stcb);
4794 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4795 sctp_m_freem(control->data);
4796 control->data = NULL;
4797 control->length = 0;
4798 sctp_free_a_readq(stcb, control);
4803 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4804 if (inp_read_lock_held == 0)
4805 SCTP_INP_READ_UNLOCK(inp);
4806 if (inp && inp->sctp_socket) {
4807 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4808 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4810 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4813 so = SCTP_INP_SO(inp);
4816 atomic_add_int(&stcb->asoc.refcnt, 1);
4817 SCTP_TCB_UNLOCK(stcb);
4819 SCTP_SOCKET_LOCK(so, 1);
4821 SCTP_TCB_LOCK(stcb);
4822 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4824 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4825 SCTP_SOCKET_UNLOCK(so, 1);
4830 sctp_sorwakeup(inp, inp->sctp_socket);
4831 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4833 SCTP_SOCKET_UNLOCK(so, 1);
4842 sctp_append_to_readq(struct sctp_inpcb *inp,
4843 struct sctp_tcb *stcb,
4844 struct sctp_queued_to_read *control,
4851 * A partial delivery API event is underway. OR we are appending on
4852 * the reassembly queue.
4854 * If PDAPI this means we need to add m to the end of the data.
4855 * Increase the length in the control AND increment the sb_cc.
4856 * Otherwise sb is NULL and all we need to do is put it at the end
4857 * of the mbuf chain.
4860 struct mbuf *mm, *tail = NULL, *prev = NULL;
4863 SCTP_INP_READ_LOCK(inp);
4865 if (control == NULL) {
4868 SCTP_INP_READ_UNLOCK(inp);
4872 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4873 SCTP_INP_READ_UNLOCK(inp);
4876 if (control->end_added) {
4877 /* huh this one is complete? */
4886 if (SCTP_BUF_LEN(mm) == 0) {
4887 /* Skip mbufs with NO lenght */
4890 m = sctp_m_free(mm);
4893 SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4894 mm = SCTP_BUF_NEXT(prev);
4899 len += SCTP_BUF_LEN(mm);
4901 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4902 sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
4904 sctp_sballoc(stcb, sb, mm);
4905 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4906 sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
4909 mm = SCTP_BUF_NEXT(mm);
4914 /* Really there should always be a prev */
4916 /* Huh nothing left? */
4918 panic("Nothing left to add?");
4925 if (control->tail_mbuf) {
4927 SCTP_BUF_NEXT(control->tail_mbuf) = m;
4928 control->tail_mbuf = tail;
4932 if (control->data != NULL) {
4933 panic("This should NOT happen");
4937 control->tail_mbuf = tail;
4939 atomic_add_int(&control->length, len);
4941 /* message is complete */
4942 if (stcb && (control == stcb->asoc.control_pdapi)) {
4943 stcb->asoc.control_pdapi = NULL;
4945 control->held_length = 0;
4946 control->end_added = 1;
4949 control->do_not_ref_stcb = 1;
4952 * When we are appending in partial delivery, the cum-ack is used
4953 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4954 * is populated in the outbound sinfo structure from the true cumack
4955 * if the association exists...
4957 #if defined(__Userspace__)
4958 if (inp->recv_callback) {
4959 if (control->end_added == 1) {
4962 struct sctp_rcvinfo rcv;
4963 union sctp_sockstore addr;
4966 if ((buffer = malloc(control->length)) == NULL) {
4969 so = stcb->sctp_socket;
4970 for (m = control->data; m; m = SCTP_BUF_NEXT(m)) {
4971 sctp_sbfree(control, control->stcb, &so->so_rcv, m);
4973 atomic_add_int(&stcb->asoc.refcnt, 1);
4974 SCTP_TCB_UNLOCK(stcb);
4975 m_copydata(control->data, 0, control->length, buffer);
4976 memset(&rcv, 0, sizeof(struct sctp_rcvinfo));
4977 rcv.rcv_sid = control->sinfo_stream;
4978 rcv.rcv_ssn = control->sinfo_ssn;
4979 rcv.rcv_flags = control->sinfo_flags;
4980 rcv.rcv_ppid = control->sinfo_ppid;
4981 rcv.rcv_tsn = control->sinfo_tsn;
4982 rcv.rcv_cumtsn = control->sinfo_cumtsn;
4983 rcv.rcv_context = control->sinfo_context;
4984 rcv.rcv_assoc_id = control->sinfo_assoc_id;
4985 memset(&addr, 0, sizeof(union sctp_sockstore));
4986 switch (control->whoFrom->ro._l_addr.sa.sa_family) {
4989 addr.sin = control->whoFrom->ro._l_addr.sin;
4994 addr.sin6 = control->whoFrom->ro._l_addr.sin6;
4997 #if defined(__Userspace__)
4999 addr.sconn = control->whoFrom->ro._l_addr.sconn;
5003 addr.sa = control->whoFrom->ro._l_addr.sa;
5007 if (control->spec_flags & M_NOTIFICATION) {
5008 flags |= MSG_NOTIFICATION;
5010 inp->recv_callback(so, addr, buffer, control->length, rcv, flags, inp->ulp_info);
5011 SCTP_TCB_LOCK(stcb);
5012 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5013 sctp_m_freem(control->data);
5014 control->data = NULL;
5015 control->length = 0;
5016 sctp_free_a_readq(stcb, control);
5019 SCTP_INP_READ_UNLOCK(inp);
5023 control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
5025 SCTP_INP_READ_UNLOCK(inp);
5027 if (inp && inp->sctp_socket) {
5028 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
5029 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
5031 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5034 so = SCTP_INP_SO(inp);
5036 atomic_add_int(&stcb->asoc.refcnt, 1);
5037 SCTP_TCB_UNLOCK(stcb);
5039 SCTP_SOCKET_LOCK(so, 1);
5041 SCTP_TCB_LOCK(stcb);
5042 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5044 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5045 SCTP_SOCKET_UNLOCK(so, 1);
5049 sctp_sorwakeup(inp, inp->sctp_socket);
5050 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5051 SCTP_SOCKET_UNLOCK(so, 1);
5060 /*************HOLD THIS COMMENT FOR PATCH FILE OF
5061 *************ALTERNATE ROUTING CODE
5064 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
5065 *************ALTERNATE ROUTING CODE
5069 sctp_generate_invmanparam(int err)
5071 /* Return a MBUF with a invalid mandatory parameter */
5074 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
5076 struct sctp_paramhdr *ph;
5078 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
5079 ph = mtod(m, struct sctp_paramhdr *);
5080 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5081 ph->param_type = htons(err);
5086 #ifdef SCTP_MBCNT_LOGGING
5088 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
5089 struct sctp_tmit_chunk *tp1, int chk_cnt)
5091 if (tp1->data == NULL) {
5094 asoc->chunks_on_out_queue -= chk_cnt;
5095 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
5096 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
5097 asoc->total_output_queue_size,
5102 if (asoc->total_output_queue_size >= tp1->book_size) {
5103 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
5105 asoc->total_output_queue_size = 0;
5108 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
5109 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
5110 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
5111 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
5113 stcb->sctp_socket->so_snd.sb_cc = 0;
5122 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
5123 uint8_t sent, int so_locked
5124 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5129 struct sctp_stream_out *strq;
5130 struct sctp_tmit_chunk *chk = NULL, *tp2;
5131 struct sctp_stream_queue_pending *sp;
5132 uint16_t stream = 0, seq = 0;
5133 uint8_t foundeom = 0;
5136 int do_wakeup_routine = 0;
5137 #if defined(__APPLE__)
5139 sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
5141 sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
5144 stream = tp1->rec.data.stream_number;
5145 seq = tp1->rec.data.stream_seq;
5147 ret_sz += tp1->book_size;
5148 if (tp1->data != NULL) {
5149 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5150 sctp_flight_size_decrease(tp1);
5151 sctp_total_flight_decrease(stcb, tp1);
5153 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
5154 stcb->asoc.peers_rwnd += tp1->send_size;
5155 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
5157 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
5159 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
5162 sctp_m_freem(tp1->data);
5165 do_wakeup_routine = 1;
5166 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5167 stcb->asoc.sent_queue_cnt_removeable--;
5170 tp1->sent = SCTP_FORWARD_TSN_SKIP;
5171 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
5172 SCTP_DATA_NOT_FRAG) {
5173 /* not frag'ed we ae done */
5176 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5177 /* end of frag, we are done */
5182 * Its a begin or middle piece, we must mark all of
5186 tp1 = TAILQ_NEXT(tp1, sctp_next);
5188 } while (tp1 && notdone);
5189 if (foundeom == 0) {
5191 * The multi-part message was scattered across the send and
5194 TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
5195 if ((tp1->rec.data.stream_number != stream) ||
5196 (tp1->rec.data.stream_seq != seq)) {
5199 /* save to chk in case we have some on stream out
5200 * queue. If so and we have an un-transmitted one
5201 * we don't have to fudge the TSN.
5204 ret_sz += tp1->book_size;
5205 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
5207 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
5209 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
5212 sctp_m_freem(tp1->data);
5215 /* No flight involved here book the size to 0 */
5217 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5220 do_wakeup_routine = 1;
5221 tp1->sent = SCTP_FORWARD_TSN_SKIP;
5222 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
5223 /* on to the sent queue so we can wait for it to be passed by. */
5224 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
5226 stcb->asoc.send_queue_cnt--;
5227 stcb->asoc.sent_queue_cnt++;
5230 if (foundeom == 0) {
5232 * Still no eom found. That means there
5233 * is stuff left on the stream out queue.. yuck.
5235 SCTP_TCB_SEND_LOCK(stcb);
5236 strq = &stcb->asoc.strmout[stream];
5237 sp = TAILQ_FIRST(&strq->outqueue);
5239 sp->discard_rest = 1;
5241 * We may need to put a chunk on the
5242 * queue that holds the TSN that
5243 * would have been sent with the LAST
5247 /* Yep, we have to */
5248 sctp_alloc_a_chunk(stcb, chk);
5250 /* we are hosed. All we can
5251 * do is nothing.. which will
5252 * cause an abort if the peer is
5257 memset(chk, 0, sizeof(*chk));
5258 chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
5259 chk->sent = SCTP_FORWARD_TSN_SKIP;
5260 chk->asoc = &stcb->asoc;
5261 chk->rec.data.stream_seq = strq->next_sequence_send;
5262 chk->rec.data.stream_number = sp->stream;
5263 chk->rec.data.payloadtype = sp->ppid;
5264 chk->rec.data.context = sp->context;
5265 chk->flags = sp->act_flags;
5267 chk->whoTo = sp->net;
5269 chk->whoTo = stcb->asoc.primary_destination;
5270 atomic_add_int(&chk->whoTo->ref_count, 1);
5271 #if defined(__FreeBSD__) || defined(__Panda__)
5272 chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
5274 chk->rec.data.TSN_seq = stcb->asoc.sending_seq++;
5276 stcb->asoc.pr_sctp_cnt++;
5277 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
5278 stcb->asoc.sent_queue_cnt++;
5279 stcb->asoc.pr_sctp_cnt++;
5281 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
5283 strq->next_sequence_send++;
5286 /* Pull any data to free up the SB and
5287 * allow sender to "add more" while we
5288 * will throw away :-)
5290 sctp_free_spbufspace(stcb, &stcb->asoc, sp);
5291 ret_sz += sp->length;
5292 do_wakeup_routine = 1;
5294 sctp_m_freem(sp->data);
5296 sp->tail_mbuf = NULL;
5300 SCTP_TCB_SEND_UNLOCK(stcb);
5302 if (do_wakeup_routine) {
5303 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5306 so = SCTP_INP_SO(stcb->sctp_ep);
5308 atomic_add_int(&stcb->asoc.refcnt, 1);
5309 SCTP_TCB_UNLOCK(stcb);
5310 SCTP_SOCKET_LOCK(so, 1);
5311 SCTP_TCB_LOCK(stcb);
5312 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5313 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5314 /* assoc was freed while we were unlocked */
5315 SCTP_SOCKET_UNLOCK(so, 1);
5320 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
5321 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5323 SCTP_SOCKET_UNLOCK(so, 1);
5331 * checks to see if the given address, sa, is one that is currently known by
5332 * the kernel note: can't distinguish the same address on multiple interfaces
5333 * and doesn't handle multiple addresses with different zone/scope id's note:
5334 * ifa_ifwithaddr() compares the entire sockaddr struct
5337 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
5340 struct sctp_laddr *laddr;
5342 if (holds_lock == 0) {
5343 SCTP_INP_RLOCK(inp);
5346 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5347 if (laddr->ifa == NULL)
5349 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
5352 if (addr->sa_family == AF_INET) {
5353 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5354 laddr->ifa->address.sin.sin_addr.s_addr) {
5356 if (holds_lock == 0) {
5357 SCTP_INP_RUNLOCK(inp);
5359 return (laddr->ifa);
5365 if (addr->sa_family == AF_INET6) {
5366 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5367 &laddr->ifa->address.sin6)) {
5369 if (holds_lock == 0) {
5370 SCTP_INP_RUNLOCK(inp);
5372 return (laddr->ifa);
5377 #if defined(__Userspace__)
5378 if (addr->sa_family == AF_CONN) {
5379 if (((struct sockaddr_conn *)addr)->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
5381 if (holds_lock == 0) {
5382 SCTP_INP_RUNLOCK(inp);
5384 return (laddr->ifa);
5390 if (holds_lock == 0) {
5391 SCTP_INP_RUNLOCK(inp);
5397 sctp_get_ifa_hash_val(struct sockaddr *addr)
5399 switch (addr->sa_family) {
5403 struct sockaddr_in *sin;
5405 sin = (struct sockaddr_in *)addr;
5406 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5412 struct sockaddr_in6 *sin6;
5413 uint32_t hash_of_addr;
5415 sin6 = (struct sockaddr_in6 *)addr;
5416 #if !defined(__Windows__) && !defined(__Userspace_os_FreeBSD) && !defined(__Userspace_os_Darwin) && !defined(__Userspace_os_Windows)
5417 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
5418 sin6->sin6_addr.s6_addr32[1] +
5419 sin6->sin6_addr.s6_addr32[2] +
5420 sin6->sin6_addr.s6_addr32[3]);
5422 hash_of_addr = (((uint32_t *)&sin6->sin6_addr)[0] +
5423 ((uint32_t *)&sin6->sin6_addr)[1] +
5424 ((uint32_t *)&sin6->sin6_addr)[2] +
5425 ((uint32_t *)&sin6->sin6_addr)[3]);
5427 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
5428 return (hash_of_addr);
5431 #if defined(__Userspace__)
5434 struct sockaddr_conn *sconn;
5437 sconn = (struct sockaddr_conn *)addr;
5438 temp = (uintptr_t)sconn->sconn_addr;
5439 return ((uint32_t)(temp ^ (temp >> 16)));
5449 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
5451 struct sctp_ifa *sctp_ifap;
5452 struct sctp_vrf *vrf;
5453 struct sctp_ifalist *hash_head;
5454 uint32_t hash_of_addr;
5456 if (holds_lock == 0)
5457 SCTP_IPI_ADDR_RLOCK();
5459 vrf = sctp_find_vrf(vrf_id);
5462 if (holds_lock == 0)
5463 SCTP_IPI_ADDR_RUNLOCK();
5467 hash_of_addr = sctp_get_ifa_hash_val(addr);
5469 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5470 if (hash_head == NULL) {
5471 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5472 hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5473 (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5474 sctp_print_address(addr);
5475 SCTP_PRINTF("No such bucket for address\n");
5476 if (holds_lock == 0)
5477 SCTP_IPI_ADDR_RUNLOCK();
5481 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5482 if (sctp_ifap == NULL) {
5484 panic("Huh LIST_FOREACH corrupt");
5487 SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
5491 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5494 if (addr->sa_family == AF_INET) {
5495 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5496 sctp_ifap->address.sin.sin_addr.s_addr) {
5498 if (holds_lock == 0)
5499 SCTP_IPI_ADDR_RUNLOCK();
5506 if (addr->sa_family == AF_INET6) {
5507 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5508 &sctp_ifap->address.sin6)) {
5510 if (holds_lock == 0)
5511 SCTP_IPI_ADDR_RUNLOCK();
5517 #if defined(__Userspace__)
5518 if (addr->sa_family == AF_CONN) {
5519 if (((struct sockaddr_conn *)addr)->sconn_addr == sctp_ifap->address.sconn.sconn_addr) {
5521 if (holds_lock == 0)
5522 SCTP_IPI_ADDR_RUNLOCK();
5529 if (holds_lock == 0)
5530 SCTP_IPI_ADDR_RUNLOCK();
5535 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5538 /* User pulled some data, do we need a rwnd update? */
5541 struct socket *so = NULL;
5546 atomic_add_int(&stcb->asoc.refcnt, 1);
5548 if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
5549 SCTP_STATE_SHUTDOWN_RECEIVED |
5550 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5551 /* Pre-check If we are freeing no update */
5554 SCTP_INP_INCR_REF(stcb->sctp_ep);
5555 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5556 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5559 so = stcb->sctp_socket;
5563 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5564 /* Have you have freed enough to look */
5566 /* Yep, its worth a look and the lock overhead */
5568 /* Figure out what the rwnd would be */
5569 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5570 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5571 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5575 if (dif >= rwnd_req) {
5577 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5580 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5582 * One last check before we allow the guy possibly
5583 * to get in. There is a race, where the guy has not
5584 * reached the gate. In that case
5588 SCTP_TCB_LOCK(stcb);
5589 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5590 /* No reports here */
5591 SCTP_TCB_UNLOCK(stcb);
5594 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5595 sctp_send_sack(stcb, SCTP_SO_LOCKED);
5597 sctp_chunk_output(stcb->sctp_ep, stcb,
5598 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5599 /* make sure no timer is running */
5600 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL+SCTP_LOC_6);
5601 SCTP_TCB_UNLOCK(stcb);
5603 /* Update how much we have pending */
5604 stcb->freed_by_sorcv_sincelast = dif;
5607 if (so && r_unlocked && hold_rlock) {
5608 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5611 SCTP_INP_DECR_REF(stcb->sctp_ep);
5613 atomic_add_int(&stcb->asoc.refcnt, -1);
5618 sctp_sorecvmsg(struct socket *so,
5621 struct sockaddr *from,
5624 struct sctp_sndrcvinfo *sinfo,
5628 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5629 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5630 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5631 * On the way out we may send out any combination of:
5632 * MSG_NOTIFICATION MSG_EOR
5635 struct sctp_inpcb *inp = NULL;
5637 int cp_len = 0, error = 0;
5638 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5639 struct mbuf *m = NULL;
5640 struct sctp_tcb *stcb = NULL;
5641 int wakeup_read_socket = 0;
5642 int freecnt_applied = 0;
5643 int out_flags = 0, in_flags = 0;
5644 int block_allowed = 1;
5645 uint32_t freed_so_far = 0;
5646 uint32_t copied_so_far = 0;
5647 int in_eeor_mode = 0;
5648 int no_rcv_needed = 0;
5649 uint32_t rwnd_req = 0;
5650 int hold_sblock = 0;
5653 uint32_t held_length = 0;
5654 #if defined(__FreeBSD__) && __FreeBSD_version >= 700000
5655 int sockbuf_lock = 0;
5659 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5664 in_flags = *msg_flags;
5665 if (in_flags & MSG_PEEK)
5666 SCTP_STAT_INCR(sctps_read_peeks);
5670 #if defined(__APPLE__)
5671 #if defined(APPLE_LEOPARD)
5672 slen = uio->uio_resid;
5674 slen = uio_resid(uio);
5677 slen = uio->uio_resid;
5680 /* Pull in and set up our int flags */
5681 if (in_flags & MSG_OOB) {
5682 /* Out of band's NOT supported */
5683 return (EOPNOTSUPP);
5685 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5686 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5689 if ((in_flags & (MSG_DONTWAIT
5690 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
5694 SCTP_SO_IS_NBIO(so)) {
5697 /* setup the endpoint */
5698 inp = (struct sctp_inpcb *)so->so_pcb;
5700 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5703 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5704 /* Must be at least a MTU's worth */
5705 if (rwnd_req < SCTP_MIN_RWND)
5706 rwnd_req = SCTP_MIN_RWND;
5707 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5708 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5709 #if defined(__APPLE__)
5710 #if defined(APPLE_LEOPARD)
5711 sctp_misc_ints(SCTP_SORECV_ENTER,
5712 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
5714 sctp_misc_ints(SCTP_SORECV_ENTER,
5715 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio_resid(uio));
5718 sctp_misc_ints(SCTP_SORECV_ENTER,
5719 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
5722 #if (defined(__FreeBSD__) && __FreeBSD_version < 700000) || defined(__Userspace__)
5723 SOCKBUF_LOCK(&so->so_rcv);
5726 if (SCTP_BASE_SYSCTL(sctp_logging_level) &SCTP_RECV_RWND_LOGGING_ENABLE) {
5727 #if defined(__APPLE__)
5728 #if defined(APPLE_LEOPARD)
5729 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5730 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
5732 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5733 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio_resid(uio));
5736 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5737 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
5741 #if defined(__APPLE__)
5742 error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
5745 #if defined(__FreeBSD__)
5746 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5749 goto release_unlocked;
5751 #if defined(__FreeBSD__) && __FreeBSD_version >= 700000
5755 #if (defined(__FreeBSD__) && __FreeBSD_version < 700000) || defined(__Userspace__)
5756 if (hold_sblock == 0) {
5757 SOCKBUF_LOCK(&so->so_rcv);
5761 #if defined(__APPLE__)
5762 sbunlock(&so->so_rcv, 1);
5765 #if defined(__FreeBSD__) && __FreeBSD_version < 700000
5766 sbunlock(&so->so_rcv);
5770 if (hold_sblock == 0) {
5771 SOCKBUF_LOCK(&so->so_rcv);
5774 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5775 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5778 #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__)
5779 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5781 if ((so->so_state & SS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5784 error = so->so_error;
5785 if ((in_flags & MSG_PEEK) == 0)
5789 if (so->so_rcv.sb_cc == 0) {
5796 if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5797 /* we need to wait for data */
5798 if ((so->so_rcv.sb_cc == 0) &&
5799 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5800 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5801 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5802 /* For active open side clear flags for re-use
5803 * passive open is blocked by connect.
5805 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5806 /* You were aborted, passive side always hits here */
5807 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5810 so->so_state &= ~(SS_ISCONNECTING |
5811 SS_ISDISCONNECTING |
5815 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5816 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5823 error = sbwait(&so->so_rcv);
5828 goto restart_nosblocks;
5829 } else if (so->so_rcv.sb_cc == 0) {
5831 error = so->so_error;
5832 if ((in_flags & MSG_PEEK) == 0)
5835 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5836 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5837 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5838 /* For active open side clear flags for re-use
5839 * passive open is blocked by connect.
5841 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5842 /* You were aborted, passive side always hits here */
5843 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5846 so->so_state &= ~(SS_ISCONNECTING |
5847 SS_ISDISCONNECTING |
5851 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5852 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5859 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5860 error = EWOULDBLOCK;
5864 if (hold_sblock == 1) {
5865 SOCKBUF_UNLOCK(&so->so_rcv);
5868 #if defined(__APPLE__)
5869 error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
5871 #if defined(__FreeBSD__) && __FreeBSD_version < 700000
5872 error = sblock(&so->so_rcv, (block_allowed ? M_WAITOK : 0));
5874 /* we possibly have data we can read */
5875 /*sa_ignore FREED_MEMORY*/
5876 control = TAILQ_FIRST(&inp->read_queue);
5877 if (control == NULL) {
5878 /* This could be happening since
5879 * the appender did the increment but as not
5880 * yet did the tailq insert onto the read_queue
5882 if (hold_rlock == 0) {
5883 SCTP_INP_READ_LOCK(inp);
5885 control = TAILQ_FIRST(&inp->read_queue);
5886 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5888 panic("Huh, its non zero and nothing on control?");
5890 so->so_rcv.sb_cc = 0;
5892 SCTP_INP_READ_UNLOCK(inp);
5897 if ((control->length == 0) &&
5898 (control->do_not_ref_stcb)) {
5899 /* Clean up code for freeing assoc that left behind a pdapi..
5900 * maybe a peer in EEOR that just closed after sending and
5901 * never indicated a EOR.
5903 if (hold_rlock == 0) {
5905 SCTP_INP_READ_LOCK(inp);
5907 control->held_length = 0;
5908 if (control->data) {
5909 /* Hmm there is data here .. fix */
5912 m_tmp = control->data;
5914 cnt += SCTP_BUF_LEN(m_tmp);
5915 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5916 control->tail_mbuf = m_tmp;
5917 control->end_added = 1;
5919 m_tmp = SCTP_BUF_NEXT(m_tmp);
5921 control->length = cnt;
5924 TAILQ_REMOVE(&inp->read_queue, control, next);
5925 /* Add back any hiddend data */
5926 sctp_free_remote_addr(control->whoFrom);
5927 sctp_free_a_readq(stcb, control);
5931 SCTP_INP_READ_UNLOCK(inp);
5935 if ((control->length == 0) &&
5936 (control->end_added == 1)) {
5937 /* Do we also need to check for (control->pdapi_aborted == 1)? */
5938 if (hold_rlock == 0) {
5940 SCTP_INP_READ_LOCK(inp);
5942 TAILQ_REMOVE(&inp->read_queue, control, next);
5943 if (control->data) {
5945 panic("control->data not null but control->length == 0");
5947 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5948 sctp_m_freem(control->data);
5949 control->data = NULL;
5952 if (control->aux_data) {
5953 sctp_m_free (control->aux_data);
5954 control->aux_data = NULL;
5956 sctp_free_remote_addr(control->whoFrom);
5957 sctp_free_a_readq(stcb, control);
5960 SCTP_INP_READ_UNLOCK(inp);
5964 if (control->length == 0) {
5965 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5967 /* find a more suitable one then this */
5968 ctl = TAILQ_NEXT(control, next);
5970 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5972 (ctl->spec_flags & M_NOTIFICATION) ||
5973 ((ctl->do_not_ref_stcb == 0) &&
5974 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5977 * If we have a different TCB next, and there is data
5978 * present. If we have already taken some (pdapi), OR we can
5979 * ref the tcb and no delivery as started on this stream, we
5980 * take it. Note we allow a notification on a different
5981 * assoc to be delivered..
5985 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5987 ((ctl->some_taken) ||
5988 ((ctl->do_not_ref_stcb == 0) &&
5989 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5990 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5992 * If we have the same tcb, and there is data present, and we
5993 * have the strm interleave feature present. Then if we have
5994 * taken some (pdapi) or we can refer to tht tcb AND we have
5995 * not started a delivery for this stream, we can take it.
5996 * Note we do NOT allow a notificaiton on the same assoc to
6002 ctl = TAILQ_NEXT(ctl, next);
6006 * if we reach here, not suitable replacement is available
6007 * <or> fragment interleave is NOT on. So stuff the sb_cc
6008 * into the our held count, and its time to sleep again.
6010 held_length = so->so_rcv.sb_cc;
6011 control->held_length = so->so_rcv.sb_cc;
6014 /* Clear the held length since there is something to read */
6015 control->held_length = 0;
6017 SCTP_INP_READ_UNLOCK(inp);
6022 * If we reach here, control has a some data for us to read off.
6023 * Note that stcb COULD be NULL.
6025 control->some_taken++;
6027 SOCKBUF_UNLOCK(&so->so_rcv);
6030 stcb = control->stcb;
6032 if ((control->do_not_ref_stcb == 0) &&
6033 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
6034 if (freecnt_applied == 0)
6036 } else if (control->do_not_ref_stcb == 0) {
6037 /* you can't free it on me please */
6039 * The lock on the socket buffer protects us so the
6040 * free code will stop. But since we used the socketbuf
6041 * lock and the sender uses the tcb_lock to increment,
6042 * we need to use the atomic add to the refcnt
6044 if (freecnt_applied) {
6046 panic("refcnt already incremented");
6048 SCTP_PRINTF("refcnt already incremented?\n");
6051 atomic_add_int(&stcb->asoc.refcnt, 1);
6052 freecnt_applied = 1;
6055 * Setup to remember how much we have not yet told
6056 * the peer our rwnd has opened up. Note we grab
6057 * the value from the tcb from last time.
6058 * Note too that sack sending clears this when a sack
6059 * is sent, which is fine. Once we hit the rwnd_req,
6060 * we then will go to the sctp_user_rcvd() that will
6061 * not lock until it KNOWs it MUST send a WUP-SACK.
6063 freed_so_far = stcb->freed_by_sorcv_sincelast;
6064 stcb->freed_by_sorcv_sincelast = 0;
6068 ((control->spec_flags & M_NOTIFICATION) == 0) &&
6069 control->do_not_ref_stcb == 0) {
6070 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
6073 /* First lets get off the sinfo and sockaddr info */
6074 if ((sinfo) && filling_sinfo) {
6075 memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
6076 nxt = TAILQ_NEXT(control, next);
6077 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6078 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
6079 struct sctp_extrcvinfo *s_extra;
6080 s_extra = (struct sctp_extrcvinfo *)sinfo;
6083 s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
6084 if (nxt->sinfo_flags & SCTP_UNORDERED) {
6085 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
6087 if (nxt->spec_flags & M_NOTIFICATION) {
6088 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
6090 s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
6091 s_extra->sreinfo_next_length = nxt->length;
6092 s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
6093 s_extra->sreinfo_next_stream = nxt->sinfo_stream;
6094 if (nxt->tail_mbuf != NULL) {
6095 if (nxt->end_added) {
6096 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
6100 /* we explicitly 0 this, since the memcpy got
6101 * some other things beyond the older sinfo_
6102 * that is on the control's structure :-D
6105 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
6106 s_extra->sreinfo_next_aid = 0;
6107 s_extra->sreinfo_next_length = 0;
6108 s_extra->sreinfo_next_ppid = 0;
6109 s_extra->sreinfo_next_stream = 0;
6113 * update off the real current cum-ack, if we have an stcb.
6115 if ((control->do_not_ref_stcb == 0) && stcb)
6116 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
6118 * mask off the high bits, we keep the actual chunk bits in
6121 sinfo->sinfo_flags &= 0x00ff;
6122 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
6123 sinfo->sinfo_flags |= SCTP_UNORDERED;
6126 #ifdef SCTP_ASOCLOG_OF_TSNS
6128 int index, newindex;
6129 struct sctp_pcbtsn_rlog *entry;
6131 index = inp->readlog_index;
6132 newindex = index + 1;
6133 if (newindex >= SCTP_READ_LOG_SIZE) {
6136 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
6137 entry = &inp->readlog[index];
6138 entry->vtag = control->sinfo_assoc_id;
6139 entry->strm = control->sinfo_stream;
6140 entry->seq = control->sinfo_ssn;
6141 entry->sz = control->length;
6142 entry->flgs = control->sinfo_flags;
6145 if (fromlen && from) {
6147 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sa.sa_len);
6149 switch (control->whoFrom->ro._l_addr.sa.sa_family) {
6153 cp_len = min((size_t)fromlen, sizeof(struct sockaddr_in6));
6155 ((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
6161 cp_len = min((size_t)fromlen, sizeof(struct sockaddr_in));
6163 ((struct sockaddr_in *)from)->sin_port = control->port_from;
6166 #if defined(__Userspace__)
6169 cp_len = min((size_t)fromlen, sizeof(struct sockaddr_conn));
6171 ((struct sockaddr_conn *)from)->sconn_port = control->port_from;
6176 cp_len = min((size_t)fromlen, sizeof(struct sockaddr));
6180 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
6182 #if defined(INET) && defined(INET6)
6183 if ((sctp_is_feature_on(inp,SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
6184 (from->sa_family == AF_INET) &&
6185 ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
6186 struct sockaddr_in *sin;
6187 struct sockaddr_in6 sin6;
6189 sin = (struct sockaddr_in *)from;
6190 bzero(&sin6, sizeof(sin6));
6191 sin6.sin6_family = AF_INET6;
6192 #ifdef HAVE_SIN6_LEN
6193 sin6.sin6_len = sizeof(struct sockaddr_in6);
6195 #if defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_Darwin) || defined(__Userspace_os_Windows)
6196 ((uint32_t *)&sin6.sin6_addr)[2] = htonl(0xffff);
6197 bcopy(&sin->sin_addr,
6198 &(((uint32_t *)&sin6.sin6_addr)[3]),
6200 #elif defined(__Windows__)
6201 ((uint32_t *)&sin6.sin6_addr)[2] = htonl(0xffff);
6202 bcopy(&sin->sin_addr,
6203 &((uint32_t *)&sin6.sin6_addr)[3],
6206 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
6207 bcopy(&sin->sin_addr,
6208 &sin6.sin6_addr.s6_addr32[3],
6209 sizeof(sin6.sin6_addr.s6_addr32[3]));
6211 sin6.sin6_port = sin->sin_port;
6212 memcpy(from, &sin6, sizeof(struct sockaddr_in6));
6215 #if defined(SCTP_EMBEDDED_V6_SCOPE)
6218 struct sockaddr_in6 lsa6, *from6;
6220 from6 = (struct sockaddr_in6 *)from;
6221 sctp_recover_scope_mac(from6, (&lsa6));
6226 /* now copy out what data we can */
6228 /* copy out each mbuf in the chain up to length */
6232 /* Move out all we can */
6233 #if defined(__APPLE__)
6234 #if defined(APPLE_LEOPARD)
6235 cp_len = (int)uio->uio_resid;
6237 cp_len = (int)uio_resid(uio);
6240 cp_len = (int)uio->uio_resid;
6242 my_len = (int)SCTP_BUF_LEN(m);
6243 if (cp_len > my_len) {
6244 /* not enough in this buf */
6248 SCTP_INP_READ_UNLOCK(inp);
6251 #if defined(__APPLE__)
6252 SCTP_SOCKET_UNLOCK(so, 0);
6255 error = uiomove(mtod(m, char *), cp_len, uio);
6256 #if defined(__APPLE__)
6257 SCTP_SOCKET_LOCK(so, 0);
6260 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
6264 if ((control->do_not_ref_stcb == 0) && stcb &&
6265 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6269 /* error we are out of here */
6272 if ((SCTP_BUF_NEXT(m) == NULL) &&
6273 (cp_len >= SCTP_BUF_LEN(m)) &&
6274 ((control->end_added == 0) ||
6275 (control->end_added &&
6276 (TAILQ_NEXT(control, next) == NULL)))
6278 SCTP_INP_READ_LOCK(inp);
6281 if (cp_len == SCTP_BUF_LEN(m)) {
6282 if ((SCTP_BUF_NEXT(m)== NULL) &&
6283 (control->end_added)) {
6284 out_flags |= MSG_EOR;
6285 if ((control->do_not_ref_stcb == 0) &&
6286 (control->stcb != NULL) &&
6287 ((control->spec_flags & M_NOTIFICATION) == 0))
6288 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6290 if (control->spec_flags & M_NOTIFICATION) {
6291 out_flags |= MSG_NOTIFICATION;
6293 /* we ate up the mbuf */
6294 if (in_flags & MSG_PEEK) {
6296 m = SCTP_BUF_NEXT(m);
6297 copied_so_far += cp_len;
6299 /* dispose of the mbuf */
6300 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6301 sctp_sblog(&so->so_rcv,
6302 control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
6304 sctp_sbfree(control, stcb, &so->so_rcv, m);
6305 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6306 sctp_sblog(&so->so_rcv,
6307 control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
6309 copied_so_far += cp_len;
6310 freed_so_far += cp_len;
6311 freed_so_far += MSIZE;
6312 atomic_subtract_int(&control->length, cp_len);
6313 control->data = sctp_m_free(m);
6315 /* been through it all, must hold sb lock ok to null tail */
6316 if (control->data == NULL) {
6318 #if !defined(__APPLE__)
6319 if ((control->end_added == 0) ||
6320 (TAILQ_NEXT(control, next) == NULL)) {
6321 /* If the end is not added, OR the
6322 * next is NOT null we MUST have the lock.
6324 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
6325 panic("Hmm we don't own the lock?");
6330 control->tail_mbuf = NULL;
6332 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
6333 panic("end_added, nothing left and no MSG_EOR");
6339 /* Do we need to trim the mbuf? */
6340 if (control->spec_flags & M_NOTIFICATION) {
6341 out_flags |= MSG_NOTIFICATION;
6343 if ((in_flags & MSG_PEEK) == 0) {
6344 SCTP_BUF_RESV_UF(m, cp_len);
6345 SCTP_BUF_LEN(m) -= cp_len;
6346 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6347 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, cp_len);
6349 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
6350 if ((control->do_not_ref_stcb == 0) &&
6352 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
6354 copied_so_far += cp_len;
6355 freed_so_far += cp_len;
6356 freed_so_far += MSIZE;
6357 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6358 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb?NULL:stcb,
6359 SCTP_LOG_SBRESULT, 0);
6361 atomic_subtract_int(&control->length, cp_len);
6363 copied_so_far += cp_len;
6366 #if defined(__APPLE__)
6367 #if defined(APPLE_LEOPARD)
6368 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
6370 if ((out_flags & MSG_EOR) || (uio_resid(uio) == 0)) {
6373 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
6377 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6378 (control->do_not_ref_stcb == 0) &&
6379 (freed_so_far >= rwnd_req)) {
6380 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6382 } /* end while(m) */
6384 * At this point we have looked at it all and we either have
6385 * a MSG_EOR/or read all the user wants... <OR>
6386 * control->length == 0.
6388 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
6389 /* we are done with this control */
6390 if (control->length == 0) {
6391 if (control->data) {
6393 panic("control->data not null at read eor?");
6395 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
6396 sctp_m_freem(control->data);
6397 control->data = NULL;
6401 if (TAILQ_NEXT(control, next) == NULL) {
6402 /* If we don't have a next we need a
6403 * lock, if there is a next interrupt
6404 * is filling ahead of us and we don't
6405 * need a lock to remove this guy
6406 * (which is the head of the queue).
6408 if (hold_rlock == 0) {
6409 SCTP_INP_READ_LOCK(inp);
6413 TAILQ_REMOVE(&inp->read_queue, control, next);
6414 /* Add back any hiddend data */
6415 if (control->held_length) {
6417 control->held_length = 0;
6418 wakeup_read_socket = 1;
6420 if (control->aux_data) {
6421 sctp_m_free (control->aux_data);
6422 control->aux_data = NULL;
6424 no_rcv_needed = control->do_not_ref_stcb;
6425 sctp_free_remote_addr(control->whoFrom);
6426 control->data = NULL;
6427 sctp_free_a_readq(stcb, control);
6429 if ((freed_so_far >= rwnd_req) &&
6430 (no_rcv_needed == 0))
6431 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6435 * The user did not read all of this
6436 * message, turn off the returned MSG_EOR
6437 * since we are leaving more behind on the
6441 if (control->end_added &&
6442 (control->data == NULL) &&
6443 (control->tail_mbuf == NULL)) {
6444 panic("Gak, control->length is corrupt?");
6447 no_rcv_needed = control->do_not_ref_stcb;
6448 out_flags &= ~MSG_EOR;
6451 if (out_flags & MSG_EOR) {
6454 #if defined(__APPLE__)
6455 #if defined(APPLE_LEOPARD)
6456 if ((uio->uio_resid == 0) ||
6458 if ((uio_resid(uio) == 0) ||
6461 if ((uio->uio_resid == 0) ||
6463 ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))
6468 * If I hit here the receiver wants more and this message is
6469 * NOT done (pd-api). So two questions. Can we block? if not
6470 * we are done. Did the user NOT set MSG_WAITALL?
6472 if (block_allowed == 0) {
6476 * We need to wait for more data a few things: - We don't
6477 * sbunlock() so we don't get someone else reading. - We
6478 * must be sure to account for the case where what is added
6479 * is NOT to our control when we wakeup.
6482 /* Do we need to tell the transport a rwnd update might be
6483 * needed before we go to sleep?
6485 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6486 ((freed_so_far >= rwnd_req) &&
6487 (control->do_not_ref_stcb == 0) &&
6488 (no_rcv_needed == 0))) {
6489 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6492 #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__)
6493 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
6497 if (so->so_state & SS_CANTRCVMORE) {
6502 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
6505 if (hold_rlock == 1) {
6506 SCTP_INP_READ_UNLOCK(inp);
6509 if (hold_sblock == 0) {
6510 SOCKBUF_LOCK(&so->so_rcv);
6513 #if defined(__APPLE__)
6514 sbunlock(&so->so_rcv, 1);
6516 if ((copied_so_far) && (control->length == 0) &&
6517 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
6520 if (so->so_rcv.sb_cc <= control->held_length) {
6521 error = sbwait(&so->so_rcv);
6523 #if defined(__FreeBSD__)
6526 goto release_unlocked;
6529 control->held_length = 0;
6531 #if defined(__APPLE__)
6532 error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
6535 SOCKBUF_UNLOCK(&so->so_rcv);
6538 if (control->length == 0) {
6539 /* still nothing here */
6540 if (control->end_added == 1) {
6541 /* he aborted, or is done i.e.did a shutdown */
6542 out_flags |= MSG_EOR;
6543 if (control->pdapi_aborted) {
6544 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6545 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6547 out_flags |= MSG_TRUNC;
6549 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6550 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6552 goto done_with_control;
6554 if (so->so_rcv.sb_cc > held_length) {
6555 control->held_length = so->so_rcv.sb_cc;
6558 goto wait_some_more;
6559 } else if (control->data == NULL) {
6560 /* we must re-sync since data
6561 * is probably being added
6563 SCTP_INP_READ_LOCK(inp);
6564 if ((control->length > 0) && (control->data == NULL)) {
6565 /* big trouble.. we have the lock and its corrupt? */
6567 panic ("Impossible data==NULL length !=0");
6569 out_flags |= MSG_EOR;
6570 out_flags |= MSG_TRUNC;
6571 control->length = 0;
6572 SCTP_INP_READ_UNLOCK(inp);
6573 goto done_with_control;
6575 SCTP_INP_READ_UNLOCK(inp);
6576 /* We will fall around to get more data */
6581 * Give caller back the mbuf chain,
6582 * store in uio_resid the length
6584 wakeup_read_socket = 0;
6585 if ((control->end_added == 0) ||
6586 (TAILQ_NEXT(control, next) == NULL)) {
6587 /* Need to get rlock */
6588 if (hold_rlock == 0) {
6589 SCTP_INP_READ_LOCK(inp);
6593 if (control->end_added) {
6594 out_flags |= MSG_EOR;
6595 if ((control->do_not_ref_stcb == 0) &&
6596 (control->stcb != NULL) &&
6597 ((control->spec_flags & M_NOTIFICATION) == 0))
6598 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6600 if (control->spec_flags & M_NOTIFICATION) {
6601 out_flags |= MSG_NOTIFICATION;
6603 #if defined(__APPLE__)
6604 #if defined(APPLE_LEOPARD)
6605 uio->uio_resid = control->length;
6607 uio_setresid(uio, control->length);
6610 uio->uio_resid = control->length;
6612 *mp = control->data;
6615 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6616 sctp_sblog(&so->so_rcv,
6617 control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
6619 sctp_sbfree(control, stcb, &so->so_rcv, m);
6620 freed_so_far += SCTP_BUF_LEN(m);
6621 freed_so_far += MSIZE;
6622 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6623 sctp_sblog(&so->so_rcv,
6624 control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
6626 m = SCTP_BUF_NEXT(m);
6628 control->data = control->tail_mbuf = NULL;
6629 control->length = 0;
6630 if (out_flags & MSG_EOR) {
6631 /* Done with this control */
6632 goto done_with_control;
6636 if (hold_rlock == 1) {
6637 SCTP_INP_READ_UNLOCK(inp);
6640 #if (defined(__FreeBSD__) && __FreeBSD_version < 700000) || defined(__Userspace__)
6641 if (hold_sblock == 0) {
6642 SOCKBUF_LOCK(&so->so_rcv);
6646 if (hold_sblock == 1) {
6647 SOCKBUF_UNLOCK(&so->so_rcv);
6651 #if defined(__APPLE__)
6652 sbunlock(&so->so_rcv, 1);
6655 #if defined(__FreeBSD__)
6656 sbunlock(&so->so_rcv);
6657 #if defined(__FreeBSD__) && __FreeBSD_version >= 700000
6664 SOCKBUF_UNLOCK(&so->so_rcv);
6667 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6668 if ((freed_so_far >= rwnd_req) &&
6669 (control && (control->do_not_ref_stcb == 0)) &&
6670 (no_rcv_needed == 0))
6671 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6675 *msg_flags = out_flags;
6677 if (((out_flags & MSG_EOR) == 0) &&
6678 ((in_flags & MSG_PEEK) == 0) &&
6680 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6681 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
6682 struct sctp_extrcvinfo *s_extra;
6683 s_extra = (struct sctp_extrcvinfo *)sinfo;
6684 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
6686 if (hold_rlock == 1) {
6687 SCTP_INP_READ_UNLOCK(inp);
6690 SOCKBUF_UNLOCK(&so->so_rcv);
6692 #if defined(__FreeBSD__) && __FreeBSD_version >= 700000
6694 sbunlock(&so->so_rcv);
6698 if (freecnt_applied) {
6700 * The lock on the socket buffer protects us so the free
6701 * code will stop. But since we used the socketbuf lock and
6702 * the sender uses the tcb_lock to increment, we need to use
6703 * the atomic add to the refcnt.
6707 panic("stcb for refcnt has gone NULL?");
6713 atomic_add_int(&stcb->asoc.refcnt, -1);
6714 /* Save the value back for next time */
6715 stcb->freed_by_sorcv_sincelast = freed_so_far;
6717 if (SCTP_BASE_SYSCTL(sctp_logging_level) &SCTP_RECV_RWND_LOGGING_ENABLE) {
6719 sctp_misc_ints(SCTP_SORECV_DONE,
6721 #if defined(__APPLE__)
6722 #if defined(APPLE_LEOPARD)
6723 ((uio) ? (slen - uio->uio_resid) : slen),
6725 ((uio) ? (slen - uio_resid(uio)) : slen),
6728 ((uio) ? (slen - uio->uio_resid) : slen),
6733 sctp_misc_ints(SCTP_SORECV_DONE,
6735 #if defined(__APPLE__)
6736 #if defined(APPLE_LEOPARD)
6737 ((uio) ? (slen - uio->uio_resid) : slen),
6739 ((uio) ? (slen - uio_resid(uio)) : slen),
6742 ((uio) ? (slen - uio->uio_resid) : slen),
6749 if (wakeup_read_socket) {
6750 sctp_sorwakeup(inp, so);
6756 #ifdef SCTP_MBUF_LOGGING
6758 sctp_m_free(struct mbuf *m)
6760 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6761 if (SCTP_BUF_IS_EXTENDED(m)) {
6762 sctp_log_mb(m, SCTP_MBUF_IFREE);
6768 void sctp_m_freem(struct mbuf *mb)
6771 mb = sctp_m_free(mb);
6777 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6779 /* Given a local address. For all associations
6780 * that holds the address, request a peer-set-primary.
6782 struct sctp_ifa *ifa;
6783 struct sctp_laddr *wi;
6785 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6787 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6788 return (EADDRNOTAVAIL);
6790 /* Now that we have the ifa we must awaken the
6791 * iterator with this message.
6793 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6795 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6798 /* Now incr the count and int wi structure */
6799 SCTP_INCR_LADDR_COUNT();
6800 bzero(wi, sizeof(*wi));
6801 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6803 wi->action = SCTP_SET_PRIM_ADDR;
6804 atomic_add_int(&ifa->refcount, 1);
6806 /* Now add it to the work queue */
6807 SCTP_WQ_ADDR_LOCK();
6809 * Should this really be a tailq? As it is we will process the
6812 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6813 SCTP_WQ_ADDR_UNLOCK();
6814 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6815 (struct sctp_inpcb *)NULL,
6816 (struct sctp_tcb *)NULL,
6817 (struct sctp_nets *)NULL);
6821 #if defined(__Userspace__)
6822 /* no sctp_soreceive for __Userspace__ now */
6825 #if !defined(__Userspace__)
6827 sctp_soreceive( struct socket *so,
6828 struct sockaddr **psa,
6831 struct mbuf **controlp,
6835 uint8_t sockbuf[256];
6836 struct sockaddr *from;
6837 struct sctp_extrcvinfo sinfo;
6838 int filling_sinfo = 1;
6839 struct sctp_inpcb *inp;
6841 inp = (struct sctp_inpcb *)so->so_pcb;
6842 /* pickup the assoc we are reading from */
6844 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6847 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6848 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6849 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6850 (controlp == NULL)) {
6851 /* user does not want the sndrcv ctl */
6855 from = (struct sockaddr *)sockbuf;
6856 fromlen = sizeof(sockbuf);
6865 #if defined(__APPLE__)
6866 SCTP_SOCKET_LOCK(so, 1);
6868 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6869 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6870 if ((controlp) && (filling_sinfo)) {
6871 /* copy back the sinfo in a CMSG format */
6873 *controlp = sctp_build_ctl_nchunk(inp,
6874 (struct sctp_sndrcvinfo *)&sinfo);
6879 /* copy back the address info */
6881 if (from && from->sa_len) {
6885 #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__)
6886 *psa = sodupsockaddr(from, M_NOWAIT);
6888 *psa = dup_sockaddr(from, mp0 == 0);
6894 #if defined(__APPLE__)
6895 SCTP_SOCKET_UNLOCK(so, 1);
6901 #if (defined(__FreeBSD__) && __FreeBSD_version < 603000) || defined(__Windows__)
6903 * General routine to allocate a hash table with control of memory flags.
6904 * is in 7.0 and beyond for sure :-)
6907 sctp_hashinit_flags(int elements, struct malloc_type *type,
6908 u_long *hashmask, int flags)
6911 LIST_HEAD(generic, generic) *hashtbl;
6915 if (elements <= 0) {
6917 panic("hashinit: bad elements");
6919 SCTP_PRINTF("hashinit: bad elements?");
6923 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
6926 if (flags & HASH_WAITOK)
6927 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
6928 else if (flags & HASH_NOWAIT)
6929 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_NOWAIT);
6932 panic("flag incorrect in hashinit_flags");
6939 if (hashtbl == NULL)
6942 for (i = 0; i < hashsize; i++)
6943 LIST_INIT(&hashtbl[i]);
6944 *hashmask = hashsize - 1;
6949 #else /* __Userspace__ ifdef above sctp_soreceive */
6951 * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland.
6952 * NOTE: We don't want multiple definitions here. So sctp_hashinit_flags() above for
6953 *__FreeBSD__ must be excluded.
6958 sctp_hashinit_flags(int elements, struct malloc_type *type,
6959 u_long *hashmask, int flags)
6962 LIST_HEAD(generic, generic) *hashtbl;
6965 if (elements <= 0) {
6966 SCTP_PRINTF("hashinit: bad elements?");
6973 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
6976 /*cannot use MALLOC here because it has to be declared or defined
6977 using MALLOC_DECLARE or MALLOC_DEFINE first. */
6978 if (flags & HASH_WAITOK)
6979 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl));
6980 else if (flags & HASH_NOWAIT)
6981 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl));
6984 SCTP_PRINTF("flag incorrect in hashinit_flags");
6990 if (hashtbl == NULL)
6993 for (i = 0; i < hashsize; i++)
6994 LIST_INIT(&hashtbl[i]);
6995 *hashmask = hashsize - 1;
7001 sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
7003 LIST_HEAD(generic, generic) *hashtbl, *hp;
7006 for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
7007 if (!LIST_EMPTY(hp)) {
7008 SCTP_PRINTF("hashdestroy: hash not empty");
7011 FREE(hashtbl, type);
7016 sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
7018 LIST_HEAD(generic, generic) *hashtbl/*, *hp*/;
7020 LIST_ENTRY(type) *start, *temp;
7023 /* Apparently temp is not dynamically allocated, so attempts to
7024 free it results in error.
7025 for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
7026 if (!LIST_EMPTY(hp)) {
7027 start = LIST_FIRST(hp);
7028 while (start != NULL) {
7030 start = start->le_next;
7031 SCTP_PRINTF("%s: %p \n", __func__, (void *)temp);
7036 FREE(hashtbl, type);
7044 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
7045 int totaddr, int *error)
7049 struct sctp_inpcb *inp;
7050 struct sockaddr *sa;
7053 struct sockaddr_in *sin;
7056 struct sockaddr_in6 *sin6;
7060 inp = stcb->sctp_ep;
7062 for (i = 0; i < totaddr; i++) {
7063 switch (sa->sa_family) {
7066 incr = sizeof(struct sockaddr_in);
7067 sin = (struct sockaddr_in *)sa;
7068 if ((sin->sin_addr.s_addr == INADDR_ANY) ||
7069 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
7070 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
7071 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7072 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_7);
7076 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
7077 /* assoc gone no un-lock */
7078 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
7079 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_7);
7088 incr = sizeof(struct sockaddr_in6);
7089 sin6 = (struct sockaddr_in6 *)sa;
7090 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
7091 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
7092 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7093 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_8);
7097 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
7098 /* assoc gone no un-lock */
7099 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
7100 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_8);
7107 #if defined(__Userspace__)
7109 incr = sizeof(struct sockaddr_in6);
7110 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
7111 /* assoc gone no un-lock */
7112 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
7113 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_8);
7123 sa = (struct sockaddr *)((caddr_t)sa + incr);
7130 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
7131 int *totaddr, int *num_v4, int *num_v6, int *error,
7132 int limit, int *bad_addr)
7134 struct sockaddr *sa;
7135 struct sctp_tcb *stcb = NULL;
7140 *error = *num_v6 = *num_v4 = 0;
7141 /* account and validate addresses */
7142 for (i = 0; i < (size_t)*totaddr; i++) {
7143 switch (sa->sa_family) {
7147 incr = sizeof(struct sockaddr_in);
7149 if (sa->sa_len != incr) {
7150 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7161 struct sockaddr_in6 *sin6;
7163 sin6 = (struct sockaddr_in6 *)sa;
7164 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7165 /* Must be non-mapped for connectx */
7166 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7172 incr = sizeof(struct sockaddr_in6);
7174 if (sa->sa_len != incr) {
7175 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7189 if (i == (size_t)*totaddr) {
7192 SCTP_INP_INCR_REF(inp);
7193 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
7195 /* Already have or am bring up an association */
7198 SCTP_INP_DECR_REF(inp);
7200 if ((at + incr) > (size_t)limit) {
7204 sa = (struct sockaddr *)((caddr_t)sa + incr);
7206 return ((struct sctp_tcb *)NULL);
7210 * sctp_bindx(ADD) for one address.
7211 * assumes all arguments are valid/checked by caller.
7214 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
7215 struct sockaddr *sa, sctp_assoc_t assoc_id,
7216 uint32_t vrf_id, int *error, void *p)
7218 struct sockaddr *addr_touse;
7220 struct sockaddr_in sin;
7226 /* see if we're bound all already! */
7227 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7228 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7233 /* Is the VRF one we have */
7234 for (i = 0; i < inp->num_vrfs; i++) {
7235 if (vrf_id == inp->m_vrf_ids[i]) {
7241 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7248 if (sa->sa_family == AF_INET6) {
7249 struct sockaddr_in6 *sin6;
7251 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
7252 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7257 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7258 /* can only bind v6 on PF_INET6 sockets */
7259 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7263 sin6 = (struct sockaddr_in6 *)addr_touse;
7264 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7265 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7266 SCTP_IPV6_V6ONLY(inp)) {
7267 /* can't bind v4-mapped on PF_INET sockets */
7268 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7272 in6_sin6_2_sin(&sin, sin6);
7273 addr_touse = (struct sockaddr *)&sin;
7278 if (sa->sa_family == AF_INET) {
7280 if (sa->sa_len != sizeof(struct sockaddr_in)) {
7281 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7286 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7287 SCTP_IPV6_V6ONLY(inp)) {
7288 /* can't bind v4 on PF_INET sockets */
7289 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7295 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7296 #if !(defined(__Panda__) || defined(__Windows__))
7298 /* Can't get proc for Net/Open BSD */
7299 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7304 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
7308 * No locks required here since bind and mgmt_ep_sa
7309 * all do their own locking. If we do something for
7310 * the FIX: below we may need to lock in that case.
7312 if (assoc_id == 0) {
7313 /* add the address */
7314 struct sctp_inpcb *lep;
7315 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
7317 /* validate the incoming port */
7318 if ((lsin->sin_port != 0) &&
7319 (lsin->sin_port != inp->sctp_lport)) {
7320 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7324 /* user specified 0 port, set it to existing port */
7325 lsin->sin_port = inp->sctp_lport;
7328 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
7331 * We must decrement the refcount
7332 * since we have the ep already and
7333 * are binding. No remove going on
7336 SCTP_INP_DECR_REF(lep);
7339 /* already bound to it.. ok */
7341 } else if (lep == NULL) {
7342 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
7343 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
7344 SCTP_ADD_IP_ADDRESS,
7347 *error = EADDRINUSE;
7353 * FIX: decide whether we allow assoc based
7360 * sctp_bindx(DELETE) for one address.
7361 * assumes all arguments are valid/checked by caller.
7364 sctp_bindx_delete_address(struct sctp_inpcb *inp,
7365 struct sockaddr *sa, sctp_assoc_t assoc_id,
7366 uint32_t vrf_id, int *error)
7368 struct sockaddr *addr_touse;
7370 struct sockaddr_in sin;
7376 /* see if we're bound all already! */
7377 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7378 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7383 /* Is the VRF one we have */
7384 for (i = 0; i < inp->num_vrfs; i++) {
7385 if (vrf_id == inp->m_vrf_ids[i]) {
7391 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7398 if (sa->sa_family == AF_INET6) {
7399 struct sockaddr_in6 *sin6;
7401 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
7402 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7407 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7408 /* can only bind v6 on PF_INET6 sockets */
7409 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7413 sin6 = (struct sockaddr_in6 *)addr_touse;
7414 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7415 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7416 SCTP_IPV6_V6ONLY(inp)) {
7417 /* can't bind mapped-v4 on PF_INET sockets */
7418 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7422 in6_sin6_2_sin(&sin, sin6);
7423 addr_touse = (struct sockaddr *)&sin;
7428 if (sa->sa_family == AF_INET) {
7430 if (sa->sa_len != sizeof(struct sockaddr_in)) {
7431 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7436 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7437 SCTP_IPV6_V6ONLY(inp)) {
7438 /* can't bind v4 on PF_INET sockets */
7439 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7446 * No lock required mgmt_ep_sa does its own locking.
7447 * If the FIX: below is ever changed we may need to
7448 * lock before calling association level binding.
7450 if (assoc_id == 0) {
7451 /* delete the address */
7452 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
7453 SCTP_DEL_IP_ADDRESS,
7457 * FIX: decide whether we allow assoc based
7464 * returns the valid local address count for an assoc, taking into account
7468 sctp_local_addr_count(struct sctp_tcb *stcb)
7470 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
7471 int ipv4_addr_legal, ipv6_addr_legal;
7472 #if defined(__Userspace__)
7473 int conn_addr_legal;
7475 struct sctp_vrf *vrf;
7476 struct sctp_ifn *sctp_ifn;
7477 struct sctp_ifa *sctp_ifa;
7480 /* Turn on all the appropriate scopes */
7481 loopback_scope = stcb->asoc.scope.loopback_scope;
7482 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
7483 local_scope = stcb->asoc.scope.local_scope;
7484 site_scope = stcb->asoc.scope.site_scope;
7485 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
7486 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
7487 #if defined(__Userspace__)
7488 conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
7490 SCTP_IPI_ADDR_RLOCK();
7491 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
7493 /* no vrf, no addresses */
7494 SCTP_IPI_ADDR_RUNLOCK();
7498 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7500 * bound all case: go through all ifns on the vrf
7502 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
7503 if ((loopback_scope == 0) &&
7504 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
7507 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
7508 if (sctp_is_addr_restricted(stcb, sctp_ifa))
7510 switch (sctp_ifa->address.sa.sa_family) {
7513 if (ipv4_addr_legal) {
7514 struct sockaddr_in *sin;
7516 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
7517 if (sin->sin_addr.s_addr == 0) {
7518 /* skip unspecified addrs */
7521 if ((ipv4_local_scope == 0) &&
7522 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
7525 /* count this one */
7534 if (ipv6_addr_legal) {
7535 struct sockaddr_in6 *sin6;
7537 #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
7538 struct sockaddr_in6 lsa6;
7540 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
7541 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
7544 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
7545 if (local_scope == 0)
7547 #if defined(SCTP_EMBEDDED_V6_SCOPE)
7548 if (sin6->sin6_scope_id == 0) {
7550 if (sa6_recoverscope(sin6) != 0)
7559 if (in6_recoverscope(&lsa6,
7569 #endif /* SCTP_KAME */
7571 #endif /* SCTP_EMBEDDED_V6_SCOPE */
7573 if ((site_scope == 0) &&
7574 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
7577 /* count this one */
7582 #if defined(__Userspace__)
7584 if (conn_addr_legal) {
7599 struct sctp_laddr *laddr;
7600 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
7602 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
7605 /* count this one */
7609 SCTP_IPI_ADDR_RUNLOCK();
7613 #if defined(SCTP_LOCAL_TRACE_BUF)
7616 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)
7618 uint32_t saveindex, newindex;
7620 #if defined(__Windows__)
7621 if (SCTP_BASE_SYSCTL(sctp_log) == NULL) {
7625 saveindex = SCTP_BASE_SYSCTL(sctp_log)->index;
7626 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7629 newindex = saveindex + 1;
7631 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log)->index, saveindex, newindex) == 0);
7632 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7635 SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
7636 SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].subsys = subsys;
7637 SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[0] = a;
7638 SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[1] = b;
7639 SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[2] = c;
7640 SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[3] = d;
7641 SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[4] = e;
7642 SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[5] = f;
7645 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
7646 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7649 newindex = saveindex + 1;
7651 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
7652 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7655 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
7656 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
7657 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
7658 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
7659 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
7660 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
7661 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
7662 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
7667 #if defined(__FreeBSD__)
7668 #if __FreeBSD_version >= 800044
7670 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored)
7674 struct ip6_hdr *ip6;
7676 struct mbuf *sp, *last;
7677 struct udphdr *uhdr;
7680 if ((m->m_flags & M_PKTHDR) == 0) {
7681 /* Can't handle one that is not a pkt hdr */
7684 /* Pull the src port */
7685 iph = mtod(m, struct ip *);
7686 uhdr = (struct udphdr *)((caddr_t)iph + off);
7687 port = uhdr->uh_sport;
7688 /* Split out the mbuf chain. Leave the
7689 * IP header in m, place the
7692 sp = m_split(m, off, M_NOWAIT);
7694 /* Gak, drop packet, we can't do a split */
7697 if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
7698 /* Gak, packet can't have an SCTP header in it - too small */
7702 /* Now pull up the UDP header and SCTP header together */
7703 sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
7705 /* Gak pullup failed */
7708 /* Trim out the UDP header */
7709 m_adj(sp, sizeof(struct udphdr));
7711 /* Now reconstruct the mbuf chain */
7712 for (last = m; last->m_next; last = last->m_next);
7714 m->m_pkthdr.len += sp->m_pkthdr.len;
7715 iph = mtod(m, struct ip *);
7716 switch (iph->ip_v) {
7719 #if __FreeBSD_version >= 1000000
7720 iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
7722 iph->ip_len -= sizeof(struct udphdr);
7724 sctp_input_with_port(m, off, port);
7728 case IPV6_VERSION >> 4:
7729 ip6 = mtod(m, struct ip6_hdr *);
7730 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
7731 sctp6_input_with_port(&m, &off, port);
7745 sctp_over_udp_stop(void)
7748 * This function assumes sysctl caller holds sctp_sysctl_info_lock() for writting!
7751 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7752 soclose(SCTP_BASE_INFO(udp4_tun_socket));
7753 SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7757 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7758 soclose(SCTP_BASE_INFO(udp6_tun_socket));
7759 SCTP_BASE_INFO(udp6_tun_socket) = NULL;
7765 sctp_over_udp_start(void)
7767 #if __FreeBSD_version >= 800044
7771 struct sockaddr_in sin;
7774 struct sockaddr_in6 sin6;
7777 * This function assumes sysctl caller holds sctp_sysctl_info_lock() for writting!
7779 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
7780 if (ntohs(port) == 0) {
7781 /* Must have a port set */
7785 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7786 /* Already running -- must stop first */
7791 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7792 /* Already running -- must stop first */
7797 if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
7798 SOCK_DGRAM, IPPROTO_UDP,
7799 curthread->td_ucred, curthread))) {
7800 sctp_over_udp_stop();
7803 /* Call the special UDP hook. */
7804 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7805 sctp_recv_udp_tunneled_packet))) {
7806 sctp_over_udp_stop();
7809 /* Ok, we have a socket, bind it to the port. */
7810 memset(&sin, 0, sizeof(struct sockaddr_in));
7811 sin.sin_len = sizeof(struct sockaddr_in);
7812 sin.sin_family = AF_INET;
7813 sin.sin_port = htons(port);
7814 if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
7815 (struct sockaddr *)&sin, curthread))) {
7816 sctp_over_udp_stop();
7821 if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
7822 SOCK_DGRAM, IPPROTO_UDP,
7823 curthread->td_ucred, curthread))) {
7824 sctp_over_udp_stop();
7827 /* Call the special UDP hook. */
7828 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7829 sctp_recv_udp_tunneled_packet))) {
7830 sctp_over_udp_stop();
7833 /* Ok, we have a socket, bind it to the port. */
7834 memset(&sin6, 0, sizeof(struct sockaddr_in6));
7835 sin6.sin6_len = sizeof(struct sockaddr_in6);
7836 sin6.sin6_family = AF_INET6;
7837 sin6.sin6_port = htons(port);
7838 if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
7839 (struct sockaddr *)&sin6, curthread))) {
7840 sctp_over_udp_stop();