Willy Tarreau | 0d06df6 | 2020-08-28 15:10:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * AF_INET/AF_INET6 socket management |
| 3 | * |
| 4 | * Copyright 2000-2020 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | f2cda10 | 2020-09-17 14:02:01 +0200 | [diff] [blame] | 13 | #include <errno.h> |
Willy Tarreau | 0d06df6 | 2020-08-28 15:10:11 +0200 | [diff] [blame] | 14 | #include <string.h> |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 15 | #include <unistd.h> |
Willy Tarreau | 0d06df6 | 2020-08-28 15:10:11 +0200 | [diff] [blame] | 16 | |
| 17 | #include <sys/param.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <netinet/tcp.h> |
| 22 | #include <netinet/in.h> |
| 23 | |
| 24 | #include <haproxy/api.h> |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 25 | #include <haproxy/errors.h> |
| 26 | #include <haproxy/fd.h> |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 27 | #include <haproxy/global.h> |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 28 | #include <haproxy/namespace.h> |
| 29 | #include <haproxy/receiver-t.h> |
| 30 | #include <haproxy/sock.h> |
Willy Tarreau | 0d06df6 | 2020-08-28 15:10:11 +0200 | [diff] [blame] | 31 | #include <haproxy/sock_inet.h> |
| 32 | #include <haproxy/tools.h> |
| 33 | |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 34 | struct proto_fam proto_fam_inet4 = { |
| 35 | .name = "inet4", |
| 36 | .sock_domain = PF_INET, |
| 37 | .sock_family = AF_INET, |
| 38 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 39 | .l3_addrlen = 32/8, |
| 40 | .addrcmp = sock_inet4_addrcmp, |
| 41 | .bind = sock_inet_bind_receiver, |
| 42 | .get_src = sock_get_src, |
| 43 | .get_dst = sock_inet_get_dst, |
Willy Tarreau | 73bed9f | 2020-12-04 14:43:36 +0100 | [diff] [blame] | 44 | .set_port = sock_inet_set_port, |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct proto_fam proto_fam_inet6 = { |
| 48 | .name = "inet6", |
| 49 | .sock_domain = PF_INET6, |
| 50 | .sock_family = AF_INET6, |
| 51 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 52 | .l3_addrlen = 128/8, |
| 53 | .addrcmp = sock_inet6_addrcmp, |
| 54 | .bind = sock_inet_bind_receiver, |
| 55 | .get_src = sock_get_src, |
| 56 | .get_dst = sock_get_dst, |
Willy Tarreau | 73bed9f | 2020-12-04 14:43:36 +0100 | [diff] [blame] | 57 | .set_port = sock_inet_set_port, |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 58 | }; |
Willy Tarreau | 0d06df6 | 2020-08-28 15:10:11 +0200 | [diff] [blame] | 59 | |
| 60 | /* PLEASE NOTE for function below: |
| 61 | * - sock_inet4_* is solely for AF_INET (IPv4) |
| 62 | * - sock_inet6_* is solely for AF_INET6 (IPv6) |
| 63 | * - sock_inet_* is for either |
| 64 | * |
| 65 | * The address family SHOULD always be checked. In some cases a function will |
| 66 | * be used in a situation where the address family is guaranteed (e.g. protocol |
| 67 | * definitions), so the test may be avoided. This special case must then be |
| 68 | * mentioned in the comment before the function definition. |
| 69 | */ |
| 70 | |
Willy Tarreau | d88e8c0 | 2020-08-28 16:06:01 +0200 | [diff] [blame] | 71 | /* determine if the operating system uses IPV6_V6ONLY by default. 0=no, 1=yes. |
| 72 | * It also remains if IPv6 is not enabled/configured. |
| 73 | */ |
| 74 | int sock_inet6_v6only_default = 0; |
Willy Tarreau | 0d06df6 | 2020-08-28 15:10:11 +0200 | [diff] [blame] | 75 | |
Willy Tarreau | e5bdc51 | 2020-08-28 18:03:10 +0200 | [diff] [blame] | 76 | /* Default TCPv4/TCPv6 MSS settings. -1=unknown. */ |
| 77 | int sock_inet_tcp_maxseg_default = -1; |
| 78 | int sock_inet6_tcp_maxseg_default = -1; |
| 79 | |
Willy Tarreau | 0d06df6 | 2020-08-28 15:10:11 +0200 | [diff] [blame] | 80 | /* Compares two AF_INET sockaddr addresses. Returns 0 if they match or non-zero |
| 81 | * if they do not match. |
| 82 | */ |
| 83 | int sock_inet4_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b) |
| 84 | { |
| 85 | const struct sockaddr_in *a4 = (const struct sockaddr_in *)a; |
| 86 | const struct sockaddr_in *b4 = (const struct sockaddr_in *)b; |
| 87 | |
| 88 | if (a->ss_family != b->ss_family) |
| 89 | return -1; |
| 90 | |
| 91 | if (a->ss_family != AF_INET) |
| 92 | return -1; |
| 93 | |
| 94 | if (a4->sin_port != b4->sin_port) |
| 95 | return -1; |
| 96 | |
| 97 | return memcmp(&a4->sin_addr, &b4->sin_addr, sizeof(a4->sin_addr)); |
| 98 | } |
| 99 | |
| 100 | /* Compares two AF_INET6 sockaddr addresses. Returns 0 if they match or |
| 101 | * non-zero if they do not match. |
| 102 | */ |
| 103 | int sock_inet6_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b) |
| 104 | { |
| 105 | const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)a; |
| 106 | const struct sockaddr_in6 *b6 = (const struct sockaddr_in6 *)b; |
| 107 | |
| 108 | if (a->ss_family != b->ss_family) |
| 109 | return -1; |
| 110 | |
| 111 | if (a->ss_family != AF_INET6) |
| 112 | return -1; |
| 113 | |
| 114 | if (a6->sin6_port != b6->sin6_port) |
| 115 | return -1; |
| 116 | |
| 117 | return memcmp(&a6->sin6_addr, &b6->sin6_addr, sizeof(a6->sin6_addr)); |
| 118 | } |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 119 | |
Willy Tarreau | 73bed9f | 2020-12-04 14:43:36 +0100 | [diff] [blame] | 120 | /* Sets the port <port> on IPv4 or IPv6 address <addr>. The address family is |
| 121 | * determined from the sockaddr_storage's address family. Nothing is done for |
| 122 | * other families. |
| 123 | */ |
| 124 | void sock_inet_set_port(struct sockaddr_storage *addr, int port) |
| 125 | { |
| 126 | if (addr->ss_family == AF_INET) |
| 127 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 128 | else if (addr->ss_family == AF_INET6) |
| 129 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
| 130 | } |
| 131 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 132 | /* |
| 133 | * Retrieves the original destination address for the socket <fd> which must be |
| 134 | * of family AF_INET (not AF_INET6), with <dir> indicating if we're a listener |
| 135 | * (=0) or an initiator (!=0). In the case of a listener, if the original |
| 136 | * destination address was translated, the original address is retrieved. It |
| 137 | * returns 0 in case of success, -1 in case of error. The socket's source |
| 138 | * address is stored in <sa> for <salen> bytes. |
| 139 | */ |
| 140 | int sock_inet_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 141 | { |
| 142 | if (dir) |
| 143 | return getpeername(fd, sa, &salen); |
| 144 | else { |
| 145 | int ret = getsockname(fd, sa, &salen); |
| 146 | |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | |
| 150 | #if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST) |
| 151 | /* For TPROXY and Netfilter's NAT, we can retrieve the original |
| 152 | * IPv4 address before DNAT/REDIRECT. We must not do that with |
| 153 | * other families because v6-mapped IPv4 addresses are still |
| 154 | * reported as v4. |
| 155 | */ |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 156 | if (getsockopt(fd, IPPROTO_IP, SO_ORIGINAL_DST, sa, &salen) == 0) |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 157 | return 0; |
| 158 | #endif |
| 159 | return ret; |
| 160 | } |
| 161 | } |
Willy Tarreau | 25140cc | 2020-08-28 15:40:33 +0200 | [diff] [blame] | 162 | |
Willy Tarreau | 3fd3bdc | 2020-09-01 15:12:08 +0200 | [diff] [blame] | 163 | /* Returns true if the passed FD corresponds to a socket bound with RX_O_FOREIGN |
Willy Tarreau | 25140cc | 2020-08-28 15:40:33 +0200 | [diff] [blame] | 164 | * according to the various supported socket options. The socket's address family |
| 165 | * must be passed in <family>. |
| 166 | */ |
| 167 | int sock_inet_is_foreign(int fd, sa_family_t family) |
| 168 | { |
| 169 | int val __maybe_unused; |
| 170 | socklen_t len __maybe_unused; |
| 171 | |
| 172 | switch (family) { |
| 173 | case AF_INET: |
| 174 | #if defined(IP_TRANSPARENT) |
| 175 | val = 0; len = sizeof(val); |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 176 | if (getsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &val, &len) == 0 && val) |
Willy Tarreau | 25140cc | 2020-08-28 15:40:33 +0200 | [diff] [blame] | 177 | return 1; |
| 178 | #endif |
| 179 | #if defined(IP_FREEBIND) |
| 180 | val = 0; len = sizeof(val); |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 181 | if (getsockopt(fd, IPPROTO_IP, IP_FREEBIND, &val, &len) == 0 && val) |
Willy Tarreau | 25140cc | 2020-08-28 15:40:33 +0200 | [diff] [blame] | 182 | return 1; |
| 183 | #endif |
| 184 | #if defined(IP_BINDANY) |
| 185 | val = 0; len = sizeof(val); |
| 186 | if (getsockopt(fd, IPPROTO_IP, IP_BINDANY, &val, &len) == 0 && val) |
| 187 | return 1; |
| 188 | #endif |
| 189 | #if defined(SO_BINDANY) |
| 190 | val = 0; len = sizeof(val); |
| 191 | if (getsockopt(fd, SOL_SOCKET, SO_BINDANY, &val, &len) == 0 && val) |
| 192 | return 1; |
| 193 | #endif |
| 194 | break; |
| 195 | |
| 196 | case AF_INET6: |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 197 | #if defined(IPV6_TRANSPARENT) |
Willy Tarreau | 25140cc | 2020-08-28 15:40:33 +0200 | [diff] [blame] | 198 | val = 0; len = sizeof(val); |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 199 | if (getsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &val, &len) == 0 && val) |
Willy Tarreau | 25140cc | 2020-08-28 15:40:33 +0200 | [diff] [blame] | 200 | return 1; |
| 201 | #endif |
| 202 | #if defined(IP_FREEBIND) |
| 203 | val = 0; len = sizeof(val); |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 204 | if (getsockopt(fd, IPPROTO_IP, IP_FREEBIND, &val, &len) == 0 && val) |
Willy Tarreau | 25140cc | 2020-08-28 15:40:33 +0200 | [diff] [blame] | 205 | return 1; |
| 206 | #endif |
| 207 | #if defined(IPV6_BINDANY) |
| 208 | val = 0; len = sizeof(val); |
| 209 | if (getsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &val, &len) == 0 && val) |
| 210 | return 1; |
| 211 | #endif |
| 212 | #if defined(SO_BINDANY) |
| 213 | val = 0; len = sizeof(val); |
| 214 | if (getsockopt(fd, SOL_SOCKET, SO_BINDANY, &val, &len) == 0 && val) |
| 215 | return 1; |
| 216 | #endif |
| 217 | break; |
| 218 | } |
| 219 | return 0; |
| 220 | } |
Willy Tarreau | d88e8c0 | 2020-08-28 16:06:01 +0200 | [diff] [blame] | 221 | |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 222 | /* Attempt all known socket options to prepare an AF_INET4 socket to be bound |
| 223 | * to a foreign address. The socket must already exist and must not be bound. |
| 224 | * 1 is returned on success, 0 on failure. The caller must check the address |
| 225 | * family before calling this function. |
| 226 | */ |
| 227 | int sock_inet4_make_foreign(int fd) |
| 228 | { |
| 229 | return |
| 230 | #if defined(IP_TRANSPARENT) |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 231 | setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0 || |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 232 | #endif |
| 233 | #if defined(IP_FREEBIND) |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 234 | setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) == 0 || |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 235 | #endif |
| 236 | #if defined(IP_BINDANY) |
| 237 | setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0 || |
| 238 | #endif |
| 239 | #if defined(SO_BINDANY) |
| 240 | setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0 || |
| 241 | #endif |
| 242 | 0; |
| 243 | } |
| 244 | |
| 245 | /* Attempt all known socket options to prepare an AF_INET6 socket to be bound |
| 246 | * to a foreign address. The socket must already exist and must not be bound. |
| 247 | * 1 is returned on success, 0 on failure. The caller must check the address |
| 248 | * family before calling this function. |
| 249 | */ |
| 250 | int sock_inet6_make_foreign(int fd) |
| 251 | { |
| 252 | return |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 253 | #if defined(IPV6_TRANSPARENT) |
| 254 | setsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0 || |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 255 | #endif |
| 256 | #if defined(IP_FREEBIND) |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 257 | setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) == 0 || |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 258 | #endif |
| 259 | #if defined(IPV6_BINDANY) |
| 260 | setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0 || |
| 261 | #endif |
| 262 | #if defined(SO_BINDANY) |
| 263 | setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0 || |
| 264 | #endif |
| 265 | 0; |
| 266 | } |
| 267 | |
Willy Tarreau | 233ad28 | 2020-10-15 21:45:15 +0200 | [diff] [blame] | 268 | /* Binds receiver <rx>, and assigns rx->iocb and rx->owner as the callback and |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 269 | * context, respectively. Returns and error code made of ERR_* bits on failure |
| 270 | * or ERR_NONE on success. On failure, an error message may be passed into |
| 271 | * <errmsg>. |
| 272 | */ |
Willy Tarreau | 233ad28 | 2020-10-15 21:45:15 +0200 | [diff] [blame] | 273 | int sock_inet_bind_receiver(struct receiver *rx, char **errmsg) |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 274 | { |
| 275 | int fd, err, ext; |
| 276 | /* copy listener addr because sometimes we need to switch family */ |
| 277 | struct sockaddr_storage addr_inet = rx->addr; |
| 278 | |
| 279 | /* force to classic sock family, not AF_CUST_* */ |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 280 | addr_inet.ss_family = rx->proto->fam->sock_family; |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 281 | |
| 282 | /* ensure we never return garbage */ |
| 283 | if (errmsg) |
| 284 | *errmsg = 0; |
| 285 | |
| 286 | err = ERR_NONE; |
| 287 | |
| 288 | if (rx->flags & RX_F_BOUND) |
| 289 | return ERR_NONE; |
| 290 | |
| 291 | /* if no FD was assigned yet, we'll have to either find a compatible |
| 292 | * one or create a new one. |
| 293 | */ |
| 294 | if (rx->fd == -1) |
| 295 | rx->fd = sock_find_compatible_fd(rx); |
| 296 | |
| 297 | /* if the receiver now has an fd assigned, then we were offered the fd |
| 298 | * by an external process (most likely the parent), and we don't want |
| 299 | * to create a new socket. However we still want to set a few flags on |
| 300 | * the socket. |
| 301 | */ |
| 302 | fd = rx->fd; |
| 303 | ext = (fd >= 0); |
| 304 | |
| 305 | if (!ext) { |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 306 | fd = my_socketat(rx->settings->netns, rx->proto->fam->sock_domain, |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 307 | rx->proto->sock_type, rx->proto->sock_prot); |
| 308 | if (fd == -1) { |
| 309 | err |= ERR_RETRYABLE | ERR_ALERT; |
Willy Tarreau | 36722d2 | 2020-09-17 08:32:17 +0200 | [diff] [blame] | 310 | memprintf(errmsg, "cannot create receiving socket (%s)", strerror(errno)); |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 311 | goto bind_return; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (fd >= global.maxsock) { |
| 316 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 317 | memprintf(errmsg, "not enough free sockets (raise '-n' parameter)"); |
| 318 | goto bind_close_return; |
| 319 | } |
| 320 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 321 | if (fd_set_nonblock(fd) == -1) { |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 322 | err |= ERR_FATAL | ERR_ALERT; |
| 323 | memprintf(errmsg, "cannot make socket non-blocking"); |
| 324 | goto bind_close_return; |
| 325 | } |
| 326 | |
| 327 | if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) { |
| 328 | /* not fatal but should be reported */ |
| 329 | memprintf(errmsg, "cannot do so_reuseaddr"); |
| 330 | err |= ERR_ALERT; |
| 331 | } |
| 332 | |
| 333 | #ifdef SO_REUSEPORT |
| 334 | /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of |
| 335 | * Linux, it might return an error that we will silently ignore. |
| 336 | */ |
| 337 | if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT)) |
| 338 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); |
| 339 | #endif |
| 340 | |
| 341 | if (!ext && (rx->settings->options & RX_O_FOREIGN)) { |
| 342 | switch (addr_inet.ss_family) { |
| 343 | case AF_INET: |
| 344 | if (!sock_inet4_make_foreign(fd)) { |
| 345 | memprintf(errmsg, "cannot make receiving socket transparent"); |
| 346 | err |= ERR_ALERT; |
| 347 | } |
| 348 | break; |
| 349 | case AF_INET6: |
| 350 | if (!sock_inet6_make_foreign(fd)) { |
| 351 | memprintf(errmsg, "cannot make receiving socket transparent"); |
| 352 | err |= ERR_ALERT; |
| 353 | } |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | #ifdef SO_BINDTODEVICE |
| 359 | /* Note: this might fail if not CAP_NET_RAW */ |
| 360 | if (!ext && rx->settings->interface) { |
| 361 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
| 362 | rx->settings->interface, |
| 363 | strlen(rx->settings->interface) + 1) == -1) { |
Willy Tarreau | f78b52e | 2021-10-14 11:41:19 +0200 | [diff] [blame] | 364 | memprintf(errmsg, "cannot bind receiver to device '%s' (%s)", rx->settings->interface, strerror(errno)); |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 365 | err |= ERR_WARN; |
| 366 | } |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | #if defined(IPV6_V6ONLY) |
| 371 | if (addr_inet.ss_family == AF_INET6 && !ext) { |
| 372 | /* Prepare to match the v6only option against what we really want. Note |
| 373 | * that sadly the two options are not exclusive to each other and that |
| 374 | * v6only is stronger than v4v6. |
| 375 | */ |
| 376 | if ((rx->settings->options & RX_O_V6ONLY) || |
| 377 | (sock_inet6_v6only_default && !(rx->settings->options & RX_O_V4V6))) |
| 378 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); |
| 379 | else |
| 380 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)); |
| 381 | } |
| 382 | #endif |
| 383 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 384 | if (!ext && bind(fd, (struct sockaddr *)&addr_inet, rx->proto->fam->sock_addrlen) == -1) { |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 385 | err |= ERR_RETRYABLE | ERR_ALERT; |
Willy Tarreau | 36722d2 | 2020-09-17 08:32:17 +0200 | [diff] [blame] | 386 | memprintf(errmsg, "cannot bind socket (%s)", strerror(errno)); |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 387 | goto bind_close_return; |
| 388 | } |
| 389 | |
| 390 | rx->fd = fd; |
| 391 | rx->flags |= RX_F_BOUND; |
| 392 | |
Willy Tarreau | 9464bb1 | 2022-07-05 05:16:13 +0200 | [diff] [blame] | 393 | fd_insert(fd, rx->owner, rx->iocb, rx->bind_tgroup, rx->bind_thread); |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 394 | |
| 395 | /* for now, all regularly bound TCP listeners are exportable */ |
| 396 | if (!(rx->flags & RX_F_INHERITED)) |
Willy Tarreau | 9063a66 | 2021-04-06 18:09:06 +0200 | [diff] [blame] | 397 | HA_ATOMIC_OR(&fdtab[fd].state, FD_EXPORTED); |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 398 | |
| 399 | bind_return: |
| 400 | if (errmsg && *errmsg) { |
| 401 | char pn[INET6_ADDRSTRLEN]; |
| 402 | |
| 403 | addr_to_str(&addr_inet, pn, sizeof(pn)); |
Willy Tarreau | 6823a3a | 2021-10-14 11:59:15 +0200 | [diff] [blame] | 404 | memprintf(errmsg, "%s for [%s:%d]", *errmsg, pn, get_host_port(&addr_inet)); |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 405 | } |
| 406 | return err; |
| 407 | |
| 408 | bind_close_return: |
| 409 | close(fd); |
| 410 | goto bind_return; |
| 411 | } |
| 412 | |
Willy Tarreau | d88e8c0 | 2020-08-28 16:06:01 +0200 | [diff] [blame] | 413 | static void sock_inet_prepare() |
| 414 | { |
| 415 | int fd, val; |
| 416 | socklen_t len; |
| 417 | |
Willy Tarreau | e5bdc51 | 2020-08-28 18:03:10 +0200 | [diff] [blame] | 418 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 419 | if (fd >= 0) { |
| 420 | #ifdef TCP_MAXSEG |
| 421 | /* retrieve the OS' default mss for TCPv4 */ |
| 422 | len = sizeof(val); |
| 423 | if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &val, &len) == 0) |
| 424 | sock_inet_tcp_maxseg_default = val; |
| 425 | #endif |
| 426 | close(fd); |
| 427 | } |
| 428 | |
Willy Tarreau | d88e8c0 | 2020-08-28 16:06:01 +0200 | [diff] [blame] | 429 | fd = socket(AF_INET6, SOCK_STREAM, 0); |
| 430 | if (fd >= 0) { |
| 431 | #if defined(IPV6_V6ONLY) |
| 432 | /* retrieve the OS' bindv6only value */ |
| 433 | len = sizeof(val); |
| 434 | if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) == 0 && val > 0) |
| 435 | sock_inet6_v6only_default = 1; |
| 436 | #endif |
Willy Tarreau | e5bdc51 | 2020-08-28 18:03:10 +0200 | [diff] [blame] | 437 | |
| 438 | #ifdef TCP_MAXSEG |
| 439 | /* retrieve the OS' default mss for TCPv6 */ |
| 440 | len = sizeof(val); |
| 441 | if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &val, &len) == 0) |
| 442 | sock_inet6_tcp_maxseg_default = val; |
| 443 | #endif |
Willy Tarreau | d88e8c0 | 2020-08-28 16:06:01 +0200 | [diff] [blame] | 444 | close(fd); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | INITCALL0(STG_PREPARE, sock_inet_prepare); |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 449 | |
| 450 | |
| 451 | REGISTER_BUILD_OPTS("Built with transparent proxy support using:" |
| 452 | #if defined(IP_TRANSPARENT) |
| 453 | " IP_TRANSPARENT" |
| 454 | #endif |
| 455 | #if defined(IPV6_TRANSPARENT) |
| 456 | " IPV6_TRANSPARENT" |
| 457 | #endif |
| 458 | #if defined(IP_FREEBIND) |
| 459 | " IP_FREEBIND" |
| 460 | #endif |
| 461 | #if defined(IP_BINDANY) |
| 462 | " IP_BINDANY" |
| 463 | #endif |
| 464 | #if defined(IPV6_BINDANY) |
| 465 | " IPV6_BINDANY" |
| 466 | #endif |
| 467 | #if defined(SO_BINDANY) |
| 468 | " SO_BINDANY" |
| 469 | #endif |
| 470 | ""); |