1163953Srrs/*-
2169382Srrs * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved.
3237896Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4237896Stuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5163953Srrs *
6163953Srrs * Redistribution and use in source and binary forms, with or without
7163953Srrs * modification, are permitted provided that the following conditions are met:
8163953Srrs *
9163953Srrs * a) Redistributions of source code must retain the above copyright notice,
10163953Srrs *   this list of conditions and the following disclaimer.
11163953Srrs *
12163953Srrs * b) Redistributions in binary form must reproduce the above copyright
13163953Srrs *    notice, this list of conditions and the following disclaimer in
14163953Srrs *   the documentation and/or other materials provided with the distribution.
15163953Srrs *
16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
17163953Srrs *    contributors may be used to endorse or promote products derived
18163953Srrs *    from this software without specific prior written permission.
19163953Srrs *
20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
31163953Srrs */
32237896Stuexen
33163954Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD$");
35237896Stuexen
36237896Stuexen#ifndef _NETINET_SCTP_OS_BSD_H_
37237896Stuexen#define _NETINET_SCTP_OS_BSD_H_
38163953Srrs/*
39163953Srrs * includes
40163953Srrs */
41166086Srrs#include "opt_ipsec.h"
42166086Srrs#include "opt_compat.h"
43166086Srrs#include "opt_inet6.h"
44166086Srrs#include "opt_inet.h"
45166086Srrs#include "opt_sctp.h"
46181803Sbz
47166086Srrs#include <sys/param.h>
48170744Srrs#include <sys/ktr.h>
49166086Srrs#include <sys/systm.h>
50166086Srrs#include <sys/malloc.h>
51166086Srrs#include <sys/kernel.h>
52166086Srrs#include <sys/sysctl.h>
53166086Srrs#include <sys/mbuf.h>
54166086Srrs#include <sys/protosw.h>
55166086Srrs#include <sys/socket.h>
56166086Srrs#include <sys/socketvar.h>
57171477Srrs#include <sys/jail.h>
58166086Srrs#include <sys/sysctl.h>
59166086Srrs#include <sys/resourcevar.h>
60166086Srrs#include <sys/uio.h>
61172218Srrs#include <sys/lock.h>
62172218Srrs#include <sys/rwlock.h>
63167598Srrs#include <sys/kthread.h>
64166086Srrs#include <sys/priv.h>
65163953Srrs#include <sys/random.h>
66166086Srrs#include <sys/limits.h>
67166086Srrs#include <sys/queue.h>
68166086Srrs#include <machine/cpu.h>
69163953Srrs
70166086Srrs#include <net/if.h>
71166086Srrs#include <net/if_types.h>
72166086Srrs#include <net/if_var.h>
73166086Srrs#include <net/route.h>
74197929Stuexen#include <net/vnet.h>
75166086Srrs
76166086Srrs#include <netinet/in.h>
77166086Srrs#include <netinet/in_systm.h>
78166086Srrs#include <netinet/ip.h>
79166086Srrs#include <netinet/in_pcb.h>
80166086Srrs#include <netinet/in_var.h>
81166086Srrs#include <netinet/ip_var.h>
82166086Srrs#include <netinet/ip_icmp.h>
83166086Srrs#include <netinet/icmp_var.h>
84166086Srrs
85171167Sgnn#ifdef IPSEC
86171133Sgnn#include <netipsec/ipsec.h>
87171133Sgnn#include <netipsec/key.h>
88166086Srrs#endif				/* IPSEC */
89166086Srrs
90166086Srrs#ifdef INET6
91166086Srrs#include <sys/domain.h>
92171167Sgnn#ifdef IPSEC
93171139Sgnn#include <netipsec/ipsec6.h>
94166086Srrs#endif
95166086Srrs#include <netinet/ip6.h>
96166086Srrs#include <netinet6/ip6_var.h>
97166086Srrs#include <netinet6/in6_pcb.h>
98166086Srrs#include <netinet/icmp6.h>
99166086Srrs#include <netinet6/ip6protosw.h>
100166086Srrs#include <netinet6/nd6.h>
101166086Srrs#include <netinet6/scope6_var.h>
102166086Srrs#endif				/* INET6 */
103166086Srrs
104166675Srrs
105166086Srrs#include <netinet/ip_options.h>
106166086Srrs
107265961Stuexen#include <crypto/sha1.h>
108265961Stuexen#include <crypto/sha2/sha2.h>
109265961Stuexen
110166086Srrs#ifndef in6pcb
111166086Srrs#define in6pcb		inpcb
112166086Srrs#endif
113170091Srrs/* Declare all the malloc names for all the various mallocs */
114170091SrrsMALLOC_DECLARE(SCTP_M_MAP);
115170091SrrsMALLOC_DECLARE(SCTP_M_STRMI);
116170091SrrsMALLOC_DECLARE(SCTP_M_STRMO);
117170091SrrsMALLOC_DECLARE(SCTP_M_ASC_ADDR);
118170091SrrsMALLOC_DECLARE(SCTP_M_ASC_IT);
119170091SrrsMALLOC_DECLARE(SCTP_M_AUTH_CL);
120170091SrrsMALLOC_DECLARE(SCTP_M_AUTH_KY);
121170091SrrsMALLOC_DECLARE(SCTP_M_AUTH_HL);
122170091SrrsMALLOC_DECLARE(SCTP_M_AUTH_IF);
123170091SrrsMALLOC_DECLARE(SCTP_M_STRESET);
124170091SrrsMALLOC_DECLARE(SCTP_M_CMSG);
125170091SrrsMALLOC_DECLARE(SCTP_M_COPYAL);
126170091SrrsMALLOC_DECLARE(SCTP_M_VRF);
127170091SrrsMALLOC_DECLARE(SCTP_M_IFA);
128170091SrrsMALLOC_DECLARE(SCTP_M_IFN);
129170091SrrsMALLOC_DECLARE(SCTP_M_TIMW);
130170091SrrsMALLOC_DECLARE(SCTP_M_MVRF);
131170091SrrsMALLOC_DECLARE(SCTP_M_ITER);
132170091SrrsMALLOC_DECLARE(SCTP_M_SOCKOPT);
133218211SrrsMALLOC_DECLARE(SCTP_M_MCORE);
134166086Srrs
135171943Srrs#if defined(SCTP_LOCAL_TRACE_BUF)
136171943Srrs
137171943Srrs#define SCTP_GET_CYCLECOUNT get_cyclecount()
138171943Srrs#define SCTP_CTR6 sctp_log_trace
139171943Srrs
140171943Srrs#else
141171943Srrs#define SCTP_CTR6 CTR6
142171943Srrs#endif
143171943Srrs
144163953Srrs/*
145180387Srrs * Macros to expand out globals defined by various modules
146180387Srrs * to either a real global or a virtualized instance of one,
147181803Sbz * depending on whether VIMAGE is defined.
148180387Srrs */
149180387Srrs/* then define the macro(s) that hook into the vimage macros */
150197326Stuexen#define MODULE_GLOBAL(__SYMBOL) V_##__SYMBOL
151188067Srrs
152197326Stuexen#define V_system_base_info VNET(system_base_info)
153197288Srrs#define SCTP_BASE_INFO(__m) V_system_base_info.sctppcbinfo.__m
154197288Srrs#define SCTP_BASE_STATS V_system_base_info.sctpstat
155197326Stuexen#define SCTP_BASE_STATS_SYSCTL VNET_NAME(system_base_info.sctpstat)
156197288Srrs#define SCTP_BASE_STAT(__m)     V_system_base_info.sctpstat.__m
157197326Stuexen#define SCTP_BASE_SYSCTL(__m) VNET_NAME(system_base_info.sctpsysctl.__m)
158197288Srrs#define SCTP_BASE_VAR(__m) V_system_base_info.__m
159197288Srrs
160180387Srrs/*
161163953Srrs *
162163953Srrs */
163165220Srrs#define USER_ADDR_NULL	(NULL)	/* FIX ME: temp */
164165220Srrs
165235164Stuexen#define SCTP_PRINTF(params...)	printf(params)
166169352Srrs#if defined(SCTP_DEBUG)
167169352Srrs#define SCTPDBG(level, params...)					\
168169352Srrs{									\
169169352Srrs    do {								\
170179783Srrs	if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {			\
171235164Stuexen	    SCTP_PRINTF(params);						\
172169352Srrs	}								\
173169352Srrs    } while (0);							\
174169352Srrs}
175169352Srrs#define SCTPDBG_ADDR(level, addr)					\
176169352Srrs{									\
177169352Srrs    do {								\
178179783Srrs	if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {			\
179169352Srrs	    sctp_print_address(addr);					\
180169352Srrs	}								\
181169352Srrs    } while (0);							\
182169352Srrs}
183169352Srrs#else
184169352Srrs#define SCTPDBG(level, params...)
185169352Srrs#define SCTPDBG_ADDR(level, addr)
186169352Srrs#endif
187169352Srrs
188170744Srrs#ifdef SCTP_LTRACE_CHUNKS
189189371Srrs#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)
190170744Srrs#else
191170744Srrs#define SCTP_LTRACE_CHK(a, b, c, d)
192170744Srrs#endif
193170744Srrs
194170744Srrs#ifdef SCTP_LTRACE_ERRORS
195235164Stuexen#define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
196235164Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
197235164Stuexen        	SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
198235164Stuexen		            m, inp, stcb, net, file, __LINE__, err);
199235164Stuexen#define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
200235164Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
201235164Stuexen        	SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
202235164Stuexen		            inp, stcb, net, file, __LINE__, err);
203170744Srrs#else
204171943Srrs#define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
205171943Srrs#define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
206170744Srrs#endif
207170744Srrs
208171943Srrs
209163953Srrs/*
210167598Srrs * Local address and interface list handling
211167598Srrs */
212169352Srrs#define SCTP_MAX_VRF_ID		0
213169352Srrs#define SCTP_SIZE_OF_VRF_HASH	3
214169352Srrs#define SCTP_IFNAMSIZ		IFNAMSIZ
215169352Srrs#define SCTP_DEFAULT_VRFID	0
216169352Srrs#define SCTP_VRF_ADDR_HASH_SIZE	16
217169352Srrs#define SCTP_VRF_IFN_HASH_SIZE	3
218170205Srrs#define	SCTP_INIT_VRF_TABLEID(vrf)
219167598Srrs
220167598Srrs#define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
221179157Srrs#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)
222167598Srrs
223167598Srrs/*
224167598Srrs * Access to IFN's to help with src-addr-selection
225167598Srrs */
226167598Srrs/* This could return VOID if the index works but for BSD we provide both. */
227167598Srrs#define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
228169352Srrs#define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) (ro)->ro_rt->rt_ifp->if_index
229169352Srrs#define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
230167598Srrs
231167598Srrs/*
232163953Srrs * general memory allocation
233163953Srrs */
234163953Srrs#define SCTP_MALLOC(var, type, size, name) \
235163953Srrs    do { \
236184205Sdes	var = (type)malloc(size, name, M_NOWAIT); \
237163953Srrs    } while (0)
238163953Srrs
239184205Sdes#define SCTP_FREE(var, type)	free(var, type)
240163953Srrs
241163953Srrs#define SCTP_MALLOC_SONAME(var, type, size) \
242163953Srrs    do { \
243184205Sdes	var = (type)malloc(size, M_SONAME, M_WAITOK | M_ZERO); \
244163953Srrs    } while (0)
245163953Srrs
246184205Sdes#define SCTP_FREE_SONAME(var)	free(var, M_SONAME)
247163953Srrs
248167598Srrs#define SCTP_PROCESS_STRUCT struct proc *
249167598Srrs
250163953Srrs/*
251163953Srrs * zone allocation functions
252163953Srrs */
253163953Srrs#include <vm/uma.h>
254179157Srrs
255163953Srrs/* SCTP_ZONE_INIT: initialize the zone */
256163953Srrstypedef struct uma_zone *sctp_zone_t;
257163953Srrs
258163953Srrs#define SCTP_ZONE_INIT(zone, name, size, number) { \
259163953Srrs	zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\
260199370Stuexen		0); \
261163953Srrs	uma_zone_set_max(zone, number); \
262163953Srrs}
263163953Srrs
264179157Srrs#define SCTP_ZONE_DESTROY(zone) uma_zdestroy(zone)
265179157Srrs
266163953Srrs/* SCTP_ZONE_GET: allocate element from the zone */
267166675Srrs#define SCTP_ZONE_GET(zone, type) \
268166675Srrs	(type *)uma_zalloc(zone, M_NOWAIT);
269163953Srrs
270163953Srrs/* SCTP_ZONE_FREE: free element from the zone */
271163953Srrs#define SCTP_ZONE_FREE(zone, element) \
272163953Srrs	uma_zfree(zone, element);
273179157Srrs
274166023Srrs#define SCTP_HASH_INIT(size, hashmark) hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
275166023Srrs#define SCTP_HASH_FREE(table, hashmark) hashdestroy(table, M_PCB, hashmark)
276163953Srrs
277166023Srrs#define SCTP_M_COPYM	m_copym
278166023Srrs
279163953Srrs/*
280165647Srrs * timers
281165647Srrs */
282165647Srrs#include <sys/callout.h>
283165647Srrstypedef struct callout sctp_os_timer_t;
284165647Srrs
285179157Srrs
286165647Srrs#define SCTP_OS_TIMER_INIT(tmr)	callout_init(tmr, 1)
287165647Srrs#define SCTP_OS_TIMER_START	callout_reset
288165647Srrs#define SCTP_OS_TIMER_STOP	callout_stop
289166023Srrs#define SCTP_OS_TIMER_STOP_DRAIN callout_drain
290165647Srrs#define SCTP_OS_TIMER_PENDING	callout_pending
291165647Srrs#define SCTP_OS_TIMER_ACTIVE	callout_active
292165647Srrs#define SCTP_OS_TIMER_DEACTIVATE callout_deactivate
293165647Srrs
294170091Srrs#define sctp_get_tick_count() (ticks)
295170091Srrs
296172090Srrs#define SCTP_UNUSED __attribute__((unused))
297170606Srrs
298165647Srrs/*
299163953Srrs * Functions
300163953Srrs */
301165647Srrs/* Mbuf manipulation and access macros  */
302165647Srrs#define SCTP_BUF_LEN(m) (m->m_len)
303165647Srrs#define SCTP_BUF_NEXT(m) (m->m_next)
304165647Srrs#define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
305165647Srrs#define SCTP_BUF_RESV_UF(m, size) m->m_data += size
306165647Srrs#define SCTP_BUF_AT(m, size) m->m_data + size
307165647Srrs#define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
308165647Srrs#define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
309165647Srrs#define SCTP_BUF_TYPE(m) (m->m_type)
310165647Srrs#define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
311165647Srrs#define SCTP_BUF_PREPEND	M_PREPEND
312168124Srrs
313168124Srrs#define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
314168124Srrs                                     MH_ALIGN(m, len); \
315168124Srrs                                  } else if ((m->m_flags & M_EXT) == 0) { \
316168124Srrs                                     M_ALIGN(m, len); \
317168124Srrs                                  }
318170138Srrs
319170138Srrs/* We make it so if you have up to 4 threads
320215034Sbrucec * writing based on the default size of
321170138Srrs * the packet log 65 k, that would be
322170138Srrs * 4 16k packets before we would hit
323170138Srrs * a problem.
324170138Srrs */
325170138Srrs#define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
326170138Srrs
327169352Srrs/*************************/
328169352Srrs/*      MTU              */
329169352Srrs/*************************/
330170181Srrs#define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) ((struct ifnet *)ifn)->if_mtu
331169352Srrs#define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0)
332169352Srrs#define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) ((sctp_ifn->ifn_p != NULL) ? ((struct ifnet *)(sctp_ifn->ifn_p))->if_mtu : 0)
333169352Srrs#define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
334169352Srrs                                              if (rt != NULL) \
335169352Srrs                                                 rt->rt_rmx.rmx_mtu = mtu; \
336169352Srrs                                           } while(0)
337168124Srrs
338169352Srrs/* (de-)register interface event notifications */
339170181Srrs#define SCTP_REGISTER_INTERFACE(ifhandle, af)
340170181Srrs#define SCTP_DEREGISTER_INTERFACE(ifhandle, af)
341169352Srrs
342171477Srrs
343165647Srrs/*************************/
344165647Srrs/* These are for logging */
345165647Srrs/*************************/
346165647Srrs/* return the base ext data pointer */
347165647Srrs#define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
348165647Srrs /* return the refcnt of the data pointer */
349165647Srrs#define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
350165647Srrs/* return any buffer related flags, this is
351165647Srrs * used beyond logging for apple only.
352165647Srrs */
353165647Srrs#define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
354165647Srrs
355165647Srrs/* For BSD this just accesses the M_PKTHDR length
356165647Srrs * so it operates on an mbuf with hdr flag. Other
357215034Sbrucec * O/S's may have separate packet header and mbuf
358165647Srrs * chain pointers.. thus the macro.
359165647Srrs */
360165647Srrs#define SCTP_HEADER_TO_CHAIN(m) (m)
361169352Srrs#define SCTP_DETACH_HEADER_FROM_CHAIN(m)
362252879Stuexen#define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
363169352Srrs#define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
364169352Srrs#define SCTP_RELEASE_HEADER(m)
365169352Srrs#define SCTP_RELEASE_PKT(m)	sctp_m_freem(m)
366179783Srrs#define SCTP_ENABLE_UDP_CSUM(m) do { \
367179783Srrs					m->m_pkthdr.csum_flags = CSUM_UDP; \
368179783Srrs					m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \
369179783Srrs				} while (0)
370165647Srrs
371170806Srrs#define SCTP_GET_PKT_VRFID(m, vrf_id)  ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
372169352Srrs
373170806Srrs
374170806Srrs
375165647Srrs/* Attach the chain of data into the sendable packet. */
376165647Srrs#define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
377169352Srrs                                                 pak = m; \
378165647Srrs                                                 pak->m_pkthdr.len = packet_length; \
379165647Srrs                         } while(0)
380165647Srrs
381165647Srrs/* Other m_pkthdr type things */
382170091Srrs#define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0)
383170091Srrs#define SCTP_IS_IT_LOOPBACK(m) ((m->m_flags & M_PKTHDR) && ((m->m_pkthdr.rcvif == NULL) || (m->m_pkthdr.rcvif->if_type == IFT_LOOP)))
384165647Srrs
385165647Srrs
386165647Srrs/* This converts any input packet header
387165647Srrs * into the chain of data holders, for BSD
388165647Srrs * its a NOP.
389165647Srrs */
390165647Srrs
391169352Srrs/* get the v6 hop limit */
392169352Srrs#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)));
393169352Srrs
394166023Srrs/* is the endpoint v6only? */
395166023Srrs#define SCTP_IPV6_V6ONLY(inp)	(((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
396167598Srrs/* is the socket non-blocking? */
397167598Srrs#define SCTP_SO_IS_NBIO(so)	((so)->so_state & SS_NBIO)
398167598Srrs#define SCTP_SET_SO_NBIO(so)	((so)->so_state |= SS_NBIO)
399167598Srrs#define SCTP_CLEAR_SO_NBIO(so)	((so)->so_state &= ~SS_NBIO)
400167598Srrs/* get the socket type */
401167598Srrs#define SCTP_SO_TYPE(so)	((so)->so_type)
402167695Srrs/* reserve sb space for a socket */
403167695Srrs#define SCTP_SORESERVE(so, send, recv)	soreserve(so, send, recv)
404169352Srrs/* wakeup a socket */
405169352Srrs#define SCTP_SOWAKEUP(so)	wakeup(&(so)->so_timeo)
406167695Srrs/* clear the socket buffer state */
407167695Srrs#define SCTP_SB_CLEAR(sb)	\
408167695Srrs	(sb).sb_cc = 0;		\
409167695Srrs	(sb).sb_mb = NULL;	\
410167695Srrs	(sb).sb_mbcnt = 0;
411166023Srrs
412167736Srrs#define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
413167736Srrs#define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
414167736Srrs
415166675Srrs/*
416168299Srrs * routes, output, etc.
417168299Srrs */
418168299Srrstypedef struct route sctp_route_t;
419169352Srrstypedef struct rtentry sctp_rtentry_t;
420168299Srrs
421232292Sbz/*
422232292Sbz * XXX multi-FIB support was backed out in r179783 and it seems clear that the
423232292Sbz * VRF support as currently in FreeBSD is not ready to support multi-FIB.
424232292Sbz * It might be best to implement multi-FIB support for both v4 and v6 indepedent
425232292Sbz * of VRFs and leave those to a real MPLS stack.
426232292Sbz */
427179783Srrs#define SCTP_RTALLOC(ro, vrf_id) rtalloc_ign((struct route *)ro, 0UL)
428168299Srrs
429169352Srrs/* Future zero copy wakeup/send  function */
430169352Srrs#define SCTP_ZERO_COPY_EVENT(inp, so)
431170181Srrs/* This is re-pulse ourselves for sendbuf */
432170181Srrs#define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so)
433169352Srrs
434169352Srrs/*
435169352Srrs * IP output routines
436169352Srrs */
437170181Srrs#define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) \
438169352Srrs{ \
439211969Stuexen	int o_flgs = IP_RAWOUTPUT; \
440211969Stuexen	struct sctp_tcb *local_stcb = stcb; \
441211969Stuexen	if (local_stcb && \
442211969Stuexen	    local_stcb->sctp_ep && \
443211969Stuexen	    local_stcb->sctp_ep->sctp_socket) \
444211969Stuexen		o_flgs |= local_stcb->sctp_ep->sctp_socket->so_options & SO_DONTROUTE; \
445169352Srrs	result = ip_output(o_pak, NULL, ro, o_flgs, 0, NULL); \
446169352Srrs}
447169352Srrs
448170181Srrs#define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \
449169352Srrs{ \
450211969Stuexen	struct sctp_tcb *local_stcb = stcb; \
451211969Stuexen	if (local_stcb && local_stcb->sctp_ep) \
452169352Srrs		result = ip6_output(o_pak, \
453211969Stuexen				    ((struct in6pcb *)(local_stcb->sctp_ep))->in6p_outputopts, \
454169352Srrs				    (ro), 0, 0, ifp, NULL); \
455169352Srrs	else \
456169352Srrs		result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
457169352Srrs}
458169352Srrs
459169208Srrsstruct mbuf *
460169208Srrssctp_get_mbuf_for_msg(unsigned int space_needed,
461169208Srrs    int want_header, int how, int allonebuf, int type);
462169208Srrs
463169208Srrs
464168299Srrs/*
465166675Srrs * SCTP AUTH
466166675Srrs */
467166675Srrs#define SCTP_READ_RANDOM(buf, len)	read_random(buf, len)
468166675Srrs
469166675Srrs/* map standard crypto API names */
470265961Stuexen#define SCTP_SHA1_CTX		SHA1_CTX
471265961Stuexen#define SCTP_SHA1_INIT		SHA1Init
472265961Stuexen#define SCTP_SHA1_UPDATE	SHA1Update
473265961Stuexen#define SCTP_SHA1_FINAL(x,y)	SHA1Final((caddr_t)x, y)
474166675Srrs
475265961Stuexen#define SCTP_SHA256_CTX		SHA256_CTX
476265961Stuexen#define SCTP_SHA256_INIT	SHA256_Init
477265961Stuexen#define SCTP_SHA256_UPDATE	SHA256_Update
478265961Stuexen#define SCTP_SHA256_FINAL(x,y)	SHA256_Final((caddr_t)x, y)
479166675Srrs
480166675Srrs#endif
481185694Srrs
482185694Srrs#define SCTP_DECREMENT_AND_CHECK_REFCOUNT(addr) (atomic_fetchadd_int(addr, -1) == 1)
483185694Srrs#if defined(INVARIANTS)
484185694Srrs#define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
485185694Srrs{ \
486185694Srrs	int32_t oldval; \
487185694Srrs	oldval = atomic_fetchadd_int(addr, -val); \
488185694Srrs	if (oldval < val) { \
489185694Srrs		panic("Counter goes negative"); \
490185694Srrs	} \
491185694Srrs}
492185694Srrs#else
493185694Srrs#define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
494185694Srrs{ \
495185694Srrs	int32_t oldval; \
496185694Srrs	oldval = atomic_fetchadd_int(addr, -val); \
497185694Srrs	if (oldval < val) { \
498185694Srrs		*addr = 0; \
499185694Srrs	} \
500185694Srrs}
501185694Srrs#endif
502