blob: fd3e0d7531f5145a5ca4696c86c9207fdce3aa4e [file] [log] [blame]
James Kuszmaul4cb043c2021-01-17 11:25:51 -08001/*-
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.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * a) Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34#ifndef __sctp_os_userspace_h__
35#define __sctp_os_userspace_h__
36/*
37 * Userspace includes
38 * All the opt_xxx.h files are placed in the kernel build directory.
39 * We will place them in userspace stack build directory.
40 */
41
42#include <errno.h>
43
44#if defined(__Userspace_os_Windows)
45#include <winsock2.h>
46#include <ws2tcpip.h>
47#include <iphlpapi.h>
48#include <Mswsock.h>
49#include <Windows.h>
50#include "user_environment.h"
51typedef CRITICAL_SECTION userland_mutex_t;
52#if WINVER < 0x0600
53enum {
54 C_SIGNAL = 0,
55 C_BROADCAST = 1,
56 C_MAX_EVENTS = 2
57};
58typedef struct
59{
60 u_int waiters_count;
61 CRITICAL_SECTION waiters_count_lock;
62 HANDLE events_[C_MAX_EVENTS];
63} userland_cond_t;
64void InitializeXPConditionVariable(userland_cond_t *);
65void DeleteXPConditionVariable(userland_cond_t *);
66int SleepXPConditionVariable(userland_cond_t *, userland_mutex_t *);
67void 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)
72#else
73#define DeleteConditionVariable(cond)
74typedef CONDITION_VARIABLE userland_cond_t;
75#endif
76typedef HANDLE userland_thread_t;
77#define ADDRESS_FAMILY unsigned __int8
78#define IPVERSION 4
79#define MAXTTL 255
80/* VS2010 comes with stdint.h */
81#if _MSC_VER >= 1600
82#include <stdint.h>
83#else
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
90#define int8_t __int8
91#endif
92#ifndef _SIZE_T_DEFINED
93#define size_t __int32
94#endif
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__
107
108#ifndef EWOULDBLOCK
109#define EWOULDBLOCK WSAEWOULDBLOCK
110#endif
111#ifndef EINPROGRESS
112#define EINPROGRESS WSAEINPROGRESS
113#endif
114#ifndef EALREADY
115#define EALREADY WSAEALREADY
116#endif
117#ifndef ENOTSOCK
118#define ENOTSOCK WSAENOTSOCK
119#endif
120#ifndef EDESTADDRREQ
121#define EDESTADDRREQ WSAEDESTADDRREQ
122#endif
123#ifndef EMSGSIZE
124#define EMSGSIZE WSAEMSGSIZE
125#endif
126#ifndef EPROTOTYPE
127#define EPROTOTYPE WSAEPROTOTYPE
128#endif
129#ifndef ENOPROTOOPT
130#define ENOPROTOOPT WSAENOPROTOOPT
131#endif
132#ifndef EPROTONOSUPPORT
133#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
134#endif
135#ifndef ESOCKTNOSUPPORT
136#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
137#endif
138#ifndef EOPNOTSUPP
139#define EOPNOTSUPP WSAEOPNOTSUPP
140#endif
141#ifndef ENOTSUP
142#define ENOTSUP WSAEOPNOTSUPP
143#endif
144#ifndef EPFNOSUPPORT
145#define EPFNOSUPPORT WSAEPFNOSUPPORT
146#endif
147#ifndef EAFNOSUPPORT
148#define EAFNOSUPPORT WSAEAFNOSUPPORT
149#endif
150#ifndef EADDRINUSE
151#define EADDRINUSE WSAEADDRINUSE
152#endif
153#ifndef EADDRNOTAVAIL
154#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
155#endif
156#ifndef ENETDOWN
157#define ENETDOWN WSAENETDOWN
158#endif
159#ifndef ENETUNREACH
160#define ENETUNREACH WSAENETUNREACH
161#endif
162#ifndef ENETRESET
163#define ENETRESET WSAENETRESET
164#endif
165#ifndef ECONNABORTED
166#define ECONNABORTED WSAECONNABORTED
167#endif
168#ifndef ECONNRESET
169#define ECONNRESET WSAECONNRESET
170#endif
171#ifndef ENOBUFS
172#define ENOBUFS WSAENOBUFS
173#endif
174#ifndef EISCONN
175#define EISCONN WSAEISCONN
176#endif
177#ifndef ENOTCONN
178#define ENOTCONN WSAENOTCONN
179#endif
180#ifndef ESHUTDOWN
181#define ESHUTDOWN WSAESHUTDOWN
182#endif
183#ifndef ETOOMANYREFS
184#define ETOOMANYREFS WSAETOOMANYREFS
185#endif
186#ifndef ETIMEDOUT
187#define ETIMEDOUT WSAETIMEDOUT
188#endif
189#ifndef ECONNREFUSED
190#define ECONNREFUSED WSAECONNREFUSED
191#endif
192#ifndef ELOOP
193#define ELOOP WSAELOOP
194#endif
195#ifndef EHOSTDOWN
196#define EHOSTDOWN WSAEHOSTDOWN
197#endif
198#ifndef EHOSTUNREACH
199#define EHOSTUNREACH WSAEHOSTUNREACH
200#endif
201#ifndef EPROCLIM
202#define EPROCLIM WSAEPROCLIM
203#endif
204#ifndef EUSERS
205#define EUSERS WSAEUSERS
206#endif
207#ifndef EDQUOT
208#define EDQUOT WSAEDQUOT
209#endif
210#ifndef ESTALE
211#define ESTALE WSAESTALE
212#endif
213#ifndef EREMOTE
214#define EREMOTE WSAEREMOTE
215#endif
216
217typedef char* caddr_t;
218
219#define bzero(buf, len) memset(buf, 0, len)
220#define bcopy(srcKey, dstKey, len) memcpy(dstKey, srcKey, len)
221#if _MSC_VER < 1900
222#define snprintf(data, size, format, ...) _snprintf_s(data, size, _TRUNCATE, format, __VA_ARGS__)
223#endif
224#define inline __inline
225#define __inline__ __inline
226#define MSG_EOR 0x8 /* data completes record */
227#define MSG_DONTWAIT 0x80 /* this message should be nonblocking */
228
229#ifdef CMSG_DATA
230#undef CMSG_DATA
231#endif
232/*
233 * The following definitions should apply iff WINVER < 0x0600
234 * but that check doesn't work in all cases. So be more pedantic...
235 */
236#define CMSG_DATA(x) WSA_CMSG_DATA(x)
237#define CMSG_ALIGN(x) WSA_CMSGDATA_ALIGN(x)
238#ifndef CMSG_FIRSTHDR
239#define CMSG_FIRSTHDR(x) WSA_CMSG_FIRSTHDR(x)
240#endif
241#ifndef CMSG_NXTHDR
242#define CMSG_NXTHDR(x, y) WSA_CMSG_NXTHDR(x, y)
243#endif
244#ifndef CMSG_SPACE
245#define CMSG_SPACE(x) WSA_CMSG_SPACE(x)
246#endif
247#ifndef CMSG_LEN
248#define CMSG_LEN(x) WSA_CMSG_LEN(x)
249#endif
250
251/**** from sctp_os_windows.h ***************/
252#define SCTP_IFN_IS_IFT_LOOP(ifn) ((ifn)->ifn_type == IFT_LOOP)
253#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
255/*
256 * Access to IFN's to help with src-addr-selection
257 */
258/* This could return VOID if the index works but for BSD we provide both. */
259#define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) \
260 ((ro)->ro_rt != NULL ? (ro)->ro_rt->rt_ifp : NULL)
261#define SCTP_ROUTE_HAS_VALID_IFN(ro) \
262 ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
263/******************************************/
264
265#define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles... TODO use routing socket to determine */
266
267#define BIG_ENDIAN 1
268#define LITTLE_ENDIAN 0
269#ifdef WORDS_BIGENDIAN
270#define BYTE_ORDER BIG_ENDIAN
271#else
272#define BYTE_ORDER LITTLE_ENDIAN
273#endif
274
275#else /* !defined(Userspace_os_Windows) */
276#include <sys/socket.h>
277#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)
278#include <pthread.h>
279#endif
280typedef pthread_mutex_t userland_mutex_t;
281typedef pthread_cond_t userland_cond_t;
282typedef pthread_t userland_thread_t;
283#endif
284
285#if defined(__Userspace_os_Windows) || defined(__Userspace_os_NaCl)
286
287#define IFNAMSIZ 64
288
289#define random() rand()
290#define srandom(s) srand(s)
291
292#define timeradd(tvp, uvp, vvp) \
293 do { \
294 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
295 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
296 if ((vvp)->tv_usec >= 1000000) { \
297 (vvp)->tv_sec++; \
298 (vvp)->tv_usec -= 1000000; \
299 } \
300 } while (0)
301
302#define timersub(tvp, uvp, vvp) \
303 do { \
304 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
305 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
306 if ((vvp)->tv_usec < 0) { \
307 (vvp)->tv_sec--; \
308 (vvp)->tv_usec += 1000000; \
309 } \
310 } while (0)
311
312/*#include <packon.h>
313#pragma pack(push, 1)*/
314struct ip {
315 u_char ip_hl:4, ip_v:4;
316 u_char ip_tos;
317 u_short ip_len;
318 u_short ip_id;
319 u_short ip_off;
320#define IP_RP 0x8000
321#define IP_DF 0x4000
322#define IP_MF 0x2000
323#define IP_OFFMASK 0x1fff
324 u_char ip_ttl;
325 u_char ip_p;
326 u_short ip_sum;
327 struct in_addr ip_src, ip_dst;
328};
329
330struct ifaddrs {
331 struct ifaddrs *ifa_next;
332 char *ifa_name;
333 unsigned int ifa_flags;
334 struct sockaddr *ifa_addr;
335 struct sockaddr *ifa_netmask;
336 struct sockaddr *ifa_dstaddr;
337 void *ifa_data;
338};
339
340struct udphdr {
341 uint16_t uh_sport;
342 uint16_t uh_dport;
343 uint16_t uh_ulen;
344 uint16_t uh_sum;
345};
346
347struct iovec {
348 size_t len;
349 char *buf;
350};
351
352#define iov_base buf
353#define iov_len len
354
355struct ifa_msghdr {
356 uint16_t ifam_msglen;
357 unsigned char ifam_version;
358 unsigned char ifam_type;
359 uint32_t ifam_addrs;
360 uint32_t ifam_flags;
361 uint16_t ifam_index;
362 uint32_t ifam_metric;
363};
364
365struct ifdevmtu {
366 int ifdm_current;
367 int ifdm_min;
368 int ifdm_max;
369};
370
371struct ifkpi {
372 unsigned int ifk_module_id;
373 unsigned int ifk_type;
374 union {
375 void *ifk_ptr;
376 int ifk_value;
377 } ifk_data;
378};
379
380struct ifreq {
381 char ifr_name[16];
382 union {
383 struct sockaddr ifru_addr;
384 struct sockaddr ifru_dstaddr;
385 struct sockaddr ifru_broadaddr;
386 short ifru_flags;
387 int ifru_metric;
388 int ifru_mtu;
389 int ifru_phys;
390 int ifru_media;
391 int ifru_intval;
392 char* ifru_data;
393 struct ifdevmtu ifru_devmtu;
394 struct ifkpi ifru_kpi;
395 uint32_t ifru_wake_flags;
396 } ifr_ifru;
397#define ifr_addr ifr_ifru.ifru_addr
398#define ifr_dstaddr ifr_ifru.ifru_dstaddr
399#define ifr_broadaddr ifr_ifru.ifru_broadaddr
400#define ifr_flags ifr_ifru.ifru_flags[0]
401#define ifr_prevflags ifr_ifru.ifru_flags[1]
402#define ifr_metric ifr_ifru.ifru_metric
403#define ifr_mtu ifr_ifru.ifru_mtu
404#define ifr_phys ifr_ifru.ifru_phys
405#define ifr_media ifr_ifru.ifru_media
406#define ifr_data ifr_ifru.ifru_data
407#define ifr_devmtu ifr_ifru.ifru_devmtu
408#define ifr_intval ifr_ifru.ifru_intval
409#define ifr_kpi ifr_ifru.ifru_kpi
410#define ifr_wake_flags ifr_ifru.ifru_wake_flags
411};
412
413#endif
414
415#if defined(__Userspace_os_Windows)
416int Win_getifaddrs(struct ifaddrs**);
417#define getifaddrs(interfaces) (int)Win_getifaddrs(interfaces)
418int win_if_nametoindex(const char *);
419#define if_nametoindex(x) win_if_nametoindex(x)
420#endif
421
422#define mtx_lock(arg1)
423#define mtx_unlock(arg1)
424#define mtx_assert(arg1,arg2)
425#define MA_OWNED 7 /* sys/mutex.h typically on FreeBSD */
426#if !defined(__Userspace_os_FreeBSD)
427struct mtx {int dummy;};
428#if !defined(__Userspace_os_NetBSD)
429struct selinfo {int dummy;};
430#endif
431struct sx {int dummy;};
432#endif
433
434#include <stdio.h>
435#include <string.h>
436/* #include <sys/param.h> in FreeBSD defines MSIZE */
437/* #include <sys/ktr.h> */
438/* #include <sys/systm.h> */
439#if defined(HAVE_SYS_QUEUE_H)
440#include <sys/queue.h>
441#else
442#include <user_queue.h>
443#endif
444#include <user_malloc.h>
445/* #include <sys/kernel.h> */
446/* #include <sys/sysctl.h> */
447/* #include <sys/protosw.h> */
448/* on FreeBSD, this results in a redefintion of SOCK(BUF)_(UN)LOCK and
449 * uknown type of struct mtx for sb_mtx in struct sockbuf */
450#include "user_socketvar.h" /* MALLOC_DECLARE's M_PCB. Replacement for sys/socketvar.h */
451/* #include <sys/jail.h> */
452/* #include <sys/sysctl.h> */
453#include <user_environment.h>
454#include <user_atomic.h>
455#include <user_mbuf.h>
456/* #include <sys/uio.h> */
457/* #include <sys/lock.h> */
458#if defined(__FreeBSD__) && __FreeBSD_version > 602000
459#include <sys/rwlock.h>
460#endif
461/* #include <sys/kthread.h> */
462#if defined(__FreeBSD__) && __FreeBSD_version > 602000
463#include <sys/priv.h>
464#endif
465/* #include <sys/random.h> */
466/* #include <sys/limits.h> */
467/* #include <machine/cpu.h> */
468
469#if defined(__Userspace_os_Darwin)
470/* was a 0 byte file. needed for structs if_data(64) and net_event_data */
471#include <net/if_var.h>
472#endif
473#if defined(__Userspace_os_FreeBSD)
474#include <net/if_types.h>
475/* #include <net/if_var.h> was a 0 byte file. causes struct mtx redefinition */
476#endif
477/* OOTB only - dummy route used at the moment. should we port route to
478 * userspace as well? */
479/* on FreeBSD, this results in a redefintion of struct route */
480/* #include <net/route.h> */
481#if !defined(__Userspace_os_Windows) && !defined(__Userspace_os_NaCl)
482#include <net/if.h>
483#include <netinet/in.h>
484#include <netinet/in_systm.h>
485#include <netinet/ip.h>
486#endif
487#if defined(HAVE_NETINET_IP_ICMP_H)
488#include <netinet/ip_icmp.h>
489#else
490#include <user_ip_icmp.h>
491#endif
492/* #include <netinet/in_pcb.h> ported to userspace */
493#include <user_inpcb.h>
494
495/* for getifaddrs */
496#include <sys/types.h>
497#if !defined(__Userspace_os_Windows)
498#if defined(INET) || defined(INET6)
499#include <ifaddrs.h>
500#endif
501
502/* for ioctl */
503#include <sys/ioctl.h>
504
505/* for close, etc. */
506#include <unistd.h>
507#endif
508
509/* lots of errno's used and needed in userspace */
510
511/* for offsetof */
512#include <stddef.h>
513
514#if defined(SCTP_PROCESS_LEVEL_LOCKS) && !defined(__Userspace_os_Windows)
515/* for pthread_mutex_lock, pthread_mutex_unlock, etc. */
516#include <pthread.h>
517#endif
518
519#ifdef IPSEC
520#include <netipsec/ipsec.h>
521#include <netipsec/key.h>
522#endif /* IPSEC */
523
524#ifdef INET6
525#if defined(__Userspace_os_FreeBSD)
526#include <sys/domain.h>
527#endif
528#ifdef IPSEC
529#include <netipsec/ipsec6.h>
530#endif
531#if !defined(__Userspace_os_Windows)
532#include <netinet/ip6.h>
533#endif
534#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)
535#include "user_ip6_var.h"
536#else
537#include <netinet6/ip6_var.h>
538#endif
539#if defined(__Userspace_os_FreeBSD)
540#include <netinet6/in6_pcb.h>
541#include <netinet6/ip6protosw.h>
542/* #include <netinet6/nd6.h> was a 0 byte file */
543#include <netinet6/scope6_var.h>
544#endif
545#endif /* INET6 */
546
547#if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
548#include <sys/file.h>
549#include <sys/filedesc.h>
550#endif
551
552#include "netinet/sctp_sha1.h"
553
554#if __FreeBSD_version >= 700000
555#include <netinet/ip_options.h>
556#endif
557
558#define SCTP_PRINTF(...) \
559 if (SCTP_BASE_VAR(debug_printf)) { \
560 SCTP_BASE_VAR(debug_printf)(__VA_ARGS__); \
561 }
562
563#if defined(__FreeBSD__)
564#ifndef in6pcb
565#define in6pcb inpcb
566#endif
567#endif
568/* Declare all the malloc names for all the various mallocs */
569MALLOC_DECLARE(SCTP_M_MAP);
570MALLOC_DECLARE(SCTP_M_STRMI);
571MALLOC_DECLARE(SCTP_M_STRMO);
572MALLOC_DECLARE(SCTP_M_ASC_ADDR);
573MALLOC_DECLARE(SCTP_M_ASC_IT);
574MALLOC_DECLARE(SCTP_M_AUTH_CL);
575MALLOC_DECLARE(SCTP_M_AUTH_KY);
576MALLOC_DECLARE(SCTP_M_AUTH_HL);
577MALLOC_DECLARE(SCTP_M_AUTH_IF);
578MALLOC_DECLARE(SCTP_M_STRESET);
579MALLOC_DECLARE(SCTP_M_CMSG);
580MALLOC_DECLARE(SCTP_M_COPYAL);
581MALLOC_DECLARE(SCTP_M_VRF);
582MALLOC_DECLARE(SCTP_M_IFA);
583MALLOC_DECLARE(SCTP_M_IFN);
584MALLOC_DECLARE(SCTP_M_TIMW);
585MALLOC_DECLARE(SCTP_M_MVRF);
586MALLOC_DECLARE(SCTP_M_ITER);
587MALLOC_DECLARE(SCTP_M_SOCKOPT);
588
589#if defined(SCTP_LOCAL_TRACE_BUF)
590
591#define SCTP_GET_CYCLECOUNT get_cyclecount()
592#define SCTP_CTR6 sctp_log_trace
593
594#else
595#define SCTP_CTR6 CTR6
596#endif
597
598/* Empty ktr statement for _Userspace__ (similar to what is done for mac) */
599#define CTR6(m, d, p1, p2, p3, p4, p5, p6)
600
601
602
603#define SCTP_BASE_INFO(__m) system_base_info.sctppcbinfo.__m
604#define SCTP_BASE_STATS system_base_info.sctpstat
605#define SCTP_BASE_STAT(__m) system_base_info.sctpstat.__m
606#define SCTP_BASE_SYSCTL(__m) system_base_info.sctpsysctl.__m
607#define SCTP_BASE_VAR(__m) system_base_info.__m
608
609/*
610 *
611 */
612#if !defined(__Userspace_os_Darwin)
613#define USER_ADDR_NULL (NULL) /* FIX ME: temp */
614#endif
615
616#if defined(SCTP_DEBUG)
617#include <netinet/sctp_constants.h>
618#define SCTPDBG(level, ...) \
619{ \
620 do { \
621 if (SCTP_BASE_SYSCTL(sctp_debug_on) & level) { \
622 SCTP_PRINTF(__VA_ARGS__); \
623 } \
624 } while (0); \
625}
626#define SCTPDBG_ADDR(level, addr) \
627{ \
628 do { \
629 if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) { \
630 sctp_print_address(addr); \
631 } \
632 } while (0); \
633}
634#else
635#define SCTPDBG(level, ...)
636#define SCTPDBG_ADDR(level, addr)
637#endif
638
639#ifdef SCTP_LTRACE_CHUNKS
640#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)
641#else
642#define SCTP_LTRACE_CHK(a, b, c, d)
643#endif
644
645#ifdef SCTP_LTRACE_ERRORS
646#define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
647 if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
648 SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
649 (void *)m, (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
650#define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
651 if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
652 SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
653 (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
654#else
655#define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
656#define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
657#endif
658
659
660/*
661 * Local address and interface list handling
662 */
663#define SCTP_MAX_VRF_ID 0
664#define SCTP_SIZE_OF_VRF_HASH 3
665#define SCTP_IFNAMSIZ IFNAMSIZ
666#define SCTP_DEFAULT_VRFID 0
667#define SCTP_VRF_ADDR_HASH_SIZE 16
668#define SCTP_VRF_IFN_HASH_SIZE 3
669#define SCTP_INIT_VRF_TABLEID(vrf)
670
671#if !defined(__Userspace_os_Windows)
672#define SCTP_IFN_IS_IFT_LOOP(ifn) (strncmp((ifn)->ifn_name, "lo", 2) == 0)
673/* BSD definition */
674/* #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) */
675/* only used in IPv6 scenario, which isn't supported yet */
676#define SCTP_ROUTE_IS_REAL_LOOP(ro) 0
677
678/*
679 * Access to IFN's to help with src-addr-selection
680 */
681/* This could return VOID if the index works but for BSD we provide both. */
682#define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
683#define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles... TODO use routing socket to determine */
684#define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
685#endif
686
687/*
688 * general memory allocation
689 */
690#define SCTP_MALLOC(var, type, size, name) \
691 do { \
692 MALLOC(var, type, size, name, M_NOWAIT); \
693 } while (0)
694
695#define SCTP_FREE(var, type) FREE(var, type)
696
697#define SCTP_MALLOC_SONAME(var, type, size) \
698 do { \
699 MALLOC(var, type, size, M_SONAME, (M_WAITOK | M_ZERO)); \
700 } while (0)
701
702#define SCTP_FREE_SONAME(var) FREE(var, M_SONAME)
703
704#define SCTP_PROCESS_STRUCT struct proc *
705
706/*
707 * zone allocation functions
708 */
709
710
711#if defined(SCTP_SIMPLE_ALLOCATOR)
712/*typedef size_t sctp_zone_t;*/
713#define SCTP_ZONE_INIT(zone, name, size, number) { \
714 zone = size; \
715}
716
717/* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
718#define SCTP_ZONE_GET(zone, type) \
719 (type *)malloc(zone);
720
721
722/* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
723#define SCTP_ZONE_FREE(zone, element) { \
724 free(element); \
725}
726
727#define SCTP_ZONE_DESTROY(zone)
728#else
729/*__Userspace__
730 Compiling & linking notes: Needs libumem, which has been placed in ./user_lib
731 All userspace header files are in ./user_include. Makefile will need the
732 following.
733 CFLAGS = -I./ -Wall
734 LDFLAGS = -L./user_lib -R./user_lib -lumem
735*/
736#include "user_include/umem.h"
737
738/* __Userspace__ SCTP_ZONE_INIT: initialize the zone */
739/*
740 __Userspace__
741 No equivalent function to uma_zone_set_max added yet. (See SCTP_ZONE_INIT in sctp_os_bsd.h
742 for reference). It may not be required as mentioned in
743 http://nixdoc.net/man-pages/FreeBSD/uma_zalloc.9.html that
744 max limits may not enforced on systems with more than one CPU.
745*/
746#define SCTP_ZONE_INIT(zone, name, size, number) { \
747 zone = umem_cache_create(name, size, 0, NULL, NULL, NULL, NULL, NULL, 0); \
748 }
749
750/* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
751#define SCTP_ZONE_GET(zone, type) \
752 (type *)umem_cache_alloc(zone, UMEM_DEFAULT);
753
754
755/* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
756#define SCTP_ZONE_FREE(zone, element) \
757 umem_cache_free(zone, element);
758
759
760/* __Userspace__ SCTP_ZONE_DESTROY: destroy the zone */
761#define SCTP_ZONE_DESTROY(zone) \
762 umem_cache_destroy(zone);
763#endif
764
765/*
766 * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland.
767 */
768void *sctp_hashinit_flags(int elements, struct malloc_type *type,
769 u_long *hashmask, int flags);
770void
771sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
772
773void
774sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
775
776
777#define HASH_NOWAIT 0x00000001
778#define HASH_WAITOK 0x00000002
779
780/* M_PCB is MALLOC_DECLARE'd in sys/socketvar.h */
781#define SCTP_HASH_INIT(size, hashmark) sctp_hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
782
783#define SCTP_HASH_FREE(table, hashmark) sctp_hashdestroy(table, M_PCB, hashmark)
784
785#define SCTP_HASH_FREE_DESTROY(table, hashmark) sctp_hashfreedestroy(table, M_PCB, hashmark)
786#define SCTP_M_COPYM m_copym
787
788/*
789 * timers
790 */
791/* __Userspace__
792 * user_sctp_callout.h has typedef struct sctp_callout sctp_os_timer_t;
793 * which is used in the timer related functions such as
794 * SCTP_OS_TIMER_INIT etc.
795*/
796#include <netinet/sctp_callout.h>
797
798/* __Userspace__ Creating a receive thread */
799#include <user_recv_thread.h>
800
801/*__Userspace__ defining KTR_SUBSYS 1 as done in sctp_os_macosx.h */
802#define KTR_SUBSYS 1
803
804/* The packed define for 64 bit platforms */
805#if !defined(__Userspace_os_Windows)
806#define SCTP_PACKED __attribute__((packed))
807#define SCTP_UNUSED __attribute__((unused))
808#else
809#define SCTP_PACKED
810#define SCTP_UNUSED
811#endif
812
813/*
814 * Functions
815 */
816/* Mbuf manipulation and access macros */
817#define SCTP_BUF_LEN(m) (m->m_len)
818#define SCTP_BUF_NEXT(m) (m->m_next)
819#define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
820#define SCTP_BUF_RESV_UF(m, size) m->m_data += size
821#define SCTP_BUF_AT(m, size) m->m_data + size
822#define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
823#define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
824#define SCTP_BUF_TYPE(m) (m->m_type)
825#define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
826#define SCTP_BUF_PREPEND M_PREPEND
827
828#define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
829 MH_ALIGN(m, len); \
830 } else if ((m->m_flags & M_EXT) == 0) { \
831 M_ALIGN(m, len); \
832 }
833
834/* We make it so if you have up to 4 threads
835 * writting based on the default size of
836 * the packet log 65 k, that would be
837 * 4 16k packets before we would hit
838 * a problem.
839 */
840#define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
841
842
843/*
844 * routes, output, etc.
845 */
846
847typedef struct sctp_route sctp_route_t;
848typedef struct sctp_rtentry sctp_rtentry_t;
849
850static inline void sctp_userspace_rtalloc(sctp_route_t *ro)
851{
852 if (ro->ro_rt != NULL) {
853 ro->ro_rt->rt_refcnt++;
854 return;
855 }
856
857 ro->ro_rt = (sctp_rtentry_t *) malloc(sizeof(sctp_rtentry_t));
858 if (ro->ro_rt == NULL)
859 return;
860
861 /* initialize */
862 memset(ro->ro_rt, 0, sizeof(sctp_rtentry_t));
863 ro->ro_rt->rt_refcnt = 1;
864
865 /* set MTU */
866 /* TODO set this based on the ro->ro_dst, looking up MTU with routing socket */
867#if 0
868 if (userspace_rawroute == -1) {
869 userspace_rawroute = socket(AF_ROUTE, SOCK_RAW, 0);
870 if (userspace_rawroute == -1)
871 return;
872 }
873#endif
874 ro->ro_rt->rt_rmx.rmx_mtu = 1500; /* FIXME temporary solution */
875
876 /* TODO enable the ability to obtain interface index of route for
877 * SCTP_GET_IF_INDEX_FROM_ROUTE macro.
878 */
879}
880#define SCTP_RTALLOC(ro, vrf_id, fibnum) sctp_userspace_rtalloc((sctp_route_t *)ro)
881
882/* dummy rtfree needed once user_route.h is included */
883static inline void sctp_userspace_rtfree(sctp_rtentry_t *rt)
884{
885 if(rt == NULL) {
886 return;
887 }
888 if(--rt->rt_refcnt > 0) {
889 return;
890 }
891 free(rt);
892 rt = NULL;
893}
894#define rtfree(arg1) sctp_userspace_rtfree(arg1)
895
896
897/*************************/
898/* MTU */
899/*************************/
900int sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af);
901
902#define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) sctp_userspace_get_mtu_from_ifn(ifn_index, af)
903
904#define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0)
905
906#define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) sctp_userspace_get_mtu_from_ifn(if_nametoindex(((struct ifaddrs *) (sctp_ifn))->ifa_name), AF_INET)
907
908#define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
909 if (rt != NULL) \
910 rt->rt_rmx.rmx_mtu = mtu; \
911 } while(0)
912
913/* (de-)register interface event notifications */
914#define SCTP_REGISTER_INTERFACE(ifhandle, af)
915#define SCTP_DEREGISTER_INTERFACE(ifhandle, af)
916
917
918/*************************/
919/* These are for logging */
920/*************************/
921/* return the base ext data pointer */
922#define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
923 /* return the refcnt of the data pointer */
924#define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
925/* return any buffer related flags, this is
926 * used beyond logging for apple only.
927 */
928#define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
929
930/* For BSD this just accesses the M_PKTHDR length
931 * so it operates on an mbuf with hdr flag. Other
932 * O/S's may have seperate packet header and mbuf
933 * chain pointers.. thus the macro.
934 */
935#define SCTP_HEADER_TO_CHAIN(m) (m)
936#define SCTP_DETACH_HEADER_FROM_CHAIN(m)
937#define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
938#define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
939#define SCTP_RELEASE_HEADER(m)
940#define SCTP_RELEASE_PKT(m) sctp_m_freem(m)
941
942#define SCTP_GET_PKT_VRFID(m, vrf_id) ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
943
944
945
946/* Attach the chain of data into the sendable packet. */
947#define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
948 pak = m; \
949 pak->m_pkthdr.len = packet_length; \
950 } while(0)
951
952/* Other m_pkthdr type things */
953/* FIXME need real definitions */
954#define SCTP_IS_IT_BROADCAST(dst, m) 0
955/* OOTB only #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0) BSD def */
956#define SCTP_IS_IT_LOOPBACK(m) 0
957/* 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
959
960/* This converts any input packet header
961 * into the chain of data holders, for BSD
962 * its a NOP.
963 */
964
965/* get the v6 hop limit */
966#define SCTP_GET_HLIM(inp, ro) 128 /* As done for __Windows__ */
967#define IPv6_HOP_LIMIT 128
968
969/* is the endpoint v6only? */
970#define SCTP_IPV6_V6ONLY(inp) (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
971/* is the socket non-blocking? */
972#define SCTP_SO_IS_NBIO(so) ((so)->so_state & SS_NBIO)
973#define SCTP_SET_SO_NBIO(so) ((so)->so_state |= SS_NBIO)
974#define SCTP_CLEAR_SO_NBIO(so) ((so)->so_state &= ~SS_NBIO)
975/* get the socket type */
976#define SCTP_SO_TYPE(so) ((so)->so_type)
977
978/* reserve sb space for a socket */
979#define SCTP_SORESERVE(so, send, recv) soreserve(so, send, recv)
980
981/* wakeup a socket */
982#define SCTP_SOWAKEUP(so) wakeup(&(so)->so_timeo, so)
983/* clear the socket buffer state */
984#define SCTP_SB_CLEAR(sb) \
985 (sb).sb_cc = 0; \
986 (sb).sb_mb = NULL; \
987 (sb).sb_mbcnt = 0;
988
989#define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
990#define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
991
992/* Future zero copy wakeup/send function */
993#define SCTP_ZERO_COPY_EVENT(inp, so)
994/* This is re-pulse ourselves for sendbuf */
995#define SCTP_ZERO_COPY_SENDQ_EVENT(inp, so)
996
997#define SCTP_READ_RANDOM(buf, len) read_random(buf, len)
998
999#define SCTP_SHA1_CTX struct sctp_sha1_context
1000#define SCTP_SHA1_INIT sctp_sha1_init
1001#define SCTP_SHA1_UPDATE sctp_sha1_update
1002#define SCTP_SHA1_FINAL(x,y) sctp_sha1_final((unsigned char *)x, y)
1003
1004/* start OOTB only stuff */
1005/* TODO IFT_LOOP is in net/if_types.h on Linux */
1006#define IFT_LOOP 0x18
1007
1008/* sctp_pcb.h */
1009
1010#if defined(__Userspace_os_Windows)
1011#define SHUT_RD 1
1012#define SHUT_WR 2
1013#define SHUT_RDWR 3
1014#endif
1015#define PRU_FLUSH_RD SHUT_RD
1016#define PRU_FLUSH_WR SHUT_WR
1017#define PRU_FLUSH_RDWR SHUT_RDWR
1018
1019/* netinet/ip_var.h defintions are behind an if defined for _KERNEL on FreeBSD */
1020#define IP_RAWOUTPUT 0x2
1021
1022
1023/* end OOTB only stuff */
1024
1025#define AF_CONN 123
1026struct sockaddr_conn {
1027#ifdef HAVE_SCONN_LEN
1028 uint8_t sconn_len;
1029 uint8_t sconn_family;
1030#else
1031 uint16_t sconn_family;
1032#endif
1033 uint16_t sconn_port;
1034 void *sconn_addr;
1035};
1036
1037typedef void *(*start_routine_t)(void *);
1038
1039extern int
1040sctp_userspace_thread_create(userland_thread_t *thread, start_routine_t start_routine);
1041
1042void
1043sctp_userspace_set_threadname(const char *name);
1044
1045/*
1046 * SCTP protocol specific mbuf flags.
1047 */
1048#define M_NOTIFICATION M_PROTO5 /* SCTP notification */
1049
1050/*
1051 * IP output routines
1052 */
1053
1054/* Defining SCTP_IP_ID macro.
1055 In netinet/ip_output.c, we have u_short ip_id;
1056 In netinet/ip_var.h, we have extern u_short ip_id; (enclosed within _KERNEL_)
1057 See static __inline uint16_t ip_newid(void) in netinet/ip_var.h
1058 */
1059#define SCTP_IP_ID(inp) (ip_id)
1060
1061/* need sctphdr to get port in SCTP_IP_OUTPUT. sctphdr defined in sctp.h */
1062#include <netinet/sctp.h>
1063extern void sctp_userspace_ip_output(int *result, struct mbuf *o_pak,
1064 sctp_route_t *ro, void *stcb,
1065 uint32_t vrf_id);
1066
1067#define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) sctp_userspace_ip_output(&result, o_pak, ro, stcb, vrf_id);
1068
1069#if defined(INET6)
1070extern void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak,
1071 struct route_in6 *ro, void *stcb,
1072 uint32_t vrf_id);
1073#define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) sctp_userspace_ip6_output(&result, o_pak, ro, stcb, vrf_id);
1074#endif
1075
1076
1077
1078#if 0
1079#define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \
1080{ \
1081 if (stcb && stcb->sctp_ep) \
1082 result = ip6_output(o_pak, \
1083 ((struct in6pcb *)(stcb->sctp_ep))->in6p_outputopts, \
1084 (ro), 0, 0, ifp, NULL); \
1085 else \
1086 result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
1087}
1088#endif
1089
1090struct mbuf *
1091sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header, int how, int allonebuf, int type);
1092
1093
1094/* with the current included files, this is defined in Linux but
1095 * in FreeBSD, it is behind a _KERNEL in sys/socket.h ...
1096 */
1097#if defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_OpenBSD) || defined(__Userspace_os_NaCl)
1098/* stolen from /usr/include/sys/socket.h */
1099#define CMSG_ALIGN(n) _ALIGN(n)
1100#elif defined(__Userspace_os_NetBSD)
1101#define CMSG_ALIGN(n) (((n) + __ALIGNBYTES) & ~__ALIGNBYTES)
1102#elif defined(__Userspace_os_Darwin)
1103#if !defined(__DARWIN_ALIGNBYTES)
1104#define __DARWIN_ALIGNBYTES (sizeof(__darwin_size_t) - 1)
1105#endif
1106
1107#if !defined(__DARWIN_ALIGN)
1108#define __DARWIN_ALIGN(p) ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES) &~ __DARWIN_ALIGNBYTES)
1109#endif
1110
1111#if !defined(__DARWIN_ALIGNBYTES32)
1112#define __DARWIN_ALIGNBYTES32 (sizeof(__uint32_t) - 1)
1113#endif
1114
1115#if !defined(__DARWIN_ALIGN32)
1116#define __DARWIN_ALIGN32(p) ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES32) &~ __DARWIN_ALIGNBYTES32)
1117#endif
1118#define CMSG_ALIGN(n) __DARWIN_ALIGN32(n)
1119#endif
1120#define I_AM_HERE \
1121 do { \
1122 SCTP_PRINTF("%s:%d at %s\n", __FILE__, __LINE__ , __func__); \
1123 } while (0)
1124
1125#ifndef timevalsub
1126#define timevalsub(tp1, tp2) \
1127 do { \
1128 (tp1)->tv_sec -= (tp2)->tv_sec; \
1129 (tp1)->tv_usec -= (tp2)->tv_usec; \
1130 if ((tp1)->tv_usec < 0) { \
1131 (tp1)->tv_sec--; \
1132 (tp1)->tv_usec += 1000000; \
1133 } \
1134 } while (0)
1135#endif
1136
1137#if defined(__Userspace_os_Linux)
1138#if !defined(TAILQ_FOREACH_SAFE)
1139#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
1140 for ((var) = ((head)->tqh_first); \
1141 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
1142 (var) = (tvar))
1143#endif
1144#if !defined(LIST_FOREACH_SAFE)
1145#define LIST_FOREACH_SAFE(var, head, field, tvar) \
1146 for ((var) = ((head)->lh_first); \
1147 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
1148 (var) = (tvar))
1149#endif
1150#endif
1151#if defined(__Userspace_os_DragonFly)
1152#define TAILQ_FOREACH_SAFE TAILQ_FOREACH_MUTABLE
1153#define LIST_FOREACH_SAFE LIST_FOREACH_MUTABLE
1154#endif
1155
1156#if defined(__Userspace_os_NaCl)
1157#define timercmp(tvp, uvp, cmp) \
1158 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1159 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1160 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1161#endif
1162
1163#endif