2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
7 * Copyright (c) 2008-2011, by Brad Penoff. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * a) Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * b) Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the distribution.
19 * c) Neither the name of Cisco Systems, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef __sctp_os_userspace_h__
37 #define __sctp_os_userspace_h__
40 * All the opt_xxx.h files are placed in the kernel build directory.
41 * We will place them in userspace stack build directory.
52 #include "user_environment.h"
53 typedef CRITICAL_SECTION userland_mutex_t;
63 CRITICAL_SECTION waiters_count_lock;
64 HANDLE events_[C_MAX_EVENTS];
66 void InitializeXPConditionVariable(userland_cond_t *);
67 void DeleteXPConditionVariable(userland_cond_t *);
68 int SleepXPConditionVariable(userland_cond_t *, userland_mutex_t *);
69 void WakeAllXPConditionVariable(userland_cond_t *);
70 #define InitializeConditionVariable(cond) InitializeXPConditionVariable(cond)
71 #define DeleteConditionVariable(cond) DeleteXPConditionVariable(cond)
72 #define SleepConditionVariableCS(cond, mtx, time) SleepXPConditionVariable(cond, mtx)
73 #define WakeAllConditionVariable(cond) WakeAllXPConditionVariable(cond)
75 #define DeleteConditionVariable(cond)
76 typedef CONDITION_VARIABLE userland_cond_t;
78 typedef HANDLE userland_thread_t;
79 #define ADDRESS_FAMILY unsigned __int8
82 /* VS2010 comes with stdint.h */
83 #if !defined(_MSC_VER) || (_MSC_VER >= 1600)
86 typedef unsigned __int64 uint64_t;
87 typedef unsigned __int32 uint32_t;
88 typedef __int32 int32_t;
89 typedef unsigned __int16 uint16_t;
90 typedef __int16 int16_t;
91 typedef unsigned __int8 uint8_t;
92 typedef __int8 int8_t;
94 #ifndef _SIZE_T_DEFINED
95 #typedef __int32 size_t;
97 typedef unsigned __int32 u_int;
98 typedef unsigned char u_char;
99 typedef unsigned __int16 u_short;
100 typedef unsigned __int8 sa_family_t;
101 #ifndef _SSIZE_T_DEFINED
102 typedef __int64 ssize_t;
104 #if !defined(__MINGW32__)
105 #define __func__ __FUNCTION__
108 #define EWOULDBLOCK WSAEWOULDBLOCK
111 #define EINPROGRESS WSAEINPROGRESS
114 #define EALREADY WSAEALREADY
117 #define ENOTSOCK WSAENOTSOCK
120 #define EDESTADDRREQ WSAEDESTADDRREQ
123 #define EMSGSIZE WSAEMSGSIZE
126 #define EPROTOTYPE WSAEPROTOTYPE
129 #define ENOPROTOOPT WSAENOPROTOOPT
131 #ifndef EPROTONOSUPPORT
132 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
134 #ifndef ESOCKTNOSUPPORT
135 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
138 #define EOPNOTSUPP WSAEOPNOTSUPP
141 #define ENOTSUP WSAEOPNOTSUPP
144 #define EPFNOSUPPORT WSAEPFNOSUPPORT
147 #define EAFNOSUPPORT WSAEAFNOSUPPORT
150 #define EADDRINUSE WSAEADDRINUSE
152 #ifndef EADDRNOTAVAIL
153 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
156 #define ENETDOWN WSAENETDOWN
159 #define ENETUNREACH WSAENETUNREACH
162 #define ENETRESET WSAENETRESET
165 #define ECONNABORTED WSAECONNABORTED
168 #define ECONNRESET WSAECONNRESET
171 #define ENOBUFS WSAENOBUFS
174 #define EISCONN WSAEISCONN
177 #define ENOTCONN WSAENOTCONN
180 #define ESHUTDOWN WSAESHUTDOWN
183 #define ETOOMANYREFS WSAETOOMANYREFS
186 #define ETIMEDOUT WSAETIMEDOUT
189 #define ECONNREFUSED WSAECONNREFUSED
192 #define ELOOP WSAELOOP
195 #define EHOSTDOWN WSAEHOSTDOWN
198 #define EHOSTUNREACH WSAEHOSTUNREACH
201 #define EPROCLIM WSAEPROCLIM
204 #define EUSERS WSAEUSERS
207 #define EDQUOT WSAEDQUOT
210 #define ESTALE WSAESTALE
213 #define EREMOTE WSAEREMOTE
216 typedef char* caddr_t;
218 #define bzero(buf, len) memset(buf, 0, len)
219 #define bcopy(srcKey, dstKey, len) memcpy(dstKey, srcKey, len)
221 #if defined(_MSC_VER) && (_MSC_VER < 1900) && !defined(__MINGW32__)
222 #define SCTP_SNPRINTF(data, size, format, ...) \
223 if (_snprintf_s(data, size, _TRUNCATE, format, __VA_ARGS__) < 0) { \
227 #define SCTP_SNPRINTF(data, ...) \
228 if (snprintf(data, __VA_ARGS__) < 0 ) { \
233 #define inline __inline
234 #define __inline__ __inline
235 #define MSG_EOR 0x8 /* data completes record */
236 #define MSG_DONTWAIT 0x80 /* this message should be nonblocking */
242 * The following definitions should apply iff WINVER < 0x0600
243 * but that check doesn't work in all cases. So be more pedantic...
245 #define CMSG_DATA(x) WSA_CMSG_DATA(x)
246 #define CMSG_ALIGN(x) WSA_CMSGDATA_ALIGN(x)
247 #ifndef CMSG_FIRSTHDR
248 #define CMSG_FIRSTHDR(x) WSA_CMSG_FIRSTHDR(x)
251 #define CMSG_NXTHDR(x, y) WSA_CMSG_NXTHDR(x, y)
254 #define CMSG_SPACE(x) WSA_CMSG_SPACE(x)
257 #define CMSG_LEN(x) WSA_CMSG_LEN(x)
260 /**** from sctp_os_windows.h ***************/
261 #define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
262 #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)
265 * Access to IFN's to help with src-addr-selection
267 /* This could return VOID if the index works but for BSD we provide both. */
268 #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) \
269 ((ro)->ro_rt != NULL ? (ro)->ro_rt->rt_ifp : NULL)
270 #define SCTP_ROUTE_HAS_VALID_IFN(ro) \
271 ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
272 /******************************************/
274 #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles... TODO use routing socket to determine */
277 #define LITTLE_ENDIAN 0
278 #ifdef WORDS_BIGENDIAN
279 #define BYTE_ORDER BIG_ENDIAN
281 #define BYTE_ORDER LITTLE_ENDIAN
284 #else /* !defined(Userspace_os_Windows) */
285 #include <sys/socket.h>
286 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__native_client__) || defined(__Fuchsia__)
289 typedef pthread_mutex_t userland_mutex_t;
290 typedef pthread_cond_t userland_cond_t;
291 typedef pthread_t userland_thread_t;
294 #if defined(_WIN32) || defined(__native_client__)
298 #define random() rand()
299 #define srandom(s) srand(s)
301 #define timeradd(tvp, uvp, vvp) \
303 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
304 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
305 if ((vvp)->tv_usec >= 1000000) { \
307 (vvp)->tv_usec -= 1000000; \
311 #define timersub(tvp, uvp, vvp) \
313 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
314 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
315 if ((vvp)->tv_usec < 0) { \
317 (vvp)->tv_usec += 1000000; \
321 /*#include <packon.h>
322 #pragma pack(push, 1)*/
324 u_char ip_hl:4, ip_v:4;
332 #define IP_OFFMASK 0x1fff
336 struct in_addr ip_src, ip_dst;
340 struct ifaddrs *ifa_next;
342 unsigned int ifa_flags;
343 struct sockaddr *ifa_addr;
344 struct sockaddr *ifa_netmask;
345 struct sockaddr *ifa_dstaddr;
365 uint16_t ifam_msglen;
366 unsigned char ifam_version;
367 unsigned char ifam_type;
371 uint32_t ifam_metric;
381 unsigned int ifk_module_id;
382 unsigned int ifk_type;
391 int Win_getifaddrs(struct ifaddrs**);
392 #define getifaddrs(interfaces) (int)Win_getifaddrs(interfaces)
393 int win_if_nametoindex(const char *);
394 #define if_nametoindex(x) win_if_nametoindex(x)
397 #define mtx_lock(arg1)
398 #define mtx_unlock(arg1)
399 #define mtx_assert(arg1,arg2)
400 #define MA_OWNED 7 /* sys/mutex.h typically on FreeBSD */
401 #if !defined(__FreeBSD__)
402 struct mtx {int dummy;};
403 #if !defined(__NetBSD__)
404 struct selinfo {int dummy;};
406 struct sx {int dummy;};
412 /* #include <sys/param.h> in FreeBSD defines MSIZE */
413 /* #include <sys/ktr.h> */
414 /* #include <sys/systm.h> */
415 #if defined(HAVE_SYS_QUEUE_H)
416 #include <sys/queue.h>
418 #include <user_queue.h>
420 #include <user_malloc.h>
421 /* #include <sys/kernel.h> */
422 /* #include <sys/sysctl.h> */
423 /* #include <sys/protosw.h> */
424 /* on FreeBSD, this results in a redefintion of SOCK(BUF)_(UN)LOCK and
425 * uknown type of struct mtx for sb_mtx in struct sockbuf */
426 #include "user_socketvar.h" /* MALLOC_DECLARE's M_PCB. Replacement for sys/socketvar.h */
427 /* #include <sys/jail.h> */
428 /* #include <sys/sysctl.h> */
429 #include <user_environment.h>
430 #include <user_atomic.h>
431 #include <user_mbuf.h>
432 /* #include <sys/uio.h> */
433 /* #include <sys/lock.h> */
434 #if defined(__FreeBSD__) && !defined(__Userspace__)
435 #include <sys/rwlock.h>
437 /* #include <sys/kthread.h> */
438 #if defined(__FreeBSD__) && !defined(__Userspace__)
439 #include <sys/priv.h>
441 /* #include <sys/random.h> */
443 /* #include <machine/cpu.h> */
445 #if defined(__APPLE__)
446 /* was a 0 byte file. needed for structs if_data(64) and net_event_data */
447 #include <net/if_var.h>
449 #if defined(__FreeBSD__)
450 #include <net/if_types.h>
451 /* #include <net/if_var.h> was a 0 byte file. causes struct mtx redefinition */
453 /* OOTB only - dummy route used at the moment. should we port route to
454 * userspace as well? */
455 /* on FreeBSD, this results in a redefintion of struct route */
456 /* #include <net/route.h> */
457 #if !defined(_WIN32) && !defined(__native_client__)
459 #include <netinet/in.h>
460 #include <netinet/in_systm.h>
461 #include <netinet/ip.h>
463 #if defined(HAVE_NETINET_IP_ICMP_H)
464 #include <netinet/ip_icmp.h>
466 #include <user_ip_icmp.h>
468 /* #include <netinet/in_pcb.h> ported to userspace */
469 #include <user_inpcb.h>
472 #include <sys/types.h>
474 #if defined(INET) || defined(INET6)
479 #include <sys/ioctl.h>
481 /* for close, etc. */
483 /* for gettimeofday */
484 #include <sys/time.h>
487 /* lots of errno's used and needed in userspace */
492 #if defined(SCTP_PROCESS_LEVEL_LOCKS) && !defined(_WIN32)
493 /* for pthread_mutex_lock, pthread_mutex_unlock, etc. */
498 #include <netipsec/ipsec.h>
499 #include <netipsec/key.h>
503 #if defined(__FreeBSD__)
504 #include <sys/domain.h>
507 #include <netipsec/ipsec6.h>
510 #include <netinet/ip6.h>
512 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(_WIN32)
513 #include "user_ip6_var.h"
515 #include <netinet6/ip6_var.h>
517 #if defined(__FreeBSD__)
518 #include <netinet6/in6_pcb.h>
519 #include <netinet6/ip6protosw.h>
520 /* #include <netinet6/nd6.h> was a 0 byte file */
521 #include <netinet6/scope6_var.h>
525 #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
526 #include <sys/file.h>
527 #include <sys/filedesc.h>
530 #include "netinet/sctp_sha1.h"
532 #if defined(__FreeBSD__) && !defined(__Userspace__)
533 #include <netinet/ip_options.h>
536 #define SCTP_PRINTF(...) \
537 if (SCTP_BASE_VAR(debug_printf)) { \
538 SCTP_BASE_VAR(debug_printf)(__VA_ARGS__); \
541 /* Declare all the malloc names for all the various mallocs */
542 MALLOC_DECLARE(SCTP_M_MAP);
543 MALLOC_DECLARE(SCTP_M_STRMI);
544 MALLOC_DECLARE(SCTP_M_STRMO);
545 MALLOC_DECLARE(SCTP_M_ASC_ADDR);
546 MALLOC_DECLARE(SCTP_M_ASC_IT);
547 MALLOC_DECLARE(SCTP_M_AUTH_CL);
548 MALLOC_DECLARE(SCTP_M_AUTH_KY);
549 MALLOC_DECLARE(SCTP_M_AUTH_HL);
550 MALLOC_DECLARE(SCTP_M_AUTH_IF);
551 MALLOC_DECLARE(SCTP_M_STRESET);
552 MALLOC_DECLARE(SCTP_M_CMSG);
553 MALLOC_DECLARE(SCTP_M_COPYAL);
554 MALLOC_DECLARE(SCTP_M_VRF);
555 MALLOC_DECLARE(SCTP_M_IFA);
556 MALLOC_DECLARE(SCTP_M_IFN);
557 MALLOC_DECLARE(SCTP_M_TIMW);
558 MALLOC_DECLARE(SCTP_M_MVRF);
559 MALLOC_DECLARE(SCTP_M_ITER);
560 MALLOC_DECLARE(SCTP_M_SOCKOPT);
562 #if defined(SCTP_LOCAL_TRACE_BUF)
564 #define SCTP_GET_CYCLECOUNT get_cyclecount()
565 #define SCTP_CTR6 sctp_log_trace
568 #define SCTP_CTR6 CTR6
571 /* Empty ktr statement for _Userspace__ (similar to what is done for mac) */
572 #define CTR6(m, d, p1, p2, p3, p4, p5, p6)
576 #define SCTP_BASE_INFO(__m) system_base_info.sctppcbinfo.__m
577 #define SCTP_BASE_STATS system_base_info.sctpstat
578 #define SCTP_BASE_STAT(__m) system_base_info.sctpstat.__m
579 #define SCTP_BASE_SYSCTL(__m) system_base_info.sctpsysctl.__m
580 #define SCTP_BASE_VAR(__m) system_base_info.__m
585 #if !defined(__APPLE__)
586 #define USER_ADDR_NULL (NULL) /* FIX ME: temp */
589 #include <netinet/sctp_constants.h>
590 #if defined(SCTP_DEBUG)
591 #define SCTPDBG(level, ...) \
594 if (SCTP_BASE_SYSCTL(sctp_debug_on) & level) { \
595 SCTP_PRINTF(__VA_ARGS__); \
599 #define SCTPDBG_ADDR(level, addr) \
602 if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) { \
603 sctp_print_address(addr); \
608 #define SCTPDBG(level, ...)
609 #define SCTPDBG_ADDR(level, addr)
612 #ifdef SCTP_LTRACE_CHUNKS
613 #define SCTP_LTRACE_CHK(a, b, c, d) if(sctp_logging_level & SCTP_LTRACE_CHUNK_ENABLE) CTR6(KTR_SUBSYS, "SCTP:%d[%d]:%x-%x-%x-%x", SCTP_LOG_CHUNK_PROC, 0, a, b, c, d)
615 #define SCTP_LTRACE_CHK(a, b, c, d)
618 #ifdef SCTP_LTRACE_ERRORS
619 #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
620 if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
621 SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
622 (void *)m, (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
623 #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
624 if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
625 SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
626 (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
628 #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
629 #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
634 * Local address and interface list handling
636 #define SCTP_MAX_VRF_ID 0
637 #define SCTP_SIZE_OF_VRF_HASH 3
638 #define SCTP_IFNAMSIZ IFNAMSIZ
639 #define SCTP_DEFAULT_VRFID 0
640 #define SCTP_VRF_ADDR_HASH_SIZE 16
641 #define SCTP_VRF_IFN_HASH_SIZE 3
642 #define SCTP_INIT_VRF_TABLEID(vrf)
645 #define SCTP_IFN_IS_IFT_LOOP(ifn) (strncmp((ifn)->ifn_name, "lo", 2) == 0)
647 /* #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) */
648 /* only used in IPv6 scenario, which isn't supported yet */
649 #define SCTP_ROUTE_IS_REAL_LOOP(ro) 0
652 * Access to IFN's to help with src-addr-selection
654 /* This could return VOID if the index works but for BSD we provide both. */
655 #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
656 #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles... TODO use routing socket to determine */
657 #define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
661 * general memory allocation
663 #define SCTP_MALLOC(var, type, size, name) \
665 MALLOC(var, type, size, name, M_NOWAIT); \
668 #define SCTP_FREE(var, type) FREE(var, type)
670 #define SCTP_MALLOC_SONAME(var, type, size) \
672 MALLOC(var, type, size, M_SONAME, (M_WAITOK | M_ZERO)); \
675 #define SCTP_FREE_SONAME(var) FREE(var, M_SONAME)
677 #define SCTP_PROCESS_STRUCT struct proc *
680 * zone allocation functions
684 #if defined(SCTP_SIMPLE_ALLOCATOR)
685 /*typedef size_t sctp_zone_t;*/
686 #define SCTP_ZONE_INIT(zone, name, size, number) { \
690 /* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
691 #define SCTP_ZONE_GET(zone, type) \
692 (type *)malloc(zone);
695 /* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
696 #define SCTP_ZONE_FREE(zone, element) { \
700 #define SCTP_ZONE_DESTROY(zone)
703 Compiling & linking notes: Needs libumem, which has been placed in ./user_lib
704 All userspace header files are in ./user_include. Makefile will need the
707 LDFLAGS = -L./user_lib -R./user_lib -lumem
709 #include "user_include/umem.h"
711 /* __Userspace__ SCTP_ZONE_INIT: initialize the zone */
714 No equivalent function to uma_zone_set_max added yet. (See SCTP_ZONE_INIT in sctp_os_bsd.h
715 for reference). It may not be required as mentioned in
716 http://nixdoc.net/man-pages/FreeBSD/uma_zalloc.9.html that
717 max limits may not enforced on systems with more than one CPU.
719 #define SCTP_ZONE_INIT(zone, name, size, number) { \
720 zone = umem_cache_create(name, size, 0, NULL, NULL, NULL, NULL, NULL, 0); \
723 /* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
724 #define SCTP_ZONE_GET(zone, type) \
725 (type *)umem_cache_alloc(zone, UMEM_DEFAULT);
728 /* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
729 #define SCTP_ZONE_FREE(zone, element) \
730 umem_cache_free(zone, element);
733 /* __Userspace__ SCTP_ZONE_DESTROY: destroy the zone */
734 #define SCTP_ZONE_DESTROY(zone) \
735 umem_cache_destroy(zone);
739 * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland.
741 void *sctp_hashinit_flags(int elements, struct malloc_type *type,
742 u_long *hashmask, int flags);
744 sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
747 sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
750 #define HASH_NOWAIT 0x00000001
751 #define HASH_WAITOK 0x00000002
753 /* M_PCB is MALLOC_DECLARE'd in sys/socketvar.h */
754 #define SCTP_HASH_INIT(size, hashmark) sctp_hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
756 #define SCTP_HASH_FREE(table, hashmark) sctp_hashdestroy(table, M_PCB, hashmark)
758 #define SCTP_HASH_FREE_DESTROY(table, hashmark) sctp_hashfreedestroy(table, M_PCB, hashmark)
759 #define SCTP_M_COPYM m_copym
765 * user_sctp_callout.h has typedef struct sctp_callout sctp_os_timer_t;
766 * which is used in the timer related functions such as
767 * SCTP_OS_TIMER_INIT etc.
769 #include <netinet/sctp_callout.h>
771 /* __Userspace__ Creating a receive thread */
772 #include <user_recv_thread.h>
774 /*__Userspace__ defining KTR_SUBSYS 1 as done in sctp_os_macosx.h */
777 /* The packed define for 64 bit platforms */
779 #define SCTP_PACKED __attribute__((packed))
780 #define SCTP_UNUSED __attribute__((unused))
789 /* Mbuf manipulation and access macros */
790 #define SCTP_BUF_LEN(m) (m->m_len)
791 #define SCTP_BUF_NEXT(m) (m->m_next)
792 #define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
793 #define SCTP_BUF_RESV_UF(m, size) m->m_data += size
794 #define SCTP_BUF_AT(m, size) m->m_data + size
795 #define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
796 #define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
797 #define SCTP_BUF_TYPE(m) (m->m_type)
798 #define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
799 #define SCTP_BUF_PREPEND M_PREPEND
801 #define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
803 } else if ((m->m_flags & M_EXT) == 0) { \
808 #define SCTP_SNPRINTF(data, ...) \
809 if (snprintf(data, __VA_ARGS__) < 0) { \
814 /* We make it so if you have up to 4 threads
815 * writting based on the default size of
816 * the packet log 65 k, that would be
817 * 4 16k packets before we would hit
820 #define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
824 * routes, output, etc.
827 typedef struct sctp_route sctp_route_t;
828 typedef struct sctp_rtentry sctp_rtentry_t;
830 static inline void sctp_userspace_rtalloc(sctp_route_t *ro)
832 if (ro->ro_rt != NULL) {
833 ro->ro_rt->rt_refcnt++;
837 ro->ro_rt = (sctp_rtentry_t *) malloc(sizeof(sctp_rtentry_t));
838 if (ro->ro_rt == NULL)
842 memset(ro->ro_rt, 0, sizeof(sctp_rtentry_t));
843 ro->ro_rt->rt_refcnt = 1;
846 /* TODO set this based on the ro->ro_dst, looking up MTU with routing socket */
848 if (userspace_rawroute == -1) {
849 userspace_rawroute = socket(AF_ROUTE, SOCK_RAW, 0);
850 if (userspace_rawroute == -1)
854 ro->ro_rt->rt_rmx.rmx_mtu = 1500; /* FIXME temporary solution */
856 /* TODO enable the ability to obtain interface index of route for
857 * SCTP_GET_IF_INDEX_FROM_ROUTE macro.
860 #define SCTP_RTALLOC(ro, vrf_id, fibnum) sctp_userspace_rtalloc((sctp_route_t *)ro)
862 /* dummy rtfree needed once user_route.h is included */
863 static inline void sctp_userspace_rtfree(sctp_rtentry_t *rt)
868 if(--rt->rt_refcnt > 0) {
873 #define rtfree(arg1) sctp_userspace_rtfree(arg1)
876 /*************************/
878 /*************************/
879 int sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af);
881 #define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) sctp_userspace_get_mtu_from_ifn(ifn_index, af)
883 #define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0)
885 #define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) sctp_userspace_get_mtu_from_ifn(if_nametoindex(((struct ifaddrs *) (sctp_ifn))->ifa_name), AF_INET)
887 #define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
889 rt->rt_rmx.rmx_mtu = mtu; \
893 /*************************/
894 /* These are for logging */
895 /*************************/
896 /* return the base ext data pointer */
897 #define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
898 /* return the refcnt of the data pointer */
899 #define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
900 /* return any buffer related flags, this is
901 * used beyond logging for apple only.
903 #define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
905 /* For BSD this just accesses the M_PKTHDR length
906 * so it operates on an mbuf with hdr flag. Other
907 * O/S's may have seperate packet header and mbuf
908 * chain pointers.. thus the macro.
910 #define SCTP_HEADER_TO_CHAIN(m) (m)
911 #define SCTP_DETACH_HEADER_FROM_CHAIN(m)
912 #define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
913 #define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
914 #define SCTP_RELEASE_HEADER(m)
915 #define SCTP_RELEASE_PKT(m) sctp_m_freem(m)
917 #define SCTP_GET_PKT_VRFID(m, vrf_id) ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
921 /* Attach the chain of data into the sendable packet. */
922 #define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
924 pak->m_pkthdr.len = packet_length; \
927 /* Other m_pkthdr type things */
928 /* FIXME need real definitions */
929 #define SCTP_IS_IT_BROADCAST(dst, m) 0
930 /* OOTB only #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0) BSD def */
931 #define SCTP_IS_IT_LOOPBACK(m) 0
932 /* OOTB ONLY #define SCTP_IS_IT_LOOPBACK(m) ((m->m_flags & M_PKTHDR) && ((m->m_pkthdr.rcvif == NULL) || (m->m_pkthdr.rcvif->if_type == IFT_LOOP))) BSD def */
935 /* This converts any input packet header
936 * into the chain of data holders, for BSD
940 /* get the v6 hop limit */
941 #define SCTP_GET_HLIM(inp, ro) 128
942 #define IPv6_HOP_LIMIT 128
944 /* is the endpoint v6only? */
945 #define SCTP_IPV6_V6ONLY(sctp_inpcb) ((sctp_inpcb)->ip_inp.inp.inp_flags & IN6P_IPV6_V6ONLY)
946 /* is the socket non-blocking? */
947 #define SCTP_SO_IS_NBIO(so) ((so)->so_state & SS_NBIO)
948 #define SCTP_SET_SO_NBIO(so) ((so)->so_state |= SS_NBIO)
949 #define SCTP_CLEAR_SO_NBIO(so) ((so)->so_state &= ~SS_NBIO)
950 /* get the socket type */
951 #define SCTP_SO_TYPE(so) ((so)->so_type)
953 /* reserve sb space for a socket */
954 #define SCTP_SORESERVE(so, send, recv) soreserve(so, send, recv)
956 /* wakeup a socket */
957 #define SCTP_SOWAKEUP(so) wakeup(&(so)->so_timeo, so)
958 /* clear the socket buffer state */
959 #define SCTP_SB_CLEAR(sb) \
964 #define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
965 #define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
967 #define SCTP_READ_RANDOM(buf, len) read_random(buf, len)
969 #define SCTP_SHA1_CTX struct sctp_sha1_context
970 #define SCTP_SHA1_INIT sctp_sha1_init
971 #define SCTP_SHA1_UPDATE sctp_sha1_update
972 #define SCTP_SHA1_FINAL(x,y) sctp_sha1_final((unsigned char *)x, y)
974 /* start OOTB only stuff */
975 /* TODO IFT_LOOP is in net/if_types.h on Linux */
976 #define IFT_LOOP 0x18
985 #define PRU_FLUSH_RD SHUT_RD
986 #define PRU_FLUSH_WR SHUT_WR
987 #define PRU_FLUSH_RDWR SHUT_RDWR
989 /* netinet/ip_var.h defintions are behind an if defined for _KERNEL on FreeBSD */
990 #define IP_RAWOUTPUT 0x2
993 /* end OOTB only stuff */
996 struct sockaddr_conn {
997 #ifdef HAVE_SCONN_LEN
999 uint8_t sconn_family;
1001 uint16_t sconn_family;
1003 uint16_t sconn_port;
1007 typedef void *(*start_routine_t)(void *);
1010 sctp_userspace_thread_create(userland_thread_t *thread, start_routine_t start_routine);
1013 sctp_userspace_set_threadname(const char *name);
1016 * SCTP protocol specific mbuf flags.
1018 #define M_NOTIFICATION M_PROTO5 /* SCTP notification */
1021 * IP output routines
1024 /* Defining SCTP_IP_ID macro.
1025 In netinet/ip_output.c, we have u_short ip_id;
1026 In netinet/ip_var.h, we have extern u_short ip_id; (enclosed within _KERNEL_)
1027 See static __inline uint16_t ip_newid(void) in netinet/ip_var.h
1029 #define SCTP_IP_ID(inp) (ip_id)
1031 /* need sctphdr to get port in SCTP_IP_OUTPUT. sctphdr defined in sctp.h */
1032 #include <netinet/sctp.h>
1033 extern void sctp_userspace_ip_output(int *result, struct mbuf *o_pak,
1034 sctp_route_t *ro, void *stcb,
1037 #define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) sctp_userspace_ip_output(&result, o_pak, ro, stcb, vrf_id);
1040 extern void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak,
1041 struct route_in6 *ro, void *stcb,
1043 #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) sctp_userspace_ip6_output(&result, o_pak, ro, stcb, vrf_id);
1049 #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \
1051 if (stcb && stcb->sctp_ep) \
1052 result = ip6_output(o_pak, \
1053 ((struct inpcb *)(stcb->sctp_ep))->in6p_outputopts, \
1054 (ro), 0, 0, ifp, NULL); \
1056 result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
1061 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header, int how, int allonebuf, int type);
1064 /* with the current included files, this is defined in Linux but
1065 * in FreeBSD, it is behind a _KERNEL in sys/socket.h ...
1067 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__native_client__)
1068 /* stolen from /usr/include/sys/socket.h */
1069 #define CMSG_ALIGN(n) _ALIGN(n)
1070 #elif defined(__NetBSD__)
1071 #define CMSG_ALIGN(n) (((n) + __ALIGNBYTES) & ~__ALIGNBYTES)
1072 #elif defined(__APPLE__)
1073 #if !defined(__DARWIN_ALIGNBYTES)
1074 #define __DARWIN_ALIGNBYTES (sizeof(__darwin_size_t) - 1)
1077 #if !defined(__DARWIN_ALIGN)
1078 #define __DARWIN_ALIGN(p) ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES) &~ __DARWIN_ALIGNBYTES)
1081 #if !defined(__DARWIN_ALIGNBYTES32)
1082 #define __DARWIN_ALIGNBYTES32 (sizeof(__uint32_t) - 1)
1085 #if !defined(__DARWIN_ALIGN32)
1086 #define __DARWIN_ALIGN32(p) ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES32) &~ __DARWIN_ALIGNBYTES32)
1088 #define CMSG_ALIGN(n) __DARWIN_ALIGN32(n)
1092 SCTP_PRINTF("%s:%d at %s\n", __FILE__, __LINE__ , __func__); \
1096 #define timevalsub(tp1, tp2) \
1098 (tp1)->tv_sec -= (tp2)->tv_sec; \
1099 (tp1)->tv_usec -= (tp2)->tv_usec; \
1100 if ((tp1)->tv_usec < 0) { \
1102 (tp1)->tv_usec += 1000000; \
1107 #if defined(__linux__)
1108 #if !defined(TAILQ_FOREACH_SAFE)
1109 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
1110 for ((var) = ((head)->tqh_first); \
1111 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
1114 #if !defined(LIST_FOREACH_SAFE)
1115 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
1116 for ((var) = ((head)->lh_first); \
1117 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
1121 #if defined(__DragonFly__)
1122 #define TAILQ_FOREACH_SAFE TAILQ_FOREACH_MUTABLE
1123 #define LIST_FOREACH_SAFE LIST_FOREACH_MUTABLE
1126 #if defined(__native_client__)
1127 #define timercmp(tvp, uvp, cmp) \
1128 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1129 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1130 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1133 #define SCTP_IS_LISTENING(inp) ((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) != 0)
1135 #if defined(__APPLE__) || defined(__DragonFly__) || defined(__linux__) || defined(__native_client__) || defined(__NetBSD__) || defined(_WIN32) || defined(__Fuchsia__)
1137 timingsafe_bcmp(const void *, const void *, size_t);