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