]> git.proxmox.com Git - mirror_frr.git/blob - lib/distribute.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / distribute.c
1 /* Distribute list functions
2 * Copyright (C) 1998, 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
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your
9 * option) any 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 "if.h"
25 #include "filter.h"
26 #include "command.h"
27 #include "distribute.h"
28 #include "memory.h"
29
30 DEFINE_MTYPE_STATIC(LIB, DISTRIBUTE_CTX, "Distribute ctx")
31 DEFINE_MTYPE_STATIC(LIB, DISTRIBUTE, "Distribute list")
32 DEFINE_MTYPE_STATIC(LIB, DISTRIBUTE_IFNAME, "Dist-list ifname")
33 DEFINE_MTYPE_STATIC(LIB, DISTRIBUTE_NAME, "Dist-list name")
34
35 struct list *dist_ctx_list;
36
37 static struct distribute *distribute_new(void)
38 {
39 return XCALLOC(MTYPE_DISTRIBUTE, sizeof(struct distribute));
40 }
41
42 /* Free distribute object. */
43 static void distribute_free(struct distribute *dist)
44 {
45 int i = 0;
46
47 if (dist->ifname)
48 XFREE(MTYPE_DISTRIBUTE_IFNAME, dist->ifname);
49
50 for (i = 0; i < DISTRIBUTE_MAX; i++)
51 if (dist->list[i])
52 XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[i]);
53
54 for (i = 0; i < DISTRIBUTE_MAX; i++)
55 if (dist->prefix[i])
56 XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[i]);
57
58 XFREE(MTYPE_DISTRIBUTE, dist);
59 }
60
61 static void distribute_free_if_empty(struct distribute_ctx *ctx,
62 struct distribute *dist)
63 {
64 int i;
65
66 for (i = 0; i < DISTRIBUTE_MAX; i++)
67 if (dist->list[i] != NULL || dist->prefix[i] != NULL)
68 return;
69
70 hash_release(ctx->disthash, dist);
71 distribute_free(dist);
72 }
73
74 /* Lookup interface's distribute list. */
75 struct distribute *distribute_lookup(struct distribute_ctx *ctx,
76 const char *ifname)
77 {
78 struct distribute key;
79 struct distribute *dist;
80
81 /* temporary reference */
82 key.ifname = (ifname) ? XSTRDUP(MTYPE_DISTRIBUTE_IFNAME, ifname) : NULL;
83
84 dist = hash_lookup(ctx->disthash, &key);
85
86 if (key.ifname)
87 XFREE(MTYPE_DISTRIBUTE_IFNAME, key.ifname);
88
89 return dist;
90 }
91
92 void distribute_list_add_hook(struct distribute_ctx *ctx,
93 void (*func)(struct distribute_ctx *ctx,
94 struct distribute *))
95 {
96 ctx->distribute_add_hook = func;
97 }
98
99 void distribute_list_delete_hook(struct distribute_ctx *ctx,
100 void (*func)(struct distribute_ctx *ctx,
101 struct distribute *))
102 {
103 ctx->distribute_delete_hook = func;
104 }
105
106 static void *distribute_hash_alloc(struct distribute *arg)
107 {
108 struct distribute *dist;
109
110 dist = distribute_new();
111 if (arg->ifname)
112 dist->ifname = XSTRDUP(MTYPE_DISTRIBUTE_IFNAME, arg->ifname);
113 else
114 dist->ifname = NULL;
115 return dist;
116 }
117
118 /* Make new distribute list and push into hash. */
119 static struct distribute *distribute_get(struct distribute_ctx *ctx,
120 const char *ifname)
121 {
122 struct distribute key;
123 struct distribute *ret;
124
125 /* temporary reference */
126 key.ifname = (ifname) ? XSTRDUP(MTYPE_DISTRIBUTE_IFNAME, ifname) : NULL;
127
128 ret = hash_get(ctx->disthash, &key,
129 (void *(*)(void *))distribute_hash_alloc);
130
131 if (key.ifname)
132 XFREE(MTYPE_DISTRIBUTE_IFNAME, key.ifname);
133
134 return ret;
135 }
136
137 static unsigned int distribute_hash_make(void *arg)
138 {
139 const struct distribute *dist = arg;
140
141 return dist->ifname ? string_hash_make(dist->ifname) : 0;
142 }
143
144 /* If two distribute-list have same value then return 1 else return
145 0. This function is used by hash package. */
146 static bool distribute_cmp(const struct distribute *dist1,
147 const struct distribute *dist2)
148 {
149 if (dist1->ifname && dist2->ifname)
150 if (strcmp(dist1->ifname, dist2->ifname) == 0)
151 return true;
152 if (!dist1->ifname && !dist2->ifname)
153 return true;
154 return false;
155 }
156
157 /* Set access-list name to the distribute list. */
158 static void distribute_list_set(struct distribute_ctx *ctx,
159 const char *ifname, enum distribute_type type,
160 const char *alist_name)
161 {
162 struct distribute *dist;
163
164 dist = distribute_get(ctx, ifname);
165
166 if (dist->list[type])
167 XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[type]);
168 dist->list[type] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, alist_name);
169
170 /* Apply this distribute-list to the interface. */
171 (ctx->distribute_add_hook)(ctx, dist);
172 }
173
174 /* Unset distribute-list. If matched distribute-list exist then
175 return 1. */
176 static int distribute_list_unset(struct distribute_ctx *ctx,
177 const char *ifname,
178 enum distribute_type type,
179 const char *alist_name)
180 {
181 struct distribute *dist;
182
183 dist = distribute_lookup(ctx, ifname);
184 if (!dist)
185 return 0;
186
187 if (!dist->list[type])
188 return 0;
189 if (strcmp(dist->list[type], alist_name) != 0)
190 return 0;
191
192 XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[type]);
193 dist->list[type] = NULL;
194
195 /* Apply this distribute-list to the interface. */
196 (ctx->distribute_delete_hook)(ctx, dist);
197
198 /* If all dist are NULL, then free distribute list. */
199 distribute_free_if_empty(ctx, dist);
200 return 1;
201 }
202
203 /* Set access-list name to the distribute list. */
204 static void distribute_list_prefix_set(struct distribute_ctx *ctx,
205 const char *ifname,
206 enum distribute_type type,
207 const char *plist_name)
208 {
209 struct distribute *dist;
210
211 dist = distribute_get(ctx, ifname);
212
213 if (dist->prefix[type])
214 XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[type]);
215 dist->prefix[type] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, plist_name);
216
217 /* Apply this distribute-list to the interface. */
218 (ctx->distribute_add_hook)(ctx, dist);
219 }
220
221 /* Unset distribute-list. If matched distribute-list exist then
222 return 1. */
223 static int distribute_list_prefix_unset(struct distribute_ctx *ctx,
224 const char *ifname,
225 enum distribute_type type,
226 const char *plist_name)
227 {
228 struct distribute *dist;
229
230 dist = distribute_lookup(ctx, ifname);
231 if (!dist)
232 return 0;
233
234 if (!dist->prefix[type])
235 return 0;
236 if (strcmp(dist->prefix[type], plist_name) != 0)
237 return 0;
238
239 XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[type]);
240 dist->prefix[type] = NULL;
241
242 /* Apply this distribute-list to the interface. */
243 (ctx->distribute_delete_hook)(ctx, dist);
244
245 /* If all dist are NULL, then free distribute list. */
246 distribute_free_if_empty(ctx, dist);
247 return 1;
248 }
249
250 DEFUN (distribute_list,
251 distribute_list_cmd,
252 "distribute-list [prefix] WORD <in|out> [WORD]",
253 "Filter networks in routing updates\n"
254 "Specify a prefix\n"
255 "Access-list name\n"
256 "Filter incoming routing updates\n"
257 "Filter outgoing routing updates\n"
258 "Interface name\n")
259 {
260 int prefix = (argv[1]->type == WORD_TKN) ? 1 : 0;
261 /* Check of distribute list type. */
262 enum distribute_type type = argv[2 + prefix]->arg[0] == 'i'
263 ? DISTRIBUTE_V4_IN
264 : DISTRIBUTE_V4_OUT;
265
266 /* Set appropriate function call */
267 void (*distfn)(struct distribute_ctx *, const char *,
268 enum distribute_type, const char *) =
269 prefix ? &distribute_list_prefix_set : &distribute_list_set;
270 struct distribute_ctx *ctx =
271 (struct distribute_ctx *)listnode_head(dist_ctx_list);
272
273 /* if interface is present, get name */
274 const char *ifname = NULL;
275 if (argv[argc - 1]->type == VARIABLE_TKN)
276 ifname = argv[argc - 1]->arg;
277
278 /* Get interface name corresponding distribute list. */
279 distfn(ctx, ifname, type, argv[1 + prefix]->arg);
280
281 return CMD_SUCCESS;
282 }
283
284 DEFUN (ipv6_distribute_list,
285 ipv6_distribute_list_cmd,
286 "ipv6 distribute-list [prefix] WORD <in|out> [WORD]",
287 "IPv6\n"
288 "Filter networks in routing updates\n"
289 "Specify a prefix\n"
290 "Access-list name\n"
291 "Filter incoming routing updates\n"
292 "Filter outgoing routing updates\n"
293 "Interface name\n")
294 {
295 int prefix = (argv[2]->type == WORD_TKN) ? 1 : 0;
296 /* Check of distribute list type. */
297 enum distribute_type type = argv[3 + prefix]->arg[0] == 'i'
298 ? DISTRIBUTE_V6_IN
299 : DISTRIBUTE_V6_OUT;
300
301 /* Set appropriate function call */
302 void (*distfn)(struct distribute_ctx *, const char *,
303 enum distribute_type, const char *) =
304 prefix ? &distribute_list_prefix_set : &distribute_list_set;
305 struct distribute_ctx *ctx = listnode_head(dist_ctx_list);
306
307 /* if interface is present, get name */
308 const char *ifname = NULL;
309 if (argv[argc - 1]->type == VARIABLE_TKN)
310 ifname = argv[argc - 1]->arg;
311
312 /* Get interface name corresponding distribute list. */
313 distfn(ctx, ifname, type, argv[2 + prefix]->arg);
314
315 return CMD_SUCCESS;
316 }
317
318 DEFUN (no_distribute_list,
319 no_distribute_list_cmd,
320 "no distribute-list [prefix] WORD <in|out> [WORD]",
321 NO_STR
322 "Filter networks in routing updates\n"
323 "Specify a prefix\n"
324 "Access-list name\n"
325 "Filter incoming routing updates\n"
326 "Filter outgoing routing updates\n"
327 "Interface name\n")
328 {
329 int prefix = (argv[2]->type == WORD_TKN) ? 1 : 0;
330 int idx_alname = 2 + prefix;
331 int idx_disttype = idx_alname + 1;
332 enum distribute_type type =
333 argv[idx_disttype]->arg[0] == 'i' ?
334 DISTRIBUTE_V4_IN : DISTRIBUTE_V4_OUT;
335
336 /* Set appropriate function call */
337 int (*distfn)(struct distribute_ctx *, const char *,
338 enum distribute_type, const char *) =
339 prefix ? &distribute_list_prefix_unset : &distribute_list_unset;
340 struct distribute_ctx *ctx = listnode_head(dist_ctx_list);
341
342 /* if interface is present, get name */
343 const char *ifname = NULL;
344 if (argv[argc - 1]->type == VARIABLE_TKN)
345 ifname = argv[argc - 1]->arg;
346 /* Get interface name corresponding distribute list. */
347 int ret = distfn(ctx, ifname, type, argv[2 + prefix]->arg);
348
349 if (!ret) {
350 vty_out(vty, "distribute list doesn't exist\n");
351 return CMD_WARNING_CONFIG_FAILED;
352 }
353 return CMD_SUCCESS;
354 }
355
356 DEFUN (no_ipv6_distribute_list,
357 no_ipv6_distribute_list_cmd,
358 "no ipv6 distribute-list [prefix] WORD <in|out> [WORD]",
359 NO_STR
360 "IPv6\n"
361 "Filter networks in routing updates\n"
362 "Specify a prefix\n"
363 "Access-list name\n"
364 "Filter incoming routing updates\n"
365 "Filter outgoing routing updates\n"
366 "Interface name\n")
367 {
368 int prefix = (argv[3]->type == WORD_TKN) ? 1 : 0;
369 int idx_alname = 3 + prefix;
370 int idx_disttype = idx_alname + 1;
371
372 enum distribute_type type =
373 argv[idx_disttype]->arg[0] == 'i' ?
374 DISTRIBUTE_V6_IN : DISTRIBUTE_V6_OUT;
375 struct distribute_ctx *ctx = listnode_head(dist_ctx_list);
376
377 /* Set appropriate function call */
378 int (*distfn)(struct distribute_ctx *, const char *,
379 enum distribute_type, const char *) =
380 prefix ? &distribute_list_prefix_unset : &distribute_list_unset;
381
382 /* if interface is present, get name */
383 const char *ifname = NULL;
384
385 if (argv[argc - 1]->type == VARIABLE_TKN)
386 ifname = argv[argc - 1]->arg;
387 /* Get interface name corresponding distribute list. */
388 int ret = distfn(ctx, ifname, type, argv[3 + prefix]->arg);
389
390 if (!ret) {
391 vty_out(vty, "distribute list doesn't exist\n");
392 return CMD_WARNING_CONFIG_FAILED;
393 }
394 return CMD_SUCCESS;
395 }
396
397 static int distribute_print(struct vty *vty, char *tab[], int is_prefix,
398 enum distribute_type type, int has_print)
399 {
400 if (tab[type]) {
401 vty_out(vty, "%s %s%s", has_print ? "," : "",
402 is_prefix ? "(prefix-list) " : "", tab[type]);
403 return 1;
404 }
405 return has_print;
406 }
407
408 int config_show_distribute(struct vty *vty, struct distribute_ctx *dist_ctxt)
409 {
410 unsigned int i;
411 int has_print = 0;
412 struct hash_backet *mp;
413 struct distribute *dist;
414
415 /* Output filter configuration. */
416 dist = distribute_lookup(dist_ctxt, NULL);
417 vty_out(vty, " Outgoing update filter list for all interface is");
418 has_print = 0;
419 if (dist) {
420 has_print = distribute_print(vty, dist->list, 0,
421 DISTRIBUTE_V4_OUT, has_print);
422 has_print = distribute_print(vty, dist->prefix, 1,
423 DISTRIBUTE_V4_OUT, has_print);
424 has_print = distribute_print(vty, dist->list, 0,
425 DISTRIBUTE_V6_OUT, has_print);
426 has_print = distribute_print(vty, dist->prefix, 1,
427 DISTRIBUTE_V6_OUT, has_print);
428 }
429 if (has_print)
430 vty_out(vty, "\n");
431 else
432 vty_out(vty, " not set\n");
433
434 for (i = 0; i < dist_ctxt->disthash->size; i++)
435 for (mp = dist_ctxt->disthash->index[i]; mp; mp = mp->next) {
436 dist = mp->data;
437 if (dist->ifname) {
438 vty_out(vty, " %s filtered by",
439 dist->ifname);
440 has_print = 0;
441 has_print = distribute_print(vty, dist->list, 0,
442 DISTRIBUTE_V4_OUT,
443 has_print);
444 has_print = distribute_print(
445 vty, dist->prefix, 1, DISTRIBUTE_V4_OUT,
446 has_print);
447 has_print = distribute_print(vty, dist->list, 0,
448 DISTRIBUTE_V6_OUT,
449 has_print);
450 has_print = distribute_print(
451 vty, dist->prefix, 1, DISTRIBUTE_V6_OUT,
452 has_print);
453 if (has_print)
454 vty_out(vty, "\n");
455 else
456 vty_out(vty, " nothing\n");
457 }
458 }
459
460
461 /* Input filter configuration. */
462 dist = distribute_lookup(dist_ctxt, NULL);
463 vty_out(vty, " Incoming update filter list for all interface is");
464 has_print = 0;
465 if (dist) {
466 has_print = distribute_print(vty, dist->list, 0,
467 DISTRIBUTE_V4_IN, has_print);
468 has_print = distribute_print(vty, dist->prefix, 1,
469 DISTRIBUTE_V4_IN, has_print);
470 has_print = distribute_print(vty, dist->list, 0,
471 DISTRIBUTE_V6_IN, has_print);
472 has_print = distribute_print(vty, dist->prefix, 1,
473 DISTRIBUTE_V6_IN, has_print);
474 }
475 if (has_print)
476 vty_out(vty, "\n");
477 else
478 vty_out(vty, " not set\n");
479
480 for (i = 0; i < dist_ctxt->disthash->size; i++)
481 for (mp = dist_ctxt->disthash->index[i]; mp; mp = mp->next) {
482 dist = mp->data;
483 if (dist->ifname) {
484 vty_out(vty, " %s filtered by",
485 dist->ifname);
486 has_print = 0;
487 has_print = distribute_print(vty, dist->list, 0,
488 DISTRIBUTE_V4_IN,
489 has_print);
490 has_print = distribute_print(
491 vty, dist->prefix, 1, DISTRIBUTE_V4_IN,
492 has_print);
493 has_print = distribute_print(vty, dist->list, 0,
494 DISTRIBUTE_V6_IN,
495 has_print);
496 has_print = distribute_print(
497 vty, dist->prefix, 1, DISTRIBUTE_V6_IN,
498 has_print);
499 if (has_print)
500 vty_out(vty, "\n");
501 else
502 vty_out(vty, " nothing\n");
503 }
504 }
505 return 0;
506 }
507
508 /* Configuration write function. */
509 int config_write_distribute(struct vty *vty,
510 struct distribute_ctx *dist_ctxt)
511 {
512 unsigned int i;
513 int j;
514 int output, v6;
515 struct hash_backet *mp;
516 int write = 0;
517
518 for (i = 0; i < dist_ctxt->disthash->size; i++)
519 for (mp = dist_ctxt->disthash->index[i]; mp; mp = mp->next) {
520 struct distribute *dist;
521
522 dist = mp->data;
523
524 for (j = 0; j < DISTRIBUTE_MAX; j++)
525 if (dist->list[j]) {
526 output = j == DISTRIBUTE_V4_OUT
527 || j == DISTRIBUTE_V6_OUT;
528 v6 = j == DISTRIBUTE_V6_IN
529 || j == DISTRIBUTE_V6_OUT;
530 vty_out(vty,
531 " %sdistribute-list %s %s %s\n",
532 v6 ? "ipv6 " : "",
533 dist->list[j],
534 output ? "out" : "in",
535 dist->ifname ? dist->ifname
536 : "");
537 write++;
538 }
539
540 for (j = 0; j < DISTRIBUTE_MAX; j++)
541 if (dist->prefix[j]) {
542 output = j == DISTRIBUTE_V4_OUT
543 || j == DISTRIBUTE_V6_OUT;
544 v6 = j == DISTRIBUTE_V6_IN
545 || j == DISTRIBUTE_V6_OUT;
546 vty_out(vty,
547 " %sdistribute-list prefix %s %s %s\n",
548 v6 ? "ipv6 " : "",
549 dist->prefix[j],
550 output ? "out" : "in",
551 dist->ifname ? dist->ifname
552 : "");
553 write++;
554 }
555 }
556 return write;
557 }
558
559 void distribute_list_delete(struct distribute_ctx **ctx)
560 {
561 if ((*ctx)->disthash) {
562 hash_clean((*ctx)->disthash, (void (*)(void *))distribute_free);
563 }
564 if (!dist_ctx_list)
565 dist_ctx_list = list_new();
566 listnode_delete(dist_ctx_list, *ctx);
567 if (list_isempty(dist_ctx_list))
568 list_delete(&dist_ctx_list);
569 XFREE(MTYPE_DISTRIBUTE_CTX, (*ctx));
570 }
571
572 /* Initialize distribute list container */
573 struct distribute_ctx *distribute_list_ctx_create(struct vrf *vrf)
574 {
575 struct distribute_ctx *ctx;
576
577 ctx = XCALLOC(MTYPE_DISTRIBUTE_CTX, sizeof(struct distribute_ctx));
578 ctx->vrf = vrf;
579 ctx->disthash = hash_create(
580 distribute_hash_make,
581 (bool (*)(const void *, const void *))distribute_cmp, NULL);
582 if (!dist_ctx_list)
583 dist_ctx_list = list_new();
584 listnode_add(dist_ctx_list, ctx);
585 return ctx;
586 }
587
588 /* Initialize distribute list vty commands */
589 void distribute_list_init(int node)
590 {
591 /* vtysh command-extraction doesn't grok install_element(node, ) */
592 if (node == RIP_NODE) {
593 install_element(RIP_NODE, &distribute_list_cmd);
594 install_element(RIP_NODE, &no_distribute_list_cmd);
595 } else if (node == RIPNG_NODE) {
596 install_element(RIPNG_NODE, &distribute_list_cmd);
597 install_element(RIPNG_NODE, &no_distribute_list_cmd);
598 /* install v6 */
599 install_element(RIPNG_NODE, &ipv6_distribute_list_cmd);
600 install_element(RIPNG_NODE, &no_ipv6_distribute_list_cmd);
601 }
602
603 /* TODO: install v4 syntax command for v6 only protocols. */
604 /* if (node == RIPNG_NODE) {
605 * install_element (node, &ipv6_as_v4_distribute_list_all_cmd);
606 * install_element (node, &no_ipv6_as_v4_distribute_list_all_cmd);
607 * install_element (node, &ipv6_as_v4_distribute_list_cmd);
608 * install_element (node, &no_ipv6_as_v4_distribute_list_cmd);
609 * install_element (node, &ipv6_as_v4_distribute_list_prefix_all_cmd);
610 * install_element (node,
611 &no_ipv6_as_v4_distribute_list_prefix_all_cmd);
612 * install_element (node, &ipv6_as_v4_distribute_list_prefix_cmd);
613 * install_element (node, &no_ipv6_as_v4_distribute_list_prefix_cmd);
614 }*/
615 }