xref: /freebsd/sys/netinet/sctp_var.h (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2011, 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 /* $KAME: sctp_var.h,v 1.24 2005/03/06 16:04:19 itojun Exp $	 */
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 
54 /* managing mobility_feature in inpcb (by micchie) */
55 #define sctp_mobility_feature_on(inp, feature)  (inp->sctp_mobility_features |= feature)
56 #define sctp_mobility_feature_off(inp, feature) (inp->sctp_mobility_features &= ~feature)
57 #define sctp_is_mobility_feature_on(inp, feature) (inp->sctp_mobility_features & feature)
58 #define sctp_is_mobility_feature_off(inp, feature) ((inp->sctp_mobility_features & feature) == 0)
59 
60 #define sctp_maxspace(sb) (max((sb)->sb_hiwat,SCTP_MINIMAL_RWND))
61 
62 #define	sctp_sbspace(asoc, sb) ((long) ((sctp_maxspace(sb) > (asoc)->sb_cc) ? (sctp_maxspace(sb) - (asoc)->sb_cc) : 0))
63 
64 #define	sctp_sbspace_failedmsgs(sb) ((long) ((sctp_maxspace(sb) > (sb)->sb_cc) ? (sctp_maxspace(sb) - (sb)->sb_cc) : 0))
65 
66 #define sctp_sbspace_sub(a,b) ((a > b) ? (a - b) : 0)
67 
68 /*
69  * I tried to cache the readq entries at one point. But the reality
70  * is that it did not add any performance since this meant we had to
71  * lock the STCB on read. And at that point once you have to do an
72  * extra lock, it really does not matter if the lock is in the ZONE
73  * stuff or in our code. Note that this same problem would occur with
74  * an mbuf cache as well so it is not really worth doing, at least
75  * right now :-D
76  */
77 
78 #define sctp_free_a_readq(_stcb, _readq) { \
79 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), (_readq)); \
80 	SCTP_DECR_READQ_COUNT(); \
81 }
82 
83 #define sctp_alloc_a_readq(_stcb, _readq) { \
84 	(_readq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_readq), struct sctp_queued_to_read); \
85 	if ((_readq)) { \
86  	     SCTP_INCR_READQ_COUNT(); \
87 	} \
88 }
89 
90 #define sctp_free_a_strmoq(_stcb, _strmoq, _so_locked) { \
91 	if ((_strmoq)->holds_key_ref) { \
92 		sctp_auth_key_release(stcb, sp->auth_keyid, _so_locked); \
93 		(_strmoq)->holds_key_ref = 0; \
94 	} \
95 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), (_strmoq)); \
96 	SCTP_DECR_STRMOQ_COUNT(); \
97 }
98 
99 #define sctp_alloc_a_strmoq(_stcb, _strmoq) { \
100 	(_strmoq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_strmoq), struct sctp_stream_queue_pending); \
101          if ((_strmoq)) {			  \
102 		memset(_strmoq, 0, sizeof(struct sctp_stream_queue_pending)); \
103 		SCTP_INCR_STRMOQ_COUNT(); \
104 		(_strmoq)->holds_key_ref = 0; \
105  	} \
106 }
107 
108 #define sctp_free_a_chunk(_stcb, _chk, _so_locked) { \
109 	if ((_chk)->holds_key_ref) {\
110 		sctp_auth_key_release((_stcb), (_chk)->auth_keyid, _so_locked); \
111 		(_chk)->holds_key_ref = 0; \
112 	} \
113         if (_stcb) { \
114           SCTP_TCB_LOCK_ASSERT((_stcb)); \
115           if ((_chk)->whoTo) { \
116                   sctp_free_remote_addr((_chk)->whoTo); \
117                   (_chk)->whoTo = NULL; \
118           } \
119           if (((_stcb)->asoc.free_chunk_cnt > SCTP_BASE_SYSCTL(sctp_asoc_free_resc_limit)) || \
120                (SCTP_BASE_INFO(ipi_free_chunks) > SCTP_BASE_SYSCTL(sctp_system_free_resc_limit))) { \
121 	 	SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \
122 	 	SCTP_DECR_CHK_COUNT(); \
123 	  } else { \
124 	 	TAILQ_INSERT_TAIL(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
125 	 	(_stcb)->asoc.free_chunk_cnt++; \
126 	 	atomic_add_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \
127           } \
128         } else { \
129 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \
130 		SCTP_DECR_CHK_COUNT(); \
131 	} \
132 }
133 
134 #define sctp_alloc_a_chunk(_stcb, _chk) { \
135 	if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks))  { \
136 		(_chk) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_chunk), struct sctp_tmit_chunk); \
137 		if ((_chk)) { \
138 			SCTP_INCR_CHK_COUNT(); \
139                         (_chk)->whoTo = NULL; \
140 			(_chk)->holds_key_ref = 0; \
141 		} \
142 	} else { \
143 		(_chk) = TAILQ_FIRST(&(_stcb)->asoc.free_chunks); \
144 		TAILQ_REMOVE(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
145 		atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \
146 		(_chk)->holds_key_ref = 0; \
147                 SCTP_STAT_INCR(sctps_cached_chk); \
148 		(_stcb)->asoc.free_chunk_cnt--; \
149 	} \
150 }
151 
152 
153 #define sctp_free_remote_addr(__net) { \
154 	if ((__net)) {  \
155 		if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&(__net)->ref_count)) { \
156 			(void)SCTP_OS_TIMER_STOP(&(__net)->rxt_timer.timer); \
157 			(void)SCTP_OS_TIMER_STOP(&(__net)->pmtu_timer.timer); \
158 			(void)SCTP_OS_TIMER_STOP(&(__net)->fr_timer.timer); \
159                         if ((__net)->ro.ro_rt) { \
160 				RTFREE((__net)->ro.ro_rt); \
161 				(__net)->ro.ro_rt = NULL; \
162                         } \
163 			if ((__net)->src_addr_selected) { \
164 				sctp_free_ifa((__net)->ro._s_addr); \
165 				(__net)->ro._s_addr = NULL; \
166 			} \
167                         (__net)->src_addr_selected = 0; \
168 			(__net)->dest_state = SCTP_ADDR_NOT_REACHABLE; \
169 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_net), (__net)); \
170 			SCTP_DECR_RADDR_COUNT(); \
171 		} \
172 	} \
173 }
174 
175 #define sctp_sbfree(ctl, stcb, sb, m) { \
176 	SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_cc, SCTP_BUF_LEN((m))); \
177 	SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_mbcnt, MSIZE); \
178 	if (((ctl)->do_not_ref_stcb == 0) && stcb) {\
179 		SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \
180 		SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
181 	} \
182 	if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
183 	    SCTP_BUF_TYPE(m) != MT_OOBDATA) \
184 		atomic_subtract_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
185 }
186 
187 #define sctp_sballoc(stcb, sb, m) { \
188 	atomic_add_int(&(sb)->sb_cc,SCTP_BUF_LEN((m))); \
189 	atomic_add_int(&(sb)->sb_mbcnt, MSIZE); \
190 	if (stcb) { \
191 		atomic_add_int(&(stcb)->asoc.sb_cc,SCTP_BUF_LEN((m))); \
192 		atomic_add_int(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \
193 	} \
194 	if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
195 	    SCTP_BUF_TYPE(m) != MT_OOBDATA) \
196 		atomic_add_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
197 }
198 
199 
200 #define sctp_ucount_incr(val) { \
201 	val++; \
202 }
203 
204 #define sctp_ucount_decr(val) { \
205 	if (val > 0) { \
206 		val--; \
207 	} else { \
208 		val = 0; \
209 	} \
210 }
211 
212 #define sctp_mbuf_crush(data) do { \
213 	struct mbuf *_m; \
214 	_m = (data); \
215 	while(_m && (SCTP_BUF_LEN(_m) == 0)) { \
216 		(data)  = SCTP_BUF_NEXT(_m); \
217 		SCTP_BUF_NEXT(_m) = NULL; \
218 		sctp_m_free(_m); \
219 		_m = (data); \
220 	} \
221 } while (0)
222 
223 #define sctp_flight_size_decrease(tp1) do { \
224 	if (tp1->whoTo->flight_size >= tp1->book_size) \
225 		tp1->whoTo->flight_size -= tp1->book_size; \
226 	else \
227 		tp1->whoTo->flight_size = 0; \
228 } while (0)
229 
230 #define sctp_flight_size_increase(tp1) do { \
231        (tp1)->whoTo->flight_size += (tp1)->book_size; \
232 } while (0)
233 
234 #ifdef SCTP_FS_SPEC_LOG
235 #define sctp_total_flight_decrease(stcb, tp1) do { \
236         if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \
237 		stcb->asoc.fs_index = 0;\
238 	stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \
239 	stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.TSN_seq; \
240 	stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \
241 	stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \
242 	stcb->asoc.fslog[stcb->asoc.fs_index].incr = 0; \
243 	stcb->asoc.fslog[stcb->asoc.fs_index].decr = 1; \
244 	stcb->asoc.fs_index++; \
245         tp1->window_probe = 0; \
246 	if (stcb->asoc.total_flight >= tp1->book_size) { \
247 		stcb->asoc.total_flight -= tp1->book_size; \
248 		if (stcb->asoc.total_flight_count > 0) \
249 			stcb->asoc.total_flight_count--; \
250 	} else { \
251 		stcb->asoc.total_flight = 0; \
252 		stcb->asoc.total_flight_count = 0; \
253 	} \
254 } while (0)
255 
256 #define sctp_total_flight_increase(stcb, tp1) do { \
257         if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \
258 		stcb->asoc.fs_index = 0;\
259 	stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \
260 	stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.TSN_seq; \
261 	stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \
262 	stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \
263 	stcb->asoc.fslog[stcb->asoc.fs_index].incr = 1; \
264 	stcb->asoc.fslog[stcb->asoc.fs_index].decr = 0; \
265 	stcb->asoc.fs_index++; \
266        (stcb)->asoc.total_flight_count++; \
267        (stcb)->asoc.total_flight += (tp1)->book_size; \
268 } while (0)
269 
270 #else
271 
272 #define sctp_total_flight_decrease(stcb, tp1) do { \
273         tp1->window_probe = 0; \
274 	if (stcb->asoc.total_flight >= tp1->book_size) { \
275 		stcb->asoc.total_flight -= tp1->book_size; \
276 		if (stcb->asoc.total_flight_count > 0) \
277 			stcb->asoc.total_flight_count--; \
278 	} else { \
279 		stcb->asoc.total_flight = 0; \
280 		stcb->asoc.total_flight_count = 0; \
281 	} \
282 } while (0)
283 
284 #define sctp_total_flight_increase(stcb, tp1) do { \
285        (stcb)->asoc.total_flight_count++; \
286        (stcb)->asoc.total_flight += (tp1)->book_size; \
287 } while (0)
288 
289 #endif
290 
291 
292 struct sctp_nets;
293 struct sctp_inpcb;
294 struct sctp_tcb;
295 struct sctphdr;
296 
297 
298 void sctp_close(struct socket *so);
299 int sctp_disconnect(struct socket *so);
300 
301 void sctp_ctlinput __P((int, struct sockaddr *, void *));
302 int sctp_ctloutput __P((struct socket *, struct sockopt *));
303 
304 #ifdef INET
305 void sctp_input_with_port __P((struct mbuf *, int, uint16_t));
306 
307 #endif
308 #ifdef INET
309 void sctp_input __P((struct mbuf *, int));
310 
311 #endif
312 void sctp_pathmtu_adjustment __P((struct sctp_inpcb *, struct sctp_tcb *, struct sctp_nets *, uint16_t));
313 void sctp_drain __P((void));
314 void sctp_init __P((void));
315 
316 void sctp_finish(void);
317 
318 int sctp_flush(struct socket *, int);
319 int sctp_shutdown __P((struct socket *));
320 void sctp_notify
321 __P((struct sctp_inpcb *, struct ip *ip, struct sctphdr *,
322     struct sockaddr *, struct sctp_tcb *,
323     struct sctp_nets *));
324 
325 	int sctp_bindx(struct socket *, int, struct sockaddr_storage *,
326         int, int, struct proc *);
327 
328 /* can't use sctp_assoc_t here */
329 	int sctp_peeloff(struct socket *, struct socket *, int, caddr_t, int *);
330 
331 	int sctp_ingetaddr(struct socket *,
332         struct sockaddr **
333 );
334 
335 	int sctp_peeraddr(struct socket *,
336         struct sockaddr **
337 );
338 
339 	int sctp_listen(struct socket *, int, struct thread *);
340 
341 	int sctp_accept(struct socket *, struct sockaddr **);
342 
343 #endif				/* _KERNEL */
344 
345 #endif				/* !_NETINET_SCTP_VAR_H_ */
346