]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/f-util.c
Hash: mem_hash doesn't modify the memory, declared constant
[thirdparty/bird.git] / filter / f-util.c
CommitLineData
b9d70dc8
PM
1/*
2 * Filters: utility functions
3 *
4 * Copyright 1998 Pavel Machek <pavel@ucw.cz>
5a14df39 5 * 2017 Jan Maria Matejka <mq@ucw.cz>
b9d70dc8
PM
6 *
7 * Can be freely distributed and used under the terms of the GNU GPL.
8 */
9
b9d70dc8 10#include "nest/bird.h"
b9d70dc8
PM
11#include "conf/conf.h"
12#include "filter/filter.h"
13
7d6eebae
PM
14#define P(a,b) ((a<<8) | b)
15
b7005824 16struct f_inst *
5a14df39 17f_new_inst(enum f_instruction_code fi_code)
84c7e194 18{
b7005824 19 struct f_inst * ret;
5a14df39
MJM
20 ret = cfg_allocz(sizeof(struct f_inst));
21 ret->fi_code = fi_code;
4be266a9 22 ret->lineno = ifs->lino;
84c7e194
PM
23 return ret;
24}
25
db1326aa 26struct f_inst *
5a14df39 27f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da)
db1326aa 28{
5a14df39 29 struct f_inst *ret = f_new_inst(fi_code);
d1ba927b 30 ret->aux = (da.f_type << 8) | da.type;
5a14df39
MJM
31 ret->a2.i = da.ea_code;
32 return ret;
33}
34
35struct f_inst *
36f_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;
db1326aa
MM
43}
44
7d6eebae
PM
45/*
46 * Generate set_dynamic( operation( get_dynamic(), argument ) )
47 */
48struct f_inst *
5a14df39 49f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument)
7d6eebae 50{
5a14df39
MJM
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);
7d6eebae 54
7d6eebae
PM
55 oper->aux = operation_aux;
56 oper->a1.p = get_dyn;
57 oper->a2.p = argument;
5a14df39 58
7d6eebae
PM
59 set_dyn->a1.p = oper;
60 return set_dyn;
61}
62
af582c48 63struct f_inst *
0264ccf6 64f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn)
af582c48
OZ
65{
66 struct f_inst_roa_check *ret = cfg_allocz(sizeof(struct f_inst_roa_check));
5a14df39 67 ret->i.fi_code = FI_ROA_CHECK;
4be266a9 68 ret->i.lineno = ifs->lino;
af582c48
OZ
69 ret->i.arg1 = prefix;
70 ret->i.arg2 = asn;
71 /* prefix == NULL <-> asn == NULL */
72
0264ccf6
PT
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;
af582c48
OZ
76
77 return &ret->i;
78}
79
cff9e937
JMM
80static const char * const f_instruction_name_str[] = {
81#define F(c,a,b) \
82 [c] = #c,
83FI__LIST
84#undef F
85};
86
87const char *
88f_instruction_name(enum f_instruction_code fi)
89{
90 if (fi < FI__MAX)
91 return f_instruction_name_str[fi];
92 else
93 bug("Got unknown instruction code: %d", fi);
94}
95
63a381db
MM
96char *
97filter_name(struct filter *filter)
98{
99 if (!filter)
100 return "ACCEPT";
101 else if (filter == FILTER_REJECT)
102 return "REJECT";
8796a8a5
OZ
103 else if (!filter->name)
104 return "(unnamed)";
63a381db
MM
105 else
106 return filter->name;
107}