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