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