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