xref: /freebsd/sys/netinet/sctp_os_bsd.h (revision cd3fd531884a0d997f2e2c18ea1df887284e8c02)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved.
35d40cf5dSRandall Stewart  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
45d40cf5dSRandall Stewart  * Copyright (c) 2008-2011, 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  */
3273932c69SRandall Stewart #include <sys/cdefs.h>
33f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
34f8829a4aSRandall Stewart #ifndef __sctp_os_bsd_h__
35f8829a4aSRandall Stewart #define __sctp_os_bsd_h__
36f8829a4aSRandall Stewart /*
37f8829a4aSRandall Stewart  * includes
38f8829a4aSRandall Stewart  */
3993164cf9SRandall Stewart #include "opt_ipsec.h"
4093164cf9SRandall Stewart #include "opt_compat.h"
4193164cf9SRandall Stewart #include "opt_inet6.h"
4293164cf9SRandall Stewart #include "opt_inet.h"
4393164cf9SRandall Stewart #include "opt_sctp.h"
44603724d3SBjoern A. Zeeb 
4593164cf9SRandall Stewart #include <sys/param.h>
4680fefe0aSRandall Stewart #include <sys/ktr.h>
4793164cf9SRandall Stewart #include <sys/systm.h>
4893164cf9SRandall Stewart #include <sys/malloc.h>
4993164cf9SRandall Stewart #include <sys/kernel.h>
5093164cf9SRandall Stewart #include <sys/sysctl.h>
5193164cf9SRandall Stewart #include <sys/mbuf.h>
5293164cf9SRandall Stewart #include <sys/protosw.h>
5393164cf9SRandall Stewart #include <sys/socket.h>
5493164cf9SRandall Stewart #include <sys/socketvar.h>
5518e198d3SRandall Stewart #include <sys/jail.h>
5693164cf9SRandall Stewart #include <sys/sysctl.h>
5793164cf9SRandall Stewart #include <sys/resourcevar.h>
5893164cf9SRandall Stewart #include <sys/uio.h>
59c99efcf6SRandall Stewart #include <sys/lock.h>
60c99efcf6SRandall Stewart #include <sys/rwlock.h>
6142551e99SRandall Stewart #include <sys/kthread.h>
6293164cf9SRandall Stewart #include <sys/priv.h>
63f8829a4aSRandall Stewart #include <sys/random.h>
6493164cf9SRandall Stewart #include <sys/limits.h>
6593164cf9SRandall Stewart #include <sys/queue.h>
6693164cf9SRandall Stewart #include <machine/cpu.h>
6793164cf9SRandall Stewart 
6893164cf9SRandall Stewart #include <net/if.h>
6993164cf9SRandall Stewart #include <net/if_types.h>
7093164cf9SRandall Stewart #include <net/if_var.h>
7193164cf9SRandall Stewart #include <net/route.h>
7245623593SMichael Tuexen #include <net/vnet.h>
7393164cf9SRandall Stewart 
7493164cf9SRandall Stewart #include <netinet/in.h>
7593164cf9SRandall Stewart #include <netinet/in_systm.h>
7693164cf9SRandall Stewart #include <netinet/ip.h>
7793164cf9SRandall Stewart #include <netinet/in_pcb.h>
7893164cf9SRandall Stewart #include <netinet/in_var.h>
7993164cf9SRandall Stewart #include <netinet/ip_var.h>
8093164cf9SRandall Stewart #include <netinet/ip_icmp.h>
8193164cf9SRandall Stewart #include <netinet/icmp_var.h>
8293164cf9SRandall Stewart 
83b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
842cb64cb2SGeorge V. Neville-Neil #include <netipsec/ipsec.h>
852cb64cb2SGeorge V. Neville-Neil #include <netipsec/key.h>
8693164cf9SRandall Stewart #endif				/* IPSEC */
8793164cf9SRandall Stewart 
8893164cf9SRandall Stewart #ifdef INET6
8993164cf9SRandall Stewart #include <sys/domain.h>
90b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
910d29af67SGeorge V. Neville-Neil #include <netipsec/ipsec6.h>
9293164cf9SRandall Stewart #endif
9393164cf9SRandall Stewart #include <netinet/ip6.h>
9493164cf9SRandall Stewart #include <netinet6/ip6_var.h>
9593164cf9SRandall Stewart #include <netinet6/in6_pcb.h>
9693164cf9SRandall Stewart #include <netinet/icmp6.h>
9793164cf9SRandall Stewart #include <netinet6/ip6protosw.h>
9893164cf9SRandall Stewart #include <netinet6/nd6.h>
9993164cf9SRandall Stewart #include <netinet6/scope6_var.h>
10093164cf9SRandall Stewart #endif				/* INET6 */
10193164cf9SRandall Stewart 
102f42a358aSRandall Stewart 
10393164cf9SRandall Stewart #include <netinet/ip_options.h>
10493164cf9SRandall Stewart 
10593164cf9SRandall Stewart #ifndef in6pcb
10693164cf9SRandall Stewart #define in6pcb		inpcb
10793164cf9SRandall Stewart #endif
108207304d4SRandall Stewart /* Declare all the malloc names for all the various mallocs */
109207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_MAP);
110207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRMI);
111207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRMO);
112207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ASC_ADDR);
113207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ASC_IT);
114207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_CL);
115207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_KY);
116207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_HL);
117207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_AUTH_IF);
118207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_STRESET);
119207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_CMSG);
120207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_COPYAL);
121207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_VRF);
122207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_IFA);
123207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_IFN);
124207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_TIMW);
125207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_MVRF);
126207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_ITER);
127207304d4SRandall Stewart MALLOC_DECLARE(SCTP_M_SOCKOPT);
128bfc46083SRandall Stewart MALLOC_DECLARE(SCTP_M_MCORE);
129f8829a4aSRandall Stewart 
130c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
131c4739e2fSRandall Stewart 
132c4739e2fSRandall Stewart #define SCTP_GET_CYCLECOUNT get_cyclecount()
133c4739e2fSRandall Stewart #define SCTP_CTR6 sctp_log_trace
134c4739e2fSRandall Stewart 
135c4739e2fSRandall Stewart #else
136c4739e2fSRandall Stewart #define SCTP_CTR6 CTR6
137c4739e2fSRandall Stewart #endif
138c4739e2fSRandall Stewart 
139f8829a4aSRandall Stewart /*
140fc14de76SRandall Stewart  * Macros to expand out globals defined by various modules
141fc14de76SRandall Stewart  * to either a real global or a virtualized instance of one,
142603724d3SBjoern A. Zeeb  * depending on whether VIMAGE is defined.
143fc14de76SRandall Stewart  */
144fc14de76SRandall Stewart /* then define the macro(s) that hook into the vimage macros */
145482444b4SRandall Stewart #define MODULE_GLOBAL(__SYMBOL) V_##__SYMBOL
146482444b4SRandall Stewart 
1478518270eSMichael Tuexen #define V_system_base_info VNET(system_base_info)
148482444b4SRandall Stewart #define SCTP_BASE_INFO(__m) V_system_base_info.sctppcbinfo.__m
149482444b4SRandall Stewart #define SCTP_BASE_STATS V_system_base_info.sctpstat
1508518270eSMichael Tuexen #define SCTP_BASE_STATS_SYSCTL VNET_NAME(system_base_info.sctpstat)
151482444b4SRandall Stewart #define SCTP_BASE_STAT(__m)     V_system_base_info.sctpstat.__m
1528518270eSMichael Tuexen #define SCTP_BASE_SYSCTL(__m) VNET_NAME(system_base_info.sctpsysctl.__m)
153482444b4SRandall Stewart #define SCTP_BASE_VAR(__m) V_system_base_info.__m
154a99b6783SRandall Stewart 
155fc14de76SRandall Stewart /*
156f8829a4aSRandall Stewart  *
157f8829a4aSRandall Stewart  */
158a5d547adSRandall Stewart #define USER_ADDR_NULL	(NULL)	/* FIX ME: temp */
159a5d547adSRandall Stewart 
160*cd3fd531SMichael Tuexen #define SCTP_PRINTF(params...)	printf(params)
16117205eccSRandall Stewart #if defined(SCTP_DEBUG)
16217205eccSRandall Stewart #define SCTPDBG(level, params...)					\
16317205eccSRandall Stewart {									\
16417205eccSRandall Stewart     do {								\
165b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {			\
166*cd3fd531SMichael Tuexen 	    SCTP_PRINTF(params);						\
16717205eccSRandall Stewart 	}								\
16817205eccSRandall Stewart     } while (0);							\
16917205eccSRandall Stewart }
17017205eccSRandall Stewart #define SCTPDBG_ADDR(level, addr)					\
17117205eccSRandall Stewart {									\
17217205eccSRandall Stewart     do {								\
173b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {			\
17417205eccSRandall Stewart 	    sctp_print_address(addr);					\
17517205eccSRandall Stewart 	}								\
17617205eccSRandall Stewart     } while (0);							\
17717205eccSRandall Stewart }
178ad81507eSRandall Stewart #define SCTPDBG_PKT(level, iph, sh)					\
179ad81507eSRandall Stewart {									\
180ad81507eSRandall Stewart     do {								\
181b3f1ea41SRandall Stewart 	    if (SCTP_BASE_SYSCTL(sctp_debug_on) & level) {		\
182ad81507eSRandall Stewart 		    sctp_print_address_pkt(iph, sh);			\
183ad81507eSRandall Stewart 	    }								\
184ad81507eSRandall Stewart     } while (0);							\
185ad81507eSRandall Stewart }
18617205eccSRandall Stewart #else
18717205eccSRandall Stewart #define SCTPDBG(level, params...)
18817205eccSRandall Stewart #define SCTPDBG_ADDR(level, addr)
189ad81507eSRandall Stewart #define SCTPDBG_PKT(level, iph, sh)
19017205eccSRandall Stewart #endif
19117205eccSRandall Stewart 
19280fefe0aSRandall Stewart #ifdef SCTP_LTRACE_CHUNKS
193dfb11ef8SRandall 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)
19480fefe0aSRandall Stewart #else
19580fefe0aSRandall Stewart #define SCTP_LTRACE_CHK(a, b, c, d)
19680fefe0aSRandall Stewart #endif
19780fefe0aSRandall Stewart 
19880fefe0aSRandall Stewart #ifdef SCTP_LTRACE_ERRORS
199*cd3fd531SMichael Tuexen #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
200*cd3fd531SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
201*cd3fd531SMichael Tuexen         	SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
202c4739e2fSRandall Stewart 		            m, inp, stcb, net, file, __LINE__, err);
203*cd3fd531SMichael Tuexen #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
204*cd3fd531SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
205*cd3fd531SMichael Tuexen         	SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
206c4739e2fSRandall Stewart 		            inp, stcb, net, file, __LINE__, err);
20780fefe0aSRandall Stewart #else
208c4739e2fSRandall Stewart #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
209c4739e2fSRandall Stewart #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
21080fefe0aSRandall Stewart #endif
21180fefe0aSRandall Stewart 
212c4739e2fSRandall Stewart 
213f8829a4aSRandall Stewart /*
21442551e99SRandall Stewart  * Local address and interface list handling
21542551e99SRandall Stewart  */
21642551e99SRandall Stewart #define SCTP_MAX_VRF_ID		0
21742551e99SRandall Stewart #define SCTP_SIZE_OF_VRF_HASH	3
21842551e99SRandall Stewart #define SCTP_IFNAMSIZ		IFNAMSIZ
21942551e99SRandall Stewart #define SCTP_DEFAULT_VRFID	0
22017205eccSRandall Stewart #define SCTP_VRF_ADDR_HASH_SIZE	16
22117205eccSRandall Stewart #define SCTP_VRF_IFN_HASH_SIZE	3
222f4c93d24SRandall Stewart #define	SCTP_INIT_VRF_TABLEID(vrf)
22342551e99SRandall Stewart 
22442551e99SRandall Stewart #define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
225c54a18d2SRandall 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)
22642551e99SRandall Stewart 
22742551e99SRandall Stewart /*
22842551e99SRandall Stewart  * Access to IFN's to help with src-addr-selection
22942551e99SRandall Stewart  */
23042551e99SRandall Stewart /* This could return VOID if the index works but for BSD we provide both. */
23142551e99SRandall Stewart #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
23217205eccSRandall Stewart #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) (ro)->ro_rt->rt_ifp->if_index
23317205eccSRandall Stewart #define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
23442551e99SRandall Stewart 
23542551e99SRandall Stewart /*
236f8829a4aSRandall Stewart  * general memory allocation
237f8829a4aSRandall Stewart  */
238f8829a4aSRandall Stewart #define SCTP_MALLOC(var, type, size, name) \
239f8829a4aSRandall Stewart     do { \
2401ede983cSDag-Erling Smørgrav 	var = (type)malloc(size, name, M_NOWAIT); \
241f8829a4aSRandall Stewart     } while (0)
242f8829a4aSRandall Stewart 
2431ede983cSDag-Erling Smørgrav #define SCTP_FREE(var, type)	free(var, type)
244f8829a4aSRandall Stewart 
245f8829a4aSRandall Stewart #define SCTP_MALLOC_SONAME(var, type, size) \
246f8829a4aSRandall Stewart     do { \
2471ede983cSDag-Erling Smørgrav 	var = (type)malloc(size, M_SONAME, M_WAITOK | M_ZERO); \
248f8829a4aSRandall Stewart     } while (0)
249f8829a4aSRandall Stewart 
2501ede983cSDag-Erling Smørgrav #define SCTP_FREE_SONAME(var)	free(var, M_SONAME)
251f8829a4aSRandall Stewart 
25242551e99SRandall Stewart #define SCTP_PROCESS_STRUCT struct proc *
25342551e99SRandall Stewart 
254f8829a4aSRandall Stewart /*
255f8829a4aSRandall Stewart  * zone allocation functions
256f8829a4aSRandall Stewart  */
257f8829a4aSRandall Stewart #include <vm/uma.h>
258c54a18d2SRandall Stewart 
259f8829a4aSRandall Stewart /* SCTP_ZONE_INIT: initialize the zone */
260f8829a4aSRandall Stewart typedef struct uma_zone *sctp_zone_t;
261f8829a4aSRandall Stewart 
262f8829a4aSRandall Stewart #define SCTP_ZONE_INIT(zone, name, size, number) { \
263f8829a4aSRandall Stewart 	zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\
264cf458c64SMichael Tuexen 		0); \
265f8829a4aSRandall Stewart 	uma_zone_set_max(zone, number); \
266f8829a4aSRandall Stewart }
267f8829a4aSRandall Stewart 
268c54a18d2SRandall Stewart #define SCTP_ZONE_DESTROY(zone) uma_zdestroy(zone)
269c54a18d2SRandall Stewart 
270f8829a4aSRandall Stewart /* SCTP_ZONE_GET: allocate element from the zone */
271f42a358aSRandall Stewart #define SCTP_ZONE_GET(zone, type) \
272f42a358aSRandall Stewart 	(type *)uma_zalloc(zone, M_NOWAIT);
273f8829a4aSRandall Stewart 
274f8829a4aSRandall Stewart /* SCTP_ZONE_FREE: free element from the zone */
275f8829a4aSRandall Stewart #define SCTP_ZONE_FREE(zone, element) \
276f8829a4aSRandall Stewart 	uma_zfree(zone, element);
277c54a18d2SRandall Stewart 
27844b7479bSRandall Stewart #define SCTP_HASH_INIT(size, hashmark) hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
27944b7479bSRandall Stewart #define SCTP_HASH_FREE(table, hashmark) hashdestroy(table, M_PCB, hashmark)
28044b7479bSRandall Stewart 
28144b7479bSRandall Stewart #define SCTP_M_COPYM	m_copym
282f8829a4aSRandall Stewart 
283f8829a4aSRandall Stewart /*
284139bc87fSRandall Stewart  * timers
285139bc87fSRandall Stewart  */
286139bc87fSRandall Stewart #include <sys/callout.h>
287139bc87fSRandall Stewart typedef struct callout sctp_os_timer_t;
288139bc87fSRandall Stewart 
289c54a18d2SRandall Stewart 
290139bc87fSRandall Stewart #define SCTP_OS_TIMER_INIT(tmr)	callout_init(tmr, 1)
291139bc87fSRandall Stewart #define SCTP_OS_TIMER_START	callout_reset
292139bc87fSRandall Stewart #define SCTP_OS_TIMER_STOP	callout_stop
29344b7479bSRandall Stewart #define SCTP_OS_TIMER_STOP_DRAIN callout_drain
294139bc87fSRandall Stewart #define SCTP_OS_TIMER_PENDING	callout_pending
295139bc87fSRandall Stewart #define SCTP_OS_TIMER_ACTIVE	callout_active
296139bc87fSRandall Stewart #define SCTP_OS_TIMER_DEACTIVATE callout_deactivate
297139bc87fSRandall Stewart 
298207304d4SRandall Stewart #define sctp_get_tick_count() (ticks)
299207304d4SRandall Stewart 
300ceaad40aSRandall Stewart #define SCTP_UNUSED __attribute__((unused))
30135918f85SRandall Stewart 
302139bc87fSRandall Stewart /*
303f8829a4aSRandall Stewart  * Functions
304f8829a4aSRandall Stewart  */
305139bc87fSRandall Stewart /* Mbuf manipulation and access macros  */
306139bc87fSRandall Stewart #define SCTP_BUF_LEN(m) (m->m_len)
307139bc87fSRandall Stewart #define SCTP_BUF_NEXT(m) (m->m_next)
308139bc87fSRandall Stewart #define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
309139bc87fSRandall Stewart #define SCTP_BUF_RESV_UF(m, size) m->m_data += size
310139bc87fSRandall Stewart #define SCTP_BUF_AT(m, size) m->m_data + size
311139bc87fSRandall Stewart #define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
312139bc87fSRandall Stewart #define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
313139bc87fSRandall Stewart #define SCTP_BUF_TYPE(m) (m->m_type)
314139bc87fSRandall Stewart #define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
315139bc87fSRandall Stewart #define SCTP_BUF_PREPEND	M_PREPEND
3165e54f665SRandall Stewart 
3175e54f665SRandall Stewart #define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
3185e54f665SRandall Stewart                                      MH_ALIGN(m, len); \
3195e54f665SRandall Stewart                                   } else if ((m->m_flags & M_EXT) == 0) { \
3205e54f665SRandall Stewart                                      M_ALIGN(m, len); \
3215e54f665SRandall Stewart                                   }
3220696e120SRandall Stewart 
3230696e120SRandall Stewart /* We make it so if you have up to 4 threads
324b1ce21c6SRebecca Cran  * writing based on the default size of
3250696e120SRandall Stewart  * the packet log 65 k, that would be
3260696e120SRandall Stewart  * 4 16k packets before we would hit
3270696e120SRandall Stewart  * a problem.
3280696e120SRandall Stewart  */
3290696e120SRandall Stewart #define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
3300696e120SRandall Stewart 
33117205eccSRandall Stewart /*************************/
33217205eccSRandall Stewart /*      MTU              */
33317205eccSRandall Stewart /*************************/
334ad21a364SRandall Stewart #define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) ((struct ifnet *)ifn)->if_mtu
33517205eccSRandall Stewart #define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0)
33617205eccSRandall Stewart #define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) ((sctp_ifn->ifn_p != NULL) ? ((struct ifnet *)(sctp_ifn->ifn_p))->if_mtu : 0)
33717205eccSRandall Stewart #define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
33817205eccSRandall Stewart                                               if (rt != NULL) \
33917205eccSRandall Stewart                                                  rt->rt_rmx.rmx_mtu = mtu; \
34017205eccSRandall Stewart                                            } while(0)
34117205eccSRandall Stewart 
34217205eccSRandall Stewart /* (de-)register interface event notifications */
343ad21a364SRandall Stewart #define SCTP_REGISTER_INTERFACE(ifhandle, af)
344ad21a364SRandall Stewart #define SCTP_DEREGISTER_INTERFACE(ifhandle, af)
3455e54f665SRandall Stewart 
34618e198d3SRandall Stewart 
347139bc87fSRandall Stewart /*************************/
348139bc87fSRandall Stewart /* These are for logging */
349139bc87fSRandall Stewart /*************************/
350139bc87fSRandall Stewart /* return the base ext data pointer */
351139bc87fSRandall Stewart #define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
352139bc87fSRandall Stewart  /* return the refcnt of the data pointer */
353139bc87fSRandall Stewart #define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
354139bc87fSRandall Stewart /* return any buffer related flags, this is
355139bc87fSRandall Stewart  * used beyond logging for apple only.
356139bc87fSRandall Stewart  */
357139bc87fSRandall Stewart #define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
358139bc87fSRandall Stewart 
359139bc87fSRandall Stewart /* For BSD this just accesses the M_PKTHDR length
360139bc87fSRandall Stewart  * so it operates on an mbuf with hdr flag. Other
361b1ce21c6SRebecca Cran  * O/S's may have separate packet header and mbuf
362139bc87fSRandall Stewart  * chain pointers.. thus the macro.
363139bc87fSRandall Stewart  */
364139bc87fSRandall Stewart #define SCTP_HEADER_TO_CHAIN(m) (m)
36517205eccSRandall Stewart #define SCTP_DETACH_HEADER_FROM_CHAIN(m)
366139bc87fSRandall Stewart #define SCTP_HEADER_LEN(m) (m->m_pkthdr.len)
36717205eccSRandall Stewart #define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
36817205eccSRandall Stewart #define SCTP_RELEASE_HEADER(m)
36917205eccSRandall Stewart #define SCTP_RELEASE_PKT(m)	sctp_m_freem(m)
370b3f1ea41SRandall Stewart #define SCTP_ENABLE_UDP_CSUM(m) do { \
371b3f1ea41SRandall Stewart 					m->m_pkthdr.csum_flags = CSUM_UDP; \
372b3f1ea41SRandall Stewart 					m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \
373b3f1ea41SRandall Stewart 				} while (0)
37417205eccSRandall Stewart 
37572fb6fdbSRandall Stewart #define SCTP_GET_PKT_VRFID(m, vrf_id)  ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
37672fb6fdbSRandall Stewart 
37772fb6fdbSRandall Stewart 
378139bc87fSRandall Stewart 
379139bc87fSRandall Stewart /* Attach the chain of data into the sendable packet. */
380139bc87fSRandall Stewart #define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
38117205eccSRandall Stewart                                                  pak = m; \
382139bc87fSRandall Stewart                                                  pak->m_pkthdr.len = packet_length; \
383139bc87fSRandall Stewart                          } while(0)
384139bc87fSRandall Stewart 
385139bc87fSRandall Stewart /* Other m_pkthdr type things */
386207304d4SRandall Stewart #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0)
387207304d4SRandall 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)))
388139bc87fSRandall Stewart 
389139bc87fSRandall Stewart 
390139bc87fSRandall Stewart /* This converts any input packet header
391139bc87fSRandall Stewart  * into the chain of data holders, for BSD
392139bc87fSRandall Stewart  * its a NOP.
393139bc87fSRandall Stewart  */
394139bc87fSRandall Stewart 
395139bc87fSRandall Stewart /* Macro's for getting length from V6/V4 header */
396139bc87fSRandall Stewart #define SCTP_GET_IPV4_LENGTH(iph) (iph->ip_len)
397139bc87fSRandall Stewart #define SCTP_GET_IPV6_LENGTH(ip6) (ntohs(ip6->ip6_plen))
398f8829a4aSRandall Stewart 
39917205eccSRandall Stewart /* get the v6 hop limit */
40017205eccSRandall 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)));
40117205eccSRandall Stewart 
40244b7479bSRandall Stewart /* is the endpoint v6only? */
40344b7479bSRandall Stewart #define SCTP_IPV6_V6ONLY(inp)	(((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
40442551e99SRandall Stewart /* is the socket non-blocking? */
40542551e99SRandall Stewart #define SCTP_SO_IS_NBIO(so)	((so)->so_state & SS_NBIO)
40642551e99SRandall Stewart #define SCTP_SET_SO_NBIO(so)	((so)->so_state |= SS_NBIO)
40742551e99SRandall Stewart #define SCTP_CLEAR_SO_NBIO(so)	((so)->so_state &= ~SS_NBIO)
40842551e99SRandall Stewart /* get the socket type */
40942551e99SRandall Stewart #define SCTP_SO_TYPE(so)	((so)->so_type)
410132dea7dSRandall Stewart /* reserve sb space for a socket */
411132dea7dSRandall Stewart #define SCTP_SORESERVE(so, send, recv)	soreserve(so, send, recv)
41217205eccSRandall Stewart /* wakeup a socket */
41317205eccSRandall Stewart #define SCTP_SOWAKEUP(so)	wakeup(&(so)->so_timeo)
414132dea7dSRandall Stewart /* clear the socket buffer state */
415132dea7dSRandall Stewart #define SCTP_SB_CLEAR(sb)	\
416132dea7dSRandall Stewart 	(sb).sb_cc = 0;		\
417132dea7dSRandall Stewart 	(sb).sb_mb = NULL;	\
418132dea7dSRandall Stewart 	(sb).sb_mbcnt = 0;
41944b7479bSRandall Stewart 
42062c1ff9cSRandall Stewart #define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
42162c1ff9cSRandall Stewart #define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
42262c1ff9cSRandall Stewart 
423f42a358aSRandall Stewart /*
424bff64a4dSRandall Stewart  * routes, output, etc.
425bff64a4dSRandall Stewart  */
426bff64a4dSRandall Stewart typedef struct route sctp_route_t;
42717205eccSRandall Stewart typedef struct rtentry sctp_rtentry_t;
428bff64a4dSRandall Stewart 
4299836132cSBjoern A. Zeeb /*
4309836132cSBjoern A. Zeeb  * XXX multi-FIB support was backed out in r179783 and it seems clear that the
4319836132cSBjoern A. Zeeb  * VRF support as currently in FreeBSD is not ready to support multi-FIB.
4329836132cSBjoern A. Zeeb  * It might be best to implement multi-FIB support for both v4 and v6 indepedent
4339836132cSBjoern A. Zeeb  * of VRFs and leave those to a real MPLS stack.
4349836132cSBjoern A. Zeeb  */
435b3f1ea41SRandall Stewart #define SCTP_RTALLOC(ro, vrf_id) rtalloc_ign((struct route *)ro, 0UL)
43617205eccSRandall Stewart 
43717205eccSRandall Stewart /* Future zero copy wakeup/send  function */
43817205eccSRandall Stewart #define SCTP_ZERO_COPY_EVENT(inp, so)
439ad21a364SRandall Stewart /* This is re-pulse ourselves for sendbuf */
440ad21a364SRandall Stewart #define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so)
44117205eccSRandall Stewart 
44217205eccSRandall Stewart /*
44317205eccSRandall Stewart  * IP output routines
44417205eccSRandall Stewart  */
445ad21a364SRandall Stewart #define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) \
44617205eccSRandall Stewart { \
4479c7635e1SMichael Tuexen 	int o_flgs = IP_RAWOUTPUT; \
4489c7635e1SMichael Tuexen 	struct sctp_tcb *local_stcb = stcb; \
4499c7635e1SMichael Tuexen 	if (local_stcb && \
4509c7635e1SMichael Tuexen 	    local_stcb->sctp_ep && \
4519c7635e1SMichael Tuexen 	    local_stcb->sctp_ep->sctp_socket) \
4529c7635e1SMichael Tuexen 		o_flgs |= local_stcb->sctp_ep->sctp_socket->so_options & SO_DONTROUTE; \
45317205eccSRandall Stewart 	result = ip_output(o_pak, NULL, ro, o_flgs, 0, NULL); \
45417205eccSRandall Stewart }
45517205eccSRandall Stewart 
456ad21a364SRandall Stewart #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \
45717205eccSRandall Stewart { \
4589c7635e1SMichael Tuexen 	struct sctp_tcb *local_stcb = stcb; \
4599c7635e1SMichael Tuexen 	if (local_stcb && local_stcb->sctp_ep) \
46017205eccSRandall Stewart 		result = ip6_output(o_pak, \
4619c7635e1SMichael Tuexen 				    ((struct in6pcb *)(local_stcb->sctp_ep))->in6p_outputopts, \
46217205eccSRandall Stewart 				    (ro), 0, 0, ifp, NULL); \
46317205eccSRandall Stewart 	else \
46417205eccSRandall Stewart 		result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
46517205eccSRandall Stewart }
466bff64a4dSRandall Stewart 
467d06c82f1SRandall Stewart struct mbuf *
468d06c82f1SRandall Stewart sctp_get_mbuf_for_msg(unsigned int space_needed,
469d06c82f1SRandall Stewart     int want_header, int how, int allonebuf, int type);
470d06c82f1SRandall Stewart 
471d06c82f1SRandall Stewart 
472bff64a4dSRandall Stewart /*
473f42a358aSRandall Stewart  * SCTP AUTH
474f42a358aSRandall Stewart  */
475f42a358aSRandall Stewart #define HAVE_SHA2
476f42a358aSRandall Stewart 
477f42a358aSRandall Stewart #define SCTP_READ_RANDOM(buf, len)	read_random(buf, len)
478f42a358aSRandall Stewart 
479f42a358aSRandall Stewart #ifdef USE_SCTP_SHA1
480f42a358aSRandall Stewart #include <netinet/sctp_sha1.h>
481f42a358aSRandall Stewart #else
482f42a358aSRandall Stewart #include <crypto/sha1.h>
483f42a358aSRandall Stewart /* map standard crypto API names */
484f42a358aSRandall Stewart #define SHA1_Init	SHA1Init
485f42a358aSRandall Stewart #define SHA1_Update	SHA1Update
486f42a358aSRandall Stewart #define SHA1_Final(x,y)	SHA1Final((caddr_t)x, y)
487f42a358aSRandall Stewart #endif
488f42a358aSRandall Stewart 
489f42a358aSRandall Stewart #if defined(HAVE_SHA2)
490f42a358aSRandall Stewart #include <crypto/sha2/sha2.h>
491f42a358aSRandall Stewart #endif
492f42a358aSRandall Stewart 
493f8829a4aSRandall Stewart #endif
494830d754dSRandall Stewart 
495830d754dSRandall Stewart #define SCTP_DECREMENT_AND_CHECK_REFCOUNT(addr) (atomic_fetchadd_int(addr, -1) == 1)
496830d754dSRandall Stewart #if defined(INVARIANTS)
497830d754dSRandall Stewart #define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
498830d754dSRandall Stewart { \
499830d754dSRandall Stewart 	int32_t oldval; \
500830d754dSRandall Stewart 	oldval = atomic_fetchadd_int(addr, -val); \
501830d754dSRandall Stewart 	if (oldval < val) { \
502830d754dSRandall Stewart 		panic("Counter goes negative"); \
503830d754dSRandall Stewart 	} \
504830d754dSRandall Stewart }
505830d754dSRandall Stewart #else
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 		*addr = 0; \
512830d754dSRandall Stewart 	} \
513830d754dSRandall Stewart }
514830d754dSRandall Stewart #endif
515