sctp_os_userspace.h 33 KB

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