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" |
Donald Sharp | 0490729 | 2016-01-07 10:03:01 -0500 | [diff] [blame] | 26 | #include "filter.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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" |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 34 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | /* Lookup master structure for community-list or |
| 36 | extcommunity-list. */ |
| 37 | struct community_list_master * |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 38 | community_list_master_lookup (struct community_list_handler *ch, int master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 39 | { |
| 40 | if (ch) |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 41 | switch (master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 43 | case COMMUNITY_LIST_MASTER: |
| 44 | return &ch->community_list; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 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 * |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 53 | community_entry_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 55 | return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* Free community list entry. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 59 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | community_entry_free (struct community_entry *entry) |
| 61 | { |
| 62 | switch (entry->style) |
| 63 | { |
| 64 | case COMMUNITY_LIST_STANDARD: |
| 65 | if (entry->u.com) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 66 | community_free (entry->u.com); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | break; |
| 68 | case EXTCOMMUNITY_LIST_STANDARD: |
| 69 | /* In case of standard extcommunity-list, configuration string |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 70 | is made by ecommunity_ecom2str(). */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | if (entry->config) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 72 | XFREE (MTYPE_ECOMMUNITY_STR, entry->config); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | if (entry->u.ecom) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 74 | ecommunity_free (&entry->u.ecom); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | break; |
| 76 | case COMMUNITY_LIST_EXPANDED: |
| 77 | case EXTCOMMUNITY_LIST_EXPANDED: |
| 78 | if (entry->config) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 79 | XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 80 | if (entry->reg) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 81 | bgp_regex_free (entry->reg); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry); |
| 86 | } |
| 87 | |
| 88 | /* Allocate a new community-list. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 89 | static struct community_list * |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 90 | community_list_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 91 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 92 | return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* Free community-list. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 96 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 104 | static struct community_list * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | community_list_insert (struct community_list_handler *ch, |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 106 | const char *name, int master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | { |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 108 | size_t i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 116 | cm = community_list_master_lookup (ch, master); |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 117 | if (!cm) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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])) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 129 | number = (number * 10) + (name[i] - '0'); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | else |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 131 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 143 | if (atol (point->name) >= number) |
| 144 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | { |
| 148 | new->sort = COMMUNITY_LIST_STRING; |
| 149 | |
| 150 | /* Set access_list to string list. */ |
| 151 | list = &cm->str; |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 152 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | /* Set point to insertion point. */ |
| 154 | for (point = list->head; point; point = point->next) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 155 | if (strcmp (point->name, name) >= 0) |
| 156 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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, |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 200 | const char *name, int master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 201 | { |
| 202 | struct community_list *list; |
| 203 | struct community_list_master *cm; |
| 204 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 205 | if (!name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | return NULL; |
| 207 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 208 | cm = community_list_master_lookup (ch, master); |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 209 | if (!cm) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 222 | static struct community_list * |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 223 | community_list_get (struct community_list_handler *ch, |
| 224 | const char *name, int master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | { |
| 226 | struct community_list *list; |
| 227 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 228 | list = community_list_lookup (ch, name, master); |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 229 | if (!list) |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 230 | list = community_list_insert (ch, name, master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | return list; |
| 232 | } |
| 233 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 234 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 261 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 262 | community_list_empty_p (struct community_list *list) |
| 263 | { |
| 264 | return (list->head == NULL && list->tail == NULL) ? 1 : 0; |
| 265 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 266 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | /* Add community-list entry to the list. */ |
| 268 | static void |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 269 | community_list_entry_add (struct community_list *list, |
| 270 | struct community_entry *entry) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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, |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 285 | struct community_entry *entry, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 * |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 305 | community_list_entry_lookup (struct community_list *list, const void *arg, |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 306 | int direct) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 307 | { |
| 308 | struct community_entry *entry; |
| 309 | |
| 310 | for (entry = list->head; entry; entry = entry->next) |
| 311 | { |
| 312 | switch (entry->style) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 313 | { |
| 314 | case COMMUNITY_LIST_STANDARD: |
| 315 | if (community_cmp (entry->u.com, arg)) |
| 316 | return entry; |
| 317 | break; |
| 318 | case EXTCOMMUNITY_LIST_STANDARD: |
| 319 | if (ecommunity_cmp (entry->u.ecom, arg)) |
| 320 | return entry; |
| 321 | break; |
| 322 | case COMMUNITY_LIST_EXPANDED: |
| 323 | case EXTCOMMUNITY_LIST_EXPANDED: |
| 324 | if (strcmp (entry->config, arg) == 0) |
| 325 | return entry; |
| 326 | break; |
| 327 | default: |
| 328 | break; |
| 329 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | } |
| 331 | return NULL; |
| 332 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 333 | |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 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 | { |
Christian Franke | 32e41f7 | 2016-06-14 20:06:59 +0200 | [diff] [blame] | 405 | char *str; |
| 406 | int rv; |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 407 | |
| 408 | /* When there is no communities attribute it is treated as empty |
| 409 | * string. */ |
| 410 | if (com == NULL || com->size == 0) |
Christian Franke | 32e41f7 | 2016-06-14 20:06:59 +0200 | [diff] [blame] | 411 | str = XSTRDUP(MTYPE_COMMUNITY_STR, ""); |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 412 | else |
| 413 | str = community_str_get (com, i); |
| 414 | |
| 415 | /* Regular expression match. */ |
Christian Franke | 32e41f7 | 2016-06-14 20:06:59 +0200 | [diff] [blame] | 416 | rv = regexec (reg, str, 0, NULL, 0); |
| 417 | |
| 418 | XFREE(MTYPE_COMMUNITY_STR, str); |
| 419 | |
| 420 | if (rv == 0) |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 421 | return 1; |
| 422 | |
| 423 | /* No match. */ |
| 424 | return 0; |
| 425 | } |
| 426 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | /* Internal function to perform regular expression match for community |
| 428 | attribute. */ |
| 429 | static int |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 430 | community_regexp_match (struct community *com, regex_t * reg) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 431 | { |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 432 | const char *str; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 433 | |
| 434 | /* When there is no communities attribute it is treated as empty |
| 435 | string. */ |
| 436 | if (com == NULL || com->size == 0) |
| 437 | str = ""; |
| 438 | else |
| 439 | str = community_str (com); |
| 440 | |
| 441 | /* Regular expression match. */ |
| 442 | if (regexec (reg, str, 0, NULL, 0) == 0) |
| 443 | return 1; |
| 444 | |
| 445 | /* No match. */ |
| 446 | return 0; |
| 447 | } |
| 448 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 449 | static int |
| 450 | ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg) |
| 451 | { |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 452 | const char *str; |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 453 | |
| 454 | /* When there is no communities attribute it is treated as empty |
| 455 | string. */ |
| 456 | if (ecom == NULL || ecom->size == 0) |
| 457 | str = ""; |
| 458 | else |
| 459 | str = ecommunity_str (ecom); |
| 460 | |
| 461 | /* Regular expression match. */ |
| 462 | if (regexec (reg, str, 0, NULL, 0) == 0) |
| 463 | return 1; |
| 464 | |
| 465 | /* No match. */ |
| 466 | return 0; |
| 467 | } |
| 468 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 469 | /* When given community attribute matches to the community-list return |
| 470 | 1 else return 0. */ |
| 471 | int |
| 472 | community_list_match (struct community *com, struct community_list *list) |
| 473 | { |
| 474 | struct community_entry *entry; |
| 475 | |
| 476 | for (entry = list->head; entry; entry = entry->next) |
| 477 | { |
| 478 | if (entry->any) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 479 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 480 | |
| 481 | if (entry->style == COMMUNITY_LIST_STANDARD) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 482 | { |
| 483 | if (community_include (entry->u.com, COMMUNITY_INTERNET)) |
| 484 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 486 | if (community_match (com, entry->u.com)) |
| 487 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 488 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | else if (entry->style == COMMUNITY_LIST_EXPANDED) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 490 | { |
| 491 | if (community_regexp_match (com, entry->reg)) |
| 492 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 493 | } |
| 494 | } |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | int |
| 499 | ecommunity_list_match (struct ecommunity *ecom, struct community_list *list) |
| 500 | { |
| 501 | struct community_entry *entry; |
| 502 | |
| 503 | for (entry = list->head; entry; entry = entry->next) |
| 504 | { |
| 505 | if (entry->any) |
| 506 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 507 | |
| 508 | if (entry->style == EXTCOMMUNITY_LIST_STANDARD) |
| 509 | { |
| 510 | if (ecommunity_match (ecom, entry->u.ecom)) |
| 511 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 512 | } |
| 513 | else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED) |
| 514 | { |
| 515 | if (ecommunity_regexp_match (ecom, entry->reg)) |
| 516 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 517 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 518 | } |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /* Perform exact matching. In case of expanded community-list, do |
| 523 | same thing as community_list_match(). */ |
| 524 | int |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 525 | community_list_exact_match (struct community *com, |
| 526 | struct community_list *list) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 527 | { |
| 528 | struct community_entry *entry; |
| 529 | |
| 530 | for (entry = list->head; entry; entry = entry->next) |
| 531 | { |
| 532 | if (entry->any) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 533 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | |
| 535 | if (entry->style == COMMUNITY_LIST_STANDARD) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 536 | { |
| 537 | if (community_include (entry->u.com, COMMUNITY_INTERNET)) |
| 538 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 539 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 540 | if (community_cmp (com, entry->u.com)) |
| 541 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 542 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | else if (entry->style == COMMUNITY_LIST_EXPANDED) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 544 | { |
| 545 | if (community_regexp_match (com, entry->reg)) |
| 546 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 547 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 548 | } |
| 549 | return 0; |
| 550 | } |
| 551 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 552 | /* Delete all permitted communities in the list from com. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | struct community * |
| 554 | community_list_match_delete (struct community *com, |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 555 | struct community_list *list) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 556 | { |
| 557 | struct community_entry *entry; |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 558 | u_int32_t val; |
| 559 | u_int32_t com_index_to_delete[com->size]; |
| 560 | int delete_index = 0; |
| 561 | int 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 | /* Loop over each community value and evaluate each against the |
| 564 | * community-list. If we need to delete a community value add its index to |
| 565 | * com_index_to_delete. |
| 566 | */ |
| 567 | for (i = 0; i < com->size; i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 568 | { |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 569 | val = community_val_get (com, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 570 | |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 571 | for (entry = list->head; entry; entry = entry->next) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 572 | { |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 573 | if (entry->any) |
| 574 | { |
paul | 847375b | 2003-06-09 18:48:31 +0000 | [diff] [blame] | 575 | if (entry->direct == COMMUNITY_PERMIT) |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 576 | { |
| 577 | com_index_to_delete[delete_index] = i; |
| 578 | delete_index++; |
| 579 | } |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | else if ((entry->style == COMMUNITY_LIST_STANDARD) |
| 584 | && (community_include (entry->u.com, COMMUNITY_INTERNET) |
| 585 | || community_include (entry->u.com, val) )) |
| 586 | { |
| 587 | if (entry->direct == COMMUNITY_PERMIT) |
| 588 | { |
| 589 | com_index_to_delete[delete_index] = i; |
| 590 | delete_index++; |
| 591 | } |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | else if ((entry->style == COMMUNITY_LIST_EXPANDED) |
| 596 | && community_regexp_include (entry->reg, com, i)) |
| 597 | { |
| 598 | if (entry->direct == COMMUNITY_PERMIT) |
| 599 | { |
| 600 | com_index_to_delete[delete_index] = i; |
| 601 | delete_index++; |
| 602 | } |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* Delete all of the communities we flagged for deletion */ |
| 609 | for (i = delete_index-1; i >= 0; i--) |
| 610 | { |
| 611 | val = community_val_get (com, com_index_to_delete[i]); |
| 612 | community_del_val (com, &val); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 613 | } |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 614 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 615 | return com; |
| 616 | } |
| 617 | |
| 618 | /* To avoid duplicated entry in the community-list, this function |
| 619 | compares specified entry to existing entry. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 620 | static int |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 621 | community_list_dup_check (struct community_list *list, |
| 622 | struct community_entry *new) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 623 | { |
| 624 | struct community_entry *entry; |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 625 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 626 | for (entry = list->head; entry; entry = entry->next) |
| 627 | { |
| 628 | if (entry->style != new->style) |
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->direct != new->direct) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 632 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 633 | |
| 634 | if (entry->any != new->any) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 635 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 636 | |
| 637 | if (entry->any) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 638 | return 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 639 | |
| 640 | switch (entry->style) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 641 | { |
| 642 | case COMMUNITY_LIST_STANDARD: |
| 643 | if (community_cmp (entry->u.com, new->u.com)) |
| 644 | return 1; |
| 645 | break; |
| 646 | case EXTCOMMUNITY_LIST_STANDARD: |
| 647 | if (ecommunity_cmp (entry->u.ecom, new->u.ecom)) |
| 648 | return 1; |
| 649 | break; |
| 650 | case COMMUNITY_LIST_EXPANDED: |
| 651 | case EXTCOMMUNITY_LIST_EXPANDED: |
Christian Franke | c0a613f | 2016-06-06 22:22:15 +0200 | [diff] [blame] | 652 | if (entry->config && new->config |
| 653 | && strcmp (entry->config, new->config) == 0) |
| 654 | return 1; |
| 655 | if (!entry->config && !new->config) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 656 | return 1; |
| 657 | break; |
| 658 | default: |
| 659 | break; |
| 660 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 661 | } |
| 662 | return 0; |
| 663 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 664 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 665 | /* Set community-list. */ |
| 666 | int |
| 667 | community_list_set (struct community_list_handler *ch, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 668 | const char *name, const char *str, int direct, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 669 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 670 | struct community_entry *entry = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 671 | struct community_list *list; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 672 | struct community *com = NULL; |
| 673 | regex_t *regex = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 674 | |
| 675 | /* Get community list. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 676 | list = community_list_get (ch, name, COMMUNITY_LIST_MASTER); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 677 | |
| 678 | /* When community-list already has entry, new entry should have same |
| 679 | style. If you want to have mixed style community-list, you can |
| 680 | comment out this check. */ |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 681 | if (!community_list_empty_p (list)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 682 | { |
| 683 | struct community_entry *first; |
| 684 | |
| 685 | first = list->head; |
| 686 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 687 | if (style != first->style) |
| 688 | { |
| 689 | return (first->style == COMMUNITY_LIST_STANDARD |
| 690 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT |
| 691 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); |
| 692 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 693 | } |
| 694 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 695 | if (str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 696 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 697 | if (style == COMMUNITY_LIST_STANDARD) |
| 698 | com = community_str2com (str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 699 | else |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 700 | regex = bgp_regcomp (str); |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 701 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 702 | if (! com && ! regex) |
| 703 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 704 | } |
| 705 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 706 | entry = community_entry_new (); |
| 707 | entry->direct = direct; |
| 708 | entry->style = style; |
| 709 | entry->any = (str ? 0 : 1); |
| 710 | entry->u.com = com; |
| 711 | entry->reg = regex; |
| 712 | entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL); |
| 713 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 714 | /* Do not put duplicated community entry. */ |
| 715 | if (community_list_dup_check (list, entry)) |
| 716 | community_entry_free (entry); |
| 717 | else |
| 718 | community_list_entry_add (list, entry); |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | /* Unset community-list. When str is NULL, delete all of |
| 724 | community-list entry belongs to the specified name. */ |
| 725 | int |
| 726 | community_list_unset (struct community_list_handler *ch, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 727 | const char *name, const char *str, |
| 728 | int direct, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 729 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 730 | struct community_entry *entry = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 731 | struct community_list *list; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 732 | struct community *com = NULL; |
| 733 | regex_t *regex = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 734 | |
| 735 | /* Lookup community list. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 736 | list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | if (list == NULL) |
| 738 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 739 | |
| 740 | /* Delete all of entry belongs to this community-list. */ |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 741 | if (!str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 742 | { |
| 743 | community_list_delete (list); |
| 744 | return 0; |
| 745 | } |
| 746 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 747 | if (style == COMMUNITY_LIST_STANDARD) |
| 748 | com = community_str2com (str); |
| 749 | else |
| 750 | regex = bgp_regcomp (str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 751 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 752 | if (! com && ! regex) |
| 753 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 754 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 755 | if (com) |
| 756 | entry = community_list_entry_lookup (list, com, direct); |
| 757 | else |
| 758 | entry = community_list_entry_lookup (list, str, direct); |
| 759 | |
| 760 | if (com) |
| 761 | community_free (com); |
| 762 | if (regex) |
| 763 | bgp_regex_free (regex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 764 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 765 | if (!entry) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 766 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 767 | |
| 768 | community_list_entry_delete (list, entry, style); |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | /* Set extcommunity-list. */ |
| 774 | int |
| 775 | extcommunity_list_set (struct community_list_handler *ch, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 776 | const char *name, const char *str, |
| 777 | int direct, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 778 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 779 | struct community_entry *entry = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 780 | struct community_list *list; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 781 | struct ecommunity *ecom = NULL; |
| 782 | regex_t *regex = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 783 | |
| 784 | entry = NULL; |
| 785 | |
| 786 | /* Get community list. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 787 | list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 788 | |
| 789 | /* When community-list already has entry, new entry should have same |
| 790 | style. If you want to have mixed style community-list, you can |
| 791 | comment out this check. */ |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 792 | if (!community_list_empty_p (list)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 793 | { |
| 794 | struct community_entry *first; |
| 795 | |
| 796 | first = list->head; |
| 797 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 798 | if (style != first->style) |
| 799 | { |
| 800 | return (first->style == EXTCOMMUNITY_LIST_STANDARD |
| 801 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT |
| 802 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); |
| 803 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 804 | } |
| 805 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 806 | if (str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 807 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 808 | if (style == EXTCOMMUNITY_LIST_STANDARD) |
| 809 | ecom = ecommunity_str2com (str, 0, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 810 | else |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 811 | regex = bgp_regcomp (str); |
| 812 | |
| 813 | if (! ecom && ! regex) |
| 814 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 815 | } |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 816 | |
| 817 | if (ecom) |
| 818 | ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY); |
| 819 | |
| 820 | entry = community_entry_new (); |
| 821 | entry->direct = direct; |
| 822 | entry->style = style; |
| 823 | entry->any = (str ? 0 : 1); |
| 824 | if (ecom) |
| 825 | entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST); |
| 826 | else if (regex) |
| 827 | entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 828 | else |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 829 | entry->config = NULL; |
| 830 | entry->u.ecom = ecom; |
| 831 | entry->reg = regex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 832 | |
| 833 | /* Do not put duplicated community entry. */ |
| 834 | if (community_list_dup_check (list, entry)) |
| 835 | community_entry_free (entry); |
| 836 | else |
| 837 | community_list_entry_add (list, entry); |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | /* Unset extcommunity-list. When str is NULL, delete all of |
| 843 | extcommunity-list entry belongs to the specified name. */ |
| 844 | int |
| 845 | extcommunity_list_unset (struct community_list_handler *ch, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 846 | const char *name, const char *str, |
| 847 | int direct, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 848 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 849 | struct community_entry *entry = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 850 | struct community_list *list; |
| 851 | struct ecommunity *ecom = NULL; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 852 | regex_t *regex = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 853 | |
| 854 | /* Lookup extcommunity list. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 855 | list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 856 | if (list == NULL) |
| 857 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 858 | |
| 859 | /* Delete all of entry belongs to this extcommunity-list. */ |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 860 | if (!str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 861 | { |
| 862 | community_list_delete (list); |
| 863 | return 0; |
| 864 | } |
| 865 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 866 | if (style == EXTCOMMUNITY_LIST_STANDARD) |
| 867 | ecom = ecommunity_str2com (str, 0, 1); |
| 868 | else |
| 869 | regex = bgp_regcomp (str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 870 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 871 | if (! ecom && ! regex) |
| 872 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 873 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 874 | if (ecom) |
| 875 | entry = community_list_entry_lookup (list, ecom, direct); |
| 876 | else |
| 877 | entry = community_list_entry_lookup (list, str, direct); |
| 878 | |
| 879 | if (ecom) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 880 | ecommunity_free (&ecom); |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 881 | if (regex) |
| 882 | bgp_regex_free (regex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 883 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 884 | if (!entry) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 885 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 886 | |
| 887 | community_list_entry_delete (list, entry, style); |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | /* Initializa community-list. Return community-list handler. */ |
| 893 | struct community_list_handler * |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 894 | community_list_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 895 | { |
| 896 | struct community_list_handler *ch; |
| 897 | ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER, |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 898 | sizeof (struct community_list_handler)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 899 | return ch; |
| 900 | } |
| 901 | |
| 902 | /* Terminate community-list. */ |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 903 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 904 | community_list_terminate (struct community_list_handler *ch) |
| 905 | { |
| 906 | struct community_list_master *cm; |
| 907 | struct community_list *list; |
| 908 | |
| 909 | cm = &ch->community_list; |
| 910 | while ((list = cm->num.head) != NULL) |
| 911 | community_list_delete (list); |
| 912 | while ((list = cm->str.head) != NULL) |
| 913 | community_list_delete (list); |
| 914 | |
| 915 | cm = &ch->extcommunity_list; |
| 916 | while ((list = cm->num.head) != NULL) |
| 917 | community_list_delete (list); |
| 918 | while ((list = cm->str.head) != NULL) |
| 919 | community_list_delete (list); |
| 920 | |
| 921 | XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch); |
| 922 | } |