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