2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctputil.c,v 1.37 2005/03/07 23:26:09 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctputil.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
42 #include <netinet6/sctp6_var.h>
44 #include <netinet/sctp_header.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_crc32.h>
49 #include <netinet/sctp_indata.h>/* for sctp_deliver_data() */
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_asconf.h>
53 #define NUMBER_OF_MTU_SIZES 18
57 #define KTR_SCTP KTR_SUBSYS
61 sctp_sblog(struct sockbuf *sb,
62 struct sctp_tcb *stcb, int from, int incr)
64 struct sctp_cwnd_log sctp_clog;
66 sctp_clog.x.sb.stcb = stcb;
67 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
69 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
71 sctp_clog.x.sb.stcb_sbcc = 0;
72 sctp_clog.x.sb.incr = incr;
73 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
76 sctp_clog.x.misc.log1,
77 sctp_clog.x.misc.log2,
78 sctp_clog.x.misc.log3,
79 sctp_clog.x.misc.log4);
83 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
85 struct sctp_cwnd_log sctp_clog;
87 sctp_clog.x.close.inp = (void *)inp;
88 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
90 sctp_clog.x.close.stcb = (void *)stcb;
91 sctp_clog.x.close.state = (uint16_t) stcb->asoc.state;
93 sctp_clog.x.close.stcb = 0;
94 sctp_clog.x.close.state = 0;
96 sctp_clog.x.close.loc = loc;
97 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
100 sctp_clog.x.misc.log1,
101 sctp_clog.x.misc.log2,
102 sctp_clog.x.misc.log3,
103 sctp_clog.x.misc.log4);
108 rto_logging(struct sctp_nets *net, int from)
110 struct sctp_cwnd_log sctp_clog;
112 sctp_clog.x.rto.net = (void *)net;
113 sctp_clog.x.rto.rtt = net->prev_rtt;
114 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
117 sctp_clog.x.misc.log1,
118 sctp_clog.x.misc.log2,
119 sctp_clog.x.misc.log3,
120 sctp_clog.x.misc.log4);
125 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
127 struct sctp_cwnd_log sctp_clog;
129 sctp_clog.x.strlog.stcb = stcb;
130 sctp_clog.x.strlog.n_tsn = tsn;
131 sctp_clog.x.strlog.n_sseq = sseq;
132 sctp_clog.x.strlog.e_tsn = 0;
133 sctp_clog.x.strlog.e_sseq = 0;
134 sctp_clog.x.strlog.strm = stream;
135 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
138 sctp_clog.x.misc.log1,
139 sctp_clog.x.misc.log2,
140 sctp_clog.x.misc.log3,
141 sctp_clog.x.misc.log4);
146 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
148 struct sctp_cwnd_log sctp_clog;
150 sctp_clog.x.nagle.stcb = (void *)stcb;
151 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
152 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
153 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
154 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
155 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
156 SCTP_LOG_EVENT_NAGLE,
158 sctp_clog.x.misc.log1,
159 sctp_clog.x.misc.log2,
160 sctp_clog.x.misc.log3,
161 sctp_clog.x.misc.log4);
166 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
168 struct sctp_cwnd_log sctp_clog;
170 sctp_clog.x.sack.cumack = cumack;
171 sctp_clog.x.sack.oldcumack = old_cumack;
172 sctp_clog.x.sack.tsn = tsn;
173 sctp_clog.x.sack.numGaps = gaps;
174 sctp_clog.x.sack.numDups = dups;
175 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
178 sctp_clog.x.misc.log1,
179 sctp_clog.x.misc.log2,
180 sctp_clog.x.misc.log3,
181 sctp_clog.x.misc.log4);
185 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
187 struct sctp_cwnd_log sctp_clog;
189 sctp_clog.x.map.base = map;
190 sctp_clog.x.map.cum = cum;
191 sctp_clog.x.map.high = high;
192 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
195 sctp_clog.x.misc.log1,
196 sctp_clog.x.misc.log2,
197 sctp_clog.x.misc.log3,
198 sctp_clog.x.misc.log4);
202 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn,
205 struct sctp_cwnd_log sctp_clog;
207 sctp_clog.x.fr.largest_tsn = biggest_tsn;
208 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
209 sctp_clog.x.fr.tsn = tsn;
210 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
213 sctp_clog.x.misc.log1,
214 sctp_clog.x.misc.log2,
215 sctp_clog.x.misc.log3,
216 sctp_clog.x.misc.log4);
222 sctp_log_mb(struct mbuf *m, int from)
224 struct sctp_cwnd_log sctp_clog;
226 sctp_clog.x.mb.mp = m;
227 sctp_clog.x.mb.mbuf_flags = (uint8_t) (SCTP_BUF_GET_FLAGS(m));
228 sctp_clog.x.mb.size = (uint16_t) (SCTP_BUF_LEN(m));
229 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
230 if (SCTP_BUF_IS_EXTENDED(m)) {
231 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
232 sctp_clog.x.mb.refcnt = (uint8_t) (SCTP_BUF_EXTEND_REFCNT(m));
234 sctp_clog.x.mb.ext = 0;
235 sctp_clog.x.mb.refcnt = 0;
237 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
240 sctp_clog.x.misc.log1,
241 sctp_clog.x.misc.log2,
242 sctp_clog.x.misc.log3,
243 sctp_clog.x.misc.log4);
248 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk,
251 struct sctp_cwnd_log sctp_clog;
253 if (control == NULL) {
254 SCTP_PRINTF("Gak log of NULL?\n");
257 sctp_clog.x.strlog.stcb = control->stcb;
258 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
259 sctp_clog.x.strlog.n_sseq = control->sinfo_ssn;
260 sctp_clog.x.strlog.strm = control->sinfo_stream;
261 if (poschk != NULL) {
262 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
263 sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn;
265 sctp_clog.x.strlog.e_tsn = 0;
266 sctp_clog.x.strlog.e_sseq = 0;
268 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
271 sctp_clog.x.misc.log1,
272 sctp_clog.x.misc.log2,
273 sctp_clog.x.misc.log3,
274 sctp_clog.x.misc.log4);
279 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
281 struct sctp_cwnd_log sctp_clog;
283 sctp_clog.x.cwnd.net = net;
284 if (stcb->asoc.send_queue_cnt > 255)
285 sctp_clog.x.cwnd.cnt_in_send = 255;
287 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
288 if (stcb->asoc.stream_queue_cnt > 255)
289 sctp_clog.x.cwnd.cnt_in_str = 255;
291 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
294 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
295 sctp_clog.x.cwnd.inflight = net->flight_size;
296 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
297 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
298 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
300 if (SCTP_CWNDLOG_PRESEND == from) {
301 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
303 sctp_clog.x.cwnd.cwnd_augment = augment;
304 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
307 sctp_clog.x.misc.log1,
308 sctp_clog.x.misc.log2,
309 sctp_clog.x.misc.log3,
310 sctp_clog.x.misc.log4);
315 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
317 struct sctp_cwnd_log sctp_clog;
320 sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
323 sctp_clog.x.lock.sock = (void *)NULL;
325 sctp_clog.x.lock.inp = (void *)inp;
327 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
329 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
332 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
333 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
335 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
336 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
338 sctp_clog.x.lock.info_lock = mtx_owned(&sctppcbinfo.ipi_ep_mtx);
339 if (inp->sctp_socket) {
340 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
341 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
342 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
344 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
345 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
346 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
348 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
351 sctp_clog.x.misc.log1,
352 sctp_clog.x.misc.log2,
353 sctp_clog.x.misc.log3,
354 sctp_clog.x.misc.log4);
359 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
361 struct sctp_cwnd_log sctp_clog;
363 sctp_clog.x.cwnd.net = net;
364 sctp_clog.x.cwnd.cwnd_new_value = error;
365 sctp_clog.x.cwnd.inflight = net->flight_size;
366 sctp_clog.x.cwnd.cwnd_augment = burst;
367 if (stcb->asoc.send_queue_cnt > 255)
368 sctp_clog.x.cwnd.cnt_in_send = 255;
370 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
371 if (stcb->asoc.stream_queue_cnt > 255)
372 sctp_clog.x.cwnd.cnt_in_str = 255;
374 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
375 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
376 SCTP_LOG_EVENT_MAXBURST,
378 sctp_clog.x.misc.log1,
379 sctp_clog.x.misc.log2,
380 sctp_clog.x.misc.log3,
381 sctp_clog.x.misc.log4);
386 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
388 struct sctp_cwnd_log sctp_clog;
390 sctp_clog.x.rwnd.rwnd = peers_rwnd;
391 sctp_clog.x.rwnd.send_size = snd_size;
392 sctp_clog.x.rwnd.overhead = overhead;
393 sctp_clog.x.rwnd.new_rwnd = 0;
394 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
397 sctp_clog.x.misc.log1,
398 sctp_clog.x.misc.log2,
399 sctp_clog.x.misc.log3,
400 sctp_clog.x.misc.log4);
404 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
406 struct sctp_cwnd_log sctp_clog;
408 sctp_clog.x.rwnd.rwnd = peers_rwnd;
409 sctp_clog.x.rwnd.send_size = flight_size;
410 sctp_clog.x.rwnd.overhead = overhead;
411 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
412 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
415 sctp_clog.x.misc.log1,
416 sctp_clog.x.misc.log2,
417 sctp_clog.x.misc.log3,
418 sctp_clog.x.misc.log4);
422 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
424 struct sctp_cwnd_log sctp_clog;
426 sctp_clog.x.mbcnt.total_queue_size = total_oq;
427 sctp_clog.x.mbcnt.size_change = book;
428 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
429 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
430 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
431 SCTP_LOG_EVENT_MBCNT,
433 sctp_clog.x.misc.log1,
434 sctp_clog.x.misc.log2,
435 sctp_clog.x.misc.log3,
436 sctp_clog.x.misc.log4);
441 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
443 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
450 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t cumtsn, uint32_t wake_cnt, int from)
452 struct sctp_cwnd_log sctp_clog;
454 sctp_clog.x.wake.stcb = (void *)stcb;
455 sctp_clog.x.wake.wake_cnt = wake_cnt;
456 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
457 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
458 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
460 if (stcb->asoc.stream_queue_cnt < 0xff)
461 sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
463 sctp_clog.x.wake.stream_qcnt = 0xff;
465 if (stcb->asoc.chunks_on_out_queue < 0xff)
466 sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
468 sctp_clog.x.wake.chunks_on_oque = 0xff;
470 sctp_clog.x.wake.sctpflags = 0;
471 /* set in the defered mode stuff */
472 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
473 sctp_clog.x.wake.sctpflags |= 1;
474 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
475 sctp_clog.x.wake.sctpflags |= 2;
476 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
477 sctp_clog.x.wake.sctpflags |= 4;
478 /* what about the sb */
479 if (stcb->sctp_socket) {
480 struct socket *so = stcb->sctp_socket;
482 sctp_clog.x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
484 sctp_clog.x.wake.sbflags = 0xff;
486 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
489 sctp_clog.x.misc.log1,
490 sctp_clog.x.misc.log2,
491 sctp_clog.x.misc.log3,
492 sctp_clog.x.misc.log4);
497 sctp_log_block(uint8_t from, struct socket *so, struct sctp_association *asoc, int sendlen)
499 struct sctp_cwnd_log sctp_clog;
501 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
502 sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
503 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
504 sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
505 sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
506 sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
507 sctp_clog.x.blk.sndlen = sendlen;
508 CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
509 SCTP_LOG_EVENT_BLOCK,
511 sctp_clog.x.misc.log1,
512 sctp_clog.x.misc.log2,
513 sctp_clog.x.misc.log3,
514 sctp_clog.x.misc.log4);
519 sctp_fill_stat_log(void *optval, size_t *optsize)
521 /* May need to fix this if ktrdump does not work */
525 #ifdef SCTP_AUDITING_ENABLED
526 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
527 static int sctp_audit_indx = 0;
531 sctp_print_audit_report(void)
537 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
538 if ((sctp_audit_data[i][0] == 0xe0) &&
539 (sctp_audit_data[i][1] == 0x01)) {
542 } else if (sctp_audit_data[i][0] == 0xf0) {
545 } else if ((sctp_audit_data[i][0] == 0xc0) &&
546 (sctp_audit_data[i][1] == 0x01)) {
550 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
551 (uint32_t) sctp_audit_data[i][1]);
556 for (i = 0; i < sctp_audit_indx; i++) {
557 if ((sctp_audit_data[i][0] == 0xe0) &&
558 (sctp_audit_data[i][1] == 0x01)) {
561 } else if (sctp_audit_data[i][0] == 0xf0) {
564 } else if ((sctp_audit_data[i][0] == 0xc0) &&
565 (sctp_audit_data[i][1] == 0x01)) {
569 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
570 (uint32_t) sctp_audit_data[i][1]);
579 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
580 struct sctp_nets *net)
582 int resend_cnt, tot_out, rep, tot_book_cnt;
583 struct sctp_nets *lnet;
584 struct sctp_tmit_chunk *chk;
586 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
587 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
589 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
593 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
594 sctp_audit_data[sctp_audit_indx][1] = 0x01;
596 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
602 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
603 sctp_audit_data[sctp_audit_indx][1] = 0x02;
605 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
610 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
611 sctp_audit_data[sctp_audit_indx][1] =
612 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
614 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
619 resend_cnt = tot_out = 0;
620 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
621 if (chk->sent == SCTP_DATAGRAM_RESEND) {
623 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
624 tot_out += chk->book_size;
628 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
629 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
630 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
632 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
635 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
636 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
638 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
639 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
640 sctp_audit_data[sctp_audit_indx][1] =
641 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
643 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
647 if (tot_out != stcb->asoc.total_flight) {
648 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
649 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
651 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
655 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
656 (int)stcb->asoc.total_flight);
657 stcb->asoc.total_flight = tot_out;
659 if (tot_book_cnt != stcb->asoc.total_flight_count) {
660 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
661 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
663 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
667 SCTP_PRINTF("tot_flt_book:%d\n", tot_book);
669 stcb->asoc.total_flight_count = tot_book_cnt;
672 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
673 tot_out += lnet->flight_size;
675 if (tot_out != stcb->asoc.total_flight) {
676 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
677 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
679 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
683 SCTP_PRINTF("real flight:%d net total was %d\n",
684 stcb->asoc.total_flight, tot_out);
685 /* now corrective action */
686 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
689 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
690 if ((chk->whoTo == lnet) &&
691 (chk->sent < SCTP_DATAGRAM_RESEND)) {
692 tot_out += chk->book_size;
695 if (lnet->flight_size != tot_out) {
696 SCTP_PRINTF("net:%x flight was %d corrected to %d\n",
697 (uint32_t) lnet, lnet->flight_size,
699 lnet->flight_size = tot_out;
704 sctp_print_audit_report();
709 sctp_audit_log(uint8_t ev, uint8_t fd)
712 sctp_audit_data[sctp_audit_indx][0] = ev;
713 sctp_audit_data[sctp_audit_indx][1] = fd;
715 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
723 * a list of sizes based on typical mtu's, used only if next hop size not
726 static int sctp_mtu_sizes[] = {
748 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
750 struct sctp_association *asoc;
751 struct sctp_nets *net;
755 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
756 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
757 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
758 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
759 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
760 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
761 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
762 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
763 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
768 find_next_best_mtu(int totsz)
773 * if we are in here we must find the next best fit based on the
774 * size of the dg that failed to be sent.
777 for (i = 0; i < NUMBER_OF_MTU_SIZES; i++) {
778 if (totsz < sctp_mtu_sizes[i]) {
785 return (sctp_mtu_sizes[perfer]);
789 sctp_fill_random_store(struct sctp_pcb *m)
792 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
793 * our counter. The result becomes our good random numbers and we
794 * then setup to give these out. Note that we do no locking to
795 * protect this. This is ok, since if competing folks call this we
796 * will get more gobbled gook in the random store which is what we
797 * want. There is a danger that two guys will use the same random
798 * numbers, but thats ok too since that is random as well :->
801 (void)sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
802 sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
803 sizeof(m->random_counter), (uint8_t *) m->random_store);
808 sctp_select_initial_TSN(struct sctp_pcb *m)
811 * A true implementation should use random selection process to get
812 * the initial stream sequence number, using RFC1750 as a good
818 if (m->initial_sequence_debug != 0) {
821 ret = m->initial_sequence_debug;
822 m->initial_sequence_debug++;
825 if ((m->store_at + sizeof(u_long)) > SCTP_SIGNATURE_SIZE) {
826 /* Refill the random store */
827 sctp_fill_random_store(m);
829 p = &m->random_store[(int)m->store_at];
832 m->store_at += sizeof(uint32_t);
837 sctp_select_a_tag(struct sctp_inpcb *m)
842 (void)SCTP_GETTIME_TIMEVAL(&now);
845 x = sctp_select_initial_TSN(&m->sctp_ep);
850 if (sctp_is_vtag_good(m, x, &now)) {
859 sctp_init_asoc(struct sctp_inpcb *m, struct sctp_tcb *stcb,
860 int for_a_init, uint32_t override_tag, uint32_t vrf_id)
862 struct sctp_association *asoc;
865 * Anything set to zero is taken care of by the allocation routine's
870 * Up front select what scoping to apply on addresses I tell my peer
871 * Not sure what to do with these right now, we will need to come up
872 * with a way to set them. We may need to pass them through from the
873 * caller in the sctp_aloc_assoc() function.
878 /* init all variables to a known value. */
879 asoc->state = SCTP_STATE_INUSE;
880 asoc->max_burst = m->sctp_ep.max_burst;
881 asoc->heart_beat_delay = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
882 asoc->cookie_life = m->sctp_ep.def_cookie_life;
883 asoc->sctp_cmt_on_off = (uint8_t) sctp_cmt_on_off;
884 asoc->sctp_frag_point = m->sctp_frag_point;
886 asoc->default_tos = m->ip_inp.inp.inp_ip_tos;
888 asoc->default_tos = 0;
892 asoc->default_flowlabel = ((struct in6pcb *)m)->in6p_flowinfo;
894 asoc->default_flowlabel = 0;
899 (void)SCTP_GETTIME_TIMEVAL(&now);
900 if (sctp_is_vtag_good(m, override_tag, &now)) {
901 asoc->my_vtag = override_tag;
907 asoc->my_vtag = sctp_select_a_tag(m);
909 /* Get the nonce tags */
910 asoc->my_vtag_nonce = sctp_select_a_tag(m);
911 asoc->peer_vtag_nonce = sctp_select_a_tag(m);
912 asoc->vrf_id = vrf_id;
914 if (sctp_is_feature_on(m, SCTP_PCB_FLAGS_DONOT_HEARTBEAT))
915 asoc->hb_is_disabled = 1;
917 asoc->hb_is_disabled = 0;
920 asoc->assoc_up_sent = 0;
921 asoc->assoc_id = asoc->my_vtag;
922 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
923 sctp_select_initial_TSN(&m->sctp_ep);
924 /* we are optimisitic here */
925 asoc->peer_supports_pktdrop = 1;
927 asoc->sent_queue_retran_cnt = 0;
930 asoc->last_net_data_came_from = NULL;
932 /* This will need to be adjusted */
933 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
934 asoc->last_acked_seq = asoc->init_seq_number - 1;
935 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
936 asoc->asconf_seq_in = asoc->last_acked_seq;
938 /* here we are different, we hold the next one we expect */
939 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
941 asoc->initial_init_rto_max = m->sctp_ep.initial_init_rto_max;
942 asoc->initial_rto = m->sctp_ep.initial_rto;
944 asoc->max_init_times = m->sctp_ep.max_init_times;
945 asoc->max_send_times = m->sctp_ep.max_send_times;
946 asoc->def_net_failure = m->sctp_ep.def_net_failure;
947 asoc->free_chunk_cnt = 0;
949 asoc->iam_blocking = 0;
950 /* ECN Nonce initialization */
951 asoc->context = m->sctp_context;
952 asoc->def_send = m->def_send;
953 asoc->ecn_nonce_allowed = 0;
954 asoc->receiver_nonce_sum = 1;
955 asoc->nonce_sum_expect_base = 1;
956 asoc->nonce_sum_check = 1;
957 asoc->nonce_resync_tsn = 0;
958 asoc->nonce_wait_for_ecne = 0;
959 asoc->nonce_wait_tsn = 0;
960 asoc->delayed_ack = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
961 asoc->sack_freq = m->sctp_ep.sctp_sack_freq;
962 asoc->pr_sctp_cnt = 0;
963 asoc->total_output_queue_size = 0;
965 if (m->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
968 /* Its a V6 socket */
969 inp6 = (struct in6pcb *)m;
970 asoc->ipv6_addr_legal = 1;
971 /* Now look at the binding flag to see if V4 will be legal */
972 if (SCTP_IPV6_V6ONLY(inp6) == 0) {
973 asoc->ipv4_addr_legal = 1;
975 /* V4 addresses are NOT legal on the association */
976 asoc->ipv4_addr_legal = 0;
979 /* Its a V4 socket, no - V6 */
980 asoc->ipv4_addr_legal = 1;
981 asoc->ipv6_addr_legal = 0;
984 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(m->sctp_socket), SCTP_MINIMAL_RWND);
985 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(m->sctp_socket);
987 asoc->smallest_mtu = m->sctp_frag_point;
988 #ifdef SCTP_PRINT_FOR_B_AND_M
989 SCTP_PRINTF("smallest_mtu init'd with asoc to :%d\n",
992 asoc->minrto = m->sctp_ep.sctp_minrto;
993 asoc->maxrto = m->sctp_ep.sctp_maxrto;
995 asoc->locked_on_sending = NULL;
996 asoc->stream_locked_on = 0;
997 asoc->ecn_echo_cnt_onq = 0;
998 asoc->stream_locked = 0;
1000 asoc->send_sack = 1;
1002 LIST_INIT(&asoc->sctp_restricted_addrs);
1004 TAILQ_INIT(&asoc->nets);
1005 TAILQ_INIT(&asoc->pending_reply_queue);
1006 asoc->last_asconf_ack_sent = NULL;
1007 /* Setup to fill the hb random cache at first HB */
1008 asoc->hb_random_idx = 4;
1010 asoc->sctp_autoclose_ticks = m->sctp_ep.auto_close_time;
1013 * Now the stream parameters, here we allocate space for all streams
1014 * that we request by default.
1016 asoc->streamoutcnt = asoc->pre_open_streams =
1017 m->sctp_ep.pre_open_stream_count;
1018 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1019 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1021 if (asoc->strmout == NULL) {
1022 /* big trouble no memory */
1025 for (i = 0; i < asoc->streamoutcnt; i++) {
1027 * inbound side must be set to 0xffff, also NOTE when we get
1028 * the INIT-ACK back (for INIT sender) we MUST reduce the
1029 * count (streamoutcnt) but first check if we sent to any of
1030 * the upper streams that were dropped (if some were). Those
1031 * that were dropped must be notified to the upper layer as
1034 asoc->strmout[i].next_sequence_sent = 0x0;
1035 TAILQ_INIT(&asoc->strmout[i].outqueue);
1036 asoc->strmout[i].stream_no = i;
1037 asoc->strmout[i].last_msg_incomplete = 0;
1038 asoc->strmout[i].next_spoke.tqe_next = 0;
1039 asoc->strmout[i].next_spoke.tqe_prev = 0;
1041 /* Now the mapping array */
1042 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1043 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1045 if (asoc->mapping_array == NULL) {
1046 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1049 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1050 /* Now the init of the other outqueues */
1051 TAILQ_INIT(&asoc->free_chunks);
1052 TAILQ_INIT(&asoc->out_wheel);
1053 TAILQ_INIT(&asoc->control_send_queue);
1054 TAILQ_INIT(&asoc->send_queue);
1055 TAILQ_INIT(&asoc->sent_queue);
1056 TAILQ_INIT(&asoc->reasmqueue);
1057 TAILQ_INIT(&asoc->resetHead);
1058 asoc->max_inbound_streams = m->sctp_ep.max_open_streams_intome;
1059 TAILQ_INIT(&asoc->asconf_queue);
1060 /* authentication fields */
1061 asoc->authinfo.random = NULL;
1062 asoc->authinfo.assoc_key = NULL;
1063 asoc->authinfo.assoc_keyid = 0;
1064 asoc->authinfo.recv_key = NULL;
1065 asoc->authinfo.recv_keyid = 0;
1066 LIST_INIT(&asoc->shared_keys);
1067 asoc->marked_retrans = 0;
1071 asoc->timoshutdown = 0;
1072 asoc->timoheartbeat = 0;
1073 asoc->timocookie = 0;
1074 asoc->timoshutdownack = 0;
1075 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1076 asoc->discontinuity_time = asoc->start_time;
1078 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1079 * freed later whe the association is freed.
1085 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1087 /* mapping array needs to grow */
1092 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1093 SCTP_MALLOC(new_array, uint8_t *, new_size, SCTP_M_MAP);
1094 if (new_array == NULL) {
1095 /* can't get more, forget it */
1096 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n",
1100 memset(new_array, 0, new_size);
1101 memcpy(new_array, asoc->mapping_array, asoc->mapping_array_size);
1102 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1103 asoc->mapping_array = new_array;
1104 asoc->mapping_array_size = new_size;
1108 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
1110 sctp_iterator_work(struct sctp_iterator *it)
1112 int iteration_count = 0;
1115 SCTP_ITERATOR_LOCK();
1117 SCTP_INP_DECR_REF(it->inp);
1119 if (it->inp == NULL) {
1120 /* iterator is complete */
1122 SCTP_ITERATOR_UNLOCK();
1123 if (it->function_atend != NULL) {
1124 (*it->function_atend) (it->pointer, it->val);
1126 SCTP_FREE(it, SCTP_M_ITER);
1130 SCTP_INP_WLOCK(it->inp);
1131 while (((it->pcb_flags) &&
1132 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1133 ((it->pcb_features) &&
1134 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1135 /* endpoint flags or features don't match, so keep looking */
1136 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1137 SCTP_INP_WUNLOCK(it->inp);
1138 goto done_with_iterator;
1140 SCTP_INP_WUNLOCK(it->inp);
1141 it->inp = LIST_NEXT(it->inp, sctp_list);
1142 if (it->inp == NULL) {
1143 goto done_with_iterator;
1145 SCTP_INP_WLOCK(it->inp);
1148 /* mark the current iterator on the endpoint */
1149 it->inp->inp_starting_point_for_iterator = it;
1150 SCTP_INP_WUNLOCK(it->inp);
1151 SCTP_INP_RLOCK(it->inp);
1153 /* now go through each assoc which is in the desired state */
1154 if (it->done_current_ep == 0) {
1155 if (it->function_inp != NULL)
1156 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1157 it->done_current_ep = 1;
1159 if (it->stcb == NULL) {
1160 /* run the per instance function */
1161 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1163 if ((inp_skip) || it->stcb == NULL) {
1164 if (it->function_inp_end != NULL) {
1165 inp_skip = (*it->function_inp_end) (it->inp,
1169 SCTP_INP_RUNLOCK(it->inp);
1173 (it->stcb->asoc.stcb_starting_point_for_iterator == it)) {
1174 it->stcb->asoc.stcb_starting_point_for_iterator = NULL;
1177 SCTP_TCB_LOCK(it->stcb);
1178 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1179 /* not in the right state... keep looking */
1180 SCTP_TCB_UNLOCK(it->stcb);
1183 /* mark the current iterator on the assoc */
1184 it->stcb->asoc.stcb_starting_point_for_iterator = it;
1185 /* see if we have limited out the iterator loop */
1187 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1188 /* Pause to let others grab the lock */
1189 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1190 SCTP_TCB_UNLOCK(it->stcb);
1191 SCTP_INP_RUNLOCK(it->inp);
1192 SCTP_ITERATOR_UNLOCK();
1193 SCTP_ITERATOR_LOCK();
1194 SCTP_INP_RLOCK(it->inp);
1195 SCTP_TCB_LOCK(it->stcb);
1196 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1197 iteration_count = 0;
1199 /* run function on this one */
1200 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1203 * we lie here, it really needs to have its own type but
1204 * first I must verify that this won't effect things :-0
1206 if (it->no_chunk_output == 0)
1207 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3);
1209 SCTP_TCB_UNLOCK(it->stcb);
1211 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1212 if (it->stcb == NULL) {
1213 /* Run last function */
1214 if (it->function_inp_end != NULL) {
1215 inp_skip = (*it->function_inp_end) (it->inp,
1221 SCTP_INP_RUNLOCK(it->inp);
1223 /* done with all assocs on this endpoint, move on to next endpoint */
1224 it->done_current_ep = 0;
1225 SCTP_INP_WLOCK(it->inp);
1226 it->inp->inp_starting_point_for_iterator = NULL;
1227 SCTP_INP_WUNLOCK(it->inp);
1228 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1231 SCTP_INP_INFO_RLOCK();
1232 it->inp = LIST_NEXT(it->inp, sctp_list);
1233 SCTP_INP_INFO_RUNLOCK();
1235 if (it->inp == NULL) {
1236 goto done_with_iterator;
1238 goto select_a_new_ep;
1242 sctp_iterator_worker(void)
1244 struct sctp_iterator *it = NULL;
1246 /* This function is called with the WQ lock in place */
1248 sctppcbinfo.iterator_running = 1;
1250 it = TAILQ_FIRST(&sctppcbinfo.iteratorhead);
1252 /* now lets work on this one */
1253 TAILQ_REMOVE(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
1254 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1255 sctp_iterator_work(it);
1256 SCTP_IPI_ITERATOR_WQ_LOCK();
1257 /* sa_ignore FREED_MEMORY */
1258 it = TAILQ_FIRST(&sctppcbinfo.iteratorhead);
1260 if (TAILQ_FIRST(&sctppcbinfo.iteratorhead)) {
1263 sctppcbinfo.iterator_running = 0;
1271 sctp_handle_addr_wq(void)
1273 /* deal with the ADDR wq from the rtsock calls */
1274 struct sctp_laddr *wi;
1275 struct sctp_asconf_iterator *asc;
1277 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1278 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1280 /* Try later, no memory */
1281 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1282 (struct sctp_inpcb *)NULL,
1283 (struct sctp_tcb *)NULL,
1284 (struct sctp_nets *)NULL);
1287 LIST_INIT(&asc->list_of_work);
1289 SCTP_IPI_ITERATOR_WQ_LOCK();
1290 wi = LIST_FIRST(&sctppcbinfo.addr_wq);
1291 while (wi != NULL) {
1292 LIST_REMOVE(wi, sctp_nxt_addr);
1293 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1295 wi = LIST_FIRST(&sctppcbinfo.addr_wq);
1297 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1298 if (asc->cnt == 0) {
1299 SCTP_FREE(asc, SCTP_M_ASC_IT);
1301 (void)sctp_initiate_iterator(sctp_iterator_ep,
1303 NULL, /* No ep end for boundall */
1304 SCTP_PCB_FLAGS_BOUNDALL,
1305 SCTP_PCB_ANY_FEATURES,
1306 SCTP_ASOC_ANY_STATE, (void *)asc, 0,
1307 sctp_iterator_end, NULL, 0);
1313 sctp_timeout_handler(void *t)
1315 struct sctp_inpcb *inp;
1316 struct sctp_tcb *stcb;
1317 struct sctp_nets *net;
1318 struct sctp_timer *tmr;
1320 struct sctp_iterator *it = NULL;
1323 tmr = (struct sctp_timer *)t;
1324 inp = (struct sctp_inpcb *)tmr->ep;
1325 stcb = (struct sctp_tcb *)tmr->tcb;
1326 net = (struct sctp_nets *)tmr->net;
1329 #ifdef SCTP_AUDITING_ENABLED
1330 sctp_audit_log(0xF0, (uint8_t) tmr->type);
1331 sctp_auditing(3, inp, stcb, net);
1334 /* sanity checks... */
1335 if (tmr->self != (void *)tmr) {
1337 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1342 tmr->stopped_from = 0xa001;
1343 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1345 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1350 tmr->stopped_from = 0xa002;
1351 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1354 /* if this is an iterator timeout, get the struct and clear inp */
1355 tmr->stopped_from = 0xa003;
1356 if (tmr->type == SCTP_TIMER_TYPE_ITERATOR) {
1357 it = (struct sctp_iterator *)inp;
1361 SCTP_INP_INCR_REF(inp);
1362 if ((inp->sctp_socket == 0) &&
1363 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1364 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1365 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1366 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1367 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1369 SCTP_INP_DECR_REF(inp);
1373 tmr->stopped_from = 0xa004;
1375 atomic_add_int(&stcb->asoc.refcnt, 1);
1376 if (stcb->asoc.state == 0) {
1377 atomic_add_int(&stcb->asoc.refcnt, -1);
1379 SCTP_INP_DECR_REF(inp);
1384 tmr->stopped_from = 0xa005;
1385 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1386 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1388 SCTP_INP_DECR_REF(inp);
1391 atomic_add_int(&stcb->asoc.refcnt, -1);
1395 tmr->stopped_from = 0xa006;
1398 SCTP_TCB_LOCK(stcb);
1399 atomic_add_int(&stcb->asoc.refcnt, -1);
1401 /* record in stopped what t-o occured */
1402 tmr->stopped_from = tmr->type;
1404 /* mark as being serviced now */
1405 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1407 * Callout has been rescheduled.
1411 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1413 * Not active, so no action.
1417 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1419 /* call the handler for the appropriate timer type */
1420 switch (tmr->type) {
1421 case SCTP_TIMER_TYPE_ZERO_COPY:
1425 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1426 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1429 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1433 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1434 SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1437 case SCTP_TIMER_TYPE_ADDR_WQ:
1438 sctp_handle_addr_wq();
1440 case SCTP_TIMER_TYPE_ITERATOR:
1441 SCTP_STAT_INCR(sctps_timoiterator);
1442 sctp_iterator_timer(it);
1444 case SCTP_TIMER_TYPE_SEND:
1445 if ((stcb == NULL) || (inp == NULL)) {
1448 SCTP_STAT_INCR(sctps_timodata);
1449 stcb->asoc.timodata++;
1450 stcb->asoc.num_send_timers_up--;
1451 if (stcb->asoc.num_send_timers_up < 0) {
1452 stcb->asoc.num_send_timers_up = 0;
1454 if (sctp_t3rxt_timer(inp, stcb, net)) {
1455 /* no need to unlock on tcb its gone */
1459 #ifdef SCTP_AUDITING_ENABLED
1460 sctp_auditing(4, inp, stcb, net);
1462 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1463 if ((stcb->asoc.num_send_timers_up == 0) &&
1464 (stcb->asoc.sent_queue_cnt > 0)
1466 struct sctp_tmit_chunk *chk;
1469 * safeguard. If there on some on the sent queue
1470 * somewhere but no timers running something is
1471 * wrong... so we start a timer on the first chunk
1472 * on the send queue on whatever net it is sent to.
1474 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1475 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1479 case SCTP_TIMER_TYPE_INIT:
1480 if ((stcb == NULL) || (inp == NULL)) {
1483 SCTP_STAT_INCR(sctps_timoinit);
1484 stcb->asoc.timoinit++;
1485 if (sctp_t1init_timer(inp, stcb, net)) {
1486 /* no need to unlock on tcb its gone */
1489 /* We do output but not here */
1492 case SCTP_TIMER_TYPE_RECV:
1493 if ((stcb == NULL) || (inp == NULL)) {
1496 SCTP_STAT_INCR(sctps_timosack);
1497 stcb->asoc.timosack++;
1498 sctp_send_sack(stcb);
1499 #ifdef SCTP_AUDITING_ENABLED
1500 sctp_auditing(4, inp, stcb, net);
1502 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR);
1504 case SCTP_TIMER_TYPE_SHUTDOWN:
1505 if ((stcb == NULL) || (inp == NULL)) {
1508 if (sctp_shutdown_timer(inp, stcb, net)) {
1509 /* no need to unlock on tcb its gone */
1512 SCTP_STAT_INCR(sctps_timoshutdown);
1513 stcb->asoc.timoshutdown++;
1514 #ifdef SCTP_AUDITING_ENABLED
1515 sctp_auditing(4, inp, stcb, net);
1517 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR);
1519 case SCTP_TIMER_TYPE_HEARTBEAT:
1521 struct sctp_nets *lnet;
1522 int cnt_of_unconf = 0;
1524 if ((stcb == NULL) || (inp == NULL)) {
1527 SCTP_STAT_INCR(sctps_timoheartbeat);
1528 stcb->asoc.timoheartbeat++;
1529 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1530 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1531 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1535 if (cnt_of_unconf == 0) {
1536 if (sctp_heartbeat_timer(inp, stcb, lnet,
1538 /* no need to unlock on tcb its gone */
1542 #ifdef SCTP_AUDITING_ENABLED
1543 sctp_auditing(4, inp, stcb, lnet);
1545 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
1546 stcb->sctp_ep, stcb, lnet);
1547 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR);
1550 case SCTP_TIMER_TYPE_COOKIE:
1551 if ((stcb == NULL) || (inp == NULL)) {
1554 if (sctp_cookie_timer(inp, stcb, net)) {
1555 /* no need to unlock on tcb its gone */
1558 SCTP_STAT_INCR(sctps_timocookie);
1559 stcb->asoc.timocookie++;
1560 #ifdef SCTP_AUDITING_ENABLED
1561 sctp_auditing(4, inp, stcb, net);
1564 * We consider T3 and Cookie timer pretty much the same with
1565 * respect to where from in chunk_output.
1567 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1569 case SCTP_TIMER_TYPE_NEWCOOKIE:
1577 SCTP_STAT_INCR(sctps_timosecret);
1578 (void)SCTP_GETTIME_TIMEVAL(&tv);
1579 SCTP_INP_WLOCK(inp);
1580 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1581 inp->sctp_ep.last_secret_number =
1582 inp->sctp_ep.current_secret_number;
1583 inp->sctp_ep.current_secret_number++;
1584 if (inp->sctp_ep.current_secret_number >=
1585 SCTP_HOW_MANY_SECRETS) {
1586 inp->sctp_ep.current_secret_number = 0;
1588 secret = (int)inp->sctp_ep.current_secret_number;
1589 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1590 inp->sctp_ep.secret_key[secret][i] =
1591 sctp_select_initial_TSN(&inp->sctp_ep);
1593 SCTP_INP_WUNLOCK(inp);
1594 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1598 case SCTP_TIMER_TYPE_PATHMTURAISE:
1599 if ((stcb == NULL) || (inp == NULL)) {
1602 SCTP_STAT_INCR(sctps_timopathmtu);
1603 sctp_pathmtu_timer(inp, stcb, net);
1606 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1607 if ((stcb == NULL) || (inp == NULL)) {
1610 if (sctp_shutdownack_timer(inp, stcb, net)) {
1611 /* no need to unlock on tcb its gone */
1614 SCTP_STAT_INCR(sctps_timoshutdownack);
1615 stcb->asoc.timoshutdownack++;
1616 #ifdef SCTP_AUDITING_ENABLED
1617 sctp_auditing(4, inp, stcb, net);
1619 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR);
1621 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1622 if ((stcb == NULL) || (inp == NULL)) {
1625 SCTP_STAT_INCR(sctps_timoshutdownguard);
1626 sctp_abort_an_association(inp, stcb,
1627 SCTP_SHUTDOWN_GUARD_EXPIRES, NULL);
1628 /* no need to unlock on tcb its gone */
1631 case SCTP_TIMER_TYPE_STRRESET:
1632 if ((stcb == NULL) || (inp == NULL)) {
1635 if (sctp_strreset_timer(inp, stcb, net)) {
1636 /* no need to unlock on tcb its gone */
1639 SCTP_STAT_INCR(sctps_timostrmrst);
1640 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR);
1642 case SCTP_TIMER_TYPE_EARLYFR:
1643 /* Need to do FR of things for net */
1644 if ((stcb == NULL) || (inp == NULL)) {
1647 SCTP_STAT_INCR(sctps_timoearlyfr);
1648 sctp_early_fr_timer(inp, stcb, net);
1650 case SCTP_TIMER_TYPE_ASCONF:
1651 if ((stcb == NULL) || (inp == NULL)) {
1654 if (sctp_asconf_timer(inp, stcb, net)) {
1655 /* no need to unlock on tcb its gone */
1658 SCTP_STAT_INCR(sctps_timoasconf);
1659 #ifdef SCTP_AUDITING_ENABLED
1660 sctp_auditing(4, inp, stcb, net);
1662 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR);
1665 case SCTP_TIMER_TYPE_AUTOCLOSE:
1666 if ((stcb == NULL) || (inp == NULL)) {
1669 SCTP_STAT_INCR(sctps_timoautoclose);
1670 sctp_autoclose_timer(inp, stcb, net);
1671 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR);
1674 case SCTP_TIMER_TYPE_ASOCKILL:
1675 if ((stcb == NULL) || (inp == NULL)) {
1678 SCTP_STAT_INCR(sctps_timoassockill);
1679 /* Can we free it yet? */
1680 SCTP_INP_DECR_REF(inp);
1681 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1682 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1684 * free asoc, always unlocks (or destroy's) so prevent
1685 * duplicate unlock or unlock of a free mtx :-0
1689 case SCTP_TIMER_TYPE_INPKILL:
1690 SCTP_STAT_INCR(sctps_timoinpkill);
1695 * special case, take away our increment since WE are the
1698 SCTP_INP_DECR_REF(inp);
1699 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1700 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1701 SCTP_CALLED_DIRECTLY_NOCMPSET);
1704 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1708 #ifdef SCTP_AUDITING_ENABLED
1709 sctp_audit_log(0xF1, (uint8_t) tmr->type);
1711 sctp_auditing(5, inp, stcb, net);
1713 if ((did_output) && stcb) {
1715 * Now we need to clean up the control chunk chain if an
1716 * ECNE is on it. It must be marked as UNSENT again so next
1717 * call will continue to send it until such time that we get
1718 * a CWR, to remove it. It is, however, less likely that we
1719 * will find a ecn echo on the chain though.
1721 sctp_fix_ecn_echo(&stcb->asoc);
1725 SCTP_TCB_UNLOCK(stcb);
1729 SCTP_INP_DECR_REF(inp);
1732 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1739 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1740 struct sctp_nets *net)
1743 struct sctp_timer *tmr;
1745 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1752 SCTP_TCB_LOCK_ASSERT(stcb);
1755 case SCTP_TIMER_TYPE_ZERO_COPY:
1756 tmr = &inp->sctp_ep.zero_copy_timer;
1757 to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1759 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1760 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1761 to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1763 case SCTP_TIMER_TYPE_ADDR_WQ:
1764 /* Only 1 tick away :-) */
1765 tmr = &sctppcbinfo.addr_wq_timer;
1766 to_ticks = SCTP_ADDRESS_TICK_DELAY;
1768 case SCTP_TIMER_TYPE_ITERATOR:
1770 struct sctp_iterator *it;
1772 it = (struct sctp_iterator *)inp;
1774 to_ticks = SCTP_ITERATOR_TICKS;
1777 case SCTP_TIMER_TYPE_SEND:
1778 /* Here we use the RTO timer */
1782 if ((stcb == NULL) || (net == NULL)) {
1785 tmr = &net->rxt_timer;
1786 if (net->RTO == 0) {
1787 rto_val = stcb->asoc.initial_rto;
1791 to_ticks = MSEC_TO_TICKS(rto_val);
1794 case SCTP_TIMER_TYPE_INIT:
1796 * Here we use the INIT timer default usually about 1
1799 if ((stcb == NULL) || (net == NULL)) {
1802 tmr = &net->rxt_timer;
1803 if (net->RTO == 0) {
1804 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1806 to_ticks = MSEC_TO_TICKS(net->RTO);
1809 case SCTP_TIMER_TYPE_RECV:
1811 * Here we use the Delayed-Ack timer value from the inp
1812 * ususually about 200ms.
1817 tmr = &stcb->asoc.dack_timer;
1818 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1820 case SCTP_TIMER_TYPE_SHUTDOWN:
1821 /* Here we use the RTO of the destination. */
1822 if ((stcb == NULL) || (net == NULL)) {
1825 if (net->RTO == 0) {
1826 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1828 to_ticks = MSEC_TO_TICKS(net->RTO);
1830 tmr = &net->rxt_timer;
1832 case SCTP_TIMER_TYPE_HEARTBEAT:
1834 * the net is used here so that we can add in the RTO. Even
1835 * though we use a different timer. We also add the HB timer
1836 * PLUS a random jitter.
1838 if ((inp == NULL) || (stcb == NULL)) {
1842 uint8_t this_random;
1843 int cnt_of_unconf = 0;
1844 struct sctp_nets *lnet;
1846 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1847 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1848 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1852 if (cnt_of_unconf) {
1854 (void)sctp_heartbeat_timer(inp, stcb, lnet, cnt_of_unconf);
1856 if (stcb->asoc.hb_random_idx > 3) {
1857 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
1858 memcpy(stcb->asoc.hb_random_values, &rndval,
1859 sizeof(stcb->asoc.hb_random_values));
1860 stcb->asoc.hb_random_idx = 0;
1862 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
1863 stcb->asoc.hb_random_idx++;
1864 stcb->asoc.hb_ect_randombit = 0;
1866 * this_random will be 0 - 256 ms RTO is in ms.
1868 if ((stcb->asoc.hb_is_disabled) &&
1869 (cnt_of_unconf == 0)) {
1875 delay = stcb->asoc.heart_beat_delay;
1876 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1877 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1878 ((lnet->dest_state & SCTP_ADDR_OUT_OF_SCOPE) == 0) &&
1879 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1883 if (net->RTO == 0) {
1884 /* Never been checked */
1885 to_ticks = this_random + stcb->asoc.initial_rto + delay;
1887 /* set rto_val to the ms */
1888 to_ticks = delay + net->RTO + this_random;
1891 if (cnt_of_unconf) {
1892 to_ticks = this_random + stcb->asoc.initial_rto;
1894 to_ticks = stcb->asoc.heart_beat_delay + this_random + stcb->asoc.initial_rto;
1898 * Now we must convert the to_ticks that are now in
1901 to_ticks = MSEC_TO_TICKS(to_ticks);
1902 tmr = &stcb->asoc.hb_timer;
1905 case SCTP_TIMER_TYPE_COOKIE:
1907 * Here we can use the RTO timer from the network since one
1908 * RTT was compelete. If a retran happened then we will be
1909 * using the RTO initial value.
1911 if ((stcb == NULL) || (net == NULL)) {
1914 if (net->RTO == 0) {
1915 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1917 to_ticks = MSEC_TO_TICKS(net->RTO);
1919 tmr = &net->rxt_timer;
1921 case SCTP_TIMER_TYPE_NEWCOOKIE:
1923 * nothing needed but the endpoint here ususually about 60
1929 tmr = &inp->sctp_ep.signature_change;
1930 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
1932 case SCTP_TIMER_TYPE_ASOCKILL:
1936 tmr = &stcb->asoc.strreset_timer;
1937 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
1939 case SCTP_TIMER_TYPE_INPKILL:
1941 * The inp is setup to die. We re-use the signature_chage
1942 * timer since that has stopped and we are in the GONE
1948 tmr = &inp->sctp_ep.signature_change;
1949 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
1951 case SCTP_TIMER_TYPE_PATHMTURAISE:
1953 * Here we use the value found in the EP for PMTU ususually
1956 if ((stcb == NULL) || (inp == NULL)) {
1962 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
1963 tmr = &net->pmtu_timer;
1965 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1966 /* Here we use the RTO of the destination */
1967 if ((stcb == NULL) || (net == NULL)) {
1970 if (net->RTO == 0) {
1971 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1973 to_ticks = MSEC_TO_TICKS(net->RTO);
1975 tmr = &net->rxt_timer;
1977 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1979 * Here we use the endpoints shutdown guard timer usually
1982 if ((inp == NULL) || (stcb == NULL)) {
1985 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
1986 tmr = &stcb->asoc.shut_guard_timer;
1988 case SCTP_TIMER_TYPE_STRRESET:
1990 * Here the timer comes from the inp but its value is from
1993 if ((stcb == NULL) || (net == NULL)) {
1996 if (net->RTO == 0) {
1997 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1999 to_ticks = MSEC_TO_TICKS(net->RTO);
2001 tmr = &stcb->asoc.strreset_timer;
2004 case SCTP_TIMER_TYPE_EARLYFR:
2008 if ((stcb == NULL) || (net == NULL)) {
2011 if (net->flight_size > net->cwnd) {
2012 /* no need to start */
2015 SCTP_STAT_INCR(sctps_earlyfrstart);
2016 if (net->lastsa == 0) {
2017 /* Hmm no rtt estimate yet? */
2018 msec = stcb->asoc.initial_rto >> 2;
2020 msec = ((net->lastsa >> 2) + net->lastsv) >> 1;
2022 if (msec < sctp_early_fr_msec) {
2023 msec = sctp_early_fr_msec;
2024 if (msec < SCTP_MINFR_MSEC_FLOOR) {
2025 msec = SCTP_MINFR_MSEC_FLOOR;
2028 to_ticks = MSEC_TO_TICKS(msec);
2029 tmr = &net->fr_timer;
2032 case SCTP_TIMER_TYPE_ASCONF:
2034 * Here the timer comes from the inp but its value is from
2037 if ((stcb == NULL) || (net == NULL)) {
2040 if (net->RTO == 0) {
2041 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2043 to_ticks = MSEC_TO_TICKS(net->RTO);
2045 tmr = &stcb->asoc.asconf_timer;
2047 case SCTP_TIMER_TYPE_AUTOCLOSE:
2051 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2053 * Really an error since stcb is NOT set to
2058 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2059 tmr = &stcb->asoc.autoclose_timer;
2062 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2063 __FUNCTION__, t_type);
2067 if ((to_ticks <= 0) || (tmr == NULL)) {
2068 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2069 __FUNCTION__, t_type, to_ticks, tmr);
2072 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2074 * we do NOT allow you to have it already running. if it is
2075 * we leave the current one up unchanged
2079 /* At this point we can proceed */
2080 if (t_type == SCTP_TIMER_TYPE_SEND) {
2081 stcb->asoc.num_send_timers_up++;
2083 tmr->stopped_from = 0;
2085 tmr->ep = (void *)inp;
2086 tmr->tcb = (void *)stcb;
2087 tmr->net = (void *)net;
2088 tmr->self = (void *)tmr;
2090 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2095 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2096 struct sctp_nets *net, uint32_t from)
2098 struct sctp_timer *tmr;
2100 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2106 SCTP_TCB_LOCK_ASSERT(stcb);
2109 case SCTP_TIMER_TYPE_ZERO_COPY:
2110 tmr = &inp->sctp_ep.zero_copy_timer;
2112 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2113 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2115 case SCTP_TIMER_TYPE_ADDR_WQ:
2116 tmr = &sctppcbinfo.addr_wq_timer;
2118 case SCTP_TIMER_TYPE_EARLYFR:
2119 if ((stcb == NULL) || (net == NULL)) {
2122 tmr = &net->fr_timer;
2123 SCTP_STAT_INCR(sctps_earlyfrstop);
2125 case SCTP_TIMER_TYPE_ITERATOR:
2127 struct sctp_iterator *it;
2129 it = (struct sctp_iterator *)inp;
2133 case SCTP_TIMER_TYPE_SEND:
2134 if ((stcb == NULL) || (net == NULL)) {
2137 tmr = &net->rxt_timer;
2139 case SCTP_TIMER_TYPE_INIT:
2140 if ((stcb == NULL) || (net == NULL)) {
2143 tmr = &net->rxt_timer;
2145 case SCTP_TIMER_TYPE_RECV:
2149 tmr = &stcb->asoc.dack_timer;
2151 case SCTP_TIMER_TYPE_SHUTDOWN:
2152 if ((stcb == NULL) || (net == NULL)) {
2155 tmr = &net->rxt_timer;
2157 case SCTP_TIMER_TYPE_HEARTBEAT:
2161 tmr = &stcb->asoc.hb_timer;
2163 case SCTP_TIMER_TYPE_COOKIE:
2164 if ((stcb == NULL) || (net == NULL)) {
2167 tmr = &net->rxt_timer;
2169 case SCTP_TIMER_TYPE_NEWCOOKIE:
2170 /* nothing needed but the endpoint here */
2171 tmr = &inp->sctp_ep.signature_change;
2173 * We re-use the newcookie timer for the INP kill timer. We
2174 * must assure that we do not kill it by accident.
2177 case SCTP_TIMER_TYPE_ASOCKILL:
2179 * Stop the asoc kill timer.
2184 tmr = &stcb->asoc.strreset_timer;
2187 case SCTP_TIMER_TYPE_INPKILL:
2189 * The inp is setup to die. We re-use the signature_chage
2190 * timer since that has stopped and we are in the GONE
2193 tmr = &inp->sctp_ep.signature_change;
2195 case SCTP_TIMER_TYPE_PATHMTURAISE:
2196 if ((stcb == NULL) || (net == NULL)) {
2199 tmr = &net->pmtu_timer;
2201 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2202 if ((stcb == NULL) || (net == NULL)) {
2205 tmr = &net->rxt_timer;
2207 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2211 tmr = &stcb->asoc.shut_guard_timer;
2213 case SCTP_TIMER_TYPE_STRRESET:
2217 tmr = &stcb->asoc.strreset_timer;
2219 case SCTP_TIMER_TYPE_ASCONF:
2223 tmr = &stcb->asoc.asconf_timer;
2225 case SCTP_TIMER_TYPE_AUTOCLOSE:
2229 tmr = &stcb->asoc.autoclose_timer;
2232 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2233 __FUNCTION__, t_type);
2239 if ((tmr->type != t_type) && tmr->type) {
2241 * Ok we have a timer that is under joint use. Cookie timer
2242 * per chance with the SEND timer. We therefore are NOT
2243 * running the timer that the caller wants stopped. So just
2248 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2249 stcb->asoc.num_send_timers_up--;
2250 if (stcb->asoc.num_send_timers_up < 0) {
2251 stcb->asoc.num_send_timers_up = 0;
2255 tmr->stopped_from = from;
2256 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2260 #ifdef SCTP_USE_ADLER32
2262 update_adler32(uint32_t adler, uint8_t * buf, int32_t len)
2264 uint32_t s1 = adler & 0xffff;
2265 uint32_t s2 = (adler >> 16) & 0xffff;
2268 for (n = 0; n < len; n++, buf++) {
2269 /* s1 = (s1 + buf[n]) % BASE */
2273 * now if we need to, we do a mod by subtracting. It seems a
2274 * bit faster since I really will only ever do one subtract
2275 * at the MOST, since buf[n] is a max of 255.
2277 if (s1 >= SCTP_ADLER32_BASE) {
2278 s1 -= SCTP_ADLER32_BASE;
2280 /* s2 = (s2 + s1) % BASE */
2284 * again, it is more efficent (it seems) to subtract since
2285 * the most s2 will ever be is (BASE-1 + BASE-1) in the
2286 * worse case. This would then be (2 * BASE) - 2, which will
2287 * still only do one subtract. On Intel this is much better
2288 * to do this way and avoid the divide. Have not -pg'd on
2291 if (s2 >= SCTP_ADLER32_BASE) {
2292 s2 -= SCTP_ADLER32_BASE;
2295 /* Return the adler32 of the bytes buf[0..len-1] */
2296 return ((s2 << 16) + s1);
2303 sctp_calculate_len(struct mbuf *m)
2310 tlen += SCTP_BUF_LEN(at);
2311 at = SCTP_BUF_NEXT(at);
2316 #if defined(SCTP_WITH_NO_CSUM)
2319 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2322 * given a mbuf chain with a packetheader offset by 'offset'
2323 * pointing at a sctphdr (with csum set to 0) go through the chain
2324 * of SCTP_BUF_NEXT()'s and calculate the SCTP checksum. This also
2325 * has a side bonus as it will calculate the total length of the
2326 * mbuf chain. Note: if offset is greater than the total mbuf
2327 * length, checksum=1, pktlen=0 is returned (ie. no real error code)
2331 *pktlen = sctp_calculate_len(m);
2335 #elif defined(SCTP_USE_INCHKSUM)
2337 #include <machine/in_cksum.h>
2340 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2343 * given a mbuf chain with a packetheader offset by 'offset'
2344 * pointing at a sctphdr (with csum set to 0) go through the chain
2345 * of SCTP_BUF_NEXT()'s and calculate the SCTP checksum. This also
2346 * has a side bonus as it will calculate the total length of the
2347 * mbuf chain. Note: if offset is greater than the total mbuf
2348 * length, checksum=1, pktlen=0 is returned (ie. no real error code)
2352 uint32_t the_sum, retsum;
2356 tlen += SCTP_BUF_LEN(at);
2357 at = SCTP_BUF_NEXT(at);
2359 the_sum = (uint32_t) (in_cksum_skip(m, tlen, offset));
2361 *pktlen = (tlen - offset);
2362 retsum = htons(the_sum);
2369 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2372 * given a mbuf chain with a packetheader offset by 'offset'
2373 * pointing at a sctphdr (with csum set to 0) go through the chain
2374 * of SCTP_BUF_NEXT()'s and calculate the SCTP checksum. This also
2375 * has a side bonus as it will calculate the total length of the
2376 * mbuf chain. Note: if offset is greater than the total mbuf
2377 * length, checksum=1, pktlen=0 is returned (ie. no real error code)
2381 #ifdef SCTP_USE_ADLER32
2385 uint32_t base = 0xffffffff;
2391 /* find the correct mbuf and offset into mbuf */
2392 while ((at != NULL) && (offset > (uint32_t) SCTP_BUF_LEN(at))) {
2393 offset -= SCTP_BUF_LEN(at); /* update remaining offset
2395 at = SCTP_BUF_NEXT(at);
2397 while (at != NULL) {
2398 if ((SCTP_BUF_LEN(at) - offset) > 0) {
2399 #ifdef SCTP_USE_ADLER32
2400 base = update_adler32(base,
2401 (unsigned char *)(SCTP_BUF_AT(at, offset)),
2402 (unsigned int)(SCTP_BUF_LEN(at) - offset));
2404 if ((SCTP_BUF_LEN(at) - offset) < 4) {
2405 /* Use old method if less than 4 bytes */
2406 base = old_update_crc32(base,
2407 (unsigned char *)(SCTP_BUF_AT(at, offset)),
2408 (unsigned int)(SCTP_BUF_LEN(at) - offset));
2410 base = update_crc32(base,
2411 (unsigned char *)(SCTP_BUF_AT(at, offset)),
2412 (unsigned int)(SCTP_BUF_LEN(at) - offset));
2415 tlen += SCTP_BUF_LEN(at) - offset;
2416 /* we only offset once into the first mbuf */
2419 if (offset < (uint32_t) SCTP_BUF_LEN(at))
2422 offset -= SCTP_BUF_LEN(at);
2424 at = SCTP_BUF_NEXT(at);
2426 if (pktlen != NULL) {
2429 #ifdef SCTP_USE_ADLER32
2434 base = sctp_csum_finalize(base);
2443 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2444 struct sctp_association *asoc, uint32_t mtu)
2447 * Reset the P-MTU size on this association, this involves changing
2448 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2449 * allow the DF flag to be cleared.
2451 struct sctp_tmit_chunk *chk;
2452 unsigned int eff_mtu, ovh;
2454 #ifdef SCTP_PRINT_FOR_B_AND_M
2455 SCTP_PRINTF("sctp_mtu_size_reset(%p, asoc:%p mtu:%d\n",
2458 asoc->smallest_mtu = mtu;
2459 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2460 ovh = SCTP_MIN_OVERHEAD;
2462 ovh = SCTP_MIN_V4_OVERHEAD;
2464 eff_mtu = mtu - ovh;
2465 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2467 if (chk->send_size > eff_mtu) {
2468 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2471 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2472 if (chk->send_size > eff_mtu) {
2473 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2480 * given an association and starting time of the current RTT period return
2481 * RTO in number of msecs net should point to the current network
2484 sctp_calculate_rto(struct sctp_tcb *stcb,
2485 struct sctp_association *asoc,
2486 struct sctp_nets *net,
2487 struct timeval *old)
2490 * given an association and the starting time of the current RTT
2491 * period (in value1/value2) return RTO in number of msecs.
2495 uint32_t new_rto = 0;
2496 int first_measure = 0;
2499 /************************/
2500 /* 1. calculate new RTT */
2501 /************************/
2502 /* get the current time */
2503 (void)SCTP_GETTIME_TIMEVAL(&now);
2504 /* compute the RTT value */
2505 if ((u_long)now.tv_sec > (u_long)old->tv_sec) {
2506 calc_time = ((u_long)now.tv_sec - (u_long)old->tv_sec) * 1000;
2507 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2508 calc_time += (((u_long)now.tv_usec -
2509 (u_long)old->tv_usec) / 1000);
2510 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2511 /* Borrow 1,000ms from current calculation */
2513 /* Add in the slop over */
2514 calc_time += ((int)now.tv_usec / 1000);
2515 /* Add in the pre-second ms's */
2516 calc_time += (((int)1000000 - (int)old->tv_usec) / 1000);
2518 } else if ((u_long)now.tv_sec == (u_long)old->tv_sec) {
2519 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2520 calc_time = ((u_long)now.tv_usec -
2521 (u_long)old->tv_usec) / 1000;
2522 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2523 /* impossible .. garbage in nothing out */
2525 } else if ((u_long)now.tv_usec == (u_long)old->tv_usec) {
2527 * We have to have 1 usec :-D this must be the
2532 /* impossible .. garbage in nothing out */
2536 /* Clock wrapped? */
2539 /***************************/
2540 /* 2. update RTTVAR & SRTT */
2541 /***************************/
2542 o_calctime = calc_time;
2543 /* this is Van Jacobson's integer version */
2544 if (net->RTO_measured) {
2545 calc_time -= (net->lastsa >> SCTP_RTT_SHIFT); /* take away 1/8th when
2547 if (sctp_logging_level & SCTP_RTTVAR_LOGGING_ENABLE) {
2548 rto_logging(net, SCTP_LOG_RTTVAR);
2550 net->prev_rtt = o_calctime;
2551 net->lastsa += calc_time; /* add 7/8th into sa when
2553 if (calc_time < 0) {
2554 calc_time = -calc_time;
2556 calc_time -= (net->lastsv >> SCTP_RTT_VAR_SHIFT); /* take away 1/4 when
2558 net->lastsv += calc_time;
2559 if (net->lastsv == 0) {
2560 net->lastsv = SCTP_CLOCK_GRANULARITY;
2563 /* First RTO measurment */
2564 net->RTO_measured = 1;
2565 net->lastsa = calc_time << SCTP_RTT_SHIFT; /* Multiply by 8 when
2567 net->lastsv = calc_time;
2568 if (net->lastsv == 0) {
2569 net->lastsv = SCTP_CLOCK_GRANULARITY;
2572 net->prev_rtt = o_calctime;
2573 if (sctp_logging_level & SCTP_RTTVAR_LOGGING_ENABLE) {
2574 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2578 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2579 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2580 (stcb->asoc.sat_network_lockout == 0)) {
2581 stcb->asoc.sat_network = 1;
2582 } else if ((!first_measure) && stcb->asoc.sat_network) {
2583 stcb->asoc.sat_network = 0;
2584 stcb->asoc.sat_network_lockout = 1;
2586 /* bound it, per C6/C7 in Section 5.3.1 */
2587 if (new_rto < stcb->asoc.minrto) {
2588 new_rto = stcb->asoc.minrto;
2590 if (new_rto > stcb->asoc.maxrto) {
2591 new_rto = stcb->asoc.maxrto;
2593 /* we are now returning the RTO */
2598 * return a pointer to a contiguous piece of data from the given mbuf chain
2599 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2600 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2601 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2604 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2610 if ((off < 0) || (len <= 0))
2613 /* find the desired start location */
2614 while ((m != NULL) && (off > 0)) {
2615 if (off < SCTP_BUF_LEN(m))
2617 off -= SCTP_BUF_LEN(m);
2618 m = SCTP_BUF_NEXT(m);
2623 /* is the current mbuf large enough (eg. contiguous)? */
2624 if ((SCTP_BUF_LEN(m) - off) >= len) {
2625 return (mtod(m, caddr_t)+off);
2627 /* else, it spans more than one mbuf, so save a temp copy... */
2628 while ((m != NULL) && (len > 0)) {
2629 count = min(SCTP_BUF_LEN(m) - off, len);
2630 bcopy(mtod(m, caddr_t)+off, ptr, count);
2634 m = SCTP_BUF_NEXT(m);
2636 if ((m == NULL) && (len > 0))
2639 return ((caddr_t)in_ptr);
2645 struct sctp_paramhdr *
2646 sctp_get_next_param(struct mbuf *m,
2648 struct sctp_paramhdr *pull,
2651 /* This just provides a typed signature to Peter's Pull routine */
2652 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2658 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2661 * add padlen bytes of 0 filled padding to the end of the mbuf. If
2662 * padlen is > 3 this routine will fail.
2670 if (M_TRAILINGSPACE(m)) {
2672 * The easy way. We hope the majority of the time we hit
2675 dp = (uint8_t *) (mtod(m, caddr_t)+SCTP_BUF_LEN(m));
2676 SCTP_BUF_LEN(m) += padlen;
2678 /* Hard way we must grow the mbuf */
2681 tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA);
2683 /* Out of space GAK! we are in big trouble. */
2686 /* setup and insert in middle */
2687 SCTP_BUF_NEXT(tmp) = SCTP_BUF_NEXT(m);
2688 SCTP_BUF_LEN(tmp) = padlen;
2689 SCTP_BUF_NEXT(m) = tmp;
2690 dp = mtod(tmp, uint8_t *);
2692 /* zero out the pad */
2693 for (i = 0; i < padlen; i++) {
2701 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2703 /* find the last mbuf in chain and pad it */
2708 return (sctp_add_pad_tombuf(last_mbuf, padval));
2711 if (SCTP_BUF_NEXT(m_at) == NULL) {
2712 return (sctp_add_pad_tombuf(m_at, padval));
2714 m_at = SCTP_BUF_NEXT(m_at);
2720 int sctp_asoc_change_wake = 0;
2723 sctp_notify_assoc_change(uint32_t event, struct sctp_tcb *stcb,
2724 uint32_t error, void *data)
2726 struct mbuf *m_notify;
2727 struct sctp_assoc_change *sac;
2728 struct sctp_queued_to_read *control;
2731 * First if we are are going down dump everything we can to the
2735 if ((stcb == NULL) ||
2736 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
2737 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
2738 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
2740 /* If the socket is gone we are out of here */
2744 * For TCP model AND UDP connected sockets we will send an error up
2745 * when an ABORT comes in.
2747 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2748 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2749 ((event == SCTP_COMM_LOST) || (event == SCTP_CANT_STR_ASSOC))) {
2750 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT)
2751 stcb->sctp_socket->so_error = ECONNREFUSED;
2753 stcb->sctp_socket->so_error = ECONNRESET;
2754 /* Wake ANY sleepers */
2755 sorwakeup(stcb->sctp_socket);
2756 sowwakeup(stcb->sctp_socket);
2757 sctp_asoc_change_wake++;
2759 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2760 /* event not enabled */
2763 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_change), 0, M_DONTWAIT, 1, MT_DATA);
2764 if (m_notify == NULL)
2767 SCTP_BUF_LEN(m_notify) = 0;
2769 sac = mtod(m_notify, struct sctp_assoc_change *);
2770 sac->sac_type = SCTP_ASSOC_CHANGE;
2772 sac->sac_length = sizeof(struct sctp_assoc_change);
2773 sac->sac_state = event;
2774 sac->sac_error = error;
2775 /* XXX verify these stream counts */
2776 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2777 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2778 sac->sac_assoc_id = sctp_get_associd(stcb);
2779 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_change);
2780 SCTP_BUF_NEXT(m_notify) = NULL;
2781 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2784 if (control == NULL) {
2786 sctp_m_freem(m_notify);
2789 control->length = SCTP_BUF_LEN(m_notify);
2790 /* not that we need this */
2791 control->tail_mbuf = m_notify;
2792 control->spec_flags = M_NOTIFICATION;
2793 sctp_add_to_readq(stcb->sctp_ep, stcb,
2795 &stcb->sctp_socket->so_rcv, 1);
2796 if (event == SCTP_COMM_LOST) {
2797 /* Wake up any sleeper */
2798 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
2803 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2804 struct sockaddr *sa, uint32_t error)
2806 struct mbuf *m_notify;
2807 struct sctp_paddr_change *spc;
2808 struct sctp_queued_to_read *control;
2810 if ((stcb == NULL) || (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVPADDREVNT)))
2811 /* event not enabled */
2814 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_DONTWAIT, 1, MT_DATA);
2815 if (m_notify == NULL)
2817 SCTP_BUF_LEN(m_notify) = 0;
2818 spc = mtod(m_notify, struct sctp_paddr_change *);
2819 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2821 spc->spc_length = sizeof(struct sctp_paddr_change);
2822 if (sa->sa_family == AF_INET) {
2823 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2825 struct sockaddr_in6 *sin6;
2827 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2829 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2830 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2831 if (sin6->sin6_scope_id == 0) {
2832 /* recover scope_id for user */
2833 (void)sa6_recoverscope(sin6);
2835 /* clear embedded scope_id for user */
2836 in6_clearscope(&sin6->sin6_addr);
2840 spc->spc_state = state;
2841 spc->spc_error = error;
2842 spc->spc_assoc_id = sctp_get_associd(stcb);
2844 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2845 SCTP_BUF_NEXT(m_notify) = NULL;
2847 /* append to socket */
2848 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2851 if (control == NULL) {
2853 sctp_m_freem(m_notify);
2856 control->length = SCTP_BUF_LEN(m_notify);
2857 control->spec_flags = M_NOTIFICATION;
2858 /* not that we need this */
2859 control->tail_mbuf = m_notify;
2860 sctp_add_to_readq(stcb->sctp_ep, stcb,
2862 &stcb->sctp_socket->so_rcv, 1);
2867 sctp_notify_send_failed(struct sctp_tcb *stcb, uint32_t error,
2868 struct sctp_tmit_chunk *chk)
2870 struct mbuf *m_notify;
2871 struct sctp_send_failed *ssf;
2872 struct sctp_queued_to_read *control;
2875 if ((stcb == NULL) || (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)))
2876 /* event not enabled */
2879 length = sizeof(struct sctp_send_failed) + chk->send_size;
2880 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
2881 if (m_notify == NULL)
2884 SCTP_BUF_LEN(m_notify) = 0;
2885 ssf = mtod(m_notify, struct sctp_send_failed *);
2886 ssf->ssf_type = SCTP_SEND_FAILED;
2887 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2888 ssf->ssf_flags = SCTP_DATA_UNSENT;
2890 ssf->ssf_flags = SCTP_DATA_SENT;
2891 ssf->ssf_length = length;
2892 ssf->ssf_error = error;
2893 /* not exactly what the user sent in, but should be close :) */
2894 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2895 ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2896 ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2897 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2898 ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2899 ssf->ssf_info.sinfo_context = chk->rec.data.context;
2900 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2901 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2902 SCTP_BUF_NEXT(m_notify) = chk->data;
2903 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
2905 /* Steal off the mbuf */
2908 * For this case, we check the actual socket buffer, since the assoc
2909 * is going away we don't want to overfill the socket buffer for a
2912 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2913 sctp_m_freem(m_notify);
2916 /* append to socket */
2917 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2920 if (control == NULL) {
2922 sctp_m_freem(m_notify);
2925 control->spec_flags = M_NOTIFICATION;
2926 sctp_add_to_readq(stcb->sctp_ep, stcb,
2928 &stcb->sctp_socket->so_rcv, 1);
2933 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
2934 struct sctp_stream_queue_pending *sp)
2936 struct mbuf *m_notify;
2937 struct sctp_send_failed *ssf;
2938 struct sctp_queued_to_read *control;
2941 if ((stcb == NULL) || (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)))
2942 /* event not enabled */
2945 length = sizeof(struct sctp_send_failed) + sp->length;
2946 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
2947 if (m_notify == NULL)
2950 SCTP_BUF_LEN(m_notify) = 0;
2951 ssf = mtod(m_notify, struct sctp_send_failed *);
2952 ssf->ssf_type = SCTP_SEND_FAILED;
2953 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2954 ssf->ssf_flags = SCTP_DATA_UNSENT;
2956 ssf->ssf_flags = SCTP_DATA_SENT;
2957 ssf->ssf_length = length;
2958 ssf->ssf_error = error;
2959 /* not exactly what the user sent in, but should be close :) */
2960 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2961 ssf->ssf_info.sinfo_stream = sp->stream;
2962 ssf->ssf_info.sinfo_ssn = sp->strseq;
2963 ssf->ssf_info.sinfo_flags = sp->sinfo_flags;
2964 ssf->ssf_info.sinfo_ppid = sp->ppid;
2965 ssf->ssf_info.sinfo_context = sp->context;
2966 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2967 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2968 SCTP_BUF_NEXT(m_notify) = sp->data;
2969 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
2971 /* Steal off the mbuf */
2974 * For this case, we check the actual socket buffer, since the assoc
2975 * is going away we don't want to overfill the socket buffer for a
2978 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2979 sctp_m_freem(m_notify);
2982 /* append to socket */
2983 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2986 if (control == NULL) {
2988 sctp_m_freem(m_notify);
2991 control->spec_flags = M_NOTIFICATION;
2992 sctp_add_to_readq(stcb->sctp_ep, stcb,
2994 &stcb->sctp_socket->so_rcv, 1);
3000 sctp_notify_adaptation_layer(struct sctp_tcb *stcb,
3003 struct mbuf *m_notify;
3004 struct sctp_adaptation_event *sai;
3005 struct sctp_queued_to_read *control;
3007 if ((stcb == NULL) || (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_ADAPTATIONEVNT)))
3008 /* event not enabled */
3011 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_DONTWAIT, 1, MT_DATA);
3012 if (m_notify == NULL)
3015 SCTP_BUF_LEN(m_notify) = 0;
3016 sai = mtod(m_notify, struct sctp_adaptation_event *);
3017 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3019 sai->sai_length = sizeof(struct sctp_adaptation_event);
3020 sai->sai_adaptation_ind = error;
3021 sai->sai_assoc_id = sctp_get_associd(stcb);
3023 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3024 SCTP_BUF_NEXT(m_notify) = NULL;
3026 /* append to socket */
3027 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3030 if (control == NULL) {
3032 sctp_m_freem(m_notify);
3035 control->length = SCTP_BUF_LEN(m_notify);
3036 control->spec_flags = M_NOTIFICATION;
3037 /* not that we need this */
3038 control->tail_mbuf = m_notify;
3039 sctp_add_to_readq(stcb->sctp_ep, stcb,
3041 &stcb->sctp_socket->so_rcv, 1);
3044 /* This always must be called with the read-queue LOCKED in the INP */
3046 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb,
3047 uint32_t error, int nolock, uint32_t val)
3049 struct mbuf *m_notify;
3050 struct sctp_pdapi_event *pdapi;
3051 struct sctp_queued_to_read *control;
3054 if ((stcb == NULL) || sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_PDAPIEVNT))
3055 /* event not enabled */
3058 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_DONTWAIT, 1, MT_DATA);
3059 if (m_notify == NULL)
3062 SCTP_BUF_LEN(m_notify) = 0;
3063 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3064 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3065 pdapi->pdapi_flags = 0;
3066 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3067 pdapi->pdapi_indication = error;
3068 pdapi->pdapi_stream = (val >> 16);
3069 pdapi->pdapi_seq = (val & 0x0000ffff);
3070 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3072 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3073 SCTP_BUF_NEXT(m_notify) = NULL;
3074 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3077 if (control == NULL) {
3079 sctp_m_freem(m_notify);
3082 control->spec_flags = M_NOTIFICATION;
3083 control->length = SCTP_BUF_LEN(m_notify);
3084 /* not that we need this */
3085 control->tail_mbuf = m_notify;
3086 control->held_length = 0;
3087 control->length = 0;
3089 SCTP_INP_READ_LOCK(stcb->sctp_ep);
3091 sb = &stcb->sctp_socket->so_rcv;
3092 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
3093 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3095 sctp_sballoc(stcb, sb, m_notify);
3096 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
3097 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3099 atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
3100 control->end_added = 1;
3101 if (stcb->asoc.control_pdapi)
3102 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3104 /* we really should not see this case */
3105 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3108 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
3110 if (stcb->sctp_ep && stcb->sctp_socket) {
3111 /* This should always be the case */
3112 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3117 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3119 struct mbuf *m_notify;
3120 struct sctp_shutdown_event *sse;
3121 struct sctp_queued_to_read *control;
3124 * For TCP model AND UDP connected sockets we will send an error up
3125 * when an SHUTDOWN completes
3130 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3131 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3132 /* mark socket closed for read/write and wakeup! */
3133 socantsendmore(stcb->sctp_socket);
3135 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
3136 /* event not enabled */
3139 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_DONTWAIT, 1, MT_DATA);
3140 if (m_notify == NULL)
3143 sse = mtod(m_notify, struct sctp_shutdown_event *);
3144 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3146 sse->sse_length = sizeof(struct sctp_shutdown_event);
3147 sse->sse_assoc_id = sctp_get_associd(stcb);
3149 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3150 SCTP_BUF_NEXT(m_notify) = NULL;
3152 /* append to socket */
3153 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3156 if (control == NULL) {
3158 sctp_m_freem(m_notify);
3161 control->spec_flags = M_NOTIFICATION;
3162 control->length = SCTP_BUF_LEN(m_notify);
3163 /* not that we need this */
3164 control->tail_mbuf = m_notify;
3165 sctp_add_to_readq(stcb->sctp_ep, stcb,
3167 &stcb->sctp_socket->so_rcv, 1);
3171 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3172 int number_entries, uint16_t * list, int flag)
3174 struct mbuf *m_notify;
3175 struct sctp_queued_to_read *control;
3176 struct sctp_stream_reset_event *strreset;
3182 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
3183 /* event not enabled */
3186 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3187 if (m_notify == NULL)
3190 SCTP_BUF_LEN(m_notify) = 0;
3191 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3192 if (len > M_TRAILINGSPACE(m_notify)) {
3193 /* never enough room */
3194 sctp_m_freem(m_notify);
3197 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3198 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3199 if (number_entries == 0) {
3200 strreset->strreset_flags = flag | SCTP_STRRESET_ALL_STREAMS;
3202 strreset->strreset_flags = flag | SCTP_STRRESET_STREAM_LIST;
3204 strreset->strreset_length = len;
3205 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3206 if (number_entries) {
3209 for (i = 0; i < number_entries; i++) {
3210 strreset->strreset_list[i] = ntohs(list[i]);
3213 SCTP_BUF_LEN(m_notify) = len;
3214 SCTP_BUF_NEXT(m_notify) = NULL;
3215 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3217 sctp_m_freem(m_notify);
3220 /* append to socket */
3221 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3224 if (control == NULL) {
3226 sctp_m_freem(m_notify);
3229 control->spec_flags = M_NOTIFICATION;
3230 control->length = SCTP_BUF_LEN(m_notify);
3231 /* not that we need this */
3232 control->tail_mbuf = m_notify;
3233 sctp_add_to_readq(stcb->sctp_ep, stcb,
3235 &stcb->sctp_socket->so_rcv, 1);
3240 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3241 uint32_t error, void *data)
3247 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3248 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3249 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
3251 /* No notifications up when we are in a no socket state */
3254 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3255 /* Can't send up to a closed socket any notifications */
3258 if (stcb && ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3259 (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED))) {
3260 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3261 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3262 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3263 /* Don't report these in front states */
3267 if (stcb && (stcb->asoc.assoc_up_sent == 0) && (notification != SCTP_NOTIFY_ASSOC_UP)) {
3268 if ((notification != SCTP_NOTIFY_ASSOC_DOWN) &&
3269 (notification != SCTP_NOTIFY_ASSOC_ABORTED) &&
3270 (notification != SCTP_NOTIFY_SPECIAL_SP_FAIL) &&
3271 (notification != SCTP_NOTIFY_DG_FAIL) &&
3272 (notification != SCTP_NOTIFY_PEER_SHUTDOWN)) {
3273 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, 0, NULL);
3274 stcb->asoc.assoc_up_sent = 1;
3277 switch (notification) {
3278 case SCTP_NOTIFY_ASSOC_UP:
3279 if (stcb->asoc.assoc_up_sent == 0) {
3280 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL);
3281 stcb->asoc.assoc_up_sent = 1;
3284 case SCTP_NOTIFY_ASSOC_DOWN:
3285 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL);
3287 case SCTP_NOTIFY_INTERFACE_DOWN:
3289 struct sctp_nets *net;
3291 net = (struct sctp_nets *)data;
3292 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3293 (struct sockaddr *)&net->ro._l_addr, error);
3296 case SCTP_NOTIFY_INTERFACE_UP:
3298 struct sctp_nets *net;
3300 net = (struct sctp_nets *)data;
3301 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3302 (struct sockaddr *)&net->ro._l_addr, error);
3305 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3307 struct sctp_nets *net;
3309 net = (struct sctp_nets *)data;
3310 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3311 (struct sockaddr *)&net->ro._l_addr, error);
3314 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3315 sctp_notify_send_failed2(stcb, error,
3316 (struct sctp_stream_queue_pending *)data);
3318 case SCTP_NOTIFY_DG_FAIL:
3319 sctp_notify_send_failed(stcb, error,
3320 (struct sctp_tmit_chunk *)data);
3322 case SCTP_NOTIFY_ADAPTATION_INDICATION:
3323 /* Here the error is the adaptation indication */
3324 sctp_notify_adaptation_layer(stcb, error);
3326 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3330 val = *((uint32_t *) data);
3332 sctp_notify_partial_delivery_indication(stcb, error, 0, val);
3335 case SCTP_NOTIFY_STRDATA_ERR:
3337 case SCTP_NOTIFY_ASSOC_ABORTED:
3338 if ((stcb) && (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3339 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED))) {
3340 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, NULL);
3342 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, NULL);
3345 case SCTP_NOTIFY_PEER_OPENED_STREAM:
3347 case SCTP_NOTIFY_STREAM_OPENED_OK:
3349 case SCTP_NOTIFY_ASSOC_RESTART:
3350 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, data);
3352 case SCTP_NOTIFY_HB_RESP:
3354 case SCTP_NOTIFY_STR_RESET_SEND:
3355 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_OUTBOUND_STR);
3357 case SCTP_NOTIFY_STR_RESET_RECV:
3358 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_INBOUND_STR);
3360 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3361 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_OUTBOUND_STR | SCTP_STRRESET_FAILED));
3364 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3365 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_INBOUND_STR | SCTP_STRRESET_FAILED));
3368 case SCTP_NOTIFY_ASCONF_ADD_IP:
3369 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3372 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3373 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3376 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3377 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3380 case SCTP_NOTIFY_ASCONF_SUCCESS:
3382 case SCTP_NOTIFY_ASCONF_FAILED:
3384 case SCTP_NOTIFY_PEER_SHUTDOWN:
3385 sctp_notify_shutdown_event(stcb);
3387 case SCTP_NOTIFY_AUTH_NEW_KEY:
3388 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, error,
3389 (uint16_t) (uintptr_t) data);
3392 case SCTP_NOTIFY_AUTH_KEY_CONFLICT:
3393 sctp_notify_authentication(stcb, SCTP_AUTH_KEY_CONFLICT,
3394 error, (uint16_t) (uintptr_t) data);
3396 #endif /* not yet? remove? */
3400 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3401 __FUNCTION__, notification, notification);
3407 sctp_report_all_outbound(struct sctp_tcb *stcb, int holds_lock)
3409 struct sctp_association *asoc;
3410 struct sctp_stream_out *outs;
3411 struct sctp_tmit_chunk *chk;
3412 struct sctp_stream_queue_pending *sp;
3420 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3421 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3422 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3425 /* now through all the gunk freeing chunks */
3426 if (holds_lock == 0) {
3427 SCTP_TCB_SEND_LOCK(stcb);
3429 /* sent queue SHOULD be empty */
3430 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3431 chk = TAILQ_FIRST(&asoc->sent_queue);
3433 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3434 asoc->sent_queue_cnt--;
3437 * trim off the sctp chunk header(it should
3440 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3441 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3442 sctp_mbuf_crush(chk->data);
3443 chk->send_size -= sizeof(struct sctp_data_chunk);
3446 sctp_free_bufspace(stcb, asoc, chk, 1);
3447 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3448 SCTP_NOTIFY_DATAGRAM_SENT, chk);
3450 sctp_m_freem(chk->data);
3453 sctp_free_a_chunk(stcb, chk);
3454 /* sa_ignore FREED_MEMORY */
3455 chk = TAILQ_FIRST(&asoc->sent_queue);
3458 /* pending send queue SHOULD be empty */
3459 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3460 chk = TAILQ_FIRST(&asoc->send_queue);
3462 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3463 asoc->send_queue_cnt--;
3466 * trim off the sctp chunk header(it should
3469 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3470 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3471 sctp_mbuf_crush(chk->data);
3472 chk->send_size -= sizeof(struct sctp_data_chunk);
3475 sctp_free_bufspace(stcb, asoc, chk, 1);
3476 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, chk);
3478 sctp_m_freem(chk->data);
3481 sctp_free_a_chunk(stcb, chk);
3482 /* sa_ignore FREED_MEMORY */
3483 chk = TAILQ_FIRST(&asoc->send_queue);
3486 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3487 /* For each stream */
3488 outs = &stcb->asoc.strmout[i];
3489 /* clean up any sends there */
3490 stcb->asoc.locked_on_sending = NULL;
3491 sp = TAILQ_FIRST(&outs->outqueue);
3493 stcb->asoc.stream_queue_cnt--;
3494 TAILQ_REMOVE(&outs->outqueue, sp, next);
3495 sctp_free_spbufspace(stcb, asoc, sp);
3496 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3497 SCTP_NOTIFY_DATAGRAM_UNSENT, (void *)sp);
3499 sctp_m_freem(sp->data);
3503 sctp_free_remote_addr(sp->net);
3505 /* Free the chunk */
3506 sctp_free_a_strmoq(stcb, sp);
3507 /* sa_ignore FREED_MEMORY */
3508 sp = TAILQ_FIRST(&outs->outqueue);
3512 if (holds_lock == 0) {
3513 SCTP_TCB_SEND_UNLOCK(stcb);
3518 sctp_abort_notification(struct sctp_tcb *stcb, int error)
3524 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3525 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3526 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3529 /* Tell them we lost the asoc */
3530 sctp_report_all_outbound(stcb, 1);
3531 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3532 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3533 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3534 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3536 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_ABORTED, stcb, error, NULL);
3540 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3541 struct mbuf *m, int iphlen, struct sctphdr *sh, struct mbuf *op_err,
3548 /* We have a TCB to abort, send notification too */
3549 vtag = stcb->asoc.peer_vtag;
3550 sctp_abort_notification(stcb, 0);
3551 /* get the assoc vrf id and table id */
3552 vrf_id = stcb->asoc.vrf_id;
3554 sctp_send_abort(m, iphlen, sh, vtag, op_err, vrf_id);
3556 /* Ok, now lets free it */
3557 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3559 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3560 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3561 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3562 SCTP_CALLED_DIRECTLY_NOCMPSET);
3568 #ifdef SCTP_ASOCLOG_OF_TSNS
3570 sctp_print_out_track_log(struct sctp_tcb *stcb)
3574 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3575 SCTP_PRINTF("IN bound TSN log-aaa\n");
3576 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3577 SCTP_PRINTF("None rcvd\n");
3580 if (stcb->asoc.tsn_in_wrapped) {
3581 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3582 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3583 stcb->asoc.in_tsnlog[i].tsn,
3584 stcb->asoc.in_tsnlog[i].strm,
3585 stcb->asoc.in_tsnlog[i].seq,
3586 stcb->asoc.in_tsnlog[i].flgs,
3587 stcb->asoc.in_tsnlog[i].sz);
3590 if (stcb->asoc.tsn_in_at) {
3591 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3592 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3593 stcb->asoc.in_tsnlog[i].tsn,
3594 stcb->asoc.in_tsnlog[i].strm,
3595 stcb->asoc.in_tsnlog[i].seq,
3596 stcb->asoc.in_tsnlog[i].flgs,
3597 stcb->asoc.in_tsnlog[i].sz);
3601 SCTP_PRINTF("OUT bound TSN log-aaa\n");
3602 if ((stcb->asoc.tsn_out_at == 0) &&
3603 (stcb->asoc.tsn_out_wrapped == 0)) {
3604 SCTP_PRINTF("None sent\n");
3606 if (stcb->asoc.tsn_out_wrapped) {
3607 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3608 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3609 stcb->asoc.out_tsnlog[i].tsn,
3610 stcb->asoc.out_tsnlog[i].strm,
3611 stcb->asoc.out_tsnlog[i].seq,
3612 stcb->asoc.out_tsnlog[i].flgs,
3613 stcb->asoc.out_tsnlog[i].sz);
3616 if (stcb->asoc.tsn_out_at) {
3617 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3618 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3619 stcb->asoc.out_tsnlog[i].tsn,
3620 stcb->asoc.out_tsnlog[i].strm,
3621 stcb->asoc.out_tsnlog[i].seq,
3622 stcb->asoc.out_tsnlog[i].flgs,
3623 stcb->asoc.out_tsnlog[i].sz);
3631 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3632 int error, struct mbuf *op_err)
3637 /* Got to have a TCB */
3638 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3639 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3640 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3641 SCTP_CALLED_DIRECTLY_NOCMPSET);
3646 vtag = stcb->asoc.peer_vtag;
3647 /* notify the ulp */
3648 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)
3649 sctp_abort_notification(stcb, error);
3650 /* notify the peer */
3651 sctp_send_abort_tcb(stcb, op_err);
3652 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3653 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3654 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3655 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3657 /* now free the asoc */
3658 #ifdef SCTP_ASOCLOG_OF_TSNS
3659 sctp_print_out_track_log(stcb);
3661 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
3665 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
3666 struct sctp_inpcb *inp, struct mbuf *op_err, uint32_t vrf_id)
3668 struct sctp_chunkhdr *ch, chunk_buf;
3669 unsigned int chk_length;
3671 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
3672 /* Generate a TO address for future reference */
3673 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3674 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3675 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3676 SCTP_CALLED_DIRECTLY_NOCMPSET);
3679 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3680 sizeof(*ch), (uint8_t *) & chunk_buf);
3681 while (ch != NULL) {
3682 chk_length = ntohs(ch->chunk_length);
3683 if (chk_length < sizeof(*ch)) {
3684 /* break to abort land */
3687 switch (ch->chunk_type) {
3688 case SCTP_PACKET_DROPPED:
3689 /* we don't respond to pkt-dropped */
3691 case SCTP_ABORT_ASSOCIATION:
3692 /* we don't respond with an ABORT to an ABORT */
3694 case SCTP_SHUTDOWN_COMPLETE:
3696 * we ignore it since we are not waiting for it and
3700 case SCTP_SHUTDOWN_ACK:
3701 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
3706 offset += SCTP_SIZE32(chk_length);
3707 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3708 sizeof(*ch), (uint8_t *) & chunk_buf);
3710 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id);
3714 * check the inbound datagram to make sure there is not an abort inside it,
3715 * if there is return 1, else return 0.
3718 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
3720 struct sctp_chunkhdr *ch;
3721 struct sctp_init_chunk *init_chk, chunk_buf;
3723 unsigned int chk_length;
3725 offset = iphlen + sizeof(struct sctphdr);
3726 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
3727 (uint8_t *) & chunk_buf);
3728 while (ch != NULL) {
3729 chk_length = ntohs(ch->chunk_length);
3730 if (chk_length < sizeof(*ch)) {
3731 /* packet is probably corrupt */
3734 /* we seem to be ok, is it an abort? */
3735 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
3736 /* yep, tell them */
3739 if (ch->chunk_type == SCTP_INITIATION) {
3740 /* need to update the Vtag */
3741 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
3742 offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
3743 if (init_chk != NULL) {
3744 *vtagfill = ntohl(init_chk->init.initiate_tag);
3747 /* Nope, move to the next chunk */
3748 offset += SCTP_SIZE32(chk_length);
3749 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3750 sizeof(*ch), (uint8_t *) & chunk_buf);
3756 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
3757 * set (i.e. it's 0) so, create this function to compare link local scopes
3760 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
3762 struct sockaddr_in6 a, b;
3768 if (a.sin6_scope_id == 0)
3769 if (sa6_recoverscope(&a)) {
3770 /* can't get scope, so can't match */
3773 if (b.sin6_scope_id == 0)
3774 if (sa6_recoverscope(&b)) {
3775 /* can't get scope, so can't match */
3778 if (a.sin6_scope_id != b.sin6_scope_id)
3785 * returns a sockaddr_in6 with embedded scope recovered and removed
3787 struct sockaddr_in6 *
3788 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
3790 /* check and strip embedded scope junk */
3791 if (addr->sin6_family == AF_INET6) {
3792 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
3793 if (addr->sin6_scope_id == 0) {
3795 if (!sa6_recoverscope(store)) {
3796 /* use the recovered scope */
3800 /* else, return the original "to" addr */
3801 in6_clearscope(&addr->sin6_addr);
3809 * are the two addresses the same? currently a "scopeless" check returns: 1
3813 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
3817 if (sa1 == NULL || sa2 == NULL)
3820 /* must be the same family */
3821 if (sa1->sa_family != sa2->sa_family)
3824 if (sa1->sa_family == AF_INET6) {
3825 /* IPv6 addresses */
3826 struct sockaddr_in6 *sin6_1, *sin6_2;
3828 sin6_1 = (struct sockaddr_in6 *)sa1;
3829 sin6_2 = (struct sockaddr_in6 *)sa2;
3830 return (SCTP6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr,
3831 &sin6_2->sin6_addr));
3832 } else if (sa1->sa_family == AF_INET) {
3833 /* IPv4 addresses */
3834 struct sockaddr_in *sin_1, *sin_2;
3836 sin_1 = (struct sockaddr_in *)sa1;
3837 sin_2 = (struct sockaddr_in *)sa2;
3838 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
3840 /* we don't do these... */
3846 sctp_print_address(struct sockaddr *sa)
3848 char ip6buf[INET6_ADDRSTRLEN];
3851 if (sa->sa_family == AF_INET6) {
3852 struct sockaddr_in6 *sin6;
3854 sin6 = (struct sockaddr_in6 *)sa;
3855 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
3856 ip6_sprintf(ip6buf, &sin6->sin6_addr),
3857 ntohs(sin6->sin6_port),
3858 sin6->sin6_scope_id);
3859 } else if (sa->sa_family == AF_INET) {
3860 struct sockaddr_in *sin;
3863 sin = (struct sockaddr_in *)sa;
3864 p = (unsigned char *)&sin->sin_addr;
3865 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
3866 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
3873 sctp_print_address_pkt(struct ip *iph, struct sctphdr *sh)
3875 if (iph->ip_v == IPVERSION) {
3876 struct sockaddr_in lsa, fsa;
3878 bzero(&lsa, sizeof(lsa));
3879 lsa.sin_len = sizeof(lsa);
3880 lsa.sin_family = AF_INET;
3881 lsa.sin_addr = iph->ip_src;
3882 lsa.sin_port = sh->src_port;
3883 bzero(&fsa, sizeof(fsa));
3884 fsa.sin_len = sizeof(fsa);
3885 fsa.sin_family = AF_INET;
3886 fsa.sin_addr = iph->ip_dst;
3887 fsa.sin_port = sh->dest_port;
3888 SCTP_PRINTF("src: ");
3889 sctp_print_address((struct sockaddr *)&lsa);
3890 SCTP_PRINTF("dest: ");
3891 sctp_print_address((struct sockaddr *)&fsa);
3892 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3893 struct ip6_hdr *ip6;
3894 struct sockaddr_in6 lsa6, fsa6;
3896 ip6 = (struct ip6_hdr *)iph;
3897 bzero(&lsa6, sizeof(lsa6));
3898 lsa6.sin6_len = sizeof(lsa6);
3899 lsa6.sin6_family = AF_INET6;
3900 lsa6.sin6_addr = ip6->ip6_src;
3901 lsa6.sin6_port = sh->src_port;
3902 bzero(&fsa6, sizeof(fsa6));
3903 fsa6.sin6_len = sizeof(fsa6);
3904 fsa6.sin6_family = AF_INET6;
3905 fsa6.sin6_addr = ip6->ip6_dst;
3906 fsa6.sin6_port = sh->dest_port;
3907 SCTP_PRINTF("src: ");
3908 sctp_print_address((struct sockaddr *)&lsa6);
3909 SCTP_PRINTF("dest: ");
3910 sctp_print_address((struct sockaddr *)&fsa6);
3915 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
3916 struct sctp_inpcb *new_inp,
3917 struct sctp_tcb *stcb,
3921 * go through our old INP and pull off any control structures that
3922 * belong to stcb and move then to the new inp.
3924 struct socket *old_so, *new_so;
3925 struct sctp_queued_to_read *control, *nctl;
3926 struct sctp_readhead tmp_queue;
3930 old_so = old_inp->sctp_socket;
3931 new_so = new_inp->sctp_socket;
3932 TAILQ_INIT(&tmp_queue);
3933 error = sblock(&old_so->so_rcv, waitflags);
3936 * Gak, can't get sblock, we have a problem. data will be
3937 * left stranded.. and we don't dare look at it since the
3938 * other thread may be reading something. Oh well, its a
3939 * screwed up app that does a peeloff OR a accept while
3940 * reading from the main socket... actually its only the
3941 * peeloff() case, since I think read will fail on a
3942 * listening socket..
3946 /* lock the socket buffers */
3947 SCTP_INP_READ_LOCK(old_inp);
3948 control = TAILQ_FIRST(&old_inp->read_queue);
3949 /* Pull off all for out target stcb */
3951 nctl = TAILQ_NEXT(control, next);
3952 if (control->stcb == stcb) {
3953 /* remove it we want it */
3954 TAILQ_REMOVE(&old_inp->read_queue, control, next);
3955 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
3958 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
3959 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
3961 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
3962 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
3963 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3965 m = SCTP_BUF_NEXT(m);
3970 SCTP_INP_READ_UNLOCK(old_inp);
3971 /* Remove the sb-lock on the old socket */
3973 sbunlock(&old_so->so_rcv);
3974 /* Now we move them over to the new socket buffer */
3975 control = TAILQ_FIRST(&tmp_queue);
3976 SCTP_INP_READ_LOCK(new_inp);
3978 nctl = TAILQ_NEXT(control, next);
3979 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
3982 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
3983 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
3985 sctp_sballoc(stcb, &new_so->so_rcv, m);
3986 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
3987 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3989 m = SCTP_BUF_NEXT(m);
3993 SCTP_INP_READ_UNLOCK(new_inp);
3998 sctp_add_to_readq(struct sctp_inpcb *inp,
3999 struct sctp_tcb *stcb,
4000 struct sctp_queued_to_read *control,
4005 * Here we must place the control on the end of the socket read
4006 * queue AND increment sb_cc so that select will work properly on
4009 struct mbuf *m, *prev = NULL;
4014 panic("Gak, inp NULL on add_to_readq");
4018 SCTP_INP_READ_LOCK(inp);
4019 if (!(control->spec_flags & M_NOTIFICATION)) {
4020 atomic_add_int(&inp->total_recvs, 1);
4021 if (!control->do_not_ref_stcb) {
4022 atomic_add_int(&stcb->total_recvs, 1);
4026 control->held_length = 0;
4027 control->length = 0;
4029 if (SCTP_BUF_LEN(m) == 0) {
4030 /* Skip mbufs with NO length */
4033 control->data = sctp_m_free(m);
4036 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4037 m = SCTP_BUF_NEXT(prev);
4040 control->tail_mbuf = prev;;
4045 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
4046 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4048 sctp_sballoc(stcb, sb, m);
4049 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
4050 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4052 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4053 m = SCTP_BUF_NEXT(m);
4056 control->tail_mbuf = prev;
4058 /* Everything got collapsed out?? */
4062 control->end_added = 1;
4064 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4065 SCTP_INP_READ_UNLOCK(inp);
4066 if (inp && inp->sctp_socket) {
4067 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4068 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4070 sctp_sorwakeup(inp, inp->sctp_socket);
4076 sctp_append_to_readq(struct sctp_inpcb *inp,
4077 struct sctp_tcb *stcb,
4078 struct sctp_queued_to_read *control,
4085 * A partial delivery API event is underway. OR we are appending on
4086 * the reassembly queue.
4088 * If PDAPI this means we need to add m to the end of the data.
4089 * Increase the length in the control AND increment the sb_cc.
4090 * Otherwise sb is NULL and all we need to do is put it at the end
4091 * of the mbuf chain.
4094 struct mbuf *mm, *tail = NULL, *prev = NULL;
4097 SCTP_INP_READ_LOCK(inp);
4099 if (control == NULL) {
4102 SCTP_INP_READ_UNLOCK(inp);
4106 if (control->end_added) {
4107 /* huh this one is complete? */
4115 if (SCTP_BUF_LEN(mm) == 0) {
4116 /* Skip mbufs with NO lenght */
4119 m = sctp_m_free(mm);
4122 SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4123 mm = SCTP_BUF_NEXT(prev);
4128 len += SCTP_BUF_LEN(mm);
4130 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
4131 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
4133 sctp_sballoc(stcb, sb, mm);
4134 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
4135 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4138 mm = SCTP_BUF_NEXT(mm);
4143 /* Really there should always be a prev */
4145 /* Huh nothing left? */
4147 panic("Nothing left to add?");
4155 /* message is complete */
4156 if (stcb && (control == stcb->asoc.control_pdapi)) {
4157 stcb->asoc.control_pdapi = NULL;
4159 control->held_length = 0;
4160 control->end_added = 1;
4162 atomic_add_int(&control->length, len);
4163 if (control->tail_mbuf) {
4165 SCTP_BUF_NEXT(control->tail_mbuf) = m;
4166 control->tail_mbuf = tail;
4170 if (control->data != NULL) {
4171 panic("This should NOT happen");
4175 control->tail_mbuf = tail;
4178 control->do_not_ref_stcb = 1;
4181 * When we are appending in partial delivery, the cum-ack is used
4182 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4183 * is populated in the outbound sinfo structure from the true cumack
4184 * if the association exists...
4186 control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4188 SCTP_INP_READ_UNLOCK(inp);
4190 if (inp && inp->sctp_socket) {
4191 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4192 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4194 sctp_sorwakeup(inp, inp->sctp_socket);
4201 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4202 *************ALTERNATE ROUTING CODE
4205 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4206 *************ALTERNATE ROUTING CODE
4210 sctp_generate_invmanparam(int err)
4212 /* Return a MBUF with a invalid mandatory parameter */
4215 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
4217 struct sctp_paramhdr *ph;
4219 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
4220 ph = mtod(m, struct sctp_paramhdr *);
4221 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4222 ph->param_type = htons(err);
4227 #ifdef SCTP_MBCNT_LOGGING
4229 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4230 struct sctp_tmit_chunk *tp1, int chk_cnt)
4232 if (tp1->data == NULL) {
4235 asoc->chunks_on_out_queue -= chk_cnt;
4236 if (sctp_logging_level & SCTP_MBCNT_LOGGING_ENABLE) {
4237 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4238 asoc->total_output_queue_size,
4243 if (asoc->total_output_queue_size >= tp1->book_size) {
4244 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4246 asoc->total_output_queue_size = 0;
4249 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4250 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4251 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4252 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4254 stcb->sctp_socket->so_snd.sb_cc = 0;
4263 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4264 int reason, struct sctpchunk_listhead *queue)
4268 uint8_t foundeom = 0;
4271 ret_sz += tp1->book_size;
4272 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4274 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4275 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1);
4276 sctp_m_freem(tp1->data);
4278 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4280 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4281 stcb->asoc.sent_queue_cnt_removeable--;
4283 if (queue == &stcb->asoc.send_queue) {
4284 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4285 /* on to the sent queue */
4286 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4288 stcb->asoc.sent_queue_cnt++;
4290 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4291 SCTP_DATA_NOT_FRAG) {
4292 /* not frag'ed we ae done */
4295 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4296 /* end of frag, we are done */
4301 * Its a begin or middle piece, we must mark all of
4305 tp1 = TAILQ_NEXT(tp1, sctp_next);
4307 } while (tp1 && notdone);
4308 if ((foundeom == 0) && (queue == &stcb->asoc.sent_queue)) {
4310 * The multi-part message was scattered across the send and
4313 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
4315 * recurse throught the send_queue too, starting at the
4319 ret_sz += sctp_release_pr_sctp_chunk(stcb, tp1, reason,
4320 &stcb->asoc.send_queue);
4322 SCTP_PRINTF("hmm, nothing on the send queue and no EOM?\n");
4329 * checks to see if the given address, sa, is one that is currently known by
4330 * the kernel note: can't distinguish the same address on multiple interfaces
4331 * and doesn't handle multiple addresses with different zone/scope id's note:
4332 * ifa_ifwithaddr() compares the entire sockaddr struct
4335 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4338 struct sctp_laddr *laddr;
4340 if (holds_lock == 0) {
4341 SCTP_INP_RLOCK(inp);
4343 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4344 if (laddr->ifa == NULL)
4346 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4348 if (addr->sa_family == AF_INET) {
4349 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4350 laddr->ifa->address.sin.sin_addr.s_addr) {
4352 if (holds_lock == 0) {
4353 SCTP_INP_RUNLOCK(inp);
4355 return (laddr->ifa);
4358 } else if (addr->sa_family == AF_INET6) {
4359 if (SCTP6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)addr)->sin6_addr,
4360 &laddr->ifa->address.sin6.sin6_addr)) {
4362 if (holds_lock == 0) {
4363 SCTP_INP_RUNLOCK(inp);
4365 return (laddr->ifa);
4370 if (holds_lock == 0) {
4371 SCTP_INP_RUNLOCK(inp);
4377 sctp_get_ifa_hash_val(struct sockaddr *addr)
4380 if (addr->sa_family == AF_INET) {
4381 struct sockaddr_in *sin;
4383 sin = (struct sockaddr_in *)addr;
4384 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4385 } else if (addr->sa_family == AF_INET6) {
4386 struct sockaddr_in6 *sin6;
4387 uint32_t hash_of_addr;
4389 sin6 = (struct sockaddr_in6 *)addr;
4390 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
4391 sin6->sin6_addr.s6_addr32[1] +
4392 sin6->sin6_addr.s6_addr32[2] +
4393 sin6->sin6_addr.s6_addr32[3]);
4394 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
4395 return (hash_of_addr);
4401 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
4403 struct sctp_ifa *sctp_ifap;
4404 struct sctp_vrf *vrf;
4405 struct sctp_ifalist *hash_head;
4406 uint32_t hash_of_addr;
4408 if (holds_lock == 0)
4409 SCTP_IPI_ADDR_LOCK();
4411 vrf = sctp_find_vrf(vrf_id);
4413 if (holds_lock == 0)
4414 SCTP_IPI_ADDR_UNLOCK();
4417 hash_of_addr = sctp_get_ifa_hash_val(addr);
4419 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
4420 if (hash_head == NULL) {
4421 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
4422 (u_int)hash_of_addr, (u_int)vrf->vrf_addr_hashmark,
4423 (u_int)(hash_of_addr & vrf->vrf_addr_hashmark));
4424 sctp_print_address(addr);
4425 SCTP_PRINTF("No such bucket for address\n");
4426 if (holds_lock == 0)
4427 SCTP_IPI_ADDR_UNLOCK();
4431 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
4432 if (sctp_ifap == NULL) {
4433 panic("Huh LIST_FOREACH corrupt");
4435 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
4437 if (addr->sa_family == AF_INET) {
4438 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4439 sctp_ifap->address.sin.sin_addr.s_addr) {
4441 if (holds_lock == 0)
4442 SCTP_IPI_ADDR_UNLOCK();
4446 } else if (addr->sa_family == AF_INET6) {
4447 if (SCTP6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)addr)->sin6_addr,
4448 &sctp_ifap->address.sin6.sin6_addr)) {
4450 if (holds_lock == 0)
4451 SCTP_IPI_ADDR_UNLOCK();
4457 if (holds_lock == 0)
4458 SCTP_IPI_ADDR_UNLOCK();
4463 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
4466 /* User pulled some data, do we need a rwnd update? */
4469 struct socket *so = NULL;
4474 atomic_add_int(&stcb->asoc.refcnt, 1);
4476 if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
4477 SCTP_STATE_SHUTDOWN_RECEIVED |
4478 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4479 /* Pre-check If we are freeing no update */
4482 SCTP_INP_INCR_REF(stcb->sctp_ep);
4483 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4484 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4487 so = stcb->sctp_socket;
4491 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
4492 /* Have you have freed enough to look */
4494 /* Yep, its worth a look and the lock overhead */
4496 /* Figure out what the rwnd would be */
4497 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
4498 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
4499 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
4503 if (dif >= rwnd_req) {
4505 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
4508 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4510 * One last check before we allow the guy possibly
4511 * to get in. There is a race, where the guy has not
4512 * reached the gate. In that case
4516 SCTP_TCB_LOCK(stcb);
4517 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4518 /* No reports here */
4519 SCTP_TCB_UNLOCK(stcb);
4522 SCTP_STAT_INCR(sctps_wu_sacks_sent);
4523 sctp_send_sack(stcb);
4524 sctp_chunk_output(stcb->sctp_ep, stcb,
4525 SCTP_OUTPUT_FROM_USR_RCVD);
4526 /* make sure no timer is running */
4527 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
4528 SCTP_TCB_UNLOCK(stcb);
4530 /* Update how much we have pending */
4531 stcb->freed_by_sorcv_sincelast = dif;
4534 if (so && r_unlocked && hold_rlock) {
4535 SCTP_INP_READ_LOCK(stcb->sctp_ep);
4537 SCTP_INP_DECR_REF(stcb->sctp_ep);
4539 atomic_add_int(&stcb->asoc.refcnt, -1);
4544 sctp_sorecvmsg(struct socket *so,
4547 struct sockaddr *from,
4550 struct sctp_sndrcvinfo *sinfo,
4554 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
4555 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
4556 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
4557 * On the way out we may send out any combination of:
4558 * MSG_NOTIFICATION MSG_EOR
4561 struct sctp_inpcb *inp = NULL;
4563 int cp_len = 0, error = 0;
4564 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
4565 struct mbuf *m = NULL, *embuf = NULL;
4566 struct sctp_tcb *stcb = NULL;
4567 int wakeup_read_socket = 0;
4568 int freecnt_applied = 0;
4569 int out_flags = 0, in_flags = 0;
4570 int block_allowed = 1;
4571 uint32_t freed_so_far = 0;
4572 int copied_so_far = 0;
4573 int in_eeor_mode = 0;
4574 int no_rcv_needed = 0;
4575 uint32_t rwnd_req = 0;
4576 int hold_sblock = 0;
4580 uint32_t held_length = 0;
4581 int sockbuf_lock = 0;
4587 in_flags = *msg_flags;
4588 if (in_flags & MSG_PEEK)
4589 SCTP_STAT_INCR(sctps_read_peeks);
4593 slen = uio->uio_resid;
4595 /* Pull in and set up our int flags */
4596 if (in_flags & MSG_OOB) {
4597 /* Out of band's NOT supported */
4598 return (EOPNOTSUPP);
4600 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
4603 if ((in_flags & (MSG_DONTWAIT
4606 SCTP_SO_IS_NBIO(so)) {
4609 /* setup the endpoint */
4610 inp = (struct sctp_inpcb *)so->so_pcb;
4614 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
4615 /* Must be at least a MTU's worth */
4616 if (rwnd_req < SCTP_MIN_RWND)
4617 rwnd_req = SCTP_MIN_RWND;
4618 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
4619 if (sctp_logging_level & SCTP_RECV_RWND_LOGGING_ENABLE) {
4620 sctp_misc_ints(SCTP_SORECV_ENTER,
4621 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
4623 if (sctp_logging_level & SCTP_RECV_RWND_LOGGING_ENABLE) {
4624 sctp_misc_ints(SCTP_SORECV_ENTERPL,
4625 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
4627 error = sblock(&so->so_rcv, (block_allowed ? M_WAITOK : 0));
4630 goto release_unlocked;
4636 if (hold_sblock == 0) {
4637 SOCKBUF_LOCK(&so->so_rcv);
4640 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4641 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4644 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
4646 error = so->so_error;
4647 if ((in_flags & MSG_PEEK) == 0)
4654 if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
4655 /* we need to wait for data */
4656 if ((so->so_rcv.sb_cc == 0) &&
4657 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4658 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
4659 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
4661 * For active open side clear flags for
4662 * re-use passive open is blocked by
4665 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
4667 * You were aborted, passive side
4672 * You get this once if you are
4675 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4677 * Remove flag if on the
4680 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
4683 so->so_state &= ~(SS_ISCONNECTING |
4684 SS_ISDISCONNECTING |
4688 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
4691 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
4697 error = sbwait(&so->so_rcv);
4702 goto restart_nosblocks;
4703 } else if (so->so_rcv.sb_cc == 0) {
4705 error = so->so_error;
4706 if ((in_flags & MSG_PEEK) == 0)
4709 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4710 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4711 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
4713 * For active open side clear flags
4714 * for re-use passive open is
4715 * blocked by connect.
4717 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
4719 * You were aborted, passive
4720 * side always hits here
4724 * You get this once if you
4725 * are active open side
4727 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4733 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
4736 so->so_state &= ~(SS_ISCONNECTING |
4737 SS_ISDISCONNECTING |
4741 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
4744 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
4750 error = EWOULDBLOCK;
4754 if (hold_sblock == 1) {
4755 SOCKBUF_UNLOCK(&so->so_rcv);
4758 /* we possibly have data we can read */
4759 /* sa_ignore FREED_MEMORY */
4760 control = TAILQ_FIRST(&inp->read_queue);
4761 if (control == NULL) {
4763 * This could be happening since the appender did the
4764 * increment but as not yet did the tailq insert onto the
4767 if (hold_rlock == 0) {
4768 SCTP_INP_READ_LOCK(inp);
4771 control = TAILQ_FIRST(&inp->read_queue);
4772 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
4774 panic("Huh, its non zero and nothing on control?");
4776 so->so_rcv.sb_cc = 0;
4778 SCTP_INP_READ_UNLOCK(inp);
4782 if ((control->length == 0) &&
4783 (control->do_not_ref_stcb)) {
4785 * Clean up code for freeing assoc that left behind a
4786 * pdapi.. maybe a peer in EEOR that just closed after
4787 * sending and never indicated a EOR.
4789 if (hold_rlock == 0) {
4791 SCTP_INP_READ_LOCK(inp);
4793 control->held_length = 0;
4794 if (control->data) {
4795 /* Hmm there is data here .. fix */
4799 m_tmp = control->data;
4801 cnt += SCTP_BUF_LEN(m_tmp);
4802 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
4803 control->tail_mbuf = m_tmp;
4804 control->end_added = 1;
4806 m_tmp = SCTP_BUF_NEXT(m_tmp);
4808 control->length = cnt;
4811 TAILQ_REMOVE(&inp->read_queue, control, next);
4812 /* Add back any hiddend data */
4813 sctp_free_remote_addr(control->whoFrom);
4814 sctp_free_a_readq(stcb, control);
4818 SCTP_INP_READ_UNLOCK(inp);
4822 if (control->length == 0) {
4823 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
4825 /* find a more suitable one then this */
4826 ctl = TAILQ_NEXT(control, next);
4828 if ((ctl->stcb != control->stcb) && (ctl->length) &&
4830 (ctl->spec_flags & M_NOTIFICATION) ||
4831 ((ctl->do_not_ref_stcb == 0) &&
4832 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
4835 * If we have a different TCB next, and there is data
4836 * present. If we have already taken some (pdapi), OR we can
4837 * ref the tcb and no delivery as started on this stream, we
4838 * take it. Note we allow a notification on a different
4839 * assoc to be delivered..
4843 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
4845 ((ctl->some_taken) ||
4846 ((ctl->do_not_ref_stcb == 0) &&
4847 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
4848 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
4851 * If we have the same tcb, and there is data present, and we
4852 * have the strm interleave feature present. Then if we have
4853 * taken some (pdapi) or we can refer to tht tcb AND we have
4854 * not started a delivery for this stream, we can take it.
4855 * Note we do NOT allow a notificaiton on the same assoc to
4861 ctl = TAILQ_NEXT(ctl, next);
4865 * if we reach here, not suitable replacement is available
4866 * <or> fragment interleave is NOT on. So stuff the sb_cc
4867 * into the our held count, and its time to sleep again.
4869 held_length = so->so_rcv.sb_cc;
4870 control->held_length = so->so_rcv.sb_cc;
4873 /* Clear the held length since there is something to read */
4874 control->held_length = 0;
4876 SCTP_INP_READ_UNLOCK(inp);
4881 * If we reach here, control has a some data for us to read off.
4882 * Note that stcb COULD be NULL.
4884 control->some_taken = 1;
4886 SOCKBUF_UNLOCK(&so->so_rcv);
4889 stcb = control->stcb;
4891 if ((control->do_not_ref_stcb == 0) &&
4892 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
4893 if (freecnt_applied == 0)
4895 } else if (control->do_not_ref_stcb == 0) {
4896 /* you can't free it on me please */
4898 * The lock on the socket buffer protects us so the
4899 * free code will stop. But since we used the
4900 * socketbuf lock and the sender uses the tcb_lock
4901 * to increment, we need to use the atomic add to
4904 if (freecnt_applied)
4905 panic("refcnt already incremented");
4906 atomic_add_int(&stcb->asoc.refcnt, 1);
4907 freecnt_applied = 1;
4909 * Setup to remember how much we have not yet told
4910 * the peer our rwnd has opened up. Note we grab the
4911 * value from the tcb from last time. Note too that
4912 * sack sending clears this when a sack is sent,
4913 * which is fine. Once we hit the rwnd_req, we then
4914 * will go to the sctp_user_rcvd() that will not
4915 * lock until it KNOWs it MUST send a WUP-SACK.
4917 freed_so_far = stcb->freed_by_sorcv_sincelast;
4918 stcb->freed_by_sorcv_sincelast = 0;
4922 ((control->spec_flags & M_NOTIFICATION) == 0) &&
4923 control->do_not_ref_stcb == 0) {
4924 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
4926 /* First lets get off the sinfo and sockaddr info */
4927 if ((sinfo) && filling_sinfo) {
4928 memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
4929 nxt = TAILQ_NEXT(control, next);
4930 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
4931 struct sctp_extrcvinfo *s_extra;
4933 s_extra = (struct sctp_extrcvinfo *)sinfo;
4936 s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
4937 if (nxt->sinfo_flags & SCTP_UNORDERED) {
4938 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
4940 if (nxt->spec_flags & M_NOTIFICATION) {
4941 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
4943 s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
4944 s_extra->sreinfo_next_length = nxt->length;
4945 s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
4946 s_extra->sreinfo_next_stream = nxt->sinfo_stream;
4947 if (nxt->tail_mbuf != NULL) {
4948 if (nxt->end_added) {
4949 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
4954 * we explicitly 0 this, since the memcpy
4955 * got some other things beyond the older
4956 * sinfo_ that is on the control's structure
4960 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
4961 s_extra->sreinfo_next_aid = 0;
4962 s_extra->sreinfo_next_length = 0;
4963 s_extra->sreinfo_next_ppid = 0;
4964 s_extra->sreinfo_next_stream = 0;
4968 * update off the real current cum-ack, if we have an stcb.
4970 if ((control->do_not_ref_stcb == 0) && stcb)
4971 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4973 * mask off the high bits, we keep the actual chunk bits in
4976 sinfo->sinfo_flags &= 0x00ff;
4977 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
4978 sinfo->sinfo_flags |= SCTP_UNORDERED;
4981 if (fromlen && from) {
4982 struct sockaddr *to;
4985 cp_len = min(fromlen, control->whoFrom->ro._l_addr.sin.sin_len);
4986 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
4987 ((struct sockaddr_in *)from)->sin_port = control->port_from;
4989 /* No AF_INET use AF_INET6 */
4990 cp_len = min(fromlen, control->whoFrom->ro._l_addr.sin6.sin6_len);
4991 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
4992 ((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
4996 #if defined(INET) && defined(INET6)
4997 if ((inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
4998 (to->sa_family == AF_INET) &&
4999 ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
5000 struct sockaddr_in *sin;
5001 struct sockaddr_in6 sin6;
5003 sin = (struct sockaddr_in *)to;
5004 bzero(&sin6, sizeof(sin6));
5005 sin6.sin6_family = AF_INET6;
5006 sin6.sin6_len = sizeof(struct sockaddr_in6);
5007 sin6.sin6_addr.s6_addr16[2] = 0xffff;
5008 bcopy(&sin->sin_addr,
5009 &sin6.sin6_addr.s6_addr16[3],
5010 sizeof(sin6.sin6_addr.s6_addr16[3]));
5011 sin6.sin6_port = sin->sin_port;
5012 memcpy(from, (caddr_t)&sin6, sizeof(sin6));
5017 struct sockaddr_in6 lsa6, *to6;
5019 to6 = (struct sockaddr_in6 *)to;
5020 sctp_recover_scope_mac(to6, (&lsa6));
5024 /* now copy out what data we can */
5026 /* copy out each mbuf in the chain up to length */
5030 /* Move out all we can */
5031 cp_len = (int)uio->uio_resid;
5032 my_len = (int)SCTP_BUF_LEN(m);
5033 if (cp_len > my_len) {
5034 /* not enough in this buf */
5038 SCTP_INP_READ_UNLOCK(inp);
5042 error = uiomove(mtod(m, char *), cp_len, uio);
5044 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5047 if ((control->do_not_ref_stcb == 0) && stcb &&
5048 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5052 /* error we are out of here */
5055 if ((SCTP_BUF_NEXT(m) == NULL) &&
5056 (cp_len >= SCTP_BUF_LEN(m)) &&
5057 ((control->end_added == 0) ||
5058 (control->end_added &&
5059 (TAILQ_NEXT(control, next) == NULL)))
5061 SCTP_INP_READ_LOCK(inp);
5064 if (cp_len == SCTP_BUF_LEN(m)) {
5065 if ((SCTP_BUF_NEXT(m) == NULL) &&
5066 (control->end_added)) {
5067 out_flags |= MSG_EOR;
5068 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5069 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5071 if (control->spec_flags & M_NOTIFICATION) {
5072 out_flags |= MSG_NOTIFICATION;
5074 /* we ate up the mbuf */
5075 if (in_flags & MSG_PEEK) {
5077 m = SCTP_BUF_NEXT(m);
5078 copied_so_far += cp_len;
5080 /* dispose of the mbuf */
5081 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
5082 sctp_sblog(&so->so_rcv,
5083 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5085 sctp_sbfree(control, stcb, &so->so_rcv, m);
5086 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
5087 sctp_sblog(&so->so_rcv,
5088 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5091 copied_so_far += cp_len;
5092 freed_so_far += cp_len;
5093 alen = atomic_fetchadd_int(&control->length, -(cp_len));
5094 if (alen < cp_len) {
5095 panic("Control length goes negative?");
5097 control->data = sctp_m_free(m);
5100 * been through it all, must hold sb
5101 * lock ok to null tail
5103 if (control->data == NULL) {
5105 if ((control->end_added == 0) ||
5106 (TAILQ_NEXT(control, next) == NULL)) {
5114 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5115 panic("Hmm we don't own the lock?");
5119 control->tail_mbuf = NULL;
5121 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5122 panic("end_added, nothing left and no MSG_EOR");
5128 /* Do we need to trim the mbuf? */
5129 if (control->spec_flags & M_NOTIFICATION) {
5130 out_flags |= MSG_NOTIFICATION;
5132 if ((in_flags & MSG_PEEK) == 0) {
5133 SCTP_BUF_RESV_UF(m, cp_len);
5134 SCTP_BUF_LEN(m) -= cp_len;
5135 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
5136 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5138 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5139 if ((control->do_not_ref_stcb == 0) &&
5141 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5143 copied_so_far += cp_len;
5145 freed_so_far += cp_len;
5146 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
5147 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5148 SCTP_LOG_SBRESULT, 0);
5150 alen = atomic_fetchadd_int(&control->length, -(cp_len));
5151 if (alen < cp_len) {
5152 panic("Control length goes negative2?");
5155 copied_so_far += cp_len;
5158 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5161 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5162 (control->do_not_ref_stcb == 0) &&
5163 (freed_so_far >= rwnd_req)) {
5164 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5166 } /* end while(m) */
5168 * At this point we have looked at it all and we either have
5169 * a MSG_EOR/or read all the user wants... <OR>
5170 * control->length == 0.
5172 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5173 /* we are done with this control */
5174 if (control->length == 0) {
5175 if (control->data) {
5177 panic("control->data not null at read eor?");
5179 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5180 sctp_m_freem(control->data);
5181 control->data = NULL;
5185 if (TAILQ_NEXT(control, next) == NULL) {
5187 * If we don't have a next we need a
5188 * lock, if there is a next interupt
5189 * is filling ahead of us and we
5190 * don't need a lock to remove this
5191 * guy (which is the head of the
5194 if (hold_rlock == 0) {
5195 SCTP_INP_READ_LOCK(inp);
5199 TAILQ_REMOVE(&inp->read_queue, control, next);
5200 /* Add back any hiddend data */
5201 if (control->held_length) {
5203 control->held_length = 0;
5204 wakeup_read_socket = 1;
5206 if (control->aux_data) {
5207 sctp_m_free(control->aux_data);
5208 control->aux_data = NULL;
5210 no_rcv_needed = control->do_not_ref_stcb;
5211 sctp_free_remote_addr(control->whoFrom);
5212 control->data = NULL;
5213 sctp_free_a_readq(stcb, control);
5215 if ((freed_so_far >= rwnd_req) &&
5216 (no_rcv_needed == 0))
5217 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5221 * The user did not read all of this
5222 * message, turn off the returned MSG_EOR
5223 * since we are leaving more behind on the
5227 if (control->end_added &&
5228 (control->data == NULL) &&
5229 (control->tail_mbuf == NULL)) {
5230 panic("Gak, control->length is corrupt?");
5233 no_rcv_needed = control->do_not_ref_stcb;
5234 out_flags &= ~MSG_EOR;
5237 if (out_flags & MSG_EOR) {
5240 if ((uio->uio_resid == 0) ||
5241 ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))
5246 * If I hit here the receiver wants more and this message is
5247 * NOT done (pd-api). So two questions. Can we block? if not
5248 * we are done. Did the user NOT set MSG_WAITALL?
5250 if (block_allowed == 0) {
5254 * We need to wait for more data a few things: - We don't
5255 * sbunlock() so we don't get someone else reading. - We
5256 * must be sure to account for the case where what is added
5257 * is NOT to our control when we wakeup.
5261 * Do we need to tell the transport a rwnd update might be
5262 * needed before we go to sleep?
5264 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5265 ((freed_so_far >= rwnd_req) &&
5266 (control->do_not_ref_stcb == 0) &&
5267 (no_rcv_needed == 0))) {
5268 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5271 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5274 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5277 if (hold_rlock == 1) {
5278 SCTP_INP_READ_UNLOCK(inp);
5281 if (hold_sblock == 0) {
5282 SOCKBUF_LOCK(&so->so_rcv);
5285 if (so->so_rcv.sb_cc <= control->held_length) {
5286 error = sbwait(&so->so_rcv);
5290 control->held_length = 0;
5293 SOCKBUF_UNLOCK(&so->so_rcv);
5296 if (control->length == 0) {
5297 /* still nothing here */
5298 if (control->end_added == 1) {
5299 /* he aborted, or is done i.e.did a shutdown */
5300 out_flags |= MSG_EOR;
5301 if (control->pdapi_aborted) {
5302 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5303 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5305 out_flags |= MSG_TRUNC;
5307 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5308 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5310 goto done_with_control;
5312 if (so->so_rcv.sb_cc > held_length) {
5313 control->held_length = so->so_rcv.sb_cc;
5316 goto wait_some_more;
5317 } else if (control->data == NULL) {
5319 * we must re-sync since data is probably being
5322 SCTP_INP_READ_LOCK(inp);
5323 if ((control->length > 0) && (control->data == NULL)) {
5325 * big trouble.. we have the lock and its
5328 panic("Impossible data==NULL length !=0");
5330 SCTP_INP_READ_UNLOCK(inp);
5331 /* We will fall around to get more data */
5336 * Give caller back the mbuf chain,
5337 * store in uio_resid the length
5339 wakeup_read_socket = 0;
5340 if ((control->end_added == 0) ||
5341 (TAILQ_NEXT(control, next) == NULL)) {
5342 /* Need to get rlock */
5343 if (hold_rlock == 0) {
5344 SCTP_INP_READ_LOCK(inp);
5348 if (control->end_added) {
5349 out_flags |= MSG_EOR;
5350 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5351 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5353 if (control->spec_flags & M_NOTIFICATION) {
5354 out_flags |= MSG_NOTIFICATION;
5356 uio->uio_resid = control->length;
5357 *mp = control->data;
5360 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
5361 sctp_sblog(&so->so_rcv,
5362 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5364 sctp_sbfree(control, stcb, &so->so_rcv, m);
5365 freed_so_far += SCTP_BUF_LEN(m);
5366 if (sctp_logging_level & SCTP_SB_LOGGING_ENABLE) {
5367 sctp_sblog(&so->so_rcv,
5368 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5370 m = SCTP_BUF_NEXT(m);
5372 control->data = control->tail_mbuf = NULL;
5373 control->length = 0;
5374 if (out_flags & MSG_EOR) {
5375 /* Done with this control */
5376 goto done_with_control;
5380 if (hold_rlock == 1) {
5381 SCTP_INP_READ_UNLOCK(inp);
5384 if (hold_sblock == 1) {
5385 SOCKBUF_UNLOCK(&so->so_rcv);
5388 sbunlock(&so->so_rcv);
5393 SOCKBUF_UNLOCK(&so->so_rcv);
5396 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
5397 if ((freed_so_far >= rwnd_req) &&
5398 (control && (control->do_not_ref_stcb == 0)) &&
5399 (no_rcv_needed == 0))
5400 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5403 *msg_flags |= out_flags;
5405 if (((out_flags & MSG_EOR) == 0) &&
5406 ((in_flags & MSG_PEEK) == 0) &&
5408 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO))) {
5409 struct sctp_extrcvinfo *s_extra;
5411 s_extra = (struct sctp_extrcvinfo *)sinfo;
5412 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
5414 if (hold_rlock == 1) {
5415 SCTP_INP_READ_UNLOCK(inp);
5419 SOCKBUF_UNLOCK(&so->so_rcv);
5423 sbunlock(&so->so_rcv);
5425 if (freecnt_applied) {
5427 * The lock on the socket buffer protects us so the free
5428 * code will stop. But since we used the socketbuf lock and
5429 * the sender uses the tcb_lock to increment, we need to use
5430 * the atomic add to the refcnt.
5433 panic("stcb for refcnt has gone NULL?");
5435 atomic_add_int(&stcb->asoc.refcnt, -1);
5436 freecnt_applied = 0;
5437 /* Save the value back for next time */
5438 stcb->freed_by_sorcv_sincelast = freed_so_far;
5440 if (sctp_logging_level & SCTP_RECV_RWND_LOGGING_ENABLE) {
5442 sctp_misc_ints(SCTP_SORECV_DONE,
5444 ((uio) ? (slen - uio->uio_resid) : slen),
5448 sctp_misc_ints(SCTP_SORECV_DONE,
5450 ((uio) ? (slen - uio->uio_resid) : slen),
5455 if (wakeup_read_socket) {
5456 sctp_sorwakeup(inp, so);
5462 #ifdef SCTP_MBUF_LOGGING
5464 sctp_m_free(struct mbuf *m)
5466 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
5467 if (SCTP_BUF_IS_EXTENDED(m)) {
5468 sctp_log_mb(m, SCTP_MBUF_IFREE);
5475 sctp_m_freem(struct mbuf *mb)
5478 mb = sctp_m_free(mb);
5484 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
5487 * Given a local address. For all associations that holds the
5488 * address, request a peer-set-primary.
5490 struct sctp_ifa *ifa;
5491 struct sctp_laddr *wi;
5493 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
5495 return (EADDRNOTAVAIL);
5498 * Now that we have the ifa we must awaken the iterator with this
5501 wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
5505 /* Now incr the count and int wi structure */
5506 SCTP_INCR_LADDR_COUNT();
5507 bzero(wi, sizeof(*wi));
5508 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
5510 wi->action = SCTP_SET_PRIM_ADDR;
5511 atomic_add_int(&ifa->refcount, 1);
5513 /* Now add it to the work queue */
5514 SCTP_IPI_ITERATOR_WQ_LOCK();
5516 * Should this really be a tailq? As it is we will process the
5519 LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr);
5520 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
5521 (struct sctp_inpcb *)NULL,
5522 (struct sctp_tcb *)NULL,
5523 (struct sctp_nets *)NULL);
5524 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5532 sctp_soreceive(struct socket *so,
5533 struct sockaddr **psa,
5536 struct mbuf **controlp,
5540 uint8_t sockbuf[256];
5541 struct sockaddr *from;
5542 struct sctp_extrcvinfo sinfo;
5543 int filling_sinfo = 1;
5544 struct sctp_inpcb *inp;
5546 inp = (struct sctp_inpcb *)so->so_pcb;
5547 /* pickup the assoc we are reading from */
5551 if ((sctp_is_feature_off(inp,
5552 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
5553 (controlp == NULL)) {
5554 /* user does not want the sndrcv ctl */
5558 from = (struct sockaddr *)sockbuf;
5559 fromlen = sizeof(sockbuf);
5566 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
5567 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
5568 if ((controlp) && (filling_sinfo)) {
5569 /* copy back the sinfo in a CMSG format */
5571 *controlp = sctp_build_ctl_nchunk(inp,
5572 (struct sctp_sndrcvinfo *)&sinfo);
5577 /* copy back the address info */
5578 if (from && from->sa_len) {
5579 *psa = sodupsockaddr(from, M_NOWAIT);
5589 sctp_l_soreceive(struct socket *so,
5590 struct sockaddr **name,
5597 uint8_t sockbuf[256];
5598 struct sockaddr *from;
5599 struct sctp_extrcvinfo sinfo;
5600 int filling_sinfo = 1;
5601 struct sctp_inpcb *inp;
5603 inp = (struct sctp_inpcb *)so->so_pcb;
5604 /* pickup the assoc we are reading from */
5608 if ((sctp_is_feature_off(inp,
5609 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
5610 (controlp == NULL)) {
5611 /* user does not want the sndrcv ctl */
5615 from = (struct sockaddr *)sockbuf;
5616 fromlen = sizeof(sockbuf);
5623 error = sctp_sorecvmsg(so, uio,
5624 (struct mbuf **)NULL,
5625 from, fromlen, flag,
5626 (struct sctp_sndrcvinfo *)&sinfo,
5628 if ((controlp) && (filling_sinfo)) {
5630 * copy back the sinfo in a CMSG format note that the caller
5631 * has reponsibility for freeing the memory.
5634 *controlp = sctp_build_ctl_cchunk(inp,
5636 (struct sctp_sndrcvinfo *)&sinfo);
5639 /* copy back the address info */
5640 if (from && from->sa_len) {
5641 *name = sodupsockaddr(from, M_WAIT);
5656 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
5657 int totaddr, int *error)
5661 struct sctp_inpcb *inp;
5662 struct sockaddr *sa;
5666 inp = stcb->sctp_ep;
5668 for (i = 0; i < totaddr; i++) {
5669 if (sa->sa_family == AF_INET) {
5670 incr = sizeof(struct sockaddr_in);
5671 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
5672 /* assoc gone no un-lock */
5673 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
5678 } else if (sa->sa_family == AF_INET6) {
5679 incr = sizeof(struct sockaddr_in6);
5680 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
5681 /* assoc gone no un-lock */
5682 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
5688 sa = (struct sockaddr *)((caddr_t)sa + incr);
5695 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
5696 int *totaddr, int *num_v4, int *num_v6, int *error,
5697 int limit, int *bad_addr)
5699 struct sockaddr *sa;
5700 struct sctp_tcb *stcb = NULL;
5705 *error = *num_v6 = *num_v4 = 0;
5706 /* account and validate addresses */
5707 for (i = 0; i < (size_t)*totaddr; i++) {
5708 if (sa->sa_family == AF_INET) {
5710 incr = sizeof(struct sockaddr_in);
5711 if (sa->sa_len != incr) {
5716 } else if (sa->sa_family == AF_INET6) {
5717 struct sockaddr_in6 *sin6;
5719 sin6 = (struct sockaddr_in6 *)sa;
5720 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5721 /* Must be non-mapped for connectx */
5727 incr = sizeof(struct sockaddr_in6);
5728 if (sa->sa_len != incr) {
5738 SCTP_INP_INCR_REF(inp);
5739 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
5741 /* Already have or am bring up an association */
5744 SCTP_INP_DECR_REF(inp);
5746 if ((at + incr) > (size_t)limit) {
5750 sa = (struct sockaddr *)((caddr_t)sa + incr);
5752 return ((struct sctp_tcb *)NULL);
5756 * sctp_bindx(ADD) for one address.
5757 * assumes all arguments are valid/checked by caller.
5760 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
5761 struct sockaddr *sa, sctp_assoc_t assoc_id,
5762 uint32_t vrf_id, int *error, void *p)
5764 struct sockaddr *addr_touse;
5765 struct sockaddr_in sin;
5767 /* see if we're bound all already! */
5768 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5774 if (sa->sa_family == AF_INET6) {
5775 struct sockaddr_in6 *sin6;
5777 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
5781 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
5782 /* can only bind v6 on PF_INET6 sockets */
5786 sin6 = (struct sockaddr_in6 *)addr_touse;
5787 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5788 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
5789 SCTP_IPV6_V6ONLY(inp)) {
5790 /* can't bind v4-mapped on PF_INET sockets */
5794 in6_sin6_2_sin(&sin, sin6);
5795 addr_touse = (struct sockaddr *)&sin;
5799 if (sa->sa_family == AF_INET) {
5800 if (sa->sa_len != sizeof(struct sockaddr_in)) {
5804 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
5805 SCTP_IPV6_V6ONLY(inp)) {
5806 /* can't bind v4 on PF_INET sockets */
5811 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
5813 /* Can't get proc for Net/Open BSD */
5817 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
5821 * No locks required here since bind and mgmt_ep_sa all do their own
5822 * locking. If we do something for the FIX: below we may need to
5823 * lock in that case.
5825 if (assoc_id == 0) {
5826 /* add the address */
5827 struct sctp_inpcb *lep;
5828 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
5830 /* validate the incoming port */
5831 if ((lsin->sin_port != 0) &&
5832 (lsin->sin_port != inp->sctp_lport)) {
5836 /* user specified 0 port, set it to existing port */
5837 lsin->sin_port = inp->sctp_lport;
5840 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
5843 * We must decrement the refcount since we have the
5844 * ep already and are binding. No remove going on
5847 SCTP_INP_DECR_REF(inp);
5850 /* already bound to it.. ok */
5852 } else if (lep == NULL) {
5853 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
5854 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
5855 SCTP_ADD_IP_ADDRESS,
5858 *error = EADDRINUSE;
5864 * FIX: decide whether we allow assoc based bindx
5870 * sctp_bindx(DELETE) for one address.
5871 * assumes all arguments are valid/checked by caller.
5874 sctp_bindx_delete_address(struct socket *so, struct sctp_inpcb *inp,
5875 struct sockaddr *sa, sctp_assoc_t assoc_id,
5876 uint32_t vrf_id, int *error)
5878 struct sockaddr *addr_touse;
5879 struct sockaddr_in sin;
5881 /* see if we're bound all already! */
5882 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5888 if (sa->sa_family == AF_INET6) {
5889 struct sockaddr_in6 *sin6;
5891 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
5895 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
5896 /* can only bind v6 on PF_INET6 sockets */
5900 sin6 = (struct sockaddr_in6 *)addr_touse;
5901 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5902 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
5903 SCTP_IPV6_V6ONLY(inp)) {
5904 /* can't bind mapped-v4 on PF_INET sockets */
5908 in6_sin6_2_sin(&sin, sin6);
5909 addr_touse = (struct sockaddr *)&sin;
5913 if (sa->sa_family == AF_INET) {
5914 if (sa->sa_len != sizeof(struct sockaddr_in)) {
5918 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
5919 SCTP_IPV6_V6ONLY(inp)) {
5920 /* can't bind v4 on PF_INET sockets */
5926 * No lock required mgmt_ep_sa does its own locking. If the FIX:
5927 * below is ever changed we may need to lock before calling
5928 * association level binding.
5930 if (assoc_id == 0) {
5931 /* delete the address */
5932 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
5933 SCTP_DEL_IP_ADDRESS,
5937 * FIX: decide whether we allow assoc based bindx