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> |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 20 | #include <haproxy/xprt_quic.h> |
| 21 | |
| 22 | /* This function is called from the protocol layer accept() in order to |
| 23 | * instantiate a new session on behalf of a given listener and frontend. It |
| 24 | * returns a positive value upon success, 0 if the connection can be ignored, |
| 25 | * or a negative value upon critical failure. The accepted connection is |
| 26 | * closed if we return <= 0. If no handshake is needed, it immediately tries |
| 27 | * to instantiate a new stream. The connection must already have been filled |
| 28 | * with the incoming connection handle (a fd), a target (the listener) and a |
| 29 | * source address. |
| 30 | */ |
| 31 | int quic_session_accept(struct connection *cli_conn) |
| 32 | { |
| 33 | struct listener *l = __objt_listener(cli_conn->target); |
| 34 | struct proxy *p = l->bind_conf->frontend; |
| 35 | struct session *sess; |
| 36 | |
| 37 | cli_conn->proxy_netns = l->rx.settings->netns; |
| 38 | conn_prepare(cli_conn, l->rx.proto, l->bind_conf->xprt); |
| 39 | |
| 40 | /* This flag is ordinarily set by conn_ctrl_init() which cannot |
| 41 | * be called for now. |
| 42 | */ |
| 43 | cli_conn->flags |= CO_FL_CTRL_READY; |
| 44 | |
| 45 | /* wait for a PROXY protocol header */ |
| 46 | if (l->options & LI_O_ACC_PROXY) |
| 47 | cli_conn->flags |= CO_FL_ACCEPT_PROXY; |
| 48 | |
| 49 | /* wait for a NetScaler client IP insertion protocol header */ |
| 50 | if (l->options & LI_O_ACC_CIP) |
| 51 | cli_conn->flags |= CO_FL_ACCEPT_CIP; |
| 52 | |
| 53 | if (conn_xprt_init(cli_conn) < 0) |
| 54 | goto out_free_conn; |
| 55 | |
| 56 | /* Add the handshake pseudo-XPRT */ |
| 57 | if (cli_conn->flags & (CO_FL_ACCEPT_PROXY | CO_FL_ACCEPT_CIP)) { |
| 58 | if (xprt_add_hs(cli_conn) != 0) |
| 59 | goto out_free_conn; |
| 60 | } |
| 61 | sess = session_new(p, l, &cli_conn->obj_type); |
| 62 | if (!sess) |
| 63 | goto out_free_conn; |
| 64 | |
| 65 | conn_set_owner(cli_conn, sess, NULL); |
| 66 | |
| 67 | return 1; |
| 68 | |
| 69 | out_free_sess: |
| 70 | /* prevent call to listener_release during session_free. It will be |
| 71 | * done below, for all errors. */ |
| 72 | sess->listener = NULL; |
| 73 | session_free(sess); |
| 74 | out_free_conn: |
| 75 | cli_conn->qc->conn = NULL; |
| 76 | conn_stop_tracking(cli_conn); |
| 77 | conn_xprt_close(cli_conn); |
| 78 | conn_free(cli_conn); |
| 79 | out: |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Inspired from session_accept_fd(). |
| 86 | * Instantiate a new connection (connection struct) to be attached to <qc> |
| 87 | * QUIC connection of <l> listener. |
| 88 | * Returns 1 if succeeded, 0 if not. |
| 89 | */ |
| 90 | static int new_quic_cli_conn(struct quic_conn *qc, struct listener *l, |
| 91 | struct sockaddr_storage *saddr) |
| 92 | { |
| 93 | struct connection *cli_conn; |
| 94 | struct sockaddr_storage *dst; |
| 95 | |
| 96 | dst = NULL; |
| 97 | if (unlikely((cli_conn = conn_new(&l->obj_type)) == NULL)) |
| 98 | goto out; |
| 99 | |
| 100 | if (!sockaddr_alloc(&dst, saddr, sizeof *saddr)) |
| 101 | goto out_free_conn; |
| 102 | |
| 103 | qc->conn = cli_conn; |
| 104 | cli_conn->qc = qc; |
| 105 | |
| 106 | cli_conn->dst = dst; |
| 107 | cli_conn->handle.fd = l->rx.fd; |
| 108 | cli_conn->flags |= CO_FL_ADDR_FROM_SET; |
| 109 | cli_conn->target = &l->obj_type; |
| 110 | |
| 111 | /* XXX Should not be there. */ |
| 112 | l->accept = quic_session_accept; |
| 113 | |
| 114 | return 1; |
| 115 | |
| 116 | out_free_conn: |
| 117 | conn_stop_tracking(cli_conn); |
| 118 | conn_xprt_close(cli_conn); |
| 119 | conn_free(cli_conn); |
| 120 | qc->conn = NULL; |
| 121 | out: |
| 122 | |
| 123 | return 0; |
| 124 | } |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 125 | |
| 126 | /* Tests if the receiver supports accepting connections. Returns positive on |
| 127 | * success, 0 if not possible |
| 128 | */ |
| 129 | int quic_sock_accepting_conn(const struct receiver *rx) |
| 130 | { |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | /* Accept an incoming connection from listener <l>, and return it, as well as |
| 135 | * a CO_AC_* status code into <status> if not null. Null is returned on error. |
| 136 | * <l> must be a valid listener with a valid frontend. |
| 137 | */ |
| 138 | struct connection *quic_sock_accept_conn(struct listener *l, int *status) |
| 139 | { |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 140 | struct quic_conn *qc; |
| 141 | struct quic_rx_packet *pkt; |
| 142 | struct quic_cid *odcid; |
| 143 | int ret, ipv4; |
| 144 | |
| 145 | qc = NULL; |
| 146 | pkt = LIST_ELEM(l->rx.qpkts.n, struct quic_rx_packet *, rx_list); |
| 147 | /* Should never happen. */ |
| 148 | if (&pkt->rx_list == &l->rx.qpkts) |
| 149 | goto err; |
| 150 | |
| 151 | qc = pkt->qc; |
| 152 | LIST_DEL(&pkt->rx_list); |
| 153 | if (!new_quic_cli_conn(qc, l, &pkt->saddr)) |
| 154 | goto err; |
| 155 | |
| 156 | ipv4 = pkt->saddr.ss_family == AF_INET; |
| 157 | if (!qc_new_conn_init(qc, ipv4, &l->rx.odcids, &l->rx.cids, |
| 158 | pkt->dcid.data, pkt->dcid.len, |
| 159 | pkt->scid.data, pkt->scid.len)) |
| 160 | goto err; |
| 161 | |
| 162 | odcid = &qc->params.original_destination_connection_id; |
| 163 | /* Copy the transport parameters. */ |
| 164 | qc->params = l->bind_conf->quic_params; |
| 165 | /* Copy original_destination_connection_id transport parameter. */ |
| 166 | memcpy(odcid->data, &pkt->dcid, pkt->odcid_len); |
| 167 | odcid->len = pkt->odcid_len; |
| 168 | /* Copy the initial source connection ID. */ |
| 169 | quic_cid_cpy(&qc->params.initial_source_connection_id, &qc->scid); |
| 170 | qc->enc_params_len = |
| 171 | quic_transport_params_encode(qc->enc_params, |
| 172 | qc->enc_params + sizeof qc->enc_params, |
| 173 | &qc->params, 1); |
| 174 | if (!qc->enc_params_len) |
| 175 | goto err; |
| 176 | |
| 177 | ret = CO_AC_DONE; |
| 178 | |
| 179 | done: |
| 180 | if (status) |
| 181 | *status = ret; |
| 182 | |
| 183 | return qc ? qc->conn : NULL; |
| 184 | |
| 185 | err: |
| 186 | ret = CO_AC_PAUSE; |
| 187 | goto done; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* Function called on a read event from a listening socket. It tries |
| 191 | * to handle as many connections as possible. |
| 192 | */ |
| 193 | void quic_sock_fd_iocb(int fd) |
| 194 | { |
| 195 | ssize_t ret; |
| 196 | struct buffer *buf; |
| 197 | struct listener *l = objt_listener(fdtab[fd].owner); |
| 198 | /* Source address */ |
| 199 | struct sockaddr_storage saddr = {0}; |
| 200 | socklen_t saddrlen; |
| 201 | |
| 202 | if (!l) |
| 203 | ABORT_NOW(); |
| 204 | |
| 205 | if (!(fdtab[fd].ev & FD_POLL_IN) || !fd_recv_ready(fd)) |
| 206 | return; |
| 207 | |
| 208 | buf = get_trash_chunk(); |
| 209 | saddrlen = sizeof saddr; |
| 210 | do { |
| 211 | ret = recvfrom(fd, buf->area, buf->size, 0, |
| 212 | (struct sockaddr *)&saddr, &saddrlen); |
| 213 | if (ret < 0) { |
| 214 | if (errno == EINTR) |
| 215 | continue; |
| 216 | if (errno == EAGAIN) |
| 217 | fd_cant_recv(fd); |
| 218 | return; |
| 219 | } |
| 220 | } while (0); |
| 221 | |
| 222 | buf->data = ret; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 223 | 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] | 224 | } |