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