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