-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathcfg.c
2023 lines (1714 loc) · 49.6 KB
/
cfg.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
*
* Tempesta FW Configuration Framework.
*
* Requirements:
* - The configuring process must be habitual for any system administrator.
* - An ability to specify relatively complex configuration entities
* (lists, dictionaries, trees, etc).
* - Decomposition into modules. Other Tempesta subsystems should be able to
* register their sections in a configuration file. That should be possible
* for other kernel modules as well, so late binding has to be used.
* - Configuration refresh in run time (at least partially).
* - An ability to manage very large lists (e.g. blocked IP addresses).
*
* None of existing approaches (sysfs, configfs, sysctl, ioctl) mets all the
* requirements, so we implement our own configuration subsystem. Current
* implementation doesn't fit all requirements either, but it is flexible
* enough for future extensions.
*
* Basically, we store configuration in plain-text files and read them via VFS
* and parse right in the kernel space. This is not very conventional, but
* allows to pass relatively complex data structures to the kernel.
*
* The configuration looks like this:
* entry1 42;
* entry2 1 2 3 foo=bar;
* entry3 {
* sub_entry1;
* sub_entry2;
* }
* entry4 with_value {
* and_subentries {
* and_subsubentries;
* }
* }
* It consists of entries. Each entry has:
* 1. name;
* 2. values (usually just one, but variable number of values is supported);
* 3. attributes (a dictionary of key-value pairs);
* 4. children entries (such entries act as sections or trees);
* The only name is required. Everything else is optional. The idea is similar
* to SDL (http://www.ikayzo.org/display/SDL/Language+Guide), but our syntax
* and terminology is more habitual for C/Linux programmers and users.
*
* Tempesta FW modules register themselves and provide configuration specs
* via TfwMod{} and TfwCfgSpec{} structures. The code here pushes events
* and parsed configuration via callbacks specified in these structures.
*
* The code in this unit contains four main entities:
* 1. The configuration parser.
* We utilize FSM approach for the parser. The code is divided into two
* FSMs: TFSM (tokenizer) and PFSM (the parser that produces entries).
* 2. A bunch of generic TfwCfgSpec->handler callbacks for the parser.
* 3. TfwMod{} list related routines, the top-level parsing routine.
* This part of code implements publishing start/stop events and parsed
* configuration data across modules.
* 4. The list of registered modules, VFS and sysctl helpers, kernel module
* parameters. The stateful part of code.
*
* TODO:
* - "include" directives.
* - Handling large sets of data, possibly via TDB.
* - Improve efficiency: too many memory allocations and data copying.
*
* Copyright (C) 2014 NatSys Lab. ([email protected]).
* Copyright (C) 2015-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.
*/
#include <linux/ctype.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>
#undef DEBUG
#if DBG_CFG > 0
#define DEBUG DBG_CFG
#endif
#include "addr.h"
#include "cfg.h"
#include "client.h"
#include "log.h"
/*
* ------------------------------------------------------------------------
* Configuration Parser - TfwCfgEntry helpers
* ------------------------------------------------------------------------
*
* TfwCfgEntry is a temporary structure that servers only as an interface
* between the parser and TfwCfgSpec->handler callbacks.
* The parser walks over input entries accumulating data in the TfwCfgEntry
* structure. As soon as an entry is parsed, the parser invokes the handler
* callback and then destroys the TfwCfgEntry object.
*
* Strings in the TfwCfgEntry are pieces of the input plain-text configuration,
* but they have to be NULL-terminated, so we have to allocate space and copy
* them. Helpers below facilitate that.
*/
static const char *
__alloc_and_copy_literal(const char *src, size_t len, bool keep_bs)
{
const char *src_pos, *src_end;
char *dst, *dst_pos;
bool is_escaped;
BUG_ON(!src);
dst = kmalloc(len + 1, GFP_KERNEL);
if (!dst) {
T_ERR_NL("can't allocate memory\n");
return NULL;
}
/* Copy the string. Eat escaping backslashes if @keep_bs is not set. */
/* TODO: the logic looks like a tiny FSM,
* so perhaps it should be included to the TFSM. */
src_end = src + len;
src_pos = src;
dst_pos = dst;
is_escaped = false;
while (src_pos < src_end) {
if (*src_pos != '\\' || is_escaped || keep_bs) {
is_escaped = false;
*dst_pos = *src_pos;
++dst_pos;
}
else if (*src_pos == '\\') {
is_escaped = true;
}
++src_pos;
}
*dst_pos = '\0';
return dst;
}
static inline const char *
alloc_and_copy_literal(const char *src, size_t len)
{
return __alloc_and_copy_literal(src, len, false);
}
static inline const char *
alloc_and_copy_literal_bs(const char *src, size_t len)
{
return __alloc_and_copy_literal(src, len, true);
}
/**
* Check name of an attribute or name
*
* Much like C identifiers, names must start with a letter and consist
* only of alphanumeric and underscore characters. Currently this is
* only a sanity check and the parser code would work without it, but in
* future it may help to preserve compatibility if we decide to change
* the parser.
*/
static bool
check_identifier(const char *buf, size_t len)
{
size_t i;
if (!len) {
T_ERR_NL("the string is empty\n");
return false;
}
if ((len == 1) && (buf[0] == '*'))
return true;
if (!isalpha(buf[0])) {
T_ERR_NL("the first character is not a letter: '%c'\n", buf[0]);
return false;
}
for (i = 0; i < len; ++i) {
if (!isalnum(buf[i]) && buf[i] != '_') {
T_ERR_NL("invalid character: '%c' in '%.*s'\n",
buf[i], (int)len, buf);
return false;
}
}
return true;
}
static inline void
rule_reset(TfwCfgRule *rule)
{
kfree(rule->fst);
kfree(rule->fst_ext);
kfree(rule->snd);
kfree(rule->act);
kfree(rule->val);
}
static void
entry_reset(TfwCfgEntry *e)
{
const char *key, *val;
size_t i;
BUG_ON(!e);
kfree(e->name);
kfree(e->ftoken);
TFW_CFG_ENTRY_FOR_EACH_VAL(e, i, val)
kfree(val);
TFW_CFG_ENTRY_FOR_EACH_ATTR(e, i, key, val) {
kfree(key);
kfree(val);
}
rule_reset(&e->rule);
memset(e, 0, sizeof(*e));
}
static int
entry_set_name(TfwCfgEntry *e)
{
int len;
const char *name;
bool rule = !e->ftoken;
BUG_ON(!e);
BUG_ON(e->name);
if (!rule) {
name = e->ftoken;
len = strlen(e->ftoken);
} else {
name = TFW_CFG_RULE_NAME;
len = sizeof(TFW_CFG_RULE_NAME) - 1;
}
T_DBG3("set name: %.*s\n", len, name);
if (!check_identifier(name, len))
return -EINVAL;
if (!rule) {
e->name = e->ftoken;
e->ftoken = NULL;
return 0;
}
if (!(e->name = alloc_and_copy_literal(name, len)))
return -ENOMEM;
return 0;
}
static int
entry_set_first_token(TfwCfgEntry *e, const char *src, int len)
{
BUG_ON(!e);
BUG_ON(e->ftoken);
T_DBG3("set first token: %.*s\n", len, src);
if (!src || !len)
return -EINVAL;
e->ftoken = alloc_and_copy_literal(src, len);
if (!e->ftoken)
return -ENOMEM;
return 0;
}
static int
entry_add_val(TfwCfgEntry *e, const char *val_src, size_t val_len)
{
const char *val;
BUG_ON(!e);
BUG_ON(e->val_n > ARRAY_SIZE(e->vals));
if (!val_src)
return -EINVAL;
if (e->val_n == ARRAY_SIZE(e->vals)) {
T_ERR_NL("maximum number of values per entry reached\n");
return -ENOBUFS;
}
/* Store an incoming value even if it's an empty string. */
if (val_len)
val = alloc_and_copy_literal(val_src, val_len);
else
val = alloc_and_copy_literal("", 0);
if (!val)
return -ENOMEM;
e->vals[e->val_n++] = val;
return 0;
}
static int
entry_add_attr(TfwCfgEntry *e, const char *key_src, size_t key_len,
const char *val_src, size_t val_len)
{
const char *key, *val;
BUG_ON(!e);
BUG_ON(e->attr_n > ARRAY_SIZE(e->attrs));
if (!key_src || !key_len || !val_src || !val_len)
return -EINVAL;
if (e->attr_n == ARRAY_SIZE(e->attrs)) {
T_ERR_NL("maximum number of attributes per entry reached\n");
return -ENOBUFS;
}
if (!check_identifier(key_src, key_len))
return -EINVAL;
key = alloc_and_copy_literal(key_src, key_len);
val = alloc_and_copy_literal(val_src, val_len);
if (!key || !val) {
kfree(key);
kfree(val);
return -ENOMEM;
}
e->attrs[e->attr_n].key = key;
e->attrs[e->attr_n].val = val;
++e->attr_n;
return 0;
}
static int
entry_add_rule_param(const char **param, const char *src, size_t len)
{
const char *dst;
BUG_ON(!src);
if (!(dst = alloc_and_copy_literal(src, len)))
return -ENOMEM;
*param = dst;
return 0;
}
/*
* ------------------------------------------------------------------------
* Configuration parser - tokenizer and parser FSMs
* ------------------------------------------------------------------------
*
* Basic terms used in this code:
* - MOVE - change FSM state and read the next character/token.
* - JMP - change the state without reading anything.
* - SKIP - read the next character/token and re-enter the current state.
* - TURN - enter a new state (not re-enter the current state).
* - COND_JMP/COND_MOVE/COND_SKIP/etc - do it if the given condition is true.
* - lexeme - a sequence of characters in the input buffer.
* - token - type/class of a lexeme.
* - literal - a lexeme that carries a string value. Regular tokens are syntax
* elements, they don't have a value and their lexemes are always
* special control characters. Literals are not part of the syntax
* and they do have a value.
*
* Macro ownership:
* - FSM_*() - generic macros shared between PFSM and TFSM.
* - PFSM_*()/TFSM_*() - macros specific to parser/tokenizer.
* For example, FSM_STATE() and FSM_JMP() are generic, they do the same thing
* in both FSMs (a label and a jump to it); but PFSM_MOVE() and TFSM_MOVE() are
* different, since they read values from different input streams (tokens and
* characters respectively).
*/
typedef enum {
TOKEN_NA = 0,
TOKEN_LBRACE,
TOKEN_RBRACE,
TOKEN_EQSIGN,
TOKEN_DEQSIGN,
TOKEN_NEQSIGN,
TOKEN_SEMICOLON,
TOKEN_LITERAL,
TOKEN_ARROW,
_TOKEN_COUNT,
} token_t;
typedef struct {
const char *in; /* The whole input buffer. */
const char *pos; /* Current position in the @in buffer. */
/* Current FSM state is saved to here. */
const void *fsm_s; /* Pointer to label (GCC extension). */
const char *fsm_ss; /* Label name as string (for debugging). */
/* Currently/previously processed character. */
char c;
char prev_c;
/* Currently/previously processed token.
* The language is context-sensitive, so we need to store all these
* previous tokens and literals to parse it without peek()'ing. */
token_t t;
token_t prev_t;
/* Literal value (not NULL only when @t == TOKEN_LITERAL). */
const char *lit;
const char *prev_lit;
/* Length of @lit (the @lit is not terminated). */
int lit_len;
int prev_lit_len;
/* Current line. */
size_t line_no;
const char *line;
int err; /* The latest error code. */
/* Currently parsed entry. Accumulates literals as values/attributes.
* When current entry is done, a TfwCfgSpec->handler is called and a new
* entry is started. */
TfwCfgEntry e;
} TfwCfgParserState;
/* Macros common for both TFSM and PFSM. */
#define FSM_STATE(name) \
T_DBG3("fsm: implicit exit from: %s\n", ps->fsm_ss); \
BUG(); \
name: \
if (ps->fsm_s != &&name) { \
T_DBG3("fsm turn: %s -> %s\n", ps->fsm_ss, #name); \
ps->fsm_s = &&name; \
ps->fsm_ss = #name; \
}
#define FSM_JMP(to_state) goto to_state
#define FSM_COND_JMP(cond, to_state) \
FSM_COND_LAMBDA(cond, FSM_JMP(to_state))
#define FSM_COND_LAMBDA(cond, ...) \
do { \
if (cond) { \
__VA_ARGS__; \
} \
} while (0) \
/* Macros specific to TFSM. */
#define TFSM_MOVE(to_state) \
do { \
ps->prev_c = ps->c; \
ps->c = *(++ps->pos); \
T_DBG3("tfsm move: '%c' -> '%c'\n", ps->prev_c, ps->c); \
if (ps->prev_c == '\n') { \
++ps->line_no; \
ps->line = ps->pos; \
} \
FSM_JMP(to_state); \
} while (0)
#define TFSM_MOVE_EXIT(token_type) \
do { \
ps->t = token_type; \
TFSM_MOVE(TS_EXIT); \
} while (0)
#define TFSM_JMP_EXIT(token_type) \
do { \
ps->t = token_type; \
FSM_JMP(TS_EXIT); \
} while (0)
#define TFSM_SKIP() TFSM_MOVE(*ps->fsm_s);
#define TFSM_COND_SKIP(cond) \
FSM_COND_LAMBDA(cond, TFSM_SKIP())
#define TFSM_COND_MOVE_EXIT(cond, token_type) \
FSM_COND_LAMBDA(cond, TFSM_MOVE_EXIT(token_type))
#define TFSM_COND_JMP_EXIT(cond, token_type) \
FSM_COND_LAMBDA(cond, TFSM_JMP_EXIT(token_type))
#define TFSM_COND_MOVE(cond, to_state) \
FSM_COND_LAMBDA(cond, TFSM_MOVE(to_state))
/* Macros specific to PFSM. */
#define PFSM_MOVE(to_state) \
do { \
read_next_token(ps); \
T_DBG3("pfsm move: %d (\"%.*s\") -> %d (\"%.*s\")", \
ps->prev_t, ps->prev_lit_len, ps->prev_lit, \
ps->t, ps->lit_len, ps->lit); \
if(!ps->t) { \
ps->err = -EINVAL; \
FSM_JMP(PS_EXIT); \
} \
FSM_JMP(to_state); \
} while (0)
#define PFSM_COND_MOVE(cond, to_state) \
FSM_COND_LAMBDA(cond, PFSM_MOVE(to_state))
#define PFSM_COND_JMP_EXIT_ERROR(cond) \
do { \
if (cond) { \
T_DBG3("pfsm: rule error, %d -> %d", ps->prev_t, ps->t); \
ps->err = -EINVAL; \
FSM_JMP(PS_EXIT); \
} \
} while (0)
/**
* The TFSM (Tokenizer Finite State Machine).
*
* Steps over characters in the input stream and classifies them as tokens.
* Eats whitespace and comments automatically, never produces tokens for them.
* Accumulates string literals in @ps->lit.
* Produces one token per call (puts it to @ps->t), shifts current position
* accordingly. Produces TOKEN_NA on EOF or invalid input.
*/
static void
read_next_token(TfwCfgParserState *ps)
{
ps->prev_t = ps->t;
ps->prev_lit = ps->lit;
ps->prev_lit_len = ps->lit_len;
ps->lit = NULL;
ps->lit_len = 0;
ps->t = TOKEN_NA;
ps->c = *ps->pos;
T_DBG3("tfsm start, char: '%c', pos: %.20s\n", ps->c, ps->pos);
FSM_JMP(TS_START_NEW_TOKEN);
/* The next character is read at _TFSM_MOVE(), so we have a fresh
* character automatically whenever we enter a state. */
FSM_STATE(TS_START_NEW_TOKEN) {
TFSM_COND_JMP_EXIT(!ps->c, TOKEN_NA);
/* A backslash means that the next character definitely has
* no special meaning and thus starts a literal. */
FSM_COND_JMP(ps->c == '\\', TS_LITERAL_FIRST_CHAR);
/* Eat non-is_escaped spaces. */
TFSM_COND_SKIP(isspace(ps->c));
/* A character next to a double quote is the first character
* of a literal. The quote itself is not included to the
* literal's value. */
TFSM_COND_MOVE(ps->c == '"', TS_QUOTED_LITERAL_FIRST_CHAR);
/* A comment is starts with '#' (and ends with a like break) */
TFSM_COND_MOVE(ps->c == '#', TS_COMMENT);
/* Self-meaning single-character tokens. */
TFSM_COND_MOVE_EXIT(ps->c == '{', TOKEN_LBRACE);
TFSM_COND_MOVE_EXIT(ps->c == '}', TOKEN_RBRACE);
TFSM_COND_MOVE_EXIT(ps->c == ';', TOKEN_SEMICOLON);
/* Self-meaning double-character tokens. */
TFSM_COND_MOVE(ps->c == '!' || ps->c == '-', TS_DCHAR);
/* Special cases to determine double-character tokens during
* literals accumulating. */
TFSM_COND_MOVE_EXIT(ps->c == '=' && ps->prev_c == '!',
TOKEN_NEQSIGN);
TFSM_COND_MOVE_EXIT(ps->c == '>' && ps->prev_c == '-',
TOKEN_ARROW);
/* Special case to differ single equal sign from double one. */
TFSM_COND_MOVE(ps->c == '=', TS_EQSIGN);
/* Everything else is not a special character and therefore
* it starts a literal. */
FSM_JMP(TS_LITERAL_FIRST_CHAR);
}
FSM_STATE(TS_DCHAR) {
TFSM_COND_JMP_EXIT(!ps->c, TOKEN_NA);
/* Jump to literals accumulating, if '!=' or '->' tokens are
* not matched. */
TFSM_COND_MOVE_EXIT(ps->c == '=' && ps->prev_c == '!',
TOKEN_NEQSIGN);
TFSM_COND_MOVE_EXIT(ps->c == '>' && ps->prev_c == '-',
TOKEN_ARROW);
ps->lit = ps->pos - 1;
++ps->lit_len;
FSM_JMP(TS_LITERAL_ACCUMULATE);
}
FSM_STATE(TS_EQSIGN) {
TFSM_COND_JMP_EXIT(!ps->c, TOKEN_NA);
/* If this is double equal sign, eat second sign and exit. */
TFSM_COND_MOVE_EXIT(ps->c == '=', TOKEN_DEQSIGN);
TFSM_JMP_EXIT(TOKEN_EQSIGN);
}
FSM_STATE(TS_COMMENT) {
TFSM_COND_JMP_EXIT(!ps->c, TOKEN_NA);
/* Eat everything until a new line is reached.
* The line break cannot be is_escaped within a comment. */
TFSM_COND_SKIP(ps->c != '\n');
TFSM_MOVE(TS_START_NEW_TOKEN);
}
FSM_STATE(TS_LITERAL_FIRST_CHAR) {
ps->lit = ps->pos;
FSM_JMP(TS_LITERAL_ACCUMULATE);
}
FSM_STATE(TS_LITERAL_ACCUMULATE) {
/* EOF terminates a literal if there is any chars saved. */
TFSM_COND_JMP_EXIT(!ps->c && !ps->lit_len, TOKEN_NA);
TFSM_COND_JMP_EXIT(!ps->c && ps->lit_len, TOKEN_LITERAL);
/* Accumulate backslash together with any next character. */
TFSM_COND_MOVE(ps->c == '\\', TS_LITERAL_ACC_ESCAPE);
/* Non-escaped special characters terminate the literal. */
TFSM_COND_JMP_EXIT(isspace(ps->c), TOKEN_LITERAL);
TFSM_COND_JMP_EXIT(ps->c == '"', TOKEN_LITERAL);
TFSM_COND_JMP_EXIT(ps->c == '#', TOKEN_LITERAL);
TFSM_COND_JMP_EXIT(ps->c == '{', TOKEN_LITERAL);
TFSM_COND_JMP_EXIT(ps->c == '}', TOKEN_LITERAL);
TFSM_COND_JMP_EXIT(ps->c == ';', TOKEN_LITERAL);
TFSM_COND_JMP_EXIT(ps->c == '=', TOKEN_LITERAL);
/* Non-escaped first char of double-character special tokens. */
TFSM_COND_MOVE(ps->c == '-' || ps->c == '!',
TS_DOUBLE_CHARACTER_FIRST_CHAR);
/* Accumulate everything else. */
++ps->lit_len;
TFSM_SKIP();
}
FSM_STATE(TS_LITERAL_ACC_ESCAPE) {
ps->lit_len += 2;
TFSM_MOVE(TS_LITERAL_ACCUMULATE);
}
FSM_STATE(TS_QUOTED_LITERAL_FIRST_CHAR) {
ps->lit = ps->pos;
FSM_JMP(TS_QUOTED_LITERAL_ACCUMULATE);
}
FSM_STATE(TS_QUOTED_LITERAL_ACCUMULATE) {
/* EOF means there is no matching double quote. */
TFSM_COND_JMP_EXIT(!ps->c, TOKEN_NA);
/* A double quote terminates the literal,
* but it may be escaped with a backslash. */
TFSM_COND_MOVE(ps->c == '\\', TS_QUOTED_LIT_ACC_ESCAPE);
TFSM_COND_MOVE_EXIT(ps->c == '"', TOKEN_LITERAL);
/* Everything else is accumulated (including line breaks). */
++ps->lit_len;
TFSM_SKIP();
}
FSM_STATE(TS_QUOTED_LIT_ACC_ESCAPE) {
ps->lit_len += 2;
TFSM_MOVE(TS_QUOTED_LITERAL_ACCUMULATE);
}
FSM_STATE(TS_DOUBLE_CHARACTER_FIRST_CHAR) {
/* Check double-character tokens and continue accumulate
* literal, if not matched. */
TFSM_COND_JMP_EXIT(ps->c == '>' && ps->prev_c == '-',
TOKEN_LITERAL);
TFSM_COND_JMP_EXIT(ps->c == '=' && ps->prev_c == '!',
TOKEN_LITERAL);
++ps->lit_len;
FSM_JMP(TS_LITERAL_ACCUMULATE);
}
FSM_STATE(TS_EXIT) {
T_DBG3("tfsm exit: t: %d, lit: %.*s\n",
ps->t, ps->lit_len, ps->lit);
}
}
STACK_FRAME_NON_STANDARD(read_next_token);
static int
entry_set_cond(TfwCfgEntry *e, token_t cond_type, const char *src, int len)
{
const char *name = TFW_CFG_RULE_NAME;
int name_len = sizeof(TFW_CFG_RULE_NAME) - 1;
TfwCfgRule *rule = &e->rule;
BUG_ON(!e->ftoken);
BUG_ON(e->name);
T_DBG3("set entry rule name '%.*s', 1st operand '%.*s', 2nd operand"
" '%.*s', and condition type '%d'\n", name_len, name,
(int)strlen(e->ftoken), e->ftoken, len, src, cond_type);
if (!src || !len)
return -EINVAL;
rule->fst = e->ftoken;
e->ftoken = NULL;
if (!(rule->snd = alloc_and_copy_literal_bs(src, len)))
return -ENOMEM;
if (!(e->name = alloc_and_copy_literal(name, name_len)))
return -ENOMEM;
rule->inv = cond_type == TOKEN_DEQSIGN ? false : true;
return 0;
}
/**
* The PFSM (Parser Finite State Machine).
*
* Steps over a stream of tokens (produces by the TFSM), accumulates values
* in TfwCfgEntry and returns it when the input entry is terminated with ';'.
* Returns one entry at a time and shifts the input position accordingly.
* Should be called in a loop until NULL is returned.
*
* Doesn't recurse into nested entries.
* I.e. it doesn't fully parse this:
* entry1 {
* entry2;
* }
* Instead, it stops at the '{' character and the higher-level code has to use
* push-down automaton approach to parse the section between '{' and '}'.
* That is done because we are not going to complicate things here by building
* a large syntax tree and creating a DSL to query it.
*/
static void
parse_cfg_entry(TfwCfgParserState *ps)
{
T_DBG3("pfsm: start\n");
BUG_ON(ps->err);
/* Start of the input? Read the first token and start a new entry. */
if (ps->in == ps->pos) {
read_next_token(ps);
if (!ps->t)
FSM_JMP(PS_EXIT);
}
/* Continue: start a new entry at the current position. */
BUG_ON(!ps->t);
FSM_JMP(PS_START_NEW_ENTRY);
/* Every _PFSM_MOVE() invokes _read_next_token(), so when we enter
* any state, we get a new token automatically.
* Four different situations may occur here:
* 1. In case of plain directive parsing:
* name key = value;
* ^
* 2. In case of rule parsing:
* key == (!=) value -> action [= val]
* ^
* 3. In case of extended rule parsing:
* key key_ext == (!=) value -> action [= val]
* ^
* 4. In case of parsing of pure action rule:
* -> action [= val]
* ^
* current token is here; so at first we need to differentiate fourth
* situation, and in first three ones - save first token in special location
* to decide later whether use it as name for plain directive or as
* condition key for rule; in last three cases predefined rule name is used.
*/
FSM_STATE(PS_START_NEW_ENTRY) {
entry_reset(&ps->e);
ps->e.line_no = ps->line_no;
ps->e.line = ps->line;
PFSM_COND_MOVE(ps->t == TOKEN_ARROW, PS_RULE_PURE_ACTION);
ps->err = entry_set_first_token(&ps->e, ps->lit, ps->lit_len);
FSM_COND_JMP(ps->err, PS_EXIT);
PFSM_MOVE(PS_PLAIN_OR_RULE);
}
FSM_STATE(PS_PLAIN_OR_RULE) {
PFSM_COND_MOVE(ps->t == TOKEN_DEQSIGN ||
ps->t == TOKEN_NEQSIGN,
PS_RULE_COND);
PFSM_COND_MOVE(ps->t == TOKEN_LITERAL, PS_PLAIN_OR_LONG_RULE);
/* Jump to plain val/attr scheme to make remained checks
* for left brace and semicolon. */
ps->err = entry_set_name(&ps->e);
FSM_COND_JMP(ps->err, PS_EXIT);
FSM_JMP(PS_VAL_OR_ATTR);
}
FSM_STATE(PS_PLAIN_OR_LONG_RULE) {
FSM_COND_JMP(ps->t == TOKEN_DEQSIGN ||
ps->t == TOKEN_NEQSIGN,
PS_LONG_RULE_COND);
/* This is not rule (simple or extended), so jump to
* plain val/attr scheme. */
ps->err = entry_set_name(&ps->e);
FSM_COND_JMP(ps->err, PS_EXIT);
FSM_COND_JMP(ps->t == TOKEN_EQSIGN, PS_STORE_ATTR_PREV);
FSM_COND_JMP(ps->t == TOKEN_LITERAL ||
ps->t == TOKEN_SEMICOLON ||
ps->t == TOKEN_LBRACE,
PS_STORE_VAL_PREV);
ps->err = -EINVAL;
FSM_JMP(PS_EXIT);
}
FSM_STATE(PS_LONG_RULE_COND) {
ps->err = entry_add_rule_param(&ps->e.rule.fst_ext,
ps->prev_lit,
ps->prev_lit_len);
FSM_COND_JMP(ps->err, PS_EXIT);
PFSM_MOVE(PS_RULE_COND);
}
FSM_STATE(PS_RULE_COND) {
PFSM_COND_JMP_EXIT_ERROR(ps->t != TOKEN_LITERAL);
ps->err = entry_set_cond(&ps->e, ps->prev_t, ps->lit,
ps->lit_len);
FSM_COND_JMP(ps->err, PS_EXIT);
PFSM_MOVE(PS_RULE_COND_END);
}
FSM_STATE(PS_RULE_COND_END) {
PFSM_COND_JMP_EXIT_ERROR(ps->t != TOKEN_ARROW);
PFSM_MOVE(PS_RULE_ACTION);
}
FSM_STATE(PS_RULE_PURE_ACTION) {
ps->err = entry_set_name(&ps->e);
FSM_COND_JMP(ps->err, PS_EXIT);
FSM_JMP(PS_RULE_ACTION);
}
FSM_STATE(PS_RULE_ACTION) {
PFSM_COND_JMP_EXIT_ERROR(ps->t != TOKEN_LITERAL);
ps->err = entry_add_rule_param(&ps->e.rule.act, ps->lit,
ps->lit_len);
FSM_COND_JMP(ps->err, PS_EXIT);
PFSM_MOVE(PS_RULE_ACTION_VAL);
}
FSM_STATE(PS_RULE_ACTION_VAL) {
FSM_COND_JMP(ps->t == TOKEN_SEMICOLON, PS_SEMICOLON);
PFSM_COND_JMP_EXIT_ERROR(ps->t != TOKEN_EQSIGN);
read_next_token(ps);
PFSM_COND_JMP_EXIT_ERROR(ps->t != TOKEN_LITERAL);
ps->err = entry_add_rule_param(&ps->e.rule.val, ps->lit,
ps->lit_len);
FSM_COND_JMP(ps->err, PS_EXIT);
read_next_token(ps);
PFSM_COND_JMP_EXIT_ERROR(ps->t != TOKEN_SEMICOLON);
FSM_JMP(PS_SEMICOLON);
}
/*
* Now we have a situation where at current position we don't know
* whether we have a value or an attribute:
* name key = value;
* ^
* current position here
*
* An implementation of peek_token() would be tricky here because the
* TFSM is not pure (it alters the current state). So instead of looking
* forward, we move to the next position and look to the '=' sign:
* if it is there - then we treat previous value as an attribute name,
* otherwise we save it as a value of the current node.
*/
FSM_STATE(PS_VAL_OR_ATTR) {
PFSM_COND_MOVE(ps->t == TOKEN_LITERAL, PS_MAYBE_EQSIGN);
FSM_COND_JMP(ps->t == TOKEN_SEMICOLON, PS_SEMICOLON);
FSM_COND_JMP(ps->t == TOKEN_LBRACE, PS_LBRACE);
ps->err = -EINVAL;
FSM_JMP(PS_EXIT);
}
FSM_STATE(PS_MAYBE_EQSIGN) {
FSM_COND_JMP(ps->t == TOKEN_EQSIGN, PS_STORE_ATTR_PREV);
FSM_JMP(PS_STORE_VAL_PREV);
}
FSM_STATE(PS_STORE_VAL_PREV) {
/* name val1 val2;
* ^
* We are here (but still need to store val1). */
T_DBG3("add value: %.*s\n", ps->prev_lit_len, ps->prev_lit);
ps->err = entry_add_val(&ps->e, ps->prev_lit, ps->prev_lit_len);
FSM_COND_JMP(ps->err, PS_EXIT);
FSM_JMP(PS_VAL_OR_ATTR);
}
FSM_STATE(PS_STORE_ATTR_PREV) {
/* name key = val;
* ^
* We are here. */
const char *key, *val;
int key_len, val_len;
key = ps->prev_lit;
key_len = ps->prev_lit_len;
read_next_token(ps); /* eat '=' */
val = ps->lit;
val_len = ps->lit_len;
T_DBG3("add attr: %.*s = %.*s\n", key_len, key, val_len, val);
ps->err = entry_add_attr(&ps->e, key, key_len, val, val_len);
FSM_COND_JMP(ps->err, PS_EXIT);
PFSM_MOVE(PS_VAL_OR_ATTR);
}
FSM_STATE(PS_LBRACE) {
/* Simply exit on '{' leaving nested nodes untouched and
* surrounded with braces. The caller should detect it and parse
* them in a loop. */
ps->e.have_children = true;
FSM_JMP(PS_EXIT);
}
FSM_STATE(PS_SEMICOLON) {
/* Simply eat ';'. Don't MOVE because the next character may be
* '\0' and that triggers an error (because we expect more input
* tokens when we do _PFSM_MOVE()). */
read_next_token(ps);
FSM_JMP(PS_EXIT);
}
FSM_STATE(PS_EXIT) {
/* Cleanup of entry is done in tfw_cfg_parse_mods() */
T_DBG3("pfsm: exit\n");
}
}
/*
* ------------------------------------------------------------------------
* Configuration Parser - TfwCfgSpec helpers.
* ------------------------------------------------------------------------
*
* The configuration parsing is done slightly differently depending on the
* context (top-level vs recursing into children entries), but the TfwCfgSpec
* is handled in the same way in both cases. So the code below is the shared
* logic between these two cases.
*/
static TfwCfgSpec *
spec_find(TfwCfgSpec specs[], const char *name)
{
TfwCfgSpec *spec;
TFW_CFG_FOR_EACH_SPEC(spec, specs) {
if (!strcmp(spec->name, name))
return spec;
}
return NULL;
}
static int
__spec_start_handling(TfwCfgSpec *parent, TfwCfgSpec specs[])
{
TfwCfgSpec *spec;