1 /* AS path filter list.
2 Copyright (C) 1999 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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
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.
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include "bgpd/bgpd.h"
29 #include "bgpd/bgp_aspath.h"
30 #include "bgpd/bgp_regex.h"
31 #include "bgpd/bgp_filter.h"
33 /* List of AS filter list. */
40 /* AS path filter master. */
43 /* List of access_list which name is number. */
44 struct as_list_list num
;
46 /* List of access_list which name is string. */
47 struct as_list_list str
;
49 /* Hook function which is executed when new access_list is added. */
52 /* Hook function which is executed when access_list is deleted. */
53 void (*delete_hook
) ();
56 /* Element of AS path filter. */
59 struct as_filter
*next
;
60 struct as_filter
*prev
;
62 enum as_filter_type type
;
74 /* AS path filter list. */
79 enum as_list_type type
;
84 struct as_filter
*head
;
85 struct as_filter
*tail
;
88 /* ip as-path access-list 10 permit AS1. */
90 static struct as_list_master as_list_master
=
98 /* Allocate new AS filter. */
102 struct as_filter
*new;
104 new = XMALLOC (MTYPE_AS_FILTER
, sizeof (struct as_filter
));
105 memset (new, 0, sizeof (struct as_filter
));
109 /* Free allocated AS filter. */
111 as_filter_free (struct as_filter
*asfilter
)
114 bgp_regex_free (asfilter
->reg
);
115 if (asfilter
->reg_str
)
116 XFREE (MTYPE_AS_FILTER_STR
, asfilter
->reg_str
);
117 XFREE (MTYPE_AS_FILTER
, asfilter
);
120 /* Make new AS filter. */
122 as_filter_make (regex_t
*reg
, char *reg_str
, enum as_filter_type type
)
124 struct as_filter
*asfilter
;
126 asfilter
= as_filter_new ();
128 asfilter
->type
= type
;
129 asfilter
->reg_str
= XSTRDUP (MTYPE_AS_FILTER_STR
, reg_str
);
135 as_filter_lookup (struct as_list
*aslist
, char *reg_str
,
136 enum as_filter_type type
)
138 struct as_filter
*asfilter
;
140 for (asfilter
= aslist
->head
; asfilter
; asfilter
= asfilter
->next
)
141 if (strcmp (reg_str
, asfilter
->reg_str
) == 0)
147 as_list_filter_add (struct as_list
*aslist
, struct as_filter
*asfilter
)
149 asfilter
->next
= NULL
;
150 asfilter
->prev
= aslist
->tail
;
153 aslist
->tail
->next
= asfilter
;
155 aslist
->head
= asfilter
;
156 aslist
->tail
= asfilter
;
159 /* Lookup as_list from list of as_list by name. */
161 as_list_lookup (char *name
)
163 struct as_list
*aslist
;
168 for (aslist
= as_list_master
.num
.head
; aslist
; aslist
= aslist
->next
)
169 if (strcmp (aslist
->name
, name
) == 0)
172 for (aslist
= as_list_master
.str
.head
; aslist
; aslist
= aslist
->next
)
173 if (strcmp (aslist
->name
, name
) == 0)
184 new = XMALLOC (MTYPE_AS_LIST
, sizeof (struct as_list
));
185 memset (new, 0, sizeof (struct as_list
));
190 as_list_free (struct as_list
*aslist
)
192 XFREE (MTYPE_AS_LIST
, aslist
);
195 /* Insert new AS list to list of as_list. Each as_list is sorted by
198 as_list_insert (char *name
)
202 struct as_list
*aslist
;
203 struct as_list
*point
;
204 struct as_list_list
*list
;
206 /* Allocate new access_list and copy given name. */
207 aslist
= as_list_new ();
208 aslist
->name
= strdup (name
);
210 /* If name is made by all digit character. We treat it as
212 for (number
= 0, i
= 0; i
< strlen (name
); i
++)
214 if (isdigit ((int) name
[i
]))
215 number
= (number
* 10) + (name
[i
] - '0');
220 /* In case of name is all digit character */
221 if (i
== strlen (name
))
223 aslist
->type
= ACCESS_TYPE_NUMBER
;
225 /* Set access_list to number list. */
226 list
= &as_list_master
.num
;
228 for (point
= list
->head
; point
; point
= point
->next
)
229 if (atol (point
->name
) >= number
)
234 aslist
->type
= ACCESS_TYPE_STRING
;
236 /* Set access_list to string list. */
237 list
= &as_list_master
.str
;
239 /* Set point to insertion point. */
240 for (point
= list
->head
; point
; point
= point
->next
)
241 if (strcmp (point
->name
, name
) >= 0)
245 /* In case of this is the first element of master. */
246 if (list
->head
== NULL
)
248 list
->head
= list
->tail
= aslist
;
252 /* In case of insertion is made at the tail of access_list. */
255 aslist
->prev
= list
->tail
;
256 list
->tail
->next
= aslist
;
261 /* In case of insertion is made at the head of access_list. */
262 if (point
== list
->head
)
264 aslist
->next
= list
->head
;
265 list
->head
->prev
= aslist
;
270 /* Insertion is made at middle of the access_list. */
271 aslist
->next
= point
;
272 aslist
->prev
= point
->prev
;
275 point
->prev
->next
= aslist
;
276 point
->prev
= aslist
;
282 as_list_get (char *name
)
284 struct as_list
*aslist
;
286 aslist
= as_list_lookup (name
);
289 aslist
= as_list_insert (name
);
291 /* Run hook function. */
292 if (as_list_master
.add_hook
)
293 (*as_list_master
.add_hook
) ();
300 filter_type_str (enum as_filter_type type
)
304 case AS_FILTER_PERMIT
:
317 as_list_delete (struct as_list
*aslist
)
319 struct as_list_list
*list
;
320 struct as_filter
*filter
, *next
;
322 for (filter
= aslist
->head
; filter
; filter
= next
)
325 as_filter_free (filter
);
328 if (aslist
->type
== ACCESS_TYPE_NUMBER
)
329 list
= &as_list_master
.num
;
331 list
= &as_list_master
.str
;
334 aslist
->next
->prev
= aslist
->prev
;
336 list
->tail
= aslist
->prev
;
339 aslist
->prev
->next
= aslist
->next
;
341 list
->head
= aslist
->next
;
343 as_list_free (aslist
);
347 as_list_empty (struct as_list
*aslist
)
349 if (aslist
->head
== NULL
&& aslist
->tail
== NULL
)
356 as_list_filter_delete (struct as_list
*aslist
, struct as_filter
*asfilter
)
359 asfilter
->next
->prev
= asfilter
->prev
;
361 aslist
->tail
= asfilter
->prev
;
364 asfilter
->prev
->next
= asfilter
->next
;
366 aslist
->head
= asfilter
->next
;
368 as_filter_free (asfilter
);
370 /* If access_list becomes empty delete it from access_master. */
371 if (as_list_empty (aslist
))
372 as_list_delete (aslist
);
374 /* Run hook function. */
375 if (as_list_master
.delete_hook
)
376 (*as_list_master
.delete_hook
) ();
380 as_filter_match (struct as_filter
*asfilter
, struct aspath
*aspath
)
382 if (bgp_regexec (asfilter
->reg
, aspath
) != REG_NOMATCH
)
387 /* Apply AS path filter to AS. */
389 as_list_apply (struct as_list
*aslist
, void *object
)
391 struct as_filter
*asfilter
;
392 struct aspath
*aspath
;
394 aspath
= (struct aspath
*) object
;
397 return AS_FILTER_DENY
;
399 for (asfilter
= aslist
->head
; asfilter
; asfilter
= asfilter
->next
)
401 if (as_filter_match (asfilter
, aspath
))
402 return asfilter
->type
;
404 return AS_FILTER_DENY
;
407 /* Add hook function. */
409 as_list_add_hook (void (*func
) ())
411 as_list_master
.add_hook
= func
;
414 /* Delete hook function. */
416 as_list_delete_hook (void (*func
) ())
418 as_list_master
.delete_hook
= func
;
422 as_list_dup_check (struct as_list
*aslist
, struct as_filter
*new)
424 struct as_filter
*asfilter
;
426 for (asfilter
= aslist
->head
; asfilter
; asfilter
= asfilter
->next
)
428 if (asfilter
->type
== new->type
429 && strcmp (asfilter
->reg_str
, new->reg_str
) == 0)
435 DEFUN (ip_as_path
, ip_as_path_cmd
,
436 "ip as-path access-list WORD (deny|permit) .LINE",
438 "BGP autonomous system path filter\n"
439 "Specify an access list name\n"
440 "Regular expression access list name\n"
441 "Specify packets to reject\n"
442 "Specify packets to forward\n"
443 "A regular-expression to match the BGP AS paths\n")
445 enum as_filter_type type
;
446 struct as_filter
*asfilter
;
447 struct as_list
*aslist
;
454 /* Check the filter type. */
455 if (strncmp (argv
[1], "p", 1) == 0)
456 type
= AS_FILTER_PERMIT
;
457 else if (strncmp (argv
[1], "d", 1) == 0)
458 type
= AS_FILTER_DENY
;
461 vty_out (vty
, "filter type must be [permit|deny]%s", VTY_NEWLINE
);
465 /* Check AS path regex. */
466 b
= buffer_new (1024);
467 for (i
= 2; i
< argc
; i
++)
470 buffer_putc (b
, ' ');
474 buffer_putstr (b
, argv
[i
]);
476 buffer_putc (b
, '\0');
478 regstr
= buffer_getstr (b
);
481 regex
= bgp_regcomp (regstr
);
485 vty_out (vty
, "can't compile regexp %s%s", argv
[0],
490 asfilter
= as_filter_make (regex
, regstr
, type
);
494 /* Install new filter to the access_list. */
495 aslist
= as_list_get (argv
[0]);
497 /* Duplicate insertion check. */;
498 if (as_list_dup_check (aslist
, asfilter
))
499 as_filter_free (asfilter
);
501 as_list_filter_add (aslist
, asfilter
);
506 DEFUN (no_ip_as_path
,
508 "no ip as-path access-list WORD (deny|permit) .LINE",
511 "BGP autonomous system path filter\n"
512 "Specify an access list name\n"
513 "Regular expression access list name\n"
514 "Specify packets to reject\n"
515 "Specify packets to forward\n"
516 "A regular-expression to match the BGP AS paths\n")
518 enum as_filter_type type
;
519 struct as_filter
*asfilter
;
520 struct as_list
*aslist
;
527 /* Lookup AS list from AS path list. */
528 aslist
= as_list_lookup (argv
[0]);
531 vty_out (vty
, "ip as-path access-list %s doesn't exist%s", argv
[0],
536 /* Check the filter type. */
537 if (strncmp (argv
[1], "p", 1) == 0)
538 type
= AS_FILTER_PERMIT
;
539 else if (strncmp (argv
[1], "d", 1) == 0)
540 type
= AS_FILTER_DENY
;
543 vty_out (vty
, "filter type must be [permit|deny]%s", VTY_NEWLINE
);
547 /* Compile AS path. */
548 b
= buffer_new (1024);
549 for (i
= 2; i
< argc
; i
++)
552 buffer_putc (b
, ' ');
556 buffer_putstr (b
, argv
[i
]);
558 buffer_putc (b
, '\0');
560 regstr
= buffer_getstr (b
);
563 regex
= bgp_regcomp (regstr
);
567 vty_out (vty
, "can't compile regexp %s%s", argv
[0],
572 /* Lookup asfilter. */
573 asfilter
= as_filter_lookup (aslist
, regstr
, type
);
576 bgp_regex_free (regex
);
578 if (asfilter
== NULL
)
580 vty_out (vty
, "%s", VTY_NEWLINE
);
584 as_list_filter_delete (aslist
, asfilter
);
589 DEFUN (no_ip_as_path_all
,
590 no_ip_as_path_all_cmd
,
591 "no ip as-path access-list WORD",
594 "BGP autonomous system path filter\n"
595 "Specify an access list name\n"
596 "Regular expression access list name\n")
598 struct as_list
*aslist
;
600 aslist
= as_list_lookup (argv
[0]);
603 vty_out (vty
, "ip as-path access-list %s doesn't exist%s", argv
[0],
608 as_list_delete (aslist
);
610 /* Run hook function. */
611 if (as_list_master
.delete_hook
)
612 (*as_list_master
.delete_hook
) ();
618 as_list_show (struct vty
*vty
, struct as_list
*aslist
)
620 struct as_filter
*asfilter
;
622 vty_out (vty
, "AS path access list %s%s", aslist
->name
, VTY_NEWLINE
);
624 for (asfilter
= aslist
->head
; asfilter
; asfilter
= asfilter
->next
)
626 vty_out (vty
, " %s %s%s", filter_type_str (asfilter
->type
),
627 asfilter
->reg_str
, VTY_NEWLINE
);
632 as_list_show_all (struct vty
*vty
)
634 struct as_list
*aslist
;
635 struct as_filter
*asfilter
;
637 for (aslist
= as_list_master
.num
.head
; aslist
; aslist
= aslist
->next
)
639 vty_out (vty
, "AS path access list %s%s", aslist
->name
, VTY_NEWLINE
);
641 for (asfilter
= aslist
->head
; asfilter
; asfilter
= asfilter
->next
)
643 vty_out (vty
, " %s %s%s", filter_type_str (asfilter
->type
),
644 asfilter
->reg_str
, VTY_NEWLINE
);
648 for (aslist
= as_list_master
.str
.head
; aslist
; aslist
= aslist
->next
)
650 vty_out (vty
, "AS path access list %s%s", aslist
->name
, VTY_NEWLINE
);
652 for (asfilter
= aslist
->head
; asfilter
; asfilter
= asfilter
->next
)
654 vty_out (vty
, " %s %s%s", filter_type_str (asfilter
->type
),
655 asfilter
->reg_str
, VTY_NEWLINE
);
660 DEFUN (show_ip_as_path_access_list
,
661 show_ip_as_path_access_list_cmd
,
662 "show ip as-path-access-list WORD",
665 "List AS path access lists\n"
666 "AS path access list name\n")
668 struct as_list
*aslist
;
670 aslist
= as_list_lookup (argv
[0]);
672 as_list_show (vty
, aslist
);
677 DEFUN (show_ip_as_path_access_list_all
,
678 show_ip_as_path_access_list_all_cmd
,
679 "show ip as-path-access-list",
682 "List AS path access lists\n")
684 as_list_show_all (vty
);
689 config_write_as_list (struct vty
*vty
)
691 struct as_list
*aslist
;
692 struct as_filter
*asfilter
;
695 for (aslist
= as_list_master
.num
.head
; aslist
; aslist
= aslist
->next
)
696 for (asfilter
= aslist
->head
; asfilter
; asfilter
= asfilter
->next
)
698 vty_out (vty
, "ip as-path access-list %s %s %s%s",
699 aslist
->name
, filter_type_str (asfilter
->type
),
705 for (aslist
= as_list_master
.str
.head
; aslist
; aslist
= aslist
->next
)
706 for (asfilter
= aslist
->head
; asfilter
; asfilter
= asfilter
->next
)
708 vty_out (vty
, "ip as-path access-list %s %s %s%s",
709 aslist
->name
, filter_type_str (asfilter
->type
),
717 struct cmd_node as_list_node
=
724 /* Register functions. */
728 install_node (&as_list_node
, config_write_as_list
);
730 install_element (CONFIG_NODE
, &ip_as_path_cmd
);
731 install_element (CONFIG_NODE
, &no_ip_as_path_cmd
);
732 install_element (CONFIG_NODE
, &no_ip_as_path_all_cmd
);
734 install_element (VIEW_NODE
, &show_ip_as_path_access_list_cmd
);
735 install_element (VIEW_NODE
, &show_ip_as_path_access_list_all_cmd
);
736 install_element (ENABLE_NODE
, &show_ip_as_path_access_list_cmd
);
737 install_element (ENABLE_NODE
, &show_ip_as_path_access_list_all_cmd
);