📄 bgp_filter.c
字号:
if (as_list_empty (aslist)) as_list_delete (aslist); /* Run hook function. */ if (as_list_master.delete_hook) (*as_list_master.delete_hook) ();}static intas_filter_match (struct as_filter *asfilter, struct aspath *aspath){ if (bgp_regexec (asfilter->reg, aspath) != REG_NOMATCH) return 1; return 0;}/* Apply AS path filter to AS. */enum as_filter_typeas_list_apply (struct as_list *aslist, void *object){ struct as_filter *asfilter; struct aspath *aspath; aspath = (struct aspath *) object; if (aslist == NULL) return AS_FILTER_DENY; for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { if (as_filter_match (asfilter, aspath)) return asfilter->type; } return AS_FILTER_DENY;}/* Add hook function. */voidas_list_add_hook (void (*func) ()){ as_list_master.add_hook = func;}/* Delete hook function. */voidas_list_delete_hook (void (*func) ()){ as_list_master.delete_hook = func;}intas_list_dup_check (struct as_list *aslist, struct as_filter *new){ struct as_filter *asfilter; for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { if (asfilter->type == new->type && strcmp (asfilter->reg_str, new->reg_str) == 0) return 1; } return 0;}DEFUN (ip_as_path, ip_as_path_cmd, "ip as-path access-list WORD (deny|permit) .LINE", IP_STR "BGP autonomous system path filter\n" "Specify an access list name\n" "Regular expression access list name\n" "Specify packets to reject\n" "Specify packets to forward\n" "A regular-expression to match the BGP AS paths\n"){ enum as_filter_type type; struct as_filter *asfilter; struct as_list *aslist; regex_t *regex; struct buffer *b; int i; char *regstr; int first = 0; /* Check the filter type. */ if (strncmp (argv[1], "p", 1) == 0) type = AS_FILTER_PERMIT; else if (strncmp (argv[1], "d", 1) == 0) type = AS_FILTER_DENY; else { vty_out (vty, "filter type must be [permit|deny]%s", VTY_NEWLINE); return CMD_WARNING; } /* Check AS path regex. */ b = buffer_new (1024); for (i = 2; i < argc; i++) { if (first) buffer_putc (b, ' '); else first = 1; buffer_putstr (b, argv[i]); } buffer_putc (b, '\0'); regstr = buffer_getstr (b); buffer_free (b); regex = bgp_regcomp (regstr); if (!regex) { free (regstr); vty_out (vty, "can't compile regexp %s%s", argv[0], VTY_NEWLINE); return CMD_WARNING; } asfilter = as_filter_make (regex, regstr, type); free (regstr); /* Install new filter to the access_list. */ aslist = as_list_get (argv[0]); /* Duplicate insertion check. */; if (as_list_dup_check (aslist, asfilter)) as_filter_free (asfilter); else as_list_filter_add (aslist, asfilter); return CMD_SUCCESS;}DEFUN (no_ip_as_path, no_ip_as_path_cmd, "no ip as-path access-list WORD (deny|permit) .LINE", NO_STR IP_STR "BGP autonomous system path filter\n" "Specify an access list name\n" "Regular expression access list name\n" "Specify packets to reject\n" "Specify packets to forward\n" "A regular-expression to match the BGP AS paths\n"){ enum as_filter_type type; struct as_filter *asfilter; struct as_list *aslist; struct buffer *b; int i; int first = 0; char *regstr; regex_t *regex; /* Lookup AS list from AS path list. */ aslist = as_list_lookup (argv[0]); if (aslist == NULL) { vty_out (vty, "ip as-path access-list %s doesn't exist%s", argv[0], VTY_NEWLINE); return CMD_WARNING; } /* Check the filter type. */ if (strncmp (argv[1], "p", 1) == 0) type = AS_FILTER_PERMIT; else if (strncmp (argv[1], "d", 1) == 0) type = AS_FILTER_DENY; else { vty_out (vty, "filter type must be [permit|deny]%s", VTY_NEWLINE); return CMD_WARNING; } /* Compile AS path. */ b = buffer_new (1024); for (i = 2; i < argc; i++) { if (first) buffer_putc (b, ' '); else first = 1; buffer_putstr (b, argv[i]); } buffer_putc (b, '\0'); regstr = buffer_getstr (b); buffer_free (b); regex = bgp_regcomp (regstr); if (!regex) { free (regstr); vty_out (vty, "can't compile regexp %s%s", argv[0], VTY_NEWLINE); return CMD_WARNING; } /* Lookup asfilter. */ asfilter = as_filter_lookup (aslist, regstr, type); free (regstr); bgp_regex_free (regex); if (asfilter == NULL) { vty_out (vty, "%s", VTY_NEWLINE); return CMD_WARNING; } as_list_filter_delete (aslist, asfilter); return CMD_SUCCESS;}DEFUN (no_ip_as_path_all, no_ip_as_path_all_cmd, "no ip as-path access-list WORD", NO_STR IP_STR "BGP autonomous system path filter\n" "Specify an access list name\n" "Regular expression access list name\n"){ struct as_list *aslist; aslist = as_list_lookup (argv[0]); if (aslist == NULL) { vty_out (vty, "ip as-path access-list %s doesn't exist%s", argv[0], VTY_NEWLINE); return CMD_WARNING; } as_list_delete (aslist); /* Run hook function. */ if (as_list_master.delete_hook) (*as_list_master.delete_hook) (); return CMD_SUCCESS;}voidas_list_show (struct vty *vty, struct as_list *aslist){ struct as_filter *asfilter; vty_out (vty, "AS path access list %s%s", aslist->name, VTY_NEWLINE); for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { vty_out (vty, " %s %s%s", filter_type_str (asfilter->type), asfilter->reg_str, VTY_NEWLINE); }}voidas_list_show_all (struct vty *vty){ struct as_list *aslist; struct as_filter *asfilter; for (aslist = as_list_master.num.head; aslist; aslist = aslist->next) { vty_out (vty, "AS path access list %s%s", aslist->name, VTY_NEWLINE); for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { vty_out (vty, " %s %s%s", filter_type_str (asfilter->type), asfilter->reg_str, VTY_NEWLINE); } } for (aslist = as_list_master.str.head; aslist; aslist = aslist->next) { vty_out (vty, "AS path access list %s%s", aslist->name, VTY_NEWLINE); for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { vty_out (vty, " %s %s%s", filter_type_str (asfilter->type), asfilter->reg_str, VTY_NEWLINE); } }}DEFUN (show_ip_as_path_access_list, show_ip_as_path_access_list_cmd, "show ip as-path-access-list WORD", SHOW_STR IP_STR "List AS path access lists\n" "AS path access list name\n"){ struct as_list *aslist; aslist = as_list_lookup (argv[0]); if (aslist) as_list_show (vty, aslist); return CMD_SUCCESS;}DEFUN (show_ip_as_path_access_list_all, show_ip_as_path_access_list_all_cmd, "show ip as-path-access-list", SHOW_STR IP_STR "List AS path access lists\n"){ as_list_show_all (vty); return CMD_SUCCESS;}intconfig_write_as_list (struct vty *vty){ struct as_list *aslist; struct as_filter *asfilter; int write = 0; for (aslist = as_list_master.num.head; aslist; aslist = aslist->next) for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { vty_out (vty, "ip as-path access-list %s %s %s%s", aslist->name, filter_type_str (asfilter->type), asfilter->reg_str, VTY_NEWLINE); write++; } for (aslist = as_list_master.str.head; aslist; aslist = aslist->next) for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { vty_out (vty, "ip as-path access-list %s %s %s%s", aslist->name, filter_type_str (asfilter->type), asfilter->reg_str, VTY_NEWLINE); write++; } return write;}struct cmd_node as_list_node ={ AS_LIST_NODE, "", 1};/* Register functions. */voidbgp_filter_init (){ install_node (&as_list_node, config_write_as_list); install_element (CONFIG_NODE, &ip_as_path_cmd); install_element (CONFIG_NODE, &no_ip_as_path_cmd); install_element (CONFIG_NODE, &no_ip_as_path_all_cmd); install_element (VIEW_NODE, &show_ip_as_path_access_list_cmd); install_element (VIEW_NODE, &show_ip_as_path_access_list_all_cmd); install_element (ENABLE_NODE, &show_ip_as_path_access_list_cmd); install_element (ENABLE_NODE, &show_ip_as_path_access_list_all_cmd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -