src/event/quic/ngx_event_quic.c - nginx

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Nginx, Inc.
  3. */


  4. #include <ngx_config.h>
  5. #include <ngx_core.h>
  6. #include <ngx_event.h>
  7. #include <ngx_sha1.h>
  8. #include <ngx_event_quic_connection.h>


  9. static ngx_quic_connection_t *ngx_quic_new_connection(ngx_connection_t *c,
  10.     ngx_quic_conf_t *conf, ngx_quic_header_t *pkt);
  11. static ngx_int_t ngx_quic_handle_stateless_reset(ngx_connection_t *c,
  12.     ngx_quic_header_t *pkt);
  13. static void ngx_quic_input_handler(ngx_event_t *rev);
  14. static void ngx_quic_close_handler(ngx_event_t *ev);

  15. static ngx_int_t ngx_quic_handle_datagram(ngx_connection_t *c, ngx_buf_t *b,
  16.     ngx_quic_conf_t *conf);
  17. static ngx_int_t ngx_quic_handle_packet(ngx_connection_t *c,
  18.     ngx_quic_conf_t *conf, ngx_quic_header_t *pkt);
  19. static ngx_int_t ngx_quic_handle_payload(ngx_connection_t *c,
  20.     ngx_quic_header_t *pkt);
  21. static ngx_int_t ngx_quic_check_csid(ngx_quic_connection_t *qc,
  22.     ngx_quic_header_t *pkt);
  23. static ngx_int_t ngx_quic_handle_frames(ngx_connection_t *c,
  24.     ngx_quic_header_t *pkt);

  25. static void ngx_quic_push_handler(ngx_event_t *ev);


  26. static ngx_core_module_t  ngx_quic_module_ctx = {
  27.     ngx_string("quic"),
  28.     NULL,
  29.     NULL
  30. };


  31. ngx_module_t  ngx_quic_module = {
  32.     NGX_MODULE_V1,
  33.     &ngx_quic_module_ctx,                  /* module context */
  34.     NULL,                                  /* module directives */
  35.     NGX_CORE_MODULE,                       /* module type */
  36.     NULL,                                  /* init master */
  37.     NULL,                                  /* init module */
  38.     NULL,                                  /* init process */
  39.     NULL,                                  /* init thread */
  40.     NULL,                                  /* exit thread */
  41.     NULL,                                  /* exit process */
  42.     NULL,                                  /* exit master */
  43.     NGX_MODULE_V1_PADDING
  44. };


  45. #if (NGX_DEBUG)

  46. void
  47. ngx_quic_connstate_dbg(ngx_connection_t *c)
  48. {
  49.     u_char                 *p, *last;
  50.     ngx_quic_connection_t  *qc;
  51.     u_char                  buf[NGX_MAX_ERROR_STR];

  52.     p = buf;
  53.     last = p + sizeof(buf);

  54.     qc = ngx_quic_get_connection(c);

  55.     p = ngx_slprintf(p, last, "state:");

  56.     if (qc) {

  57.         if (qc->error) {
  58.             p = ngx_slprintf(p, last, "%s", qc->error_app ? " app" : "");
  59.             p = ngx_slprintf(p, last, " error:%ui", qc->error);

  60.             if (qc->error_reason) {
  61.                 p = ngx_slprintf(p, last, " \"%s\"", qc->error_reason);
  62.             }
  63.         }

  64.         p = ngx_slprintf(p, last, "%s", qc->shutdown ? " shutdown" : "");
  65.         p = ngx_slprintf(p, last, "%s", qc->closing ? " closing" : "");
  66.         p = ngx_slprintf(p, last, "%s", qc->draining ? " draining" : "");
  67.         p = ngx_slprintf(p, last, "%s", qc->key_phase ? " kp" : "");

  68.     } else {
  69.         p = ngx_slprintf(p, last, " early");
  70.     }

  71.     if (c->read->timer_set) {
  72.         p = ngx_slprintf(p, last,
  73.                          qc && qc->send_timer_set ? " send:%M" : " read:%M",
  74.                          c->read->timer.key - ngx_current_msec);
  75.     }

  76.     if (qc) {

  77.         if (qc->push.timer_set) {
  78.             p = ngx_slprintf(p, last, " push:%M",
  79.                              qc->push.timer.key - ngx_current_msec);
  80.         }

  81.         if (qc->pto.timer_set) {
  82.             p = ngx_slprintf(p, last, " pto:%M",
  83.                              qc->pto.timer.key - ngx_current_msec);
  84.         }

  85.         if (qc->close.timer_set) {
  86.             p = ngx_slprintf(p, last, " close:%M",
  87.                              qc->close.timer.key - ngx_current_msec);
  88.         }
  89.     }

  90.     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
  91.                    "quic %*s", p - buf, buf);
  92. }

  93. #endif


  94. ngx_int_t
  95. ngx_quic_apply_transport_params(ngx_connection_t *c, ngx_quic_tp_t *ctp)
  96. {
  97.     ngx_str_t               scid;
  98.     ngx_quic_connection_t  *qc;

  99.     qc = ngx_quic_get_connection(c);

  100.     scid.data = qc->path->cid->id;
  101.     scid.len = qc->path->cid->len;

  102.     if (scid.len != ctp->initial_scid.len
  103.         || ngx_memcmp(scid.data, ctp->initial_scid.data, scid.len) != 0)
  104.     {
  105.         qc->error = NGX_QUIC_ERR_TRANSPORT_PARAMETER_ERROR;
  106.         qc->error_reason = "invalid initial_source_connection_id";

  107.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  108.                       "quic client initial_source_connection_id mismatch");
  109.         return NGX_ERROR;
  110.     }

  111.     if (ctp->max_udp_payload_size < NGX_QUIC_MIN_INITIAL_SIZE
  112.         || ctp->max_udp_payload_size > NGX_QUIC_MAX_UDP_PAYLOAD_SIZE)
  113.     {
  114.         qc->error = NGX_QUIC_ERR_TRANSPORT_PARAMETER_ERROR;
  115.         qc->error_reason = "invalid maximum packet size";

  116.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  117.                       "quic maximum packet size is invalid");
  118.         return NGX_ERROR;
  119.     }

  120.     if (ctp->active_connection_id_limit < 2) {
  121.         qc->error = NGX_QUIC_ERR_TRANSPORT_PARAMETER_ERROR;
  122.         qc->error_reason = "invalid active_connection_id_limit";

  123.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  124.                       "quic active_connection_id_limit is invalid");
  125.         return NGX_ERROR;
  126.     }

  127.     if (ctp->ack_delay_exponent > 20) {
  128.         qc->error = NGX_QUIC_ERR_TRANSPORT_PARAMETER_ERROR;
  129.         qc->error_reason = "invalid ack_delay_exponent";

  130.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  131.                       "quic ack_delay_exponent is invalid");
  132.         return NGX_ERROR;
  133.     }

  134.     if (ctp->max_ack_delay >= 16384) {
  135.         qc->error = NGX_QUIC_ERR_TRANSPORT_PARAMETER_ERROR;
  136.         qc->error_reason = "invalid max_ack_delay";

  137.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  138.                       "quic max_ack_delay is invalid");
  139.         return NGX_ERROR;
  140.     }

  141.     if (ctp->max_idle_timeout > 0
  142.         && ctp->max_idle_timeout < qc->tp.max_idle_timeout)
  143.     {
  144.         qc->tp.max_idle_timeout = ctp->max_idle_timeout;
  145.     }

  146.     qc->streams.server_max_streams_bidi = ctp->initial_max_streams_bidi;
  147.     qc->streams.server_max_streams_uni = ctp->initial_max_streams_uni;

  148.     ngx_memcpy(&qc->ctp, ctp, sizeof(ngx_quic_tp_t));

  149.     return NGX_OK;
  150. }


  151. void
  152. ngx_quic_run(ngx_connection_t *c, ngx_quic_conf_t *conf)
  153. {
  154.     ngx_int_t               rc;
  155.     ngx_quic_connection_t  *qc;

  156.     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic run");

  157.     rc = ngx_quic_handle_datagram(c, c->buffer, conf);
  158.     if (rc != NGX_OK) {
  159.         ngx_quic_close_connection(c, rc);
  160.         return;
  161.     }

  162.     /* quic connection is now created */
  163.     qc = ngx_quic_get_connection(c);

  164.     ngx_add_timer(c->read, qc->tp.max_idle_timeout);

  165.     if (!qc->streams.initialized) {
  166.         ngx_add_timer(&qc->close, qc->conf->handshake_timeout);
  167.     }

  168.     ngx_quic_connstate_dbg(c);

  169.     c->read->handler = ngx_quic_input_handler;

  170.     return;
  171. }


  172. static ngx_quic_connection_t *
  173. ngx_quic_new_connection(ngx_connection_t *c, ngx_quic_conf_t *conf,
  174.     ngx_quic_header_t *pkt)
  175. {
  176.     ngx_uint_t              i;
  177.     ngx_quic_tp_t          *ctp;
  178.     ngx_quic_connection_t  *qc;

  179.     qc = ngx_pcalloc(c->pool, sizeof(ngx_quic_connection_t));
  180.     if (qc == NULL) {
  181.         return NULL;
  182.     }

  183.     qc->keys = ngx_pcalloc(c->pool, sizeof(ngx_quic_keys_t));
  184.     if (qc->keys == NULL) {
  185.         return NULL;
  186.     }

  187.     qc->version = pkt->version;

  188.     ngx_rbtree_init(&qc->streams.tree, &qc->streams.sentinel,
  189.                     ngx_quic_rbtree_insert_stream);

  190.     for (i = 0; i < NGX_QUIC_SEND_CTX_LAST; i++) {
  191.         ngx_queue_init(&qc->send_ctx[i].frames);
  192.         ngx_queue_init(&qc->send_ctx[i].sending);
  193.         ngx_queue_init(&qc->send_ctx[i].sent);
  194.         qc->send_ctx[i].largest_pn = NGX_QUIC_UNSET_PN;
  195.         qc->send_ctx[i].largest_ack = NGX_QUIC_UNSET_PN;
  196.         qc->send_ctx[i].largest_range = NGX_QUIC_UNSET_PN;
  197.         qc->send_ctx[i].pending_ack = NGX_QUIC_UNSET_PN;
  198.     }

  199.     qc->send_ctx[0].level = NGX_QUIC_ENCRYPTION_INITIAL;
  200.     qc->send_ctx[1].level = NGX_QUIC_ENCRYPTION_HANDSHAKE;
  201.     qc->send_ctx[2].level = NGX_QUIC_ENCRYPTION_APPLICATION;

  202.     ngx_queue_init(&qc->free_frames);

  203.     ngx_quic_init_rtt(qc);

  204.     qc->pto.log = c->log;
  205.     qc->pto.data = c;
  206.     qc->pto.handler = ngx_quic_pto_handler;

  207.     qc->push.log = c->log;
  208.     qc->push.data = c;
  209.     qc->push.handler = ngx_quic_push_handler;

  210.     qc->close.log = c->log;
  211.     qc->close.data = c;
  212.     qc->close.handler = ngx_quic_close_handler;

  213.     qc->path_validation.log = c->log;
  214.     qc->path_validation.data = c;
  215.     qc->path_validation.handler = ngx_quic_path_handler;

  216.     qc->key_update.log = c->log;
  217.     qc->key_update.data = c;
  218.     qc->key_update.handler = ngx_quic_keys_update;

  219.     qc->conf = conf;

  220.     if (ngx_quic_init_transport_params(&qc->tp, conf) != NGX_OK) {
  221.         return NULL;
  222.     }

  223.     ctp = &qc->ctp;

  224.     /* defaults to be used before actual client parameters are received */
  225.     ctp->max_udp_payload_size = NGX_QUIC_MAX_UDP_PAYLOAD_SIZE;
  226.     ctp->ack_delay_exponent = NGX_QUIC_DEFAULT_ACK_DELAY_EXPONENT;
  227.     ctp->max_ack_delay = NGX_QUIC_DEFAULT_MAX_ACK_DELAY;
  228.     ctp->active_connection_id_limit = 2;

  229.     ngx_queue_init(&qc->streams.uninitialized);
  230.     ngx_queue_init(&qc->streams.free);

  231.     qc->streams.recv_max_data = qc->tp.initial_max_data;
  232.     qc->streams.recv_window = qc->streams.recv_max_data;

  233.     qc->streams.client_max_streams_uni = qc->tp.initial_max_streams_uni;
  234.     qc->streams.client_max_streams_bidi = qc->tp.initial_max_streams_bidi;

  235.     qc->congestion.window = ngx_min(10 * NGX_QUIC_MIN_INITIAL_SIZE,
  236.                                     ngx_max(2 * NGX_QUIC_MIN_INITIAL_SIZE,
  237.                                             14720));
  238.     qc->congestion.ssthresh = (size_t) -1;
  239.     qc->congestion.mtu = NGX_QUIC_MIN_INITIAL_SIZE;
  240.     qc->congestion.recovery_start = ngx_current_msec - 1;

  241.     qc->max_frames = (conf->max_concurrent_streams_uni
  242.                       + conf->max_concurrent_streams_bidi)
  243.                      * conf->stream_buffer_size / 2000;

  244.     if (pkt->validated && pkt->retried) {
  245.         qc->tp.retry_scid.len = pkt->dcid.len;
  246.         qc->tp.retry_scid.data = ngx_pstrdup(c->pool, &pkt->dcid);
  247.         if (qc->tp.retry_scid.data == NULL) {
  248.             return NULL;
  249.         }
  250.     }

  251.     if (ngx_quic_keys_set_initial_secret(qc->keys, &pkt->dcid, c->log)
  252.         != NGX_OK)
  253.     {
  254.         return NULL;
  255.     }

  256.     qc->validated = pkt->validated;

  257.     if (ngx_quic_open_sockets(c, qc, pkt) != NGX_OK) {
  258.         ngx_quic_keys_cleanup(qc->keys);
  259.         return NULL;
  260.     }

  261.     c->idle = 1;
  262.     ngx_reusable_connection(c, 1);

  263.     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
  264.                    "quic connection created");

  265.     return qc;
  266. }


  267. static ngx_int_t
  268. ngx_quic_handle_stateless_reset(ngx_connection_t *c, ngx_quic_header_t *pkt)
  269. {
  270.     u_char                 *tail, ch;
  271.     ngx_uint_t              i;
  272.     ngx_queue_t            *q;
  273.     ngx_quic_client_id_t   *cid;
  274.     ngx_quic_connection_t  *qc;

  275.     qc = ngx_quic_get_connection(c);

  276.     /* A stateless reset uses an entire UDP datagram */
  277.     if (!pkt->first) {
  278.         return NGX_DECLINED;
  279.     }

  280.     tail = pkt->raw->last - NGX_QUIC_SR_TOKEN_LEN;

  281.     for (q = ngx_queue_head(&qc->client_ids);
  282.          q != ngx_queue_sentinel(&qc->client_ids);
  283.          q = ngx_queue_next(q))
  284.     {
  285.         cid = ngx_queue_data(q, ngx_quic_client_id_t, queue);

  286.         if (cid->seqnum == 0 || !cid->used) {
  287.             /*
  288.              * No stateless reset token in initial connection id.
  289.              * Don't accept a token from an unused connection id.
  290.              */
  291.             continue;
  292.         }

  293.         /* constant time comparison */

  294.         for (ch = 0, i = 0; i < NGX_QUIC_SR_TOKEN_LEN; i++) {
  295.             ch |= tail[i] ^ cid->sr_token[i];
  296.         }

  297.         if (ch == 0) {
  298.             return NGX_OK;
  299.         }
  300.     }

  301.     return NGX_DECLINED;
  302. }


  303. static void
  304. ngx_quic_input_handler(ngx_event_t *rev)
  305. {
  306.     ngx_int_t               rc;
  307.     ngx_buf_t              *b;
  308.     ngx_connection_t       *c;
  309.     ngx_quic_connection_t  *qc;

  310.     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, rev->log, 0, "quic input handler");

  311.     c = rev->data;
  312.     qc = ngx_quic_get_connection(c);

  313.     c->log->action = "handling quic input";

  314.     if (rev->timedout) {
  315.         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  316.                       "quic client timed out");
  317.         ngx_quic_close_connection(c, NGX_DONE);
  318.         return;
  319.     }

  320.     if (c->close) {
  321.         c->close = 0;

  322.         if (!ngx_exiting || !qc->streams.initialized) {
  323.             qc->error = NGX_QUIC_ERR_NO_ERROR;
  324.             qc->error_reason = "graceful shutdown";
  325.             ngx_quic_close_connection(c, NGX_ERROR);
  326.             return;
  327.         }

  328.         if (!qc->closing && qc->conf->shutdown) {
  329.             qc->conf->shutdown(c);
  330.         }

  331.         return;
  332.     }

  333.     b = c->udp->buffer;
  334.     if (b == NULL) {
  335.         return;
  336.     }

  337.     rc = ngx_quic_handle_datagram(c, b, NULL);

  338.     if (rc == NGX_ERROR) {
  339.         ngx_quic_close_connection(c, NGX_ERROR);
  340.         return;
  341.     }

  342.     if (rc == NGX_DONE) {
  343.         return;
  344.     }

  345.     /* rc == NGX_OK */

  346.     qc->send_timer_set = 0;
  347.     ngx_add_timer(rev, qc->tp.max_idle_timeout);

  348.     ngx_quic_connstate_dbg(c);
  349. }


  350. void
  351. ngx_quic_close_connection(ngx_connection_t *c, ngx_int_t rc)
  352. {
  353.     ngx_uint_t              i;
  354.     ngx_pool_t             *pool;
  355.     ngx_quic_send_ctx_t    *ctx;
  356.     ngx_quic_connection_t  *qc;

  357.     qc = ngx_quic_get_connection(c);

  358.     if (qc == NULL) {
  359.         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
  360.                        "quic packet rejected rc:%i, cleanup connection", rc);
  361.         goto quic_done;
  362.     }

  363.     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
  364.                    "quic close %s rc:%i",
  365.                    qc->closing ? "resumed": "initiated", rc);

  366.     if (!qc->closing) {

  367.         /* drop packets from retransmit queues, no ack is expected */
  368.         for (i = 0; i < NGX_QUIC_SEND_CTX_LAST; i++) {
  369.             ngx_quic_free_frames(c, &qc->send_ctx[i].frames);
  370.             ngx_quic_free_frames(c, &qc->send_ctx[i].sent);
  371.         }

  372.         if (qc->close.timer_set) {
  373.             ngx_del_timer(&qc->close);
  374.         }

  375.         if (rc == NGX_DONE) {

  376.             /*
  377.              * RFC 9000, 10.1.  Idle Timeout
  378.              *
  379.              *  If a max_idle_timeout is specified by either endpoint in its
  380.              *  transport parameters (Section 18.2), the connection is silently
  381.              *  closed and its state is discarded when it remains idle
  382.              */

  383.             /* this case also handles some errors from ngx_quic_run() */

  384.             ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
  385.                            "quic close silent drain:%d timedout:%d",
  386.                            qc->draining, c->read->timedout);
  387.         } else {

  388.             /*
  389.              * RFC 9000, 10.2.  Immediate Close
  390.              *
  391.              *  An endpoint sends a CONNECTION_CLOSE frame (Section 19.19)
  392.              *  to terminate the connection immediately.
  393.              */

  394.             if (qc->error == 0 && rc == NGX_ERROR) {
  395.                 qc->error = NGX_QUIC_ERR_INTERNAL_ERROR;
  396.                 qc->error_app = 0;
  397.             }

  398.             ngx_log_debug5(NGX_LOG_DEBUG_EVENT, c->log, 0,
  399.                            "quic close immediate term:%d drain:%d "
  400.                            "%serror:%ui \"%s\"",
  401.                            rc == NGX_ERROR ? 1 : 0, qc->draining,
  402.                            qc->error_app ? "app " : "", qc->error,
  403.                            qc->error_reason ? qc->error_reason : "");

  404.             for (i = 0; i < NGX_QUIC_SEND_CTX_LAST; i++) {
  405.                 ctx = &qc->send_ctx[i];

  406.                 if (!ngx_quic_keys_available(qc->keys, ctx->level, 1)) {
  407.                     continue;
  408.                 }

  409.                 qc->error_level = ctx->level;
  410.                 (void) ngx_quic_send_cc(c);

  411.                 if (rc == NGX_OK) {
  412.                     ngx_add_timer(&qc->close, 3 * ngx_quic_pto(c, ctx));
  413.                 }
  414.             }
  415.         }

  416.         qc->closing = 1;
  417.     }

  418.     if (rc == NGX_ERROR && qc->close.timer_set) {
  419.         /* do not wait for timer in case of fatal error */
  420.         ngx_del_timer(&qc->close);
  421.     }

  422.     if (ngx_quic_close_streams(c, qc) == NGX_AGAIN) {
  423.         return;
  424.     }

  425.     if (qc->push.timer_set) {
  426.         ngx_del_timer(&qc->push);
  427.     }

  428.     if (qc->pto.timer_set) {
  429.         ngx_del_timer(&qc->pto);
  430.     }

  431.     if (qc->path_validation.timer_set) {
  432.         ngx_del_timer(&qc->path_validation);
  433.     }

  434.     if (qc->push.posted) {
  435.         ngx_delete_posted_event(&qc->push);
  436.     }

  437.     if (qc->key_update.posted) {
  438.         ngx_delete_posted_event(&qc->key_update);
  439.     }

  440.     if (qc->close.timer_set) {
  441.         return;
  442.     }

  443.     if (qc->close.posted) {
  444.         ngx_delete_posted_event(&qc->close);
  445.     }

  446.     ngx_quic_close_sockets(c);

  447.     ngx_quic_keys_cleanup(qc->keys);

  448.     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic close completed");

  449.     /* may be tested from SSL callback during SSL shutdown */
  450.     c->udp = NULL;

  451. quic_done:

  452.     if (c->ssl) {
  453.         (void) ngx_ssl_shutdown(c);
  454.     }

  455.     if (c->read->timer_set) {
  456.         ngx_del_timer(c->read);
  457.     }

  458. #if (NGX_STAT_STUB)
  459.     (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
  460. #endif

  461.     c->destroyed = 1;

  462.     pool = c->pool;

  463.     ngx_close_connection(c);

  464.     ngx_destroy_pool(pool);
  465. }


  466. void
  467. ngx_quic_finalize_connection(ngx_connection_t *c, ngx_uint_t err,
  468.     const char *reason)
  469. {
  470.     ngx_quic_connection_t  *qc;

  471.     qc = ngx_quic_get_connection(c);

  472.     if (qc->closing) {
  473.         return;
  474.     }

  475.     qc->error = err;
  476.     qc->error_reason = reason;
  477.     qc->error_app = 1;
  478.     qc->error_ftype = 0;

  479.     ngx_post_event(&qc->close, &ngx_posted_events);
  480. }


  481. void
  482. ngx_quic_shutdown_connection(ngx_connection_t *c, ngx_uint_t err,
  483.     const char *reason)
  484. {
  485.     ngx_quic_connection_t  *qc;

  486.     qc = ngx_quic_get_connection(c);
  487.     qc->shutdown = 1;
  488.     qc->shutdown_code = err;
  489.     qc->shutdown_reason = reason;

  490.     ngx_quic_shutdown_quic(c);
  491. }


  492. static void
  493. ngx_quic_close_handler(ngx_event_t *ev)
  494. {
  495.     ngx_connection_t  *c;

  496.     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0, "quic close handler");

  497.     c = ev->data;

  498.     ngx_quic_close_connection(c, NGX_OK);
  499. }


  500. static ngx_int_t
  501. ngx_quic_handle_datagram(ngx_connection_t *c, ngx_buf_t *b,
  502.     ngx_quic_conf_t *conf)
  503. {
  504.     size_t                  size;
  505.     u_char                 *p, *start;
  506.     ngx_int_t               rc;
  507.     ngx_uint_t              good;
  508.     ngx_quic_path_t        *path;
  509.     ngx_quic_header_t       pkt;
  510.     ngx_quic_connection_t  *qc;

  511.     good = 0;
  512.     path = NULL;

  513.     size = b->last - b->pos;

  514.     p = start = b->pos;

  515.     while (p < b->last) {

  516.         ngx_memzero(&pkt, sizeof(ngx_quic_header_t));
  517.         pkt.raw = b;
  518.         pkt.data = p;
  519.         pkt.len = b->last - p;
  520.         pkt.log = c->log;
  521.         pkt.first = (p == start) ? 1 : 0;
  522.         pkt.path = path;
  523.         pkt.flags = p[0];
  524.         pkt.raw->pos++;

  525.         rc = ngx_quic_handle_packet(c, conf, &pkt);

  526. #if (NGX_DEBUG)
  527.         if (pkt.parsed) {
  528.             ngx_log_debug5(NGX_LOG_DEBUG_EVENT, c->log, 0,
  529.                            "quic packet done rc:%i level:%s"
  530.                            " decr:%d pn:%L perr:%ui",
  531.                            rc, ngx_quic_level_name(pkt.level),
  532.                            pkt.decrypted, pkt.pn, pkt.error);
  533.         } else {
  534.             ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
  535.                            "quic packet done rc:%i parse failed", rc);
  536.         }
  537. #endif

  538.         if (rc == NGX_ERROR || rc == NGX_DONE) {
  539.             return rc;
  540.         }

  541.         if (rc == NGX_OK) {
  542.             good = 1;
  543.         }

  544.         path = pkt.path; /* preserve packet path from 1st packet */

  545.         /* NGX_OK || NGX_DECLINED */

  546.         /*
  547.          * we get NGX_DECLINED when there are no keys [yet] available
  548.          * to decrypt packet.
  549.          * Instead of queueing it, we ignore it and rely on the sender's
  550.          * retransmission:
  551.          *
  552.          * RFC 9000, 12.2.  Coalescing Packets
  553.          *
  554.          * For example, if decryption fails (because the keys are
  555.          * not available or for any other reason), the receiver MAY either
  556.          * discard or buffer the packet for later processing and MUST
  557.          * attempt to process the remaining packets.
  558.          *
  559.          * We also skip packets that don't match connection state
  560.          * or cannot be parsed properly.
  561.          */

  562.         /* b->pos is at header end, adjust by actual packet length */
  563.         b->pos = pkt.data + pkt.len;

  564.         p = b->pos;
  565.     }

  566.     if (!good) {
  567.         return NGX_DONE;
  568.     }

  569.     qc = ngx_quic_get_connection(c);

  570.     if (qc) {
  571.         qc->received += size;

  572.         if ((uint64_t) (c->sent + qc->received) / 8 >
  573.             (qc->streams.sent + qc->streams.recv_last) + 1048576)
  574.         {
  575.             ngx_log_error(NGX_LOG_INFO, c->log, 0, "quic flood detected");

  576.             qc->error = NGX_QUIC_ERR_NO_ERROR;
  577.             qc->error_reason = "QUIC flood detected";
  578.             return NGX_ERROR;
  579.         }
  580.     }

  581.     return NGX_OK;
  582. }


  583. static ngx_int_t
  584. ngx_quic_handle_packet(ngx_connection_t *c, ngx_quic_conf_t *conf,
  585.     ngx_quic_header_t *pkt)
  586. {
  587.     ngx_int_t               rc;
  588.     ngx_quic_socket_t      *qsock;
  589.     ngx_quic_connection_t  *qc;

  590.     c->log->action = "parsing quic packet";

  591.     rc = ngx_quic_parse_packet(pkt);

  592.     if (rc == NGX_ERROR) {
  593.         return NGX_DECLINED;
  594.     }

  595.     pkt->parsed = 1;

  596.     c->log->action = "handling quic packet";

  597.     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
  598.                    "quic packet rx dcid len:%uz %xV",
  599.                    pkt->dcid.len, &pkt->dcid);

  600. #if (NGX_DEBUG)
  601.     if (pkt->level != NGX_QUIC_ENCRYPTION_APPLICATION) {
  602.         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
  603.                        "quic packet rx scid len:%uz %xV",
  604.                        pkt->scid.len, &pkt->scid);
  605.     }

  606.     if (pkt->level == NGX_QUIC_ENCRYPTION_INITIAL) {
  607.         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
  608.                        "quic address validation token len:%uz %xV",
  609.                        pkt->token.len, &pkt->token);
  610.     }
  611. #endif

  612.     qc = ngx_quic_get_connection(c);

  613.     if (qc) {

  614.         if (rc == NGX_ABORT) {
  615.             ngx_log_error(NGX_LOG_INFO, c->log, 0,
  616.                           "quic unsupported version: 0x%xD", pkt->version);
  617.             return NGX_DECLINED;
  618.         }

  619.         if (pkt->level != NGX_QUIC_ENCRYPTION_APPLICATION) {

  620.             if (pkt->version != qc->version) {
  621.                 ngx_log_error(NGX_LOG_INFO, c->log, 0,
  622.                               "quic version mismatch: 0x%xD", pkt->version);
  623.                 return NGX_DECLINED;
  624.             }

  625.             if (pkt->first) {
  626.                 qsock = ngx_quic_get_socket(c);

  627.                 if (ngx_cmp_sockaddr(&qsock->sockaddr.sockaddr, qsock->socklen,
  628.                                      qc->path->sockaddr, qc->path->socklen, 1)
  629.                     != NGX_OK)
  630.                 {
  631.                     /* packet comes from unknown path, possibly migration */
  632.                     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
  633.                                    "quic too early migration attempt");
  634.                     return NGX_DONE;
  635.                 }
  636.             }

  637.             if (ngx_quic_check_csid(qc, pkt) != NGX_OK) {
  638.                 return NGX_DECLINED;
  639.             }

  640.         }

  641.         rc = ngx_quic_handle_payload(c, pkt);

  642.         if (rc == NGX_DECLINED
  643.             && pkt->level == NGX_QUIC_ENCRYPTION_APPLICATION)
  644.         {
  645.             if (ngx_quic_handle_stateless_reset(c, pkt) == NGX_OK) {
  646.                 ngx_log_error(NGX_LOG_INFO, c->log, 0,
  647.                               "quic stateless reset packet detected");

  648.                 qc->draining = 1;
  649.                 ngx_post_event(&qc->close, &ngx_posted_events);

  650.                 return NGX_OK;
  651.             }
  652.         }

  653.         return rc;
  654.     }

  655.     /* packet does not belong to a connection */

  656.     if (rc == NGX_ABORT) {
  657.         return ngx_quic_negotiate_version(c, pkt);
  658.     }

  659.     if (pkt->level == NGX_QUIC_ENCRYPTION_APPLICATION) {
  660.         return ngx_quic_send_stateless_reset(c, conf, pkt);
  661.     }

  662.     if (pkt->level != NGX_QUIC_ENCRYPTION_INITIAL) {
  663.         ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
  664.                        "quic expected initial, got handshake");
  665.         return NGX_ERROR;
  666.     }

  667.     c->log->action = "handling initial packet";

  668.     if (pkt->dcid.len < NGX_QUIC_CID_LEN_MIN) {
  669.         /* RFC 9000, 7.2.  Negotiating Connection IDs */
  670.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  671.                       "quic too short dcid in initial"
  672.                       " packet: len:%i", pkt->dcid.len);
  673.         return NGX_ERROR;
  674.     }

  675.     /* process retry and initialize connection IDs */

  676.     if (pkt->token.len) {

  677.         rc = ngx_quic_validate_token(c, conf->av_token_key, pkt);

  678.         if (rc == NGX_ERROR) {
  679.             /* internal error */
  680.             return NGX_ERROR;

  681.         } else if (rc == NGX_ABORT) {
  682.             /* token cannot be decrypted */
  683.             return ngx_quic_send_early_cc(c, pkt,
  684.                                           NGX_QUIC_ERR_INVALID_TOKEN,
  685.                                           "cannot decrypt token");
  686.         } else if (rc == NGX_DECLINED) {
  687.             /* token is invalid */

  688.             if (pkt->retried) {
  689.                 /* invalid address validation token */
  690.                 return ngx_quic_send_early_cc(c, pkt,
  691.                                           NGX_QUIC_ERR_INVALID_TOKEN,
  692.                                           "invalid address validation token");
  693.             } else if (conf->retry) {
  694.                 /* invalid NEW_TOKEN */
  695.                 return ngx_quic_send_retry(c, conf, pkt);
  696.             }
  697.         }

  698.         /* NGX_OK */

  699.     } else if (conf->retry) {
  700.         return ngx_quic_send_retry(c, conf, pkt);

  701.     } else {
  702.         pkt->odcid = pkt->dcid;
  703.     }

  704.     if (ngx_terminate || ngx_exiting) {
  705.         if (conf->retry) {
  706.             return ngx_quic_send_retry(c, conf, pkt);
  707.         }

  708.         return NGX_ERROR;
  709.     }

  710.     c->log->action = "creating quic connection";

  711.     qc = ngx_quic_new_connection(c, conf, pkt);
  712.     if (qc == NULL) {
  713.         return NGX_ERROR;
  714.     }

  715.     return ngx_quic_handle_payload(c, pkt);
  716. }


  717. static ngx_int_t
  718. ngx_quic_handle_payload(ngx_connection_t *c, ngx_quic_header_t *pkt)
  719. {
  720.     ngx_int_t               rc;
  721.     ngx_quic_send_ctx_t    *ctx;
  722.     ngx_quic_connection_t  *qc;
  723.     static u_char           buf[NGX_QUIC_MAX_UDP_PAYLOAD_SIZE];

  724.     qc = ngx_quic_get_connection(c);

  725.     qc->error = 0;
  726.     qc->error_reason = NULL;

  727.     c->log->action = "decrypting packet";

  728.     if (!ngx_quic_keys_available(qc->keys, pkt->level, 0)) {
  729.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  730.                       "quic no %s keys, ignoring packet",
  731.                       ngx_quic_level_name(pkt->level));
  732.         return NGX_DECLINED;
  733.     }

  734. #if (NGX_QUIC_QUICTLS_API)
  735.     /* QuicTLS provides app read keys before completing handshake */

  736.     if (pkt->level == NGX_QUIC_ENCRYPTION_APPLICATION && !c->ssl->handshaked) {
  737.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  738.                       "quic no %s keys ready, ignoring packet",
  739.                       ngx_quic_level_name(pkt->level));
  740.         return NGX_DECLINED;
  741.     }
  742. #endif

  743.     pkt->keys = qc->keys;
  744.     pkt->key_phase = qc->key_phase;
  745.     pkt->plaintext = buf;

  746.     ctx = ngx_quic_get_send_ctx(qc, pkt->level);

  747.     rc = ngx_quic_decrypt(pkt, &ctx->largest_pn);
  748.     if (rc != NGX_OK) {
  749.         qc->error = pkt->error;
  750.         qc->error_reason = "failed to decrypt packet";
  751.         return rc;
  752.     }

  753.     pkt->decrypted = 1;

  754.     c->log->action = "handling decrypted packet";

  755.     if (pkt->path == NULL) {
  756.         rc = ngx_quic_set_path(c, pkt);
  757.         if (rc != NGX_OK) {
  758.             return rc;
  759.         }
  760.     }

  761.     if (c->ssl == NULL) {
  762.         if (ngx_quic_init_connection(c) != NGX_OK) {
  763.             return NGX_ERROR;
  764.         }
  765.     }

  766.     if (pkt->level == NGX_QUIC_ENCRYPTION_HANDSHAKE) {
  767.         /*
  768.          * RFC 9001, 4.9.1.  Discarding Initial Keys
  769.          *
  770.          * The successful use of Handshake packets indicates
  771.          * that no more Initial packets need to be exchanged
  772.          */
  773.         ngx_quic_discard_ctx(c, NGX_QUIC_ENCRYPTION_INITIAL);

  774.         if (!qc->path->validated) {
  775.             qc->path->validated = 1;
  776.             ngx_quic_path_dbg(c, "in handshake", qc->path);
  777.             ngx_post_event(&qc->push, &ngx_posted_events);
  778.         }
  779.     }

  780.     if (pkt->level == NGX_QUIC_ENCRYPTION_APPLICATION) {
  781.         /*
  782.          * RFC 9001, 4.9.3.  Discarding 0-RTT Keys
  783.          *
  784.          * After receiving a 1-RTT packet, servers MUST discard
  785.          * 0-RTT keys within a short time
  786.          */
  787.         ngx_quic_keys_discard(qc->keys, NGX_QUIC_ENCRYPTION_EARLY_DATA);
  788.     }

  789.     if (qc->closing) {
  790.         /*
  791.          * RFC 9000, 10.2.  Immediate Close
  792.          *
  793.          * ... delayed or reordered packets are properly discarded.
  794.          *
  795.          *  In the closing state, an endpoint retains only enough information
  796.          *  to generate a packet containing a CONNECTION_CLOSE frame and to
  797.          *  identify packets as belonging to the connection.
  798.          */

  799.         qc->error_level = pkt->level;
  800.         qc->error = NGX_QUIC_ERR_NO_ERROR;
  801.         qc->error_reason = "connection is closing, packet discarded";
  802.         qc->error_ftype = 0;
  803.         qc->error_app = 0;

  804.         return ngx_quic_send_cc(c);
  805.     }

  806.     pkt->received = ngx_current_msec;

  807.     c->log->action = "handling payload";

  808.     if (pkt->level != NGX_QUIC_ENCRYPTION_APPLICATION) {
  809.         return ngx_quic_handle_frames(c, pkt);
  810.     }

  811.     if (!pkt->key_update) {
  812.         return ngx_quic_handle_frames(c, pkt);
  813.     }

  814.     /* switch keys and generate next on Key Phase change */

  815.     qc->key_phase ^= 1;
  816.     ngx_quic_keys_switch(c, qc->keys);

  817.     rc = ngx_quic_handle_frames(c, pkt);
  818.     if (rc != NGX_OK) {
  819.         return rc;
  820.     }

  821.     ngx_post_event(&qc->key_update, &ngx_posted_events);

  822.     return NGX_OK;
  823. }


  824. void
  825. ngx_quic_discard_ctx(ngx_connection_t *c, ngx_uint_t level)
  826. {
  827.     ngx_queue_t            *q;
  828.     ngx_quic_frame_t       *f;
  829.     ngx_quic_socket_t      *qsock;
  830.     ngx_quic_send_ctx_t    *ctx;
  831.     ngx_quic_connection_t  *qc;

  832.     qc = ngx_quic_get_connection(c);

  833.     if (!ngx_quic_keys_available(qc->keys, level, 0)
  834.         && !ngx_quic_keys_available(qc->keys, level, 1))
  835.     {
  836.         return;
  837.     }

  838.     ngx_quic_keys_discard(qc->keys, level);

  839.     qc->pto_count = 0;

  840.     ctx = ngx_quic_get_send_ctx(qc, level);

  841.     ngx_quic_free_buffer(c, &ctx->crypto);

  842.     while (!ngx_queue_empty(&ctx->sent)) {
  843.         q = ngx_queue_head(&ctx->sent);
  844.         ngx_queue_remove(q);

  845.         f = ngx_queue_data(q, ngx_quic_frame_t, queue);
  846.         ngx_quic_congestion_ack(c, f);
  847.         ngx_quic_free_frame(c, f);
  848.     }

  849.     while (!ngx_queue_empty(&ctx->frames)) {
  850.         q = ngx_queue_head(&ctx->frames);
  851.         ngx_queue_remove(q);

  852.         f = ngx_queue_data(q, ngx_quic_frame_t, queue);
  853.         ngx_quic_free_frame(c, f);
  854.     }

  855.     if (level == NGX_QUIC_ENCRYPTION_INITIAL) {
  856.         /* close temporary listener with initial dcid */
  857.         qsock = ngx_quic_find_socket(c, NGX_QUIC_UNSET_PN);
  858.         if (qsock) {
  859.             ngx_quic_close_socket(c, qsock);
  860.         }
  861.     }

  862.     ctx->send_ack = 0;

  863.     ngx_quic_set_lost_timer(c);
  864. }


  865. static ngx_int_t
  866. ngx_quic_check_csid(ngx_quic_connection_t *qc, ngx_quic_header_t *pkt)
  867. {
  868.     ngx_queue_t           *q;
  869.     ngx_quic_client_id_t  *cid;

  870.     for (q = ngx_queue_head(&qc->client_ids);
  871.          q != ngx_queue_sentinel(&qc->client_ids);
  872.          q = ngx_queue_next(q))
  873.     {
  874.         cid = ngx_queue_data(q, ngx_quic_client_id_t, queue);

  875.         if (pkt->scid.len == cid->len
  876.             && ngx_memcmp(pkt->scid.data, cid->id, cid->len) == 0)
  877.         {
  878.             return NGX_OK;
  879.         }
  880.     }

  881.     ngx_log_error(NGX_LOG_INFO, pkt->log, 0, "quic unexpected quic scid");
  882.     return NGX_ERROR;
  883. }


  884. static ngx_int_t
  885. ngx_quic_handle_frames(ngx_connection_t *c, ngx_quic_header_t *pkt)
  886. {
  887.     u_char                 *end, *p;
  888.     ssize_t                 len;
  889.     ngx_buf_t               buf;
  890.     ngx_uint_t              do_close, nonprobing;
  891.     ngx_chain_t             chain;
  892.     ngx_quic_frame_t        frame;
  893.     ngx_quic_connection_t  *qc;

  894.     qc = ngx_quic_get_connection(c);

  895.     p = pkt->payload.data;
  896.     end = p + pkt->payload.len;

  897.     do_close = 0;
  898.     nonprobing = 0;

  899.     while (p < end) {

  900.         c->log->action = "parsing frames";

  901.         ngx_memzero(&frame, sizeof(ngx_quic_frame_t));
  902.         ngx_memzero(&buf, sizeof(ngx_buf_t));
  903.         buf.temporary = 1;

  904.         chain.buf = &buf;
  905.         chain.next = NULL;
  906.         frame.data = &chain;

  907.         len = ngx_quic_parse_frame(pkt, p, end, &frame);

  908.         if (len < 0) {
  909.             qc->error = pkt->error;
  910.             return NGX_ERROR;
  911.         }

  912.         ngx_quic_log_frame(c->log, &frame, 0);

  913.         c->log->action = "handling frames";

  914.         p += len;

  915.         switch (frame.type) {
  916.         /* probing frames */
  917.         case NGX_QUIC_FT_PADDING:
  918.         case NGX_QUIC_FT_PATH_CHALLENGE:
  919.         case NGX_QUIC_FT_PATH_RESPONSE:
  920.         case NGX_QUIC_FT_NEW_CONNECTION_ID:
  921.             break;

  922.         /* non-probing frames */
  923.         default:
  924.             nonprobing = 1;
  925.             break;
  926.         }

  927.         switch (frame.type) {

  928.         case NGX_QUIC_FT_ACK:
  929.             if (ngx_quic_handle_ack_frame(c, pkt, &frame) != NGX_OK) {
  930.                 return NGX_ERROR;
  931.             }

  932.             continue;

  933.         case NGX_QUIC_FT_PADDING:
  934.             /* no action required */
  935.             continue;

  936.         case NGX_QUIC_FT_CONNECTION_CLOSE:
  937.         case NGX_QUIC_FT_CONNECTION_CLOSE_APP:
  938.             do_close = 1;
  939.             continue;
  940.         }

  941.         /* got there with ack-eliciting packet */
  942.         pkt->need_ack = 1;

  943.         switch (frame.type) {

  944.         case NGX_QUIC_FT_CRYPTO:

  945.             if (ngx_quic_handle_crypto_frame(c, pkt, &frame) != NGX_OK) {
  946.                 return NGX_ERROR;
  947.             }

  948.             break;

  949.         case NGX_QUIC_FT_PING:
  950.             break;

  951.         case NGX_QUIC_FT_STREAM:

  952.             if (ngx_quic_handle_stream_frame(c, pkt, &frame) != NGX_OK) {
  953.                 return NGX_ERROR;
  954.             }

  955.             break;

  956.         case NGX_QUIC_FT_MAX_DATA:

  957.             if (ngx_quic_handle_max_data_frame(c, &frame.u.max_data) != NGX_OK)
  958.             {
  959.                 return NGX_ERROR;
  960.             }

  961.             break;

  962.         case NGX_QUIC_FT_STREAMS_BLOCKED:
  963.         case NGX_QUIC_FT_STREAMS_BLOCKED2:

  964.             if (ngx_quic_handle_streams_blocked_frame(c, pkt,
  965.                                                       &frame.u.streams_blocked)
  966.                 != NGX_OK)
  967.             {
  968.                 return NGX_ERROR;
  969.             }

  970.             break;

  971.         case NGX_QUIC_FT_DATA_BLOCKED:

  972.             if (ngx_quic_handle_data_blocked_frame(c, pkt,
  973.                                                    &frame.u.data_blocked)
  974.                 != NGX_OK)
  975.             {
  976.                 return NGX_ERROR;
  977.             }

  978.             break;

  979.         case NGX_QUIC_FT_STREAM_DATA_BLOCKED:

  980.             if (ngx_quic_handle_stream_data_blocked_frame(c, pkt,
  981.                                                   &frame.u.stream_data_blocked)
  982.                 != NGX_OK)
  983.             {
  984.                 return NGX_ERROR;
  985.             }

  986.             break;

  987.         case NGX_QUIC_FT_MAX_STREAM_DATA:

  988.             if (ngx_quic_handle_max_stream_data_frame(c, pkt,
  989.                                                       &frame.u.max_stream_data)
  990.                 != NGX_OK)
  991.             {
  992.                 return NGX_ERROR;
  993.             }

  994.             break;

  995.         case NGX_QUIC_FT_RESET_STREAM:

  996.             if (ngx_quic_handle_reset_stream_frame(c, pkt,
  997.                                                    &frame.u.reset_stream)
  998.                 != NGX_OK)
  999.             {
  1000.                 return NGX_ERROR;
  1001.             }

  1002.             break;

  1003.         case NGX_QUIC_FT_STOP_SENDING:

  1004.             if (ngx_quic_handle_stop_sending_frame(c, pkt,
  1005.                                                    &frame.u.stop_sending)
  1006.                 != NGX_OK)
  1007.             {
  1008.                 return NGX_ERROR;
  1009.             }

  1010.             break;

  1011.         case NGX_QUIC_FT_MAX_STREAMS:
  1012.         case NGX_QUIC_FT_MAX_STREAMS2:

  1013.             if (ngx_quic_handle_max_streams_frame(c, pkt, &frame.u.max_streams)
  1014.                 != NGX_OK)
  1015.             {
  1016.                 return NGX_ERROR;
  1017.             }

  1018.             break;

  1019.         case NGX_QUIC_FT_PATH_CHALLENGE:

  1020.             if (ngx_quic_handle_path_challenge_frame(c, pkt,
  1021.                                                      &frame.u.path_challenge)
  1022.                 != NGX_OK)
  1023.             {
  1024.                 return NGX_ERROR;
  1025.             }

  1026.             break;

  1027.         case NGX_QUIC_FT_PATH_RESPONSE:

  1028.             if (ngx_quic_handle_path_response_frame(c, &frame.u.path_response)
  1029.                 != NGX_OK)
  1030.             {
  1031.                 return NGX_ERROR;
  1032.             }

  1033.             break;

  1034.         case NGX_QUIC_FT_NEW_CONNECTION_ID:

  1035.             if (ngx_quic_handle_new_connection_id_frame(c, &frame.u.ncid)
  1036.                 != NGX_OK)
  1037.             {
  1038.                 return NGX_ERROR;
  1039.             }

  1040.             break;

  1041.         case NGX_QUIC_FT_RETIRE_CONNECTION_ID:

  1042.             if (ngx_quic_handle_retire_connection_id_frame(c,
  1043.                                                            &frame.u.retire_cid)
  1044.                 != NGX_OK)
  1045.             {
  1046.                 return NGX_ERROR;
  1047.             }

  1048.             break;

  1049.         default:
  1050.             ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
  1051.                            "quic missing frame handler");
  1052.             return NGX_ERROR;
  1053.         }
  1054.     }

  1055.     if (p != end) {
  1056.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  1057.                       "quic trailing garbage in payload:%ui bytes", end - p);

  1058.         qc->error = NGX_QUIC_ERR_FRAME_ENCODING_ERROR;
  1059.         return NGX_ERROR;
  1060.     }

  1061.     if (do_close) {
  1062.         qc->draining = 1;
  1063.         ngx_post_event(&qc->close, &ngx_posted_events);
  1064.     }

  1065.     if (pkt->path != qc->path && nonprobing) {

  1066.         /*
  1067.          * RFC 9000, 9.2.  Initiating Connection Migration
  1068.          *
  1069.          * An endpoint can migrate a connection to a new local
  1070.          * address by sending packets containing non-probing frames
  1071.          * from that address.
  1072.          */
  1073.         if (ngx_quic_handle_migration(c, pkt) != NGX_OK) {
  1074.             return NGX_ERROR;
  1075.         }
  1076.     }

  1077.     if (ngx_quic_ack_packet(c, pkt) != NGX_OK) {
  1078.         return NGX_ERROR;
  1079.     }

  1080.     return NGX_OK;
  1081. }


  1082. static void
  1083. ngx_quic_push_handler(ngx_event_t *ev)
  1084. {
  1085.     ngx_connection_t  *c;

  1086.     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0, "quic push handler");

  1087.     c = ev->data;

  1088.     if (ngx_quic_output(c) != NGX_OK) {
  1089.         ngx_quic_close_connection(c, NGX_ERROR);
  1090.         return;
  1091.     }

  1092.     ngx_quic_connstate_dbg(c);
  1093. }


  1094. void
  1095. ngx_quic_shutdown_quic(ngx_connection_t *c)
  1096. {
  1097.     ngx_quic_connection_t  *qc;

  1098.     if (c->reusable) {
  1099.         qc = ngx_quic_get_connection(c);
  1100.         ngx_quic_finalize_connection(c, qc->shutdown_code, qc->shutdown_reason);
  1101.     }
  1102. }


  1103. void
  1104. ngx_quic_address_hash(struct sockaddr *sockaddr, socklen_t socklen,
  1105.     ngx_uint_t no_port, u_char *salt, size_t saltlen, u_char buf[20])
  1106. {
  1107.     size_t                len;
  1108.     u_char               *data;
  1109.     ngx_sha1_t            sha1;
  1110.     struct sockaddr_in   *sin;
  1111. #if (NGX_HAVE_INET6)
  1112.     struct sockaddr_in6  *sin6;
  1113. #endif

  1114.     len = (size_t) socklen;
  1115.     data = (u_char *) sockaddr;

  1116.     if (no_port) {
  1117.         switch (sockaddr->sa_family) {

  1118. #if (NGX_HAVE_INET6)
  1119.         case AF_INET6:
  1120.             sin6 = (struct sockaddr_in6 *) sockaddr;

  1121.             len = sizeof(struct in6_addr);
  1122.             data = sin6->sin6_addr.s6_addr;

  1123.             break;
  1124. #endif

  1125.         case AF_INET:
  1126.             sin = (struct sockaddr_in *) sockaddr;

  1127.             len = sizeof(in_addr_t);
  1128.             data = (u_char *) &sin->sin_addr;

  1129.             break;
  1130.         }
  1131.     }

  1132.     ngx_sha1_init(&sha1);
  1133.     ngx_sha1_update(&sha1, data, len);

  1134.     if (salt) {
  1135.         ngx_sha1_update(&sha1, salt, saltlen);
  1136.     }

  1137.     ngx_sha1_final(buf, &sha1);
  1138. }