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