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