2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #if defined(__FreeBSD__) && !defined(__Userspace__)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_var.h 363323 2020-07-19 12:34:19Z tuexen $");
40 #ifndef _NETINET_SCTP_VAR_H_
41 #define _NETINET_SCTP_VAR_H_
43 #include <netinet/sctp_uio.h>
45 #if defined(_KERNEL) || defined(__Userspace__)
47 #if !defined(__Userspace__)
48 extern struct pr_usrreqs sctp_usrreqs;
52 #define sctp_feature_on(inp, feature) (inp->sctp_features |= feature)
53 #define sctp_feature_off(inp, feature) (inp->sctp_features &= ~feature)
54 #define sctp_is_feature_on(inp, feature) ((inp->sctp_features & feature) == feature)
55 #define sctp_is_feature_off(inp, feature) ((inp->sctp_features & feature) == 0)
57 #define sctp_stcb_feature_on(inp, stcb, feature) {\
59 stcb->asoc.sctp_features |= feature; \
61 inp->sctp_features |= feature; \
64 #define sctp_stcb_feature_off(inp, stcb, feature) {\
66 stcb->asoc.sctp_features &= ~feature; \
68 inp->sctp_features &= ~feature; \
71 #define sctp_stcb_is_feature_on(inp, stcb, feature) \
73 ((stcb->asoc.sctp_features & feature) == feature)) || \
74 ((stcb == NULL) && (inp != NULL) && \
75 ((inp->sctp_features & feature) == feature)))
76 #define sctp_stcb_is_feature_off(inp, stcb, feature) \
78 ((stcb->asoc.sctp_features & feature) == 0)) || \
79 ((stcb == NULL) && (inp != NULL) && \
80 ((inp->sctp_features & feature) == 0)) || \
81 ((stcb == NULL) && (inp == NULL)))
83 /* managing mobility_feature in inpcb (by micchie) */
84 #define sctp_mobility_feature_on(inp, feature) (inp->sctp_mobility_features |= feature)
85 #define sctp_mobility_feature_off(inp, feature) (inp->sctp_mobility_features &= ~feature)
86 #define sctp_is_mobility_feature_on(inp, feature) (inp->sctp_mobility_features & feature)
87 #define sctp_is_mobility_feature_off(inp, feature) ((inp->sctp_mobility_features & feature) == 0)
89 #define sctp_maxspace(sb) (max((sb)->sb_hiwat,SCTP_MINIMAL_RWND))
91 #define sctp_sbspace(asoc, sb) ((long) ((sctp_maxspace(sb) > (asoc)->sb_cc) ? (sctp_maxspace(sb) - (asoc)->sb_cc) : 0))
93 #define sctp_sbspace_failedmsgs(sb) ((long) ((sctp_maxspace(sb) > (sb)->sb_cc) ? (sctp_maxspace(sb) - (sb)->sb_cc) : 0))
95 #define sctp_sbspace_sub(a,b) (((a) > (b)) ? ((a) - (b)) : 0)
98 * I tried to cache the readq entries at one point. But the reality
99 * is that it did not add any performance since this meant we had to
100 * lock the STCB on read. And at that point once you have to do an
101 * extra lock, it really does not matter if the lock is in the ZONE
102 * stuff or in our code. Note that this same problem would occur with
103 * an mbuf cache as well so it is not really worth doing, at least
107 #define sctp_free_a_readq(_stcb, _readq) { \
108 if ((_readq)->on_strm_q) \
109 panic("On strm q stcb:%p readq:%p", (_stcb), (_readq)); \
110 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), (_readq)); \
111 SCTP_DECR_READQ_COUNT(); \
114 #define sctp_free_a_readq(_stcb, _readq) { \
115 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), (_readq)); \
116 SCTP_DECR_READQ_COUNT(); \
120 #define sctp_alloc_a_readq(_stcb, _readq) { \
121 (_readq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_readq), struct sctp_queued_to_read); \
123 SCTP_INCR_READQ_COUNT(); \
127 #define sctp_free_a_strmoq(_stcb, _strmoq, _so_locked) { \
128 if ((_strmoq)->holds_key_ref) { \
129 sctp_auth_key_release(stcb, sp->auth_keyid, _so_locked); \
130 (_strmoq)->holds_key_ref = 0; \
132 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), (_strmoq)); \
133 SCTP_DECR_STRMOQ_COUNT(); \
136 #define sctp_alloc_a_strmoq(_stcb, _strmoq) { \
137 (_strmoq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_strmoq), struct sctp_stream_queue_pending); \
139 memset(_strmoq, 0, sizeof(struct sctp_stream_queue_pending)); \
140 SCTP_INCR_STRMOQ_COUNT(); \
141 (_strmoq)->holds_key_ref = 0; \
145 #define sctp_free_a_chunk(_stcb, _chk, _so_locked) { \
146 if ((_chk)->holds_key_ref) {\
147 sctp_auth_key_release((_stcb), (_chk)->auth_keyid, _so_locked); \
148 (_chk)->holds_key_ref = 0; \
151 SCTP_TCB_LOCK_ASSERT((_stcb)); \
152 if ((_chk)->whoTo) { \
153 sctp_free_remote_addr((_chk)->whoTo); \
154 (_chk)->whoTo = NULL; \
156 if (((_stcb)->asoc.free_chunk_cnt > SCTP_BASE_SYSCTL(sctp_asoc_free_resc_limit)) || \
157 (SCTP_BASE_INFO(ipi_free_chunks) > SCTP_BASE_SYSCTL(sctp_system_free_resc_limit))) { \
158 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \
159 SCTP_DECR_CHK_COUNT(); \
161 TAILQ_INSERT_TAIL(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
162 (_stcb)->asoc.free_chunk_cnt++; \
163 atomic_add_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \
166 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \
167 SCTP_DECR_CHK_COUNT(); \
171 #define sctp_alloc_a_chunk(_stcb, _chk) { \
172 if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks)) { \
173 (_chk) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_chunk), struct sctp_tmit_chunk); \
175 SCTP_INCR_CHK_COUNT(); \
176 (_chk)->whoTo = NULL; \
177 (_chk)->holds_key_ref = 0; \
180 (_chk) = TAILQ_FIRST(&(_stcb)->asoc.free_chunks); \
181 TAILQ_REMOVE(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
182 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \
183 (_chk)->holds_key_ref = 0; \
184 SCTP_STAT_INCR(sctps_cached_chk); \
185 (_stcb)->asoc.free_chunk_cnt--; \
189 #if defined(__FreeBSD__) && !defined(__Userspace__)
191 #define sctp_free_remote_addr(__net) { \
193 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&(__net)->ref_count)) { \
194 RO_NHFREE(&(__net)->ro); \
195 if ((__net)->src_addr_selected) { \
196 sctp_free_ifa((__net)->ro._s_addr); \
197 (__net)->ro._s_addr = NULL; \
199 (__net)->src_addr_selected = 0; \
200 (__net)->dest_state &= ~SCTP_ADDR_REACHABLE; \
201 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_net), (__net)); \
202 SCTP_DECR_RADDR_COUNT(); \
207 #define sctp_sbfree(ctl, stcb, sb, m) { \
208 SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_cc, SCTP_BUF_LEN((m))); \
209 SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_mbcnt, MSIZE); \
210 if (((ctl)->do_not_ref_stcb == 0) && stcb) {\
211 SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \
212 SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
214 if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
215 SCTP_BUF_TYPE(m) != MT_OOBDATA) \
216 atomic_subtract_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
219 #define sctp_sballoc(stcb, sb, m) { \
220 atomic_add_int(&(sb)->sb_cc,SCTP_BUF_LEN((m))); \
221 atomic_add_int(&(sb)->sb_mbcnt, MSIZE); \
223 atomic_add_int(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \
224 atomic_add_int(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
226 if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
227 SCTP_BUF_TYPE(m) != MT_OOBDATA) \
228 atomic_add_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
231 #else /* FreeBSD Version <= 500000 or non-FreeBSD */
233 #define sctp_free_remote_addr(__net) { \
235 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&(__net)->ref_count)) { \
236 if ((__net)->ro.ro_rt) { \
237 RTFREE((__net)->ro.ro_rt); \
238 (__net)->ro.ro_rt = NULL; \
240 if ((__net)->src_addr_selected) { \
241 sctp_free_ifa((__net)->ro._s_addr); \
242 (__net)->ro._s_addr = NULL; \
244 (__net)->src_addr_selected = 0; \
245 (__net)->dest_state &=~SCTP_ADDR_REACHABLE; \
246 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_net), (__net)); \
247 SCTP_DECR_RADDR_COUNT(); \
252 #define sctp_sbfree(ctl, stcb, sb, m) { \
253 SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_cc, SCTP_BUF_LEN((m))); \
254 SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_mbcnt, MSIZE); \
255 if (((ctl)->do_not_ref_stcb == 0) && stcb) { \
256 SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \
257 SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
261 #define sctp_sballoc(stcb, sb, m) { \
262 atomic_add_int(&(sb)->sb_cc, SCTP_BUF_LEN((m))); \
263 atomic_add_int(&(sb)->sb_mbcnt, MSIZE); \
265 atomic_add_int(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \
266 atomic_add_int(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
271 #define sctp_ucount_incr(val) { \
275 #define sctp_ucount_decr(val) { \
283 #define sctp_mbuf_crush(data) do { \
286 while (_m && (SCTP_BUF_LEN(_m) == 0)) { \
287 (data) = SCTP_BUF_NEXT(_m); \
288 SCTP_BUF_NEXT(_m) = NULL; \
294 #define sctp_flight_size_decrease(tp1) do { \
295 if (tp1->whoTo->flight_size >= tp1->book_size) \
296 tp1->whoTo->flight_size -= tp1->book_size; \
298 tp1->whoTo->flight_size = 0; \
301 #define sctp_flight_size_increase(tp1) do { \
302 (tp1)->whoTo->flight_size += (tp1)->book_size; \
305 #ifdef SCTP_FS_SPEC_LOG
306 #define sctp_total_flight_decrease(stcb, tp1) do { \
307 if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \
308 stcb->asoc.fs_index = 0;\
309 stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \
310 stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.tsn; \
311 stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \
312 stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \
313 stcb->asoc.fslog[stcb->asoc.fs_index].incr = 0; \
314 stcb->asoc.fslog[stcb->asoc.fs_index].decr = 1; \
315 stcb->asoc.fs_index++; \
316 tp1->window_probe = 0; \
317 if (stcb->asoc.total_flight >= tp1->book_size) { \
318 stcb->asoc.total_flight -= tp1->book_size; \
319 if (stcb->asoc.total_flight_count > 0) \
320 stcb->asoc.total_flight_count--; \
322 stcb->asoc.total_flight = 0; \
323 stcb->asoc.total_flight_count = 0; \
327 #define sctp_total_flight_increase(stcb, tp1) do { \
328 if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \
329 stcb->asoc.fs_index = 0;\
330 stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \
331 stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.tsn; \
332 stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \
333 stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \
334 stcb->asoc.fslog[stcb->asoc.fs_index].incr = 1; \
335 stcb->asoc.fslog[stcb->asoc.fs_index].decr = 0; \
336 stcb->asoc.fs_index++; \
337 (stcb)->asoc.total_flight_count++; \
338 (stcb)->asoc.total_flight += (tp1)->book_size; \
343 #define sctp_total_flight_decrease(stcb, tp1) do { \
344 tp1->window_probe = 0; \
345 if (stcb->asoc.total_flight >= tp1->book_size) { \
346 stcb->asoc.total_flight -= tp1->book_size; \
347 if (stcb->asoc.total_flight_count > 0) \
348 stcb->asoc.total_flight_count--; \
350 stcb->asoc.total_flight = 0; \
351 stcb->asoc.total_flight_count = 0; \
355 #define sctp_total_flight_increase(stcb, tp1) do { \
356 (stcb)->asoc.total_flight_count++; \
357 (stcb)->asoc.total_flight += (tp1)->book_size; \
362 #define SCTP_PF_ENABLED(_net) (_net->pf_threshold < _net->failure_threshold)
363 #define SCTP_NET_IS_PF(_net) (_net->pf_threshold < _net->error_count)
371 #if defined(__FreeBSD__) || defined(_WIN32) || defined(__Userspace__)
372 void sctp_close(struct socket *so);
374 int sctp_detach(struct socket *so);
376 int sctp_disconnect(struct socket *so);
377 #if !defined(__Userspace__)
378 #if defined(__APPLE__) && !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) && !defined(APPLE_ELCAPITAN)
379 void sctp_ctlinput(int, struct sockaddr *, void *, struct ifnet * SCTP_UNUSED);
381 void sctp_ctlinput(int, struct sockaddr *, void *);
383 int sctp_ctloutput(struct socket *, struct sockopt *);
385 void sctp_input_with_port(struct mbuf *, int, uint16_t);
386 #if defined(__FreeBSD__) && !defined(__Userspace__)
387 int sctp_input(struct mbuf **, int *, int);
389 void sctp_input(struct mbuf *, int);
392 void sctp_pathmtu_adjustment(struct sctp_tcb *, uint16_t);
394 #if defined(__Userspace__)
395 void sctp_pathmtu_adjustment(struct sctp_tcb *, uint16_t);
397 void sctp_input(struct mbuf *,...);
399 void *sctp_ctlinput(int, struct sockaddr *, void *);
400 int sctp_ctloutput(int, struct socket *, int, int, struct mbuf **);
402 void sctp_drain(void);
403 #if defined(__Userspace__)
404 void sctp_init(uint16_t,
405 int (*)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df),
406 void (*)(const char *, ...), int start_threads);
407 #elif defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) &&!defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION))
408 void sctp_init(struct protosw *pp, struct domain *dp);
410 void sctp_init(void);
411 void sctp_notify(struct sctp_inpcb *, struct sctp_tcb *, struct sctp_nets *,
412 uint8_t, uint8_t, uint16_t, uint32_t);
414 #if !defined(__FreeBSD__) && !defined(__Userspace__)
415 void sctp_finish(void);
417 #if defined(__FreeBSD__) || defined(_WIN32) || defined(__Userspace__)
418 int sctp_flush(struct socket *, int);
420 int sctp_shutdown(struct socket *);
421 int sctp_bindx(struct socket *, int, struct sockaddr_storage *,
422 int, int, struct proc *);
423 /* can't use sctp_assoc_t here */
424 int sctp_peeloff(struct socket *, struct socket *, int, caddr_t, int *);
425 #if !defined(__Userspace__)
426 int sctp_ingetaddr(struct socket *, struct sockaddr **);
428 int sctp_ingetaddr(struct socket *, struct mbuf *);
430 #if !defined(__Userspace__)
431 int sctp_peeraddr(struct socket *, struct sockaddr **);
433 int sctp_peeraddr(struct socket *, struct mbuf *);
435 #if defined(__FreeBSD__) && !defined(__Userspace__)
436 int sctp_listen(struct socket *, int, struct thread *);
437 #elif defined(_WIN32) && !defined(__Userspace__)
438 int sctp_listen(struct socket *, int, PKTHREAD);
439 #elif defined(__Userspace__)
440 int sctp_listen(struct socket *, int, struct proc *);
442 int sctp_listen(struct socket *, struct proc *);
444 int sctp_accept(struct socket *, struct sockaddr **);
448 #endif /* !_NETINET_SCTP_VAR_H_ */