2 * Copyright (c) 2001-2006, 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$");
37 #include "opt_ipsec.h"
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/fcntl.h>
47 #include <sys/malloc.h>
49 #include <sys/domain.h>
50 #include <sys/file.h> /* for struct knote */
51 #include <sys/kernel.h>
52 #include <sys/event.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
59 #include <sys/kernel.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/sysctl.h>
66 #include <sys/callout.h>
68 #include <net/radix.h>
69 #include <net/route.h>
72 #include <sys/domain.h>
75 #include <sys/limits.h>
77 #include <sys/mutex.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/in_pcb.h>
87 #include <netinet/in_var.h>
88 #include <netinet/ip_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
94 #include <netinet6/in6_pcb.h>
96 #include <netinet6/scope6_var.h>
100 #include <netinet6/ipsec.h>
101 #include <netkey/key.h>
104 #include <netinet/sctp_os.h>
105 #include <netinet/sctp_pcb.h>
106 #include <netinet/sctputil.h>
107 #include <netinet/sctp_var.h>
109 #include <netinet6/sctp6_var.h>
111 #include <netinet/sctp_header.h>
112 #include <netinet/sctp_output.h>
113 #include <netinet/sctp_uio.h>
114 #include <netinet/sctp_timer.h>
115 #include <netinet/sctp_crc32.h>
116 #include <netinet/sctp_indata.h>/* for sctp_deliver_data() */
117 #include <netinet/sctp_auth.h>
118 #include <netinet/sctp_asconf.h>
120 extern int sctp_warm_the_crc32_table;
122 #define NUMBER_OF_MTU_SIZES 18
125 extern uint32_t sctp_debug_on;
130 #ifdef SCTP_STAT_LOGGING
131 int global_sctp_cwnd_log_at = 0;
132 int global_sctp_cwnd_log_rolled = 0;
133 struct sctp_cwnd_log sctp_clog[SCTP_STAT_LOG_SIZE];
136 sctp_get_time_of_event(void)
141 SCTP_GETPTIME_TIMEVAL(&now);
142 timeval = (now.tv_sec % 0x00000fff);
144 timeval |= now.tv_usec & 0xfffff;
150 sctp_clr_stat_log(void)
152 global_sctp_cwnd_log_at = 0;
153 global_sctp_cwnd_log_rolled = 0;
158 sctp_sblog(struct sockbuf *sb,
159 struct sctp_tcb *stcb, int from, int incr)
161 int sctp_cwnd_log_at;
163 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
164 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
165 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
166 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_SB;
167 sctp_clog[sctp_cwnd_log_at].x.sb.stcb = stcb;
168 sctp_clog[sctp_cwnd_log_at].x.sb.so_sbcc = sb->sb_cc;
170 sctp_clog[sctp_cwnd_log_at].x.sb.stcb_sbcc = stcb->asoc.sb_cc;
172 sctp_clog[sctp_cwnd_log_at].x.sb.stcb_sbcc = 0;
173 sctp_clog[sctp_cwnd_log_at].x.sb.incr = incr;
177 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
179 int sctp_cwnd_log_at;
181 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
182 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
183 sctp_clog[sctp_cwnd_log_at].from = 0;
184 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_CLOSE;
185 sctp_clog[sctp_cwnd_log_at].x.close.inp = (void *)inp;
186 sctp_clog[sctp_cwnd_log_at].x.close.sctp_flags = inp->sctp_flags;
188 sctp_clog[sctp_cwnd_log_at].x.close.stcb = (void *)stcb;
189 sctp_clog[sctp_cwnd_log_at].x.close.state = (uint16_t) stcb->asoc.state;
191 sctp_clog[sctp_cwnd_log_at].x.close.stcb = 0;
192 sctp_clog[sctp_cwnd_log_at].x.close.state = 0;
194 sctp_clog[sctp_cwnd_log_at].x.close.loc = loc;
199 rto_logging(struct sctp_nets *net, int from)
201 int sctp_cwnd_log_at;
203 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
204 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
205 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
206 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_RTT;
207 sctp_clog[sctp_cwnd_log_at].x.rto.net = (void *)net;
208 sctp_clog[sctp_cwnd_log_at].x.rto.rtt = net->prev_rtt;
209 sctp_clog[sctp_cwnd_log_at].x.rto.rttvar = net->rtt_variance;
210 sctp_clog[sctp_cwnd_log_at].x.rto.direction = net->rto_variance_dir;
214 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
216 int sctp_cwnd_log_at;
218 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
219 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
220 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
221 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_STRM;
222 sctp_clog[sctp_cwnd_log_at].x.strlog.stcb = stcb;
223 sctp_clog[sctp_cwnd_log_at].x.strlog.n_tsn = tsn;
224 sctp_clog[sctp_cwnd_log_at].x.strlog.n_sseq = sseq;
225 sctp_clog[sctp_cwnd_log_at].x.strlog.e_tsn = 0;
226 sctp_clog[sctp_cwnd_log_at].x.strlog.e_sseq = 0;
227 sctp_clog[sctp_cwnd_log_at].x.strlog.strm = stream;
231 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
233 int sctp_cwnd_log_at;
235 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
236 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
237 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) action;
238 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_NAGLE;
239 sctp_clog[sctp_cwnd_log_at].x.nagle.stcb = (void *)stcb;
240 sctp_clog[sctp_cwnd_log_at].x.nagle.total_flight = stcb->asoc.total_flight;
241 sctp_clog[sctp_cwnd_log_at].x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
242 sctp_clog[sctp_cwnd_log_at].x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
243 sctp_clog[sctp_cwnd_log_at].x.nagle.count_in_flight = stcb->asoc.total_flight_count;
248 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
250 int sctp_cwnd_log_at;
252 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
253 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
254 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
255 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_SACK;
256 sctp_clog[sctp_cwnd_log_at].x.sack.cumack = cumack;
257 sctp_clog[sctp_cwnd_log_at].x.sack.oldcumack = old_cumack;
258 sctp_clog[sctp_cwnd_log_at].x.sack.tsn = tsn;
259 sctp_clog[sctp_cwnd_log_at].x.sack.numGaps = gaps;
260 sctp_clog[sctp_cwnd_log_at].x.sack.numDups = dups;
264 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
266 int sctp_cwnd_log_at;
268 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
269 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
270 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
271 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_MAP;
272 sctp_clog[sctp_cwnd_log_at].x.map.base = map;
273 sctp_clog[sctp_cwnd_log_at].x.map.cum = cum;
274 sctp_clog[sctp_cwnd_log_at].x.map.high = high;
278 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn,
281 int sctp_cwnd_log_at;
283 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
284 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
285 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
286 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_FR;
287 sctp_clog[sctp_cwnd_log_at].x.fr.largest_tsn = biggest_tsn;
288 sctp_clog[sctp_cwnd_log_at].x.fr.largest_new_tsn = biggest_new_tsn;
289 sctp_clog[sctp_cwnd_log_at].x.fr.tsn = tsn;
294 sctp_log_mb(struct mbuf *m, int from)
296 int sctp_cwnd_log_at;
298 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
299 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
300 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
301 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_MBUF;
302 sctp_clog[sctp_cwnd_log_at].x.mb.mp = m;
303 sctp_clog[sctp_cwnd_log_at].x.mb.mbuf_flags = (uint8_t) (m->m_flags);
304 sctp_clog[sctp_cwnd_log_at].x.mb.size = (uint16_t) (m->m_len);
305 sctp_clog[sctp_cwnd_log_at].x.mb.data = m->m_data;
306 if (m->m_flags & M_EXT) {
307 sctp_clog[sctp_cwnd_log_at].x.mb.ext = m->m_ext.ext_buf;
308 sctp_clog[sctp_cwnd_log_at].x.mb.refcnt = (uint8_t) (*m->m_ext.ref_cnt);
310 sctp_clog[sctp_cwnd_log_at].x.mb.ext = 0;
311 sctp_clog[sctp_cwnd_log_at].x.mb.refcnt = 0;
317 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk,
320 int sctp_cwnd_log_at;
322 if (control == NULL) {
323 printf("Gak log of NULL?\n");
326 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
327 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
328 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
329 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_STRM;
330 sctp_clog[sctp_cwnd_log_at].x.strlog.stcb = control->stcb;
331 sctp_clog[sctp_cwnd_log_at].x.strlog.n_tsn = control->sinfo_tsn;
332 sctp_clog[sctp_cwnd_log_at].x.strlog.n_sseq = control->sinfo_ssn;
333 sctp_clog[sctp_cwnd_log_at].x.strlog.strm = control->sinfo_stream;
334 if (poschk != NULL) {
335 sctp_clog[sctp_cwnd_log_at].x.strlog.e_tsn = poschk->sinfo_tsn;
336 sctp_clog[sctp_cwnd_log_at].x.strlog.e_sseq = poschk->sinfo_ssn;
338 sctp_clog[sctp_cwnd_log_at].x.strlog.e_tsn = 0;
339 sctp_clog[sctp_cwnd_log_at].x.strlog.e_sseq = 0;
344 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
346 int sctp_cwnd_log_at;
348 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
349 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
350 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
351 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_CWND;
352 sctp_clog[sctp_cwnd_log_at].x.cwnd.net = net;
353 if (stcb->asoc.send_queue_cnt > 255)
354 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_send = 255;
356 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
357 if (stcb->asoc.stream_queue_cnt > 255)
358 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_str = 255;
360 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
363 sctp_clog[sctp_cwnd_log_at].x.cwnd.cwnd_new_value = net->cwnd;
364 sctp_clog[sctp_cwnd_log_at].x.cwnd.inflight = net->flight_size;
365 sctp_clog[sctp_cwnd_log_at].x.cwnd.pseudo_cumack = net->pseudo_cumack;
366 sctp_clog[sctp_cwnd_log_at].x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
367 sctp_clog[sctp_cwnd_log_at].x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
369 if (SCTP_CWNDLOG_PRESEND == from) {
370 sctp_clog[sctp_cwnd_log_at].x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
372 sctp_clog[sctp_cwnd_log_at].x.cwnd.cwnd_augment = augment;
376 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
378 int sctp_cwnd_log_at;
380 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
381 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
382 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
383 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_LOCK_EVENT;
385 sctp_clog[sctp_cwnd_log_at].x.lock.sock = (void *)inp->sctp_socket;
388 sctp_clog[sctp_cwnd_log_at].x.lock.sock = (void *)NULL;
390 sctp_clog[sctp_cwnd_log_at].x.lock.inp = (void *)inp;
392 sctp_clog[sctp_cwnd_log_at].x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
394 sctp_clog[sctp_cwnd_log_at].x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
397 sctp_clog[sctp_cwnd_log_at].x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
398 sctp_clog[sctp_cwnd_log_at].x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
400 sctp_clog[sctp_cwnd_log_at].x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
401 sctp_clog[sctp_cwnd_log_at].x.lock.create_lock = SCTP_LOCK_UNKNOWN;
403 sctp_clog[sctp_cwnd_log_at].x.lock.info_lock = mtx_owned(&sctppcbinfo.ipi_ep_mtx);
404 if (inp->sctp_socket) {
405 sctp_clog[sctp_cwnd_log_at].x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
406 sctp_clog[sctp_cwnd_log_at].x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
407 sctp_clog[sctp_cwnd_log_at].x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
409 sctp_clog[sctp_cwnd_log_at].x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
410 sctp_clog[sctp_cwnd_log_at].x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
411 sctp_clog[sctp_cwnd_log_at].x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
416 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
418 int sctp_cwnd_log_at;
420 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
421 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
422 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
423 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_MAXBURST;
424 sctp_clog[sctp_cwnd_log_at].x.cwnd.net = net;
425 sctp_clog[sctp_cwnd_log_at].x.cwnd.cwnd_new_value = error;
426 sctp_clog[sctp_cwnd_log_at].x.cwnd.inflight = net->flight_size;
427 sctp_clog[sctp_cwnd_log_at].x.cwnd.cwnd_augment = burst;
428 if (stcb->asoc.send_queue_cnt > 255)
429 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_send = 255;
431 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
432 if (stcb->asoc.stream_queue_cnt > 255)
433 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_str = 255;
435 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
439 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
441 int sctp_cwnd_log_at;
443 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
444 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
445 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
446 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_RWND;
447 sctp_clog[sctp_cwnd_log_at].x.rwnd.rwnd = peers_rwnd;
448 sctp_clog[sctp_cwnd_log_at].x.rwnd.send_size = snd_size;
449 sctp_clog[sctp_cwnd_log_at].x.rwnd.overhead = overhead;
450 sctp_clog[sctp_cwnd_log_at].x.rwnd.new_rwnd = 0;
454 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
456 int sctp_cwnd_log_at;
458 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
459 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
460 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
461 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_RWND;
462 sctp_clog[sctp_cwnd_log_at].x.rwnd.rwnd = peers_rwnd;
463 sctp_clog[sctp_cwnd_log_at].x.rwnd.send_size = flight_size;
464 sctp_clog[sctp_cwnd_log_at].x.rwnd.overhead = overhead;
465 sctp_clog[sctp_cwnd_log_at].x.rwnd.new_rwnd = a_rwndval;
469 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
471 int sctp_cwnd_log_at;
473 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
474 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
475 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
476 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_MBCNT;
477 sctp_clog[sctp_cwnd_log_at].x.mbcnt.total_queue_size = total_oq;
478 sctp_clog[sctp_cwnd_log_at].x.mbcnt.size_change = book;
479 sctp_clog[sctp_cwnd_log_at].x.mbcnt.total_queue_mb_size = total_mbcnt_q;
480 sctp_clog[sctp_cwnd_log_at].x.mbcnt.mbcnt_change = mbcnt;
484 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
486 int sctp_cwnd_log_at;
488 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
489 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
490 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
491 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_MISC_EVENT;
492 sctp_clog[sctp_cwnd_log_at].x.misc.log1 = a;
493 sctp_clog[sctp_cwnd_log_at].x.misc.log2 = b;
494 sctp_clog[sctp_cwnd_log_at].x.misc.log3 = c;
495 sctp_clog[sctp_cwnd_log_at].x.misc.log4 = d;
499 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t cumtsn, uint32_t wake_cnt, int from)
501 int sctp_cwnd_log_at;
503 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
504 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
505 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
506 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_WAKE;
507 sctp_clog[sctp_cwnd_log_at].x.wake.stcb = (void *)stcb;
508 sctp_clog[sctp_cwnd_log_at].x.wake.wake_cnt = wake_cnt;
509 sctp_clog[sctp_cwnd_log_at].x.wake.flight = stcb->asoc.total_flight_count;
510 sctp_clog[sctp_cwnd_log_at].x.wake.send_q = stcb->asoc.send_queue_cnt;
511 sctp_clog[sctp_cwnd_log_at].x.wake.sent_q = stcb->asoc.sent_queue_cnt;
513 if (stcb->asoc.stream_queue_cnt < 0xff)
514 sctp_clog[sctp_cwnd_log_at].x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
516 sctp_clog[sctp_cwnd_log_at].x.wake.stream_qcnt = 0xff;
518 if (stcb->asoc.chunks_on_out_queue < 0xff)
519 sctp_clog[sctp_cwnd_log_at].x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
521 sctp_clog[sctp_cwnd_log_at].x.wake.chunks_on_oque = 0xff;
523 sctp_clog[sctp_cwnd_log_at].x.wake.sctpflags = 0;
524 /* set in the defered mode stuff */
525 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
526 sctp_clog[sctp_cwnd_log_at].x.wake.sctpflags |= 1;
527 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
528 sctp_clog[sctp_cwnd_log_at].x.wake.sctpflags |= 2;
529 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
530 sctp_clog[sctp_cwnd_log_at].x.wake.sctpflags |= 4;
531 /* what about the sb */
532 if (stcb->sctp_socket) {
533 struct socket *so = stcb->sctp_socket;
535 sctp_clog[sctp_cwnd_log_at].x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
537 sctp_clog[sctp_cwnd_log_at].x.wake.sbflags = 0xff;
542 sctp_log_block(uint8_t from, struct socket *so, struct sctp_association *asoc, int sendlen)
544 int sctp_cwnd_log_at;
546 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
547 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
548 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
549 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_BLOCK;
550 sctp_clog[sctp_cwnd_log_at].x.blk.onsb = asoc->total_output_queue_size;
551 sctp_clog[sctp_cwnd_log_at].x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
552 sctp_clog[sctp_cwnd_log_at].x.blk.peer_rwnd = asoc->peers_rwnd;
553 sctp_clog[sctp_cwnd_log_at].x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
554 sctp_clog[sctp_cwnd_log_at].x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
555 sctp_clog[sctp_cwnd_log_at].x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
556 sctp_clog[sctp_cwnd_log_at].x.blk.sndlen = sendlen;
560 sctp_fill_stat_log(struct mbuf *m)
562 int sctp_cwnd_log_at;
563 struct sctp_cwnd_log_req *req;
565 int num, i, at, cnt_out = 0;
570 size_limit = (m->m_len - sizeof(struct sctp_cwnd_log_req));
571 if (size_limit < sizeof(struct sctp_cwnd_log)) {
574 sctp_cwnd_log_at = global_sctp_cwnd_log_at;
575 req = mtod(m, struct sctp_cwnd_log_req *);
576 num = size_limit / sizeof(struct sctp_cwnd_log);
577 if (global_sctp_cwnd_log_rolled) {
578 req->num_in_log = SCTP_STAT_LOG_SIZE;
580 req->num_in_log = sctp_cwnd_log_at;
582 * if the log has not rolled, we don't let you have old
585 if (req->end_at > sctp_cwnd_log_at) {
586 req->end_at = sctp_cwnd_log_at;
589 if ((num < SCTP_STAT_LOG_SIZE) &&
590 ((global_sctp_cwnd_log_rolled) || (sctp_cwnd_log_at > num))) {
591 /* we can't return all of it */
592 if (((req->start_at == 0) && (req->end_at == 0)) ||
593 (req->start_at >= SCTP_STAT_LOG_SIZE) ||
594 (req->end_at >= SCTP_STAT_LOG_SIZE)) {
595 /* No user request or user is wacked. */
597 req->end_at = sctp_cwnd_log_at - 1;
598 if ((sctp_cwnd_log_at - num) < 0) {
601 cc = num - sctp_cwnd_log_at;
602 req->start_at = SCTP_STAT_LOG_SIZE - cc;
604 req->start_at = sctp_cwnd_log_at - num;
610 if (req->start_at > req->end_at) {
611 cc = (SCTP_STAT_LOG_SIZE - req->start_at) +
615 cc = (req->end_at - req->start_at) + 1;
623 /* We can return all of it */
625 req->end_at = sctp_cwnd_log_at - 1;
626 req->num_ret = sctp_cwnd_log_at;
629 if (req->num_ret > num) {
630 panic("Bad statlog get?");
633 for (i = 0, at = req->start_at; i < req->num_ret; i++) {
634 req->log[i] = sctp_clog[at];
637 if (at >= SCTP_STAT_LOG_SIZE)
640 m->m_len = (cnt_out * sizeof(struct sctp_cwnd_log)) + sizeof(struct sctp_cwnd_log_req);
646 #ifdef SCTP_AUDITING_ENABLED
647 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
648 static int sctp_audit_indx = 0;
652 sctp_print_audit_report(void)
658 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
659 if ((sctp_audit_data[i][0] == 0xe0) &&
660 (sctp_audit_data[i][1] == 0x01)) {
663 } else if (sctp_audit_data[i][0] == 0xf0) {
666 } else if ((sctp_audit_data[i][0] == 0xc0) &&
667 (sctp_audit_data[i][1] == 0x01)) {
671 printf("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
672 (uint32_t) sctp_audit_data[i][1]);
677 for (i = 0; i < sctp_audit_indx; i++) {
678 if ((sctp_audit_data[i][0] == 0xe0) &&
679 (sctp_audit_data[i][1] == 0x01)) {
682 } else if (sctp_audit_data[i][0] == 0xf0) {
685 } else if ((sctp_audit_data[i][0] == 0xc0) &&
686 (sctp_audit_data[i][1] == 0x01)) {
690 printf("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
691 (uint32_t) sctp_audit_data[i][1]);
700 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
701 struct sctp_nets *net)
703 int resend_cnt, tot_out, rep, tot_book_cnt;
704 struct sctp_nets *lnet;
705 struct sctp_tmit_chunk *chk;
707 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
708 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
710 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
714 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
715 sctp_audit_data[sctp_audit_indx][1] = 0x01;
717 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
723 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
724 sctp_audit_data[sctp_audit_indx][1] = 0x02;
726 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
731 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
732 sctp_audit_data[sctp_audit_indx][1] =
733 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
735 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
740 resend_cnt = tot_out = 0;
741 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
742 if (chk->sent == SCTP_DATAGRAM_RESEND) {
744 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
745 tot_out += chk->book_size;
749 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
750 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
751 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
753 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
756 printf("resend_cnt:%d asoc-tot:%d\n",
757 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
759 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
760 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
761 sctp_audit_data[sctp_audit_indx][1] =
762 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
764 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
768 if (tot_out != stcb->asoc.total_flight) {
769 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
770 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
772 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
776 printf("tot_flt:%d asoc_tot:%d\n", tot_out,
777 (int)stcb->asoc.total_flight);
778 stcb->asoc.total_flight = tot_out;
780 if (tot_book_cnt != stcb->asoc.total_flight_count) {
781 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
782 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
784 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
788 printf("tot_flt_book:%d\n", tot_book);
790 stcb->asoc.total_flight_count = tot_book_cnt;
793 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
794 tot_out += lnet->flight_size;
796 if (tot_out != stcb->asoc.total_flight) {
797 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
798 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
800 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
804 printf("real flight:%d net total was %d\n",
805 stcb->asoc.total_flight, tot_out);
806 /* now corrective action */
807 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
810 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
811 if ((chk->whoTo == lnet) &&
812 (chk->sent < SCTP_DATAGRAM_RESEND)) {
813 tot_out += chk->book_size;
816 if (lnet->flight_size != tot_out) {
817 printf("net:%x flight was %d corrected to %d\n",
818 (uint32_t) lnet, lnet->flight_size, tot_out);
819 lnet->flight_size = tot_out;
824 sctp_print_audit_report();
829 sctp_audit_log(uint8_t ev, uint8_t fd)
834 sctp_audit_data[sctp_audit_indx][0] = ev;
835 sctp_audit_data[sctp_audit_indx][1] = fd;
837 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
846 * a list of sizes based on typical mtu's, used only if next hop size not
849 static int sctp_mtu_sizes[] = {
871 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
873 struct sctp_association *asoc;
874 struct sctp_nets *net;
878 callout_stop(&asoc->hb_timer.timer);
879 callout_stop(&asoc->dack_timer.timer);
880 callout_stop(&asoc->strreset_timer.timer);
881 callout_stop(&asoc->asconf_timer.timer);
882 callout_stop(&asoc->autoclose_timer.timer);
883 callout_stop(&asoc->delayed_event_timer.timer);
884 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
885 callout_stop(&net->fr_timer.timer);
886 callout_stop(&net->pmtu_timer.timer);
891 find_next_best_mtu(int totsz)
896 * if we are in here we must find the next best fit based on the
897 * size of the dg that failed to be sent.
900 for (i = 0; i < NUMBER_OF_MTU_SIZES; i++) {
901 if (totsz < sctp_mtu_sizes[i]) {
908 return (sctp_mtu_sizes[perfer]);
912 sctp_fill_random_store(struct sctp_pcb *m)
915 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
916 * our counter. The result becomes our good random numbers and we
917 * then setup to give these out. Note that we do no locking to
918 * protect this. This is ok, since if competing folks call this we
919 * will get more gobbled gook in the random store whic is what we
920 * want. There is a danger that two guys will use the same random
921 * numbers, but thats ok too since that is random as well :->
924 sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
925 sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
926 sizeof(m->random_counter), (uint8_t *) m->random_store);
931 sctp_select_initial_TSN(struct sctp_pcb *m)
934 * A true implementation should use random selection process to get
935 * the initial stream sequence number, using RFC1750 as a good
941 if (m->initial_sequence_debug != 0) {
944 ret = m->initial_sequence_debug;
945 m->initial_sequence_debug++;
948 if ((m->store_at + sizeof(u_long)) > SCTP_SIGNATURE_SIZE) {
949 /* Refill the random store */
950 sctp_fill_random_store(m);
952 p = &m->random_store[(int)m->store_at];
955 m->store_at += sizeof(u_long);
960 sctp_select_a_tag(struct sctp_inpcb *m)
965 SCTP_GETTIME_TIMEVAL(&now);
968 x = sctp_select_initial_TSN(&m->sctp_ep);
973 if (sctp_is_vtag_good(m, x, &now)) {
982 sctp_init_asoc(struct sctp_inpcb *m, struct sctp_association *asoc,
983 int for_a_init, uint32_t override_tag)
986 * Anything set to zero is taken care of by the allocation routine's
991 * Up front select what scoping to apply on addresses I tell my peer
992 * Not sure what to do with these right now, we will need to come up
993 * with a way to set them. We may need to pass them through from the
994 * caller in the sctp_aloc_assoc() function.
998 /* init all variables to a known value. */
999 asoc->state = SCTP_STATE_INUSE;
1000 asoc->max_burst = m->sctp_ep.max_burst;
1001 asoc->heart_beat_delay = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1002 asoc->cookie_life = m->sctp_ep.def_cookie_life;
1003 asoc->sctp_cmt_on_off = (uint8_t) sctp_cmt_on_off;
1005 asoc->default_tos = m->ip_inp.inp.inp_ip_tos;
1007 asoc->default_tos = 0;
1011 asoc->default_flowlabel = ((struct in6pcb *)m)->in6p_flowinfo;
1013 asoc->default_flowlabel = 0;
1018 if (sctp_is_vtag_good(m, override_tag, &now)) {
1019 asoc->my_vtag = override_tag;
1025 asoc->my_vtag = sctp_select_a_tag(m);
1027 /* Get the nonce tags */
1028 asoc->my_vtag_nonce = sctp_select_a_tag(m);
1029 asoc->peer_vtag_nonce = sctp_select_a_tag(m);
1031 if (sctp_is_feature_on(m, SCTP_PCB_FLAGS_DONOT_HEARTBEAT))
1032 asoc->hb_is_disabled = 1;
1034 asoc->hb_is_disabled = 0;
1037 asoc->assoc_up_sent = 0;
1038 asoc->assoc_id = asoc->my_vtag;
1039 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1040 sctp_select_initial_TSN(&m->sctp_ep);
1041 /* we are optimisitic here */
1042 asoc->peer_supports_pktdrop = 1;
1044 asoc->sent_queue_retran_cnt = 0;
1047 asoc->last_net_data_came_from = NULL;
1049 /* This will need to be adjusted */
1050 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1051 asoc->last_acked_seq = asoc->init_seq_number - 1;
1052 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1053 asoc->asconf_seq_in = asoc->last_acked_seq;
1055 /* here we are different, we hold the next one we expect */
1056 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1058 asoc->initial_init_rto_max = m->sctp_ep.initial_init_rto_max;
1059 asoc->initial_rto = m->sctp_ep.initial_rto;
1061 asoc->max_init_times = m->sctp_ep.max_init_times;
1062 asoc->max_send_times = m->sctp_ep.max_send_times;
1063 asoc->def_net_failure = m->sctp_ep.def_net_failure;
1064 asoc->free_chunk_cnt = 0;
1066 asoc->iam_blocking = 0;
1067 /* ECN Nonce initialization */
1068 asoc->context = m->sctp_context;
1069 asoc->def_send = m->def_send;
1070 asoc->ecn_nonce_allowed = 0;
1071 asoc->receiver_nonce_sum = 1;
1072 asoc->nonce_sum_expect_base = 1;
1073 asoc->nonce_sum_check = 1;
1074 asoc->nonce_resync_tsn = 0;
1075 asoc->nonce_wait_for_ecne = 0;
1076 asoc->nonce_wait_tsn = 0;
1077 asoc->delayed_ack = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1078 asoc->pr_sctp_cnt = 0;
1079 asoc->total_output_queue_size = 0;
1081 if (m->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1082 struct in6pcb *inp6;
1085 /* Its a V6 socket */
1086 inp6 = (struct in6pcb *)m;
1087 asoc->ipv6_addr_legal = 1;
1088 /* Now look at the binding flag to see if V4 will be legal */
1090 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1092 asoc->ipv4_addr_legal = 1;
1094 /* V4 addresses are NOT legal on the association */
1095 asoc->ipv4_addr_legal = 0;
1098 /* Its a V4 socket, no - V6 */
1099 asoc->ipv4_addr_legal = 1;
1100 asoc->ipv6_addr_legal = 0;
1104 asoc->my_rwnd = max(m->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND);
1105 asoc->peers_rwnd = m->sctp_socket->so_rcv.sb_hiwat;
1107 asoc->smallest_mtu = m->sctp_frag_point;
1108 asoc->minrto = m->sctp_ep.sctp_minrto;
1109 asoc->maxrto = m->sctp_ep.sctp_maxrto;
1111 asoc->locked_on_sending = NULL;
1112 asoc->stream_locked_on = 0;
1113 asoc->ecn_echo_cnt_onq = 0;
1114 asoc->stream_locked = 0;
1116 LIST_INIT(&asoc->sctp_local_addr_list);
1117 TAILQ_INIT(&asoc->nets);
1118 TAILQ_INIT(&asoc->pending_reply_queue);
1119 asoc->last_asconf_ack_sent = NULL;
1120 /* Setup to fill the hb random cache at first HB */
1121 asoc->hb_random_idx = 4;
1123 asoc->sctp_autoclose_ticks = m->sctp_ep.auto_close_time;
1126 * Now the stream parameters, here we allocate space for all streams
1127 * that we request by default.
1129 asoc->streamoutcnt = asoc->pre_open_streams =
1130 m->sctp_ep.pre_open_stream_count;
1131 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1132 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1134 if (asoc->strmout == NULL) {
1135 /* big trouble no memory */
1138 for (i = 0; i < asoc->streamoutcnt; i++) {
1140 * inbound side must be set to 0xffff, also NOTE when we get
1141 * the INIT-ACK back (for INIT sender) we MUST reduce the
1142 * count (streamoutcnt) but first check if we sent to any of
1143 * the upper streams that were dropped (if some were). Those
1144 * that were dropped must be notified to the upper layer as
1147 asoc->strmout[i].next_sequence_sent = 0x0;
1148 TAILQ_INIT(&asoc->strmout[i].outqueue);
1149 asoc->strmout[i].stream_no = i;
1150 asoc->strmout[i].last_msg_incomplete = 0;
1151 asoc->strmout[i].next_spoke.tqe_next = 0;
1152 asoc->strmout[i].next_spoke.tqe_prev = 0;
1154 /* Now the mapping array */
1155 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1156 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1158 if (asoc->mapping_array == NULL) {
1159 SCTP_FREE(asoc->strmout);
1162 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1163 /* Now the init of the other outqueues */
1164 TAILQ_INIT(&asoc->free_chunks);
1165 TAILQ_INIT(&asoc->free_strmoq);
1166 TAILQ_INIT(&asoc->out_wheel);
1167 TAILQ_INIT(&asoc->control_send_queue);
1168 TAILQ_INIT(&asoc->send_queue);
1169 TAILQ_INIT(&asoc->sent_queue);
1170 TAILQ_INIT(&asoc->reasmqueue);
1171 TAILQ_INIT(&asoc->resetHead);
1172 asoc->max_inbound_streams = m->sctp_ep.max_open_streams_intome;
1173 TAILQ_INIT(&asoc->asconf_queue);
1174 /* authentication fields */
1175 asoc->authinfo.random = NULL;
1176 asoc->authinfo.assoc_key = NULL;
1177 asoc->authinfo.assoc_keyid = 0;
1178 asoc->authinfo.recv_key = NULL;
1179 asoc->authinfo.recv_keyid = 0;
1180 LIST_INIT(&asoc->shared_keys);
1186 sctp_expand_mapping_array(struct sctp_association *asoc)
1188 /* mapping array needs to grow */
1192 new_size = asoc->mapping_array_size + SCTP_MAPPING_ARRAY_INCR;
1193 SCTP_MALLOC(new_array, uint8_t *, new_size, "MappingArray");
1194 if (new_array == NULL) {
1195 /* can't get more, forget it */
1196 printf("No memory for expansion of SCTP mapping array %d\n",
1200 memset(new_array, 0, new_size);
1201 memcpy(new_array, asoc->mapping_array, asoc->mapping_array_size);
1202 SCTP_FREE(asoc->mapping_array);
1203 asoc->mapping_array = new_array;
1204 asoc->mapping_array_size = new_size;
1208 extern unsigned int sctp_early_fr_msec;
1211 sctp_handle_addr_wq(void)
1213 /* deal with the ADDR wq from the rtsock calls */
1214 struct sctp_laddr *wi;
1216 SCTP_IPI_ADDR_LOCK();
1217 wi = LIST_FIRST(&sctppcbinfo.addr_wq);
1219 SCTP_IPI_ADDR_UNLOCK();
1222 LIST_REMOVE(wi, sctp_nxt_addr);
1223 if (!LIST_EMPTY(&sctppcbinfo.addr_wq)) {
1224 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1225 (struct sctp_inpcb *)NULL,
1226 (struct sctp_tcb *)NULL,
1227 (struct sctp_nets *)NULL);
1229 SCTP_IPI_ADDR_UNLOCK();
1230 if (wi->action == RTM_ADD) {
1231 sctp_add_ip_address(wi->ifa);
1232 } else if (wi->action == RTM_DELETE) {
1233 sctp_delete_ip_address(wi->ifa);
1236 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, wi);
1237 SCTP_DECR_LADDR_COUNT();
1241 sctp_timeout_handler(void *t)
1243 struct sctp_inpcb *inp;
1244 struct sctp_tcb *stcb;
1245 struct sctp_nets *net;
1246 struct sctp_timer *tmr;
1248 struct sctp_iterator *it = NULL;
1252 tmr = (struct sctp_timer *)t;
1253 inp = (struct sctp_inpcb *)tmr->ep;
1254 stcb = (struct sctp_tcb *)tmr->tcb;
1255 net = (struct sctp_nets *)tmr->net;
1258 #ifdef SCTP_AUDITING_ENABLED
1259 sctp_audit_log(0xF0, (uint8_t) tmr->type);
1260 sctp_auditing(3, inp, stcb, net);
1263 /* sanity checks... */
1264 if (tmr->self != (void *)tmr) {
1266 * printf("Stale SCTP timer fired (%p), ignoring...\n",
1272 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1274 * printf("SCTP timer fired with invalid type: 0x%x\n",
1280 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1284 /* if this is an iterator timeout, get the struct and clear inp */
1285 if (tmr->type == SCTP_TIMER_TYPE_ITERATOR) {
1286 it = (struct sctp_iterator *)inp;
1290 SCTP_INP_INCR_REF(inp);
1291 if ((inp->sctp_socket == 0) &&
1292 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1293 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1294 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1295 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1296 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1299 SCTP_INP_DECR_REF(inp);
1304 if (stcb->asoc.state == 0) {
1307 SCTP_INP_DECR_REF(inp);
1313 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1314 printf("Timer type %d goes off\n", tmr->type);
1316 #endif /* SCTP_DEBUG */
1317 if (!callout_active(&tmr->timer)) {
1320 SCTP_INP_DECR_REF(inp);
1325 atomic_add_int(&stcb->asoc.refcnt, 1);
1326 SCTP_TCB_LOCK(stcb);
1327 atomic_add_int(&stcb->asoc.refcnt, -1);
1329 /* mark as being serviced now */
1330 callout_deactivate(&tmr->timer);
1332 /* call the handler for the appropriate timer type */
1333 switch (tmr->type) {
1334 case SCTP_TIMER_TYPE_ADDR_WQ:
1335 sctp_handle_addr_wq();
1337 case SCTP_TIMER_TYPE_ITERATOR:
1338 SCTP_STAT_INCR(sctps_timoiterator);
1339 sctp_iterator_timer(it);
1341 case SCTP_TIMER_TYPE_SEND:
1342 SCTP_STAT_INCR(sctps_timodata);
1343 stcb->asoc.num_send_timers_up--;
1344 if (stcb->asoc.num_send_timers_up < 0) {
1345 stcb->asoc.num_send_timers_up = 0;
1347 if (sctp_t3rxt_timer(inp, stcb, net)) {
1348 /* no need to unlock on tcb its gone */
1352 #ifdef SCTP_AUDITING_ENABLED
1353 sctp_auditing(4, inp, stcb, net);
1355 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1356 if ((stcb->asoc.num_send_timers_up == 0) &&
1357 (stcb->asoc.sent_queue_cnt > 0)
1359 struct sctp_tmit_chunk *chk;
1362 * safeguard. If there on some on the sent queue
1363 * somewhere but no timers running something is
1364 * wrong... so we start a timer on the first chunk
1365 * on the send queue on whatever net it is sent to.
1367 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1368 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1372 case SCTP_TIMER_TYPE_INIT:
1373 SCTP_STAT_INCR(sctps_timoinit);
1374 if (sctp_t1init_timer(inp, stcb, net)) {
1375 /* no need to unlock on tcb its gone */
1378 /* We do output but not here */
1381 case SCTP_TIMER_TYPE_RECV:
1382 SCTP_STAT_INCR(sctps_timosack);
1383 sctp_send_sack(stcb);
1384 #ifdef SCTP_AUDITING_ENABLED
1385 sctp_auditing(4, inp, stcb, net);
1387 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR);
1389 case SCTP_TIMER_TYPE_SHUTDOWN:
1390 if (sctp_shutdown_timer(inp, stcb, net)) {
1391 /* no need to unlock on tcb its gone */
1394 SCTP_STAT_INCR(sctps_timoshutdown);
1395 #ifdef SCTP_AUDITING_ENABLED
1396 sctp_auditing(4, inp, stcb, net);
1398 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR);
1400 case SCTP_TIMER_TYPE_HEARTBEAT:
1402 struct sctp_nets *net;
1403 int cnt_of_unconf = 0;
1405 SCTP_STAT_INCR(sctps_timoheartbeat);
1406 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1407 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1408 (net->dest_state & SCTP_ADDR_REACHABLE)) {
1412 if (cnt_of_unconf == 0) {
1413 if (sctp_heartbeat_timer(inp, stcb, net, cnt_of_unconf)) {
1414 /* no need to unlock on tcb its gone */
1418 #ifdef SCTP_AUDITING_ENABLED
1419 sctp_auditing(4, inp, stcb, net);
1421 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
1423 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR);
1426 case SCTP_TIMER_TYPE_COOKIE:
1427 if (sctp_cookie_timer(inp, stcb, net)) {
1428 /* no need to unlock on tcb its gone */
1431 SCTP_STAT_INCR(sctps_timocookie);
1432 #ifdef SCTP_AUDITING_ENABLED
1433 sctp_auditing(4, inp, stcb, net);
1436 * We consider T3 and Cookie timer pretty much the same with
1437 * respect to where from in chunk_output.
1439 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1441 case SCTP_TIMER_TYPE_NEWCOOKIE:
1446 SCTP_STAT_INCR(sctps_timosecret);
1447 SCTP_GETTIME_TIMEVAL(&tv);
1448 SCTP_INP_WLOCK(inp);
1449 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1450 inp->sctp_ep.last_secret_number =
1451 inp->sctp_ep.current_secret_number;
1452 inp->sctp_ep.current_secret_number++;
1453 if (inp->sctp_ep.current_secret_number >=
1454 SCTP_HOW_MANY_SECRETS) {
1455 inp->sctp_ep.current_secret_number = 0;
1457 secret = (int)inp->sctp_ep.current_secret_number;
1458 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1459 inp->sctp_ep.secret_key[secret][i] =
1460 sctp_select_initial_TSN(&inp->sctp_ep);
1462 SCTP_INP_WUNLOCK(inp);
1463 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1467 case SCTP_TIMER_TYPE_PATHMTURAISE:
1468 SCTP_STAT_INCR(sctps_timopathmtu);
1469 sctp_pathmtu_timer(inp, stcb, net);
1472 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1473 if (sctp_shutdownack_timer(inp, stcb, net)) {
1474 /* no need to unlock on tcb its gone */
1477 SCTP_STAT_INCR(sctps_timoshutdownack);
1478 #ifdef SCTP_AUDITING_ENABLED
1479 sctp_auditing(4, inp, stcb, net);
1481 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR);
1483 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1484 SCTP_STAT_INCR(sctps_timoshutdownguard);
1485 sctp_abort_an_association(inp, stcb,
1486 SCTP_SHUTDOWN_GUARD_EXPIRES, NULL);
1487 /* no need to unlock on tcb its gone */
1491 case SCTP_TIMER_TYPE_STRRESET:
1492 if (sctp_strreset_timer(inp, stcb, net)) {
1493 /* no need to unlock on tcb its gone */
1496 SCTP_STAT_INCR(sctps_timostrmrst);
1497 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR);
1499 case SCTP_TIMER_TYPE_EARLYFR:
1500 /* Need to do FR of things for net */
1501 SCTP_STAT_INCR(sctps_timoearlyfr);
1502 sctp_early_fr_timer(inp, stcb, net);
1504 case SCTP_TIMER_TYPE_ASCONF:
1505 if (sctp_asconf_timer(inp, stcb, net)) {
1506 /* no need to unlock on tcb its gone */
1509 SCTP_STAT_INCR(sctps_timoasconf);
1510 #ifdef SCTP_AUDITING_ENABLED
1511 sctp_auditing(4, inp, stcb, net);
1513 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR);
1516 case SCTP_TIMER_TYPE_AUTOCLOSE:
1517 SCTP_STAT_INCR(sctps_timoautoclose);
1518 sctp_autoclose_timer(inp, stcb, net);
1519 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR);
1522 case SCTP_TIMER_TYPE_ASOCKILL:
1523 SCTP_STAT_INCR(sctps_timoassockill);
1524 /* Can we free it yet? */
1525 SCTP_INP_DECR_REF(inp);
1526 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
1527 sctp_free_assoc(inp, stcb, 0);
1529 * free asoc, always unlocks (or destroy's) so prevent
1530 * duplicate unlock or unlock of a free mtx :-0
1535 case SCTP_TIMER_TYPE_INPKILL:
1536 SCTP_STAT_INCR(sctps_timoinpkill);
1538 * special case, take away our increment since WE are the
1541 SCTP_INP_DECR_REF(inp);
1542 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
1543 sctp_inpcb_free(inp, 1, 0);
1548 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1549 printf("sctp_timeout_handler:unknown timer %d\n",
1552 #endif /* SCTP_DEBUG */
1555 #ifdef SCTP_AUDITING_ENABLED
1556 sctp_audit_log(0xF1, (uint8_t) tmr->type);
1558 sctp_auditing(5, inp, stcb, net);
1560 if ((did_output) && stcb) {
1562 * Now we need to clean up the control chunk chain if an
1563 * ECNE is on it. It must be marked as UNSENT again so next
1564 * call will continue to send it until such time that we get
1565 * a CWR, to remove it. It is, however, less likely that we
1566 * will find a ecn echo on the chain though.
1568 sctp_fix_ecn_echo(&stcb->asoc);
1571 SCTP_TCB_UNLOCK(stcb);
1575 SCTP_INP_DECR_REF(inp);
1580 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1581 printf("Timer now complete (type %d)\n", tmr->type);
1583 #endif /* SCTP_DEBUG */
1590 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1591 struct sctp_nets *net)
1594 struct sctp_timer *tmr;
1597 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
1605 SCTP_TCB_LOCK_ASSERT(stcb);
1608 case SCTP_TIMER_TYPE_ADDR_WQ:
1609 /* Only 1 tick away :-) */
1610 tmr = &sctppcbinfo.addr_wq_timer;
1613 case SCTP_TIMER_TYPE_ITERATOR:
1615 struct sctp_iterator *it;
1617 it = (struct sctp_iterator *)inp;
1619 to_ticks = SCTP_ITERATOR_TICKS;
1622 case SCTP_TIMER_TYPE_SEND:
1623 /* Here we use the RTO timer */
1627 if ((stcb == NULL) || (net == NULL)) {
1630 tmr = &net->rxt_timer;
1631 if (net->RTO == 0) {
1632 rto_val = stcb->asoc.initial_rto;
1636 to_ticks = MSEC_TO_TICKS(rto_val);
1639 case SCTP_TIMER_TYPE_INIT:
1641 * Here we use the INIT timer default usually about 1
1644 if ((stcb == NULL) || (net == NULL)) {
1647 tmr = &net->rxt_timer;
1648 if (net->RTO == 0) {
1649 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1651 to_ticks = MSEC_TO_TICKS(net->RTO);
1654 case SCTP_TIMER_TYPE_RECV:
1656 * Here we use the Delayed-Ack timer value from the inp
1657 * ususually about 200ms.
1662 tmr = &stcb->asoc.dack_timer;
1663 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1665 case SCTP_TIMER_TYPE_SHUTDOWN:
1666 /* Here we use the RTO of the destination. */
1667 if ((stcb == NULL) || (net == NULL)) {
1670 if (net->RTO == 0) {
1671 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1673 to_ticks = MSEC_TO_TICKS(net->RTO);
1675 tmr = &net->rxt_timer;
1677 case SCTP_TIMER_TYPE_HEARTBEAT:
1679 * the net is used here so that we can add in the RTO. Even
1680 * though we use a different timer. We also add the HB timer
1681 * PLUS a random jitter.
1687 uint8_t this_random;
1688 int cnt_of_unconf = 0;
1689 struct sctp_nets *lnet;
1691 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1692 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1693 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1697 if (cnt_of_unconf) {
1699 sctp_heartbeat_timer(inp, stcb, lnet, cnt_of_unconf);
1701 if (stcb->asoc.hb_random_idx > 3) {
1702 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
1703 memcpy(stcb->asoc.hb_random_values, &rndval,
1704 sizeof(stcb->asoc.hb_random_values));
1705 this_random = stcb->asoc.hb_random_values[0];
1706 stcb->asoc.hb_random_idx = 0;
1707 stcb->asoc.hb_ect_randombit = 0;
1709 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
1710 stcb->asoc.hb_random_idx++;
1711 stcb->asoc.hb_ect_randombit = 0;
1714 * this_random will be 0 - 256 ms RTO is in ms.
1716 if ((stcb->asoc.hb_is_disabled) &&
1717 (cnt_of_unconf == 0)) {
1721 struct sctp_nets *lnet;
1724 delay = stcb->asoc.heart_beat_delay;
1725 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1726 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1727 ((lnet->dest_state & SCTP_ADDR_OUT_OF_SCOPE) == 0) &&
1728 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1732 if (net->RTO == 0) {
1733 /* Never been checked */
1734 to_ticks = this_random + stcb->asoc.initial_rto + delay;
1736 /* set rto_val to the ms */
1737 to_ticks = delay + net->RTO + this_random;
1740 if (cnt_of_unconf) {
1741 to_ticks = this_random + stcb->asoc.initial_rto;
1743 to_ticks = stcb->asoc.heart_beat_delay + this_random + stcb->asoc.initial_rto;
1747 * Now we must convert the to_ticks that are now in
1750 to_ticks = MSEC_TO_TICKS(to_ticks);
1751 tmr = &stcb->asoc.hb_timer;
1754 case SCTP_TIMER_TYPE_COOKIE:
1756 * Here we can use the RTO timer from the network since one
1757 * RTT was compelete. If a retran happened then we will be
1758 * using the RTO initial value.
1760 if ((stcb == NULL) || (net == NULL)) {
1763 if (net->RTO == 0) {
1764 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1766 to_ticks = MSEC_TO_TICKS(net->RTO);
1768 tmr = &net->rxt_timer;
1770 case SCTP_TIMER_TYPE_NEWCOOKIE:
1772 * nothing needed but the endpoint here ususually about 60
1775 tmr = &inp->sctp_ep.signature_change;
1776 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
1778 case SCTP_TIMER_TYPE_ASOCKILL:
1782 tmr = &stcb->asoc.strreset_timer;
1783 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
1785 case SCTP_TIMER_TYPE_INPKILL:
1787 * The inp is setup to die. We re-use the signature_chage
1788 * timer since that has stopped and we are in the GONE
1791 tmr = &inp->sctp_ep.signature_change;
1792 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
1794 case SCTP_TIMER_TYPE_PATHMTURAISE:
1796 * Here we use the value found in the EP for PMTU ususually
1805 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
1806 tmr = &net->pmtu_timer;
1808 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1809 /* Here we use the RTO of the destination */
1810 if ((stcb == NULL) || (net == NULL)) {
1813 if (net->RTO == 0) {
1814 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1816 to_ticks = MSEC_TO_TICKS(net->RTO);
1818 tmr = &net->rxt_timer;
1820 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1822 * Here we use the endpoints shutdown guard timer usually
1828 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
1829 tmr = &stcb->asoc.shut_guard_timer;
1831 case SCTP_TIMER_TYPE_STRRESET:
1833 * Here the timer comes from the inp but its value is from
1836 if ((stcb == NULL) || (net == NULL)) {
1839 if (net->RTO == 0) {
1840 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1842 to_ticks = MSEC_TO_TICKS(net->RTO);
1844 tmr = &stcb->asoc.strreset_timer;
1847 case SCTP_TIMER_TYPE_EARLYFR:
1851 if ((stcb == NULL) || (net == NULL)) {
1854 if (net->flight_size > net->cwnd) {
1855 /* no need to start */
1858 SCTP_STAT_INCR(sctps_earlyfrstart);
1859 if (net->lastsa == 0) {
1860 /* Hmm no rtt estimate yet? */
1861 msec = stcb->asoc.initial_rto >> 2;
1863 msec = ((net->lastsa >> 2) + net->lastsv) >> 1;
1865 if (msec < sctp_early_fr_msec) {
1866 msec = sctp_early_fr_msec;
1867 if (msec < SCTP_MINFR_MSEC_FLOOR) {
1868 msec = SCTP_MINFR_MSEC_FLOOR;
1871 to_ticks = MSEC_TO_TICKS(msec);
1872 tmr = &net->fr_timer;
1875 case SCTP_TIMER_TYPE_ASCONF:
1877 * Here the timer comes from the inp but its value is from
1880 if ((stcb == NULL) || (net == NULL)) {
1883 if (net->RTO == 0) {
1884 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1886 to_ticks = MSEC_TO_TICKS(net->RTO);
1888 tmr = &stcb->asoc.asconf_timer;
1890 case SCTP_TIMER_TYPE_AUTOCLOSE:
1894 if (stcb->asoc.sctp_autoclose_ticks == 0) {
1896 * Really an error since stcb is NOT set to
1901 to_ticks = stcb->asoc.sctp_autoclose_ticks;
1902 tmr = &stcb->asoc.autoclose_timer;
1906 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1907 printf("sctp_timer_start:Unknown timer type %d\n",
1910 #endif /* SCTP_DEBUG */
1914 if ((to_ticks <= 0) || (tmr == NULL)) {
1916 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1917 printf("sctp_timer_start:%d:software error to_ticks:%d tmr:%p not set ??\n",
1918 t_type, to_ticks, tmr);
1920 #endif /* SCTP_DEBUG */
1923 if (callout_pending(&tmr->timer)) {
1925 * we do NOT allow you to have it already running. if it is
1926 * we leave the current one up unchanged
1930 /* At this point we can proceed */
1931 if (t_type == SCTP_TIMER_TYPE_SEND) {
1932 stcb->asoc.num_send_timers_up++;
1935 tmr->ep = (void *)inp;
1936 tmr->tcb = (void *)stcb;
1937 tmr->net = (void *)net;
1938 tmr->self = (void *)tmr;
1940 callout_reset(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
1945 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1946 struct sctp_nets *net)
1948 struct sctp_timer *tmr;
1950 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
1956 SCTP_TCB_LOCK_ASSERT(stcb);
1959 case SCTP_TIMER_TYPE_ADDR_WQ:
1960 tmr = &sctppcbinfo.addr_wq_timer;
1962 case SCTP_TIMER_TYPE_EARLYFR:
1963 if ((stcb == NULL) || (net == NULL)) {
1966 tmr = &net->fr_timer;
1967 SCTP_STAT_INCR(sctps_earlyfrstop);
1969 case SCTP_TIMER_TYPE_ITERATOR:
1971 struct sctp_iterator *it;
1973 it = (struct sctp_iterator *)inp;
1977 case SCTP_TIMER_TYPE_SEND:
1978 if ((stcb == NULL) || (net == NULL)) {
1981 tmr = &net->rxt_timer;
1983 case SCTP_TIMER_TYPE_INIT:
1984 if ((stcb == NULL) || (net == NULL)) {
1987 tmr = &net->rxt_timer;
1989 case SCTP_TIMER_TYPE_RECV:
1993 tmr = &stcb->asoc.dack_timer;
1995 case SCTP_TIMER_TYPE_SHUTDOWN:
1996 if ((stcb == NULL) || (net == NULL)) {
1999 tmr = &net->rxt_timer;
2001 case SCTP_TIMER_TYPE_HEARTBEAT:
2005 tmr = &stcb->asoc.hb_timer;
2007 case SCTP_TIMER_TYPE_COOKIE:
2008 if ((stcb == NULL) || (net == NULL)) {
2011 tmr = &net->rxt_timer;
2013 case SCTP_TIMER_TYPE_NEWCOOKIE:
2014 /* nothing needed but the endpoint here */
2015 tmr = &inp->sctp_ep.signature_change;
2017 * We re-use the newcookie timer for the INP kill timer. We
2018 * must assure that we do not kill it by accident.
2021 case SCTP_TIMER_TYPE_ASOCKILL:
2023 * Stop the asoc kill timer.
2028 tmr = &stcb->asoc.strreset_timer;
2031 case SCTP_TIMER_TYPE_INPKILL:
2033 * The inp is setup to die. We re-use the signature_chage
2034 * timer since that has stopped and we are in the GONE
2037 tmr = &inp->sctp_ep.signature_change;
2039 case SCTP_TIMER_TYPE_PATHMTURAISE:
2040 if ((stcb == NULL) || (net == NULL)) {
2043 tmr = &net->pmtu_timer;
2045 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2046 if ((stcb == NULL) || (net == NULL)) {
2049 tmr = &net->rxt_timer;
2051 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2055 tmr = &stcb->asoc.shut_guard_timer;
2057 case SCTP_TIMER_TYPE_STRRESET:
2061 tmr = &stcb->asoc.strreset_timer;
2063 case SCTP_TIMER_TYPE_ASCONF:
2067 tmr = &stcb->asoc.asconf_timer;
2069 case SCTP_TIMER_TYPE_AUTOCLOSE:
2073 tmr = &stcb->asoc.autoclose_timer;
2077 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
2078 printf("sctp_timer_stop:Unknown timer type %d\n",
2081 #endif /* SCTP_DEBUG */
2087 if ((tmr->type != t_type) && tmr->type) {
2089 * Ok we have a timer that is under joint use. Cookie timer
2090 * per chance with the SEND timer. We therefore are NOT
2091 * running the timer that the caller wants stopped. So just
2096 if (t_type == SCTP_TIMER_TYPE_SEND) {
2097 stcb->asoc.num_send_timers_up--;
2098 if (stcb->asoc.num_send_timers_up < 0) {
2099 stcb->asoc.num_send_timers_up = 0;
2103 callout_stop(&tmr->timer);
2107 #ifdef SCTP_USE_ADLER32
2109 update_adler32(uint32_t adler, uint8_t * buf, int32_t len)
2111 uint32_t s1 = adler & 0xffff;
2112 uint32_t s2 = (adler >> 16) & 0xffff;
2115 for (n = 0; n < len; n++, buf++) {
2116 /* s1 = (s1 + buf[n]) % BASE */
2120 * now if we need to, we do a mod by subtracting. It seems a
2121 * bit faster since I really will only ever do one subtract
2122 * at the MOST, since buf[n] is a max of 255.
2124 if (s1 >= SCTP_ADLER32_BASE) {
2125 s1 -= SCTP_ADLER32_BASE;
2127 /* s2 = (s2 + s1) % BASE */
2131 * again, it is more efficent (it seems) to subtract since
2132 * the most s2 will ever be is (BASE-1 + BASE-1) in the
2133 * worse case. This would then be (2 * BASE) - 2, which will
2134 * still only do one subtract. On Intel this is much better
2135 * to do this way and avoid the divide. Have not -pg'd on
2138 if (s2 >= SCTP_ADLER32_BASE) {
2139 s2 -= SCTP_ADLER32_BASE;
2142 /* Return the adler32 of the bytes buf[0..len-1] */
2143 return ((s2 << 16) + s1);
2150 sctp_calculate_len(struct mbuf *m)
2163 #if defined(SCTP_WITH_NO_CSUM)
2166 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2169 * given a mbuf chain with a packetheader offset by 'offset'
2170 * pointing at a sctphdr (with csum set to 0) go through the chain
2171 * of m_next's and calculate the SCTP checksum. This is currently
2172 * Adler32 but will change to CRC32x soon. Also has a side bonus
2173 * calculate the total length of the mbuf chain. Note: if offset is
2174 * greater than the total mbuf length, checksum=1, pktlen=0 is
2175 * returned (ie. no real error code)
2179 *pktlen = sctp_calculate_len(m);
2183 #elif defined(SCTP_USE_INCHKSUM)
2185 #include <machine/in_cksum.h>
2188 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2191 * given a mbuf chain with a packetheader offset by 'offset'
2192 * pointing at a sctphdr (with csum set to 0) go through the chain
2193 * of m_next's and calculate the SCTP checksum. This is currently
2194 * Adler32 but will change to CRC32x soon. Also has a side bonus
2195 * calculate the total length of the mbuf chain. Note: if offset is
2196 * greater than the total mbuf length, checksum=1, pktlen=0 is
2197 * returned (ie. no real error code)
2201 uint32_t the_sum, retsum;
2208 the_sum = (uint32_t) (in_cksum_skip(m, tlen, offset));
2210 *pktlen = (tlen - offset);
2211 retsum = htons(the_sum);
2218 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2221 * given a mbuf chain with a packetheader offset by 'offset'
2222 * pointing at a sctphdr (with csum set to 0) go through the chain
2223 * of m_next's and calculate the SCTP checksum. This is currently
2224 * Adler32 but will change to CRC32x soon. Also has a side bonus
2225 * calculate the total length of the mbuf chain. Note: if offset is
2226 * greater than the total mbuf length, checksum=1, pktlen=0 is
2227 * returned (ie. no real error code)
2231 #ifdef SCTP_USE_ADLER32
2235 uint32_t base = 0xffffffff;
2237 #endif /* SCTP_USE_ADLER32 */
2241 /* find the correct mbuf and offset into mbuf */
2242 while ((at != NULL) && (offset > (uint32_t) at->m_len)) {
2243 offset -= at->m_len; /* update remaining offset left */
2246 while (at != NULL) {
2247 if ((at->m_len - offset) > 0) {
2248 #ifdef SCTP_USE_ADLER32
2249 base = update_adler32(base,
2250 (unsigned char *)(at->m_data + offset),
2251 (unsigned int)(at->m_len - offset));
2253 if ((at->m_len - offset) < 4) {
2254 /* Use old method if less than 4 bytes */
2255 base = old_update_crc32(base,
2256 (unsigned char *)(at->m_data + offset),
2257 (unsigned int)(at->m_len - offset));
2259 base = update_crc32(base,
2260 (unsigned char *)(at->m_data + offset),
2261 (unsigned int)(at->m_len - offset));
2263 #endif /* SCTP_USE_ADLER32 */
2264 tlen += at->m_len - offset;
2265 /* we only offset once into the first mbuf */
2268 if (offset < at->m_len)
2271 offset -= at->m_len;
2275 if (pktlen != NULL) {
2278 #ifdef SCTP_USE_ADLER32
2283 base = sctp_csum_finalize(base);
2292 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2293 struct sctp_association *asoc, u_long mtu)
2296 * Reset the P-MTU size on this association, this involves changing
2297 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2298 * allow the DF flag to be cleared.
2300 struct sctp_tmit_chunk *chk;
2301 unsigned int eff_mtu, ovh;
2303 asoc->smallest_mtu = mtu;
2304 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2305 ovh = SCTP_MIN_OVERHEAD;
2307 ovh = SCTP_MIN_V4_OVERHEAD;
2309 eff_mtu = mtu - ovh;
2310 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2312 if (chk->send_size > eff_mtu) {
2313 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2316 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2317 if (chk->send_size > eff_mtu) {
2318 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2325 * given an association and starting time of the current RTT period return
2326 * RTO in number of usecs net should point to the current network
2329 sctp_calculate_rto(struct sctp_tcb *stcb,
2330 struct sctp_association *asoc,
2331 struct sctp_nets *net,
2332 struct timeval *old)
2335 * given an association and the starting time of the current RTT
2336 * period (in value1/value2) return RTO in number of usecs.
2340 unsigned int new_rto = 0;
2341 int first_measure = 0;
2344 /************************/
2345 /* 1. calculate new RTT */
2346 /************************/
2347 /* get the current time */
2348 SCTP_GETTIME_TIMEVAL(&now);
2349 /* compute the RTT value */
2350 if ((u_long)now.tv_sec > (u_long)old->tv_sec) {
2351 calc_time = ((u_long)now.tv_sec - (u_long)old->tv_sec) * 1000;
2352 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2353 calc_time += (((u_long)now.tv_usec -
2354 (u_long)old->tv_usec) / 1000);
2355 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2356 /* Borrow 1,000ms from current calculation */
2358 /* Add in the slop over */
2359 calc_time += ((int)now.tv_usec / 1000);
2360 /* Add in the pre-second ms's */
2361 calc_time += (((int)1000000 - (int)old->tv_usec) / 1000);
2363 } else if ((u_long)now.tv_sec == (u_long)old->tv_sec) {
2364 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2365 calc_time = ((u_long)now.tv_usec -
2366 (u_long)old->tv_usec) / 1000;
2367 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2368 /* impossible .. garbage in nothing out */
2369 return (((net->lastsa >> 2) + net->lastsv) >> 1);
2371 /* impossible .. garbage in nothing out */
2372 return (((net->lastsa >> 2) + net->lastsv) >> 1);
2375 /* Clock wrapped? */
2376 return (((net->lastsa >> 2) + net->lastsv) >> 1);
2378 /***************************/
2379 /* 2. update RTTVAR & SRTT */
2380 /***************************/
2382 /* if (net->lastsv || net->lastsa) { */
2383 /* per Section 5.3.1 C3 in SCTP */
2384 /* net->lastsv = (int) *//* RTTVAR */
2386 * (((double)(1.0 - 0.25) * (double)net->lastsv) + (double)(0.25 *
2387 * (double)abs(net->lastsa - calc_time))); net->lastsa = (int)
2390 * (((double)(1.0 - 0.125) * (double)net->lastsa) + (double)(0.125 *
2391 * (double)calc_time)); } else {
2392 *//* the first RTT calculation, per C2 Section 5.3.1 */
2393 /* net->lastsa = calc_time; *//* SRTT */
2394 /* net->lastsv = calc_time / 2; *//* RTTVAR */
2396 /* if RTTVAR goes to 0 you set to clock grainularity */
2398 * if (net->lastsv == 0) { net->lastsv = SCTP_CLOCK_GRANULARITY; }
2399 * new_rto = net->lastsa + 4 * net->lastsv;
2402 o_calctime = calc_time;
2403 /* this is Van Jacobson's integer version */
2405 calc_time -= (net->lastsa >> 3);
2406 if ((int)net->prev_rtt > o_calctime) {
2407 net->rtt_variance = net->prev_rtt - o_calctime;
2409 net->rto_variance_dir = 0;
2412 net->rtt_variance = o_calctime - net->prev_rtt;
2413 net->rto_variance_dir = 1;
2415 #ifdef SCTP_RTTVAR_LOGGING
2416 rto_logging(net, SCTP_LOG_RTTVAR);
2418 net->prev_rtt = o_calctime;
2419 net->lastsa += calc_time;
2420 if (calc_time < 0) {
2421 calc_time = -calc_time;
2423 calc_time -= (net->lastsv >> 2);
2424 net->lastsv += calc_time;
2425 if (net->lastsv == 0) {
2426 net->lastsv = SCTP_CLOCK_GRANULARITY;
2429 /* First RTO measurment */
2430 net->lastsa = calc_time;
2431 net->lastsv = calc_time >> 1;
2433 net->rto_variance_dir = 1;
2434 net->prev_rtt = o_calctime;
2435 net->rtt_variance = 0;
2436 #ifdef SCTP_RTTVAR_LOGGING
2437 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2440 new_rto = ((net->lastsa >> 2) + net->lastsv) >> 1;
2441 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2442 (stcb->asoc.sat_network_lockout == 0)) {
2443 stcb->asoc.sat_network = 1;
2444 } else if ((!first_measure) && stcb->asoc.sat_network) {
2445 stcb->asoc.sat_network = 0;
2446 stcb->asoc.sat_network_lockout = 1;
2448 /* bound it, per C6/C7 in Section 5.3.1 */
2449 if (new_rto < stcb->asoc.minrto) {
2450 new_rto = stcb->asoc.minrto;
2452 if (new_rto > stcb->asoc.maxrto) {
2453 new_rto = stcb->asoc.maxrto;
2455 /* we are now returning the RTT Smoothed */
2456 return ((uint32_t) new_rto);
2461 * return a pointer to a contiguous piece of data from the given mbuf chain
2462 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2463 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2464 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2467 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2473 if ((off < 0) || (len <= 0))
2476 /* find the desired start location */
2477 while ((m != NULL) && (off > 0)) {
2486 /* is the current mbuf large enough (eg. contiguous)? */
2487 if ((m->m_len - off) >= len) {
2488 return (mtod(m, caddr_t)+off);
2490 /* else, it spans more than one mbuf, so save a temp copy... */
2491 while ((m != NULL) && (len > 0)) {
2492 count = min(m->m_len - off, len);
2493 bcopy(mtod(m, caddr_t)+off, ptr, count);
2499 if ((m == NULL) && (len > 0))
2502 return ((caddr_t)in_ptr);
2507 struct sctp_paramhdr *
2508 sctp_get_next_param(struct mbuf *m,
2510 struct sctp_paramhdr *pull,
2513 /* This just provides a typed signature to Peter's Pull routine */
2514 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2520 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2523 * add padlen bytes of 0 filled padding to the end of the mbuf. If
2524 * padlen is > 3 this routine will fail.
2532 if (M_TRAILINGSPACE(m)) {
2534 * The easy way. We hope the majority of the time we hit
2537 dp = (uint8_t *) (mtod(m, caddr_t)+m->m_len);
2540 /* Hard way we must grow the mbuf */
2543 tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA);
2545 /* Out of space GAK! we are in big trouble. */
2548 /* setup and insert in middle */
2549 tmp->m_next = m->m_next;
2550 tmp->m_len = padlen;
2552 dp = mtod(tmp, uint8_t *);
2554 /* zero out the pad */
2555 for (i = 0; i < padlen; i++) {
2563 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2565 /* find the last mbuf in chain and pad it */
2570 return (sctp_add_pad_tombuf(last_mbuf, padval));
2573 if (m_at->m_next == NULL) {
2574 return (sctp_add_pad_tombuf(m_at, padval));
2576 m_at = m_at->m_next;
2582 int sctp_asoc_change_wake = 0;
2585 sctp_notify_assoc_change(uint32_t event, struct sctp_tcb *stcb,
2586 uint32_t error, void *data)
2588 struct mbuf *m_notify;
2589 struct sctp_assoc_change *sac;
2590 struct sctp_queued_to_read *control;
2593 * First if we are are going down dump everything we can to the
2597 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
2598 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
2599 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
2601 /* If the socket is gone we are out of here */
2605 * For TCP model AND UDP connected sockets we will send an error up
2606 * when an ABORT comes in.
2608 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2609 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2610 (event == SCTP_COMM_LOST)) {
2611 if (TAILQ_EMPTY(&stcb->sctp_ep->read_queue)) {
2612 stcb->sctp_socket->so_error = ECONNRESET;
2614 /* Wake ANY sleepers */
2615 sorwakeup(stcb->sctp_socket);
2616 sowwakeup(stcb->sctp_socket);
2617 sctp_asoc_change_wake++;
2619 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2620 /* event not enabled */
2623 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_change), 1, M_DONTWAIT, 1, MT_DATA);
2624 if (m_notify == NULL)
2627 m_notify->m_len = 0;
2629 sac = mtod(m_notify, struct sctp_assoc_change *);
2630 sac->sac_type = SCTP_ASSOC_CHANGE;
2632 sac->sac_length = sizeof(struct sctp_assoc_change);
2633 sac->sac_state = event;
2634 sac->sac_error = error;
2635 /* XXX verify these stream counts */
2636 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2637 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2638 sac->sac_assoc_id = sctp_get_associd(stcb);
2639 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
2640 m_notify->m_pkthdr.len = sizeof(struct sctp_assoc_change);
2641 m_notify->m_pkthdr.rcvif = 0;
2642 m_notify->m_len = sizeof(struct sctp_assoc_change);
2643 m_notify->m_next = NULL;
2644 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2647 if (control == NULL) {
2649 sctp_m_freem(m_notify);
2652 control->length = m_notify->m_len;
2653 /* not that we need this */
2654 control->tail_mbuf = m_notify;
2655 sctp_add_to_readq(stcb->sctp_ep, stcb,
2657 &stcb->sctp_socket->so_rcv, 1);
2658 if (event == SCTP_COMM_LOST) {
2659 /* Wake up any sleeper */
2660 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
2665 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2666 struct sockaddr *sa, uint32_t error)
2668 struct mbuf *m_notify;
2669 struct sctp_paddr_change *spc;
2670 struct sctp_queued_to_read *control;
2672 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVPADDREVNT))
2673 /* event not enabled */
2676 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 1, M_DONTWAIT, 1, MT_DATA);
2677 if (m_notify == NULL)
2679 m_notify->m_len = 0;
2680 spc = mtod(m_notify, struct sctp_paddr_change *);
2681 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2683 spc->spc_length = sizeof(struct sctp_paddr_change);
2684 if (sa->sa_family == AF_INET) {
2685 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2687 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2689 spc->spc_state = state;
2690 spc->spc_error = error;
2691 spc->spc_assoc_id = sctp_get_associd(stcb);
2693 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
2694 m_notify->m_pkthdr.len = sizeof(struct sctp_paddr_change);
2695 m_notify->m_pkthdr.rcvif = 0;
2696 m_notify->m_len = sizeof(struct sctp_paddr_change);
2697 m_notify->m_next = NULL;
2699 /* append to socket */
2700 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2703 if (control == NULL) {
2705 sctp_m_freem(m_notify);
2708 control->length = m_notify->m_len;
2709 /* not that we need this */
2710 control->tail_mbuf = m_notify;
2711 sctp_add_to_readq(stcb->sctp_ep, stcb,
2713 &stcb->sctp_socket->so_rcv, 1);
2718 sctp_notify_send_failed(struct sctp_tcb *stcb, uint32_t error,
2719 struct sctp_tmit_chunk *chk)
2721 struct mbuf *m_notify;
2722 struct sctp_send_failed *ssf;
2723 struct sctp_queued_to_read *control;
2726 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2727 /* event not enabled */
2730 length = sizeof(struct sctp_send_failed) + chk->send_size;
2731 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 1, M_DONTWAIT, 1, MT_DATA);
2732 if (m_notify == NULL)
2735 m_notify->m_len = 0;
2736 ssf = mtod(m_notify, struct sctp_send_failed *);
2737 ssf->ssf_type = SCTP_SEND_FAILED;
2738 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2739 ssf->ssf_flags = SCTP_DATA_UNSENT;
2741 ssf->ssf_flags = SCTP_DATA_SENT;
2742 ssf->ssf_length = length;
2743 ssf->ssf_error = error;
2744 /* not exactly what the user sent in, but should be close :) */
2745 ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2746 ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2747 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2748 ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2749 ssf->ssf_info.sinfo_context = chk->rec.data.context;
2750 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2751 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2752 m_notify->m_next = chk->data;
2753 m_notify->m_flags |= M_NOTIFICATION;
2754 m_notify->m_pkthdr.len = length;
2755 m_notify->m_pkthdr.rcvif = 0;
2756 m_notify->m_len = sizeof(struct sctp_send_failed);
2758 /* Steal off the mbuf */
2761 * For this case, we check the actual socket buffer, since the assoc
2762 * is going away we don't want to overfill the socket buffer for a
2765 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < m_notify->m_len) {
2766 sctp_m_freem(m_notify);
2769 /* append to socket */
2770 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2773 if (control == NULL) {
2775 sctp_m_freem(m_notify);
2778 sctp_add_to_readq(stcb->sctp_ep, stcb,
2780 &stcb->sctp_socket->so_rcv, 1);
2785 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
2786 struct sctp_stream_queue_pending *sp)
2788 struct mbuf *m_notify;
2789 struct sctp_send_failed *ssf;
2790 struct sctp_queued_to_read *control;
2793 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2794 /* event not enabled */
2797 length = sizeof(struct sctp_send_failed) + sp->length;
2798 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 1, M_DONTWAIT, 1, MT_DATA);
2799 if (m_notify == NULL)
2802 m_notify->m_len = 0;
2803 ssf = mtod(m_notify, struct sctp_send_failed *);
2804 ssf->ssf_type = SCTP_SEND_FAILED;
2805 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2806 ssf->ssf_flags = SCTP_DATA_UNSENT;
2808 ssf->ssf_flags = SCTP_DATA_SENT;
2809 ssf->ssf_length = length;
2810 ssf->ssf_error = error;
2811 /* not exactly what the user sent in, but should be close :) */
2812 ssf->ssf_info.sinfo_stream = sp->stream;
2813 ssf->ssf_info.sinfo_ssn = sp->strseq;
2814 ssf->ssf_info.sinfo_flags = sp->sinfo_flags;
2815 ssf->ssf_info.sinfo_ppid = sp->ppid;
2816 ssf->ssf_info.sinfo_context = sp->context;
2817 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2818 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2819 m_notify->m_next = sp->data;
2820 m_notify->m_flags |= M_NOTIFICATION;
2821 m_notify->m_pkthdr.len = length;
2822 m_notify->m_pkthdr.rcvif = 0;
2823 m_notify->m_len = sizeof(struct sctp_send_failed);
2825 /* Steal off the mbuf */
2828 * For this case, we check the actual socket buffer, since the assoc
2829 * is going away we don't want to overfill the socket buffer for a
2832 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < m_notify->m_len) {
2833 sctp_m_freem(m_notify);
2836 /* append to socket */
2837 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2840 if (control == NULL) {
2842 sctp_m_freem(m_notify);
2845 sctp_add_to_readq(stcb->sctp_ep, stcb,
2847 &stcb->sctp_socket->so_rcv, 1);
2853 sctp_notify_adaptation_layer(struct sctp_tcb *stcb,
2856 struct mbuf *m_notify;
2857 struct sctp_adaptation_event *sai;
2858 struct sctp_queued_to_read *control;
2860 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2861 /* event not enabled */
2864 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 1, M_DONTWAIT, 1, MT_DATA);
2865 if (m_notify == NULL)
2868 m_notify->m_len = 0;
2869 sai = mtod(m_notify, struct sctp_adaptation_event *);
2870 sai->sai_type = SCTP_ADAPTATION_INDICATION;
2872 sai->sai_length = sizeof(struct sctp_adaptation_event);
2873 sai->sai_adaptation_ind = error;
2874 sai->sai_assoc_id = sctp_get_associd(stcb);
2876 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
2877 m_notify->m_pkthdr.len = sizeof(struct sctp_adaptation_event);
2878 m_notify->m_pkthdr.rcvif = 0;
2879 m_notify->m_len = sizeof(struct sctp_adaptation_event);
2880 m_notify->m_next = NULL;
2882 /* append to socket */
2883 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2886 if (control == NULL) {
2888 sctp_m_freem(m_notify);
2891 control->length = m_notify->m_len;
2892 /* not that we need this */
2893 control->tail_mbuf = m_notify;
2894 sctp_add_to_readq(stcb->sctp_ep, stcb,
2896 &stcb->sctp_socket->so_rcv, 1);
2899 /* This always must be called with the read-queue LOCKED in the INP */
2901 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb,
2902 uint32_t error, int nolock)
2904 struct mbuf *m_notify;
2905 struct sctp_pdapi_event *pdapi;
2906 struct sctp_queued_to_read *control;
2909 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_PDAPIEVNT))
2910 /* event not enabled */
2913 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 1, M_DONTWAIT, 1, MT_DATA);
2914 if (m_notify == NULL)
2917 m_notify->m_len = 0;
2918 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
2919 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
2920 pdapi->pdapi_flags = 0;
2921 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
2922 pdapi->pdapi_indication = error;
2923 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
2925 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
2926 m_notify->m_pkthdr.len = sizeof(struct sctp_pdapi_event);
2927 m_notify->m_pkthdr.rcvif = 0;
2928 m_notify->m_len = sizeof(struct sctp_pdapi_event);
2929 m_notify->m_next = NULL;
2930 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2933 if (control == NULL) {
2935 sctp_m_freem(m_notify);
2938 control->length = m_notify->m_len;
2939 /* not that we need this */
2940 control->tail_mbuf = m_notify;
2941 control->held_length = 0;
2942 control->length = 0;
2944 SCTP_INP_READ_LOCK(stcb->sctp_ep);
2946 sb = &stcb->sctp_socket->so_rcv;
2947 #ifdef SCTP_SB_LOGGING
2948 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, m_notify->m_len);
2950 sctp_sballoc(stcb, sb, m_notify);
2951 #ifdef SCTP_SB_LOGGING
2952 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
2954 atomic_add_int(&control->length, m_notify->m_len);
2955 control->end_added = 1;
2956 if (stcb->asoc.control_pdapi)
2957 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
2959 /* we really should not see this case */
2960 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
2963 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
2965 if (stcb->sctp_ep && stcb->sctp_socket) {
2966 /* This should always be the case */
2967 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
2972 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
2974 struct mbuf *m_notify;
2975 struct sctp_shutdown_event *sse;
2976 struct sctp_queued_to_read *control;
2979 * For TCP model AND UDP connected sockets we will send an error up
2980 * when an SHUTDOWN completes
2982 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2983 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2984 /* mark socket closed for read/write and wakeup! */
2985 socantsendmore(stcb->sctp_socket);
2987 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2988 /* event not enabled */
2991 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 1, M_DONTWAIT, 1, MT_DATA);
2992 if (m_notify == NULL)
2995 m_notify->m_len = 0;
2996 sse = mtod(m_notify, struct sctp_shutdown_event *);
2997 sse->sse_type = SCTP_SHUTDOWN_EVENT;
2999 sse->sse_length = sizeof(struct sctp_shutdown_event);
3000 sse->sse_assoc_id = sctp_get_associd(stcb);
3002 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
3003 m_notify->m_pkthdr.len = sizeof(struct sctp_shutdown_event);
3004 m_notify->m_pkthdr.rcvif = 0;
3005 m_notify->m_len = sizeof(struct sctp_shutdown_event);
3006 m_notify->m_next = NULL;
3008 /* append to socket */
3009 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3012 if (control == NULL) {
3014 sctp_m_freem(m_notify);
3017 control->length = m_notify->m_len;
3018 /* not that we need this */
3019 control->tail_mbuf = m_notify;
3020 sctp_add_to_readq(stcb->sctp_ep, stcb,
3022 &stcb->sctp_socket->so_rcv, 1);
3026 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3027 int number_entries, uint16_t * list, int flag)
3029 struct mbuf *m_notify;
3030 struct sctp_queued_to_read *control;
3031 struct sctp_stream_reset_event *strreset;
3034 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
3035 /* event not enabled */
3038 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
3039 if (m_notify == NULL)
3042 m_notify->m_len = 0;
3043 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3044 if (len > M_TRAILINGSPACE(m_notify)) {
3045 /* never enough room */
3046 sctp_m_freem(m_notify);
3049 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3050 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3051 if (number_entries == 0) {
3052 strreset->strreset_flags = flag | SCTP_STRRESET_ALL_STREAMS;
3054 strreset->strreset_flags = flag | SCTP_STRRESET_STREAM_LIST;
3056 strreset->strreset_length = len;
3057 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3058 if (number_entries) {
3061 for (i = 0; i < number_entries; i++) {
3062 strreset->strreset_list[i] = ntohs(list[i]);
3065 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
3066 m_notify->m_pkthdr.len = len;
3067 m_notify->m_pkthdr.rcvif = 0;
3068 m_notify->m_len = len;
3069 m_notify->m_next = NULL;
3070 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < m_notify->m_len) {
3072 sctp_m_freem(m_notify);
3075 /* append to socket */
3076 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3079 if (control == NULL) {
3081 sctp_m_freem(m_notify);
3084 control->length = m_notify->m_len;
3085 /* not that we need this */
3086 control->tail_mbuf = m_notify;
3087 sctp_add_to_readq(stcb->sctp_ep, stcb,
3089 &stcb->sctp_socket->so_rcv, 1);
3094 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3095 uint32_t error, void *data)
3101 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3102 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3103 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
3105 /* No notifications up when we are in a no socket state */
3108 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3109 /* Can't send up to a closed socket any notifications */
3112 if (stcb && (stcb->asoc.assoc_up_sent == 0) && (notification != SCTP_NOTIFY_ASSOC_UP)) {
3113 if ((notification != SCTP_NOTIFY_ASSOC_DOWN) &&
3114 (notification != SCTP_NOTIFY_ASSOC_ABORTED) &&
3115 (notification != SCTP_NOTIFY_SPECIAL_SP_FAIL) &&
3116 (notification != SCTP_NOTIFY_DG_FAIL) &&
3117 (notification != SCTP_NOTIFY_PEER_SHUTDOWN)) {
3118 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, 0, NULL);
3119 stcb->asoc.assoc_up_sent = 1;
3122 switch (notification) {
3123 case SCTP_NOTIFY_ASSOC_UP:
3124 if (stcb->asoc.assoc_up_sent == 0) {
3125 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL);
3126 stcb->asoc.assoc_up_sent = 1;
3129 case SCTP_NOTIFY_ASSOC_DOWN:
3130 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL);
3132 case SCTP_NOTIFY_INTERFACE_DOWN:
3134 struct sctp_nets *net;
3136 net = (struct sctp_nets *)data;
3137 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3138 (struct sockaddr *)&net->ro._l_addr, error);
3141 case SCTP_NOTIFY_INTERFACE_UP:
3143 struct sctp_nets *net;
3145 net = (struct sctp_nets *)data;
3146 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3147 (struct sockaddr *)&net->ro._l_addr, error);
3150 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3152 struct sctp_nets *net;
3154 net = (struct sctp_nets *)data;
3155 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3156 (struct sockaddr *)&net->ro._l_addr, error);
3159 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3160 sctp_notify_send_failed2(stcb, error,
3161 (struct sctp_stream_queue_pending *)data);
3163 case SCTP_NOTIFY_DG_FAIL:
3164 sctp_notify_send_failed(stcb, error,
3165 (struct sctp_tmit_chunk *)data);
3167 case SCTP_NOTIFY_ADAPTATION_INDICATION:
3168 /* Here the error is the adaptation indication */
3169 sctp_notify_adaptation_layer(stcb, error);
3171 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3172 sctp_notify_partial_delivery_indication(stcb, error, 0);
3174 case SCTP_NOTIFY_STRDATA_ERR:
3176 case SCTP_NOTIFY_ASSOC_ABORTED:
3177 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, NULL);
3179 case SCTP_NOTIFY_PEER_OPENED_STREAM:
3181 case SCTP_NOTIFY_STREAM_OPENED_OK:
3183 case SCTP_NOTIFY_ASSOC_RESTART:
3184 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, data);
3186 case SCTP_NOTIFY_HB_RESP:
3188 case SCTP_NOTIFY_STR_RESET_SEND:
3189 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_OUTBOUND_STR);
3191 case SCTP_NOTIFY_STR_RESET_RECV:
3192 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_INBOUND_STR);
3194 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3195 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_OUTBOUND_STR | SCTP_STRRESET_INBOUND_STR));
3198 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3199 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_INBOUND_STR | SCTP_STRRESET_INBOUND_STR));
3202 case SCTP_NOTIFY_ASCONF_ADD_IP:
3203 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3206 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3207 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3210 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3211 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3214 case SCTP_NOTIFY_ASCONF_SUCCESS:
3216 case SCTP_NOTIFY_ASCONF_FAILED:
3218 case SCTP_NOTIFY_PEER_SHUTDOWN:
3219 sctp_notify_shutdown_event(stcb);
3221 case SCTP_NOTIFY_AUTH_NEW_KEY:
3222 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, error,
3223 (uint16_t) (uintptr_t) data);
3226 case SCTP_NOTIFY_AUTH_KEY_CONFLICT:
3227 sctp_notify_authentication(stcb, SCTP_AUTH_KEY_CONFLICT,
3228 error, (uint16_t) (uintptr_t) data);
3230 #endif /* not yet? remove? */
3235 if (sctp_debug_on & SCTP_DEBUG_UTIL1) {
3236 printf("NOTIFY: unknown notification %xh (%u)\n",
3237 notification, notification);
3239 #endif /* SCTP_DEBUG */
3245 sctp_report_all_outbound(struct sctp_tcb *stcb, int holds_lock)
3247 struct sctp_association *asoc;
3248 struct sctp_stream_out *outs;
3249 struct sctp_tmit_chunk *chk;
3250 struct sctp_stream_queue_pending *sp;
3255 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3256 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3257 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3260 /* now through all the gunk freeing chunks */
3261 if (holds_lock == 0)
3262 SCTP_TCB_SEND_LOCK(stcb);
3263 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3264 /* For each stream */
3265 outs = &stcb->asoc.strmout[i];
3266 /* clean up any sends there */
3267 stcb->asoc.locked_on_sending = NULL;
3268 sp = TAILQ_FIRST(&outs->outqueue);
3270 stcb->asoc.stream_queue_cnt--;
3271 TAILQ_REMOVE(&outs->outqueue, sp, next);
3272 sctp_free_spbufspace(stcb, asoc, sp);
3273 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3274 SCTP_NOTIFY_DATAGRAM_UNSENT, (void *)sp);
3276 sctp_m_freem(sp->data);
3280 sctp_free_remote_addr(sp->net);
3282 /* Free the chunk */
3283 sctp_free_a_strmoq(stcb, sp);
3284 sp = TAILQ_FIRST(&outs->outqueue);
3288 /* pending send queue SHOULD be empty */
3289 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3290 chk = TAILQ_FIRST(&asoc->send_queue);
3292 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3295 * trim off the sctp chunk header(it should
3298 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3299 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3300 sctp_mbuf_crush(chk->data);
3303 sctp_free_bufspace(stcb, asoc, chk, 1);
3304 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, chk);
3306 sctp_m_freem(chk->data);
3310 sctp_free_remote_addr(chk->whoTo);
3312 sctp_free_a_chunk(stcb, chk);
3313 chk = TAILQ_FIRST(&asoc->send_queue);
3316 /* sent queue SHOULD be empty */
3317 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3318 chk = TAILQ_FIRST(&asoc->sent_queue);
3320 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3323 * trim off the sctp chunk header(it should
3326 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3327 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3328 sctp_mbuf_crush(chk->data);
3331 sctp_free_bufspace(stcb, asoc, chk, 1);
3332 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3333 SCTP_NOTIFY_DATAGRAM_SENT, chk);
3335 sctp_m_freem(chk->data);
3339 sctp_free_remote_addr(chk->whoTo);
3341 sctp_free_a_chunk(stcb, chk);
3342 chk = TAILQ_FIRST(&asoc->sent_queue);
3345 if (holds_lock == 0)
3346 SCTP_TCB_SEND_UNLOCK(stcb);
3350 sctp_abort_notification(struct sctp_tcb *stcb, int error)
3353 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3354 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3355 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3358 /* Tell them we lost the asoc */
3359 sctp_report_all_outbound(stcb, 1);
3360 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3361 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3362 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3363 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3365 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_ABORTED, stcb, error, NULL);
3369 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3370 struct mbuf *m, int iphlen, struct sctphdr *sh, struct mbuf *op_err)
3376 /* We have a TCB to abort, send notification too */
3377 vtag = stcb->asoc.peer_vtag;
3378 sctp_abort_notification(stcb, 0);
3380 sctp_send_abort(m, iphlen, sh, vtag, op_err);
3382 /* Ok, now lets free it */
3383 sctp_free_assoc(inp, stcb, 0);
3385 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3386 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3387 sctp_inpcb_free(inp, 1, 0);
3394 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3395 int error, struct mbuf *op_err)
3400 /* Got to have a TCB */
3401 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3402 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3403 sctp_inpcb_free(inp, 1, 0);
3408 vtag = stcb->asoc.peer_vtag;
3409 /* notify the ulp */
3410 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)
3411 sctp_abort_notification(stcb, error);
3412 /* notify the peer */
3413 sctp_send_abort_tcb(stcb, op_err);
3414 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3415 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3416 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3417 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3419 /* now free the asoc */
3420 sctp_free_assoc(inp, stcb, 0);
3424 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
3425 struct sctp_inpcb *inp, struct mbuf *op_err)
3427 struct sctp_chunkhdr *ch, chunk_buf;
3428 unsigned int chk_length;
3430 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
3431 /* Generate a TO address for future reference */
3432 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3433 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3434 sctp_inpcb_free(inp, 1, 0);
3437 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3438 sizeof(*ch), (uint8_t *) & chunk_buf);
3439 while (ch != NULL) {
3440 chk_length = ntohs(ch->chunk_length);
3441 if (chk_length < sizeof(*ch)) {
3442 /* break to abort land */
3445 switch (ch->chunk_type) {
3446 case SCTP_PACKET_DROPPED:
3447 /* we don't respond to pkt-dropped */
3449 case SCTP_ABORT_ASSOCIATION:
3450 /* we don't respond with an ABORT to an ABORT */
3452 case SCTP_SHUTDOWN_COMPLETE:
3454 * we ignore it since we are not waiting for it and
3458 case SCTP_SHUTDOWN_ACK:
3459 sctp_send_shutdown_complete2(m, iphlen, sh);
3464 offset += SCTP_SIZE32(chk_length);
3465 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3466 sizeof(*ch), (uint8_t *) & chunk_buf);
3468 sctp_send_abort(m, iphlen, sh, 0, op_err);
3472 * check the inbound datagram to make sure there is not an abort inside it,
3473 * if there is return 1, else return 0.
3476 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
3478 struct sctp_chunkhdr *ch;
3479 struct sctp_init_chunk *init_chk, chunk_buf;
3481 unsigned int chk_length;
3483 offset = iphlen + sizeof(struct sctphdr);
3484 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
3485 (uint8_t *) & chunk_buf);
3486 while (ch != NULL) {
3487 chk_length = ntohs(ch->chunk_length);
3488 if (chk_length < sizeof(*ch)) {
3489 /* packet is probably corrupt */
3492 /* we seem to be ok, is it an abort? */
3493 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
3494 /* yep, tell them */
3497 if (ch->chunk_type == SCTP_INITIATION) {
3498 /* need to update the Vtag */
3499 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
3500 offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
3501 if (init_chk != NULL) {
3502 *vtagfill = ntohl(init_chk->init.initiate_tag);
3505 /* Nope, move to the next chunk */
3506 offset += SCTP_SIZE32(chk_length);
3507 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3508 sizeof(*ch), (uint8_t *) & chunk_buf);
3514 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
3515 * set (i.e. it's 0) so, create this function to compare link local scopes
3518 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
3520 struct sockaddr_in6 a, b;
3526 if (a.sin6_scope_id == 0)
3527 if (sa6_recoverscope(&a)) {
3528 /* can't get scope, so can't match */
3531 if (b.sin6_scope_id == 0)
3532 if (sa6_recoverscope(&b)) {
3533 /* can't get scope, so can't match */
3536 if (a.sin6_scope_id != b.sin6_scope_id)
3543 * returns a sockaddr_in6 with embedded scope recovered and removed
3545 struct sockaddr_in6 *
3546 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
3549 /* check and strip embedded scope junk */
3550 if (addr->sin6_family == AF_INET6) {
3551 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
3552 if (addr->sin6_scope_id == 0) {
3554 if (!sa6_recoverscope(store)) {
3555 /* use the recovered scope */
3558 /* else, return the original "to" addr */
3566 * are the two addresses the same? currently a "scopeless" check returns: 1
3570 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
3574 if (sa1 == NULL || sa2 == NULL)
3577 /* must be the same family */
3578 if (sa1->sa_family != sa2->sa_family)
3581 if (sa1->sa_family == AF_INET6) {
3582 /* IPv6 addresses */
3583 struct sockaddr_in6 *sin6_1, *sin6_2;
3585 sin6_1 = (struct sockaddr_in6 *)sa1;
3586 sin6_2 = (struct sockaddr_in6 *)sa2;
3587 return (SCTP6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr,
3588 &sin6_2->sin6_addr));
3589 } else if (sa1->sa_family == AF_INET) {
3590 /* IPv4 addresses */
3591 struct sockaddr_in *sin_1, *sin_2;
3593 sin_1 = (struct sockaddr_in *)sa1;
3594 sin_2 = (struct sockaddr_in *)sa2;
3595 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
3597 /* we don't do these... */
3603 sctp_print_address(struct sockaddr *sa)
3606 if (sa->sa_family == AF_INET6) {
3607 struct sockaddr_in6 *sin6;
3608 char ip6buf[INET6_ADDRSTRLEN];
3610 sin6 = (struct sockaddr_in6 *)sa;
3611 printf("IPv6 address: %s:%d scope:%u\n",
3612 ip6_sprintf(ip6buf, &sin6->sin6_addr),
3613 ntohs(sin6->sin6_port),
3614 sin6->sin6_scope_id);
3615 } else if (sa->sa_family == AF_INET) {
3616 struct sockaddr_in *sin;
3619 sin = (struct sockaddr_in *)sa;
3620 p = (unsigned char *)&sin->sin_addr;
3621 printf("IPv4 address: %u.%u.%u.%u:%d\n",
3622 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
3629 sctp_print_address_pkt(struct ip *iph, struct sctphdr *sh)
3631 if (iph->ip_v == IPVERSION) {
3632 struct sockaddr_in lsa, fsa;
3634 bzero(&lsa, sizeof(lsa));
3635 lsa.sin_len = sizeof(lsa);
3636 lsa.sin_family = AF_INET;
3637 lsa.sin_addr = iph->ip_src;
3638 lsa.sin_port = sh->src_port;
3639 bzero(&fsa, sizeof(fsa));
3640 fsa.sin_len = sizeof(fsa);
3641 fsa.sin_family = AF_INET;
3642 fsa.sin_addr = iph->ip_dst;
3643 fsa.sin_port = sh->dest_port;
3645 sctp_print_address((struct sockaddr *)&lsa);
3647 sctp_print_address((struct sockaddr *)&fsa);
3648 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3649 struct ip6_hdr *ip6;
3650 struct sockaddr_in6 lsa6, fsa6;
3652 ip6 = (struct ip6_hdr *)iph;
3653 bzero(&lsa6, sizeof(lsa6));
3654 lsa6.sin6_len = sizeof(lsa6);
3655 lsa6.sin6_family = AF_INET6;
3656 lsa6.sin6_addr = ip6->ip6_src;
3657 lsa6.sin6_port = sh->src_port;
3658 bzero(&fsa6, sizeof(fsa6));
3659 fsa6.sin6_len = sizeof(fsa6);
3660 fsa6.sin6_family = AF_INET6;
3661 fsa6.sin6_addr = ip6->ip6_dst;
3662 fsa6.sin6_port = sh->dest_port;
3664 sctp_print_address((struct sockaddr *)&lsa6);
3666 sctp_print_address((struct sockaddr *)&fsa6);
3670 #if defined(HAVE_SCTP_SO_LASTRECORD)
3672 /* cloned from uipc_socket.c */
3674 #define SCTP_SBLINKRECORD(sb, m0) do { \
3675 if ((sb)->sb_lastrecord != NULL) \
3676 (sb)->sb_lastrecord->m_nextpkt = (m0); \
3678 (sb)->sb_mb = (m0); \
3679 (sb)->sb_lastrecord = (m0); \
3680 } while (/*CONSTCOND*/0)
3684 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
3685 struct sctp_inpcb *new_inp,
3686 struct sctp_tcb *stcb)
3689 * go through our old INP and pull off any control structures that
3690 * belong to stcb and move then to the new inp.
3692 struct socket *old_so, *new_so;
3693 struct sctp_queued_to_read *control, *nctl;
3694 struct sctp_readhead tmp_queue;
3698 old_so = old_inp->sctp_socket;
3699 new_so = new_inp->sctp_socket;
3700 TAILQ_INIT(&tmp_queue);
3702 SOCKBUF_LOCK(&(old_so->so_rcv));
3704 error = sblock(&old_so->so_rcv, 0);
3706 SOCKBUF_UNLOCK(&(old_so->so_rcv));
3709 * Gak, can't get sblock, we have a problem. data will be
3710 * left stranded.. and we don't dare look at it since the
3711 * other thread may be reading something. Oh well, its a
3712 * screwed up app that does a peeloff OR a accept while
3713 * reading from the main socket... actually its only the
3714 * peeloff() case, since I think read will fail on a
3715 * listening socket..
3719 /* lock the socket buffers */
3720 SCTP_INP_READ_LOCK(old_inp);
3721 control = TAILQ_FIRST(&old_inp->read_queue);
3722 /* Pull off all for out target stcb */
3724 nctl = TAILQ_NEXT(control, next);
3725 if (control->stcb == stcb) {
3726 /* remove it we want it */
3727 TAILQ_REMOVE(&old_inp->read_queue, control, next);
3728 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
3731 #ifdef SCTP_SB_LOGGING
3732 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, m->m_len);
3734 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
3735 #ifdef SCTP_SB_LOGGING
3736 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3743 SCTP_INP_READ_UNLOCK(old_inp);
3745 /* Remove the sb-lock on the old socket */
3746 SOCKBUF_LOCK(&(old_so->so_rcv));
3748 sbunlock(&old_so->so_rcv);
3749 SOCKBUF_UNLOCK(&(old_so->so_rcv));
3751 /* Now we move them over to the new socket buffer */
3752 control = TAILQ_FIRST(&tmp_queue);
3753 SCTP_INP_READ_LOCK(new_inp);
3755 nctl = TAILQ_NEXT(control, next);
3756 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
3759 #ifdef SCTP_SB_LOGGING
3760 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, m->m_len);
3762 sctp_sballoc(stcb, &new_so->so_rcv, m);
3763 #ifdef SCTP_SB_LOGGING
3764 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3770 SCTP_INP_READ_UNLOCK(new_inp);
3775 sctp_add_to_readq(struct sctp_inpcb *inp,
3776 struct sctp_tcb *stcb,
3777 struct sctp_queued_to_read *control,
3782 * Here we must place the control on the end of the socket read
3783 * queue AND increment sb_cc so that select will work properly on
3786 struct mbuf *m, *prev = NULL;
3791 panic("Gak, inp NULL on add_to_readq");
3795 SCTP_INP_READ_LOCK(inp);
3797 control->held_length = 0;
3798 control->length = 0;
3800 if (m->m_len == 0) {
3801 /* Skip mbufs with NO length */
3804 control->data = sctp_m_free(m);
3807 prev->m_next = sctp_m_free(m);
3811 control->tail_mbuf = prev;;
3816 #ifdef SCTP_SB_LOGGING
3817 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, m->m_len);
3819 sctp_sballoc(stcb, sb, m);
3820 #ifdef SCTP_SB_LOGGING
3821 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3823 atomic_add_int(&control->length, m->m_len);
3827 control->tail_mbuf = prev;
3829 prev->m_flags |= M_EOR;
3835 control->end_added = 1;
3837 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
3838 SCTP_INP_READ_UNLOCK(inp);
3839 if (inp && inp->sctp_socket) {
3840 sctp_sorwakeup(inp, inp->sctp_socket);
3846 sctp_append_to_readq(struct sctp_inpcb *inp,
3847 struct sctp_tcb *stcb,
3848 struct sctp_queued_to_read *control,
3855 * A partial delivery API event is underway. OR we are appending on
3856 * the reassembly queue.
3858 * If PDAPI this means we need to add m to the end of the data.
3859 * Increase the length in the control AND increment the sb_cc.
3860 * Otherwise sb is NULL and all we need to do is put it at the end
3861 * of the mbuf chain.
3864 struct mbuf *mm, *tail = NULL, *prev = NULL;
3867 SCTP_INP_READ_LOCK(inp);
3869 if (control == NULL) {
3872 SCTP_INP_READ_UNLOCK(inp);
3876 if ((control->tail_mbuf) &&
3877 (control->tail_mbuf->m_flags & M_EOR)) {
3878 /* huh this one is complete? */
3886 if (mm->m_len == 0) {
3887 /* Skip mbufs with NO lenght */
3890 m = sctp_m_free(mm);
3893 prev->m_next = sctp_m_free(mm);
3901 #ifdef SCTP_SB_LOGGING
3902 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, mm->m_len);
3904 sctp_sballoc(stcb, sb, mm);
3905 #ifdef SCTP_SB_LOGGING
3906 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3914 /* Really there should always be a prev */
3916 /* Huh nothing left? */
3918 panic("Nothing left to add?");
3926 /* message is complete */
3927 tail->m_flags |= M_EOR;
3928 if (control == stcb->asoc.control_pdapi) {
3929 stcb->asoc.control_pdapi = NULL;
3931 control->held_length = 0;
3932 control->end_added = 1;
3934 atomic_add_int(&control->length, len);
3935 if (control->tail_mbuf) {
3937 control->tail_mbuf->m_next = m;
3938 control->tail_mbuf = tail;
3942 if (control->data != NULL) {
3943 panic("This should NOT happen");
3947 control->tail_mbuf = tail;
3950 * When we are appending in partial delivery, the cum-ack is used
3951 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
3952 * is populated in the outbound sinfo structure from the true cumack
3953 * if the association exists...
3955 control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
3957 SCTP_INP_READ_UNLOCK(inp);
3959 if (inp && inp->sctp_socket) {
3960 sctp_sorwakeup(inp, inp->sctp_socket);
3967 /*************HOLD THIS COMMENT FOR PATCH FILE OF
3968 *************ALTERNATE ROUTING CODE
3971 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
3972 *************ALTERNATE ROUTING CODE
3976 sctp_generate_invmanparam(int err)
3978 /* Return a MBUF with a invalid mandatory parameter */
3981 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
3983 struct sctp_paramhdr *ph;
3985 m->m_len = sizeof(struct sctp_paramhdr);
3986 ph = mtod(m, struct sctp_paramhdr *);
3987 ph->param_length = htons(sizeof(struct sctp_paramhdr));
3988 ph->param_type = htons(err);
3993 #ifdef SCTP_MBCNT_LOGGING
3995 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
3996 struct sctp_tmit_chunk *tp1, int chk_cnt)
3998 if (tp1->data == NULL) {
4001 asoc->chunks_on_out_queue -= chk_cnt;
4002 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4003 asoc->total_output_queue_size,
4007 if (asoc->total_output_queue_size >= tp1->book_size) {
4008 asoc->total_output_queue_size -= tp1->book_size;
4010 asoc->total_output_queue_size = 0;
4013 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4014 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4015 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4016 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4018 stcb->sctp_socket->so_snd.sb_cc = 0;
4027 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4028 int reason, struct sctpchunk_listhead *queue)
4032 uint8_t foundeom = 0;
4035 ret_sz += tp1->book_size;
4036 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4038 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4039 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1);
4040 sctp_m_freem(tp1->data);
4042 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4044 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4045 stcb->asoc.sent_queue_cnt_removeable--;
4047 if (queue == &stcb->asoc.send_queue) {
4048 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4049 /* on to the sent queue */
4050 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4052 stcb->asoc.sent_queue_cnt++;
4054 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4055 SCTP_DATA_NOT_FRAG) {
4056 /* not frag'ed we ae done */
4059 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4060 /* end of frag, we are done */
4065 * Its a begin or middle piece, we must mark all of
4069 tp1 = TAILQ_NEXT(tp1, sctp_next);
4071 } while (tp1 && notdone);
4072 if ((foundeom == 0) && (queue == &stcb->asoc.sent_queue)) {
4074 * The multi-part message was scattered across the send and
4077 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
4079 * recurse throught the send_queue too, starting at the
4083 ret_sz += sctp_release_pr_sctp_chunk(stcb, tp1, reason,
4084 &stcb->asoc.send_queue);
4086 printf("hmm, nothing on the send queue and no EOM?\n");
4093 * checks to see if the given address, sa, is one that is currently known by
4094 * the kernel note: can't distinguish the same address on multiple interfaces
4095 * and doesn't handle multiple addresses with different zone/scope id's note:
4096 * ifa_ifwithaddr() compares the entire sockaddr struct
4099 sctp_find_ifa_by_addr(struct sockaddr *sa)
4104 /* go through all our known interfaces */
4105 TAILQ_FOREACH(ifn, &ifnet, if_list) {
4106 /* go through each interface addresses */
4107 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
4108 /* correct family? */
4109 if (ifa->ifa_addr->sa_family != sa->sa_family)
4113 if (ifa->ifa_addr->sa_family == AF_INET6) {
4115 struct sockaddr_in6 *sin1, *sin2, sin6_tmp;
4117 sin1 = (struct sockaddr_in6 *)ifa->ifa_addr;
4118 if (IN6_IS_SCOPE_LINKLOCAL(&sin1->sin6_addr)) {
4119 /* create a copy and clear scope */
4120 memcpy(&sin6_tmp, sin1,
4121 sizeof(struct sockaddr_in6));
4123 in6_clearscope(&sin1->sin6_addr);
4125 sin2 = (struct sockaddr_in6 *)sa;
4126 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4127 sizeof(struct in6_addr)) == 0) {
4133 if (ifa->ifa_addr->sa_family == AF_INET) {
4135 struct sockaddr_in *sin1, *sin2;
4137 sin1 = (struct sockaddr_in *)ifa->ifa_addr;
4138 sin2 = (struct sockaddr_in *)sa;
4139 if (sin1->sin_addr.s_addr ==
4140 sin2->sin_addr.s_addr) {
4145 /* else, not AF_INET or AF_INET6, so skip */
4146 } /* end foreach ifa */
4147 } /* end foreach ifn */
4160 sctp_user_rcvd(struct sctp_tcb *stcb, int *freed_so_far, int hold_rlock,
4163 /* User pulled some data, do we need a rwnd update? */
4166 struct socket *so = NULL;
4171 atomic_add_int(&stcb->asoc.refcnt, 1);
4173 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4174 /* Pre-check If we are freeing no update */
4177 SCTP_INP_INCR_REF(stcb->sctp_ep);
4178 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4179 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4182 so = stcb->sctp_socket;
4186 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
4187 /* Have you have freed enough to look */
4188 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4189 sctp_misc_ints(SCTP_ENTER_USER_RECV,
4190 (stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd),
4192 stcb->freed_by_sorcv_sincelast,
4196 /* Yep, its worth a look and the lock overhead */
4198 /* Figure out what the rwnd would be */
4199 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
4200 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
4201 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
4205 if (dif >= rwnd_req) {
4207 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
4210 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4212 * One last check before we allow the guy possibly
4213 * to get in. There is a race, where the guy has not
4214 * reached the gate. In that case
4218 SCTP_TCB_LOCK(stcb);
4219 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4220 /* No reports here */
4221 SCTP_TCB_UNLOCK(stcb);
4224 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4225 sctp_misc_ints(SCTP_USER_RECV_SACKS,
4227 stcb->asoc.my_last_reported_rwnd,
4228 stcb->freed_by_sorcv_sincelast,
4231 SCTP_STAT_INCR(sctps_wu_sacks_sent);
4232 sctp_send_sack(stcb);
4233 sctp_chunk_output(stcb->sctp_ep, stcb,
4234 SCTP_OUTPUT_FROM_USR_RCVD);
4235 /* make sure no timer is running */
4236 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL);
4237 SCTP_TCB_UNLOCK(stcb);
4239 /* Update how much we have pending */
4240 stcb->freed_by_sorcv_sincelast = dif;
4241 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4242 sctp_misc_ints(SCTP_USER_RECV_SACKS,
4244 stcb->asoc.my_last_reported_rwnd,
4245 stcb->freed_by_sorcv_sincelast,
4250 if (so && r_unlocked && hold_rlock) {
4251 SCTP_STAT_INCR(sctps_locks_in_rcv);
4252 SCTP_INP_READ_LOCK(stcb->sctp_ep);
4254 SCTP_INP_DECR_REF(stcb->sctp_ep);
4256 atomic_add_int(&stcb->asoc.refcnt, -1);
4261 sctp_sorecvmsg(struct socket *so,
4264 struct sockaddr *from,
4267 struct sctp_sndrcvinfo *sinfo,
4271 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
4272 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
4273 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
4274 * On the way out we may send out any combination of:
4275 * MSG_NOTIFICATION MSG_EOR
4278 struct sctp_inpcb *inp = NULL;
4280 int cp_len = 0, error = 0;
4281 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
4282 struct mbuf *m = NULL, *embuf = NULL;
4283 struct sctp_tcb *stcb = NULL;
4284 int wakeup_read_socket = 0;
4285 int freecnt_applied = 0;
4286 int out_flags = 0, in_flags = 0;
4287 int block_allowed = 1;
4288 int freed_so_far = 0;
4289 int copied_so_far = 0;
4290 int s, in_eeor_mode = 0;
4291 int no_rcv_needed = 0;
4292 uint32_t rwnd_req = 0;
4293 int hold_sblock = 0;
4295 int alen = 0, slen = 0;
4296 int held_length = 0;
4299 in_flags = *msg_flags;
4303 slen = uio->uio_resid;
4304 /* Pull in and set up our int flags */
4305 if (in_flags & MSG_OOB) {
4306 /* Out of band's NOT supported */
4307 return (EOPNOTSUPP);
4309 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
4312 if ((in_flags & (MSG_DONTWAIT
4315 (so->so_state & SS_NBIO)) {
4318 /* setup the endpoint */
4319 inp = (struct sctp_inpcb *)so->so_pcb;
4324 rwnd_req = (so->so_rcv.sb_hiwat >> SCTP_RWND_HIWAT_SHIFT);
4325 /* Must be at least a MTU's worth */
4326 if (rwnd_req < SCTP_MIN_RWND)
4327 rwnd_req = SCTP_MIN_RWND;
4328 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
4329 #ifdef SCTP_RECV_RWND_LOGGING
4330 sctp_misc_ints(SCTP_SORECV_ENTER,
4331 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
4333 SOCKBUF_LOCK(&so->so_rcv);
4335 #ifdef SCTP_RECV_RWND_LOGGING
4336 sctp_misc_ints(SCTP_SORECV_ENTERPL,
4337 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
4341 error = sblock(&so->so_rcv, (block_allowed ? M_WAITOK : 0));
4343 goto release_unlocked;
4346 if (hold_sblock == 0) {
4347 SOCKBUF_LOCK(&so->so_rcv);
4350 sbunlock(&so->so_rcv);
4353 if (hold_sblock == 0) {
4354 SOCKBUF_LOCK(&so->so_rcv);
4357 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4358 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4361 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE) {
4363 error = so->so_error;
4369 if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
4370 /* we need to wait for data */
4371 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4372 sctp_misc_ints(SCTP_SORECV_BLOCKSA,
4373 0, 0, so->so_rcv.sb_cc, uio->uio_resid);
4375 if ((so->so_rcv.sb_cc == 0) &&
4376 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4377 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
4378 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
4380 * For active open side clear flags for
4381 * re-use passive open is blocked by
4384 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
4386 * You were aborted, passive side
4391 * You get this once if you are
4394 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4396 * Remove flag if on the
4399 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
4402 so->so_state &= ~(SS_ISCONNECTING |
4403 SS_ISDISCONNECTING |
4407 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
4410 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
4416 error = sbwait(&so->so_rcv);
4421 goto restart_nosblocks;
4422 } else if (so->so_rcv.sb_cc == 0) {
4423 error = EWOULDBLOCK;
4426 error = sblock(&so->so_rcv, (block_allowed ? M_WAITOK : 0));
4427 /* we possibly have data we can read */
4428 control = TAILQ_FIRST(&inp->read_queue);
4429 if (control == NULL) {
4431 * This could be happening since the appender did the
4432 * increment but as not yet did the tailq insert onto the
4435 if (hold_rlock == 0) {
4436 SCTP_INP_READ_LOCK(inp);
4439 control = TAILQ_FIRST(&inp->read_queue);
4440 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
4442 panic("Huh, its non zero and nothing on control?");
4444 so->so_rcv.sb_cc = 0;
4446 SCTP_INP_READ_UNLOCK(inp);
4450 if ((control->length == 0) &&
4451 (control->do_not_ref_stcb)) {
4453 * Clean up code for freeing assoc that left behind a
4454 * pdapi.. maybe a peer in EEOR that just closed after
4455 * sending and never indicated a EOR.
4457 SCTP_STAT_INCR(sctps_locks_in_rcva);
4458 if (hold_rlock == 0) {
4460 SCTP_INP_READ_LOCK(inp);
4462 control->held_length = 0;
4463 if (control->data) {
4464 /* Hmm there is data here .. fix */
4471 if (m->m_next == NULL) {
4472 control->tail_mbuf = m;
4473 m->m_flags |= M_EOR;
4474 control->end_added = 1;
4478 control->length = cnt;
4481 TAILQ_REMOVE(&inp->read_queue, control, next);
4482 /* Add back any hiddend data */
4483 sctp_free_remote_addr(control->whoFrom);
4484 sctp_free_a_readq(stcb, control);
4488 SCTP_INP_READ_UNLOCK(inp);
4492 if (control->length == 0) {
4493 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
4495 /* find a more suitable one then this */
4496 ctl = TAILQ_NEXT(control, next);
4498 if ((ctl->stcb != control->stcb) && (ctl->length)) {
4503 ctl = TAILQ_NEXT(ctl, next);
4507 * if we reach here, not suitable replacement is available
4508 * <or> fragment interleave is NOT on. So stuff the sb_cc
4509 * into the our held count, and its time to sleep again.
4511 held_length = so->so_rcv.sb_cc;
4512 control->held_length = so->so_rcv.sb_cc;
4515 /* Clear the held length since there is something to read */
4516 control->held_length = 0;
4518 SCTP_INP_READ_UNLOCK(inp);
4523 * If we reach here, control has a some data for us to read off.
4524 * Note that stcb COULD be NULL.
4527 SOCKBUF_UNLOCK(&so->so_rcv);
4530 stcb = control->stcb;
4532 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4533 (control->do_not_ref_stcb == 0)) {
4534 if (freecnt_applied == 0)
4536 } else if (control->do_not_ref_stcb == 0) {
4537 /* you can't free it on me please */
4539 * The lock on the socket buffer protects us so the
4540 * free code will stop. But since we used the
4541 * socketbuf lock and the sender uses the tcb_lock
4542 * to increment, we need to use the atomic add to
4545 atomic_add_int(&stcb->asoc.refcnt, 1);
4546 freecnt_applied = 1;
4548 * Setup to remember how much we have not yet told
4549 * the peer our rwnd has opened up. Note we grab the
4550 * value from the tcb from last time. Note too that
4551 * sack sending clears this when a sack is sent..
4552 * which is fine. Once we hit the rwnd_req, we then
4553 * will go to the sctp_user_rcvd() that will not
4554 * lock until it KNOWs it MUST send a WUP-SACK.
4557 freed_so_far = stcb->freed_by_sorcv_sincelast;
4558 stcb->freed_by_sorcv_sincelast = 0;
4561 /* First lets get off the sinfo and sockaddr info */
4562 if ((sinfo) && filling_sinfo) {
4563 memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
4564 nxt = TAILQ_NEXT(control, next);
4565 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
4566 struct sctp_extrcvinfo *s_extra;
4568 s_extra = (struct sctp_extrcvinfo *)sinfo;
4570 s_extra->next_flags = SCTP_NEXT_MSG_AVAIL;
4571 if (nxt->sinfo_flags & SCTP_UNORDERED) {
4572 s_extra->next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
4574 s_extra->next_asocid = nxt->sinfo_assoc_id;
4575 s_extra->next_length = nxt->length;
4576 s_extra->next_ppid = nxt->sinfo_ppid;
4577 s_extra->next_stream = nxt->sinfo_stream;
4578 if (nxt->tail_mbuf != NULL) {
4579 if (nxt->tail_mbuf->m_flags & M_EOR) {
4580 s_extra->next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
4585 * we explicitly 0 this, since the memcpy
4586 * got some other things beyond the older
4587 * sinfo_ that is on the control's structure
4590 s_extra->next_flags = SCTP_NO_NEXT_MSG;
4591 s_extra->next_asocid = 0;
4592 s_extra->next_length = 0;
4593 s_extra->next_ppid = 0;
4594 s_extra->next_stream = 0;
4598 * update off the real current cum-ack, if we have an stcb.
4601 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4603 * mask off the high bits, we keep the actual chunk bits in
4606 sinfo->sinfo_flags &= 0x00ff;
4608 if (fromlen && from) {
4609 struct sockaddr *to;
4612 cp_len = min(fromlen, control->whoFrom->ro._l_addr.sin.sin_len);
4613 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
4614 ((struct sockaddr_in *)from)->sin_port = control->port_from;
4616 /* No AF_INET use AF_INET6 */
4617 cp_len = min(fromlen, control->whoFrom->ro._l_addr.sin6.sin6_len);
4618 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
4619 ((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
4623 #if defined(AF_INET) && defined(AF_INET6)
4624 if ((inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
4625 (to->sa_family == AF_INET) &&
4626 ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
4627 struct sockaddr_in *sin;
4628 struct sockaddr_in6 sin6;
4630 sin = (struct sockaddr_in *)to;
4631 bzero(&sin6, sizeof(sin6));
4632 sin6.sin6_family = AF_INET6;
4633 sin6.sin6_len = sizeof(struct sockaddr_in6);
4634 sin6.sin6_addr.s6_addr16[2] = 0xffff;
4635 bcopy(&sin->sin_addr,
4636 &sin6.sin6_addr.s6_addr16[3],
4637 sizeof(sin6.sin6_addr.s6_addr16[3]));
4638 sin6.sin6_port = sin->sin_port;
4639 memcpy(from, (caddr_t)&sin6, sizeof(sin6));
4642 #if defined(AF_INET6)
4644 struct sockaddr_in6 lsa6, *to6;
4646 to6 = (struct sockaddr_in6 *)to;
4647 sctp_recover_scope_mac(to6, (&lsa6));
4652 /* now copy out what data we can */
4654 /* copy out each mbuf in the chain up to length */
4658 /* Move out all we can */
4659 cp_len = (int)uio->uio_resid;
4660 my_len = (int)m->m_len;
4661 if (cp_len > my_len) {
4662 /* not enough in this buf */
4666 SCTP_INP_READ_UNLOCK(inp);
4671 error = uiomove(mtod(m, char *), cp_len, uio);
4673 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4674 sctp_misc_ints(SCTP_SORCV_DOESCPY,
4681 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4685 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4689 /* error we are out of here */
4692 if ((m->m_next == NULL) &&
4693 (cp_len >= m->m_len) &&
4694 ((control->end_added == 0) ||
4695 (control->end_added && (TAILQ_NEXT(control, next) == NULL)))
4697 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4698 sctp_misc_ints(SCTP_SORCV_DOESLCK,
4704 SCTP_STAT_INCR(sctps_locks_in_rcvb);
4705 SCTP_INP_READ_LOCK(inp);
4708 if (cp_len == m->m_len) {
4709 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4710 sctp_misc_ints(SCTP_SORCV_DOESADJ,
4716 if (m->m_flags & M_EOR) {
4717 out_flags |= MSG_EOR;
4719 if (m->m_flags & M_NOTIFICATION) {
4720 out_flags |= MSG_NOTIFICATION;
4722 /* we ate up the mbuf */
4723 if (in_flags & MSG_PEEK) {
4726 copied_so_far += cp_len;
4728 /* dispose of the mbuf */
4729 #ifdef SCTP_SB_LOGGING
4730 sctp_sblog(&so->so_rcv,
4731 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, m->m_len);
4733 sctp_sbfree(control, stcb, &so->so_rcv, m);
4734 #ifdef SCTP_SB_LOGGING
4735 sctp_sblog(&so->so_rcv,
4736 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4739 copied_so_far += cp_len;
4740 freed_so_far += cp_len;
4741 alen = atomic_fetchadd_int(&control->length, -(cp_len));
4742 if (alen < cp_len) {
4743 panic("Control length goes negative?");
4745 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4746 sctp_misc_ints(SCTP_SORCV_PASSBF,
4752 control->data = sctp_m_free(m);
4755 * been through it all, must hold sb
4756 * lock ok to null tail
4758 if (control->data == NULL) {
4760 if ((control->end_added == 0) ||
4761 (TAILQ_NEXT(control, next) == NULL)) {
4769 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
4770 panic("Hmm we don't own the lock?");
4774 control->tail_mbuf = NULL;
4776 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
4777 panic("end_added, nothing left and no MSG_EOR");
4781 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4782 sctp_misc_ints(SCTP_SORCV_ADJD,
4790 /* Do we need to trim the mbuf? */
4791 if (m->m_flags & M_NOTIFICATION) {
4792 out_flags |= MSG_NOTIFICATION;
4794 if ((in_flags & MSG_PEEK) == 0) {
4795 if (out_flags & MSG_NOTIFICATION) {
4797 * remark this one with the
4798 * notify flag, they read
4802 m->m_flags |= M_NOTIFICATION;
4804 m->m_data += cp_len;
4806 #ifdef SCTP_SB_LOGGING
4807 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
4809 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
4811 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
4813 copied_so_far += cp_len;
4815 freed_so_far += cp_len;
4816 #ifdef SCTP_SB_LOGGING
4817 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
4818 SCTP_LOG_SBRESULT, 0);
4820 alen = atomic_fetchadd_int(&control->length, -(cp_len));
4821 if (alen < cp_len) {
4822 panic("Control length goes negative2?");
4825 copied_so_far += cp_len;
4828 if ((out_flags & MSG_EOR) ||
4829 (uio->uio_resid == 0)
4833 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
4834 (control->do_not_ref_stcb == 0) &&
4835 (freed_so_far >= rwnd_req)) {
4836 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
4838 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4839 sctp_misc_ints(SCTP_SORCV_BOTWHILE,
4846 } /* end while(m) */
4848 * At this point we have looked at it all and we either have
4849 * a MSG_EOR/or read all the user wants... <OR>
4850 * control->length == 0.
4852 if ((out_flags & MSG_EOR) &&
4853 ((in_flags & MSG_PEEK) == 0)) {
4854 /* we are done with this control */
4855 if (control->length == 0) {
4856 if (control->data) {
4858 panic("control->data not null at read eor?");
4860 printf("Strange, data left in the control buffer .. invarients would panic?\n");
4861 sctp_m_freem(control->data);
4862 control->data = NULL;
4866 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4867 sctp_misc_ints(SCTP_SORCV_FREECTL,
4873 if (TAILQ_NEXT(control, next) == NULL) {
4875 * If we don't have a next we need a
4876 * lock, if there is a next interupt
4877 * is filling ahead of us and we
4878 * don't need a lock to remove this
4879 * guy (which is the head of the
4882 if (hold_rlock == 0) {
4883 SCTP_STAT_INCR(sctps_locks_in_rcvc);
4884 SCTP_INP_READ_LOCK(inp);
4888 TAILQ_REMOVE(&inp->read_queue, control, next);
4889 /* Add back any hiddend data */
4890 if (control->held_length) {
4892 control->held_length = 0;
4893 wakeup_read_socket = 1;
4895 no_rcv_needed = control->do_not_ref_stcb;
4896 sctp_free_remote_addr(control->whoFrom);
4897 control->data = NULL;
4898 sctp_free_a_readq(stcb, control);
4900 if ((freed_so_far >= rwnd_req) && (no_rcv_needed == 0))
4901 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
4905 * The user did not read all of this
4906 * message, turn off the returned MSG_EOR
4907 * since we are leaving more behind on the
4911 if (control->end_added && (control->data == NULL) &&
4912 (control->tail_mbuf == NULL)) {
4913 panic("Gak, control->length is corrupt?");
4916 no_rcv_needed = control->do_not_ref_stcb;
4917 out_flags &= ~MSG_EOR;
4920 if (out_flags & MSG_EOR) {
4923 if ((uio->uio_resid == 0) ||
4924 ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))
4929 * If I hit here the receiver wants more and this message is
4930 * NOT done (pd-api). So two questions. Can we block? if not
4931 * we are done. Did the user NOT set MSG_WAITALL?
4933 if (block_allowed == 0) {
4937 * We need to wait for more data a few things: - We don't
4938 * sbunlock() so we don't get someone else reading. - We
4939 * must be sure to account for the case where what is added
4940 * is NOT to our control when we wakeup.
4944 * Do we need to tell the transport a rwnd update might be
4945 * needed before we go to sleep?
4947 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
4948 ((freed_so_far >= rwnd_req) &&
4949 (control->do_not_ref_stcb == 0) &&
4950 (no_rcv_needed == 0))) {
4951 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
4954 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE) {
4957 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
4960 if (hold_rlock == 1) {
4961 SCTP_INP_READ_UNLOCK(inp);
4964 if (hold_sblock == 0) {
4965 SOCKBUF_LOCK(&so->so_rcv);
4968 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4970 sctp_misc_ints(SCTP_SORECV_BLOCKSB,
4976 sctp_misc_ints(SCTP_SORECV_BLOCKSB,
4982 if (so->so_rcv.sb_cc <= control->held_length) {
4983 error = sbwait(&so->so_rcv);
4987 control->held_length = 0;
4990 SOCKBUF_UNLOCK(&so->so_rcv);
4993 if (control->length == 0) {
4994 /* still nothing here */
4995 if (control->end_added == 1) {
4996 /* he aborted, or is done i.e.did a shutdown */
4997 out_flags |= MSG_EOR;
4998 if (control->pdapi_aborted)
4999 out_flags |= MSG_TRUNC;
5000 goto done_with_control;
5002 if (so->so_rcv.sb_cc > held_length) {
5003 SCTP_STAT_INCR(sctps_locks_in_rcvf);
5004 control->held_length = so->so_rcv.sb_cc;
5007 goto wait_some_more;
5008 } else if (control->data == NULL) {
5010 * we must re-sync since data is probably being
5013 SCTP_INP_READ_LOCK(inp);
5014 if ((control->length > 0) && (control->data == NULL)) {
5016 * big trouble.. we have the lock and its
5019 panic("Impossible data==NULL length !=0");
5021 SCTP_INP_READ_UNLOCK(inp);
5022 /* We will fall around to get more data */
5026 /* copy out the mbuf chain */
5029 * Do we have a uio, I doubt it if so we grab the size from
5030 * it, if not you get it all
5033 cp_len = uio->uio_resid;
5035 cp_len = control->length;
5037 if ((uint32_t) cp_len >= control->length) {
5039 if ((control->end_added == 0) ||
5040 (TAILQ_NEXT(control, next) == NULL)) {
5041 /* Need to get rlock */
5042 if (hold_rlock == 0) {
5043 SCTP_INP_READ_LOCK(inp);
5047 if (control->tail_mbuf->m_flags & M_EOR) {
5048 out_flags |= MSG_EOR;
5050 if (control->data->m_flags & M_NOTIFICATION) {
5051 out_flags |= MSG_NOTIFICATION;
5054 uio->uio_resid -= control->length;
5055 *mp = control->data;
5058 #ifdef SCTP_SB_LOGGING
5059 sctp_sblog(&so->so_rcv,
5060 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, m->m_len);
5062 sctp_sbfree(control, stcb, &so->so_rcv, m);
5063 freed_so_far += m->m_len;
5064 #ifdef SCTP_SB_LOGGING
5065 sctp_sblog(&so->so_rcv,
5066 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5070 control->data = control->tail_mbuf = NULL;
5071 control->length = 0;
5072 if (out_flags & MSG_EOR) {
5073 /* Done with this control */
5074 goto done_with_control;
5076 /* still more to do with this conntrol */
5077 /* do we really support msg_waitall here? */
5078 if ((block_allowed == 0) ||
5079 ((in_flags & MSG_WAITALL) == 0)) {
5083 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
5085 if (hold_rlock == 1) {
5086 SCTP_INP_READ_UNLOCK(inp);
5089 if (hold_sblock == 0) {
5090 SOCKBUF_LOCK(&so->so_rcv);
5093 if (so->so_rcv.sb_cc <= control->held_length) {
5094 error = sbwait(&so->so_rcv);
5100 SOCKBUF_UNLOCK(&so->so_rcv);
5103 if (control->length == 0) {
5104 /* still nothing here */
5105 if (control->end_added == 1) {
5107 * he aborted, or is done i.e.
5110 out_flags |= MSG_EOR;
5111 if (control->pdapi_aborted)
5112 out_flags |= MSG_TRUNC;
5113 goto done_with_control;
5115 if (so->so_rcv.sb_cc > held_length) {
5116 control->held_length = so->so_rcv.sb_cc;
5118 * We don't use held_length while
5123 goto wait_some_more2;
5125 goto get_more_data2;
5127 /* hard way mbuf by mbuf */
5129 if (control->end_added == 0) {
5130 /* need the rlock */
5131 if (hold_rlock == 0) {
5132 SCTP_INP_READ_LOCK(inp);
5136 if (m->m_flags & M_NOTIFICATION) {
5137 out_flags |= MSG_NOTIFICATION;
5139 while ((m) && (cp_len > 0)) {
5140 if (cp_len >= m->m_len) {
5142 atomic_subtract_int(&control->length, m->m_len);
5144 uio->uio_resid -= m->m_len;
5146 control->data = m->m_next;
5148 #ifdef SCTP_SB_LOGGING
5149 sctp_sblog(&so->so_rcv,
5150 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, m->m_len);
5152 sctp_sbfree(control, stcb, &so->so_rcv, m);
5153 freed_so_far += m->m_len;
5154 #ifdef SCTP_SB_LOGGING
5155 sctp_sblog(&so->so_rcv,
5156 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5162 * got all he wants and its part of
5166 uio->uio_resid -= m->m_len;
5169 SCTP_INP_READ_UNLOCK(inp);
5173 SOCKBUF_UNLOCK(&so->so_rcv);
5177 *mp = sctp_m_copym(m, 0, cp_len,
5181 #ifdef SCTP_LOCK_LOGGING
5182 sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCKBUF_R);
5184 if (hold_sblock == 0) {
5185 SOCKBUF_LOCK(&so->so_rcv);
5188 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5192 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5195 m->m_data += cp_len;
5197 #ifdef SCTP_SB_LOGGING
5198 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5200 freed_so_far += cp_len;
5201 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5203 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5204 if ((freed_so_far >= rwnd_req) &&
5205 (control->do_not_ref_stcb == 0) &&
5206 (no_rcv_needed == 0))
5207 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5209 #ifdef SCTP_SB_LOGGING
5210 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5211 SCTP_LOG_SBRESULT, 0);
5213 if (out_flags & MSG_NOTIFICATION) {
5215 * remark the first mbuf if
5216 * they took a partial read.
5218 control->data->m_flags |= M_NOTIFICATION;
5226 if (hold_rlock == 1) {
5227 SCTP_INP_READ_UNLOCK(inp);
5230 if (hold_sblock == 0) {
5231 SOCKBUF_LOCK(&so->so_rcv);
5234 sbunlock(&so->so_rcv);
5238 SOCKBUF_UNLOCK(&so->so_rcv);
5241 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
5242 if ((freed_so_far >= rwnd_req) &&
5243 (control && (control->do_not_ref_stcb == 0)) &&
5244 (no_rcv_needed == 0))
5245 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5248 *msg_flags |= out_flags;
5250 if (hold_rlock == 1) {
5251 SCTP_INP_READ_UNLOCK(inp);
5255 SOCKBUF_UNLOCK(&so->so_rcv);
5258 if (freecnt_applied) {
5260 * The lock on the socket buffer protects us so the free
5261 * code will stop. But since we used the socketbuf lock and
5262 * the sender uses the tcb_lock to increment, we need to use
5263 * the atomic add to the refcnt.
5266 panic("stcb for refcnt has gone NULL?");
5268 atomic_add_int(&stcb->asoc.refcnt, -1);
5269 freecnt_applied = 0;
5270 /* Save the value back for next time */
5271 stcb->freed_by_sorcv_sincelast = freed_so_far;
5274 #ifdef SCTP_RECV_RWND_LOGGING
5276 sctp_misc_ints(SCTP_SORECV_DONE,
5278 ((uio) ? (slen - uio->uio_resid) : slen),
5282 sctp_misc_ints(SCTP_SORECV_DONE,
5284 ((uio) ? (slen - uio->uio_resid) : slen),
5289 if (wakeup_read_socket) {
5290 sctp_sorwakeup(inp, so);
5296 #ifdef SCTP_MBUF_LOGGING
5298 sctp_m_free(struct mbuf *m)
5300 if (m->m_flags & M_EXT) {
5301 sctp_log_mb(m, SCTP_MBUF_IFREE);
5307 sctp_m_freem(struct mbuf *mb)
5310 mb = sctp_m_free(mb);
5317 sctp_soreceive(so, psa, uio, mp0, controlp, flagsp)
5319 struct sockaddr **psa;
5322 struct mbuf **controlp;
5326 uint8_t sockbuf[256];
5327 struct sockaddr *from;
5328 struct sctp_extrcvinfo sinfo;
5329 int filling_sinfo = 1;
5330 struct sctp_inpcb *inp;
5332 inp = (struct sctp_inpcb *)so->so_pcb;
5333 /* pickup the assoc we are reading from */
5337 if ((sctp_is_feature_off(inp,
5338 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
5339 (controlp == NULL)) {
5340 /* user does not want the sndrcv ctl */
5344 from = (struct sockaddr *)sockbuf;
5345 fromlen = sizeof(sockbuf);
5352 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
5353 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
5354 if ((controlp) && (filling_sinfo)) {
5355 /* copy back the sinfo in a CMSG format */
5357 *controlp = sctp_build_ctl_nchunk(inp,
5358 (struct sctp_sndrcvinfo *)&sinfo);
5363 /* copy back the address info */
5364 if (from && from->sa_len) {
5365 *psa = sodupsockaddr(from, M_NOWAIT);