]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/f-util.c
Filters: split the large filter.h file to smaller files.
[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 #include "filter/f-inst.h"
14 #include "lib/idm.h"
15 #include "nest/protocol.h"
16 #include "nest/route.h"
17
18 #define P(a,b) ((a<<8) | b)
19
20 static const char * const f_instruction_name_str[] = {
21 #define F(c,...) \
22 [c] = #c,
23 FI__LIST
24 #undef F
25 };
26
27 const char *
28 f_instruction_name(enum f_instruction_code fi)
29 {
30 if (fi < FI__MAX)
31 return f_instruction_name_str[fi];
32 else
33 bug("Got unknown instruction code: %d", fi);
34 }
35
36 char *
37 filter_name(struct filter *filter)
38 {
39 if (!filter)
40 return "ACCEPT";
41 else if (filter == FILTER_REJECT)
42 return "REJECT";
43 else if (!filter->name)
44 return "(unnamed)";
45 else
46 return filter->name;
47 }
48
49 void f_inst_next(struct f_inst *first, const struct f_inst *append)
50 {
51 first->next = append;
52 }
53
54 struct filter *f_new_where(const struct f_inst *where)
55 {
56 struct f_inst acc = {
57 .fi_code = FI_PRINT_AND_DIE,
58 .lineno = ifs->lino,
59 .i_FI_PRINT_AND_DIE = { .fret = F_ACCEPT, },
60 };
61
62 struct f_inst rej = {
63 .fi_code = FI_PRINT_AND_DIE,
64 .lineno = ifs->lino,
65 .i_FI_PRINT_AND_DIE = { .fret = F_REJECT, },
66 };
67
68 struct f_inst i = {
69 .fi_code = FI_CONDITION,
70 .lineno = ifs->lino,
71 .i_FI_CONDITION = {
72 .f1 = where,
73 .f2 = &acc,
74 .f3 = &rej,
75 },
76 };
77
78 struct filter *f = cfg_alloc(sizeof(struct filter));
79 f->name = NULL;
80 f->root = f_postfixify(&i);
81 return f;
82 }
83
84 struct f_inst *f_clear_local_vars(struct f_inst *decls)
85 {
86 /* Prepend instructions to clear local variables */
87 struct f_inst *head = NULL;
88
89 for (const struct f_inst *si = decls; si; si = si->next) {
90 struct f_inst *cur = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID });
91 if (head)
92 f_inst_next(cur, head);
93 else
94 f_inst_next(cur, si);
95 head = cur; /* The first FI_CONSTANT put there */
96 }
97
98 return head;
99 }
100
101 #define CA_KEY(n) n->name, n->fda.type
102 #define CA_NEXT(n) n->next
103 #define CA_EQ(na,ta,nb,tb) (!strcmp(na,nb) && (ta == tb))
104 #define CA_FN(n,t) (mem_hash(n, strlen(n)) ^ (t*0xaae99453U))
105 #define CA_ORDER 8 /* Fixed */
106
107 struct ca_storage {
108 struct ca_storage *next;
109 struct f_dynamic_attr fda;
110 u32 uc;
111 char name[0];
112 };
113
114 HASH(struct ca_storage) ca_hash;
115
116 static struct idm ca_idm;
117 static struct ca_storage **ca_storage;
118 static uint ca_storage_max;
119
120 static void
121 ca_free(resource *r)
122 {
123 struct custom_attribute *ca = (void *) r;
124 struct ca_storage *cas = HASH_FIND(ca_hash, CA, ca->name, ca->fda->type);
125 ASSERT(cas);
126
127 ca->name = NULL;
128 ca->fda = NULL;
129 if (!--cas->uc) {
130 uint id = EA_CUSTOM_ID(cas->fda.ea_code);
131 idm_free(&ca_idm, id);
132 HASH_REMOVE(ca_hash, CA, cas);
133 ca_storage[id] = NULL;
134 mb_free(cas);
135 }
136 }
137
138 static void
139 ca_dump(resource *r)
140 {
141 struct custom_attribute *ca = (void *) r;
142 debug("name \"%s\" id 0x%04x ea_type 0x%02x f_type 0x%02x\n",
143 ca->name, ca->fda->ea_code, ca->fda->type, ca->fda->f_type);
144 }
145
146 static struct resclass ca_class = {
147 .name = "Custom attribute",
148 .size = sizeof(struct custom_attribute),
149 .free = ca_free,
150 .dump = ca_dump,
151 .lookup = NULL,
152 .memsize = NULL,
153 };
154
155 struct custom_attribute *
156 ca_lookup(pool *p, const char *name, int f_type)
157 {
158 int ea_type;
159
160 switch (f_type) {
161 case T_INT:
162 ea_type = EAF_TYPE_INT;
163 break;
164 case T_IP:
165 ea_type = EAF_TYPE_IP_ADDRESS;
166 break;
167 case T_QUAD:
168 ea_type = EAF_TYPE_ROUTER_ID;
169 break;
170 case T_PATH:
171 ea_type = EAF_TYPE_AS_PATH;
172 break;
173 case T_CLIST:
174 ea_type = EAF_TYPE_INT_SET;
175 break;
176 case T_ECLIST:
177 ea_type = EAF_TYPE_EC_SET;
178 break;
179 case T_LCLIST:
180 ea_type = EAF_TYPE_LC_SET;
181 break;
182 default:
183 cf_error("Custom route attribute of unsupported type");
184 }
185
186 static int inited = 0;
187 if (!inited) {
188 idm_init(&ca_idm, &root_pool, 8);
189 HASH_INIT(ca_hash, &root_pool, CA_ORDER);
190
191 ca_storage_max = 256;
192 ca_storage = mb_allocz(&root_pool, sizeof(struct ca_storage *) * ca_storage_max);
193
194 inited++;
195 }
196
197 struct ca_storage *cas = HASH_FIND(ca_hash, CA, name, ea_type);
198 if (cas) {
199 cas->uc++;
200 } else {
201
202 uint id = idm_alloc(&ca_idm);
203
204 if (id >= EA_CUSTOM_BIT)
205 cf_error("Too many custom attributes.");
206
207 if (id >= ca_storage_max) {
208 ca_storage_max *= 2;
209 ca_storage = mb_realloc(ca_storage, sizeof(struct ca_storage *) * ca_storage_max * 2);
210 }
211
212 cas = mb_allocz(&root_pool, sizeof(struct ca_storage) + strlen(name) + 1);
213 cas->fda = f_new_dynamic_attr(ea_type, 0, f_type, EA_CUSTOM(id));
214 cas->uc = 1;
215
216 strcpy(cas->name, name);
217 ca_storage[id] = cas;
218
219 HASH_INSERT(ca_hash, CA, cas);
220 }
221
222 struct custom_attribute *ca = ralloc(p, &ca_class);
223 ca->fda = &(cas->fda);
224 ca->name = cas->name;
225 return ca;
226 }
227
228 const char *
229 ea_custom_name(uint ea)
230 {
231 uint id = EA_CUSTOM_ID(ea);
232 if (id >= ca_storage_max)
233 return NULL;
234
235 if (!ca_storage[id])
236 return NULL;
237
238 return ca_storage[id]->name;
239 }
240