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