]> git.proxmox.com Git - mirror_frr.git/blob - lib/if_rmap.c
doc: add instructions for static linking (#5668)
[mirror_frr.git] / lib / if_rmap.c
1 /* route-map for interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "hash.h"
24 #include "command.h"
25 #include "memory.h"
26 #include "if.h"
27 #include "if_rmap.h"
28
29 DEFINE_MTYPE_STATIC(LIB, IF_RMAP_CTX, "Interface route map container")
30 DEFINE_MTYPE_STATIC(LIB, IF_RMAP_CTX_NAME, "Interface route map container name")
31 DEFINE_MTYPE_STATIC(LIB, IF_RMAP, "Interface route map")
32 DEFINE_MTYPE_STATIC(LIB, IF_RMAP_NAME, "I.f. route map name")
33
34 static struct list *if_rmap_ctx_list;
35
36 static struct if_rmap *if_rmap_new(void)
37 {
38 struct if_rmap *new;
39
40 new = XCALLOC(MTYPE_IF_RMAP, sizeof(struct if_rmap));
41
42 return new;
43 }
44
45 static void if_rmap_free(struct if_rmap *if_rmap)
46 {
47 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->ifname);
48
49 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
50 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
51
52 XFREE(MTYPE_IF_RMAP, if_rmap);
53 }
54
55 struct if_rmap *if_rmap_lookup(struct if_rmap_ctx *ctx, const char *ifname)
56 {
57 struct if_rmap key;
58 struct if_rmap *if_rmap;
59
60 /* temporary copy */
61 key.ifname = (ifname) ? XSTRDUP(MTYPE_IF_RMAP_NAME, ifname) : NULL;
62
63 if_rmap = hash_lookup(ctx->ifrmaphash, &key);
64
65 XFREE(MTYPE_IF_RMAP_NAME, key.ifname);
66
67 return if_rmap;
68 }
69
70 void if_rmap_hook_add(struct if_rmap_ctx *ctx,
71 void (*func)(struct if_rmap_ctx *ctx,
72 struct if_rmap *))
73 {
74 ctx->if_rmap_add_hook = func;
75 }
76
77 void if_rmap_hook_delete(struct if_rmap_ctx *ctx,
78 void (*func)(struct if_rmap_ctx *ctx,
79 struct if_rmap *))
80 {
81 ctx->if_rmap_delete_hook = func;
82 }
83
84 static void *if_rmap_hash_alloc(void *arg)
85 {
86 struct if_rmap *ifarg = (struct if_rmap *)arg;
87 struct if_rmap *if_rmap;
88
89 if_rmap = if_rmap_new();
90 if_rmap->ifname = XSTRDUP(MTYPE_IF_RMAP_NAME, ifarg->ifname);
91
92 return if_rmap;
93 }
94
95 static struct if_rmap *if_rmap_get(struct if_rmap_ctx *ctx, const char *ifname)
96 {
97 struct if_rmap key;
98 struct if_rmap *ret;
99
100 /* temporary copy */
101 key.ifname = (ifname) ? XSTRDUP(MTYPE_IF_RMAP_NAME, ifname) : NULL;
102
103 ret = hash_get(ctx->ifrmaphash, &key, if_rmap_hash_alloc);
104
105 XFREE(MTYPE_IF_RMAP_NAME, key.ifname);
106
107 return ret;
108 }
109
110 static unsigned int if_rmap_hash_make(const void *data)
111 {
112 const struct if_rmap *if_rmap = data;
113
114 return string_hash_make(if_rmap->ifname);
115 }
116
117 static bool if_rmap_hash_cmp(const void *arg1, const void *arg2)
118 {
119 const struct if_rmap *if_rmap1 = arg1;
120 const struct if_rmap *if_rmap2 = arg2;
121
122 return strcmp(if_rmap1->ifname, if_rmap2->ifname) == 0;
123 }
124
125 static struct if_rmap *if_rmap_set(struct if_rmap_ctx *ctx,
126 const char *ifname, enum if_rmap_type type,
127 const char *routemap_name)
128 {
129 struct if_rmap *if_rmap;
130
131 if_rmap = if_rmap_get(ctx, ifname);
132
133 if (type == IF_RMAP_IN) {
134 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
135 if_rmap->routemap[IF_RMAP_IN] =
136 XSTRDUP(MTYPE_IF_RMAP_NAME, routemap_name);
137 }
138 if (type == IF_RMAP_OUT) {
139 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
140 if_rmap->routemap[IF_RMAP_OUT] =
141 XSTRDUP(MTYPE_IF_RMAP_NAME, routemap_name);
142 }
143
144 if (ctx->if_rmap_add_hook)
145 (ctx->if_rmap_add_hook)(ctx, if_rmap);
146
147 return if_rmap;
148 }
149
150 static int if_rmap_unset(struct if_rmap_ctx *ctx,
151 const char *ifname, enum if_rmap_type type,
152 const char *routemap_name)
153 {
154 struct if_rmap *if_rmap;
155
156 if_rmap = if_rmap_lookup(ctx, ifname);
157 if (!if_rmap)
158 return 0;
159
160 if (type == IF_RMAP_IN) {
161 if (!if_rmap->routemap[IF_RMAP_IN])
162 return 0;
163 if (strcmp(if_rmap->routemap[IF_RMAP_IN], routemap_name) != 0)
164 return 0;
165
166 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
167 if_rmap->routemap[IF_RMAP_IN] = NULL;
168 }
169
170 if (type == IF_RMAP_OUT) {
171 if (!if_rmap->routemap[IF_RMAP_OUT])
172 return 0;
173 if (strcmp(if_rmap->routemap[IF_RMAP_OUT], routemap_name) != 0)
174 return 0;
175
176 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
177 if_rmap->routemap[IF_RMAP_OUT] = NULL;
178 }
179
180 if (ctx->if_rmap_delete_hook)
181 ctx->if_rmap_delete_hook(ctx, if_rmap);
182
183 if (if_rmap->routemap[IF_RMAP_IN] == NULL
184 && if_rmap->routemap[IF_RMAP_OUT] == NULL) {
185 hash_release(ctx->ifrmaphash, if_rmap);
186 if_rmap_free(if_rmap);
187 }
188
189 return 1;
190 }
191
192 DEFUN (if_rmap,
193 if_rmap_cmd,
194 "route-map RMAP_NAME <in|out> IFNAME",
195 "Route map set\n"
196 "Route map name\n"
197 "Route map set for input filtering\n"
198 "Route map set for output filtering\n"
199 "Route map interface name\n")
200 {
201 int idx_rmap_name = 1;
202 int idx_in_out = 2;
203 int idx_ifname = 3;
204 enum if_rmap_type type;
205 struct if_rmap_ctx *ctx =
206 (struct if_rmap_ctx *)listnode_head(if_rmap_ctx_list);
207
208 if (strncmp(argv[idx_in_out]->text, "in", 1) == 0)
209 type = IF_RMAP_IN;
210 else if (strncmp(argv[idx_in_out]->text, "out", 1) == 0)
211 type = IF_RMAP_OUT;
212 else {
213 vty_out(vty, "route-map direction must be [in|out]\n");
214 return CMD_WARNING_CONFIG_FAILED;
215 }
216
217 if_rmap_set(ctx, argv[idx_ifname]->arg,
218 type, argv[idx_rmap_name]->arg);
219
220 return CMD_SUCCESS;
221 }
222
223 DEFUN (no_if_rmap,
224 no_if_rmap_cmd,
225 "no route-map ROUTEMAP_NAME <in|out> IFNAME",
226 NO_STR
227 "Route map unset\n"
228 "Route map name\n"
229 "Route map for input filtering\n"
230 "Route map for output filtering\n"
231 "Route map interface name\n")
232 {
233 int idx_routemap_name = 2;
234 int idx_in_out = 3;
235 int idx_ifname = 4;
236 int ret;
237 enum if_rmap_type type;
238 struct if_rmap_ctx *ctx =
239 (struct if_rmap_ctx *)listnode_head(if_rmap_ctx_list);
240
241 if (strncmp(argv[idx_in_out]->arg, "i", 1) == 0)
242 type = IF_RMAP_IN;
243 else if (strncmp(argv[idx_in_out]->arg, "o", 1) == 0)
244 type = IF_RMAP_OUT;
245 else {
246 vty_out(vty, "route-map direction must be [in|out]\n");
247 return CMD_WARNING_CONFIG_FAILED;
248 }
249
250 ret = if_rmap_unset(ctx, argv[idx_ifname]->arg, type,
251 argv[idx_routemap_name]->arg);
252 if (!ret) {
253 vty_out(vty, "route-map doesn't exist\n");
254 return CMD_WARNING_CONFIG_FAILED;
255 }
256 return CMD_SUCCESS;
257 }
258
259
260 /* Configuration write function. */
261 int config_write_if_rmap(struct vty *vty,
262 struct if_rmap_ctx *ctx)
263 {
264 unsigned int i;
265 struct hash_bucket *mp;
266 int write = 0;
267 struct hash *ifrmaphash = ctx->ifrmaphash;
268
269 for (i = 0; i < ifrmaphash->size; i++)
270 for (mp = ifrmaphash->index[i]; mp; mp = mp->next) {
271 struct if_rmap *if_rmap;
272
273 if_rmap = mp->data;
274
275 if (if_rmap->routemap[IF_RMAP_IN]) {
276 vty_out(vty, " route-map %s in %s\n",
277 if_rmap->routemap[IF_RMAP_IN],
278 if_rmap->ifname);
279 write++;
280 }
281
282 if (if_rmap->routemap[IF_RMAP_OUT]) {
283 vty_out(vty, " route-map %s out %s\n",
284 if_rmap->routemap[IF_RMAP_OUT],
285 if_rmap->ifname);
286 write++;
287 }
288 }
289 return write;
290 }
291
292 void if_rmap_ctx_delete(struct if_rmap_ctx *ctx)
293 {
294 listnode_delete(if_rmap_ctx_list, ctx);
295 hash_clean(ctx->ifrmaphash, (void (*)(void *))if_rmap_free);
296 if (ctx->name)
297 XFREE(MTYPE_IF_RMAP_CTX_NAME, ctx);
298 XFREE(MTYPE_IF_RMAP_CTX, ctx);
299 }
300
301 /* name is optional: either vrf name, or other */
302 struct if_rmap_ctx *if_rmap_ctx_create(const char *name)
303 {
304 struct if_rmap_ctx *ctx;
305
306 ctx = XCALLOC(MTYPE_IF_RMAP_CTX, sizeof(struct if_rmap_ctx));
307
308 if (ctx->name)
309 ctx->name = XSTRDUP(MTYPE_IF_RMAP_CTX_NAME, name);
310 ctx->ifrmaphash = hash_create_size(4, if_rmap_hash_make, if_rmap_hash_cmp,
311 "Interface Route-Map Hash");
312 if (!if_rmap_ctx_list)
313 if_rmap_ctx_list = list_new();
314 listnode_add(if_rmap_ctx_list, ctx);
315 return ctx;
316 }
317
318 void if_rmap_init(int node)
319 {
320 if (node == RIPNG_NODE) {
321 } else if (node == RIP_NODE) {
322 install_element(RIP_NODE, &if_rmap_cmd);
323 install_element(RIP_NODE, &no_if_rmap_cmd);
324 }
325 if_rmap_ctx_list = list_new();
326 }
327
328 void if_rmap_terminate(void)
329 {
330 if (!if_rmap_ctx_list)
331 return;
332 list_delete(&if_rmap_ctx_list);
333 }