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