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