1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * a) Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 14 * b) Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the distribution. 17 * 18 * c) Neither the name of Cisco Systems, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 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 protosw sctp_seqpacket_protosw, sctp_stream_protosw; 44 45 #define sctp_feature_on(inp, feature) (inp->sctp_features |= feature) 46 #define sctp_feature_off(inp, feature) (inp->sctp_features &= ~feature) 47 #define sctp_is_feature_on(inp, feature) ((inp->sctp_features & feature) == feature) 48 #define sctp_is_feature_off(inp, feature) ((inp->sctp_features & feature) == 0) 49 50 #define sctp_stcb_feature_on(inp, stcb, feature) {\ 51 if (stcb) { \ 52 stcb->asoc.sctp_features |= feature; \ 53 } else if (inp) { \ 54 inp->sctp_features |= feature; \ 55 } \ 56 } 57 #define sctp_stcb_feature_off(inp, stcb, feature) {\ 58 if (stcb) { \ 59 stcb->asoc.sctp_features &= ~feature; \ 60 } else if (inp) { \ 61 inp->sctp_features &= ~feature; \ 62 } \ 63 } 64 #define sctp_stcb_is_feature_on(inp, stcb, feature) \ 65 (((stcb != NULL) && \ 66 ((stcb->asoc.sctp_features & feature) == feature)) || \ 67 ((stcb == NULL) && (inp != NULL) && \ 68 ((inp->sctp_features & feature) == feature))) 69 #define sctp_stcb_is_feature_off(inp, stcb, feature) \ 70 (((stcb != NULL) && \ 71 ((stcb->asoc.sctp_features & feature) == 0)) || \ 72 ((stcb == NULL) && (inp != NULL) && \ 73 ((inp->sctp_features & feature) == 0)) || \ 74 ((stcb == NULL) && (inp == NULL))) 75 76 /* managing mobility_feature in inpcb (by micchie) */ 77 #define sctp_mobility_feature_on(inp, feature) (inp->sctp_mobility_features |= feature) 78 #define sctp_mobility_feature_off(inp, feature) (inp->sctp_mobility_features &= ~feature) 79 #define sctp_is_mobility_feature_on(inp, feature) (inp->sctp_mobility_features & feature) 80 #define sctp_is_mobility_feature_off(inp, feature) ((inp->sctp_mobility_features & feature) == 0) 81 82 #define sctp_maxspace(sb) (max((sb)->sb_hiwat,SCTP_MINIMAL_RWND)) 83 84 #define sctp_sbspace(asoc, sb) ((long) ((sctp_maxspace(sb) > (asoc)->sb_cc) ? (sctp_maxspace(sb) - (asoc)->sb_cc) : 0)) 85 86 #define sctp_sbspace_failedmsgs(sb) ((long) ((sctp_maxspace(sb) > SCTP_SBAVAIL(sb)) ? (sctp_maxspace(sb) - SCTP_SBAVAIL(sb)) : 0)) 87 88 #define sctp_sbspace_sub(a,b) (((a) > (b)) ? ((a) - (b)) : 0) 89 90 /* 91 * I tried to cache the readq entries at one point. But the reality 92 * is that it did not add any performance since this meant we had to 93 * lock the STCB on read. And at that point once you have to do an 94 * extra lock, it really does not matter if the lock is in the ZONE 95 * stuff or in our code. Note that this same problem would occur with 96 * an mbuf cache as well so it is not really worth doing, at least 97 * right now :-D 98 */ 99 #ifdef INVARIANTS 100 #define sctp_free_a_readq(_stcb, _readq) { \ 101 if ((_readq)->on_strm_q) \ 102 panic("On strm q stcb:%p readq:%p", (_stcb), (_readq)); \ 103 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), (_readq)); \ 104 SCTP_DECR_READQ_COUNT(); \ 105 } 106 #else 107 #define sctp_free_a_readq(_stcb, _readq) { \ 108 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), (_readq)); \ 109 SCTP_DECR_READQ_COUNT(); \ 110 } 111 #endif 112 113 #define sctp_alloc_a_readq(_stcb, _readq) { \ 114 (_readq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_readq), struct sctp_queued_to_read); \ 115 if ((_readq)) { \ 116 SCTP_INCR_READQ_COUNT(); \ 117 } \ 118 } 119 120 #define sctp_free_a_strmoq(_stcb, _strmoq, _so_locked) { \ 121 if ((_strmoq)->holds_key_ref) { \ 122 sctp_auth_key_release(stcb, sp->auth_keyid, _so_locked); \ 123 (_strmoq)->holds_key_ref = 0; \ 124 } \ 125 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), (_strmoq)); \ 126 SCTP_DECR_STRMOQ_COUNT(); \ 127 } 128 129 #define sctp_alloc_a_strmoq(_stcb, _strmoq) { \ 130 (_strmoq) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_strmoq), struct sctp_stream_queue_pending); \ 131 if ((_strmoq)) { \ 132 memset(_strmoq, 0, sizeof(struct sctp_stream_queue_pending)); \ 133 SCTP_INCR_STRMOQ_COUNT(); \ 134 (_strmoq)->holds_key_ref = 0; \ 135 } \ 136 } 137 138 #define sctp_free_a_chunk(_stcb, _chk, _so_locked) { \ 139 if ((_chk)->holds_key_ref) {\ 140 sctp_auth_key_release((_stcb), (_chk)->auth_keyid, _so_locked); \ 141 (_chk)->holds_key_ref = 0; \ 142 } \ 143 if (_stcb) { \ 144 SCTP_TCB_LOCK_ASSERT((_stcb)); \ 145 if ((_chk)->whoTo) { \ 146 sctp_free_remote_addr((_chk)->whoTo); \ 147 (_chk)->whoTo = NULL; \ 148 } \ 149 if (((_stcb)->asoc.free_chunk_cnt > SCTP_BASE_SYSCTL(sctp_asoc_free_resc_limit)) || \ 150 (SCTP_BASE_INFO(ipi_free_chunks) > SCTP_BASE_SYSCTL(sctp_system_free_resc_limit))) { \ 151 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \ 152 SCTP_DECR_CHK_COUNT(); \ 153 } else { \ 154 TAILQ_INSERT_TAIL(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \ 155 (_stcb)->asoc.free_chunk_cnt++; \ 156 atomic_add_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \ 157 } \ 158 } else { \ 159 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), (_chk)); \ 160 SCTP_DECR_CHK_COUNT(); \ 161 } \ 162 } 163 164 #define sctp_alloc_a_chunk(_stcb, _chk) { \ 165 if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks)) { \ 166 (_chk) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_chunk), struct sctp_tmit_chunk); \ 167 if ((_chk)) { \ 168 SCTP_INCR_CHK_COUNT(); \ 169 (_chk)->whoTo = NULL; \ 170 (_chk)->holds_key_ref = 0; \ 171 } \ 172 } else { \ 173 (_chk) = TAILQ_FIRST(&(_stcb)->asoc.free_chunks); \ 174 TAILQ_REMOVE(&(_stcb)->asoc.free_chunks, (_chk), sctp_next); \ 175 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); \ 176 (_chk)->holds_key_ref = 0; \ 177 SCTP_STAT_INCR(sctps_cached_chk); \ 178 (_stcb)->asoc.free_chunk_cnt--; \ 179 } \ 180 } 181 182 #define sctp_free_remote_addr(__net) { \ 183 if ((__net)) { \ 184 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&(__net)->ref_count)) { \ 185 RO_NHFREE(&(__net)->ro); \ 186 if ((__net)->src_addr_selected) { \ 187 sctp_free_ifa((__net)->ro._s_addr); \ 188 (__net)->ro._s_addr = NULL; \ 189 } \ 190 (__net)->src_addr_selected = 0; \ 191 (__net)->dest_state &= ~SCTP_ADDR_REACHABLE; \ 192 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_net), (__net)); \ 193 SCTP_DECR_RADDR_COUNT(); \ 194 } \ 195 } \ 196 } 197 198 #define sctp_sbfree(ctl, stcb, sb, m) { \ 199 SCTP_SB_DECR(sb, SCTP_BUF_LEN((m))); \ 200 SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_mbcnt, MSIZE); \ 201 if (((ctl)->do_not_ref_stcb == 0) && stcb) {\ 202 SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \ 203 SCTP_SAVE_ATOMIC_DECREMENT(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \ 204 } \ 205 if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \ 206 SCTP_BUF_TYPE(m) != MT_OOBDATA) \ 207 atomic_subtract_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \ 208 } 209 210 #define sctp_sballoc(stcb, sb, m) { \ 211 SCTP_SB_INCR(sb, SCTP_BUF_LEN((m))); \ 212 atomic_add_int(&(sb)->sb_mbcnt, MSIZE); \ 213 if (stcb) { \ 214 atomic_add_int(&(stcb)->asoc.sb_cc, SCTP_BUF_LEN((m))); \ 215 atomic_add_int(&(stcb)->asoc.my_rwnd_control_len, MSIZE); \ 216 } \ 217 if (SCTP_BUF_TYPE(m) != MT_DATA && SCTP_BUF_TYPE(m) != MT_HEADER && \ 218 SCTP_BUF_TYPE(m) != MT_OOBDATA) \ 219 atomic_add_int(&(sb)->sb_ctl,SCTP_BUF_LEN((m))); \ 220 } 221 222 #define sctp_ucount_incr(val) { \ 223 val++; \ 224 } 225 226 #define sctp_ucount_decr(val) { \ 227 if (val > 0) { \ 228 val--; \ 229 } else { \ 230 val = 0; \ 231 } \ 232 } 233 234 #define sctp_mbuf_crush(data) do { \ 235 struct mbuf *_m; \ 236 _m = (data); \ 237 while (_m && (SCTP_BUF_LEN(_m) == 0)) { \ 238 (data) = SCTP_BUF_NEXT(_m); \ 239 SCTP_BUF_NEXT(_m) = NULL; \ 240 sctp_m_free(_m); \ 241 _m = (data); \ 242 } \ 243 } while (0) 244 245 #define sctp_flight_size_decrease(tp1) do { \ 246 if (tp1->whoTo->flight_size >= tp1->book_size) \ 247 tp1->whoTo->flight_size -= tp1->book_size; \ 248 else \ 249 tp1->whoTo->flight_size = 0; \ 250 } while (0) 251 252 #define sctp_flight_size_increase(tp1) do { \ 253 (tp1)->whoTo->flight_size += (tp1)->book_size; \ 254 } while (0) 255 256 #ifdef SCTP_FS_SPEC_LOG 257 #define sctp_total_flight_decrease(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; \ 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 = 0; \ 265 stcb->asoc.fslog[stcb->asoc.fs_index].decr = 1; \ 266 stcb->asoc.fs_index++; \ 267 tp1->window_probe = 0; \ 268 if (stcb->asoc.total_flight >= tp1->book_size) { \ 269 stcb->asoc.total_flight -= tp1->book_size; \ 270 if (stcb->asoc.total_flight_count > 0) \ 271 stcb->asoc.total_flight_count--; \ 272 } else { \ 273 stcb->asoc.total_flight = 0; \ 274 stcb->asoc.total_flight_count = 0; \ 275 } \ 276 } while (0) 277 278 #define sctp_total_flight_increase(stcb, tp1) do { \ 279 if (stcb->asoc.fs_index > SCTP_FS_SPEC_LOG_SIZE) \ 280 stcb->asoc.fs_index = 0;\ 281 stcb->asoc.fslog[stcb->asoc.fs_index].total_flight = stcb->asoc.total_flight; \ 282 stcb->asoc.fslog[stcb->asoc.fs_index].tsn = tp1->rec.data.tsn; \ 283 stcb->asoc.fslog[stcb->asoc.fs_index].book = tp1->book_size; \ 284 stcb->asoc.fslog[stcb->asoc.fs_index].sent = tp1->sent; \ 285 stcb->asoc.fslog[stcb->asoc.fs_index].incr = 1; \ 286 stcb->asoc.fslog[stcb->asoc.fs_index].decr = 0; \ 287 stcb->asoc.fs_index++; \ 288 (stcb)->asoc.total_flight_count++; \ 289 (stcb)->asoc.total_flight += (tp1)->book_size; \ 290 } while (0) 291 292 #else 293 294 #define sctp_total_flight_decrease(stcb, tp1) do { \ 295 tp1->window_probe = 0; \ 296 if (stcb->asoc.total_flight >= tp1->book_size) { \ 297 stcb->asoc.total_flight -= tp1->book_size; \ 298 if (stcb->asoc.total_flight_count > 0) \ 299 stcb->asoc.total_flight_count--; \ 300 } else { \ 301 stcb->asoc.total_flight = 0; \ 302 stcb->asoc.total_flight_count = 0; \ 303 } \ 304 } while (0) 305 306 #define sctp_total_flight_increase(stcb, tp1) do { \ 307 (stcb)->asoc.total_flight_count++; \ 308 (stcb)->asoc.total_flight += (tp1)->book_size; \ 309 } while (0) 310 311 #endif 312 313 #define SCTP_PF_ENABLED(_net) (_net->pf_threshold < _net->failure_threshold) 314 #define SCTP_NET_IS_PF(_net) (_net->pf_threshold < _net->error_count) 315 316 struct sctp_nets; 317 struct sctp_inpcb; 318 struct sctp_tcb; 319 struct sctphdr; 320 321 void sctp_close(struct socket *so); 322 void sctp_abort(struct socket *so); 323 int sctp_disconnect(struct socket *so); 324 ipproto_ctlinput_t sctp_ctlinput; 325 int sctp_ctloutput(struct socket *, struct sockopt *); 326 #ifdef INET 327 void sctp_input_with_port(struct mbuf *, int, uint16_t); 328 int sctp_input(struct mbuf **, int *, int); 329 #endif 330 void sctp_pathmtu_adjustment(struct sctp_tcb *, uint32_t, bool); 331 void 332 sctp_notify(struct sctp_inpcb *, struct sctp_tcb *, struct sctp_nets *, 333 uint8_t, uint8_t, uint16_t, uint32_t); 334 int sctp_flush(struct socket *, int); 335 int sctp_shutdown(struct socket *); 336 int 337 sctp_bindx(struct socket *, int, struct sockaddr_storage *, 338 int, int, struct proc *); 339 340 /* can't use sctp_assoc_t here */ 341 int sctp_peeloff(struct socket *, struct socket *, int, caddr_t, int *); 342 int sctp_ingetaddr(struct socket *, struct sockaddr **); 343 int sctp_peeraddr(struct socket *, struct sockaddr **); 344 int sctp_listen(struct socket *, int, struct thread *); 345 int sctp_accept(struct socket *, struct sockaddr **); 346 347 #endif /* _KERNEL */ 348 349 #endif /* !_NETINET_SCTP_VAR_H_ */ 350