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