Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp) |
| 3 | * |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 4 | * Copyright 2000-2013 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 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 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <time.h> |
| 20 | |
| 21 | #include <sys/param.h> |
| 22 | #include <sys/socket.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 23 | #include <sys/types.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 24 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 25 | #include <netinet/tcp.h> |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 26 | #include <netinet/in.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 27 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 29 | #include <haproxy/arg.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 30 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 31 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 32 | #include <haproxy/fd.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 33 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 34 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 35 | #include <haproxy/listener.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 36 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/namespace.h> |
| 38 | #include <haproxy/port_range.h> |
| 39 | #include <haproxy/proto_tcp.h> |
| 40 | #include <haproxy/protocol.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 41 | #include <haproxy/proxy-t.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 42 | #include <haproxy/sock.h> |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 43 | #include <haproxy/sock_inet.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 44 | #include <haproxy/tools.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 45 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 46 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 47 | static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 48 | static void tcpv4_add_listener(struct listener *listener, int port); |
| 49 | static void tcpv6_add_listener(struct listener *listener, int port); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 50 | |
| 51 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 52 | static struct protocol proto_tcpv4 = { |
| 53 | .name = "tcpv4", |
| 54 | .sock_domain = AF_INET, |
| 55 | .sock_type = SOCK_STREAM, |
| 56 | .sock_prot = IPPROTO_TCP, |
| 57 | .sock_family = AF_INET, |
| 58 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 59 | .l3_addrlen = 32/8, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 60 | .accept = &listener_accept, |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 61 | .connect = tcp_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 62 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 63 | .enable_all = enable_all_listeners, |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 64 | .get_src = sock_get_src, |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 65 | .get_dst = sock_inet_get_dst, |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 66 | .pause = tcp_pause_listener, |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 67 | .add = tcpv4_add_listener, |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 68 | .addrcmp = sock_inet4_addrcmp, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 69 | .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners), |
| 70 | .nb_listeners = 0, |
| 71 | }; |
| 72 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 73 | INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4); |
| 74 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 75 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 76 | static struct protocol proto_tcpv6 = { |
| 77 | .name = "tcpv6", |
| 78 | .sock_domain = AF_INET6, |
| 79 | .sock_type = SOCK_STREAM, |
| 80 | .sock_prot = IPPROTO_TCP, |
| 81 | .sock_family = AF_INET6, |
| 82 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 83 | .l3_addrlen = 128/8, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 84 | .accept = &listener_accept, |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 85 | .connect = tcp_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 86 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 87 | .enable_all = enable_all_listeners, |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 88 | .get_src = sock_get_src, |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 89 | .get_dst = sock_get_dst, |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 90 | .pause = tcp_pause_listener, |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 91 | .add = tcpv6_add_listener, |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 92 | .addrcmp = sock_inet6_addrcmp, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 93 | .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners), |
| 94 | .nb_listeners = 0, |
| 95 | }; |
| 96 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 97 | INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6); |
| 98 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 99 | /* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 100 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 101 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 102 | * - 1 : use provided address |
| 103 | * - 2 : use provided port |
| 104 | * - 3 : use both |
| 105 | * |
| 106 | * The function supports multiple foreign binding methods : |
| 107 | * - linux_tproxy: we directly bind to the foreign address |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 108 | * The second one can be used as a fallback for the first one. |
| 109 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 110 | * local address, 2 if it could not bind to the foreign address. |
| 111 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 112 | int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote) |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 113 | { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 114 | struct sockaddr_storage bind_addr; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 115 | int foreign_ok = 0; |
| 116 | int ret; |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 117 | static THREAD_LOCAL int ip_transp_working = 1; |
| 118 | static THREAD_LOCAL int ip6_transp_working = 1; |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 119 | |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 120 | switch (local->ss_family) { |
| 121 | case AF_INET: |
| 122 | if (flags && ip_transp_working) { |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 123 | /* This deserves some explanation. Some platforms will support |
| 124 | * multiple combinations of certain methods, so we try the |
| 125 | * supported ones until one succeeds. |
| 126 | */ |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 127 | if (sock_inet4_make_foreign(fd)) |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 128 | foreign_ok = 1; |
| 129 | else |
| 130 | ip_transp_working = 0; |
| 131 | } |
| 132 | break; |
| 133 | case AF_INET6: |
| 134 | if (flags && ip6_transp_working) { |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 135 | if (sock_inet6_make_foreign(fd)) |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 136 | foreign_ok = 1; |
| 137 | else |
| 138 | ip6_transp_working = 0; |
| 139 | } |
| 140 | break; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 141 | } |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 142 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 143 | if (flags) { |
| 144 | memset(&bind_addr, 0, sizeof(bind_addr)); |
Willy Tarreau | 96dd079 | 2011-04-19 07:20:57 +0200 | [diff] [blame] | 145 | bind_addr.ss_family = remote->ss_family; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 146 | switch (remote->ss_family) { |
| 147 | case AF_INET: |
| 148 | if (flags & 1) |
| 149 | ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr; |
| 150 | if (flags & 2) |
| 151 | ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port; |
| 152 | break; |
| 153 | case AF_INET6: |
| 154 | if (flags & 1) |
| 155 | ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr; |
| 156 | if (flags & 2) |
| 157 | ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port; |
| 158 | break; |
Willy Tarreau | 5dc1e98 | 2011-12-16 21:25:11 +0100 | [diff] [blame] | 159 | default: |
| 160 | /* we don't want to try to bind to an unknown address family */ |
| 161 | foreign_ok = 0; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 162 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 165 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 166 | if (foreign_ok) { |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 167 | if (is_inet_addr(&bind_addr)) { |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 168 | ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr)); |
| 169 | if (ret < 0) |
| 170 | return 2; |
| 171 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 172 | } |
| 173 | else { |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 174 | if (is_inet_addr(local)) { |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 175 | ret = bind(fd, (struct sockaddr *)local, get_addr_len(local)); |
| 176 | if (ret < 0) |
| 177 | return 1; |
| 178 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (!flags) |
| 182 | return 0; |
| 183 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 184 | if (!foreign_ok) |
| 185 | /* we could not bind to a foreign address */ |
| 186 | return 2; |
| 187 | |
| 188 | return 0; |
| 189 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 190 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 191 | /* |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 192 | * This function initiates a TCP connection establishment to the target assigned |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 193 | * to connection <conn> using (si->{target,dst}). A source address may be |
| 194 | * pointed to by conn->src in case of transparent proxying. Normal source |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 195 | * bind addresses are still determined locally (due to the possible need of a |
| 196 | * source port). conn->target may point either to a valid server or to a backend, |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 197 | * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 198 | * supported. The <data> parameter is a boolean indicating whether there are data |
| 199 | * waiting for being sent or not, in order to adjust data write polling and on |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 200 | * some platforms, the ability to avoid an empty initial ACK. The <flags> argument |
| 201 | * allows the caller to force using a delayed ACK when establishing the connection |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 202 | * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 203 | * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data |
| 204 | * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 205 | * |
Willy Tarreau | 1ec74bf | 2013-10-24 21:45:00 +0200 | [diff] [blame] | 206 | * Note that a pending send_proxy message accounts for data. |
| 207 | * |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 208 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 209 | * - SF_ERR_NONE if everything's OK |
| 210 | * - SF_ERR_SRVTO if there are no more servers |
| 211 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 212 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 213 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 214 | * - SF_ERR_INTERNAL for any other purely internal errors |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 215 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 216 | * |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 217 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 218 | * it's invalid and the caller has nothing to do. |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 219 | */ |
Willy Tarreau | f153686 | 2011-03-03 18:27:32 +0100 | [diff] [blame] | 220 | |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 221 | int tcp_connect_server(struct connection *conn, int flags) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 222 | { |
| 223 | int fd; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 224 | struct server *srv; |
| 225 | struct proxy *be; |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 226 | struct conn_src *src; |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 227 | int use_fastopen = 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 228 | struct sockaddr_storage *addr; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 229 | |
Olivier Houchard | 637b695 | 2018-11-23 14:23:07 +0100 | [diff] [blame] | 230 | conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */ |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 231 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 232 | switch (obj_type(conn->target)) { |
| 233 | case OBJ_TYPE_PROXY: |
| 234 | be = objt_proxy(conn->target); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 235 | srv = NULL; |
| 236 | break; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 237 | case OBJ_TYPE_SERVER: |
| 238 | srv = objt_server(conn->target); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 239 | be = srv->proxy; |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 240 | /* Make sure we check that we have data before activating |
| 241 | * TFO, or we could trigger a kernel issue whereby after |
| 242 | * a successful connect() == 0, any subsequent connect() |
| 243 | * will return EINPROGRESS instead of EISCONN. |
| 244 | */ |
| 245 | use_fastopen = (srv->flags & SRV_F_FASTOPEN) && |
| 246 | ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) == |
| 247 | (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 248 | break; |
| 249 | default: |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 250 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 251 | return SF_ERR_INTERNAL; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 252 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 253 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 254 | if (!conn->dst) { |
| 255 | conn->flags |= CO_FL_ERROR; |
| 256 | return SF_ERR_INTERNAL; |
| 257 | } |
| 258 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 259 | fd = conn->handle.fd = sock_create_server_socket(conn); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 260 | |
| 261 | if (fd == -1) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 262 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 263 | |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 264 | if (errno == ENFILE) { |
| 265 | conn->err_code = CO_ER_SYS_FDLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 266 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 267 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 268 | be->id, global.maxsock); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 269 | } |
| 270 | else if (errno == EMFILE) { |
| 271 | conn->err_code = CO_ER_PROC_FDLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 272 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 273 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 274 | be->id, global.maxsock); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 275 | } |
| 276 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 277 | conn->err_code = CO_ER_SYS_MEMLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 278 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 279 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 280 | be->id, global.maxsock); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 281 | } |
| 282 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 283 | conn->err_code = CO_ER_NOPROTO; |
| 284 | } |
| 285 | else |
| 286 | conn->err_code = CO_ER_SOCK_ERR; |
| 287 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 288 | /* this is a resource error */ |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 289 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 290 | return SF_ERR_RESOURCE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (fd >= global.maxsock) { |
| 294 | /* do not log anything there, it's a normal condition when this option |
| 295 | * is used to serialize connections to a server ! |
| 296 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 297 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 298 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 299 | conn->err_code = CO_ER_CONF_FDLIM; |
| 300 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 301 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 305 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 306 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 307 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 308 | conn->err_code = CO_ER_SOCK_ERR; |
| 309 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 310 | return SF_ERR_INTERNAL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 311 | } |
| 312 | |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 313 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 314 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 315 | close(fd); |
| 316 | conn->err_code = CO_ER_SOCK_ERR; |
| 317 | conn->flags |= CO_FL_ERROR; |
| 318 | return SF_ERR_INTERNAL; |
| 319 | } |
| 320 | |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 321 | if (be->options & PR_O_TCP_SRV_KA) { |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 322 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 323 | |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 324 | #ifdef TCP_KEEPCNT |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 325 | if (be->srvtcpka_cnt) |
| 326 | setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt)); |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 327 | #endif |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 328 | |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 329 | #ifdef TCP_KEEPIDLE |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 330 | if (be->srvtcpka_idle) |
| 331 | setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle)); |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 332 | #endif |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 333 | |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 334 | #ifdef TCP_KEEPINTVL |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 335 | if (be->srvtcpka_intvl) |
| 336 | setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl)); |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 337 | #endif |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 338 | } |
| 339 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 340 | /* allow specific binding : |
| 341 | * - server-specific at first |
| 342 | * - proxy-specific next |
| 343 | */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 344 | if (srv && srv->conn_src.opts & CO_SRC_BIND) |
| 345 | src = &srv->conn_src; |
| 346 | else if (be->conn_src.opts & CO_SRC_BIND) |
| 347 | src = &be->conn_src; |
| 348 | else |
| 349 | src = NULL; |
| 350 | |
| 351 | if (src) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 352 | int ret, flags = 0; |
| 353 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 354 | if (conn->src && is_inet_addr(conn->src)) { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 355 | switch (src->opts & CO_SRC_TPROXY_MASK) { |
Willy Tarreau | ef9a360 | 2012-12-08 22:29:20 +0100 | [diff] [blame] | 356 | case CO_SRC_TPROXY_CLI: |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 357 | conn_set_private(conn); |
Willy Tarreau | 387ebf8 | 2015-08-04 19:24:13 +0200 | [diff] [blame] | 358 | /* fall through */ |
| 359 | case CO_SRC_TPROXY_ADDR: |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 360 | flags = 3; |
| 361 | break; |
Willy Tarreau | ef9a360 | 2012-12-08 22:29:20 +0100 | [diff] [blame] | 362 | case CO_SRC_TPROXY_CIP: |
| 363 | case CO_SRC_TPROXY_DYN: |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 364 | conn_set_private(conn); |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 365 | flags = 1; |
| 366 | break; |
| 367 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 368 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 369 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 370 | #ifdef SO_BINDTODEVICE |
| 371 | /* Note: this might fail if not CAP_NET_RAW */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 372 | if (src->iface_name) |
| 373 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 374 | #endif |
| 375 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 376 | if (src->sport_range) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 377 | int attempts = 10; /* should be more than enough to find a spare port */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 378 | struct sockaddr_storage sa; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 379 | |
| 380 | ret = 1; |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 381 | memcpy(&sa, &src->source_addr, sizeof(sa)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 382 | |
| 383 | do { |
| 384 | /* note: in case of retry, we may have to release a previously |
| 385 | * allocated port, hence this loop's construct. |
| 386 | */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 387 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 388 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 389 | |
| 390 | if (!attempts) |
| 391 | break; |
| 392 | attempts--; |
| 393 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 394 | fdinfo[fd].local_port = port_range_alloc_port(src->sport_range); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 395 | if (!fdinfo[fd].local_port) { |
| 396 | conn->err_code = CO_ER_PORT_RANGE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 397 | break; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 398 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 399 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 400 | fdinfo[fd].port_range = src->sport_range; |
| 401 | set_host_port(&sa, fdinfo[fd].local_port); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 402 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 403 | ret = tcp_bind_socket(fd, flags, &sa, conn->src); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 404 | if (ret != 0) |
| 405 | conn->err_code = CO_ER_CANT_BIND; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 406 | } while (ret != 0); /* binding NOK */ |
| 407 | } |
| 408 | else { |
Lukas Tribus | 7d56c6d | 2016-09-13 09:51:15 +0000 | [diff] [blame] | 409 | #ifdef IP_BIND_ADDRESS_NO_PORT |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 410 | static THREAD_LOCAL int bind_address_no_port = 1; |
Lukas Tribus | 7d56c6d | 2016-09-13 09:51:15 +0000 | [diff] [blame] | 411 | setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int)); |
| 412 | #endif |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 413 | ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 414 | if (ret != 0) |
| 415 | conn->err_code = CO_ER_CANT_BIND; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 418 | if (unlikely(ret != 0)) { |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 419 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 420 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 421 | close(fd); |
| 422 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 423 | if (ret == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 424 | ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n", |
| 425 | be->id); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 426 | send_log(be, LOG_EMERG, |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 427 | "Cannot bind to source address before connect() for backend %s.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 428 | be->id); |
| 429 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 430 | ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n", |
| 431 | be->id); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 432 | send_log(be, LOG_EMERG, |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 433 | "Cannot bind to tproxy source address before connect() for backend %s.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 434 | be->id); |
| 435 | } |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 436 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 437 | return SF_ERR_RESOURCE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 441 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 442 | /* disabling tcp quick ack now allows the first request to leave the |
| 443 | * machine with the first ACK. We only do this if there are pending |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 444 | * data in the buffer. |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 445 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 446 | if (flags & (CONNECT_DELACK_ALWAYS) || |
| 447 | ((flags & CONNECT_DELACK_SMART_CONNECT || |
| 448 | (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) && |
| 449 | (be->options2 & PR_O2_SMARTCON))) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 450 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 451 | #endif |
| 452 | |
Willy Tarreau | 163d462 | 2015-10-13 16:16:41 +0200 | [diff] [blame] | 453 | #ifdef TCP_USER_TIMEOUT |
| 454 | /* there is not much more we can do here when it fails, it's still minor */ |
| 455 | if (srv && srv->tcp_ut) |
| 456 | setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut)); |
| 457 | #endif |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 458 | |
| 459 | if (use_fastopen) { |
| 460 | #if defined(TCP_FASTOPEN_CONNECT) |
| 461 | setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one)); |
| 462 | #endif |
| 463 | } |
Willy Tarreau | e803de2 | 2010-01-21 17:43:04 +0100 | [diff] [blame] | 464 | if (global.tune.server_sndbuf) |
| 465 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 466 | |
| 467 | if (global.tune.server_rcvbuf) |
| 468 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 469 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 470 | addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 471 | if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) { |
Willy Tarreau | 819efbf | 2017-01-25 14:12:22 +0100 | [diff] [blame] | 472 | if (errno == EINPROGRESS || errno == EALREADY) { |
| 473 | /* common case, let's wait for connect status */ |
| 474 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 475 | } |
| 476 | else if (errno == EISCONN) { |
| 477 | /* should normally not happen but if so, indicates that it's OK */ |
| 478 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 479 | } |
| 480 | else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 481 | char *msg; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 482 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 483 | msg = "no free ports"; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 484 | conn->err_code = CO_ER_FREE_PORTS; |
| 485 | } |
| 486 | else { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 487 | msg = "local address already in use"; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 488 | conn->err_code = CO_ER_ADDR_INUSE; |
| 489 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 490 | |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 491 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 492 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 493 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 494 | close(fd); |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 495 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 496 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 497 | return SF_ERR_RESOURCE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 498 | } else if (errno == ETIMEDOUT) { |
| 499 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 500 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 501 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 502 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 503 | conn->err_code = CO_ER_SOCK_ERR; |
| 504 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 505 | return SF_ERR_SRVTO; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 506 | } else { |
| 507 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 508 | //qfprintf(stderr,"Connect(): %d", errno); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 509 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 510 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 511 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 512 | conn->err_code = CO_ER_SOCK_ERR; |
| 513 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 514 | return SF_ERR_SRVCL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 515 | } |
| 516 | } |
Willy Tarreau | 819efbf | 2017-01-25 14:12:22 +0100 | [diff] [blame] | 517 | else { |
| 518 | /* connect() == 0, this is great! */ |
| 519 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 520 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 521 | |
Willy Tarreau | fc8f1f0 | 2012-12-08 18:53:44 +0100 | [diff] [blame] | 522 | conn->flags |= CO_FL_ADDR_TO_SET; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 523 | |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 524 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 525 | fdtab[fd].linger_risk = 1; /* close hard if needed */ |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 526 | |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 527 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 528 | fd_want_send(fd); |
| 529 | fd_cant_send(fd); |
Willy Tarreau | 8dbd1a2 | 2020-07-31 08:59:09 +0200 | [diff] [blame] | 530 | fd_cant_recv(fd); |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 531 | } |
Willy Tarreau | ccf3f6d | 2019-09-05 17:05:05 +0200 | [diff] [blame] | 532 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 533 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | 3f2770b | 2017-10-05 18:01:29 +0200 | [diff] [blame] | 534 | conn_full_close(conn); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 535 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 536 | return SF_ERR_RESOURCE; |
Willy Tarreau | 184636e | 2012-09-06 14:04:41 +0200 | [diff] [blame] | 537 | } |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 538 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 539 | return SF_ERR_NONE; /* connection is OK */ |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 542 | /* This function tries to bind a TCPv4/v6 listener. It may return a warning or |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 543 | * an error message in <errmsg> if the message is at most <errlen> bytes long |
| 544 | * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero. |
| 545 | * The return value is composed from ERR_ABORT, ERR_WARN, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 546 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 547 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 548 | * error occurred but that it may vanish after a retry (eg: port in use), and |
Aman Gupta | d94991d | 2012-04-06 17:39:26 -0700 | [diff] [blame] | 549 | * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 550 | * the meaning of the error, but just indicate that a message is present which |
| 551 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 552 | * that it's pointless to try to start other listeners. No error message is |
| 553 | * returned if errlen is NULL. |
| 554 | */ |
| 555 | int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 556 | { |
| 557 | __label__ tcp_return, tcp_close_return; |
| 558 | int fd, err; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 559 | int ext, ready; |
| 560 | socklen_t ready_len; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 561 | const char *msg = NULL; |
| 562 | |
| 563 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 564 | if (errlen) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 565 | *errmsg = 0; |
| 566 | |
| 567 | if (listener->state != LI_ASSIGNED) |
| 568 | return ERR_NONE; /* already bound */ |
| 569 | |
| 570 | err = ERR_NONE; |
| 571 | |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame^] | 572 | if (listener->rx.fd == -1) |
| 573 | listener->rx.fd = sock_find_compatible_fd(listener); |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 574 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 575 | /* if the listener already has an fd assigned, then we were offered the |
| 576 | * fd by an external process (most likely the parent), and we don't want |
| 577 | * to create a new socket. However we still want to set a few flags on |
| 578 | * the socket. |
| 579 | */ |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame^] | 580 | fd = listener->rx.fd; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 581 | ext = (fd >= 0); |
| 582 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 583 | if (!ext) { |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 584 | fd = my_socketat(listener->bind_conf->settings.netns, listener->rx.addr.ss_family, SOCK_STREAM, IPPROTO_TCP); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 585 | |
| 586 | if (fd == -1) { |
| 587 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 588 | msg = "cannot create listening socket"; |
| 589 | goto tcp_return; |
| 590 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 591 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 592 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 593 | if (fd >= global.maxsock) { |
| 594 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 595 | msg = "not enough free sockets (raise '-n' parameter)"; |
| 596 | goto tcp_close_return; |
| 597 | } |
| 598 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 599 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 600 | err |= ERR_FATAL | ERR_ALERT; |
| 601 | msg = "cannot make socket non-blocking"; |
| 602 | goto tcp_close_return; |
| 603 | } |
| 604 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 605 | if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 606 | /* not fatal but should be reported */ |
| 607 | msg = "cannot do so_reuseaddr"; |
| 608 | err |= ERR_ALERT; |
| 609 | } |
| 610 | |
| 611 | if (listener->options & LI_O_NOLINGER) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 612 | setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger)); |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 613 | else { |
| 614 | struct linger tmplinger; |
| 615 | socklen_t len = sizeof(tmplinger); |
| 616 | if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 && |
| 617 | (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) { |
| 618 | tmplinger.l_onoff = 0; |
| 619 | tmplinger.l_linger = 0; |
| 620 | setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, |
| 621 | sizeof(tmplinger)); |
| 622 | } |
| 623 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 624 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 625 | #ifdef SO_REUSEPORT |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 626 | /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of |
| 627 | * Linux, it might return an error that we will silently ignore. |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 628 | */ |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 629 | if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT)) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 630 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 631 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 632 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 633 | if (!ext && (listener->options & LI_O_FOREIGN)) { |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 634 | switch (listener->rx.addr.ss_family) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 635 | case AF_INET: |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 636 | if (!sock_inet4_make_foreign(fd)) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 637 | msg = "cannot make listening socket transparent"; |
| 638 | err |= ERR_ALERT; |
| 639 | } |
| 640 | break; |
| 641 | case AF_INET6: |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 642 | if (!sock_inet6_make_foreign(fd)) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 643 | msg = "cannot make listening socket transparent"; |
| 644 | err |= ERR_ALERT; |
| 645 | } |
| 646 | break; |
| 647 | } |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 648 | } |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 649 | |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 650 | #ifdef SO_BINDTODEVICE |
| 651 | /* Note: this might fail if not CAP_NET_RAW */ |
Willy Tarreau | 7e30721 | 2020-09-03 07:23:34 +0200 | [diff] [blame] | 652 | if (!ext && listener->bind_conf->settings.interface) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 653 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 7e30721 | 2020-09-03 07:23:34 +0200 | [diff] [blame] | 654 | listener->bind_conf->settings.interface, |
| 655 | strlen(listener->bind_conf->settings.interface) + 1) == -1) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 656 | msg = "cannot bind listener to device"; |
| 657 | err |= ERR_WARN; |
| 658 | } |
| 659 | } |
| 660 | #endif |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 661 | #if defined(TCP_MAXSEG) |
Willy Tarreau | 48a7e72 | 2010-12-24 15:26:39 +0100 | [diff] [blame] | 662 | if (listener->maxseg > 0) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 663 | if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 664 | &listener->maxseg, sizeof(listener->maxseg)) == -1) { |
| 665 | msg = "cannot set MSS"; |
| 666 | err |= ERR_WARN; |
| 667 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 668 | } else if (ext) { |
| 669 | int tmpmaxseg = -1; |
| 670 | int defaultmss; |
| 671 | socklen_t len = sizeof(tmpmaxseg); |
| 672 | |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 673 | if (listener->rx.addr.ss_family == AF_INET) |
Willy Tarreau | e5bdc51 | 2020-08-28 18:03:10 +0200 | [diff] [blame] | 674 | defaultmss = sock_inet_tcp_maxseg_default; |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 675 | else |
Willy Tarreau | e5bdc51 | 2020-08-28 18:03:10 +0200 | [diff] [blame] | 676 | defaultmss = sock_inet6_tcp_maxseg_default; |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 677 | |
| 678 | getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len); |
William Dauchy | 97a7bda | 2020-02-12 15:53:04 +0100 | [diff] [blame] | 679 | if (defaultmss > 0 && |
| 680 | tmpmaxseg != defaultmss && |
| 681 | setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) { |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 682 | msg = "cannot set MSS"; |
| 683 | err |= ERR_WARN; |
| 684 | } |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 685 | } |
| 686 | #endif |
Willy Tarreau | 2af207a | 2015-02-04 00:45:58 +0100 | [diff] [blame] | 687 | #if defined(TCP_USER_TIMEOUT) |
| 688 | if (listener->tcp_ut) { |
| 689 | if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, |
| 690 | &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) { |
| 691 | msg = "cannot set TCP User Timeout"; |
| 692 | err |= ERR_WARN; |
| 693 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 694 | } else |
| 695 | setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero, |
| 696 | sizeof(zero)); |
Willy Tarreau | 2af207a | 2015-02-04 00:45:58 +0100 | [diff] [blame] | 697 | #endif |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 698 | #if defined(TCP_DEFER_ACCEPT) |
| 699 | if (listener->options & LI_O_DEF_ACCEPT) { |
| 700 | /* defer accept by up to one second */ |
| 701 | int accept_delay = 1; |
| 702 | if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) { |
| 703 | msg = "cannot enable DEFER_ACCEPT"; |
| 704 | err |= ERR_WARN; |
| 705 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 706 | } else |
| 707 | setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero, |
| 708 | sizeof(zero)); |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 709 | #endif |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 710 | #if defined(TCP_FASTOPEN) |
| 711 | if (listener->options & LI_O_TCP_FO) { |
| 712 | /* TFO needs a queue length, let's use the configured backlog */ |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 713 | int qlen = listener_backlog(listener); |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 714 | if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) { |
| 715 | msg = "cannot enable TCP_FASTOPEN"; |
| 716 | err |= ERR_WARN; |
| 717 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 718 | } else { |
| 719 | socklen_t len; |
| 720 | int qlen; |
| 721 | len = sizeof(qlen); |
| 722 | /* Only disable fast open if it was enabled, we don't want |
| 723 | * the kernel to create a fast open queue if there's none. |
| 724 | */ |
| 725 | if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 && |
| 726 | qlen != 0) { |
| 727 | if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero, |
| 728 | sizeof(zero)) == -1) { |
| 729 | msg = "cannot disable TCP_FASTOPEN"; |
| 730 | err |= ERR_WARN; |
| 731 | } |
| 732 | } |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 733 | } |
| 734 | #endif |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 735 | #if defined(IPV6_V6ONLY) |
Willy Tarreau | bbb284d | 2020-08-26 10:21:06 +0200 | [diff] [blame] | 736 | if (!ext && listener->options & LI_O_V6ONLY) |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 737 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); |
Willy Tarreau | bbb284d | 2020-08-26 10:21:06 +0200 | [diff] [blame] | 738 | else if (!ext && listener->options & LI_O_V4V6) |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 739 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)); |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 740 | #endif |
| 741 | |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 742 | if (!ext && bind(fd, (struct sockaddr *)&listener->rx.addr, listener->proto->sock_addrlen) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 743 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 744 | msg = "cannot bind socket"; |
| 745 | goto tcp_close_return; |
| 746 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 747 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 748 | ready = 0; |
| 749 | ready_len = sizeof(ready); |
| 750 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1) |
| 751 | ready = 0; |
| 752 | |
| 753 | if (!(ext && ready) && /* only listen if not already done by external process */ |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 754 | listen(fd, listener_backlog(listener)) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 755 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 756 | msg = "cannot listen to socket"; |
| 757 | goto tcp_close_return; |
| 758 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 759 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 760 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 761 | if (listener->options & LI_O_NOQUICKACK) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 762 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 763 | else |
| 764 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 765 | #endif |
| 766 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 767 | /* the socket is ready */ |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame^] | 768 | listener->rx.fd = fd; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 769 | listener->state = LI_LISTEN; |
| 770 | |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 771 | fd_insert(fd, listener, listener->proto->accept, |
Willy Tarreau | e26993c | 2020-09-03 07:18:55 +0200 | [diff] [blame] | 772 | thread_mask(listener->bind_conf->settings.bind_thread) & all_threads_mask); |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 773 | |
Willy Tarreau | bb1caff | 2020-08-19 10:00:57 +0200 | [diff] [blame] | 774 | /* for now, all regularly bound TCP listeners are exportable */ |
| 775 | if (!(listener->options & LI_O_INHERITED)) |
| 776 | fdtab[fd].exported = 1; |
| 777 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 778 | tcp_return: |
Cyril Bonté | 43ba1b3 | 2010-11-01 19:26:01 +0100 | [diff] [blame] | 779 | if (msg && errlen) { |
| 780 | char pn[INET6_ADDRSTRLEN]; |
| 781 | |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 782 | addr_to_str(&listener->rx.addr, pn, sizeof(pn)); |
| 783 | snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr)); |
Cyril Bonté | 43ba1b3 | 2010-11-01 19:26:01 +0100 | [diff] [blame] | 784 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 785 | return err; |
| 786 | |
| 787 | tcp_close_return: |
| 788 | close(fd); |
| 789 | goto tcp_return; |
| 790 | } |
| 791 | |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 792 | /* Add <listener> to the list of tcpv4 listeners, on port <port>. The |
| 793 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 794 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 795 | * |
| 796 | * Must be called with proto_lock held. |
| 797 | * |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 798 | */ |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 799 | static void tcpv4_add_listener(struct listener *listener, int port) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 800 | { |
| 801 | if (listener->state != LI_INIT) |
| 802 | return; |
| 803 | listener->state = LI_ASSIGNED; |
| 804 | listener->proto = &proto_tcpv4; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 805 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 806 | LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list); |
| 807 | proto_tcpv4.nb_listeners++; |
| 808 | } |
| 809 | |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 810 | /* Add <listener> to the list of tcpv6 listeners, on port <port>. The |
| 811 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 812 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 813 | * |
| 814 | * Must be called with proto_lock held. |
| 815 | * |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 816 | */ |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 817 | static void tcpv6_add_listener(struct listener *listener, int port) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 818 | { |
| 819 | if (listener->state != LI_INIT) |
| 820 | return; |
| 821 | listener->state = LI_ASSIGNED; |
| 822 | listener->proto = &proto_tcpv6; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 823 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 824 | LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list); |
| 825 | proto_tcpv6.nb_listeners++; |
| 826 | } |
| 827 | |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 828 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 829 | * was totally stopped, or > 0 if correctly paused. |
| 830 | */ |
| 831 | int tcp_pause_listener(struct listener *l) |
| 832 | { |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame^] | 833 | if (shutdown(l->rx.fd, SHUT_WR) != 0) |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 834 | return -1; /* Solaris dies here */ |
| 835 | |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame^] | 836 | if (listen(l->rx.fd, listener_backlog(l)) != 0) |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 837 | return -1; /* OpenBSD dies here */ |
| 838 | |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame^] | 839 | if (shutdown(l->rx.fd, SHUT_RD) != 0) |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 840 | return -1; /* should always be OK */ |
| 841 | return 1; |
| 842 | } |
| 843 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 844 | |
| 845 | /* |
| 846 | * Local variables: |
| 847 | * c-indent-level: 8 |
| 848 | * c-basic-offset: 8 |
| 849 | * End: |
| 850 | */ |