Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes-entries/h3_listen_address.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*) mod_http3: Serve HTTP/3 only on the address given to Listen, as for
TCP, rather than on every address of the host. When Listen gives the
HTTP/3 host several addresses, HTTP/3 is not started.
[Arturo Bernal]
2 changes: 2 additions & 0 deletions modules/http3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Listen 4433 https
</VirtualHost>
```

The QUIC socket follows the address of `Listen`: `Listen 192.0.2.1:4433` serves HTTP/3 on 192.0.2.1 only, as for TCP. When `Listen` gives the HTTP/3 host several addresses, one QUIC socket cannot serve them alone and HTTP/3 is not started.

### 2. Client Discovery via `Alt-Svc`

Because HTTP/3 runs over UDP, web browsers typically connect via TCP (HTTP/1.1 or HTTP/2) first. `mod_http3` automatically injects the `Alt-Svc` header into responses:
Expand Down
2 changes: 1 addition & 1 deletion modules/http3/h3_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp,
SSL_CTX_set_cert_cb(conf->ssl_ctx, h3_sni_select_cert, sni);

h3_request_init();
h3_server_post_config(s);
h3_server_post_config(p, s);
return OK;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/http3/h3_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int h3_hook_fixups(request_rec* r)

h3_server_conf* conf = ap_get_module_config(r->server->module_config, &http3_module);

if (!conf || !conf->ssl_ctx || conf->h3_port == 0)
if (!conf || !conf->ssl_ctx || conf->h3_port == 0 || !conf->quic_addr)
{
return DECLINED;
}
Expand Down
105 changes: 98 additions & 7 deletions modules/http3/h3_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,53 @@ struct port_acquire_args
h3_server_conf* conf;
};

/* The QUIC socket serves the addresses httpd listens on for the TCP port of
* the HTTP/3 host: Alt-Svc names no host, so a client comes back over UDP to
* the address it reached over TCP. A host with no port (the main server, or
* a <VirtualHost> without one) is served by every listener. A wildcard Listen
* keeps the dual-stack wildcard, as does a port no Listen names. One QUIC
* socket cannot serve several addresses alone, which gives APR_EINVAL. */
static apr_status_t choose_quic_addr(apr_pool_t* p, apr_port_t tcp_port, apr_port_t h3_port, apr_sockaddr_t** out)
{
apr_sockaddr_t* one = NULL;
int wild4 = 0;
int wild6 = 0;
int several = 0;
for (ap_listen_rec* lr = ap_listeners; lr; lr = lr->next)
{
apr_sockaddr_t* sa = lr->bind_addr;
if (!sa || (tcp_port && sa->port != tcp_port))
{
continue;
}
if (apr_sockaddr_is_wildcard(sa))
{
wild4 |= sa->family == APR_INET;
wild6 |= sa->family == APR_INET6;
}
else if (!one)
{
one = sa;
}
else if (one->family != sa->family || !apr_sockaddr_equal(one, sa))
{
several = 1;
}
}
if (several || (one && (wild4 || wild6)))
{
return APR_EINVAL;
}
if (one)
{
/* By its text, which keeps the zone of a link-local address. */
char ip[64];
apr_status_t rv = apr_sockaddr_ip_getbuf(ip, sizeof(ip), one);
return rv == APR_SUCCESS ? apr_sockaddr_info_get(out, ip, one->family, h3_port, 0, p) : rv;
}
return apr_sockaddr_info_get(out, NULL, wild4 && !wild6 ? APR_INET : APR_INET6, h3_port, 0, p);
}

#ifdef __linux__
/* The parent binds the QUIC sockets while still privileged and keeps them across restarts.
* One SO_REUSEPORT socket per httpd listener bucket (ListenCoresBucketsRatio), so the kernel hash stays stable when children come and go. */
Expand All @@ -66,9 +113,15 @@ typedef struct
int* fds;
int n;
apr_port_t port;
apr_sockaddr_t* addr; /* the bound address, from pool */
int routed; /* an eBPF program routes the packets by connection ID */
} h3_parent_socket;

static int same_addr(const apr_sockaddr_t* a, const apr_sockaddr_t* b)
{
return a && b && a->family == b->family && apr_sockaddr_equal(a, b);
}

/* The same count as httpd for ListenCoresBucketsRatio: online cores / ratio. Some MPMs count their buckets after post_config.
* ponytail: top-level directive only, not one inside <IfModule>. */
static int listen_buckets(void)
Expand Down Expand Up @@ -133,7 +186,7 @@ static apr_status_t acquire_port(apr_pool_t* pool, server_rec* s, h3_server_conf
return APR_EAGAIN;
}
#endif
return h3_socket_open(conf->h3_port, conf->h3_socket_buffer_size, pool, 0, fd);
return h3_socket_open(conf->quic_addr, conf->h3_socket_buffer_size, pool, 0, fd);
}

/* Give the port back when this child cannot serve it after all. */
Expand Down Expand Up @@ -238,6 +291,10 @@ void h3_child_init(apr_pool_t* pchild, server_rec* s)
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "h3_child_init: no H3 cert/key configured, skipping");
return;
}
if (!conf->quic_addr)
{
return; /* refused in post_config, which logged why */
}
apr_pool_pre_cleanup_register(pchild, NULL, child_pool_cleanup);
if (h3_thread_safe_pool_create(pchild, "h3_child", &pool) != APR_SUCCESS)
{
Expand Down Expand Up @@ -314,7 +371,7 @@ int h3_server_route_bucket(server_rec* s)
#endif
}

void h3_server_post_config(server_rec* s)
void h3_server_post_config(apr_pool_t* p, server_rec* s)
{
if (!h3_server_secret(s))
{
Expand All @@ -324,21 +381,51 @@ void h3_server_post_config(server_rec* s)
apr_pool_userdata_set(secret, H3_SECRET_KEY, apr_pool_cleanup_null, s->process->pool);
}
}
server_rec* vhost = NULL;
h3_server_conf* conf = find_h3_server(s, &vhost);
apr_sockaddr_t* quic_addr = NULL;
if (conf)
{
apr_status_t rv = choose_quic_addr(p, conf->host_port, conf->h3_port, &quic_addr);
if (rv != APR_SUCCESS)
{
/* Binding the wildcard instead would serve addresses httpd was told not to. */
if (rv == APR_EINVAL)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, vhost, "mod_http3: Listen gives the HTTP/3 host several addresses, which one QUIC socket cannot serve alone; HTTP/3 is not started");
}
else
{
ap_log_error(APLOG_MARK, APLOG_ERR, rv, vhost, "mod_http3: no address for the QUIC socket on port %d; HTTP/3 is not started", (int)conf->h3_port);
}
quic_addr = NULL;
conf = NULL;
}
/* Every HTTP/3 host is served by the one QUIC socket; a NULL address
* keeps Alt-Svc off for all of them. */
for (server_rec* cur = s; cur; cur = cur->next)
{
h3_server_conf* hc = ap_get_module_config(cur->module_config, &http3_module);
if (hc && hc->ssl_ctx)
{
hc->quic_addr = quic_addr;
}
}
}
#ifdef __linux__
h3_parent_socket* ps = parent_socket(s);
if (!ps)
{
ps = apr_pcalloc(s->process->pool, sizeof(*ps));
apr_pool_userdata_set(ps, "mod_http3.parent_socket", apr_pool_cleanup_null, s->process->pool);
}
server_rec* vhost = NULL;
h3_server_conf* conf = find_h3_server(s, &vhost);
int n = listen_buckets();
/* ponytail: a new port or bucket count binds again. An old single socket without SO_REUSEPORT blocks this until its children stop. */
if (ps->n && (!conf || conf->h3_port != ps->port || n != ps->n))
/* ponytail: a new address, port or bucket count binds again. An old single socket without SO_REUSEPORT blocks this until its children stop. */
if (ps->n && (!conf || conf->h3_port != ps->port || n != ps->n || !same_addr(ps->addr, quic_addr)))
{
apr_pool_destroy(ps->pool); /* closes the old sockets */
ps->pool = NULL;
ps->addr = NULL;
ps->n = 0;
ps->routed = 0;
}
Expand All @@ -350,10 +437,13 @@ void h3_server_post_config(server_rec* s)
if (ok)
{
ps->fds = apr_palloc(ps->pool, sizeof(int) * (size_t)n);
/* The sockets outlive this configuration, so bind them with a copy
* that lives in their pool. */
ok = apr_sockaddr_info_copy(&ps->addr, quic_addr, ps->pool) == APR_SUCCESS;
}
for (int i = 0; ok && i < n; i++)
{
ok = h3_socket_open(conf->h3_port, conf->h3_socket_buffer_size, ps->pool, n > 1, &ps->fds[i]) == APR_SUCCESS;
ok = h3_socket_open(ps->addr, conf->h3_socket_buffer_size, ps->pool, n > 1, &ps->fds[i]) == APR_SUCCESS;
}
if (!ok)
{
Expand All @@ -363,6 +453,7 @@ void h3_server_post_config(server_rec* s)
apr_pool_destroy(ps->pool);
}
ps->pool = NULL;
ps->addr = NULL;
return;
}
ps->n = n;
Expand Down
31 changes: 17 additions & 14 deletions modules/http3/h3_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,29 @@ static void tune_buffers(apr_socket_t* sock, apr_size_t want, apr_port_t port, a
}
}

apr_status_t h3_socket_open(apr_port_t port, apr_size_t buffer_size, apr_pool_t* pool, int reuse_port, int* out_fd)
apr_status_t h3_socket_open(apr_sockaddr_t* addr, apr_size_t buffer_size, apr_pool_t* pool, int reuse_port, int* out_fd)
{
CHECK(addr);
CHECK(pool);
CHECK(out_fd);
apr_port_t port = addr->port;
char ip[64];
if (apr_sockaddr_ip_getbuf(ip, sizeof(ip), addr) != APR_SUCCESS)
{
ip[0] = '\0';
}
apr_socket_t* sock = NULL;
apr_status_t rv = apr_socket_create(&sock, APR_INET6, SOCK_DGRAM, APR_PROTO_UDP, pool);
apr_status_t rv = apr_socket_create(&sock, addr->family, SOCK_DGRAM, APR_PROTO_UDP, pool);
if (rv != APR_SUCCESS)
{
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, pool, "h3_socket_open: apr_socket_create failed");
ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool, "h3_socket_open: apr_socket_create failed");
return rv;
}
apr_socket_opt_set(sock, APR_IPV6_V6ONLY, 0);
if (addr->family == APR_INET6)
{
/* The wildcard takes IPv4 as well; an address from Listen is served alone. */
apr_socket_opt_set(sock, APR_IPV6_V6ONLY, apr_sockaddr_is_wildcard(addr) ? 0 : 1);
}
tune_buffers(sock, buffer_size, port, pool);
#ifdef SO_REUSEPORT
apr_os_sock_t reuse_sock = -1;
Expand All @@ -79,24 +90,16 @@ apr_status_t h3_socket_open(apr_port_t port, apr_size_t buffer_size, apr_pool_t*
#else
(void)reuse_port;
#endif
apr_sockaddr_t* addr = NULL;
rv = apr_sockaddr_info_get(&addr, NULL, APR_INET6, port, 0, pool);
if (rv != APR_SUCCESS)
{
apr_socket_close(sock);
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, pool, "h3_socket_open: apr_sockaddr_info_get failed");
return rv;
}
rv = apr_socket_bind(sock, addr);
if (rv != APR_SUCCESS)
{
apr_socket_close(sock);
if (h3_socket_os_is_eaddrinuse(rv))
{
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, pool, "bind(%d) skipped (port already owned)", (int)port);
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, pool, "bind(%s:%d) skipped (port already owned)", ip, (int)port);
return APR_EAGAIN;
}
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, pool, "bind(%d) failed", (int)port);
ap_log_perror(APLOG_MARK, APLOG_ERR, rv, pool, "bind(%s:%d) failed", ip, (int)port);
return rv;
}
rv = apr_socket_timeout_set(sock, 0);
Expand Down
2 changes: 2 additions & 0 deletions modules/http3/include/h3_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct h3_server_conf
struct ssl_ctx_st* ssl_ctx;

apr_port_t h3_port;
/** Address the QUIC socket binds, chosen in post_config; NULL when HTTP/3 is not started. */
apr_sockaddr_t* quic_addr;
apr_uint32_t h3_max_concurrent_streams;
apr_uint32_t h3_max_connections;
apr_size_t h3_stream_buffer_size;
Expand Down
6 changes: 4 additions & 2 deletions modules/http3/include/h3_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
void h3_child_init(apr_pool_t* pchild, server_rec* s);

/**
* Bind the QUIC socket in the parent while it is still privileged (Linux), and
* Choose the address of the QUIC socket from the Listen directives, then
* bind the socket in the parent while it is still privileged (Linux), and
* keep it across graceful restarts for the children to inherit.
* @param p Configuration pool.
* @param s Main server.
*/
void h3_server_post_config(server_rec* s);
void h3_server_post_config(apr_pool_t* p, server_rec* s);

/**
* The QUIC token key. The parent makes it once and keeps it across restarts,
Expand Down
8 changes: 5 additions & 3 deletions modules/http3/include/h3_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
#include <apr_portable.h>

/**
* Open a non-blocking IPv6 dual-stack UDP socket and bind it to the given port.
* Open a non-blocking UDP socket and bind it to @p addr. The IPv6 wildcard
* is bound dual-stack, so it takes IPv4 too; any other address is bound
* alone, in its own family.
* With @p reuse_port, sets SO_REUSEPORT (where the OS has it), so several
* sockets can share the port and the kernel spreads the peers over them.
* Also asks for @p buffer_size on the send and receive buffers, which the OS
* may cap; a refused or capped buffer is logged, never fatal.
* @param port Port to bind; 0 lets the OS pick.
* @param addr Address and port to bind; port 0 lets the OS pick.
* @param buffer_size Bytes requested for SO_RCVBUF and SO_SNDBUF; 0 keeps the
* OS default.
* @param pool Pool used for the underlying apr_socket_t lifetime.
Expand All @@ -38,7 +40,7 @@
* @return APR_SUCCESS, APR_EAGAIN if the port is already bound, or another
* APR error code.
*/
apr_status_t h3_socket_open(apr_port_t port, apr_size_t buffer_size, apr_pool_t* pool, int reuse_port, int* out_fd);
apr_status_t h3_socket_open(apr_sockaddr_t* addr, apr_size_t buffer_size, apr_pool_t* pool, int reuse_port, int* out_fd);

/**
* Route QUIC packets by connection ID: attach an SK_REUSEPORT eBPF program
Expand Down
9 changes: 9 additions & 0 deletions test/modules/http3/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ def hold_udp_port(port: int) -> socket.socket:
s.bind(("::", port))
return s

def rewrite_test_conf(self, old: str, new: str):
"""Replace text in the installed configuration while the server
runs, for a graceful restart to pick up; install() stops it."""
with open(self._test_conf) as fd:
conf = fd.read()
assert old in conf, f"{old!r} not in {self._test_conf}"
with open(self._test_conf, 'w') as fd:
fd.write(conf.replace(old, new))

def alt_svc(self, domain: str = None, port: int = None,
scheme: str = "https"):
"""The Alt-Svc header of a TCP request to the vhost, or None."""
Expand Down
Loading
Loading