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