]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/f-util.c
Filter: renaming pointers for consistency
[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"
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
20const char *
21filter_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
63f49457 33struct filter *f_new_where(struct f_inst *where)
9b46748d
MM
34{
35 struct f_inst acc = {
36 .fi_code = FI_PRINT_AND_DIE,
37 .lineno = ifs->lino,
4f082dfa 38 .size = 1,
9b46748d
MM
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,
4f082dfa 45 .size = 1,
9b46748d
MM
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,
f249d0b8 52 .size = 3 + where->size,
9b46748d
MM
53 .i_FI_CONDITION = {
54 .f1 = where,
55 .f2 = &acc,
56 .f3 = &rej,
57 },
58 };
59
f249d0b8 60 struct filter *f = cfg_allocz(sizeof(struct filter));
23e3b1e6 61 f->root = f_linearize(&i);
9b46748d
MM
62 return f;
63}
64
265419a3
MM
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
71struct ca_storage {
72 struct ca_storage *next;
73 struct f_dynamic_attr fda;
74 u32 uc;
75 char name[0];
76};
77
78HASH(struct ca_storage) ca_hash;
79
80static struct idm ca_idm;
81static struct ca_storage **ca_storage;
82static uint ca_storage_max;
83
84static void
85ca_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
102static void
103ca_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
110static 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
119struct custom_attribute *
120ca_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);
4c553c5a 177 cas->fda = f_new_dynamic_attr(ea_type, 0, f_type, EA_CUSTOM(id));
265419a3
MM
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
192const char *
193ea_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