]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* |
3 | * Prefix related functions. | |
4 | * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro | |
718e3744 | 5 | */ |
6 | ||
7 | #include <zebra.h> | |
8 | ||
d80132b1 | 9 | #include "command.h" |
718e3744 | 10 | #include "prefix.h" |
dc5d0186 | 11 | #include "ipaddr.h" |
718e3744 | 12 | #include "vty.h" |
13 | #include "sockunion.h" | |
14 | #include "memory.h" | |
15 | #include "log.h" | |
7a7761d2 | 16 | #include "jhash.h" |
472878dc | 17 | #include "lib_errors.h" |
d52ec572 | 18 | #include "printfrr.h" |
74e2bd89 | 19 | #include "vxlan.h" |
6b0655a2 | 20 | |
bf8d3d6a DL |
21 | DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix"); |
22 | DEFINE_MTYPE_STATIC(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec"); | |
4a1ab8e4 | 23 | |
718e3744 | 24 | /* Maskbit. */ |
d7c0a89a QY |
25 | static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, |
26 | 0xf8, 0xfc, 0xfe, 0xff}; | |
d62a17ae | 27 | |
718e3744 | 28 | /* Number of bits in prefix type. */ |
29 | #ifndef PNBBY | |
30 | #define PNBBY 8 | |
31 | #endif /* PNBBY */ | |
32 | ||
33 | #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff) | |
34 | ||
a9e08ebc | 35 | int is_zero_mac(const struct ethaddr *mac) |
69b61704 MK |
36 | { |
37 | int i = 0; | |
38 | ||
39 | for (i = 0; i < ETH_ALEN; i++) { | |
40 | if (mac->octet[i]) | |
41 | return 0; | |
42 | } | |
43 | ||
44 | return 1; | |
45 | } | |
46 | ||
c6ec0c74 KA |
47 | bool is_bcast_mac(const struct ethaddr *mac) |
48 | { | |
49 | int i = 0; | |
50 | ||
51 | for (i = 0; i < ETH_ALEN; i++) | |
52 | if (mac->octet[i] != 0xFF) | |
53 | return false; | |
54 | ||
55 | return true; | |
56 | } | |
57 | ||
58 | bool is_mcast_mac(const struct ethaddr *mac) | |
59 | { | |
60 | if ((mac->octet[0] & 0x01) == 0x01) | |
61 | return true; | |
62 | ||
63 | return false; | |
64 | } | |
65 | ||
adeb0672 | 66 | unsigned int prefix_bit(const uint8_t *prefix, const uint16_t bit_index) |
f63f06da | 67 | { |
adeb0672 QY |
68 | unsigned int offset = bit_index / 8; |
69 | unsigned int shift = 7 - (bit_index % 8); | |
d62a17ae | 70 | |
71 | return (prefix[offset] >> shift) & 1; | |
f63f06da PJ |
72 | } |
73 | ||
d62a17ae | 74 | int str2family(const char *string) |
f3ccedaa | 75 | { |
d62a17ae | 76 | if (!strcmp("ipv4", string)) |
77 | return AF_INET; | |
78 | else if (!strcmp("ipv6", string)) | |
79 | return AF_INET6; | |
80 | else if (!strcmp("ethernet", string)) | |
81 | return AF_ETHERNET; | |
b03b8898 DS |
82 | else if (!strcmp("evpn", string)) |
83 | return AF_EVPN; | |
d62a17ae | 84 | return -1; |
f3ccedaa CF |
85 | } |
86 | ||
db2fde34 PZ |
87 | const char *family2str(int family) |
88 | { | |
89 | switch (family) { | |
90 | case AF_INET: | |
91 | return "IPv4"; | |
92 | case AF_INET6: | |
93 | return "IPv6"; | |
94 | case AF_ETHERNET: | |
95 | return "Ethernet"; | |
96 | case AF_EVPN: | |
97 | return "Evpn"; | |
98 | } | |
99 | return "?"; | |
100 | } | |
101 | ||
214d8a60 | 102 | /* Address Family Identifier to Address Family converter. */ |
d62a17ae | 103 | int afi2family(afi_t afi) |
718e3744 | 104 | { |
d62a17ae | 105 | if (afi == AFI_IP) |
106 | return AF_INET; | |
107 | else if (afi == AFI_IP6) | |
108 | return AF_INET6; | |
109 | else if (afi == AFI_L2VPN) | |
110 | return AF_ETHERNET; | |
b03b8898 | 111 | /* NOTE: EVPN code should NOT use this interface. */ |
d62a17ae | 112 | return 0; |
718e3744 | 113 | } |
114 | ||
d62a17ae | 115 | afi_t family2afi(int family) |
718e3744 | 116 | { |
d62a17ae | 117 | if (family == AF_INET) |
118 | return AFI_IP; | |
119 | else if (family == AF_INET6) | |
120 | return AFI_IP6; | |
b03b8898 | 121 | else if (family == AF_ETHERNET || family == AF_EVPN) |
d62a17ae | 122 | return AFI_L2VPN; |
123 | return 0; | |
718e3744 | 124 | } |
125 | ||
d62a17ae | 126 | const char *afi2str(afi_t afi) |
32ac65d9 | 127 | { |
d62a17ae | 128 | switch (afi) { |
129 | case AFI_IP: | |
130 | return "IPv4"; | |
131 | case AFI_IP6: | |
132 | return "IPv6"; | |
133 | case AFI_L2VPN: | |
134 | return "l2vpn"; | |
135 | case AFI_MAX: | |
bde30e78 | 136 | case AFI_UNSPEC: |
d62a17ae | 137 | return "bad-value"; |
d62a17ae | 138 | } |
bde30e78 DS |
139 | |
140 | assert(!"Reached end of function we should never reach"); | |
32ac65d9 LB |
141 | } |
142 | ||
d62a17ae | 143 | const char *safi2str(safi_t safi) |
1ec23d90 | 144 | { |
d62a17ae | 145 | switch (safi) { |
146 | case SAFI_UNICAST: | |
147 | return "unicast"; | |
148 | case SAFI_MULTICAST: | |
149 | return "multicast"; | |
150 | case SAFI_MPLS_VPN: | |
151 | return "vpn"; | |
152 | case SAFI_ENCAP: | |
153 | return "encap"; | |
154 | case SAFI_EVPN: | |
155 | return "evpn"; | |
156 | case SAFI_LABELED_UNICAST: | |
157 | return "labeled-unicast"; | |
7c40bf39 | 158 | case SAFI_FLOWSPEC: |
159 | return "flowspec"; | |
bde30e78 DS |
160 | case SAFI_UNSPEC: |
161 | case SAFI_MAX: | |
5c525538 | 162 | return "unknown"; |
d62a17ae | 163 | } |
bde30e78 DS |
164 | |
165 | assert(!"Reached end of function we should never reach"); | |
1ec23d90 LB |
166 | } |
167 | ||
718e3744 | 168 | /* If n includes p prefix then return 1 else return 0. */ |
3125fa6d | 169 | int prefix_match(union prefixconstptr unet, union prefixconstptr upfx) |
718e3744 | 170 | { |
3125fa6d DL |
171 | const struct prefix *n = unet.p; |
172 | const struct prefix *p = upfx.p; | |
d62a17ae | 173 | int offset; |
174 | int shift; | |
d7c0a89a | 175 | const uint8_t *np, *pp; |
d62a17ae | 176 | |
177 | /* If n's prefix is longer than p's one return 0. */ | |
178 | if (n->prefixlen > p->prefixlen) | |
179 | return 0; | |
180 | ||
9a14899b PG |
181 | if (n->family == AF_FLOWSPEC) { |
182 | /* prefixlen is unused. look at fs prefix len */ | |
e4552d66 PG |
183 | if (n->u.prefix_flowspec.family != |
184 | p->u.prefix_flowspec.family) | |
185 | return 0; | |
186 | ||
9a14899b PG |
187 | if (n->u.prefix_flowspec.prefixlen > |
188 | p->u.prefix_flowspec.prefixlen) | |
189 | return 0; | |
190 | ||
191 | /* Set both prefix's head pointer. */ | |
192 | np = (const uint8_t *)&n->u.prefix_flowspec.ptr; | |
193 | pp = (const uint8_t *)&p->u.prefix_flowspec.ptr; | |
194 | ||
195 | offset = n->u.prefix_flowspec.prefixlen; | |
196 | ||
197 | while (offset--) | |
198 | if (np[offset] != pp[offset]) | |
199 | return 0; | |
200 | return 1; | |
201 | } | |
202 | ||
d62a17ae | 203 | /* Set both prefix's head pointer. */ |
f0ed6bea | 204 | np = n->u.val; |
205 | pp = p->u.val; | |
d62a17ae | 206 | |
207 | offset = n->prefixlen / PNBBY; | |
208 | shift = n->prefixlen % PNBBY; | |
209 | ||
210 | if (shift) | |
211 | if (maskbit[shift] & (np[offset] ^ pp[offset])) | |
212 | return 0; | |
213 | ||
214 | while (offset--) | |
215 | if (np[offset] != pp[offset]) | |
216 | return 0; | |
217 | return 1; | |
44c69747 LK |
218 | |
219 | } | |
220 | ||
221 | /* | |
222 | * n is a type5 evpn prefix. This function tries to see if there is an | |
223 | * ip-prefix within n which matches prefix p | |
224 | * If n includes p prefix then return 1 else return 0. | |
225 | */ | |
226 | int evpn_type5_prefix_match(const struct prefix *n, const struct prefix *p) | |
227 | { | |
228 | int offset; | |
229 | int shift; | |
230 | int prefixlen; | |
231 | const uint8_t *np, *pp; | |
232 | struct prefix_evpn *evp; | |
233 | ||
234 | if (n->family != AF_EVPN) | |
235 | return 0; | |
236 | ||
237 | evp = (struct prefix_evpn *)n; | |
238 | pp = p->u.val; | |
239 | ||
240 | if ((evp->prefix.route_type != 5) || | |
241 | (p->family == AF_INET6 && !is_evpn_prefix_ipaddr_v6(evp)) || | |
242 | (p->family == AF_INET && !is_evpn_prefix_ipaddr_v4(evp)) || | |
243 | (is_evpn_prefix_ipaddr_none(evp))) | |
244 | return 0; | |
245 | ||
246 | prefixlen = evp->prefix.prefix_addr.ip_prefix_length; | |
247 | np = &evp->prefix.prefix_addr.ip.ip.addr; | |
248 | ||
249 | /* If n's prefix is longer than p's one return 0. */ | |
250 | if (prefixlen > p->prefixlen) | |
251 | return 0; | |
252 | ||
253 | offset = prefixlen / PNBBY; | |
254 | shift = prefixlen % PNBBY; | |
255 | ||
256 | if (shift) | |
257 | if (maskbit[shift] & (np[offset] ^ pp[offset])) | |
258 | return 0; | |
259 | ||
260 | while (offset--) | |
261 | if (np[offset] != pp[offset]) | |
262 | return 0; | |
263 | return 1; | |
264 | ||
3bec29ac DS |
265 | } |
266 | ||
267 | /* If n includes p then return 1 else return 0. Prefix mask is not considered */ | |
3125fa6d DL |
268 | int prefix_match_network_statement(union prefixconstptr unet, |
269 | union prefixconstptr upfx) | |
3bec29ac | 270 | { |
3125fa6d DL |
271 | const struct prefix *n = unet.p; |
272 | const struct prefix *p = upfx.p; | |
d62a17ae | 273 | int offset; |
274 | int shift; | |
d7c0a89a | 275 | const uint8_t *np, *pp; |
3bec29ac | 276 | |
d62a17ae | 277 | /* Set both prefix's head pointer. */ |
f0ed6bea | 278 | np = n->u.val; |
279 | pp = p->u.val; | |
3bec29ac | 280 | |
d62a17ae | 281 | offset = n->prefixlen / PNBBY; |
282 | shift = n->prefixlen % PNBBY; | |
3bec29ac | 283 | |
d62a17ae | 284 | if (shift) |
285 | if (maskbit[shift] & (np[offset] ^ pp[offset])) | |
286 | return 0; | |
3bec29ac | 287 | |
d62a17ae | 288 | while (offset--) |
289 | if (np[offset] != pp[offset]) | |
290 | return 0; | |
291 | return 1; | |
718e3744 | 292 | } |
293 | ||
4937287f DL |
294 | #ifdef __clang_analyzer__ |
295 | #undef prefix_copy /* cf. prefix.h */ | |
296 | #endif | |
297 | ||
9c3a2171 | 298 | void prefix_copy(union prefixptr udest, union prefixconstptr usrc) |
718e3744 | 299 | { |
9c3a2171 DL |
300 | struct prefix *dest = udest.p; |
301 | const struct prefix *src = usrc.p; | |
302 | ||
d62a17ae | 303 | dest->family = src->family; |
304 | dest->prefixlen = src->prefixlen; | |
305 | ||
306 | if (src->family == AF_INET) | |
307 | dest->u.prefix4 = src->u.prefix4; | |
308 | else if (src->family == AF_INET6) | |
309 | dest->u.prefix6 = src->u.prefix6; | |
310 | else if (src->family == AF_ETHERNET) { | |
b03b8898 DS |
311 | memcpy(&dest->u.prefix_eth, &src->u.prefix_eth, |
312 | sizeof(struct ethaddr)); | |
313 | } else if (src->family == AF_EVPN) { | |
d62a17ae | 314 | memcpy(&dest->u.prefix_evpn, &src->u.prefix_evpn, |
315 | sizeof(struct evpn_addr)); | |
316 | } else if (src->family == AF_UNSPEC) { | |
317 | dest->u.lp.id = src->u.lp.id; | |
318 | dest->u.lp.adv_router = src->u.lp.adv_router; | |
9a14899b PG |
319 | } else if (src->family == AF_FLOWSPEC) { |
320 | void *temp; | |
321 | int len; | |
322 | ||
323 | len = src->u.prefix_flowspec.prefixlen; | |
324 | dest->u.prefix_flowspec.prefixlen = | |
325 | src->u.prefix_flowspec.prefixlen; | |
e4552d66 PG |
326 | dest->u.prefix_flowspec.family = |
327 | src->u.prefix_flowspec.family; | |
9a14899b PG |
328 | dest->family = src->family; |
329 | temp = XCALLOC(MTYPE_PREFIX_FLOWSPEC, len); | |
330 | dest->u.prefix_flowspec.ptr = (uintptr_t)temp; | |
331 | memcpy((void *)dest->u.prefix_flowspec.ptr, | |
332 | (void *)src->u.prefix_flowspec.ptr, len); | |
d62a17ae | 333 | } else { |
450971aa | 334 | flog_err(EC_LIB_DEVELOPMENT, |
1c50c1c0 QY |
335 | "prefix_copy(): Unknown address family %d", |
336 | src->family); | |
d62a17ae | 337 | assert(0); |
338 | } | |
718e3744 | 339 | } |
340 | ||
d62a17ae | 341 | /* |
9d24baaa | 342 | * Return 1 if the address/netmask contained in the prefix structure |
343 | * is the same, and else return 0. For this routine, 'same' requires | |
344 | * that not only the prefix length and the network part be the same, | |
345 | * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not | |
346 | * the same. Note that this routine has the same return value sense | |
347 | * as '==' (which is different from prefix_cmp). | |
348 | */ | |
9c3a2171 | 349 | int prefix_same(union prefixconstptr up1, union prefixconstptr up2) |
718e3744 | 350 | { |
9c3a2171 DL |
351 | const struct prefix *p1 = up1.p; |
352 | const struct prefix *p2 = up2.p; | |
353 | ||
d62a17ae | 354 | if ((p1 && !p2) || (!p1 && p2)) |
355 | return 0; | |
356 | ||
357 | if (!p1 && !p2) | |
358 | return 1; | |
359 | ||
360 | if (p1->family == p2->family && p1->prefixlen == p2->prefixlen) { | |
361 | if (p1->family == AF_INET) | |
19aad877 | 362 | if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4)) |
d62a17ae | 363 | return 1; |
364 | if (p1->family == AF_INET6) | |
365 | if (IPV6_ADDR_SAME(&p1->u.prefix6.s6_addr, | |
366 | &p2->u.prefix6.s6_addr)) | |
367 | return 1; | |
368 | if (p1->family == AF_ETHERNET) | |
b03b8898 DS |
369 | if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth, |
370 | sizeof(struct ethaddr))) | |
371 | return 1; | |
372 | if (p1->family == AF_EVPN) | |
d62a17ae | 373 | if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn, |
374 | sizeof(struct evpn_addr))) | |
375 | return 1; | |
9a14899b | 376 | if (p1->family == AF_FLOWSPEC) { |
e4552d66 PG |
377 | if (p1->u.prefix_flowspec.family != |
378 | p2->u.prefix_flowspec.family) | |
379 | return 0; | |
9a14899b PG |
380 | if (p1->u.prefix_flowspec.prefixlen != |
381 | p2->u.prefix_flowspec.prefixlen) | |
382 | return 0; | |
383 | if (!memcmp(&p1->u.prefix_flowspec.ptr, | |
384 | &p2->u.prefix_flowspec.ptr, | |
385 | p2->u.prefix_flowspec.prefixlen)) | |
386 | return 1; | |
387 | } | |
d62a17ae | 388 | } |
389 | return 0; | |
718e3744 | 390 | } |
391 | ||
9d24baaa | 392 | /* |
1315d74d DL |
393 | * Return -1/0/1 comparing the prefixes in a way that gives a full/linear |
394 | * order. | |
395 | * | |
396 | * Network prefixes are considered the same if the prefix lengths are equal | |
397 | * and the network parts are the same. Host bits (which are considered masked | |
9d24baaa | 398 | * by the prefix length) are not significant. Thus, 10.0.0.1/8 and |
399 | * 10.0.0.2/8 are considered equivalent by this routine. Note that | |
400 | * this routine has the same return sense as strcmp (which is different | |
401 | * from prefix_same). | |
402 | */ | |
9c3a2171 | 403 | int prefix_cmp(union prefixconstptr up1, union prefixconstptr up2) |
718e3744 | 404 | { |
9c3a2171 DL |
405 | const struct prefix *p1 = up1.p; |
406 | const struct prefix *p2 = up2.p; | |
d62a17ae | 407 | int offset; |
408 | int shift; | |
1315d74d | 409 | int i; |
718e3744 | 410 | |
d62a17ae | 411 | /* Set both prefix's head pointer. */ |
9a14899b PG |
412 | const uint8_t *pp1; |
413 | const uint8_t *pp2; | |
718e3744 | 414 | |
9a14899b | 415 | if (p1->family != p2->family) |
1315d74d | 416 | return numcmp(p1->family, p2->family); |
9a14899b PG |
417 | if (p1->family == AF_FLOWSPEC) { |
418 | pp1 = (const uint8_t *)p1->u.prefix_flowspec.ptr; | |
419 | pp2 = (const uint8_t *)p2->u.prefix_flowspec.ptr; | |
718e3744 | 420 | |
e4552d66 PG |
421 | if (p1->u.prefix_flowspec.family != |
422 | p2->u.prefix_flowspec.family) | |
423 | return 1; | |
424 | ||
9a14899b PG |
425 | if (p1->u.prefix_flowspec.prefixlen != |
426 | p2->u.prefix_flowspec.prefixlen) | |
1315d74d DL |
427 | return numcmp(p1->u.prefix_flowspec.prefixlen, |
428 | p2->u.prefix_flowspec.prefixlen); | |
9a14899b PG |
429 | |
430 | offset = p1->u.prefix_flowspec.prefixlen; | |
431 | while (offset--) | |
432 | if (pp1[offset] != pp2[offset]) | |
1315d74d | 433 | return numcmp(pp1[offset], pp2[offset]); |
9a14899b PG |
434 | return 0; |
435 | } | |
f0ed6bea | 436 | pp1 = p1->u.val; |
437 | pp2 = p2->u.val; | |
9a14899b PG |
438 | |
439 | if (p1->prefixlen != p2->prefixlen) | |
1315d74d | 440 | return numcmp(p1->prefixlen, p2->prefixlen); |
d62a17ae | 441 | offset = p1->prefixlen / PNBBY; |
442 | shift = p1->prefixlen % PNBBY; | |
718e3744 | 443 | |
1315d74d DL |
444 | i = memcmp(pp1, pp2, offset); |
445 | if (i) | |
446 | return i; | |
718e3744 | 447 | |
dd5bab0c DS |
448 | /* |
449 | * At this point offset was the same, if we have shift | |
450 | * that means we still have data to compare, if shift is | |
451 | * 0 then we are at the end of the data structure | |
452 | * and should just return, as that we will be accessing | |
453 | * memory beyond the end of the party zone | |
454 | */ | |
455 | if (shift) | |
456 | return numcmp(pp1[offset] & maskbit[shift], | |
457 | pp2[offset] & maskbit[shift]); | |
458 | ||
459 | return 0; | |
718e3744 | 460 | } |
461 | ||
17e52061 DL |
462 | /* |
463 | * Count the number of common bits in 2 prefixes. The prefix length is | |
464 | * ignored for this function; the whole prefix is compared. If the prefix | |
465 | * address families don't match, return -1; otherwise the return value is | |
466 | * in range 0 ... maximum prefix length for the address family. | |
467 | */ | |
3125fa6d | 468 | int prefix_common_bits(union prefixconstptr ua, union prefixconstptr ub) |
17e52061 | 469 | { |
3125fa6d DL |
470 | const struct prefix *p1 = ua.p; |
471 | const struct prefix *p2 = ub.p; | |
d62a17ae | 472 | int pos, bit; |
473 | int length = 0; | |
d7c0a89a | 474 | uint8_t xor ; |
d62a17ae | 475 | |
476 | /* Set both prefix's head pointer. */ | |
f0ed6bea | 477 | const uint8_t *pp1 = p1->u.val; |
478 | const uint8_t *pp2 = p2->u.val; | |
d62a17ae | 479 | |
480 | if (p1->family == AF_INET) | |
481 | length = IPV4_MAX_BYTELEN; | |
482 | if (p1->family == AF_INET6) | |
483 | length = IPV6_MAX_BYTELEN; | |
484 | if (p1->family == AF_ETHERNET) | |
b03b8898 DS |
485 | length = ETH_ALEN; |
486 | if (p1->family == AF_EVPN) | |
d62a17ae | 487 | length = 8 * sizeof(struct evpn_addr); |
488 | ||
489 | if (p1->family != p2->family || !length) | |
490 | return -1; | |
491 | ||
492 | for (pos = 0; pos < length; pos++) | |
493 | if (pp1[pos] != pp2[pos]) | |
494 | break; | |
495 | if (pos == length) | |
496 | return pos * 8; | |
497 | ||
498 | xor = pp1[pos] ^ pp2[pos]; | |
499 | for (bit = 0; bit < 8; bit++) | |
500 | if (xor&(1 << (7 - bit))) | |
501 | break; | |
502 | ||
503 | return pos * 8 + bit; | |
17e52061 DL |
504 | } |
505 | ||
718e3744 | 506 | /* Return prefix family type string. */ |
3125fa6d | 507 | const char *prefix_family_str(union prefixconstptr pu) |
718e3744 | 508 | { |
3125fa6d DL |
509 | const struct prefix *p = pu.p; |
510 | ||
d62a17ae | 511 | if (p->family == AF_INET) |
512 | return "inet"; | |
513 | if (p->family == AF_INET6) | |
514 | return "inet6"; | |
515 | if (p->family == AF_ETHERNET) | |
516 | return "ether"; | |
b03b8898 DS |
517 | if (p->family == AF_EVPN) |
518 | return "evpn"; | |
d62a17ae | 519 | return "unspec"; |
718e3744 | 520 | } |
521 | ||
522 | /* Allocate new prefix_ipv4 structure. */ | |
4d762f26 | 523 | struct prefix_ipv4 *prefix_ipv4_new(void) |
718e3744 | 524 | { |
d62a17ae | 525 | struct prefix_ipv4 *p; |
526 | ||
527 | /* Call prefix_new to allocate a full-size struct prefix to avoid | |
528 | problems | |
529 | where the struct prefix_ipv4 is cast to struct prefix and unallocated | |
530 | bytes were being referenced (e.g. in structure assignments). */ | |
531 | p = (struct prefix_ipv4 *)prefix_new(); | |
532 | p->family = AF_INET; | |
533 | return p; | |
718e3744 | 534 | } |
535 | ||
536 | /* Free prefix_ipv4 structure. */ | |
63265b5c | 537 | void prefix_ipv4_free(struct prefix_ipv4 **p) |
718e3744 | 538 | { |
63265b5c | 539 | prefix_free((struct prefix **)p); |
718e3744 | 540 | } |
541 | ||
3923b6e3 | 542 | /* If given string is valid return 1 else return 0 */ |
d62a17ae | 543 | int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p) |
718e3744 | 544 | { |
d62a17ae | 545 | int ret; |
546 | int plen; | |
547 | char *pnt; | |
548 | char *cp; | |
549 | ||
550 | /* Find slash inside string. */ | |
551 | pnt = strchr(str, '/'); | |
552 | ||
553 | /* String doesn't contail slash. */ | |
554 | if (pnt == NULL) { | |
555 | /* Convert string to prefix. */ | |
8d920049 | 556 | ret = inet_pton(AF_INET, str, &p->prefix); |
d62a17ae | 557 | if (ret == 0) |
558 | return 0; | |
559 | ||
560 | /* If address doesn't contain slash we assume it host address. | |
561 | */ | |
562 | p->family = AF_INET; | |
563 | p->prefixlen = IPV4_MAX_BITLEN; | |
564 | ||
565 | return ret; | |
566 | } else { | |
567 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 568 | memcpy(cp, str, pnt - str); |
d62a17ae | 569 | *(cp + (pnt - str)) = '\0'; |
ef231ac7 | 570 | ret = inet_pton(AF_INET, cp, &p->prefix); |
d62a17ae | 571 | XFREE(MTYPE_TMP, cp); |
ef231ac7 RW |
572 | if (ret == 0) |
573 | return 0; | |
d62a17ae | 574 | |
575 | /* Get prefix length. */ | |
d7c0a89a | 576 | plen = (uint8_t)atoi(++pnt); |
936fbaef | 577 | if (plen > IPV4_MAX_BITLEN) |
d62a17ae | 578 | return 0; |
579 | ||
580 | p->family = AF_INET; | |
581 | p->prefixlen = plen; | |
582 | } | |
718e3744 | 583 | |
d62a17ae | 584 | return ret; |
718e3744 | 585 | } |
586 | ||
32ac65d9 | 587 | /* When string format is invalid return 0. */ |
d62a17ae | 588 | int str2prefix_eth(const char *str, struct prefix_eth *p) |
32ac65d9 | 589 | { |
d62a17ae | 590 | int ret = 0; |
591 | int plen = 48; | |
592 | char *pnt; | |
593 | char *cp = NULL; | |
594 | const char *str_addr = str; | |
595 | unsigned int a[6]; | |
596 | int i; | |
0f6476cc | 597 | bool slash = false; |
d62a17ae | 598 | |
3b0f6068 DL |
599 | if (!strcmp(str, "any")) { |
600 | memset(p, 0, sizeof(*p)); | |
601 | p->family = AF_ETHERNET; | |
602 | return 1; | |
603 | } | |
604 | ||
d62a17ae | 605 | /* Find slash inside string. */ |
606 | pnt = strchr(str, '/'); | |
607 | ||
608 | if (pnt) { | |
609 | /* Get prefix length. */ | |
d7c0a89a | 610 | plen = (uint8_t)atoi(++pnt); |
d62a17ae | 611 | if (plen > 48) { |
612 | ret = 0; | |
613 | goto done; | |
614 | } | |
615 | ||
616 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 617 | memcpy(cp, str, pnt - str); |
d62a17ae | 618 | *(cp + (pnt - str)) = '\0'; |
619 | ||
620 | str_addr = cp; | |
0f6476cc | 621 | slash = true; |
32ac65d9 LB |
622 | } |
623 | ||
d62a17ae | 624 | /* Convert string to prefix. */ |
625 | if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, | |
626 | a + 3, a + 4, a + 5) | |
627 | != 6) { | |
628 | ret = 0; | |
629 | goto done; | |
630 | } | |
631 | for (i = 0; i < 6; ++i) { | |
632 | p->eth_addr.octet[i] = a[i] & 0xff; | |
633 | } | |
634 | p->prefixlen = plen; | |
635 | p->family = AF_ETHERNET; | |
0f6476cc DS |
636 | |
637 | /* | |
638 | * special case to allow old configurations to work | |
639 | * Since all zero's is implicitly meant to allow | |
640 | * a comparison to zero, let's assume | |
641 | */ | |
642 | if (!slash && is_zero_mac(&(p->eth_addr))) | |
996c9314 | 643 | p->prefixlen = 0; |
0f6476cc | 644 | |
d62a17ae | 645 | ret = 1; |
32ac65d9 LB |
646 | |
647 | done: | |
0a22ddfb | 648 | XFREE(MTYPE_TMP, cp); |
32ac65d9 | 649 | |
d62a17ae | 650 | return ret; |
32ac65d9 LB |
651 | } |
652 | ||
051954f5 | 653 | /* Convert masklen into IP address's netmask (network byte order). */ |
d62a17ae | 654 | void masklen2ip(const int masklen, struct in_addr *netmask) |
718e3744 | 655 | { |
d62a17ae | 656 | assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN); |
e96b3121 | 657 | |
d62a17ae | 658 | /* left shift is only defined for less than the size of the type. |
659 | * we unconditionally use long long in case the target platform | |
660 | * has defined behaviour for << 32 (or has a 64-bit left shift) */ | |
e96b3121 | 661 | |
d62a17ae | 662 | if (sizeof(unsigned long long) > 4) |
663 | netmask->s_addr = htonl(0xffffffffULL << (32 - masklen)); | |
664 | else | |
665 | netmask->s_addr = | |
666 | htonl(masklen ? 0xffffffffU << (32 - masklen) : 0); | |
718e3744 | 667 | } |
668 | ||
669 | /* Convert IP address's netmask into integer. We assume netmask is | |
f93eee44 | 670 | * sequential one. Argument netmask should be network byte order. */ |
d7c0a89a | 671 | uint8_t ip_masklen(struct in_addr netmask) |
718e3744 | 672 | { |
d62a17ae | 673 | uint32_t tmp = ~ntohl(netmask.s_addr); |
f93eee44 | 674 | |
61be6e94 QY |
675 | /* |
676 | * clz: count leading zeroes. sadly, the behaviour of this builtin is | |
677 | * undefined for a 0 argument, even though most CPUs give 32 | |
678 | */ | |
f93eee44 | 679 | return tmp ? __builtin_clz(tmp) : 32; |
718e3744 | 680 | } |
681 | ||
caff7905 | 682 | /* Apply mask to IPv4 prefix (network byte order). */ |
d62a17ae | 683 | void apply_mask_ipv4(struct prefix_ipv4 *p) |
718e3744 | 684 | { |
d62a17ae | 685 | struct in_addr mask; |
686 | masklen2ip(p->prefixlen, &mask); | |
687 | p->prefix.s_addr &= mask.s_addr; | |
718e3744 | 688 | } |
689 | ||
690 | /* If prefix is 0.0.0.0/0 then return 1 else return 0. */ | |
d62a17ae | 691 | int prefix_ipv4_any(const struct prefix_ipv4 *p) |
718e3744 | 692 | { |
975a328e | 693 | return (p->prefix.s_addr == INADDR_ANY && p->prefixlen == 0); |
718e3744 | 694 | } |
6b0655a2 | 695 | |
718e3744 | 696 | /* Allocate a new ip version 6 route */ |
d62a17ae | 697 | struct prefix_ipv6 *prefix_ipv6_new(void) |
718e3744 | 698 | { |
d62a17ae | 699 | struct prefix_ipv6 *p; |
718e3744 | 700 | |
d62a17ae | 701 | /* Allocate a full-size struct prefix to avoid problems with structure |
702 | size mismatches. */ | |
703 | p = (struct prefix_ipv6 *)prefix_new(); | |
704 | p->family = AF_INET6; | |
705 | return p; | |
718e3744 | 706 | } |
707 | ||
708 | /* Free prefix for IPv6. */ | |
63265b5c | 709 | void prefix_ipv6_free(struct prefix_ipv6 **p) |
718e3744 | 710 | { |
63265b5c | 711 | prefix_free((struct prefix **)p); |
718e3744 | 712 | } |
713 | ||
3923b6e3 | 714 | /* If given string is valid return 1 else return 0 */ |
d62a17ae | 715 | int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p) |
718e3744 | 716 | { |
d62a17ae | 717 | char *pnt; |
718 | char *cp; | |
719 | int ret; | |
720 | ||
721 | pnt = strchr(str, '/'); | |
722 | ||
723 | /* If string doesn't contain `/' treat it as host route. */ | |
724 | if (pnt == NULL) { | |
725 | ret = inet_pton(AF_INET6, str, &p->prefix); | |
726 | if (ret == 0) | |
727 | return 0; | |
728 | p->prefixlen = IPV6_MAX_BITLEN; | |
729 | } else { | |
730 | int plen; | |
731 | ||
732 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 733 | memcpy(cp, str, pnt - str); |
d62a17ae | 734 | *(cp + (pnt - str)) = '\0'; |
735 | ret = inet_pton(AF_INET6, cp, &p->prefix); | |
736 | XFREE(MTYPE_TMP, cp); | |
737 | if (ret == 0) | |
738 | return 0; | |
d7c0a89a | 739 | plen = (uint8_t)atoi(++pnt); |
d62a17ae | 740 | if (plen > IPV6_MAX_BITLEN) |
741 | return 0; | |
742 | p->prefixlen = plen; | |
743 | } | |
744 | p->family = AF_INET6; | |
718e3744 | 745 | |
d62a17ae | 746 | return ret; |
718e3744 | 747 | } |
748 | ||
b04c699e | 749 | /* Convert struct in6_addr netmask into integer. |
d7c0a89a | 750 | * FIXME return uint8_t as ip_maskleni() does. */ |
d62a17ae | 751 | int ip6_masklen(struct in6_addr netmask) |
718e3744 | 752 | { |
25d86233 DL |
753 | if (netmask.s6_addr32[0] != 0xffffffffU) |
754 | return __builtin_clz(~ntohl(netmask.s6_addr32[0])); | |
755 | if (netmask.s6_addr32[1] != 0xffffffffU) | |
756 | return __builtin_clz(~ntohl(netmask.s6_addr32[1])) + 32; | |
757 | if (netmask.s6_addr32[2] != 0xffffffffU) | |
758 | return __builtin_clz(~ntohl(netmask.s6_addr32[2])) + 64; | |
759 | if (netmask.s6_addr32[3] != 0xffffffffU) | |
760 | return __builtin_clz(~ntohl(netmask.s6_addr32[3])) + 96; | |
761 | /* note __builtin_clz(0) is undefined */ | |
762 | return 128; | |
718e3744 | 763 | } |
764 | ||
d62a17ae | 765 | void masklen2ip6(const int masklen, struct in6_addr *netmask) |
718e3744 | 766 | { |
d62a17ae | 767 | assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN); |
25d86233 DL |
768 | |
769 | if (masklen == 0) { | |
770 | /* note << 32 is undefined */ | |
771 | memset(netmask, 0, sizeof(*netmask)); | |
772 | } else if (masklen <= 32) { | |
773 | netmask->s6_addr32[0] = htonl(0xffffffffU << (32 - masklen)); | |
774 | netmask->s6_addr32[1] = 0; | |
775 | netmask->s6_addr32[2] = 0; | |
776 | netmask->s6_addr32[3] = 0; | |
777 | } else if (masklen <= 64) { | |
778 | netmask->s6_addr32[0] = 0xffffffffU; | |
779 | netmask->s6_addr32[1] = htonl(0xffffffffU << (64 - masklen)); | |
780 | netmask->s6_addr32[2] = 0; | |
781 | netmask->s6_addr32[3] = 0; | |
782 | } else if (masklen <= 96) { | |
783 | netmask->s6_addr32[0] = 0xffffffffU; | |
784 | netmask->s6_addr32[1] = 0xffffffffU; | |
785 | netmask->s6_addr32[2] = htonl(0xffffffffU << (96 - masklen)); | |
786 | netmask->s6_addr32[3] = 0; | |
787 | } else { | |
788 | netmask->s6_addr32[0] = 0xffffffffU; | |
789 | netmask->s6_addr32[1] = 0xffffffffU; | |
790 | netmask->s6_addr32[2] = 0xffffffffU; | |
791 | netmask->s6_addr32[3] = htonl(0xffffffffU << (128 - masklen)); | |
792 | } | |
718e3744 | 793 | } |
794 | ||
d62a17ae | 795 | void apply_mask_ipv6(struct prefix_ipv6 *p) |
718e3744 | 796 | { |
d7c0a89a | 797 | uint8_t *pnt; |
d62a17ae | 798 | int index; |
799 | int offset; | |
8c7f49d2 | 800 | |
d62a17ae | 801 | index = p->prefixlen / 8; |
8c7f49d2 | 802 | |
d62a17ae | 803 | if (index < 16) { |
d7c0a89a | 804 | pnt = (uint8_t *)&p->prefix; |
d62a17ae | 805 | offset = p->prefixlen % 8; |
8c7f49d2 | 806 | |
d62a17ae | 807 | pnt[index] &= maskbit[offset]; |
808 | index++; | |
8c7f49d2 | 809 | |
d62a17ae | 810 | while (index < 16) |
811 | pnt[index++] = 0; | |
812 | } | |
718e3744 | 813 | } |
814 | ||
3125fa6d | 815 | void apply_mask(union prefixptr pu) |
718e3744 | 816 | { |
3125fa6d DL |
817 | struct prefix *p = pu.p; |
818 | ||
d62a17ae | 819 | switch (p->family) { |
820 | case AF_INET: | |
3125fa6d | 821 | apply_mask_ipv4(pu.p4); |
d62a17ae | 822 | break; |
823 | case AF_INET6: | |
3125fa6d | 824 | apply_mask_ipv6(pu.p6); |
d62a17ae | 825 | break; |
826 | default: | |
827 | break; | |
828 | } | |
829 | return; | |
718e3744 | 830 | } |
831 | ||
b04c699e | 832 | /* Utility function of convert between struct prefix <=> union sockunion. */ |
d62a17ae | 833 | struct prefix *sockunion2hostprefix(const union sockunion *su, |
834 | struct prefix *prefix) | |
718e3744 | 835 | { |
d62a17ae | 836 | if (su->sa.sa_family == AF_INET) { |
837 | struct prefix_ipv4 *p; | |
838 | ||
839 | p = prefix ? (struct prefix_ipv4 *)prefix : prefix_ipv4_new(); | |
840 | p->family = AF_INET; | |
841 | p->prefix = su->sin.sin_addr; | |
842 | p->prefixlen = IPV4_MAX_BITLEN; | |
843 | return (struct prefix *)p; | |
844 | } | |
845 | if (su->sa.sa_family == AF_INET6) { | |
846 | struct prefix_ipv6 *p; | |
847 | ||
848 | p = prefix ? (struct prefix_ipv6 *)prefix : prefix_ipv6_new(); | |
849 | p->family = AF_INET6; | |
850 | p->prefixlen = IPV6_MAX_BITLEN; | |
851 | memcpy(&p->prefix, &su->sin6.sin6_addr, | |
852 | sizeof(struct in6_addr)); | |
853 | return (struct prefix *)p; | |
854 | } | |
855 | return NULL; | |
718e3744 | 856 | } |
857 | ||
d62a17ae | 858 | void prefix2sockunion(const struct prefix *p, union sockunion *su) |
17e52061 | 859 | { |
d62a17ae | 860 | memset(su, 0, sizeof(*su)); |
861 | ||
862 | su->sa.sa_family = p->family; | |
863 | if (p->family == AF_INET) | |
864 | su->sin.sin_addr = p->u.prefix4; | |
865 | if (p->family == AF_INET6) | |
866 | memcpy(&su->sin6.sin6_addr, &p->u.prefix6, | |
867 | sizeof(struct in6_addr)); | |
17e52061 DL |
868 | } |
869 | ||
3125fa6d | 870 | int prefix_blen(union prefixconstptr pu) |
718e3744 | 871 | { |
3125fa6d DL |
872 | const struct prefix *p = pu.p; |
873 | ||
d62a17ae | 874 | switch (p->family) { |
875 | case AF_INET: | |
876 | return IPV4_MAX_BYTELEN; | |
d62a17ae | 877 | case AF_INET6: |
878 | return IPV6_MAX_BYTELEN; | |
d62a17ae | 879 | case AF_ETHERNET: |
7628d862 | 880 | return ETH_ALEN; |
d62a17ae | 881 | } |
882 | return 0; | |
718e3744 | 883 | } |
884 | ||
885 | /* Generic function for conversion string to struct prefix. */ | |
d62a17ae | 886 | int str2prefix(const char *str, struct prefix *p) |
718e3744 | 887 | { |
d62a17ae | 888 | int ret; |
718e3744 | 889 | |
c37a11ad | 890 | if (!str || !p) |
891 | return 0; | |
892 | ||
d62a17ae | 893 | /* First we try to convert string to struct prefix_ipv4. */ |
894 | ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p); | |
895 | if (ret) | |
896 | return ret; | |
718e3744 | 897 | |
d62a17ae | 898 | /* Next we try to convert string to struct prefix_ipv6. */ |
899 | ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p); | |
900 | if (ret) | |
901 | return ret; | |
718e3744 | 902 | |
d62a17ae | 903 | /* Next we try to convert string to struct prefix_eth. */ |
904 | ret = str2prefix_eth(str, (struct prefix_eth *)p); | |
905 | if (ret) | |
906 | return ret; | |
32ac65d9 | 907 | |
d62a17ae | 908 | return 0; |
718e3744 | 909 | } |
910 | ||
3714a385 | 911 | static const char *prefixevpn_ead2str(const struct prefix_evpn *p, char *str, |
912 | int size) | |
913 | { | |
f137734b | 914 | uint8_t family; |
8d78eeb5 | 915 | char buf[ESI_STR_LEN]; |
f137734b | 916 | char buf1[INET6_ADDRSTRLEN]; |
8d78eeb5 | 917 | |
f137734b | 918 | family = IS_IPADDR_V4(&p->prefix.ead_addr.ip) ? AF_INET : AF_INET6; |
7b0db0e4 AK |
919 | snprintf(str, size, "[%d]:[%u]:[%s]:[%d]:[%s]:[%u]", |
920 | p->prefix.route_type, p->prefix.ead_addr.eth_tag, | |
8d78eeb5 | 921 | esi_to_str(&p->prefix.ead_addr.esi, buf, sizeof(buf)), |
f137734b PR |
922 | (family == AF_INET) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN, |
923 | inet_ntop(family, &p->prefix.ead_addr.ip.ipaddr_v4, buf1, | |
7b0db0e4 AK |
924 | sizeof(buf1)), |
925 | p->prefix.ead_addr.frag_id); | |
3714a385 | 926 | return str; |
927 | } | |
928 | ||
929 | static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str, | |
930 | int size) | |
86f1ef44 | 931 | { |
d7c0a89a | 932 | uint8_t family; |
8d78eeb5 PR |
933 | char buf1[ETHER_ADDR_STRLEN]; |
934 | char buf2[PREFIX2STR_BUFFER]; | |
d62a17ae | 935 | |
3714a385 | 936 | if (is_evpn_prefix_ipaddr_none(p)) |
8d78eeb5 PR |
937 | snprintf(str, size, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type, |
938 | p->prefix.macip_addr.eth_tag, 8 * ETH_ALEN, | |
939 | prefix_mac2str(&p->prefix.macip_addr.mac, buf1, | |
940 | sizeof(buf1))); | |
3714a385 | 941 | else { |
8d78eeb5 PR |
942 | family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6; |
943 | snprintf(str, size, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]", | |
944 | p->prefix.route_type, p->prefix.macip_addr.eth_tag, | |
945 | 8 * ETH_ALEN, | |
946 | prefix_mac2str(&p->prefix.macip_addr.mac, buf1, | |
947 | sizeof(buf1)), | |
948 | family == AF_INET ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN, | |
949 | inet_ntop(family, &p->prefix.macip_addr.ip.ip.addr, | |
950 | buf2, PREFIX2STR_BUFFER)); | |
d62a17ae | 951 | } |
3714a385 | 952 | return str; |
953 | } | |
954 | ||
955 | static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str, | |
956 | int size) | |
957 | { | |
f137734b PR |
958 | uint8_t family; |
959 | char buf[INET6_ADDRSTRLEN]; | |
960 | ||
961 | family = IS_IPADDR_V4(&p->prefix.imet_addr.ip) ? AF_INET : AF_INET6; | |
8d78eeb5 PR |
962 | snprintf(str, size, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type, |
963 | p->prefix.imet_addr.eth_tag, | |
f137734b PR |
964 | (family == AF_INET) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN, |
965 | inet_ntop(family, &p->prefix.imet_addr.ip.ipaddr_v4, buf, | |
966 | sizeof(buf))); | |
d62a17ae | 967 | |
3714a385 | 968 | return str; |
969 | } | |
970 | ||
971 | static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str, | |
972 | int size) | |
973 | { | |
f137734b | 974 | uint8_t family; |
50f74cf1 | 975 | char buf[ESI_STR_LEN]; |
f137734b | 976 | char buf1[INET6_ADDRSTRLEN]; |
50f74cf1 | 977 | |
f137734b | 978 | family = IS_IPADDR_V4(&p->prefix.es_addr.ip) ? AF_INET : AF_INET6; |
8d78eeb5 | 979 | snprintf(str, size, "[%d]:[%s]:[%d]:[%s]", p->prefix.route_type, |
50f74cf1 | 980 | esi_to_str(&p->prefix.es_addr.esi, buf, sizeof(buf)), |
f137734b PR |
981 | (family == AF_INET) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN, |
982 | inet_ntop(family, &p->prefix.es_addr.ip.ipaddr_v4, buf1, | |
983 | sizeof(buf1))); | |
8d78eeb5 | 984 | |
3714a385 | 985 | return str; |
986 | } | |
987 | ||
988 | static const char *prefixevpn_prefix2str(const struct prefix_evpn *p, char *str, | |
989 | int size) | |
990 | { | |
f137734b PR |
991 | uint8_t family; |
992 | char buf[INET6_ADDRSTRLEN]; | |
993 | ||
994 | family = IS_IPADDR_V4(&p->prefix.prefix_addr.ip) ? AF_INET : AF_INET6; | |
8d78eeb5 | 995 | snprintf(str, size, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type, |
3714a385 | 996 | p->prefix.prefix_addr.eth_tag, |
3714a385 | 997 | p->prefix.prefix_addr.ip_prefix_length, |
f137734b PR |
998 | inet_ntop(family, &p->prefix.prefix_addr.ip.ipaddr_v4, buf, |
999 | sizeof(buf))); | |
3714a385 | 1000 | return str; |
1001 | } | |
1002 | ||
1003 | static const char *prefixevpn2str(const struct prefix_evpn *p, char *str, | |
1004 | int size) | |
1005 | { | |
1006 | switch (p->prefix.route_type) { | |
8d78eeb5 | 1007 | case BGP_EVPN_AD_ROUTE: |
3714a385 | 1008 | return prefixevpn_ead2str(p, str, size); |
8d78eeb5 | 1009 | case BGP_EVPN_MAC_IP_ROUTE: |
3714a385 | 1010 | return prefixevpn_macip2str(p, str, size); |
8d78eeb5 | 1011 | case BGP_EVPN_IMET_ROUTE: |
3714a385 | 1012 | return prefixevpn_imet2str(p, str, size); |
8d78eeb5 | 1013 | case BGP_EVPN_ES_ROUTE: |
3714a385 | 1014 | return prefixevpn_es2str(p, str, size); |
8d78eeb5 | 1015 | case BGP_EVPN_IP_PREFIX_ROUTE: |
3714a385 | 1016 | return prefixevpn_prefix2str(p, str, size); |
1017 | default: | |
1018 | snprintf(str, size, "Unsupported EVPN prefix"); | |
1019 | break; | |
1020 | } | |
d62a17ae | 1021 | return str; |
86f1ef44 | 1022 | } |
1023 | ||
d62a17ae | 1024 | const char *prefix2str(union prefixconstptr pu, char *str, int size) |
718e3744 | 1025 | { |
d62a17ae | 1026 | const struct prefix *p = pu.p; |
1027 | char buf[PREFIX2STR_BUFFER]; | |
ec466f65 QY |
1028 | int byte, tmp, a, b; |
1029 | bool z = false; | |
1030 | size_t l; | |
d62a17ae | 1031 | |
1032 | switch (p->family) { | |
1033 | case AF_INET: | |
1034 | case AF_INET6: | |
ec466f65 QY |
1035 | inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf)); |
1036 | l = strlen(buf); | |
1037 | buf[l++] = '/'; | |
1038 | byte = p->prefixlen; | |
0e2d7076 DA |
1039 | tmp = p->prefixlen - 100; |
1040 | if (tmp >= 0) { | |
ec466f65 QY |
1041 | buf[l++] = '1'; |
1042 | z = true; | |
1043 | byte = tmp; | |
1044 | } | |
1045 | b = byte % 10; | |
1046 | a = byte / 10; | |
1047 | if (a || z) | |
1048 | buf[l++] = '0' + a; | |
1049 | buf[l++] = '0' + b; | |
1050 | buf[l] = '\0'; | |
1051 | strlcpy(str, buf, size); | |
d62a17ae | 1052 | break; |
1053 | ||
1054 | case AF_ETHERNET: | |
b03b8898 DS |
1055 | snprintf(str, size, "%s/%d", |
1056 | prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)), | |
1057 | p->prefixlen); | |
1058 | break; | |
1059 | ||
1060 | case AF_EVPN: | |
3714a385 | 1061 | prefixevpn2str((const struct prefix_evpn *)p, str, size); |
d62a17ae | 1062 | break; |
1063 | ||
9a14899b | 1064 | case AF_FLOWSPEC: |
ec466f65 | 1065 | strlcpy(str, "FS prefix", size); |
9a14899b PG |
1066 | break; |
1067 | ||
d62a17ae | 1068 | default: |
ec466f65 | 1069 | strlcpy(str, "UNK prefix", size); |
d62a17ae | 1070 | break; |
1071 | } | |
1072 | ||
1073 | return str; | |
718e3744 | 1074 | } |
1075 | ||
543a2684 DL |
1076 | static ssize_t prefixhost2str(struct fbuf *fbuf, union prefixconstptr pu) |
1077 | { | |
1078 | const struct prefix *p = pu.p; | |
1079 | char buf[PREFIX2STR_BUFFER]; | |
1080 | ||
1081 | switch (p->family) { | |
1082 | case AF_INET: | |
1083 | case AF_INET6: | |
1084 | inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf)); | |
1085 | return bputs(fbuf, buf); | |
1086 | ||
1087 | case AF_ETHERNET: | |
1088 | prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)); | |
1089 | return bputs(fbuf, buf); | |
1090 | ||
1091 | default: | |
1092 | return bprintfrr(fbuf, "{prefix.af=%dPF}", p->family); | |
1093 | } | |
1094 | } | |
1095 | ||
c6b6b53b AK |
1096 | void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr, |
1097 | char *buf, int buf_size) | |
1098 | { | |
1099 | int save_errno = errno; | |
1100 | ||
1101 | if (addr.s_addr == INADDR_ANY) | |
9f73d2c9 | 1102 | strlcpy(buf, "*", buf_size); |
c6b6b53b AK |
1103 | else { |
1104 | if (!inet_ntop(AF_INET, &addr, buf, buf_size)) { | |
1105 | if (onfail) | |
1106 | snprintf(buf, buf_size, "%s", onfail); | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | errno = save_errno; | |
1111 | } | |
1112 | ||
1113 | const char *prefix_sg2str(const struct prefix_sg *sg, char *sg_str) | |
1114 | { | |
1115 | char src_str[INET_ADDRSTRLEN]; | |
1116 | char grp_str[INET_ADDRSTRLEN]; | |
1117 | ||
1118 | prefix_mcast_inet4_dump("<src?>", sg->src, src_str, sizeof(src_str)); | |
1119 | prefix_mcast_inet4_dump("<grp?>", sg->grp, grp_str, sizeof(grp_str)); | |
1120 | snprintf(sg_str, PREFIX_SG_STR_LEN, "(%s,%s)", src_str, grp_str); | |
1121 | ||
1122 | return sg_str; | |
1123 | } | |
1124 | ||
4d762f26 | 1125 | struct prefix *prefix_new(void) |
718e3744 | 1126 | { |
d62a17ae | 1127 | struct prefix *p; |
718e3744 | 1128 | |
0d6f7fd6 | 1129 | p = XCALLOC(MTYPE_PREFIX, sizeof(*p)); |
d62a17ae | 1130 | return p; |
718e3744 | 1131 | } |
1132 | ||
63265b5c DS |
1133 | void prefix_free_lists(void *arg) |
1134 | { | |
1135 | struct prefix *p = arg; | |
1136 | ||
1137 | prefix_free(&p); | |
1138 | } | |
1139 | ||
718e3744 | 1140 | /* Free prefix structure. */ |
63265b5c | 1141 | void prefix_free(struct prefix **p) |
718e3744 | 1142 | { |
63265b5c | 1143 | XFREE(MTYPE_PREFIX, *p); |
718e3744 | 1144 | } |
1145 | ||
718e3744 | 1146 | /* Utility function to convert ipv4 prefixes to Classful prefixes */ |
d62a17ae | 1147 | void apply_classful_mask_ipv4(struct prefix_ipv4 *p) |
718e3744 | 1148 | { |
1149 | ||
d7c0a89a | 1150 | uint32_t destination; |
d62a17ae | 1151 | |
1152 | destination = ntohl(p->prefix.s_addr); | |
1153 | ||
936fbaef | 1154 | if (p->prefixlen == IPV4_MAX_BITLEN) |
d62a17ae | 1155 | ; |
1156 | /* do nothing for host routes */ | |
1157 | else if (IN_CLASSC(destination)) { | |
1158 | p->prefixlen = 24; | |
1159 | apply_mask_ipv4(p); | |
1160 | } else if (IN_CLASSB(destination)) { | |
1161 | p->prefixlen = 16; | |
1162 | apply_mask_ipv4(p); | |
1163 | } else { | |
1164 | p->prefixlen = 8; | |
1165 | apply_mask_ipv4(p); | |
1166 | } | |
718e3744 | 1167 | } |
1168 | ||
d62a17ae | 1169 | in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen) |
3fb9cd6e | 1170 | { |
d62a17ae | 1171 | struct in_addr mask; |
1172 | ||
1173 | masklen2ip(masklen, &mask); | |
936fbaef DA |
1174 | return (masklen != IPV4_MAX_BITLEN - 1) |
1175 | ? | |
1176 | /* normal case */ | |
1177 | (hostaddr | ~mask.s_addr) | |
1178 | : | |
1179 | /* For prefix 31 return 255.255.255.255 (RFC3021) */ | |
1180 | htonl(0xFFFFFFFF); | |
3fb9cd6e | 1181 | } |
1182 | ||
d62a17ae | 1183 | /* Utility function to convert ipv4 netmask to prefixes |
718e3744 | 1184 | ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16" |
1185 | ex.) "1.0.0.0" NULL => "1.0.0.0/8" */ | |
d62a17ae | 1186 | int netmask_str2prefix_str(const char *net_str, const char *mask_str, |
7533cad7 | 1187 | char *prefix_str, size_t prefix_str_len) |
718e3744 | 1188 | { |
d62a17ae | 1189 | struct in_addr network; |
1190 | struct in_addr mask; | |
d7c0a89a QY |
1191 | uint8_t prefixlen; |
1192 | uint32_t destination; | |
d62a17ae | 1193 | int ret; |
1194 | ||
1195 | ret = inet_aton(net_str, &network); | |
1196 | if (!ret) | |
1197 | return 0; | |
1198 | ||
1199 | if (mask_str) { | |
1200 | ret = inet_aton(mask_str, &mask); | |
1201 | if (!ret) | |
1202 | return 0; | |
1203 | ||
1204 | prefixlen = ip_masklen(mask); | |
1205 | } else { | |
1206 | destination = ntohl(network.s_addr); | |
1207 | ||
975a328e | 1208 | if (network.s_addr == INADDR_ANY) |
d62a17ae | 1209 | prefixlen = 0; |
1210 | else if (IN_CLASSC(destination)) | |
1211 | prefixlen = 24; | |
1212 | else if (IN_CLASSB(destination)) | |
1213 | prefixlen = 16; | |
1214 | else if (IN_CLASSA(destination)) | |
1215 | prefixlen = 8; | |
1216 | else | |
1217 | return 0; | |
1218 | } | |
718e3744 | 1219 | |
7533cad7 | 1220 | snprintf(prefix_str, prefix_str_len, "%s/%d", net_str, prefixlen); |
718e3744 | 1221 | |
d62a17ae | 1222 | return 1; |
718e3744 | 1223 | } |
1224 | ||
c215ecaf | 1225 | /* converts to internal representation of mac address |
d62a17ae | 1226 | * returns 1 on success, 0 otherwise |
c215ecaf PG |
1227 | * format accepted: AA:BB:CC:DD:EE:FF |
1228 | * if mac parameter is null, then check only | |
1229 | */ | |
db42a173 | 1230 | int prefix_str2mac(const char *str, struct ethaddr *mac) |
c215ecaf | 1231 | { |
d62a17ae | 1232 | unsigned int a[6]; |
1233 | int i; | |
1234 | ||
1235 | if (!str) | |
1236 | return 0; | |
1237 | ||
1238 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3, | |
1239 | a + 4, a + 5) | |
1240 | != 6) { | |
1241 | /* error in incoming str length */ | |
1242 | return 0; | |
1243 | } | |
1244 | /* valid mac address */ | |
1245 | if (!mac) | |
1246 | return 1; | |
1247 | for (i = 0; i < 6; ++i) | |
1248 | mac->octet[i] = a[i] & 0xff; | |
1249 | return 1; | |
c215ecaf PG |
1250 | } |
1251 | ||
db42a173 | 1252 | char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size) |
c215ecaf | 1253 | { |
d62a17ae | 1254 | char *ptr; |
1255 | ||
1256 | if (!mac) | |
1257 | return NULL; | |
1258 | if (!buf) | |
9f5dc319 | 1259 | ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char)); |
d62a17ae | 1260 | else { |
1261 | assert(size >= ETHER_ADDR_STRLEN); | |
1262 | ptr = buf; | |
1263 | } | |
1264 | snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x", | |
1265 | (uint8_t)mac->octet[0], (uint8_t)mac->octet[1], | |
1266 | (uint8_t)mac->octet[2], (uint8_t)mac->octet[3], | |
1267 | (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]); | |
1268 | return ptr; | |
c215ecaf | 1269 | } |
7a7761d2 | 1270 | |
62b4b3b6 | 1271 | unsigned prefix_hash_key(const void *pp) |
7a7761d2 CF |
1272 | { |
1273 | struct prefix copy; | |
1274 | ||
9a14899b PG |
1275 | if (((struct prefix *)pp)->family == AF_FLOWSPEC) { |
1276 | uint32_t len; | |
1277 | void *temp; | |
1278 | ||
1279 | /* make sure *all* unused bits are zero, | |
1280 | * particularly including alignment / | |
1281 | * padding and unused prefix bytes. | |
1282 | */ | |
1283 | memset(©, 0, sizeof(copy)); | |
1284 | prefix_copy(©, (struct prefix *)pp); | |
1285 | len = jhash((void *)copy.u.prefix_flowspec.ptr, | |
1286 | copy.u.prefix_flowspec.prefixlen, | |
1287 | 0x55aa5a5a); | |
1288 | temp = (void *)copy.u.prefix_flowspec.ptr; | |
1289 | XFREE(MTYPE_PREFIX_FLOWSPEC, temp); | |
1290 | copy.u.prefix_flowspec.ptr = (uintptr_t)NULL; | |
1291 | return len; | |
1292 | } | |
7a7761d2 CF |
1293 | /* make sure *all* unused bits are zero, particularly including |
1294 | * alignment / | |
1295 | * padding and unused prefix bytes. */ | |
1296 | memset(©, 0, sizeof(copy)); | |
1297 | prefix_copy(©, (struct prefix *)pp); | |
996c9314 LB |
1298 | return jhash(©, |
1299 | offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen), | |
1300 | 0x55aa5a5a); | |
7a7761d2 | 1301 | } |
50f74cf1 | 1302 | |
1303 | /* converts to internal representation of esi | |
1304 | * returns 1 on success, 0 otherwise | |
1305 | * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa | |
1306 | * if esi parameter is null, then check only | |
1307 | */ | |
1308 | int str_to_esi(const char *str, esi_t *esi) | |
1309 | { | |
1310 | int i; | |
1311 | unsigned int a[ESI_BYTES]; | |
1312 | ||
1313 | if (!str) | |
1314 | return 0; | |
1315 | ||
1316 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x", | |
1317 | a + 0, a + 1, a + 2, a + 3, | |
1318 | a + 4, a + 5, a + 6, a + 7, | |
1319 | a + 8, a + 9) | |
1320 | != ESI_BYTES) { | |
1321 | /* error in incoming str length */ | |
1322 | return 0; | |
1323 | } | |
1324 | ||
1325 | /* valid ESI */ | |
1326 | if (!esi) | |
1327 | return 1; | |
1328 | for (i = 0; i < ESI_BYTES; ++i) | |
1329 | esi->val[i] = a[i] & 0xff; | |
1330 | return 1; | |
1331 | } | |
1332 | ||
1333 | char *esi_to_str(const esi_t *esi, char *buf, int size) | |
1334 | { | |
1335 | char *ptr; | |
1336 | ||
1337 | if (!esi) | |
1338 | return NULL; | |
1339 | if (!buf) | |
9f5dc319 | 1340 | ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char)); |
50f74cf1 | 1341 | else { |
1342 | assert(size >= ESI_STR_LEN); | |
1343 | ptr = buf; | |
1344 | } | |
1345 | ||
1346 | snprintf(ptr, ESI_STR_LEN, | |
1347 | "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", | |
1348 | esi->val[0], esi->val[1], esi->val[2], | |
1349 | esi->val[3], esi->val[4], esi->val[5], | |
1350 | esi->val[6], esi->val[7], esi->val[8], | |
1351 | esi->val[9]); | |
1352 | return ptr; | |
1353 | } | |
d52ec572 | 1354 | |
74e2bd89 AK |
1355 | char *evpn_es_df_alg2str(uint8_t df_alg, char *buf, int buf_len) |
1356 | { | |
1357 | switch (df_alg) { | |
1358 | case EVPN_MH_DF_ALG_SERVICE_CARVING: | |
1359 | snprintf(buf, buf_len, "service-carving"); | |
1360 | break; | |
1361 | ||
1362 | case EVPN_MH_DF_ALG_HRW: | |
1363 | snprintf(buf, buf_len, "HRW"); | |
1364 | break; | |
1365 | ||
1366 | case EVPN_MH_DF_ALG_PREF: | |
1367 | snprintf(buf, buf_len, "preference"); | |
1368 | break; | |
1369 | ||
1370 | default: | |
1371 | snprintf(buf, buf_len, "unknown %u", df_alg); | |
1372 | break; | |
1373 | } | |
1374 | ||
1375 | return buf; | |
1376 | } | |
1377 | ||
d80132b1 DA |
1378 | bool ipv4_unicast_valid(const struct in_addr *addr) |
1379 | { | |
1380 | in_addr_t ip = ntohl(addr->s_addr); | |
1381 | ||
1382 | if (IPV4_CLASS_D(ip)) | |
1383 | return false; | |
1384 | ||
1385 | if (IPV4_CLASS_E(ip)) { | |
1386 | if (cmd_allow_reserved_ranges_get()) | |
1387 | return true; | |
1388 | else | |
1389 | return false; | |
1390 | } | |
1391 | ||
1392 | return true; | |
1393 | } | |
1394 | ||
6eb83505 SW |
1395 | static int ipaddr2prefix(const struct ipaddr *ip, uint16_t prefixlen, |
1396 | struct prefix *p) | |
1397 | { | |
1398 | switch (ip->ipa_type) { | |
1399 | case (IPADDR_V4): | |
1400 | p->family = AF_INET; | |
1401 | p->u.prefix4 = ip->ipaddr_v4; | |
1402 | p->prefixlen = prefixlen; | |
1403 | break; | |
1404 | case (IPADDR_V6): | |
1405 | p->family = AF_INET6; | |
1406 | p->u.prefix6 = ip->ipaddr_v6; | |
1407 | p->prefixlen = prefixlen; | |
1408 | break; | |
1409 | case (IPADDR_NONE): | |
1410 | p->family = AF_UNSPEC; | |
1411 | break; | |
1412 | } | |
1413 | ||
1414 | return 0; | |
1415 | } | |
1416 | ||
1417 | /* | |
1418 | * Convert type-2 and type-5 evpn route prefixes into the more | |
1419 | * general ipv4/ipv6 prefix types so we can match prefix lists | |
1420 | * and such. | |
1421 | */ | |
1422 | int evpn_prefix2prefix(const struct prefix *evpn, struct prefix *to) | |
1423 | { | |
1424 | const struct evpn_addr *addr; | |
1425 | ||
1426 | if (evpn->family != AF_EVPN) | |
1427 | return -1; | |
1428 | ||
1429 | addr = &evpn->u.prefix_evpn; | |
1430 | ||
1431 | switch (addr->route_type) { | |
5ad4fc6c | 1432 | case BGP_EVPN_MAC_IP_ROUTE: |
6eb83505 SW |
1433 | if (IS_IPADDR_V4(&addr->macip_addr.ip)) |
1434 | ipaddr2prefix(&addr->macip_addr.ip, 32, to); | |
1435 | else if (IS_IPADDR_V6(&addr->macip_addr.ip)) | |
1436 | ipaddr2prefix(&addr->macip_addr.ip, 128, to); | |
1437 | else | |
1438 | return -1; /* mac only? */ | |
1439 | ||
1440 | break; | |
5ad4fc6c | 1441 | case BGP_EVPN_IP_PREFIX_ROUTE: |
6eb83505 SW |
1442 | ipaddr2prefix(&addr->prefix_addr.ip, |
1443 | addr->prefix_addr.ip_prefix_length, to); | |
1444 | break; | |
1445 | default: | |
1446 | return -1; | |
1447 | } | |
1448 | ||
1449 | return 0; | |
1450 | } | |
1451 | ||
54929fd3 | 1452 | printfrr_ext_autoreg_p("EA", printfrr_ea); |
3ea79430 DL |
1453 | static ssize_t printfrr_ea(struct fbuf *buf, struct printfrr_eargs *ea, |
1454 | const void *ptr) | |
bd0ab4d8 DL |
1455 | { |
1456 | const struct ethaddr *mac = ptr; | |
212e04e5 | 1457 | char cbuf[ETHER_ADDR_STRLEN]; |
bd0ab4d8 | 1458 | |
212e04e5 | 1459 | if (!mac) |
eba599a3 | 1460 | return bputs(buf, "(null)"); |
8e2c653e | 1461 | |
212e04e5 DL |
1462 | /* need real length even if buffer is too short */ |
1463 | prefix_mac2str(mac, cbuf, sizeof(cbuf)); | |
1464 | return bputs(buf, cbuf); | |
bd0ab4d8 DL |
1465 | } |
1466 | ||
54929fd3 | 1467 | printfrr_ext_autoreg_p("IA", printfrr_ia); |
3ea79430 DL |
1468 | static ssize_t printfrr_ia(struct fbuf *buf, struct printfrr_eargs *ea, |
1469 | const void *ptr) | |
dc5d0186 DL |
1470 | { |
1471 | const struct ipaddr *ipa = ptr; | |
212e04e5 | 1472 | char cbuf[INET6_ADDRSTRLEN]; |
2c5b4d80 DL |
1473 | bool use_star = false; |
1474 | ||
1475 | if (ea->fmt[0] == 's') { | |
1476 | use_star = true; | |
1477 | ea->fmt++; | |
1478 | } | |
dc5d0186 | 1479 | |
927c633d | 1480 | if (!ipa || !ipa->ipa_type) |
eba599a3 | 1481 | return bputs(buf, "(null)"); |
8e2c653e | 1482 | |
2c5b4d80 DL |
1483 | if (use_star) { |
1484 | struct in_addr zero4 = {}; | |
1485 | struct in6_addr zero6 = {}; | |
1486 | ||
1487 | switch (ipa->ipa_type) { | |
1488 | case IPADDR_V4: | |
1489 | if (!memcmp(&ipa->ip.addr, &zero4, sizeof(zero4))) | |
1490 | return bputch(buf, '*'); | |
1491 | break; | |
1492 | ||
1493 | case IPADDR_V6: | |
1494 | if (!memcmp(&ipa->ip.addr, &zero6, sizeof(zero6))) | |
1495 | return bputch(buf, '*'); | |
1496 | break; | |
1497 | ||
bde30e78 | 1498 | case IPADDR_NONE: |
2c5b4d80 DL |
1499 | break; |
1500 | } | |
1501 | } | |
1502 | ||
212e04e5 DL |
1503 | ipaddr2str(ipa, cbuf, sizeof(cbuf)); |
1504 | return bputs(buf, cbuf); | |
dc5d0186 DL |
1505 | } |
1506 | ||
54929fd3 | 1507 | printfrr_ext_autoreg_p("I4", printfrr_i4); |
3ea79430 DL |
1508 | static ssize_t printfrr_i4(struct fbuf *buf, struct printfrr_eargs *ea, |
1509 | const void *ptr) | |
d52ec572 | 1510 | { |
212e04e5 | 1511 | char cbuf[INET_ADDRSTRLEN]; |
2c5b4d80 DL |
1512 | bool use_star = false; |
1513 | struct in_addr zero = {}; | |
1514 | ||
1515 | if (ea->fmt[0] == 's') { | |
1516 | use_star = true; | |
1517 | ea->fmt++; | |
1518 | } | |
212e04e5 DL |
1519 | |
1520 | if (!ptr) | |
eba599a3 | 1521 | return bputs(buf, "(null)"); |
8e2c653e | 1522 | |
2c5b4d80 DL |
1523 | if (use_star && !memcmp(ptr, &zero, sizeof(zero))) |
1524 | return bputch(buf, '*'); | |
1525 | ||
212e04e5 DL |
1526 | inet_ntop(AF_INET, ptr, cbuf, sizeof(cbuf)); |
1527 | return bputs(buf, cbuf); | |
d52ec572 DL |
1528 | } |
1529 | ||
54929fd3 | 1530 | printfrr_ext_autoreg_p("I6", printfrr_i6); |
3ea79430 DL |
1531 | static ssize_t printfrr_i6(struct fbuf *buf, struct printfrr_eargs *ea, |
1532 | const void *ptr) | |
d52ec572 | 1533 | { |
212e04e5 | 1534 | char cbuf[INET6_ADDRSTRLEN]; |
2c5b4d80 DL |
1535 | bool use_star = false; |
1536 | struct in6_addr zero = {}; | |
1537 | ||
1538 | if (ea->fmt[0] == 's') { | |
1539 | use_star = true; | |
1540 | ea->fmt++; | |
1541 | } | |
212e04e5 DL |
1542 | |
1543 | if (!ptr) | |
eba599a3 | 1544 | return bputs(buf, "(null)"); |
8e2c653e | 1545 | |
2c5b4d80 DL |
1546 | if (use_star && !memcmp(ptr, &zero, sizeof(zero))) |
1547 | return bputch(buf, '*'); | |
1548 | ||
212e04e5 DL |
1549 | inet_ntop(AF_INET6, ptr, cbuf, sizeof(cbuf)); |
1550 | return bputs(buf, cbuf); | |
d52ec572 DL |
1551 | } |
1552 | ||
54929fd3 | 1553 | printfrr_ext_autoreg_p("FX", printfrr_pfx); |
3ea79430 DL |
1554 | static ssize_t printfrr_pfx(struct fbuf *buf, struct printfrr_eargs *ea, |
1555 | const void *ptr) | |
d52ec572 | 1556 | { |
543a2684 DL |
1557 | bool host_only = false; |
1558 | ||
1559 | if (ea->fmt[0] == 'h') { | |
1560 | ea->fmt++; | |
1561 | host_only = true; | |
1562 | } | |
212e04e5 DL |
1563 | |
1564 | if (!ptr) | |
eba599a3 | 1565 | return bputs(buf, "(null)"); |
8e2c653e | 1566 | |
543a2684 DL |
1567 | if (host_only) |
1568 | return prefixhost2str(buf, (struct prefix *)ptr); | |
1569 | else { | |
1570 | char cbuf[PREFIX_STRLEN]; | |
1571 | ||
1572 | prefix2str(ptr, cbuf, sizeof(cbuf)); | |
1573 | return bputs(buf, cbuf); | |
1574 | } | |
d52ec572 DL |
1575 | } |
1576 | ||
54929fd3 | 1577 | printfrr_ext_autoreg_p("PSG4", printfrr_psg); |
3ea79430 DL |
1578 | static ssize_t printfrr_psg(struct fbuf *buf, struct printfrr_eargs *ea, |
1579 | const void *ptr) | |
d52ec572 DL |
1580 | { |
1581 | const struct prefix_sg *sg = ptr; | |
212e04e5 | 1582 | ssize_t ret = 0; |
d52ec572 | 1583 | |
212e04e5 | 1584 | if (!sg) |
eba599a3 | 1585 | return bputs(buf, "(null)"); |
8e2c653e | 1586 | |
212e04e5 DL |
1587 | if (sg->src.s_addr == INADDR_ANY) |
1588 | ret += bputs(buf, "(*,"); | |
1589 | else | |
1590 | ret += bprintfrr(buf, "(%pI4,", &sg->src); | |
8e2c653e | 1591 | |
212e04e5 DL |
1592 | if (sg->grp.s_addr == INADDR_ANY) |
1593 | ret += bputs(buf, "*)"); | |
1594 | else | |
1595 | ret += bprintfrr(buf, "%pI4)", &sg->grp); | |
d52ec572 | 1596 | |
212e04e5 | 1597 | return ret; |
d52ec572 | 1598 | } |