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 #define uint64_t unsigned __int64
81 #define u_long unsigned __int64
82 #define u_int unsigned __int32
83 #define uint32_t unsigned __int32
84 #define u_int32_t unsigned __int32
85 #define int32_t __int32
86 #define int16_t __int16
87 #define uint16_t unsigned __int16
88 #define u_int16_t unsigned __int16
89 #define uint8_t unsigned __int8
90 #define u_int8_t unsigned __int8
92 #define u_char unsigned char
93 #define n_short unsigned __int16
94 #define u_short unsigned __int16
95 #define ssize_t __int64
96 #define size_t __int32
97 #define n_time unsigned __int32
98 #define sa_family_t unsigned __int8
100 #define __func__ __FUNCTION__
103 #define EWOULDBLOCK WSAEWOULDBLOCK
106 #define EINPROGRESS WSAEINPROGRESS
109 #define EALREADY WSAEALREADY
112 #define ENOTSOCK WSAENOTSOCK
115 #define EDESTADDRREQ WSAEDESTADDRREQ
118 #define EMSGSIZE WSAEMSGSIZE
121 #define EPROTOTYPE WSAEPROTOTYPE
124 #define ENOPROTOOPT WSAENOPROTOOPT
126 #ifndef EPROTONOSUPPORT
127 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
129 #ifndef ESOCKTNOSUPPORT
130 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
133 #define EOPNOTSUPP WSAEOPNOTSUPP
136 #define ENOTSUP WSAEOPNOTSUPP
139 #define EPFNOSUPPORT WSAEPFNOSUPPORT
142 #define EAFNOSUPPORT WSAEAFNOSUPPORT
145 #define EADDRINUSE WSAEADDRINUSE
147 #ifndef EADDRNOTAVAIL
148 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
151 #define ENETDOWN WSAENETDOWN
154 #define ENETUNREACH WSAENETUNREACH
157 #define ENETRESET WSAENETRESET
160 #define ECONNABORTED WSAECONNABORTED
163 #define ECONNRESET WSAECONNRESET
166 #define ENOBUFS WSAENOBUFS
169 #define EISCONN WSAEISCONN
172 #define ENOTCONN WSAENOTCONN
175 #define ESHUTDOWN WSAESHUTDOWN
178 #define ETOOMANYREFS WSAETOOMANYREFS
181 #define ETIMEDOUT WSAETIMEDOUT
184 #define ECONNREFUSED WSAECONNREFUSED
187 #define ELOOP WSAELOOP
190 #define EHOSTDOWN WSAEHOSTDOWN
193 #define EHOSTUNREACH WSAEHOSTUNREACH
196 #define EPROCLIM WSAEPROCLIM
199 #define EUSERS WSAEUSERS
202 #define EDQUOT WSAEDQUOT
205 #define ESTALE WSAESTALE
208 #define EREMOTE WSAEREMOTE
211 typedef char* caddr_t;
213 int Win_getifaddrs(struct ifaddrs**);
214 #define getifaddrs(interfaces) (int)Win_getifaddrs(interfaces)
215 int win_if_nametoindex(const char *);
216 #define if_nametoindex(x) win_if_nametoindex(x)
218 #define bzero(buf, len) memset(buf, 0, len)
219 #define bcopy(srcKey, dstKey, len) memcpy(dstKey, srcKey, len)
220 #define snprintf(data, size, format, name) _snprintf_s(data, size, _TRUNCATE, format, name)
221 #define inline __inline
222 #define __inline__ __inline
223 #define random() rand()
224 #define srandom(s) srand(s)
225 #define MSG_EOR 0x8 /* data completes record */
226 #define MSG_DONTWAIT 0x80 /* this message should be nonblocking */
232 * The following definitions should apply iff WINVER < 0x0600
233 * but that check doesn't work in all cases. So be more pedantic...
235 #define CMSG_DATA(x) WSA_CMSG_DATA(x)
236 #define CMSG_ALIGN(x) WSA_CMSGDATA_ALIGN(x)
237 #ifndef CMSG_FIRSTHDR
238 #define CMSG_FIRSTHDR(x) WSA_CMSG_FIRSTHDR(x)
241 #define CMSG_NXTHDR(x, y) WSA_CMSG_NXTHDR(x, y)
244 #define CMSG_SPACE(x) WSA_CMSG_SPACE(x)
247 #define CMSG_LEN(x) WSA_CMSG_LEN(x)
250 /**** from sctp_os_windows.h ***************/
251 #define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
252 #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)
255 * Access to IFN's to help with src-addr-selection
257 /* This could return VOID if the index works but for BSD we provide both. */
258 #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) \
259 ((ro)->ro_rt != NULL ? (ro)->ro_rt->rt_ifp : NULL)
260 #define SCTP_ROUTE_HAS_VALID_IFN(ro) \
261 ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
262 /******************************************/
264 #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles... TODO use routing socket to determine */
266 #define timeradd(tvp, uvp, vvp) \
268 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
269 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
270 if ((vvp)->tv_usec >= 1000000) { \
272 (vvp)->tv_usec -= 1000000; \
276 #define timersub(tvp, uvp, vvp) \
278 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
279 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
280 if ((vvp)->tv_usec < 0) { \
282 (vvp)->tv_usec += 1000000; \
287 #define LITTLE_ENDIAN 0
288 #ifdef WORDS_BIGENDIAN
289 #define BYTE_ORDER BIG_ENDIAN
291 #define BYTE_ORDER LITTLE_ENDIAN
303 unsigned __int16 ifam_msglen;
304 unsigned char ifam_version;
305 unsigned char ifam_type;
308 unsigned __int16 ifam_index;
319 unsigned int ifk_module_id;
320 unsigned int ifk_type;
330 struct sockaddr ifru_addr;
331 struct sockaddr ifru_dstaddr;
332 struct sockaddr ifru_broadaddr;
340 struct ifdevmtu ifru_devmtu;
341 struct ifkpi ifru_kpi;
342 unsigned __int32 ifru_wake_flags;
344 #define ifr_addr ifr_ifru.ifru_addr
345 #define ifr_dstaddr ifr_ifru.ifru_dstaddr
346 #define ifr_broadaddr ifr_ifru.ifru_broadaddr
347 #define ifr_flags ifr_ifru.ifru_flags[0]
348 #define ifr_prevflags ifr_ifru.ifru_flags[1]
349 #define ifr_metric ifr_ifru.ifru_metric
350 #define ifr_mtu ifr_ifru.ifru_mtu
351 #define ifr_phys ifr_ifru.ifru_phys
352 #define ifr_media ifr_ifru.ifru_media
353 #define ifr_data ifr_ifru.ifru_data
354 #define ifr_devmtu ifr_ifru.ifru_devmtu
355 #define ifr_intval ifr_ifru.ifru_intval
356 #define ifr_kpi ifr_ifru.ifru_kpi
357 #define ifr_wake_flags ifr_ifru.ifru_wake_flags
360 /*#include <packon.h>
361 #pragma pack(push, 1)*/
363 u_char ip_hl:4, ip_v:4;
371 #define IP_OFFMASK 0x1fff
375 struct in_addr ip_src, ip_dst;
379 struct ifaddrs *ifa_next;
381 unsigned int ifa_flags;
382 struct sockaddr *ifa_addr;
383 struct sockaddr *ifa_netmask;
384 struct sockaddr *ifa_dstaddr;
389 unsigned __int16 uh_sport;
390 unsigned __int16 uh_dport;
391 unsigned __int16 uh_ulen;
392 unsigned __int16 uh_sum;
395 #else /* !defined(Userspace_os_Windows) */
396 #include <sys/cdefs.h> /* needed? added from old __FreeBSD__ */
397 #include <sys/socket.h>
398 #if defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_OpenBSD) || defined(__Userspace_os_Linux)
401 typedef pthread_mutex_t userland_mutex_t;
402 typedef pthread_cond_t userland_cond_t;
403 typedef pthread_t userland_thread_t;
406 #define mtx_lock(arg1)
407 #define mtx_unlock(arg1)
408 #define mtx_assert(arg1,arg2)
409 #define MA_OWNED 7 /* sys/mutex.h typically on FreeBSD */
410 #if !defined(__Userspace_os_FreeBSD)
411 struct mtx {int dummy;};
412 struct selinfo {int dummy;};
413 struct sx {int dummy;};
418 /* #include <sys/param.h> in FreeBSD defines MSIZE */
419 /* #include <sys/ktr.h> */
420 /* #include <sys/systm.h> */
421 #if defined(__Userspace_os_Windows)
422 #include <user_queue.h>
424 #include <sys/queue.h>
426 #include <user_malloc.h>
427 /* #include <sys/kernel.h> */
428 /* #include <sys/sysctl.h> */
429 /* #include <sys/protosw.h> */
430 /* on FreeBSD, this results in a redefintion of SOCK(BUF)_(UN)LOCK and
431 * uknown type of struct mtx for sb_mtx in struct sockbuf */
432 #include "user_socketvar.h" /* MALLOC_DECLARE's M_PCB. Replacement for sys/socketvar.h */
433 /* #include <sys/jail.h> */
434 /* #include <sys/sysctl.h> */
435 #include <user_environment.h>
436 #include <user_atomic.h>
437 #include <user_mbuf.h>
438 /* #include <sys/uio.h> */
439 /* #include <sys/lock.h> */
440 #if defined(__FreeBSD__) && __FreeBSD_version > 602000
441 #include <sys/rwlock.h>
443 /* #include <sys/kthread.h> */
444 #if defined(__FreeBSD__) && __FreeBSD_version > 602000
445 #include <sys/priv.h>
447 /* #include <sys/random.h> */
448 /* #include <sys/limits.h> */
449 /* #include <machine/cpu.h> */
451 #if defined(__Userspace_os_Darwin)
452 /* was a 0 byte file. needed for structs if_data(64) and net_event_data */
453 #include <net/if_var.h>
455 #if defined(__Userspace_os_FreeBSD)
456 #include <net/if_types.h>
457 /* #include <net/if_var.h> was a 0 byte file. causes struct mtx redefinition */
459 /* OOTB only - dummy route used at the moment. should we port route to
460 * userspace as well? */
461 /* on FreeBSD, this results in a redefintion of struct route */
462 /* #include <net/route.h> */
463 #if !defined(__Userspace_os_Windows)
465 #include <netinet/in.h>
466 #include <netinet/in_systm.h>
467 #include <netinet/ip.h>
468 #include <netinet/ip_icmp.h>
470 #include <user_ip_icmp.h>
472 /* #include <netinet/in_pcb.h> ported to userspace */
473 #include <user_inpcb.h>
476 #include <sys/types.h>
477 #if !defined(__Userspace_os_Windows)
478 #if defined(INET) || defined(INET6)
483 #include <sys/ioctl.h>
485 /* for close, etc. */
489 /* lots of errno's used and needed in userspace */
494 #if defined(SCTP_PROCESS_LEVEL_LOCKS) && !defined(__Userspace_os_Windows)
495 /* for pthread_mutex_lock, pthread_mutex_unlock, etc. */
500 #include <netipsec/ipsec.h>
501 #include <netipsec/key.h>
505 #if defined(__Userspace_os_FreeBSD)
506 #include <sys/domain.h>
509 #include <netipsec/ipsec6.h>
511 #if !defined(__Userspace_os_Windows)
512 #include <netinet/ip6.h>
513 #include <netinet/icmp6.h>
515 #if defined(__Userspace_os_Linux) || defined(__Userspace_os_Darwin) || defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_OpenBSD) ||defined(__Userspace_os_Windows)
516 #include "user_ip6_var.h"
518 #include <netinet6/ip6_var.h>
520 #if defined(__Userspace_os_FreeBSD)
521 #include <netinet6/in6_pcb.h>
522 #include <netinet6/ip6protosw.h>
523 /* #include <netinet6/nd6.h> was a 0 byte file */
524 #include <netinet6/scope6_var.h>
528 #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
529 #include <sys/file.h>
530 #include <sys/filedesc.h>
533 #include "netinet/sctp_sha1.h"
535 #if __FreeBSD_version >= 700000
536 #include <netinet/ip_options.h>
539 #define SCTP_PRINTF(...) \
540 if (SCTP_BASE_VAR(debug_printf)) { \
541 SCTP_BASE_VAR(debug_printf)(__VA_ARGS__); \
544 #if defined(__FreeBSD__)
549 /* Declare all the malloc names for all the various mallocs */
550 MALLOC_DECLARE(SCTP_M_MAP);
551 MALLOC_DECLARE(SCTP_M_STRMI);
552 MALLOC_DECLARE(SCTP_M_STRMO);
553 MALLOC_DECLARE(SCTP_M_ASC_ADDR);
554 MALLOC_DECLARE(SCTP_M_ASC_IT);
555 MALLOC_DECLARE(SCTP_M_AUTH_CL);
556 MALLOC_DECLARE(SCTP_M_AUTH_KY);
557 MALLOC_DECLARE(SCTP_M_AUTH_HL);
558 MALLOC_DECLARE(SCTP_M_AUTH_IF);
559 MALLOC_DECLARE(SCTP_M_STRESET);
560 MALLOC_DECLARE(SCTP_M_CMSG);
561 MALLOC_DECLARE(SCTP_M_COPYAL);
562 MALLOC_DECLARE(SCTP_M_VRF);
563 MALLOC_DECLARE(SCTP_M_IFA);
564 MALLOC_DECLARE(SCTP_M_IFN);
565 MALLOC_DECLARE(SCTP_M_TIMW);
566 MALLOC_DECLARE(SCTP_M_MVRF);
567 MALLOC_DECLARE(SCTP_M_ITER);
568 MALLOC_DECLARE(SCTP_M_SOCKOPT);
570 #if defined(SCTP_LOCAL_TRACE_BUF)
572 #define SCTP_GET_CYCLECOUNT get_cyclecount()
573 #define SCTP_CTR6 sctp_log_trace
576 #define SCTP_CTR6 CTR6
579 /* Empty ktr statement for _Userspace__ (similar to what is done for mac) */
580 #define CTR6(m, d, p1, p2, p3, p4, p5, p6)
584 #define SCTP_BASE_INFO(__m) system_base_info.sctppcbinfo.__m
585 #define SCTP_BASE_STATS system_base_info.sctpstat
586 #define SCTP_BASE_STAT(__m) system_base_info.sctpstat.__m
587 #define SCTP_BASE_SYSCTL(__m) system_base_info.sctpsysctl.__m
588 #define SCTP_BASE_VAR(__m) system_base_info.__m
593 #if !defined(__Userspace_os_Darwin)
594 #define USER_ADDR_NULL (NULL) /* FIX ME: temp */
597 #if defined(SCTP_DEBUG)
598 #include <netinet/sctp_constants.h>
599 #define SCTPDBG(level, ...) \
602 if (SCTP_BASE_SYSCTL(sctp_debug_on) & level) { \
603 SCTP_PRINTF(__VA_ARGS__); \
607 #define SCTPDBG_ADDR(level, addr) \
610 if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) { \
611 sctp_print_address(addr); \
616 #define SCTPDBG(level, ...)
617 #define SCTPDBG_ADDR(level, addr)
620 #ifdef SCTP_LTRACE_CHUNKS
621 #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)
623 #define SCTP_LTRACE_CHK(a, b, c, d)
626 #ifdef SCTP_LTRACE_ERRORS
627 #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
628 if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
629 SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
630 (void *)m, (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
631 #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
632 if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
633 SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
634 (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
636 #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
637 #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
642 * Local address and interface list handling
644 #define SCTP_MAX_VRF_ID 0
645 #define SCTP_SIZE_OF_VRF_HASH 3
646 #define SCTP_IFNAMSIZ IFNAMSIZ
647 #define SCTP_DEFAULT_VRFID 0
648 #define SCTP_VRF_ADDR_HASH_SIZE 16
649 #define SCTP_VRF_IFN_HASH_SIZE 3
650 #define SCTP_INIT_VRF_TABLEID(vrf)
652 #if !defined(__Userspace_os_Windows)
653 #define SCTP_IFN_IS_IFT_LOOP(ifn) (strncmp((ifn)->ifn_name, "lo", 2) == 0)
655 /* #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) */
656 /* only used in IPv6 scenario, which isn't supported yet */
657 #define SCTP_ROUTE_IS_REAL_LOOP(ro) 0
660 * Access to IFN's to help with src-addr-selection
662 /* This could return VOID if the index works but for BSD we provide both. */
663 #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
664 #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles... TODO use routing socket to determine */
665 #define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
669 * general memory allocation
671 #define SCTP_MALLOC(var, type, size, name) \
673 MALLOC(var, type, size, name, M_NOWAIT); \
676 #define SCTP_FREE(var, type) FREE(var, type)
678 #define SCTP_MALLOC_SONAME(var, type, size) \
680 MALLOC(var, type, size, M_SONAME, (M_WAITOK | M_ZERO)); \
683 #define SCTP_FREE_SONAME(var) FREE(var, M_SONAME)
685 #define SCTP_PROCESS_STRUCT struct proc *
688 * zone allocation functions
692 #if defined(SCTP_SIMPLE_ALLOCATOR)
693 /*typedef size_t sctp_zone_t;*/
694 #define SCTP_ZONE_INIT(zone, name, size, number) { \
698 /* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
699 #define SCTP_ZONE_GET(zone, type) \
700 (type *)malloc(zone);
703 /* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
704 #define SCTP_ZONE_FREE(zone, element) { \
708 #define SCTP_ZONE_DESTROY(zone)
711 Compiling & linking notes: Needs libumem, which has been placed in ./user_lib
712 All userspace header files are in ./user_include. Makefile will need the
715 LDFLAGS = -L./user_lib -R./user_lib -lumem
717 #include "user_include/umem.h"
719 /* __Userspace__ SCTP_ZONE_INIT: initialize the zone */
722 No equivalent function to uma_zone_set_max added yet. (See SCTP_ZONE_INIT in sctp_os_bsd.h
723 for reference). It may not be required as mentioned in
724 http://nixdoc.net/man-pages/FreeBSD/uma_zalloc.9.html that
725 max limits may not enforced on systems with more than one CPU.
727 #define SCTP_ZONE_INIT(zone, name, size, number) { \
728 zone = umem_cache_create(name, size, 0, NULL, NULL, NULL, NULL, NULL, 0); \
731 /* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
732 #define SCTP_ZONE_GET(zone, type) \
733 (type *)umem_cache_alloc(zone, UMEM_DEFAULT);
736 /* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
737 #define SCTP_ZONE_FREE(zone, element) \
738 umem_cache_free(zone, element);
741 /* __Userspace__ SCTP_ZONE_DESTROY: destroy the zone */
742 #define SCTP_ZONE_DESTROY(zone) \
743 umem_cache_destroy(zone);
746 /* global struct ifaddrs used in sctp_init_ifns_for_vrf getifaddrs call
747 * but references to fields are needed to persist as the vrf is queried.
748 * getifaddrs allocates memory that needs to be freed with a freeifaddrs
749 * call; this global is used to call freeifaddrs upon in sctp_pcb_finish
751 extern struct ifaddrs *g_interfaces;
755 * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland.
757 void *sctp_hashinit_flags(int elements, struct malloc_type *type,
758 u_long *hashmask, int flags);
760 sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
763 sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
766 #define HASH_NOWAIT 0x00000001
767 #define HASH_WAITOK 0x00000002
769 /* M_PCB is MALLOC_DECLARE'd in sys/socketvar.h */
770 #define SCTP_HASH_INIT(size, hashmark) sctp_hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
772 #define SCTP_HASH_FREE(table, hashmark) sctp_hashdestroy(table, M_PCB, hashmark)
774 #define SCTP_HASH_FREE_DESTROY(table, hashmark) sctp_hashfreedestroy(table, M_PCB, hashmark)
775 #define SCTP_M_COPYM m_copym
781 * user_sctp_callout.h has typedef struct sctp_callout sctp_os_timer_t;
782 * which is used in the timer related functions such as
783 * SCTP_OS_TIMER_INIT etc.
785 #include <netinet/sctp_callout.h>
787 /* __Userspace__ Creating a receive thread */
788 #include <user_recv_thread.h>
790 /*__Userspace__ defining KTR_SUBSYS 1 as done in sctp_os_macosx.h */
793 #define sctp_get_tick_count() (ticks)
795 /* The packed define for 64 bit platforms */
796 #if !defined(__Userspace_os_Windows)
797 #define SCTP_PACKED __attribute__((packed))
798 #define SCTP_UNUSED __attribute__((unused))
807 /* Mbuf manipulation and access macros */
808 #define SCTP_BUF_LEN(m) (m->m_len)
809 #define SCTP_BUF_NEXT(m) (m->m_next)
810 #define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
811 #define SCTP_BUF_RESV_UF(m, size) m->m_data += size
812 #define SCTP_BUF_AT(m, size) m->m_data + size
813 #define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
814 #define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
815 #define SCTP_BUF_TYPE(m) (m->m_type)
816 #define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
817 #define SCTP_BUF_PREPEND M_PREPEND
819 #define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
821 } else if ((m->m_flags & M_EXT) == 0) { \
825 /* We make it so if you have up to 4 threads
826 * writting based on the default size of
827 * the packet log 65 k, that would be
828 * 4 16k packets before we would hit
831 #define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
835 * routes, output, etc.
838 typedef struct sctp_route sctp_route_t;
839 typedef struct sctp_rtentry sctp_rtentry_t;
841 static inline void sctp_userspace_rtalloc(sctp_route_t *ro)
843 if (ro->ro_rt != NULL) {
844 ro->ro_rt->rt_refcnt++;
848 ro->ro_rt = (sctp_rtentry_t *) malloc(sizeof(sctp_rtentry_t));
849 if (ro->ro_rt == NULL)
853 memset(ro->ro_rt, 0, sizeof(sctp_rtentry_t));
854 ro->ro_rt->rt_refcnt = 1;
857 /* TODO set this based on the ro->ro_dst, looking up MTU with routing socket */
859 if (userspace_rawroute == -1) {
860 userspace_rawroute = socket(AF_ROUTE, SOCK_RAW, 0);
861 if (userspace_rawroute == -1)
865 ro->ro_rt->rt_rmx.rmx_mtu = 1500; /* FIXME temporary solution */
867 /* TODO enable the ability to obtain interface index of route for
868 * SCTP_GET_IF_INDEX_FROM_ROUTE macro.
871 #define SCTP_RTALLOC(ro, vrf_id) sctp_userspace_rtalloc((sctp_route_t *)ro)
873 /* dummy rtfree needed once user_route.h is included */
874 static inline void sctp_userspace_rtfree(sctp_rtentry_t *rt)
879 if(--rt->rt_refcnt > 0) {
885 #define rtfree(arg1) sctp_userspace_rtfree(arg1)
888 /*************************/
890 /*************************/
891 int sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af);
893 #define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) sctp_userspace_get_mtu_from_ifn(ifn_index, af)
895 #define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0)
897 #define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) sctp_userspace_get_mtu_from_ifn(if_nametoindex(((struct ifaddrs *) (sctp_ifn))->ifa_name), AF_INET)
899 #define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
901 rt->rt_rmx.rmx_mtu = mtu; \
904 /* (de-)register interface event notifications */
905 #define SCTP_REGISTER_INTERFACE(ifhandle, af)
906 #define SCTP_DEREGISTER_INTERFACE(ifhandle, af)
909 /*************************/
910 /* These are for logging */
911 /*************************/
912 /* return the base ext data pointer */
913 #define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
914 /* return the refcnt of the data pointer */
915 #define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
916 /* return any buffer related flags, this is
917 * used beyond logging for apple only.
919 #define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
921 /* For BSD this just accesses the M_PKTHDR length
922 * so it operates on an mbuf with hdr flag. Other
923 * O/S's may have seperate packet header and mbuf
924 * chain pointers.. thus the macro.
926 #define SCTP_HEADER_TO_CHAIN(m) (m)
927 #define SCTP_DETACH_HEADER_FROM_CHAIN(m)
928 #define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
929 #define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
930 #define SCTP_RELEASE_HEADER(m)
931 #define SCTP_RELEASE_PKT(m) sctp_m_freem(m)
932 /* UDP __Userspace__ - dummy definition */
933 #define SCTP_ENABLE_UDP_CSUM(m) m=m
935 /* #define SCTP_ENABLE_UDP_CSUM(m) do { \ */
936 /* m->m_pkthdr.csum_flags = CSUM_UDP; \ */
937 /* m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); \ */
940 #define SCTP_GET_PKT_VRFID(m, vrf_id) ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
944 /* Attach the chain of data into the sendable packet. */
945 #define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
947 pak->m_pkthdr.len = packet_length; \
950 /* Other m_pkthdr type things */
951 /* FIXME need real definitions */
952 #define SCTP_IS_IT_BROADCAST(dst, m) 0
953 /* OOTB only #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0) BSD def */
954 #define SCTP_IS_IT_LOOPBACK(m) 0
955 /* 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 */
958 /* This converts any input packet header
959 * into the chain of data holders, for BSD
963 /* get the v6 hop limit */
964 #define SCTP_GET_HLIM(inp, ro) 128 /* As done for __Windows__ */
965 #define IPv6_HOP_LIMIT 128
967 /* is the endpoint v6only? */
968 #define SCTP_IPV6_V6ONLY(inp) (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
969 /* is the socket non-blocking? */
970 #define SCTP_SO_IS_NBIO(so) ((so)->so_state & SS_NBIO)
971 #define SCTP_SET_SO_NBIO(so) ((so)->so_state |= SS_NBIO)
972 #define SCTP_CLEAR_SO_NBIO(so) ((so)->so_state &= ~SS_NBIO)
973 /* get the socket type */
974 #define SCTP_SO_TYPE(so) ((so)->so_type)
976 /* reserve sb space for a socket */
977 #define SCTP_SORESERVE(so, send, recv) soreserve(so, send, recv)
979 /* wakeup a socket */
980 #define SCTP_SOWAKEUP(so) wakeup(&(so)->so_timeo, so)
981 /* clear the socket buffer state */
982 #define SCTP_SB_CLEAR(sb) \
987 #define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
988 #define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
990 /* Future zero copy wakeup/send function */
991 #define SCTP_ZERO_COPY_EVENT(inp, so)
992 /* This is re-pulse ourselves for sendbuf */
993 #define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so)
995 #define SCTP_READ_RANDOM(buf, len) read_random(buf, len)
997 #define SCTP_SHA1_CTX struct sctp_sha1_context
998 #define SCTP_SHA1_INIT sctp_sha1_init
999 #define SCTP_SHA1_UPDATE sctp_sha1_update
1000 #define SCTP_SHA1_FINAL(x,y) sctp_sha1_final((unsigned char *)x, y)
1002 /* start OOTB only stuff */
1003 /* TODO IFT_LOOP is in net/if_types.h on Linux */
1004 #define IFT_LOOP 0x18
1008 #if defined(__Userspace_os_Windows)
1013 #define PRU_FLUSH_RD SHUT_RD
1014 #define PRU_FLUSH_WR SHUT_WR
1015 #define PRU_FLUSH_RDWR SHUT_RDWR
1017 /* netinet/ip_var.h defintions are behind an if defined for _KERNEL on FreeBSD */
1018 #define IP_RAWOUTPUT 0x2
1021 /* end OOTB only stuff */
1024 struct sockaddr_conn {
1025 #ifdef HAVE_SCONN_LEN
1028 uint8_t sconn_family;
1029 uint16_t sconn_port;
1034 * SCTP protocol specific mbuf flags.
1036 #define M_NOTIFICATION M_PROTO5 /* SCTP notification */
1039 * IP output routines
1042 /* Defining SCTP_IP_ID macro.
1043 In netinet/ip_output.c, we have u_short ip_id;
1044 In netinet/ip_var.h, we have extern u_short ip_id; (enclosed within _KERNEL_)
1045 See static __inline uint16_t ip_newid(void) in netinet/ip_var.h
1047 #define SCTP_IP_ID(inp) (ip_id)
1049 /* need sctphdr to get port in SCTP_IP_OUTPUT. sctphdr defined in sctp.h */
1050 #include <netinet/sctp.h>
1051 extern void sctp_userspace_ip_output(int *result, struct mbuf *o_pak,
1052 sctp_route_t *ro, void *stcb,
1055 #define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) sctp_userspace_ip_output(&result, o_pak, ro, stcb, vrf_id);
1058 extern void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak,
1059 struct route_in6 *ro, void *stcb,
1061 #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) sctp_userspace_ip6_output(&result, o_pak, ro, stcb, vrf_id);
1067 #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \
1069 if (stcb && stcb->sctp_ep) \
1070 result = ip6_output(o_pak, \
1071 ((struct in6pcb *)(stcb->sctp_ep))->in6p_outputopts, \
1072 (ro), 0, 0, ifp, NULL); \
1074 result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
1079 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header, int how, int allonebuf, int type);
1082 /* with the current included files, this is defined in Linux but
1083 * in FreeBSD, it is behind a _KERNEL in sys/socket.h ...
1085 #if defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_OpenBSD)
1086 /* stolen from /usr/include/sys/socket.h */
1087 #define CMSG_ALIGN(n) _ALIGN(n)
1088 #elif defined(__Userspace_os_Darwin)
1089 #if !defined(__DARWIN_ALIGNBYTES)
1090 #define __DARWIN_ALIGNBYTES (sizeof(__darwin_size_t) - 1)
1093 #if !defined(__DARWIN_ALIGN)
1094 #define __DARWIN_ALIGN(p) ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES) &~ __DARWIN_ALIGNBYTES)
1097 #if !defined(__DARWIN_ALIGNBYTES32)
1098 #define __DARWIN_ALIGNBYTES32 (sizeof(__uint32_t) - 1)
1101 #if !defined(__DARWIN_ALIGN32)
1102 #define __DARWIN_ALIGN32(p) ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES32) &~ __DARWIN_ALIGNBYTES32)
1104 #define CMSG_ALIGN(n) __DARWIN_ALIGN32(n)
1108 SCTP_PRINTF("%s:%d at %s\n", __FILE__, __LINE__ , __FUNCTION__); \
1112 #define timevalsub(tp1, tp2) \
1114 (tp1)->tv_sec -= (tp2)->tv_sec; \
1115 (tp1)->tv_usec -= (tp2)->tv_usec; \
1116 if ((tp1)->tv_usec < 0) { \
1118 (tp1)->tv_usec += 1000000; \
1123 #if defined(__Userspace_os_Linux)
1124 #if !defined(TAILQ_FOREACH_SAFE)
1125 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
1126 for ((var) = ((head)->tqh_first); \
1127 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
1130 #if !defined(LIST_FOREACH_SAFE)
1131 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
1132 for ((var) = ((head)->lh_first); \
1133 (var) && ((tvar) = LIST_NEXT((var), field), 1); \