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