1/*-
2 * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * a) Redistributions of source code must retain the above copyright notice,
10 *   this list of conditions and the following disclaimer.
11 *
12 * b) Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in
14 *   the documentation and/or other materials provided with the distribution.
15 *
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 *    contributors may be used to endorse or promote products derived
18 *    from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#ifndef _NETINET_SCTP_OS_BSD_H_
37#define _NETINET_SCTP_OS_BSD_H_
38/*
39 * includes
40 */
41#include "opt_ipsec.h"
42#include "opt_compat.h"
43#include "opt_inet6.h"
44#include "opt_inet.h"
45#include "opt_sctp.h"
46
47#include <sys/param.h>
48#include <sys/ktr.h>
49#include <sys/systm.h>
50#include <sys/malloc.h>
51#include <sys/kernel.h>
52#include <sys/sysctl.h>
53#include <sys/mbuf.h>
54#include <sys/protosw.h>
55#include <sys/socket.h>
56#include <sys/socketvar.h>
57#include <sys/jail.h>
58#include <sys/sysctl.h>
59#include <sys/resourcevar.h>
60#include <sys/uio.h>
61#include <sys/lock.h>
62#include <sys/rwlock.h>
63#include <sys/kthread.h>
64#include <sys/priv.h>
65#include <sys/random.h>
66#include <sys/limits.h>
67#include <sys/queue.h>
68#include <machine/cpu.h>
69
70#include <net/if.h>
71#include <net/if_types.h>
72#include <net/if_var.h>
73#include <net/route.h>
74#include <net/vnet.h>
75
76#include <netinet/in.h>
77#include <netinet/in_systm.h>
78#include <netinet/ip.h>
79#include <netinet/in_pcb.h>
80#include <netinet/in_var.h>
81#include <netinet/ip_var.h>
82#include <netinet/ip_icmp.h>
83#include <netinet/icmp_var.h>
84
85#ifdef IPSEC
86#include <netipsec/ipsec.h>
87#include <netipsec/key.h>
88#endif				/* IPSEC */
89
90#ifdef INET6
91#include <sys/domain.h>
92#ifdef IPSEC
93#include <netipsec/ipsec6.h>
94#endif
95#include <netinet/ip6.h>
96#include <netinet6/ip6_var.h>
97#include <netinet6/in6_pcb.h>
98#include <netinet/icmp6.h>
99#include <netinet6/ip6protosw.h>
100#include <netinet6/nd6.h>
101#include <netinet6/scope6_var.h>
102#endif				/* INET6 */
103
104
105#include <netinet/ip_options.h>
106
107#include <crypto/sha1.h>
108#include <crypto/sha2/sha2.h>
109
110#ifndef in6pcb
111#define in6pcb		inpcb
112#endif
113/* Declare all the malloc names for all the various mallocs */
114MALLOC_DECLARE(SCTP_M_MAP);
115MALLOC_DECLARE(SCTP_M_STRMI);
116MALLOC_DECLARE(SCTP_M_STRMO);
117MALLOC_DECLARE(SCTP_M_ASC_ADDR);
118MALLOC_DECLARE(SCTP_M_ASC_IT);
119MALLOC_DECLARE(SCTP_M_AUTH_CL);
120MALLOC_DECLARE(SCTP_M_AUTH_KY);
121MALLOC_DECLARE(SCTP_M_AUTH_HL);
122MALLOC_DECLARE(SCTP_M_AUTH_IF);
123MALLOC_DECLARE(SCTP_M_STRESET);
124MALLOC_DECLARE(SCTP_M_CMSG);
125MALLOC_DECLARE(SCTP_M_COPYAL);
126MALLOC_DECLARE(SCTP_M_VRF);
127MALLOC_DECLARE(SCTP_M_IFA);
128MALLOC_DECLARE(SCTP_M_IFN);
129MALLOC_DECLARE(SCTP_M_TIMW);
130MALLOC_DECLARE(SCTP_M_MVRF);
131MALLOC_DECLARE(SCTP_M_ITER);
132MALLOC_DECLARE(SCTP_M_SOCKOPT);
133MALLOC_DECLARE(SCTP_M_MCORE);
134
135#if defined(SCTP_LOCAL_TRACE_BUF)
136
137#define SCTP_GET_CYCLECOUNT get_cyclecount()
138#define SCTP_CTR6 sctp_log_trace
139
140#else
141#define SCTP_CTR6 CTR6
142#endif
143
144/*
145 * Macros to expand out globals defined by various modules
146 * to either a real global or a virtualized instance of one,
147 * depending on whether VIMAGE is defined.
148 */
149/* then define the macro(s) that hook into the vimage macros */
150#define MODULE_GLOBAL(__SYMBOL) V_##__SYMBOL
151
152#define V_system_base_info VNET(system_base_info)
153#define SCTP_BASE_INFO(__m) V_system_base_info.sctppcbinfo.__m
154#define SCTP_BASE_STATS V_system_base_info.sctpstat
155#define SCTP_BASE_STAT(__m) V_system_base_info.sctpstat.__m
156#define SCTP_BASE_SYSCTL(__m) V_system_base_info.sctpsysctl.__m
157#define SCTP_BASE_VAR(__m) V_system_base_info.__m
158
159#define SCTP_PRINTF(params...)	printf(params)
160#if defined(SCTP_DEBUG)
161#define SCTPDBG(level, params...)					\
162{									\
163	do {								\
164		if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {		\
165			SCTP_PRINTF(params);				\
166		}							\
167	} while (0);							\
168}
169#define SCTPDBG_ADDR(level, addr)					\
170{									\
171	do {								\
172		if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {		\
173			sctp_print_address(addr);			\
174		}							\
175	} while (0);							\
176}
177#else
178#define SCTPDBG(level, params...)
179#define SCTPDBG_ADDR(level, addr)
180#endif
181
182#ifdef SCTP_LTRACE_CHUNKS
183#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)
184#else
185#define SCTP_LTRACE_CHK(a, b, c, d)
186#endif
187
188#ifdef SCTP_LTRACE_ERRORS
189#define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
190	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
191		SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
192		            m, inp, stcb, net, file, __LINE__, err);
193#define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
194	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LTRACE_ERROR_ENABLE) \
195		SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
196		            inp, stcb, net, file, __LINE__, err);
197#else
198#define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
199#define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
200#endif
201
202
203/*
204 * Local address and interface list handling
205 */
206#define SCTP_MAX_VRF_ID		0
207#define SCTP_SIZE_OF_VRF_HASH	3
208#define SCTP_IFNAMSIZ		IFNAMSIZ
209#define SCTP_DEFAULT_VRFID	0
210#define SCTP_VRF_ADDR_HASH_SIZE	16
211#define SCTP_VRF_IFN_HASH_SIZE	3
212#define	SCTP_INIT_VRF_TABLEID(vrf)
213
214#define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
215#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)
216
217/*
218 * Access to IFN's to help with src-addr-selection
219 */
220/* This could return VOID if the index works but for BSD we provide both. */
221#define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
222#define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) (ro)->ro_rt->rt_ifp->if_index
223#define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
224
225/*
226 * general memory allocation
227 */
228#define SCTP_MALLOC(var, type, size, name) \
229	do { \
230		var = (type)malloc(size, name, M_NOWAIT); \
231	} while (0)
232
233#define SCTP_FREE(var, type)	free(var, type)
234
235#define SCTP_MALLOC_SONAME(var, type, size) \
236	do { \
237		var = (type)malloc(size, M_SONAME, M_WAITOK | M_ZERO); \
238	} while (0)
239
240#define SCTP_FREE_SONAME(var)	free(var, M_SONAME)
241
242#define SCTP_PROCESS_STRUCT struct proc *
243
244/*
245 * zone allocation functions
246 */
247#include <vm/uma.h>
248
249/* SCTP_ZONE_INIT: initialize the zone */
250typedef struct uma_zone *sctp_zone_t;
251
252#define SCTP_ZONE_INIT(zone, name, size, number) { \
253	zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\
254		0); \
255	uma_zone_set_max(zone, number); \
256}
257
258#define SCTP_ZONE_DESTROY(zone) uma_zdestroy(zone)
259
260/* SCTP_ZONE_GET: allocate element from the zone */
261#define SCTP_ZONE_GET(zone, type) \
262	(type *)uma_zalloc(zone, M_NOWAIT);
263
264/* SCTP_ZONE_FREE: free element from the zone */
265#define SCTP_ZONE_FREE(zone, element) \
266	uma_zfree(zone, element);
267
268#define SCTP_HASH_INIT(size, hashmark) hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
269#define SCTP_HASH_FREE(table, hashmark) hashdestroy(table, M_PCB, hashmark)
270
271#define SCTP_M_COPYM	m_copym
272
273/*
274 * timers
275 */
276#include <sys/callout.h>
277typedef struct callout sctp_os_timer_t;
278
279
280#define SCTP_OS_TIMER_INIT(tmr)	callout_init(tmr, 1)
281#define SCTP_OS_TIMER_START	callout_reset
282#define SCTP_OS_TIMER_STOP	callout_stop
283#define SCTP_OS_TIMER_STOP_DRAIN callout_drain
284#define SCTP_OS_TIMER_PENDING	callout_pending
285#define SCTP_OS_TIMER_ACTIVE	callout_active
286#define SCTP_OS_TIMER_DEACTIVATE callout_deactivate
287
288#define sctp_get_tick_count() (ticks)
289
290#define SCTP_UNUSED __attribute__((unused))
291
292/*
293 * Functions
294 */
295/* Mbuf manipulation and access macros  */
296#define SCTP_BUF_LEN(m) (m->m_len)
297#define SCTP_BUF_NEXT(m) (m->m_next)
298#define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
299#define SCTP_BUF_RESV_UF(m, size) m->m_data += size
300#define SCTP_BUF_AT(m, size) m->m_data + size
301#define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
302#define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
303#define SCTP_BUF_TYPE(m) (m->m_type)
304#define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
305#define SCTP_BUF_PREPEND	M_PREPEND
306
307#define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
308                                     MH_ALIGN(m, len); \
309                                  } else if ((m->m_flags & M_EXT) == 0) { \
310                                     M_ALIGN(m, len); \
311                                  }
312
313/* We make it so if you have up to 4 threads
314 * writing based on the default size of
315 * the packet log 65 k, that would be
316 * 4 16k packets before we would hit
317 * a problem.
318 */
319#define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
320
321/*************************/
322/*      MTU              */
323/*************************/
324#define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) ((struct ifnet *)ifn)->if_mtu
325#define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((uint32_t)((rt != NULL) ? rt->rt_mtu : 0))
326#define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) ((sctp_ifn->ifn_p != NULL) ? ((struct ifnet *)(sctp_ifn->ifn_p))->if_mtu : 0)
327#define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
328                                              if (rt != NULL) \
329                                                 rt->rt_mtu = mtu; \
330                                           } while(0)
331
332/* (de-)register interface event notifications */
333#define SCTP_REGISTER_INTERFACE(ifhandle, af)
334#define SCTP_DEREGISTER_INTERFACE(ifhandle, af)
335
336
337/*************************/
338/* These are for logging */
339/*************************/
340/* return the base ext data pointer */
341#define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
342 /* return the refcnt of the data pointer */
343#define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
344/* return any buffer related flags, this is
345 * used beyond logging for apple only.
346 */
347#define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
348
349/* For BSD this just accesses the M_PKTHDR length
350 * so it operates on an mbuf with hdr flag. Other
351 * O/S's may have separate packet header and mbuf
352 * chain pointers.. thus the macro.
353 */
354#define SCTP_HEADER_TO_CHAIN(m) (m)
355#define SCTP_DETACH_HEADER_FROM_CHAIN(m)
356#define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
357#define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
358#define SCTP_RELEASE_HEADER(m)
359#define SCTP_RELEASE_PKT(m)	sctp_m_freem(m)
360#define SCTP_ENABLE_UDP_CSUM(m) do { \
361					m->m_pkthdr.csum_flags = CSUM_UDP; \
362					m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \
363				} while (0)
364
365#define SCTP_GET_PKT_VRFID(m, vrf_id)  ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
366
367
368
369/* Attach the chain of data into the sendable packet. */
370#define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
371                                                 pak = m; \
372                                                 pak->m_pkthdr.len = packet_length; \
373                         } while(0)
374
375/* Other m_pkthdr type things */
376#define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0)
377#define SCTP_IS_IT_LOOPBACK(m) ((m->m_flags & M_PKTHDR) && ((m->m_pkthdr.rcvif == NULL) || (m->m_pkthdr.rcvif->if_type == IFT_LOOP)))
378
379
380/* This converts any input packet header
381 * into the chain of data holders, for BSD
382 * its a NOP.
383 */
384
385/* get the v6 hop limit */
386#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)));
387
388/* is the endpoint v6only? */
389#define SCTP_IPV6_V6ONLY(inp)	(((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
390/* is the socket non-blocking? */
391#define SCTP_SO_IS_NBIO(so)	((so)->so_state & SS_NBIO)
392#define SCTP_SET_SO_NBIO(so)	((so)->so_state |= SS_NBIO)
393#define SCTP_CLEAR_SO_NBIO(so)	((so)->so_state &= ~SS_NBIO)
394/* get the socket type */
395#define SCTP_SO_TYPE(so)	((so)->so_type)
396/* reserve sb space for a socket */
397#define SCTP_SORESERVE(so, send, recv)	soreserve(so, send, recv)
398/* wakeup a socket */
399#define SCTP_SOWAKEUP(so)	wakeup(&(so)->so_timeo)
400/* clear the socket buffer state */
401#define SCTP_SB_CLEAR(sb)	\
402	(sb).sb_cc = 0;		\
403	(sb).sb_mb = NULL;	\
404	(sb).sb_mbcnt = 0;
405
406#define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
407#define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
408
409/*
410 * routes, output, etc.
411 */
412typedef struct route sctp_route_t;
413typedef struct rtentry sctp_rtentry_t;
414
415/*
416 * XXX multi-FIB support was backed out in r179783 and it seems clear that the
417 * VRF support as currently in FreeBSD is not ready to support multi-FIB.
418 * It might be best to implement multi-FIB support for both v4 and v6 indepedent
419 * of VRFs and leave those to a real MPLS stack.
420 */
421#define SCTP_RTALLOC(ro, vrf_id) rtalloc_ign((struct route *)ro, 0UL)
422
423/* Future zero copy wakeup/send  function */
424#define SCTP_ZERO_COPY_EVENT(inp, so)
425/* This is re-pulse ourselves for sendbuf */
426#define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so)
427
428/*
429 * SCTP protocol specific mbuf flags.
430 */
431#define	M_NOTIFICATION		M_PROTO1	/* SCTP notification */
432
433/*
434 * IP output routines
435 */
436#define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) \
437{ \
438	int o_flgs = IP_RAWOUTPUT; \
439	struct sctp_tcb *local_stcb = stcb; \
440	if (local_stcb && \
441	    local_stcb->sctp_ep && \
442	    local_stcb->sctp_ep->sctp_socket) \
443		o_flgs |= local_stcb->sctp_ep->sctp_socket->so_options & SO_DONTROUTE; \
444	m_clrprotoflags(o_pak); \
445	result = ip_output(o_pak, NULL, ro, o_flgs, 0, NULL); \
446}
447
448#define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \
449{ \
450	struct sctp_tcb *local_stcb = stcb; \
451	m_clrprotoflags(o_pak); \
452	if (local_stcb && local_stcb->sctp_ep) \
453		result = ip6_output(o_pak, \
454				    ((struct in6pcb *)(local_stcb->sctp_ep))->in6p_outputopts, \
455				    (ro), 0, 0, ifp, NULL); \
456	else \
457		result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
458}
459
460struct mbuf *
461sctp_get_mbuf_for_msg(unsigned int space_needed,
462    int want_header, int how, int allonebuf, int type);
463
464
465/*
466 * SCTP AUTH
467 */
468#define SCTP_READ_RANDOM(buf, len)	read_random(buf, len)
469
470/* map standard crypto API names */
471#define SCTP_SHA1_CTX		SHA1_CTX
472#define SCTP_SHA1_INIT		SHA1Init
473#define SCTP_SHA1_UPDATE	SHA1Update
474#define SCTP_SHA1_FINAL(x,y)	SHA1Final((caddr_t)x, y)
475
476#define SCTP_SHA256_CTX		SHA256_CTX
477#define SCTP_SHA256_INIT	SHA256_Init
478#define SCTP_SHA256_UPDATE	SHA256_Update
479#define SCTP_SHA256_FINAL(x,y)	SHA256_Final((caddr_t)x, y)
480
481#endif
482
483#define SCTP_DECREMENT_AND_CHECK_REFCOUNT(addr) (atomic_fetchadd_int(addr, -1) == 1)
484#if defined(INVARIANTS)
485#define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
486{ \
487	int32_t oldval; \
488	oldval = atomic_fetchadd_int(addr, -val); \
489	if (oldval < val) { \
490		panic("Counter goes negative"); \
491	} \
492}
493#else
494#define SCTP_SAVE_ATOMIC_DECREMENT(addr, val) \
495{ \
496	int32_t oldval; \
497	oldval = atomic_fetchadd_int(addr, -val); \
498	if (oldval < val) { \
499		*addr = 0; \
500	} \
501}
502#endif
503