forked from tempesta-tech/tempesta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_frame.c
1826 lines (1523 loc) · 44.4 KB
/
http_frame.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Tempesta FW
*
* Copyright (C) 2019-2022 Tempesta Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#undef DEBUG
#if DBG_HTTP_FRAME > 0
#define DEBUG DBG_HTTP_FRAME
#endif
#include "lib/fsm.h"
#include "lib/str.h"
#include "procfs.h"
#include "http.h"
#include "http_frame.h"
#define FRAME_PREFACE_CLI_MAGIC "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
#define FRAME_PREFACE_CLI_MAGIC_LEN 24
#define FRAME_WND_UPDATE_SIZE 4
#define FRAME_RST_STREAM_SIZE 4
#define FRAME_PRIORITY_SIZE 5
#define FRAME_SETTINGS_ENTRY_SIZE 6
#define FRAME_PING_SIZE 8
#define FRAME_GOAWAY_SIZE 8
#define WND_INCREMENT_SIZE 4
#define SETTINGS_KEY_SIZE 2
#define SETTINGS_VAL_SIZE 4
#define STREAM_ID_SIZE 4
#define ERR_CODE_SIZE 4
#define MAX_WND_SIZE ((1U << 31) - 1)
#define DEF_WND_SIZE ((1U << 16) - 1)
#define TFW_MAX_CLOSED_STREAMS 5
/**
* FSM states for HTTP/2 frames processing.
*/
typedef enum {
HTTP2_RECV_FRAME_HEADER,
HTTP2_RECV_CLI_START_SEQ,
HTTP2_RECV_FIRST_SETTINGS,
HTTP2_RECV_FRAME_PRIORITY,
HTTP2_RECV_FRAME_WND_UPDATE,
HTTP2_RECV_FRAME_PING,
HTTP2_RECV_FRAME_RST_STREAM,
HTTP2_RECV_FRAME_SETTINGS,
HTTP2_RECV_FRAME_GOAWAY,
HTTP2_RECV_FRAME_PADDED,
HTTP2_RECV_HEADER_PRI,
HTTP2_IGNORE_FRAME_DATA,
__HTTP2_RECV_FRAME_APP,
HTTP2_RECV_HEADER = __HTTP2_RECV_FRAME_APP,
HTTP2_RECV_CONT,
HTTP2_RECV_DATA
} TfwFrameState;
/**
* IDs for SETTINGS parameters of HTTP/2 connection (RFC 7540
* section 6.5.2).
*/
typedef enum {
HTTP2_SETTINGS_TABLE_SIZE = 0x01,
HTTP2_SETTINGS_ENABLE_PUSH,
HTTP2_SETTINGS_MAX_STREAMS,
HTTP2_SETTINGS_INIT_WND_SIZE,
HTTP2_SETTINGS_MAX_FRAME_SIZE,
HTTP2_SETTINGS_MAX_HDR_LIST_SIZE
} TfwSettingsId;
#define __FRAME_FSM_EXIT() \
do { \
ctx->rlen = 0; \
T_FSM_EXIT(); \
} while (0)
#define FRAME_FSM_EXIT(ret) \
do { \
r = ret; \
__FRAME_FSM_EXIT(); \
} while (0)
#define FRAME_FSM_FINISH() \
T_FSM_FINISH(r, ctx->state); \
*read += p - buf;
#define FRAME_FSM_MOVE(st) \
do { \
WARN_ON_ONCE(p - buf > len); \
ctx->rlen = 0; \
T_FSM_MOVE(st, \
if (unlikely(p - buf >= len)) { \
__fsm_const_state = st; \
T_FSM_EXIT(); \
}); \
} while (0)
#define FRAME_FSM_NEXT() \
do { \
WARN_ON_ONCE(p - buf > len); \
ctx->rlen = 0; \
if (unlikely(p - buf >= len)) { \
__fsm_const_state = ctx->state; \
T_FSM_EXIT(); \
} \
T_FSM_NEXT(); \
} while (0)
#define FRAME_FSM_READ_LAMBDA(to_read, lambda) \
do { \
WARN_ON_ONCE(ctx->rlen >= (to_read)); \
n = min_t(int, (to_read) - ctx->rlen, buf + len - p); \
lambda; \
p += n; \
ctx->rlen += n; \
if (unlikely(ctx->rlen < (to_read))) \
T_FSM_EXIT(); \
} while (0)
#define FRAME_FSM_READ_SRVC(to_read) \
BUG_ON((to_read) > sizeof(ctx->rbuf)); \
FRAME_FSM_READ_LAMBDA(to_read, { \
memcpy_fast(ctx->rbuf + ctx->rlen, p, n); \
})
#define FRAME_FSM_READ(to_read) \
FRAME_FSM_READ_LAMBDA(to_read, { })
#define SET_TO_READ(ctx) \
do { \
(ctx)->to_read = (ctx)->hdr.length; \
(ctx)->hdr.length = 0; \
} while (0)
#define SET_TO_READ_VERIFY(ctx, next_state) \
do { \
(ctx)->to_read = (ctx)->hdr.length; \
if ((ctx)->hdr.length) { \
(ctx)->state = next_state; \
(ctx)->hdr.length = 0; \
} else { \
(ctx)->state = HTTP2_IGNORE_FRAME_DATA; \
} \
} while (0)
#define APP_FRAME(ctx) \
((ctx)->state >= __HTTP2_RECV_FRAME_APP)
#define STREAM_RECV_PROCESS(ctx, hdr) \
({ \
TfwStreamFsmRes res; \
TfwH2Err err = HTTP2_ECODE_NO_ERROR; \
BUG_ON(!(ctx)->cur_stream); \
if ((res = tfw_h2_stream_fsm((ctx)->cur_stream, (hdr)->type, \
(hdr)->flags, false, &err))) \
{ \
T_DBG3("stream recv processed: result=%d, state=%d, id=%u," \
" err=%d\n", res, (ctx)->cur_stream->state, \
(ctx)->cur_stream->id, err); \
SET_TO_READ_VERIFY((ctx), HTTP2_IGNORE_FRAME_DATA); \
if (res == STREAM_FSM_RES_TERM_CONN) { \
tfw_h2_conn_terminate((ctx), err); \
return T_DROP; \
} else if (res == STREAM_FSM_RES_TERM_STREAM) { \
return tfw_h2_stream_close((ctx), \
(hdr)->stream_id, \
&(ctx)->cur_stream, \
err); \
} \
return T_OK; \
} \
})
int
tfw_h2_init(void)
{
return tfw_h2_stream_cache_create();
}
void
tfw_h2_cleanup(void)
{
tfw_h2_stream_cache_destroy();
}
int
tfw_h2_context_init(TfwH2Ctx *ctx)
{
TfwClosedQueue *hclosed_streams = &ctx->hclosed_streams;
TfwSettings *lset = &ctx->lsettings;
TfwSettings *rset = &ctx->rsettings;
bzero_fast(ctx, sizeof(*ctx));
ctx->state = HTTP2_RECV_CLI_START_SEQ;
ctx->loc_wnd = MAX_WND_SIZE;
spin_lock_init(&ctx->lock);
INIT_LIST_HEAD(&hclosed_streams->list);
lset->hdr_tbl_sz = rset->hdr_tbl_sz = HPACK_TABLE_DEF_SIZE;
lset->push = rset->push = 1;
lset->max_streams = rset->max_streams = 0xffffffff;
lset->max_frame_sz = rset->max_frame_sz = FRAME_DEF_LENGTH;
lset->max_lhdr_sz = rset->max_lhdr_sz = UINT_MAX;
/*
* We ignore client's window size until #498, so currently
* we set it to maximum allowed value.
*/
lset->wnd_sz = rset->wnd_sz = MAX_WND_SIZE;
return tfw_hpack_init(&ctx->hpack, HPACK_TABLE_DEF_SIZE);
}
void
tfw_h2_context_clear(TfwH2Ctx *ctx)
{
WARN_ON_ONCE(ctx->streams_num);
tfw_hpack_clean(&ctx->hpack);
}
static inline void
tfw_h2_unpack_frame_header(TfwFrameHdr *hdr, const unsigned char *buf)
{
hdr->length = ntohl(*(int *)buf) >> 8;
hdr->type = buf[3];
hdr->flags = buf[4];
hdr->stream_id = ntohl(*(unsigned int *)&buf[5]) & FRAME_STREAM_ID_MASK;
T_DBG3("%s: parsed, length=%d, stream_id=%u, type=%hhu, flags=0x%hhx\n",
__func__, hdr->length, hdr->stream_id, hdr->type, hdr->flags);
}
static inline void
tfw_h2_unpack_priority(TfwFramePri *pri, const unsigned char *buf)
{
pri->stream_id = ntohl(*(unsigned int *)buf) & FRAME_STREAM_ID_MASK;
pri->exclusive = (buf[0] & 0x80) > 0;
pri->weight = buf[4] + 1;
}
/**
* Prepare and send HTTP/2 frame to the client; @hdr must contain
* the valid data to fill in the frame's header; @data may carry
* additional data as frame's payload.
*
* NOTE: Caller must leave first chunk of @data unoccupied - to
* provide the place for frame's header which will be packed and
* written in this procedure.
*/
static int
__tfw_h2_send_frame(TfwH2Ctx *ctx, TfwFrameHdr *hdr, TfwStr *data, bool close)
{
int r;
TfwMsgIter it;
TfwMsg msg = {};
unsigned char buf[FRAME_HEADER_SIZE];
TfwStr *hdr_str = TFW_STR_CHUNK(data, 0);
TfwH2Conn *conn = container_of(ctx, TfwH2Conn, h2);
BUG_ON(hdr_str->data);
hdr_str->data = buf;
hdr_str->len = FRAME_HEADER_SIZE;
if (data != hdr_str)
data->len += FRAME_HEADER_SIZE;
tfw_h2_pack_frame_header(buf, hdr);
T_DBG2("Preparing HTTP/2 message with %lu bytes data\n", data->len);
msg.len = data->len;
if ((r = tfw_msg_iter_setup(&it, &msg.skb_head, msg.len, 0)))
goto err;
if ((r = tfw_msg_write(&it, data)))
goto err;
if (close)
msg.ss_flags |= SS_F_CONN_CLOSE;
if ((r = tfw_connection_send((TfwConn *)conn, &msg)))
goto err;
/*
* We do not close client connection automatically here in case
* of failed sending, the caller must make such decision instead;
* thus, we should set Conn_Stop flag only if sending procedure
* was successful - to avoid hanged unclosed client connection.
*/
if (close)
TFW_CONN_TYPE((TfwConn *)conn) |= Conn_Stop;
return 0;
err:
ss_skb_queue_purge(&msg.skb_head);
return r;
}
static inline int
tfw_h2_send_frame(TfwH2Ctx *ctx, TfwFrameHdr *hdr, TfwStr *data)
{
return __tfw_h2_send_frame(ctx, hdr, data, false);
}
static inline int
tfw_h2_send_frame_close(TfwH2Ctx *ctx, TfwFrameHdr *hdr, TfwStr *data)
{
return __tfw_h2_send_frame(ctx, hdr, data, true);
}
static inline int
tfw_h2_send_ping(TfwH2Ctx *ctx)
{
TfwStr data = {
.chunks = (TfwStr []){
{},
{ .data = ctx->rbuf, .len = ctx->rlen }
},
.len = ctx->rlen,
.nchunks = 2
};
TfwFrameHdr hdr = {
.length = ctx->rlen,
.stream_id = 0,
.type = HTTP2_PING,
.flags = HTTP2_F_ACK
};
WARN_ON_ONCE(ctx->rlen != FRAME_PING_SIZE);
return tfw_h2_send_frame(ctx, &hdr, &data);
}
static inline int
tfw_h2_send_wnd_update(TfwH2Ctx *ctx, unsigned int id, unsigned int wnd_incr)
{
unsigned char incr_buf[WND_INCREMENT_SIZE];
TfwStr data = {
.chunks = (TfwStr []){
{},
{ .data = incr_buf, .len = WND_INCREMENT_SIZE }
},
.len = WND_INCREMENT_SIZE,
.nchunks = 2
};
TfwFrameHdr hdr = {
.length = data.len,
.stream_id = id,
.type = HTTP2_WINDOW_UPDATE,
.flags = 0
};
WARN_ON_ONCE((unsigned int)(wnd_incr & FRAME_RESERVED_BIT_MASK));
*(unsigned int *)incr_buf = htonl(wnd_incr);
return tfw_h2_send_frame(ctx, &hdr, &data);
}
static inline int
tfw_h2_send_settings_init(TfwH2Ctx *ctx)
{
unsigned char key_buf[SETTINGS_KEY_SIZE];
unsigned char val_buf[SETTINGS_VAL_SIZE];
TfwStr data = {
.chunks = (TfwStr []){
{},
{ .data = key_buf, .len = SETTINGS_KEY_SIZE },
{ .data = val_buf, .len = SETTINGS_VAL_SIZE }
},
.len = SETTINGS_KEY_SIZE + SETTINGS_VAL_SIZE,
.nchunks = 3
};
TfwFrameHdr hdr = {
.length = data.len,
.stream_id = 0,
.type = HTTP2_SETTINGS,
.flags = 0
};
BUILD_BUG_ON(SETTINGS_KEY_SIZE != sizeof(unsigned short)
|| SETTINGS_VAL_SIZE != sizeof(unsigned int)
|| SETTINGS_VAL_SIZE != sizeof(ctx->lsettings.wnd_sz));
*(unsigned short *)key_buf = htons(HTTP2_SETTINGS_INIT_WND_SIZE);
*(unsigned int *)val_buf = htonl(ctx->lsettings.wnd_sz);
return tfw_h2_send_frame(ctx, &hdr, &data);
}
static inline int
tfw_h2_send_settings_ack(TfwH2Ctx *ctx)
{
TfwStr data = {};
TfwFrameHdr hdr = {
.length = 0,
.stream_id = 0,
.type = HTTP2_SETTINGS,
.flags = HTTP2_F_ACK
};
return tfw_h2_send_frame(ctx, &hdr, &data);
}
static inline int
tfw_h2_send_goaway(TfwH2Ctx *ctx, TfwH2Err err_code)
{
unsigned char id_buf[STREAM_ID_SIZE];
unsigned char err_buf[ERR_CODE_SIZE];
TfwStr data = {
.chunks = (TfwStr []){
{},
{ .data = id_buf, .len = STREAM_ID_SIZE },
{ .data = err_buf, .len = ERR_CODE_SIZE }
},
.len = STREAM_ID_SIZE + ERR_CODE_SIZE,
.nchunks = 3
};
TfwFrameHdr hdr = {
.length = data.len,
.stream_id = 0,
.type = HTTP2_GOAWAY,
.flags = 0
};
WARN_ON_ONCE((unsigned int)(ctx->lstream_id & FRAME_RESERVED_BIT_MASK));
BUILD_BUG_ON(STREAM_ID_SIZE != sizeof(unsigned int)
|| STREAM_ID_SIZE != sizeof(ctx->lstream_id)
|| ERR_CODE_SIZE != sizeof(unsigned int)
|| ERR_CODE_SIZE != sizeof(err_code));
*(unsigned int *)id_buf = htonl(ctx->lstream_id);
*(unsigned int *)err_buf = htonl(err_code);
return tfw_h2_send_frame_close(ctx, &hdr, &data);
}
int
tfw_h2_send_rst_stream(TfwH2Ctx *ctx, unsigned int id, TfwH2Err err_code)
{
unsigned char buf[ERR_CODE_SIZE];
TfwStr data = {
.chunks = (TfwStr []){
{},
{ .data = buf, .len = ERR_CODE_SIZE }
},
.len = ERR_CODE_SIZE,
.nchunks = 2
};
TfwFrameHdr hdr = {
.length = data.len,
.stream_id = id,
.type = HTTP2_RST_STREAM,
.flags = 0
};
*(unsigned int *)buf = htonl(err_code);
return tfw_h2_send_frame(ctx, &hdr, &data);
}
void
tfw_h2_conn_terminate_close(TfwH2Ctx *ctx, TfwH2Err err_code, bool close)
{
TfwH2Conn *conn = container_of(ctx, TfwH2Conn, h2);
if (tfw_h2_send_goaway(ctx, err_code) && close)
tfw_connection_close((TfwConn *)conn, true);
}
static inline void
tfw_h2_conn_terminate(TfwH2Ctx *ctx, TfwH2Err err_code)
{
tfw_h2_conn_terminate_close(ctx, err_code, false);
}
#define VERIFY_FRAME_SIZE(ctx) \
do { \
if ((ctx)->hdr.length < 0) { \
tfw_h2_conn_terminate(ctx, HTTP2_ECODE_SIZE); \
return T_DROP; \
} \
} while (0)
static inline int
tfw_h2_recv_priority(TfwH2Ctx *ctx)
{
ctx->to_read = FRAME_PRIORITY_SIZE;
ctx->hdr.length -= ctx->to_read;
ctx->plen -= ctx->to_read;
VERIFY_FRAME_SIZE(ctx);
ctx->state = HTTP2_RECV_HEADER_PRI;
return T_OK;
}
static inline int
tfw_h2_recv_padded(TfwH2Ctx *ctx)
{
ctx->to_read = 1;
ctx->hdr.length -= ctx->to_read;
ctx->plen -= ctx->to_read;
VERIFY_FRAME_SIZE(ctx);
ctx->state = HTTP2_RECV_FRAME_PADDED;
return T_OK;
}
static int
tfw_h2_headers_pri_process(TfwH2Ctx *ctx)
{
TfwFramePri *pri = &ctx->priority;
TfwFrameHdr *hdr = &ctx->hdr;
BUG_ON(!(hdr->flags & HTTP2_F_PRIORITY));
tfw_h2_unpack_priority(pri, ctx->rbuf);
T_DBG3("%s: parsed, stream_id=%u, dep_stream_id=%u, weight=%hu,"
" excl=%hhu\n", __func__, hdr->stream_id, pri->stream_id,
pri->weight, pri->exclusive);
ctx->data_off += FRAME_PRIORITY_SIZE;
SET_TO_READ_VERIFY(ctx, HTTP2_RECV_HEADER);
return T_OK;
}
/*
* Create a new stream and add it to the streams storage and to the dependency
* tree. Note, that we do not need to protect the streams storage in @sched from
* concurrent access, since all operations with it (adding, searching and
* deletion) are done only in receiving flow of Frame layer.
*/
static TfwStream *
tfw_h2_stream_create(TfwH2Ctx *ctx, unsigned int id)
{
TfwStream *stream, *dep = NULL;
TfwFramePri *pri = &ctx->priority;
bool excl = pri->exclusive;
if (tfw_h2_find_stream_dep(&ctx->sched, pri->stream_id, &dep))
return NULL;
stream = tfw_h2_add_stream(&ctx->sched, id, pri->weight,
ctx->lsettings.wnd_sz);
if (!stream)
return NULL;
tfw_h2_add_stream_dep(&ctx->sched, stream, dep, excl);
++ctx->streams_num;
T_DBG3("%s: stream added, id=%u, stream=[%p] weight=%hu,"
" streams_num=%lu, dep_stream_id=%u, dep_stream=[%p],"
" excl=%hhu\n", __func__, id, stream, stream->weight,
ctx->streams_num, pri->stream_id, dep, pri->exclusive);
return stream;
}
static inline void
tfw_h2_stream_clean(TfwH2Ctx *ctx, TfwStream *stream)
{
tfw_h2_stop_stream(&ctx->sched, stream);
tfw_h2_delete_stream(stream);
--ctx->streams_num;
}
/*
* Unlink the stream from a corresponding request (if linked) and from special
* queue of closed streams (if it is contained there).
*
* NOTE: call to this procedure should be protected by special lock for
* Stream linkage protection.
*/
static void
__tfw_h2_stream_unlink(TfwH2Ctx *ctx, TfwStream *stream)
{
TfwHttpMsg *hmreq = (TfwHttpMsg *)stream->msg;
if (!list_empty(&stream->hcl_node)) {
list_del_init(&stream->hcl_node);
--ctx->hclosed_streams.num;
}
if (hmreq) {
hmreq->stream = NULL;
/*
* If the request is linked with a stream, but not complete yet,
* it must be deleted right here to avoid leakage, because in
* this case it is not used anywhere yet. When request is
* assembled and complete, it will be removed (due to some
* processing error) in @tfw_http_req_process(), or in other
* cases controlled by server connection side (after adding to
* @fwd_queue): successful response sending, eviction etc.
*/
if (!test_bit(TFW_HTTP_B_FULLY_PARSED, hmreq->flags))
tfw_http_conn_msg_free(hmreq);
}
}
static inline void
tfw_h2_stream_unlink(TfwH2Ctx *ctx, TfwStream *stream)
{
spin_lock(&ctx->lock);
__tfw_h2_stream_unlink(ctx, stream);
spin_unlock(&ctx->lock);
}
static inline void
tfw_h2_current_stream_remove(TfwH2Ctx *ctx)
{
tfw_h2_stream_unlink(ctx, ctx->cur_stream);
tfw_h2_stream_clean(ctx, ctx->cur_stream);
ctx->cur_stream = NULL;
}
void
tfw_h2_conn_streams_cleanup(TfwH2Ctx *ctx)
{
TfwStream *cur, *next;
TfwH2Conn *conn = container_of(ctx, TfwH2Conn, h2);
TfwStreamSched *sched = &ctx->sched;
WARN_ON_ONCE(((TfwConn *)conn)->stream.msg);
rbtree_postorder_for_each_entry_safe(cur, next, &sched->streams, node) {
tfw_h2_stream_unlink(ctx, cur);
tfw_h2_stream_clean(ctx, cur);
}
}
/*
* Add stream to special queue of closed streams.
*
* NOTE: call to this procedure should be protected by special lock for
* Stream linkage protection.
*/
static inline void
__tfw_h2_stream_add_closed(TfwClosedQueue *hclosed_streams, TfwStream *stream)
{
if (!list_empty(&stream->hcl_node))
return;
list_add_tail(&stream->hcl_node, &hclosed_streams->list);
++hclosed_streams->num;
}
static inline void
tfw_h2_stream_add_closed(TfwH2Ctx *ctx, TfwStream *stream)
{
spin_lock(&ctx->lock);
__tfw_h2_stream_add_closed(&ctx->hclosed_streams, stream);
spin_unlock(&ctx->lock);
}
/*
* Stream closing procedure: move the stream into special queue of closed
* streams and send RST_STREAM frame to peer. This procedure is intended
* for usage only in receiving flow of Framing layer, thus the stream is
* definitely alive here and we need not any unlinking operations since
* all the unlinking and cleaning work will be made later, during shrinking
* the queue of closed streams; thus, we just move the stream into the
* closed queue here.
*/
static int
tfw_h2_stream_close(TfwH2Ctx *ctx, unsigned int id, TfwStream **stream,
TfwH2Err err_code)
{
if (stream && *stream) {
tfw_h2_stream_add_closed(ctx, *stream);
*stream = NULL;
}
return tfw_h2_send_rst_stream(ctx, id, err_code);
}
/*
* Get stream ID for upper layer to create frames info.
*/
unsigned int
tfw_h2_stream_id(TfwHttpReq *req)
{
unsigned int id = 0;
TfwH2Ctx *ctx = tfw_h2_context(req->conn);
spin_lock(&ctx->lock);
if (req->stream)
id = req->stream->id;
spin_unlock(&ctx->lock);
return id;
}
/*
* Get stream ID for upper layer to prepare and send frame with response to
* client, and process stream FSM for the frame (of type specified in @type
* and with flags set in @flags). This procedure also unlinks request from
* corresponding stream (if linked) and moves the stream to the queue of
* closed streams (if it is not contained there yet).
*/
unsigned int
tfw_h2_stream_id_close(TfwHttpReq *req, unsigned char type,
unsigned char flags)
{
TfwStream *stream;
unsigned int id = 0;
TfwH2Ctx *ctx = tfw_h2_context(req->conn);
spin_lock(&ctx->lock);
stream = req->stream;
if (!stream) {
spin_unlock(&ctx->lock);
return 0;
}
if (type < _HTTP2_UNDEFINED &&
!STREAM_SEND_PROCESS(stream, type, flags))
{
id = stream->id;
}
req->stream = NULL;
stream->msg = NULL;
__tfw_h2_stream_add_closed(&ctx->hclosed_streams, stream);
spin_unlock(&ctx->lock);
return id;
}
/*
* Clean the queue of closed streams if its size has exceeded a certain
* value.
*/
static void
tfw_h2_closed_streams_shrink(TfwH2Ctx *ctx)
{
TfwStream *cur;
unsigned int max_streams = ctx->lsettings.max_streams;
TfwClosedQueue *hclosed_streams = &ctx->hclosed_streams;
while (1)
{
spin_lock(&ctx->lock);
if (hclosed_streams->num <= TFW_MAX_CLOSED_STREAMS
|| (max_streams == ctx->streams_num
&& hclosed_streams->num))
{
spin_unlock(&ctx->lock);
break;
}
BUG_ON(list_empty(&hclosed_streams->list));
cur = list_first_entry(&hclosed_streams->list, TfwStream,
hcl_node);
__tfw_h2_stream_unlink(ctx, cur);
spin_unlock(&ctx->lock);
tfw_h2_stream_clean(ctx, cur);
}
}
static inline void
tfw_h2_check_closed_stream(TfwH2Ctx *ctx)
{
BUG_ON(!ctx->cur_stream);
T_DBG3("%s: stream->id=%u, stream->state=%d, stream=[%p], streams_num="
"%lu\n", __func__, ctx->cur_stream->id, ctx->cur_stream->state,
ctx->cur_stream, ctx->streams_num);
if (tfw_h2_stream_is_closed(ctx->cur_stream))
tfw_h2_current_stream_remove(ctx);
}
static inline int
tfw_h2_stream_state_process(TfwH2Ctx *ctx)
{
TfwFrameHdr *hdr = &ctx->hdr;
STREAM_RECV_PROCESS(ctx, hdr);
tfw_h2_check_closed_stream(ctx);
return T_OK;
}
static int
tfw_h2_headers_process(TfwH2Ctx *ctx)
{
TfwFrameHdr *hdr = &ctx->hdr;
T_DBG3("%s: stream->id=%u, cur_stream=[%p]\n", __func__,
hdr->stream_id, ctx->cur_stream);
/*
* Stream cannot depend on itself (see RFC 7540 section 5.1.2 for
* details).
*/
if (ctx->priority.stream_id == hdr->stream_id) {
T_DBG("Invalid dependency: new stream with %u depends on"
" itself\n", hdr->stream_id);
ctx->state = HTTP2_IGNORE_FRAME_DATA;
if (!STREAM_SEND_PROCESS(ctx->cur_stream, HTTP2_RST_STREAM, 0))
return tfw_h2_stream_close(ctx, hdr->stream_id,
&ctx->cur_stream,
HTTP2_ECODE_PROTO);
return T_OK;
}
if (!ctx->cur_stream) {
ctx->cur_stream = tfw_h2_stream_create(ctx, hdr->stream_id);
if (!ctx->cur_stream)
return T_DROP;
ctx->lstream_id = hdr->stream_id;
}
/*
* Since the same received HEADERS frame can cause the stream to become
* 'open' (i.e. created) and right away become 'half-closed (remote)'
* (in case of both END_STREAM and END_HEADERS flags set in initial
* HEADERS frame), we should process its state here - when frame is
* fully received and new stream is created.
*/
return tfw_h2_stream_state_process(ctx);
}
static int
tfw_h2_wnd_update_process(TfwH2Ctx *ctx)
{
unsigned int wnd_incr;
TfwFrameHdr *hdr = &ctx->hdr;
wnd_incr = ntohl(*(unsigned int *)ctx->rbuf) & ((1U << 31) - 1);
if (wnd_incr) {
/*
* TODO #498: apply new window size for entire connection or
* particular stream.
*/
return T_OK;
}
if (!ctx->cur_stream) {
tfw_h2_conn_terminate(ctx, HTTP2_ECODE_PROTO);
return T_DROP;
}
if (STREAM_SEND_PROCESS(ctx->cur_stream, HTTP2_RST_STREAM, 0))
return T_OK;
return tfw_h2_stream_close(ctx, hdr->stream_id, &ctx->cur_stream,
HTTP2_ECODE_PROTO);
}
static inline int
tfw_h2_priority_process(TfwH2Ctx *ctx)
{
TfwFrameHdr *hdr = &ctx->hdr;
TfwFramePri *pri = &ctx->priority;
tfw_h2_unpack_priority(pri, ctx->rbuf);
if (pri->stream_id != hdr->stream_id) {
T_DBG3("%s: parsed, stream_id=%u, dep_stream_id=%u, weight=%hu,"
" excl=%hhu\n", __func__, hdr->stream_id, pri->stream_id,
pri->weight, pri->exclusive);
tfw_h2_change_stream_dep(&ctx->sched, hdr->stream_id,
pri->stream_id, pri->weight,
pri->exclusive);
return T_OK;
}
/*
* Stream cannot depend on itself (see RFC 7540 section 5.1.2 for
* details).
*/
T_DBG("Invalid dependency: new stream with %u depends on"
" itself\n", hdr->stream_id);
if (STREAM_SEND_PROCESS(ctx->cur_stream, HTTP2_RST_STREAM, 0))
return T_OK;
return tfw_h2_stream_close(ctx, hdr->stream_id, &ctx->cur_stream,
HTTP2_ECODE_PROTO);
}
static inline void
tfw_h2_rst_stream_process(TfwH2Ctx *ctx)
{
BUG_ON(!ctx->cur_stream);
T_DBG3("%s: parsed, stream_id=%u, stream=[%p], err_code=%u\n",
__func__, ctx->hdr.stream_id, ctx->cur_stream,
ntohl(*(unsigned int *)ctx->rbuf));
tfw_h2_current_stream_remove(ctx);
}
static int
tfw_h2_apply_settings_entry(TfwH2Ctx *ctx, unsigned short id,
unsigned int val)
{
TfwSettings *dest = &ctx->rsettings;
switch (id) {
case HTTP2_SETTINGS_TABLE_SIZE:
dest->hdr_tbl_sz = min_t(unsigned int,
val, HPACK_ENC_TABLE_MAX_SIZE);
tfw_hpack_set_rbuf_size(&ctx->hpack.enc_tbl, dest->hdr_tbl_sz);
break;
case HTTP2_SETTINGS_ENABLE_PUSH:
if (val > 1)
return T_BAD;
dest->push = val;
break;
case HTTP2_SETTINGS_MAX_STREAMS:
dest->max_streams = val;
break;
case HTTP2_SETTINGS_INIT_WND_SIZE:
if (val > MAX_WND_SIZE)
return T_BAD;
dest->wnd_sz = val;
break;
case HTTP2_SETTINGS_MAX_FRAME_SIZE:
if (val < FRAME_DEF_LENGTH || val > FRAME_MAX_LENGTH)
return T_BAD;
dest->max_frame_sz = val;
break;
case HTTP2_SETTINGS_MAX_HDR_LIST_SIZE:
dest->max_lhdr_sz = val;
break;
default:
/*
* We should silently ignore unknown identifiers (see
* RFC 7540 section 6.5.2)
*/
return T_OK;
}
return T_OK;
}
static void
tfw_h2_settings_ack_process(TfwH2Ctx *ctx)
{
T_DBG3("%s: parsed, stream_id=%u, flags=%hhu\n", __func__,
ctx->hdr.stream_id, ctx->hdr.flags);
ctx->hpack.max_window = ctx->lsettings.hdr_tbl_sz;
/*
* TODO: apply other local settings on ACK receiving.
*/
}
static int
tfw_h2_settings_process(TfwH2Ctx *ctx)
{
TfwFrameHdr *hdr = &ctx->hdr;
unsigned short id = ntohs(*(unsigned short *)&ctx->rbuf[0]);
unsigned int val = ntohl(*(unsigned int *)&ctx->rbuf[2]);
T_DBG3("%s: entry parsed, id=%hu, val=%u\n", __func__, id, val);
if (tfw_h2_apply_settings_entry(ctx, id, val))
return T_BAD;
ctx->to_read = hdr->length ? FRAME_SETTINGS_ENTRY_SIZE : 0;
hdr->length -= ctx->to_read;