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 = (uint32_t) 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 = (uint32_t) 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 = (uint32_t) 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 = (uint32_t) 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(uint32_t tsn, uint16_t sseq, 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.n_tsn = tsn;
223 sctp_clog[sctp_cwnd_log_at].x.strlog.n_sseq = sseq;
224 sctp_clog[sctp_cwnd_log_at].x.strlog.e_tsn = 0;
225 sctp_clog[sctp_cwnd_log_at].x.strlog.e_sseq = 0;
229 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
231 int sctp_cwnd_log_at;
233 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
234 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
235 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) action;
236 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_NAGLE;
237 sctp_clog[sctp_cwnd_log_at].x.nagle.stcb = (uint32_t) stcb;
238 sctp_clog[sctp_cwnd_log_at].x.nagle.total_flight = stcb->asoc.total_flight;
239 sctp_clog[sctp_cwnd_log_at].x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
240 sctp_clog[sctp_cwnd_log_at].x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
241 sctp_clog[sctp_cwnd_log_at].x.nagle.count_in_flight = stcb->asoc.total_flight_count;
246 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
248 int sctp_cwnd_log_at;
250 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
251 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
252 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
253 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_SACK;
254 sctp_clog[sctp_cwnd_log_at].x.sack.cumack = cumack;
255 sctp_clog[sctp_cwnd_log_at].x.sack.oldcumack = old_cumack;
256 sctp_clog[sctp_cwnd_log_at].x.sack.tsn = tsn;
257 sctp_clog[sctp_cwnd_log_at].x.sack.numGaps = gaps;
258 sctp_clog[sctp_cwnd_log_at].x.sack.numDups = dups;
262 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
264 int sctp_cwnd_log_at;
266 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
267 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
268 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
269 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_MAP;
270 sctp_clog[sctp_cwnd_log_at].x.map.base = map;
271 sctp_clog[sctp_cwnd_log_at].x.map.cum = cum;
272 sctp_clog[sctp_cwnd_log_at].x.map.high = high;
276 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn,
279 int sctp_cwnd_log_at;
281 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
282 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
283 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
284 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_FR;
285 sctp_clog[sctp_cwnd_log_at].x.fr.largest_tsn = biggest_tsn;
286 sctp_clog[sctp_cwnd_log_at].x.fr.largest_new_tsn = biggest_new_tsn;
287 sctp_clog[sctp_cwnd_log_at].x.fr.tsn = tsn;
292 sctp_log_mb(struct mbuf *m, int from)
294 int sctp_cwnd_log_at;
296 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
297 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
298 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
299 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_MBUF;
300 sctp_clog[sctp_cwnd_log_at].x.mb.mp = m;
301 sctp_clog[sctp_cwnd_log_at].x.mb.mbuf_flags = (uint8_t) (m->m_flags);
302 sctp_clog[sctp_cwnd_log_at].x.mb.size = (uint16_t) (m->m_len);
303 sctp_clog[sctp_cwnd_log_at].x.mb.data = m->m_data;
304 if (m->m_flags & M_EXT) {
305 sctp_clog[sctp_cwnd_log_at].x.mb.ext = m->m_ext.ext_buf;
306 sctp_clog[sctp_cwnd_log_at].x.mb.refcnt = (uint8_t) (*m->m_ext.ref_cnt);
308 sctp_clog[sctp_cwnd_log_at].x.mb.ext = 0;
309 sctp_clog[sctp_cwnd_log_at].x.mb.refcnt = 0;
315 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk,
318 int sctp_cwnd_log_at;
320 if (control == NULL) {
321 printf("Gak log of NULL?\n");
324 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
325 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
326 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
327 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_STRM;
328 sctp_clog[sctp_cwnd_log_at].x.strlog.n_tsn = control->sinfo_tsn;
329 sctp_clog[sctp_cwnd_log_at].x.strlog.n_sseq = control->sinfo_ssn;
330 if (poschk != NULL) {
331 sctp_clog[sctp_cwnd_log_at].x.strlog.e_tsn = poschk->sinfo_tsn;
332 sctp_clog[sctp_cwnd_log_at].x.strlog.e_sseq = poschk->sinfo_ssn;
334 sctp_clog[sctp_cwnd_log_at].x.strlog.e_tsn = 0;
335 sctp_clog[sctp_cwnd_log_at].x.strlog.e_sseq = 0;
340 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
342 int sctp_cwnd_log_at;
344 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
345 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
346 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
347 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_CWND;
348 sctp_clog[sctp_cwnd_log_at].x.cwnd.net = net;
349 if (stcb->asoc.send_queue_cnt > 255)
350 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_send = 255;
352 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
353 if (stcb->asoc.stream_queue_cnt > 255)
354 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_str = 255;
356 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
359 sctp_clog[sctp_cwnd_log_at].x.cwnd.cwnd_new_value = net->cwnd;
360 sctp_clog[sctp_cwnd_log_at].x.cwnd.inflight = net->flight_size;
361 sctp_clog[sctp_cwnd_log_at].x.cwnd.pseudo_cumack = net->pseudo_cumack;
362 sctp_clog[sctp_cwnd_log_at].x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
363 sctp_clog[sctp_cwnd_log_at].x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
365 if (SCTP_CWNDLOG_PRESEND == from) {
366 sctp_clog[sctp_cwnd_log_at].x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
368 sctp_clog[sctp_cwnd_log_at].x.cwnd.cwnd_augment = augment;
372 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
374 int sctp_cwnd_log_at;
376 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
377 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
378 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
379 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_LOCK_EVENT;
380 sctp_clog[sctp_cwnd_log_at].x.lock.sock = (uint32_t) inp->sctp_socket;
381 sctp_clog[sctp_cwnd_log_at].x.lock.inp = (uint32_t) inp;
383 sctp_clog[sctp_cwnd_log_at].x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
385 sctp_clog[sctp_cwnd_log_at].x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
388 sctp_clog[sctp_cwnd_log_at].x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
389 sctp_clog[sctp_cwnd_log_at].x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
391 sctp_clog[sctp_cwnd_log_at].x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
392 sctp_clog[sctp_cwnd_log_at].x.lock.create_lock = SCTP_LOCK_UNKNOWN;
394 sctp_clog[sctp_cwnd_log_at].x.lock.info_lock = mtx_owned(&sctppcbinfo.ipi_ep_mtx);
395 if (inp->sctp_socket) {
396 sctp_clog[sctp_cwnd_log_at].x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
397 sctp_clog[sctp_cwnd_log_at].x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
398 sctp_clog[sctp_cwnd_log_at].x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
400 sctp_clog[sctp_cwnd_log_at].x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
401 sctp_clog[sctp_cwnd_log_at].x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
402 sctp_clog[sctp_cwnd_log_at].x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
407 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
409 int sctp_cwnd_log_at;
411 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
412 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
413 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
414 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_MAXBURST;
415 sctp_clog[sctp_cwnd_log_at].x.cwnd.net = net;
416 sctp_clog[sctp_cwnd_log_at].x.cwnd.cwnd_new_value = error;
417 sctp_clog[sctp_cwnd_log_at].x.cwnd.inflight = net->flight_size;
418 sctp_clog[sctp_cwnd_log_at].x.cwnd.cwnd_augment = burst;
419 if (stcb->asoc.send_queue_cnt > 255)
420 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_send = 255;
422 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
423 if (stcb->asoc.stream_queue_cnt > 255)
424 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_str = 255;
426 sctp_clog[sctp_cwnd_log_at].x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
430 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
432 int sctp_cwnd_log_at;
434 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
435 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
436 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
437 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_RWND;
438 sctp_clog[sctp_cwnd_log_at].x.rwnd.rwnd = peers_rwnd;
439 sctp_clog[sctp_cwnd_log_at].x.rwnd.send_size = snd_size;
440 sctp_clog[sctp_cwnd_log_at].x.rwnd.overhead = overhead;
441 sctp_clog[sctp_cwnd_log_at].x.rwnd.new_rwnd = 0;
445 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
447 int sctp_cwnd_log_at;
449 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
450 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
451 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
452 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_RWND;
453 sctp_clog[sctp_cwnd_log_at].x.rwnd.rwnd = peers_rwnd;
454 sctp_clog[sctp_cwnd_log_at].x.rwnd.send_size = flight_size;
455 sctp_clog[sctp_cwnd_log_at].x.rwnd.overhead = overhead;
456 sctp_clog[sctp_cwnd_log_at].x.rwnd.new_rwnd = a_rwndval;
460 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
462 int sctp_cwnd_log_at;
464 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
465 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
466 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
467 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_MBCNT;
468 sctp_clog[sctp_cwnd_log_at].x.mbcnt.total_queue_size = total_oq;
469 sctp_clog[sctp_cwnd_log_at].x.mbcnt.size_change = book;
470 sctp_clog[sctp_cwnd_log_at].x.mbcnt.total_queue_mb_size = total_mbcnt_q;
471 sctp_clog[sctp_cwnd_log_at].x.mbcnt.mbcnt_change = mbcnt;
475 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
477 int sctp_cwnd_log_at;
479 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
480 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
481 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
482 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_MISC_EVENT;
483 sctp_clog[sctp_cwnd_log_at].x.misc.log1 = a;
484 sctp_clog[sctp_cwnd_log_at].x.misc.log2 = b;
485 sctp_clog[sctp_cwnd_log_at].x.misc.log3 = c;
486 sctp_clog[sctp_cwnd_log_at].x.misc.log4 = d;
490 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t cumtsn, uint32_t wake_cnt, int from)
492 int sctp_cwnd_log_at;
494 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
495 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
496 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
497 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_WAKE;
498 sctp_clog[sctp_cwnd_log_at].x.wake.stcb = (uint32_t) stcb;
499 sctp_clog[sctp_cwnd_log_at].x.wake.wake_cnt = wake_cnt;
500 sctp_clog[sctp_cwnd_log_at].x.wake.flight = stcb->asoc.total_flight_count;
501 sctp_clog[sctp_cwnd_log_at].x.wake.send_q = stcb->asoc.send_queue_cnt;
502 sctp_clog[sctp_cwnd_log_at].x.wake.sent_q = stcb->asoc.sent_queue_cnt;
504 if (stcb->asoc.stream_queue_cnt < 0xff)
505 sctp_clog[sctp_cwnd_log_at].x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
507 sctp_clog[sctp_cwnd_log_at].x.wake.stream_qcnt = 0xff;
509 if (stcb->asoc.chunks_on_out_queue < 0xff)
510 sctp_clog[sctp_cwnd_log_at].x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
512 sctp_clog[sctp_cwnd_log_at].x.wake.chunks_on_oque = 0xff;
514 sctp_clog[sctp_cwnd_log_at].x.wake.sctpflags = 0;
515 /* set in the defered mode stuff */
516 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
517 sctp_clog[sctp_cwnd_log_at].x.wake.sctpflags |= 1;
518 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
519 sctp_clog[sctp_cwnd_log_at].x.wake.sctpflags |= 2;
520 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
521 sctp_clog[sctp_cwnd_log_at].x.wake.sctpflags |= 4;
522 /* what about the sb */
523 if (stcb->sctp_socket) {
524 struct socket *so = stcb->sctp_socket;
526 sctp_clog[sctp_cwnd_log_at].x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
528 sctp_clog[sctp_cwnd_log_at].x.wake.sbflags = 0xff;
533 sctp_log_block(uint8_t from, struct socket *so, struct sctp_association *asoc, int sendlen)
535 int sctp_cwnd_log_at;
537 SCTP_STATLOG_GETREF(sctp_cwnd_log_at);
538 sctp_clog[sctp_cwnd_log_at].from = (uint8_t) from;
539 sctp_clog[sctp_cwnd_log_at].time_event = sctp_get_time_of_event();
540 sctp_clog[sctp_cwnd_log_at].event_type = (uint8_t) SCTP_LOG_EVENT_BLOCK;
541 sctp_clog[sctp_cwnd_log_at].x.blk.onsb = asoc->total_output_queue_size;
542 sctp_clog[sctp_cwnd_log_at].x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
543 sctp_clog[sctp_cwnd_log_at].x.blk.peer_rwnd = asoc->peers_rwnd;
544 sctp_clog[sctp_cwnd_log_at].x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
545 sctp_clog[sctp_cwnd_log_at].x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
546 sctp_clog[sctp_cwnd_log_at].x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
547 sctp_clog[sctp_cwnd_log_at].x.blk.sndlen = sendlen;
551 sctp_fill_stat_log(struct mbuf *m)
553 int sctp_cwnd_log_at;
554 struct sctp_cwnd_log_req *req;
556 int num, i, at, cnt_out = 0;
561 size_limit = (m->m_len - sizeof(struct sctp_cwnd_log_req));
562 if (size_limit < sizeof(struct sctp_cwnd_log)) {
565 sctp_cwnd_log_at = global_sctp_cwnd_log_at;
566 req = mtod(m, struct sctp_cwnd_log_req *);
567 num = size_limit / sizeof(struct sctp_cwnd_log);
568 if (global_sctp_cwnd_log_rolled) {
569 req->num_in_log = SCTP_STAT_LOG_SIZE;
571 req->num_in_log = sctp_cwnd_log_at;
573 * if the log has not rolled, we don't let you have old
576 if (req->end_at > sctp_cwnd_log_at) {
577 req->end_at = sctp_cwnd_log_at;
580 if ((num < SCTP_STAT_LOG_SIZE) &&
581 ((global_sctp_cwnd_log_rolled) || (sctp_cwnd_log_at > num))) {
582 /* we can't return all of it */
583 if (((req->start_at == 0) && (req->end_at == 0)) ||
584 (req->start_at >= SCTP_STAT_LOG_SIZE) ||
585 (req->end_at >= SCTP_STAT_LOG_SIZE)) {
586 /* No user request or user is wacked. */
588 req->end_at = sctp_cwnd_log_at - 1;
589 if ((sctp_cwnd_log_at - num) < 0) {
592 cc = num - sctp_cwnd_log_at;
593 req->start_at = SCTP_STAT_LOG_SIZE - cc;
595 req->start_at = sctp_cwnd_log_at - num;
601 if (req->start_at > req->end_at) {
602 cc = (SCTP_STAT_LOG_SIZE - req->start_at) +
606 cc = (req->end_at - req->start_at) + 1;
614 /* We can return all of it */
616 req->end_at = sctp_cwnd_log_at - 1;
617 req->num_ret = sctp_cwnd_log_at;
620 if (req->num_ret > num) {
621 panic("Bad statlog get?");
624 for (i = 0, at = req->start_at; i < req->num_ret; i++) {
625 req->log[i] = sctp_clog[at];
628 if (at >= SCTP_STAT_LOG_SIZE)
631 m->m_len = (cnt_out * sizeof(struct sctp_cwnd_log)) + sizeof(struct sctp_cwnd_log_req);
637 #ifdef SCTP_AUDITING_ENABLED
638 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
639 static int sctp_audit_indx = 0;
643 sctp_print_audit_report(void)
649 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
650 if ((sctp_audit_data[i][0] == 0xe0) &&
651 (sctp_audit_data[i][1] == 0x01)) {
654 } else if (sctp_audit_data[i][0] == 0xf0) {
657 } else if ((sctp_audit_data[i][0] == 0xc0) &&
658 (sctp_audit_data[i][1] == 0x01)) {
662 printf("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
663 (uint32_t) sctp_audit_data[i][1]);
668 for (i = 0; i < sctp_audit_indx; i++) {
669 if ((sctp_audit_data[i][0] == 0xe0) &&
670 (sctp_audit_data[i][1] == 0x01)) {
673 } else if (sctp_audit_data[i][0] == 0xf0) {
676 } else if ((sctp_audit_data[i][0] == 0xc0) &&
677 (sctp_audit_data[i][1] == 0x01)) {
681 printf("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
682 (uint32_t) sctp_audit_data[i][1]);
691 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
692 struct sctp_nets *net)
694 int resend_cnt, tot_out, rep, tot_book_cnt;
695 struct sctp_nets *lnet;
696 struct sctp_tmit_chunk *chk;
698 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
699 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
701 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
705 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
706 sctp_audit_data[sctp_audit_indx][1] = 0x01;
708 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
714 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
715 sctp_audit_data[sctp_audit_indx][1] = 0x02;
717 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
722 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
723 sctp_audit_data[sctp_audit_indx][1] =
724 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
726 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
731 resend_cnt = tot_out = 0;
732 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
733 if (chk->sent == SCTP_DATAGRAM_RESEND) {
735 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
736 tot_out += chk->book_size;
740 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
741 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
742 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
744 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
747 printf("resend_cnt:%d asoc-tot:%d\n",
748 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
750 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
751 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
752 sctp_audit_data[sctp_audit_indx][1] =
753 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
755 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
759 if (tot_out != stcb->asoc.total_flight) {
760 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
761 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
763 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
767 printf("tot_flt:%d asoc_tot:%d\n", tot_out,
768 (int)stcb->asoc.total_flight);
769 stcb->asoc.total_flight = tot_out;
771 if (tot_book_cnt != stcb->asoc.total_flight_count) {
772 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
773 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
775 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
779 printf("tot_flt_book:%d\n", tot_book);
781 stcb->asoc.total_flight_count = tot_book_cnt;
784 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
785 tot_out += lnet->flight_size;
787 if (tot_out != stcb->asoc.total_flight) {
788 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
789 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
791 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
795 printf("real flight:%d net total was %d\n",
796 stcb->asoc.total_flight, tot_out);
797 /* now corrective action */
798 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
801 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
802 if ((chk->whoTo == lnet) &&
803 (chk->sent < SCTP_DATAGRAM_RESEND)) {
804 tot_out += chk->book_size;
807 if (lnet->flight_size != tot_out) {
808 printf("net:%x flight was %d corrected to %d\n",
809 (uint32_t) lnet, lnet->flight_size, tot_out);
810 lnet->flight_size = tot_out;
815 sctp_print_audit_report();
820 sctp_audit_log(uint8_t ev, uint8_t fd)
825 sctp_audit_data[sctp_audit_indx][0] = ev;
826 sctp_audit_data[sctp_audit_indx][1] = fd;
828 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
837 * a list of sizes based on typical mtu's, used only if next hop size not
840 static int sctp_mtu_sizes[] = {
862 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
864 struct sctp_association *asoc;
865 struct sctp_nets *net;
869 callout_stop(&asoc->hb_timer.timer);
870 callout_stop(&asoc->dack_timer.timer);
871 callout_stop(&asoc->strreset_timer.timer);
872 callout_stop(&asoc->asconf_timer.timer);
873 callout_stop(&asoc->autoclose_timer.timer);
874 callout_stop(&asoc->delayed_event_timer.timer);
875 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
876 callout_stop(&net->fr_timer.timer);
877 callout_stop(&net->pmtu_timer.timer);
882 find_next_best_mtu(int totsz)
887 * if we are in here we must find the next best fit based on the
888 * size of the dg that failed to be sent.
891 for (i = 0; i < NUMBER_OF_MTU_SIZES; i++) {
892 if (totsz < sctp_mtu_sizes[i]) {
899 return (sctp_mtu_sizes[perfer]);
903 sctp_fill_random_store(struct sctp_pcb *m)
906 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
907 * our counter. The result becomes our good random numbers and we
908 * then setup to give these out. Note that we do no locking to
909 * protect this. This is ok, since if competing folks call this we
910 * will get more gobbled gook in the random store whic is what we
911 * want. There is a danger that two guys will use the same random
912 * numbers, but thats ok too since that is random as well :->
915 sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
916 sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
917 sizeof(m->random_counter), (uint8_t *) m->random_store);
922 sctp_select_initial_TSN(struct sctp_pcb *m)
925 * A true implementation should use random selection process to get
926 * the initial stream sequence number, using RFC1750 as a good
932 if (m->initial_sequence_debug != 0) {
935 ret = m->initial_sequence_debug;
936 m->initial_sequence_debug++;
939 if ((m->store_at + sizeof(u_long)) > SCTP_SIGNATURE_SIZE) {
940 /* Refill the random store */
941 sctp_fill_random_store(m);
943 p = &m->random_store[(int)m->store_at];
946 m->store_at += sizeof(u_long);
951 sctp_select_a_tag(struct sctp_inpcb *m)
956 SCTP_GETTIME_TIMEVAL(&now);
959 x = sctp_select_initial_TSN(&m->sctp_ep);
964 if (sctp_is_vtag_good(m, x, &now)) {
973 sctp_init_asoc(struct sctp_inpcb *m, struct sctp_association *asoc,
974 int for_a_init, uint32_t override_tag)
977 * Anything set to zero is taken care of by the allocation routine's
982 * Up front select what scoping to apply on addresses I tell my peer
983 * Not sure what to do with these right now, we will need to come up
984 * with a way to set them. We may need to pass them through from the
985 * caller in the sctp_aloc_assoc() function.
989 /* init all variables to a known value. */
990 asoc->state = SCTP_STATE_INUSE;
991 asoc->max_burst = m->sctp_ep.max_burst;
992 asoc->heart_beat_delay = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
993 asoc->cookie_life = m->sctp_ep.def_cookie_life;
994 asoc->sctp_cmt_on_off = (uint8_t) sctp_cmt_on_off;
996 asoc->default_tos = m->ip_inp.inp.inp_ip_tos;
998 asoc->default_tos = 0;
1002 asoc->default_flowlabel = ((struct in6pcb *)m)->in6p_flowinfo;
1004 asoc->default_flowlabel = 0;
1009 if (sctp_is_vtag_good(m, override_tag, &now)) {
1010 asoc->my_vtag = override_tag;
1016 asoc->my_vtag = sctp_select_a_tag(m);
1018 if (sctp_is_feature_on(m, SCTP_PCB_FLAGS_DONOT_HEARTBEAT))
1019 asoc->hb_is_disabled = 1;
1021 asoc->hb_is_disabled = 0;
1024 asoc->assoc_up_sent = 0;
1025 asoc->assoc_id = asoc->my_vtag;
1026 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1027 sctp_select_initial_TSN(&m->sctp_ep);
1028 /* we are optimisitic here */
1029 asoc->peer_supports_pktdrop = 1;
1031 asoc->sent_queue_retran_cnt = 0;
1034 asoc->last_net_data_came_from = NULL;
1036 /* This will need to be adjusted */
1037 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1038 asoc->last_acked_seq = asoc->init_seq_number - 1;
1039 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1040 asoc->asconf_seq_in = asoc->last_acked_seq;
1042 /* here we are different, we hold the next one we expect */
1043 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1045 asoc->initial_init_rto_max = m->sctp_ep.initial_init_rto_max;
1046 asoc->initial_rto = m->sctp_ep.initial_rto;
1048 asoc->max_init_times = m->sctp_ep.max_init_times;
1049 asoc->max_send_times = m->sctp_ep.max_send_times;
1050 asoc->def_net_failure = m->sctp_ep.def_net_failure;
1051 asoc->free_chunk_cnt = 0;
1053 asoc->iam_blocking = 0;
1054 /* ECN Nonce initialization */
1055 asoc->context = m->sctp_context;
1056 asoc->def_send = m->def_send;
1057 asoc->ecn_nonce_allowed = 0;
1058 asoc->receiver_nonce_sum = 1;
1059 asoc->nonce_sum_expect_base = 1;
1060 asoc->nonce_sum_check = 1;
1061 asoc->nonce_resync_tsn = 0;
1062 asoc->nonce_wait_for_ecne = 0;
1063 asoc->nonce_wait_tsn = 0;
1064 asoc->delayed_ack = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1065 asoc->pr_sctp_cnt = 0;
1066 asoc->total_output_queue_size = 0;
1068 if (m->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1069 struct in6pcb *inp6;
1072 /* Its a V6 socket */
1073 inp6 = (struct in6pcb *)m;
1074 asoc->ipv6_addr_legal = 1;
1075 /* Now look at the binding flag to see if V4 will be legal */
1077 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1079 asoc->ipv4_addr_legal = 1;
1081 /* V4 addresses are NOT legal on the association */
1082 asoc->ipv4_addr_legal = 0;
1085 /* Its a V4 socket, no - V6 */
1086 asoc->ipv4_addr_legal = 1;
1087 asoc->ipv6_addr_legal = 0;
1091 asoc->my_rwnd = max(m->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND);
1092 asoc->peers_rwnd = m->sctp_socket->so_rcv.sb_hiwat;
1094 asoc->smallest_mtu = m->sctp_frag_point;
1095 asoc->minrto = m->sctp_ep.sctp_minrto;
1096 asoc->maxrto = m->sctp_ep.sctp_maxrto;
1098 asoc->locked_on_sending = NULL;
1099 asoc->stream_locked_on = 0;
1100 asoc->ecn_echo_cnt_onq = 0;
1101 asoc->stream_locked = 0;
1103 LIST_INIT(&asoc->sctp_local_addr_list);
1104 TAILQ_INIT(&asoc->nets);
1105 TAILQ_INIT(&asoc->pending_reply_queue);
1106 asoc->last_asconf_ack_sent = NULL;
1107 /* Setup to fill the hb random cache at first HB */
1108 asoc->hb_random_idx = 4;
1110 asoc->sctp_autoclose_ticks = m->sctp_ep.auto_close_time;
1113 * Now the stream parameters, here we allocate space for all streams
1114 * that we request by default.
1116 asoc->streamoutcnt = asoc->pre_open_streams =
1117 m->sctp_ep.pre_open_stream_count;
1118 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1119 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1121 if (asoc->strmout == NULL) {
1122 /* big trouble no memory */
1125 for (i = 0; i < asoc->streamoutcnt; i++) {
1127 * inbound side must be set to 0xffff, also NOTE when we get
1128 * the INIT-ACK back (for INIT sender) we MUST reduce the
1129 * count (streamoutcnt) but first check if we sent to any of
1130 * the upper streams that were dropped (if some were). Those
1131 * that were dropped must be notified to the upper layer as
1134 asoc->strmout[i].next_sequence_sent = 0x0;
1135 TAILQ_INIT(&asoc->strmout[i].outqueue);
1136 asoc->strmout[i].stream_no = i;
1137 asoc->strmout[i].last_msg_incomplete = 0;
1138 asoc->strmout[i].next_spoke.tqe_next = 0;
1139 asoc->strmout[i].next_spoke.tqe_prev = 0;
1141 /* Now the mapping array */
1142 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1143 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1145 if (asoc->mapping_array == NULL) {
1146 SCTP_FREE(asoc->strmout);
1149 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1150 /* Now the init of the other outqueues */
1151 TAILQ_INIT(&asoc->free_chunks);
1152 TAILQ_INIT(&asoc->free_strmoq);
1153 TAILQ_INIT(&asoc->out_wheel);
1154 TAILQ_INIT(&asoc->control_send_queue);
1155 TAILQ_INIT(&asoc->send_queue);
1156 TAILQ_INIT(&asoc->sent_queue);
1157 TAILQ_INIT(&asoc->reasmqueue);
1158 TAILQ_INIT(&asoc->resetHead);
1159 asoc->max_inbound_streams = m->sctp_ep.max_open_streams_intome;
1160 TAILQ_INIT(&asoc->asconf_queue);
1161 /* authentication fields */
1162 asoc->authinfo.random = NULL;
1163 asoc->authinfo.assoc_key = NULL;
1164 asoc->authinfo.assoc_keyid = 0;
1165 asoc->authinfo.recv_key = NULL;
1166 asoc->authinfo.recv_keyid = 0;
1167 LIST_INIT(&asoc->shared_keys);
1173 sctp_expand_mapping_array(struct sctp_association *asoc)
1175 /* mapping array needs to grow */
1179 new_size = asoc->mapping_array_size + SCTP_MAPPING_ARRAY_INCR;
1180 SCTP_MALLOC(new_array, uint8_t *, new_size, "MappingArray");
1181 if (new_array == NULL) {
1182 /* can't get more, forget it */
1183 printf("No memory for expansion of SCTP mapping array %d\n",
1187 memset(new_array, 0, new_size);
1188 memcpy(new_array, asoc->mapping_array, asoc->mapping_array_size);
1189 SCTP_FREE(asoc->mapping_array);
1190 asoc->mapping_array = new_array;
1191 asoc->mapping_array_size = new_size;
1195 extern unsigned int sctp_early_fr_msec;
1198 sctp_handle_addr_wq(void)
1200 /* deal with the ADDR wq from the rtsock calls */
1201 struct sctp_laddr *wi;
1203 SCTP_IPI_ADDR_LOCK();
1204 wi = LIST_FIRST(&sctppcbinfo.addr_wq);
1206 SCTP_IPI_ADDR_UNLOCK();
1209 LIST_REMOVE(wi, sctp_nxt_addr);
1210 if (!LIST_EMPTY(&sctppcbinfo.addr_wq)) {
1211 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1212 (struct sctp_inpcb *)NULL,
1213 (struct sctp_tcb *)NULL,
1214 (struct sctp_nets *)NULL);
1216 SCTP_IPI_ADDR_UNLOCK();
1217 if (wi->action == RTM_ADD) {
1218 sctp_add_ip_address(wi->ifa);
1219 } else if (wi->action == RTM_DELETE) {
1220 sctp_delete_ip_address(wi->ifa);
1223 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, wi);
1224 SCTP_DECR_LADDR_COUNT();
1228 sctp_timeout_handler(void *t)
1230 struct sctp_inpcb *inp;
1231 struct sctp_tcb *stcb;
1232 struct sctp_nets *net;
1233 struct sctp_timer *tmr;
1235 struct sctp_iterator *it = NULL;
1239 tmr = (struct sctp_timer *)t;
1240 inp = (struct sctp_inpcb *)tmr->ep;
1241 stcb = (struct sctp_tcb *)tmr->tcb;
1242 net = (struct sctp_nets *)tmr->net;
1245 #ifdef SCTP_AUDITING_ENABLED
1246 sctp_audit_log(0xF0, (uint8_t) tmr->type);
1247 sctp_auditing(3, inp, stcb, net);
1250 /* sanity checks... */
1251 if (tmr->self != (void *)tmr) {
1253 * printf("Stale SCTP timer fired (%p), ignoring...\n",
1259 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1261 * printf("SCTP timer fired with invalid type: 0x%x\n",
1267 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1271 /* if this is an iterator timeout, get the struct and clear inp */
1272 if (tmr->type == SCTP_TIMER_TYPE_ITERATOR) {
1273 it = (struct sctp_iterator *)inp;
1277 SCTP_INP_INCR_REF(inp);
1278 if ((inp->sctp_socket == 0) &&
1279 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1280 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1281 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1282 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1283 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1286 SCTP_INP_DECR_REF(inp);
1291 if (stcb->asoc.state == 0) {
1294 SCTP_INP_DECR_REF(inp);
1300 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1301 printf("Timer type %d goes off\n", tmr->type);
1303 #endif /* SCTP_DEBUG */
1304 if (!callout_active(&tmr->timer)) {
1307 SCTP_INP_DECR_REF(inp);
1312 atomic_add_16(&stcb->asoc.refcnt, 1);
1313 SCTP_TCB_LOCK(stcb);
1314 atomic_add_16(&stcb->asoc.refcnt, -1);
1316 /* mark as being serviced now */
1317 callout_deactivate(&tmr->timer);
1319 /* call the handler for the appropriate timer type */
1320 switch (tmr->type) {
1321 case SCTP_TIMER_TYPE_ADDR_WQ:
1322 sctp_handle_addr_wq();
1324 case SCTP_TIMER_TYPE_ITERATOR:
1325 SCTP_STAT_INCR(sctps_timoiterator);
1326 sctp_iterator_timer(it);
1328 case SCTP_TIMER_TYPE_SEND:
1329 SCTP_STAT_INCR(sctps_timodata);
1330 stcb->asoc.num_send_timers_up--;
1331 if (stcb->asoc.num_send_timers_up < 0) {
1332 stcb->asoc.num_send_timers_up = 0;
1334 if (sctp_t3rxt_timer(inp, stcb, net)) {
1335 /* no need to unlock on tcb its gone */
1339 #ifdef SCTP_AUDITING_ENABLED
1340 sctp_auditing(4, inp, stcb, net);
1342 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1343 if ((stcb->asoc.num_send_timers_up == 0) &&
1344 (stcb->asoc.sent_queue_cnt > 0)
1346 struct sctp_tmit_chunk *chk;
1349 * safeguard. If there on some on the sent queue
1350 * somewhere but no timers running something is
1351 * wrong... so we start a timer on the first chunk
1352 * on the send queue on whatever net it is sent to.
1354 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1355 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1359 case SCTP_TIMER_TYPE_INIT:
1360 SCTP_STAT_INCR(sctps_timoinit);
1361 if (sctp_t1init_timer(inp, stcb, net)) {
1362 /* no need to unlock on tcb its gone */
1365 /* We do output but not here */
1368 case SCTP_TIMER_TYPE_RECV:
1369 SCTP_STAT_INCR(sctps_timosack);
1370 sctp_send_sack(stcb);
1371 #ifdef SCTP_AUDITING_ENABLED
1372 sctp_auditing(4, inp, stcb, net);
1374 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR);
1376 case SCTP_TIMER_TYPE_SHUTDOWN:
1377 if (sctp_shutdown_timer(inp, stcb, net)) {
1378 /* no need to unlock on tcb its gone */
1381 SCTP_STAT_INCR(sctps_timoshutdown);
1382 #ifdef SCTP_AUDITING_ENABLED
1383 sctp_auditing(4, inp, stcb, net);
1385 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR);
1387 case SCTP_TIMER_TYPE_HEARTBEAT:
1389 struct sctp_nets *net;
1390 int cnt_of_unconf = 0;
1392 SCTP_STAT_INCR(sctps_timoheartbeat);
1393 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1394 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1395 (net->dest_state & SCTP_ADDR_REACHABLE)) {
1399 if (cnt_of_unconf == 0) {
1400 if (sctp_heartbeat_timer(inp, stcb, net, cnt_of_unconf)) {
1401 /* no need to unlock on tcb its gone */
1405 #ifdef SCTP_AUDITING_ENABLED
1406 sctp_auditing(4, inp, stcb, net);
1408 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
1410 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR);
1413 case SCTP_TIMER_TYPE_COOKIE:
1414 if (sctp_cookie_timer(inp, stcb, net)) {
1415 /* no need to unlock on tcb its gone */
1418 SCTP_STAT_INCR(sctps_timocookie);
1419 #ifdef SCTP_AUDITING_ENABLED
1420 sctp_auditing(4, inp, stcb, net);
1423 * We consider T3 and Cookie timer pretty much the same with
1424 * respect to where from in chunk_output.
1426 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1428 case SCTP_TIMER_TYPE_NEWCOOKIE:
1433 SCTP_STAT_INCR(sctps_timosecret);
1434 SCTP_GETTIME_TIMEVAL(&tv);
1435 SCTP_INP_WLOCK(inp);
1436 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1437 inp->sctp_ep.last_secret_number =
1438 inp->sctp_ep.current_secret_number;
1439 inp->sctp_ep.current_secret_number++;
1440 if (inp->sctp_ep.current_secret_number >=
1441 SCTP_HOW_MANY_SECRETS) {
1442 inp->sctp_ep.current_secret_number = 0;
1444 secret = (int)inp->sctp_ep.current_secret_number;
1445 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1446 inp->sctp_ep.secret_key[secret][i] =
1447 sctp_select_initial_TSN(&inp->sctp_ep);
1449 SCTP_INP_WUNLOCK(inp);
1450 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1454 case SCTP_TIMER_TYPE_PATHMTURAISE:
1455 SCTP_STAT_INCR(sctps_timopathmtu);
1456 sctp_pathmtu_timer(inp, stcb, net);
1459 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1460 if (sctp_shutdownack_timer(inp, stcb, net)) {
1461 /* no need to unlock on tcb its gone */
1464 SCTP_STAT_INCR(sctps_timoshutdownack);
1465 #ifdef SCTP_AUDITING_ENABLED
1466 sctp_auditing(4, inp, stcb, net);
1468 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR);
1470 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1471 SCTP_STAT_INCR(sctps_timoshutdownguard);
1472 sctp_abort_an_association(inp, stcb,
1473 SCTP_SHUTDOWN_GUARD_EXPIRES, NULL);
1474 /* no need to unlock on tcb its gone */
1478 case SCTP_TIMER_TYPE_STRRESET:
1479 if (sctp_strreset_timer(inp, stcb, net)) {
1480 /* no need to unlock on tcb its gone */
1483 SCTP_STAT_INCR(sctps_timostrmrst);
1484 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR);
1486 case SCTP_TIMER_TYPE_EARLYFR:
1487 /* Need to do FR of things for net */
1488 SCTP_STAT_INCR(sctps_timoearlyfr);
1489 sctp_early_fr_timer(inp, stcb, net);
1491 case SCTP_TIMER_TYPE_ASCONF:
1492 if (sctp_asconf_timer(inp, stcb, net)) {
1493 /* no need to unlock on tcb its gone */
1496 SCTP_STAT_INCR(sctps_timoasconf);
1497 #ifdef SCTP_AUDITING_ENABLED
1498 sctp_auditing(4, inp, stcb, net);
1500 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR);
1503 case SCTP_TIMER_TYPE_AUTOCLOSE:
1504 SCTP_STAT_INCR(sctps_timoautoclose);
1505 sctp_autoclose_timer(inp, stcb, net);
1506 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR);
1509 case SCTP_TIMER_TYPE_ASOCKILL:
1510 SCTP_STAT_INCR(sctps_timoassockill);
1511 /* Can we free it yet? */
1512 SCTP_INP_DECR_REF(inp);
1513 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
1514 sctp_free_assoc(inp, stcb, 0);
1516 * free asoc, always unlocks (or destroy's) so prevent
1517 * duplicate unlock or unlock of a free mtx :-0
1522 case SCTP_TIMER_TYPE_INPKILL:
1523 SCTP_STAT_INCR(sctps_timoinpkill);
1525 * special case, take away our increment since WE are the
1528 SCTP_INP_DECR_REF(inp);
1529 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
1530 sctp_inpcb_free(inp, 1, 0);
1535 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1536 printf("sctp_timeout_handler:unknown timer %d\n",
1539 #endif /* SCTP_DEBUG */
1542 #ifdef SCTP_AUDITING_ENABLED
1543 sctp_audit_log(0xF1, (uint8_t) tmr->type);
1545 sctp_auditing(5, inp, stcb, net);
1547 if ((did_output) && stcb) {
1549 * Now we need to clean up the control chunk chain if an
1550 * ECNE is on it. It must be marked as UNSENT again so next
1551 * call will continue to send it until such time that we get
1552 * a CWR, to remove it. It is, however, less likely that we
1553 * will find a ecn echo on the chain though.
1555 sctp_fix_ecn_echo(&stcb->asoc);
1558 SCTP_TCB_UNLOCK(stcb);
1562 SCTP_INP_DECR_REF(inp);
1567 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1568 printf("Timer now complete (type %d)\n", tmr->type);
1570 #endif /* SCTP_DEBUG */
1577 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1578 struct sctp_nets *net)
1581 struct sctp_timer *tmr;
1584 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
1592 SCTP_TCB_LOCK_ASSERT(stcb);
1595 case SCTP_TIMER_TYPE_ADDR_WQ:
1596 /* Only 1 tick away :-) */
1597 tmr = &sctppcbinfo.addr_wq_timer;
1600 case SCTP_TIMER_TYPE_ITERATOR:
1602 struct sctp_iterator *it;
1604 it = (struct sctp_iterator *)inp;
1606 to_ticks = SCTP_ITERATOR_TICKS;
1609 case SCTP_TIMER_TYPE_SEND:
1610 /* Here we use the RTO timer */
1614 if ((stcb == NULL) || (net == NULL)) {
1617 tmr = &net->rxt_timer;
1618 if (net->RTO == 0) {
1619 rto_val = stcb->asoc.initial_rto;
1623 to_ticks = MSEC_TO_TICKS(rto_val);
1626 case SCTP_TIMER_TYPE_INIT:
1628 * Here we use the INIT timer default usually about 1
1631 if ((stcb == NULL) || (net == NULL)) {
1634 tmr = &net->rxt_timer;
1635 if (net->RTO == 0) {
1636 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1638 to_ticks = MSEC_TO_TICKS(net->RTO);
1641 case SCTP_TIMER_TYPE_RECV:
1643 * Here we use the Delayed-Ack timer value from the inp
1644 * ususually about 200ms.
1649 tmr = &stcb->asoc.dack_timer;
1650 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1652 case SCTP_TIMER_TYPE_SHUTDOWN:
1653 /* Here we use the RTO of the destination. */
1654 if ((stcb == NULL) || (net == NULL)) {
1657 if (net->RTO == 0) {
1658 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1660 to_ticks = MSEC_TO_TICKS(net->RTO);
1662 tmr = &net->rxt_timer;
1664 case SCTP_TIMER_TYPE_HEARTBEAT:
1666 * the net is used here so that we can add in the RTO. Even
1667 * though we use a different timer. We also add the HB timer
1668 * PLUS a random jitter.
1674 uint8_t this_random;
1675 int cnt_of_unconf = 0;
1676 struct sctp_nets *lnet;
1678 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1679 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1680 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1684 if (cnt_of_unconf) {
1686 sctp_heartbeat_timer(inp, stcb, lnet, cnt_of_unconf);
1688 if (stcb->asoc.hb_random_idx > 3) {
1689 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
1690 memcpy(stcb->asoc.hb_random_values, &rndval,
1691 sizeof(stcb->asoc.hb_random_values));
1692 this_random = stcb->asoc.hb_random_values[0];
1693 stcb->asoc.hb_random_idx = 0;
1694 stcb->asoc.hb_ect_randombit = 0;
1696 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
1697 stcb->asoc.hb_random_idx++;
1698 stcb->asoc.hb_ect_randombit = 0;
1701 * this_random will be 0 - 256 ms RTO is in ms.
1703 if ((stcb->asoc.hb_is_disabled) &&
1704 (cnt_of_unconf == 0)) {
1708 struct sctp_nets *lnet;
1711 delay = stcb->asoc.heart_beat_delay;
1712 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1713 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1714 ((lnet->dest_state & SCTP_ADDR_OUT_OF_SCOPE) == 0) &&
1715 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1719 if (net->RTO == 0) {
1720 /* Never been checked */
1721 to_ticks = this_random + stcb->asoc.initial_rto + delay;
1723 /* set rto_val to the ms */
1724 to_ticks = delay + net->RTO + this_random;
1727 if (cnt_of_unconf) {
1728 to_ticks = this_random + stcb->asoc.initial_rto;
1730 to_ticks = stcb->asoc.heart_beat_delay + this_random + stcb->asoc.initial_rto;
1734 * Now we must convert the to_ticks that are now in
1737 to_ticks = MSEC_TO_TICKS(to_ticks);
1738 tmr = &stcb->asoc.hb_timer;
1741 case SCTP_TIMER_TYPE_COOKIE:
1743 * Here we can use the RTO timer from the network since one
1744 * RTT was compelete. If a retran happened then we will be
1745 * using the RTO initial value.
1747 if ((stcb == NULL) || (net == NULL)) {
1750 if (net->RTO == 0) {
1751 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1753 to_ticks = MSEC_TO_TICKS(net->RTO);
1755 tmr = &net->rxt_timer;
1757 case SCTP_TIMER_TYPE_NEWCOOKIE:
1759 * nothing needed but the endpoint here ususually about 60
1762 tmr = &inp->sctp_ep.signature_change;
1763 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
1765 case SCTP_TIMER_TYPE_ASOCKILL:
1769 tmr = &stcb->asoc.strreset_timer;
1770 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
1772 case SCTP_TIMER_TYPE_INPKILL:
1774 * The inp is setup to die. We re-use the signature_chage
1775 * timer since that has stopped and we are in the GONE
1778 tmr = &inp->sctp_ep.signature_change;
1779 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
1781 case SCTP_TIMER_TYPE_PATHMTURAISE:
1783 * Here we use the value found in the EP for PMTU ususually
1792 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
1793 tmr = &net->pmtu_timer;
1795 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1796 /* Here we use the RTO of the destination */
1797 if ((stcb == NULL) || (net == NULL)) {
1800 if (net->RTO == 0) {
1801 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1803 to_ticks = MSEC_TO_TICKS(net->RTO);
1805 tmr = &net->rxt_timer;
1807 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1809 * Here we use the endpoints shutdown guard timer usually
1815 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
1816 tmr = &stcb->asoc.shut_guard_timer;
1818 case SCTP_TIMER_TYPE_STRRESET:
1820 * Here the timer comes from the inp but its value is from
1823 if ((stcb == NULL) || (net == NULL)) {
1826 if (net->RTO == 0) {
1827 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1829 to_ticks = MSEC_TO_TICKS(net->RTO);
1831 tmr = &stcb->asoc.strreset_timer;
1834 case SCTP_TIMER_TYPE_EARLYFR:
1838 if ((stcb == NULL) || (net == NULL)) {
1841 if (net->flight_size > net->cwnd) {
1842 /* no need to start */
1845 SCTP_STAT_INCR(sctps_earlyfrstart);
1846 if (net->lastsa == 0) {
1847 /* Hmm no rtt estimate yet? */
1848 msec = stcb->asoc.initial_rto >> 2;
1850 msec = ((net->lastsa >> 2) + net->lastsv) >> 1;
1852 if (msec < sctp_early_fr_msec) {
1853 msec = sctp_early_fr_msec;
1854 if (msec < SCTP_MINFR_MSEC_FLOOR) {
1855 msec = SCTP_MINFR_MSEC_FLOOR;
1858 to_ticks = MSEC_TO_TICKS(msec);
1859 tmr = &net->fr_timer;
1862 case SCTP_TIMER_TYPE_ASCONF:
1864 * Here the timer comes from the inp but its value is from
1867 if ((stcb == NULL) || (net == NULL)) {
1870 if (net->RTO == 0) {
1871 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1873 to_ticks = MSEC_TO_TICKS(net->RTO);
1875 tmr = &stcb->asoc.asconf_timer;
1877 case SCTP_TIMER_TYPE_AUTOCLOSE:
1881 if (stcb->asoc.sctp_autoclose_ticks == 0) {
1883 * Really an error since stcb is NOT set to
1888 to_ticks = stcb->asoc.sctp_autoclose_ticks;
1889 tmr = &stcb->asoc.autoclose_timer;
1893 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1894 printf("sctp_timer_start:Unknown timer type %d\n",
1897 #endif /* SCTP_DEBUG */
1901 if ((to_ticks <= 0) || (tmr == NULL)) {
1903 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1904 printf("sctp_timer_start:%d:software error to_ticks:%d tmr:%p not set ??\n",
1905 t_type, to_ticks, tmr);
1907 #endif /* SCTP_DEBUG */
1910 if (callout_pending(&tmr->timer)) {
1912 * we do NOT allow you to have it already running. if it is
1913 * we leave the current one up unchanged
1917 /* At this point we can proceed */
1918 if (t_type == SCTP_TIMER_TYPE_SEND) {
1919 stcb->asoc.num_send_timers_up++;
1922 tmr->ep = (void *)inp;
1923 tmr->tcb = (void *)stcb;
1924 tmr->net = (void *)net;
1925 tmr->self = (void *)tmr;
1927 callout_reset(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
1932 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1933 struct sctp_nets *net)
1935 struct sctp_timer *tmr;
1937 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
1943 SCTP_TCB_LOCK_ASSERT(stcb);
1946 case SCTP_TIMER_TYPE_ADDR_WQ:
1947 tmr = &sctppcbinfo.addr_wq_timer;
1949 case SCTP_TIMER_TYPE_EARLYFR:
1950 if ((stcb == NULL) || (net == NULL)) {
1953 tmr = &net->fr_timer;
1954 SCTP_STAT_INCR(sctps_earlyfrstop);
1956 case SCTP_TIMER_TYPE_ITERATOR:
1958 struct sctp_iterator *it;
1960 it = (struct sctp_iterator *)inp;
1964 case SCTP_TIMER_TYPE_SEND:
1965 if ((stcb == NULL) || (net == NULL)) {
1968 tmr = &net->rxt_timer;
1970 case SCTP_TIMER_TYPE_INIT:
1971 if ((stcb == NULL) || (net == NULL)) {
1974 tmr = &net->rxt_timer;
1976 case SCTP_TIMER_TYPE_RECV:
1980 tmr = &stcb->asoc.dack_timer;
1982 case SCTP_TIMER_TYPE_SHUTDOWN:
1983 if ((stcb == NULL) || (net == NULL)) {
1986 tmr = &net->rxt_timer;
1988 case SCTP_TIMER_TYPE_HEARTBEAT:
1992 tmr = &stcb->asoc.hb_timer;
1994 case SCTP_TIMER_TYPE_COOKIE:
1995 if ((stcb == NULL) || (net == NULL)) {
1998 tmr = &net->rxt_timer;
2000 case SCTP_TIMER_TYPE_NEWCOOKIE:
2001 /* nothing needed but the endpoint here */
2002 tmr = &inp->sctp_ep.signature_change;
2004 * We re-use the newcookie timer for the INP kill timer. We
2005 * must assure that we do not kill it by accident.
2008 case SCTP_TIMER_TYPE_ASOCKILL:
2010 * Stop the asoc kill timer.
2015 tmr = &stcb->asoc.strreset_timer;
2018 case SCTP_TIMER_TYPE_INPKILL:
2020 * The inp is setup to die. We re-use the signature_chage
2021 * timer since that has stopped and we are in the GONE
2024 tmr = &inp->sctp_ep.signature_change;
2026 case SCTP_TIMER_TYPE_PATHMTURAISE:
2027 if ((stcb == NULL) || (net == NULL)) {
2030 tmr = &net->pmtu_timer;
2032 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2033 if ((stcb == NULL) || (net == NULL)) {
2036 tmr = &net->rxt_timer;
2038 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2042 tmr = &stcb->asoc.shut_guard_timer;
2044 case SCTP_TIMER_TYPE_STRRESET:
2048 tmr = &stcb->asoc.strreset_timer;
2050 case SCTP_TIMER_TYPE_ASCONF:
2054 tmr = &stcb->asoc.asconf_timer;
2056 case SCTP_TIMER_TYPE_AUTOCLOSE:
2060 tmr = &stcb->asoc.autoclose_timer;
2064 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
2065 printf("sctp_timer_stop:Unknown timer type %d\n",
2068 #endif /* SCTP_DEBUG */
2074 if ((tmr->type != t_type) && tmr->type) {
2076 * Ok we have a timer that is under joint use. Cookie timer
2077 * per chance with the SEND timer. We therefore are NOT
2078 * running the timer that the caller wants stopped. So just
2083 if (t_type == SCTP_TIMER_TYPE_SEND) {
2084 stcb->asoc.num_send_timers_up--;
2085 if (stcb->asoc.num_send_timers_up < 0) {
2086 stcb->asoc.num_send_timers_up = 0;
2090 callout_stop(&tmr->timer);
2094 #ifdef SCTP_USE_ADLER32
2096 update_adler32(uint32_t adler, uint8_t * buf, int32_t len)
2098 uint32_t s1 = adler & 0xffff;
2099 uint32_t s2 = (adler >> 16) & 0xffff;
2102 for (n = 0; n < len; n++, buf++) {
2103 /* s1 = (s1 + buf[n]) % BASE */
2107 * now if we need to, we do a mod by subtracting. It seems a
2108 * bit faster since I really will only ever do one subtract
2109 * at the MOST, since buf[n] is a max of 255.
2111 if (s1 >= SCTP_ADLER32_BASE) {
2112 s1 -= SCTP_ADLER32_BASE;
2114 /* s2 = (s2 + s1) % BASE */
2118 * again, it is more efficent (it seems) to subtract since
2119 * the most s2 will ever be is (BASE-1 + BASE-1) in the
2120 * worse case. This would then be (2 * BASE) - 2, which will
2121 * still only do one subtract. On Intel this is much better
2122 * to do this way and avoid the divide. Have not -pg'd on
2125 if (s2 >= SCTP_ADLER32_BASE) {
2126 s2 -= SCTP_ADLER32_BASE;
2129 /* Return the adler32 of the bytes buf[0..len-1] */
2130 return ((s2 << 16) + s1);
2137 sctp_calculate_len(struct mbuf *m)
2150 #if defined(SCTP_WITH_NO_CSUM)
2153 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2156 * given a mbuf chain with a packetheader offset by 'offset'
2157 * pointing at a sctphdr (with csum set to 0) go through the chain
2158 * of m_next's and calculate the SCTP checksum. This is currently
2159 * Adler32 but will change to CRC32x soon. Also has a side bonus
2160 * calculate the total length of the mbuf chain. Note: if offset is
2161 * greater than the total mbuf length, checksum=1, pktlen=0 is
2162 * returned (ie. no real error code)
2166 *pktlen = sctp_calculate_len(m);
2170 #elif defined(SCTP_USE_INCHKSUM)
2172 #include <machine/in_cksum.h>
2175 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2178 * given a mbuf chain with a packetheader offset by 'offset'
2179 * pointing at a sctphdr (with csum set to 0) go through the chain
2180 * of m_next's and calculate the SCTP checksum. This is currently
2181 * Adler32 but will change to CRC32x soon. Also has a side bonus
2182 * calculate the total length of the mbuf chain. Note: if offset is
2183 * greater than the total mbuf length, checksum=1, pktlen=0 is
2184 * returned (ie. no real error code)
2188 uint32_t the_sum, retsum;
2195 the_sum = (uint32_t) (in_cksum_skip(m, tlen, offset));
2197 *pktlen = (tlen - offset);
2198 retsum = htons(the_sum);
2205 sctp_calculate_sum(struct mbuf *m, int32_t * pktlen, uint32_t offset)
2208 * given a mbuf chain with a packetheader offset by 'offset'
2209 * pointing at a sctphdr (with csum set to 0) go through the chain
2210 * of m_next's and calculate the SCTP checksum. This is currently
2211 * Adler32 but will change to CRC32x soon. Also has a side bonus
2212 * calculate the total length of the mbuf chain. Note: if offset is
2213 * greater than the total mbuf length, checksum=1, pktlen=0 is
2214 * returned (ie. no real error code)
2218 #ifdef SCTP_USE_ADLER32
2222 uint32_t base = 0xffffffff;
2224 #endif /* SCTP_USE_ADLER32 */
2228 /* find the correct mbuf and offset into mbuf */
2229 while ((at != NULL) && (offset > (uint32_t) at->m_len)) {
2230 offset -= at->m_len; /* update remaining offset left */
2233 while (at != NULL) {
2234 if ((at->m_len - offset) > 0) {
2235 #ifdef SCTP_USE_ADLER32
2236 base = update_adler32(base,
2237 (unsigned char *)(at->m_data + offset),
2238 (unsigned int)(at->m_len - offset));
2240 if ((at->m_len - offset) < 4) {
2241 /* Use old method if less than 4 bytes */
2242 base = old_update_crc32(base,
2243 (unsigned char *)(at->m_data + offset),
2244 (unsigned int)(at->m_len - offset));
2246 base = update_crc32(base,
2247 (unsigned char *)(at->m_data + offset),
2248 (unsigned int)(at->m_len - offset));
2250 #endif /* SCTP_USE_ADLER32 */
2251 tlen += at->m_len - offset;
2252 /* we only offset once into the first mbuf */
2255 if (offset < at->m_len)
2258 offset -= at->m_len;
2262 if (pktlen != NULL) {
2265 #ifdef SCTP_USE_ADLER32
2270 base = sctp_csum_finalize(base);
2279 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2280 struct sctp_association *asoc, u_long mtu)
2283 * Reset the P-MTU size on this association, this involves changing
2284 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2285 * allow the DF flag to be cleared.
2287 struct sctp_tmit_chunk *chk;
2288 unsigned int eff_mtu, ovh;
2290 asoc->smallest_mtu = mtu;
2291 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2292 ovh = SCTP_MIN_OVERHEAD;
2294 ovh = SCTP_MIN_V4_OVERHEAD;
2296 eff_mtu = mtu - ovh;
2297 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2299 if (chk->send_size > eff_mtu) {
2300 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2303 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2304 if (chk->send_size > eff_mtu) {
2305 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2312 * given an association and starting time of the current RTT period return
2313 * RTO in number of usecs net should point to the current network
2316 sctp_calculate_rto(struct sctp_tcb *stcb,
2317 struct sctp_association *asoc,
2318 struct sctp_nets *net,
2319 struct timeval *old)
2322 * given an association and the starting time of the current RTT
2323 * period (in value1/value2) return RTO in number of usecs.
2327 unsigned int new_rto = 0;
2328 int first_measure = 0;
2331 /************************/
2332 /* 1. calculate new RTT */
2333 /************************/
2334 /* get the current time */
2335 SCTP_GETTIME_TIMEVAL(&now);
2336 /* compute the RTT value */
2337 if ((u_long)now.tv_sec > (u_long)old->tv_sec) {
2338 calc_time = ((u_long)now.tv_sec - (u_long)old->tv_sec) * 1000;
2339 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2340 calc_time += (((u_long)now.tv_usec -
2341 (u_long)old->tv_usec) / 1000);
2342 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2343 /* Borrow 1,000ms from current calculation */
2345 /* Add in the slop over */
2346 calc_time += ((int)now.tv_usec / 1000);
2347 /* Add in the pre-second ms's */
2348 calc_time += (((int)1000000 - (int)old->tv_usec) / 1000);
2350 } else if ((u_long)now.tv_sec == (u_long)old->tv_sec) {
2351 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2352 calc_time = ((u_long)now.tv_usec -
2353 (u_long)old->tv_usec) / 1000;
2354 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2355 /* impossible .. garbage in nothing out */
2356 return (((net->lastsa >> 2) + net->lastsv) >> 1);
2358 /* impossible .. garbage in nothing out */
2359 return (((net->lastsa >> 2) + net->lastsv) >> 1);
2362 /* Clock wrapped? */
2363 return (((net->lastsa >> 2) + net->lastsv) >> 1);
2365 /***************************/
2366 /* 2. update RTTVAR & SRTT */
2367 /***************************/
2369 /* if (net->lastsv || net->lastsa) { */
2370 /* per Section 5.3.1 C3 in SCTP */
2371 /* net->lastsv = (int) *//* RTTVAR */
2373 * (((double)(1.0 - 0.25) * (double)net->lastsv) + (double)(0.25 *
2374 * (double)abs(net->lastsa - calc_time))); net->lastsa = (int)
2377 * (((double)(1.0 - 0.125) * (double)net->lastsa) + (double)(0.125 *
2378 * (double)calc_time)); } else {
2379 *//* the first RTT calculation, per C2 Section 5.3.1 */
2380 /* net->lastsa = calc_time; *//* SRTT */
2381 /* net->lastsv = calc_time / 2; *//* RTTVAR */
2383 /* if RTTVAR goes to 0 you set to clock grainularity */
2385 * if (net->lastsv == 0) { net->lastsv = SCTP_CLOCK_GRANULARITY; }
2386 * new_rto = net->lastsa + 4 * net->lastsv;
2389 o_calctime = calc_time;
2390 /* this is Van Jacobson's integer version */
2392 calc_time -= (net->lastsa >> 3);
2393 if ((int)net->prev_rtt > o_calctime) {
2394 net->rtt_variance = net->prev_rtt - o_calctime;
2396 net->rto_variance_dir = 0;
2399 net->rtt_variance = o_calctime - net->prev_rtt;
2400 net->rto_variance_dir = 1;
2402 #ifdef SCTP_RTTVAR_LOGGING
2403 rto_logging(net, SCTP_LOG_RTTVAR);
2405 net->prev_rtt = o_calctime;
2406 net->lastsa += calc_time;
2407 if (calc_time < 0) {
2408 calc_time = -calc_time;
2410 calc_time -= (net->lastsv >> 2);
2411 net->lastsv += calc_time;
2412 if (net->lastsv == 0) {
2413 net->lastsv = SCTP_CLOCK_GRANULARITY;
2416 /* First RTO measurment */
2417 net->lastsa = calc_time;
2418 net->lastsv = calc_time >> 1;
2420 net->rto_variance_dir = 1;
2421 net->prev_rtt = o_calctime;
2422 net->rtt_variance = 0;
2423 #ifdef SCTP_RTTVAR_LOGGING
2424 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2427 new_rto = ((net->lastsa >> 2) + net->lastsv) >> 1;
2428 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2429 (stcb->asoc.sat_network_lockout == 0)) {
2430 stcb->asoc.sat_network = 1;
2431 } else if ((!first_measure) && stcb->asoc.sat_network) {
2432 stcb->asoc.sat_network = 0;
2433 stcb->asoc.sat_network_lockout = 1;
2435 /* bound it, per C6/C7 in Section 5.3.1 */
2436 if (new_rto < stcb->asoc.minrto) {
2437 new_rto = stcb->asoc.minrto;
2439 if (new_rto > stcb->asoc.maxrto) {
2440 new_rto = stcb->asoc.maxrto;
2442 /* we are now returning the RTT Smoothed */
2443 return ((uint32_t) new_rto);
2448 * return a pointer to a contiguous piece of data from the given mbuf chain
2449 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2450 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2451 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2454 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2460 if ((off < 0) || (len <= 0))
2463 /* find the desired start location */
2464 while ((m != NULL) && (off > 0)) {
2473 /* is the current mbuf large enough (eg. contiguous)? */
2474 if ((m->m_len - off) >= len) {
2475 return (mtod(m, caddr_t)+off);
2477 /* else, it spans more than one mbuf, so save a temp copy... */
2478 while ((m != NULL) && (len > 0)) {
2479 count = min(m->m_len - off, len);
2480 bcopy(mtod(m, caddr_t)+off, ptr, count);
2486 if ((m == NULL) && (len > 0))
2489 return ((caddr_t)in_ptr);
2494 struct sctp_paramhdr *
2495 sctp_get_next_param(struct mbuf *m,
2497 struct sctp_paramhdr *pull,
2500 /* This just provides a typed signature to Peter's Pull routine */
2501 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2507 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2510 * add padlen bytes of 0 filled padding to the end of the mbuf. If
2511 * padlen is > 3 this routine will fail.
2519 if (M_TRAILINGSPACE(m)) {
2521 * The easy way. We hope the majority of the time we hit
2524 dp = (uint8_t *) (mtod(m, caddr_t)+m->m_len);
2527 /* Hard way we must grow the mbuf */
2530 tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA);
2532 /* Out of space GAK! we are in big trouble. */
2535 /* setup and insert in middle */
2536 tmp->m_next = m->m_next;
2537 tmp->m_len = padlen;
2539 dp = mtod(tmp, uint8_t *);
2541 /* zero out the pad */
2542 for (i = 0; i < padlen; i++) {
2550 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2552 /* find the last mbuf in chain and pad it */
2557 return (sctp_add_pad_tombuf(last_mbuf, padval));
2560 if (m_at->m_next == NULL) {
2561 return (sctp_add_pad_tombuf(m_at, padval));
2563 m_at = m_at->m_next;
2569 int sctp_asoc_change_wake = 0;
2572 sctp_notify_assoc_change(uint32_t event, struct sctp_tcb *stcb,
2573 uint32_t error, void *data)
2575 struct mbuf *m_notify;
2576 struct sctp_assoc_change *sac;
2577 struct sctp_queued_to_read *control;
2581 * First if we are are going down dump everything we can to the
2585 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
2586 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
2587 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
2589 /* If the socket is gone we are out of here */
2592 if ((event == SCTP_COMM_LOST) || (event == SCTP_SHUTDOWN_COMP)) {
2593 if (stcb->asoc.control_pdapi) {
2595 * we were in the middle of a PD-API verify its
2598 SCTP_INP_READ_LOCK(stcb->sctp_ep);
2600 TAILQ_FOREACH(control, &stcb->sctp_ep->read_queue, next) {
2601 if (control == stcb->asoc.control_pdapi) {
2602 /* Yep its here, notify them */
2603 if (event == SCTP_COMM_LOST) {
2605 * Abort/broken we had a
2606 * real PD-API aborted
2608 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_PDAPIEVNT)) {
2617 if (control->held_length) {
2618 control->held_length = 0;
2619 control->end_added = 1;
2622 sctp_notify_partial_delivery_indication(stcb, event, 1);
2626 /* implicit EOR on EOF */
2627 control->held_length = 0;
2628 control->end_added = 1;
2630 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
2633 control->do_not_ref_stcb = 1;
2634 stcb->asoc.control_pdapi = NULL;
2635 sorwakeup(stcb->sctp_socket);
2640 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
2645 * For TCP model AND UDP connected sockets we will send an error up
2646 * when an ABORT comes in.
2648 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2649 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2650 (event == SCTP_COMM_LOST)) {
2651 if (TAILQ_EMPTY(&stcb->sctp_ep->read_queue)) {
2652 stcb->sctp_socket->so_error = ECONNRESET;
2654 /* Wake ANY sleepers */
2655 sorwakeup(stcb->sctp_socket);
2656 sowwakeup(stcb->sctp_socket);
2657 sctp_asoc_change_wake++;
2659 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2660 /* event not enabled */
2663 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_change), 1, M_DONTWAIT, 1, MT_DATA);
2664 if (m_notify == NULL)
2667 m_notify->m_len = 0;
2669 sac = mtod(m_notify, struct sctp_assoc_change *);
2670 sac->sac_type = SCTP_ASSOC_CHANGE;
2672 sac->sac_length = sizeof(struct sctp_assoc_change);
2673 sac->sac_state = event;
2674 sac->sac_error = error;
2675 /* XXX verify these stream counts */
2676 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2677 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2678 sac->sac_assoc_id = sctp_get_associd(stcb);
2679 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
2680 m_notify->m_pkthdr.len = sizeof(struct sctp_assoc_change);
2681 m_notify->m_pkthdr.rcvif = 0;
2682 m_notify->m_len = sizeof(struct sctp_assoc_change);
2683 m_notify->m_next = NULL;
2684 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2687 if (control == NULL) {
2689 sctp_m_freem(m_notify);
2692 control->length = m_notify->m_len;
2693 /* not that we need this */
2694 control->tail_mbuf = m_notify;
2695 sctp_add_to_readq(stcb->sctp_ep, stcb,
2697 &stcb->sctp_socket->so_rcv, 1);
2698 if (event == SCTP_COMM_LOST) {
2699 /* Wake up any sleeper */
2700 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
2705 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2706 struct sockaddr *sa, uint32_t error)
2708 struct mbuf *m_notify;
2709 struct sctp_paddr_change *spc;
2710 struct sctp_queued_to_read *control;
2712 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVPADDREVNT))
2713 /* event not enabled */
2716 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 1, M_DONTWAIT, 1, MT_DATA);
2717 if (m_notify == NULL)
2719 m_notify->m_len = 0;
2720 spc = mtod(m_notify, struct sctp_paddr_change *);
2721 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2723 spc->spc_length = sizeof(struct sctp_paddr_change);
2724 if (sa->sa_family == AF_INET) {
2725 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2727 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2729 spc->spc_state = state;
2730 spc->spc_error = error;
2731 spc->spc_assoc_id = sctp_get_associd(stcb);
2733 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
2734 m_notify->m_pkthdr.len = sizeof(struct sctp_paddr_change);
2735 m_notify->m_pkthdr.rcvif = 0;
2736 m_notify->m_len = sizeof(struct sctp_paddr_change);
2737 m_notify->m_next = NULL;
2739 /* append to socket */
2740 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2743 if (control == NULL) {
2745 sctp_m_freem(m_notify);
2748 control->length = m_notify->m_len;
2749 /* not that we need this */
2750 control->tail_mbuf = m_notify;
2751 sctp_add_to_readq(stcb->sctp_ep, stcb,
2753 &stcb->sctp_socket->so_rcv, 1);
2758 sctp_notify_send_failed(struct sctp_tcb *stcb, uint32_t error,
2759 struct sctp_tmit_chunk *chk)
2761 struct mbuf *m_notify;
2762 struct sctp_send_failed *ssf;
2763 struct sctp_queued_to_read *control;
2766 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2767 /* event not enabled */
2770 length = sizeof(struct sctp_send_failed) + chk->send_size;
2771 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 1, M_DONTWAIT, 1, MT_DATA);
2772 if (m_notify == NULL)
2775 m_notify->m_len = 0;
2776 ssf = mtod(m_notify, struct sctp_send_failed *);
2777 ssf->ssf_type = SCTP_SEND_FAILED;
2778 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2779 ssf->ssf_flags = SCTP_DATA_UNSENT;
2781 ssf->ssf_flags = SCTP_DATA_SENT;
2782 ssf->ssf_length = length;
2783 ssf->ssf_error = error;
2784 /* not exactly what the user sent in, but should be close :) */
2785 ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2786 ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2787 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2788 ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2789 ssf->ssf_info.sinfo_context = chk->rec.data.context;
2790 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2791 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2792 m_notify->m_next = chk->data;
2793 m_notify->m_flags |= M_NOTIFICATION;
2794 m_notify->m_pkthdr.len = length;
2795 m_notify->m_pkthdr.rcvif = 0;
2796 m_notify->m_len = sizeof(struct sctp_send_failed);
2798 /* Steal off the mbuf */
2801 * For this case, we check the actual socket buffer, since the assoc
2802 * is going away we don't want to overfill the socket buffer for a
2805 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < m_notify->m_len) {
2806 sctp_m_freem(m_notify);
2809 /* append to socket */
2810 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2813 if (control == NULL) {
2815 sctp_m_freem(m_notify);
2818 sctp_add_to_readq(stcb->sctp_ep, stcb,
2820 &stcb->sctp_socket->so_rcv, 1);
2825 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
2826 struct sctp_stream_queue_pending *sp)
2828 struct mbuf *m_notify;
2829 struct sctp_send_failed *ssf;
2830 struct sctp_queued_to_read *control;
2833 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2834 /* event not enabled */
2837 length = sizeof(struct sctp_send_failed) + sp->length;
2838 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 1, M_DONTWAIT, 1, MT_DATA);
2839 if (m_notify == NULL)
2842 m_notify->m_len = 0;
2843 ssf = mtod(m_notify, struct sctp_send_failed *);
2844 ssf->ssf_type = SCTP_SEND_FAILED;
2845 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2846 ssf->ssf_flags = SCTP_DATA_UNSENT;
2848 ssf->ssf_flags = SCTP_DATA_SENT;
2849 ssf->ssf_length = length;
2850 ssf->ssf_error = error;
2851 /* not exactly what the user sent in, but should be close :) */
2852 ssf->ssf_info.sinfo_stream = sp->stream;
2853 ssf->ssf_info.sinfo_ssn = sp->strseq;
2854 ssf->ssf_info.sinfo_flags = sp->sinfo_flags;
2855 ssf->ssf_info.sinfo_ppid = sp->ppid;
2856 ssf->ssf_info.sinfo_context = sp->context;
2857 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2858 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2859 m_notify->m_next = sp->data;
2860 m_notify->m_flags |= M_NOTIFICATION;
2861 m_notify->m_pkthdr.len = length;
2862 m_notify->m_pkthdr.rcvif = 0;
2863 m_notify->m_len = sizeof(struct sctp_send_failed);
2865 /* Steal off the mbuf */
2868 * For this case, we check the actual socket buffer, since the assoc
2869 * is going away we don't want to overfill the socket buffer for a
2872 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < m_notify->m_len) {
2873 sctp_m_freem(m_notify);
2876 /* append to socket */
2877 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2880 if (control == NULL) {
2882 sctp_m_freem(m_notify);
2885 sctp_add_to_readq(stcb->sctp_ep, stcb,
2887 &stcb->sctp_socket->so_rcv, 1);
2893 sctp_notify_adaptation_layer(struct sctp_tcb *stcb,
2896 struct mbuf *m_notify;
2897 struct sctp_adaptation_event *sai;
2898 struct sctp_queued_to_read *control;
2900 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2901 /* event not enabled */
2904 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 1, M_DONTWAIT, 1, MT_DATA);
2905 if (m_notify == NULL)
2908 m_notify->m_len = 0;
2909 sai = mtod(m_notify, struct sctp_adaptation_event *);
2910 sai->sai_type = SCTP_ADAPTATION_INDICATION;
2912 sai->sai_length = sizeof(struct sctp_adaptation_event);
2913 sai->sai_adaptation_ind = error;
2914 sai->sai_assoc_id = sctp_get_associd(stcb);
2916 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
2917 m_notify->m_pkthdr.len = sizeof(struct sctp_adaptation_event);
2918 m_notify->m_pkthdr.rcvif = 0;
2919 m_notify->m_len = sizeof(struct sctp_adaptation_event);
2920 m_notify->m_next = NULL;
2922 /* append to socket */
2923 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2926 if (control == NULL) {
2928 sctp_m_freem(m_notify);
2931 control->length = m_notify->m_len;
2932 /* not that we need this */
2933 control->tail_mbuf = m_notify;
2934 sctp_add_to_readq(stcb->sctp_ep, stcb,
2936 &stcb->sctp_socket->so_rcv, 1);
2940 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb,
2941 uint32_t error, int no_lock)
2943 struct mbuf *m_notify;
2944 struct sctp_pdapi_event *pdapi;
2945 struct sctp_queued_to_read *control;
2947 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_PDAPIEVNT))
2948 /* event not enabled */
2951 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 1, M_DONTWAIT, 1, MT_DATA);
2952 if (m_notify == NULL)
2955 m_notify->m_len = 0;
2956 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
2957 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
2958 pdapi->pdapi_flags = 0;
2959 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
2960 pdapi->pdapi_indication = error;
2961 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
2963 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
2964 m_notify->m_pkthdr.len = sizeof(struct sctp_pdapi_event);
2965 m_notify->m_pkthdr.rcvif = 0;
2966 m_notify->m_len = sizeof(struct sctp_pdapi_event);
2967 m_notify->m_next = NULL;
2969 if (stcb->asoc.control_pdapi != NULL) {
2970 /* we will do some substitution */
2971 control = stcb->asoc.control_pdapi;
2973 SCTP_INP_READ_LOCK(stcb->sctp_ep);
2975 if (control->data == NULL) {
2976 control->data = control->tail_mbuf = m_notify;
2977 control->held_length = 0;
2978 control->length = m_notify->m_len;
2979 control->end_added = 1;
2981 &stcb->sctp_socket->so_rcv,
2983 } else if (control->end_added == 0) {
2984 struct mbuf *m = NULL;
2988 sctp_sbfree(control, stcb,
2989 &stcb->sctp_socket->so_rcv, m);
2992 control->data = NULL;
2993 control->length = m_notify->m_len;
2994 control->data = control->tail_mbuf = m_notify;
2995 control->held_length = 0;
2996 control->end_added = 1;
2997 sctp_sballoc(stcb, &stcb->sctp_socket->so_rcv, m);
2999 /* Hmm .. should not happen */
3000 control->end_added = 1;
3001 stcb->asoc.control_pdapi = NULL;
3005 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
3007 /* 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);
3027 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3029 struct mbuf *m_notify;
3030 struct sctp_shutdown_event *sse;
3031 struct sctp_queued_to_read *control;
3034 * For TCP model AND UDP connected sockets we will send an error up
3035 * when an SHUTDOWN completes
3037 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3038 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3039 /* mark socket closed for read/write and wakeup! */
3040 socantsendmore(stcb->sctp_socket);
3042 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
3043 /* event not enabled */
3046 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 1, M_DONTWAIT, 1, MT_DATA);
3047 if (m_notify == NULL)
3050 m_notify->m_len = 0;
3051 sse = mtod(m_notify, struct sctp_shutdown_event *);
3052 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3054 sse->sse_length = sizeof(struct sctp_shutdown_event);
3055 sse->sse_assoc_id = sctp_get_associd(stcb);
3057 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
3058 m_notify->m_pkthdr.len = sizeof(struct sctp_shutdown_event);
3059 m_notify->m_pkthdr.rcvif = 0;
3060 m_notify->m_len = sizeof(struct sctp_shutdown_event);
3061 m_notify->m_next = NULL;
3063 /* append to socket */
3064 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3067 if (control == NULL) {
3069 sctp_m_freem(m_notify);
3072 control->length = m_notify->m_len;
3073 /* not that we need this */
3074 control->tail_mbuf = m_notify;
3075 sctp_add_to_readq(stcb->sctp_ep, stcb,
3077 &stcb->sctp_socket->so_rcv, 1);
3081 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3082 int number_entries, uint16_t * list, int flag)
3084 struct mbuf *m_notify;
3085 struct sctp_queued_to_read *control;
3086 struct sctp_stream_reset_event *strreset;
3089 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
3090 /* event not enabled */
3093 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
3094 if (m_notify == NULL)
3097 m_notify->m_len = 0;
3098 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3099 if (len > M_TRAILINGSPACE(m_notify)) {
3100 /* never enough room */
3101 sctp_m_freem(m_notify);
3104 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3105 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3106 if (number_entries == 0) {
3107 strreset->strreset_flags = flag | SCTP_STRRESET_ALL_STREAMS;
3109 strreset->strreset_flags = flag | SCTP_STRRESET_STREAM_LIST;
3111 strreset->strreset_length = len;
3112 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3113 if (number_entries) {
3116 for (i = 0; i < number_entries; i++) {
3117 strreset->strreset_list[i] = ntohs(list[i]);
3120 m_notify->m_flags |= M_EOR | M_NOTIFICATION;
3121 m_notify->m_pkthdr.len = len;
3122 m_notify->m_pkthdr.rcvif = 0;
3123 m_notify->m_len = len;
3124 m_notify->m_next = NULL;
3125 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < m_notify->m_len) {
3127 sctp_m_freem(m_notify);
3130 /* append to socket */
3131 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3134 if (control == NULL) {
3136 sctp_m_freem(m_notify);
3139 control->length = m_notify->m_len;
3140 /* not that we need this */
3141 control->tail_mbuf = m_notify;
3142 sctp_add_to_readq(stcb->sctp_ep, stcb,
3144 &stcb->sctp_socket->so_rcv, 1);
3149 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3150 uint32_t error, void *data)
3152 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3153 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3154 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
3156 /* No notifications up when we are in a no socket state */
3159 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3160 /* Can't send up to a closed socket any notifications */
3163 if (stcb && (stcb->asoc.assoc_up_sent == 0) && (notification != SCTP_NOTIFY_ASSOC_UP)) {
3164 if ((notification != SCTP_NOTIFY_ASSOC_DOWN) &&
3165 (notification != SCTP_NOTIFY_ASSOC_ABORTED) &&
3166 (notification != SCTP_NOTIFY_SPECIAL_SP_FAIL) &&
3167 (notification != SCTP_NOTIFY_DG_FAIL) &&
3168 (notification != SCTP_NOTIFY_PEER_SHUTDOWN)) {
3169 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, 0, NULL);
3170 stcb->asoc.assoc_up_sent = 1;
3173 switch (notification) {
3174 case SCTP_NOTIFY_ASSOC_UP:
3175 if (stcb->asoc.assoc_up_sent == 0) {
3176 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL);
3177 stcb->asoc.assoc_up_sent = 1;
3180 case SCTP_NOTIFY_ASSOC_DOWN:
3181 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL);
3183 case SCTP_NOTIFY_INTERFACE_DOWN:
3185 struct sctp_nets *net;
3187 net = (struct sctp_nets *)data;
3188 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3189 (struct sockaddr *)&net->ro._l_addr, error);
3192 case SCTP_NOTIFY_INTERFACE_UP:
3194 struct sctp_nets *net;
3196 net = (struct sctp_nets *)data;
3197 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3198 (struct sockaddr *)&net->ro._l_addr, error);
3201 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3203 struct sctp_nets *net;
3205 net = (struct sctp_nets *)data;
3206 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3207 (struct sockaddr *)&net->ro._l_addr, error);
3210 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3211 sctp_notify_send_failed2(stcb, error,
3212 (struct sctp_stream_queue_pending *)data);
3214 case SCTP_NOTIFY_DG_FAIL:
3215 sctp_notify_send_failed(stcb, error,
3216 (struct sctp_tmit_chunk *)data);
3218 case SCTP_NOTIFY_ADAPTATION_INDICATION:
3219 /* Here the error is the adaptation indication */
3220 sctp_notify_adaptation_layer(stcb, error);
3222 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3223 sctp_notify_partial_delivery_indication(stcb, error, 0);
3225 case SCTP_NOTIFY_STRDATA_ERR:
3227 case SCTP_NOTIFY_ASSOC_ABORTED:
3228 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, NULL);
3230 case SCTP_NOTIFY_PEER_OPENED_STREAM:
3232 case SCTP_NOTIFY_STREAM_OPENED_OK:
3234 case SCTP_NOTIFY_ASSOC_RESTART:
3235 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, data);
3237 case SCTP_NOTIFY_HB_RESP:
3239 case SCTP_NOTIFY_STR_RESET_SEND:
3240 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_OUTBOUND_STR);
3242 case SCTP_NOTIFY_STR_RESET_RECV:
3243 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_INBOUND_STR);
3245 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3246 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_OUTBOUND_STR | SCTP_STRRESET_INBOUND_STR));
3249 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3250 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_INBOUND_STR | SCTP_STRRESET_INBOUND_STR));
3253 case SCTP_NOTIFY_ASCONF_ADD_IP:
3254 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3257 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3258 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3261 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3262 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3265 case SCTP_NOTIFY_ASCONF_SUCCESS:
3267 case SCTP_NOTIFY_ASCONF_FAILED:
3269 case SCTP_NOTIFY_PEER_SHUTDOWN:
3270 sctp_notify_shutdown_event(stcb);
3272 case SCTP_NOTIFY_AUTH_NEW_KEY:
3273 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, error,
3277 case SCTP_NOTIFY_AUTH_KEY_CONFLICT:
3278 sctp_notify_authentication(stcb, SCTP_AUTH_KEY_CONFLICT,
3279 error, (uint32_t) data);
3281 #endif /* not yet? remove? */
3286 if (sctp_debug_on & SCTP_DEBUG_UTIL1) {
3287 printf("NOTIFY: unknown notification %xh (%u)\n",
3288 notification, notification);
3290 #endif /* SCTP_DEBUG */
3296 sctp_report_all_outbound(struct sctp_tcb *stcb)
3298 struct sctp_association *asoc;
3299 struct sctp_stream_out *outs;
3300 struct sctp_tmit_chunk *chk;
3301 struct sctp_stream_queue_pending *sp;
3305 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3306 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3307 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3310 /* now through all the gunk freeing chunks */
3312 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
3313 /* now clean up any chunks here */
3314 stcb->asoc.locked_on_sending = NULL;
3315 sp = TAILQ_FIRST(&outs->outqueue);
3317 stcb->asoc.stream_queue_cnt--;
3318 TAILQ_REMOVE(&outs->outqueue, sp, next);
3319 sctp_free_spbufspace(stcb, asoc, sp);
3320 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3321 SCTP_NOTIFY_DATAGRAM_UNSENT, (void *)sp);
3323 sctp_m_freem(sp->data);
3327 sctp_free_remote_addr(sp->net);
3329 /* Free the chunk */
3330 sctp_free_a_strmoq(stcb, sp);
3331 sp = TAILQ_FIRST(&outs->outqueue);
3335 /* pending send queue SHOULD be empty */
3336 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3337 chk = TAILQ_FIRST(&asoc->send_queue);
3339 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3342 * trim off the sctp chunk header(it should
3345 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3346 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3347 sctp_mbuf_crush(chk->data);
3350 sctp_free_bufspace(stcb, asoc, chk, 1);
3351 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, SCTP_NOTIFY_DATAGRAM_UNSENT, chk);
3353 sctp_m_freem(chk->data);
3357 sctp_free_remote_addr(chk->whoTo);
3359 sctp_free_a_chunk(stcb, chk);
3360 chk = TAILQ_FIRST(&asoc->send_queue);
3363 /* sent queue SHOULD be empty */
3364 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3365 chk = TAILQ_FIRST(&asoc->sent_queue);
3367 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3370 * trim off the sctp chunk header(it should
3373 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3374 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3375 sctp_mbuf_crush(chk->data);
3378 sctp_free_bufspace(stcb, asoc, chk, 1);
3379 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3380 SCTP_NOTIFY_DATAGRAM_SENT, chk);
3382 sctp_m_freem(chk->data);
3386 sctp_free_remote_addr(chk->whoTo);
3388 sctp_free_a_chunk(stcb, chk);
3389 chk = TAILQ_FIRST(&asoc->sent_queue);
3395 sctp_abort_notification(struct sctp_tcb *stcb, int error)
3398 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3399 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3400 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3403 /* Tell them we lost the asoc */
3404 sctp_report_all_outbound(stcb);
3405 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3406 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3407 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3408 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3410 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_ABORTED, stcb, error, NULL);
3414 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3415 struct mbuf *m, int iphlen, struct sctphdr *sh, struct mbuf *op_err)
3421 /* We have a TCB to abort, send notification too */
3422 vtag = stcb->asoc.peer_vtag;
3423 sctp_abort_notification(stcb, 0);
3425 sctp_send_abort(m, iphlen, sh, vtag, op_err);
3427 /* Ok, now lets free it */
3428 sctp_free_assoc(inp, stcb, 0);
3430 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3431 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3432 sctp_inpcb_free(inp, 1, 0);
3439 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3440 int error, struct mbuf *op_err)
3445 /* Got to have a TCB */
3446 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3447 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3448 sctp_inpcb_free(inp, 1, 0);
3453 vtag = stcb->asoc.peer_vtag;
3454 /* notify the ulp */
3455 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)
3456 sctp_abort_notification(stcb, error);
3457 /* notify the peer */
3458 sctp_send_abort_tcb(stcb, op_err);
3459 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3460 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3461 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3462 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3464 /* now free the asoc */
3465 sctp_free_assoc(inp, stcb, 0);
3469 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
3470 struct sctp_inpcb *inp, struct mbuf *op_err)
3472 struct sctp_chunkhdr *ch, chunk_buf;
3473 unsigned int chk_length;
3475 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
3476 /* Generate a TO address for future reference */
3477 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3478 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3479 sctp_inpcb_free(inp, 1, 0);
3482 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3483 sizeof(*ch), (uint8_t *) & chunk_buf);
3484 while (ch != NULL) {
3485 chk_length = ntohs(ch->chunk_length);
3486 if (chk_length < sizeof(*ch)) {
3487 /* break to abort land */
3490 switch (ch->chunk_type) {
3491 case SCTP_PACKET_DROPPED:
3492 /* we don't respond to pkt-dropped */
3494 case SCTP_ABORT_ASSOCIATION:
3495 /* we don't respond with an ABORT to an ABORT */
3497 case SCTP_SHUTDOWN_COMPLETE:
3499 * we ignore it since we are not waiting for it and
3503 case SCTP_SHUTDOWN_ACK:
3504 sctp_send_shutdown_complete2(m, iphlen, sh);
3509 offset += SCTP_SIZE32(chk_length);
3510 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3511 sizeof(*ch), (uint8_t *) & chunk_buf);
3513 sctp_send_abort(m, iphlen, sh, 0, op_err);
3517 * check the inbound datagram to make sure there is not an abort inside it,
3518 * if there is return 1, else return 0.
3521 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
3523 struct sctp_chunkhdr *ch;
3524 struct sctp_init_chunk *init_chk, chunk_buf;
3526 unsigned int chk_length;
3528 offset = iphlen + sizeof(struct sctphdr);
3529 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
3530 (uint8_t *) & chunk_buf);
3531 while (ch != NULL) {
3532 chk_length = ntohs(ch->chunk_length);
3533 if (chk_length < sizeof(*ch)) {
3534 /* packet is probably corrupt */
3537 /* we seem to be ok, is it an abort? */
3538 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
3539 /* yep, tell them */
3542 if (ch->chunk_type == SCTP_INITIATION) {
3543 /* need to update the Vtag */
3544 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
3545 offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
3546 if (init_chk != NULL) {
3547 *vtagfill = ntohl(init_chk->init.initiate_tag);
3550 /* Nope, move to the next chunk */
3551 offset += SCTP_SIZE32(chk_length);
3552 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3553 sizeof(*ch), (uint8_t *) & chunk_buf);
3559 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
3560 * set (i.e. it's 0) so, create this function to compare link local scopes
3563 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
3565 struct sockaddr_in6 a, b;
3571 if (a.sin6_scope_id == 0)
3572 if (sa6_recoverscope(&a)) {
3573 /* can't get scope, so can't match */
3576 if (b.sin6_scope_id == 0)
3577 if (sa6_recoverscope(&b)) {
3578 /* can't get scope, so can't match */
3581 if (a.sin6_scope_id != b.sin6_scope_id)
3588 * returns a sockaddr_in6 with embedded scope recovered and removed
3590 struct sockaddr_in6 *
3591 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
3594 /* check and strip embedded scope junk */
3595 if (addr->sin6_family == AF_INET6) {
3596 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
3597 if (addr->sin6_scope_id == 0) {
3599 if (!sa6_recoverscope(store)) {
3600 /* use the recovered scope */
3603 /* else, return the original "to" addr */
3611 * are the two addresses the same? currently a "scopeless" check returns: 1
3615 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
3619 if (sa1 == NULL || sa2 == NULL)
3622 /* must be the same family */
3623 if (sa1->sa_family != sa2->sa_family)
3626 if (sa1->sa_family == AF_INET6) {
3627 /* IPv6 addresses */
3628 struct sockaddr_in6 *sin6_1, *sin6_2;
3630 sin6_1 = (struct sockaddr_in6 *)sa1;
3631 sin6_2 = (struct sockaddr_in6 *)sa2;
3632 return (SCTP6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr,
3633 &sin6_2->sin6_addr));
3634 } else if (sa1->sa_family == AF_INET) {
3635 /* IPv4 addresses */
3636 struct sockaddr_in *sin_1, *sin_2;
3638 sin_1 = (struct sockaddr_in *)sa1;
3639 sin_2 = (struct sockaddr_in *)sa2;
3640 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
3642 /* we don't do these... */
3648 sctp_print_address(struct sockaddr *sa)
3651 if (sa->sa_family == AF_INET6) {
3652 struct sockaddr_in6 *sin6;
3654 sin6 = (struct sockaddr_in6 *)sa;
3655 printf("IPv6 address: %s:%d scope:%u\n",
3656 ip6_sprintf(&sin6->sin6_addr), ntohs(sin6->sin6_port),
3657 sin6->sin6_scope_id);
3658 } else if (sa->sa_family == AF_INET) {
3659 struct sockaddr_in *sin;
3662 sin = (struct sockaddr_in *)sa;
3663 p = (unsigned char *)&sin->sin_addr;
3664 printf("IPv4 address: %u.%u.%u.%u:%d\n",
3665 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
3672 sctp_print_address_pkt(struct ip *iph, struct sctphdr *sh)
3674 if (iph->ip_v == IPVERSION) {
3675 struct sockaddr_in lsa, fsa;
3677 bzero(&lsa, sizeof(lsa));
3678 lsa.sin_len = sizeof(lsa);
3679 lsa.sin_family = AF_INET;
3680 lsa.sin_addr = iph->ip_src;
3681 lsa.sin_port = sh->src_port;
3682 bzero(&fsa, sizeof(fsa));
3683 fsa.sin_len = sizeof(fsa);
3684 fsa.sin_family = AF_INET;
3685 fsa.sin_addr = iph->ip_dst;
3686 fsa.sin_port = sh->dest_port;
3688 sctp_print_address((struct sockaddr *)&lsa);
3690 sctp_print_address((struct sockaddr *)&fsa);
3691 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3692 struct ip6_hdr *ip6;
3693 struct sockaddr_in6 lsa6, fsa6;
3695 ip6 = (struct ip6_hdr *)iph;
3696 bzero(&lsa6, sizeof(lsa6));
3697 lsa6.sin6_len = sizeof(lsa6);
3698 lsa6.sin6_family = AF_INET6;
3699 lsa6.sin6_addr = ip6->ip6_src;
3700 lsa6.sin6_port = sh->src_port;
3701 bzero(&fsa6, sizeof(fsa6));
3702 fsa6.sin6_len = sizeof(fsa6);
3703 fsa6.sin6_family = AF_INET6;
3704 fsa6.sin6_addr = ip6->ip6_dst;
3705 fsa6.sin6_port = sh->dest_port;
3707 sctp_print_address((struct sockaddr *)&lsa6);
3709 sctp_print_address((struct sockaddr *)&fsa6);
3713 #if defined(HAVE_SCTP_SO_LASTRECORD)
3715 /* cloned from uipc_socket.c */
3717 #define SCTP_SBLINKRECORD(sb, m0) do { \
3718 if ((sb)->sb_lastrecord != NULL) \
3719 (sb)->sb_lastrecord->m_nextpkt = (m0); \
3721 (sb)->sb_mb = (m0); \
3722 (sb)->sb_lastrecord = (m0); \
3723 } while (/*CONSTCOND*/0)
3727 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
3728 struct sctp_inpcb *new_inp,
3729 struct sctp_tcb *stcb)
3732 * go through our old INP and pull off any control structures that
3733 * belong to stcb and move then to the new inp.
3735 struct socket *old_so, *new_so;
3736 struct sctp_queued_to_read *control, *nctl;
3737 struct sctp_readhead tmp_queue;
3741 old_so = old_inp->sctp_socket;
3742 new_so = new_inp->sctp_socket;
3743 TAILQ_INIT(&tmp_queue);
3745 SOCKBUF_LOCK(&(old_so->so_rcv));
3747 error = sblock(&old_so->so_rcv, 0);
3749 SOCKBUF_UNLOCK(&(old_so->so_rcv));
3752 * Gak, can't get sblock, we have a problem. data will be
3753 * left stranded.. and we don't dare look at it since the
3754 * other thread may be reading something. Oh well, its a
3755 * screwed up app that does a peeloff OR a accept while
3756 * reading from the main socket... actually its only the
3757 * peeloff() case, since I think read will fail on a
3758 * listening socket..
3762 /* lock the socket buffers */
3763 SCTP_INP_READ_LOCK(old_inp);
3764 control = TAILQ_FIRST(&old_inp->read_queue);
3765 /* Pull off all for out target stcb */
3767 nctl = TAILQ_NEXT(control, next);
3768 if (control->stcb == stcb) {
3769 /* remove it we want it */
3770 TAILQ_REMOVE(&old_inp->read_queue, control, next);
3771 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
3774 #ifdef SCTP_SB_LOGGING
3775 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, m->m_len);
3777 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
3778 #ifdef SCTP_SB_LOGGING
3779 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3786 SCTP_INP_READ_UNLOCK(old_inp);
3788 /* Remove the sb-lock on the old socket */
3789 SOCKBUF_LOCK(&(old_so->so_rcv));
3791 sbunlock(&old_so->so_rcv);
3792 SOCKBUF_UNLOCK(&(old_so->so_rcv));
3794 /* Now we move them over to the new socket buffer */
3795 control = TAILQ_FIRST(&tmp_queue);
3796 SCTP_INP_READ_LOCK(new_inp);
3798 nctl = TAILQ_NEXT(control, next);
3799 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
3802 #ifdef SCTP_SB_LOGGING
3803 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, m->m_len);
3805 sctp_sballoc(stcb, &new_so->so_rcv, m);
3806 #ifdef SCTP_SB_LOGGING
3807 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3813 SCTP_INP_READ_UNLOCK(new_inp);
3818 sctp_add_to_readq(struct sctp_inpcb *inp,
3819 struct sctp_tcb *stcb,
3820 struct sctp_queued_to_read *control,
3825 * Here we must place the control on the end of the socket read
3826 * queue AND increment sb_cc so that select will work properly on
3829 struct mbuf *m, *prev = NULL;
3831 SCTP_INP_READ_LOCK(inp);
3833 control->held_length = 0;
3834 control->length = 0;
3836 if (m->m_len == 0) {
3837 /* Skip mbufs with NO length */
3840 control->data = sctp_m_free(m);
3843 prev->m_next = sctp_m_free(m);
3847 control->tail_mbuf = prev;;
3852 #ifdef SCTP_SB_LOGGING
3853 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, m->m_len);
3855 sctp_sballoc(stcb, sb, m);
3856 #ifdef SCTP_SB_LOGGING
3857 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3859 atomic_add_int(&control->length, m->m_len);
3863 control->tail_mbuf = prev;
3865 prev->m_flags |= M_EOR;
3871 control->end_added = 1;
3873 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
3874 SCTP_INP_READ_UNLOCK(inp);
3875 if (inp && inp->sctp_socket) {
3876 sctp_sorwakeup(inp, inp->sctp_socket);
3882 sctp_append_to_readq(struct sctp_inpcb *inp,
3883 struct sctp_tcb *stcb,
3884 struct sctp_queued_to_read *control,
3891 * A partial delivery API event is underway. OR we are appending on
3892 * the reassembly queue.
3894 * If PDAPI this means we need to add m to the end of the data.
3895 * Increase the length in the control AND increment the sb_cc.
3896 * Otherwise sb is NULL and all we need to do is put it at the end
3897 * of the mbuf chain.
3900 struct mbuf *mm, *tail = NULL, *prev = NULL;
3903 SCTP_INP_READ_LOCK(inp);
3905 if (control == NULL) {
3908 SCTP_INP_READ_UNLOCK(inp);
3912 if ((control->tail_mbuf) &&
3913 (control->tail_mbuf->m_flags & M_EOR)) {
3914 /* huh this one is complete? */
3922 if (mm->m_len == 0) {
3923 /* Skip mbufs with NO lenght */
3926 m = sctp_m_free(mm);
3929 prev->m_next = sctp_m_free(mm);
3937 #ifdef SCTP_SB_LOGGING
3938 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, mm->m_len);
3940 sctp_sballoc(stcb, sb, mm);
3941 #ifdef SCTP_SB_LOGGING
3942 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3950 /* Really there should always be a prev */
3952 /* Huh nothing left? */
3954 panic("Nothing left to add?");
3962 /* message is complete */
3963 tail->m_flags |= M_EOR;
3964 if (control == stcb->asoc.control_pdapi) {
3965 stcb->asoc.control_pdapi = NULL;
3967 control->held_length = 0;
3968 control->end_added = 1;
3970 atomic_add_int(&control->length, len);
3971 if (control->tail_mbuf) {
3973 control->tail_mbuf->m_next = m;
3974 control->tail_mbuf = tail;
3978 if (control->data != NULL) {
3979 panic("This should NOT happen");
3983 control->tail_mbuf = tail;
3986 * When we are appending in partial delivery, the cum-ack is used
3987 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
3988 * is populated in the outbound sinfo structure from the true cumack
3989 * if the association exists...
3991 control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
3993 SCTP_INP_READ_UNLOCK(inp);
3995 if (inp && inp->sctp_socket) {
3996 sctp_sorwakeup(inp, inp->sctp_socket);
4003 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4004 *************ALTERNATE ROUTING CODE
4007 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4008 *************ALTERNATE ROUTING CODE
4012 sctp_generate_invmanparam(int err)
4014 /* Return a MBUF with a invalid mandatory parameter */
4017 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
4019 struct sctp_paramhdr *ph;
4021 m->m_len = sizeof(struct sctp_paramhdr);
4022 ph = mtod(m, struct sctp_paramhdr *);
4023 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4024 ph->param_type = htons(err);
4029 #ifdef SCTP_MBCNT_LOGGING
4031 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4032 struct sctp_tmit_chunk *tp1, int chk_cnt)
4034 if (tp1->data == NULL) {
4037 asoc->chunks_on_out_queue -= chk_cnt;
4038 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4039 asoc->total_output_queue_size,
4043 if (asoc->total_output_queue_size >= tp1->book_size) {
4044 asoc->total_output_queue_size -= tp1->book_size;
4046 asoc->total_output_queue_size = 0;
4049 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4050 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4051 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4052 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4054 stcb->sctp_socket->so_snd.sb_cc = 0;
4063 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4064 int reason, struct sctpchunk_listhead *queue)
4068 uint8_t foundeom = 0;
4071 ret_sz += tp1->book_size;
4072 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4074 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4075 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1);
4076 sctp_m_freem(tp1->data);
4078 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4080 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4081 stcb->asoc.sent_queue_cnt_removeable--;
4083 if (queue == &stcb->asoc.send_queue) {
4084 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4085 /* on to the sent queue */
4086 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4088 stcb->asoc.sent_queue_cnt++;
4090 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4091 SCTP_DATA_NOT_FRAG) {
4092 /* not frag'ed we ae done */
4095 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4096 /* end of frag, we are done */
4101 * Its a begin or middle piece, we must mark all of
4105 tp1 = TAILQ_NEXT(tp1, sctp_next);
4107 } while (tp1 && notdone);
4108 if ((foundeom == 0) && (queue == &stcb->asoc.sent_queue)) {
4110 * The multi-part message was scattered across the send and
4113 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
4115 * recurse throught the send_queue too, starting at the
4119 ret_sz += sctp_release_pr_sctp_chunk(stcb, tp1, reason,
4120 &stcb->asoc.send_queue);
4122 printf("hmm, nothing on the send queue and no EOM?\n");
4129 * checks to see if the given address, sa, is one that is currently known by
4130 * the kernel note: can't distinguish the same address on multiple interfaces
4131 * and doesn't handle multiple addresses with different zone/scope id's note:
4132 * ifa_ifwithaddr() compares the entire sockaddr struct
4135 sctp_find_ifa_by_addr(struct sockaddr *sa)
4140 /* go through all our known interfaces */
4141 TAILQ_FOREACH(ifn, &ifnet, if_list) {
4142 /* go through each interface addresses */
4143 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
4144 /* correct family? */
4145 if (ifa->ifa_addr->sa_family != sa->sa_family)
4149 if (ifa->ifa_addr->sa_family == AF_INET6) {
4151 struct sockaddr_in6 *sin1, *sin2, sin6_tmp;
4153 sin1 = (struct sockaddr_in6 *)ifa->ifa_addr;
4154 if (IN6_IS_SCOPE_LINKLOCAL(&sin1->sin6_addr)) {
4155 /* create a copy and clear scope */
4156 memcpy(&sin6_tmp, sin1,
4157 sizeof(struct sockaddr_in6));
4159 in6_clearscope(&sin1->sin6_addr);
4161 sin2 = (struct sockaddr_in6 *)sa;
4162 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4163 sizeof(struct in6_addr)) == 0) {
4169 if (ifa->ifa_addr->sa_family == AF_INET) {
4171 struct sockaddr_in *sin1, *sin2;
4173 sin1 = (struct sockaddr_in *)ifa->ifa_addr;
4174 sin2 = (struct sockaddr_in *)sa;
4175 if (sin1->sin_addr.s_addr ==
4176 sin2->sin_addr.s_addr) {
4181 /* else, not AF_INET or AF_INET6, so skip */
4182 } /* end foreach ifa */
4183 } /* end foreach ifn */
4196 sctp_user_rcvd(struct sctp_tcb *stcb, int *freed_so_far, int hold_rlock,
4199 /* User pulled some data, do we need a rwnd update? */
4201 int tcb_incr_up = 0;
4203 struct socket *so = NULL;
4208 atomic_add_16(&stcb->asoc.refcnt, 1);
4211 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4212 /* Pre-check If we are freeing no update */
4215 SCTP_INP_INCR_REF(stcb->sctp_ep);
4216 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4217 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4220 so = stcb->sctp_socket;
4224 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
4225 /* Have you have freed enough to look */
4226 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4227 sctp_misc_ints(SCTP_ENTER_USER_RECV,
4228 (stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd),
4230 stcb->freed_by_sorcv_sincelast,
4234 /* Yep, its worth a look and the lock overhead */
4236 /* Figure out what the rwnd would be */
4237 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
4238 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
4239 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
4243 if (dif >= rwnd_req) {
4245 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
4248 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4250 * One last check before we allow the guy possibly
4251 * to get in. There is a race, where the guy has not
4252 * reached the gate. In that case
4256 SCTP_TCB_LOCK(stcb);
4257 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4258 /* No reports here */
4259 SCTP_TCB_UNLOCK(stcb);
4262 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4263 sctp_misc_ints(SCTP_USER_RECV_SACKS,
4265 stcb->asoc.my_last_reported_rwnd,
4266 stcb->freed_by_sorcv_sincelast,
4269 SCTP_STAT_INCR(sctps_wu_sacks_sent);
4270 sctp_send_sack(stcb);
4271 sctp_chunk_output(stcb->sctp_ep, stcb,
4272 SCTP_OUTPUT_FROM_USR_RCVD);
4273 /* make sure no timer is running */
4274 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL);
4275 SCTP_TCB_UNLOCK(stcb);
4277 /* Update how much we have pending */
4278 stcb->freed_by_sorcv_sincelast = dif;
4279 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4280 sctp_misc_ints(SCTP_USER_RECV_SACKS,
4282 stcb->asoc.my_last_reported_rwnd,
4283 stcb->freed_by_sorcv_sincelast,
4288 if (so && r_unlocked && hold_rlock) {
4289 SCTP_STAT_INCR(sctps_locks_in_rcv);
4290 SCTP_INP_READ_LOCK(stcb->sctp_ep);
4292 SCTP_INP_DECR_REF(stcb->sctp_ep);
4295 atomic_add_16(&stcb->asoc.refcnt, -1);
4301 sctp_sorecvmsg(struct socket *so,
4304 struct sockaddr *from,
4307 struct sctp_sndrcvinfo *sinfo,
4311 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
4312 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
4313 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
4314 * On the way out we may send out any combination of:
4315 * MSG_NOTIFICATION MSG_EOR
4318 struct sctp_inpcb *inp = NULL;
4320 int cp_len = 0, error = 0;
4321 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
4322 struct mbuf *m = NULL, *embuf = NULL;
4323 struct sctp_tcb *stcb = NULL;
4324 int wakeup_read_socket = 0;
4325 int freecnt_applied = 0;
4326 int out_flags = 0, in_flags = 0;
4327 int block_allowed = 1;
4328 int freed_so_far = 0;
4329 int copied_so_far = 0;
4330 int s, in_eeor_mode = 0;
4331 int no_rcv_needed = 0;
4332 uint32_t rwnd_req = 0;
4333 int hold_sblock = 0;
4335 int alen = 0, slen = 0;
4336 int held_length = 0;
4339 in_flags = *msg_flags;
4343 slen = uio->uio_resid;
4344 /* Pull in and set up our int flags */
4345 if (in_flags & MSG_OOB) {
4346 /* Out of band's NOT supported */
4347 return (EOPNOTSUPP);
4349 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
4352 if ((in_flags & (MSG_DONTWAIT
4355 (so->so_state & SS_NBIO)) {
4358 /* setup the endpoint */
4359 inp = (struct sctp_inpcb *)so->so_pcb;
4364 rwnd_req = (so->so_rcv.sb_hiwat >> SCTP_RWND_HIWAT_SHIFT);
4365 /* Must be at least a MTU's worth */
4366 if (rwnd_req < SCTP_MIN_RWND)
4367 rwnd_req = SCTP_MIN_RWND;
4368 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
4369 #ifdef SCTP_RECV_RWND_LOGGING
4370 sctp_misc_ints(SCTP_SORECV_ENTER,
4371 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
4373 SOCKBUF_LOCK(&so->so_rcv);
4375 #ifdef SCTP_RECV_RWND_LOGGING
4376 sctp_misc_ints(SCTP_SORECV_ENTERPL,
4377 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
4381 error = sblock(&so->so_rcv, (block_allowed ? M_WAITOK : 0));
4383 goto release_unlocked;
4386 if (hold_sblock == 0) {
4387 SOCKBUF_LOCK(&so->so_rcv);
4390 sbunlock(&so->so_rcv);
4393 if (hold_sblock == 0) {
4394 SOCKBUF_LOCK(&so->so_rcv);
4397 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4398 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4401 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE) {
4403 error = so->so_error;
4409 if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
4410 /* we need to wait for data */
4411 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4412 sctp_misc_ints(SCTP_SORECV_BLOCKSA,
4413 0, 0, so->so_rcv.sb_cc, uio->uio_resid);
4415 if ((so->so_rcv.sb_cc == 0) &&
4416 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4417 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
4418 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
4420 * For active open side clear flags for
4421 * re-use passive open is blocked by
4424 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
4426 * You were aborted, passive side
4431 * You get this once if you are
4434 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4436 * Remove flag if on the
4439 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
4442 so->so_state &= ~(SS_ISCONNECTING |
4443 SS_ISDISCONNECTING |
4447 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
4450 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
4456 error = sbwait(&so->so_rcv);
4461 goto restart_nosblocks;
4462 } else if (so->so_rcv.sb_cc == 0) {
4463 error = EWOULDBLOCK;
4466 error = sblock(&so->so_rcv, (block_allowed ? M_WAITOK : 0));
4467 /* we possibly have data we can read */
4468 control = TAILQ_FIRST(&inp->read_queue);
4469 if (control == NULL) {
4471 * This could be happening since the appender did the
4472 * increment but as not yet did the tailq insert onto the
4475 if (hold_rlock == 0) {
4476 SCTP_INP_READ_LOCK(inp);
4479 control = TAILQ_FIRST(&inp->read_queue);
4480 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
4482 panic("Huh, its non zero and nothing on control?");
4484 so->so_rcv.sb_cc = 0;
4486 SCTP_INP_READ_UNLOCK(inp);
4490 if ((control->length == 0) &&
4491 (control->do_not_ref_stcb)) {
4493 * Clean up code for freeing assoc that left behind a
4494 * pdapi.. maybe a peer in EEOR that just closed after
4495 * sending and never indicated a EOR.
4497 SCTP_STAT_INCR(sctps_locks_in_rcva);
4498 if (hold_rlock == 0) {
4500 SCTP_INP_READ_LOCK(inp);
4502 control->held_length = 0;
4503 if (control->data) {
4504 /* Hmm there is data here .. fix */
4511 if (m->m_next == NULL) {
4512 control->tail_mbuf = m;
4513 m->m_flags |= M_EOR;
4514 control->end_added = 1;
4518 control->length = cnt;
4521 TAILQ_REMOVE(&inp->read_queue, control, next);
4522 /* Add back any hiddend data */
4523 sctp_free_remote_addr(control->whoFrom);
4524 sctp_free_a_readq(stcb, control);
4528 SCTP_INP_READ_UNLOCK(inp);
4532 if (control->length == 0) {
4533 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
4535 /* find a more suitable one then this */
4536 ctl = TAILQ_NEXT(control, next);
4538 if ((ctl->stcb != control->stcb) && (ctl->length)) {
4543 ctl = TAILQ_NEXT(ctl, next);
4547 * if we reach here, not suitable replacement is available
4548 * <or> fragment interleave is NOT on. So stuff the sb_cc
4549 * into the our held count, and its time to sleep again.
4551 held_length = so->so_rcv.sb_cc;
4552 control->held_length = so->so_rcv.sb_cc;
4555 /* Clear the held length since there is something to read */
4556 control->held_length = 0;
4558 SCTP_INP_READ_UNLOCK(inp);
4563 * If we reach here, control has a some data for us to read off.
4564 * Note that stcb COULD be NULL.
4567 SOCKBUF_UNLOCK(&so->so_rcv);
4570 stcb = control->stcb;
4572 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4573 (control->do_not_ref_stcb == 0)) {
4575 } else if (control->do_not_ref_stcb == 0) {
4576 /* you can't free it on me please */
4578 * The lock on the socket buffer protects us so the
4579 * free code will stop. But since we used the
4580 * socketbuf lock and the sender uses the tcb_lock
4581 * to increment, we need to use the atomic add to
4584 atomic_add_16(&stcb->asoc.refcnt, 1);
4585 freecnt_applied = 1;
4587 * Setup to remember how much we have not yet told
4588 * the peer our rwnd has opened up. Note we grab the
4589 * value from the tcb from last time. Note too that
4590 * sack sending clears this when a sack is sent..
4591 * which is fine. Once we hit the rwnd_req, we then
4592 * will go to the sctp_user_rcvd() that will not
4593 * lock until it KNOWs it MUST send a WUP-SACK.
4596 freed_so_far = stcb->freed_by_sorcv_sincelast;
4597 stcb->freed_by_sorcv_sincelast = 0;
4600 /* First lets get off the sinfo and sockaddr info */
4601 if ((sinfo) && filling_sinfo) {
4602 memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
4603 nxt = TAILQ_NEXT(control, next);
4604 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
4605 struct sctp_extrcvinfo *s_extra;
4607 s_extra = (struct sctp_extrcvinfo *)sinfo;
4609 s_extra->next_flags = SCTP_NEXT_MSG_AVAIL;
4610 if (nxt->sinfo_flags & SCTP_UNORDERED) {
4611 s_extra->next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
4613 s_extra->next_asocid = nxt->sinfo_assoc_id;
4614 s_extra->next_length = nxt->length;
4615 s_extra->next_ppid = nxt->sinfo_ppid;
4616 s_extra->next_stream = nxt->sinfo_stream;
4617 if (nxt->tail_mbuf != NULL) {
4618 if (nxt->tail_mbuf->m_flags & M_EOR) {
4619 s_extra->next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
4624 * we explicitly 0 this, since the memcpy
4625 * got some other things beyond the older
4626 * sinfo_ that is on the control's structure
4629 s_extra->next_flags = SCTP_NO_NEXT_MSG;
4630 s_extra->next_asocid = 0;
4631 s_extra->next_length = 0;
4632 s_extra->next_ppid = 0;
4633 s_extra->next_stream = 0;
4637 * update off the real current cum-ack, if we have an stcb.
4640 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4642 * mask off the high bits, we keep the actual chunk bits in
4645 sinfo->sinfo_flags &= 0x00ff;
4647 if (fromlen && from) {
4648 struct sockaddr *to;
4651 cp_len = min(fromlen, control->whoFrom->ro._l_addr.sin.sin_len);
4652 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
4653 ((struct sockaddr_in *)from)->sin_port = control->port_from;
4655 /* No AF_INET use AF_INET6 */
4656 cp_len = min(fromlen, control->whoFrom->ro._l_addr.sin6.sin6_len);
4657 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
4658 ((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
4662 #if defined(AF_INET) && defined(AF_INET6)
4663 if ((inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
4664 (to->sa_family == AF_INET) &&
4665 ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
4666 struct sockaddr_in *sin;
4667 struct sockaddr_in6 sin6;
4669 sin = (struct sockaddr_in *)to;
4670 bzero(&sin6, sizeof(sin6));
4671 sin6.sin6_family = AF_INET6;
4672 sin6.sin6_len = sizeof(struct sockaddr_in6);
4673 sin6.sin6_addr.s6_addr16[2] = 0xffff;
4674 bcopy(&sin->sin_addr,
4675 &sin6.sin6_addr.s6_addr16[3],
4676 sizeof(sin6.sin6_addr.s6_addr16[3]));
4677 sin6.sin6_port = sin->sin_port;
4678 memcpy(from, (caddr_t)&sin6, sizeof(sin6));
4681 #if defined(AF_INET6)
4683 struct sockaddr_in6 lsa6, *to6;
4685 to6 = (struct sockaddr_in6 *)to;
4686 sctp_recover_scope_mac(to6, (&lsa6));
4691 /* now copy out what data we can */
4693 /* copy out each mbuf in the chain up to length */
4697 /* Move out all we can */
4698 cp_len = (int)uio->uio_resid;
4699 my_len = (int)m->m_len;
4700 if (cp_len > my_len) {
4701 /* not enough in this buf */
4705 SCTP_INP_READ_UNLOCK(inp);
4710 error = uiomove(mtod(m, char *), cp_len, uio);
4712 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4713 sctp_misc_ints(SCTP_SORCV_DOESCPY,
4720 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4724 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4728 /* error we are out of here */
4731 if ((m->m_next == NULL) &&
4732 (cp_len >= m->m_len) &&
4733 ((control->end_added == 0) ||
4734 (control->end_added && (TAILQ_NEXT(control, next) == NULL)))
4736 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4737 sctp_misc_ints(SCTP_SORCV_DOESLCK,
4743 SCTP_STAT_INCR(sctps_locks_in_rcvb);
4744 SCTP_INP_READ_LOCK(inp);
4747 if (cp_len == m->m_len) {
4748 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4749 sctp_misc_ints(SCTP_SORCV_DOESADJ,
4755 if (m->m_flags & M_EOR) {
4756 out_flags |= MSG_EOR;
4758 if (m->m_flags & M_NOTIFICATION) {
4759 out_flags |= MSG_NOTIFICATION;
4761 /* we ate up the mbuf */
4762 if (in_flags & MSG_PEEK) {
4765 copied_so_far += cp_len;
4767 /* dispose of the mbuf */
4768 #ifdef SCTP_SB_LOGGING
4769 sctp_sblog(&so->so_rcv,
4770 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, m->m_len);
4772 sctp_sbfree(control, stcb, &so->so_rcv, m);
4773 #ifdef SCTP_SB_LOGGING
4774 sctp_sblog(&so->so_rcv,
4775 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4778 copied_so_far += cp_len;
4779 freed_so_far += cp_len;
4780 alen = atomic_fetchadd_int(&control->length, -(cp_len));
4781 if (alen < cp_len) {
4782 panic("Control length goes negative?");
4784 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4785 sctp_misc_ints(SCTP_SORCV_PASSBF,
4791 control->data = sctp_m_free(m);
4794 * been through it all, must hold sb
4795 * lock ok to null tail
4797 if (control->data == NULL) {
4799 if ((control->end_added == 0) ||
4800 (TAILQ_NEXT(control, next) == NULL)) {
4808 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
4809 panic("Hmm we don't own the lock?");
4813 control->tail_mbuf = NULL;
4815 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
4816 panic("end_added, nothing left and no MSG_EOR");
4820 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4821 sctp_misc_ints(SCTP_SORCV_ADJD,
4829 /* Do we need to trim the mbuf? */
4830 if (m->m_flags & M_NOTIFICATION) {
4831 out_flags |= MSG_NOTIFICATION;
4833 if ((in_flags & MSG_PEEK) == 0) {
4834 if (out_flags & MSG_NOTIFICATION) {
4836 * remark this one with the
4837 * notify flag, they read
4841 m->m_flags |= M_NOTIFICATION;
4843 m->m_data += cp_len;
4845 #ifdef SCTP_SB_LOGGING
4846 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
4848 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
4850 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
4852 copied_so_far += cp_len;
4854 freed_so_far += cp_len;
4855 #ifdef SCTP_SB_LOGGING
4856 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
4857 SCTP_LOG_SBRESULT, 0);
4859 alen = atomic_fetchadd_int(&control->length, -(cp_len));
4860 if (alen < cp_len) {
4861 panic("Control length goes negative2?");
4864 copied_so_far += cp_len;
4867 if ((out_flags & MSG_EOR) ||
4868 (uio->uio_resid == 0)
4872 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
4873 (control->do_not_ref_stcb == 0) &&
4874 (freed_so_far >= rwnd_req)) {
4875 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
4877 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4878 sctp_misc_ints(SCTP_SORCV_BOTWHILE,
4885 } /* end while(m) */
4887 * At this point we have looked at it all and we either have
4888 * a MSG_EOR/or read all the user wants... <OR>
4889 * control->length == 0.
4891 if ((out_flags & MSG_EOR) &&
4892 ((in_flags & MSG_PEEK) == 0)) {
4893 /* we are done with this control */
4894 if (control->length == 0) {
4895 if (control->data) {
4897 panic("control->data not null at read eor?");
4899 printf("Strange, data left in the control buffer .. invarients would panic?\n");
4900 sctp_m_freem(control->data);
4901 control->data = NULL;
4905 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
4906 sctp_misc_ints(SCTP_SORCV_FREECTL,
4912 if (TAILQ_NEXT(control, next) == NULL) {
4914 * If we don't have a next we need a
4915 * lock, if there is a next interupt
4916 * is filling ahead of us and we
4917 * don't need a lock to remove this
4918 * guy (which is the head of the
4921 if (hold_rlock == 0) {
4922 SCTP_STAT_INCR(sctps_locks_in_rcvc);
4923 SCTP_INP_READ_LOCK(inp);
4927 TAILQ_REMOVE(&inp->read_queue, control, next);
4928 /* Add back any hiddend data */
4929 if (control->held_length) {
4931 control->held_length = 0;
4932 wakeup_read_socket = 1;
4934 no_rcv_needed = control->do_not_ref_stcb;
4935 sctp_free_remote_addr(control->whoFrom);
4936 control->data = NULL;
4937 sctp_free_a_readq(stcb, control);
4939 if ((freed_so_far >= rwnd_req) && (no_rcv_needed == 0))
4940 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
4944 * The user did not read all of this
4945 * message, turn off the returned MSG_EOR
4946 * since we are leaving more behind on the
4950 if (control->end_added && (control->data == NULL) &&
4951 (control->tail_mbuf == NULL)) {
4952 panic("Gak, control->length is corrupt?");
4955 no_rcv_needed = control->do_not_ref_stcb;
4956 out_flags &= ~MSG_EOR;
4959 if (out_flags & MSG_EOR) {
4962 if ((uio->uio_resid == 0) ||
4963 ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))
4968 * If I hit here the receiver wants more and this message is
4969 * NOT done (pd-api). So two questions. Can we block? if not
4970 * we are done. Did the user NOT set MSG_WAITALL?
4972 if (block_allowed == 0) {
4976 * We need to wait for more data a few things: - We don't
4977 * sbunlock() so we don't get someone else reading. - We
4978 * must be sure to account for the case where what is added
4979 * is NOT to our control when we wakeup.
4983 * Do we need to tell the transport a rwnd update might be
4984 * needed before we go to sleep?
4986 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
4987 ((freed_so_far >= rwnd_req) &&
4988 (control->do_not_ref_stcb == 0) &&
4989 (no_rcv_needed == 0))) {
4990 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
4993 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE) {
4996 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
4999 if (hold_rlock == 1) {
5000 SCTP_INP_READ_UNLOCK(inp);
5003 if (hold_sblock == 0) {
5004 SOCKBUF_LOCK(&so->so_rcv);
5007 #ifdef SCTP_RECV_DETAIL_RWND_LOGGING
5009 sctp_misc_ints(SCTP_SORECV_BLOCKSB,
5015 sctp_misc_ints(SCTP_SORECV_BLOCKSB,
5021 if (so->so_rcv.sb_cc <= control->held_length) {
5022 error = sbwait(&so->so_rcv);
5026 control->held_length = 0;
5029 SOCKBUF_UNLOCK(&so->so_rcv);
5032 if (control->length == 0) {
5033 /* still nothing here */
5034 if (control->end_added == 1) {
5035 /* he aborted, or is done i.e.did a shutdown */
5036 out_flags |= MSG_EOR;
5037 goto done_with_control;
5039 if (so->so_rcv.sb_cc > held_length) {
5040 SCTP_STAT_INCR(sctps_locks_in_rcvf);
5041 control->held_length = so->so_rcv.sb_cc;
5044 goto wait_some_more;
5045 } else if (control->data == NULL) {
5046 panic("Impossible data==NULL length !=0");
5050 /* copy out the mbuf chain */
5053 * Do we have a uio, I doubt it if so we grab the size from
5054 * it, if not you get it all
5057 cp_len = uio->uio_resid;
5059 cp_len = control->length;
5061 if ((uint32_t) cp_len >= control->length) {
5063 if ((control->end_added == 0) ||
5064 (TAILQ_NEXT(control, next) == NULL)) {
5065 /* Need to get rlock */
5066 if (hold_rlock == 0) {
5067 SCTP_INP_READ_LOCK(inp);
5071 if (control->tail_mbuf->m_flags & M_EOR) {
5072 out_flags |= MSG_EOR;
5074 if (control->data->m_flags & M_NOTIFICATION) {
5075 out_flags |= MSG_NOTIFICATION;
5078 uio->uio_resid -= control->length;
5079 *mp = control->data;
5082 #ifdef SCTP_SB_LOGGING
5083 sctp_sblog(&so->so_rcv,
5084 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, m->m_len);
5086 sctp_sbfree(control, stcb, &so->so_rcv, m);
5087 freed_so_far += m->m_len;
5088 #ifdef SCTP_SB_LOGGING
5089 sctp_sblog(&so->so_rcv,
5090 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5094 control->data = control->tail_mbuf = NULL;
5095 control->length = 0;
5096 if (out_flags & MSG_EOR) {
5097 /* Done with this control */
5098 goto done_with_control;
5100 /* still more to do with this conntrol */
5101 /* do we really support msg_waitall here? */
5102 if ((block_allowed == 0) ||
5103 ((in_flags & MSG_WAITALL) == 0)) {
5107 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
5109 if (hold_rlock == 1) {
5110 SCTP_INP_READ_UNLOCK(inp);
5113 if (hold_sblock == 0) {
5114 SOCKBUF_LOCK(&so->so_rcv);
5117 if (so->so_rcv.sb_cc <= control->held_length) {
5118 error = sbwait(&so->so_rcv);
5124 SOCKBUF_UNLOCK(&so->so_rcv);
5127 if (control->length == 0) {
5128 /* still nothing here */
5129 if (control->end_added == 1) {
5131 * he aborted, or is done i.e.
5134 out_flags |= MSG_EOR;
5135 goto done_with_control;
5137 if (so->so_rcv.sb_cc > held_length) {
5138 control->held_length = so->so_rcv.sb_cc;
5140 * We don't use held_length while
5145 goto wait_some_more2;
5147 goto get_more_data2;
5149 /* hard way mbuf by mbuf */
5151 if (control->end_added == 0) {
5152 /* need the rlock */
5153 if (hold_rlock == 0) {
5154 SCTP_INP_READ_LOCK(inp);
5158 if (m->m_flags & M_NOTIFICATION) {
5159 out_flags |= MSG_NOTIFICATION;
5161 while ((m) && (cp_len > 0)) {
5162 if (cp_len >= m->m_len) {
5164 atomic_subtract_int(&control->length, m->m_len);
5166 uio->uio_resid -= m->m_len;
5168 control->data = m->m_next;
5170 #ifdef SCTP_SB_LOGGING
5171 sctp_sblog(&so->so_rcv,
5172 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, m->m_len);
5174 sctp_sbfree(control, stcb, &so->so_rcv, m);
5175 freed_so_far += m->m_len;
5176 #ifdef SCTP_SB_LOGGING
5177 sctp_sblog(&so->so_rcv,
5178 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5184 * got all he wants and its part of
5188 uio->uio_resid -= m->m_len;
5191 SCTP_INP_READ_UNLOCK(inp);
5195 SOCKBUF_UNLOCK(&so->so_rcv);
5199 *mp = sctp_m_copym(m, 0, cp_len,
5203 #ifdef SCTP_LOCK_LOGGING
5204 sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCKBUF_R);
5206 if (hold_sblock == 0) {
5207 SOCKBUF_LOCK(&so->so_rcv);
5210 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5214 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5217 m->m_data += cp_len;
5219 #ifdef SCTP_SB_LOGGING
5220 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5222 freed_so_far += cp_len;
5223 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5225 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5226 if ((freed_so_far >= rwnd_req) &&
5227 (control->do_not_ref_stcb == 0) &&
5228 (no_rcv_needed == 0))
5229 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5231 #ifdef SCTP_SB_LOGGING
5232 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5233 SCTP_LOG_SBRESULT, 0);
5235 if (out_flags & MSG_NOTIFICATION) {
5237 * remark the first mbuf if
5238 * they took a partial read.
5240 control->data->m_flags |= M_NOTIFICATION;
5248 if (hold_rlock == 1) {
5249 SCTP_INP_READ_UNLOCK(inp);
5252 if (hold_sblock == 0) {
5253 SOCKBUF_LOCK(&so->so_rcv);
5256 sbunlock(&so->so_rcv);
5260 SOCKBUF_UNLOCK(&so->so_rcv);
5263 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
5264 if ((freed_so_far >= rwnd_req) &&
5265 (control && (control->do_not_ref_stcb == 0)) &&
5266 (no_rcv_needed == 0))
5267 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5270 *msg_flags |= out_flags;
5272 if (hold_rlock == 1) {
5273 SCTP_INP_READ_UNLOCK(inp);
5277 SOCKBUF_UNLOCK(&so->so_rcv);
5280 if ((stcb) && freecnt_applied) {
5282 * The lock on the socket buffer protects us so the free
5283 * code will stop. But since we used the socketbuf lock and
5284 * the sender uses the tcb_lock to increment, we need to use
5285 * the atomic add to the refcnt.
5287 atomic_add_16(&stcb->asoc.refcnt, -1);
5288 freecnt_applied = 0;
5289 /* Save the value back for next time */
5290 stcb->freed_by_sorcv_sincelast = freed_so_far;
5293 #ifdef SCTP_RECV_RWND_LOGGING
5295 sctp_misc_ints(SCTP_SORECV_DONE,
5297 ((uio) ? (slen - uio->uio_resid) : slen),
5301 sctp_misc_ints(SCTP_SORECV_DONE,
5303 ((uio) ? (slen - uio->uio_resid) : slen),
5308 if (wakeup_read_socket) {
5309 sctp_sorwakeup(inp, so);
5315 #ifdef SCTP_MBUF_LOGGING
5317 sctp_m_free(struct mbuf *m)
5319 if (m->m_flags & M_EXT) {
5320 sctp_log_mb(m, SCTP_MBUF_IFREE);
5326 sctp_m_freem(struct mbuf *mb)
5329 mb = sctp_m_free(mb);
5336 sctp_soreceive(so, psa, uio, mp0, controlp, flagsp)
5338 struct sockaddr **psa;
5341 struct mbuf **controlp;
5345 uint8_t sockbuf[256];
5346 struct sockaddr *from;
5347 struct sctp_extrcvinfo sinfo;
5348 int filling_sinfo = 1;
5349 struct sctp_inpcb *inp;
5351 inp = (struct sctp_inpcb *)so->so_pcb;
5352 /* pickup the assoc we are reading from */
5356 if ((sctp_is_feature_off(inp,
5357 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
5358 (controlp == NULL)) {
5359 /* user does not want the sndrcv ctl */
5363 from = (struct sockaddr *)sockbuf;
5364 fromlen = sizeof(sockbuf);
5371 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
5372 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
5373 if ((controlp) && (filling_sinfo)) {
5374 /* copy back the sinfo in a CMSG format */
5376 *controlp = sctp_build_ctl_nchunk(inp,
5377 (struct sctp_sndrcvinfo *)&sinfo);
5382 /* copy back the address info */
5383 if (from && from->sa_len) {
5384 *psa = sodupsockaddr(from, M_NOWAIT);