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