1163953Srrs/*-
2169382Srrs * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved.
3235828Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4235828Stuexen * 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 */
32235828Stuexen
33163954Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD$");
35235828Stuexen
36235828Stuexen#ifndef _NETINET_SCTP_OS_BSD_H_
37235828Stuexen#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
107255160Stuexen#include <crypto/sha1.h>
108255160Stuexen#include <crypto/sha2/sha2.h>
109255160Stuexen
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
155271746Stuexen#define SCTP_BASE_STAT(__m) V_system_base_info.sctpstat.__m
156271746Stuexen#define SCTP_BASE_SYSCTL(__m) V_system_base_info.sctpsysctl.__m
157197288Srrs#define SCTP_BASE_VAR(__m) V_system_base_info.__m
158197288Srrs
159234995Stuexen#define SCTP_PRINTF(params...)	printf(params)
160169352Srrs#if defined(SCTP_DEBUG)
161169352Srrs#define SCTPDBG(level, params...)					\
162169352Srrs{									\
163270352Stuexen	do {								\
164270352Stuexen		if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {		\
165270352Stuexen			SCTP_PRINTF(params);				\
166270352Stuexen		}							\
167270352Stuexen	} while (0);							\
168169352Srrs}
169169352Srrs#define SCTPDBG_ADDR(level, addr)					\
170169352Srrs{									\
171270352Stuexen	do {								\
172271746Stuexen		if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {		\
173271746Stuexen			sctp_print_address(addr);			\
174271746Stuexen		}							\
175270352Stuexen	} while (0);							\
176169352Srrs}
177169352Srrs#else
178169352Srrs#define SCTPDBG(level, params...)
179169352Srrs#define SCTPDBG_ADDR(level, addr)
180169352Srrs#endif
181169352Srrs
182170744Srrs#ifdef SCTP_LTRACE_CHUNKS
183189371Srrs#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)
184170744Srrs#else
185170744Srrs#define SCTP_LTRACE_CHK(a, b, c, d)
186170744Srrs#endif
187170744Srrs
188170744Srrs#ifdef SCTP_LTRACE_ERRORS
189234995Stuexen#define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
190234995Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
191270352Stuexen		SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
192234995Stuexen		            m, inp, stcb, net, file, __LINE__, err);
193234995Stuexen#define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
194234995Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
195270352Stuexen		SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
196234995Stuexen		            inp, stcb, net, file, __LINE__, err);
197170744Srrs#else
198171943Srrs#define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
199171943Srrs#define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
200170744Srrs#endif
201170744Srrs
202171943Srrs
203163953Srrs/*
204167598Srrs * Local address and interface list handling
205167598Srrs */
206169352Srrs#define SCTP_MAX_VRF_ID		0
207169352Srrs#define SCTP_SIZE_OF_VRF_HASH	3
208169352Srrs#define SCTP_IFNAMSIZ		IFNAMSIZ
209169352Srrs#define SCTP_DEFAULT_VRFID	0
210169352Srrs#define SCTP_VRF_ADDR_HASH_SIZE	16
211169352Srrs#define SCTP_VRF_IFN_HASH_SIZE	3
212170205Srrs#define	SCTP_INIT_VRF_TABLEID(vrf)
213167598Srrs
214167598Srrs#define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
215179157Srrs#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)
216167598Srrs
217167598Srrs/*
218167598Srrs * Access to IFN's to help with src-addr-selection
219167598Srrs */
220167598Srrs/* This could return VOID if the index works but for BSD we provide both. */
221167598Srrs#define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
222169352Srrs#define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) (ro)->ro_rt->rt_ifp->if_index
223169352Srrs#define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
224167598Srrs
225167598Srrs/*
226163953Srrs * general memory allocation
227163953Srrs */
228163953Srrs#define SCTP_MALLOC(var, type, size, name) \
229270352Stuexen	do { \
230270352Stuexen		var = (type)malloc(size, name, M_NOWAIT); \
231270352Stuexen	} while (0)
232163953Srrs
233184205Sdes#define SCTP_FREE(var, type)	free(var, type)
234163953Srrs
235163953Srrs#define SCTP_MALLOC_SONAME(var, type, size) \
236270352Stuexen	do { \
237270352Stuexen		var = (type)malloc(size, M_SONAME, M_WAITOK | M_ZERO); \
238270352Stuexen	} while (0)
239163953Srrs
240184205Sdes#define SCTP_FREE_SONAME(var)	free(var, M_SONAME)
241163953Srrs
242167598Srrs#define SCTP_PROCESS_STRUCT struct proc *
243167598Srrs
244163953Srrs/*
245163953Srrs * zone allocation functions
246163953Srrs */
247163953Srrs#include <vm/uma.h>
248179157Srrs
249163953Srrs/* SCTP_ZONE_INIT: initialize the zone */
250163953Srrstypedef struct uma_zone *sctp_zone_t;
251163953Srrs
252163953Srrs#define SCTP_ZONE_INIT(zone, name, size, number) { \
253163953Srrs	zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\
254199370Stuexen		0); \
255163953Srrs	uma_zone_set_max(zone, number); \
256163953Srrs}
257163953Srrs
258179157Srrs#define SCTP_ZONE_DESTROY(zone) uma_zdestroy(zone)
259179157Srrs
260163953Srrs/* SCTP_ZONE_GET: allocate element from the zone */
261166675Srrs#define SCTP_ZONE_GET(zone, type) \
262166675Srrs	(type *)uma_zalloc(zone, M_NOWAIT);
263163953Srrs
264163953Srrs/* SCTP_ZONE_FREE: free element from the zone */
265163953Srrs#define SCTP_ZONE_FREE(zone, element) \
266163953Srrs	uma_zfree(zone, element);
267179157Srrs
268166023Srrs#define SCTP_HASH_INIT(size, hashmark) hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
269166023Srrs#define SCTP_HASH_FREE(table, hashmark) hashdestroy(table, M_PCB, hashmark)
270163953Srrs
271166023Srrs#define SCTP_M_COPYM	m_copym
272166023Srrs
273163953Srrs/*
274165647Srrs * timers
275165647Srrs */
276165647Srrs#include <sys/callout.h>
277165647Srrstypedef struct callout sctp_os_timer_t;
278165647Srrs
279179157Srrs
280165647Srrs#define SCTP_OS_TIMER_INIT(tmr)	callout_init(tmr, 1)
281165647Srrs#define SCTP_OS_TIMER_START	callout_reset
282165647Srrs#define SCTP_OS_TIMER_STOP	callout_stop
283166023Srrs#define SCTP_OS_TIMER_STOP_DRAIN callout_drain
284165647Srrs#define SCTP_OS_TIMER_PENDING	callout_pending
285165647Srrs#define SCTP_OS_TIMER_ACTIVE	callout_active
286165647Srrs#define SCTP_OS_TIMER_DEACTIVATE callout_deactivate
287165647Srrs
288170091Srrs#define sctp_get_tick_count() (ticks)
289170091Srrs
290172090Srrs#define SCTP_UNUSED __attribute__((unused))
291170606Srrs
292165647Srrs/*
293163953Srrs * Functions
294163953Srrs */
295165647Srrs/* Mbuf manipulation and access macros  */
296165647Srrs#define SCTP_BUF_LEN(m) (m->m_len)
297165647Srrs#define SCTP_BUF_NEXT(m) (m->m_next)
298165647Srrs#define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
299165647Srrs#define SCTP_BUF_RESV_UF(m, size) m->m_data += size
300165647Srrs#define SCTP_BUF_AT(m, size) m->m_data + size
301165647Srrs#define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
302165647Srrs#define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
303165647Srrs#define SCTP_BUF_TYPE(m) (m->m_type)
304165647Srrs#define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
305165647Srrs#define SCTP_BUF_PREPEND	M_PREPEND
306168124Srrs
307168124Srrs#define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
308168124Srrs                                     MH_ALIGN(m, len); \
309168124Srrs                                  } else if ((m->m_flags & M_EXT) == 0) { \
310168124Srrs                                     M_ALIGN(m, len); \
311168124Srrs                                  }
312170138Srrs
313170138Srrs/* We make it so if you have up to 4 threads
314215034Sbrucec * writing based on the default size of
315170138Srrs * the packet log 65 k, that would be
316170138Srrs * 4 16k packets before we would hit
317170138Srrs * a problem.
318170138Srrs */
319170138Srrs#define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
320170138Srrs
321169352Srrs/*************************/
322169352Srrs/*      MTU              */
323169352Srrs/*************************/
324170181Srrs#define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) ((struct ifnet *)ifn)->if_mtu
325271750Stuexen#define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((uint32_t)((rt != NULL) ? rt->rt_mtu : 0))
326169352Srrs#define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) ((sctp_ifn->ifn_p != NULL) ? ((struct ifnet *)(sctp_ifn->ifn_p))->if_mtu : 0)
327169352Srrs#define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
328169352Srrs                                              if (rt != NULL) \
329263478Sglebius                                                 rt->rt_mtu = mtu; \
330169352Srrs                                           } while(0)
331168124Srrs
332169352Srrs/* (de-)register interface event notifications */
333170181Srrs#define SCTP_REGISTER_INTERFACE(ifhandle, af)
334170181Srrs#define SCTP_DEREGISTER_INTERFACE(ifhandle, af)
335169352Srrs
336171477Srrs
337165647Srrs/*************************/
338165647Srrs/* These are for logging */
339165647Srrs/*************************/
340165647Srrs/* return the base ext data pointer */
341165647Srrs#define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
342165647Srrs /* return the refcnt of the data pointer */
343165647Srrs#define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
344165647Srrs/* return any buffer related flags, this is
345165647Srrs * used beyond logging for apple only.
346165647Srrs */
347165647Srrs#define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
348165647Srrs
349165647Srrs/* For BSD this just accesses the M_PKTHDR length
350165647Srrs * so it operates on an mbuf with hdr flag. Other
351215034Sbrucec * O/S's may have separate packet header and mbuf
352165647Srrs * chain pointers.. thus the macro.
353165647Srrs */
354165647Srrs#define SCTP_HEADER_TO_CHAIN(m) (m)
355169352Srrs#define SCTP_DETACH_HEADER_FROM_CHAIN(m)
356237540Stuexen#define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
357169352Srrs#define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
358169352Srrs#define SCTP_RELEASE_HEADER(m)
359169352Srrs#define SCTP_RELEASE_PKT(m)	sctp_m_freem(m)
360179783Srrs#define SCTP_ENABLE_UDP_CSUM(m) do { \
361179783Srrs					m->m_pkthdr.csum_flags = CSUM_UDP; \
362179783Srrs					m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \
363179783Srrs				} while (0)
364165647Srrs
365170806Srrs#define SCTP_GET_PKT_VRFID(m, vrf_id)  ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
366169352Srrs
367170806Srrs
368170806Srrs
369165647Srrs/* Attach the chain of data into the sendable packet. */
370165647Srrs#define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
371169352Srrs                                                 pak = m; \
372165647Srrs                                                 pak->m_pkthdr.len = packet_length; \
373165647Srrs                         } while(0)
374165647Srrs
375165647Srrs/* Other m_pkthdr type things */
376170091Srrs#define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0)
377170091Srrs#define SCTP_IS_IT_LOOPBACK(m) ((m->m_flags & M_PKTHDR) && ((m->m_pkthdr.rcvif == NULL) || (m->m_pkthdr.rcvif->if_type == IFT_LOOP)))
378165647Srrs
379165647Srrs
380165647Srrs/* This converts any input packet header
381165647Srrs * into the chain of data holders, for BSD
382165647Srrs * its a NOP.
383165647Srrs */
384165647Srrs
385169352Srrs/* get the v6 hop limit */
386169352Srrs#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)));
387169352Srrs
388166023Srrs/* is the endpoint v6only? */
389166023Srrs#define SCTP_IPV6_V6ONLY(inp)	(((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
390167598Srrs/* is the socket non-blocking? */
391167598Srrs#define SCTP_SO_IS_NBIO(so)	((so)->so_state & SS_NBIO)
392167598Srrs#define SCTP_SET_SO_NBIO(so)	((so)->so_state |= SS_NBIO)
393167598Srrs#define SCTP_CLEAR_SO_NBIO(so)	((so)->so_state &= ~SS_NBIO)
394167598Srrs/* get the socket type */
395167598Srrs#define SCTP_SO_TYPE(so)	((so)->so_type)
396167695Srrs/* reserve sb space for a socket */
397167695Srrs#define SCTP_SORESERVE(so, send, recv)	soreserve(so, send, recv)
398169352Srrs/* wakeup a socket */
399169352Srrs#define SCTP_SOWAKEUP(so)	wakeup(&(so)->so_timeo)
400167695Srrs/* clear the socket buffer state */
401167695Srrs#define SCTP_SB_CLEAR(sb)	\
402167695Srrs	(sb).sb_cc = 0;		\
403167695Srrs	(sb).sb_mb = NULL;	\
404167695Srrs	(sb).sb_mbcnt = 0;
405166023Srrs
406167736Srrs#define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
407167736Srrs#define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
408167736Srrs
409166675Srrs/*
410168299Srrs * routes, output, etc.
411168299Srrs */
412168299Srrstypedef struct route sctp_route_t;
413169352Srrstypedef struct rtentry sctp_rtentry_t;
414168299Srrs
415231852Sbz/*
416231852Sbz * XXX multi-FIB support was backed out in r179783 and it seems clear that the
417231852Sbz * VRF support as currently in FreeBSD is not ready to support multi-FIB.
418231852Sbz * It might be best to implement multi-FIB support for both v4 and v6 indepedent
419231852Sbz * of VRFs and leave those to a real MPLS stack.
420231852Sbz */
421179783Srrs#define SCTP_RTALLOC(ro, vrf_id) rtalloc_ign((struct route *)ro, 0UL)
422168299Srrs
423169352Srrs/* Future zero copy wakeup/send  function */
424169352Srrs#define SCTP_ZERO_COPY_EVENT(inp, so)
425170181Srrs/* This is re-pulse ourselves for sendbuf */
426170181Srrs#define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so)
427169352Srrs
428169352Srrs/*
429254521Sandre * SCTP protocol specific mbuf flags.
430254521Sandre */
431254527Sandre#define	M_NOTIFICATION		M_PROTO1	/* SCTP notification */
432254521Sandre
433254521Sandre/*
434169352Srrs * IP output routines
435169352Srrs */
436170181Srrs#define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) \
437169352Srrs{ \
438211969Stuexen	int o_flgs = IP_RAWOUTPUT; \
439211969Stuexen	struct sctp_tcb *local_stcb = stcb; \
440211969Stuexen	if (local_stcb && \
441211969Stuexen	    local_stcb->sctp_ep && \
442211969Stuexen	    local_stcb->sctp_ep->sctp_socket) \
443211969Stuexen		o_flgs |= local_stcb->sctp_ep->sctp_socket->so_options & SO_DONTROUTE; \
444254523Sandre	m_clrprotoflags(o_pak); \
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; \
451254523Sandre	m_clrprotoflags(o_pak); \
452211969Stuexen	if (local_stcb && local_stcb->sctp_ep) \
453169352Srrs		result = ip6_output(o_pak, \
454211969Stuexen				    ((struct in6pcb *)(local_stcb->sctp_ep))->in6p_outputopts, \
455169352Srrs				    (ro), 0, 0, ifp, NULL); \
456169352Srrs	else \
457169352Srrs		result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
458169352Srrs}
459169352Srrs
460169208Srrsstruct mbuf *
461169208Srrssctp_get_mbuf_for_msg(unsigned int space_needed,
462169208Srrs    int want_header, int how, int allonebuf, int type);
463169208Srrs
464169208Srrs
465168299Srrs/*
466166675Srrs * SCTP AUTH
467166675Srrs */
468166675Srrs#define SCTP_READ_RANDOM(buf, len)	read_random(buf, len)
469166675Srrs
470166675Srrs/* map standard crypto API names */
471255160Stuexen#define SCTP_SHA1_CTX		SHA1_CTX
472255160Stuexen#define SCTP_SHA1_INIT		SHA1Init
473255160Stuexen#define SCTP_SHA1_UPDATE	SHA1Update
474255160Stuexen#define SCTP_SHA1_FINAL(x,y)	SHA1Final((caddr_t)x, y)
475166675Srrs
476255160Stuexen#define SCTP_SHA256_CTX		SHA256_CTX
477255160Stuexen#define SCTP_SHA256_INIT	SHA256_Init
478255160Stuexen#define SCTP_SHA256_UPDATE	SHA256_Update
479255160Stuexen#define SCTP_SHA256_FINAL(x,y)	SHA256_Final((caddr_t)x, y)
480166675Srrs
481166675Srrs#endif
482185694Srrs
483185694Srrs#define SCTP_DECREMENT_AND_CHECK_REFCOUNT(addr) (atomic_fetchadd_int(addr, -1) == 1)
484185694Srrs#if defined(INVARIANTS)
485185694Srrs#define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
486185694Srrs{ \
487185694Srrs	int32_t oldval; \
488185694Srrs	oldval = atomic_fetchadd_int(addr, -val); \
489185694Srrs	if (oldval < val) { \
490185694Srrs		panic("Counter goes negative"); \
491185694Srrs	} \
492185694Srrs}
493185694Srrs#else
494185694Srrs#define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
495185694Srrs{ \
496185694Srrs	int32_t oldval; \
497185694Srrs	oldval = atomic_fetchadd_int(addr, -val); \
498185694Srrs	if (oldval < val) { \
499185694Srrs		*addr = 0; \
500185694Srrs	} \
501185694Srrs}
502185694Srrs#endif
503