]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* BGP community-list and extcommunity-list. |
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 | |
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 | |
19 | 02111-1307, USA. */ | |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
24 | #include "prefix.h" | |
25 | #include "memory.h" | |
3f9c7369 | 26 | #include "queue.h" |
039f3a34 | 27 | #include "filter.h" |
718e3744 | 28 | |
29 | #include "bgpd/bgpd.h" | |
30 | #include "bgpd/bgp_community.h" | |
31 | #include "bgpd/bgp_ecommunity.h" | |
32 | #include "bgpd/bgp_aspath.h" | |
33 | #include "bgpd/bgp_regex.h" | |
34 | #include "bgpd/bgp_clist.h" | |
6b0655a2 | 35 | |
718e3744 | 36 | /* Lookup master structure for community-list or |
37 | extcommunity-list. */ | |
38 | struct community_list_master * | |
fee6e4e4 | 39 | community_list_master_lookup (struct community_list_handler *ch, int master) |
718e3744 | 40 | { |
41 | if (ch) | |
fee6e4e4 | 42 | switch (master) |
718e3744 | 43 | { |
fee6e4e4 | 44 | case COMMUNITY_LIST_MASTER: |
45 | return &ch->community_list; | |
fee6e4e4 | 46 | case EXTCOMMUNITY_LIST_MASTER: |
47 | return &ch->extcommunity_list; | |
718e3744 | 48 | } |
49 | return NULL; | |
50 | } | |
51 | ||
52 | /* Allocate a new community list entry. */ | |
94f2b392 | 53 | static struct community_entry * |
66e5cd87 | 54 | community_entry_new (void) |
718e3744 | 55 | { |
393deb9b | 56 | return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry)); |
718e3744 | 57 | } |
58 | ||
59 | /* Free community list entry. */ | |
94f2b392 | 60 | static void |
718e3744 | 61 | community_entry_free (struct community_entry *entry) |
62 | { | |
63 | switch (entry->style) | |
64 | { | |
65 | case COMMUNITY_LIST_STANDARD: | |
66 | if (entry->u.com) | |
8708b74f | 67 | community_free (entry->u.com); |
718e3744 | 68 | break; |
69 | case EXTCOMMUNITY_LIST_STANDARD: | |
70 | /* In case of standard extcommunity-list, configuration string | |
8708b74f | 71 | is made by ecommunity_ecom2str(). */ |
718e3744 | 72 | if (entry->config) |
8708b74f | 73 | XFREE (MTYPE_ECOMMUNITY_STR, entry->config); |
718e3744 | 74 | if (entry->u.ecom) |
f6f434b2 | 75 | ecommunity_free (&entry->u.ecom); |
718e3744 | 76 | break; |
77 | case COMMUNITY_LIST_EXPANDED: | |
78 | case EXTCOMMUNITY_LIST_EXPANDED: | |
79 | if (entry->config) | |
8708b74f | 80 | XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config); |
718e3744 | 81 | if (entry->reg) |
8708b74f | 82 | bgp_regex_free (entry->reg); |
718e3744 | 83 | default: |
84 | break; | |
85 | } | |
86 | XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry); | |
87 | } | |
88 | ||
89 | /* Allocate a new community-list. */ | |
94f2b392 | 90 | static struct community_list * |
66e5cd87 | 91 | community_list_new (void) |
718e3744 | 92 | { |
393deb9b | 93 | return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list)); |
718e3744 | 94 | } |
95 | ||
96 | /* Free community-list. */ | |
94f2b392 | 97 | static void |
718e3744 | 98 | community_list_free (struct community_list *list) |
99 | { | |
100 | if (list->name) | |
101 | XFREE (MTYPE_COMMUNITY_LIST_NAME, list->name); | |
102 | XFREE (MTYPE_COMMUNITY_LIST, list); | |
103 | } | |
104 | ||
94f2b392 | 105 | static struct community_list * |
718e3744 | 106 | community_list_insert (struct community_list_handler *ch, |
fee6e4e4 | 107 | const char *name, int master) |
718e3744 | 108 | { |
fd79ac91 | 109 | size_t i; |
718e3744 | 110 | long number; |
111 | struct community_list *new; | |
112 | struct community_list *point; | |
113 | struct community_list_list *list; | |
114 | struct community_list_master *cm; | |
115 | ||
116 | /* Lookup community-list master. */ | |
fee6e4e4 | 117 | cm = community_list_master_lookup (ch, master); |
8708b74f | 118 | if (!cm) |
718e3744 | 119 | return NULL; |
120 | ||
121 | /* Allocate new community_list and copy given name. */ | |
122 | new = community_list_new (); | |
123 | new->name = XSTRDUP (MTYPE_COMMUNITY_LIST_NAME, name); | |
124 | ||
125 | /* If name is made by all digit character. We treat it as | |
126 | number. */ | |
127 | for (number = 0, i = 0; i < strlen (name); i++) | |
128 | { | |
129 | if (isdigit ((int) name[i])) | |
8708b74f | 130 | number = (number * 10) + (name[i] - '0'); |
718e3744 | 131 | else |
8708b74f | 132 | break; |
718e3744 | 133 | } |
134 | ||
135 | /* In case of name is all digit character */ | |
136 | if (i == strlen (name)) | |
137 | { | |
138 | new->sort = COMMUNITY_LIST_NUMBER; | |
139 | ||
140 | /* Set access_list to number list. */ | |
141 | list = &cm->num; | |
142 | ||
143 | for (point = list->head; point; point = point->next) | |
8708b74f | 144 | if (atol (point->name) >= number) |
145 | break; | |
718e3744 | 146 | } |
147 | else | |
148 | { | |
149 | new->sort = COMMUNITY_LIST_STRING; | |
150 | ||
151 | /* Set access_list to string list. */ | |
152 | list = &cm->str; | |
8708b74f | 153 | |
718e3744 | 154 | /* Set point to insertion point. */ |
155 | for (point = list->head; point; point = point->next) | |
8708b74f | 156 | if (strcmp (point->name, name) >= 0) |
157 | break; | |
718e3744 | 158 | } |
159 | ||
160 | /* Link to upper list. */ | |
161 | new->parent = list; | |
162 | ||
163 | /* In case of this is the first element of master. */ | |
164 | if (list->head == NULL) | |
165 | { | |
166 | list->head = list->tail = new; | |
167 | return new; | |
168 | } | |
169 | ||
170 | /* In case of insertion is made at the tail of access_list. */ | |
171 | if (point == NULL) | |
172 | { | |
173 | new->prev = list->tail; | |
174 | list->tail->next = new; | |
175 | list->tail = new; | |
176 | return new; | |
177 | } | |
178 | ||
179 | /* In case of insertion is made at the head of access_list. */ | |
180 | if (point == list->head) | |
181 | { | |
182 | new->next = list->head; | |
183 | list->head->prev = new; | |
184 | list->head = new; | |
185 | return new; | |
186 | } | |
187 | ||
188 | /* Insertion is made at middle of the access_list. */ | |
189 | new->next = point; | |
190 | new->prev = point->prev; | |
191 | ||
192 | if (point->prev) | |
193 | point->prev->next = new; | |
194 | point->prev = new; | |
195 | ||
196 | return new; | |
197 | } | |
198 | ||
199 | struct community_list * | |
200 | community_list_lookup (struct community_list_handler *ch, | |
fee6e4e4 | 201 | const char *name, int master) |
718e3744 | 202 | { |
203 | struct community_list *list; | |
204 | struct community_list_master *cm; | |
205 | ||
8708b74f | 206 | if (!name) |
718e3744 | 207 | return NULL; |
208 | ||
fee6e4e4 | 209 | cm = community_list_master_lookup (ch, master); |
8708b74f | 210 | if (!cm) |
718e3744 | 211 | return NULL; |
212 | ||
213 | for (list = cm->num.head; list; list = list->next) | |
214 | if (strcmp (list->name, name) == 0) | |
215 | return list; | |
216 | for (list = cm->str.head; list; list = list->next) | |
217 | if (strcmp (list->name, name) == 0) | |
218 | return list; | |
219 | ||
220 | return NULL; | |
221 | } | |
222 | ||
94f2b392 | 223 | static struct community_list * |
fee6e4e4 | 224 | community_list_get (struct community_list_handler *ch, |
225 | const char *name, int master) | |
718e3744 | 226 | { |
227 | struct community_list *list; | |
228 | ||
fee6e4e4 | 229 | list = community_list_lookup (ch, name, master); |
8708b74f | 230 | if (!list) |
fee6e4e4 | 231 | list = community_list_insert (ch, name, master); |
718e3744 | 232 | return list; |
233 | } | |
234 | ||
94f2b392 | 235 | static void |
718e3744 | 236 | community_list_delete (struct community_list *list) |
237 | { | |
238 | struct community_list_list *clist; | |
239 | struct community_entry *entry, *next; | |
240 | ||
241 | for (entry = list->head; entry; entry = next) | |
242 | { | |
243 | next = entry->next; | |
244 | community_entry_free (entry); | |
245 | } | |
246 | ||
247 | clist = list->parent; | |
248 | ||
249 | if (list->next) | |
250 | list->next->prev = list->prev; | |
251 | else | |
252 | clist->tail = list->prev; | |
253 | ||
254 | if (list->prev) | |
255 | list->prev->next = list->next; | |
256 | else | |
257 | clist->head = list->next; | |
258 | ||
259 | community_list_free (list); | |
260 | } | |
261 | ||
94f2b392 | 262 | static int |
718e3744 | 263 | community_list_empty_p (struct community_list *list) |
264 | { | |
265 | return (list->head == NULL && list->tail == NULL) ? 1 : 0; | |
266 | } | |
6b0655a2 | 267 | |
718e3744 | 268 | /* Add community-list entry to the list. */ |
269 | static void | |
8708b74f | 270 | community_list_entry_add (struct community_list *list, |
271 | struct community_entry *entry) | |
718e3744 | 272 | { |
273 | entry->next = NULL; | |
274 | entry->prev = list->tail; | |
275 | ||
276 | if (list->tail) | |
277 | list->tail->next = entry; | |
278 | else | |
279 | list->head = entry; | |
280 | list->tail = entry; | |
281 | } | |
282 | ||
283 | /* Delete community-list entry from the list. */ | |
284 | static void | |
285 | community_list_entry_delete (struct community_list *list, | |
8708b74f | 286 | struct community_entry *entry, int style) |
718e3744 | 287 | { |
288 | if (entry->next) | |
289 | entry->next->prev = entry->prev; | |
290 | else | |
291 | list->tail = entry->prev; | |
292 | ||
293 | if (entry->prev) | |
294 | entry->prev->next = entry->next; | |
295 | else | |
296 | list->head = entry->next; | |
297 | ||
298 | community_entry_free (entry); | |
299 | ||
300 | if (community_list_empty_p (list)) | |
301 | community_list_delete (list); | |
302 | } | |
303 | ||
304 | /* Lookup community-list entry from the list. */ | |
305 | static struct community_entry * | |
fd79ac91 | 306 | community_list_entry_lookup (struct community_list *list, const void *arg, |
8708b74f | 307 | int direct) |
718e3744 | 308 | { |
309 | struct community_entry *entry; | |
310 | ||
311 | for (entry = list->head; entry; entry = entry->next) | |
312 | { | |
313 | switch (entry->style) | |
8708b74f | 314 | { |
315 | case COMMUNITY_LIST_STANDARD: | |
813d4307 | 316 | if (entry->direct == direct && community_cmp (entry->u.com, arg)) |
8708b74f | 317 | return entry; |
318 | break; | |
319 | case EXTCOMMUNITY_LIST_STANDARD: | |
813d4307 | 320 | if (entry->direct == direct && ecommunity_cmp (entry->u.ecom, arg)) |
8708b74f | 321 | return entry; |
322 | break; | |
323 | case COMMUNITY_LIST_EXPANDED: | |
324 | case EXTCOMMUNITY_LIST_EXPANDED: | |
813d4307 | 325 | if (entry->direct == direct && strcmp (entry->config, arg) == 0) |
8708b74f | 326 | return entry; |
327 | break; | |
328 | default: | |
329 | break; | |
330 | } | |
718e3744 | 331 | } |
332 | return NULL; | |
333 | } | |
6b0655a2 | 334 | |
5cbea288 DS |
335 | static char * |
336 | community_str_get (struct community *com, int i) | |
337 | { | |
338 | int len; | |
339 | u_int32_t comval; | |
340 | u_int16_t as; | |
341 | u_int16_t val; | |
342 | char *str; | |
343 | char *pnt; | |
344 | ||
345 | memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t)); | |
346 | comval = ntohl (comval); | |
347 | ||
348 | switch (comval) | |
349 | { | |
350 | case COMMUNITY_INTERNET: | |
351 | len = strlen (" internet"); | |
352 | break; | |
353 | case COMMUNITY_NO_EXPORT: | |
354 | len = strlen (" no-export"); | |
355 | break; | |
356 | case COMMUNITY_NO_ADVERTISE: | |
357 | len = strlen (" no-advertise"); | |
358 | break; | |
359 | case COMMUNITY_LOCAL_AS: | |
360 | len = strlen (" local-AS"); | |
361 | break; | |
362 | default: | |
363 | len = strlen (" 65536:65535"); | |
364 | break; | |
365 | } | |
366 | ||
367 | /* Allocate memory. */ | |
368 | str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len); | |
369 | ||
370 | switch (comval) | |
371 | { | |
372 | case COMMUNITY_INTERNET: | |
373 | strcpy (pnt, "internet"); | |
374 | pnt += strlen ("internet"); | |
375 | break; | |
376 | case COMMUNITY_NO_EXPORT: | |
377 | strcpy (pnt, "no-export"); | |
378 | pnt += strlen ("no-export"); | |
379 | break; | |
380 | case COMMUNITY_NO_ADVERTISE: | |
381 | strcpy (pnt, "no-advertise"); | |
382 | pnt += strlen ("no-advertise"); | |
383 | break; | |
384 | case COMMUNITY_LOCAL_AS: | |
385 | strcpy (pnt, "local-AS"); | |
386 | pnt += strlen ("local-AS"); | |
387 | break; | |
388 | default: | |
389 | as = (comval >> 16) & 0xFFFF; | |
390 | val = comval & 0xFFFF; | |
391 | sprintf (pnt, "%u:%d", as, val); | |
392 | pnt += strlen (pnt); | |
393 | break; | |
394 | } | |
395 | ||
396 | *pnt = '\0'; | |
397 | ||
398 | return str; | |
399 | } | |
400 | ||
401 | /* Internal function to perform regular expression match for | |
402 | * * a single community. */ | |
403 | static int | |
404 | community_regexp_include (regex_t * reg, struct community *com, int i) | |
405 | { | |
406 | const char *str; | |
407 | ||
408 | /* When there is no communities attribute it is treated as empty | |
409 | * string. */ | |
410 | if (com == NULL || com->size == 0) | |
411 | str = ""; | |
412 | else | |
413 | str = community_str_get (com, i); | |
414 | ||
415 | /* Regular expression match. */ | |
416 | if (regexec (reg, str, 0, NULL, 0) == 0) | |
417 | return 1; | |
418 | ||
419 | /* No match. */ | |
420 | return 0; | |
421 | } | |
422 | ||
718e3744 | 423 | /* Internal function to perform regular expression match for community |
424 | attribute. */ | |
425 | static int | |
8708b74f | 426 | community_regexp_match (struct community *com, regex_t * reg) |
718e3744 | 427 | { |
fd79ac91 | 428 | const char *str; |
718e3744 | 429 | |
430 | /* When there is no communities attribute it is treated as empty | |
431 | string. */ | |
432 | if (com == NULL || com->size == 0) | |
433 | str = ""; | |
434 | else | |
435 | str = community_str (com); | |
436 | ||
437 | /* Regular expression match. */ | |
438 | if (regexec (reg, str, 0, NULL, 0) == 0) | |
439 | return 1; | |
440 | ||
441 | /* No match. */ | |
442 | return 0; | |
443 | } | |
444 | ||
8708b74f | 445 | static int |
446 | ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg) | |
447 | { | |
fd79ac91 | 448 | const char *str; |
8708b74f | 449 | |
450 | /* When there is no communities attribute it is treated as empty | |
451 | string. */ | |
452 | if (ecom == NULL || ecom->size == 0) | |
453 | str = ""; | |
454 | else | |
455 | str = ecommunity_str (ecom); | |
456 | ||
457 | /* Regular expression match. */ | |
458 | if (regexec (reg, str, 0, NULL, 0) == 0) | |
459 | return 1; | |
460 | ||
461 | /* No match. */ | |
462 | return 0; | |
463 | } | |
464 | ||
ffd0c037 | 465 | #if 0 |
718e3744 | 466 | /* Delete community attribute using regular expression match. Return |
467 | modified communites attribute. */ | |
468 | static struct community * | |
8708b74f | 469 | community_regexp_delete (struct community *com, regex_t * reg) |
718e3744 | 470 | { |
471 | int i; | |
472 | u_int32_t comval; | |
473 | /* Maximum is "65535:65535" + '\0'. */ | |
474 | char c[12]; | |
fd79ac91 | 475 | const char *str; |
718e3744 | 476 | |
8708b74f | 477 | if (!com) |
718e3744 | 478 | return NULL; |
479 | ||
480 | i = 0; | |
481 | while (i < com->size) | |
482 | { | |
483 | memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t)); | |
484 | comval = ntohl (comval); | |
485 | ||
486 | switch (comval) | |
8708b74f | 487 | { |
488 | case COMMUNITY_INTERNET: | |
489 | str = "internet"; | |
490 | break; | |
491 | case COMMUNITY_NO_EXPORT: | |
492 | str = "no-export"; | |
493 | break; | |
494 | case COMMUNITY_NO_ADVERTISE: | |
495 | str = "no-advertise"; | |
496 | break; | |
497 | case COMMUNITY_LOCAL_AS: | |
498 | str = "local-AS"; | |
499 | break; | |
500 | default: | |
501 | sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF, comval & 0xFFFF); | |
502 | str = c; | |
503 | break; | |
504 | } | |
718e3744 | 505 | |
506 | if (regexec (reg, str, 0, NULL, 0) == 0) | |
8708b74f | 507 | community_del_val (com, com_nthval (com, i)); |
718e3744 | 508 | else |
8708b74f | 509 | i++; |
718e3744 | 510 | } |
511 | return com; | |
512 | } | |
ffd0c037 | 513 | #endif |
718e3744 | 514 | |
515 | /* When given community attribute matches to the community-list return | |
516 | 1 else return 0. */ | |
517 | int | |
518 | community_list_match (struct community *com, struct community_list *list) | |
519 | { | |
520 | struct community_entry *entry; | |
521 | ||
522 | for (entry = list->head; entry; entry = entry->next) | |
523 | { | |
524 | if (entry->any) | |
8708b74f | 525 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
718e3744 | 526 | |
527 | if (entry->style == COMMUNITY_LIST_STANDARD) | |
8708b74f | 528 | { |
529 | if (community_include (entry->u.com, COMMUNITY_INTERNET)) | |
530 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
718e3744 | 531 | |
8708b74f | 532 | if (community_match (com, entry->u.com)) |
533 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
534 | } | |
718e3744 | 535 | else if (entry->style == COMMUNITY_LIST_EXPANDED) |
8708b74f | 536 | { |
537 | if (community_regexp_match (com, entry->reg)) | |
538 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
539 | } | |
540 | } | |
541 | return 0; | |
542 | } | |
543 | ||
544 | int | |
545 | ecommunity_list_match (struct ecommunity *ecom, struct community_list *list) | |
546 | { | |
547 | struct community_entry *entry; | |
548 | ||
549 | for (entry = list->head; entry; entry = entry->next) | |
550 | { | |
551 | if (entry->any) | |
552 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
553 | ||
554 | if (entry->style == EXTCOMMUNITY_LIST_STANDARD) | |
555 | { | |
556 | if (ecommunity_match (ecom, entry->u.ecom)) | |
557 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
558 | } | |
559 | else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED) | |
560 | { | |
561 | if (ecommunity_regexp_match (ecom, entry->reg)) | |
562 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
563 | } | |
718e3744 | 564 | } |
565 | return 0; | |
566 | } | |
567 | ||
568 | /* Perform exact matching. In case of expanded community-list, do | |
569 | same thing as community_list_match(). */ | |
570 | int | |
8708b74f | 571 | community_list_exact_match (struct community *com, |
572 | struct community_list *list) | |
718e3744 | 573 | { |
574 | struct community_entry *entry; | |
575 | ||
576 | for (entry = list->head; entry; entry = entry->next) | |
577 | { | |
578 | if (entry->any) | |
8708b74f | 579 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
718e3744 | 580 | |
581 | if (entry->style == COMMUNITY_LIST_STANDARD) | |
8708b74f | 582 | { |
583 | if (community_include (entry->u.com, COMMUNITY_INTERNET)) | |
584 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
718e3744 | 585 | |
8708b74f | 586 | if (community_cmp (com, entry->u.com)) |
587 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
588 | } | |
718e3744 | 589 | else if (entry->style == COMMUNITY_LIST_EXPANDED) |
8708b74f | 590 | { |
591 | if (community_regexp_match (com, entry->reg)) | |
592 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; | |
593 | } | |
718e3744 | 594 | } |
595 | return 0; | |
596 | } | |
597 | ||
8708b74f | 598 | /* Delete all permitted communities in the list from com. */ |
718e3744 | 599 | struct community * |
600 | community_list_match_delete (struct community *com, | |
8708b74f | 601 | struct community_list *list) |
718e3744 | 602 | { |
603 | struct community_entry *entry; | |
5cbea288 DS |
604 | u_int32_t val; |
605 | u_int32_t com_index_to_delete[com->size]; | |
606 | int delete_index = 0; | |
607 | int i; | |
718e3744 | 608 | |
5cbea288 DS |
609 | /* Loop over each community value and evaluate each against the |
610 | * community-list. If we need to delete a community value add its index to | |
611 | * com_index_to_delete. | |
612 | */ | |
613 | for (i = 0; i < com->size; i++) | |
718e3744 | 614 | { |
5cbea288 DS |
615 | val = community_val_get (com, i); |
616 | ||
617 | for (entry = list->head; entry; entry = entry->next) | |
8708b74f | 618 | { |
5cbea288 | 619 | if (entry->any) |
847375b9 | 620 | { |
5cbea288 DS |
621 | if (entry->direct == COMMUNITY_PERMIT) |
622 | { | |
623 | com_index_to_delete[delete_index] = i; | |
624 | delete_index++; | |
625 | } | |
626 | break; | |
847375b9 | 627 | } |
718e3744 | 628 | |
5cbea288 DS |
629 | else if ((entry->style == COMMUNITY_LIST_STANDARD) |
630 | && (community_include (entry->u.com, COMMUNITY_INTERNET) | |
631 | || community_include (entry->u.com, val) )) | |
632 | { | |
847375b9 | 633 | if (entry->direct == COMMUNITY_PERMIT) |
5cbea288 DS |
634 | { |
635 | com_index_to_delete[delete_index] = i; | |
636 | delete_index++; | |
637 | } | |
638 | break; | |
639 | } | |
640 | ||
641 | else if ((entry->style == COMMUNITY_LIST_EXPANDED) | |
642 | && community_regexp_include (entry->reg, com, i)) | |
643 | { | |
644 | if (entry->direct == COMMUNITY_PERMIT) | |
645 | { | |
646 | com_index_to_delete[delete_index] = i; | |
647 | delete_index++; | |
648 | } | |
649 | break; | |
650 | } | |
651 | } | |
652 | } | |
653 | ||
654 | /* Delete all of the communities we flagged for deletion */ | |
655 | for (i = delete_index-1; i >= 0; i--) | |
656 | { | |
657 | val = community_val_get (com, com_index_to_delete[i]); | |
658 | community_del_val (com, &val); | |
718e3744 | 659 | } |
5cbea288 | 660 | |
718e3744 | 661 | return com; |
662 | } | |
663 | ||
664 | /* To avoid duplicated entry in the community-list, this function | |
665 | compares specified entry to existing entry. */ | |
94f2b392 | 666 | static int |
8708b74f | 667 | community_list_dup_check (struct community_list *list, |
668 | struct community_entry *new) | |
718e3744 | 669 | { |
670 | struct community_entry *entry; | |
8708b74f | 671 | |
718e3744 | 672 | for (entry = list->head; entry; entry = entry->next) |
673 | { | |
674 | if (entry->style != new->style) | |
8708b74f | 675 | continue; |
718e3744 | 676 | |
677 | if (entry->direct != new->direct) | |
8708b74f | 678 | continue; |
718e3744 | 679 | |
680 | if (entry->any != new->any) | |
8708b74f | 681 | continue; |
718e3744 | 682 | |
683 | if (entry->any) | |
8708b74f | 684 | return 1; |
718e3744 | 685 | |
686 | switch (entry->style) | |
8708b74f | 687 | { |
688 | case COMMUNITY_LIST_STANDARD: | |
689 | if (community_cmp (entry->u.com, new->u.com)) | |
690 | return 1; | |
691 | break; | |
692 | case EXTCOMMUNITY_LIST_STANDARD: | |
693 | if (ecommunity_cmp (entry->u.ecom, new->u.ecom)) | |
694 | return 1; | |
695 | break; | |
696 | case COMMUNITY_LIST_EXPANDED: | |
697 | case EXTCOMMUNITY_LIST_EXPANDED: | |
698 | if (strcmp (entry->config, new->config) == 0) | |
699 | return 1; | |
700 | break; | |
701 | default: | |
702 | break; | |
703 | } | |
718e3744 | 704 | } |
705 | return 0; | |
706 | } | |
6b0655a2 | 707 | |
718e3744 | 708 | /* Set community-list. */ |
709 | int | |
710 | community_list_set (struct community_list_handler *ch, | |
fd79ac91 | 711 | const char *name, const char *str, int direct, int style) |
718e3744 | 712 | { |
fee6e4e4 | 713 | struct community_entry *entry = NULL; |
718e3744 | 714 | struct community_list *list; |
fee6e4e4 | 715 | struct community *com = NULL; |
716 | regex_t *regex = NULL; | |
718e3744 | 717 | |
718 | /* Get community list. */ | |
fee6e4e4 | 719 | list = community_list_get (ch, name, COMMUNITY_LIST_MASTER); |
718e3744 | 720 | |
721 | /* When community-list already has entry, new entry should have same | |
722 | style. If you want to have mixed style community-list, you can | |
723 | comment out this check. */ | |
8708b74f | 724 | if (!community_list_empty_p (list)) |
718e3744 | 725 | { |
726 | struct community_entry *first; | |
727 | ||
728 | first = list->head; | |
729 | ||
fee6e4e4 | 730 | if (style != first->style) |
731 | { | |
732 | return (first->style == COMMUNITY_LIST_STANDARD | |
733 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT | |
734 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); | |
735 | } | |
718e3744 | 736 | } |
737 | ||
fee6e4e4 | 738 | if (str) |
718e3744 | 739 | { |
fee6e4e4 | 740 | if (style == COMMUNITY_LIST_STANDARD) |
741 | com = community_str2com (str); | |
718e3744 | 742 | else |
fee6e4e4 | 743 | regex = bgp_regcomp (str); |
8708b74f | 744 | |
fee6e4e4 | 745 | if (! com && ! regex) |
746 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; | |
718e3744 | 747 | } |
748 | ||
fee6e4e4 | 749 | entry = community_entry_new (); |
750 | entry->direct = direct; | |
751 | entry->style = style; | |
752 | entry->any = (str ? 0 : 1); | |
753 | entry->u.com = com; | |
754 | entry->reg = regex; | |
755 | entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL); | |
756 | ||
718e3744 | 757 | /* Do not put duplicated community entry. */ |
758 | if (community_list_dup_check (list, entry)) | |
759 | community_entry_free (entry); | |
760 | else | |
518f0eb1 DS |
761 | { |
762 | community_list_entry_add (list, entry); | |
763 | route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED); | |
764 | } | |
718e3744 | 765 | |
766 | return 0; | |
767 | } | |
768 | ||
813d4307 | 769 | /* Unset community-list */ |
718e3744 | 770 | int |
771 | community_list_unset (struct community_list_handler *ch, | |
fd79ac91 | 772 | const char *name, const char *str, |
813d4307 | 773 | int direct, int style, int delete_all) |
718e3744 | 774 | { |
fee6e4e4 | 775 | struct community_entry *entry = NULL; |
718e3744 | 776 | struct community_list *list; |
fee6e4e4 | 777 | struct community *com = NULL; |
718e3744 | 778 | |
779 | /* Lookup community list. */ | |
fee6e4e4 | 780 | list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER); |
718e3744 | 781 | if (list == NULL) |
782 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; | |
783 | ||
784 | /* Delete all of entry belongs to this community-list. */ | |
813d4307 | 785 | if (delete_all) |
718e3744 | 786 | { |
787 | community_list_delete (list); | |
518f0eb1 | 788 | route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED); |
718e3744 | 789 | return 0; |
790 | } | |
791 | ||
fee6e4e4 | 792 | if (style == COMMUNITY_LIST_STANDARD) |
813d4307 DW |
793 | { |
794 | if (str) | |
795 | com = community_str2com (str); | |
796 | } | |
718e3744 | 797 | |
fee6e4e4 | 798 | if (com) |
813d4307 DW |
799 | { |
800 | entry = community_list_entry_lookup (list, com, direct); | |
801 | community_free (com); | |
802 | } | |
fee6e4e4 | 803 | else |
804 | entry = community_list_entry_lookup (list, str, direct); | |
805 | ||
8708b74f | 806 | if (!entry) |
718e3744 | 807 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
808 | ||
809 | community_list_entry_delete (list, entry, style); | |
518f0eb1 | 810 | route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED); |
718e3744 | 811 | |
812 | return 0; | |
813 | } | |
814 | ||
815 | /* Set extcommunity-list. */ | |
816 | int | |
817 | extcommunity_list_set (struct community_list_handler *ch, | |
fd79ac91 | 818 | const char *name, const char *str, |
819 | int direct, int style) | |
718e3744 | 820 | { |
fee6e4e4 | 821 | struct community_entry *entry = NULL; |
718e3744 | 822 | struct community_list *list; |
fee6e4e4 | 823 | struct ecommunity *ecom = NULL; |
824 | regex_t *regex = NULL; | |
718e3744 | 825 | |
826 | entry = NULL; | |
827 | ||
828 | /* Get community list. */ | |
fee6e4e4 | 829 | list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER); |
718e3744 | 830 | |
831 | /* When community-list already has entry, new entry should have same | |
832 | style. If you want to have mixed style community-list, you can | |
833 | comment out this check. */ | |
8708b74f | 834 | if (!community_list_empty_p (list)) |
718e3744 | 835 | { |
836 | struct community_entry *first; | |
837 | ||
838 | first = list->head; | |
839 | ||
fee6e4e4 | 840 | if (style != first->style) |
841 | { | |
842 | return (first->style == EXTCOMMUNITY_LIST_STANDARD | |
843 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT | |
844 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); | |
845 | } | |
718e3744 | 846 | } |
847 | ||
fee6e4e4 | 848 | if (str) |
718e3744 | 849 | { |
fee6e4e4 | 850 | if (style == EXTCOMMUNITY_LIST_STANDARD) |
851 | ecom = ecommunity_str2com (str, 0, 1); | |
718e3744 | 852 | else |
fee6e4e4 | 853 | regex = bgp_regcomp (str); |
8708b74f | 854 | |
fee6e4e4 | 855 | if (! ecom && ! regex) |
856 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; | |
718e3744 | 857 | } |
858 | ||
fee6e4e4 | 859 | if (ecom) |
860 | ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY); | |
861 | ||
862 | entry = community_entry_new (); | |
863 | entry->direct = direct; | |
864 | entry->style = style; | |
865 | entry->any = (str ? 0 : 1); | |
866 | if (ecom) | |
867 | entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST); | |
868 | else if (regex) | |
869 | entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str); | |
870 | else | |
871 | entry->config = NULL; | |
872 | entry->u.ecom = ecom; | |
873 | entry->reg = regex; | |
874 | ||
718e3744 | 875 | /* Do not put duplicated community entry. */ |
876 | if (community_list_dup_check (list, entry)) | |
877 | community_entry_free (entry); | |
878 | else | |
518f0eb1 DS |
879 | { |
880 | community_list_entry_add (list, entry); | |
881 | route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED); | |
882 | } | |
718e3744 | 883 | |
884 | return 0; | |
885 | } | |
886 | ||
887 | /* Unset extcommunity-list. When str is NULL, delete all of | |
888 | extcommunity-list entry belongs to the specified name. */ | |
889 | int | |
890 | extcommunity_list_unset (struct community_list_handler *ch, | |
fd79ac91 | 891 | const char *name, const char *str, |
813d4307 | 892 | int direct, int style, int delete_all) |
718e3744 | 893 | { |
fee6e4e4 | 894 | struct community_entry *entry = NULL; |
718e3744 | 895 | struct community_list *list; |
896 | struct ecommunity *ecom = NULL; | |
718e3744 | 897 | |
898 | /* Lookup extcommunity list. */ | |
fee6e4e4 | 899 | list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER); |
718e3744 | 900 | if (list == NULL) |
901 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; | |
902 | ||
903 | /* Delete all of entry belongs to this extcommunity-list. */ | |
813d4307 | 904 | if (delete_all) |
718e3744 | 905 | { |
906 | community_list_delete (list); | |
518f0eb1 | 907 | route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED); |
718e3744 | 908 | return 0; |
909 | } | |
910 | ||
fee6e4e4 | 911 | if (style == EXTCOMMUNITY_LIST_STANDARD) |
813d4307 DW |
912 | { |
913 | if (str) | |
914 | ecom = ecommunity_str2com (str, 0, 1); | |
915 | } | |
718e3744 | 916 | |
fee6e4e4 | 917 | if (ecom) |
813d4307 DW |
918 | { |
919 | entry = community_list_entry_lookup (list, ecom, direct); | |
920 | ecommunity_free (&ecom); | |
921 | } | |
fee6e4e4 | 922 | else |
923 | entry = community_list_entry_lookup (list, str, direct); | |
924 | ||
8708b74f | 925 | if (!entry) |
718e3744 | 926 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
927 | ||
928 | community_list_entry_delete (list, entry, style); | |
518f0eb1 | 929 | route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED); |
718e3744 | 930 | |
931 | return 0; | |
932 | } | |
933 | ||
934 | /* Initializa community-list. Return community-list handler. */ | |
935 | struct community_list_handler * | |
94f2b392 | 936 | community_list_init (void) |
718e3744 | 937 | { |
938 | struct community_list_handler *ch; | |
939 | ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER, | |
8708b74f | 940 | sizeof (struct community_list_handler)); |
718e3744 | 941 | return ch; |
942 | } | |
943 | ||
944 | /* Terminate community-list. */ | |
228da428 | 945 | void |
718e3744 | 946 | community_list_terminate (struct community_list_handler *ch) |
947 | { | |
948 | struct community_list_master *cm; | |
949 | struct community_list *list; | |
950 | ||
951 | cm = &ch->community_list; | |
952 | while ((list = cm->num.head) != NULL) | |
953 | community_list_delete (list); | |
954 | while ((list = cm->str.head) != NULL) | |
955 | community_list_delete (list); | |
956 | ||
957 | cm = &ch->extcommunity_list; | |
958 | while ((list = cm->num.head) != NULL) | |
959 | community_list_delete (list); | |
960 | while ((list = cm->str.head) != NULL) | |
961 | community_list_delete (list); | |
962 | ||
963 | XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch); | |
964 | } |