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