]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* AS path filter list. |
896014f4 DL |
2 | * Copyright (C) 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 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 | |
9 | * 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 | */ | |
718e3744 | 20 | |
21 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
24 | #include "log.h" | |
25 | #include "memory.h" | |
26 | #include "buffer.h" | |
3f9c7369 | 27 | #include "queue.h" |
039f3a34 | 28 | #include "filter.h" |
718e3744 | 29 | |
30 | #include "bgpd/bgpd.h" | |
31 | #include "bgpd/bgp_aspath.h" | |
32 | #include "bgpd/bgp_regex.h" | |
33 | #include "bgpd/bgp_filter.h" | |
34 | ||
35 | /* List of AS filter list. */ | |
d62a17ae | 36 | struct as_list_list { |
37 | struct as_list *head; | |
38 | struct as_list *tail; | |
718e3744 | 39 | }; |
40 | ||
41 | /* AS path filter master. */ | |
d62a17ae | 42 | struct as_list_master { |
43 | /* List of access_list which name is number. */ | |
44 | struct as_list_list num; | |
718e3744 | 45 | |
d62a17ae | 46 | /* List of access_list which name is string. */ |
47 | struct as_list_list str; | |
718e3744 | 48 | |
d62a17ae | 49 | /* Hook function which is executed when new access_list is added. */ |
50 | void (*add_hook)(char *); | |
718e3744 | 51 | |
d62a17ae | 52 | /* Hook function which is executed when access_list is deleted. */ |
53 | void (*delete_hook)(const char *); | |
718e3744 | 54 | }; |
55 | ||
56 | /* Element of AS path filter. */ | |
d62a17ae | 57 | struct as_filter { |
58 | struct as_filter *next; | |
59 | struct as_filter *prev; | |
718e3744 | 60 | |
d62a17ae | 61 | enum as_filter_type type; |
718e3744 | 62 | |
d62a17ae | 63 | regex_t *reg; |
64 | char *reg_str; | |
718e3744 | 65 | }; |
66 | ||
718e3744 | 67 | /* AS path filter list. */ |
d62a17ae | 68 | struct as_list { |
69 | char *name; | |
718e3744 | 70 | |
d62a17ae | 71 | enum access_type type; |
718e3744 | 72 | |
d62a17ae | 73 | struct as_list *next; |
74 | struct as_list *prev; | |
718e3744 | 75 | |
d62a17ae | 76 | struct as_filter *head; |
77 | struct as_filter *tail; | |
718e3744 | 78 | }; |
6b0655a2 | 79 | |
7336e101 | 80 | /* as-path access-list 10 permit AS1. */ |
718e3744 | 81 | |
d62a17ae | 82 | static struct as_list_master as_list_master = {{NULL, NULL}, |
83 | {NULL, NULL}, | |
84 | NULL, | |
85 | NULL}; | |
718e3744 | 86 | |
87 | /* Allocate new AS filter. */ | |
d62a17ae | 88 | static struct as_filter *as_filter_new(void) |
718e3744 | 89 | { |
d62a17ae | 90 | return XCALLOC(MTYPE_AS_FILTER, sizeof(struct as_filter)); |
718e3744 | 91 | } |
92 | ||
93 | /* Free allocated AS filter. */ | |
d62a17ae | 94 | static void as_filter_free(struct as_filter *asfilter) |
718e3744 | 95 | { |
d62a17ae | 96 | if (asfilter->reg) |
97 | bgp_regex_free(asfilter->reg); | |
98 | if (asfilter->reg_str) | |
99 | XFREE(MTYPE_AS_FILTER_STR, asfilter->reg_str); | |
100 | XFREE(MTYPE_AS_FILTER, asfilter); | |
718e3744 | 101 | } |
102 | ||
103 | /* Make new AS filter. */ | |
d62a17ae | 104 | static struct as_filter *as_filter_make(regex_t *reg, const char *reg_str, |
105 | enum as_filter_type type) | |
718e3744 | 106 | { |
d62a17ae | 107 | struct as_filter *asfilter; |
718e3744 | 108 | |
d62a17ae | 109 | asfilter = as_filter_new(); |
110 | asfilter->reg = reg; | |
111 | asfilter->type = type; | |
112 | asfilter->reg_str = XSTRDUP(MTYPE_AS_FILTER_STR, reg_str); | |
718e3744 | 113 | |
d62a17ae | 114 | return asfilter; |
718e3744 | 115 | } |
116 | ||
d62a17ae | 117 | static struct as_filter *as_filter_lookup(struct as_list *aslist, |
118 | const char *reg_str, | |
119 | enum as_filter_type type) | |
718e3744 | 120 | { |
d62a17ae | 121 | struct as_filter *asfilter; |
718e3744 | 122 | |
d62a17ae | 123 | for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) |
124 | if (strcmp(reg_str, asfilter->reg_str) == 0) | |
125 | return asfilter; | |
126 | return NULL; | |
718e3744 | 127 | } |
128 | ||
d62a17ae | 129 | static void as_list_filter_add(struct as_list *aslist, |
130 | struct as_filter *asfilter) | |
718e3744 | 131 | { |
d62a17ae | 132 | asfilter->next = NULL; |
133 | asfilter->prev = aslist->tail; | |
718e3744 | 134 | |
d62a17ae | 135 | if (aslist->tail) |
136 | aslist->tail->next = asfilter; | |
137 | else | |
138 | aslist->head = asfilter; | |
139 | aslist->tail = asfilter; | |
518f0eb1 | 140 | |
d62a17ae | 141 | /* Run hook function. */ |
142 | if (as_list_master.add_hook) | |
143 | (*as_list_master.add_hook)(aslist->name); | |
718e3744 | 144 | } |
145 | ||
146 | /* Lookup as_list from list of as_list by name. */ | |
d62a17ae | 147 | struct as_list *as_list_lookup(const char *name) |
718e3744 | 148 | { |
d62a17ae | 149 | struct as_list *aslist; |
718e3744 | 150 | |
d62a17ae | 151 | if (name == NULL) |
152 | return NULL; | |
718e3744 | 153 | |
d62a17ae | 154 | for (aslist = as_list_master.num.head; aslist; aslist = aslist->next) |
155 | if (strcmp(aslist->name, name) == 0) | |
156 | return aslist; | |
718e3744 | 157 | |
d62a17ae | 158 | for (aslist = as_list_master.str.head; aslist; aslist = aslist->next) |
159 | if (strcmp(aslist->name, name) == 0) | |
160 | return aslist; | |
718e3744 | 161 | |
d62a17ae | 162 | return NULL; |
718e3744 | 163 | } |
164 | ||
d62a17ae | 165 | static struct as_list *as_list_new(void) |
718e3744 | 166 | { |
d62a17ae | 167 | return XCALLOC(MTYPE_AS_LIST, sizeof(struct as_list)); |
718e3744 | 168 | } |
169 | ||
d62a17ae | 170 | static void as_list_free(struct as_list *aslist) |
718e3744 | 171 | { |
d62a17ae | 172 | if (aslist->name) { |
173 | XFREE(MTYPE_AS_STR, aslist->name); | |
174 | aslist->name = NULL; | |
175 | } | |
176 | XFREE(MTYPE_AS_LIST, aslist); | |
718e3744 | 177 | } |
178 | ||
179 | /* Insert new AS list to list of as_list. Each as_list is sorted by | |
180 | the name. */ | |
d62a17ae | 181 | static struct as_list *as_list_insert(const char *name) |
182 | { | |
183 | size_t i; | |
184 | long number; | |
185 | struct as_list *aslist; | |
186 | struct as_list *point; | |
187 | struct as_list_list *list; | |
188 | ||
189 | /* Allocate new access_list and copy given name. */ | |
190 | aslist = as_list_new(); | |
191 | aslist->name = XSTRDUP(MTYPE_AS_STR, name); | |
192 | assert(aslist->name); | |
193 | ||
194 | /* If name is made by all digit character. We treat it as | |
195 | number. */ | |
196 | for (number = 0, i = 0; i < strlen(name); i++) { | |
197 | if (isdigit((int)name[i])) | |
198 | number = (number * 10) + (name[i] - '0'); | |
199 | else | |
200 | break; | |
201 | } | |
202 | ||
203 | /* In case of name is all digit character */ | |
204 | if (i == strlen(name)) { | |
205 | aslist->type = ACCESS_TYPE_NUMBER; | |
206 | ||
207 | /* Set access_list to number list. */ | |
208 | list = &as_list_master.num; | |
209 | ||
210 | for (point = list->head; point; point = point->next) | |
211 | if (atol(point->name) >= number) | |
212 | break; | |
213 | } else { | |
214 | aslist->type = ACCESS_TYPE_STRING; | |
215 | ||
216 | /* Set access_list to string list. */ | |
217 | list = &as_list_master.str; | |
218 | ||
219 | /* Set point to insertion point. */ | |
220 | for (point = list->head; point; point = point->next) | |
221 | if (strcmp(point->name, name) >= 0) | |
222 | break; | |
223 | } | |
224 | ||
225 | /* In case of this is the first element of master. */ | |
226 | if (list->head == NULL) { | |
227 | list->head = list->tail = aslist; | |
228 | return aslist; | |
229 | } | |
230 | ||
231 | /* In case of insertion is made at the tail of access_list. */ | |
232 | if (point == NULL) { | |
233 | aslist->prev = list->tail; | |
234 | list->tail->next = aslist; | |
235 | list->tail = aslist; | |
236 | return aslist; | |
237 | } | |
238 | ||
239 | /* In case of insertion is made at the head of access_list. */ | |
240 | if (point == list->head) { | |
241 | aslist->next = list->head; | |
242 | list->head->prev = aslist; | |
243 | list->head = aslist; | |
244 | return aslist; | |
245 | } | |
246 | ||
247 | /* Insertion is made at middle of the access_list. */ | |
248 | aslist->next = point; | |
249 | aslist->prev = point->prev; | |
250 | ||
251 | if (point->prev) | |
252 | point->prev->next = aslist; | |
253 | point->prev = aslist; | |
254 | ||
255 | return aslist; | |
718e3744 | 256 | } |
257 | ||
d62a17ae | 258 | static struct as_list *as_list_get(const char *name) |
718e3744 | 259 | { |
d62a17ae | 260 | struct as_list *aslist; |
718e3744 | 261 | |
d62a17ae | 262 | aslist = as_list_lookup(name); |
263 | if (aslist == NULL) | |
264 | aslist = as_list_insert(name); | |
718e3744 | 265 | |
d62a17ae | 266 | return aslist; |
718e3744 | 267 | } |
268 | ||
d62a17ae | 269 | static const char *filter_type_str(enum as_filter_type type) |
718e3744 | 270 | { |
d62a17ae | 271 | switch (type) { |
272 | case AS_FILTER_PERMIT: | |
273 | return "permit"; | |
274 | case AS_FILTER_DENY: | |
275 | return "deny"; | |
276 | default: | |
277 | return ""; | |
278 | } | |
718e3744 | 279 | } |
280 | ||
d62a17ae | 281 | static void as_list_delete(struct as_list *aslist) |
718e3744 | 282 | { |
d62a17ae | 283 | struct as_list_list *list; |
284 | struct as_filter *filter, *next; | |
285 | ||
286 | for (filter = aslist->head; filter; filter = next) { | |
287 | next = filter->next; | |
288 | as_filter_free(filter); | |
289 | } | |
290 | ||
291 | if (aslist->type == ACCESS_TYPE_NUMBER) | |
292 | list = &as_list_master.num; | |
293 | else | |
294 | list = &as_list_master.str; | |
295 | ||
296 | if (aslist->next) | |
297 | aslist->next->prev = aslist->prev; | |
298 | else | |
299 | list->tail = aslist->prev; | |
300 | ||
301 | if (aslist->prev) | |
302 | aslist->prev->next = aslist->next; | |
303 | else | |
304 | list->head = aslist->next; | |
305 | ||
306 | as_list_free(aslist); | |
718e3744 | 307 | } |
308 | ||
d62a17ae | 309 | static int as_list_empty(struct as_list *aslist) |
718e3744 | 310 | { |
d62a17ae | 311 | if (aslist->head == NULL && aslist->tail == NULL) |
312 | return 1; | |
313 | else | |
314 | return 0; | |
718e3744 | 315 | } |
316 | ||
d62a17ae | 317 | static void as_list_filter_delete(struct as_list *aslist, |
318 | struct as_filter *asfilter) | |
718e3744 | 319 | { |
d62a17ae | 320 | char *name = XSTRDUP(MTYPE_AS_STR, aslist->name); |
518f0eb1 | 321 | |
d62a17ae | 322 | if (asfilter->next) |
323 | asfilter->next->prev = asfilter->prev; | |
324 | else | |
325 | aslist->tail = asfilter->prev; | |
718e3744 | 326 | |
d62a17ae | 327 | if (asfilter->prev) |
328 | asfilter->prev->next = asfilter->next; | |
329 | else | |
330 | aslist->head = asfilter->next; | |
718e3744 | 331 | |
d62a17ae | 332 | as_filter_free(asfilter); |
718e3744 | 333 | |
d62a17ae | 334 | /* If access_list becomes empty delete it from access_master. */ |
335 | if (as_list_empty(aslist)) | |
336 | as_list_delete(aslist); | |
718e3744 | 337 | |
d62a17ae | 338 | /* Run hook function. */ |
339 | if (as_list_master.delete_hook) | |
340 | (*as_list_master.delete_hook)(name); | |
341 | if (name) | |
342 | XFREE(MTYPE_AS_STR, name); | |
718e3744 | 343 | } |
6b0655a2 | 344 | |
d62a17ae | 345 | static int as_filter_match(struct as_filter *asfilter, struct aspath *aspath) |
718e3744 | 346 | { |
d62a17ae | 347 | if (bgp_regexec(asfilter->reg, aspath) != REG_NOMATCH) |
348 | return 1; | |
349 | return 0; | |
718e3744 | 350 | } |
351 | ||
352 | /* Apply AS path filter to AS. */ | |
d62a17ae | 353 | enum as_filter_type as_list_apply(struct as_list *aslist, void *object) |
718e3744 | 354 | { |
d62a17ae | 355 | struct as_filter *asfilter; |
356 | struct aspath *aspath; | |
718e3744 | 357 | |
d62a17ae | 358 | aspath = (struct aspath *)object; |
718e3744 | 359 | |
d62a17ae | 360 | if (aslist == NULL) |
361 | return AS_FILTER_DENY; | |
718e3744 | 362 | |
d62a17ae | 363 | for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { |
364 | if (as_filter_match(asfilter, aspath)) | |
365 | return asfilter->type; | |
366 | } | |
367 | return AS_FILTER_DENY; | |
718e3744 | 368 | } |
369 | ||
370 | /* Add hook function. */ | |
d62a17ae | 371 | void as_list_add_hook(void (*func)(char *)) |
718e3744 | 372 | { |
d62a17ae | 373 | as_list_master.add_hook = func; |
718e3744 | 374 | } |
375 | ||
376 | /* Delete hook function. */ | |
d62a17ae | 377 | void as_list_delete_hook(void (*func)(const char *)) |
718e3744 | 378 | { |
d62a17ae | 379 | as_list_master.delete_hook = func; |
718e3744 | 380 | } |
6b0655a2 | 381 | |
d62a17ae | 382 | static int as_list_dup_check(struct as_list *aslist, struct as_filter *new) |
718e3744 | 383 | { |
d62a17ae | 384 | struct as_filter *asfilter; |
385 | ||
386 | for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { | |
387 | if (asfilter->type == new->type | |
388 | && strcmp(asfilter->reg_str, new->reg_str) == 0) | |
389 | return 1; | |
390 | } | |
391 | return 0; | |
718e3744 | 392 | } |
393 | ||
672c2d75 DA |
394 | static int config_bgp_aspath_validate(const char *regstr) |
395 | { | |
396 | char valid_chars[] = "1234567890_^|[,{}() ]$*+.?-"; | |
397 | ||
398 | if (strspn(regstr, valid_chars) == strlen(regstr)) | |
399 | return 1; | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
7336e101 SP |
404 | DEFUN(as_path, bgp_as_path_cmd, |
405 | "bgp as-path access-list WORD <deny|permit> LINE...", | |
406 | BGP_STR | |
672c2d75 DA |
407 | "BGP autonomous system path filter\n" |
408 | "Specify an access list name\n" | |
409 | "Regular expression access list name\n" | |
410 | "Specify packets to reject\n" | |
411 | "Specify packets to forward\n" | |
412 | "A regular-expression (1234567890_(^|[,{}() ]|$)) to match the BGP AS paths\n") | |
718e3744 | 413 | { |
d62a17ae | 414 | int idx = 0; |
415 | enum as_filter_type type; | |
416 | struct as_filter *asfilter; | |
417 | struct as_list *aslist; | |
418 | regex_t *regex; | |
419 | char *regstr; | |
420 | ||
7336e101 SP |
421 | if (argv_find(argv, argc, "ip", &idx)) { |
422 | vty_out(vty, "This config option is deprecated and is scheduled for removal.\n"); | |
423 | vty_out(vty, "if you are using this please migrate to the below command\n"); | |
424 | vty_out(vty, "'bgp as-path access-list WORD <deny|permit> LINE'\n"); | |
425 | zlog_warn("Deprecated option: 'ip as-path access-list WORD <deny|permit> LINE' being used"); | |
426 | } | |
427 | ||
d62a17ae | 428 | /* Retrieve access list name */ |
e991f75c VJ |
429 | argv_find(argv, argc, "WORD", &idx); |
430 | char *alname = argv[idx]->arg; | |
d62a17ae | 431 | |
432 | /* Check the filter type. */ | |
433 | type = argv_find(argv, argc, "deny", &idx) ? AS_FILTER_DENY | |
434 | : AS_FILTER_PERMIT; | |
435 | ||
436 | /* Check AS path regex. */ | |
437 | argv_find(argv, argc, "LINE", &idx); | |
438 | regstr = argv_concat(argv, argc, idx); | |
439 | ||
440 | regex = bgp_regcomp(regstr); | |
441 | if (!regex) { | |
442 | vty_out(vty, "can't compile regexp %s\n", regstr); | |
443 | XFREE(MTYPE_TMP, regstr); | |
444 | return CMD_WARNING_CONFIG_FAILED; | |
445 | } | |
446 | ||
672c2d75 DA |
447 | if (!config_bgp_aspath_validate(regstr)) { |
448 | vty_out(vty, "Invalid character in as-path access-list %s\n", | |
449 | regstr); | |
450 | return CMD_WARNING_CONFIG_FAILED; | |
451 | } | |
452 | ||
d62a17ae | 453 | asfilter = as_filter_make(regex, regstr, type); |
454 | ||
455 | XFREE(MTYPE_TMP, regstr); | |
456 | ||
457 | /* Install new filter to the access_list. */ | |
458 | aslist = as_list_get(alname); | |
459 | ||
460 | /* Duplicate insertion check. */; | |
461 | if (as_list_dup_check(aslist, asfilter)) | |
462 | as_filter_free(asfilter); | |
463 | else | |
464 | as_list_filter_add(aslist, asfilter); | |
465 | ||
466 | return CMD_SUCCESS; | |
718e3744 | 467 | } |
468 | ||
7336e101 SP |
469 | #if CONFDATE > 20191005 |
470 | CPP_NOTICE("bgpd: remove deprecated 'ip as-path access-list WORD <deny|permit> LINE' command") | |
471 | #endif | |
472 | ALIAS(as_path, ip_as_path_cmd, | |
473 | "ip as-path access-list WORD <deny|permit> LINE...", | |
474 | IP_STR | |
475 | "BGP autonomous system path filter\n" | |
476 | "Specify an access list name\n" | |
477 | "Regular expression access list name\n" | |
478 | "Specify packets to reject\n" | |
479 | "Specify packets to forward\n" | |
480 | "A regular-expression (1234567890_(^|[,{}() ]|$)) to match the BGP AS paths\n") | |
481 | ||
482 | DEFUN(no_as_path, no_bgp_as_path_cmd, | |
483 | "no bgp as-path access-list WORD <deny|permit> LINE...", | |
484 | NO_STR | |
485 | BGP_STR | |
672c2d75 DA |
486 | "BGP autonomous system path filter\n" |
487 | "Specify an access list name\n" | |
488 | "Regular expression access list name\n" | |
489 | "Specify packets to reject\n" | |
490 | "Specify packets to forward\n" | |
491 | "A regular-expression (1234567890_(^|[,{}() ]|$)) to match the BGP AS paths\n") | |
718e3744 | 492 | { |
d62a17ae | 493 | int idx = 0; |
494 | enum as_filter_type type; | |
495 | struct as_filter *asfilter; | |
496 | struct as_list *aslist; | |
497 | char *regstr; | |
498 | regex_t *regex; | |
499 | ||
7336e101 SP |
500 | if (argv_find(argv, argc, "ip", &idx)) { |
501 | vty_out(vty, "This config option is deprecated, and is scheduled for removal.\n"); | |
502 | vty_out(vty, "if you are using this please migrate to the below command\n"); | |
503 | vty_out(vty, "'no bgp as-path access-list WORD <deny|permit> LINE'\n"); | |
504 | zlog_warn("Deprecated option: 'no ip as-path access-list WORD <deny|permit> LINE' being used"); | |
505 | } | |
d62a17ae | 506 | char *aslistname = |
507 | argv_find(argv, argc, "WORD", &idx) ? argv[idx]->arg : NULL; | |
508 | ||
509 | /* Lookup AS list from AS path list. */ | |
510 | aslist = as_list_lookup(aslistname); | |
511 | if (aslist == NULL) { | |
7336e101 | 512 | vty_out(vty, "bgp as-path access-list %s doesn't exist\n", |
d62a17ae | 513 | aslistname); |
514 | return CMD_WARNING_CONFIG_FAILED; | |
515 | } | |
516 | ||
517 | /* Check the filter type. */ | |
518 | if (argv_find(argv, argc, "permit", &idx)) | |
519 | type = AS_FILTER_PERMIT; | |
520 | else if (argv_find(argv, argc, "deny", &idx)) | |
521 | type = AS_FILTER_DENY; | |
522 | else { | |
523 | vty_out(vty, "filter type must be [permit|deny]\n"); | |
524 | return CMD_WARNING_CONFIG_FAILED; | |
525 | } | |
526 | ||
527 | /* Compile AS path. */ | |
528 | argv_find(argv, argc, "LINE", &idx); | |
529 | regstr = argv_concat(argv, argc, idx); | |
530 | ||
672c2d75 DA |
531 | if (!config_bgp_aspath_validate(regstr)) { |
532 | vty_out(vty, "Invalid character in as-path access-list %s\n", | |
533 | regstr); | |
534 | return CMD_WARNING_CONFIG_FAILED; | |
535 | } | |
536 | ||
d62a17ae | 537 | regex = bgp_regcomp(regstr); |
538 | if (!regex) { | |
539 | vty_out(vty, "can't compile regexp %s\n", regstr); | |
540 | XFREE(MTYPE_TMP, regstr); | |
541 | return CMD_WARNING_CONFIG_FAILED; | |
542 | } | |
543 | ||
544 | /* Lookup asfilter. */ | |
545 | asfilter = as_filter_lookup(aslist, regstr, type); | |
546 | ||
547 | XFREE(MTYPE_TMP, regstr); | |
548 | bgp_regex_free(regex); | |
549 | ||
550 | if (asfilter == NULL) { | |
551 | vty_out(vty, "\n"); | |
552 | return CMD_WARNING_CONFIG_FAILED; | |
553 | } | |
554 | ||
555 | as_list_filter_delete(aslist, asfilter); | |
556 | ||
557 | return CMD_SUCCESS; | |
718e3744 | 558 | } |
559 | ||
7336e101 SP |
560 | ALIAS(no_as_path, no_ip_as_path_cmd, |
561 | "no ip as-path access-list WORD <deny|permit> LINE...", | |
562 | NO_STR IP_STR | |
563 | "BGP autonomous system path filter\n" | |
564 | "Specify an access list name\n" | |
565 | "Regular expression access list name\n" | |
566 | "Specify packets to reject\n" | |
567 | "Specify packets to forward\n" | |
568 | "A regular-expression (1234567890_(^|[,{}() ]|$)) to match the BGP AS paths\n") | |
569 | ||
570 | DEFUN (no_as_path_all, | |
571 | no_bgp_as_path_all_cmd, | |
572 | "no bgp as-path access-list WORD", | |
718e3744 | 573 | NO_STR |
7336e101 | 574 | BGP_STR |
718e3744 | 575 | "BGP autonomous system path filter\n" |
576 | "Specify an access list name\n" | |
577 | "Regular expression access list name\n") | |
578 | { | |
d62a17ae | 579 | int idx_word = 4; |
580 | struct as_list *aslist; | |
7336e101 SP |
581 | int idx = 0; |
582 | ||
583 | if (argv_find(argv, argc, "ip", &idx)) { | |
584 | vty_out(vty, "This config option is deprecated, and is scheduled for removal.\n"); | |
585 | vty_out(vty, "if you are using this please migrate to the below command\n"); | |
586 | vty_out(vty, "'no bgp as-path access-list WORD'\n"); | |
587 | zlog_warn("Deprecated option: `no ip as-path access-list WORD` being used"); | |
588 | } | |
718e3744 | 589 | |
d62a17ae | 590 | aslist = as_list_lookup(argv[idx_word]->arg); |
591 | if (aslist == NULL) { | |
7336e101 | 592 | vty_out(vty, "bgp as-path access-list %s doesn't exist\n", |
d62a17ae | 593 | argv[idx_word]->arg); |
594 | return CMD_WARNING_CONFIG_FAILED; | |
595 | } | |
718e3744 | 596 | |
d62a17ae | 597 | as_list_delete(aslist); |
718e3744 | 598 | |
d62a17ae | 599 | /* Run hook function. */ |
600 | if (as_list_master.delete_hook) | |
601 | (*as_list_master.delete_hook)(argv[idx_word]->arg); | |
e0701b79 | 602 | |
d62a17ae | 603 | return CMD_SUCCESS; |
718e3744 | 604 | } |
605 | ||
7336e101 SP |
606 | ALIAS (no_as_path_all, |
607 | no_ip_as_path_all_cmd, | |
608 | "no ip as-path access-list WORD", | |
609 | NO_STR | |
610 | IP_STR | |
611 | "BGP autonomous system path filter\n" | |
612 | "Specify an access list name\n" | |
613 | "Regular expression access list name\n") | |
614 | ||
d62a17ae | 615 | static void as_list_show(struct vty *vty, struct as_list *aslist) |
4f991ef0 | 616 | { |
d62a17ae | 617 | struct as_filter *asfilter; |
4f991ef0 | 618 | |
d62a17ae | 619 | vty_out(vty, "AS path access list %s\n", aslist->name); |
4f991ef0 | 620 | |
d62a17ae | 621 | for (asfilter = aslist->head; asfilter; asfilter = asfilter->next) { |
622 | vty_out(vty, " %s %s\n", filter_type_str(asfilter->type), | |
623 | asfilter->reg_str); | |
624 | } | |
4f991ef0 | 625 | } |
626 | ||
d62a17ae | 627 | static void as_list_show_all(struct vty *vty) |
4f991ef0 | 628 | { |
d62a17ae | 629 | struct as_list *aslist; |
630 | struct as_filter *asfilter; | |
4f991ef0 | 631 | |
d62a17ae | 632 | for (aslist = as_list_master.num.head; aslist; aslist = aslist->next) { |
633 | vty_out(vty, "AS path access list %s\n", aslist->name); | |
4f991ef0 | 634 | |
d62a17ae | 635 | for (asfilter = aslist->head; asfilter; |
636 | asfilter = asfilter->next) { | |
637 | vty_out(vty, " %s %s\n", | |
638 | filter_type_str(asfilter->type), | |
639 | asfilter->reg_str); | |
640 | } | |
4f991ef0 | 641 | } |
4f991ef0 | 642 | |
d62a17ae | 643 | for (aslist = as_list_master.str.head; aslist; aslist = aslist->next) { |
644 | vty_out(vty, "AS path access list %s\n", aslist->name); | |
4f991ef0 | 645 | |
d62a17ae | 646 | for (asfilter = aslist->head; asfilter; |
647 | asfilter = asfilter->next) { | |
648 | vty_out(vty, " %s %s\n", | |
649 | filter_type_str(asfilter->type), | |
650 | asfilter->reg_str); | |
651 | } | |
4f991ef0 | 652 | } |
4f991ef0 | 653 | } |
654 | ||
7336e101 SP |
655 | DEFUN (show_as_path_access_list, |
656 | show_bgp_as_path_access_list_cmd, | |
657 | "show bgp as-path-access-list WORD", | |
4f991ef0 | 658 | SHOW_STR |
7336e101 | 659 | BGP_STR |
4f991ef0 | 660 | "List AS path access lists\n" |
661 | "AS path access list name\n") | |
662 | { | |
d62a17ae | 663 | int idx_word = 3; |
664 | struct as_list *aslist; | |
7336e101 | 665 | int idx = 0; |
4f991ef0 | 666 | |
7336e101 SP |
667 | if (argv_find(argv, argc, "ip", &idx)) { |
668 | vty_out(vty, "This config option is deprecated, and is scheduled for removal.\n"); | |
669 | vty_out(vty, "if you are using this please migrate to the below command\n"); | |
670 | vty_out(vty, "'show bgp as-path-access-list WORD'\n"); | |
671 | zlog_warn("Deprecated option: 'show ip as-path-access-list WORD' being used"); | |
672 | } | |
d62a17ae | 673 | aslist = as_list_lookup(argv[idx_word]->arg); |
674 | if (aslist) | |
675 | as_list_show(vty, aslist); | |
4f991ef0 | 676 | |
d62a17ae | 677 | return CMD_SUCCESS; |
4f991ef0 | 678 | } |
679 | ||
7336e101 SP |
680 | ALIAS (show_as_path_access_list, |
681 | show_ip_as_path_access_list_cmd, | |
682 | "show ip as-path-access-list WORD", | |
4f991ef0 | 683 | SHOW_STR |
684 | IP_STR | |
7336e101 SP |
685 | "List AS path access lists\n" |
686 | "AS path access list name\n") | |
687 | ||
688 | DEFUN (show_as_path_access_list_all, | |
689 | show_bgp_as_path_access_list_all_cmd, | |
690 | "show bgp as-path-access-list", | |
691 | SHOW_STR | |
692 | BGP_STR | |
4f991ef0 | 693 | "List AS path access lists\n") |
694 | { | |
7336e101 SP |
695 | int idx = 0; |
696 | ||
697 | if (argv_find(argv, argc, "ip", &idx)) { | |
698 | vty_out(vty, "This config option is deprecated, and is scheduled for removal.\n"); | |
699 | vty_out(vty, "if you are using this please migrate to the below command\n"); | |
700 | vty_out(vty, "'show bgp as-path-access-list'\n"); | |
701 | zlog_warn("Deprecated option: 'show ip as-path-access-list' being used"); | |
702 | } | |
d62a17ae | 703 | as_list_show_all(vty); |
704 | return CMD_SUCCESS; | |
4f991ef0 | 705 | } |
706 | ||
7336e101 SP |
707 | ALIAS (show_as_path_access_list_all, |
708 | show_ip_as_path_access_list_all_cmd, | |
709 | "show ip as-path-access-list", | |
710 | SHOW_STR | |
711 | IP_STR | |
712 | "List AS path access lists\n") | |
713 | ||
d62a17ae | 714 | static int config_write_as_list(struct vty *vty) |
715 | { | |
716 | struct as_list *aslist; | |
717 | struct as_filter *asfilter; | |
718 | int write = 0; | |
719 | ||
720 | for (aslist = as_list_master.num.head; aslist; aslist = aslist->next) | |
721 | for (asfilter = aslist->head; asfilter; | |
722 | asfilter = asfilter->next) { | |
7336e101 | 723 | vty_out(vty, "bgp as-path access-list %s %s %s\n", |
d62a17ae | 724 | aslist->name, filter_type_str(asfilter->type), |
725 | asfilter->reg_str); | |
726 | write++; | |
727 | } | |
728 | ||
729 | for (aslist = as_list_master.str.head; aslist; aslist = aslist->next) | |
730 | for (asfilter = aslist->head; asfilter; | |
731 | asfilter = asfilter->next) { | |
7336e101 | 732 | vty_out(vty, "bgp as-path access-list %s %s %s\n", |
d62a17ae | 733 | aslist->name, filter_type_str(asfilter->type), |
734 | asfilter->reg_str); | |
735 | write++; | |
736 | } | |
737 | return write; | |
718e3744 | 738 | } |
739 | ||
d62a17ae | 740 | static struct cmd_node as_list_node = {AS_LIST_NODE, "", 1}; |
718e3744 | 741 | |
742 | /* Register functions. */ | |
d62a17ae | 743 | void bgp_filter_init(void) |
718e3744 | 744 | { |
d62a17ae | 745 | install_node(&as_list_node, config_write_as_list); |
718e3744 | 746 | |
7336e101 | 747 | install_element(CONFIG_NODE, &bgp_as_path_cmd); |
d62a17ae | 748 | install_element(CONFIG_NODE, &ip_as_path_cmd); |
7336e101 | 749 | install_element(CONFIG_NODE, &no_bgp_as_path_cmd); |
d62a17ae | 750 | install_element(CONFIG_NODE, &no_ip_as_path_cmd); |
7336e101 | 751 | install_element(CONFIG_NODE, &no_bgp_as_path_all_cmd); |
d62a17ae | 752 | install_element(CONFIG_NODE, &no_ip_as_path_all_cmd); |
4f991ef0 | 753 | |
7336e101 | 754 | install_element(VIEW_NODE, &show_bgp_as_path_access_list_cmd); |
d62a17ae | 755 | install_element(VIEW_NODE, &show_ip_as_path_access_list_cmd); |
7336e101 | 756 | install_element(VIEW_NODE, &show_bgp_as_path_access_list_all_cmd); |
d62a17ae | 757 | install_element(VIEW_NODE, &show_ip_as_path_access_list_all_cmd); |
718e3744 | 758 | } |
228da428 | 759 | |
d62a17ae | 760 | void bgp_filter_reset(void) |
228da428 | 761 | { |
d62a17ae | 762 | struct as_list *aslist; |
763 | struct as_list *next; | |
764 | ||
765 | for (aslist = as_list_master.num.head; aslist; aslist = next) { | |
766 | next = aslist->next; | |
767 | as_list_delete(aslist); | |
768 | } | |
769 | ||
770 | for (aslist = as_list_master.str.head; aslist; aslist = next) { | |
771 | next = aslist->next; | |
772 | as_list_delete(aslist); | |
773 | } | |
774 | ||
775 | assert(as_list_master.num.head == NULL); | |
776 | assert(as_list_master.num.tail == NULL); | |
777 | ||
778 | assert(as_list_master.str.head == NULL); | |
779 | assert(as_list_master.str.tail == NULL); | |
228da428 | 780 | } |