vinyl-cache/bin/vinyld/http2/cache_http2_proto.c
0
/*-
1
 * Copyright (c) 2016-2019 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 */
30
31
#include "config.h"
32
33
#include "cache/cache_int.h"
34
35
#include <stdio.h>
36
#include <stdlib.h>
37
38
#include "cache/cache_transport.h"
39
#include "cache/cache_filter.h"
40
#include "http2/cache_http2.h"
41
#include "cache/cache_objhead.h"
42
#include "storage/storage.h"
43
44
#include "vend.h"
45
#include "vtcp.h"
46
#include "vtim.h"
47
48
#define H2_CUSTOM_ERRORS
49
#define H2EC1(U,v,g,r,d)        \
50
        const struct h2_error_s H2CE_##U[1] = {{"H2CE_" #U,d,v,0,1,g,r}};
51
#define H2EC2(U,v,g,r,d)        \
52
        const struct h2_error_s H2SE_##U[1] = {{"H2SE_" #U,d,v,1,0,g,r}};
53
#define H2EC3(U,v,g,r,d) H2EC1(U,v,g,r,d) H2EC2(U,v,g,r,d)
54
#define H2_ERROR(NAME, val, sc, goaway, reason, desc)   \
55
        H2EC##sc(NAME, val, goaway, reason, desc)
56
#include "tbl/h2_error.h"
57
#undef H2EC1
58
#undef H2EC2
59
#undef H2EC3
60
61
static const struct h2_error_s H2NN_ERROR[1] = {{
62
        "UNKNOWN_ERROR",
63
        "Unknown error number",
64
        0xffffffff,
65
        1,
66
        1,
67
        0,
68
        SC_RX_JUNK
69
}};
70
71
enum h2frame {
72
#define H2_FRAME(l,u,t,f,...)   H2F_##u = t,
73
#include "tbl/h2_frames.h"
74
};
75
76
static const char *
77 3512
h2_framename(enum h2frame h2f)
78
{
79
80 3512
        switch (h2f) {
81
#define H2_FRAME(l,u,t,f,...)   case H2F_##u: return (#u);
82
#include "tbl/h2_frames.h"
83
        default:
84
                return (NULL);
85
        }
86 3512
}
87
88
#define H2_FRAME_FLAGS(l,u,v)   const uint8_t H2FF_##u = v;
89
#include "tbl/h2_frames.h"
90
91
/**********************************************************************
92
 */
93
94
static const h2_error stream_errors[] = {
95
#define H2EC1(U,v,g,r,d)
96
#define H2EC2(U,v,g,r,d) [v] = H2SE_##U,
97
#define H2EC3(U,v,g,r,d) H2EC1(U,v,g,r,d) H2EC2(U,v,g,r,d)
98
#define H2_ERROR(NAME, val, sc, goaway, reason, desc)   \
99
        H2EC##sc(NAME, val, goaway, reason, desc)
100
#include "tbl/h2_error.h"
101
#undef H2EC1
102
#undef H2EC2
103
#undef H2EC3
104
};
105
106
#define NSTREAMERRORS vcountof(stream_errors)
107
108
static h2_error
109 60
h2_streamerror(uint32_t u)
110
{
111 60
        if (u < NSTREAMERRORS && stream_errors[u] != NULL)
112 52
                return (stream_errors[u]);
113
        else
114 8
                return (H2NN_ERROR);
115 60
}
116
117
/**********************************************************************
118
 */
119
120
static const h2_error conn_errors[] = {
121
#define H2EC1(U,v,g,r,d) [v] = H2CE_##U,
122
#define H2EC2(U,v,g,r,d)
123
#define H2EC3(U,v,g,r,d) H2EC1(U,v,g,r,d) H2EC2(U,v,g,r,d)
124
#define H2_ERROR(NAME, val, sc, goaway, reason, desc)   \
125
        H2EC##sc(NAME, val, goaway, reason, desc)
126
#include "tbl/h2_error.h"
127
#undef H2EC1
128
#undef H2EC2
129
#undef H2EC3
130
};
131
132
#define NCONNERRORS vcountof(conn_errors)
133
134
static h2_error
135 12
h2_connectionerror(uint32_t u)
136
{
137 12
        if (u < NCONNERRORS && conn_errors[u] != NULL)
138 8
                return (conn_errors[u]);
139
        else
140 4
                return (H2NN_ERROR);
141 12
}
142
143
/**********************************************************************/
144
145
struct h2_req *
146 1396
h2_new_req(struct h2_sess *h2, unsigned stream, struct req *req)
147
{
148
        struct h2_req *r2;
149
150 1396
        ASSERT_RXTHR(h2);
151 1396
        if (req == NULL)
152 1376
                req = Req_New(h2->sess, NULL);
153 1396
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
154
155 1396
        r2 = WS_Alloc(req->ws, sizeof *r2);
156 1396
        AN(r2);
157 1396
        INIT_OBJ(r2, H2_REQ_MAGIC);
158 1396
        r2->state = H2_S_IDLE;
159 1396
        r2->h2sess = h2;
160 1396
        r2->stream = stream;
161 1396
        r2->req = req;
162 1396
        if (stream)
163 776
                r2->counted = 1;
164 1396
        r2->r_window = h2->local_settings.initial_window_size;
165 1396
        r2->t_window = h2->remote_settings.initial_window_size;
166 1396
        req->transport_priv = r2;
167 1396
        Lck_Lock(&h2->sess->mtx);
168 1396
        if (stream)
169 776
                h2->open_streams++;
170 1396
        VTAILQ_INSERT_TAIL(&h2->streams, r2, list);
171 1396
        Lck_Unlock(&h2->sess->mtx);
172 1396
        h2->refcnt++;
173 1396
        return (r2);
174
}
175
176
void
177 1388
h2_del_req(struct worker *wrk, struct h2_req *r2)
178
{
179
        struct h2_sess *h2;
180
        struct sess *sp;
181
        struct stv_buffer *stvbuf;
182
183 1388
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
184 1388
        AZ(r2->scheduled);
185 1388
        h2 = r2->h2sess;
186 1388
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
187 1388
        ASSERT_RXTHR(h2);
188 1388
        sp = h2->sess;
189 1388
        Lck_Lock(&sp->mtx);
190 1388
        assert(h2->refcnt > 0);
191 1388
        --h2->refcnt;
192
        /* XXX: PRIORITY reshuffle */
193 1388
        VTAILQ_REMOVE(&h2->streams, r2, list);
194 1388
        if (r2->req == h2->new_req)
195 164
                h2->new_req = NULL;
196 1388
        Lck_Unlock(&sp->mtx);
197
198 1388
        assert(!WS_IsReserved(r2->req->ws));
199 1388
        AZ(r2->req->ws->r);
200
201 1388
        CHECK_OBJ_ORNULL(r2->rxbuf, H2_RXBUF_MAGIC);
202 1388
        if (r2->rxbuf) {
203 16
                stvbuf = r2->rxbuf->stvbuf;
204 16
                r2->rxbuf = NULL;
205 16
                STV_FreeBuf(wrk, &stvbuf);
206 16
                AZ(stvbuf);
207 16
        }
208
209 1388
        Req_Cleanup(sp, wrk, r2->req);
210 1388
        if (FEATURE(FEATURE_BUSY_STATS_RATE))
211 0
                WRK_AddStat(wrk);
212 1388
        Req_Release(r2->req);
213 1388
}
214
215
void
216 700
h2_kill_req(struct worker *wrk, struct h2_sess *h2,
217
    struct h2_req *r2, h2_error h2e)
218
{
219
220 700
        ASSERT_RXTHR(h2);
221 700
        AN(h2e);
222 700
        Lck_Lock(&h2->sess->mtx);
223 1400
        VSLb(h2->vsl, SLT_Debug, "KILL st=%u state=%d sched=%d",
224 700
            r2->stream, r2->state, r2->scheduled);
225 700
        if (r2->counted) {
226 204
                assert(h2->open_streams > 0);
227 204
                h2->open_streams--;
228 204
                r2->counted = 0;
229 204
        }
230 700
        if (r2->error == NULL)
231 60
                r2->error = h2e;
232 700
        if (r2->scheduled) {
233 248
                if (r2->cond != NULL)
234 23
                        PTOK(pthread_cond_signal(r2->cond));
235 248
                r2 = NULL;
236 248
                Lck_Unlock(&h2->sess->mtx);
237 248
        } else {
238 452
                Lck_Unlock(&h2->sess->mtx);
239 452
                if (r2->state == H2_S_OPEN && h2->new_req == r2->req)
240 20
                        (void)h2h_decode_hdr_fini(h2);
241
        }
242 700
        if (r2 != NULL)
243 452
                h2_del_req(wrk, r2);
244 700
}
245
246
/**********************************************************************/
247
248
static void
249 3572
h2_vsl_frame(const struct h2_sess *h2, const void *ptr, size_t len)
250
{
251
        const uint8_t *b;
252
        struct vsb *vsb;
253
        const char *p;
254
        unsigned u;
255
256 3572
        if (VSL_tag_is_masked(SLT_H2RxHdr) &&
257 60
            VSL_tag_is_masked(SLT_H2RxBody))
258 60
                return;
259
260 3512
        AN(ptr);
261 3512
        assert(len >= 9);
262 3512
        b = ptr;
263
264 3512
        vsb = VSB_new_auto();
265 3512
        AN(vsb);
266 3512
        p = h2_framename((enum h2frame)b[3]);
267 3512
        if (p != NULL)
268 3508
                VSB_cat(vsb, p);
269
        else
270 4
                VSB_quote(vsb, b + 3, 1, VSB_QUOTE_HEX);
271
272 3512
        u = vbe32dec(b) >> 8;
273 3512
        VSB_printf(vsb, "[%u] ", u);
274 3512
        VSB_quote(vsb, b + 4, 1, VSB_QUOTE_HEX);
275 3512
        VSB_putc(vsb, ' ');
276 3512
        VSB_quote(vsb, b + 5, 4, VSB_QUOTE_HEX);
277 3512
        if (u > 0) {
278 2352
                VSB_putc(vsb, ' ');
279 2352
                VSB_quote(vsb, b + 9, len - 9, VSB_QUOTE_HEX);
280 2352
        }
281 3512
        AZ(VSB_finish(vsb));
282 3512
        Lck_Lock(&h2->sess->mtx);
283 3512
        VSLb_bin(h2->vsl, SLT_H2RxHdr, 9, b);
284 3512
        if (len > 9)
285 2352
                VSLb_bin(h2->vsl, SLT_H2RxBody, len - 9, b + 9);
286
287 3512
        VSLb(h2->vsl, SLT_Debug, "H2RXF %s", VSB_data(vsb));
288 3512
        Lck_Unlock(&h2->sess->mtx);
289 3512
        VSB_destroy(&vsb);
290 3572
}
291
292
293
/**********************************************************************
294
 */
295
296
static h2_error v_matchproto_(h2_rxframe_f)
297 24
h2_rx_ping(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
298
{
299
300 24
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
301 24
        ASSERT_RXTHR(h2);
302 24
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
303 24
        assert(r2 == h2->req0);
304
305 24
        if (h2->rxf_len != 8) {                         // rfc7540,l,2364,2366
306 4
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx ping with (len != 8)");
307 4
                return (H2CE_FRAME_SIZE_ERROR);
308
        }
309 20
        AZ(h2->rxf_stream);                             // rfc7540,l,2359,2362
310 20
        if (h2->rxf_flags != 0) {                       // We never send pings
311 4
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx ping ack");
312 4
                return (H2SE_PROTOCOL_ERROR);
313
        }
314 16
        H2_Send_Get(wrk, h2, r2);
315 32
        H2_Send_Frame(wrk, h2,
316 16
            H2_F_PING, H2FF_PING_ACK, 8, 0, h2->rxf_data);
317 16
        H2_Send_Rel(h2, r2);
318 16
        return (0);
319 24
}
320
321
/**********************************************************************
322
 */
323
324
static h2_error v_matchproto_(h2_rxframe_f)
325 8
h2_rx_push_promise(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
326
{
327
328 8
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
329 8
        ASSERT_RXTHR(h2);
330 8
        CHECK_OBJ_ORNULL(r2, H2_REQ_MAGIC);
331
332
        // rfc7540,l,2262,2267
333 8
        H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx push promise");
334 8
        return (H2CE_PROTOCOL_ERROR);
335
}
336
337
/**********************************************************************
338
 */
339
340
int
341 306
h2_rapid_reset_check(struct worker *wrk, struct h2_sess *h2,
342
    const struct h2_req *r2)
343
{
344
        vtim_real now;
345
346 306
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
347 306
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
348 306
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
349
350 306
        if (h2->rapid_reset_limit == 0)
351 0
                return (0);
352
353 306
        now = VTIM_real();
354 306
        CHECK_OBJ_NOTNULL(r2->req, REQ_MAGIC);
355 306
        AN(r2->req->t_first);
356 306
        if (now - r2->req->t_first > h2->rapid_reset)
357 26
                return (0);
358
359 280
        return (1);
360 306
}
361
362
h2_error
363 280
h2_rapid_reset_charge(struct worker *wrk, struct h2_sess *h2,
364
    const struct h2_req *r2)
365
{
366
        vtim_real now;
367
        vtim_dur d;
368 280
        h2_error h2e = NULL;
369
370 280
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
371 280
        AN(H2_SEND_HELD(h2, r2));
372 280
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
373
374 280
        now = VTIM_real();
375
376 280
        d = now - h2->last_rst;
377 560
        h2->rst_budget += h2->rapid_reset_limit * d /
378 280
            h2->rapid_reset_period;
379 280
        h2->rst_budget = vmin_t(double, h2->rst_budget,
380
            h2->rapid_reset_limit);
381 280
        h2->last_rst = now;
382
383 280
        h2->rst_budget -= 1.0;
384
385 280
        if (h2->rst_budget < 0) {
386 12
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: Hit RST limit. Closing session.");
387 12
                h2e = H2CE_RAPID_RESET;
388 12
                H2_Send_GOAWAY(wrk, h2, r2, h2e);
389 12
        }
390
391 280
        return (h2e);
392
}
393
394
static h2_error v_matchproto_(h2_rxframe_f)
395 76
h2_rx_rst_stream(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
396
{
397 76
        h2_error h2e = NULL;
398
399 76
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
400 76
        ASSERT_RXTHR(h2);
401 76
        CHECK_OBJ_ORNULL(r2, H2_REQ_MAGIC);
402
403 76
        if (h2->rxf_len != 4) {                 // rfc7540,l,2003,2004
404 4
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx rst with (len != 4)");
405 4
                return (H2CE_FRAME_SIZE_ERROR);
406
        }
407 72
        if (r2 == NULL)
408 12
                return (0);
409 60
        if (h2_rapid_reset_check(wrk, h2, r2)) {
410 60
                H2_Send_Get(wrk, h2, h2->req0);
411 60
                h2e = h2_rapid_reset_charge(wrk, h2, h2->req0);
412 60
                H2_Send_Rel(h2, h2->req0);
413 60
        }
414 60
        h2_kill_req(wrk, h2, r2, h2_streamerror(vbe32dec(h2->rxf_data)));
415 60
        return (h2e);
416 76
}
417
418
/**********************************************************************
419
 */
420
421
static h2_error v_matchproto_(h2_rxframe_f)
422 12
h2_rx_goaway(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
423
{
424
425 12
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
426 12
        ASSERT_RXTHR(h2);
427 12
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
428 12
        assert(r2 == h2->req0);
429
430 12
        h2->goaway = 1;
431 12
        h2->goaway_last_stream = vbe32dec(h2->rxf_data);
432 12
        h2->error = h2_connectionerror(vbe32dec(h2->rxf_data + 4));
433 12
        H2S_Lock_VSLb(h2, SLT_Debug, "GOAWAY %s", h2->error->name);
434 12
        return (h2->error);
435
}
436
437
static void
438 584
h2_tx_goaway(struct worker *wrk, struct h2_sess *h2, h2_error h2e)
439
{
440 584
        ASSERT_RXTHR(h2);
441 584
        AN(h2e);
442
443 584
        if (h2->goaway || !h2e->send_goaway)
444 8
                return;
445
446 576
        H2_Send_Get(wrk, h2, h2->req0);
447 576
        H2_Send_GOAWAY(wrk, h2, h2->req0, h2e);
448 576
        H2_Send_Rel(h2, h2->req0);
449 584
}
450
451
/**********************************************************************
452
 */
453
454
static h2_error v_matchproto_(h2_rxframe_f)
455 104
h2_rx_window_update(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
456
{
457
        uint32_t wu;
458
459 104
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
460 104
        ASSERT_RXTHR(h2);
461 104
        CHECK_OBJ_ORNULL(r2, H2_REQ_MAGIC);
462
463 104
        if (h2->rxf_len != 4) {
464 4
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx winup with (len != 4)");
465 4
                return (H2CE_FRAME_SIZE_ERROR);
466
        }
467 100
        wu = vbe32dec(h2->rxf_data) & ~(1LU<<31);
468 100
        if (wu == 0)
469 20
                return (H2SE_PROTOCOL_ERROR);
470 80
        if (r2 == NULL)
471 4
                return (0);
472 76
        Lck_Lock(&h2->sess->mtx);
473 76
        r2->t_window += wu;
474 76
        if (r2 == h2->req0)
475 32
                PTOK(pthread_cond_broadcast(h2->winupd_cond));
476 44
        else if (r2->cond != NULL)
477 32
                PTOK(pthread_cond_signal(r2->cond));
478 76
        Lck_Unlock(&h2->sess->mtx);
479 76
        if (r2->t_window >= (1LL << 31))
480 8
                return (H2SE_FLOW_CONTROL_ERROR);
481 68
        return (0);
482 104
}
483
484
/**********************************************************************
485
 * Incoming PRIORITY, possibly an ACK of one we sent.
486
 *
487
 * deprecated, rfc9113,l,1103,1104
488
 */
489
490
static h2_error v_matchproto_(h2_rxframe_f)
491 36
h2_rx_priority(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
492
{
493
494 36
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
495 36
        ASSERT_RXTHR(h2);
496 36
        CHECK_OBJ_ORNULL(r2, H2_REQ_MAGIC);
497 36
        return (0);
498
}
499
500
/**********************************************************************
501
 * Incoming SETTINGS, possibly an ACK of one we sent.
502
 */
503
504
#define H2_SETTING(U,l, ...)                                    \
505
static void v_matchproto_(h2_setsetting_f)                      \
506
h2_setting_##l(struct h2_settings* s, uint32_t v)               \
507
{                                                               \
508
        s -> l = v;                                             \
509
}
510
#include <tbl/h2_settings.h>
511
512
#define H2_SETTING(U, l, ...)                                   \
513
const struct h2_setting_s H2_SET_##U[1] = {{                    \
514
        #l,                                                     \
515
        h2_setting_##l,                                         \
516
        __VA_ARGS__                                             \
517
}};
518
#include <tbl/h2_settings.h>
519
520
static const struct h2_setting_s * const h2_setting_tbl[] = {
521
#define H2_SETTING(U,l,v, ...) [v] = H2_SET_##U,
522
#include <tbl/h2_settings.h>
523
};
524
525
#define H2_SETTING_TBL_LEN vcountof(h2_setting_tbl)
526
527
static void
528 36
h2_win_adjust(const struct h2_sess *h2, uint32_t oldval, uint32_t newval)
529
{
530
        struct h2_req *r2;
531
532 36
        Lck_AssertHeld(&h2->sess->mtx);
533
        // rfc7540,l,2668,2674
534 72
        VTAILQ_FOREACH(r2, &h2->streams, list) {
535 36
                CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
536 36
                if (r2 == h2->req0)
537 36
                        continue; // rfc7540,l,2699,2699
538 0
                switch (r2->state) {
539
                case H2_S_IDLE:
540
                case H2_S_OPEN:
541
                case H2_S_CLOS_REM:
542
                        /*
543
                         * We allow a window to go negative, as per
544
                         * rfc7540,l,2676,2680
545
                         */
546 0
                        r2->t_window += (int64_t)newval - oldval;
547 0
                        break;
548
                default:
549 0
                        break;
550
                }
551 0
        }
552 36
}
553
554
h2_error
555 72
h2_set_setting(struct h2_sess *h2, const uint8_t *d)
556
{
557
        const struct h2_setting_s *s;
558
        uint16_t x;
559
        uint32_t y;
560
561 72
        x = vbe16dec(d);
562 72
        y = vbe32dec(d + 2);
563 72
        if (x >= H2_SETTING_TBL_LEN || h2_setting_tbl[x] == NULL) {
564
                // rfc7540,l,2181,2182
565 8
                H2S_Lock_VSLb(h2, SLT_Debug,
566 4
                    "H2SETTING unknown setting 0x%04x=%08x (ignored)", x, y);
567 4
                return (0);
568
        }
569 68
        s = h2_setting_tbl[x];
570 68
        AN(s);
571 68
        if (y < s->minval || y > s->maxval) {
572 24
                H2S_Lock_VSLb(h2, SLT_Debug, "H2SETTING invalid %s=0x%08x",
573 12
                    s->name, y);
574 12
                AN(s->range_error);
575 12
                if (!DO_DEBUG(DBG_H2_NOCHECK))
576 4
                        return (s->range_error);
577 8
        }
578 64
        Lck_Lock(&h2->sess->mtx);
579 64
        if (s == H2_SET_INITIAL_WINDOW_SIZE)
580 36
                h2_win_adjust(h2, h2->remote_settings.initial_window_size, y);
581 64
        VSLb(h2->vsl, SLT_Debug, "H2SETTING %s=0x%08x", s->name, y);
582 64
        Lck_Unlock(&h2->sess->mtx);
583 64
        AN(s->setfunc);
584 64
        s->setfunc(&h2->remote_settings, y);
585 64
        return (0);
586 72
}
587
588
static h2_error v_matchproto_(h2_rxframe_f)
589 1184
h2_rx_settings(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
590
{
591
        const uint8_t *p;
592
        unsigned l;
593 1184
        h2_error retval = 0;
594
595 1184
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
596 1184
        ASSERT_RXTHR(h2);
597 1184
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
598 1184
        assert(r2 == h2->req0);
599 1184
        AZ(h2->rxf_stream);
600
601 1184
        if (h2->rxf_flags == H2FF_SETTINGS_ACK) {
602 584
                if (h2->rxf_len > 0) {                  // rfc7540,l,2047,2049
603 4
                        H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx settings ack with "
604
                            "(len > 0)");
605 4
                        return (H2CE_FRAME_SIZE_ERROR);
606
                }
607 580
                return (0);
608
        } else {
609 600
                if (h2->rxf_len % 6) {                  // rfc7540,l,2062,2064
610 4
                        H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx settings with "
611
                            "((len %% 6) != 0)");
612 4
                        return (H2CE_PROTOCOL_ERROR);
613
                }
614 596
                p = h2->rxf_data;
615 624
                for (l = h2->rxf_len; l >= 6; l -= 6, p += 6) {
616 32
                        retval = h2_set_setting(h2, p);
617 32
                        if (retval)
618 4
                                return (retval);
619 28
                }
620 592
                H2_Send_Get(wrk, h2, r2);
621 592
                H2_Send_Frame(wrk, h2,
622
                    H2_F_SETTINGS, H2FF_SETTINGS_ACK, 0, 0, NULL);
623 592
                H2_Send_Rel(h2, r2);
624
        }
625 592
        return (0);
626 1184
}
627
628
/**********************************************************************
629
 * Incoming HEADERS, this is where the party's at...
630
 */
631
632
void v_matchproto_(task_func_t)
633 604
h2_do_req(struct worker *wrk, void *priv)
634
{
635
        struct req *req;
636
        struct h2_req *r2;
637
        struct h2_sess *h2;
638
639 604
        CAST_OBJ_NOTNULL(req, priv, REQ_MAGIC);
640 604
        CAST_OBJ_NOTNULL(r2, req->transport_priv, H2_REQ_MAGIC);
641 604
        THR_SetRequest(req);
642 604
        CNT_Embark(wrk, req);
643
644 604
        if (CNT_Request(req) != REQ_FSM_DISEMBARK) {
645 552
                wrk->stats->client_req++;
646 552
                assert(!WS_IsReserved(req->ws));
647 552
                AZ(req->top->vcl0);
648 552
                h2 = r2->h2sess;
649 552
                CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
650 552
                Lck_Lock(&h2->sess->mtx);
651 552
                r2->scheduled = 0;
652 552
                r2->state = H2_S_CLOSED;
653 552
                r2->h2sess->do_sweep = 1;
654 552
                Lck_Unlock(&h2->sess->mtx);
655 552
        }
656 604
        THR_SetRequest(NULL);
657 604
}
658
659
static h2_error
660 592
h2_end_headers(struct worker *wrk, struct h2_sess *h2,
661
    struct req *req, struct h2_req *r2)
662
{
663
        h2_error h2e;
664
        ssize_t cl;
665
666 592
        ASSERT_RXTHR(h2);
667 592
        assert(r2->state == H2_S_OPEN);
668 592
        h2e = h2h_decode_hdr_fini(h2);
669 592
        h2->new_req = NULL;
670 592
        if (h2e != NULL) {
671 20
                H2S_Lock_VSLb(h2, SLT_Debug, "HPACK/FINI %s", h2e->name);
672 20
                assert(!WS_IsReserved(r2->req->ws));
673 20
                h2_del_req(wrk, r2);
674 20
                return (h2e);
675
        }
676 572
        req->t_req = VTIM_real();
677 572
        VSLb_ts_req(req, "Req", req->t_req);
678
679
        // XXX: Smarter to do this already at HPACK time into tail end of
680
        // XXX: WS, then copy back once all headers received.
681
        // XXX: Have I mentioned H/2 Is hodge-podge ?
682 572
        http_CollectHdrSep(req->http, H_Cookie, "; ");  // rfc7540,l,3114,3120
683
684 572
        cl = http_GetContentLength(req->http);
685 572
        assert(cl >= -2);
686 572
        if (cl == -2) {
687 0
                H2S_Lock_VSLb(h2, SLT_Debug, "Non-parseable Content-Length");
688 0
                return (H2SE_PROTOCOL_ERROR);
689
        }
690
691 572
        if (req->req_body_status == NULL) {
692 144
                if (cl == -1)
693 76
                        req->req_body_status = BS_EOF;
694
                else {
695
                        /* Note: If cl==0 here, we still need to have
696
                         * req_body_status==BS_LENGTH, so that there will
697
                         * be a wait for the stream to reach H2_S_CLOS_REM
698
                         * while dealing with the request body. */
699 68
                        req->req_body_status = BS_LENGTH;
700
                }
701
                /* Set req->htc->content_length because this is used as
702
                 * the hint in vrb_pull() for how large the storage
703
                 * buffers need to be */
704 144
                req->htc->content_length = cl;
705 144
        } else {
706
                /* A HEADER frame contained END_STREAM */
707 428
                assert (req->req_body_status == BS_NONE);
708 428
                r2->state = H2_S_CLOS_REM;
709 428
                if (cl > 0) {
710 4
                        H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx header with END_STREAM "
711
                            "and content-length > 0");
712 4
                        return (H2CE_PROTOCOL_ERROR); //rfc7540,l,1838,1840
713
                }
714
        }
715
716 568
        if (req->http->hd[HTTP_HDR_METHOD].b == NULL) {
717 4
                H2S_Lock_VSLb(h2, SLT_Debug, "Missing :method");
718 4
                return (H2SE_PROTOCOL_ERROR); //rfc7540,l,3087,3090
719
        }
720
721 564
        http_SetWellKnownMethod(req->http);
722
723 564
        if (req->http->hd[HTTP_HDR_URL].b == NULL) {
724 4
                H2S_Lock_VSLb(h2, SLT_Debug, "Missing :path");
725 4
                return (H2SE_PROTOCOL_ERROR); //rfc7540,l,3087,3090
726
        }
727
728 560
        AN(req->http->hd[HTTP_HDR_PROTO].b);
729
730 560
        if (*req->http->hd[HTTP_HDR_URL].b == '*' &&
731 16
            ! http_method_eq(req->http->wkm, WKM_OPTIONS)) {
732 12
                H2S_Lock_VSLb(h2, SLT_BogoHeader, "Illegal :path pseudo-header");
733 12
                return (H2SE_PROTOCOL_ERROR); //rfc7540,l,3068,3071
734
        }
735
736 548
        assert(req->req_step == R_STP_TRANSPORT);
737 548
        VCL_TaskEnter(req->privs);
738 548
        VCL_TaskEnter(req->top->privs);
739 548
        req->task->func = h2_do_req;
740 548
        req->task->priv = req;
741 548
        r2->scheduled = 1;
742 548
        if (Pool_Task(wrk->pool, req->task, TASK_QUEUE_STR) != 0) {
743 4
                r2->scheduled = 0;
744 4
                r2->state = H2_S_CLOSED;
745 4
                return (H2SE_REFUSED_STREAM); //rfc7540,l,3326,3329
746
        }
747 544
        return (0);
748 592
}
749
750
static h2_error v_matchproto_(h2_rxframe_f)
751 768
h2_rx_headers(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
752
{
753
        struct req *req;
754
        h2_error h2e;
755
        const uint8_t *p;
756
        size_t l;
757
758 768
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
759 768
        ASSERT_RXTHR(h2);
760
761 768
        if (r2 != NULL) {
762 4
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx headers on non-idle stream");
763 4
                return (H2CE_PROTOCOL_ERROR);   // rfc9113,l,887,891
764
        }
765
766 764
        if (h2->rxf_stream <= h2->highest_stream) {
767 4
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: new stream ID < highest stream");
768 4
                return (H2CE_PROTOCOL_ERROR);   // rfc7540,l,1153,1158
769
        }
770
        /* NB: we don't need to guard the read of h2->open_streams
771
         * because headers are handled sequentially so it cannot
772
         * increase under our feet.
773
         */
774 1520
        if (h2->open_streams >=
775 760
            (int)h2->local_settings.max_concurrent_streams) {
776 8
                H2S_Lock_VSLb(h2, SLT_Debug,
777
                    "H2: stream %u: Hit maximum number of "
778 4
                    "concurrent streams", h2->rxf_stream);
779 4
                return (H2SE_REFUSED_STREAM);   // rfc7540,l,1200,1205
780
        }
781 756
        h2->highest_stream = h2->rxf_stream;
782 756
        r2 = h2_new_req(h2, h2->rxf_stream, NULL);
783 756
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
784 756
        assert(r2->state == H2_S_IDLE);
785 756
        r2->state = H2_S_OPEN;
786
787 756
        req = r2->req;
788 756
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
789
790 756
        req->vsl->wid = VXID_Get(wrk, VSL_CLIENTMARKER);
791 756
        VSLb(req->vsl, SLT_Begin, "req %ju rxreq", VXID(req->sp->vxid));
792 756
        VSL(SLT_Link, req->sp->vxid, "req %ju rxreq", VXID(req->vsl->wid));
793
794 756
        h2->new_req = req;
795 756
        req->sp = h2->sess;
796 756
        req->transport = &HTTP2_transport;
797
798 756
        req->t_first = h2->t1;
799 756
        req->t_prev = req->t_first;
800 756
        VSLb_ts_req(req, "Start", req->t_first);
801 756
        req->acct.req_hdrbytes += h2->rxf_len;
802
803 756
        HTTP_Setup(req->http, req->ws, req->vsl, SLT_ReqMethod);
804 756
        http_SetH(req->http, HTTP_HDR_PROTO, "HTTP/2.0");
805
806 756
        h2h_decode_hdr_init(h2);
807
808 756
        p = h2->rxf_data;
809 756
        l = h2->rxf_len;
810 756
        if (h2->rxf_flags & H2FF_HEADERS_PADDED) {
811 28
                if (*p + 1 > l) {
812 8
                        H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx headers with pad length > frame len");
813 8
                        return (H2CE_PROTOCOL_ERROR);   // rfc7540,l,1884,1887
814
                }
815 20
                l -= 1 + *p;
816 20
                p += 1;
817 20
        }
818 748
        if (h2->rxf_flags & H2FF_HEADERS_PRIORITY) {
819 12
                if (l < 5) {
820 4
                        H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx headers with incorrect "
821
                            "priority data");
822 4
                        return (H2CE_PROTOCOL_ERROR);
823
                }
824 8
                l -= 5;
825 8
                p += 5;
826 8
        }
827 744
        h2e = h2h_decode_bytes(h2, p, l);
828 744
        if (h2e != NULL) {
829 136
                H2S_Lock_VSLb(h2, SLT_Debug, "HPACK(hdr) %s", h2e->name);
830 136
                (void)h2h_decode_hdr_fini(h2);
831 136
                assert(!WS_IsReserved(r2->req->ws));
832 136
                h2_del_req(wrk, r2);
833 136
                return (h2e);
834
        }
835
836 608
        if (h2->rxf_flags & H2FF_HEADERS_END_STREAM)
837 464
                req->req_body_status = BS_NONE;
838
839 608
        if (h2->rxf_flags & H2FF_HEADERS_END_HEADERS)
840 580
                return (h2_end_headers(wrk, h2, req, r2));
841 28
        return (0);
842 768
}
843
844
/**********************************************************************/
845
846
static h2_error v_matchproto_(h2_rxframe_f)
847 76
h2_rx_continuation(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
848
{
849
        struct req *req;
850
        h2_error h2e;
851
852 76
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
853 76
        ASSERT_RXTHR(h2);
854 76
        CHECK_OBJ_ORNULL(r2, H2_REQ_MAGIC);
855
856 76
        if (r2 == NULL || r2->state != H2_S_OPEN || r2->req != h2->new_req) {
857 16
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: rx unexpected CONT frame"
858 8
                    " on stream %d", h2->rxf_stream);
859 8
                return (H2CE_PROTOCOL_ERROR);   // XXX spec ?
860
        }
861 68
        req = r2->req;
862 68
        h2e = h2h_decode_bytes(h2, h2->rxf_data, h2->rxf_len);
863 68
        r2->req->acct.req_hdrbytes += h2->rxf_len;
864 68
        if (h2e != NULL) {
865 8
                H2S_Lock_VSLb(h2, SLT_Debug, "HPACK(cont) %s", h2e->name);
866 8
                (void)h2h_decode_hdr_fini(h2);
867 8
                assert(!WS_IsReserved(r2->req->ws));
868 8
                h2_del_req(wrk, r2);
869 8
                return (h2e);
870
        }
871 60
        if (h2->rxf_flags & H2FF_HEADERS_END_HEADERS)
872 12
                return (h2_end_headers(wrk, h2, req, r2));
873 48
        return (0);
874 76
}
875
876
/**********************************************************************/
877
878
static h2_error v_matchproto_(h2_rxframe_f)
879 1256
h2_rx_data(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
880
{
881
        char buf[4];
882
        ssize_t l;
883
        uint64_t l2, head;
884
        const uint8_t *src;
885
        unsigned len;
886
887
        /* XXX: Shouldn't error handling, setting of r2->error and
888
         * r2->cond signalling be handled more generally at the end of
889
         * procframe()??? */
890
891 1256
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
892 1256
        ASSERT_RXTHR(h2);
893 1256
        CHECK_OBJ_ORNULL(r2, H2_REQ_MAGIC);
894
895 1256
        if (r2 == NULL)
896 4
                return (0);
897
898 1252
        if (r2->state >= H2_S_CLOS_REM) {
899 8
                r2->error = H2SE_STREAM_CLOSED;
900 8
                return (H2SE_STREAM_CLOSED); // rfc7540,l,1766,1769
901
        }
902
903 1244
        Lck_Lock(&h2->sess->mtx);
904 1244
        CHECK_OBJ_ORNULL(r2->rxbuf, H2_RXBUF_MAGIC);
905
906 1244
        if (h2->error != NULL || r2->error != NULL) {
907 4
                if (r2->cond)
908 0
                        PTOK(pthread_cond_signal(r2->cond));
909 4
                Lck_Unlock(&h2->sess->mtx);
910 4
                return (h2->error != NULL ? h2->error : r2->error);
911
        }
912
913
        /* Check padding if present */
914 1240
        src = h2->rxf_data;
915 1240
        len = h2->rxf_len;
916 1240
        if (h2->rxf_flags & H2FF_DATA_PADDED) {
917 1064
                if (*src >= len) {
918 0
                        VSLb(h2->vsl, SLT_SessError,
919
                            "H2: stream %u: Padding larger than frame length",
920 0
                            h2->rxf_stream);
921 0
                        r2->error = H2CE_PROTOCOL_ERROR;
922 0
                        if (r2->cond)
923 0
                                PTOK(pthread_cond_signal(r2->cond));
924 0
                        Lck_Unlock(&h2->sess->mtx);
925 0
                        return (H2CE_PROTOCOL_ERROR);
926
                }
927 1064
                len -= 1 + *src;
928 1064
                src += 1;
929 1064
        }
930
931
        /* Check against the Content-Length header if given */
932 1240
        if (r2->req->htc->content_length >= 0) {
933 1204
                if (r2->rxbuf)
934 112
                        l = r2->rxbuf->head;
935
                else
936 1092
                        l = 0;
937 1204
                l += len;
938 1260
                if (l > r2->req->htc->content_length ||
939 1192
                    ((h2->rxf_flags & H2FF_DATA_END_STREAM) &&
940 56
                     l != r2->req->htc->content_length)) {
941 24
                        VSLb(h2->vsl, SLT_Debug,
942
                            "H2: stream %u: Received data and Content-Length"
943 12
                            " mismatch", h2->rxf_stream);
944 12
                        r2->error = H2SE_PROTOCOL_ERROR;
945 12
                        if (r2->cond)
946 4
                                PTOK(pthread_cond_signal(r2->cond));
947 12
                        Lck_Unlock(&h2->sess->mtx);
948 12
                        return (H2SE_PROTOCOL_ERROR);
949
                }
950 1192
        }
951
952
        /* Check and charge connection window. The entire frame including
953
         * padding (h2->rxf_len) counts towards the window. */
954 1228
        if (h2->rxf_len > h2->req0->r_window) {
955 0
                VSLb(h2->vsl, SLT_SessError,
956
                    "H2: stream %u: Exceeded connection receive window",
957 0
                    h2->rxf_stream);
958 0
                r2->error = H2CE_FLOW_CONTROL_ERROR;
959 0
                if (r2->cond)
960 0
                        PTOK(pthread_cond_signal(r2->cond));
961 0
                Lck_Unlock(&h2->sess->mtx);
962 0
                return (H2CE_FLOW_CONTROL_ERROR);
963
        }
964 1228
        h2->req0->r_window -= h2->rxf_len;
965 1228
        if (h2->req0->r_window < cache_param->h2_rx_window_low_water) {
966 172
                h2->req0->r_window += cache_param->h2_rx_window_increment;
967 172
                vbe32enc(buf, cache_param->h2_rx_window_increment);
968 172
                Lck_Unlock(&h2->sess->mtx);
969 172
                H2_Send_Get(wrk, h2, h2->req0);
970 172
                H2_Send_Frame(wrk, h2, H2_F_WINDOW_UPDATE, 0, 4, 0, buf);
971 172
                H2_Send_Rel(h2, h2->req0);
972 172
                Lck_Lock(&h2->sess->mtx);
973 172
        }
974
975
        /* Check stream window. The entire frame including padding
976
         * (h2->rxf_len) counts towards the window. */
977 1228
        if (h2->rxf_len > r2->r_window) {
978 0
                VSLb(h2->vsl, SLT_Debug,
979
                    "H2: stream %u: Exceeded stream receive window",
980 0
                    h2->rxf_stream);
981 0
                r2->error = H2SE_FLOW_CONTROL_ERROR;
982 0
                if (r2->cond)
983 0
                        PTOK(pthread_cond_signal(r2->cond));
984 0
                Lck_Unlock(&h2->sess->mtx);
985 0
                return (H2SE_FLOW_CONTROL_ERROR);
986
        }
987
988
        /* Handle zero size frame before starting to allocate buffers */
989 1228
        if (len == 0) {
990 1036
                r2->r_window -= h2->rxf_len;
991
992
                /* Handle the specific corner case where the entire window
993
                 * has been exhausted using nothing but padding
994
                 * bytes. Since no bytes have been buffered, no bytes
995
                 * would be consumed by the request thread and no stream
996
                 * window updates sent. Unpaint ourselves from this corner
997
                 * by sending a stream window update here. */
998 1036
                CHECK_OBJ_ORNULL(r2->rxbuf, H2_RXBUF_MAGIC);
999 1036
                if (r2->r_window == 0 &&
1000 4
                    (r2->rxbuf == NULL || r2->rxbuf->tail == r2->rxbuf->head)) {
1001 4
                        if (r2->rxbuf)
1002 0
                                l = r2->rxbuf->size;
1003
                        else
1004 4
                                l = h2->local_settings.initial_window_size;
1005 4
                        r2->r_window += l;
1006 4
                        Lck_Unlock(&h2->sess->mtx);
1007 4
                        vbe32enc(buf, l);
1008 4
                        H2_Send_Get(wrk, h2, h2->req0);
1009 8
                        H2_Send_Frame(wrk, h2, H2_F_WINDOW_UPDATE, 0, 4,
1010 4
                            r2->stream, buf);
1011 4
                        H2_Send_Rel(h2, h2->req0);
1012 4
                        Lck_Lock(&h2->sess->mtx);
1013 4
                }
1014
1015 1036
                if (h2->rxf_flags & H2FF_DATA_END_STREAM)
1016 12
                        r2->state = H2_S_CLOS_REM;
1017 1036
                if (r2->cond)
1018 1008
                        PTOK(pthread_cond_signal(r2->cond));
1019 1036
                Lck_Unlock(&h2->sess->mtx);
1020 1036
                return (0);
1021
        }
1022
1023
        /* Make the buffer on demand */
1024 192
        if (r2->rxbuf == NULL) {
1025
                unsigned bufsize;
1026
                size_t bstest;
1027
                struct stv_buffer *stvbuf;
1028
                struct h2_rxbuf *rxbuf;
1029
1030 88
                Lck_Unlock(&h2->sess->mtx);
1031
1032 88
                bufsize = h2->local_settings.initial_window_size;
1033 88
                if (bufsize < r2->r_window) {
1034
                        /* This will not happen because we do not have any
1035
                         * mechanism to change the initial window size on
1036
                         * a running session. But if we gain that ability,
1037
                         * this future proofs it. */
1038 0
                        bufsize = r2->r_window;
1039 0
                }
1040 88
                assert(bufsize > 0);
1041 88
                if ((h2->rxf_flags & H2FF_DATA_END_STREAM) &&
1042 52
                    bufsize > len)
1043
                        /* Cap the buffer size when we know this is the
1044
                         * single data frame. */
1045 52
                        bufsize = len;
1046 88
                CHECK_OBJ_NOTNULL(stv_h2_rxbuf, STEVEDORE_MAGIC);
1047 176
                stvbuf = STV_AllocBuf(wrk, stv_h2_rxbuf,
1048 88
                    bufsize + sizeof *rxbuf);
1049 88
                if (stvbuf == NULL) {
1050 0
                        Lck_Lock(&h2->sess->mtx);
1051 0
                        VSLb(h2->vsl, SLT_Debug,
1052
                            "H2: stream %u: Failed to allocate request body"
1053
                            " buffer",
1054 0
                            h2->rxf_stream);
1055 0
                        r2->error = H2SE_INTERNAL_ERROR;
1056 0
                        if (r2->cond)
1057 0
                                PTOK(pthread_cond_signal(r2->cond));
1058 0
                        Lck_Unlock(&h2->sess->mtx);
1059 0
                        return (H2SE_INTERNAL_ERROR);
1060
                }
1061 88
                rxbuf = STV_GetBufPtr(stvbuf, &bstest);
1062 88
                AN(rxbuf);
1063 88
                assert(bstest >= bufsize + sizeof *rxbuf);
1064 88
                assert(PAOK(rxbuf));
1065 88
                INIT_OBJ(rxbuf, H2_RXBUF_MAGIC);
1066 88
                rxbuf->size = bufsize;
1067 88
                rxbuf->stvbuf = stvbuf;
1068
1069 88
                r2->rxbuf = rxbuf;
1070
1071 88
                Lck_Lock(&h2->sess->mtx);
1072 88
        }
1073
1074 192
        CHECK_OBJ_NOTNULL(r2->rxbuf, H2_RXBUF_MAGIC);
1075 192
        assert(r2->rxbuf->tail <= r2->rxbuf->head);
1076 192
        l = r2->rxbuf->head - r2->rxbuf->tail;
1077 192
        assert(l <= r2->rxbuf->size);
1078 192
        l = r2->rxbuf->size - l;
1079 192
        assert(len <= l); /* Stream window handling ensures this */
1080
1081 192
        Lck_Unlock(&h2->sess->mtx);
1082
1083 192
        l = len;
1084 192
        head = r2->rxbuf->head;
1085 192
        do {
1086 212
                l2 = l;
1087 212
                if ((head % r2->rxbuf->size) + l2 > r2->rxbuf->size)
1088 20
                        l2 = r2->rxbuf->size - (head % r2->rxbuf->size);
1089 212
                assert(l2 > 0);
1090 212
                memcpy(&r2->rxbuf->data[head % r2->rxbuf->size], src, l2);
1091 212
                src += l2;
1092 212
                head += l2;
1093 212
                l -= l2;
1094 212
        } while (l > 0);
1095
1096 192
        Lck_Lock(&h2->sess->mtx);
1097
1098
        /* Charge stream window. The entire frame including padding
1099
         * (h2->rxf_len) counts towards the window. The used padding
1100
         * bytes will be included in the next connection window update
1101
         * sent when the buffer bytes are consumed because that is
1102
         * calculated against the available buffer space. */
1103 192
        r2->r_window -= h2->rxf_len;
1104 192
        r2->rxbuf->head += len;
1105 192
        assert(r2->rxbuf->tail <= r2->rxbuf->head);
1106 192
        if (h2->rxf_flags & H2FF_DATA_END_STREAM)
1107 72
                r2->state = H2_S_CLOS_REM;
1108 192
        if (r2->cond)
1109 108
                PTOK(pthread_cond_signal(r2->cond));
1110 192
        Lck_Unlock(&h2->sess->mtx);
1111
1112 192
        return (0);
1113 1256
}
1114
1115
static enum vfp_status v_matchproto_(vfp_pull_f)
1116 200
h2_vfp_body(struct vfp_ctx *vc, struct vfp_entry *vfe, void *ptr, ssize_t *lp)
1117
{
1118
        struct h2_req *r2;
1119
        struct h2_sess *h2;
1120
        enum vfp_status retval;
1121
        ssize_t l, l2;
1122
        uint64_t tail;
1123
        uint8_t *dst;
1124
        char buf[4];
1125
        int i;
1126
1127 200
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
1128 200
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
1129 200
        CAST_OBJ_NOTNULL(r2, vfe->priv1, H2_REQ_MAGIC);
1130 200
        h2 = r2->h2sess;
1131
1132 200
        AN(ptr);
1133 200
        AN(lp);
1134 200
        assert(*lp >= 0);
1135
1136 200
        Lck_Lock(&h2->sess->mtx);
1137
1138 200
        r2->cond = &vc->wrk->cond;
1139 1340
        while (1) {
1140 1340
                CHECK_OBJ_ORNULL(r2->rxbuf, H2_RXBUF_MAGIC);
1141 1340
                if (r2->rxbuf) {
1142 272
                        assert(r2->rxbuf->tail <= r2->rxbuf->head);
1143 272
                        l = r2->rxbuf->head - r2->rxbuf->tail;
1144 272
                } else
1145 1068
                        l = 0;
1146
1147 1340
                if (h2->error != NULL || r2->error != NULL)
1148 24
                        retval = VFP_ERROR;
1149 1316
                else if (r2->state >= H2_S_CLOS_REM && l <= *lp)
1150 80
                        retval = VFP_END;
1151
                else {
1152 1236
                        if (l > *lp)
1153 0
                                l = *lp;
1154 1236
                        retval = VFP_OK;
1155
                }
1156
1157 1340
                if (retval != VFP_OK || l > 0)
1158 200
                        break;
1159
1160 2280
                i = Lck_CondWaitTimeout(r2->cond, &h2->sess->mtx,
1161 1140
                    SESS_TMO(h2->sess, timeout_idle));
1162 1140
                if (i == ETIMEDOUT) {
1163 0
                        retval = VFP_ERROR;
1164 0
                        break;
1165
                }
1166
        }
1167 200
        r2->cond = NULL;
1168
1169 200
        Lck_Unlock(&h2->sess->mtx);
1170
1171 200
        if (l == 0 || retval == VFP_ERROR) {
1172 36
                *lp = 0;
1173 36
                return (retval);
1174
        }
1175
1176 164
        *lp = l;
1177 164
        dst = ptr;
1178 164
        tail = r2->rxbuf->tail;
1179 164
        do {
1180 184
                l2 = l;
1181 184
                if ((tail % r2->rxbuf->size) + l2 > r2->rxbuf->size)
1182 20
                        l2 = r2->rxbuf->size - (tail % r2->rxbuf->size);
1183 184
                assert(l2 > 0);
1184 184
                memcpy(dst, &r2->rxbuf->data[tail % r2->rxbuf->size], l2);
1185 184
                dst += l2;
1186 184
                tail += l2;
1187 184
                l -= l2;
1188 184
        } while (l > 0);
1189
1190 164
        Lck_Lock(&h2->sess->mtx);
1191
1192 164
        CHECK_OBJ_NOTNULL(r2->rxbuf, H2_RXBUF_MAGIC);
1193 164
        r2->rxbuf->tail = tail;
1194 164
        assert(r2->rxbuf->tail <= r2->rxbuf->head);
1195
1196 164
        if (r2->r_window < cache_param->h2_rx_window_low_water &&
1197 116
            r2->state < H2_S_CLOS_REM) {
1198
                /* l is free buffer space */
1199
                /* l2 is calculated window increment */
1200 76
                l = r2->rxbuf->size - (r2->rxbuf->head - r2->rxbuf->tail);
1201 76
                assert(r2->r_window <= l);
1202 76
                l2 = cache_param->h2_rx_window_increment;
1203 76
                if (r2->r_window + l2 > l)
1204 76
                        l2 = l - r2->r_window;
1205 76
                r2->r_window += l2;
1206 76
        } else
1207 88
                l2 = 0;
1208
1209 164
        Lck_Unlock(&h2->sess->mtx);
1210
1211 164
        if (l2 > 0) {
1212 76
                vbe32enc(buf, l2);
1213 76
                H2_Send_Get(vc->wrk, h2, r2);
1214 152
                H2_Send_Frame(vc->wrk, h2, H2_F_WINDOW_UPDATE, 0, 4,
1215 76
                    r2->stream, buf);
1216 76
                H2_Send_Rel(h2, r2);
1217 76
        }
1218
1219 164
        return (retval);
1220 200
}
1221
1222
static void
1223 104
h2_vfp_body_fini(struct vfp_ctx *vc, struct vfp_entry *vfe)
1224
{
1225
        struct h2_req *r2;
1226
        struct h2_sess *h2;
1227 104
        struct stv_buffer *stvbuf = NULL;
1228
1229 104
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
1230 104
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
1231 104
        CAST_OBJ_NOTNULL(r2, vfe->priv1, H2_REQ_MAGIC);
1232 104
        CHECK_OBJ_NOTNULL(r2->req, REQ_MAGIC);
1233 104
        h2 = r2->h2sess;
1234
1235 104
        if (vc->failed) {
1236 0
                CHECK_OBJ_NOTNULL(r2->req->wrk, WORKER_MAGIC);
1237 0
                H2_Send_Get(r2->req->wrk, h2, r2);
1238 0
                H2_Send_RST(r2->req->wrk, h2, r2, r2->stream,
1239
                    H2SE_REFUSED_STREAM);
1240 0
                H2_Send_Rel(h2, r2);
1241 0
                Lck_Lock(&h2->sess->mtx);
1242 0
                r2->error = H2SE_REFUSED_STREAM;
1243 0
                Lck_Unlock(&h2->sess->mtx);
1244 0
        }
1245
1246 104
        if (r2->state >= H2_S_CLOS_REM && r2->rxbuf != NULL) {
1247 72
                Lck_Lock(&h2->sess->mtx);
1248 72
                CHECK_OBJ_ORNULL(r2->rxbuf, H2_RXBUF_MAGIC);
1249 72
                if (r2->rxbuf != NULL) {
1250 72
                        stvbuf = r2->rxbuf->stvbuf;
1251 72
                        r2->rxbuf = NULL;
1252 72
                }
1253 72
                Lck_Unlock(&h2->sess->mtx);
1254 72
                if (stvbuf != NULL) {
1255 72
                        STV_FreeBuf(vc->wrk, &stvbuf);
1256 72
                        AZ(stvbuf);
1257 72
                }
1258 72
        }
1259 104
}
1260
1261
static const struct vfp h2_body = {
1262
        .name = "H2_BODY",
1263
        .pull = h2_vfp_body,
1264
        .fini = h2_vfp_body_fini
1265
};
1266
1267
void v_matchproto_(vtr_req_body_t)
1268 144
h2_req_body(struct req *req)
1269
{
1270
        struct h2_req *r2;
1271
        struct vfp_entry *vfe;
1272
1273 144
        CHECK_OBJ(req, REQ_MAGIC);
1274 144
        CAST_OBJ_NOTNULL(r2, req->transport_priv, H2_REQ_MAGIC);
1275 144
        vfe = VFP_Push(req->vfc, &h2_body);
1276 144
        AN(vfe);
1277 144
        vfe->priv1 = r2;
1278 144
}
1279
1280
/**********************************************************************/
1281
1282
void v_matchproto_(vtr_req_fail_f)
1283 4
h2_req_fail(struct req *req, stream_close_t reason)
1284
{
1285 4
        assert(reason != SC_NULL);
1286 4
        assert(req->sp->fd != 0);
1287 4
        VSLb(req->vsl, SLT_Debug, "H2FAILREQ");
1288 4
}
1289
1290
/**********************************************************************/
1291
1292
static enum htc_status_e v_matchproto_(htc_complete_f)
1293 6994
h2_frame_complete(struct http_conn *htc)
1294
{
1295
        struct h2_sess *h2;
1296
1297 6994
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
1298 6994
        CAST_OBJ_NOTNULL(h2, htc->priv, H2_SESS_MAGIC);
1299 6994
        if (htc->rxbuf_b + 9 > htc->rxbuf_e ||
1300 3669
            htc->rxbuf_b + 9 + (vbe32dec(htc->rxbuf_b) >> 8) > htc->rxbuf_e)
1301 3422
                return (HTC_S_MORE);
1302 3572
        return (HTC_S_COMPLETE);
1303 6994
}
1304
1305
/**********************************************************************/
1306
1307
static h2_error
1308 3568
h2_procframe(struct worker *wrk, struct h2_sess *h2, h2_frame h2f)
1309
{
1310
        struct h2_req *r2;
1311
        h2_error h2e;
1312
1313 3568
        ASSERT_RXTHR(h2);
1314 3568
        if (h2->rxf_stream == 0 && h2f->act_szero != 0) {
1315 8
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: unexpected %s frame on stream 0",
1316 4
                    h2f->name);
1317 4
                return (h2f->act_szero);
1318
        }
1319
1320 3564
        if (h2->rxf_stream != 0 && h2f->act_snonzero != 0) {
1321 8
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: unexpected %s frame on stream %d",
1322 4
                    h2f->name, h2->rxf_stream);
1323 4
                return (h2f->act_snonzero);
1324
        }
1325
1326 3560
        if (h2->rxf_stream > h2->highest_stream && h2f->act_sidle != 0) {
1327 16
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: unexpected %s frame on idle stream "
1328 8
                    "%d", h2f->name, h2->rxf_stream);
1329 8
                return (h2f->act_sidle);
1330
        }
1331
1332 3552
        if (h2->rxf_stream != 0 && !(h2->rxf_stream & 1)) {
1333
                // rfc7540,l,1140,1145
1334
                // rfc7540,l,1153,1158
1335
                /* No even streams, we don't do PUSH_PROMISE */
1336 8
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: illegal stream (=%u)",
1337 4
                    h2->rxf_stream);
1338 4
                return (H2CE_PROTOCOL_ERROR);
1339
        }
1340
1341 6154
        VTAILQ_FOREACH(r2, &h2->streams, list)
1342 5322
                if (r2->stream == h2->rxf_stream)
1343 2716
                        break;
1344
1345 3548
        if (h2->new_req != NULL && h2f != H2_F_CONTINUATION) {
1346 8
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: expected continuation but "
1347 4
                    " received %s on stream %d", h2f->name, h2->rxf_stream);
1348 4
                return (H2CE_PROTOCOL_ERROR);   // rfc7540,l,1859,1863
1349
        }
1350
1351 3544
        h2e = h2f->rxfunc(wrk, h2, r2);
1352 3544
        if (h2e == NULL)
1353 3212
                return (NULL);
1354 332
        if (h2->rxf_stream == 0 || h2e->connection)
1355 112
                return (h2e);   // Connection errors one level up
1356
1357 220
        H2_Send_Get(wrk, h2, h2->req0);
1358 220
        H2_Send_RST(wrk, h2, h2->req0, h2->rxf_stream, h2e);
1359 220
        H2_Send_Rel(h2, h2->req0);
1360 220
        return (NULL);
1361 3568
}
1362
1363
h2_error
1364 273
h2_stream_tmo(struct h2_sess *h2, const struct h2_req *r2, vtim_real now)
1365
{
1366 273
        h2_error h2e = NULL;
1367
1368 273
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
1369 273
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
1370 273
        Lck_AssertHeld(&h2->sess->mtx);
1371
1372
        /* NB: when now is NAN, it means that h2_window_timeout was hit
1373
         * on a lock condwait operation.
1374
         */
1375 273
        if (isnan(now))
1376 4
                AN(r2->t_winupd);
1377
1378 273
        if (h2->error != NULL && h2->error->connection &&
1379 0
            !h2->error->send_goaway)
1380 0
                return (h2->error);
1381
1382 273
        if (r2->t_winupd == 0 && r2->t_send == 0)
1383 164
                return (NULL);
1384
1385 109
        if (isnan(now) || (r2->t_winupd != 0 &&
1386 91
            now - r2->t_winupd > cache_param->h2_window_timeout)) {
1387 16
                VSLb(h2->vsl, SLT_Debug,
1388 8
                     "H2: stream %u: Hit h2_window_timeout", r2->stream);
1389 8
                h2e = H2SE_BROKE_WINDOW;
1390 8
        }
1391
1392 210
        if (h2e == NULL && r2->t_send != 0 &&
1393 101
            now - r2->t_send > SESS_TMO(h2->sess, send_timeout)) {
1394 8
                VSLb(h2->vsl, SLT_Debug,
1395 4
                     "H2: stream %u: Hit send_timeout", r2->stream);
1396 4
                h2e = H2SE_CANCEL;
1397 4
        }
1398
1399 109
        return (h2e);
1400 273
}
1401
1402
static h2_error
1403 222
h2_stream_tmo_unlocked(struct h2_sess *h2, const struct h2_req *r2)
1404
{
1405
        h2_error h2e;
1406
1407 222
        Lck_Lock(&h2->sess->mtx);
1408 222
        h2e = h2_stream_tmo(h2, r2, h2->sess->t_idle);
1409 222
        Lck_Unlock(&h2->sess->mtx);
1410
1411 222
        return (h2e);
1412
}
1413
1414
/*
1415
 * This is the janitorial task of cleaning up any closed & refused
1416
 * streams, and checking if the session is timed out.
1417
 */
