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