Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 17f449b | 2010-11-07 11:44:13 +0100 | [diff] [blame] | 2 | * include/common/compat.h |
| 3 | * Operating system compatibility interface. |
| 4 | * |
| 5 | * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_COMPAT_H |
| 23 | #define _COMMON_COMPAT_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Joris Dedieu | 9dd44ba | 2015-06-13 01:05:11 +0200 | [diff] [blame] | 25 | #include <limits.h> |
Willy Tarreau | f4c1e56 | 2019-05-23 08:40:50 +0200 | [diff] [blame] | 26 | #include <signal.h> |
Willy Tarreau | b81939c | 2019-05-20 20:18:26 +0200 | [diff] [blame] | 27 | #include <time.h> |
| 28 | #include <unistd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | /* This is needed on Linux for Netfilter includes */ |
Willy Tarreau | 17f449b | 2010-11-07 11:44:13 +0100 | [diff] [blame] | 30 | #include <sys/param.h> |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 31 | #include <sys/types.h> |
Willy Tarreau | 0e996c6 | 2010-02-26 22:00:19 +0100 | [diff] [blame] | 32 | #include <sys/socket.h> |
Willy Tarreau | 17f449b | 2010-11-07 11:44:13 +0100 | [diff] [blame] | 33 | #include <arpa/inet.h> |
Joris Dedieu | 9dd44ba | 2015-06-13 01:05:11 +0200 | [diff] [blame] | 34 | #include <netinet/in.h> |
Willy Tarreau | 9d87ca0 | 2016-08-10 21:09:24 +0200 | [diff] [blame] | 35 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 9a85a17 | 2019-05-21 19:48:44 +0200 | [diff] [blame] | 37 | /* set any optional field in a struct to this type to save ifdefs. Its address |
| 38 | * will still be valid but it will not reserve any room nor require any |
| 39 | * initialization. |
| 40 | */ |
| 41 | typedef struct { } empty_t; |
| 42 | |
Willy Tarreau | e3e2b72 | 2019-05-20 19:10:52 +0200 | [diff] [blame] | 43 | // Redefine some limits that are not present everywhere |
| 44 | #ifndef LLONG_MAX |
| 45 | # define LLONG_MAX 9223372036854775807LL |
| 46 | # define LLONG_MIN (-LLONG_MAX - 1LL) |
| 47 | #endif |
| 48 | |
| 49 | #ifndef ULLONG_MAX |
| 50 | # define ULLONG_MAX (LLONG_MAX * 2ULL + 1) |
| 51 | #endif |
| 52 | |
| 53 | #ifndef LONGBITS |
| 54 | #define LONGBITS ((unsigned int)sizeof(long) * 8) |
| 55 | #endif |
| 56 | |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 57 | #ifndef BITS_PER_INT |
| 58 | #define BITS_PER_INT (8*sizeof(int)) |
| 59 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | |
| 61 | /* this is for libc5 for example */ |
| 62 | #ifndef TCP_NODELAY |
| 63 | #define TCP_NODELAY 1 |
| 64 | #endif |
| 65 | |
| 66 | #ifndef SHUT_RD |
| 67 | #define SHUT_RD 0 |
| 68 | #endif |
| 69 | |
| 70 | #ifndef SHUT_WR |
| 71 | #define SHUT_WR 1 |
| 72 | #endif |
| 73 | |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 74 | /* only Linux defines it */ |
| 75 | #ifndef MSG_NOSIGNAL |
| 76 | #define MSG_NOSIGNAL 0 |
| 77 | #endif |
| 78 | |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 79 | /* AIX does not define MSG_DONTWAIT. We'll define it to zero, and test it |
| 80 | * wherever appropriate. |
| 81 | */ |
| 82 | #ifndef MSG_DONTWAIT |
| 83 | #define MSG_DONTWAIT 0 |
| 84 | #endif |
| 85 | |
Willy Tarreau | 6db06d3 | 2009-08-19 11:14:11 +0200 | [diff] [blame] | 86 | /* Only Linux defines MSG_MORE */ |
| 87 | #ifndef MSG_MORE |
| 88 | #define MSG_MORE 0 |
| 89 | #endif |
| 90 | |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 91 | /* On Linux 2.4 and above, MSG_TRUNC can be used on TCP sockets to drop any |
| 92 | * pending data. Let's rely on NETFILTER to detect if this is supported. |
| 93 | */ |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 94 | #ifdef USE_NETFILTER |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 95 | #define MSG_TRUNC_CLEARS_INPUT |
| 96 | #endif |
| 97 | |
Willy Tarreau | 17f449b | 2010-11-07 11:44:13 +0100 | [diff] [blame] | 98 | /* Maximum path length, OS-dependant */ |
| 99 | #ifndef MAXPATHLEN |
| 100 | #define MAXPATHLEN 128 |
| 101 | #endif |
| 102 | |
Willy Tarreau | bd9a0a7 | 2011-10-23 21:14:29 +0200 | [diff] [blame] | 103 | /* On Linux, allows pipes to be resized */ |
| 104 | #ifndef F_SETPIPE_SZ |
| 105 | #define F_SETPIPE_SZ (1024 + 7) |
| 106 | #endif |
| 107 | |
Willy Tarreau | f4c1e56 | 2019-05-23 08:40:50 +0200 | [diff] [blame] | 108 | /* On FreeBSD we don't have SI_TKILL but SI_LWP instead */ |
| 109 | #if !defined(SI_TKILL) && defined(SI_LWP) |
| 110 | #define SI_TKILL SI_LWP |
| 111 | #endif |
| 112 | |
Willy Tarreau | 430f590 | 2019-05-21 20:01:26 +0200 | [diff] [blame] | 113 | /* systems without such defines do not know clockid_t or timer_t */ |
Willy Tarreau | 823bda0 | 2019-05-22 19:12:54 +0200 | [diff] [blame] | 114 | #if !(_POSIX_TIMERS > 0) |
Willy Tarreau | 80daaa1 | 2019-05-21 19:58:16 +0200 | [diff] [blame] | 115 | #undef clockid_t |
| 116 | #define clockid_t empty_t |
Willy Tarreau | 430f590 | 2019-05-21 20:01:26 +0200 | [diff] [blame] | 117 | #undef timer_t |
| 118 | #define timer_t empty_t |
| 119 | #endif |
| 120 | |
| 121 | /* define a dummy value to designate "no timer". Use only 32 bits. */ |
| 122 | #ifndef TIMER_INVALID |
| 123 | #define TIMER_INVALID ((timer_t)(unsigned long)(0xfffffffful)) |
Willy Tarreau | b81939c | 2019-05-20 20:18:26 +0200 | [diff] [blame] | 124 | #endif |
| 125 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 126 | #if defined(USE_TPROXY) && defined(USE_NETFILTER) |
Jeremy Hinegardner | e7dd2f2 | 2008-04-21 07:34:31 +0200 | [diff] [blame] | 127 | #include <linux/types.h> |
| 128 | #include <linux/netfilter_ipv6.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 129 | #include <linux/netfilter_ipv4.h> |
| 130 | #endif |
| 131 | |
Willy Tarreau | 0a45989 | 2008-01-13 17:37:16 +0100 | [diff] [blame] | 132 | /* On Linux, IP_TRANSPARENT and/or IP_FREEBIND generally require a kernel patch */ |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 133 | #if defined(USE_LINUX_TPROXY) |
Willy Tarreau | 0a45989 | 2008-01-13 17:37:16 +0100 | [diff] [blame] | 134 | #if !defined(IP_FREEBIND) |
| 135 | #define IP_FREEBIND 15 |
| 136 | #endif /* !IP_FREEBIND */ |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 137 | #if !defined(IP_TRANSPARENT) |
| 138 | #define IP_TRANSPARENT 19 |
| 139 | #endif /* !IP_TRANSPARENT */ |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 140 | #if !defined(IPV6_TRANSPARENT) |
| 141 | #define IPV6_TRANSPARENT 75 |
| 142 | #endif /* !IPV6_TRANSPARENT */ |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 143 | #endif /* USE_LINUX_TPROXY */ |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 144 | |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 145 | #if defined(IP_FREEBIND) \ |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 146 | || defined(IP_BINDANY) \ |
| 147 | || defined(IPV6_BINDANY) \ |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 148 | || defined(SO_BINDANY) \ |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 149 | || defined(IP_TRANSPARENT) \ |
| 150 | || defined(IPV6_TRANSPARENT) |
| 151 | #define CONFIG_HAP_TRANSPARENT |
| 152 | #endif |
| 153 | |
Willy Tarreau | 58b2f83 | 2006-11-13 01:22:38 +0100 | [diff] [blame] | 154 | /* We'll try to enable SO_REUSEPORT on Linux 2.4 and 2.6 if not defined. |
| 155 | * There are two families of values depending on the architecture. Those |
| 156 | * are at least valid on Linux 2.4 and 2.6, reason why we'll rely on the |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 157 | * USE_NETFILTER define. |
Willy Tarreau | 58b2f83 | 2006-11-13 01:22:38 +0100 | [diff] [blame] | 158 | */ |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 159 | #if !defined(SO_REUSEPORT) && defined(USE_NETFILTER) |
Willy Tarreau | 58b2f83 | 2006-11-13 01:22:38 +0100 | [diff] [blame] | 160 | #if (SO_REUSEADDR == 2) |
| 161 | #define SO_REUSEPORT 15 |
| 162 | #elif (SO_REUSEADDR == 0x0004) |
| 163 | #define SO_REUSEPORT 0x0200 |
| 164 | #endif /* SO_REUSEADDR */ |
| 165 | #endif /* SO_REUSEPORT */ |
| 166 | |
Lukas Tribus | 0999f76 | 2013-04-02 16:43:24 +0200 | [diff] [blame] | 167 | /* only Linux defines TCP_FASTOPEN */ |
| 168 | #ifdef USE_TFO |
| 169 | #ifndef TCP_FASTOPEN |
| 170 | #define TCP_FASTOPEN 23 |
| 171 | #endif |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 172 | |
| 173 | #ifndef TCP_FASTOPEN_CONNECT |
| 174 | #define TCP_FASTOPEN_CONNECT 30 |
| 175 | #endif |
Lukas Tribus | 0999f76 | 2013-04-02 16:43:24 +0200 | [diff] [blame] | 176 | #endif |
| 177 | |
Willy Tarreau | ae459f3 | 2015-09-29 18:19:32 +0200 | [diff] [blame] | 178 | /* FreeBSD doesn't define SOL_IP and prefers IPPROTO_IP */ |
| 179 | #ifndef SOL_IP |
| 180 | #define SOL_IP IPPROTO_IP |
| 181 | #endif |
| 182 | |
Willy Tarreau | 9d87ca0 | 2016-08-10 21:09:24 +0200 | [diff] [blame] | 183 | /* same for SOL_TCP */ |
| 184 | #ifndef SOL_TCP |
| 185 | #define SOL_TCP IPPROTO_TCP |
| 186 | #endif |
| 187 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 188 | /* If IPv6 is supported, define IN6_IS_ADDR_V4MAPPED() if missing. */ |
| 189 | #if defined(IPV6_TCLASS) && !defined(IN6_IS_ADDR_V4MAPPED) |
| 190 | #define IN6_IS_ADDR_V4MAPPED(a) \ |
| 191 | ((((const uint32_t *) (a))[0] == 0) \ |
| 192 | && (((const uint32_t *) (a))[1] == 0) \ |
| 193 | && (((const uint32_t *) (a))[2] == htonl (0xffff))) |
| 194 | #endif |
| 195 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 196 | #if defined(__dietlibc__) |
| 197 | #include <strings.h> |
| 198 | #endif |
| 199 | |
Willy Tarreau | 4698adf | 2018-10-29 19:14:14 +0100 | [diff] [blame] | 200 | /* crypt_r() has been present in glibc since 2.2 and on FreeBSD since 12.0 |
| 201 | * (12000002). No other OS makes any mention of it for now. Feel free to add |
| 202 | * valid known combinations below if needed to relax the crypt() lock when |
| 203 | * using threads. |
| 204 | */ |
| 205 | #if (defined(__GNU_LIBRARY__) && (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) \ |
| 206 | || (defined(__FreeBSD__) && __FreeBSD_version >= 1200002) |
| 207 | #define HA_HAVE_CRYPT_R |
| 208 | #endif |
| 209 | |
Willy Tarreau | 13faf16 | 2020-03-04 07:44:06 +0100 | [diff] [blame] | 210 | /* some backtrace() implementations are broken or incomplete, in this case we |
| 211 | * can replace them. We must not do it all the time as some are more accurate |
| 212 | * than ours. |
| 213 | */ |
| 214 | #ifdef USE_BACKTRACE |
| 215 | #if defined(__aarch64__) |
| 216 | /* on aarch64 at least from gcc-4.7.4 to 7.4.1 we only get a single entry, which |
| 217 | * is pointless. Ours works though it misses the faulty function itself, |
| 218 | * probably due to an alternate stack for the signal handler which does not |
| 219 | * create a new frame hence doesn't store the caller's return address. |
| 220 | */ |
Willy Tarreau | 899e5f6 | 2020-03-04 11:54:16 +0100 | [diff] [blame] | 221 | #elif defined(__clang__) && defined(__x86_64__) |
| 222 | /* this is on FreeBSD, clang 4.0 to 8.0 produce don't go further than the |
| 223 | * sighandler. |
| 224 | */ |
Willy Tarreau | 13faf16 | 2020-03-04 07:44:06 +0100 | [diff] [blame] | 225 | #else |
| 226 | #define HA_HAVE_WORKING_BACKTRACE |
| 227 | #endif |
| 228 | #endif |
| 229 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 230 | #endif /* _COMMON_COMPAT_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * Local variables: |
| 234 | * c-indent-level: 8 |
| 235 | * c-basic-offset: 8 |
| 236 | * End: |
| 237 | */ |