1418
static h2_error
1419 415
h2_sweep(struct worker *wrk, struct h2_sess *h2)
1420
{
1421
        struct h2_req *r2, *r22;
1422
        h2_error h2e, tmo;
1423
        vtim_real now;
1424
1425 415
        ASSERT_RXTHR(h2);
1426
1427 415
        h2e = h2->error;
1428 415
        now = VTIM_real();
1429 415
        if (h2e == NULL && h2->open_streams == 0 &&
1430 270
            h2->sess->t_idle + cache_param->timeout_idle < now)
1431 12
                h2e = H2CE_NO_ERROR;
1432
1433 415
        h2->do_sweep = 0;
1434 1200
        VTAILQ_FOREACH_SAFE(r2, &h2->streams, list, r22) {
1435 785
                if (r2 == h2->req0) {
1436 415
                        assert (r2->state == H2_S_IDLE);
1437 415
                        continue;
1438
                }
1439 370
                switch (r2->state) {
1440
                case H2_S_CLOSED:
1441 148
                        AZ(r2->scheduled);
1442 148
                        h2_del_req(wrk, r2);
1443 148
                        break;
1444
                case H2_S_CLOS_REM:
1445 198
                        if (!r2->scheduled) {
1446 0
                                H2_Send_Get(wrk, h2, h2->req0);
1447 0
                                H2_Send_RST(wrk, h2, h2->req0, r2->stream,
1448
                                    H2SE_REFUSED_STREAM);
1449 0
                                H2_Send_Rel(h2, h2->req0);
1450 0
                                h2_del_req(wrk, r2);
1451 0
                                continue;
1452
                        }
1453
                        /* FALLTHROUGH */
1454
                case H2_S_CLOS_LOC:
1455
                case H2_S_OPEN:
1456 222
                        tmo = h2_stream_tmo_unlocked(h2, r2);
1457 222
                        if (h2e == NULL)
1458 222
                                h2e = tmo;
1459 222
                        break;
1460 0
                case H2_S_IDLE:
1461
                        /* Current code make this unreachable: h2_new_req is
1462
                         * only called inside h2_rx_headers, which immediately
1463
                         * sets the new stream state to H2_S_OPEN */
1464
                        /* FALLTHROUGH */
1465
                default:
1466 0
                        WRONG("Wrong h2 stream state");
1467 0
                        break;
1468
                }
1469 370
        }
1470 415
        return (h2e);
1471
}
1472
1473
/*
1474
 * if we have received end_headers, the new request is started
1475
 * if we have not received end_stream, DATA frames are expected later
1476
 *
1477
 * neither of these make much sense to output here
1478
 *
1479
 * goaway currently is always 0, see #4285
1480
 */
