1f8829a4aSRandall Stewart /*- 2b1006367SRandall Stewart * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved. 3807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 4807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 5f8829a4aSRandall Stewart * 6f8829a4aSRandall Stewart * Redistribution and use in source and binary forms, with or without 7f8829a4aSRandall Stewart * modification, are permitted provided that the following conditions are met: 8f8829a4aSRandall Stewart * 9f8829a4aSRandall Stewart * a) Redistributions of source code must retain the above copyright notice, 10f8829a4aSRandall Stewart * this list of conditions and the following disclaimer. 11f8829a4aSRandall Stewart * 12f8829a4aSRandall Stewart * b) Redistributions in binary form must reproduce the above copyright 13f8829a4aSRandall Stewart * notice, this list of conditions and the following disclaimer in 14f8829a4aSRandall Stewart * the documentation and/or other materials provided with the distribution. 15f8829a4aSRandall Stewart * 16f8829a4aSRandall Stewart * c) Neither the name of Cisco Systems, Inc. nor the names of its 17f8829a4aSRandall Stewart * contributors may be used to endorse or promote products derived 18f8829a4aSRandall Stewart * from this software without specific prior written permission. 19f8829a4aSRandall Stewart * 20f8829a4aSRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21f8829a4aSRandall Stewart * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22f8829a4aSRandall Stewart * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23f8829a4aSRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24f8829a4aSRandall Stewart * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25f8829a4aSRandall Stewart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26f8829a4aSRandall Stewart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27f8829a4aSRandall Stewart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28f8829a4aSRandall Stewart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29f8829a4aSRandall Stewart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30f8829a4aSRandall Stewart * THE POSSIBILITY OF SUCH DAMAGE. 31f8829a4aSRandall Stewart */ 32807aad63SMichael Tuexen 3373932c69SRandall Stewart #include <sys/cdefs.h> 34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$"); 35807aad63SMichael Tuexen 36807aad63SMichael Tuexen #ifndef _NETINET_SCTP_OS_BSD_H_ 37807aad63SMichael Tuexen #define _NETINET_SCTP_OS_BSD_H_ 38f8829a4aSRandall Stewart /* 39f8829a4aSRandall Stewart * includes 40f8829a4aSRandall Stewart */ 4193164cf9SRandall Stewart #include "opt_ipsec.h" 4293164cf9SRandall Stewart #include "opt_compat.h" 4393164cf9SRandall Stewart #include "opt_inet6.h" 4493164cf9SRandall Stewart #include "opt_inet.h" 4593164cf9SRandall Stewart #include "opt_sctp.h" 46603724d3SBjoern A. Zeeb 4793164cf9SRandall Stewart #include <sys/param.h> 4880fefe0aSRandall Stewart #include <sys/ktr.h> 4993164cf9SRandall Stewart #include <sys/systm.h> 5093164cf9SRandall Stewart #include <sys/malloc.h> 5193164cf9SRandall Stewart #include <sys/kernel.h> 5293164cf9SRandall Stewart #include <sys/sysctl.h> 5393164cf9SRandall Stewart #include <sys/mbuf.h> 5493164cf9SRandall Stewart #include <sys/protosw.h> 5593164cf9SRandall Stewart #include <sys/socket.h> 5693164cf9SRandall Stewart #include <sys/socketvar.h> 5718e198d3SRandall Stewart #include <sys/jail.h> 5893164cf9SRandall Stewart #include <sys/sysctl.h> 5993164cf9SRandall Stewart #include <sys/resourcevar.h> 6093164cf9SRandall Stewart #include <sys/uio.h> 61c99efcf6SRandall Stewart #include <sys/lock.h> 62c99efcf6SRandall Stewart #include <sys/rwlock.h> 6342551e99SRandall Stewart #include <sys/kthread.h> 6493164cf9SRandall Stewart #include <sys/priv.h> 65f8829a4aSRandall Stewart #include <sys/random.h> 6693164cf9SRandall Stewart #include <sys/limits.h> 6793164cf9SRandall Stewart #include <sys/queue.h> 6893164cf9SRandall Stewart #include <machine/cpu.h> 6993164cf9SRandall Stewart 7093164cf9SRandall Stewart #include <net/if.h> 7193164cf9SRandall Stewart #include <net/if_types.h> 7293164cf9SRandall Stewart #include <net/if_var.h> 7393164cf9SRandall Stewart #include <net/route.h> 7445623593SMichael Tuexen #include <net/vnet.h> 7593164cf9SRandall Stewart 7693164cf9SRandall Stewart #include <netinet/in.h> 7793164cf9SRandall Stewart #include <netinet/in_systm.h> 7893164cf9SRandall Stewart #include <netinet/ip.h> 7993164cf9SRandall Stewart #include <netinet/in_pcb.h> 8093164cf9SRandall Stewart #include <netinet/in_var.h> 8193164cf9SRandall Stewart #include <netinet/ip_var.h> 8293164cf9SRandall Stewart #include <netinet/ip_icmp.h> 8393164cf9SRandall Stewart #include <netinet/icmp_var.h> 8493164cf9SRandall Stewart 85b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 862cb64cb2SGeorge V. Neville-Neil #include <netipsec/ipsec.h> 872cb64cb2SGeorge V. Neville-Neil #include <netipsec/key.h> 8893164cf9SRandall Stewart #endif /* IPSEC */ 8993164cf9SRandall Stewart 9093164cf9SRandall Stewart #ifdef INET6 9193164cf9SRandall Stewart #include <sys/domain.h> 92b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 930d29af67SGeorge V. Neville-Neil #include <netipsec/ipsec6.h> 9493164cf9SRandall Stewart #endif 9593164cf9SRandall Stewart #include <netinet/ip6.h> 9693164cf9SRandall Stewart #include <netinet6/ip6_var.h> 9793164cf9SRandall Stewart #include <netinet6/in6_pcb.h> 9893164cf9SRandall Stewart #include <netinet/icmp6.h> 9993164cf9SRandall Stewart #include <netinet6/ip6protosw.h> 10093164cf9SRandall Stewart #include <netinet6/nd6.h> 10193164cf9SRandall Stewart #include <netinet6/scope6_var.h> 10293164cf9SRandall Stewart #endif /* INET6 */ 10393164cf9SRandall Stewart 104f42a358aSRandall Stewart 10593164cf9SRandall Stewart #include <netinet/ip_options.h> 10693164cf9SRandall Stewart 107e6b2b4b6SMichael Tuexen #include <crypto/sha1.h> 108e6b2b4b6SMichael Tuexen #include <crypto/sha2/sha2.h> 109e6b2b4b6SMichael Tuexen 11093164cf9SRandall Stewart #ifndef in6pcb 11193164cf9SRandall Stewart #define in6pcb inpcb 11293164cf9SRandall Stewart #endif 113207304d4SRandall Stewart /* Declare all the malloc names for all the various mallocs */ 114207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_MAP); 115207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRMI); 116207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRMO); 117207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ASC_ADDR); 118207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ASC_IT); 119207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_CL); 120207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_KY); 121207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_HL); 122207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_IF); 123207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRESET); 124207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_CMSG); 125207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_COPYAL); 126207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_VRF); 127207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_IFA); 128207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_IFN); 129207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_TIMW); 130207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_MVRF); 131207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ITER); 132207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_SOCKOPT); 133bfc46083SRandall Stewart MALLOC_DECLARE(SCTP_M_MCORE); 134f8829a4aSRandall Stewart 135c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF) 136c4739e2fSRandall Stewart 137c4739e2fSRandall Stewart #define SCTP_GET_CYCLECOUNT get_cyclecount() 138c4739e2fSRandall Stewart #define SCTP_CTR6 sctp_log_trace 139c4739e2fSRandall Stewart 140c4739e2fSRandall Stewart #else 141c4739e2fSRandall Stewart #define SCTP_CTR6 CTR6 142c4739e2fSRandall Stewart #endif 143c4739e2fSRandall Stewart 144f8829a4aSRandall Stewart /* 145fc14de76SRandall Stewart * Macros to expand out globals defined by various modules 146fc14de76SRandall Stewart * to either a real global or a virtualized instance of one, 147603724d3SBjoern A. Zeeb * depending on whether VIMAGE is defined. 148fc14de76SRandall Stewart */ 149fc14de76SRandall Stewart /* then define the macro(s) that hook into the vimage macros */ 150482444b4SRandall Stewart #define MODULE_GLOBAL(__SYMBOL) V_##__SYMBOL 151482444b4SRandall Stewart 1528518270eSMichael Tuexen #define V_system_base_info VNET(system_base_info) 153482444b4SRandall Stewart #define SCTP_BASE_INFO(__m) V_system_base_info.sctppcbinfo.__m 154482444b4SRandall Stewart #define SCTP_BASE_STATS V_system_base_info.sctpstat 155482444b4SRandall Stewart #define SCTP_BASE_STAT(__m) V_system_base_info.sctpstat.__m 156f47f328dSMichael Tuexen #define SCTP_BASE_SYSCTL(__m) V_system_base_info.sctpsysctl.__m 157482444b4SRandall Stewart #define SCTP_BASE_VAR(__m) V_system_base_info.__m 158a99b6783SRandall Stewart 159cd3fd531SMichael Tuexen #define SCTP_PRINTF(params...) printf(params) 16017205eccSRandall Stewart #if defined(SCTP_DEBUG) 16117205eccSRandall Stewart #define SCTPDBG(level, params...) \ 16217205eccSRandall Stewart { \ 16317205eccSRandall Stewart do { \ 164b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) { \ 165cd3fd531SMichael Tuexen SCTP_PRINTF(params); \ 16617205eccSRandall Stewart } \ 16717205eccSRandall Stewart } while (0); \ 16817205eccSRandall Stewart } 16917205eccSRandall Stewart #define SCTPDBG_ADDR(level, addr) \ 17017205eccSRandall Stewart { \ 17117205eccSRandall Stewart do { \ 172b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) { \ 17317205eccSRandall Stewart sctp_print_address(addr); \ 17417205eccSRandall Stewart } \ 17517205eccSRandall Stewart } while (0); \ 17617205eccSRandall Stewart } 17717205eccSRandall Stewart #else 17817205eccSRandall Stewart #define SCTPDBG(level, params...) 17917205eccSRandall Stewart #define SCTPDBG_ADDR(level, addr) 18017205eccSRandall Stewart #endif 18117205eccSRandall Stewart 18280fefe0aSRandall Stewart #ifdef SCTP_LTRACE_CHUNKS 183dfb11ef8SRandall Stewart #define SCTP_LTRACE_CHK(a, b, c, d) if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_CHUNK_ENABLE) SCTP_CTR6(KTR_SUBSYS, "SCTP:%d[%d]:%x-%x-%x-%x", SCTP_LOG_CHUNK_PROC, 0, a, b, c, d) 18480fefe0aSRandall Stewart #else 18580fefe0aSRandall Stewart #define SCTP_LTRACE_CHK(a, b, c, d) 18680fefe0aSRandall Stewart #endif 18780fefe0aSRandall Stewart 18880fefe0aSRandall Stewart #ifdef SCTP_LTRACE_ERRORS 189cd3fd531SMichael Tuexen #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \ 190cd3fd531SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \ 191cd3fd531SMichael Tuexen SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \ 192c4739e2fSRandall Stewart m, inp, stcb, net, file, __LINE__, err); 193cd3fd531SMichael Tuexen #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \ 194cd3fd531SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \ 195cd3fd531SMichael Tuexen SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \ 196c4739e2fSRandall Stewart inp, stcb, net, file, __LINE__, err); 19780fefe0aSRandall Stewart #else 198c4739e2fSRandall Stewart #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) 199c4739e2fSRandall Stewart #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) 20080fefe0aSRandall Stewart #endif 20180fefe0aSRandall Stewart 202c4739e2fSRandall Stewart 203f8829a4aSRandall Stewart /* 20442551e99SRandall Stewart * Local address and interface list handling 20542551e99SRandall Stewart */ 20642551e99SRandall Stewart #define SCTP_MAX_VRF_ID 0 20742551e99SRandall Stewart #define SCTP_SIZE_OF_VRF_HASH 3 20842551e99SRandall Stewart #define SCTP_IFNAMSIZ IFNAMSIZ 20942551e99SRandall Stewart #define SCTP_DEFAULT_VRFID 0 21017205eccSRandall Stewart #define SCTP_VRF_ADDR_HASH_SIZE 16 21117205eccSRandall Stewart #define SCTP_VRF_IFN_HASH_SIZE 3 212f4c93d24SRandall Stewart #define SCTP_INIT_VRF_TABLEID(vrf) 21342551e99SRandall Stewart 21442551e99SRandall Stewart #define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP) 215c54a18d2SRandall 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) 21642551e99SRandall Stewart 21742551e99SRandall Stewart /* 21842551e99SRandall Stewart * Access to IFN's to help with src-addr-selection 21942551e99SRandall Stewart */ 22042551e99SRandall Stewart /* This could return VOID if the index works but for BSD we provide both. */ 22142551e99SRandall Stewart #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp 22217205eccSRandall Stewart #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) (ro)->ro_rt->rt_ifp->if_index 22317205eccSRandall Stewart #define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp) 22442551e99SRandall Stewart 22542551e99SRandall Stewart /* 226f8829a4aSRandall Stewart * general memory allocation 227f8829a4aSRandall Stewart */ 228f8829a4aSRandall Stewart #define SCTP_MALLOC(var, type, size, name) \ 229f8829a4aSRandall Stewart do { \ 2301ede983cSDag-Erling Smørgrav var = (type)malloc(size, name, M_NOWAIT); \ 231f8829a4aSRandall Stewart } while (0) 232f8829a4aSRandall Stewart 2331ede983cSDag-Erling Smørgrav #define SCTP_FREE(var, type) free(var, type) 234f8829a4aSRandall Stewart 235f8829a4aSRandall Stewart #define SCTP_MALLOC_SONAME(var, type, size) \ 236f8829a4aSRandall Stewart do { \ 2371ede983cSDag-Erling Smørgrav var = (type)malloc(size, M_SONAME, M_WAITOK | M_ZERO); \ 238f8829a4aSRandall Stewart } while (0) 239f8829a4aSRandall Stewart 2401ede983cSDag-Erling Smørgrav #define SCTP_FREE_SONAME(var) free(var, M_SONAME) 241f8829a4aSRandall Stewart 24242551e99SRandall Stewart #define SCTP_PROCESS_STRUCT struct proc * 24342551e99SRandall Stewart 244f8829a4aSRandall Stewart /* 245f8829a4aSRandall Stewart * zone allocation functions 246f8829a4aSRandall Stewart */ 247f8829a4aSRandall Stewart #include <vm/uma.h> 248c54a18d2SRandall Stewart 249f8829a4aSRandall Stewart /* SCTP_ZONE_INIT: initialize the zone */ 250f8829a4aSRandall Stewart typedef struct uma_zone *sctp_zone_t; 251f8829a4aSRandall Stewart 252f8829a4aSRandall Stewart #define SCTP_ZONE_INIT(zone, name, size, number) { \ 253f8829a4aSRandall Stewart zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\ 254cf458c64SMichael Tuexen 0); \ 255f8829a4aSRandall Stewart uma_zone_set_max(zone, number); \ 256f8829a4aSRandall Stewart } 257f8829a4aSRandall Stewart 258c54a18d2SRandall Stewart #define SCTP_ZONE_DESTROY(zone) uma_zdestroy(zone) 259c54a18d2SRandall Stewart 260f8829a4aSRandall Stewart /* SCTP_ZONE_GET: allocate element from the zone */ 261f42a358aSRandall Stewart #define SCTP_ZONE_GET(zone, type) \ 262f42a358aSRandall Stewart (type *)uma_zalloc(zone, M_NOWAIT); 263f8829a4aSRandall Stewart 264f8829a4aSRandall Stewart /* SCTP_ZONE_FREE: free element from the zone */ 265f8829a4aSRandall Stewart #define SCTP_ZONE_FREE(zone, element) \ 266f8829a4aSRandall Stewart uma_zfree(zone, element); 267c54a18d2SRandall Stewart 26844b7479bSRandall Stewart #define SCTP_HASH_INIT(size, hashmark) hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT) 26944b7479bSRandall Stewart #define SCTP_HASH_FREE(table, hashmark) hashdestroy(table, M_PCB, hashmark) 27044b7479bSRandall Stewart 27144b7479bSRandall Stewart #define SCTP_M_COPYM m_copym 272f8829a4aSRandall Stewart 273f8829a4aSRandall Stewart /* 274139bc87fSRandall Stewart * timers 275139bc87fSRandall Stewart */ 276139bc87fSRandall Stewart #include <sys/callout.h> 277139bc87fSRandall Stewart typedef struct callout sctp_os_timer_t; 278139bc87fSRandall Stewart 279c54a18d2SRandall Stewart 280139bc87fSRandall Stewart #define SCTP_OS_TIMER_INIT(tmr) callout_init(tmr, 1) 281139bc87fSRandall Stewart #define SCTP_OS_TIMER_START callout_reset 282139bc87fSRandall Stewart #define SCTP_OS_TIMER_STOP callout_stop 28344b7479bSRandall Stewart #define SCTP_OS_TIMER_STOP_DRAIN callout_drain 284139bc87fSRandall Stewart #define SCTP_OS_TIMER_PENDING callout_pending 285139bc87fSRandall Stewart #define SCTP_OS_TIMER_ACTIVE callout_active 286139bc87fSRandall Stewart #define SCTP_OS_TIMER_DEACTIVATE callout_deactivate 287139bc87fSRandall Stewart 288207304d4SRandall Stewart #define sctp_get_tick_count() (ticks) 289207304d4SRandall Stewart 290ceaad40aSRandall Stewart #define SCTP_UNUSED __attribute__((unused)) 29135918f85SRandall Stewart 292139bc87fSRandall Stewart /* 293f8829a4aSRandall Stewart * Functions 294f8829a4aSRandall Stewart */ 295139bc87fSRandall Stewart /* Mbuf manipulation and access macros */ 296139bc87fSRandall Stewart #define SCTP_BUF_LEN(m) (m->m_len) 297139bc87fSRandall Stewart #define SCTP_BUF_NEXT(m) (m->m_next) 298139bc87fSRandall Stewart #define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt) 299139bc87fSRandall Stewart #define SCTP_BUF_RESV_UF(m, size) m->m_data += size 300139bc87fSRandall Stewart #define SCTP_BUF_AT(m, size) m->m_data + size 301139bc87fSRandall Stewart #define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT) 302*296d0b94SMichael Tuexen #define SCTP_BUF_SIZE M_SIZE 303139bc87fSRandall Stewart #define SCTP_BUF_TYPE(m) (m->m_type) 304139bc87fSRandall Stewart #define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif) 305139bc87fSRandall Stewart #define SCTP_BUF_PREPEND M_PREPEND 3065e54f665SRandall Stewart 307ed6a66caSRobert Watson #define SCTP_ALIGN_TO_END(m, len) M_ALIGN(m, len) 3080696e120SRandall Stewart 3090696e120SRandall Stewart /* We make it so if you have up to 4 threads 310b1ce21c6SRebecca Cran * writing based on the default size of 3110696e120SRandall Stewart * the packet log 65 k, that would be 3120696e120SRandall Stewart * 4 16k packets before we would hit 3130696e120SRandall Stewart * a problem. 3140696e120SRandall Stewart */ 3150696e120SRandall Stewart #define SCTP_PKTLOG_WRITERS_NEED_LOCK 3 3160696e120SRandall Stewart 31717205eccSRandall Stewart /*************************/ 31817205eccSRandall Stewart /* MTU */ 31917205eccSRandall Stewart /*************************/ 320ad21a364SRandall Stewart #define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) ((struct ifnet *)ifn)->if_mtu 32143f9f175SMichael Tuexen #define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((uint32_t)((rt != NULL) ? rt->rt_mtu : 0)) 32217205eccSRandall Stewart #define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) ((sctp_ifn->ifn_p != NULL) ? ((struct ifnet *)(sctp_ifn->ifn_p))->if_mtu : 0) 32317205eccSRandall Stewart #define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \ 32417205eccSRandall Stewart if (rt != NULL) \ 325e3a7aa6fSGleb Smirnoff rt->rt_mtu = mtu; \ 32617205eccSRandall Stewart } while(0) 32717205eccSRandall Stewart 32817205eccSRandall Stewart /* (de-)register interface event notifications */ 329ad21a364SRandall Stewart #define SCTP_REGISTER_INTERFACE(ifhandle, af) 330ad21a364SRandall Stewart #define SCTP_DEREGISTER_INTERFACE(ifhandle, af) 3315e54f665SRandall Stewart 33218e198d3SRandall Stewart 333139bc87fSRandall Stewart /*************************/ 334139bc87fSRandall Stewart /* These are for logging */ 335139bc87fSRandall Stewart /*************************/ 336139bc87fSRandall Stewart /* return the base ext data pointer */ 337139bc87fSRandall Stewart #define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf) 338139bc87fSRandall Stewart /* return the refcnt of the data pointer */ 339fcc34a23SGleb Smirnoff #define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ext_cnt) 340139bc87fSRandall Stewart /* return any buffer related flags, this is 341139bc87fSRandall Stewart * used beyond logging for apple only. 342139bc87fSRandall Stewart */ 343139bc87fSRandall Stewart #define SCTP_BUF_GET_FLAGS(m) (m->m_flags) 344139bc87fSRandall Stewart 345139bc87fSRandall Stewart /* For BSD this just accesses the M_PKTHDR length 346139bc87fSRandall Stewart * so it operates on an mbuf with hdr flag. Other 347b1ce21c6SRebecca Cran * O/S's may have separate packet header and mbuf 348139bc87fSRandall Stewart * chain pointers.. thus the macro. 349139bc87fSRandall Stewart */ 350139bc87fSRandall Stewart #define SCTP_HEADER_TO_CHAIN(m) (m) 35117205eccSRandall Stewart #define SCTP_DETACH_HEADER_FROM_CHAIN(m) 352f9384252SMichael Tuexen #define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len) 35317205eccSRandall Stewart #define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0 35417205eccSRandall Stewart #define SCTP_RELEASE_HEADER(m) 35517205eccSRandall Stewart #define SCTP_RELEASE_PKT(m) sctp_m_freem(m) 356b3f1ea41SRandall Stewart #define SCTP_ENABLE_UDP_CSUM(m) do { \ 357b3f1ea41SRandall Stewart m->m_pkthdr.csum_flags = CSUM_UDP; \ 358b3f1ea41SRandall Stewart m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \ 359b3f1ea41SRandall Stewart } while (0) 36017205eccSRandall Stewart 36172fb6fdbSRandall Stewart #define SCTP_GET_PKT_VRFID(m, vrf_id) ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID) 36272fb6fdbSRandall Stewart 36372fb6fdbSRandall Stewart 364139bc87fSRandall Stewart 365139bc87fSRandall Stewart /* Attach the chain of data into the sendable packet. */ 366139bc87fSRandall Stewart #define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \ 36717205eccSRandall Stewart pak = m; \ 368139bc87fSRandall Stewart pak->m_pkthdr.len = packet_length; \ 369139bc87fSRandall Stewart } while(0) 370139bc87fSRandall Stewart 371139bc87fSRandall Stewart /* Other m_pkthdr type things */ 372207304d4SRandall Stewart #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0) 373207304d4SRandall 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))) 374139bc87fSRandall Stewart 375139bc87fSRandall Stewart 376139bc87fSRandall Stewart /* This converts any input packet header 377139bc87fSRandall Stewart * into the chain of data holders, for BSD 378139bc87fSRandall Stewart * its a NOP. 379139bc87fSRandall Stewart */ 380139bc87fSRandall Stewart 38117205eccSRandall Stewart /* get the v6 hop limit */ 38217205eccSRandall 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))); 38317205eccSRandall Stewart 38444b7479bSRandall Stewart /* is the endpoint v6only? */ 38544b7479bSRandall Stewart #define SCTP_IPV6_V6ONLY(inp) (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY) 38642551e99SRandall Stewart /* is the socket non-blocking? */ 38742551e99SRandall Stewart #define SCTP_SO_IS_NBIO(so) ((so)->so_state & SS_NBIO) 38842551e99SRandall Stewart #define SCTP_SET_SO_NBIO(so) ((so)->so_state |= SS_NBIO) 38942551e99SRandall Stewart #define SCTP_CLEAR_SO_NBIO(so) ((so)->so_state &= ~SS_NBIO) 39042551e99SRandall Stewart /* get the socket type */ 39142551e99SRandall Stewart #define SCTP_SO_TYPE(so) ((so)->so_type) 3924e88d37aSMichael Tuexen /* Use a macro for renaming sb_cc to sb_ccc */ 3934e88d37aSMichael Tuexen #define sb_cc sb_ccc 394132dea7dSRandall Stewart /* reserve sb space for a socket */ 395132dea7dSRandall Stewart #define SCTP_SORESERVE(so, send, recv) soreserve(so, send, recv) 39617205eccSRandall Stewart /* wakeup a socket */ 39717205eccSRandall Stewart #define SCTP_SOWAKEUP(so) wakeup(&(so)->so_timeo) 398132dea7dSRandall Stewart /* clear the socket buffer state */ 399132dea7dSRandall Stewart #define SCTP_SB_CLEAR(sb) \ 4004e88d37aSMichael Tuexen (sb).sb_cc = 0; \ 401132dea7dSRandall Stewart (sb).sb_mb = NULL; \ 402132dea7dSRandall Stewart (sb).sb_mbcnt = 0; 40344b7479bSRandall Stewart 40462c1ff9cSRandall Stewart #define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat 40562c1ff9cSRandall Stewart #define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat 40662c1ff9cSRandall Stewart 407f42a358aSRandall Stewart /* 408bff64a4dSRandall Stewart * routes, output, etc. 409bff64a4dSRandall Stewart */ 410bff64a4dSRandall Stewart typedef struct route sctp_route_t; 41117205eccSRandall Stewart typedef struct rtentry sctp_rtentry_t; 412bff64a4dSRandall Stewart 4139836132cSBjoern A. Zeeb /* 4149836132cSBjoern A. Zeeb * XXX multi-FIB support was backed out in r179783 and it seems clear that the 4159836132cSBjoern A. Zeeb * VRF support as currently in FreeBSD is not ready to support multi-FIB. 4169836132cSBjoern A. Zeeb * It might be best to implement multi-FIB support for both v4 and v6 indepedent 4179836132cSBjoern A. Zeeb * of VRFs and leave those to a real MPLS stack. 4189836132cSBjoern A. Zeeb */ 419b3f1ea41SRandall Stewart #define SCTP_RTALLOC(ro, vrf_id) rtalloc_ign((struct route *)ro, 0UL) 42017205eccSRandall Stewart 42117205eccSRandall Stewart /* Future zero copy wakeup/send function */ 42217205eccSRandall Stewart #define SCTP_ZERO_COPY_EVENT(inp, so) 423ad21a364SRandall Stewart /* This is re-pulse ourselves for sendbuf */ 424ad21a364SRandall Stewart #define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so) 42517205eccSRandall Stewart 42617205eccSRandall Stewart /* 427678d7b94SAndre Oppermann * SCTP protocol specific mbuf flags. 428678d7b94SAndre Oppermann */ 4295fc98a78SAndre Oppermann #define M_NOTIFICATION M_PROTO1 /* SCTP notification */ 430678d7b94SAndre Oppermann 431678d7b94SAndre Oppermann /* 43217205eccSRandall Stewart * IP output routines 43317205eccSRandall Stewart */ 434ad21a364SRandall Stewart #define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) \ 43517205eccSRandall Stewart { \ 4369c7635e1SMichael Tuexen int o_flgs = IP_RAWOUTPUT; \ 4379c7635e1SMichael Tuexen struct sctp_tcb *local_stcb = stcb; \ 4389c7635e1SMichael Tuexen if (local_stcb && \ 4399c7635e1SMichael Tuexen local_stcb->sctp_ep && \ 4409c7635e1SMichael Tuexen local_stcb->sctp_ep->sctp_socket) \ 4419c7635e1SMichael Tuexen o_flgs |= local_stcb->sctp_ep->sctp_socket->so_options & SO_DONTROUTE; \ 44286bd0491SAndre Oppermann m_clrprotoflags(o_pak); \ 44317205eccSRandall Stewart result = ip_output(o_pak, NULL, ro, o_flgs, 0, NULL); \ 44417205eccSRandall Stewart } 44517205eccSRandall Stewart 446ad21a364SRandall Stewart #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \ 44717205eccSRandall Stewart { \ 4489c7635e1SMichael Tuexen struct sctp_tcb *local_stcb = stcb; \ 44986bd0491SAndre Oppermann m_clrprotoflags(o_pak); \ 4509c7635e1SMichael Tuexen if (local_stcb && local_stcb->sctp_ep) \ 45117205eccSRandall Stewart result = ip6_output(o_pak, \ 4529c7635e1SMichael Tuexen ((struct in6pcb *)(local_stcb->sctp_ep))->in6p_outputopts, \ 45317205eccSRandall Stewart (ro), 0, 0, ifp, NULL); \ 45417205eccSRandall Stewart else \ 45517205eccSRandall Stewart result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \ 45617205eccSRandall Stewart } 457bff64a4dSRandall Stewart 458d06c82f1SRandall Stewart struct mbuf * 459d06c82f1SRandall Stewart sctp_get_mbuf_for_msg(unsigned int space_needed, 460d06c82f1SRandall Stewart int want_header, int how, int allonebuf, int type); 461d06c82f1SRandall Stewart 462d06c82f1SRandall Stewart 463bff64a4dSRandall Stewart /* 464f42a358aSRandall Stewart * SCTP AUTH 465f42a358aSRandall Stewart */ 466f42a358aSRandall Stewart #define SCTP_READ_RANDOM(buf, len) read_random(buf, len) 467f42a358aSRandall Stewart 468f42a358aSRandall Stewart /* map standard crypto API names */ 469e6b2b4b6SMichael Tuexen #define SCTP_SHA1_CTX SHA1_CTX 470e6b2b4b6SMichael Tuexen #define SCTP_SHA1_INIT SHA1Init 471e6b2b4b6SMichael Tuexen #define SCTP_SHA1_UPDATE SHA1Update 472e6b2b4b6SMichael Tuexen #define SCTP_SHA1_FINAL(x,y) SHA1Final((caddr_t)x, y) 473f42a358aSRandall Stewart 474e6b2b4b6SMichael Tuexen #define SCTP_SHA256_CTX SHA256_CTX 475e6b2b4b6SMichael Tuexen #define SCTP_SHA256_INIT SHA256_Init 476e6b2b4b6SMichael Tuexen #define SCTP_SHA256_UPDATE SHA256_Update 477e6b2b4b6SMichael Tuexen #define SCTP_SHA256_FINAL(x,y) SHA256_Final((caddr_t)x, y) 478f42a358aSRandall Stewart 479f8829a4aSRandall Stewart #endif 480830d754dSRandall Stewart 481830d754dSRandall Stewart #define SCTP_DECREMENT_AND_CHECK_REFCOUNT(addr) (atomic_fetchadd_int(addr, -1) == 1) 482830d754dSRandall Stewart #if defined(INVARIANTS) 483830d754dSRandall Stewart #define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \ 484830d754dSRandall Stewart { \ 485830d754dSRandall Stewart int32_t oldval; \ 486830d754dSRandall Stewart oldval = atomic_fetchadd_int(addr, -val); \ 487830d754dSRandall Stewart if (oldval < val) { \ 488830d754dSRandall Stewart panic("Counter goes negative"); \ 489830d754dSRandall Stewart } \ 490830d754dSRandall Stewart } 491830d754dSRandall Stewart #else 492830d754dSRandall Stewart #define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \ 493830d754dSRandall Stewart { \ 494830d754dSRandall Stewart int32_t oldval; \ 495830d754dSRandall Stewart oldval = atomic_fetchadd_int(addr, -val); \ 496830d754dSRandall Stewart if (oldval < val) { \ 497830d754dSRandall Stewart *addr = 0; \ 498830d754dSRandall Stewart } \ 499830d754dSRandall Stewart } 500830d754dSRandall Stewart #endif 501