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