xref: /freebsd/sys/netinet/sctp_os_bsd.h (revision 197fc4cad0973efa4bb2ab86ed537eafa3ae254e)
1f8829a4aSRandall Stewart /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4b1006367SRandall Stewart  * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved.
5807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7f8829a4aSRandall Stewart  *
8f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
9f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
10f8829a4aSRandall Stewart  *
11f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
12f8829a4aSRandall Stewart  *   this list of conditions and the following disclaimer.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
15f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
16f8829a4aSRandall Stewart  *   the documentation and/or other materials provided with the distribution.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
20f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
21f8829a4aSRandall Stewart  *
22f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
33f8829a4aSRandall Stewart  */
34807aad63SMichael Tuexen 
35807aad63SMichael Tuexen #ifndef _NETINET_SCTP_OS_BSD_H_
36807aad63SMichael Tuexen #define _NETINET_SCTP_OS_BSD_H_
37c3179e66SMichael Tuexen 
38c3179e66SMichael Tuexen #include <sys/cdefs.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>
44052c5ec4SMark Johnston #include <sys/domain.h>
45052c5ec4SMark Johnston #include <sys/eventhandler.h>
4680fefe0aSRandall Stewart #include <sys/ktr.h>
4793164cf9SRandall Stewart #include <sys/systm.h>
4893164cf9SRandall Stewart #include <sys/malloc.h>
49052c5ec4SMark Johnston #include <sys/module.h>
5093164cf9SRandall Stewart #include <sys/kernel.h>
5193164cf9SRandall Stewart #include <sys/sysctl.h>
5293164cf9SRandall Stewart #include <sys/mbuf.h>
5393164cf9SRandall Stewart #include <sys/protosw.h>
5493164cf9SRandall Stewart #include <sys/socket.h>
5593164cf9SRandall Stewart #include <sys/socketvar.h>
5618e198d3SRandall Stewart #include <sys/jail.h>
5793164cf9SRandall Stewart #include <sys/sysctl.h>
5893164cf9SRandall Stewart #include <sys/resourcevar.h>
5993164cf9SRandall Stewart #include <sys/uio.h>
60c99efcf6SRandall Stewart #include <sys/lock.h>
61c99efcf6SRandall Stewart #include <sys/rwlock.h>
6242551e99SRandall Stewart #include <sys/kthread.h>
6393164cf9SRandall Stewart #include <sys/priv.h>
64f8829a4aSRandall Stewart #include <sys/random.h>
6593164cf9SRandall Stewart #include <sys/limits.h>
6693164cf9SRandall Stewart #include <sys/queue.h>
6793164cf9SRandall Stewart #include <machine/cpu.h>
6893164cf9SRandall Stewart 
6993164cf9SRandall Stewart #include <net/if.h>
7093164cf9SRandall Stewart #include <net/if_types.h>
7193164cf9SRandall Stewart #include <net/if_var.h>
723d0d5b21SJustin Hibbits #include <net/if_private.h>
7393164cf9SRandall Stewart #include <net/route.h>
74983066f0SAlexander V. Chernikov #include <net/route/nhop.h>
7545623593SMichael Tuexen #include <net/vnet.h>
7693164cf9SRandall Stewart 
7793164cf9SRandall Stewart #include <netinet/in.h>
7893164cf9SRandall Stewart #include <netinet/in_systm.h>
7993164cf9SRandall Stewart #include <netinet/ip.h>
80983066f0SAlexander V. Chernikov #include <netinet/in_fib.h>
8193164cf9SRandall Stewart #include <netinet/in_pcb.h>
8293164cf9SRandall Stewart #include <netinet/in_var.h>
8393164cf9SRandall Stewart #include <netinet/ip_var.h>
8493164cf9SRandall Stewart #include <netinet/ip_icmp.h>
8593164cf9SRandall Stewart #include <netinet/icmp_var.h>
8693164cf9SRandall Stewart 
8793164cf9SRandall Stewart #ifdef INET6
8893164cf9SRandall Stewart #include <netinet/ip6.h>
89983066f0SAlexander V. Chernikov #include <netinet6/in6_fib.h>
9093164cf9SRandall Stewart #include <netinet6/ip6_var.h>
9193164cf9SRandall Stewart #include <netinet6/in6_pcb.h>
9293164cf9SRandall Stewart #include <netinet6/nd6.h>
9393164cf9SRandall Stewart #include <netinet6/scope6_var.h>
9493164cf9SRandall Stewart #endif				/* INET6 */
9593164cf9SRandall Stewart 
9693164cf9SRandall Stewart #include <netinet/ip_options.h>
9793164cf9SRandall Stewart 
98e6b2b4b6SMichael Tuexen #include <crypto/sha1.h>
997a3f5d11SAllan Jude #include <crypto/sha2/sha256.h>
100e6b2b4b6SMichael Tuexen 
101207304d4SRandall Stewart /* Declare all the malloc names for all the various mallocs */
102207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_MAP);
103207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRMI);
104207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRMO);
105207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ASC_ADDR);
106207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ASC_IT);
107207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_CL);
108207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_KY);
109207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_HL);
110207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_IF);
111207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRESET);
112207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_CMSG);
113207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_COPYAL);
114207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_VRF);
115207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_IFA);
116207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_IFN);
117207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_TIMW);
118207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_MVRF);
119207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ITER);
120207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_SOCKOPT);
121bfc46083SRandall Stewart MALLOC_DECLARE(SCTP_M_MCORE);
122f8829a4aSRandall Stewart 
123c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
124c4739e2fSRandall Stewart 
125c4739e2fSRandall Stewart #define SCTP_GET_CYCLECOUNT get_cyclecount()
126c4739e2fSRandall Stewart #define SCTP_CTR6 sctp_log_trace
127c4739e2fSRandall Stewart 
128c4739e2fSRandall Stewart #else
129c4739e2fSRandall Stewart #define SCTP_CTR6 CTR6
130c4739e2fSRandall Stewart #endif
131c4739e2fSRandall Stewart 
132f8829a4aSRandall Stewart /*
133fc14de76SRandall Stewart  * Macros to expand out globals defined by various modules
134fc14de76SRandall Stewart  * to either a real global or a virtualized instance of one,
135603724d3SBjoern A. Zeeb  * depending on whether VIMAGE is defined.
136fc14de76SRandall Stewart  */
137fc14de76SRandall Stewart /* then define the macro(s) that hook into the vimage macros */
138482444b4SRandall Stewart #define MODULE_GLOBAL(__SYMBOL) V_##__SYMBOL
139482444b4SRandall Stewart 
1408518270eSMichael Tuexen #define V_system_base_info VNET(system_base_info)
141482444b4SRandall Stewart #define SCTP_BASE_INFO(__m) V_system_base_info.sctppcbinfo.__m
142482444b4SRandall Stewart #define SCTP_BASE_STATS V_system_base_info.sctpstat
143482444b4SRandall Stewart #define SCTP_BASE_STAT(__m) V_system_base_info.sctpstat.__m
144f47f328dSMichael Tuexen #define SCTP_BASE_SYSCTL(__m) V_system_base_info.sctpsysctl.__m
145482444b4SRandall Stewart #define SCTP_BASE_VAR(__m) V_system_base_info.__m
146a99b6783SRandall Stewart 
147cd3fd531SMichael Tuexen #define SCTP_PRINTF(params...)	printf(params)
14817205eccSRandall Stewart #if defined(SCTP_DEBUG)
14917205eccSRandall Stewart #define SCTPDBG(level, params...)					\
15017205eccSRandall Stewart {									\
15117205eccSRandall Stewart 	do {								\
152b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {		\
153cd3fd531SMichael Tuexen 			SCTP_PRINTF(params);				\
15417205eccSRandall Stewart 		}							\
15517205eccSRandall Stewart 	} while (0);							\
15617205eccSRandall Stewart }
15717205eccSRandall Stewart #define SCTPDBG_ADDR(level, addr)					\
15817205eccSRandall Stewart {									\
15917205eccSRandall Stewart 	do {								\
160b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {		\
16117205eccSRandall Stewart 			sctp_print_address(addr);			\
16217205eccSRandall Stewart 		}							\
16317205eccSRandall Stewart 	} while (0);							\
16417205eccSRandall Stewart }
16517205eccSRandall Stewart #else
16617205eccSRandall Stewart #define SCTPDBG(level, params...)
16717205eccSRandall Stewart #define SCTPDBG_ADDR(level, addr)
16817205eccSRandall Stewart #endif
16917205eccSRandall Stewart 
17080fefe0aSRandall Stewart #ifdef SCTP_LTRACE_CHUNKS
171dfb11ef8SRandall 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)
17280fefe0aSRandall Stewart #else
17380fefe0aSRandall Stewart #define SCTP_LTRACE_CHK(a, b, c, d)
17480fefe0aSRandall Stewart #endif
17580fefe0aSRandall Stewart 
17680fefe0aSRandall Stewart #ifdef SCTP_LTRACE_ERRORS
177cd3fd531SMichael Tuexen #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
178cd3fd531SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
179cd3fd531SMichael Tuexen 		SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
180c4739e2fSRandall Stewart 		            m, inp, stcb, net, file, __LINE__, err);
181cd3fd531SMichael Tuexen #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
182cd3fd531SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
183cd3fd531SMichael Tuexen 		SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
184c4739e2fSRandall Stewart 		            inp, stcb, net, file, __LINE__, err);
18580fefe0aSRandall Stewart #else
186c4739e2fSRandall Stewart #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
187c4739e2fSRandall Stewart #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
18880fefe0aSRandall Stewart #endif
18980fefe0aSRandall Stewart 
190f8829a4aSRandall Stewart /*
19142551e99SRandall Stewart  * Local address and interface list handling
19242551e99SRandall Stewart  */
19342551e99SRandall Stewart #define SCTP_MAX_VRF_ID		0
19442551e99SRandall Stewart #define SCTP_SIZE_OF_VRF_HASH	3
19542551e99SRandall Stewart #define SCTP_IFNAMSIZ		IFNAMSIZ
19642551e99SRandall Stewart #define SCTP_DEFAULT_VRFID	0
19717205eccSRandall Stewart #define SCTP_VRF_ADDR_HASH_SIZE	16
19817205eccSRandall Stewart #define SCTP_VRF_IFN_HASH_SIZE	3
199f4c93d24SRandall Stewart #define	SCTP_INIT_VRF_TABLEID(vrf)
20042551e99SRandall Stewart 
20142551e99SRandall Stewart #define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
202983066f0SAlexander V. Chernikov #define SCTP_ROUTE_IS_REAL_LOOP(ro) ((ro)->ro_nh && (ro)->ro_nh->nh_ifa && (ro)->ro_nh->nh_ifa->ifa_ifp && (ro)->ro_nh->nh_ifa->ifa_ifp->if_type == IFT_LOOP)
20342551e99SRandall Stewart 
20442551e99SRandall Stewart /*
20542551e99SRandall Stewart  * Access to IFN's to help with src-addr-selection
20642551e99SRandall Stewart  */
20742551e99SRandall Stewart /* This could return VOID if the index works but for BSD we provide both. */
208983066f0SAlexander V. Chernikov #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_nh->nh_ifp
209983066f0SAlexander V. Chernikov #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) (ro)->ro_nh->nh_ifp->if_index
210983066f0SAlexander V. Chernikov #define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_nh && (ro)->ro_nh->nh_ifp)
21142551e99SRandall Stewart 
21242551e99SRandall Stewart /*
213f8829a4aSRandall Stewart  * general memory allocation
214f8829a4aSRandall Stewart  */
215f8829a4aSRandall Stewart #define SCTP_MALLOC(var, type, size, name) \
216f8829a4aSRandall Stewart 	do { \
2171ede983cSDag-Erling Smørgrav 		var = (type)malloc(size, name, M_NOWAIT); \
218f8829a4aSRandall Stewart 	} while (0)
219f8829a4aSRandall Stewart 
2201ede983cSDag-Erling Smørgrav #define SCTP_FREE(var, type)	free(var, type)
221f8829a4aSRandall Stewart 
22242551e99SRandall Stewart #define SCTP_PROCESS_STRUCT struct proc *
22342551e99SRandall Stewart 
224f8829a4aSRandall Stewart /*
225f8829a4aSRandall Stewart  * zone allocation functions
226f8829a4aSRandall Stewart  */
227f8829a4aSRandall Stewart #include <vm/uma.h>
228c54a18d2SRandall Stewart 
229f8829a4aSRandall Stewart /* SCTP_ZONE_INIT: initialize the zone */
230f8829a4aSRandall Stewart typedef struct uma_zone *sctp_zone_t;
231f8829a4aSRandall Stewart #define SCTP_ZONE_INIT(zone, name, size, number) { \
232f8829a4aSRandall Stewart 	zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\
233cf458c64SMichael Tuexen 		0); \
234f8829a4aSRandall Stewart 	uma_zone_set_max(zone, number); \
235f8829a4aSRandall Stewart }
236f8829a4aSRandall Stewart 
237c54a18d2SRandall Stewart #define SCTP_ZONE_DESTROY(zone) uma_zdestroy(zone)
238c54a18d2SRandall Stewart 
239f8829a4aSRandall Stewart /* SCTP_ZONE_GET: allocate element from the zone */
240f42a358aSRandall Stewart #define SCTP_ZONE_GET(zone, type) \
241f42a358aSRandall Stewart 	(type *)uma_zalloc(zone, M_NOWAIT);
242f8829a4aSRandall Stewart 
243f8829a4aSRandall Stewart /* SCTP_ZONE_FREE: free element from the zone */
244f8829a4aSRandall Stewart #define SCTP_ZONE_FREE(zone, element) \
245f8829a4aSRandall Stewart 	uma_zfree(zone, element);
246c54a18d2SRandall Stewart 
24744b7479bSRandall Stewart #define SCTP_HASH_INIT(size, hashmark) hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
24844b7479bSRandall Stewart #define SCTP_HASH_FREE(table, hashmark) hashdestroy(table, M_PCB, hashmark)
24944b7479bSRandall Stewart 
25044b7479bSRandall Stewart #define SCTP_M_COPYM	m_copym
251f8829a4aSRandall Stewart 
252f8829a4aSRandall Stewart /*
253139bc87fSRandall Stewart  * timers
254139bc87fSRandall Stewart  */
255139bc87fSRandall Stewart #include <sys/callout.h>
256139bc87fSRandall Stewart typedef struct callout sctp_os_timer_t;
257139bc87fSRandall Stewart 
258139bc87fSRandall Stewart #define SCTP_OS_TIMER_INIT(tmr)	callout_init(tmr, 1)
2598745f898SMichael Tuexen /*
2608745f898SMichael Tuexen  * NOTE: The next two shouldn't be called directly outside of sctp_timer_start()
2618745f898SMichael Tuexen  * and sctp_timer_stop(), since they don't handle incrementing/decrementing
2628745f898SMichael Tuexen  * relevant reference counts.
2638745f898SMichael Tuexen  */
264139bc87fSRandall Stewart #define SCTP_OS_TIMER_START		callout_reset
265139bc87fSRandall Stewart #define SCTP_OS_TIMER_STOP		callout_stop
26644b7479bSRandall Stewart #define SCTP_OS_TIMER_STOP_DRAIN	callout_drain
267139bc87fSRandall Stewart #define SCTP_OS_TIMER_PENDING		callout_pending
268139bc87fSRandall Stewart #define SCTP_OS_TIMER_ACTIVE		callout_active
269139bc87fSRandall Stewart #define SCTP_OS_TIMER_DEACTIVATE	callout_deactivate
270139bc87fSRandall Stewart 
271207304d4SRandall Stewart #define sctp_get_tick_count() (ticks)
272207304d4SRandall Stewart 
273ceaad40aSRandall Stewart #define SCTP_UNUSED __attribute__((unused))
27435918f85SRandall Stewart 
275139bc87fSRandall Stewart /*
276f8829a4aSRandall Stewart  * Functions
277f8829a4aSRandall Stewart  */
278139bc87fSRandall Stewart /* Mbuf manipulation and access macros  */
279139bc87fSRandall Stewart #define SCTP_BUF_LEN(m) (m->m_len)
280139bc87fSRandall Stewart #define SCTP_BUF_NEXT(m) (m->m_next)
281139bc87fSRandall Stewart #define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
282139bc87fSRandall Stewart #define SCTP_BUF_RESV_UF(m, size) m->m_data += size
283139bc87fSRandall Stewart #define SCTP_BUF_AT(m, size) m->m_data + size
284139bc87fSRandall Stewart #define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
285296d0b94SMichael Tuexen #define SCTP_BUF_SIZE M_SIZE
286139bc87fSRandall Stewart #define SCTP_BUF_TYPE(m) (m->m_type)
287139bc87fSRandall Stewart #define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
288139bc87fSRandall Stewart #define SCTP_BUF_PREPEND	M_PREPEND
2895e54f665SRandall Stewart 
290ed6a66caSRobert Watson #define SCTP_ALIGN_TO_END(m, len) M_ALIGN(m, len)
2910696e120SRandall Stewart 
292999f86d6SMichael Tuexen #define SCTP_SNPRINTF(...) snprintf(__VA_ARGS__)
293999f86d6SMichael Tuexen 
2940696e120SRandall Stewart /* We make it so if you have up to 4 threads
295b1ce21c6SRebecca Cran  * writing based on the default size of
2960696e120SRandall Stewart  * the packet log 65 k, that would be
2970696e120SRandall Stewart  * 4 16k packets before we would hit
2980696e120SRandall Stewart  * a problem.
2990696e120SRandall Stewart  */
3000696e120SRandall Stewart #define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
3010696e120SRandall Stewart 
30217205eccSRandall Stewart /*************************/
30317205eccSRandall Stewart /*      MTU              */
30417205eccSRandall Stewart /*************************/
305fd0d53f8SMichael Tuexen #define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index) ((ifn != NULL) ? ((struct ifnet *)ifn)->if_mtu : 0)
306983066f0SAlexander V. Chernikov #define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, nh) ((uint32_t)((nh != NULL) ? nh->nh_mtu : 0))
30717205eccSRandall Stewart 
308139bc87fSRandall Stewart /*************************/
309139bc87fSRandall Stewart /* These are for logging */
310139bc87fSRandall Stewart /*************************/
311139bc87fSRandall Stewart /* return the base ext data pointer */
312139bc87fSRandall Stewart #define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
313139bc87fSRandall Stewart  /* return the refcnt of the data pointer */
314fcc34a23SGleb Smirnoff #define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ext_cnt)
315139bc87fSRandall Stewart /* return any buffer related flags, this is
316139bc87fSRandall Stewart  * used beyond logging for apple only.
317139bc87fSRandall Stewart  */
318139bc87fSRandall Stewart #define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
319139bc87fSRandall Stewart 
320139bc87fSRandall Stewart /* For BSD this just accesses the M_PKTHDR length
321139bc87fSRandall Stewart  * so it operates on an mbuf with hdr flag. Other
322b1ce21c6SRebecca Cran  * O/S's may have separate packet header and mbuf
323139bc87fSRandall Stewart  * chain pointers.. thus the macro.
324139bc87fSRandall Stewart  */
325139bc87fSRandall Stewart #define SCTP_HEADER_TO_CHAIN(m) (m)
32617205eccSRandall Stewart #define SCTP_DETACH_HEADER_FROM_CHAIN(m)
327f9384252SMichael Tuexen #define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
32817205eccSRandall Stewart #define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
32917205eccSRandall Stewart #define SCTP_RELEASE_HEADER(m)
33017205eccSRandall Stewart #define SCTP_RELEASE_PKT(m)	sctp_m_freem(m)
331b3f1ea41SRandall Stewart #define SCTP_ENABLE_UDP_CSUM(m) do { \
332b3f1ea41SRandall Stewart 					m->m_pkthdr.csum_flags = CSUM_UDP; \
333b3f1ea41SRandall Stewart 					m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \
334b3f1ea41SRandall Stewart 				} while (0)
33517205eccSRandall Stewart 
33672fb6fdbSRandall Stewart #define SCTP_GET_PKT_VRFID(m, vrf_id)  ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
33772fb6fdbSRandall Stewart 
338139bc87fSRandall Stewart /* Attach the chain of data into the sendable packet. */
339139bc87fSRandall Stewart #define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
34017205eccSRandall Stewart                                                  pak = m; \
341139bc87fSRandall Stewart                                                  pak->m_pkthdr.len = packet_length; \
342139bc87fSRandall Stewart                          } while(0)
343139bc87fSRandall Stewart 
344139bc87fSRandall Stewart /* Other m_pkthdr type things */
345*197fc4caSGleb Smirnoff #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_ifnet_broadcast(dst, m->m_pkthdr.rcvif) : 0)
346207304d4SRandall 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)))
347139bc87fSRandall Stewart 
348139bc87fSRandall Stewart /* This converts any input packet header
349139bc87fSRandall Stewart  * into the chain of data holders, for BSD
350139bc87fSRandall Stewart  * its a NOP.
351139bc87fSRandall Stewart  */
352139bc87fSRandall Stewart 
35317205eccSRandall Stewart /* get the v6 hop limit */
354983066f0SAlexander V. Chernikov #define SCTP_GET_HLIM(inp, ro)	in6_selecthlim(&inp->ip_inp.inp, (ro ? (ro->ro_nh ? (ro->ro_nh->nh_ifp) : (NULL)) : (NULL)));
35517205eccSRandall Stewart 
35644b7479bSRandall Stewart /* is the endpoint v6only? */
35794962f6bSMichael Tuexen #define SCTP_IPV6_V6ONLY(sctp_inpcb)	((sctp_inpcb)->ip_inp.inp.inp_flags & IN6P_IPV6_V6ONLY)
35842551e99SRandall Stewart /* is the socket non-blocking? */
35942551e99SRandall Stewart #define SCTP_SO_IS_NBIO(so)	((so)->so_state & SS_NBIO)
36042551e99SRandall Stewart #define SCTP_SET_SO_NBIO(so)	((so)->so_state |= SS_NBIO)
36142551e99SRandall Stewart #define SCTP_CLEAR_SO_NBIO(so)	((so)->so_state &= ~SS_NBIO)
36242551e99SRandall Stewart /* get the socket type */
36342551e99SRandall Stewart #define SCTP_SO_TYPE(so)	((so)->so_type)
364132dea7dSRandall Stewart /* reserve sb space for a socket */
365132dea7dSRandall Stewart #define SCTP_SORESERVE(so, send, recv)	soreserve(so, send, recv)
36617205eccSRandall Stewart /* wakeup a socket */
36717205eccSRandall Stewart #define SCTP_SOWAKEUP(so)	wakeup(&(so)->so_timeo)
368edc5b6eaSMichael Tuexen /* number of bytes ready to read */
369edc5b6eaSMichael Tuexen #define SCTP_SBAVAIL(sb)	sbavail(sb)
370132dea7dSRandall Stewart /* clear the socket buffer state */
371c6207881SMichael Tuexen #define SCTP_SB_INCR(sb, incr)			\
372c6207881SMichael Tuexen {						\
373c6207881SMichael Tuexen 	atomic_add_int(&(sb)->sb_acc, incr);	\
374c6207881SMichael Tuexen 	atomic_add_int(&(sb)->sb_ccc, incr);	\
375c6207881SMichael Tuexen }
376c6207881SMichael Tuexen #define SCTP_SB_DECR(sb, decr)					\
377c6207881SMichael Tuexen {								\
378c6207881SMichael Tuexen 	SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_acc, decr);	\
379c6207881SMichael Tuexen 	SCTP_SAVE_ATOMIC_DECREMENT(&(sb)->sb_ccc, decr);	\
380c6207881SMichael Tuexen }
381132dea7dSRandall Stewart #define SCTP_SB_CLEAR(sb)	\
382c6207881SMichael Tuexen 	(sb).sb_acc = 0;	\
383c6207881SMichael Tuexen 	(sb).sb_ccc = 0;	\
384132dea7dSRandall Stewart 	(sb).sb_mb = NULL;	\
385132dea7dSRandall Stewart 	(sb).sb_mbcnt = 0;
38644b7479bSRandall Stewart 
3879dd6ca96SMichael Tuexen #define SCTP_SB_LIMIT_RCV(so) (SOLISTENING(so) ? so->sol_sbrcv_hiwat : so->so_rcv.sb_hiwat)
3889dd6ca96SMichael Tuexen #define SCTP_SB_LIMIT_SND(so) (SOLISTENING(so) ? so->sol_sbsnd_hiwat : so->so_snd.sb_hiwat)
38962c1ff9cSRandall Stewart 
390f42a358aSRandall Stewart /*
391bff64a4dSRandall Stewart  * routes, output, etc.
392bff64a4dSRandall Stewart  */
393bff64a4dSRandall Stewart typedef struct route sctp_route_t;
394bff64a4dSRandall Stewart 
395d089f9b9SMichael Tuexen #define SCTP_RTALLOC(ro, vrf_id, fibnum) \
396983066f0SAlexander V. Chernikov { \
397983066f0SAlexander V. Chernikov 	if ((ro)->ro_nh == NULL) { \
3981d1a743eSAlexander V. Chernikov 		(ro)->ro_nh = rib_lookup(fibnum, &(ro)->ro_dst, NHR_REF, 0); \
399983066f0SAlexander V. Chernikov 	} \
400983066f0SAlexander V. Chernikov }
40117205eccSRandall Stewart 
40217205eccSRandall Stewart /*
403678d7b94SAndre Oppermann  * SCTP protocol specific mbuf flags.
404678d7b94SAndre Oppermann  */
4055fc98a78SAndre Oppermann #define	M_NOTIFICATION		M_PROTO1	/* SCTP notification */
406678d7b94SAndre Oppermann 
407678d7b94SAndre Oppermann /*
40817205eccSRandall Stewart  * IP output routines
40917205eccSRandall Stewart  */
410f32357beSMichael Tuexen #define SCTP_IP_OUTPUT(result, o_pak, ro, _inp, vrf_id)                      \
41117205eccSRandall Stewart {                                                                            \
412f32357beSMichael Tuexen 	struct sctp_inpcb *local_inp = _inp;                                 \
4139c7635e1SMichael Tuexen 	int o_flgs = IP_RAWOUTPUT;                                           \
414f32357beSMichael Tuexen 	                                                                     \
41586bd0491SAndre Oppermann 	m_clrprotoflags(o_pak);                                              \
416f32357beSMichael Tuexen 	if ((local_inp != NULL) && (local_inp->sctp_socket != NULL)) {       \
417f32357beSMichael Tuexen 		o_flgs |= local_inp->sctp_socket->so_options & SO_DONTROUTE; \
418f32357beSMichael Tuexen 	}                                                                    \
41917205eccSRandall Stewart 	result = ip_output(o_pak, NULL, ro, o_flgs, 0, NULL);                \
42017205eccSRandall Stewart }
42117205eccSRandall Stewart 
422f32357beSMichael Tuexen #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, _inp, vrf_id)                \
42317205eccSRandall Stewart {                                                                            \
424f32357beSMichael Tuexen 	struct sctp_inpcb *local_inp = _inp;                                 \
425f32357beSMichael Tuexen 	                                                                     \
42686bd0491SAndre Oppermann 	m_clrprotoflags(o_pak);                                              \
427f32357beSMichael Tuexen 	if (local_inp != NULL) {                                             \
428a12d8933SMichael Tuexen 		INP_RLOCK(&local_inp->ip_inp.inp);                           \
42917205eccSRandall Stewart 		result = ip6_output(o_pak,                                   \
430f32357beSMichael Tuexen 		                    local_inp->ip_inp.inp.in6p_outputopts,   \
43117205eccSRandall Stewart 		                    (ro), 0, 0, ifp, NULL);                  \
432a12d8933SMichael Tuexen 		INP_RUNLOCK(&local_inp->ip_inp.inp);                         \
433f32357beSMichael Tuexen 	} else {                                                             \
43417205eccSRandall Stewart 		result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL);     \
435f32357beSMichael Tuexen 	}                                                                    \
43617205eccSRandall Stewart }
437bff64a4dSRandall Stewart 
438d06c82f1SRandall Stewart struct mbuf *
439d06c82f1SRandall Stewart sctp_get_mbuf_for_msg(unsigned int space_needed,
440d06c82f1SRandall Stewart     int want_header, int how, int allonebuf, int type);
441d06c82f1SRandall Stewart 
442bff64a4dSRandall Stewart /*
443f42a358aSRandall Stewart  * SCTP AUTH
444f42a358aSRandall Stewart  */
44590ab3571SMichael Tuexen #define SCTP_READ_RANDOM(buf, len)	arc4rand(buf, len, 0)
446f42a358aSRandall Stewart 
447f42a358aSRandall Stewart /* map standard crypto API names */
448e6b2b4b6SMichael Tuexen #define SCTP_SHA1_CTX		SHA1_CTX
449e6b2b4b6SMichael Tuexen #define SCTP_SHA1_INIT		SHA1Init
450e6b2b4b6SMichael Tuexen #define SCTP_SHA1_UPDATE	SHA1Update
451e6b2b4b6SMichael Tuexen #define SCTP_SHA1_FINAL(x,y)	SHA1Final((caddr_t)x, y)
452f42a358aSRandall Stewart 
453e6b2b4b6SMichael Tuexen #define SCTP_SHA256_CTX		SHA256_CTX
454e6b2b4b6SMichael Tuexen #define SCTP_SHA256_INIT	SHA256_Init
455e6b2b4b6SMichael Tuexen #define SCTP_SHA256_UPDATE	SHA256_Update
456e6b2b4b6SMichael Tuexen #define SCTP_SHA256_FINAL(x,y)	SHA256_Final((caddr_t)x, y)
457f42a358aSRandall Stewart 
458830d754dSRandall Stewart #define SCTP_DECREMENT_AND_CHECK_REFCOUNT(addr) (atomic_fetchadd_int(addr, -1) == 1)
459830d754dSRandall Stewart #if defined(INVARIANTS)
460830d754dSRandall Stewart #define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
461830d754dSRandall Stewart { \
462830d754dSRandall Stewart 	int32_t oldval; \
463830d754dSRandall Stewart 	oldval = atomic_fetchadd_int(addr, -val); \
464830d754dSRandall Stewart 	if (oldval < val) { \
4659d18771fSRandall Stewart 		panic("Counter goes negative"); \
466830d754dSRandall Stewart 	} \
467830d754dSRandall Stewart }
468830d754dSRandall Stewart #else
469830d754dSRandall Stewart #define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
470830d754dSRandall Stewart { \
471830d754dSRandall Stewart 	int32_t oldval; \
472830d754dSRandall Stewart 	oldval = atomic_fetchadd_int(addr, -val); \
473830d754dSRandall Stewart 	if (oldval < val) { \
474830d754dSRandall Stewart 		*addr = 0; \
475830d754dSRandall Stewart 	} \
476830d754dSRandall Stewart }
477830d754dSRandall Stewart #endif
4785d08768aSMichael Tuexen 
4795d08768aSMichael Tuexen #define SCTP_IS_LISTENING(inp) ((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) != 0)
4805d08768aSMichael Tuexen 
481052c5ec4SMark Johnston int sctp_syscalls_init(void);
482052c5ec4SMark Johnston int sctp_syscalls_uninit(void);
483052c5ec4SMark Johnston 
4845d08768aSMichael Tuexen #endif
485