2 * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5 * Copyright (c) 2008-2011, by Brad Penoff. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * a) Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * b) Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the distribution.
17 * c) Neither the name of Cisco Systems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef __sctp_os_userspace_h__
35 #define __sctp_os_userspace_h__
38 * All the opt_xxx.h files are placed in the kernel build directory.
39 * We will place them in userspace stack build directory.
44 #if defined(__Userspace_os_Windows)
50 #include "user_environment.h"
51 typedef CRITICAL_SECTION userland_mutex_t;
61 CRITICAL_SECTION waiters_count_lock;
62 HANDLE events_[C_MAX_EVENTS];
64 void InitializeXPConditionVariable(userland_cond_t *);
65 void DeleteXPConditionVariable(userland_cond_t *);
66 int SleepXPConditionVariable(userland_cond_t *, userland_mutex_t *);
67 void WakeAllXPConditionVariable(userland_cond_t *);
68 #define InitializeConditionVariable(cond) InitializeXPConditionVariable(cond)
69 #define DeleteConditionVariable(cond) DeleteXPConditionVariable(cond)
70 #define SleepConditionVariableCS(cond, mtx, time) SleepXPConditionVariable(cond, mtx)
71 #define WakeAllConditionVariable(cond) WakeAllXPConditionVariable(cond)
73 #define DeleteConditionVariable(cond)
74 typedef CONDITION_VARIABLE userland_cond_t;
76 typedef HANDLE userland_thread_t;
77 #define ADDRESS_FAMILY unsigned __int8
80 /* VS2010 comes with stdint.h */
84 #define uint64_t unsigned __int64
85 #define uint32_t unsigned __int32
86 #define int32_t __int32
87 #define uint16_t unsigned __int16
88 #define int16_t __int16
89 #define uint8_t unsigned __int8
92 #ifndef _SIZE_T_DEFINED
93 #define size_t __int32
95 #define u_long unsigned __int64
96 #define u_int unsigned __int32
97 #define u_int32_t unsigned __int32
98 #define u_int16_t unsigned __int16
99 #define u_int8_t unsigned __int8
100 #define u_char unsigned char
101 #define n_short unsigned __int16
102 #define u_short unsigned __int16
103 #define n_time unsigned __int32
104 #define sa_family_t unsigned __int8
105 #define ssize_t __int64
106 #define __func__ __FUNCTION__
109 #define EWOULDBLOCK WSAEWOULDBLOCK
112 #define EINPROGRESS WSAEINPROGRESS
115 #define EALREADY WSAEALREADY
118 #define ENOTSOCK WSAENOTSOCK
121 #define EDESTADDRREQ WSAEDESTADDRREQ
124 #define EMSGSIZE WSAEMSGSIZE
127 #define EPROTOTYPE WSAEPROTOTYPE
130 #define ENOPROTOOPT WSAENOPROTOOPT
132 #ifndef EPROTONOSUPPORT
133 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
135 #ifndef ESOCKTNOSUPPORT
136 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
139 #define EOPNOTSUPP WSAEOPNOTSUPP
142 #define ENOTSUP WSAEOPNOTSUPP
145 #define EPFNOSUPPORT WSAEPFNOSUPPORT
148 #define EAFNOSUPPORT WSAEAFNOSUPPORT
151 #define EADDRINUSE WSAEADDRINUSE
153 #ifndef EADDRNOTAVAIL
154 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
157 #define ENETDOWN WSAENETDOWN
160 #define ENETUNREACH WSAENETUNREACH
163 #define ENETRESET WSAENETRESET
166 #define ECONNABORTED WSAECONNABORTED
169 #define ECONNRESET WSAECONNRESET
172 #define ENOBUFS WSAENOBUFS
175 #define EISCONN WSAEISCONN
178 #define ENOTCONN WSAENOTCONN
181 #define ESHUTDOWN WSAESHUTDOWN
184 #define ETOOMANYREFS WSAETOOMANYREFS
187 #define ETIMEDOUT WSAETIMEDOUT
190 #define ECONNREFUSED WSAECONNREFUSED
193 #define ELOOP WSAELOOP
196 #define EHOSTDOWN WSAEHOSTDOWN
199 #define EHOSTUNREACH WSAEHOSTUNREACH
202 #define EPROCLIM WSAEPROCLIM
205 #define EUSERS WSAEUSERS
208 #define EDQUOT WSAEDQUOT
211 #define ESTALE WSAESTALE
214 #define EREMOTE WSAEREMOTE
217 typedef char* caddr_t;
219 #define bzero(buf, len) memset(buf, 0, len)
220 #define bcopy(srcKey, dstKey, len) memcpy(dstKey, srcKey, len)
221 #define snprintf(data, size, format, ...) _snprintf_s(data, size, _TRUNCATE, format, __VA_ARGS__)
222 #define inline __inline
223 #define __inline__ __inline
224 #define MSG_EOR 0x8 /* data completes record */
225 #define MSG_DONTWAIT 0x80 /* this message should be nonblocking */
231 * The following definitions should apply iff WINVER < 0x0600
232 * but that check doesn't work in all cases. So be more pedantic...
234 #define CMSG_DATA(x) WSA_CMSG_DATA(x)
235 #define CMSG_ALIGN(x) WSA_CMSGDATA_ALIGN(x)
236 #ifndef CMSG_FIRSTHDR
237 #define CMSG_FIRSTHDR(x) WSA_CMSG_FIRSTHDR(x)
240 #define CMSG_NXTHDR(x, y) WSA_CMSG_NXTHDR(x, y)
243 #define CMSG_SPACE(x) WSA_CMSG_SPACE(x)
246 #define CMSG_LEN(x) WSA_CMSG_LEN(x)
249 /**** from sctp_os_windows.h ***************/
250 #define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
251 #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)
254 * Access to IFN's to help with src-addr-selection
256 /* This could return VOID if the index works but for BSD we provide both. */
257 #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) \
258 ((ro)->ro_rt != NULL ? (ro)->ro_rt->rt_ifp : NULL)
259 #define SCTP_ROUTE_HAS_VALID_IFN(ro) \
260 ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
261 /******************************************/
263 #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles... TODO use routing socket to determine */
266 #define LITTLE_ENDIAN 0
267 #ifdef WORDS_BIGENDIAN
268 #define BYTE_ORDER BIG_ENDIAN
270 #define BYTE_ORDER LITTLE_ENDIAN
273 #else /* !defined(Userspace_os_Windows) */
274 #include <sys/cdefs.h> /* needed? added from old __FreeBSD__ */
275 #include <sys/socket.h>
276 #if defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_Linux) || defined(__Userspace_os_NetBSD) || defined(__Userspace_os_OpenBSD) || defined(__Userspace_os_NaCl)
279 typedef pthread_mutex_t userland_mutex_t;
280 typedef pthread_cond_t userland_cond_t;
281 typedef pthread_t userland_thread_t;
284 #if defined(__Userspace_os_Windows) || defined(__Userspace_os_NaCl)
288 #define random() rand()
289 #define srandom(s) srand(s)
291 #define timeradd(tvp, uvp, vvp) \
293 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
294 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
295 if ((vvp)->tv_usec >= 1000000) { \
297 (vvp)->tv_usec -= 1000000; \
301 #define timersub(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 < 0) { \
307 (vvp)->tv_usec += 1000000; \
311 /*#include <packon.h>
312 #pragma pack(push, 1)*/
314 u_char ip_hl:4, ip_v:4;
322 #define IP_OFFMASK 0x1fff
326 struct in_addr ip_src, ip_dst;
330 struct ifaddrs *ifa_next;
332 unsigned int ifa_flags;
333 struct sockaddr *ifa_addr;
334 struct sockaddr *ifa_netmask;
335 struct sockaddr *ifa_dstaddr;
355 uint16_t ifam_msglen;
356 unsigned char ifam_version;
357 unsigned char ifam_type;
361 uint32_t ifam_metric;
371 unsigned int ifk_module_id;
372 unsigned int ifk_type;
382 struct sockaddr ifru_addr;
383 struct sockaddr ifru_dstaddr;
384 struct sockaddr ifru_broadaddr;
392 struct ifdevmtu ifru_devmtu;
393 struct ifkpi ifru_kpi;
394 uint32_t ifru_wake_flags;
396 #define ifr_addr ifr_ifru.ifru_addr
397 #define ifr_dstaddr ifr_ifru.ifru_dstaddr
398 #define ifr_broadaddr ifr_ifru.ifru_broadaddr
399 #define ifr_flags ifr_ifru.ifru_flags[0]
400 #define ifr_prevflags ifr_ifru.ifru_flags[1]
401 #define ifr_metric ifr_ifru.ifru_metric
402 #define ifr_mtu ifr_ifru.ifru_mtu
403 #define ifr_phys ifr_ifru.ifru_phys
404 #define ifr_media ifr_ifru.ifru_media
405 #define ifr_data ifr_ifru.ifru_data
406 #define ifr_devmtu ifr_ifru.ifru_devmtu
407 #define ifr_intval ifr_ifru.ifru_intval
408 #define ifr_kpi ifr_ifru.ifru_kpi
409 #define ifr_wake_flags ifr_ifru.ifru_wake_flags
414 #if defined(__Userspace_os_Windows)
415 int Win_getifaddrs(struct ifaddrs**);
416 #define getifaddrs(interfaces) (int)Win_getifaddrs(interfaces)
417 int win_if_nametoindex(const char *);
418 #define if_nametoindex(x) win_if_nametoindex(x)
421 #define mtx_lock(arg1)
422 #define mtx_unlock(arg1)
423 #define mtx_assert(arg1,arg2)
424 #define MA_OWNED 7 /* sys/mutex.h typically on FreeBSD */
425 #if !defined(__Userspace_os_FreeBSD)
426 struct mtx {int dummy;};
427 #if !defined(__Userspace_os_NetBSD)
428 struct selinfo {int dummy;};
430 struct sx {int dummy;};
435 /* #include <sys/param.h> in FreeBSD defines MSIZE */
436 /* #include <sys/ktr.h> */
437 /* #include <sys/systm.h> */
438 #if defined(__Userspace_os_Windows)
439 #include <user_queue.h>
441 #include <sys/queue.h>
443 #include <user_malloc.h>
444 /* #include <sys/kernel.h> */
445 /* #include <sys/sysctl.h> */
446 /* #include <sys/protosw.h> */
447 /* on FreeBSD, this results in a redefintion of SOCK(BUF)_(UN)LOCK and
448 * uknown type of struct mtx for sb_mtx in struct sockbuf */
449 #include "user_socketvar.h" /* MALLOC_DECLARE's M_PCB. Replacement for sys/socketvar.h */
450 /* #include <sys/jail.h> */
451 /* #include <sys/sysctl.h> */
452 #include <user_environment.h>
453 #include <user_atomic.h>
454 #include <user_mbuf.h>
455 /* #include <sys/uio.h> */
456 /* #include <sys/lock.h> */
457 #if defined(__FreeBSD__) && __FreeBSD_version > 602000
458 #include <sys/rwlock.h>
460 /* #include <sys/kthread.h> */
461 #if defined(__FreeBSD__) && __FreeBSD_version > 602000
462 #include <sys/priv.h>
464 /* #include <sys/random.h> */
465 /* #include <sys/limits.h> */
466 /* #include <machine/cpu.h> */
468 #if defined(__Userspace_os_Darwin)
469 /* was a 0 byte file. needed for structs if_data(64) and net_event_data */
470 #include <net/if_var.h>
472 #if defined(__Userspace_os_FreeBSD)
473 #include <net/if_types.h>
474 /* #include <net/if_var.h> was a 0 byte file. causes struct mtx redefinition */
476 /* OOTB only - dummy route used at the moment. should we port route to
477 * userspace as well? */
478 /* on FreeBSD, this results in a redefintion of struct route */
479 /* #include <net/route.h> */
480 #if !defined(__Userspace_os_Windows) && !defined(__Userspace_os_NaCl)
482 #include <netinet/in.h>
483 #include <netinet/in_systm.h>
484 #include <netinet/ip.h>
487 #if defined(__Userspace_os_Windows)
488 #include <user_ip_icmp.h>
490 #include <netinet/ip_icmp.h>
493 /* #include <netinet/in_pcb.h> ported to userspace */
494 #include <user_inpcb.h>
497 #include <sys/types.h>
498 #if !defined(__Userspace_os_Windows)
499 #if defined(INET) || defined(INET6)
504 #include <sys/ioctl.h>
506 /* for close, etc. */
510 /* lots of errno's used and needed in userspace */
515 #if defined(SCTP_PROCESS_LEVEL_LOCKS) && !defined(__Userspace_os_Windows)
516 /* for pthread_mutex_lock, pthread_mutex_unlock, etc. */
521 #include <netipsec/ipsec.h>
522 #include <netipsec/key.h>
526 #if defined(__Userspace_os_FreeBSD)
527 #include <sys/domain.h>
530 #include <netipsec/ipsec6.h>
532 #if !defined(__Userspace_os_Windows)
533 #include <netinet/ip6.h>
534 #include <netinet/icmp6.h>
536 #if defined(__Userspace_os_Darwin) || defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_Linux) || defined(__Userspace_os_NetBSD) || defined(__Userspace_os_OpenBSD) || defined(__Userspace_os_Windows)
537 #include "user_ip6_var.h"
539 #include <netinet6/ip6_var.h>
541 #if defined(__Userspace_os_FreeBSD)
542 #include <netinet6/in6_pcb.h>
543 #include <netinet6/ip6protosw.h>
544 /* #include <netinet6/nd6.h> was a 0 byte file */
545 #include <netinet6/scope6_var.h>
549 #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
550 #include <sys/file.h>
551 #include <sys/filedesc.h>
554 #include "netinet/sctp_sha1.h"
556 #if __FreeBSD_version >= 700000
557 #include <netinet/ip_options.h>
560 #define SCTP_PRINTF(...) \
561 if (SCTP_BASE_VAR(debug_printf)) { \
562 SCTP_BASE_VAR(debug_printf)(__VA_ARGS__); \
565 #if defined(__FreeBSD__)
570 /* Declare all the malloc names for all the various mallocs */
571 MALLOC_DECLARE(SCTP_M_MAP);
572 MALLOC_DECLARE(SCTP_M_STRMI);
573 MALLOC_DECLARE(SCTP_M_STRMO);
574 MALLOC_DECLARE(SCTP_M_ASC_ADDR);
575 MALLOC_DECLARE(SCTP_M_ASC_IT);
576 MALLOC_DECLARE(SCTP_M_AUTH_CL);
577 MALLOC_DECLARE(SCTP_M_AUTH_KY);
578 MALLOC_DECLARE(SCTP_M_AUTH_HL);
579 MALLOC_DECLARE(SCTP_M_AUTH_IF);
580 MALLOC_DECLARE(SCTP_M_STRESET);
581 MALLOC_DECLARE(SCTP_M_CMSG);
582 MALLOC_DECLARE(SCTP_M_COPYAL);
583 MALLOC_DECLARE(SCTP_M_VRF);
584 MALLOC_DECLARE(SCTP_M_IFA);
585 MALLOC_DECLARE(SCTP_M_IFN);
586 MALLOC_DECLARE(SCTP_M_TIMW);
587 MALLOC_DECLARE(SCTP_M_MVRF);
588 MALLOC_DECLARE(SCTP_M_ITER);
589 MALLOC_DECLARE(SCTP_M_SOCKOPT);
591 #if defined(SCTP_LOCAL_TRACE_BUF)
593 #define SCTP_GET_CYCLECOUNT get_cyclecount()
594 #define SCTP_CTR6 sctp_log_trace
597 #define SCTP_CTR6 CTR6
600 /* Empty ktr statement for _Userspace__ (similar to what is done for mac) */
601 #define CTR6(m, d, p1, p2, p3, p4, p5, p6)
605 #define SCTP_BASE_INFO(__m) system_base_info.sctppcbinfo.__m
606 #define SCTP_BASE_STATS system_base_info.sctpstat
607 #define SCTP_BASE_STAT(__m) system_base_info.sctpstat.__m
608 #define SCTP_BASE_SYSCTL(__m) system_base_info.sctpsysctl.__m
609 #define SCTP_BASE_VAR(__m) system_base_info.__m
614 #if !defined(__Userspace_os_Darwin)
615 #define USER_ADDR_NULL (NULL) /* FIX ME: temp */
618 #if defined(SCTP_DEBUG)
619 #include <netinet/sctp_constants.h>
620 #define SCTPDBG(level, ...) \
623 if (SCTP_BASE_SYSCTL(sctp_debug_on) & level) { \
624 SCTP_PRINTF(__VA_ARGS__); \
628 #define SCTPDBG_ADDR(level, addr) \
631 if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) { \
632 sctp_print_address(addr); \
637 #define SCTPDBG(level, ...)
638 #define SCTPDBG_ADDR(level, addr)
641 #ifdef SCTP_LTRACE_CHUNKS
642 #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)
644 #define SCTP_LTRACE_CHK(a, b, c, d)
647 #ifdef SCTP_LTRACE_ERRORS
648 #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
649 if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
650 SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
651 (void *)m, (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
652 #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
653 if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
654 SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
655 (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
657 #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
658 #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
663 * Local address and interface list handling
665 #define SCTP_MAX_VRF_ID 0
666 #define SCTP_SIZE_OF_VRF_HASH 3
667 #define SCTP_IFNAMSIZ IFNAMSIZ
668 #define SCTP_DEFAULT_VRFID 0
669 #define SCTP_VRF_ADDR_HASH_SIZE 16
670 #define SCTP_VRF_IFN_HASH_SIZE 3
671 #define SCTP_INIT_VRF_TABLEID(vrf)
673 #if !defined(__Userspace_os_Windows)
674 #define SCTP_IFN_IS_IFT_LOOP(ifn) (strncmp((ifn)->ifn_name, "lo", 2) == 0)
676 /* #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) */
677 /* only used in IPv6 scenario, which isn't supported yet */
678 #define SCTP_ROUTE_IS_REAL_LOOP(ro) 0
681 * Access to IFN's to help with src-addr-selection
683 /* This could return VOID if the index works but for BSD we provide both. */
684 #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
685 #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles... TODO use routing socket to determine */
686 #define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
690 * general memory allocation
692 #define SCTP_MALLOC(var, type, size, name) \
694 MALLOC(var, type, size, name, M_NOWAIT); \
697 #define SCTP_FREE(var, type) FREE(var, type)
699 #define SCTP_MALLOC_SONAME(var, type, size) \
701 MALLOC(var, type, size, M_SONAME, (M_WAITOK | M_ZERO)); \
704 #define SCTP_FREE_SONAME(var) FREE(var, M_SONAME)
706 #define SCTP_PROCESS_STRUCT struct proc *
709 * zone allocation functions
713 #if defined(SCTP_SIMPLE_ALLOCATOR)
714 /*typedef size_t sctp_zone_t;*/
715 #define SCTP_ZONE_INIT(zone, name, size, number) { \
719 /* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
720 #define SCTP_ZONE_GET(zone, type) \
721 (type *)malloc(zone);
724 /* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
725 #define SCTP_ZONE_FREE(zone, element) { \
729 #define SCTP_ZONE_DESTROY(zone)
732 Compiling & linking notes: Needs libumem, which has been placed in ./user_lib
733 All userspace header files are in ./user_include. Makefile will need the
736 LDFLAGS = -L./user_lib -R./user_lib -lumem
738 #include "user_include/umem.h"
740 /* __Userspace__ SCTP_ZONE_INIT: initialize the zone */
743 No equivalent function to uma_zone_set_max added yet. (See SCTP_ZONE_INIT in sctp_os_bsd.h
744 for reference). It may not be required as mentioned in
745 http://nixdoc.net/man-pages/FreeBSD/uma_zalloc.9.html that
746 max limits may not enforced on systems with more than one CPU.
748 #define SCTP_ZONE_INIT(zone, name, size, number) { \
749 zone = umem_cache_create(name, size, 0, NULL, NULL, NULL, NULL, NULL, 0); \
752 /* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
753 #define SCTP_ZONE_GET(zone, type) \
754 (type *)umem_cache_alloc(zone, UMEM_DEFAULT);
757 /* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
758 #define SCTP_ZONE_FREE(zone, element) \
759 umem_cache_free(zone, element);
762 /* __Userspace__ SCTP_ZONE_DESTROY: destroy the zone */
763 #define SCTP_ZONE_DESTROY(zone) \
764 umem_cache_destroy(zone);
767 /* global struct ifaddrs used in sctp_init_ifns_for_vrf getifaddrs call
768 * but references to fields are needed to persist as the vrf is queried.
769 * getifaddrs allocates memory that needs to be freed with a freeifaddrs
770 * call; this global is used to call freeifaddrs upon in sctp_pcb_finish
772 extern struct ifaddrs *g_interfaces;
776 * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland.
778 void *sctp_hashinit_flags(int elements, struct malloc_type *type,
779 u_long *hashmask, int flags);
781 sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
784 sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
787 #define HASH_NOWAIT 0x00000001
788 #define HASH_WAITOK 0x00000002
790 /* M_PCB is MALLOC_DECLARE'd in sys/socketvar.h */
791 #define SCTP_HASH_INIT(size, hashmark) sctp_hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
793 #define SCTP_HASH_FREE(table, hashmark) sctp_hashdestroy(table, M_PCB, hashmark)
795 #define SCTP_HASH_FREE_DESTROY(table, hashmark) sctp_hashfreedestroy(table, M_PCB, hashmark)
796 #define SCTP_M_COPYM m_copym
802 * user_sctp_callout.h has typedef struct sctp_callout sctp_os_timer_t;
803 * which is used in the timer related functions such as
804 * SCTP_OS_TIMER_INIT etc.
806 #include <netinet/sctp_callout.h>
808 /* __Userspace__ Creating a receive thread */
809 #include <user_recv_thread.h>
811 /*__Userspace__ defining KTR_SUBSYS 1 as done in sctp_os_macosx.h */
814 #define sctp_get_tick_count() (ticks)
816 /* The packed define for 64 bit platforms */
817 #if !defined(__Userspace_os_Windows)
818 #define SCTP_PACKED __attribute__((packed))
819 #define SCTP_UNUSED __attribute__((unused))
828 /* Mbuf manipulation and access macros */
829 #define SCTP_BUF_LEN(m) (m->m_len)
830 #define SCTP_BUF_NEXT(m) (m->m_next)
831 #define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
832 #define SCTP_BUF_RESV_UF(m, size) m->m_data += size
833 #define SCTP_BUF_AT(m, size) m->m_data + size
834 #define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
835 #define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
836 #define SCTP_BUF_TYPE(m) (m->m_type)
837 #define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
838 #define SCTP_BUF_PREPEND M_PREPEND
840 #define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
842 } else if ((m->m_flags & M_EXT) == 0) { \
846 /* We make it so if you have up to 4 threads
847 * writting based on the default size of
848 * the packet log 65 k, that would be
849 * 4 16k packets before we would hit
852 #define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
856 * routes, output, etc.
859 typedef struct sctp_route sctp_route_t;
860 typedef struct sctp_rtentry sctp_rtentry_t;
862 static inline void sctp_userspace_rtalloc(sctp_route_t *ro)
864 if (ro->ro_rt != NULL) {
865 ro->ro_rt->rt_refcnt++;
869 ro->ro_rt = (sctp_rtentry_t *) malloc(sizeof(sctp_rtentry_t));
870 if (ro->ro_rt == NULL)
874 memset(ro->ro_rt, 0, sizeof(sctp_rtentry_t));
875 ro->ro_rt->rt_refcnt = 1;
878 /* TODO set this based on the ro->ro_dst, looking up MTU with routing socket */
880 if (userspace_rawroute == -1) {
881 userspace_rawroute = socket(AF_ROUTE, SOCK_RAW, 0);
882 if (userspace_rawroute == -1)
886 ro->ro_rt->rt_rmx.rmx_mtu = 1500; /* FIXME temporary solution */
888 /* TODO enable the ability to obtain interface index of route for
889 * SCTP_GET_IF_INDEX_FROM_ROUTE macro.
892 #define SCTP_RTALLOC(ro, vrf_id) sctp_userspace_rtalloc((sctp_route_t *)ro)
894 /* dummy rtfree needed once user_route.h is included */
895 static inline void sctp_userspace_rtfree(sctp_rtentry_t *rt)
900 if(--rt->rt_refcnt > 0) {
906 #define rtfree(arg1) sctp_userspace_rtfree(arg1)
909 /*************************/
911 /*************************/
912 int sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af);
914 #define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) sctp_userspace_get_mtu_from_ifn(ifn_index, af)
916 #define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0)
918 #define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) sctp_userspace_get_mtu_from_ifn(if_nametoindex(((struct ifaddrs *) (sctp_ifn))->ifa_name), AF_INET)
920 #define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
922 rt->rt_rmx.rmx_mtu = mtu; \
925 /* (de-)register interface event notifications */
926 #define SCTP_REGISTER_INTERFACE(ifhandle, af)
927 #define SCTP_DEREGISTER_INTERFACE(ifhandle, af)
930 /*************************/
931 /* These are for logging */
932 /*************************/
933 /* return the base ext data pointer */
934 #define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
935 /* return the refcnt of the data pointer */
936 #define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
937 /* return any buffer related flags, this is
938 * used beyond logging for apple only.
940 #define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
942 /* For BSD this just accesses the M_PKTHDR length
943 * so it operates on an mbuf with hdr flag. Other
944 * O/S's may have seperate packet header and mbuf
945 * chain pointers.. thus the macro.
947 #define SCTP_HEADER_TO_CHAIN(m) (m)
948 #define SCTP_DETACH_HEADER_FROM_CHAIN(m)
949 #define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
950 #define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
951 #define SCTP_RELEASE_HEADER(m)
952 #define SCTP_RELEASE_PKT(m) sctp_m_freem(m)
953 /* UDP __Userspace__ - dummy definition */
954 #define SCTP_ENABLE_UDP_CSUM(m) m=m
956 /* #define SCTP_ENABLE_UDP_CSUM(m) do { \ */
957 /* m->m_pkthdr.csum_flags = CSUM_UDP; \ */
958 /* m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \ */
961 #define SCTP_GET_PKT_VRFID(m, vrf_id) ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
965 /* Attach the chain of data into the sendable packet. */
966 #define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
968 pak->m_pkthdr.len = packet_length; \
971 /* Other m_pkthdr type things */
972 /* FIXME need real definitions */
973 #define SCTP_IS_IT_BROADCAST(dst, m) 0
974 /* OOTB only #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0) BSD def */
975 #define SCTP_IS_IT_LOOPBACK(m) 0
976 /* 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 */
979 /* This converts any input packet header
980 * into the chain of data holders, for BSD
984 /* get the v6 hop limit */
985 #define SCTP_GET_HLIM(inp, ro) 128 /* As done for __Windows__ */
986 #define IPv6_HOP_LIMIT 128
988 /* is the endpoint v6only? */
989 #define SCTP_IPV6_V6ONLY(inp) (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
990 /* is the socket non-blocking? */
991 #define SCTP_SO_IS_NBIO(so) ((so)->so_state & SS_NBIO)
992 #define SCTP_SET_SO_NBIO(so) ((so)->so_state |= SS_NBIO)
993 #define SCTP_CLEAR_SO_NBIO(so) ((so)->so_state &= ~SS_NBIO)
994 /* get the socket type */
995 #define SCTP_SO_TYPE(so) ((so)->so_type)
997 /* reserve sb space for a socket */
998 #define SCTP_SORESERVE(so, send, recv) soreserve(so, send, recv)
1000 /* wakeup a socket */
1001 #define SCTP_SOWAKEUP(so) wakeup(&(so)->so_timeo, so)
1002 /* clear the socket buffer state */
1003 #define SCTP_SB_CLEAR(sb) \
1005 (sb).sb_mb = NULL; \
1008 #define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
1009 #define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
1011 /* Future zero copy wakeup/send function */
1012 #define SCTP_ZERO_COPY_EVENT(inp, so)
1013 /* This is re-pulse ourselves for sendbuf */
1014 #define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so)
1016 #define SCTP_READ_RANDOM(buf, len) read_random(buf, len)
1018 #define SCTP_SHA1_CTX struct sctp_sha1_context
1019 #define SCTP_SHA1_INIT sctp_sha1_init
1020 #define SCTP_SHA1_UPDATE sctp_sha1_update
1021 #define SCTP_SHA1_FINAL(x,y) sctp_sha1_final((unsigned char *)x, y)
1023 /* start OOTB only stuff */
1024 /* TODO IFT_LOOP is in net/if_types.h on Linux */
1025 #define IFT_LOOP 0x18
1029 #if defined(__Userspace_os_Windows)
1034 #define PRU_FLUSH_RD SHUT_RD
1035 #define PRU_FLUSH_WR SHUT_WR
1036 #define PRU_FLUSH_RDWR SHUT_RDWR
1038 /* netinet/ip_var.h defintions are behind an if defined for _KERNEL on FreeBSD */
1039 #define IP_RAWOUTPUT 0x2
1042 /* end OOTB only stuff */
1045 struct sockaddr_conn {
1046 #ifdef HAVE_SCONN_LEN
1049 uint8_t sconn_family;
1050 uint16_t sconn_port;
1055 * SCTP protocol specific mbuf flags.
1057 #define M_NOTIFICATION M_PROTO5 /* SCTP notification */
1060 * IP output routines
1063 /* Defining SCTP_IP_ID macro.
1064 In netinet/ip_output.c, we have u_short ip_id;
1065 In netinet/ip_var.h, we have extern u_short ip_id; (enclosed within _KERNEL_)
1066 See static __inline uint16_t ip_newid(void) in netinet/ip_var.h
1068 #define SCTP_IP_ID(inp) (ip_id)
1070 /* need sctphdr to get port in SCTP_IP_OUTPUT. sctphdr defined in sctp.h */
1071 #include <netinet/sctp.h>
1072 extern void sctp_userspace_ip_output(int *result, struct mbuf *o_pak,
1073 sctp_route_t *ro, void *stcb,
1076 #define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) sctp_userspace_ip_output(&result, o_pak, ro, stcb, vrf_id);
1079 extern void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak,
1080 struct route_in6 *ro, void *stcb,
1082 #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) sctp_userspace_ip6_output(&result, o_pak, ro, stcb, vrf_id);
1088 #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \
1090 if (stcb && stcb->sctp_ep) \
1091 result = ip6_output(o_pak, \
1092 ((struct in6pcb *)(stcb->sctp_ep))->in6p_outputopts, \
1093 (ro), 0, 0, ifp, NULL); \
1095 result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
1100 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header, int how, int allonebuf, int type);
1103 /* with the current included files, this is defined in Linux but
1104 * in FreeBSD, it is behind a _KERNEL in sys/socket.h ...
1106 #if defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_OpenBSD) || defined(__Userspace_os_NaCl)
1107 /* stolen from /usr/include/sys/socket.h */
1108 #define CMSG_ALIGN(n) _ALIGN(n)
1109 #elif defined(__Userspace_os_NetBSD)
1110 #define CMSG_ALIGN(n) (((n) + __ALIGNBYTES) & ~__ALIGNBYTES)
1111 #elif defined(__Userspace_os_Darwin)
1112 #if !defined(__DARWIN_ALIGNBYTES)
1113 #define __DARWIN_ALIGNBYTES (sizeof(__darwin_size_t) - 1)
1116 #if !defined(__DARWIN_ALIGN)
1117 #define __DARWIN_ALIGN(p) ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES) &~ __DARWIN_ALIGNBYTES)
1120 #if !defined(__DARWIN_ALIGNBYTES32)
1121 #define __DARWIN_ALIGNBYTES32 (sizeof(__uint32_t) - 1)
1124 #if !defined(__DARWIN_ALIGN32)
1125 #define __DARWIN_ALIGN32(p) ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES32) &~ __DARWIN_ALIGNBYTES32)
1127 #define CMSG_ALIGN(n) __DARWIN_ALIGN32(n)
1131 SCTP_PRINTF("%s:%d at %s\n", __FILE__, __LINE__ , __FUNCTION__); \
1135 #define timevalsub(tp1, tp2) \
1137 (tp1)->tv_sec -= (tp2)->tv_sec; \
1138 (tp1)->tv_usec -= (tp2)->tv_usec; \
1139 if ((tp1)->tv_usec < 0) { \
1141 (tp1)->tv_usec += 1000000; \
1146 #if defined(__Userspace_os_Linux)
1147 #if !defined(TAILQ_FOREACH_SAFE)
1148 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
1149 for ((var) = ((head)->tqh_first); \
1150 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
1153 #if !defined(LIST_FOREACH_SAFE)
1154 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
1155 for ((var) = ((head)->lh_first); \
1156 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
1160 #if defined(__Userspace_os_DragonFly)
1161 #define TAILQ_FOREACH_SAFE TAILQ_FOREACH_MUTABLE
1162 #define LIST_FOREACH_SAFE LIST_FOREACH_MUTABLE
1165 #if defined(__Userspace_os_NaCl)
1166 #define timercmp(tvp, uvp, cmp) \
1167 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1168 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1169 ((tvp)->tv_sec cmp (uvp)->tv_sec))