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