]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Community attribute related functions. |
896014f4 DL |
2 | * Copyright (C) 1998, 2001 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 along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
718e3744 | 20 | |
21 | #include <zebra.h> | |
22 | ||
4dcadbef | 23 | #include "command.h" |
718e3744 | 24 | #include "hash.h" |
25 | #include "memory.h" | |
3f65c5b1 | 26 | #include "jhash.h" |
718e3744 | 27 | |
4a1ab8e4 | 28 | #include "bgpd/bgp_memory.h" |
718e3744 | 29 | #include "bgpd/bgp_community.h" |
30 | ||
31 | /* Hash of community attribute. */ | |
730394d9 | 32 | static struct hash *comhash; |
718e3744 | 33 | |
34 | /* Allocate a new communities value. */ | |
d62a17ae | 35 | static struct community *community_new(void) |
718e3744 | 36 | { |
9f5dc319 | 37 | return XCALLOC(MTYPE_COMMUNITY, sizeof(struct community)); |
718e3744 | 38 | } |
39 | ||
40 | /* Free communities value. */ | |
3c1f53de | 41 | void community_free(struct community **com) |
718e3744 | 42 | { |
0a22ddfb QY |
43 | XFREE(MTYPE_COMMUNITY_VAL, (*com)->val); |
44 | XFREE(MTYPE_COMMUNITY_STR, (*com)->str); | |
3c1f53de SMS |
45 | |
46 | if ((*com)->json) { | |
47 | json_object_free((*com)->json); | |
48 | (*com)->json = NULL; | |
d62a17ae | 49 | } |
50 | ||
3c1f53de | 51 | XFREE(MTYPE_COMMUNITY, (*com)); |
718e3744 | 52 | } |
53 | ||
54 | /* Add one community value to the community. */ | |
d7c0a89a | 55 | static void community_add_val(struct community *com, uint32_t val) |
718e3744 | 56 | { |
d62a17ae | 57 | com->size++; |
58 | if (com->val) | |
59 | com->val = XREALLOC(MTYPE_COMMUNITY_VAL, com->val, | |
60 | com_length(com)); | |
61 | else | |
62 | com->val = XMALLOC(MTYPE_COMMUNITY_VAL, com_length(com)); | |
63 | ||
64 | val = htonl(val); | |
d7c0a89a | 65 | memcpy(com_lastval(com), &val, sizeof(uint32_t)); |
718e3744 | 66 | } |
67 | ||
68 | /* Delete one community. */ | |
d7c0a89a | 69 | void community_del_val(struct community *com, uint32_t *val) |
718e3744 | 70 | { |
d62a17ae | 71 | int i = 0; |
72 | int c = 0; | |
73 | ||
74 | if (!com->val) | |
75 | return; | |
76 | ||
77 | while (i < com->size) { | |
d7c0a89a | 78 | if (memcmp(com->val + i, val, sizeof(uint32_t)) == 0) { |
d62a17ae | 79 | c = com->size - i - 1; |
80 | ||
81 | if (c > 0) | |
82 | memmove(com->val + i, com->val + (i + 1), | |
83 | c * sizeof(*val)); | |
84 | ||
85 | com->size--; | |
86 | ||
87 | if (com->size > 0) | |
88 | com->val = XREALLOC(MTYPE_COMMUNITY_VAL, | |
89 | com->val, com_length(com)); | |
90 | else { | |
91 | XFREE(MTYPE_COMMUNITY_VAL, com->val); | |
d62a17ae | 92 | } |
93 | return; | |
94 | } | |
95 | i++; | |
718e3744 | 96 | } |
718e3744 | 97 | } |
98 | ||
99 | /* Delete all communities listed in com2 from com1 */ | |
d62a17ae | 100 | struct community *community_delete(struct community *com1, |
101 | struct community *com2) | |
718e3744 | 102 | { |
d62a17ae | 103 | int i = 0; |
718e3744 | 104 | |
d62a17ae | 105 | while (i < com2->size) { |
106 | community_del_val(com1, com2->val + i); | |
107 | i++; | |
108 | } | |
718e3744 | 109 | |
d62a17ae | 110 | return com1; |
718e3744 | 111 | } |
112 | ||
113 | /* Callback function from qsort(). */ | |
d62a17ae | 114 | static int community_compare(const void *a1, const void *a2) |
718e3744 | 115 | { |
d7c0a89a QY |
116 | uint32_t v1; |
117 | uint32_t v2; | |
d62a17ae | 118 | |
d7c0a89a QY |
119 | memcpy(&v1, a1, sizeof(uint32_t)); |
120 | memcpy(&v2, a2, sizeof(uint32_t)); | |
d62a17ae | 121 | v1 = ntohl(v1); |
122 | v2 = ntohl(v2); | |
123 | ||
124 | if (v1 < v2) | |
125 | return -1; | |
126 | if (v1 > v2) | |
127 | return 1; | |
128 | return 0; | |
718e3744 | 129 | } |
130 | ||
d7c0a89a | 131 | int community_include(struct community *com, uint32_t val) |
718e3744 | 132 | { |
d62a17ae | 133 | int i; |
718e3744 | 134 | |
d62a17ae | 135 | val = htonl(val); |
718e3744 | 136 | |
d62a17ae | 137 | for (i = 0; i < com->size; i++) |
d7c0a89a | 138 | if (memcmp(&val, com_nthval(com, i), sizeof(uint32_t)) == 0) |
d62a17ae | 139 | return 1; |
718e3744 | 140 | |
d62a17ae | 141 | return 0; |
718e3744 | 142 | } |
143 | ||
d7c0a89a | 144 | uint32_t community_val_get(struct community *com, int i) |
718e3744 | 145 | { |
d7c0a89a QY |
146 | uint8_t *p; |
147 | uint32_t val; | |
718e3744 | 148 | |
d7c0a89a | 149 | p = (uint8_t *)com->val; |
d62a17ae | 150 | p += (i * 4); |
718e3744 | 151 | |
d7c0a89a | 152 | memcpy(&val, p, sizeof(uint32_t)); |
718e3744 | 153 | |
d62a17ae | 154 | return ntohl(val); |
718e3744 | 155 | } |
156 | ||
157 | /* Sort and uniq given community. */ | |
d62a17ae | 158 | struct community *community_uniq_sort(struct community *com) |
718e3744 | 159 | { |
d62a17ae | 160 | int i; |
161 | struct community *new; | |
d7c0a89a | 162 | uint32_t val; |
d62a17ae | 163 | |
164 | if (!com) | |
165 | return NULL; | |
166 | ||
167 | new = community_new(); | |
d62a17ae | 168 | new->json = NULL; |
169 | ||
170 | for (i = 0; i < com->size; i++) { | |
171 | val = community_val_get(com, i); | |
172 | ||
173 | if (!community_include(new, val)) | |
174 | community_add_val(new, val); | |
175 | } | |
176 | ||
d7c0a89a | 177 | qsort(new->val, new->size, sizeof(uint32_t), community_compare); |
d62a17ae | 178 | |
179 | return new; | |
718e3744 | 180 | } |
181 | ||
182 | /* Convert communities attribute to string. | |
183 | ||
184 | For Well-known communities value, below keyword is used. | |
185 | ||
d62a17ae | 186 | 0x0 "internet" |
aa861c10 C |
187 | 0xFFFF0000 "graceful-shutdown" |
188 | 0xFFFF0001 "accept-own" | |
189 | 0xFFFF0002 "route-filter-translated-v4" | |
190 | 0xFFFF0003 "route-filter-v4" | |
191 | 0xFFFF0004 "route-filter-translated-v6" | |
192 | 0xFFFF0005 "route-filter-v6" | |
193 | 0xFFFF0006 "llgr-stale" | |
194 | 0xFFFF0007 "no-llgr" | |
195 | 0xFFFF0008 "accept-own-nexthop" | |
196 | 0xFFFF029A "blackhole" | |
718e3744 | 197 | 0xFFFFFF01 "no-export" |
198 | 0xFFFFFF02 "no-advertise" | |
199 | 0xFFFFFF03 "local-AS" | |
aa861c10 | 200 | 0xFFFFFF04 "no-peer" |
718e3744 | 201 | |
202 | For other values, "AS:VAL" format is used. */ | |
a69ea8ae | 203 | static void set_community_string(struct community *com, bool make_json) |
718e3744 | 204 | { |
d62a17ae | 205 | int i; |
206 | char *str; | |
d62a17ae | 207 | int len; |
208 | int first; | |
d7c0a89a QY |
209 | uint32_t comval; |
210 | uint16_t as; | |
211 | uint16_t val; | |
d62a17ae | 212 | json_object *json_community_list = NULL; |
213 | json_object *json_string = NULL; | |
214 | ||
215 | if (!com) | |
216 | return; | |
217 | ||
a69ea8ae DS |
218 | if (make_json) { |
219 | com->json = json_object_new_object(); | |
220 | json_community_list = json_object_new_array(); | |
221 | } | |
d62a17ae | 222 | |
223 | /* When communities attribute is empty. */ | |
224 | if (com->size == 0) { | |
225 | str = XMALLOC(MTYPE_COMMUNITY_STR, 1); | |
226 | str[0] = '\0'; | |
227 | ||
a69ea8ae DS |
228 | if (make_json) { |
229 | json_object_string_add(com->json, "string", ""); | |
996c9314 LB |
230 | json_object_object_add(com->json, "list", |
231 | json_community_list); | |
a69ea8ae | 232 | } |
d62a17ae | 233 | com->str = str; |
234 | return; | |
718e3744 | 235 | } |
d62a17ae | 236 | |
237 | /* Memory allocation is time consuming work. So we calculate | |
238 | required string length first. */ | |
239 | len = 0; | |
240 | ||
241 | for (i = 0; i < com->size; i++) { | |
d7c0a89a | 242 | memcpy(&comval, com_nthval(com, i), sizeof(uint32_t)); |
d62a17ae | 243 | comval = ntohl(comval); |
244 | ||
245 | switch (comval) { | |
246 | case COMMUNITY_INTERNET: | |
247 | len += strlen(" internet"); | |
248 | break; | |
aa861c10 C |
249 | case COMMUNITY_GSHUT: |
250 | len += strlen(" graceful-shutdown"); | |
251 | break; | |
252 | case COMMUNITY_ACCEPT_OWN: | |
253 | len += strlen(" accept-own"); | |
254 | break; | |
255 | case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4: | |
256 | len += strlen(" route-filter-translated-v4"); | |
257 | break; | |
258 | case COMMUNITY_ROUTE_FILTER_v4: | |
259 | len += strlen(" route-filter-v4"); | |
260 | break; | |
261 | case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6: | |
262 | len += strlen(" route-filter-translated-v6"); | |
263 | break; | |
264 | case COMMUNITY_ROUTE_FILTER_v6: | |
265 | len += strlen(" route-filter-v6"); | |
266 | break; | |
267 | case COMMUNITY_LLGR_STALE: | |
268 | len += strlen(" llgr-stale"); | |
269 | break; | |
270 | case COMMUNITY_NO_LLGR: | |
271 | len += strlen(" no-llgr"); | |
272 | break; | |
273 | case COMMUNITY_ACCEPT_OWN_NEXTHOP: | |
274 | len += strlen(" accept-own-nexthop"); | |
275 | break; | |
276 | case COMMUNITY_BLACKHOLE: | |
277 | len += strlen(" blackhole"); | |
278 | break; | |
d62a17ae | 279 | case COMMUNITY_NO_EXPORT: |
280 | len += strlen(" no-export"); | |
281 | break; | |
282 | case COMMUNITY_NO_ADVERTISE: | |
283 | len += strlen(" no-advertise"); | |
284 | break; | |
285 | case COMMUNITY_LOCAL_AS: | |
286 | len += strlen(" local-AS"); | |
287 | break; | |
aa861c10 C |
288 | case COMMUNITY_NO_PEER: |
289 | len += strlen(" no-peer"); | |
7f323236 | 290 | break; |
d62a17ae | 291 | default: |
292 | len += strlen(" 65536:65535"); | |
293 | break; | |
294 | } | |
295 | } | |
296 | ||
297 | /* Allocate memory. */ | |
f9bff3be | 298 | str = XCALLOC(MTYPE_COMMUNITY_STR, len); |
d62a17ae | 299 | first = 1; |
300 | ||
301 | /* Fill in string. */ | |
302 | for (i = 0; i < com->size; i++) { | |
d7c0a89a | 303 | memcpy(&comval, com_nthval(com, i), sizeof(uint32_t)); |
d62a17ae | 304 | comval = ntohl(comval); |
305 | ||
306 | if (first) | |
307 | first = 0; | |
308 | else | |
552d6491 | 309 | strlcat(str, " ", len); |
d62a17ae | 310 | |
311 | switch (comval) { | |
312 | case COMMUNITY_INTERNET: | |
552d6491 | 313 | strlcat(str, "internet", len); |
a69ea8ae | 314 | if (make_json) { |
996c9314 LB |
315 | json_string = |
316 | json_object_new_string("internet"); | |
317 | json_object_array_add(json_community_list, | |
318 | json_string); | |
a69ea8ae | 319 | } |
d62a17ae | 320 | break; |
aa861c10 | 321 | case COMMUNITY_GSHUT: |
6e0b62b4 | 322 | strlcat(str, "graceful-shutdown", len); |
aa861c10 C |
323 | if (make_json) { |
324 | json_string = json_object_new_string( | |
325 | "gracefulShutdown"); | |
326 | json_object_array_add(json_community_list, | |
327 | json_string); | |
328 | } | |
329 | break; | |
330 | case COMMUNITY_ACCEPT_OWN: | |
6e0b62b4 | 331 | strlcat(str, "accept-own", len); |
aa861c10 C |
332 | if (make_json) { |
333 | json_string = json_object_new_string( | |
334 | "acceptown"); | |
335 | json_object_array_add(json_community_list, | |
336 | json_string); | |
337 | } | |
338 | break; | |
339 | case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4: | |
6e0b62b4 | 340 | strlcat(str, "route-filter-translated-v4", len); |
aa861c10 C |
341 | if (make_json) { |
342 | json_string = json_object_new_string( | |
343 | "routeFilterTranslatedV4"); | |
344 | json_object_array_add(json_community_list, | |
345 | json_string); | |
346 | } | |
347 | break; | |
348 | case COMMUNITY_ROUTE_FILTER_v4: | |
6e0b62b4 | 349 | strlcat(str, "route-filter-v4", len); |
aa861c10 C |
350 | if (make_json) { |
351 | json_string = json_object_new_string( | |
352 | "routeFilterV4"); | |
353 | json_object_array_add(json_community_list, | |
354 | json_string); | |
355 | } | |
356 | break; | |
357 | case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6: | |
6e0b62b4 | 358 | strlcat(str, "route-filter-translated-v6", len); |
aa861c10 C |
359 | if (make_json) { |
360 | json_string = json_object_new_string( | |
361 | "routeFilterTranslatedV6"); | |
362 | json_object_array_add(json_community_list, | |
363 | json_string); | |
364 | } | |
365 | break; | |
366 | case COMMUNITY_ROUTE_FILTER_v6: | |
6e0b62b4 | 367 | strlcat(str, "route-filter-v6", len); |
aa861c10 C |
368 | if (make_json) { |
369 | json_string = json_object_new_string( | |
370 | "routeFilterV6"); | |
371 | json_object_array_add(json_community_list, | |
372 | json_string); | |
373 | } | |
374 | break; | |
375 | case COMMUNITY_LLGR_STALE: | |
6e0b62b4 | 376 | strlcat(str, "llgr-stale", len); |
aa861c10 C |
377 | if (make_json) { |
378 | json_string = json_object_new_string( | |
379 | "llgrStale"); | |
380 | json_object_array_add(json_community_list, | |
381 | json_string); | |
382 | } | |
383 | break; | |
384 | case COMMUNITY_NO_LLGR: | |
6e0b62b4 | 385 | strlcat(str, "no-llgr", len); |
aa861c10 C |
386 | if (make_json) { |
387 | json_string = json_object_new_string( | |
388 | "noLlgr"); | |
389 | json_object_array_add(json_community_list, | |
390 | json_string); | |
391 | } | |
392 | break; | |
393 | case COMMUNITY_ACCEPT_OWN_NEXTHOP: | |
6e0b62b4 | 394 | strlcat(str, "accept-own-nexthop", len); |
aa861c10 C |
395 | if (make_json) { |
396 | json_string = json_object_new_string( | |
397 | "acceptownnexthop"); | |
398 | json_object_array_add(json_community_list, | |
399 | json_string); | |
400 | } | |
401 | break; | |
402 | case COMMUNITY_BLACKHOLE: | |
6e0b62b4 | 403 | strlcat(str, "blackhole", len); |
aa861c10 C |
404 | if (make_json) { |
405 | json_string = json_object_new_string( | |
406 | "blackhole"); | |
407 | json_object_array_add(json_community_list, | |
408 | json_string); | |
409 | } | |
410 | break; | |
d62a17ae | 411 | case COMMUNITY_NO_EXPORT: |
6e0b62b4 | 412 | strlcat(str, "no-export", len); |
a69ea8ae | 413 | if (make_json) { |
996c9314 LB |
414 | json_string = |
415 | json_object_new_string("noExport"); | |
416 | json_object_array_add(json_community_list, | |
417 | json_string); | |
a69ea8ae | 418 | } |
d62a17ae | 419 | break; |
420 | case COMMUNITY_NO_ADVERTISE: | |
6e0b62b4 | 421 | strlcat(str, "no-advertise", len); |
a69ea8ae | 422 | if (make_json) { |
996c9314 LB |
423 | json_string = |
424 | json_object_new_string("noAdvertise"); | |
425 | json_object_array_add(json_community_list, | |
426 | json_string); | |
a69ea8ae | 427 | } |
d62a17ae | 428 | break; |
429 | case COMMUNITY_LOCAL_AS: | |
6e0b62b4 | 430 | strlcat(str, "local-AS", len); |
a69ea8ae DS |
431 | if (make_json) { |
432 | json_string = json_object_new_string("localAs"); | |
996c9314 LB |
433 | json_object_array_add(json_community_list, |
434 | json_string); | |
a69ea8ae | 435 | } |
d62a17ae | 436 | break; |
aa861c10 | 437 | case COMMUNITY_NO_PEER: |
6e0b62b4 | 438 | strlcat(str, "no-peer", len); |
a69ea8ae | 439 | if (make_json) { |
aa861c10 | 440 | json_string = json_object_new_string("noPeer"); |
996c9314 LB |
441 | json_object_array_add(json_community_list, |
442 | json_string); | |
a69ea8ae | 443 | } |
7f323236 | 444 | break; |
d62a17ae | 445 | default: |
446 | as = (comval >> 16) & 0xFFFF; | |
447 | val = comval & 0xFFFF; | |
6e0b62b4 QY |
448 | char buf[32]; |
449 | snprintf(buf, sizeof(buf), "%u:%d", as, val); | |
450 | strlcat(str, buf, len); | |
a69ea8ae | 451 | if (make_json) { |
6e0b62b4 | 452 | json_string = json_object_new_string(buf); |
996c9314 LB |
453 | json_object_array_add(json_community_list, |
454 | json_string); | |
a69ea8ae | 455 | } |
d62a17ae | 456 | break; |
457 | } | |
718e3744 | 458 | } |
718e3744 | 459 | |
a69ea8ae DS |
460 | if (make_json) { |
461 | json_object_string_add(com->json, "string", str); | |
462 | json_object_object_add(com->json, "list", json_community_list); | |
463 | } | |
d62a17ae | 464 | com->str = str; |
718e3744 | 465 | } |
466 | ||
467 | /* Intern communities attribute. */ | |
d62a17ae | 468 | struct community *community_intern(struct community *com) |
718e3744 | 469 | { |
d62a17ae | 470 | struct community *find; |
718e3744 | 471 | |
d62a17ae | 472 | /* Assert this community structure is not interned. */ |
473 | assert(com->refcnt == 0); | |
718e3744 | 474 | |
d62a17ae | 475 | /* Lookup community hash. */ |
476 | find = (struct community *)hash_get(comhash, com, hash_alloc_intern); | |
718e3744 | 477 | |
d62a17ae | 478 | /* Arguemnt com is allocated temporary. So when it is not used in |
479 | hash, it should be freed. */ | |
480 | if (find != com) | |
3c1f53de | 481 | community_free(&com); |
718e3744 | 482 | |
d62a17ae | 483 | /* Increment refrence counter. */ |
484 | find->refcnt++; | |
718e3744 | 485 | |
d62a17ae | 486 | /* Make string. */ |
487 | if (!find->str) | |
a69ea8ae | 488 | set_community_string(find, false); |
718e3744 | 489 | |
d62a17ae | 490 | return find; |
718e3744 | 491 | } |
492 | ||
493 | /* Free community attribute. */ | |
d62a17ae | 494 | void community_unintern(struct community **com) |
718e3744 | 495 | { |
d62a17ae | 496 | struct community *ret; |
718e3744 | 497 | |
d62a17ae | 498 | if ((*com)->refcnt) |
499 | (*com)->refcnt--; | |
718e3744 | 500 | |
d62a17ae | 501 | /* Pull off from hash. */ |
502 | if ((*com)->refcnt == 0) { | |
503 | /* Community value com must exist in hash. */ | |
504 | ret = (struct community *)hash_release(comhash, *com); | |
505 | assert(ret != NULL); | |
718e3744 | 506 | |
3c1f53de | 507 | community_free(com); |
d62a17ae | 508 | } |
718e3744 | 509 | } |
510 | ||
511 | /* Create new community attribute. */ | |
d7c0a89a | 512 | struct community *community_parse(uint32_t *pnt, unsigned short length) |
718e3744 | 513 | { |
d62a17ae | 514 | struct community tmp; |
515 | struct community *new; | |
718e3744 | 516 | |
d62a17ae | 517 | /* If length is malformed return NULL. */ |
518 | if (length % 4) | |
519 | return NULL; | |
718e3744 | 520 | |
d62a17ae | 521 | /* Make temporary community for hash look up. */ |
522 | tmp.size = length / 4; | |
523 | tmp.val = pnt; | |
718e3744 | 524 | |
d62a17ae | 525 | new = community_uniq_sort(&tmp); |
718e3744 | 526 | |
d62a17ae | 527 | return community_intern(new); |
718e3744 | 528 | } |
529 | ||
d62a17ae | 530 | struct community *community_dup(struct community *com) |
718e3744 | 531 | { |
d62a17ae | 532 | struct community *new; |
533 | ||
534 | new = XCALLOC(MTYPE_COMMUNITY, sizeof(struct community)); | |
535 | new->size = com->size; | |
536 | if (new->size) { | |
537 | new->val = XMALLOC(MTYPE_COMMUNITY_VAL, com->size * 4); | |
538 | memcpy(new->val, com->val, com->size * 4); | |
539 | } else | |
540 | new->val = NULL; | |
541 | return new; | |
718e3744 | 542 | } |
543 | ||
544 | /* Retrun string representation of communities attribute. */ | |
a69ea8ae | 545 | char *community_str(struct community *com, bool make_json) |
718e3744 | 546 | { |
d62a17ae | 547 | if (!com) |
548 | return NULL; | |
f1aa5d8a | 549 | |
a69ea8ae DS |
550 | if (make_json && !com->json && com->str) |
551 | XFREE(MTYPE_COMMUNITY_STR, com->str); | |
552 | ||
d62a17ae | 553 | if (!com->str) |
a69ea8ae | 554 | set_community_string(com, make_json); |
d62a17ae | 555 | return com->str; |
718e3744 | 556 | } |
557 | ||
558 | /* Make hash value of community attribute. This function is used by | |
559 | hash package.*/ | |
d8b87afe | 560 | unsigned int community_hash_make(const struct community *com) |
718e3744 | 561 | { |
d7c0a89a | 562 | uint32_t *pnt = (uint32_t *)com->val; |
d62a17ae | 563 | |
3f65c5b1 | 564 | return jhash2(pnt, com->size, 0x43ea96c1); |
718e3744 | 565 | } |
566 | ||
d62a17ae | 567 | int community_match(const struct community *com1, const struct community *com2) |
718e3744 | 568 | { |
d62a17ae | 569 | int i = 0; |
570 | int j = 0; | |
571 | ||
572 | if (com1 == NULL && com2 == NULL) | |
573 | return 1; | |
574 | ||
575 | if (com1 == NULL || com2 == NULL) | |
576 | return 0; | |
577 | ||
578 | if (com1->size < com2->size) | |
579 | return 0; | |
580 | ||
581 | /* Every community on com2 needs to be on com1 for this to match */ | |
582 | while (i < com1->size && j < com2->size) { | |
d7c0a89a | 583 | if (memcmp(com1->val + i, com2->val + j, sizeof(uint32_t)) == 0) |
d62a17ae | 584 | j++; |
585 | i++; | |
586 | } | |
587 | ||
588 | if (j == com2->size) | |
589 | return 1; | |
590 | else | |
591 | return 0; | |
718e3744 | 592 | } |
593 | ||
594 | /* If two aspath have same value then return 1 else return 0. This | |
595 | function is used by hash package. */ | |
74df8d6d | 596 | bool community_cmp(const struct community *com1, const struct community *com2) |
718e3744 | 597 | { |
d62a17ae | 598 | if (com1 == NULL && com2 == NULL) |
74df8d6d | 599 | return true; |
d62a17ae | 600 | if (com1 == NULL || com2 == NULL) |
74df8d6d | 601 | return false; |
d62a17ae | 602 | |
603 | if (com1->size == com2->size) | |
604 | if (memcmp(com1->val, com2->val, com1->size * 4) == 0) | |
74df8d6d DS |
605 | return true; |
606 | return false; | |
718e3744 | 607 | } |
608 | ||
609 | /* Add com2 to the end of com1. */ | |
d62a17ae | 610 | struct community *community_merge(struct community *com1, |
611 | struct community *com2) | |
718e3744 | 612 | { |
d62a17ae | 613 | if (com1->val) |
614 | com1->val = XREALLOC(MTYPE_COMMUNITY_VAL, com1->val, | |
615 | (com1->size + com2->size) * 4); | |
616 | else | |
617 | com1->val = XMALLOC(MTYPE_COMMUNITY_VAL, | |
618 | (com1->size + com2->size) * 4); | |
718e3744 | 619 | |
d62a17ae | 620 | memcpy(com1->val + com1->size, com2->val, com2->size * 4); |
621 | com1->size += com2->size; | |
718e3744 | 622 | |
d62a17ae | 623 | return com1; |
718e3744 | 624 | } |
625 | ||
626 | /* Community token enum. */ | |
d62a17ae | 627 | enum community_token { |
628 | community_token_val, | |
aa861c10 C |
629 | community_token_gshut, |
630 | community_token_accept_own, | |
631 | community_token_route_filter_translated_v4, | |
632 | community_token_route_filter_v4, | |
633 | community_token_route_filter_translated_v6, | |
634 | community_token_route_filter_v6, | |
635 | community_token_llgr_stale, | |
636 | community_token_no_llgr, | |
637 | community_token_accept_own_nexthop, | |
638 | community_token_blackhole, | |
d62a17ae | 639 | community_token_no_export, |
640 | community_token_no_advertise, | |
641 | community_token_local_as, | |
aa861c10 | 642 | community_token_no_peer, |
d62a17ae | 643 | community_token_unknown |
718e3744 | 644 | }; |
645 | ||
646 | /* Get next community token from string. */ | |
94f2b392 | 647 | static const char * |
d7c0a89a | 648 | community_gettoken(const char *buf, enum community_token *token, uint32_t *val) |
718e3744 | 649 | { |
d62a17ae | 650 | const char *p = buf; |
651 | ||
652 | /* Skip white space. */ | |
fefa5e0f | 653 | while (isspace((unsigned char)*p)) |
d62a17ae | 654 | p++; |
655 | ||
656 | /* Check the end of the line. */ | |
657 | if (*p == '\0') | |
658 | return NULL; | |
659 | ||
660 | /* Well known community string check. */ | |
fefa5e0f | 661 | if (isalpha((unsigned char)*p)) { |
d62a17ae | 662 | if (strncmp(p, "internet", strlen("internet")) == 0) { |
663 | *val = COMMUNITY_INTERNET; | |
664 | *token = community_token_no_export; | |
665 | p += strlen("internet"); | |
666 | return p; | |
667 | } | |
aa861c10 C |
668 | if (strncmp(p, "graceful-shutdown", strlen("graceful-shutdown")) |
669 | == 0) { | |
670 | *val = COMMUNITY_GSHUT; | |
671 | *token = community_token_gshut; | |
672 | p += strlen("graceful-shutdown"); | |
673 | return p; | |
674 | } | |
675 | if (strncmp(p, "accept-own", strlen("accept-own")) | |
676 | == 0) { | |
677 | *val = COMMUNITY_ACCEPT_OWN; | |
678 | *token = community_token_accept_own; | |
679 | p += strlen("accept-own"); | |
680 | return p; | |
681 | } | |
682 | if (strncmp(p, "route-filter-translated-v4", | |
683 | strlen("route-filter-translated-v4")) | |
684 | == 0) { | |
685 | *val = COMMUNITY_ROUTE_FILTER_TRANSLATED_v4; | |
686 | *token = community_token_route_filter_translated_v4; | |
687 | p += strlen("route-filter-translated-v4"); | |
688 | return p; | |
689 | } | |
690 | if (strncmp(p, "route-filter-v4", strlen("route-filter-v4")) | |
691 | == 0) { | |
692 | *val = COMMUNITY_ROUTE_FILTER_v4; | |
693 | *token = community_token_route_filter_v4; | |
694 | p += strlen("route-filter-v4"); | |
695 | return p; | |
696 | } | |
697 | if (strncmp(p, "route-filter-translated-v6", | |
698 | strlen("route-filter-translated-v6")) | |
699 | == 0) { | |
700 | *val = COMMUNITY_ROUTE_FILTER_TRANSLATED_v6; | |
701 | *token = community_token_route_filter_translated_v6; | |
702 | p += strlen("route-filter-translated-v6"); | |
703 | return p; | |
704 | } | |
705 | if (strncmp(p, "route-filter-v6", strlen("route-filter-v6")) | |
706 | == 0) { | |
707 | *val = COMMUNITY_ROUTE_FILTER_v6; | |
708 | *token = community_token_route_filter_v6; | |
709 | p += strlen("route-filter-v6"); | |
710 | return p; | |
711 | } | |
712 | if (strncmp(p, "llgr-stale", strlen("llgr-stale")) | |
713 | == 0) { | |
714 | *val = COMMUNITY_LLGR_STALE; | |
715 | *token = community_token_llgr_stale; | |
716 | p += strlen("llgr-stale"); | |
717 | return p; | |
718 | } | |
719 | if (strncmp(p, "no-llgr", strlen("no-llgr")) | |
720 | == 0) { | |
721 | *val = COMMUNITY_NO_LLGR; | |
722 | *token = community_token_no_llgr; | |
723 | p += strlen("no-llgr"); | |
724 | return p; | |
725 | } | |
726 | if (strncmp(p, "accept-own-nexthop", | |
727 | strlen("accept-own-nexthop")) | |
728 | == 0) { | |
729 | *val = COMMUNITY_ACCEPT_OWN_NEXTHOP; | |
730 | *token = community_token_accept_own_nexthop; | |
731 | p += strlen("accept-own-nexthop"); | |
732 | return p; | |
733 | } | |
734 | if (strncmp(p, "blackhole", strlen("blackhole")) | |
735 | == 0) { | |
736 | *val = COMMUNITY_BLACKHOLE; | |
737 | *token = community_token_blackhole; | |
738 | p += strlen("blackhole"); | |
739 | return p; | |
740 | } | |
d62a17ae | 741 | if (strncmp(p, "no-export", strlen("no-export")) == 0) { |
742 | *val = COMMUNITY_NO_EXPORT; | |
743 | *token = community_token_no_export; | |
744 | p += strlen("no-export"); | |
745 | return p; | |
746 | } | |
747 | if (strncmp(p, "no-advertise", strlen("no-advertise")) == 0) { | |
748 | *val = COMMUNITY_NO_ADVERTISE; | |
749 | *token = community_token_no_advertise; | |
750 | p += strlen("no-advertise"); | |
751 | return p; | |
752 | } | |
753 | if (strncmp(p, "local-AS", strlen("local-AS")) == 0) { | |
754 | *val = COMMUNITY_LOCAL_AS; | |
755 | *token = community_token_local_as; | |
756 | p += strlen("local-AS"); | |
757 | return p; | |
758 | } | |
aa861c10 C |
759 | if (strncmp(p, "no-peer", strlen("no-peer")) == 0) { |
760 | *val = COMMUNITY_NO_PEER; | |
761 | *token = community_token_no_peer; | |
762 | p += strlen("no-peer"); | |
7f323236 DW |
763 | return p; |
764 | } | |
d62a17ae | 765 | |
766 | /* Unknown string. */ | |
767 | *token = community_token_unknown; | |
768 | return NULL; | |
718e3744 | 769 | } |
770 | ||
d62a17ae | 771 | /* Community value. */ |
fefa5e0f | 772 | if (isdigit((unsigned char)*p)) { |
d62a17ae | 773 | int separator = 0; |
774 | int digit = 0; | |
d7c0a89a QY |
775 | uint32_t community_low = 0; |
776 | uint32_t community_high = 0; | |
d62a17ae | 777 | |
fefa5e0f | 778 | while (isdigit((unsigned char)*p) || *p == ':') { |
d62a17ae | 779 | if (*p == ':') { |
780 | if (separator) { | |
781 | *token = community_token_unknown; | |
782 | return NULL; | |
783 | } else { | |
784 | separator = 1; | |
785 | digit = 0; | |
786 | ||
787 | if (community_low > UINT16_MAX) { | |
788 | *token = | |
789 | community_token_unknown; | |
790 | return NULL; | |
791 | } | |
792 | ||
793 | community_high = community_low << 16; | |
794 | community_low = 0; | |
795 | } | |
796 | } else { | |
797 | digit = 1; | |
798 | community_low *= 10; | |
799 | community_low += (*p - '0'); | |
800 | } | |
801 | p++; | |
718e3744 | 802 | } |
d62a17ae | 803 | if (!digit) { |
804 | *token = community_token_unknown; | |
805 | return NULL; | |
806 | } | |
807 | ||
808 | if (community_low > UINT16_MAX) { | |
809 | *token = community_token_unknown; | |
810 | return NULL; | |
718e3744 | 811 | } |
859d388e | 812 | |
d62a17ae | 813 | *val = community_high + community_low; |
814 | *token = community_token_val; | |
815 | return p; | |
816 | } | |
817 | *token = community_token_unknown; | |
818 | return NULL; | |
718e3744 | 819 | } |
820 | ||
821 | /* convert string to community structure */ | |
d62a17ae | 822 | struct community *community_str2com(const char *str) |
718e3744 | 823 | { |
d62a17ae | 824 | struct community *com = NULL; |
825 | struct community *com_sort = NULL; | |
d7c0a89a | 826 | uint32_t val = 0; |
d62a17ae | 827 | enum community_token token = community_token_unknown; |
828 | ||
829 | do { | |
830 | str = community_gettoken(str, &token, &val); | |
831 | ||
832 | switch (token) { | |
833 | case community_token_val: | |
aa861c10 C |
834 | case community_token_gshut: |
835 | case community_token_accept_own: | |
836 | case community_token_route_filter_translated_v4: | |
837 | case community_token_route_filter_v4: | |
838 | case community_token_route_filter_translated_v6: | |
839 | case community_token_route_filter_v6: | |
840 | case community_token_llgr_stale: | |
841 | case community_token_no_llgr: | |
842 | case community_token_accept_own_nexthop: | |
843 | case community_token_blackhole: | |
d62a17ae | 844 | case community_token_no_export: |
845 | case community_token_no_advertise: | |
846 | case community_token_local_as: | |
aa861c10 | 847 | case community_token_no_peer: |
d62a17ae | 848 | if (com == NULL) { |
849 | com = community_new(); | |
850 | com->json = NULL; | |
851 | } | |
852 | community_add_val(com, val); | |
853 | break; | |
854 | case community_token_unknown: | |
d62a17ae | 855 | if (com) |
3c1f53de | 856 | community_free(&com); |
d62a17ae | 857 | return NULL; |
858 | } | |
859 | } while (str); | |
860 | ||
d62a17ae | 861 | com_sort = community_uniq_sort(com); |
3c1f53de | 862 | community_free(&com); |
718e3744 | 863 | |
d62a17ae | 864 | return com_sort; |
718e3744 | 865 | } |
866 | ||
867 | /* Return communities hash entry count. */ | |
d62a17ae | 868 | unsigned long community_count(void) |
718e3744 | 869 | { |
d62a17ae | 870 | return comhash->count; |
718e3744 | 871 | } |
872 | ||
873 | /* Return communities hash. */ | |
d62a17ae | 874 | struct hash *community_hash(void) |
718e3744 | 875 | { |
d62a17ae | 876 | return comhash; |
718e3744 | 877 | } |
878 | ||
879 | /* Initialize comminity related hash. */ | |
d62a17ae | 880 | void community_init(void) |
718e3744 | 881 | { |
996c9314 | 882 | comhash = |
d8b87afe | 883 | hash_create((unsigned int (*)(const void *))community_hash_make, |
74df8d6d | 884 | (bool (*)(const void *, const void *))community_cmp, |
996c9314 | 885 | "BGP Community Hash"); |
718e3744 | 886 | } |
228da428 | 887 | |
d62a17ae | 888 | void community_finish(void) |
228da428 | 889 | { |
d62a17ae | 890 | hash_free(comhash); |
891 | comhash = NULL; | |
228da428 | 892 | } |
c0d7a6cc NT |
893 | |
894 | static struct community *bgp_aggr_community_lookup( | |
895 | struct bgp_aggregate *aggregate, | |
896 | struct community *community) | |
897 | { | |
898 | return hash_lookup(aggregate->community_hash, community); | |
899 | } | |
900 | ||
901 | static void *bgp_aggr_communty_hash_alloc(void *p) | |
902 | { | |
903 | struct community *ref = (struct community *)p; | |
904 | struct community *community = NULL; | |
905 | ||
906 | community = community_dup(ref); | |
907 | return community; | |
908 | } | |
909 | ||
4d67e752 | 910 | static void bgp_aggr_community_prepare(struct hash_backet *hb, void *arg) |
c0d7a6cc | 911 | { |
c0d7a6cc NT |
912 | struct community *hb_community = hb->data; |
913 | struct community **aggr_community = arg; | |
914 | ||
21fec674 | 915 | if (*aggr_community) |
916 | *aggr_community = community_merge(*aggr_community, | |
917 | hb_community); | |
918 | else | |
c0d7a6cc NT |
919 | *aggr_community = community_dup(hb_community); |
920 | } | |
921 | ||
922 | void bgp_aggr_community_remove(void *arg) | |
923 | { | |
924 | struct community *community = arg; | |
925 | ||
926 | community_free(&community); | |
927 | } | |
928 | ||
929 | void bgp_compute_aggregate_community(struct bgp_aggregate *aggregate, | |
930 | struct community *community) | |
21fec674 | 931 | { |
932 | bgp_compute_aggregate_community_hash(aggregate, community); | |
933 | bgp_compute_aggregate_community_val(aggregate); | |
934 | } | |
935 | ||
936 | ||
937 | void bgp_compute_aggregate_community_hash(struct bgp_aggregate *aggregate, | |
938 | struct community *community) | |
c0d7a6cc NT |
939 | { |
940 | struct community *aggr_community = NULL; | |
941 | ||
942 | if ((aggregate == NULL) || (community == NULL)) | |
943 | return; | |
944 | ||
945 | /* Create hash if not already created. | |
946 | */ | |
947 | if (aggregate->community_hash == NULL) | |
948 | aggregate->community_hash = hash_create( | |
d8b87afe | 949 | (unsigned int (*)(const void *))community_hash_make, |
c0d7a6cc NT |
950 | (bool (*)(const void *, const void *))community_cmp, |
951 | "BGP Aggregator community hash"); | |
952 | ||
953 | aggr_community = bgp_aggr_community_lookup(aggregate, community); | |
954 | if (aggr_community == NULL) { | |
955 | /* Insert community into hash. | |
956 | */ | |
957 | aggr_community = hash_get(aggregate->community_hash, community, | |
958 | bgp_aggr_communty_hash_alloc); | |
21fec674 | 959 | } |
c0d7a6cc | 960 | |
21fec674 | 961 | /* Increment reference counter. |
962 | */ | |
963 | aggr_community->refcnt++; | |
964 | } | |
c0d7a6cc | 965 | |
21fec674 | 966 | void bgp_compute_aggregate_community_val(struct bgp_aggregate *aggregate) |
967 | { | |
968 | struct community *commerge = NULL; | |
969 | ||
970 | if (aggregate == NULL) | |
971 | return; | |
972 | ||
973 | /* Re-compute aggregate's community. | |
974 | */ | |
975 | if (aggregate->community) | |
976 | community_free(&aggregate->community); | |
977 | if (aggregate->community_hash && | |
978 | aggregate->community_hash->count) { | |
c0d7a6cc NT |
979 | hash_iterate(aggregate->community_hash, |
980 | bgp_aggr_community_prepare, | |
981 | &aggregate->community); | |
21fec674 | 982 | commerge = aggregate->community; |
983 | aggregate->community = community_uniq_sort(commerge); | |
984 | if (commerge) | |
985 | community_free(&commerge); | |
c0d7a6cc | 986 | } |
c0d7a6cc NT |
987 | } |
988 | ||
21fec674 | 989 | |
990 | ||
c0d7a6cc NT |
991 | void bgp_remove_community_from_aggregate(struct bgp_aggregate *aggregate, |
992 | struct community *community) | |
993 | { | |
994 | struct community *aggr_community = NULL; | |
995 | struct community *ret_comm = NULL; | |
996 | ||
21fec674 | 997 | if ((!aggregate) |
998 | || (!aggregate->community_hash) | |
999 | || (!community)) | |
c0d7a6cc NT |
1000 | return; |
1001 | ||
1002 | /* Look-up the community in the hash. | |
1003 | */ | |
1004 | aggr_community = bgp_aggr_community_lookup(aggregate, community); | |
1005 | if (aggr_community) { | |
1006 | aggr_community->refcnt--; | |
1007 | ||
1008 | if (aggr_community->refcnt == 0) { | |
1009 | ret_comm = hash_release(aggregate->community_hash, | |
1010 | aggr_community); | |
1011 | community_free(&ret_comm); | |
1012 | ||
21fec674 | 1013 | bgp_compute_aggregate_community_val(aggregate); |
1014 | } | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | void bgp_remove_comm_from_aggregate_hash(struct bgp_aggregate *aggregate, | |
1019 | struct community *community) | |
1020 | { | |
1021 | ||
1022 | struct community *aggr_community = NULL; | |
1023 | struct community *ret_comm = NULL; | |
c0d7a6cc | 1024 | |
21fec674 | 1025 | if ((!aggregate) |
1026 | || (!aggregate->community_hash) | |
1027 | || (!community)) | |
1028 | return; | |
1029 | ||
1030 | /* Look-up the community in the hash. | |
1031 | */ | |
1032 | aggr_community = bgp_aggr_community_lookup(aggregate, community); | |
1033 | if (aggr_community) { | |
1034 | aggr_community->refcnt--; | |
1035 | ||
1036 | if (aggr_community->refcnt == 0) { | |
1037 | ret_comm = hash_release(aggregate->community_hash, | |
1038 | aggr_community); | |
1039 | community_free(&ret_comm); | |
c0d7a6cc NT |
1040 | } |
1041 | } | |
1042 | } |