xref: /freebsd/sys/netinet/sctp_var.h (revision 18242d3b09dbc3f5e278e39baaa3c3b76624c901)
1 /*-
2  * Copyright (c) 2001-2007, 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 	if (((_stcb)->asoc.free_strmoq_cnt > sctp_asoc_free_resc_limit) || \
81 	    (sctppcbinfo.ipi_free_strmoq > sctp_system_free_resc_limit)) { \
82 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, (_strmoq)); \
83 		SCTP_DECR_STRMOQ_COUNT(); \
84 	} else { \
85 		TAILQ_INSERT_TAIL(&(_stcb)->asoc.free_strmoq, (_strmoq), next); \
86 		(_stcb)->asoc.free_strmoq_cnt++; \
87 		atomic_add_int(&sctppcbinfo.ipi_free_strmoq, 1); \
88 	} \
89 }
90 
91 #define sctp_alloc_a_strmoq(_stcb, _strmoq) { \
92 	if (TAILQ_EMPTY(&(_stcb)->asoc.free_strmoq))  { \
93 		(_strmoq) = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_strmoq, struct sctp_stream_queue_pending); \
94 		if ((_strmoq)) { \
95 			SCTP_INCR_STRMOQ_COUNT(); \
96 		} \
97 	} else { \
98 		(_strmoq) = TAILQ_FIRST(&(_stcb)->asoc.free_strmoq); \
99 		TAILQ_REMOVE(&(_stcb)->asoc.free_strmoq, (_strmoq), next); \
100 		atomic_subtract_int(&sctppcbinfo.ipi_free_strmoq, 1); \
101                 SCTP_STAT_INCR(sctps_cached_strmoq); \
102 		(_stcb)->asoc.free_strmoq_cnt--; \
103 	} \
104 }
105 
106 
107 #define sctp_free_a_chunk(_stcb, _chk) { \
108 	if (((_stcb)->asoc.free_chunk_cnt > sctp_asoc_free_resc_limit) || \
109 	    (sctppcbinfo.ipi_free_chunks > sctp_system_free_resc_limit)) { \
110 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, (_chk)); \
111 		SCTP_DECR_CHK_COUNT(); \
112 	} else { \
113 		TAILQ_INSERT_TAIL(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
114 		(_stcb)->asoc.free_chunk_cnt++; \
115 		atomic_add_int(&sctppcbinfo.ipi_free_chunks, 1); \
116 	} \
117 }
118 
119 #define sctp_alloc_a_chunk(_stcb, _chk) { \
120 	if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks))  { \
121 		(_chk) = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk, struct sctp_tmit_chunk); \
122 		if ((_chk)) { \
123 			SCTP_INCR_CHK_COUNT(); \
124 		} \
125 	} else { \
126 		(_chk) = TAILQ_FIRST(&(_stcb)->asoc.free_chunks); \
127 		TAILQ_REMOVE(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \
128 		atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1); \
129                 SCTP_STAT_INCR(sctps_cached_chk); \
130 		(_stcb)->asoc.free_chunk_cnt--; \
131 	} \
132 }
133 
134 
135 
136 #define sctp_free_remote_addr(__net) { \
137 	if ((__net)) {  \
138 		if (atomic_fetchadd_int(&(__net)->ref_count, -1) == 1) { \
139 			SCTP_OS_TIMER_STOP(&(__net)->rxt_timer.timer); \
140 			SCTP_OS_TIMER_STOP(&(__net)->pmtu_timer.timer); \
141 			SCTP_OS_TIMER_STOP(&(__net)->fr_timer.timer); \
142                         if ((__net)->ro.ro_rt) { \
143 				RTFREE((__net)->ro.ro_rt); \
144 				(__net)->ro.ro_rt = NULL; \
145                         } \
146 			if ((__net)->src_addr_selected) { \
147 				sctp_free_ifa((__net)->ro._s_addr); \
148 				(__net)->ro._s_addr = NULL; \
149 			} \
150                         (__net)->src_addr_selected = 0; \
151 			(__net)->dest_state = SCTP_ADDR_NOT_REACHABLE; \
152 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, (__net)); \
153 			SCTP_DECR_RADDR_COUNT(); \
154 		} \
155 	} \
156 }
157 
158 #define sctp_sbfree(ctl, stcb, sb, m) { \
159 	uint32_t val; \
160 	val = atomic_fetchadd_int(&(sb)->sb_cc,-(SCTP_BUF_LEN((m)))); \
161 	if (val < SCTP_BUF_LEN((m))) { \
162 	   panic("sb_cc goes negative"); \
163 	} \
164 	val = atomic_fetchadd_int(&(sb)->sb_mbcnt,-(MSIZE)); \
165 	if (val < MSIZE) { \
166 	    panic("sb_mbcnt goes negative"); \
167 	} \
168 	if (SCTP_BUF_IS_EXTENDED(m)) { \
169 		val = atomic_fetchadd_int(&(sb)->sb_mbcnt,-(SCTP_BUF_EXTEND_SIZE(m))); \
170 		if (val < SCTP_BUF_EXTEND_SIZE(m)) { \
171 		    panic("sb_mbcnt goes negative2"); \
172 		} \
173 	} \
174 	if (((ctl)->do_not_ref_stcb == 0) && stcb) {\
175 	  val = atomic_fetchadd_int(&(stcb)->asoc.sb_cc,-(SCTP_BUF_LEN((m)))); \
176 	  if (val < SCTP_BUF_LEN((m))) {\
177 	     panic("stcb->sb_cc goes negative"); \
178 	  } \
179 	  val = atomic_fetchadd_int(&(stcb)->asoc.sb_mbcnt,-(MSIZE)); \
180 	  if (val < MSIZE) { \
181 	     panic("asoc->mbcnt goes negative"); \
182 	  } \
183 	  if (SCTP_BUF_IS_EXTENDED(m)) { \
184 		val = atomic_fetchadd_int(&(stcb)->asoc.sb_mbcnt,-(SCTP_BUF_EXTEND_SIZE(m))); \
185 		if (val < SCTP_BUF_EXTEND_SIZE(m)) { \
186 		   panic("assoc stcb->mbcnt would go negative"); \
187 		} \
188 	  } \
189 	} \
190 	if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
191 	    SCTP_BUF_TYPE(m) != MT_OOBDATA) \
192 		atomic_subtract_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
193 }
194 
195 
196 #define sctp_sballoc(stcb, sb, m) { \
197 	atomic_add_int(&(sb)->sb_cc,SCTP_BUF_LEN((m))); \
198 	atomic_add_int(&(sb)->sb_mbcnt, MSIZE); \
199 	if (SCTP_BUF_IS_EXTENDED(m)) \
200 		atomic_add_int(&(sb)->sb_mbcnt,SCTP_BUF_EXTEND_SIZE(m)); \
201 	if (stcb) { \
202 		atomic_add_int(&(stcb)->asoc.sb_cc,SCTP_BUF_LEN((m))); \
203 		atomic_add_int(&(stcb)->asoc.sb_mbcnt, MSIZE); \
204 		if (SCTP_BUF_IS_EXTENDED(m)) \
205 			atomic_add_int(&(stcb)->asoc.sb_mbcnt,SCTP_BUF_EXTEND_SIZE(m)); \
206 	} \
207 	if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \
208 	    SCTP_BUF_TYPE(m) != MT_OOBDATA) \
209 		atomic_add_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \
210 }
211 
212 
213 #define sctp_ucount_incr(val) { \
214 	val++; \
215 }
216 
217 #define sctp_ucount_decr(val) { \
218 	if (val > 0) { \
219 		val--; \
220 	} else { \
221 		val = 0; \
222 	} \
223 }
224 
225 #define sctp_mbuf_crush(data) do { \
226 	struct mbuf *_m; \
227 	_m = (data); \
228 	while(_m && (SCTP_BUF_LEN(_m) == 0)) { \
229 		(data)  = SCTP_BUF_NEXT(_m); \
230 		SCTP_BUF_NEXT(_m) = NULL; \
231 		sctp_m_free(_m); \
232 		_m = (data); \
233 	} \
234 } while (0)
235 
236 #ifdef RANDY_WILL_USE_LATER	/* this will be the non-invarant version */
237 #define sctp_flight_size_decrease(tp1) do { \
238 	if (tp1->whoTo->flight_size >= tp1->book_size) \
239 		tp1->whoTo->flight_size -= tp1->book_size; \
240 	else \
241 		tp1->whoTo->flight_size = 0; \
242 } while (0)
243 
244 
245 #define sctp_total_flight_decrease(stcb, tp1) do { \
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 #else
257 
258 #define sctp_flight_size_decrease(tp1) do { \
259 	if (tp1->whoTo->flight_size >= tp1->book_size) \
260 		tp1->whoTo->flight_size -= tp1->book_size; \
261 	else \
262 		panic("flight size corruption"); \
263 } while (0)
264 
265 
266 #define sctp_total_flight_decrease(stcb, tp1) do { \
267 	if (stcb->asoc.total_flight >= tp1->book_size) { \
268 		stcb->asoc.total_flight -= tp1->book_size; \
269 		if (stcb->asoc.total_flight_count > 0) \
270 			stcb->asoc.total_flight_count--; \
271 	} else { \
272 		panic("total flight size corruption"); \
273 	} \
274 } while (0)
275 
276 #endif
277 
278 #define sctp_flight_size_increase(tp1) do { \
279        (tp1)->whoTo->flight_size += (tp1)->book_size; \
280 } while (0)
281 
282 
283 #define sctp_total_flight_increase(stcb, tp1) do { \
284        (stcb)->asoc.total_flight_count++; \
285        (stcb)->asoc.total_flight += (tp1)->book_size; \
286 } while (0)
287 
288 struct sctp_nets;
289 struct sctp_inpcb;
290 struct sctp_tcb;
291 struct sctphdr;
292 
293 void sctp_ctlinput __P((int, struct sockaddr *, void *));
294 int sctp_ctloutput __P((struct socket *, struct sockopt *));
295 void sctp_input __P((struct mbuf *, int));
296 void sctp_drain __P((void));
297 void sctp_init __P((void));
298 
299 
300 void sctp_pcbinfo_cleanup(void);
301 
302 int sctp_shutdown __P((struct socket *));
303 void sctp_notify
304 __P((struct sctp_inpcb *, int, struct sctphdr *,
305     struct sockaddr *, struct sctp_tcb *,
306     struct sctp_nets *));
307 
308 	int sctp_bindx(struct socket *, int, struct sockaddr_storage *,
309         int, int, struct proc *);
310 
311 /* can't use sctp_assoc_t here */
312 	int sctp_peeloff(struct socket *, struct socket *, int, caddr_t, int *);
313 
314 	sctp_assoc_t sctp_getassocid(struct sockaddr *);
315 
316 
317 	int sctp_ingetaddr(struct socket *,
318         struct sockaddr **
319 );
320 
321 	int sctp_peeraddr(struct socket *,
322         struct sockaddr **
323 );
324 
325 	int sctp_listen(struct socket *, int, struct thread *);
326 
327 	int sctp_accept(struct socket *, struct sockaddr **);
328 
329 #endif				/* _KERNEL */
330 
331 #endif				/* !_NETINET_SCTP_VAR_H_ */
332