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