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