]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/f-util.c
Merge branch 'int-new' of ssh://gitlab.labs.nic.cz/labs/bird into int-new
[thirdparty/bird.git] / filter / f-util.c
1 /*
2 * Filters: utility functions
3 *
4 * Copyright 1998 Pavel Machek <pavel@ucw.cz>
5 * 2017 Jan Maria Matejka <mq@ucw.cz>
6 *
7 * Can be freely distributed and used under the terms of the GNU GPL.
8 */
9
10 #include "nest/bird.h"
11 #include "conf/conf.h"
12 #include "filter/filter.h"
13
14 #define P(a,b) ((a<<8) | b)
15
16 struct f_inst *
17 f_new_inst(enum f_instruction_code fi_code)
18 {
19 struct f_inst * ret;
20 ret = cfg_allocz(sizeof(struct f_inst));
21 ret->fi_code = fi_code;
22 ret->lineno = ifs->lino;
23 return ret;
24 }
25
26 struct f_inst *
27 f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da)
28 {
29 struct f_inst *ret = f_new_inst(fi_code);
30 ret->aux = (da.f_type << 8) | da.type;
31 ret->a2.i = da.ea_code;
32 return ret;
33 }
34
35 struct f_inst *
36 f_new_inst_sa(enum f_instruction_code fi_code, struct f_static_attr sa)
37 {
38 struct f_inst *ret = f_new_inst(fi_code);
39 ret->aux = sa.f_type;
40 ret->a2.i = sa.sa_code;
41 ret->a1.i = sa.readonly;
42 return ret;
43 }
44
45 /*
46 * Generate set_dynamic( operation( get_dynamic(), argument ) )
47 */
48 struct f_inst *
49 f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument)
50 {
51 struct f_inst *set_dyn = f_new_inst_da(FI_EA_SET, da),
52 *oper = f_new_inst(operation),
53 *get_dyn = f_new_inst_da(FI_EA_GET, da);
54
55 oper->aux = operation_aux;
56 oper->a1.p = get_dyn;
57 oper->a2.p = argument;
58
59 set_dyn->a1.p = oper;
60 return set_dyn;
61 }
62
63 struct f_inst *
64 f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn)
65 {
66 struct f_inst_roa_check *ret = cfg_allocz(sizeof(struct f_inst_roa_check));
67 ret->i.fi_code = FI_ROA_CHECK;
68 ret->i.lineno = ifs->lino;
69 ret->i.arg1 = prefix;
70 ret->i.arg2 = asn;
71 /* prefix == NULL <-> asn == NULL */
72
73 if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
74 cf_error("%s is not a ROA table", table->name);
75 ret->rtc = table;
76
77 return &ret->i;
78 }
79
80 char *
81 filter_name(struct filter *filter)
82 {
83 if (!filter)
84 return "ACCEPT";
85 else if (filter == FILTER_REJECT)
86 return "REJECT";
87 else if (!filter->name)
88 return "(unnamed)";
89 else
90 return filter->name;
91 }