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