xref: /freebsd/sys/netinet/sctp_var.h (revision 56e53cb8ef000c3ef72337a4095987a932cdedef)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
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.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
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.
17  *
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.
21  *
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.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef _NETINET_SCTP_VAR_H_
39 #define _NETINET_SCTP_VAR_H_
40 
41 #include <netinet/sctp_uio.h>
42 
43 #if defined(_KERNEL) || defined(__Userspace__)
44 
45 extern struct pr_usrreqs sctp_usrreqs;
46 
47 
48 #define sctp_feature_on(inp, feature)  (inp->sctp_features |= feature)
49 #define sctp_feature_off(inp, feature) (inp->sctp_features &= ~feature)
50 #define sctp_is_feature_on(inp, feature) ((inp->sctp_features & feature) == feature)
51 #define sctp_is_feature_off(inp, feature) ((inp->sctp_features & feature) == 0)
52 
53 #define sctp_stcb_feature_on(inp, stcb, feature) {\
54 	if (stcb) { \
55 		stcb->asoc.sctp_features |= feature; \
56 	} else if (inp) { \
57 		inp->sctp_features |= feature; \
58 	} \
59 }
60 #define sctp_stcb_feature_off(inp, stcb, feature) {\
61 	if (stcb) { \
62 		stcb->asoc.sctp_features &= ~feature; \
63 	} else if (inp) { \
64 		inp->sctp_features &= ~feature; \
65 	} \
66 }
67 #define sctp_stcb_is_feature_on(inp, stcb, feature) \
68 	(((stcb != NULL) && \
69 	  ((stcb->asoc.sctp_features & feature) == feature)) || \
70 	 ((stcb == NULL) && (inp != NULL) && \
71 	  ((inp->sctp_features & feature) == feature)))
72 #define sctp_stcb_is_feature_off(inp, stcb, feature) \
73 	(((stcb != NULL) && \
74 	  ((stcb->asoc.sctp_features & feature) == 0)) || \
75 	 ((stcb == NULL) && (inp != NULL) && \
76 	  ((inp->sctp_features & feature) == 0)) || \
77 	 ((stcb == NULL) && (inp == NULL)))
78 
79 /* managing mobility_feature in inpcb (by micchie) */
80 #define sctp_mobility_feature_on(inp, feature)  (inp->sctp_mobility_features |= feature)
81 #define sctp_mobility_feature_off(inp, feature) (inp->sctp_mobility_features &= ~feature)
82 #define sctp_is_mobility_feature_on(inp, feature) (inp->sctp_mobility_features & feature)
83 #define sctp_is_mobility_feature_off(inp, feature) ((inp->sctp_mobility_features & feature) == 0)
84 
85 #define sctp_maxspace(sb) (max((sb)->sb_hiwat,SCTP_MINIMAL_RWND))
86 
87 #define	sctp_sbspace(asoc, sb) ((long) ((sctp_maxspace(sb) > (asoc)->sb_cc) ? (sctp_maxspace(sb) - (asoc)->sb_cc) : 0))
88 
89 #define	sctp_sbspace_failedmsgs(sb) ((long) ((sctp_maxspace(sb) > (sb)->sb_cc) ? (sctp_maxspace(sb) - (sb)->sb_cc) : 0))
90 
91 #define sctp_sbspace_sub(a,b) (((a) > (b)) ? ((a) - (b)) : 0)
92 
93 /*
94  * I tried to cache the readq entries at one point. But the reality
95  * is that it did not add any performance since this meant we had to
96  * lock the STCB on read. And at that point once you have to do an
97  * extra lock, it really does not matter if the lock is in the ZONE
98  * stuff or in our code. Note that this same problem would occur with
99  * an mbuf cache as well so it is not really worth doing, at least
100  * right now :-D
101  */
102 #ifdef INVARIANTS
103 #define sctp_free_a_readq(_stcb, _readq) { \
104 	if ((_readq)->on_strm_q) \
105 		panic("On strm q stcb:%p readq:%p", (_stcb), (_readq)); \
106 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), (_readq)); \
107 	SCTP_DECR_READQ_COUNT(); \
108 }
109 #else
110 #define sctp_free_a_readq(_stcb, _readq) { \
111 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), (_readq)); \
112 	SCTP_DECR_READQ_COUNT(); \
113 }
114 #endif
115 
116 #define sctp_alloc_a_readq(_stcb, _readq) { \
117 	(_readq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_readq), struct sctp_queued_to_read); \
118 	if ((_readq)) { \
119 	     SCTP_INCR_READQ_COUNT(); \
120 	} \
121 }
122 
123 #define sctp_free_a_strmoq(_stcb, _strmoq, _so_locked) { \
124 	if ((_strmoq)->holds_key_ref) { \
125 		sctp_auth_key_release(stcb, sp->auth_keyid, _so_locked); \
126 		(_strmoq)->holds_key_ref = 0; \
127 	} \
128 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), (_strmoq)); \
129 	SCTP_DECR_STRMOQ_COUNT(); \
130 }
131 
132 #define sctp_alloc_a_strmoq(_stcb, _strmoq) { \
133 	(_strmoq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_strmoq), struct sctp_stream_queue_pending); \
134 	if ((_strmoq)) { \
135 		memset(_strmoq, 0, sizeof(struct sctp_stream_queue_pending)); \
136 		SCTP_INCR_STRMOQ_COUNT(); \
137 		(_strmoq)->holds_key_ref = 0; \
138 	} \
139 }
140 
141 #define sctp_free_a_chunk(_stcb, _chk, _so_locked) { \
142 	if ((_chk)->holds_key_ref) {\
143 		sctp_auth_key_release((_stcb), (_chk)->auth_keyid, _so_locked); \
144 		(_chk)->holds_key_ref = 0; \
145 	} \
146 	if (_stcb) { \
147 		SCTP_TCB_LOCK_ASSERT((_stcb)); \
148 		if ((_chk)->whoTo) { \
149 			sctp_free_remote_addr((_chk)->whoTo); \
150 			(_chk)->whoTo = NULL; \
151 		} \
152 		if (((_stcb)->asoc.free_chunk_cnt > SCTP_BASE_SYSCTL(sctp_asoc_free_resc_limit)) || \
153 		    (SCTP_BASE_INFO(ipi_free_chunks) > SCTP_BASE_SYSCTL(sctp_system_free_resc_limit))) { \
154 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \
155 			SCTP_DECR_CHK_COUNT(); \
156 		} else { \
157 			TAILQ_INSERT_TAIL(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
158 			(_stcb)->asoc.free_chunk_cnt++; \
159 			atomic_add_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \
160 		} \
161 	} else { \
162 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \
163 		SCTP_DECR_CHK_COUNT(); \
164 	} \
165 }
166 
167 #define sctp_alloc_a_chunk(_stcb, _chk) { \
168 	if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks)) { \
169 		(_chk) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_chunk), struct sctp_tmit_chunk); \
170 		if ((_chk)) { \
171 			SCTP_INCR_CHK_COUNT(); \
172 			(_chk)->whoTo = NULL; \
173 			(_chk)->holds_key_ref = 0; \
174 		} \
175 	} else { \
176 		(_chk) = TAILQ_FIRST(&(_stcb)->asoc.free_chunks); \
177 		TAILQ_REMOVE(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
178 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \
179 		(_chk)->holds_key_ref = 0; \
180 		SCTP_STAT_INCR(sctps_cached_chk); \
181 		(_stcb)->asoc.free_chunk_cnt--; \
182 	} \
183 }
184 
185 
186 #define sctp_free_remote_addr(__net) { \
187 	if ((__net)) {  \
188 		if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&(__net)->ref_count)) { \
189 			(void)SCTP_OS_TIMER_STOP(&(__net)->rxt_timer.timer); \
190 			(void)SCTP_OS_TIMER_STOP(&(__net)->pmtu_timer.timer); \
191 			(void)SCTP_OS_TIMER_STOP(&(__net)->hb_timer.timer); \
192 			if ((__net)->ro.ro_rt) { \
193 				RTFREE((__net)->ro.ro_rt); \
194 				(__net)->ro.ro_rt = NULL; \
195 			} \
196 			if ((__net)->src_addr_selected) { \
197 				sctp_free_ifa((__net)->ro._s_addr); \
198 				(__net)->ro._s_addr = NULL; \
199 			} \
200 			(__net)->src_addr_selected = 0; \
201 			(__net)->dest_state &= ~SCTP_ADDR_REACHABLE; \
202 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_net), (__net)); \
203 			SCTP_DECR_RADDR_COUNT(); \
204 		} \
205 	} \
206 }
207 
208 #define sctp_sbfree(ctl, stcb, sb, m) { \
209 	SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_cc, SCTP_BUF_LEN((m))); \
210 	SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_mbcnt, MSIZE); \
211 	if (((ctl)->do_not_ref_stcb == 0) && stcb) {\
212 		SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \
213 		SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
214 	} \
215 	if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
216 	    SCTP_BUF_TYPE(m) != MT_OOBDATA) \
217 		atomic_subtract_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
218 }
219 
220 #define sctp_sballoc(stcb, sb, m) { \
221 	atomic_add_int(&(sb)->sb_cc,SCTP_BUF_LEN((m))); \
222 	atomic_add_int(&(sb)->sb_mbcnt, MSIZE); \
223 	if (stcb) { \
224 		atomic_add_int(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \
225 		atomic_add_int(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
226 	} \
227 	if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
228 	    SCTP_BUF_TYPE(m) != MT_OOBDATA) \
229 		atomic_add_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
230 }
231 
232 
233 #define sctp_ucount_incr(val) { \
234 	val++; \
235 }
236 
237 #define sctp_ucount_decr(val) { \
238 	if (val > 0) { \
239 		val--; \
240 	} else { \
241 		val = 0; \
242 	} \
243 }
244 
245 #define sctp_mbuf_crush(data) do { \
246 	struct mbuf *_m; \
247 	_m = (data); \
248 	while (_m && (SCTP_BUF_LEN(_m) == 0)) { \
249 		(data)  = SCTP_BUF_NEXT(_m); \
250 		SCTP_BUF_NEXT(_m) = NULL; \
251 		sctp_m_free(_m); \
252 		_m = (data); \
253 	} \
254 } while (0)
255 
256 #define sctp_flight_size_decrease(tp1) do { \
257 	if (tp1->whoTo->flight_size >= tp1->book_size) \
258 		tp1->whoTo->flight_size -= tp1->book_size; \
259 	else \
260 		tp1->whoTo->flight_size = 0; \
261 } while (0)
262 
263 #define sctp_flight_size_increase(tp1) do { \
264 	(tp1)->whoTo->flight_size += (tp1)->book_size; \
265 } while (0)
266 
267 #ifdef SCTP_FS_SPEC_LOG
268 #define sctp_total_flight_decrease(stcb, tp1) do { \
269 	if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \
270 		stcb->asoc.fs_index = 0;\
271 	stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \
272 	stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.tsn; \
273 	stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \
274 	stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \
275 	stcb->asoc.fslog[stcb->asoc.fs_index].incr = 0; \
276 	stcb->asoc.fslog[stcb->asoc.fs_index].decr = 1; \
277 	stcb->asoc.fs_index++; \
278 	tp1->window_probe = 0; \
279 	if (stcb->asoc.total_flight >= tp1->book_size) { \
280 		stcb->asoc.total_flight -= tp1->book_size; \
281 		if (stcb->asoc.total_flight_count > 0) \
282 			stcb->asoc.total_flight_count--; \
283 	} else { \
284 		stcb->asoc.total_flight = 0; \
285 		stcb->asoc.total_flight_count = 0; \
286 	} \
287 } while (0)
288 
289 #define sctp_total_flight_increase(stcb, tp1) do { \
290 	if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \
291 		stcb->asoc.fs_index = 0;\
292 	stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \
293 	stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.tsn; \
294 	stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \
295 	stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \
296 	stcb->asoc.fslog[stcb->asoc.fs_index].incr = 1; \
297 	stcb->asoc.fslog[stcb->asoc.fs_index].decr = 0; \
298 	stcb->asoc.fs_index++; \
299 	(stcb)->asoc.total_flight_count++; \
300 	(stcb)->asoc.total_flight += (tp1)->book_size; \
301 } while (0)
302 
303 #else
304 
305 #define sctp_total_flight_decrease(stcb, tp1) do { \
306 	tp1->window_probe = 0; \
307 	if (stcb->asoc.total_flight >= tp1->book_size) { \
308 		stcb->asoc.total_flight -= tp1->book_size; \
309 		if (stcb->asoc.total_flight_count > 0) \
310 			stcb->asoc.total_flight_count--; \
311 	} else { \
312 		stcb->asoc.total_flight = 0; \
313 		stcb->asoc.total_flight_count = 0; \
314 	} \
315 } while (0)
316 
317 #define sctp_total_flight_increase(stcb, tp1) do { \
318 	(stcb)->asoc.total_flight_count++; \
319 	(stcb)->asoc.total_flight += (tp1)->book_size; \
320 } while (0)
321 
322 #endif
323 
324 #define SCTP_PF_ENABLED(_net) (_net->pf_threshold < _net->failure_threshold)
325 #define SCTP_NET_IS_PF(_net) (_net->pf_threshold < _net->error_count)
326 
327 struct sctp_nets;
328 struct sctp_inpcb;
329 struct sctp_tcb;
330 struct sctphdr;
331 
332 
333 void sctp_close(struct socket *so);
334 int sctp_disconnect(struct socket *so);
335 void sctp_ctlinput(int, struct sockaddr *, void *);
336 int sctp_ctloutput(struct socket *, struct sockopt *);
337 #ifdef INET
338 void sctp_input_with_port(struct mbuf *, int, uint16_t);
339 int sctp_input(struct mbuf **, int *, int);
340 #endif
341 void sctp_pathmtu_adjustment(struct sctp_tcb *, uint16_t);
342 void sctp_drain(void);
343 void sctp_init(void);
344 void
345 sctp_notify(struct sctp_inpcb *, struct sctp_tcb *, struct sctp_nets *,
346     uint8_t, uint8_t, uint16_t, uint32_t);
347 int sctp_flush(struct socket *, int);
348 int sctp_shutdown(struct socket *);
349 int
350 sctp_bindx(struct socket *, int, struct sockaddr_storage *,
351     int, int, struct proc *);
352 
353 /* can't use sctp_assoc_t here */
354 int sctp_peeloff(struct socket *, struct socket *, int, caddr_t, int *);
355 int sctp_ingetaddr(struct socket *, struct sockaddr **);
356 int sctp_peeraddr(struct socket *, struct sockaddr **);
357 int sctp_listen(struct socket *, int, struct thread *);
358 int sctp_accept(struct socket *, struct sockaddr **);
359 
360 #endif				/* _KERNEL */
361 
362 #endif				/* !_NETINET_SCTP_VAR_H_ */
363