1f8829a4aSRandall Stewart /*- 2b1006367SRandall Stewart * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved. 3f8829a4aSRandall Stewart * 4f8829a4aSRandall Stewart * Redistribution and use in source and binary forms, with or without 5f8829a4aSRandall Stewart * modification, are permitted provided that the following conditions are met: 6f8829a4aSRandall Stewart * 7f8829a4aSRandall Stewart * a) Redistributions of source code must retain the above copyright notice, 8f8829a4aSRandall Stewart * this list of conditions and the following disclaimer. 9f8829a4aSRandall Stewart * 10f8829a4aSRandall Stewart * b) Redistributions in binary form must reproduce the above copyright 11f8829a4aSRandall Stewart * notice, this list of conditions and the following disclaimer in 12f8829a4aSRandall Stewart * the documentation and/or other materials provided with the distribution. 13f8829a4aSRandall Stewart * 14f8829a4aSRandall Stewart * c) Neither the name of Cisco Systems, Inc. nor the names of its 15f8829a4aSRandall Stewart * contributors may be used to endorse or promote products derived 16f8829a4aSRandall Stewart * from this software without specific prior written permission. 17f8829a4aSRandall Stewart * 18f8829a4aSRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f8829a4aSRandall Stewart * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20f8829a4aSRandall Stewart * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21f8829a4aSRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22f8829a4aSRandall Stewart * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23f8829a4aSRandall Stewart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24f8829a4aSRandall Stewart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25f8829a4aSRandall Stewart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26f8829a4aSRandall Stewart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27f8829a4aSRandall Stewart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28f8829a4aSRandall Stewart * THE POSSIBILITY OF SUCH DAMAGE. 29f8829a4aSRandall Stewart */ 3073932c69SRandall Stewart #include <sys/cdefs.h> 31f8829a4aSRandall Stewart __FBSDID("$FreeBSD$"); 32f8829a4aSRandall Stewart #ifndef __sctp_os_bsd_h__ 33f8829a4aSRandall Stewart #define __sctp_os_bsd_h__ 34f8829a4aSRandall Stewart /* 35f8829a4aSRandall Stewart * includes 36f8829a4aSRandall Stewart */ 3793164cf9SRandall Stewart #include "opt_ipsec.h" 3893164cf9SRandall Stewart #include "opt_compat.h" 3993164cf9SRandall Stewart #include "opt_inet6.h" 4093164cf9SRandall Stewart #include "opt_inet.h" 4193164cf9SRandall Stewart #include "opt_sctp.h" 42603724d3SBjoern A. Zeeb 4393164cf9SRandall Stewart #include <sys/param.h> 4480fefe0aSRandall Stewart #include <sys/ktr.h> 4593164cf9SRandall Stewart #include <sys/systm.h> 4693164cf9SRandall Stewart #include <sys/malloc.h> 4793164cf9SRandall Stewart #include <sys/kernel.h> 4893164cf9SRandall Stewart #include <sys/sysctl.h> 4993164cf9SRandall Stewart #include <sys/mbuf.h> 5093164cf9SRandall Stewart #include <sys/protosw.h> 5193164cf9SRandall Stewart #include <sys/socket.h> 5293164cf9SRandall Stewart #include <sys/socketvar.h> 5318e198d3SRandall Stewart #include <sys/jail.h> 5493164cf9SRandall Stewart #include <sys/sysctl.h> 5593164cf9SRandall Stewart #include <sys/resourcevar.h> 5693164cf9SRandall Stewart #include <sys/uio.h> 57c99efcf6SRandall Stewart #include <sys/lock.h> 58c99efcf6SRandall Stewart #include <sys/rwlock.h> 5942551e99SRandall Stewart #include <sys/kthread.h> 6093164cf9SRandall Stewart #include <sys/priv.h> 61f8829a4aSRandall Stewart #include <sys/random.h> 6293164cf9SRandall Stewart #include <sys/limits.h> 6393164cf9SRandall Stewart #include <sys/queue.h> 64fc14de76SRandall Stewart #include <sys/vimage.h> 6593164cf9SRandall Stewart #include <machine/cpu.h> 6693164cf9SRandall Stewart 6793164cf9SRandall Stewart #include <net/if.h> 6893164cf9SRandall Stewart #include <net/if_types.h> 6993164cf9SRandall Stewart #include <net/if_var.h> 7093164cf9SRandall Stewart #include <net/route.h> 7193164cf9SRandall Stewart 7293164cf9SRandall Stewart #include <netinet/in.h> 7393164cf9SRandall Stewart #include <netinet/in_systm.h> 7493164cf9SRandall Stewart #include <netinet/ip.h> 7593164cf9SRandall Stewart #include <netinet/in_pcb.h> 7693164cf9SRandall Stewart #include <netinet/in_var.h> 7793164cf9SRandall Stewart #include <netinet/ip_var.h> 7893164cf9SRandall Stewart #include <netinet/ip_icmp.h> 7993164cf9SRandall Stewart #include <netinet/icmp_var.h> 804b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 8193164cf9SRandall Stewart 82b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 832cb64cb2SGeorge V. Neville-Neil #include <netipsec/ipsec.h> 842cb64cb2SGeorge V. Neville-Neil #include <netipsec/key.h> 8593164cf9SRandall Stewart #endif /* IPSEC */ 8693164cf9SRandall Stewart 8793164cf9SRandall Stewart #ifdef INET6 8893164cf9SRandall Stewart #include <sys/domain.h> 89b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 900d29af67SGeorge V. Neville-Neil #include <netipsec/ipsec6.h> 9193164cf9SRandall Stewart #endif 9293164cf9SRandall Stewart #include <netinet/ip6.h> 9393164cf9SRandall Stewart #include <netinet6/ip6_var.h> 9493164cf9SRandall Stewart #include <netinet6/in6_pcb.h> 9593164cf9SRandall Stewart #include <netinet/icmp6.h> 9693164cf9SRandall Stewart #include <netinet6/ip6protosw.h> 9793164cf9SRandall Stewart #include <netinet6/nd6.h> 9893164cf9SRandall Stewart #include <netinet6/scope6_var.h> 9993164cf9SRandall Stewart #endif /* INET6 */ 10093164cf9SRandall Stewart 101f42a358aSRandall Stewart 10293164cf9SRandall Stewart #include <netinet/ip_options.h> 10393164cf9SRandall Stewart 10493164cf9SRandall Stewart #ifndef in6pcb 10593164cf9SRandall Stewart #define in6pcb inpcb 10693164cf9SRandall Stewart #endif 107207304d4SRandall Stewart /* Declare all the malloc names for all the various mallocs */ 108207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_MAP); 109207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRMI); 110207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRMO); 111207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ASC_ADDR); 112207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ASC_IT); 113207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_CL); 114207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_KY); 115207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_HL); 116207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_IF); 117207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRESET); 118207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_CMSG); 119207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_COPYAL); 120207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_VRF); 121207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_IFA); 122207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_IFN); 123207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_TIMW); 124207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_MVRF); 125207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ITER); 126207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_SOCKOPT); 127f8829a4aSRandall Stewart 128c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF) 129c4739e2fSRandall Stewart 130c4739e2fSRandall Stewart #define SCTP_GET_CYCLECOUNT get_cyclecount() 131c4739e2fSRandall Stewart #define SCTP_CTR6 sctp_log_trace 132c4739e2fSRandall Stewart 133c4739e2fSRandall Stewart #else 134c4739e2fSRandall Stewart #define SCTP_CTR6 CTR6 135c4739e2fSRandall Stewart #endif 136c4739e2fSRandall Stewart 137b3f1ea41SRandall Stewart #define SCTP_BASE_INFO(__m) system_base_info.sctppcbinfo.__m 138b3f1ea41SRandall Stewart #define SCTP_BASE_STATS system_base_info.sctpstat 139b3f1ea41SRandall Stewart #define SCTP_BASE_STAT(__m) system_base_info.sctpstat.__m 140b3f1ea41SRandall Stewart #define SCTP_BASE_SYSCTL(__m) system_base_info.sctpsysctl.__m 141b3f1ea41SRandall Stewart #define SCTP_BASE_VAR(__m) system_base_info.__m 142c4739e2fSRandall Stewart 143f8829a4aSRandall Stewart /* 144fc14de76SRandall Stewart * Macros to expand out globals defined by various modules 145fc14de76SRandall Stewart * to either a real global or a virtualized instance of one, 146603724d3SBjoern A. Zeeb * depending on whether VIMAGE is defined. 147fc14de76SRandall Stewart */ 148fc14de76SRandall Stewart /* first define modules that supply us information */ 149fc14de76SRandall Stewart #define MOD_NET net 150fc14de76SRandall Stewart #define MOD_INET inet 151fc14de76SRandall Stewart #define MOD_INET6 inet6 152fc14de76SRandall Stewart #define MOD_IPSEC ipsec 153fc14de76SRandall Stewart 154fc14de76SRandall Stewart /* then define the macro(s) that hook into the vimage macros */ 155603724d3SBjoern A. Zeeb #if defined(__FreeBSD__) && __FreeBSD_version >= 800044 && defined(VIMAGE) 156603724d3SBjoern A. Zeeb #if 0 157fc14de76SRandall Stewart #define VSYMNAME(__MODULE) vnet_ ## __MODULE 158fc14de76SRandall Stewart #define MODULE_GLOBAL(__MODULE, __SYMBOL) VSYM(VSYMNAME(__MODULE), __SYMBOL) 159fc14de76SRandall Stewart #else 160603724d3SBjoern A. Zeeb #define MODULE_GLOBAL(__MODULE, __SYMBOL) V_ ## __SYMBOL 161603724d3SBjoern A. Zeeb #endif 162603724d3SBjoern A. Zeeb #else 163fc14de76SRandall Stewart #define MODULE_GLOBAL(__MODULE, __SYMBOL) (__SYMBOL) 164fc14de76SRandall Stewart #endif 165fc14de76SRandall Stewart /* 166f8829a4aSRandall Stewart * 167f8829a4aSRandall Stewart */ 168a5d547adSRandall Stewart #define USER_ADDR_NULL (NULL) /* FIX ME: temp */ 169f42a358aSRandall Stewart #define SCTP_LIST_EMPTY(list) LIST_EMPTY(list) 170a5d547adSRandall Stewart 17117205eccSRandall Stewart #if defined(SCTP_DEBUG) 17217205eccSRandall Stewart #define SCTPDBG(level, params...) \ 17317205eccSRandall Stewart { \ 17417205eccSRandall Stewart do { \ 175b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) { \ 17617205eccSRandall Stewart printf(params); \ 17717205eccSRandall Stewart } \ 17817205eccSRandall Stewart } while (0); \ 17917205eccSRandall Stewart } 18017205eccSRandall Stewart #define SCTPDBG_ADDR(level, addr) \ 18117205eccSRandall Stewart { \ 18217205eccSRandall Stewart do { \ 183b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) { \ 18417205eccSRandall Stewart sctp_print_address(addr); \ 18517205eccSRandall Stewart } \ 18617205eccSRandall Stewart } while (0); \ 18717205eccSRandall Stewart } 188ad81507eSRandall Stewart #define SCTPDBG_PKT(level, iph, sh) \ 189ad81507eSRandall Stewart { \ 190ad81507eSRandall Stewart do { \ 191b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_debug_on) & level) { \ 192ad81507eSRandall Stewart sctp_print_address_pkt(iph, sh); \ 193ad81507eSRandall Stewart } \ 194ad81507eSRandall Stewart } while (0); \ 195ad81507eSRandall Stewart } 19617205eccSRandall Stewart #else 19717205eccSRandall Stewart #define SCTPDBG(level, params...) 19817205eccSRandall Stewart #define SCTPDBG_ADDR(level, addr) 199ad81507eSRandall Stewart #define SCTPDBG_PKT(level, iph, sh) 20017205eccSRandall Stewart #endif 201ad81507eSRandall Stewart #define SCTP_PRINTF(params...) printf(params) 20217205eccSRandall Stewart 20380fefe0aSRandall Stewart #ifdef SCTP_LTRACE_CHUNKS 2049b023217SRandall Stewart #define SCTP_LTRACE_CHK(a, b, c, d) if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_CHUNK_ENABLE) CTR6(KTR_SUBSYS, "SCTP:%d[%d]:%x-%x-%x-%x", SCTP_LOG_CHUNK_PROC, 0, a, b, c, d) 20580fefe0aSRandall Stewart #else 20680fefe0aSRandall Stewart #define SCTP_LTRACE_CHK(a, b, c, d) 20780fefe0aSRandall Stewart #endif 20880fefe0aSRandall Stewart 20980fefe0aSRandall Stewart #ifdef SCTP_LTRACE_ERRORS 2109b023217SRandall Stewart #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \ 211c4739e2fSRandall Stewart printf("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \ 212c4739e2fSRandall Stewart m, inp, stcb, net, file, __LINE__, err); 2139b023217SRandall Stewart #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \ 214c4739e2fSRandall Stewart printf("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \ 215c4739e2fSRandall Stewart inp, stcb, net, file, __LINE__, err); 21680fefe0aSRandall Stewart #else 217c4739e2fSRandall Stewart #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) 218c4739e2fSRandall Stewart #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) 21980fefe0aSRandall Stewart #endif 22080fefe0aSRandall Stewart 221c4739e2fSRandall Stewart 222f8829a4aSRandall Stewart /* 22342551e99SRandall Stewart * Local address and interface list handling 22442551e99SRandall Stewart */ 22542551e99SRandall Stewart #define SCTP_MAX_VRF_ID 0 22642551e99SRandall Stewart #define SCTP_SIZE_OF_VRF_HASH 3 22742551e99SRandall Stewart #define SCTP_IFNAMSIZ IFNAMSIZ 22842551e99SRandall Stewart #define SCTP_DEFAULT_VRFID 0 22917205eccSRandall Stewart #define SCTP_VRF_ADDR_HASH_SIZE 16 23017205eccSRandall Stewart #define SCTP_VRF_IFN_HASH_SIZE 3 231f4c93d24SRandall Stewart #define SCTP_INIT_VRF_TABLEID(vrf) 23242551e99SRandall Stewart 23342551e99SRandall Stewart #define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP) 234c54a18d2SRandall Stewart #define SCTP_ROUTE_IS_REAL_LOOP(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifa && (ro)->ro_rt->rt_ifa->ifa_ifp && (ro)->ro_rt->rt_ifa->ifa_ifp->if_type == IFT_LOOP) 23542551e99SRandall Stewart 23642551e99SRandall Stewart /* 23742551e99SRandall Stewart * Access to IFN's to help with src-addr-selection 23842551e99SRandall Stewart */ 23942551e99SRandall Stewart /* This could return VOID if the index works but for BSD we provide both. */ 24042551e99SRandall Stewart #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp 24117205eccSRandall Stewart #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) (ro)->ro_rt->rt_ifp->if_index 24217205eccSRandall Stewart #define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp) 24342551e99SRandall Stewart 24442551e99SRandall Stewart /* 245f8829a4aSRandall Stewart * general memory allocation 246f8829a4aSRandall Stewart */ 247f8829a4aSRandall Stewart #define SCTP_MALLOC(var, type, size, name) \ 248f8829a4aSRandall Stewart do { \ 2491ede983cSDag-Erling Smørgrav var = (type)malloc(size, name, M_NOWAIT); \ 250f8829a4aSRandall Stewart } while (0) 251f8829a4aSRandall Stewart 2521ede983cSDag-Erling Smørgrav #define SCTP_FREE(var, type) free(var, type) 253f8829a4aSRandall Stewart 254f8829a4aSRandall Stewart #define SCTP_MALLOC_SONAME(var, type, size) \ 255f8829a4aSRandall Stewart do { \ 2561ede983cSDag-Erling Smørgrav var = (type)malloc(size, M_SONAME, M_WAITOK | M_ZERO); \ 257f8829a4aSRandall Stewart } while (0) 258f8829a4aSRandall Stewart 2591ede983cSDag-Erling Smørgrav #define SCTP_FREE_SONAME(var) free(var, M_SONAME) 260f8829a4aSRandall Stewart 26142551e99SRandall Stewart #define SCTP_PROCESS_STRUCT struct proc * 26242551e99SRandall Stewart 263f8829a4aSRandall Stewart /* 264f8829a4aSRandall Stewart * zone allocation functions 265f8829a4aSRandall Stewart */ 266f8829a4aSRandall Stewart #include <vm/uma.h> 267c54a18d2SRandall Stewart 268f8829a4aSRandall Stewart /* SCTP_ZONE_INIT: initialize the zone */ 269f8829a4aSRandall Stewart typedef struct uma_zone *sctp_zone_t; 270f8829a4aSRandall Stewart 271f8829a4aSRandall Stewart #define UMA_ZFLAG_FULL 0x0020 272f8829a4aSRandall Stewart #define SCTP_ZONE_INIT(zone, name, size, number) { \ 273f8829a4aSRandall Stewart zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\ 274f8829a4aSRandall Stewart UMA_ZFLAG_FULL); \ 275f8829a4aSRandall Stewart uma_zone_set_max(zone, number); \ 276f8829a4aSRandall Stewart } 277f8829a4aSRandall Stewart 278c54a18d2SRandall Stewart #define SCTP_ZONE_DESTROY(zone) uma_zdestroy(zone) 279c54a18d2SRandall Stewart 280f8829a4aSRandall Stewart /* SCTP_ZONE_GET: allocate element from the zone */ 281f42a358aSRandall Stewart #define SCTP_ZONE_GET(zone, type) \ 282f42a358aSRandall Stewart (type *)uma_zalloc(zone, M_NOWAIT); 283f8829a4aSRandall Stewart 284f8829a4aSRandall Stewart /* SCTP_ZONE_FREE: free element from the zone */ 285f8829a4aSRandall Stewart #define SCTP_ZONE_FREE(zone, element) \ 286f8829a4aSRandall Stewart uma_zfree(zone, element); 287c54a18d2SRandall Stewart 28844b7479bSRandall Stewart #define SCTP_HASH_INIT(size, hashmark) hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT) 28944b7479bSRandall Stewart #define SCTP_HASH_FREE(table, hashmark) hashdestroy(table, M_PCB, hashmark) 29044b7479bSRandall Stewart 29144b7479bSRandall Stewart #define SCTP_M_COPYM m_copym 292f8829a4aSRandall Stewart 293f8829a4aSRandall Stewart /* 294139bc87fSRandall Stewart * timers 295139bc87fSRandall Stewart */ 296139bc87fSRandall Stewart #include <sys/callout.h> 297139bc87fSRandall Stewart typedef struct callout sctp_os_timer_t; 298139bc87fSRandall Stewart 299c54a18d2SRandall Stewart 300139bc87fSRandall Stewart #define SCTP_OS_TIMER_INIT(tmr) callout_init(tmr, 1) 301139bc87fSRandall Stewart #define SCTP_OS_TIMER_START callout_reset 302139bc87fSRandall Stewart #define SCTP_OS_TIMER_STOP callout_stop 30344b7479bSRandall Stewart #define SCTP_OS_TIMER_STOP_DRAIN callout_drain 304139bc87fSRandall Stewart #define SCTP_OS_TIMER_PENDING callout_pending 305139bc87fSRandall Stewart #define SCTP_OS_TIMER_ACTIVE callout_active 306139bc87fSRandall Stewart #define SCTP_OS_TIMER_DEACTIVATE callout_deactivate 307139bc87fSRandall Stewart 308207304d4SRandall Stewart #define sctp_get_tick_count() (ticks) 309207304d4SRandall Stewart 310ceaad40aSRandall Stewart #define SCTP_UNUSED __attribute__((unused)) 31135918f85SRandall Stewart 312139bc87fSRandall Stewart /* 313f8829a4aSRandall Stewart * Functions 314f8829a4aSRandall Stewart */ 315139bc87fSRandall Stewart /* Mbuf manipulation and access macros */ 316139bc87fSRandall Stewart #define SCTP_BUF_LEN(m) (m->m_len) 317139bc87fSRandall Stewart #define SCTP_BUF_NEXT(m) (m->m_next) 318139bc87fSRandall Stewart #define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt) 319139bc87fSRandall Stewart #define SCTP_BUF_RESV_UF(m, size) m->m_data += size 320139bc87fSRandall Stewart #define SCTP_BUF_AT(m, size) m->m_data + size 321139bc87fSRandall Stewart #define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT) 322139bc87fSRandall Stewart #define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size) 323139bc87fSRandall Stewart #define SCTP_BUF_TYPE(m) (m->m_type) 324139bc87fSRandall Stewart #define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif) 325139bc87fSRandall Stewart #define SCTP_BUF_PREPEND M_PREPEND 3265e54f665SRandall Stewart 3275e54f665SRandall Stewart #define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \ 3285e54f665SRandall Stewart MH_ALIGN(m, len); \ 3295e54f665SRandall Stewart } else if ((m->m_flags & M_EXT) == 0) { \ 3305e54f665SRandall Stewart M_ALIGN(m, len); \ 3315e54f665SRandall Stewart } 3320696e120SRandall Stewart 3330696e120SRandall Stewart /* We make it so if you have up to 4 threads 3340696e120SRandall Stewart * writting based on the default size of 3350696e120SRandall Stewart * the packet log 65 k, that would be 3360696e120SRandall Stewart * 4 16k packets before we would hit 3370696e120SRandall Stewart * a problem. 3380696e120SRandall Stewart */ 3390696e120SRandall Stewart #define SCTP_PKTLOG_WRITERS_NEED_LOCK 3 3400696e120SRandall Stewart 34117205eccSRandall Stewart /*************************/ 34217205eccSRandall Stewart /* MTU */ 34317205eccSRandall Stewart /*************************/ 344ad21a364SRandall Stewart #define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) ((struct ifnet *)ifn)->if_mtu 34517205eccSRandall Stewart #define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0) 34617205eccSRandall Stewart #define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) ((sctp_ifn->ifn_p != NULL) ? ((struct ifnet *)(sctp_ifn->ifn_p))->if_mtu : 0) 34717205eccSRandall Stewart #define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \ 34817205eccSRandall Stewart if (rt != NULL) \ 34917205eccSRandall Stewart rt->rt_rmx.rmx_mtu = mtu; \ 35017205eccSRandall Stewart } while(0) 35117205eccSRandall Stewart 35217205eccSRandall Stewart /* (de-)register interface event notifications */ 353ad21a364SRandall Stewart #define SCTP_REGISTER_INTERFACE(ifhandle, af) 354ad21a364SRandall Stewart #define SCTP_DEREGISTER_INTERFACE(ifhandle, af) 3555e54f665SRandall Stewart 35618e198d3SRandall Stewart 357139bc87fSRandall Stewart /*************************/ 358139bc87fSRandall Stewart /* These are for logging */ 359139bc87fSRandall Stewart /*************************/ 360139bc87fSRandall Stewart /* return the base ext data pointer */ 361139bc87fSRandall Stewart #define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf) 362139bc87fSRandall Stewart /* return the refcnt of the data pointer */ 363139bc87fSRandall Stewart #define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt) 364139bc87fSRandall Stewart /* return any buffer related flags, this is 365139bc87fSRandall Stewart * used beyond logging for apple only. 366139bc87fSRandall Stewart */ 367139bc87fSRandall Stewart #define SCTP_BUF_GET_FLAGS(m) (m->m_flags) 368139bc87fSRandall Stewart 369139bc87fSRandall Stewart /* For BSD this just accesses the M_PKTHDR length 370139bc87fSRandall Stewart * so it operates on an mbuf with hdr flag. Other 371139bc87fSRandall Stewart * O/S's may have seperate packet header and mbuf 372139bc87fSRandall Stewart * chain pointers.. thus the macro. 373139bc87fSRandall Stewart */ 374139bc87fSRandall Stewart #define SCTP_HEADER_TO_CHAIN(m) (m) 37517205eccSRandall Stewart #define SCTP_DETACH_HEADER_FROM_CHAIN(m) 376139bc87fSRandall Stewart #define SCTP_HEADER_LEN(m) (m->m_pkthdr.len) 37717205eccSRandall Stewart #define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0 37817205eccSRandall Stewart #define SCTP_RELEASE_HEADER(m) 37917205eccSRandall Stewart #define SCTP_RELEASE_PKT(m) sctp_m_freem(m) 380b3f1ea41SRandall Stewart #define SCTP_ENABLE_UDP_CSUM(m) do { \ 381b3f1ea41SRandall Stewart m->m_pkthdr.csum_flags = CSUM_UDP; \ 382b3f1ea41SRandall Stewart m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \ 383b3f1ea41SRandall Stewart } while (0) 38417205eccSRandall Stewart 38572fb6fdbSRandall Stewart #define SCTP_GET_PKT_VRFID(m, vrf_id) ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID) 38672fb6fdbSRandall Stewart 38772fb6fdbSRandall Stewart 388139bc87fSRandall Stewart 389139bc87fSRandall Stewart /* Attach the chain of data into the sendable packet. */ 390139bc87fSRandall Stewart #define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \ 39117205eccSRandall Stewart pak = m; \ 392139bc87fSRandall Stewart pak->m_pkthdr.len = packet_length; \ 393139bc87fSRandall Stewart } while(0) 394139bc87fSRandall Stewart 395139bc87fSRandall Stewart /* Other m_pkthdr type things */ 396207304d4SRandall Stewart #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0) 397207304d4SRandall Stewart #define SCTP_IS_IT_LOOPBACK(m) ((m->m_flags & M_PKTHDR) && ((m->m_pkthdr.rcvif == NULL) || (m->m_pkthdr.rcvif->if_type == IFT_LOOP))) 398139bc87fSRandall Stewart 399139bc87fSRandall Stewart 400139bc87fSRandall Stewart /* This converts any input packet header 401139bc87fSRandall Stewart * into the chain of data holders, for BSD 402139bc87fSRandall Stewart * its a NOP. 403139bc87fSRandall Stewart */ 404139bc87fSRandall Stewart 405139bc87fSRandall Stewart /* Macro's for getting length from V6/V4 header */ 406139bc87fSRandall Stewart #define SCTP_GET_IPV4_LENGTH(iph) (iph->ip_len) 407139bc87fSRandall Stewart #define SCTP_GET_IPV6_LENGTH(ip6) (ntohs(ip6->ip6_plen)) 408f8829a4aSRandall Stewart 40917205eccSRandall Stewart /* get the v6 hop limit */ 41017205eccSRandall Stewart #define SCTP_GET_HLIM(inp, ro) in6_selecthlim((struct in6pcb *)&inp->ip_inp.inp, (ro ? (ro->ro_rt ? (ro->ro_rt->rt_ifp) : (NULL)) : (NULL))); 41117205eccSRandall Stewart 41244b7479bSRandall Stewart /* is the endpoint v6only? */ 41344b7479bSRandall Stewart #define SCTP_IPV6_V6ONLY(inp) (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY) 41442551e99SRandall Stewart /* is the socket non-blocking? */ 41542551e99SRandall Stewart #define SCTP_SO_IS_NBIO(so) ((so)->so_state & SS_NBIO) 41642551e99SRandall Stewart #define SCTP_SET_SO_NBIO(so) ((so)->so_state |= SS_NBIO) 41742551e99SRandall Stewart #define SCTP_CLEAR_SO_NBIO(so) ((so)->so_state &= ~SS_NBIO) 41842551e99SRandall Stewart /* get the socket type */ 41942551e99SRandall Stewart #define SCTP_SO_TYPE(so) ((so)->so_type) 420132dea7dSRandall Stewart /* reserve sb space for a socket */ 421132dea7dSRandall Stewart #define SCTP_SORESERVE(so, send, recv) soreserve(so, send, recv) 42217205eccSRandall Stewart /* wakeup a socket */ 42317205eccSRandall Stewart #define SCTP_SOWAKEUP(so) wakeup(&(so)->so_timeo) 424132dea7dSRandall Stewart /* clear the socket buffer state */ 425132dea7dSRandall Stewart #define SCTP_SB_CLEAR(sb) \ 426132dea7dSRandall Stewart (sb).sb_cc = 0; \ 427132dea7dSRandall Stewart (sb).sb_mb = NULL; \ 428132dea7dSRandall Stewart (sb).sb_mbcnt = 0; 42944b7479bSRandall Stewart 43062c1ff9cSRandall Stewart #define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat 43162c1ff9cSRandall Stewart #define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat 43262c1ff9cSRandall Stewart 433f42a358aSRandall Stewart /* 434bff64a4dSRandall Stewart * routes, output, etc. 435bff64a4dSRandall Stewart */ 436bff64a4dSRandall Stewart typedef struct route sctp_route_t; 43717205eccSRandall Stewart typedef struct rtentry sctp_rtentry_t; 438bff64a4dSRandall Stewart 439b3f1ea41SRandall Stewart #define SCTP_RTALLOC(ro, vrf_id) rtalloc_ign((struct route *)ro, 0UL) 44017205eccSRandall Stewart 44117205eccSRandall Stewart /* Future zero copy wakeup/send function */ 44217205eccSRandall Stewart #define SCTP_ZERO_COPY_EVENT(inp, so) 443ad21a364SRandall Stewart /* This is re-pulse ourselves for sendbuf */ 444ad21a364SRandall Stewart #define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so) 44517205eccSRandall Stewart 44617205eccSRandall Stewart /* 44717205eccSRandall Stewart * IP output routines 44817205eccSRandall Stewart */ 449ad21a364SRandall Stewart #define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) \ 45017205eccSRandall Stewart { \ 45117205eccSRandall Stewart int o_flgs = 0; \ 45217205eccSRandall Stewart if (stcb && stcb->sctp_ep && stcb->sctp_ep->sctp_socket) { \ 45317205eccSRandall Stewart o_flgs = IP_RAWOUTPUT | (stcb->sctp_ep->sctp_socket->so_options & SO_DONTROUTE); \ 45417205eccSRandall Stewart } else { \ 45517205eccSRandall Stewart o_flgs = IP_RAWOUTPUT; \ 45617205eccSRandall Stewart } \ 45717205eccSRandall Stewart result = ip_output(o_pak, NULL, ro, o_flgs, 0, NULL); \ 45817205eccSRandall Stewart } 45917205eccSRandall Stewart 460ad21a364SRandall Stewart #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \ 46117205eccSRandall Stewart { \ 46217205eccSRandall Stewart if (stcb && stcb->sctp_ep) \ 46317205eccSRandall Stewart result = ip6_output(o_pak, \ 46417205eccSRandall Stewart ((struct in6pcb *)(stcb->sctp_ep))->in6p_outputopts, \ 46517205eccSRandall Stewart (ro), 0, 0, ifp, NULL); \ 46617205eccSRandall Stewart else \ 46717205eccSRandall Stewart result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \ 46817205eccSRandall Stewart } 469bff64a4dSRandall Stewart 470d06c82f1SRandall Stewart struct mbuf * 471d06c82f1SRandall Stewart sctp_get_mbuf_for_msg(unsigned int space_needed, 472d06c82f1SRandall Stewart int want_header, int how, int allonebuf, int type); 473d06c82f1SRandall Stewart 474d06c82f1SRandall Stewart 475bff64a4dSRandall Stewart /* 476f42a358aSRandall Stewart * SCTP AUTH 477f42a358aSRandall Stewart */ 478f42a358aSRandall Stewart #define HAVE_SHA2 479f42a358aSRandall Stewart 480f42a358aSRandall Stewart #define SCTP_READ_RANDOM(buf, len) read_random(buf, len) 481f42a358aSRandall Stewart 482f42a358aSRandall Stewart #ifdef USE_SCTP_SHA1 483f42a358aSRandall Stewart #include <netinet/sctp_sha1.h> 484f42a358aSRandall Stewart #else 485f42a358aSRandall Stewart #include <crypto/sha1.h> 486f42a358aSRandall Stewart /* map standard crypto API names */ 487f42a358aSRandall Stewart #define SHA1_Init SHA1Init 488f42a358aSRandall Stewart #define SHA1_Update SHA1Update 489f42a358aSRandall Stewart #define SHA1_Final(x,y) SHA1Final((caddr_t)x, y) 490f42a358aSRandall Stewart #endif 491f42a358aSRandall Stewart 492f42a358aSRandall Stewart #if defined(HAVE_SHA2) 493f42a358aSRandall Stewart #include <crypto/sha2/sha2.h> 494f42a358aSRandall Stewart #endif 495f42a358aSRandall Stewart 496f42a358aSRandall Stewart #include <sys/md5.h> 497f42a358aSRandall Stewart /* map standard crypto API names */ 498f42a358aSRandall Stewart #define MD5_Init MD5Init 499f42a358aSRandall Stewart #define MD5_Update MD5Update 500f42a358aSRandall Stewart #define MD5_Final MD5Final 501f42a358aSRandall Stewart 502f8829a4aSRandall Stewart #endif 503830d754dSRandall Stewart 504830d754dSRandall Stewart #define SCTP_DECREMENT_AND_CHECK_REFCOUNT(addr) (atomic_fetchadd_int(addr, -1) == 1) 505830d754dSRandall Stewart #if defined(INVARIANTS) 506830d754dSRandall Stewart #define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \ 507830d754dSRandall Stewart { \ 508830d754dSRandall Stewart int32_t oldval; \ 509830d754dSRandall Stewart oldval = atomic_fetchadd_int(addr, -val); \ 510830d754dSRandall Stewart if (oldval < val) { \ 511830d754dSRandall Stewart panic("Counter goes negative"); \ 512830d754dSRandall Stewart } \ 513830d754dSRandall Stewart } 514830d754dSRandall Stewart #else 515830d754dSRandall Stewart #define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \ 516830d754dSRandall Stewart { \ 517830d754dSRandall Stewart int32_t oldval; \ 518830d754dSRandall Stewart oldval = atomic_fetchadd_int(addr, -val); \ 519830d754dSRandall Stewart if (oldval < val) { \ 520830d754dSRandall Stewart *addr = 0; \ 521830d754dSRandall Stewart } \ 522830d754dSRandall Stewart } 523830d754dSRandall Stewart #endif 524