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