]> git.proxmox.com Git - mirror_frr.git/blame - lib/if_rmap.c
Merge pull request #11076 from routingrocks/vrrp_master_ad_cli
[mirror_frr.git] / lib / if_rmap.c
CommitLineData
718e3744 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 *
896014f4
DL
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
718e3744 19 */
20
21#include <zebra.h>
22
23#include "hash.h"
24#include "command.h"
25#include "memory.h"
26#include "if.h"
0750d21f 27#include "if_rmap.h"
718e3744 28
bf8d3d6a
DL
29DEFINE_MTYPE_STATIC(LIB, IF_RMAP_CTX, "Interface route map container");
30DEFINE_MTYPE_STATIC(LIB, IF_RMAP_CTX_NAME,
31 "Interface route map container name");
32DEFINE_MTYPE_STATIC(LIB, IF_RMAP, "Interface route map");
33DEFINE_MTYPE_STATIC(LIB, IF_RMAP_NAME, "I.f. route map name");
4a1ab8e4 34
c17faa4b 35static struct list *if_rmap_ctx_list;
6b0655a2 36
d62a17ae 37static struct if_rmap *if_rmap_new(void)
718e3744 38{
d62a17ae 39 struct if_rmap *new;
718e3744 40
d62a17ae 41 new = XCALLOC(MTYPE_IF_RMAP, sizeof(struct if_rmap));
718e3744 42
d62a17ae 43 return new;
718e3744 44}
45
d62a17ae 46static void if_rmap_free(struct if_rmap *if_rmap)
718e3744 47{
0a22ddfb 48 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->ifname);
718e3744 49
0a22ddfb
QY
50 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
51 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
718e3744 52
d62a17ae 53 XFREE(MTYPE_IF_RMAP, if_rmap);
718e3744 54}
55
4b23867c 56struct if_rmap *if_rmap_lookup(struct if_rmap_ctx *ctx, const char *ifname)
718e3744 57{
d62a17ae 58 struct if_rmap key;
59 struct if_rmap *if_rmap;
718e3744 60
d62a17ae 61 /* temporary copy */
62 key.ifname = (ifname) ? XSTRDUP(MTYPE_IF_RMAP_NAME, ifname) : NULL;
718e3744 63
4b23867c 64 if_rmap = hash_lookup(ctx->ifrmaphash, &key);
3a7c85d1 65
0a22ddfb 66 XFREE(MTYPE_IF_RMAP_NAME, key.ifname);
d62a17ae 67
68 return if_rmap;
718e3744 69}
70
4b23867c
PG
71void if_rmap_hook_add(struct if_rmap_ctx *ctx,
72 void (*func)(struct if_rmap_ctx *ctx,
73 struct if_rmap *))
718e3744 74{
4b23867c 75 ctx->if_rmap_add_hook = func;
718e3744 76}
77
4b23867c
PG
78void if_rmap_hook_delete(struct if_rmap_ctx *ctx,
79 void (*func)(struct if_rmap_ctx *ctx,
80 struct if_rmap *))
718e3744 81{
4b23867c 82 ctx->if_rmap_delete_hook = func;
718e3744 83}
84
d62a17ae 85static void *if_rmap_hash_alloc(void *arg)
718e3744 86{
d62a17ae 87 struct if_rmap *ifarg = (struct if_rmap *)arg;
88 struct if_rmap *if_rmap;
718e3744 89
d62a17ae 90 if_rmap = if_rmap_new();
91 if_rmap->ifname = XSTRDUP(MTYPE_IF_RMAP_NAME, ifarg->ifname);
718e3744 92
d62a17ae 93 return if_rmap;
718e3744 94}
95
4b23867c 96static struct if_rmap *if_rmap_get(struct if_rmap_ctx *ctx, const char *ifname)
718e3744 97{
d62a17ae 98 struct if_rmap key;
99 struct if_rmap *ret;
718e3744 100
d62a17ae 101 /* temporary copy */
102 key.ifname = (ifname) ? XSTRDUP(MTYPE_IF_RMAP_NAME, ifname) : NULL;
718e3744 103
4b23867c 104 ret = hash_get(ctx->ifrmaphash, &key, if_rmap_hash_alloc);
3a7c85d1 105
0a22ddfb 106 XFREE(MTYPE_IF_RMAP_NAME, key.ifname);
24873f0c 107
d62a17ae 108 return ret;
718e3744 109}
110
d8b87afe 111static unsigned int if_rmap_hash_make(const void *data)
718e3744 112{
d62a17ae 113 const struct if_rmap *if_rmap = data;
718e3744 114
d62a17ae 115 return string_hash_make(if_rmap->ifname);
718e3744 116}
117
74df8d6d 118static bool if_rmap_hash_cmp(const void *arg1, const void *arg2)
718e3744 119{
d62a17ae 120 const struct if_rmap *if_rmap1 = arg1;
121 const struct if_rmap *if_rmap2 = arg2;
ffe11cfb 122
d62a17ae 123 return strcmp(if_rmap1->ifname, if_rmap2->ifname) == 0;
718e3744 124}
6b0655a2 125
4b23867c
PG
126static struct if_rmap *if_rmap_set(struct if_rmap_ctx *ctx,
127 const char *ifname, enum if_rmap_type type,
d62a17ae 128 const char *routemap_name)
718e3744 129{
d62a17ae 130 struct if_rmap *if_rmap;
131
4b23867c 132 if_rmap = if_rmap_get(ctx, ifname);
d62a17ae 133
134 if (type == IF_RMAP_IN) {
0a22ddfb 135 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
d62a17ae 136 if_rmap->routemap[IF_RMAP_IN] =
137 XSTRDUP(MTYPE_IF_RMAP_NAME, routemap_name);
138 }
139 if (type == IF_RMAP_OUT) {
0a22ddfb 140 XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
d62a17ae 141 if_rmap->routemap[IF_RMAP_OUT] =
142 XSTRDUP(MTYPE_IF_RMAP_NAME, routemap_name);
143 }
144
4b23867c
PG
145 if (ctx->if_rmap_add_hook)
146 (ctx->if_rmap_add_hook)(ctx, if_rmap);
d62a17ae 147
148 return if_rmap;
718e3744 149}
150
4b23867c
PG
151static int if_rmap_unset(struct if_rmap_ctx *ctx,
152 const char *ifname, enum if_rmap_type type,
d62a17ae 153 const char *routemap_name)
718e3744 154{
d62a17ae 155 struct if_rmap *if_rmap;
156
4b23867c 157 if_rmap = if_rmap_lookup(ctx, ifname);
d62a17ae 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]);
d62a17ae 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]);
d62a17ae 177 }
178
4b23867c
PG
179 if (ctx->if_rmap_delete_hook)
180 ctx->if_rmap_delete_hook(ctx, if_rmap);
d62a17ae 181
182 if (if_rmap->routemap[IF_RMAP_IN] == NULL
183 && if_rmap->routemap[IF_RMAP_OUT] == NULL) {
4b23867c 184 hash_release(ctx->ifrmaphash, if_rmap);
d62a17ae 185 if_rmap_free(if_rmap);
186 }
187
188 return 1;
718e3744 189}
190
0750d21f 191DEFUN (if_rmap,
192 if_rmap_cmd,
6147e2c6 193 "route-map RMAP_NAME <in|out> IFNAME",
718e3744 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{
d62a17ae 200 int idx_rmap_name = 1;
201 int idx_in_out = 2;
202 int idx_ifname = 3;
203 enum if_rmap_type type;
4b23867c
PG
204 struct if_rmap_ctx *ctx =
205 (struct if_rmap_ctx *)listnode_head(if_rmap_ctx_list);
d62a17ae 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
4b23867c
PG
216 if_rmap_set(ctx, argv[idx_ifname]->arg,
217 type, argv[idx_rmap_name]->arg);
d62a17ae 218
219 return CMD_SUCCESS;
c349116d 220}
4f849479 221
0750d21f 222DEFUN (no_if_rmap,
223 no_if_rmap_cmd,
6147e2c6 224 "no route-map ROUTEMAP_NAME <in|out> IFNAME",
718e3744 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{
d62a17ae 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;
4b23867c
PG
237 struct if_rmap_ctx *ctx =
238 (struct if_rmap_ctx *)listnode_head(if_rmap_ctx_list);
d62a17ae 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
4b23867c 249 ret = if_rmap_unset(ctx, argv[idx_ifname]->arg, type,
d62a17ae 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;
c349116d 256}
4f849479 257
6b0655a2 258
718e3744 259/* Configuration write function. */
4b23867c
PG
260int config_write_if_rmap(struct vty *vty,
261 struct if_rmap_ctx *ctx)
718e3744 262{
d62a17ae 263 unsigned int i;
e3b78da8 264 struct hash_bucket *mp;
d62a17ae 265 int write = 0;
4b23867c 266 struct hash *ifrmaphash = ctx->ifrmaphash;
d62a17ae 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;
718e3744 289}
290
4b23867c
PG
291void if_rmap_ctx_delete(struct if_rmap_ctx *ctx)
292{
cbcbac45 293 listnode_delete(if_rmap_ctx_list, ctx);
4b23867c 294 hash_clean(ctx->ifrmaphash, (void (*)(void *))if_rmap_free);
aec0d756
PG
295 if (ctx->name)
296 XFREE(MTYPE_IF_RMAP_CTX_NAME, ctx);
4b23867c
PG
297 XFREE(MTYPE_IF_RMAP_CTX, ctx);
298}
299
aec0d756
PG
300/* name is optional: either vrf name, or other */
301struct if_rmap_ctx *if_rmap_ctx_create(const char *name)
718e3744 302{
4b23867c
PG
303 struct if_rmap_ctx *ctx;
304
305 ctx = XCALLOC(MTYPE_IF_RMAP_CTX, sizeof(struct if_rmap_ctx));
aec0d756
PG
306
307 if (ctx->name)
308 ctx->name = XSTRDUP(MTYPE_IF_RMAP_CTX_NAME, name);
4b23867c
PG
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;
718e3744 315}
316
d62a17ae 317void if_rmap_init(int node)
718e3744 318{
d62a17ae 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 }
4b23867c
PG
324 if_rmap_ctx_list = list_new();
325}
326
327void if_rmap_terminate(void)
328{
329 if (!if_rmap_ctx_list)
330 return;
331 list_delete(&if_rmap_ctx_list);
718e3744 332}