Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AF_INET/AF_INET6 QUIC protocol layer. |
| 3 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame^] | 4 | * Copyright 2020 Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +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> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <netinet/udp.h> |
| 26 | #include <netinet/in.h> |
| 27 | |
| 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/arg.h> |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 30 | #include <haproxy/cbuf.h> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 31 | #include <haproxy/connection.h> |
| 32 | #include <haproxy/errors.h> |
| 33 | #include <haproxy/fd.h> |
| 34 | #include <haproxy/global.h> |
| 35 | #include <haproxy/list.h> |
| 36 | #include <haproxy/listener.h> |
| 37 | #include <haproxy/log.h> |
| 38 | #include <haproxy/namespace.h> |
| 39 | #include <haproxy/port_range.h> |
| 40 | #include <haproxy/protocol.h> |
| 41 | #include <haproxy/proto_quic.h> |
| 42 | #include <haproxy/proto_udp.h> |
| 43 | #include <haproxy/proxy-t.h> |
| 44 | #include <haproxy/sock.h> |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 45 | #include <haproxy/quic_sock.h> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 46 | #include <haproxy/sock_inet.h> |
| 47 | #include <haproxy/tools.h> |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 48 | #include <haproxy/xprt_quic.h> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 49 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 50 | /* per-thread quic datagram handlers */ |
| 51 | struct quic_dghdlr *quic_dghdlrs; |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 52 | |
Frédéric Lécaille | 71f3abb | 2022-02-15 16:59:48 +0100 | [diff] [blame] | 53 | /* Size of the internal buffer of QUIC RX buffer at the fd level */ |
| 54 | #define QUIC_RX_BUFSZ (1UL << 18) |
| 55 | |
| 56 | DECLARE_STATIC_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ); |
| 57 | |
Frédéric Lécaille | 884f2e9 | 2020-11-23 14:23:21 +0100 | [diff] [blame] | 58 | static void quic_add_listener(struct protocol *proto, struct listener *listener); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 59 | static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen); |
| 60 | static int quic_connect_server(struct connection *conn, int flags); |
| 61 | static void quic_enable_listener(struct listener *listener); |
| 62 | static void quic_disable_listener(struct listener *listener); |
| 63 | |
| 64 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 65 | struct protocol proto_quic4 = { |
| 66 | .name = "quic4", |
| 67 | |
| 68 | /* connection layer */ |
| 69 | .ctrl_type = SOCK_STREAM, |
| 70 | .listen = quic_bind_listener, |
| 71 | .enable = quic_enable_listener, |
| 72 | .disable = quic_disable_listener, |
Frédéric Lécaille | 884f2e9 | 2020-11-23 14:23:21 +0100 | [diff] [blame] | 73 | .add = quic_add_listener, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 74 | .unbind = default_unbind_listener, |
| 75 | .suspend = default_suspend_listener, |
| 76 | .resume = default_resume_listener, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 77 | .accept_conn = quic_sock_accept_conn, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 78 | .connect = quic_connect_server, |
| 79 | |
| 80 | /* binding layer */ |
| 81 | .rx_suspend = udp_suspend_receiver, |
| 82 | .rx_resume = udp_resume_receiver, |
| 83 | |
| 84 | /* address family */ |
| 85 | .fam = &proto_fam_inet4, |
| 86 | |
| 87 | /* socket layer */ |
Willy Tarreau | 337edfd | 2021-10-27 17:05:36 +0200 | [diff] [blame] | 88 | .proto_type = PROTO_TYPE_DGRAM, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 89 | .sock_type = SOCK_DGRAM, |
| 90 | .sock_prot = IPPROTO_UDP, |
| 91 | .rx_enable = sock_enable, |
| 92 | .rx_disable = sock_disable, |
| 93 | .rx_unbind = sock_unbind, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 94 | .rx_listening = quic_sock_accepting_conn, |
| 95 | .default_iocb = quic_sock_fd_iocb, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 96 | .receivers = LIST_HEAD_INIT(proto_quic4.receivers), |
| 97 | .nb_receivers = 0, |
| 98 | }; |
| 99 | |
| 100 | INITCALL1(STG_REGISTER, protocol_register, &proto_quic4); |
| 101 | |
| 102 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 103 | struct protocol proto_quic6 = { |
| 104 | .name = "quic6", |
| 105 | |
| 106 | /* connection layer */ |
| 107 | .ctrl_type = SOCK_STREAM, |
| 108 | .listen = quic_bind_listener, |
| 109 | .enable = quic_enable_listener, |
| 110 | .disable = quic_disable_listener, |
Frédéric Lécaille | 884f2e9 | 2020-11-23 14:23:21 +0100 | [diff] [blame] | 111 | .add = quic_add_listener, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 112 | .unbind = default_unbind_listener, |
| 113 | .suspend = default_suspend_listener, |
| 114 | .resume = default_resume_listener, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 115 | .accept_conn = quic_sock_accept_conn, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 116 | .connect = quic_connect_server, |
| 117 | |
| 118 | /* binding layer */ |
| 119 | .rx_suspend = udp_suspend_receiver, |
| 120 | .rx_resume = udp_resume_receiver, |
| 121 | |
| 122 | /* address family */ |
| 123 | .fam = &proto_fam_inet6, |
| 124 | |
| 125 | /* socket layer */ |
Willy Tarreau | 337edfd | 2021-10-27 17:05:36 +0200 | [diff] [blame] | 126 | .proto_type = PROTO_TYPE_DGRAM, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 127 | .sock_type = SOCK_DGRAM, |
| 128 | .sock_prot = IPPROTO_UDP, |
| 129 | .rx_enable = sock_enable, |
| 130 | .rx_disable = sock_disable, |
| 131 | .rx_unbind = sock_unbind, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 132 | .rx_listening = quic_sock_accepting_conn, |
| 133 | .default_iocb = quic_sock_fd_iocb, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 134 | .receivers = LIST_HEAD_INIT(proto_quic6.receivers), |
| 135 | .nb_receivers = 0, |
| 136 | }; |
| 137 | |
| 138 | INITCALL1(STG_REGISTER, protocol_register, &proto_quic6); |
| 139 | |
| 140 | /* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which |
| 141 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 142 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 143 | * - 1 : use provided address |
| 144 | * - 2 : use provided port |
| 145 | * - 3 : use both |
| 146 | * |
| 147 | * The function supports multiple foreign binding methods : |
| 148 | * - linux_tproxy: we directly bind to the foreign address |
| 149 | * The second one can be used as a fallback for the first one. |
| 150 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 151 | * local address, 2 if it could not bind to the foreign address. |
| 152 | */ |
| 153 | int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote) |
| 154 | { |
| 155 | struct sockaddr_storage bind_addr; |
| 156 | int foreign_ok = 0; |
| 157 | int ret; |
| 158 | static THREAD_LOCAL int ip_transp_working = 1; |
| 159 | static THREAD_LOCAL int ip6_transp_working = 1; |
| 160 | |
| 161 | switch (local->ss_family) { |
| 162 | case AF_INET: |
| 163 | if (flags && ip_transp_working) { |
| 164 | /* This deserves some explanation. Some platforms will support |
| 165 | * multiple combinations of certain methods, so we try the |
| 166 | * supported ones until one succeeds. |
| 167 | */ |
| 168 | if (sock_inet4_make_foreign(fd)) |
| 169 | foreign_ok = 1; |
| 170 | else |
| 171 | ip_transp_working = 0; |
| 172 | } |
| 173 | break; |
| 174 | case AF_INET6: |
| 175 | if (flags && ip6_transp_working) { |
| 176 | if (sock_inet6_make_foreign(fd)) |
| 177 | foreign_ok = 1; |
| 178 | else |
| 179 | ip6_transp_working = 0; |
| 180 | } |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | if (flags) { |
| 185 | memset(&bind_addr, 0, sizeof(bind_addr)); |
| 186 | bind_addr.ss_family = remote->ss_family; |
| 187 | switch (remote->ss_family) { |
| 188 | case AF_INET: |
| 189 | if (flags & 1) |
| 190 | ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr; |
| 191 | if (flags & 2) |
| 192 | ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port; |
| 193 | break; |
| 194 | case AF_INET6: |
| 195 | if (flags & 1) |
| 196 | ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr; |
| 197 | if (flags & 2) |
| 198 | ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port; |
| 199 | break; |
| 200 | default: |
| 201 | /* we don't want to try to bind to an unknown address family */ |
| 202 | foreign_ok = 0; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
| 207 | if (foreign_ok) { |
| 208 | if (is_inet_addr(&bind_addr)) { |
| 209 | ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr)); |
| 210 | if (ret < 0) |
| 211 | return 2; |
| 212 | } |
| 213 | } |
| 214 | else { |
| 215 | if (is_inet_addr(local)) { |
| 216 | ret = bind(fd, (struct sockaddr *)local, get_addr_len(local)); |
| 217 | if (ret < 0) |
| 218 | return 1; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (!flags) |
| 223 | return 0; |
| 224 | |
| 225 | if (!foreign_ok) |
| 226 | /* we could not bind to a foreign address */ |
| 227 | return 2; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * This function initiates a QUIC connection establishment to the target assigned |
| 234 | * to connection <conn> using (si->{target,dst}). A source address may be |
| 235 | * pointed to by conn->src in case of transparent proxying. Normal source |
| 236 | * bind addresses are still determined locally (due to the possible need of a |
| 237 | * source port). conn->target may point either to a valid server or to a backend, |
| 238 | * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are |
| 239 | * supported. The <data> parameter is a boolean indicating whether there are data |
| 240 | * waiting for being sent or not, in order to adjust data write polling and on |
| 241 | * some platforms, the ability to avoid an empty initial ACK. The <flags> argument |
| 242 | * is not used. |
| 243 | * |
| 244 | * Note that a pending send_proxy message accounts for data. |
| 245 | * |
| 246 | * It can return one of : |
| 247 | * - SF_ERR_NONE if everything's OK |
| 248 | * - SF_ERR_SRVTO if there are no more servers |
| 249 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 250 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 251 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 252 | * - SF_ERR_INTERNAL for any other purely internal errors |
| 253 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 254 | * |
| 255 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
| 256 | * it's invalid and the caller has nothing to do. |
| 257 | */ |
| 258 | |
| 259 | int quic_connect_server(struct connection *conn, int flags) |
| 260 | { |
| 261 | int fd; |
| 262 | struct server *srv; |
| 263 | struct proxy *be; |
| 264 | struct conn_src *src; |
| 265 | struct sockaddr_storage *addr; |
| 266 | |
| 267 | conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */ |
| 268 | |
| 269 | switch (obj_type(conn->target)) { |
| 270 | case OBJ_TYPE_PROXY: |
Frédéric Lécaille | e1c3546 | 2022-02-14 19:01:21 +0100 | [diff] [blame] | 271 | be = __objt_proxy(conn->target); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 272 | srv = NULL; |
| 273 | break; |
| 274 | case OBJ_TYPE_SERVER: |
Frédéric Lécaille | e1c3546 | 2022-02-14 19:01:21 +0100 | [diff] [blame] | 275 | srv = __objt_server(conn->target); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 276 | be = srv->proxy; |
| 277 | break; |
| 278 | default: |
| 279 | conn->flags |= CO_FL_ERROR; |
| 280 | return SF_ERR_INTERNAL; |
| 281 | } |
| 282 | |
| 283 | if (!conn->dst) { |
| 284 | conn->flags |= CO_FL_ERROR; |
| 285 | return SF_ERR_INTERNAL; |
| 286 | } |
| 287 | |
| 288 | fd = conn->handle.fd = sock_create_server_socket(conn); |
| 289 | |
| 290 | if (fd == -1) { |
| 291 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 292 | |
| 293 | if (errno == ENFILE) { |
| 294 | conn->err_code = CO_ER_SYS_FDLIM; |
| 295 | send_log(be, LOG_EMERG, |
| 296 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 297 | be->id, global.maxsock); |
| 298 | } |
| 299 | else if (errno == EMFILE) { |
| 300 | conn->err_code = CO_ER_PROC_FDLIM; |
| 301 | send_log(be, LOG_EMERG, |
| 302 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 303 | be->id, global.maxsock); |
| 304 | } |
| 305 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 306 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 307 | send_log(be, LOG_EMERG, |
| 308 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 309 | be->id, global.maxsock); |
| 310 | } |
| 311 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 312 | conn->err_code = CO_ER_NOPROTO; |
| 313 | } |
| 314 | else |
| 315 | conn->err_code = CO_ER_SOCK_ERR; |
| 316 | |
| 317 | /* this is a resource error */ |
| 318 | conn->flags |= CO_FL_ERROR; |
| 319 | return SF_ERR_RESOURCE; |
| 320 | } |
| 321 | |
| 322 | if (fd >= global.maxsock) { |
| 323 | /* do not log anything there, it's a normal condition when this option |
| 324 | * is used to serialize connections to a server ! |
| 325 | */ |
| 326 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 327 | close(fd); |
| 328 | conn->err_code = CO_ER_CONF_FDLIM; |
| 329 | conn->flags |= CO_FL_ERROR; |
| 330 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
| 331 | } |
| 332 | |
| 333 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1)) { |
| 334 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 335 | close(fd); |
| 336 | conn->err_code = CO_ER_SOCK_ERR; |
| 337 | conn->flags |= CO_FL_ERROR; |
| 338 | return SF_ERR_INTERNAL; |
| 339 | } |
| 340 | |
| 341 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 342 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 343 | close(fd); |
| 344 | conn->err_code = CO_ER_SOCK_ERR; |
| 345 | conn->flags |= CO_FL_ERROR; |
| 346 | return SF_ERR_INTERNAL; |
| 347 | } |
| 348 | |
| 349 | /* allow specific binding : |
| 350 | * - server-specific at first |
| 351 | * - proxy-specific next |
| 352 | */ |
| 353 | if (srv && srv->conn_src.opts & CO_SRC_BIND) |
| 354 | src = &srv->conn_src; |
| 355 | else if (be->conn_src.opts & CO_SRC_BIND) |
| 356 | src = &be->conn_src; |
| 357 | else |
| 358 | src = NULL; |
| 359 | |
| 360 | if (src) { |
| 361 | int ret, flags = 0; |
| 362 | |
| 363 | if (conn->src && is_inet_addr(conn->src)) { |
| 364 | switch (src->opts & CO_SRC_TPROXY_MASK) { |
| 365 | case CO_SRC_TPROXY_CLI: |
| 366 | conn_set_private(conn); |
| 367 | /* fall through */ |
| 368 | case CO_SRC_TPROXY_ADDR: |
| 369 | flags = 3; |
| 370 | break; |
| 371 | case CO_SRC_TPROXY_CIP: |
| 372 | case CO_SRC_TPROXY_DYN: |
| 373 | conn_set_private(conn); |
| 374 | flags = 1; |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | #ifdef SO_BINDTODEVICE |
| 380 | /* Note: this might fail if not CAP_NET_RAW */ |
| 381 | if (src->iface_name) |
| 382 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1); |
| 383 | #endif |
| 384 | |
| 385 | if (src->sport_range) { |
| 386 | int attempts = 10; /* should be more than enough to find a spare port */ |
| 387 | struct sockaddr_storage sa; |
| 388 | |
| 389 | ret = 1; |
| 390 | memcpy(&sa, &src->source_addr, sizeof(sa)); |
| 391 | |
| 392 | do { |
| 393 | /* note: in case of retry, we may have to release a previously |
| 394 | * allocated port, hence this loop's construct. |
| 395 | */ |
| 396 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 397 | fdinfo[fd].port_range = NULL; |
| 398 | |
| 399 | if (!attempts) |
| 400 | break; |
| 401 | attempts--; |
| 402 | |
| 403 | fdinfo[fd].local_port = port_range_alloc_port(src->sport_range); |
| 404 | if (!fdinfo[fd].local_port) { |
| 405 | conn->err_code = CO_ER_PORT_RANGE; |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | fdinfo[fd].port_range = src->sport_range; |
| 410 | set_host_port(&sa, fdinfo[fd].local_port); |
| 411 | |
| 412 | ret = quic_bind_socket(fd, flags, &sa, conn->src); |
| 413 | if (ret != 0) |
| 414 | conn->err_code = CO_ER_CANT_BIND; |
| 415 | } while (ret != 0); /* binding NOK */ |
| 416 | } |
| 417 | else { |
| 418 | #ifdef IP_BIND_ADDRESS_NO_PORT |
| 419 | static THREAD_LOCAL int bind_address_no_port = 1; |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 420 | setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int)); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 421 | #endif |
| 422 | ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src); |
| 423 | if (ret != 0) |
| 424 | conn->err_code = CO_ER_CANT_BIND; |
| 425 | } |
| 426 | |
| 427 | if (unlikely(ret != 0)) { |
| 428 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 429 | fdinfo[fd].port_range = NULL; |
| 430 | close(fd); |
| 431 | |
| 432 | if (ret == 1) { |
| 433 | ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n", |
| 434 | be->id); |
| 435 | send_log(be, LOG_EMERG, |
| 436 | "Cannot bind to source address before connect() for backend %s.\n", |
| 437 | be->id); |
| 438 | } else { |
| 439 | ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n", |
| 440 | be->id); |
| 441 | send_log(be, LOG_EMERG, |
| 442 | "Cannot bind to tproxy source address before connect() for backend %s.\n", |
| 443 | be->id); |
| 444 | } |
| 445 | conn->flags |= CO_FL_ERROR; |
| 446 | return SF_ERR_RESOURCE; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (global.tune.server_sndbuf) |
| 451 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 452 | |
| 453 | if (global.tune.server_rcvbuf) |
| 454 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 455 | |
| 456 | addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst; |
| 457 | if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) { |
| 458 | if (errno == EINPROGRESS || errno == EALREADY) { |
| 459 | /* common case, let's wait for connect status */ |
| 460 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 461 | } |
| 462 | else if (errno == EISCONN) { |
| 463 | /* should normally not happen but if so, indicates that it's OK */ |
| 464 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 465 | } |
| 466 | else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
| 467 | char *msg; |
| 468 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
| 469 | msg = "no free ports"; |
| 470 | conn->err_code = CO_ER_FREE_PORTS; |
| 471 | } |
| 472 | else { |
| 473 | msg = "local address already in use"; |
| 474 | conn->err_code = CO_ER_ADDR_INUSE; |
| 475 | } |
| 476 | |
| 477 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
| 478 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 479 | fdinfo[fd].port_range = NULL; |
| 480 | close(fd); |
| 481 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
| 482 | conn->flags |= CO_FL_ERROR; |
| 483 | return SF_ERR_RESOURCE; |
| 484 | } else if (errno == ETIMEDOUT) { |
| 485 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
| 486 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 487 | fdinfo[fd].port_range = NULL; |
| 488 | close(fd); |
| 489 | conn->err_code = CO_ER_SOCK_ERR; |
| 490 | conn->flags |= CO_FL_ERROR; |
| 491 | return SF_ERR_SRVTO; |
| 492 | } else { |
| 493 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 494 | //qfprintf(stderr,"Connect(): %d", errno); |
| 495 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 496 | fdinfo[fd].port_range = NULL; |
| 497 | close(fd); |
| 498 | conn->err_code = CO_ER_SOCK_ERR; |
| 499 | conn->flags |= CO_FL_ERROR; |
| 500 | return SF_ERR_SRVCL; |
| 501 | } |
| 502 | } |
| 503 | else { |
| 504 | /* connect() == 0, this is great! */ |
| 505 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 506 | } |
| 507 | |
| 508 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 509 | |
| 510 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 511 | HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */ |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 512 | |
| 513 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 514 | fd_want_send(fd); |
| 515 | fd_cant_send(fd); |
| 516 | fd_cant_recv(fd); |
| 517 | } |
| 518 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 519 | return SF_ERR_NONE; /* connection is OK */ |
| 520 | } |
| 521 | |
Frédéric Lécaille | 884f2e9 | 2020-11-23 14:23:21 +0100 | [diff] [blame] | 522 | /* Add listener <listener> to protocol <proto>. Technically speaking we just |
| 523 | * initialize a few entries which should be doable during quic_bind_listener(). |
| 524 | * The end of the initialization goes on with the default function. |
| 525 | */ |
| 526 | static void quic_add_listener(struct protocol *proto, struct listener *listener) |
| 527 | { |
Amaury Denoyelle | b59b889 | 2022-01-25 17:48:47 +0100 | [diff] [blame] | 528 | listener->flags |= LI_F_QUIC_LISTENER; |
Amaury Denoyelle | 683b5fc | 2022-01-26 11:56:48 +0100 | [diff] [blame] | 529 | listener->rx.flags |= RX_F_LOCAL_ACCEPT; |
Amaury Denoyelle | 2ce99fe | 2022-01-19 15:46:11 +0100 | [diff] [blame] | 530 | |
Frédéric Lécaille | 884f2e9 | 2020-11-23 14:23:21 +0100 | [diff] [blame] | 531 | default_add_listener(proto, listener); |
| 532 | } |
| 533 | |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 534 | /* Allocate the TX ring buffers for <l> listener. |
| 535 | * Return 1 if succeeded, 0 if not. |
| 536 | */ |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 537 | static int quic_alloc_tx_rings_listener(struct listener *l) |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 538 | { |
| 539 | struct qring *qr; |
| 540 | int i; |
| 541 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 542 | l->rx.tx_qrings = calloc(global.nbthread, sizeof *l->rx.tx_qrings); |
| 543 | if (!l->rx.tx_qrings) |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 544 | return 0; |
| 545 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 546 | MT_LIST_INIT(&l->rx.tx_qring_list); |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 547 | for (i = 0; i < global.nbthread; i++) { |
Frédéric Lécaille | 8b19a9f | 2021-08-04 15:10:32 +0200 | [diff] [blame] | 548 | unsigned char *buf; |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 549 | struct qring *qr; |
| 550 | |
| 551 | qr = calloc(1, sizeof *qr); |
| 552 | if (!qr) |
| 553 | goto err; |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 554 | |
Frédéric Lécaille | 8b19a9f | 2021-08-04 15:10:32 +0200 | [diff] [blame] | 555 | buf = pool_alloc(pool_head_quic_tx_ring); |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 556 | if (!buf) { |
| 557 | free(qr); |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 558 | goto err; |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 559 | } |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 560 | |
Frédéric Lécaille | 8b19a9f | 2021-08-04 15:10:32 +0200 | [diff] [blame] | 561 | qr->cbuf = cbuf_new(buf, QUIC_TX_RING_BUFSZ); |
| 562 | if (!qr->cbuf) { |
| 563 | pool_free(pool_head_quic_tx_ring, buf); |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 564 | free(qr); |
Frédéric Lécaille | 8b19a9f | 2021-08-04 15:10:32 +0200 | [diff] [blame] | 565 | goto err; |
| 566 | } |
| 567 | |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 568 | l->rx.tx_qrings[i] = qr; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 569 | MT_LIST_APPEND(&l->rx.tx_qring_list, &qr->mt_list); |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return 1; |
| 573 | |
| 574 | err: |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 575 | while ((qr = MT_LIST_POP(&l->rx.tx_qring_list, typeof(qr), mt_list))) { |
Frédéric Lécaille | 8b19a9f | 2021-08-04 15:10:32 +0200 | [diff] [blame] | 576 | pool_free(pool_head_quic_tx_ring, qr->cbuf->buf); |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 577 | cbuf_free(qr->cbuf); |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 578 | free(qr); |
Frédéric Lécaille | 8b19a9f | 2021-08-04 15:10:32 +0200 | [diff] [blame] | 579 | } |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 580 | free(l->rx.tx_qrings); |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 581 | return 0; |
| 582 | } |
| 583 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 584 | /* Allocate the RX buffers for <l> listener. |
| 585 | * Return 1 if succeeded, 0 if not. |
| 586 | */ |
| 587 | static int quic_alloc_rxbufs_listener(struct listener *l) |
| 588 | { |
| 589 | int i; |
| 590 | struct rxbuf *rxbuf; |
| 591 | |
| 592 | l->rx.rxbufs = calloc(global.nbthread, sizeof *l->rx.rxbufs); |
| 593 | if (!l->rx.rxbufs) |
| 594 | return 0; |
| 595 | |
| 596 | MT_LIST_INIT(&l->rx.rxbuf_list); |
| 597 | for (i = 0; i < global.nbthread; i++) { |
| 598 | char *buf; |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 599 | struct rxbuf *rxbuf; |
| 600 | |
| 601 | rxbuf = calloc(1, sizeof *rxbuf); |
| 602 | if (!rxbuf) |
| 603 | goto err; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 604 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 605 | buf = pool_alloc(pool_head_quic_rxbuf); |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 606 | if (!buf) { |
| 607 | free(rxbuf); |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 608 | goto err; |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | l->rx.rxbufs[i] = rxbuf; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 612 | |
| 613 | rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 53898bb | 2022-01-26 15:55:21 +0100 | [diff] [blame] | 614 | LIST_INIT(&rxbuf->dgrams); |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 615 | MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->mt_list); |
| 616 | } |
| 617 | |
| 618 | return 1; |
| 619 | |
| 620 | err: |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 621 | while ((rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list))) { |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 622 | pool_free(pool_head_quic_rxbuf, rxbuf->buf.area); |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 623 | free(rxbuf); |
| 624 | } |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 625 | free(l->rx.rxbufs); |
| 626 | return 0; |
| 627 | } |
| 628 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 629 | /* This function tries to bind a QUIC4/6 listener. It may return a warning or |
| 630 | * an error message in <errmsg> if the message is at most <errlen> bytes long |
| 631 | * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero. |
| 632 | * The return value is composed from ERR_ABORT, ERR_WARN, |
| 633 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 634 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 635 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 636 | * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter |
| 637 | * the meaning of the error, but just indicate that a message is present which |
| 638 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 639 | * that it's pointless to try to start other listeners. No error message is |
| 640 | * returned if errlen is NULL. |
| 641 | */ |
| 642 | static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 643 | { |
| 644 | int err = ERR_NONE; |
| 645 | char *msg = NULL; |
| 646 | |
| 647 | /* ensure we never return garbage */ |
| 648 | if (errlen) |
| 649 | *errmsg = 0; |
| 650 | |
| 651 | if (listener->state != LI_ASSIGNED) |
| 652 | return ERR_NONE; /* already bound */ |
| 653 | |
| 654 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 655 | msg = "receiving socket not bound"; |
| 656 | goto udp_return; |
| 657 | } |
| 658 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 659 | if (!quic_alloc_tx_rings_listener(listener) || |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 660 | !quic_alloc_rxbufs_listener(listener)) { |
| 661 | msg = "could not initialize tx/rx rings"; |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 662 | err |= ERR_WARN; |
| 663 | goto udp_return; |
| 664 | } |
| 665 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 666 | listener_set_state(listener, LI_LISTEN); |
| 667 | |
| 668 | udp_return: |
| 669 | if (msg && errlen) { |
| 670 | char pn[INET6_ADDRSTRLEN]; |
| 671 | |
| 672 | addr_to_str(&listener->rx.addr, pn, sizeof(pn)); |
Willy Tarreau | 6823a3a | 2021-10-14 11:59:15 +0200 | [diff] [blame] | 673 | snprintf(errmsg, errlen, "%s for [%s:%d]", msg, pn, get_host_port(&listener->rx.addr)); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 674 | } |
| 675 | return err; |
| 676 | } |
| 677 | |
| 678 | /* Enable receipt of incoming connections for listener <l>. The receiver must |
| 679 | * still be valid. Does nothing in early boot (needs fd_updt). |
| 680 | */ |
| 681 | static void quic_enable_listener(struct listener *l) |
| 682 | { |
| 683 | /* FIXME: The following statements are incorrect. This |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 684 | * is the responsibility of the QUIC xprt to stop accepting new |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 685 | * connections. |
| 686 | */ |
| 687 | if (fd_updt) |
| 688 | fd_want_recv(l->rx.fd); |
| 689 | } |
| 690 | |
| 691 | /* Disable receipt of incoming connections for listener <l>. The receiver must |
| 692 | * still be valid. Does nothing in early boot (needs fd_updt). |
| 693 | */ |
| 694 | static void quic_disable_listener(struct listener *l) |
| 695 | { |
| 696 | /* FIXME: The following statements are incorrect. This |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 697 | * is the responsibility of the QUIC xprt to start accepting new |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 698 | * connections again. |
| 699 | */ |
| 700 | if (fd_updt) |
| 701 | fd_stop_recv(l->rx.fd); |
| 702 | } |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 703 | |
| 704 | static int quic_alloc_dghdlrs(void) |
| 705 | { |
| 706 | int i; |
| 707 | |
| 708 | quic_dghdlrs = calloc(global.nbthread, sizeof(struct quic_dghdlr)); |
| 709 | if (!quic_dghdlrs) { |
| 710 | ha_alert("Failed to allocate the quic datagram handlers.\n"); |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | for (i = 0; i < global.nbthread; i++) { |
| 715 | struct quic_dghdlr *dghdlr = &quic_dghdlrs[i]; |
| 716 | |
| 717 | dghdlr->task = tasklet_new(); |
| 718 | if (!dghdlr->task) { |
| 719 | ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | tasklet_set_tid(dghdlr->task, i); |
| 724 | dghdlr->task->context = dghdlr; |
| 725 | dghdlr->task->process = quic_lstnr_dghdlr; |
| 726 | |
| 727 | dghdlr->odcids = EB_ROOT_UNIQUE; |
| 728 | dghdlr->cids = EB_ROOT_UNIQUE; |
| 729 | |
| 730 | MT_LIST_INIT(&dghdlr->dgrams); |
| 731 | } |
| 732 | |
| 733 | return 1; |
| 734 | } |
| 735 | REGISTER_POST_CHECK(quic_alloc_dghdlrs); |
| 736 | |
| 737 | static int quic_deallocate_dghdlrs(void) |
| 738 | { |
| 739 | int i; |
| 740 | |
| 741 | if (quic_dghdlrs) { |
| 742 | for (i = 0; i < global.nbthread; ++i) |
| 743 | tasklet_free(quic_dghdlrs[i].task); |
| 744 | free(quic_dghdlrs); |
| 745 | } |
| 746 | |
| 747 | return 1; |
| 748 | } |
| 749 | REGISTER_POST_DEINIT(quic_deallocate_dghdlrs); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 750 | |
| 751 | /* |
| 752 | * Local variables: |
| 753 | * c-indent-level: 8 |
| 754 | * c-basic-offset: 8 |
| 755 | * End: |
| 756 | */ |