QUIC: added "quic" listen parameter in Stream.

Also, introduced ngx_stream_quic_module.
This commit is contained in:
Roman Arutyunyan 2020-07-21 23:08:39 +03:00
parent 0c9a1fd9cc
commit 049af62328
11 changed files with 459 additions and 11 deletions

View File

@ -1065,6 +1065,20 @@ if [ $STREAM != NO ]; then
ngx_module_incs=
if [ $STREAM_QUIC = YES ]; then
USE_OPENSSL_QUIC=YES
have=NGX_STREAM_QUIC . auto/have
STREAM_SSL=YES
ngx_module_name=ngx_stream_quic_module
ngx_module_deps=src/stream/ngx_stream_quic_module.h
ngx_module_srcs=src/stream/ngx_stream_quic_module.c
ngx_module_libs=
ngx_module_link=$STREAM_QUIC
. auto/module
fi
if [ $STREAM_SSL = YES ]; then
USE_OPENSSL=YES
have=NGX_STREAM_SSL . auto/have

View File

@ -118,6 +118,7 @@ MAIL_SMTP=YES
STREAM=NO
STREAM_SSL=NO
STREAM_QUIC=NO
STREAM_REALIP=NO
STREAM_LIMIT_CONN=YES
STREAM_ACCESS=YES
@ -315,6 +316,7 @@ use the \"--with-mail_ssl_module\" option instead"
--with-stream) STREAM=YES ;;
--with-stream=dynamic) STREAM=DYNAMIC ;;
--with-stream_ssl_module) STREAM_SSL=YES ;;
--with-stream_quic_module) STREAM_QUIC=YES ;;
--with-stream_realip_module) STREAM_REALIP=YES ;;
--with-stream_geoip_module) STREAM_GEOIP=YES ;;
--with-stream_geoip_module=dynamic)
@ -534,6 +536,7 @@ cat << END
--with-stream enable TCP/UDP proxy module
--with-stream=dynamic enable dynamic TCP/UDP proxy module
--with-stream_ssl_module enable ngx_stream_ssl_module
--with-stream_quic_module enable ngx_stream_quic_module
--with-stream_realip_module enable ngx_stream_realip_module
--with-stream_geoip_module enable ngx_stream_geoip_module
--with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module

View File

@ -413,20 +413,20 @@ ngx_quic_add_handshake_data(ngx_ssl_conn_t *ssl_conn,
*/
#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation)
{
unsigned int len;
const unsigned char *data;
if (qc->conf->require_alpn) {
unsigned int len;
const unsigned char *data;
SSL_get0_alpn_selected(c->ssl->connection, &data, &len);
SSL_get0_alpn_selected(c->ssl->connection, &data, &len);
if (len == 0) {
qc->error = 0x100 + SSL_AD_NO_APPLICATION_PROTOCOL;
qc->error_reason = "unsupported protocol in ALPN extension";
if (len == 0) {
qc->error = 0x100 + SSL_AD_NO_APPLICATION_PROTOCOL;
qc->error_reason = "unsupported protocol in ALPN extension";
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"quic unsupported protocol in ALPN extension");
return 0;
}
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"quic unsupported protocol in ALPN extension");
return 0;
}
}
#endif
@ -3882,6 +3882,7 @@ ngx_quic_create_stream(ngx_connection_t *c, uint64_t id, size_t rcvbuf_size)
}
sn->c->qs = sn;
sn->c->type = SOCK_STREAM;
sn->c->pool = pool;
sn->c->ssl = c->ssl;
sn->c->sockaddr = c->sockaddr;

View File

@ -87,6 +87,7 @@ typedef struct {
typedef struct {
ngx_quic_tp_t tp;
ngx_flag_t retry;
ngx_flag_t require_alpn;
u_char token_key[32]; /* AES 256 */
} ngx_quic_conf_t;

View File

@ -241,6 +241,7 @@ ngx_http_quic_create_srv_conf(ngx_conf_t *cf)
conf->tp.active_connection_id_limit = NGX_CONF_UNSET_UINT;
conf->retry = NGX_CONF_UNSET;
conf->require_alpn = 1;
return conf;
}

View File

