1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001-2007, 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 #ifdef __FreeBSD__ 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD: head/sys/netinet/sctputil.h 359405 2020-03-28 20:25:45Z tuexen $"); 38 #endif 39 40 #ifndef _NETINET_SCTP_UTIL_H_ 41 #define _NETINET_SCTP_UTIL_H_ 42 43 #if defined(_KERNEL) || defined(__Userspace__) 44 45 #define SCTP_READ_LOCK_HELD 1 46 #define SCTP_READ_LOCK_NOT_HELD 0 47 48 #ifdef SCTP_ASOCLOG_OF_TSNS 49 void sctp_print_out_track_log(struct sctp_tcb *stcb); 50 #endif 51 52 #ifdef SCTP_MBUF_LOGGING 53 struct mbuf *sctp_m_free(struct mbuf *m); 54 void sctp_m_freem(struct mbuf *m); 55 #else 56 #define sctp_m_free m_free 57 #define sctp_m_freem m_freem 58 #endif 59 60 #if defined(SCTP_LOCAL_TRACE_BUF) || defined(__APPLE__) 61 void 62 sctp_log_trace(uint32_t fr, const char *str SCTP_UNUSED, uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f); 63 #endif 64 65 #define sctp_get_associd(stcb) ((sctp_assoc_t)stcb->asoc.assoc_id) 66 67 68 /* 69 * Function prototypes 70 */ 71 int32_t 72 sctp_map_assoc_state(int); 73 74 uint32_t 75 sctp_get_ifa_hash_val(struct sockaddr *addr); 76 77 struct sctp_ifa * 78 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr, int hold_lock); 79 80 struct sctp_ifa * 81 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock); 82 83 uint32_t sctp_select_initial_TSN(struct sctp_pcb *); 84 85 uint32_t sctp_select_a_tag(struct sctp_inpcb *, uint16_t lport, uint16_t rport, int); 86 87 int sctp_init_asoc(struct sctp_inpcb *, struct sctp_tcb *, uint32_t, uint32_t, uint16_t); 88 89 void sctp_fill_random_store(struct sctp_pcb *); 90 91 void 92 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, 93 uint16_t numberout, int flag); 94 void 95 sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag); 96 97 void 98 sctp_timer_start(int, struct sctp_inpcb *, struct sctp_tcb *, 99 struct sctp_nets *); 100 101 void 102 sctp_timer_stop(int, struct sctp_inpcb *, struct sctp_tcb *, 103 struct sctp_nets *, uint32_t); 104 105 int 106 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id); 107 108 void 109 sctp_mtu_size_reset(struct sctp_inpcb *, struct sctp_association *, uint32_t); 110 111 void 112 sctp_wakeup_the_read_socket(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 113 int so_locked 114 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 115 SCTP_UNUSED 116 #endif 117 ); 118 119 #if defined(__Userspace__) 120 void sctp_invoke_recv_callback(struct sctp_inpcb *, 121 struct sctp_tcb *, 122 struct sctp_queued_to_read *, 123 int); 124 125 #endif 126 void 127 sctp_add_to_readq(struct sctp_inpcb *inp, 128 struct sctp_tcb *stcb, 129 struct sctp_queued_to_read *control, 130 struct sockbuf *sb, 131 int end, 132 int inpread_locked, 133 int so_locked 134 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 135 SCTP_UNUSED 136 #endif 137 ); 138 139 void sctp_iterator_worker(void); 140 141 uint32_t sctp_get_prev_mtu(uint32_t); 142 uint32_t sctp_get_next_mtu(uint32_t); 143 144 void 145 sctp_timeout_handler(void *); 146 147 int 148 sctp_calculate_rto(struct sctp_tcb *, struct sctp_association *, 149 struct sctp_nets *, struct timeval *, int); 150 151 uint32_t sctp_calculate_len(struct mbuf *); 152 153 caddr_t sctp_m_getptr(struct mbuf *, int, int, uint8_t *); 154 155 struct sctp_paramhdr * 156 sctp_get_next_param(struct mbuf *, int, 157 struct sctp_paramhdr *, int); 158 159 struct mbuf * 160 sctp_add_pad_tombuf(struct mbuf *, int); 161 162 struct mbuf * 163 sctp_pad_lastmbuf(struct mbuf *, int, struct mbuf *); 164 165 void sctp_ulp_notify(uint32_t, struct sctp_tcb *, uint32_t, void *, int 166 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 167 SCTP_UNUSED 168 #endif 169 ); 170 171 void 172 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp, 173 struct sctp_inpcb *new_inp, 174 struct sctp_tcb *stcb, int waitflags); 175 176 177 void sctp_stop_timers_for_shutdown(struct sctp_tcb *); 178 179 /* Stop all timers for association and remote addresses. */ 180 void sctp_stop_association_timers(struct sctp_tcb *, bool); 181 182 void sctp_report_all_outbound(struct sctp_tcb *, uint16_t, int, int 183 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 184 SCTP_UNUSED 185 #endif 186 ); 187 188 int sctp_expand_mapping_array(struct sctp_association *, uint32_t); 189 190 void sctp_abort_notification(struct sctp_tcb *, uint8_t, uint16_t, 191 struct sctp_abort_chunk *, int 192 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 193 SCTP_UNUSED 194 #endif 195 ); 196 197 /* We abort responding to an IP packet for some reason */ 198 void 199 sctp_abort_association(struct sctp_inpcb *, struct sctp_tcb *, struct mbuf *, 200 int, struct sockaddr *, struct sockaddr *, 201 struct sctphdr *, struct mbuf *, 202 #if defined(__FreeBSD__) 203 uint8_t, uint32_t, 204 #endif 205 uint32_t, uint16_t); 206 207 208 /* We choose to abort via user input */ 209 void 210 sctp_abort_an_association(struct sctp_inpcb *, struct sctp_tcb *, 211 struct mbuf *, int 212 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 213 SCTP_UNUSED 214 #endif 215 ); 216 217 void sctp_handle_ootb(struct mbuf *, int, int, 218 struct sockaddr *, struct sockaddr *, 219 struct sctphdr *, struct sctp_inpcb *, 220 struct mbuf *, 221 #if defined(__FreeBSD__) 222 uint8_t, uint32_t, uint16_t, 223 #endif 224 uint32_t, uint16_t); 225 226 int sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr, 227 int totaddr, int *error); 228 229 int 230 sctp_connectx_helper_find(struct sctp_inpcb *, struct sockaddr *, 231 unsigned int, unsigned int *, unsigned int *, unsigned int); 232 233 int sctp_is_there_an_abort_here(struct mbuf *, int, uint32_t *); 234 #ifdef INET6 235 uint32_t sctp_is_same_scope(struct sockaddr_in6 *, struct sockaddr_in6 *); 236 237 #if defined(SCTP_EMBEDDED_V6_SCOPE) 238 struct sockaddr_in6 * 239 sctp_recover_scope(struct sockaddr_in6 *, struct sockaddr_in6 *); 240 241 #ifdef SCTP_KAME 242 #define sctp_recover_scope_mac(addr, store) do { \ 243 if ((addr->sin6_family == AF_INET6) && \ 244 (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr))) { \ 245 *store = *addr; \ 246 if (addr->sin6_scope_id == 0) { \ 247 if (!sa6_recoverscope(store)) { \ 248 addr = store; \ 249 } \ 250 } else { \ 251 in6_clearscope(&addr->sin6_addr); \ 252 addr = store; \ 253 } \ 254 } \ 255 } while (0) 256 #else 257 #define sctp_recover_scope_mac(addr, store) do { \ 258 if ((addr->sin6_family == AF_INET6) && \ 259 (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr))) { \ 260 *store = *addr; \ 261 if (addr->sin6_scope_id == 0) { \ 262 if (!in6_recoverscope(store, &store->sin6_addr, \ 263 NULL)) { \ 264 addr = store; \ 265 } \ 266 } else { \ 267 in6_clearscope(&addr->sin6_addr); \ 268 addr = store; \ 269 } \ 270 } \ 271 } while (0) 272 #endif 273 #endif 274 #endif 275 276 int sctp_cmpaddr(struct sockaddr *, struct sockaddr *); 277 278 void sctp_print_address(struct sockaddr *); 279 280 int 281 sctp_release_pr_sctp_chunk(struct sctp_tcb *, struct sctp_tmit_chunk *, 282 uint8_t, int 283 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 284 SCTP_UNUSED 285 #endif 286 ); 287 288 struct mbuf *sctp_generate_cause(uint16_t, char *); 289 struct mbuf *sctp_generate_no_user_data_cause(uint32_t); 290 291 void sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp, 292 struct sockaddr *sa, sctp_assoc_t assoc_id, 293 uint32_t vrf_id, int *error, void *p); 294 void sctp_bindx_delete_address(struct sctp_inpcb *inp, 295 struct sockaddr *sa, sctp_assoc_t assoc_id, 296 uint32_t vrf_id, int *error); 297 298 int sctp_local_addr_count(struct sctp_tcb *stcb); 299 300 #ifdef SCTP_MBCNT_LOGGING 301 void 302 sctp_free_bufspace(struct sctp_tcb *, struct sctp_association *, 303 struct sctp_tmit_chunk *, int); 304 305 #else 306 #define sctp_free_bufspace(stcb, asoc, tp1, chk_cnt) \ 307 do { \ 308 if (tp1->data != NULL) { \ 309 atomic_subtract_int(&((asoc)->chunks_on_out_queue), chk_cnt); \ 310 if ((asoc)->total_output_queue_size >= tp1->book_size) { \ 311 atomic_subtract_int(&((asoc)->total_output_queue_size), tp1->book_size); \ 312 } else { \ 313 (asoc)->total_output_queue_size = 0; \ 314 } \ 315 if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \ 316 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \ 317 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) { \ 318 atomic_subtract_int(&((stcb)->sctp_socket->so_snd.sb_cc), tp1->book_size); \ 319 } else { \ 320 stcb->sctp_socket->so_snd.sb_cc = 0; \ 321 } \ 322 } \ 323 } \ 324 } while (0) 325 326 #endif 327 328 #define sctp_free_spbufspace(stcb, asoc, sp) \ 329 do { \ 330 if (sp->data != NULL) { \ 331 if ((asoc)->total_output_queue_size >= sp->length) { \ 332 atomic_subtract_int(&(asoc)->total_output_queue_size, sp->length); \ 333 } else { \ 334 (asoc)->total_output_queue_size = 0; \ 335 } \ 336 if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \ 337 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \ 338 if (stcb->sctp_socket->so_snd.sb_cc >= sp->length) { \ 339 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc,sp->length); \ 340 } else { \ 341 stcb->sctp_socket->so_snd.sb_cc = 0; \ 342 } \ 343 } \ 344 } \ 345 } while (0) 346 347 #define sctp_snd_sb_alloc(stcb, sz) \ 348 do { \ 349 atomic_add_int(&stcb->asoc.total_output_queue_size,sz); \ 350 if ((stcb->sctp_socket != NULL) && \ 351 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \ 352 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \ 353 atomic_add_int(&stcb->sctp_socket->so_snd.sb_cc,sz); \ 354 } \ 355 } while (0) 356 357 /* functions to start/stop udp tunneling */ 358 #if defined(__APPLE__) || defined(__FreeBSD__) 359 void sctp_over_udp_stop(void); 360 int sctp_over_udp_start(void); 361 #endif 362 #if defined(__Windows__) 363 void sctp_over_udp_restart(void); 364 #endif 365 366 int 367 sctp_soreceive(struct socket *so, struct sockaddr **psa, 368 struct uio *uio, 369 struct mbuf **mp0, 370 struct mbuf **controlp, 371 int *flagsp); 372 373 void 374 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d); 375 376 void 377 sctp_wakeup_log(struct sctp_tcb *stcb, 378 uint32_t wake_cnt, int from); 379 380 void sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t, uint16_t, uint16_t, int); 381 382 void sctp_log_nagle_event(struct sctp_tcb *stcb, int action); 383 384 385 #ifdef SCTP_MBUF_LOGGING 386 void 387 sctp_log_mb(struct mbuf *m, int from); 388 389 void 390 sctp_log_mbc(struct mbuf *m, int from); 391 #endif 392 393 void 394 sctp_sblog(struct sockbuf *sb, 395 struct sctp_tcb *stcb, int from, int incr); 396 397 void 398 sctp_log_strm_del(struct sctp_queued_to_read *control, 399 struct sctp_queued_to_read *poschk, 400 int from); 401 void sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *, int, uint8_t); 402 void rto_logging(struct sctp_nets *net, int from); 403 404 void sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc); 405 406 void sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from); 407 void sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *, int, int, uint8_t); 408 void sctp_log_block(uint8_t, struct sctp_association *, ssize_t); 409 void sctp_log_rwnd(uint8_t, uint32_t, uint32_t, uint32_t); 410 void sctp_log_rwnd_set(uint8_t, uint32_t, uint32_t, uint32_t, uint32_t); 411 int sctp_fill_stat_log(void *, size_t *); 412 void sctp_log_fr(uint32_t, uint32_t, uint32_t, int); 413 void sctp_log_sack(uint32_t, uint32_t, uint32_t, uint16_t, uint16_t, int); 414 void sctp_log_map(uint32_t, uint32_t, uint32_t, int); 415 void sctp_print_mapping_array(struct sctp_association *asoc); 416 void sctp_clr_stat_log(void); 417 418 419 #ifdef SCTP_AUDITING_ENABLED 420 void 421 sctp_auditing(int, struct sctp_inpcb *, struct sctp_tcb *, 422 struct sctp_nets *); 423 void sctp_audit_log(uint8_t, uint8_t); 424 425 #endif 426 uint32_t sctp_min_mtu(uint32_t, uint32_t, uint32_t); 427 #if defined(__FreeBSD__) 428 void sctp_hc_set_mtu(union sctp_sockstore *, uint16_t, uint32_t); 429 uint32_t sctp_hc_get_mtu(union sctp_sockstore *, uint16_t); 430 #endif 431 void sctp_set_state(struct sctp_tcb *, int); 432 void sctp_add_substate(struct sctp_tcb *, int); 433 uint32_t sctp_ticks_to_msecs(uint32_t); 434 uint32_t sctp_msecs_to_ticks(uint32_t); 435 uint32_t sctp_ticks_to_secs(uint32_t); 436 uint32_t sctp_secs_to_ticks(uint32_t); 437 438 #endif /* _KERNEL */ 439 #endif 440