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