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