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