@ -571,6 +571,9 @@ ngx_stream_add_addrs(ngx_conf_t *cf, ngx_stream_port_t *stport,
addrs[i].conf.ctx = addr[i].opt.ctx;
#if (NGX_STREAM_SSL)
addrs[i].conf.ssl = addr[i].opt.ssl;
#endif
#if (NGX_STREAM_QUIC)
addrs[i].conf.quic = addr[i].opt.quic;
#endif
addrs[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;
addrs[i].conf.addr_text = addr[i].opt.addr_text;
@ -606,6 +609,9 @@ ngx_stream_add_addrs6(ngx_conf_t *cf, ngx_stream_port_t *stport,
addrs6[i].conf.ctx = addr[i].opt.ctx;
#if (NGX_STREAM_SSL)
addrs6[i].conf.ssl = addr[i].opt.ssl;
#endif
#if (NGX_STREAM_QUIC)
addrs6[i].conf.quic = addr[i].opt.quic;
#endif
addrs6[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;
addrs6[i].conf.addr_text = addr[i].opt.addr_text;

View File

@ -16,6 +16,10 @@
#include <ngx_stream_ssl_module.h>
#endif
#if (NGX_STREAM_QUIC)
#include <ngx_stream_quic_module.h>
#endif
typedef struct ngx_stream_session_s ngx_stream_session_t;
@ -51,6 +55,7 @@ typedef struct {
unsigned bind:1;
unsigned wildcard:1;
unsigned ssl:1;
unsigned quic:1;
#if (NGX_HAVE_INET6)
unsigned ipv6only:1;
#endif
@ -73,6 +78,7 @@ typedef struct {
ngx_stream_conf_ctx_t *ctx;
ngx_str_t addr_text;
unsigned ssl:1;
unsigned quic:1;
unsigned proxy_protocol:1;
} ngx_stream_addr_conf_t;

View File

@ -325,6 +325,9 @@ ngx_stream_core_content_phase(ngx_stream_session_t *s,
cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module);
if (c->type == SOCK_STREAM
#if (NGX_STREAM_QUIC)
&& c->qs == NULL
#endif
&& cscf->tcp_nodelay
&& ngx_tcp_nodelay(c) != NGX_OK)
{
@ -741,6 +744,29 @@ ngx_stream_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
#endif
}
if (ngx_strcmp(value[i].data, "quic") == 0) {
#if (NGX_STREAM_QUIC)
ngx_stream_ssl_conf_t *sslcf;
sslcf = ngx_stream_conf_get_module_srv_conf(cf,
ngx_stream_ssl_module);
sslcf->listen = 1;
sslcf->file = cf->conf_file->file.name.data;
sslcf->line = cf->conf_file->line;
ls->quic = 1;
ls->type = SOCK_DGRAM;
continue;
#else
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the \"quic\" parameter requires "
"ngx_stream_quic_module");
return NGX_CONF_ERROR;
#endif
}
if (ngx_strncmp(value[i].data, "so_keepalive=", 13) == 0) {
if (ngx_strcmp(&value[i].data[13], "on") == 0) {
@ -852,6 +878,12 @@ ngx_stream_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}
#endif
#if (NGX_STREAM_SSL && NGX_STREAM_QUIC)
if (ls->ssl && ls->quic) {
return "\"ssl\" parameter is incompatible with \"quic\"";
}
#endif
if (ls->so_keepalive) {
return "\"so_keepalive\" parameter is incompatible with \"udp\"";
}

View File

@ -115,6 +115,27 @@ ngx_stream_init_connection(ngx_connection_t *c)
}
}
#if (NGX_STREAM_QUIC)
if (addr_conf->quic) {
ngx_quic_conf_t *qcf;
ngx_stream_ssl_conf_t *scf;
if (c->qs == NULL) {
c->log->connection = c->number;
qcf = ngx_stream_get_module_srv_conf(addr_conf->ctx,
ngx_stream_quic_module);
scf = ngx_stream_get_module_srv_conf(addr_conf->ctx,
ngx_stream_ssl_module);
ngx_quic_run(c, &scf->ssl, qcf, ngx_stream_init_connection);
return;
}
}
#endif
s = ngx_pcalloc(c->pool, sizeof(ngx_stream_session_t));
if (s == NULL) {
ngx_stream_close_connection(c);

View File

@ -0,0 +1,343 @@
/*
* Copyright (C) Nginx, Inc.
* Copyright (C) Roman Arutyunyan
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
static ngx_int_t ngx_stream_variable_quic(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_stream_quic_add_variables(ngx_conf_t *cf);
static void *ngx_stream_quic_create_srv_conf(ngx_conf_t *cf);
static char *ngx_stream_quic_merge_srv_conf(ngx_conf_t *cf, void *parent,
void *child);
static char *ngx_stream_quic_max_ack_delay(ngx_conf_t *cf, void *post,
void *data);
static char *ngx_stream_quic_max_udp_payload_size(ngx_conf_t *cf, void *post,
void *data);
static ngx_conf_post_t ngx_stream_quic_max_ack_delay_post =
{ ngx_stream_quic_max_ack_delay };
static ngx_conf_post_t ngx_stream_quic_max_udp_payload_size_post =
{ ngx_stream_quic_max_udp_payload_size };
static ngx_conf_num_bounds_t ngx_stream_quic_ack_delay_exponent_bounds =
{ ngx_conf_check_num_bounds, 0, 20 };
static ngx_conf_num_bounds_t
ngx_stream_quic_active_connection_id_limit_bounds =
{ ngx_conf_check_num_bounds, 2, -1 };
static ngx_command_t ngx_stream_quic_commands[] = {
{ ngx_string("quic_max_idle_timeout"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.max_idle_timeout),
NULL },
{ ngx_string("quic_max_ack_delay"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.max_ack_delay),
&ngx_stream_quic_max_ack_delay_post },
{ ngx_string("quic_max_udp_payload_size"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.max_udp_payload_size),
&ngx_stream_quic_max_udp_payload_size_post },
{ ngx_string("quic_initial_max_data"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.initial_max_data),
NULL },
{ ngx_string("quic_initial_max_stream_data_bidi_local"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.initial_max_stream_data_bidi_local),
NULL },
{ ngx_string("quic_initial_max_stream_data_bidi_remote"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.initial_max_stream_data_bidi_remote),
NULL },
{ ngx_string("quic_initial_max_stream_data_uni"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.initial_max_stream_data_uni),
NULL },
{ ngx_string("quic_initial_max_streams_bidi"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.initial_max_streams_bidi),
NULL },
{ ngx_string("quic_initial_max_streams_uni"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.initial_max_streams_uni),
NULL },
{ ngx_string("quic_ack_delay_exponent"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.ack_delay_exponent),
&ngx_stream_quic_ack_delay_exponent_bounds },
{ ngx_string("quic_active_migration"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.disable_active_migration),
NULL },
{ ngx_string("quic_active_connection_id_limit"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, tp.active_connection_id_limit),
&ngx_stream_quic_active_connection_id_limit_bounds },
{ ngx_string("quic_retry"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_quic_conf_t, retry),
NULL },
ngx_null_command
};
static ngx_stream_module_t ngx_stream_quic_module_ctx = {
ngx_stream_quic_add_variables, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
ngx_stream_quic_create_srv_conf, /* create server configuration */
ngx_stream_quic_merge_srv_conf, /* merge server configuration */
};
ngx_module_t ngx_stream_quic_module = {
NGX_MODULE_V1,
&ngx_stream_quic_module_ctx, /* module context */
ngx_stream_quic_commands, /* module directives */
NGX_STREAM_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_stream_variable_t ngx_stream_quic_vars[] = {
{ ngx_string("quic"), NULL, ngx_stream_variable_quic, 0, 0, 0 },
ngx_stream_null_variable
};
static ngx_int_t
ngx_stream_variable_quic(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data)
{
if (s->connection->qs) {
v->len = 4;
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
v->data = (u_char *) "quic";
return NGX_OK;
}
v->not_found = 1;
return NGX_OK;
}
static ngx_int_t
ngx_stream_quic_add_variables(ngx_conf_t *cf)
{
ngx_stream_variable_t *var, *v;
for (v = ngx_stream_quic_vars; v->name.len; v++) {
var = ngx_stream_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static void *
ngx_stream_quic_create_srv_conf(ngx_conf_t *cf)
{
ngx_quic_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_quic_conf_t));
if (conf == NULL) {
return NULL;
}
/*
* set by ngx_pcalloc():
*
* conf->tp.original_dcid = { 0, NULL };
* conf->tp.initial_scid = { 0, NULL };
* conf->tp.retry_scid = { 0, NULL };
* conf->tp.stateless_reset_token = { 0 }
* conf->tp.preferred_address = NULL
* conf->require_alpn = 0;
*/
conf->tp.max_idle_timeout = NGX_CONF_UNSET_MSEC;
conf->tp.max_ack_delay = NGX_CONF_UNSET_MSEC;
conf->tp.max_udp_payload_size = NGX_CONF_UNSET_SIZE;
conf->tp.initial_max_data = NGX_CONF_UNSET_SIZE;
conf->tp.initial_max_stream_data_bidi_local = NGX_CONF_UNSET_SIZE;
conf->tp.initial_max_stream_data_bidi_remote = NGX_CONF_UNSET_SIZE;
conf->tp.initial_max_stream_data_uni = NGX_CONF_UNSET_SIZE;
conf->tp.initial_max_streams_bidi = NGX_CONF_UNSET_UINT;
conf->tp.initial_max_streams_uni = NGX_CONF_UNSET_UINT;
conf->tp.ack_delay_exponent = NGX_CONF_UNSET_UINT;
conf->tp.disable_active_migration = NGX_CONF_UNSET_UINT;
conf->tp.active_connection_id_limit = NGX_CONF_UNSET_UINT;
conf->retry = NGX_CONF_UNSET;
return conf;
}
static char *
ngx_stream_quic_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_quic_conf_t *prev = parent;
ngx_quic_conf_t *conf = child;
ngx_conf_merge_msec_value(conf->tp.max_idle_timeout,
prev->tp.max_idle_timeout, 60000);
ngx_conf_merge_msec_value(conf->tp.max_ack_delay,
prev->tp.max_ack_delay,
NGX_QUIC_DEFAULT_MAX_ACK_DELAY);
ngx_conf_merge_size_value(conf->tp.max_udp_payload_size,
prev->tp.max_udp_payload_size,
NGX_QUIC_MAX_UDP_PAYLOAD_SIZE);
ngx_conf_merge_size_value(conf->tp.initial_max_data,
prev->tp.initial_max_data,
16 * NGX_QUIC_STREAM_BUFSIZE);
ngx_conf_merge_size_value(conf->tp.initial_max_stream_data_bidi_local,
prev->tp.initial_max_stream_data_bidi_local,
NGX_QUIC_STREAM_BUFSIZE);
ngx_conf_merge_size_value(conf->tp.initial_max_stream_data_bidi_remote,
prev->tp.initial_max_stream_data_bidi_remote,
NGX_QUIC_STREAM_BUFSIZE);
ngx_conf_merge_size_value(conf->tp.initial_max_stream_data_uni,
prev->tp.initial_max_stream_data_uni,
NGX_QUIC_STREAM_BUFSIZE);
ngx_conf_merge_uint_value(conf->tp.initial_max_streams_bidi,
prev->tp.initial_max_streams_bidi, 16);
ngx_conf_merge_uint_value(conf->tp.initial_max_streams_uni,
prev->tp.initial_max_streams_uni, 16);
ngx_conf_merge_uint_value(conf->tp.ack_delay_exponent,
prev->tp.ack_delay_exponent,
NGX_QUIC_DEFAULT_ACK_DELAY_EXPONENT);
ngx_conf_merge_uint_value(conf->tp.disable_active_migration,
prev->tp.disable_active_migration, 1);
ngx_conf_merge_uint_value(conf->tp.active_connection_id_limit,
prev->tp.active_connection_id_limit, 2);
ngx_conf_merge_value(conf->retry, prev->retry, 0);
if (conf->retry) {
if (RAND_bytes(conf->token_key, sizeof(conf->token_key)) <= 0) {
return NGX_CONF_ERROR;
}
}
return NGX_CONF_OK;
}
static char *
ngx_stream_quic_max_ack_delay(ngx_conf_t *cf, void *post, void *data)
{
ngx_msec_t *sp = data;
if (*sp > 16384) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"\"quic_max_ack_delay\" must be less than 16384");
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static char *
ngx_stream_quic_max_udp_payload_size(ngx_conf_t *cf, void *post, void *data)
{
size_t *sp = data;
if (*sp < NGX_QUIC_MIN_INITIAL_SIZE
|| *sp > NGX_QUIC_MAX_UDP_PAYLOAD_SIZE)
{
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"\"quic_max_udp_payload_size\" must be between "
"%d and %d",
NGX_QUIC_MIN_INITIAL_SIZE,
NGX_QUIC_MAX_UDP_PAYLOAD_SIZE);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (C) Roman Arutyunyan
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGX_STREAM_QUIC_H_INCLUDED_
#define _NGX_STREAM_QUIC_H_INCLUDED_
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
extern ngx_module_t ngx_stream_quic_module;
#endif /* _NGX_STREAM_QUIC_H_INCLUDED_ */