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