xref: /freebsd/sys/netinet/sctp_var.h (revision 94942af266ac119ede0ca836f9aa5a5ac0582938)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_var.h,v 1.24 2005/03/06 16:04:19 itojun Exp $	 */
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)
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)
49 #define sctp_is_feature_off(inp, feature) ((inp->sctp_features & feature) == 0)
50 
51 #define	sctp_sbspace(asoc, sb) ((long) (((sb)->sb_hiwat > (asoc)->sb_cc) ? ((sb)->sb_hiwat - (asoc)->sb_cc) : 0))
52 
53 #define	sctp_sbspace_failedmsgs(sb) ((long) (((sb)->sb_hiwat > (sb)->sb_cc) ? ((sb)->sb_hiwat - (sb)->sb_cc) : 0))
54 
55 #define sctp_sbspace_sub(a,b) ((a > b) ? (a - b) : 0)
56 
57 /*
58  * I tried to cache the readq entries at one point. But the reality
59  * is that it did not add any performance since this meant we had to
60  * lock the STCB on read. And at that point once you have to do an
61  * extra lock, it really does not matter if the lock is in the ZONE
62  * stuff or in our code. Note that this same problem would occur with
63  * an mbuf cache as well so it is not really worth doing, at least
64  * right now :-D
65  */
66 
67 #define sctp_free_a_readq(_stcb, _readq) { \
68 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, (_readq)); \
69 	SCTP_DECR_READQ_COUNT(); \
70 }
71 
72 #define sctp_alloc_a_readq(_stcb, _readq) { \
73 	(_readq) = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_readq, struct sctp_queued_to_read); \
74 	if ((_readq)) { \
75  	     SCTP_INCR_READQ_COUNT(); \
76 	} \
77 }
78 
79 #define sctp_free_a_strmoq(_stcb, _strmoq) { \
80 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, (_strmoq)); \
81 	SCTP_DECR_STRMOQ_COUNT(); \
82 }
83 
84 #define sctp_alloc_a_strmoq(_stcb, _strmoq) { \
85 	(_strmoq) = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_strmoq, struct sctp_stream_queue_pending); \
86 	if ((_strmoq)) { \
87 		SCTP_INCR_STRMOQ_COUNT(); \
88  	} \
89 }
90 
91 
92 #define sctp_free_a_chunk(_stcb, _chk) { \
93 	if (((_stcb)->asoc.free_chunk_cnt > sctp_asoc_free_resc_limit) || \
94 	    (sctppcbinfo.ipi_free_chunks > sctp_system_free_resc_limit)) { \
95 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, (_chk)); \
96 		SCTP_DECR_CHK_COUNT(); \
97 	} else { \
98 		TAILQ_INSERT_TAIL(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
99 		(_stcb)->asoc.free_chunk_cnt++; \
100 		atomic_add_int(&sctppcbinfo.ipi_free_chunks, 1); \
101 	} \
102 }
103 
104 #define sctp_alloc_a_chunk(_stcb, _chk) { \
105 	if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks))  { \
106 		(_chk) = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk, struct sctp_tmit_chunk); \
107 		if ((_chk)) { \
108 			SCTP_INCR_CHK_COUNT(); \
109 		} \
110 	} else { \
111 		(_chk) = TAILQ_FIRST(&(_stcb)->asoc.free_chunks); \
112 		TAILQ_REMOVE(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
113 		atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1); \
114                 SCTP_STAT_INCR(sctps_cached_chk); \
115 		(_stcb)->asoc.free_chunk_cnt--; \
116 	} \
117 }
118 
119 
120 
121 #define sctp_free_remote_addr(__net) { \
122 	if ((__net)) {  \
123 		if (atomic_fetchadd_int(&(__net)->ref_count, -1) == 1) { \
124 			(void)SCTP_OS_TIMER_STOP(&(__net)->rxt_timer.timer); \
125 			(void)SCTP_OS_TIMER_STOP(&(__net)->pmtu_timer.timer); \
126 			(void)SCTP_OS_TIMER_STOP(&(__net)->fr_timer.timer); \
127                         if ((__net)->ro.ro_rt) { \
128 				RTFREE((__net)->ro.ro_rt); \
129 				(__net)->ro.ro_rt = NULL; \
130                         } \
131 			if ((__net)->src_addr_selected) { \
132 				sctp_free_ifa((__net)->ro._s_addr); \
133 				(__net)->ro._s_addr = NULL; \
134 			} \
135                         (__net)->src_addr_selected = 0; \
136 			(__net)->dest_state = SCTP_ADDR_NOT_REACHABLE; \
137 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, (__net)); \
138 			SCTP_DECR_RADDR_COUNT(); \
139 		} \
140 	} \
141 }
142 
143 #define sctp_sbfree(ctl, stcb, sb, m) { \
144 	uint32_t val; \
145 	val = atomic_fetchadd_int(&(sb)->sb_cc,-(SCTP_BUF_LEN((m)))); \
146 	if (val < SCTP_BUF_LEN((m))) { \
147 	   panic("sb_cc goes negative"); \
148 	} \
149 	val = atomic_fetchadd_int(&(sb)->sb_mbcnt,-(MSIZE)); \
150 	if (val < MSIZE) { \
151 	    panic("sb_mbcnt goes negative"); \
152 	} \
153 	if (SCTP_BUF_IS_EXTENDED(m)) { \
154 		val = atomic_fetchadd_int(&(sb)->sb_mbcnt,-(SCTP_BUF_EXTEND_SIZE(m))); \
155 		if (val < SCTP_BUF_EXTEND_SIZE(m)) { \
156 		    panic("sb_mbcnt goes negative2"); \
157 		} \
158 	} \
159 	if (((ctl)->do_not_ref_stcb == 0) && stcb) {\
160 	  val = atomic_fetchadd_int(&(stcb)->asoc.sb_cc,-(SCTP_BUF_LEN((m)))); \
161 	  if (val < SCTP_BUF_LEN((m))) {\
162 	     panic("stcb->sb_cc goes negative"); \
163 	  } \
164 	  val = atomic_fetchadd_int(&(stcb)->asoc.sb_mbcnt,-(MSIZE)); \
165 	  if (val < MSIZE) { \
166 	     panic("asoc->mbcnt goes negative"); \
167 	  } \
168 	  if (SCTP_BUF_IS_EXTENDED(m)) { \
169 		val = atomic_fetchadd_int(&(stcb)->asoc.sb_mbcnt,-(SCTP_BUF_EXTEND_SIZE(m))); \
170 		if (val < SCTP_BUF_EXTEND_SIZE(m)) { \
171 		   panic("assoc stcb->mbcnt would go negative"); \
172 		} \
173 	  } \
174 	} \
175 	if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
176 	    SCTP_BUF_TYPE(m) != MT_OOBDATA) \
177 		atomic_subtract_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
178 }
179 
180 
181 #define sctp_sballoc(stcb, sb, m) { \
182 	atomic_add_int(&(sb)->sb_cc,SCTP_BUF_LEN((m))); \
183 	atomic_add_int(&(sb)->sb_mbcnt, MSIZE); \
184 	if (SCTP_BUF_IS_EXTENDED(m)) \
185 		atomic_add_int(&(sb)->sb_mbcnt,SCTP_BUF_EXTEND_SIZE(m)); \
186 	if (stcb) { \
187 		atomic_add_int(&(stcb)->asoc.sb_cc,SCTP_BUF_LEN((m))); \
188 		atomic_add_int(&(stcb)->asoc.sb_mbcnt, MSIZE); \
189 		if (SCTP_BUF_IS_EXTENDED(m)) \
190 			atomic_add_int(&(stcb)->asoc.sb_mbcnt,SCTP_BUF_EXTEND_SIZE(m)); \
191 	} \
192 	if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
193 	    SCTP_BUF_TYPE(m) != MT_OOBDATA) \
194 		atomic_add_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
195 }
196 
197 
198 #define sctp_ucount_incr(val) { \
199 	val++; \
200 }
201 
202 #define sctp_ucount_decr(val) { \
203 	if (val > 0) { \
204 		val--; \
205 	} else { \
206 		val = 0; \
207 	} \
208 }
209 
210 #define sctp_mbuf_crush(data) do { \
211 	struct mbuf *_m; \
212 	_m = (data); \
213 	while(_m && (SCTP_BUF_LEN(_m) == 0)) { \
214 		(data)  = SCTP_BUF_NEXT(_m); \
215 		SCTP_BUF_NEXT(_m) = NULL; \
216 		sctp_m_free(_m); \
217 		_m = (data); \
218 	} \
219 } while (0)
220 
221 #ifdef RANDY_WILL_USE_LATER	/* this will be the non-invarant version */
222 #define sctp_flight_size_decrease(tp1) do { \
223 	if (tp1->whoTo->flight_size >= tp1->book_size) \
224 		tp1->whoTo->flight_size -= tp1->book_size; \
225 	else \
226 		tp1->whoTo->flight_size = 0; \
227 } while (0)
228 
229 
230 #define sctp_total_flight_decrease(stcb, tp1) do { \
231 	if (stcb->asoc.total_flight >= tp1->book_size) { \
232 		stcb->asoc.total_flight -= tp1->book_size; \
233 		if (stcb->asoc.total_flight_count > 0) \
234 			stcb->asoc.total_flight_count--; \
235 	} else { \
236 		stcb->asoc.total_flight = 0; \
237 		stcb->asoc.total_flight_count = 0; \
238 	} \
239 } while (0)
240 
241 #else
242 
243 #define sctp_flight_size_decrease(tp1) do { \
244 	if (tp1->whoTo->flight_size >= tp1->book_size) \
245 		tp1->whoTo->flight_size -= tp1->book_size; \
246 	else \
247 		panic("flight size corruption"); \
248 } while (0)
249 
250 
251 #define sctp_total_flight_decrease(stcb, tp1) do { \
252 	if (stcb->asoc.total_flight >= tp1->book_size) { \
253 		stcb->asoc.total_flight -= tp1->book_size; \
254 		if (stcb->asoc.total_flight_count > 0) \
255 			stcb->asoc.total_flight_count--; \
256 	} else { \
257 		panic("total flight size corruption"); \
258 	} \
259 } while (0)
260 
261 #endif
262 
263 #define sctp_flight_size_increase(tp1) do { \
264        (tp1)->whoTo->flight_size += (tp1)->book_size; \
265 } while (0)
266 
267 
268 #define sctp_total_flight_increase(stcb, tp1) do { \
269        (stcb)->asoc.total_flight_count++; \
270        (stcb)->asoc.total_flight += (tp1)->book_size; \
271 } while (0)
272 
273 struct sctp_nets;
274 struct sctp_inpcb;
275 struct sctp_tcb;
276 struct sctphdr;
277 
278 void sctp_ctlinput __P((int, struct sockaddr *, void *));
279 int sctp_ctloutput __P((struct socket *, struct sockopt *));
280 void sctp_input __P((struct mbuf *, int));
281 void sctp_drain __P((void));
282 void sctp_init __P((void));
283 
284 
285 void sctp_pcbinfo_cleanup(void);
286 
287 int sctp_shutdown __P((struct socket *));
288 void sctp_notify
289 __P((struct sctp_inpcb *, int, struct sctphdr *,
290     struct sockaddr *, struct sctp_tcb *,
291     struct sctp_nets *));
292 
293 	int sctp_bindx(struct socket *, int, struct sockaddr_storage *,
294         int, int, struct proc *);
295 
296 /* can't use sctp_assoc_t here */
297 	int sctp_peeloff(struct socket *, struct socket *, int, caddr_t, int *);
298 
299 	int sctp_ingetaddr(struct socket *,
300         struct sockaddr **
301 );
302 
303 	int sctp_peeraddr(struct socket *,
304         struct sockaddr **
305 );
306 
307 	int sctp_listen(struct socket *, int, struct thread *);
308 
309 	int sctp_accept(struct socket *, struct sockaddr **);
310 
311 #endif				/* _KERNEL */
312 
313 #endif				/* !_NETINET_SCTP_VAR_H_ */
314