2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #ifndef _NETINET_SCTP_VAR_H_
37 #define _NETINET_SCTP_VAR_H_
39 #include <netinet/sctp_uio.h>
41 #if defined(_KERNEL) || defined(__Userspace__)
43 extern struct pr_usrreqs sctp_usrreqs;
46 #define sctp_feature_on(inp, feature) (inp->sctp_features |= feature)
47 #define sctp_feature_off(inp, feature) (inp->sctp_features &= ~feature)
48 #define sctp_is_feature_on(inp, feature) ((inp->sctp_features & feature) == feature)
49 #define sctp_is_feature_off(inp, feature) ((inp->sctp_features & feature) == 0)
51 #define sctp_stcb_feature_on(inp, stcb, feature) {\
53 stcb->asoc.sctp_features |= feature; \
55 inp->sctp_features |= feature; \
58 #define sctp_stcb_feature_off(inp, stcb, feature) {\
60 stcb->asoc.sctp_features &= ~feature; \
62 inp->sctp_features &= ~feature; \
65 #define sctp_stcb_is_feature_on(inp, stcb, feature) \
67 ((stcb->asoc.sctp_features & feature) == feature)) || \
68 ((stcb == NULL) && (inp != NULL) && \
69 ((inp->sctp_features & feature) == feature)))
70 #define sctp_stcb_is_feature_off(inp, stcb, feature) \
72 ((stcb->asoc.sctp_features & feature) == 0)) || \
73 ((stcb == NULL) && (inp != NULL) && \
74 ((inp->sctp_features & feature) == 0)) || \
75 ((stcb == NULL) && (inp == NULL)))
77 /* managing mobility_feature in inpcb (by micchie) */
78 #define sctp_mobility_feature_on(inp, feature) (inp->sctp_mobility_features |= feature)
79 #define sctp_mobility_feature_off(inp, feature) (inp->sctp_mobility_features &= ~feature)
80 #define sctp_is_mobility_feature_on(inp, feature) (inp->sctp_mobility_features & feature)
81 #define sctp_is_mobility_feature_off(inp, feature) ((inp->sctp_mobility_features & feature) == 0)
83 #define sctp_maxspace(sb) (max((sb)->sb_hiwat,SCTP_MINIMAL_RWND))
85 #define sctp_sbspace(asoc, sb) ((long) ((sctp_maxspace(sb) > (asoc)->sb_cc) ? (sctp_maxspace(sb) - (asoc)->sb_cc) : 0))
87 #define sctp_sbspace_failedmsgs(sb) ((long) ((sctp_maxspace(sb) > (sb)->sb_cc) ? (sctp_maxspace(sb) - (sb)->sb_cc) : 0))
89 #define sctp_sbspace_sub(a,b) ((a > b) ? (a - b) : 0)
92 * I tried to cache the readq entries at one point. But the reality
93 * is that it did not add any performance since this meant we had to
94 * lock the STCB on read. And at that point once you have to do an
95 * extra lock, it really does not matter if the lock is in the ZONE
96 * stuff or in our code. Note that this same problem would occur with
97 * an mbuf cache as well so it is not really worth doing, at least
101 #define sctp_free_a_readq(_stcb, _readq) { \
102 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), (_readq)); \
103 SCTP_DECR_READQ_COUNT(); \
106 #define sctp_alloc_a_readq(_stcb, _readq) { \
107 (_readq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_readq), struct sctp_queued_to_read); \
109 SCTP_INCR_READQ_COUNT(); \
113 #define sctp_free_a_strmoq(_stcb, _strmoq, _so_locked) { \
114 if ((_strmoq)->holds_key_ref) { \
115 sctp_auth_key_release(stcb, sp->auth_keyid, _so_locked); \
116 (_strmoq)->holds_key_ref = 0; \
118 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), (_strmoq)); \
119 SCTP_DECR_STRMOQ_COUNT(); \
122 #define sctp_alloc_a_strmoq(_stcb, _strmoq) { \
123 (_strmoq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_strmoq), struct sctp_stream_queue_pending); \
125 memset(_strmoq, 0, sizeof(struct sctp_stream_queue_pending)); \
126 SCTP_INCR_STRMOQ_COUNT(); \
127 (_strmoq)->holds_key_ref = 0; \
131 #define sctp_free_a_chunk(_stcb, _chk, _so_locked) { \
132 if ((_chk)->holds_key_ref) {\
133 sctp_auth_key_release((_stcb), (_chk)->auth_keyid, _so_locked); \
134 (_chk)->holds_key_ref = 0; \
137 SCTP_TCB_LOCK_ASSERT((_stcb)); \
138 if ((_chk)->whoTo) { \
139 sctp_free_remote_addr((_chk)->whoTo); \
140 (_chk)->whoTo = NULL; \
142 if (((_stcb)->asoc.free_chunk_cnt > SCTP_BASE_SYSCTL(sctp_asoc_free_resc_limit)) || \
143 (SCTP_BASE_INFO(ipi_free_chunks) > SCTP_BASE_SYSCTL(sctp_system_free_resc_limit))) { \
144 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \
145 SCTP_DECR_CHK_COUNT(); \
147 TAILQ_INSERT_TAIL(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
148 (_stcb)->asoc.free_chunk_cnt++; \
149 atomic_add_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \
152 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \
153 SCTP_DECR_CHK_COUNT(); \
157 #define sctp_alloc_a_chunk(_stcb, _chk) { \
158 if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks)) { \
159 (_chk) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_chunk), struct sctp_tmit_chunk); \
161 SCTP_INCR_CHK_COUNT(); \
162 (_chk)->whoTo = NULL; \
163 (_chk)->holds_key_ref = 0; \
166 (_chk) = TAILQ_FIRST(&(_stcb)->asoc.free_chunks); \
167 TAILQ_REMOVE(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
168 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \
169 (_chk)->holds_key_ref = 0; \
170 SCTP_STAT_INCR(sctps_cached_chk); \
171 (_stcb)->asoc.free_chunk_cnt--; \
176 #define sctp_free_remote_addr(__net) { \
178 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&(__net)->ref_count)) { \
179 (void)SCTP_OS_TIMER_STOP(&(__net)->rxt_timer.timer); \
180 (void)SCTP_OS_TIMER_STOP(&(__net)->pmtu_timer.timer); \
181 if ((__net)->ro.ro_rt) { \
182 RTFREE((__net)->ro.ro_rt); \
183 (__net)->ro.ro_rt = NULL; \
185 if ((__net)->src_addr_selected) { \
186 sctp_free_ifa((__net)->ro._s_addr); \
187 (__net)->ro._s_addr = NULL; \
189 (__net)->src_addr_selected = 0; \
190 (__net)->dest_state &= ~SCTP_ADDR_REACHABLE; \
191 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_net), (__net)); \
192 SCTP_DECR_RADDR_COUNT(); \
197 #define sctp_sbfree(ctl, stcb, sb, m) { \
198 SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_cc, SCTP_BUF_LEN((m))); \
199 SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_mbcnt, MSIZE); \
200 if (((ctl)->do_not_ref_stcb == 0) && stcb) {\
201 SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \
202 SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
204 if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
205 SCTP_BUF_TYPE(m) != MT_OOBDATA) \
206 atomic_subtract_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
209 #define sctp_sballoc(stcb, sb, m) { \
210 atomic_add_int(&(sb)->sb_cc,SCTP_BUF_LEN((m))); \
211 atomic_add_int(&(sb)->sb_mbcnt, MSIZE); \
213 atomic_add_int(&(stcb)->asoc.sb_cc,SCTP_BUF_LEN((m))); \
214 atomic_add_int(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
216 if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
217 SCTP_BUF_TYPE(m) != MT_OOBDATA) \
218 atomic_add_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
222 #define sctp_ucount_incr(val) { \
226 #define sctp_ucount_decr(val) { \
234 #define sctp_mbuf_crush(data) do { \
237 while (_m && (SCTP_BUF_LEN(_m) == 0)) { \
238 (data) = SCTP_BUF_NEXT(_m); \
239 SCTP_BUF_NEXT(_m) = NULL; \
245 #define sctp_flight_size_decrease(tp1) do { \
246 if (tp1->whoTo->flight_size >= tp1->book_size) \
247 tp1->whoTo->flight_size -= tp1->book_size; \
249 tp1->whoTo->flight_size = 0; \
252 #define sctp_flight_size_increase(tp1) do { \
253 (tp1)->whoTo->flight_size += (tp1)->book_size; \
256 #ifdef SCTP_FS_SPEC_LOG
257 #define sctp_total_flight_decrease(stcb, tp1) do { \
258 if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \
259 stcb->asoc.fs_index = 0;\
260 stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \
261 stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.TSN_seq; \
262 stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \
263 stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \
264 stcb->asoc.fslog[stcb->asoc.fs_index].incr = 0; \
265 stcb->asoc.fslog[stcb->asoc.fs_index].decr = 1; \
266 stcb->asoc.fs_index++; \
267 tp1->window_probe = 0; \
268 if (stcb->asoc.total_flight >= tp1->book_size) { \
269 stcb->asoc.total_flight -= tp1->book_size; \
270 if (stcb->asoc.total_flight_count > 0) \
271 stcb->asoc.total_flight_count--; \
273 stcb->asoc.total_flight = 0; \
274 stcb->asoc.total_flight_count = 0; \
278 #define sctp_total_flight_increase(stcb, tp1) do { \
279 if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \
280 stcb->asoc.fs_index = 0;\
281 stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \
282 stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.TSN_seq; \
283 stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \
284 stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \
285 stcb->asoc.fslog[stcb->asoc.fs_index].incr = 1; \
286 stcb->asoc.fslog[stcb->asoc.fs_index].decr = 0; \
287 stcb->asoc.fs_index++; \
288 (stcb)->asoc.total_flight_count++; \
289 (stcb)->asoc.total_flight += (tp1)->book_size; \
294 #define sctp_total_flight_decrease(stcb, tp1) do { \
295 tp1->window_probe = 0; \
296 if (stcb->asoc.total_flight >= tp1->book_size) { \
297 stcb->asoc.total_flight -= tp1->book_size; \
298 if (stcb->asoc.total_flight_count > 0) \
299 stcb->asoc.total_flight_count--; \
301 stcb->asoc.total_flight = 0; \
302 stcb->asoc.total_flight_count = 0; \
306 #define sctp_total_flight_increase(stcb, tp1) do { \
307 (stcb)->asoc.total_flight_count++; \
308 (stcb)->asoc.total_flight += (tp1)->book_size; \
313 #define SCTP_PF_ENABLED(_net) (_net->pf_threshold < _net->failure_threshold)
314 #define SCTP_NET_IS_PF(_net) (_net->pf_threshold < _net->error_count)
322 void sctp_close(struct socket *so);
323 int sctp_disconnect(struct socket *so);
324 void sctp_ctlinput(int, struct sockaddr *, void *);
325 int sctp_ctloutput(struct socket *, struct sockopt *);
328 void sctp_input_with_port(struct mbuf *, int, uint16_t);
329 void sctp_input(struct mbuf *, int);
332 void sctp_pathmtu_adjustment(struct sctp_tcb *, uint16_t);
333 void sctp_drain(void);
334 void sctp_init(void);
335 void sctp_finish(void);
336 int sctp_flush(struct socket *, int);
337 int sctp_shutdown(struct socket *);
339 sctp_notify(struct sctp_inpcb *, struct ip *ip, struct sctphdr *,
340 struct sockaddr *, struct sctp_tcb *,
343 sctp_bindx(struct socket *, int, struct sockaddr_storage *,
344 int, int, struct proc *);
346 /* can't use sctp_assoc_t here */
347 int sctp_peeloff(struct socket *, struct socket *, int, caddr_t, int *);
348 int sctp_ingetaddr(struct socket *, struct sockaddr **);
349 int sctp_peeraddr(struct socket *, struct sockaddr **);
350 int sctp_listen(struct socket *, int, struct thread *);
351 int sctp_accept(struct socket *, struct sockaddr **);
355 #endif /* !_NETINET_SCTP_VAR_H_ */