sctputil.h revision 267723
1163953Srrs/*- 2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3235828Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 4235828Stuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 5163953Srrs * 6163953Srrs * Redistribution and use in source and binary forms, with or without 7163953Srrs * modification, are permitted provided that the following conditions are met: 8163953Srrs * 9163953Srrs * a) Redistributions of source code must retain the above copyright notice, 10228653Stuexen * this list of conditions and the following disclaimer. 11163953Srrs * 12163953Srrs * b) Redistributions in binary form must reproduce the above copyright 13163953Srrs * notice, this list of conditions and the following disclaimer in 14228653Stuexen * the documentation and/or other materials provided with the distribution. 15163953Srrs * 16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its 17163953Srrs * contributors may be used to endorse or promote products derived 18163953Srrs * from this software without specific prior written permission. 19163953Srrs * 20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE. 31163953Srrs */ 32163953Srrs 33163953Srrs#include <sys/cdefs.h> 34163953Srrs__FBSDID("$FreeBSD: stable/10/sys/netinet/sctputil.h 267723 2014-06-22 16:28:52Z tuexen $"); 35163953Srrs 36235828Stuexen#ifndef _NETINET_SCTP_UTIL_H_ 37235828Stuexen#define _NETINET_SCTP_UTIL_H_ 38163953Srrs 39180387Srrs#if defined(_KERNEL) || defined(__Userspace__) 40163953Srrs 41195918Srrs#define SCTP_READ_LOCK_HELD 1 42195918Srrs#define SCTP_READ_LOCK_NOT_HELD 0 43168755Srrs 44168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS 45168859Srrsvoid sctp_print_out_track_log(struct sctp_tcb *stcb); 46168859Srrs 47168859Srrs#endif 48168859Srrs 49163953Srrs#ifdef SCTP_MBUF_LOGGING 50163953Srrsstruct mbuf *sctp_m_free(struct mbuf *m); 51163953Srrsvoid sctp_m_freem(struct mbuf *m); 52163953Srrs 53163953Srrs#else 54163953Srrs#define sctp_m_free m_free 55163953Srrs#define sctp_m_freem m_freem 56163953Srrs#endif 57163953Srrs 58171943Srrs#if defined(SCTP_LOCAL_TRACE_BUF) || defined(__APPLE__) 59171943Srrsvoid 60172157Srrs 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); 61163953Srrs 62171943Srrs#endif 63171943Srrs 64163953Srrs#define sctp_get_associd(stcb) ((sctp_assoc_t)stcb->asoc.assoc_id) 65163953Srrs 66163953Srrs 67163953Srrs/* 68163953Srrs * Function prototypes 69163953Srrs */ 70167698Srrsuint32_t 71167698Srrssctp_get_ifa_hash_val(struct sockaddr *addr); 72167698Srrs 73167598Srrsstruct sctp_ifa * 74167598Srrs sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr, int hold_lock); 75163953Srrs 76167598Srrsstruct sctp_ifa * 77167598Srrs sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock); 78167598Srrs 79163953Srrsuint32_t sctp_select_initial_TSN(struct sctp_pcb *); 80163953Srrs 81185694Srrsuint32_t sctp_select_a_tag(struct sctp_inpcb *, uint16_t lport, uint16_t rport, int); 82163953Srrs 83206137Stuexenint sctp_init_asoc(struct sctp_inpcb *, struct sctp_tcb *, uint32_t, uint32_t); 84163953Srrs 85163953Srrsvoid sctp_fill_random_store(struct sctp_pcb *); 86163953Srrs 87169420Srrsvoid 88233660Srrssctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, 89233660Srrs uint16_t numberout, int flag); 90233660Srrsvoid 91233660Srrs sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag); 92233660Srrs 93233660Srrsvoid 94163953Srrssctp_timer_start(int, struct sctp_inpcb *, struct sctp_tcb *, 95163953Srrs struct sctp_nets *); 96163953Srrs 97169378Srrsvoid 98163953Srrssctp_timer_stop(int, struct sctp_inpcb *, struct sctp_tcb *, 99165220Srrs struct sctp_nets *, uint32_t); 100163953Srrs 101167598Srrsint 102167598Srrs sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id); 103167598Srrs 104163953Srrsvoid 105166023Srrs sctp_mtu_size_reset(struct sctp_inpcb *, struct sctp_association *, uint32_t); 106163953Srrs 107163953Srrsvoid 108163953Srrssctp_add_to_readq(struct sctp_inpcb *inp, 109163953Srrs struct sctp_tcb *stcb, 110163953Srrs struct sctp_queued_to_read *control, 111163953Srrs struct sockbuf *sb, 112172090Srrs int end, 113195918Srrs int inpread_locked, 114172090Srrs int so_locked 115172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 116172090Srrs SCTP_UNUSED 117172090Srrs#endif 118172090Srrs); 119163953Srrs 120163953Srrsint 121163953Srrssctp_append_to_readq(struct sctp_inpcb *inp, 122163953Srrs struct sctp_tcb *stcb, 123163953Srrs struct sctp_queued_to_read *control, 124163953Srrs struct mbuf *m, 125163953Srrs int end, 126163953Srrs int new_cumack, 127163953Srrs struct sockbuf *sb); 128163953Srrs 129163953Srrs 130167598Srrsvoid sctp_iterator_worker(void); 131167598Srrs 132214939Stuexenuint32_t sctp_get_prev_mtu(uint32_t); 133228653Stuexenuint32_t sctp_get_next_mtu(uint32_t); 134163953Srrs 135163953Srrsvoid 136163953Srrs sctp_timeout_handler(void *); 137163953Srrs 138163953Srrsuint32_t 139163953Srrssctp_calculate_rto(struct sctp_tcb *, struct sctp_association *, 140218186Srrs struct sctp_nets *, struct timeval *, int, int); 141163953Srrs 142163953Srrsuint32_t sctp_calculate_len(struct mbuf *); 143163953Srrs 144163953Srrscaddr_t sctp_m_getptr(struct mbuf *, int, int, uint8_t *); 145163953Srrs 146163953Srrsstruct sctp_paramhdr * 147163953Srrssctp_get_next_param(struct mbuf *, int, 148163953Srrs struct sctp_paramhdr *, int); 149163953Srrs 150163953Srrsint sctp_add_pad_tombuf(struct mbuf *, int); 151163953Srrs 152163953Srrsint sctp_pad_lastmbuf(struct mbuf *, int, struct mbuf *); 153163953Srrs 154172090Srrsvoid 155172090Srrssctp_ulp_notify(uint32_t, struct sctp_tcb *, uint32_t, void *, int 156172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 157172090Srrs SCTP_UNUSED 158172090Srrs#endif 159172090Srrs); 160163953Srrs 161163953Srrsvoid 162163953Srrssctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp, 163163953Srrs struct sctp_inpcb *new_inp, 164169208Srrs struct sctp_tcb *stcb, int waitflags); 165163953Srrs 166163953Srrs 167163953Srrsvoid sctp_stop_timers_for_shutdown(struct sctp_tcb *); 168163953Srrs 169172090Srrsvoid 170235416Stuexensctp_report_all_outbound(struct sctp_tcb *, uint16_t, int, int 171172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 172172090Srrs SCTP_UNUSED 173172090Srrs#endif 174172090Srrs); 175163953Srrs 176170138Srrsint sctp_expand_mapping_array(struct sctp_association *, uint32_t); 177163953Srrs 178172090Srrsvoid 179235403Stuexensctp_abort_notification(struct sctp_tcb *, uint8_t, uint16_t, 180235360Stuexen struct sctp_abort_chunk *, int 181172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 182172090Srrs SCTP_UNUSED 183172090Srrs#endif 184172090Srrs); 185163953Srrs 186163953Srrs/* We abort responding to an IP packet for some reason */ 187163953Srrsvoid 188237049Stuexensctp_abort_association(struct sctp_inpcb *, struct sctp_tcb *, struct mbuf *, 189237715Stuexen int, struct sockaddr *, struct sockaddr *, 190237715Stuexen struct sctphdr *, struct mbuf *, 191237049Stuexen uint8_t, uint32_t, 192237049Stuexen uint32_t, uint16_t); 193163953Srrs 194169352Srrs 195163953Srrs/* We choose to abort via user input */ 196163953Srrsvoid 197235360Stuexensctp_abort_an_association(struct sctp_inpcb *, struct sctp_tcb *, 198172090Srrs struct mbuf *, int 199172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 200172090Srrs SCTP_UNUSED 201172090Srrs#endif 202172090Srrs); 203163953Srrs 204169352Srrsvoid 205237715Stuexensctp_handle_ootb(struct mbuf *, int, int, 206237715Stuexen struct sockaddr *, struct sockaddr *, 207237715Stuexen struct sctphdr *, struct sctp_inpcb *, 208267723Stuexen struct mbuf *, 209237049Stuexen uint8_t, uint32_t, 210237049Stuexen uint32_t, uint16_t); 211163953Srrs 212170056Srrsint 213170056Srrssctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr, 214170056Srrs int totaddr, int *error); 215169352Srrs 216169352Srrsstruct sctp_tcb * 217170056Srrssctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr, 218170056Srrs int *totaddr, int *num_v4, int *num_v6, int *error, int limit, int *bad_addr); 219169352Srrs 220163953Srrsint sctp_is_there_an_abort_here(struct mbuf *, int, uint32_t *); 221178251Srrs 222178251Srrs#ifdef INET6 223163953Srrsuint32_t sctp_is_same_scope(struct sockaddr_in6 *, struct sockaddr_in6 *); 224166675Srrs 225163953Srrsstruct sockaddr_in6 * 226166675Srrs sctp_recover_scope(struct sockaddr_in6 *, struct sockaddr_in6 *); 227163953Srrs 228163953Srrs#define sctp_recover_scope_mac(addr, store) do { \ 229166675Srrs if ((addr->sin6_family == AF_INET6) && \ 230166675Srrs (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr))) { \ 231166675Srrs *store = *addr; \ 232166675Srrs if (addr->sin6_scope_id == 0) { \ 233166675Srrs if (!sa6_recoverscope(store)) { \ 234166675Srrs addr = store; \ 235166675Srrs } \ 236166675Srrs } else { \ 237166675Srrs in6_clearscope(&addr->sin6_addr); \ 238166675Srrs addr = store; \ 239166675Srrs } \ 240166675Srrs } \ 241166675Srrs} while (0) 242178251Srrs#endif 243163953Srrs 244163953Srrsint sctp_cmpaddr(struct sockaddr *, struct sockaddr *); 245163953Srrs 246163953Srrsvoid sctp_print_address(struct sockaddr *); 247163953Srrs 248163953Srrsint 249163953Srrssctp_release_pr_sctp_chunk(struct sctp_tcb *, struct sctp_tmit_chunk *, 250235416Stuexen uint8_t, int 251172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 252172090Srrs SCTP_UNUSED 253172090Srrs#endif 254172090Srrs); 255163953Srrs 256267723Stuexenstruct mbuf *sctp_generate_cause(uint16_t, char *); 257163953Srrs 258170606Srrsvoid 259170606Srrssctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp, 260170606Srrs struct sockaddr *sa, sctp_assoc_t assoc_id, 261170606Srrs uint32_t vrf_id, int *error, void *p); 262170606Srrsvoid 263228653Stuexensctp_bindx_delete_address(struct sctp_inpcb *inp, 264170606Srrs struct sockaddr *sa, sctp_assoc_t assoc_id, 265170606Srrs uint32_t vrf_id, int *error); 266163953Srrs 267171572Srrsint sctp_local_addr_count(struct sctp_tcb *stcb); 268171572Srrs 269163953Srrs#ifdef SCTP_MBCNT_LOGGING 270163953Srrsvoid 271163953Srrssctp_free_bufspace(struct sctp_tcb *, struct sctp_association *, 272170744Srrs struct sctp_tmit_chunk *, int); 273163953Srrs 274163953Srrs#else 275163953Srrs#define sctp_free_bufspace(stcb, asoc, tp1, chk_cnt) \ 276163953Srrsdo { \ 277163953Srrs if (tp1->data != NULL) { \ 278169420Srrs atomic_subtract_int(&((asoc)->chunks_on_out_queue), chk_cnt); \ 279163953Srrs if ((asoc)->total_output_queue_size >= tp1->book_size) { \ 280169420Srrs atomic_subtract_int(&((asoc)->total_output_queue_size), tp1->book_size); \ 281163953Srrs } else { \ 282163953Srrs (asoc)->total_output_queue_size = 0; \ 283163953Srrs } \ 284163953Srrs if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \ 285163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \ 286163953Srrs if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) { \ 287169420Srrs atomic_subtract_int(&((stcb)->sctp_socket->so_snd.sb_cc), tp1->book_size); \ 288163953Srrs } else { \ 289163953Srrs stcb->sctp_socket->so_snd.sb_cc = 0; \ 290163953Srrs } \ 291163953Srrs } \ 292163953Srrs } \ 293163953Srrs} while (0) 294163953Srrs 295163953Srrs#endif 296163953Srrs 297163953Srrs#define sctp_free_spbufspace(stcb, asoc, sp) \ 298163953Srrsdo { \ 299163953Srrs if (sp->data != NULL) { \ 300163953Srrs if ((asoc)->total_output_queue_size >= sp->length) { \ 301169420Srrs atomic_subtract_int(&(asoc)->total_output_queue_size, sp->length); \ 302163953Srrs } else { \ 303163953Srrs (asoc)->total_output_queue_size = 0; \ 304163953Srrs } \ 305163953Srrs if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \ 306163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \ 307163953Srrs if (stcb->sctp_socket->so_snd.sb_cc >= sp->length) { \ 308169420Srrs atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc,sp->length); \ 309163953Srrs } else { \ 310163953Srrs stcb->sctp_socket->so_snd.sb_cc = 0; \ 311163953Srrs } \ 312163953Srrs } \ 313163953Srrs } \ 314163953Srrs} while (0) 315163953Srrs 316163953Srrs#define sctp_snd_sb_alloc(stcb, sz) \ 317163953Srrsdo { \ 318163953Srrs atomic_add_int(&stcb->asoc.total_output_queue_size,sz); \ 319163953Srrs if ((stcb->sctp_socket != NULL) && \ 320163953Srrs ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \ 321163953Srrs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \ 322163953Srrs atomic_add_int(&stcb->sctp_socket->so_snd.sb_cc,sz); \ 323163953Srrs } \ 324163953Srrs} while (0) 325163953Srrs 326221328Stuexen/* functions to start/stop udp tunneling */ 327179157Srrsvoid sctp_over_udp_stop(void); 328179157Srrsint sctp_over_udp_start(void); 329163953Srrs 330163953Srrsint 331163953Srrssctp_soreceive(struct socket *so, struct sockaddr **psa, 332163953Srrs struct uio *uio, 333163953Srrs struct mbuf **mp0, 334163953Srrs struct mbuf **controlp, 335163953Srrs int *flagsp); 336163953Srrs 337163953Srrsvoid 338163953Srrs sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d); 339163953Srrs 340163953Srrsvoid 341163953Srrssctp_wakeup_log(struct sctp_tcb *stcb, 342163953Srrs uint32_t wake_cnt, int from); 343163953Srrs 344164181Srrsvoid sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t, uint16_t, uint16_t, int); 345163953Srrs 346163953Srrsvoid sctp_log_nagle_event(struct sctp_tcb *stcb, int action); 347163953Srrs 348163953Srrs 349163953Srrsvoid 350163953Srrs sctp_log_mb(struct mbuf *m, int from); 351163953Srrs 352163953Srrsvoid 353163953Srrssctp_sblog(struct sockbuf *sb, 354163953Srrs struct sctp_tcb *stcb, int from, int incr); 355163953Srrs 356163953Srrsvoid 357163953Srrssctp_log_strm_del(struct sctp_queued_to_read *control, 358163953Srrs struct sctp_queued_to_read *poschk, 359163953Srrs int from); 360163953Srrsvoid sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *, int, uint8_t); 361163953Srrsvoid rto_logging(struct sctp_nets *net, int from); 362163953Srrs 363163953Srrsvoid sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc); 364163953Srrs 365163953Srrsvoid sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from); 366163953Srrsvoid sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *, int, int, uint8_t); 367228653Stuexenvoid sctp_log_block(uint8_t, struct sctp_association *, int); 368163953Srrsvoid sctp_log_rwnd(uint8_t, uint32_t, uint32_t, uint32_t); 369163953Srrsvoid sctp_log_mbcnt(uint8_t, uint32_t, uint32_t, uint32_t, uint32_t); 370163953Srrsvoid sctp_log_rwnd_set(uint8_t, uint32_t, uint32_t, uint32_t, uint32_t); 371167598Srrsint sctp_fill_stat_log(void *, size_t *); 372163953Srrsvoid sctp_log_fr(uint32_t, uint32_t, uint32_t, int); 373163953Srrsvoid sctp_log_sack(uint32_t, uint32_t, uint32_t, uint16_t, uint16_t, int); 374163953Srrsvoid sctp_log_map(uint32_t, uint32_t, uint32_t, int); 375205502Srrsvoid sctp_print_mapping_array(struct sctp_association *asoc); 376163953Srrsvoid sctp_clr_stat_log(void); 377163953Srrs 378163953Srrs 379163953Srrs#ifdef SCTP_AUDITING_ENABLED 380163953Srrsvoid 381163953Srrssctp_auditing(int, struct sctp_inpcb *, struct sctp_tcb *, 382163953Srrs struct sctp_nets *); 383163953Srrsvoid sctp_audit_log(uint8_t, uint8_t); 384163953Srrs 385163953Srrs#endif 386163953Srrs#endif /* _KERNEL */ 387163953Srrs#endif 388