• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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__)
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>  in FreeBSD defines MSIZE */
413 /* #include <sys/ktr.h> */
414 /* #include <sys/systm.h> */
415 #if defined(HAVE_SYS_QUEUE_H)
416 #include <sys/queue.h>
417 #else
418 #include <user_queue.h>
419 #endif
420 #include <user_malloc.h>
421 /* #include <sys/kernel.h> */
422 /* #include <sys/sysctl.h> */
423 /* #include <sys/protosw.h> */
424 /* on FreeBSD, this results in a redefintion of SOCK(BUF)_(UN)LOCK and
425  *  uknown type of struct mtx for sb_mtx in struct sockbuf */
426 #include "user_socketvar.h" /* MALLOC_DECLARE's M_PCB. Replacement for sys/socketvar.h */
427 /* #include <sys/jail.h> */
428 /* #include <sys/sysctl.h> */
429 #include <user_environment.h>
430 #include <user_atomic.h>
431 #include <user_mbuf.h>
432 /* #include <sys/uio.h> */
433 /* #include <sys/lock.h> */
434 #if defined(__FreeBSD__) && !defined(__Userspace__)
435 #include <sys/rwlock.h>
436 #endif
437 /* #include <sys/kthread.h> */
438 #if defined(__FreeBSD__) && !defined(__Userspace__)
439 #include <sys/priv.h>
440 #endif
441 /* #include <sys/random.h> */
442 #include <limits.h>
443 /* #include <machine/cpu.h> */
444 
445 #if defined(__APPLE__)
446 /* was a 0 byte file.  needed for structs if_data(64) and net_event_data */
447 #include <net/if_var.h>
448 #endif
449 #if defined(__FreeBSD__)
450 #include <net/if_types.h>
451 /* #include <net/if_var.h> was a 0 byte file.  causes struct mtx redefinition */
452 #endif
453 /* OOTB only - dummy route used at the moment. should we port route to
454  *  userspace as well? */
455 /* on FreeBSD, this results in a redefintion of struct route */
456 /* #include <net/route.h> */
457 #if !defined(_WIN32) && !defined(__native_client__)
458 #include <net/if.h>
459 #include <netinet/in.h>
460 #include <netinet/in_systm.h>
461 #include <netinet/ip.h>
462 #endif
463 #if defined(HAVE_NETINET_IP_ICMP_H)
464 #include <netinet/ip_icmp.h>
465 #else
466 #include <user_ip_icmp.h>
467 #endif
468 /* #include <netinet/in_pcb.h> ported to userspace */
469 #include <user_inpcb.h>
470 
471 /* for getifaddrs */
472 #include <sys/types.h>
473 #if !defined(_WIN32)
474 #if defined(INET) || defined(INET6)
475 #include <ifaddrs.h>
476 #endif
477 
478 /* for ioctl */
479 #include <sys/ioctl.h>
480 
481 /* for close, etc. */
482 #include <unistd.h>
483 /* for gettimeofday */
484 #include <sys/time.h>
485 #endif
486 
487 /* lots of errno's used and needed in userspace */
488 
489 /* for offsetof */
490 #include <stddef.h>
491 
492 #if defined(SCTP_PROCESS_LEVEL_LOCKS) && !defined(_WIN32)
493 /* for pthread_mutex_lock, pthread_mutex_unlock, etc. */
494 #include <pthread.h>
495 #endif
496 
497 #ifdef IPSEC
498 #include <netipsec/ipsec.h>
499 #include <netipsec/key.h>
500 #endif				/* IPSEC */
501 
502 #ifdef INET6
503 #if defined(__FreeBSD__)
504 #include <sys/domain.h>
505 #endif
506 #ifdef IPSEC
507 #include <netipsec/ipsec6.h>
508 #endif
509 #if !defined(_WIN32)
510 #include <netinet/ip6.h>
511 #endif
512 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(_WIN32)
513 #include "user_ip6_var.h"
514 #else
515 #include <netinet6/ip6_var.h>
516 #endif
517 #if defined(__FreeBSD__)
518 #include <netinet6/in6_pcb.h>
519 #include <netinet6/ip6protosw.h>
520 /* #include <netinet6/nd6.h> was a 0 byte file */
521 #include <netinet6/scope6_var.h>
522 #endif
523 #endif /* INET6 */
524 
525 #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
526 #include <sys/file.h>
527 #include <sys/filedesc.h>
528 #endif
529 
530 #include "netinet/sctp_sha1.h"
531 
532 #if defined(__FreeBSD__) && !defined(__Userspace__)
533 #include <netinet/ip_options.h>
534 #endif
535 
536 #define SCTP_PRINTF(...)                                  \
537 	if (SCTP_BASE_VAR(debug_printf)) {                \
538 		SCTP_BASE_VAR(debug_printf)(__VA_ARGS__); \
539 	}
540 
541 /* Declare all the malloc names for all the various mallocs */
542 MALLOC_DECLARE(SCTP_M_MAP);
543 MALLOC_DECLARE(SCTP_M_STRMI);
544 MALLOC_DECLARE(SCTP_M_STRMO);
545 MALLOC_DECLARE(SCTP_M_ASC_ADDR);
546 MALLOC_DECLARE(SCTP_M_ASC_IT);
547 MALLOC_DECLARE(SCTP_M_AUTH_CL);
548 MALLOC_DECLARE(SCTP_M_AUTH_KY);
549 MALLOC_DECLARE(SCTP_M_AUTH_HL);
550 MALLOC_DECLARE(SCTP_M_AUTH_IF);
551 MALLOC_DECLARE(SCTP_M_STRESET);
552 MALLOC_DECLARE(SCTP_M_CMSG);
553 MALLOC_DECLARE(SCTP_M_COPYAL);
554 MALLOC_DECLARE(SCTP_M_VRF);
555 MALLOC_DECLARE(SCTP_M_IFA);
556 MALLOC_DECLARE(SCTP_M_IFN);
557 MALLOC_DECLARE(SCTP_M_TIMW);
558 MALLOC_DECLARE(SCTP_M_MVRF);
559 MALLOC_DECLARE(SCTP_M_ITER);
560 MALLOC_DECLARE(SCTP_M_SOCKOPT);
561 
562 #if defined(SCTP_LOCAL_TRACE_BUF)
563 
564 #define SCTP_GET_CYCLECOUNT get_cyclecount()
565 #define SCTP_CTR6 sctp_log_trace
566 
567 #else
568 #define SCTP_CTR6 CTR6
569 #endif
570 
571 /* Empty ktr statement for _Userspace__ (similar to what is done for mac) */
572 #define	CTR6(m, d, p1, p2, p3, p4, p5, p6)
573 
574 
575 
576 #define SCTP_BASE_INFO(__m) system_base_info.sctppcbinfo.__m
577 #define SCTP_BASE_STATS system_base_info.sctpstat
578 #define SCTP_BASE_STAT(__m)     system_base_info.sctpstat.__m
579 #define SCTP_BASE_SYSCTL(__m) system_base_info.sctpsysctl.__m
580 #define SCTP_BASE_VAR(__m) system_base_info.__m
581 
582 /*
583  *
584  */
585 #if !defined(__APPLE__)
586 #define USER_ADDR_NULL	(NULL)		/* FIX ME: temp */
587 #endif
588 
589 #include <netinet/sctp_constants.h>
590 #if defined(SCTP_DEBUG)
591 #define SCTPDBG(level, ...)					\
592 {								\
593 	do {							\
594 		if (SCTP_BASE_SYSCTL(sctp_debug_on) & level) {	\
595 			SCTP_PRINTF(__VA_ARGS__);		\
596 		}						\
597 	} while (0);						\
598 }
599 #define SCTPDBG_ADDR(level, addr)				\
600 {								\
601 	do {							\
602 		if (SCTP_BASE_SYSCTL(sctp_debug_on) & level ) {	\
603 		    sctp_print_address(addr);			\
604 		}						\
605 	} while (0);						\
606 }
607 #else
608 #define SCTPDBG(level, ...)
609 #define SCTPDBG_ADDR(level, addr)
610 #endif
611 
612 #ifdef SCTP_LTRACE_CHUNKS
613 #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)
614 #else
615 #define SCTP_LTRACE_CHK(a, b, c, d)
616 #endif
617 
618 #ifdef SCTP_LTRACE_ERRORS
619 #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err) \
620 	if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
621 		SCTP_PRINTF("mbuf:%p inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
622 		            (void *)m, (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
623 #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err) \
624 	if (sctp_logging_level & SCTP_LTRACE_ERROR_ENABLE) \
625 		SCTP_PRINTF("inp:%p stcb:%p net:%p file:%x line:%d error:%d\n", \
626 		            (void *)inp, (void *)stcb, (void *)net, file, __LINE__, err);
627 #else
628 #define SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, file, err)
629 #define SCTP_LTRACE_ERR_RET(inp, stcb, net, file, err)
630 #endif
631 
632 
633 /*
634  * Local address and interface list handling
635  */
636 #define SCTP_MAX_VRF_ID		0
637 #define SCTP_SIZE_OF_VRF_HASH	3
638 #define SCTP_IFNAMSIZ		IFNAMSIZ
639 #define SCTP_DEFAULT_VRFID	0
640 #define SCTP_VRF_ADDR_HASH_SIZE	16
641 #define SCTP_VRF_IFN_HASH_SIZE	3
642 #define	SCTP_INIT_VRF_TABLEID(vrf)
643 
644 #if !defined(_WIN32)
645 #define SCTP_IFN_IS_IFT_LOOP(ifn) (strncmp((ifn)->ifn_name, "lo", 2) == 0)
646 /* BSD definition */
647 /* #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) */
648 /* only used in IPv6 scenario, which isn't supported yet */
649 #define SCTP_ROUTE_IS_REAL_LOOP(ro) 0
650 
651 /*
652  * Access to IFN's to help with src-addr-selection
653  */
654 /* This could return VOID if the index works but for BSD we provide both. */
655 #define SCTP_GET_IFN_VOID_FROM_ROUTE(ro) (void *)ro->ro_rt->rt_ifp
656 #define SCTP_GET_IF_INDEX_FROM_ROUTE(ro) 1 /* compiles...  TODO use routing socket to determine */
657 #define SCTP_ROUTE_HAS_VALID_IFN(ro) ((ro)->ro_rt && (ro)->ro_rt->rt_ifp)
658 #endif
659 
660 /*
661  * general memory allocation
662  */
663 #define SCTP_MALLOC(var, type, size, name)				\
664 	do {								\
665 		MALLOC(var, type, size, name, M_NOWAIT);		\
666 	} while (0)
667 
668 #define SCTP_FREE(var, type)	FREE(var, type)
669 
670 #define SCTP_MALLOC_SONAME(var, type, size)				\
671 	do {								\
672 		MALLOC(var, type, size, M_SONAME, (M_WAITOK | M_ZERO));	\
673 	} while (0)
674 
675 #define SCTP_FREE_SONAME(var)	FREE(var, M_SONAME)
676 
677 #define SCTP_PROCESS_STRUCT struct proc *
678 
679 /*
680  * zone allocation functions
681  */
682 
683 
684 #if defined(SCTP_SIMPLE_ALLOCATOR)
685 /*typedef size_t sctp_zone_t;*/
686 #define SCTP_ZONE_INIT(zone, name, size, number) { \
687 	zone = size; \
688 }
689 
690 /* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
691 #define SCTP_ZONE_GET(zone, type)  \
692         (type *)malloc(zone);
693 
694 
695 /* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
696 #define SCTP_ZONE_FREE(zone, element) { \
697 	free(element);  \
698 }
699 
700 #define SCTP_ZONE_DESTROY(zone)
701 #else
702 /*__Userspace__
703   Compiling & linking notes: Needs libumem, which has been placed in ./user_lib
704   All userspace header files are in ./user_include. Makefile will need the
705   following.
706   CFLAGS = -I./ -Wall
707   LDFLAGS = -L./user_lib -R./user_lib -lumem
708 */
709 #include "user_include/umem.h"
710 
711 /* __Userspace__ SCTP_ZONE_INIT: initialize the zone */
712 /*
713   __Userspace__
714   No equivalent function to uma_zone_set_max added yet. (See SCTP_ZONE_INIT in sctp_os_bsd.h
715   for reference). It may not be required as mentioned in
716   http://nixdoc.net/man-pages/FreeBSD/uma_zalloc.9.html that
717   max limits may not enforced on systems with more than one CPU.
718 */
719 #define SCTP_ZONE_INIT(zone, name, size, number) { \
720 	zone = umem_cache_create(name, size, 0, NULL, NULL, NULL, NULL, NULL, 0); \
721   }
722 
723 /* __Userspace__ SCTP_ZONE_GET: allocate element from the zone */
724 #define SCTP_ZONE_GET(zone, type) \
725         (type *)umem_cache_alloc(zone, UMEM_DEFAULT);
726 
727 
728 /* __Userspace__ SCTP_ZONE_FREE: free element from the zone */
729 #define SCTP_ZONE_FREE(zone, element) \
730 	umem_cache_free(zone, element);
731 
732 
733 /* __Userspace__ SCTP_ZONE_DESTROY: destroy the zone */
734 #define SCTP_ZONE_DESTROY(zone) \
735 	umem_cache_destroy(zone);
736 #endif
737 
738 /*
739  * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland.
740  */
741 void *sctp_hashinit_flags(int elements, struct malloc_type *type,
742                     u_long *hashmask, int flags);
743 void
744 sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
745 
746 void
747 sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
748 
749 
750 #define HASH_NOWAIT 0x00000001
751 #define HASH_WAITOK 0x00000002
752 
753 /* M_PCB is MALLOC_DECLARE'd in sys/socketvar.h */
754 #define SCTP_HASH_INIT(size, hashmark) sctp_hashinit_flags(size, M_PCB, hashmark, HASH_NOWAIT)
755 
756 #define SCTP_HASH_FREE(table, hashmark) sctp_hashdestroy(table, M_PCB, hashmark)
757 
758 #define SCTP_HASH_FREE_DESTROY(table, hashmark)  sctp_hashfreedestroy(table, M_PCB, hashmark)
759 #define SCTP_M_COPYM	m_copym
760 
761 /*
762  * timers
763  */
764 /* __Userspace__
765  * user_sctp_callout.h has typedef struct sctp_callout sctp_os_timer_t;
766  * which is used in the timer related functions such as
767  * SCTP_OS_TIMER_INIT etc.
768 */
769 #include <netinet/sctp_callout.h>
770 
771 /* __Userspace__ Creating a receive thread */
772 #include <user_recv_thread.h>
773 
774 /*__Userspace__ defining KTR_SUBSYS 1 as done in sctp_os_macosx.h */
775 #define KTR_SUBSYS 1
776 
777 /* The packed define for 64 bit platforms */
778 #if !defined(_WIN32)
779 #define SCTP_PACKED __attribute__((packed))
780 #define SCTP_UNUSED __attribute__((unused))
781 #else
782 #define SCTP_PACKED
783 #define SCTP_UNUSED
784 #endif
785 
786 /*
787  * Functions
788  */
789 /* Mbuf manipulation and access macros  */
790 #define SCTP_BUF_LEN(m) (m->m_len)
791 #define SCTP_BUF_NEXT(m) (m->m_next)
792 #define SCTP_BUF_NEXT_PKT(m) (m->m_nextpkt)
793 #define SCTP_BUF_RESV_UF(m, size) m->m_data += size
794 #define SCTP_BUF_AT(m, size) m->m_data + size
795 #define SCTP_BUF_IS_EXTENDED(m) (m->m_flags & M_EXT)
796 #define SCTP_BUF_EXTEND_SIZE(m) (m->m_ext.ext_size)
797 #define SCTP_BUF_TYPE(m) (m->m_type)
798 #define SCTP_BUF_RECVIF(m) (m->m_pkthdr.rcvif)
799 #define SCTP_BUF_PREPEND	M_PREPEND
800 
801 #define SCTP_ALIGN_TO_END(m, len) if(m->m_flags & M_PKTHDR) { \
802                                      MH_ALIGN(m, len); \
803                                   } else if ((m->m_flags & M_EXT) == 0) { \
804                                      M_ALIGN(m, len); \
805                                   }
806 
807 #if !defined(_WIN32)
808 #define SCTP_SNPRINTF(data, ...)						\
809 	if (snprintf(data, __VA_ARGS__) < 0) {					\
810 		data[0] = '\0';							\
811 	}
812 #endif
813 
814 /* We make it so if you have up to 4 threads
815  * writting based on the default size of
816  * the packet log 65 k, that would be
817  * 4 16k packets before we would hit
818  * a problem.
819  */
820 #define SCTP_PKTLOG_WRITERS_NEED_LOCK 3
821 
822 
823 /*
824  * routes, output, etc.
825  */
826 
827 typedef struct sctp_route	sctp_route_t;
828 typedef struct sctp_rtentry	sctp_rtentry_t;
829 
sctp_userspace_rtalloc(sctp_route_t * ro)830 static inline void sctp_userspace_rtalloc(sctp_route_t *ro)
831 {
832 	if (ro->ro_rt != NULL) {
833 		ro->ro_rt->rt_refcnt++;
834 		return;
835 	}
836 
837 	ro->ro_rt = (sctp_rtentry_t *) malloc(sizeof(sctp_rtentry_t));
838 	if (ro->ro_rt == NULL)
839 		return;
840 
841 	/* initialize */
842 	memset(ro->ro_rt, 0, sizeof(sctp_rtentry_t));
843 	ro->ro_rt->rt_refcnt = 1;
844 
845 	/* set MTU */
846 	/* TODO set this based on the ro->ro_dst, looking up MTU with routing socket */
847 #if 0
848 	if (userspace_rawroute == -1) {
849 		userspace_rawroute = socket(AF_ROUTE, SOCK_RAW, 0);
850 		if (userspace_rawroute == -1)
851 			return;
852 	}
853 #endif
854 	ro->ro_rt->rt_rmx.rmx_mtu = 1500; /* FIXME temporary solution */
855 
856 	/* TODO enable the ability to obtain interface index of route for
857 	 *  SCTP_GET_IF_INDEX_FROM_ROUTE macro.
858 	 */
859 }
860 #define SCTP_RTALLOC(ro, vrf_id, fibnum) sctp_userspace_rtalloc((sctp_route_t *)ro)
861 
862 /* dummy rtfree needed once user_route.h is included */
sctp_userspace_rtfree(sctp_rtentry_t * rt)863 static inline void sctp_userspace_rtfree(sctp_rtentry_t *rt)
864 {
865 	if(rt == NULL) {
866 		return;
867 	}
868 	if(--rt->rt_refcnt > 0) {
869 		return;
870 	}
871 	free(rt);
872 }
873 #define rtfree(arg1) sctp_userspace_rtfree(arg1)
874 
875 
876 /*************************/
877 /*      MTU              */
878 /*************************/
879 int sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af);
880 
881 #define SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, af) sctp_userspace_get_mtu_from_ifn(ifn_index, af)
882 
883 #define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0)
884 
885 #define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn)  sctp_userspace_get_mtu_from_ifn(if_nametoindex(((struct ifaddrs *) (sctp_ifn))->ifa_name), AF_INET)
886 
887 #define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
888                                               if (rt != NULL) \
889                                                  rt->rt_rmx.rmx_mtu = mtu; \
890                                            } while(0)
891 
892 
893 /*************************/
894 /* These are for logging */
895 /*************************/
896 /* return the base ext data pointer */
897 #define SCTP_BUF_EXTEND_BASE(m) (m->m_ext.ext_buf)
898  /* return the refcnt of the data pointer */
899 #define SCTP_BUF_EXTEND_REFCNT(m) (*m->m_ext.ref_cnt)
900 /* return any buffer related flags, this is
901  * used beyond logging for apple only.
902  */
903 #define SCTP_BUF_GET_FLAGS(m) (m->m_flags)
904 
905 /* For BSD this just accesses the M_PKTHDR length
906  * so it operates on an mbuf with hdr flag. Other
907  * O/S's may have seperate packet header and mbuf
908  * chain pointers.. thus the macro.
909  */
910 #define SCTP_HEADER_TO_CHAIN(m) (m)
911 #define SCTP_DETACH_HEADER_FROM_CHAIN(m)
912 #define SCTP_HEADER_LEN(m) ((m)->m_pkthdr.len)
913 #define SCTP_GET_HEADER_FOR_OUTPUT(o_pak) 0
914 #define SCTP_RELEASE_HEADER(m)
915 #define SCTP_RELEASE_PKT(m)	sctp_m_freem(m)
916 
917 #define SCTP_GET_PKT_VRFID(m, vrf_id)  ((vrf_id = SCTP_DEFAULT_VRFID) != SCTP_DEFAULT_VRFID)
918 
919 
920 
921 /* Attach the chain of data into the sendable packet. */
922 #define SCTP_ATTACH_CHAIN(pak, m, packet_length) do { \
923                                                   pak = m; \
924                                                   pak->m_pkthdr.len = packet_length; \
925                           } while(0)
926 
927 /* Other m_pkthdr type things */
928 /* FIXME need real definitions */
929 #define SCTP_IS_IT_BROADCAST(dst, m) 0
930 /* OOTB only #define SCTP_IS_IT_BROADCAST(dst, m) ((m->m_flags & M_PKTHDR) ? in_broadcast(dst, m->m_pkthdr.rcvif) : 0)  BSD def */
931 #define SCTP_IS_IT_LOOPBACK(m) 0
932 /* 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 */
933 
934 
935 /* This converts any input packet header
936  * into the chain of data holders, for BSD
937  * its a NOP.
938  */
939 
940 /* get the v6 hop limit */
941 #define SCTP_GET_HLIM(inp, ro) 128
942 #define IPv6_HOP_LIMIT 128
943 
944 /* is the endpoint v6only? */
945 #define SCTP_IPV6_V6ONLY(sctp_inpcb)	((sctp_inpcb)->ip_inp.inp.inp_flags & IN6P_IPV6_V6ONLY)
946 /* is the socket non-blocking? */
947 #define SCTP_SO_IS_NBIO(so)	((so)->so_state & SS_NBIO)
948 #define SCTP_SET_SO_NBIO(so)	((so)->so_state |= SS_NBIO)
949 #define SCTP_CLEAR_SO_NBIO(so)	((so)->so_state &= ~SS_NBIO)
950 /* get the socket type */
951 #define SCTP_SO_TYPE(so)	((so)->so_type)
952 
953 /* reserve sb space for a socket */
954 #define SCTP_SORESERVE(so, send, recv)	soreserve(so, send, recv)
955 
956 /* wakeup a socket */
957 #define SCTP_SOWAKEUP(so)	wakeup(&(so)->so_timeo, so)
958 /* clear the socket buffer state */
959 #define SCTP_SB_CLEAR(sb)	\
960 	(sb).sb_cc = 0;		\
961 	(sb).sb_mb = NULL;	\
962 	(sb).sb_mbcnt = 0;
963 
964 #define SCTP_SB_LIMIT_RCV(so) so->so_rcv.sb_hiwat
965 #define SCTP_SB_LIMIT_SND(so) so->so_snd.sb_hiwat
966 
967 #define SCTP_READ_RANDOM(buf, len)	read_random(buf, len)
968 
969 #define SCTP_SHA1_CTX		struct sctp_sha1_context
970 #define SCTP_SHA1_INIT		sctp_sha1_init
971 #define SCTP_SHA1_UPDATE	sctp_sha1_update
972 #define SCTP_SHA1_FINAL(x,y)	sctp_sha1_final((unsigned char *)x, y)
973 
974 /* start OOTB only stuff */
975 /* TODO IFT_LOOP is in net/if_types.h on Linux */
976 #define IFT_LOOP 0x18
977 
978 /* sctp_pcb.h */
979 
980 #if defined(_WIN32)
981 #define SHUT_RD 1
982 #define SHUT_WR 2
983 #define SHUT_RDWR 3
984 #endif
985 #define PRU_FLUSH_RD SHUT_RD
986 #define PRU_FLUSH_WR SHUT_WR
987 #define PRU_FLUSH_RDWR SHUT_RDWR
988 
989 /* netinet/ip_var.h defintions are behind an if defined for _KERNEL on FreeBSD */
990 #define	IP_RAWOUTPUT		0x2
991 
992 
993 /* end OOTB only stuff */
994 
995 #define AF_CONN 123
996 struct sockaddr_conn {
997 #ifdef HAVE_SCONN_LEN
998 	uint8_t sconn_len;
999 	uint8_t sconn_family;
1000 #else
1001 	uint16_t sconn_family;
1002 #endif
1003 	uint16_t sconn_port;
1004 	void *sconn_addr;
1005 };
1006 
1007 typedef void *(*start_routine_t)(void *);
1008 
1009 extern int
1010 sctp_userspace_thread_create(userland_thread_t *thread, start_routine_t start_routine);
1011 
1012 void
1013 sctp_userspace_set_threadname(const char *name);
1014 
1015 /*
1016  * SCTP protocol specific mbuf flags.
1017  */
1018 #define	M_NOTIFICATION		M_PROTO5	/* SCTP notification */
1019 
1020 /*
1021  * IP output routines
1022  */
1023 
1024 /* Defining SCTP_IP_ID macro.
1025    In netinet/ip_output.c, we have u_short ip_id;
1026    In netinet/ip_var.h, we have extern u_short	ip_id; (enclosed within _KERNEL_)
1027    See static __inline uint16_t ip_newid(void) in netinet/ip_var.h
1028  */
1029 #define SCTP_IP_ID(inp) (ip_id)
1030 
1031 /* need sctphdr to get port in SCTP_IP_OUTPUT. sctphdr defined in sctp.h  */
1032 #include <netinet/sctp.h>
1033 extern void sctp_userspace_ip_output(int *result, struct mbuf *o_pak,
1034                                      sctp_route_t *ro, void *stcb,
1035                                      uint32_t vrf_id);
1036 
1037 #define SCTP_IP_OUTPUT(result, o_pak, ro, stcb, vrf_id) sctp_userspace_ip_output(&result, o_pak, ro, stcb, vrf_id);
1038 
1039 #if defined(INET6)
1040 extern void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak,
1041                                       struct route_in6 *ro, void *stcb,
1042                                       uint32_t vrf_id);
1043 #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) sctp_userspace_ip6_output(&result, o_pak, ro, stcb, vrf_id);
1044 #endif
1045 
1046 
1047 
1048 #if 0
1049 #define SCTP_IP6_OUTPUT(result, o_pak, ro, ifp, stcb, vrf_id) \
1050 { \
1051 	if (stcb && stcb->sctp_ep) \
1052 		result = ip6_output(o_pak, \
1053 				    ((struct inpcb *)(stcb->sctp_ep))->in6p_outputopts, \
1054 				    (ro), 0, 0, ifp, NULL); \
1055 	else \
1056 		result = ip6_output(o_pak, NULL, (ro), 0, 0, ifp, NULL); \
1057 }
1058 #endif
1059 
1060 struct mbuf *
1061 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header, int how, int allonebuf, int type);
1062 
1063 
1064 /* with the current included files, this is defined in Linux but
1065  *  in FreeBSD, it is behind a _KERNEL in sys/socket.h ...
1066  */
1067 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__native_client__)
1068 /* stolen from /usr/include/sys/socket.h */
1069 #define CMSG_ALIGN(n)   _ALIGN(n)
1070 #elif defined(__NetBSD__)
1071 #define CMSG_ALIGN(n)   (((n) + __ALIGNBYTES) & ~__ALIGNBYTES)
1072 #elif defined(__APPLE__)
1073 #if !defined(__DARWIN_ALIGNBYTES)
1074 #define	__DARWIN_ALIGNBYTES	(sizeof(__darwin_size_t) - 1)
1075 #endif
1076 
1077 #if !defined(__DARWIN_ALIGN)
1078 #define	__DARWIN_ALIGN(p)	((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES) &~ __DARWIN_ALIGNBYTES)
1079 #endif
1080 
1081 #if !defined(__DARWIN_ALIGNBYTES32)
1082 #define __DARWIN_ALIGNBYTES32     (sizeof(__uint32_t) - 1)
1083 #endif
1084 
1085 #if !defined(__DARWIN_ALIGN32)
1086 #define __DARWIN_ALIGN32(p)       ((__darwin_size_t)((char *)(uintptr_t)(p) + __DARWIN_ALIGNBYTES32) &~ __DARWIN_ALIGNBYTES32)
1087 #endif
1088 #define CMSG_ALIGN(n)   __DARWIN_ALIGN32(n)
1089 #endif
1090 #define I_AM_HERE \
1091                 do { \
1092 			SCTP_PRINTF("%s:%d at %s\n", __FILE__, __LINE__ , __func__); \
1093 		} while (0)
1094 
1095 #ifndef timevalsub
1096 #define timevalsub(tp1, tp2)                       \
1097 	do {                                       \
1098 		(tp1)->tv_sec -= (tp2)->tv_sec;    \
1099 		(tp1)->tv_usec -= (tp2)->tv_usec;  \
1100 		if ((tp1)->tv_usec < 0) {          \
1101 			(tp1)->tv_sec--;           \
1102 			(tp1)->tv_usec += 1000000; \
1103 		}                                  \
1104 	} while (0)
1105 #endif
1106 
1107 #if defined(__linux__)
1108 #if !defined(TAILQ_FOREACH_SAFE)
1109 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)             \
1110          for ((var) = ((head)->tqh_first);                     \
1111               (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
1112               (var) = (tvar))
1113 #endif
1114 #if !defined(LIST_FOREACH_SAFE)
1115 #define LIST_FOREACH_SAFE(var, head, field, tvar)              \
1116          for ((var) = ((head)->lh_first);                      \
1117               (var) && ((tvar) = LIST_NEXT((var), field), 1);  \
1118               (var) = (tvar))
1119 #endif
1120 #endif
1121 #if defined(__DragonFly__)
1122 #define TAILQ_FOREACH_SAFE TAILQ_FOREACH_MUTABLE
1123 #define LIST_FOREACH_SAFE LIST_FOREACH_MUTABLE
1124 #endif
1125 
1126 #if defined(__native_client__)
1127 #define	timercmp(tvp, uvp, cmp)						\
1128 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
1129 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
1130 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
1131 #endif
1132 
1133 #define SCTP_IS_LISTENING(inp) ((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) != 0)
1134 
1135 #if defined(__APPLE__) || defined(__DragonFly__) || defined(__linux__) || defined(__native_client__) || defined(__NetBSD__) || defined(_WIN32) || defined(__Fuchsia__)
1136 int
1137 timingsafe_bcmp(const void *, const void *, size_t);
1138 #endif
1139 
1140 #endif
1141