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