1481
static void
1482 448
h2_htc_debug(enum htc_status_e hs, struct h2_sess *h2)
1483
{
1484
        const char *s, *r;
1485
1486 448
        if (LIKELY(VSL_tag_is_masked(SLT_Debug)))
1487 0
                return;
1488
1489 448
        HTC_Status(hs, &s, &r);
1490 896
        H2S_Lock_VSLb(h2, SLT_Debug, "H2: HTC %s (%s) frame=%s goaway=%d",
1491 448
            s, r, h2->htc->rxbuf_b == h2->htc->rxbuf_e ? "complete" : "partial",
1492 448
            h2->goaway);
1493 448
}
1494
1495
/***********************************************************************
1496
 * Called in loop from h2_new_session()
1497
 */
1498
1499
#define H2_FRAME(l,U,...) const struct h2_frame_s H2_F_##U[1] = \
1500
    {{ #U, h2_rx_##l, __VA_ARGS__ }};
1501
#include "tbl/h2_frames.h"
1502
1503
static const h2_frame h2flist[] = {
1504
#define H2_FRAME(l,U,t,...) [t] = H2_F_##U,
1505
#include "tbl/h2_frames.h"
1506
};
1507
1508
#define H2FMAX vcountof(h2flist)
1509
1510
int
1511 4348
h2_rxframe(struct worker *wrk, struct h2_sess *h2)
1512
{
1513
        enum htc_status_e hs;
1514
        h2_frame h2f;
1515
        h2_error h2e;
1516
        const char *s, *r;
1517
1518 4348
        ASSERT_RXTHR(h2);
1519
1520 4348
        if (h2->goaway && h2->open_streams == 0) {
1521
                // h2 WS must always be released before returning
1522 0
                WS_ReleaseP(h2->ws, h2->htc->rxbuf_b);
1523 0
                return (0);
1524
        }
1525
1526 4348
        h2->t1 = NAN;
1527 4348
        VTCP_blocking(*h2->htc->rfd);
1528 8696
        hs = HTC_RxStuff(h2->htc, h2_frame_complete, &h2->t1, NULL, NAN,
1529 4348
            VTIM_real() + 0.5, NAN, h2->local_settings.max_frame_size + 9);
1530
1531 4348
        h2e = NULL;
1532 4348
        switch (hs) {
1533
        case HTC_S_EOF:
1534 448
                h2_htc_debug(hs, h2);
1535 448
                h2e = H2CE_NO_ERROR;
1536 448
                break;
1537
        case HTC_S_COMPLETE:
1538 3572
                h2->sess->t_idle = VTIM_real();
1539 3572
                if (h2->do_sweep)
1540 87
                        h2e = h2_sweep(wrk, h2);
1541 3572
                break;
1542
        case HTC_S_TIMEOUT:
1543
                //// #4279
1544
                // h2_htc_debug(hs, h2);
1545 328
                h2e = h2_sweep(wrk, h2);
1546 328
                break;
1547
        default:
1548 0
                HTC_Status(hs, &s, &r);
1549 0
                H2S_Lock_VSLb(h2, SLT_SessError, "H2: HTC %s (%s)", s, r);
1550 0
                h2e = H2CE_ENHANCE_YOUR_CALM;
1551 0
        }
1552
1553 4348
        if (h2e != NULL && h2e->connection) {
1554 460
                h2->error = h2e;
1555 460
                h2_tx_goaway(wrk, h2, h2e);
1556 460
                return (0);
1557
        }
1558
1559 3888
        if (hs != HTC_S_COMPLETE)
1560 316
                return (1);
1561
1562 3572
        h2->rxf_len = vbe32dec(h2->htc->rxbuf_b) >> 8;
1563 3572
        h2->rxf_type = h2->htc->rxbuf_b[3];
1564 3572
        h2->rxf_flags = h2->htc->rxbuf_b[4];
1565 3572
        h2->rxf_stream = vbe32dec(h2->htc->rxbuf_b + 5);
1566 3572
        h2->rxf_stream &= ~(1LU<<31);                   // rfc7540,l,690,692
1567 3572
        h2->rxf_data = (void*)(h2->htc->rxbuf_b + 9);
1568
        /* XXX: later full DATA will not be rx'ed yet. */
1569 3572
        HTC_RxPipeline(h2->htc, h2->htc->rxbuf_b + h2->rxf_len + 9);
1570
1571 3572
        h2_vsl_frame(h2, h2->htc->rxbuf_b, 9L + h2->rxf_len);
1572 3572
        h2->srq->acct.req_hdrbytes += 9;
1573
1574 3572
        if (h2->rxf_type >= H2FMAX) {
1575
                // rfc7540,l,679,681
1576
                // XXX: later, drain rest of frame
1577 4
                h2->bogosity++;
1578 8
                H2S_Lock_VSLb(h2, SLT_Debug,
1579
                    "H2: Unknown frame type 0x%02x (ignored)",
1580 4
                    (uint8_t)h2->rxf_type);
1581 4
                h2->srq->acct.req_bodybytes += h2->rxf_len;
1582 4
                return (1);
1583
        }
1584 3568
        h2f = h2flist[h2->rxf_type];
1585
1586 3568
        AN(h2f->name);
1587 3568
        AN(h2f->rxfunc);
1588 3568
        if (h2f->overhead)
1589 1460
                h2->srq->acct.req_bodybytes += h2->rxf_len;
1590
1591 3568
        if (h2->rxf_flags & ~h2f->flags) {
1592
                // rfc7540,l,687,688
1593 8
                h2->bogosity++;
1594 16
                H2S_Lock_VSLb(h2, SLT_Debug,
1595
                    "H2: Unknown flags 0x%02x on %s (ignored)",
1596 8
                    (uint8_t)h2->rxf_flags & ~h2f->flags, h2f->name);
1597 8
                h2->rxf_flags &= h2f->flags;
1598 8
        }
1599
1600 3568
        h2e = h2_procframe(wrk, h2, h2f);
1601 3568
        if (h2->error == NULL && h2e != NULL) {
1602 124
                h2->error = h2e;
1603 124
                h2_tx_goaway(wrk, h2, h2e);
1604 124
        }
1605
1606 3568
        return (h2->error != NULL ? 0 : 1);
1607 4348
}