]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
bgpd: support for DF election in EVPN-MH
[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 Famiy 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_PREFIXLEN)
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 if ((tmp = p->prefixlen - 100) >= 0) {
1038 buf[l++] = '1';
1039 z = true;
1040 byte = tmp;
1041 }
1042 b = byte % 10;
1043 a = byte / 10;
1044 if (a || z)
1045 buf[l++] = '0' + a;
1046 buf[l++] = '0' + b;
1047 buf[l] = '\0';
1048 strlcpy(str, buf, size);
1049 break;
1050
1051 case AF_ETHERNET:
1052 snprintf(str, size, "%s/%d",
1053 prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)),
1054 p->prefixlen);
1055 break;
1056
1057 case AF_EVPN:
1058 prefixevpn2str((const struct prefix_evpn *)p, str, size);
1059 break;
1060
1061 case AF_FLOWSPEC:
1062 strlcpy(str, "FS prefix", size);
1063 break;
1064
1065 default:
1066 strlcpy(str, "UNK prefix", size);
1067 break;
1068 }
1069
1070 return str;
1071 }
1072
1073 void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr,
1074 char *buf, int buf_size)
1075 {
1076 int save_errno = errno;
1077
1078 if (addr.s_addr == INADDR_ANY)
1079 strlcpy(buf, "*", buf_size);
1080 else {
1081 if (!inet_ntop(AF_INET, &addr, buf, buf_size)) {
1082 if (onfail)
1083 snprintf(buf, buf_size, "%s", onfail);
1084 }
1085 }
1086
1087 errno = save_errno;
1088 }
1089
1090 const char *prefix_sg2str(const struct prefix_sg *sg, char *sg_str)
1091 {
1092 char src_str[INET_ADDRSTRLEN];
1093 char grp_str[INET_ADDRSTRLEN];
1094
1095 prefix_mcast_inet4_dump("<src?>", sg->src, src_str, sizeof(src_str));
1096 prefix_mcast_inet4_dump("<grp?>", sg->grp, grp_str, sizeof(grp_str));
1097 snprintf(sg_str, PREFIX_SG_STR_LEN, "(%s,%s)", src_str, grp_str);
1098
1099 return sg_str;
1100 }
1101
1102 struct prefix *prefix_new(void)
1103 {
1104 struct prefix *p;
1105
1106 p = XCALLOC(MTYPE_PREFIX, sizeof(*p));
1107 return p;
1108 }
1109
1110 void prefix_free_lists(void *arg)
1111 {
1112 struct prefix *p = arg;
1113
1114 prefix_free(&p);
1115 }
1116
1117 /* Free prefix structure. */
1118 void prefix_free(struct prefix **p)
1119 {
1120 XFREE(MTYPE_PREFIX, *p);
1121 }
1122
1123 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1124 void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
1125 {
1126
1127 uint32_t destination;
1128
1129 destination = ntohl(p->prefix.s_addr);
1130
1131 if (p->prefixlen == IPV4_MAX_PREFIXLEN)
1132 ;
1133 /* do nothing for host routes */
1134 else if (IN_CLASSC(destination)) {
1135 p->prefixlen = 24;
1136 apply_mask_ipv4(p);
1137 } else if (IN_CLASSB(destination)) {
1138 p->prefixlen = 16;
1139 apply_mask_ipv4(p);
1140 } else {
1141 p->prefixlen = 8;
1142 apply_mask_ipv4(p);
1143 }
1144 }
1145
1146 in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
1147 {
1148 struct in_addr mask;
1149
1150 masklen2ip(masklen, &mask);
1151 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1152 /* normal case */
1153 (hostaddr | ~mask.s_addr)
1154 :
1155 /* For prefix 31 return 255.255.255.255 (RFC3021) */
1156 htonl(0xFFFFFFFF);
1157 }
1158
1159 /* Utility function to convert ipv4 netmask to prefixes
1160 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1161 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1162 int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1163 char *prefix_str)
1164 {
1165 struct in_addr network;
1166 struct in_addr mask;
1167 uint8_t prefixlen;
1168 uint32_t destination;
1169 int ret;
1170
1171 ret = inet_aton(net_str, &network);
1172 if (!ret)
1173 return 0;
1174
1175 if (mask_str) {
1176 ret = inet_aton(mask_str, &mask);
1177 if (!ret)
1178 return 0;
1179
1180 prefixlen = ip_masklen(mask);
1181 } else {
1182 destination = ntohl(network.s_addr);
1183
1184 if (network.s_addr == INADDR_ANY)
1185 prefixlen = 0;
1186 else if (IN_CLASSC(destination))
1187 prefixlen = 24;
1188 else if (IN_CLASSB(destination))
1189 prefixlen = 16;
1190 else if (IN_CLASSA(destination))
1191 prefixlen = 8;
1192 else
1193 return 0;
1194 }
1195
1196 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
1197
1198 return 1;
1199 }
1200
1201 /* Utility function for making IPv6 address string. */
1202 const char *inet6_ntoa(struct in6_addr addr)
1203 {
1204 static char buf[INET6_ADDRSTRLEN];
1205
1206 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1207 return buf;
1208 }
1209
1210 /* converts to internal representation of mac address
1211 * returns 1 on success, 0 otherwise
1212 * format accepted: AA:BB:CC:DD:EE:FF
1213 * if mac parameter is null, then check only
1214 */
1215 int prefix_str2mac(const char *str, struct ethaddr *mac)
1216 {
1217 unsigned int a[6];
1218 int i;
1219
1220 if (!str)
1221 return 0;
1222
1223 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3,
1224 a + 4, a + 5)
1225 != 6) {
1226 /* error in incoming str length */
1227 return 0;
1228 }
1229 /* valid mac address */
1230 if (!mac)
1231 return 1;
1232 for (i = 0; i < 6; ++i)
1233 mac->octet[i] = a[i] & 0xff;
1234 return 1;
1235 }
1236
1237 char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
1238 {
1239 char *ptr;
1240
1241 if (!mac)
1242 return NULL;
1243 if (!buf)
1244 ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char));
1245 else {
1246 assert(size >= ETHER_ADDR_STRLEN);
1247 ptr = buf;
1248 }
1249 snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x",
1250 (uint8_t)mac->octet[0], (uint8_t)mac->octet[1],
1251 (uint8_t)mac->octet[2], (uint8_t)mac->octet[3],
1252 (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]);
1253 return ptr;
1254 }
1255
1256 unsigned prefix_hash_key(const void *pp)
1257 {
1258 struct prefix copy;
1259
1260 if (((struct prefix *)pp)->family == AF_FLOWSPEC) {
1261 uint32_t len;
1262 void *temp;
1263
1264 /* make sure *all* unused bits are zero,
1265 * particularly including alignment /
1266 * padding and unused prefix bytes.
1267 */
1268 memset(&copy, 0, sizeof(copy));
1269 prefix_copy(&copy, (struct prefix *)pp);
1270 len = jhash((void *)copy.u.prefix_flowspec.ptr,
1271 copy.u.prefix_flowspec.prefixlen,
1272 0x55aa5a5a);
1273 temp = (void *)copy.u.prefix_flowspec.ptr;
1274 XFREE(MTYPE_PREFIX_FLOWSPEC, temp);
1275 copy.u.prefix_flowspec.ptr = (uintptr_t)NULL;
1276 return len;
1277 }
1278 /* make sure *all* unused bits are zero, particularly including
1279 * alignment /
1280 * padding and unused prefix bytes. */
1281 memset(&copy, 0, sizeof(copy));
1282 prefix_copy(&copy, (struct prefix *)pp);
1283 return jhash(&copy,
1284 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1285 0x55aa5a5a);
1286 }
1287
1288 /* converts to internal representation of esi
1289 * returns 1 on success, 0 otherwise
1290 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1291 * if esi parameter is null, then check only
1292 */
1293 int str_to_esi(const char *str, esi_t *esi)
1294 {
1295 int i;
1296 unsigned int a[ESI_BYTES];
1297
1298 if (!str)
1299 return 0;
1300
1301 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1302 a + 0, a + 1, a + 2, a + 3,
1303 a + 4, a + 5, a + 6, a + 7,
1304 a + 8, a + 9)
1305 != ESI_BYTES) {
1306 /* error in incoming str length */
1307 return 0;
1308 }
1309
1310 /* valid ESI */
1311 if (!esi)
1312 return 1;
1313 for (i = 0; i < ESI_BYTES; ++i)
1314 esi->val[i] = a[i] & 0xff;
1315 return 1;
1316 }
1317
1318 char *esi_to_str(const esi_t *esi, char *buf, int size)
1319 {
1320 char *ptr;
1321
1322 if (!esi)
1323 return NULL;
1324 if (!buf)
1325 ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char));
1326 else {
1327 assert(size >= ESI_STR_LEN);
1328 ptr = buf;
1329 }
1330
1331 snprintf(ptr, ESI_STR_LEN,
1332 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1333 esi->val[0], esi->val[1], esi->val[2],
1334 esi->val[3], esi->val[4], esi->val[5],
1335 esi->val[6], esi->val[7], esi->val[8],
1336 esi->val[9]);
1337 return ptr;
1338 }
1339
1340 char *evpn_es_df_alg2str(uint8_t df_alg, char *buf, int buf_len)
1341 {
1342 switch (df_alg) {
1343 case EVPN_MH_DF_ALG_SERVICE_CARVING:
1344 snprintf(buf, buf_len, "service-carving");
1345 break;
1346
1347 case EVPN_MH_DF_ALG_HRW:
1348 snprintf(buf, buf_len, "HRW");
1349 break;
1350
1351 case EVPN_MH_DF_ALG_PREF:
1352 snprintf(buf, buf_len, "preference");
1353 break;
1354
1355 default:
1356 snprintf(buf, buf_len, "unknown %u", df_alg);
1357 break;
1358 }
1359
1360 return buf;
1361 }
1362
1363 printfrr_ext_autoreg_p("EA", printfrr_ea)
1364 static ssize_t printfrr_ea(char *buf, size_t bsz, const char *fmt,
1365 int prec, const void *ptr)
1366 {
1367 const struct ethaddr *mac = ptr;
1368
1369 prefix_mac2str(mac, buf, bsz);
1370 return 2;
1371 }
1372
1373 printfrr_ext_autoreg_p("IA", printfrr_ia)
1374 static ssize_t printfrr_ia(char *buf, size_t bsz, const char *fmt,
1375 int prec, const void *ptr)
1376 {
1377 const struct ipaddr *ipa = ptr;
1378
1379 ipaddr2str(ipa, buf, bsz);
1380 return 2;
1381 }
1382
1383 printfrr_ext_autoreg_p("I4", printfrr_i4)
1384 static ssize_t printfrr_i4(char *buf, size_t bsz, const char *fmt,
1385 int prec, const void *ptr)
1386 {
1387 inet_ntop(AF_INET, ptr, buf, bsz);
1388 return 2;
1389 }
1390
1391 printfrr_ext_autoreg_p("I6", printfrr_i6)
1392 static ssize_t printfrr_i6(char *buf, size_t bsz, const char *fmt,
1393 int prec, const void *ptr)
1394 {
1395 inet_ntop(AF_INET6, ptr, buf, bsz);
1396 return 2;
1397 }
1398
1399 printfrr_ext_autoreg_p("FX", printfrr_pfx)
1400 static ssize_t printfrr_pfx(char *buf, size_t bsz, const char *fmt,
1401 int prec, const void *ptr)
1402 {
1403 prefix2str(ptr, buf, bsz);
1404 return 2;
1405 }
1406
1407 printfrr_ext_autoreg_p("SG4", printfrr_psg)
1408 static ssize_t printfrr_psg(char *buf, size_t bsz, const char *fmt,
1409 int prec, const void *ptr)
1410 {
1411 const struct prefix_sg *sg = ptr;
1412 struct fbuf fb = { .buf = buf, .pos = buf, .len = bsz - 1 };
1413
1414 if (sg->src.s_addr == INADDR_ANY)
1415 bprintfrr(&fb, "(*,");
1416 else
1417 bprintfrr(&fb, "(%pI4,", &sg->src);
1418
1419 if (sg->grp.s_addr == INADDR_ANY)
1420 bprintfrr(&fb, "*)");
1421 else
1422 bprintfrr(&fb, "%pI4)", &sg->grp);
1423
1424 fb.pos[0] = '\0';
1425 return 3;
1426 }