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" |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 31 | #include "bgpd/bgp_lcommunity.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 32 | #include "bgpd/bgp_aspath.h" |
| 33 | #include "bgpd/bgp_regex.h" |
| 34 | #include "bgpd/bgp_clist.h" |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 35 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | /* Lookup master structure for community-list or |
| 37 | extcommunity-list. */ |
| 38 | struct community_list_master * |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 39 | community_list_master_lookup (struct community_list_handler *ch, int master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | { |
| 41 | if (ch) |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 42 | switch (master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 44 | case COMMUNITY_LIST_MASTER: |
| 45 | return &ch->community_list; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 46 | case EXTCOMMUNITY_LIST_MASTER: |
| 47 | return &ch->extcommunity_list; |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 48 | case LARGE_COMMUNITY_LIST_MASTER: |
| 49 | return &ch->lcommunity_list; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | } |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | /* Allocate a new community list entry. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 55 | static struct community_entry * |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 56 | community_entry_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 58 | return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* Free community list entry. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 62 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | community_entry_free (struct community_entry *entry) |
| 64 | { |
| 65 | switch (entry->style) |
| 66 | { |
| 67 | case COMMUNITY_LIST_STANDARD: |
| 68 | if (entry->u.com) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 69 | community_free (entry->u.com); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 70 | break; |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 71 | case LARGE_COMMUNITY_LIST_STANDARD: |
| 72 | if (entry->u.lcom) |
| 73 | lcommunity_free (&entry->u.lcom); |
| 74 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | case EXTCOMMUNITY_LIST_STANDARD: |
| 76 | /* In case of standard extcommunity-list, configuration string |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 77 | is made by ecommunity_ecom2str(). */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | if (entry->config) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 79 | XFREE (MTYPE_ECOMMUNITY_STR, entry->config); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 80 | if (entry->u.ecom) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 81 | ecommunity_free (&entry->u.ecom); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | break; |
| 83 | case COMMUNITY_LIST_EXPANDED: |
| 84 | case EXTCOMMUNITY_LIST_EXPANDED: |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 85 | case LARGE_COMMUNITY_LIST_EXPANDED: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 86 | if (entry->config) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 87 | XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | if (entry->reg) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 89 | bgp_regex_free (entry->reg); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry); |
| 94 | } |
| 95 | |
| 96 | /* Allocate a new community-list. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 97 | static struct community_list * |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 98 | community_list_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 100 | return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 274 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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; |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 326 | case LARGE_COMMUNITY_LIST_STANDARD: |
| 327 | if (lcommunity_cmp (entry->u.lcom, arg)) |
| 328 | return entry; |
| 329 | break; |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 330 | case EXTCOMMUNITY_LIST_STANDARD: |
| 331 | if (ecommunity_cmp (entry->u.ecom, arg)) |
| 332 | return entry; |
| 333 | break; |
| 334 | case COMMUNITY_LIST_EXPANDED: |
| 335 | case EXTCOMMUNITY_LIST_EXPANDED: |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 336 | case LARGE_COMMUNITY_LIST_EXPANDED: |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 337 | if (strcmp (entry->config, arg) == 0) |
| 338 | return entry; |
| 339 | break; |
| 340 | default: |
| 341 | break; |
| 342 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 343 | } |
| 344 | return NULL; |
| 345 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 346 | |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 347 | static char * |
| 348 | community_str_get (struct community *com, int i) |
| 349 | { |
| 350 | int len; |
| 351 | u_int32_t comval; |
| 352 | u_int16_t as; |
| 353 | u_int16_t val; |
| 354 | char *str; |
| 355 | char *pnt; |
| 356 | |
| 357 | memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t)); |
| 358 | comval = ntohl (comval); |
| 359 | |
| 360 | switch (comval) |
| 361 | { |
| 362 | case COMMUNITY_INTERNET: |
| 363 | len = strlen (" internet"); |
| 364 | break; |
| 365 | case COMMUNITY_NO_EXPORT: |
| 366 | len = strlen (" no-export"); |
| 367 | break; |
| 368 | case COMMUNITY_NO_ADVERTISE: |
| 369 | len = strlen (" no-advertise"); |
| 370 | break; |
| 371 | case COMMUNITY_LOCAL_AS: |
| 372 | len = strlen (" local-AS"); |
| 373 | break; |
| 374 | default: |
| 375 | len = strlen (" 65536:65535"); |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | /* Allocate memory. */ |
| 380 | str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len); |
| 381 | |
| 382 | switch (comval) |
| 383 | { |
| 384 | case COMMUNITY_INTERNET: |
| 385 | strcpy (pnt, "internet"); |
| 386 | pnt += strlen ("internet"); |
| 387 | break; |
| 388 | case COMMUNITY_NO_EXPORT: |
| 389 | strcpy (pnt, "no-export"); |
| 390 | pnt += strlen ("no-export"); |
| 391 | break; |
| 392 | case COMMUNITY_NO_ADVERTISE: |
| 393 | strcpy (pnt, "no-advertise"); |
| 394 | pnt += strlen ("no-advertise"); |
| 395 | break; |
| 396 | case COMMUNITY_LOCAL_AS: |
| 397 | strcpy (pnt, "local-AS"); |
| 398 | pnt += strlen ("local-AS"); |
| 399 | break; |
| 400 | default: |
| 401 | as = (comval >> 16) & 0xFFFF; |
| 402 | val = comval & 0xFFFF; |
| 403 | sprintf (pnt, "%u:%d", as, val); |
| 404 | pnt += strlen (pnt); |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | *pnt = '\0'; |
| 409 | |
| 410 | return str; |
| 411 | } |
| 412 | |
| 413 | /* Internal function to perform regular expression match for |
| 414 | * * a single community. */ |
| 415 | static int |
| 416 | community_regexp_include (regex_t * reg, struct community *com, int i) |
| 417 | { |
Christian Franke | 32e41f7 | 2016-06-14 20:06:59 +0200 | [diff] [blame] | 418 | char *str; |
| 419 | int rv; |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 420 | |
| 421 | /* When there is no communities attribute it is treated as empty |
| 422 | * string. */ |
| 423 | if (com == NULL || com->size == 0) |
Christian Franke | 32e41f7 | 2016-06-14 20:06:59 +0200 | [diff] [blame] | 424 | str = XSTRDUP(MTYPE_COMMUNITY_STR, ""); |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 425 | else |
| 426 | str = community_str_get (com, i); |
| 427 | |
| 428 | /* Regular expression match. */ |
Christian Franke | 32e41f7 | 2016-06-14 20:06:59 +0200 | [diff] [blame] | 429 | rv = regexec (reg, str, 0, NULL, 0); |
| 430 | |
| 431 | XFREE(MTYPE_COMMUNITY_STR, str); |
| 432 | |
| 433 | if (rv == 0) |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 434 | return 1; |
| 435 | |
| 436 | /* No match. */ |
| 437 | return 0; |
| 438 | } |
| 439 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 440 | /* Internal function to perform regular expression match for community |
| 441 | attribute. */ |
| 442 | static int |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 443 | community_regexp_match (struct community *com, regex_t * reg) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | { |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 445 | const char *str; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 446 | |
| 447 | /* When there is no communities attribute it is treated as empty |
| 448 | string. */ |
| 449 | if (com == NULL || com->size == 0) |
| 450 | str = ""; |
| 451 | else |
| 452 | str = community_str (com); |
| 453 | |
| 454 | /* Regular expression match. */ |
| 455 | if (regexec (reg, str, 0, NULL, 0) == 0) |
| 456 | return 1; |
| 457 | |
| 458 | /* No match. */ |
| 459 | return 0; |
| 460 | } |
| 461 | |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 462 | static char * |
| 463 | lcommunity_str_get (struct lcommunity *lcom, int i) |
| 464 | { |
| 465 | struct lcommunity_val lcomval; |
| 466 | u_int32_t globaladmin; |
| 467 | u_int32_t localdata1; |
| 468 | u_int32_t localdata2; |
| 469 | char *str; |
| 470 | u_char *ptr; |
| 471 | char *pnt; |
| 472 | |
| 473 | ptr = lcom->val; |
| 474 | ptr += (i * LCOMMUNITY_SIZE); |
| 475 | |
| 476 | memcpy (&lcomval, ptr, LCOMMUNITY_SIZE); |
| 477 | |
| 478 | /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */ |
| 479 | str = pnt = XMALLOC (MTYPE_LCOMMUNITY_STR, 48); |
| 480 | |
| 481 | ptr = (u_char *)lcomval.val; |
| 482 | globaladmin = (*ptr++ << 24); |
| 483 | globaladmin |= (*ptr++ << 16); |
| 484 | globaladmin |= (*ptr++ << 8); |
| 485 | globaladmin |= (*ptr++); |
| 486 | |
| 487 | localdata1 = (*ptr++ << 24); |
| 488 | localdata1 |= (*ptr++ << 16); |
| 489 | localdata1 |= (*ptr++ << 8); |
| 490 | localdata1 |= (*ptr++); |
| 491 | |
| 492 | localdata2 = (*ptr++ << 24); |
| 493 | localdata2 |= (*ptr++ << 16); |
| 494 | localdata2 |= (*ptr++ << 8); |
| 495 | localdata2 |= (*ptr++); |
| 496 | |
| 497 | sprintf (pnt, "%u:%u:%u", globaladmin, localdata1, localdata2); |
| 498 | pnt += strlen (pnt); |
| 499 | *pnt = '\0'; |
| 500 | |
| 501 | return str; |
| 502 | } |
| 503 | |
| 504 | /* Internal function to perform regular expression match for |
| 505 | * * a single community. */ |
| 506 | static int |
| 507 | lcommunity_regexp_include (regex_t * reg, struct lcommunity *lcom, int i) |
| 508 | { |
| 509 | const char *str; |
| 510 | |
| 511 | /* When there is no communities attribute it is treated as empty |
| 512 | * string. */ |
| 513 | if (lcom == NULL || lcom->size == 0) |
| 514 | str = ""; |
| 515 | else |
| 516 | str = lcommunity_str_get (lcom, i); |
| 517 | |
| 518 | /* Regular expression match. */ |
| 519 | if (regexec (reg, str, 0, NULL, 0) == 0) |
| 520 | return 1; |
| 521 | |
| 522 | /* No match. */ |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int |
| 527 | lcommunity_regexp_match (struct lcommunity *com, regex_t * reg) |
| 528 | { |
| 529 | const char *str; |
| 530 | |
| 531 | /* When there is no communities attribute it is treated as empty |
| 532 | string. */ |
| 533 | if (com == NULL || com->size == 0) |
| 534 | str = ""; |
| 535 | else |
| 536 | str = lcommunity_str (com); |
| 537 | |
| 538 | /* Regular expression match. */ |
| 539 | if (regexec (reg, str, 0, NULL, 0) == 0) |
| 540 | return 1; |
| 541 | |
| 542 | /* No match. */ |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 547 | static int |
| 548 | ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg) |
| 549 | { |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 550 | const char *str; |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 551 | |
| 552 | /* When there is no communities attribute it is treated as empty |
| 553 | string. */ |
| 554 | if (ecom == NULL || ecom->size == 0) |
| 555 | str = ""; |
| 556 | else |
| 557 | str = ecommunity_str (ecom); |
| 558 | |
| 559 | /* Regular expression match. */ |
| 560 | if (regexec (reg, str, 0, NULL, 0) == 0) |
| 561 | return 1; |
| 562 | |
| 563 | /* No match. */ |
| 564 | return 0; |
| 565 | } |
| 566 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 567 | /* When given community attribute matches to the community-list return |
| 568 | 1 else return 0. */ |
| 569 | int |
| 570 | community_list_match (struct community *com, struct community_list *list) |
| 571 | { |
| 572 | struct community_entry *entry; |
| 573 | |
| 574 | for (entry = list->head; entry; entry = entry->next) |
| 575 | { |
| 576 | if (entry->any) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 577 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 578 | |
| 579 | if (entry->style == COMMUNITY_LIST_STANDARD) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 580 | { |
| 581 | if (community_include (entry->u.com, COMMUNITY_INTERNET)) |
| 582 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 584 | if (community_match (com, entry->u.com)) |
| 585 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 586 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 587 | else if (entry->style == COMMUNITY_LIST_EXPANDED) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 588 | { |
| 589 | if (community_regexp_match (com, entry->reg)) |
| 590 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 591 | } |
| 592 | } |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | int |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 597 | lcommunity_list_match (struct lcommunity *lcom, struct community_list *list) |
| 598 | { |
| 599 | struct community_entry *entry; |
| 600 | |
| 601 | for (entry = list->head; entry; entry = entry->next) |
| 602 | { |
| 603 | if (entry->any) |
| 604 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 605 | |
| 606 | if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) |
| 607 | { |
| 608 | if (lcommunity_match (lcom, entry->u.lcom)) |
| 609 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 610 | } |
| 611 | else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) |
| 612 | { |
| 613 | if (lcommunity_regexp_match (lcom, entry->reg)) |
| 614 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 615 | } |
| 616 | } |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | int |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 621 | ecommunity_list_match (struct ecommunity *ecom, struct community_list *list) |
| 622 | { |
| 623 | struct community_entry *entry; |
| 624 | |
| 625 | for (entry = list->head; entry; entry = entry->next) |
| 626 | { |
| 627 | if (entry->any) |
| 628 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 629 | |
| 630 | if (entry->style == EXTCOMMUNITY_LIST_STANDARD) |
| 631 | { |
| 632 | if (ecommunity_match (ecom, entry->u.ecom)) |
| 633 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 634 | } |
| 635 | else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED) |
| 636 | { |
| 637 | if (ecommunity_regexp_match (ecom, entry->reg)) |
| 638 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 639 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 640 | } |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | /* Perform exact matching. In case of expanded community-list, do |
| 645 | same thing as community_list_match(). */ |
| 646 | int |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 647 | community_list_exact_match (struct community *com, |
| 648 | struct community_list *list) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 649 | { |
| 650 | struct community_entry *entry; |
| 651 | |
| 652 | for (entry = list->head; entry; entry = entry->next) |
| 653 | { |
| 654 | if (entry->any) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 655 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 656 | |
| 657 | if (entry->style == COMMUNITY_LIST_STANDARD) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 658 | { |
| 659 | if (community_include (entry->u.com, COMMUNITY_INTERNET)) |
| 660 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 661 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 662 | if (community_cmp (com, entry->u.com)) |
| 663 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 664 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 665 | else if (entry->style == COMMUNITY_LIST_EXPANDED) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 666 | { |
| 667 | if (community_regexp_match (com, entry->reg)) |
| 668 | return entry->direct == COMMUNITY_PERMIT ? 1 : 0; |
| 669 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 670 | } |
| 671 | return 0; |
| 672 | } |
| 673 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 674 | /* Delete all permitted communities in the list from com. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 675 | struct community * |
| 676 | community_list_match_delete (struct community *com, |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 677 | struct community_list *list) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 678 | { |
| 679 | struct community_entry *entry; |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 680 | u_int32_t val; |
| 681 | u_int32_t com_index_to_delete[com->size]; |
| 682 | int delete_index = 0; |
| 683 | int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 684 | |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 685 | /* Loop over each community value and evaluate each against the |
| 686 | * community-list. If we need to delete a community value add its index to |
| 687 | * com_index_to_delete. |
| 688 | */ |
| 689 | for (i = 0; i < com->size; i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | { |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 691 | val = community_val_get (com, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 692 | |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 693 | for (entry = list->head; entry; entry = entry->next) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 694 | { |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 695 | if (entry->any) |
| 696 | { |
paul | 847375b | 2003-06-09 18:48:31 +0000 | [diff] [blame] | 697 | if (entry->direct == COMMUNITY_PERMIT) |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 698 | { |
| 699 | com_index_to_delete[delete_index] = i; |
| 700 | delete_index++; |
| 701 | } |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | else if ((entry->style == COMMUNITY_LIST_STANDARD) |
| 706 | && (community_include (entry->u.com, COMMUNITY_INTERNET) |
| 707 | || community_include (entry->u.com, val) )) |
| 708 | { |
| 709 | if (entry->direct == COMMUNITY_PERMIT) |
| 710 | { |
| 711 | com_index_to_delete[delete_index] = i; |
| 712 | delete_index++; |
| 713 | } |
| 714 | break; |
| 715 | } |
| 716 | |
| 717 | else if ((entry->style == COMMUNITY_LIST_EXPANDED) |
| 718 | && community_regexp_include (entry->reg, com, i)) |
| 719 | { |
| 720 | if (entry->direct == COMMUNITY_PERMIT) |
| 721 | { |
| 722 | com_index_to_delete[delete_index] = i; |
| 723 | delete_index++; |
| 724 | } |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* Delete all of the communities we flagged for deletion */ |
| 731 | for (i = delete_index-1; i >= 0; i--) |
| 732 | { |
| 733 | val = community_val_get (com, com_index_to_delete[i]); |
| 734 | community_del_val (com, &val); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 735 | } |
Daniel Walton | d3ac733 | 2015-08-24 10:19:10 -0400 | [diff] [blame] | 736 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | return com; |
| 738 | } |
| 739 | |
| 740 | /* To avoid duplicated entry in the community-list, this function |
| 741 | compares specified entry to existing entry. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 742 | static int |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 743 | community_list_dup_check (struct community_list *list, |
| 744 | struct community_entry *new) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 745 | { |
| 746 | struct community_entry *entry; |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 747 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 748 | for (entry = list->head; entry; entry = entry->next) |
| 749 | { |
| 750 | if (entry->style != new->style) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 751 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 752 | |
| 753 | if (entry->direct != new->direct) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 754 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 755 | |
| 756 | if (entry->any != new->any) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 757 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 758 | |
| 759 | if (entry->any) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 760 | return 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 761 | |
| 762 | switch (entry->style) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 763 | { |
| 764 | case COMMUNITY_LIST_STANDARD: |
| 765 | if (community_cmp (entry->u.com, new->u.com)) |
| 766 | return 1; |
| 767 | break; |
| 768 | case EXTCOMMUNITY_LIST_STANDARD: |
| 769 | if (ecommunity_cmp (entry->u.ecom, new->u.ecom)) |
| 770 | return 1; |
| 771 | break; |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 772 | case LARGE_COMMUNITY_LIST_STANDARD: |
| 773 | if (lcommunity_cmp (entry->u.lcom, new->u.lcom)) |
| 774 | return 1; |
| 775 | break; |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 776 | case COMMUNITY_LIST_EXPANDED: |
| 777 | case EXTCOMMUNITY_LIST_EXPANDED: |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 778 | case LARGE_COMMUNITY_LIST_EXPANDED: |
Christian Franke | c0a613f | 2016-06-06 22:22:15 +0200 | [diff] [blame] | 779 | if (entry->config && new->config |
| 780 | && strcmp (entry->config, new->config) == 0) |
| 781 | return 1; |
| 782 | if (!entry->config && !new->config) |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 783 | return 1; |
| 784 | break; |
| 785 | default: |
| 786 | break; |
| 787 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 788 | } |
| 789 | return 0; |
| 790 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 791 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 792 | /* Set community-list. */ |
| 793 | int |
| 794 | community_list_set (struct community_list_handler *ch, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 795 | const char *name, const char *str, int direct, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 796 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 797 | struct community_entry *entry = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 798 | struct community_list *list; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 799 | struct community *com = NULL; |
| 800 | regex_t *regex = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 801 | |
| 802 | /* Get community list. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 803 | list = community_list_get (ch, name, COMMUNITY_LIST_MASTER); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 804 | |
| 805 | /* When community-list already has entry, new entry should have same |
| 806 | style. If you want to have mixed style community-list, you can |
| 807 | comment out this check. */ |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 808 | if (!community_list_empty_p (list)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 809 | { |
| 810 | struct community_entry *first; |
| 811 | |
| 812 | first = list->head; |
| 813 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 814 | if (style != first->style) |
| 815 | { |
| 816 | return (first->style == COMMUNITY_LIST_STANDARD |
| 817 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT |
| 818 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); |
| 819 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 820 | } |
| 821 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 822 | if (str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 823 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 824 | if (style == COMMUNITY_LIST_STANDARD) |
| 825 | com = community_str2com (str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 826 | else |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 827 | regex = bgp_regcomp (str); |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 828 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 829 | if (! com && ! regex) |
| 830 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 831 | } |
| 832 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 833 | entry = community_entry_new (); |
| 834 | entry->direct = direct; |
| 835 | entry->style = style; |
| 836 | entry->any = (str ? 0 : 1); |
| 837 | entry->u.com = com; |
| 838 | entry->reg = regex; |
| 839 | entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL); |
| 840 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 841 | /* Do not put duplicated community entry. */ |
| 842 | if (community_list_dup_check (list, entry)) |
| 843 | community_entry_free (entry); |
| 844 | else |
| 845 | community_list_entry_add (list, entry); |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | /* Unset community-list. When str is NULL, delete all of |
| 851 | community-list entry belongs to the specified name. */ |
| 852 | int |
| 853 | community_list_unset (struct community_list_handler *ch, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 854 | const char *name, const char *str, |
| 855 | int direct, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 856 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 857 | struct community_entry *entry = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 858 | struct community_list *list; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 859 | struct community *com = NULL; |
| 860 | regex_t *regex = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 861 | |
| 862 | /* Lookup community list. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 863 | list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 864 | if (list == NULL) |
| 865 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 866 | |
| 867 | /* Delete all of entry belongs to this community-list. */ |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 868 | if (!str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 869 | { |
| 870 | community_list_delete (list); |
| 871 | return 0; |
| 872 | } |
| 873 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 874 | if (style == COMMUNITY_LIST_STANDARD) |
| 875 | com = community_str2com (str); |
| 876 | else |
| 877 | regex = bgp_regcomp (str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 878 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 879 | if (! com && ! regex) |
| 880 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 881 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 882 | if (com) |
| 883 | entry = community_list_entry_lookup (list, com, direct); |
| 884 | else |
| 885 | entry = community_list_entry_lookup (list, str, direct); |
| 886 | |
| 887 | if (com) |
| 888 | community_free (com); |
| 889 | if (regex) |
| 890 | bgp_regex_free (regex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 891 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 892 | if (!entry) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 893 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 894 | |
| 895 | community_list_entry_delete (list, entry, style); |
| 896 | |
| 897 | return 0; |
| 898 | } |
| 899 | |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 900 | /* Delete all permitted large communities in the list from com. */ |
| 901 | struct lcommunity * |
| 902 | lcommunity_list_match_delete (struct lcommunity *lcom, |
| 903 | struct community_list *list) |
| 904 | { |
| 905 | struct community_entry *entry; |
| 906 | u_int32_t com_index_to_delete[lcom->size]; |
| 907 | u_char *ptr; |
| 908 | int delete_index = 0; |
| 909 | int i; |
| 910 | |
| 911 | /* Loop over each lcommunity value and evaluate each against the |
| 912 | * community-list. If we need to delete a community value add its index to |
| 913 | * com_index_to_delete. |
| 914 | */ |
| 915 | |
| 916 | for (i = 0; i < lcom->size; i++) |
| 917 | { |
| 918 | ptr = lcom->val + (i * LCOMMUNITY_SIZE); |
| 919 | for (entry = list->head; entry; entry = entry->next) |
| 920 | { |
| 921 | if (entry->any) |
| 922 | { |
| 923 | if (entry->direct == COMMUNITY_PERMIT) |
| 924 | { |
| 925 | com_index_to_delete[delete_index] = i; |
| 926 | delete_index++; |
| 927 | } |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD) |
| 932 | && lcommunity_include (entry->u.lcom, ptr) ) |
| 933 | { |
| 934 | if (entry->direct == COMMUNITY_PERMIT) |
| 935 | { |
| 936 | com_index_to_delete[delete_index] = i; |
| 937 | delete_index++; |
| 938 | } |
| 939 | break; |
| 940 | } |
| 941 | |
| 942 | else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD) |
| 943 | && entry->reg |
| 944 | && lcommunity_regexp_include (entry->reg, lcom, i)) |
| 945 | { |
| 946 | if (entry->direct == COMMUNITY_PERMIT) |
| 947 | { |
| 948 | com_index_to_delete[delete_index] = i; |
| 949 | delete_index++; |
| 950 | } |
| 951 | break; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | /* Delete all of the communities we flagged for deletion */ |
| 957 | |
| 958 | for (i = delete_index-1; i >= 0; i--) |
| 959 | { |
| 960 | ptr = lcom->val + (com_index_to_delete[i] * LCOMMUNITY_SIZE); |
| 961 | lcommunity_del_val (lcom, ptr); |
| 962 | } |
| 963 | |
| 964 | return lcom; |
| 965 | } |
| 966 | |
| 967 | /* Set lcommunity-list. */ |
| 968 | int |
| 969 | lcommunity_list_set (struct community_list_handler *ch, |
| 970 | const char *name, const char *str, int direct, int style) |
| 971 | { |
| 972 | struct community_entry *entry = NULL; |
| 973 | struct community_list *list; |
| 974 | struct lcommunity *lcom = NULL; |
| 975 | regex_t *regex = NULL; |
| 976 | |
| 977 | /* Get community list. */ |
| 978 | list = community_list_get (ch, name, LARGE_COMMUNITY_LIST_MASTER); |
| 979 | |
| 980 | /* When community-list already has entry, new entry should have same |
| 981 | style. If you want to have mixed style community-list, you can |
| 982 | comment out this check. */ |
| 983 | if (!community_list_empty_p (list)) |
| 984 | { |
| 985 | struct community_entry *first; |
| 986 | |
| 987 | first = list->head; |
| 988 | |
| 989 | if (style != first->style) |
| 990 | { |
| 991 | return (first->style == COMMUNITY_LIST_STANDARD |
| 992 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT |
| 993 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | if (str) |
| 998 | { |
| 999 | if (style == LARGE_COMMUNITY_LIST_STANDARD) |
| 1000 | lcom = lcommunity_str2com (str); |
| 1001 | else |
| 1002 | regex = bgp_regcomp (str); |
| 1003 | |
| 1004 | if (! lcom && ! regex) |
| 1005 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
| 1006 | } |
| 1007 | |
| 1008 | entry = community_entry_new (); |
| 1009 | entry->direct = direct; |
| 1010 | entry->style = style; |
| 1011 | entry->any = (str ? 0 : 1); |
| 1012 | entry->u.lcom = lcom; |
| 1013 | entry->reg = regex; |
| 1014 | if (lcom) |
| 1015 | entry->config = lcommunity_lcom2str (lcom, LCOMMUNITY_FORMAT_COMMUNITY_LIST); |
| 1016 | else if (regex) |
| 1017 | entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str); |
| 1018 | else |
| 1019 | entry->config = NULL; |
| 1020 | |
| 1021 | /* Do not put duplicated community entry. */ |
| 1022 | if (community_list_dup_check (list, entry)) |
| 1023 | community_entry_free (entry); |
| 1024 | else |
| 1025 | community_list_entry_add (list, entry); |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | /* Unset community-list. When str is NULL, delete all of |
| 1031 | community-list entry belongs to the specified name. */ |
| 1032 | int |
| 1033 | lcommunity_list_unset (struct community_list_handler *ch, |
| 1034 | const char *name, const char *str, |
| 1035 | int direct, int style) |
| 1036 | { |
| 1037 | struct community_entry *entry = NULL; |
| 1038 | struct community_list *list; |
| 1039 | struct lcommunity *lcom = NULL; |
| 1040 | regex_t *regex = NULL; |
| 1041 | |
| 1042 | /* Lookup community list. */ |
| 1043 | list = community_list_lookup (ch, name, LARGE_COMMUNITY_LIST_MASTER); |
| 1044 | if (list == NULL) |
| 1045 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 1046 | |
| 1047 | /* Delete all of entry belongs to this community-list. */ |
| 1048 | if (!str) |
| 1049 | { |
| 1050 | community_list_delete (list); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | if (style == LARGE_COMMUNITY_LIST_STANDARD) |
| 1055 | lcom = lcommunity_str2com (str); |
| 1056 | else |
| 1057 | regex = bgp_regcomp (str); |
| 1058 | |
| 1059 | if (! lcom && ! regex) |
| 1060 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
| 1061 | |
| 1062 | if (lcom) |
| 1063 | entry = community_list_entry_lookup (list, lcom, direct); |
| 1064 | else |
| 1065 | entry = community_list_entry_lookup (list, str, direct); |
| 1066 | |
| 1067 | if (lcom) |
| 1068 | lcommunity_free (&lcom); |
| 1069 | if (regex) |
| 1070 | bgp_regex_free (regex); |
| 1071 | |
| 1072 | if (!entry) |
| 1073 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 1074 | |
| 1075 | community_list_entry_delete (list, entry, style); |
| 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1080 | /* Set extcommunity-list. */ |
| 1081 | int |
| 1082 | extcommunity_list_set (struct community_list_handler *ch, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1083 | const char *name, const char *str, |
| 1084 | int direct, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1085 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1086 | struct community_entry *entry = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1087 | struct community_list *list; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1088 | struct ecommunity *ecom = NULL; |
| 1089 | regex_t *regex = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1090 | |
| 1091 | entry = NULL; |
| 1092 | |
| 1093 | /* Get community list. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1094 | list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1095 | |
| 1096 | /* When community-list already has entry, new entry should have same |
| 1097 | style. If you want to have mixed style community-list, you can |
| 1098 | comment out this check. */ |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 1099 | if (!community_list_empty_p (list)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1100 | { |
| 1101 | struct community_entry *first; |
| 1102 | |
| 1103 | first = list->head; |
| 1104 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1105 | if (style != first->style) |
| 1106 | { |
| 1107 | return (first->style == EXTCOMMUNITY_LIST_STANDARD |
| 1108 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT |
| 1109 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); |
| 1110 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1113 | if (str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1114 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1115 | if (style == EXTCOMMUNITY_LIST_STANDARD) |
| 1116 | ecom = ecommunity_str2com (str, 0, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1117 | else |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1118 | regex = bgp_regcomp (str); |
| 1119 | |
| 1120 | if (! ecom && ! regex) |
| 1121 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1122 | } |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1123 | |
| 1124 | if (ecom) |
| 1125 | ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY); |
| 1126 | |
| 1127 | entry = community_entry_new (); |
| 1128 | entry->direct = direct; |
| 1129 | entry->style = style; |
| 1130 | entry->any = (str ? 0 : 1); |
| 1131 | if (ecom) |
| 1132 | entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST); |
| 1133 | else if (regex) |
| 1134 | entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1135 | else |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1136 | entry->config = NULL; |
| 1137 | entry->u.ecom = ecom; |
| 1138 | entry->reg = regex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1139 | |
| 1140 | /* Do not put duplicated community entry. */ |
| 1141 | if (community_list_dup_check (list, entry)) |
| 1142 | community_entry_free (entry); |
| 1143 | else |
| 1144 | community_list_entry_add (list, entry); |
| 1145 | |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | /* Unset extcommunity-list. When str is NULL, delete all of |
| 1150 | extcommunity-list entry belongs to the specified name. */ |
| 1151 | int |
| 1152 | extcommunity_list_unset (struct community_list_handler *ch, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1153 | const char *name, const char *str, |
| 1154 | int direct, int style) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1155 | { |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1156 | struct community_entry *entry = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1157 | struct community_list *list; |
| 1158 | struct ecommunity *ecom = NULL; |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1159 | regex_t *regex = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* Lookup extcommunity list. */ |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1162 | list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1163 | if (list == NULL) |
| 1164 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 1165 | |
| 1166 | /* Delete all of entry belongs to this extcommunity-list. */ |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 1167 | if (!str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1168 | { |
| 1169 | community_list_delete (list); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1173 | if (style == EXTCOMMUNITY_LIST_STANDARD) |
| 1174 | ecom = ecommunity_str2com (str, 0, 1); |
| 1175 | else |
| 1176 | regex = bgp_regcomp (str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1177 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1178 | if (! ecom && ! regex) |
| 1179 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1180 | |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1181 | if (ecom) |
| 1182 | entry = community_list_entry_lookup (list, ecom, direct); |
| 1183 | else |
| 1184 | entry = community_list_entry_lookup (list, str, direct); |
| 1185 | |
| 1186 | if (ecom) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 1187 | ecommunity_free (&ecom); |
hasso | fee6e4e | 2005-02-02 16:29:31 +0000 | [diff] [blame] | 1188 | if (regex) |
| 1189 | bgp_regex_free (regex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1190 | |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 1191 | if (!entry) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1192 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; |
| 1193 | |
| 1194 | community_list_entry_delete (list, entry, style); |
| 1195 | |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | /* Initializa community-list. Return community-list handler. */ |
| 1200 | struct community_list_handler * |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1201 | community_list_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1202 | { |
| 1203 | struct community_list_handler *ch; |
| 1204 | ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER, |
paul | 8708b74 | 2003-06-07 02:03:11 +0000 | [diff] [blame] | 1205 | sizeof (struct community_list_handler)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1206 | return ch; |
| 1207 | } |
| 1208 | |
| 1209 | /* Terminate community-list. */ |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 1210 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1211 | community_list_terminate (struct community_list_handler *ch) |
| 1212 | { |
| 1213 | struct community_list_master *cm; |
| 1214 | struct community_list *list; |
| 1215 | |
| 1216 | cm = &ch->community_list; |
| 1217 | while ((list = cm->num.head) != NULL) |
| 1218 | community_list_delete (list); |
| 1219 | while ((list = cm->str.head) != NULL) |
| 1220 | community_list_delete (list); |
| 1221 | |
Job Snijders | 3334bab | 2017-01-20 14:47:12 +0000 | [diff] [blame] | 1222 | cm = &ch->lcommunity_list; |
| 1223 | while ((list = cm->num.head) != NULL) |
| 1224 | community_list_delete (list); |
| 1225 | while ((list = cm->str.head) != NULL) |
| 1226 | community_list_delete (list); |
| 1227 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1228 | cm = &ch->extcommunity_list; |
| 1229 | while ((list = cm->num.head) != NULL) |
| 1230 | community_list_delete (list); |
| 1231 | while ((list = cm->str.head) != NULL) |
| 1232 | community_list_delete (list); |
| 1233 | |
| 1234 | XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch); |
| 1235 | } |