Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC socket management. |
| 3 | * |
| 4 | * Copyright 2020 HAProxy Technologies, 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 <errno.h> |
| 14 | |
| 15 | #include <sys/socket.h> |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | #include <haproxy/connection.h> |
| 19 | #include <haproxy/listener.h> |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 20 | #include <haproxy/session.h> |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 21 | #include <haproxy/xprt_quic.h> |
| 22 | |
| 23 | /* This function is called from the protocol layer accept() in order to |
| 24 | * instantiate a new session on behalf of a given listener and frontend. It |
| 25 | * returns a positive value upon success, 0 if the connection can be ignored, |
| 26 | * or a negative value upon critical failure. The accepted connection is |
| 27 | * closed if we return <= 0. If no handshake is needed, it immediately tries |
| 28 | * to instantiate a new stream. The connection must already have been filled |
| 29 | * with the incoming connection handle (a fd), a target (the listener) and a |
| 30 | * source address. |
| 31 | */ |
| 32 | int quic_session_accept(struct connection *cli_conn) |
| 33 | { |
| 34 | struct listener *l = __objt_listener(cli_conn->target); |
| 35 | struct proxy *p = l->bind_conf->frontend; |
| 36 | struct session *sess; |
| 37 | |
| 38 | cli_conn->proxy_netns = l->rx.settings->netns; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 39 | /* This flag is ordinarily set by conn_ctrl_init() which cannot |
| 40 | * be called for now. |
| 41 | */ |
| 42 | cli_conn->flags |= CO_FL_CTRL_READY; |
| 43 | |
| 44 | /* wait for a PROXY protocol header */ |
| 45 | if (l->options & LI_O_ACC_PROXY) |
| 46 | cli_conn->flags |= CO_FL_ACCEPT_PROXY; |
| 47 | |
| 48 | /* wait for a NetScaler client IP insertion protocol header */ |
| 49 | if (l->options & LI_O_ACC_CIP) |
| 50 | cli_conn->flags |= CO_FL_ACCEPT_CIP; |
| 51 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 52 | /* Add the handshake pseudo-XPRT */ |
| 53 | if (cli_conn->flags & (CO_FL_ACCEPT_PROXY | CO_FL_ACCEPT_CIP)) { |
| 54 | if (xprt_add_hs(cli_conn) != 0) |
| 55 | goto out_free_conn; |
| 56 | } |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 57 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 58 | sess = session_new(p, l, &cli_conn->obj_type); |
| 59 | if (!sess) |
| 60 | goto out_free_conn; |
| 61 | |
| 62 | conn_set_owner(cli_conn, sess, NULL); |
| 63 | |
Frédéric Lécaille | ecb5872 | 2021-05-27 17:12:36 +0200 | [diff] [blame] | 64 | if (conn_complete_session(cli_conn) < 0) |
| 65 | goto out_free_sess; |
| 66 | |
| 67 | if (conn_xprt_start(cli_conn) >= 0) |
Frédéric Lécaille | 27faba7 | 2021-03-03 16:21:00 +0100 | [diff] [blame] | 68 | return 1; |
| 69 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 70 | out_free_sess: |
| 71 | /* prevent call to listener_release during session_free. It will be |
| 72 | * done below, for all errors. */ |
| 73 | sess->listener = NULL; |
| 74 | session_free(sess); |
| 75 | out_free_conn: |
| 76 | cli_conn->qc->conn = NULL; |
| 77 | conn_stop_tracking(cli_conn); |
| 78 | conn_xprt_close(cli_conn); |
| 79 | conn_free(cli_conn); |
| 80 | out: |
| 81 | |
Frédéric Lécaille | e8139f3 | 2021-03-11 17:06:30 +0100 | [diff] [blame] | 82 | return -1; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Inspired from session_accept_fd(). |
| 87 | * Instantiate a new connection (connection struct) to be attached to <qc> |
| 88 | * QUIC connection of <l> listener. |
| 89 | * Returns 1 if succeeded, 0 if not. |
| 90 | */ |
| 91 | static int new_quic_cli_conn(struct quic_conn *qc, struct listener *l, |
| 92 | struct sockaddr_storage *saddr) |
| 93 | { |
| 94 | struct connection *cli_conn; |
| 95 | struct sockaddr_storage *dst; |
| 96 | |
| 97 | dst = NULL; |
| 98 | if (unlikely((cli_conn = conn_new(&l->obj_type)) == NULL)) |
| 99 | goto out; |
| 100 | |
| 101 | if (!sockaddr_alloc(&dst, saddr, sizeof *saddr)) |
| 102 | goto out_free_conn; |
| 103 | |
| 104 | qc->conn = cli_conn; |
| 105 | cli_conn->qc = qc; |
| 106 | |
| 107 | cli_conn->dst = dst; |
| 108 | cli_conn->handle.fd = l->rx.fd; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 109 | cli_conn->target = &l->obj_type; |
| 110 | |
| 111 | /* XXX Should not be there. */ |
| 112 | l->accept = quic_session_accept; |
Frédéric Lécaille | 01ab661 | 2021-06-14 10:31:43 +0200 | [diff] [blame] | 113 | /* We need the xprt context before accepting (->accept()) the connection: |
| 114 | * we may receive packet before this connection acception. |
| 115 | */ |
| 116 | if (conn_prepare(cli_conn, l->rx.proto, l->bind_conf->xprt) < 0) |
| 117 | goto out_free_conn; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 118 | |
| 119 | return 1; |
| 120 | |
| 121 | out_free_conn: |
Frédéric Lécaille | 01ab661 | 2021-06-14 10:31:43 +0200 | [diff] [blame] | 122 | qc->conn = NULL; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 123 | conn_stop_tracking(cli_conn); |
| 124 | conn_xprt_close(cli_conn); |
| 125 | conn_free(cli_conn); |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 126 | out: |
| 127 | |
| 128 | return 0; |
| 129 | } |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 130 | |
| 131 | /* Tests if the receiver supports accepting connections. Returns positive on |
| 132 | * success, 0 if not possible |
| 133 | */ |
| 134 | int quic_sock_accepting_conn(const struct receiver *rx) |
| 135 | { |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | /* Accept an incoming connection from listener <l>, and return it, as well as |
| 140 | * a CO_AC_* status code into <status> if not null. Null is returned on error. |
| 141 | * <l> must be a valid listener with a valid frontend. |
| 142 | */ |
| 143 | struct connection *quic_sock_accept_conn(struct listener *l, int *status) |
| 144 | { |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 145 | struct quic_conn *qc; |
| 146 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 147 | int ret; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 148 | |
| 149 | qc = NULL; |
Frédéric Lécaille | c28aba2 | 2021-06-07 10:28:10 +0200 | [diff] [blame] | 150 | pkt = MT_LIST_POP(&l->rx.pkts, struct quic_rx_packet *, rx_list); |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 151 | /* Should never happen. */ |
Frédéric Lécaille | c28aba2 | 2021-06-07 10:28:10 +0200 | [diff] [blame] | 152 | if (!pkt) |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 153 | goto err; |
| 154 | |
| 155 | qc = pkt->qc; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 156 | if (!new_quic_cli_conn(qc, l, &pkt->saddr)) |
| 157 | goto err; |
| 158 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 159 | ret = CO_AC_DONE; |
| 160 | |
| 161 | done: |
| 162 | if (status) |
| 163 | *status = ret; |
| 164 | |
| 165 | return qc ? qc->conn : NULL; |
| 166 | |
| 167 | err: |
| 168 | ret = CO_AC_PAUSE; |
| 169 | goto done; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Function called on a read event from a listening socket. It tries |
| 173 | * to handle as many connections as possible. |
| 174 | */ |
| 175 | void quic_sock_fd_iocb(int fd) |
| 176 | { |
| 177 | ssize_t ret; |
| 178 | struct buffer *buf; |
| 179 | struct listener *l = objt_listener(fdtab[fd].owner); |
| 180 | /* Source address */ |
| 181 | struct sockaddr_storage saddr = {0}; |
| 182 | socklen_t saddrlen; |
| 183 | |
Tim Duesterhus | 1655424 | 2021-09-15 13:58:49 +0200 | [diff] [blame] | 184 | BUG_ON(!l); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 185 | |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 186 | if (!(fdtab[fd].state & FD_POLL_IN) || !fd_recv_ready(fd)) |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 187 | return; |
| 188 | |
| 189 | buf = get_trash_chunk(); |
| 190 | saddrlen = sizeof saddr; |
| 191 | do { |
| 192 | ret = recvfrom(fd, buf->area, buf->size, 0, |
| 193 | (struct sockaddr *)&saddr, &saddrlen); |
| 194 | if (ret < 0) { |
| 195 | if (errno == EINTR) |
| 196 | continue; |
| 197 | if (errno == EAGAIN) |
| 198 | fd_cant_recv(fd); |
| 199 | return; |
| 200 | } |
| 201 | } while (0); |
| 202 | |
| 203 | buf->data = ret; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 204 | quic_lstnr_dgram_read(buf->area, buf->data, l, &saddr); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 205 | } |