]> git.proxmox.com Git - mirror_frr.git/blob - lib/if_rmap.c
5895924a5e90072e554624e84ea42ad6392263a0
[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 * Copyright (C) 2023 LabN Consulting, L.L.C.
5 */
6
7 #include <zebra.h>
8
9 #include "hash.h"
10 #include "command.h"
11 #include "memory.h"
12 #include "if.h"
13 #include "if_rmap.h"
14 #include "northbound_cli.h"
15
16 #include "lib/if_rmap_clippy.c"
17
18 DEFINE_MTYPE_STATIC(LIB, IF_RMAP_CTX, "Interface route map container");
19 DEFINE_MTYPE_STATIC(LIB, IF_RMAP_CTX_NAME,
20 "Interface route map container name");
21 DEFINE_MTYPE_STATIC(LIB, IF_RMAP, "Interface route map");
22 DEFINE_MTYPE_STATIC(LIB, IF_RMAP_NAME, "I.f. route map name");
23
24 static struct if_rmap *if_rmap_new(void)
25 {
26 struct if_rmap *new;
27
28 new = XCALLOC(MTYPE_IF_RMAP, sizeof(struct if_rmap));
29
30 return new;
31 }
32
33 static void if_rmap_free(struct if_rmap *if_rmap)
34 {
35 char *no_const_ifname = (char *)if_rmap->ifname;
36
37 XFREE(MTYPE_IF_RMAP_NAME, no_const_ifname);
38
39 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
40 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
41
42 XFREE(MTYPE_IF_RMAP, if_rmap);
43 }
44
45 struct if_rmap *if_rmap_lookup(struct if_rmap_ctx *ctx, const char *ifname)
46 {
47 struct if_rmap key = {.ifname = ifname};
48 struct if_rmap *if_rmap;
49
50 if_rmap = hash_lookup(ctx->ifrmaphash, &key);
51
52 return if_rmap;
53 }
54
55 void if_rmap_hook_add(struct if_rmap_ctx *ctx,
56 void (*func)(struct if_rmap_ctx *ctx, struct if_rmap *))
57 {
58 ctx->if_rmap_add_hook = func;
59 }
60
61 void if_rmap_hook_delete(struct if_rmap_ctx *ctx,
62 void (*func)(struct if_rmap_ctx *ctx,
63 struct if_rmap *))
64 {
65 ctx->if_rmap_delete_hook = func;
66 }
67
68 static void *if_rmap_hash_alloc(void *arg)
69 {
70 struct if_rmap *ifarg = (struct if_rmap *)arg;
71 struct if_rmap *if_rmap;
72
73 if_rmap = if_rmap_new();
74 if_rmap->ifname = XSTRDUP(MTYPE_IF_RMAP_NAME, ifarg->ifname);
75
76 return if_rmap;
77 }
78
79 static struct if_rmap *if_rmap_get(struct if_rmap_ctx *ctx, const char *ifname)
80 {
81 struct if_rmap key = {.ifname = ifname};
82 struct if_rmap *ret;
83
84 ret = hash_get(ctx->ifrmaphash, &key, if_rmap_hash_alloc);
85
86 return ret;
87 }
88
89 static unsigned int if_rmap_hash_make(const void *data)
90 {
91 const struct if_rmap *if_rmap = data;
92
93 return string_hash_make(if_rmap->ifname);
94 }
95
96 static bool if_rmap_hash_cmp(const void *arg1, const void *arg2)
97 {
98 const struct if_rmap *if_rmap1 = arg1;
99 const struct if_rmap *if_rmap2 = arg2;
100
101 return strcmp(if_rmap1->ifname, if_rmap2->ifname) == 0;
102 }
103
104 static void if_rmap_set(struct if_rmap_ctx *ctx, const char *ifname,
105 enum if_rmap_type type, const char *routemap_name)
106 {
107 struct if_rmap *if_rmap = if_rmap_get(ctx, ifname);
108
109 assert(type == IF_RMAP_IN || type == IF_RMAP_OUT);
110 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[type]);
111 if_rmap->routemap[type] = XSTRDUP(MTYPE_IF_RMAP_NAME, routemap_name);
112
113 if (ctx->if_rmap_add_hook)
114 (ctx->if_rmap_add_hook)(ctx, if_rmap);
115 }
116
117 static void if_rmap_unset(struct if_rmap_ctx *ctx, const char *ifname,
118 enum if_rmap_type type)
119 {
120 struct if_rmap *if_rmap = if_rmap_lookup(ctx, ifname);
121
122 if (!if_rmap)
123 return;
124
125 assert(type == IF_RMAP_IN || type == IF_RMAP_OUT);
126 if (!if_rmap->routemap[type])
127 return;
128
129 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[type]);
130
131 if (ctx->if_rmap_delete_hook)
132 ctx->if_rmap_delete_hook(ctx, if_rmap);
133
134 if (if_rmap->routemap[IF_RMAP_IN] == NULL &&
135 if_rmap->routemap[IF_RMAP_OUT] == NULL) {
136 hash_release(ctx->ifrmaphash, if_rmap);
137 if_rmap_free(if_rmap);
138 }
139 }
140
141 static int if_route_map_handler(struct vty *vty, bool no, const char *dir,
142 const char *other_dir, const char *ifname,
143 const char *route_map)
144 {
145 enum nb_operation op = no ? NB_OP_DESTROY : NB_OP_MODIFY;
146 const struct lyd_node *dnode;
147 char xpath[XPATH_MAXLEN];
148
149 if (!no) {
150 snprintf(
151 xpath, sizeof(xpath),
152 "./if-route-maps/if-route-map[interface='%s']/%s-route-map",
153 ifname, dir);
154 } else {
155 /*
156 * If we are deleting the last policy for this interface,
157 * (i.e., no `in` or `out` policy). delete the interface list
158 * node instead.
159 */
160 dnode = yang_dnode_get(vty->candidate_config->dnode,
161 VTY_CURR_XPATH);
162 if (yang_dnode_existsf(
163 dnode,
164 "./if-route-maps/if-route-map[interface='%s']/%s-route-map",
165 ifname, other_dir)) {
166 snprintf(
167 xpath, sizeof(xpath),
168 "./if-route-maps/if-route-map[interface='%s']/%s-route-map",
169 ifname, dir);
170 } else {
171 /* both dir will be empty so delete the list node */
172 snprintf(xpath, sizeof(xpath),
173 "./if-route-maps/if-route-map[interface='%s']",
174 ifname);
175 }
176 }
177 nb_cli_enqueue_change(vty, xpath, op, route_map);
178
179 return nb_cli_apply_changes(vty, NULL);
180 }
181
182 DEFPY_YANG(if_ipv4_route_map, if_ipv4_route_map_cmd,
183 "route-map ROUTE-MAP <in$in|out> IFNAME",
184 "Route map set\n"
185 "Route map name\n"
186 "Route map set for input filtering\n"
187 "Route map set for output filtering\n" INTERFACE_STR)
188 {
189 const char *dir = in ? "in" : "out";
190 const char *other_dir = in ? "out" : "in";
191
192 return if_route_map_handler(vty, false, dir, other_dir, ifname,
193 route_map);
194 }
195
196 DEFPY_YANG(no_if_ipv4_route_map, no_if_ipv4_route_map_cmd,
197 "no route-map [ROUTE-MAP] <in$in|out> IFNAME",
198 NO_STR
199 "Route map set\n"
200 "Route map name\n"
201 "Route map set for input filtering\n"
202 "Route map set for output filtering\n" INTERFACE_STR)
203 {
204 const char *dir = in ? "in" : "out";
205 const char *other_dir = in ? "out" : "in";
206
207 return if_route_map_handler(vty, true, dir, other_dir, ifname,
208 route_map);
209 }
210
211 /*
212 * CLI infra requires new handlers for ripngd
213 */
214 DEFPY_YANG(if_ipv6_route_map, if_ipv6_route_map_cmd,
215 "route-map ROUTE-MAP <in$in|out> IFNAME",
216 "Route map set\n"
217 "Route map name\n"
218 "Route map set for input filtering\n"
219 "Route map set for output filtering\n" INTERFACE_STR)
220 {
221 const char *dir = in ? "in" : "out";
222 const char *other_dir = in ? "out" : "in";
223
224 return if_route_map_handler(vty, false, dir, other_dir, ifname,
225 route_map);
226 }
227
228 DEFPY_YANG(no_if_ipv6_route_map, no_if_ipv6_route_map_cmd,
229 "no route-map [ROUTE-MAP] <in$in|out> IFNAME",
230 NO_STR
231 "Route map set\n"
232 "Route map name\n"
233 "Route map set for input filtering\n"
234 "Route map set for output filtering\n" INTERFACE_STR)
235 {
236 const char *dir = in ? "in" : "out";
237 const char *other_dir = in ? "out" : "in";
238
239 return if_route_map_handler(vty, true, dir, other_dir, ifname,
240 route_map);
241 }
242
243
244 void if_rmap_yang_modify_cb(struct if_rmap_ctx *ctx,
245 const struct lyd_node *dnode,
246 enum if_rmap_type type, bool del)
247 {
248
249 const char *mapname = yang_dnode_get_string(dnode, NULL);
250 const char *ifname = yang_dnode_get_string(dnode, "../interface");
251
252 if (del)
253 if_rmap_unset(ctx, ifname, type);
254 else
255 if_rmap_set(ctx, ifname, type, mapname);
256 }
257
258 void if_rmap_yang_destroy_cb(struct if_rmap_ctx *ctx,
259 const struct lyd_node *dnode)
260 {
261 const char *ifname = yang_dnode_get_string(dnode, "interface");
262 if_rmap_unset(ctx, ifname, IF_RMAP_IN);
263 if_rmap_unset(ctx, ifname, IF_RMAP_OUT);
264 }
265
266
267 /* Configuration write function. */
268 int config_write_if_rmap(struct vty *vty, struct if_rmap_ctx *ctx)
269 {
270 unsigned int i;
271 struct hash_bucket *mp;
272 int write = 0;
273 struct hash *ifrmaphash = ctx->ifrmaphash;
274
275 for (i = 0; i < ifrmaphash->size; i++)
276 for (mp = ifrmaphash->index[i]; mp; mp = mp->next) {
277 struct if_rmap *if_rmap;
278
279 if_rmap = mp->data;
280
281 if (if_rmap->routemap[IF_RMAP_IN]) {
282 vty_out(vty, " route-map %s in %s\n",
283 if_rmap->routemap[IF_RMAP_IN],
284 if_rmap->ifname);
285 write++;
286 }
287
288 if (if_rmap->routemap[IF_RMAP_OUT]) {
289 vty_out(vty, " route-map %s out %s\n",
290 if_rmap->routemap[IF_RMAP_OUT],
291 if_rmap->ifname);
292 write++;
293 }
294 }
295 return write;
296 }
297
298 void if_rmap_ctx_delete(struct if_rmap_ctx *ctx)
299 {
300 hash_clean_and_free(&ctx->ifrmaphash, (void (*)(void *))if_rmap_free);
301 XFREE(MTYPE_IF_RMAP_CTX_NAME, ctx->name);
302 XFREE(MTYPE_IF_RMAP_CTX, ctx);
303 }
304
305 /* name is optional: either vrf name, or other */
306 struct if_rmap_ctx *if_rmap_ctx_create(const char *name)
307 {
308 struct if_rmap_ctx *ctx;
309
310 ctx = XCALLOC(MTYPE_IF_RMAP_CTX, sizeof(struct if_rmap_ctx));
311
312 ctx->name = XSTRDUP(MTYPE_IF_RMAP_CTX_NAME, name);
313 ctx->ifrmaphash =
314 hash_create_size(4, if_rmap_hash_make, if_rmap_hash_cmp,
315 "Interface Route-Map Hash");
316 return ctx;
317 }
318
319 void if_rmap_init(int node)
320 {
321 if (node == RIP_NODE) {
322 install_element(RIP_NODE, &if_ipv4_route_map_cmd);
323 install_element(RIP_NODE, &no_if_ipv4_route_map_cmd);
324 } else if (node == RIPNG_NODE) {
325 install_element(RIPNG_NODE, &if_ipv6_route_map_cmd);
326 install_element(RIPNG_NODE, &no_if_ipv6_route_map_cmd);
327 }
328 }
329
330 void if_rmap_terminate(void)
331 {
332 }