]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / lib / prefix.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Prefix related functions.
4 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
5 */
6
7 #include <zebra.h>
8
9 #include "command.h"
10 #include "prefix.h"
11 #include "ipaddr.h"
12 #include "vty.h"
13 #include "sockunion.h"
14 #include "memory.h"
15 #include "log.h"
16 #include "jhash.h"
17 #include "lib_errors.h"
18 #include "printfrr.h"
19 #include "vxlan.h"
20
21 DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix");
22 DEFINE_MTYPE_STATIC(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec");
23
24 /* Maskbit. */
25 static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
26 0xf8, 0xfc, 0xfe, 0xff};
27
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
35 int is_zero_mac(const struct ethaddr *mac)
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
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
66 unsigned int prefix_bit(const uint8_t *prefix, const uint16_t bit_index)
67 {
68 unsigned int offset = bit_index / 8;
69 unsigned int shift = 7 - (bit_index % 8);
70
71 return (prefix[offset] >> shift) & 1;
72 }
73
74 int str2family(const char *string)
75 {
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;
82 else if (!strcmp("evpn", string))
83 return AF_EVPN;
84 return -1;
85 }
86
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
102 /* Address Family Identifier to Address Family converter. */
103 int afi2family(afi_t afi)
104 {
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;
111 /* NOTE: EVPN code should NOT use this interface. */
112 return 0;
113 }
114
115 afi_t family2afi(int family)
116 {
117 if (family == AF_INET)
118 return AFI_IP;
119 else if (family == AF_INET6)
120 return AFI_IP6;
121 else if (family == AF_ETHERNET || family == AF_EVPN)
122 return AFI_L2VPN;
123 return 0;
124 }
125
126 const char *afi2str(afi_t afi)
127 {
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:
136 case AFI_UNSPEC:
137 return "bad-value";
138 }
139
140 assert(!"Reached end of function we should never reach");
141 }
142
143 const char *safi2str(safi_t safi)
144 {
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";
158 case SAFI_FLOWSPEC:
159 return "flowspec";
160 case SAFI_UNSPEC:
161 case SAFI_MAX:
162 return "unknown";
163 }
164
165 assert(!"Reached end of function we should never reach");
166 }
167
168 /* If n includes p prefix then return 1 else return 0. */
169 int prefix_match(union prefixconstptr unet, union prefixconstptr upfx)
170 {
171 const struct prefix *n = unet.p;
172 const struct prefix *p = upfx.p;
173 int offset;
174 int shift;
175 const uint8_t *np, *pp;
176
177 /* If n's prefix is longer than p's one return 0. */
178 if (n->prefixlen > p->prefixlen)
179 return 0;
180
181 if (n->family == AF_FLOWSPEC) {
182 /* prefixlen is unused. look at fs prefix len */
183 if (n->u.prefix_flowspec.family !=
184 p->u.prefix_flowspec.family)
185 return 0;
186
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
203 /* Set both prefix's head pointer. */
204 np = n->u.val;
205 pp = p->u.val;
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;
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
265 }
266
267 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
268 int prefix_match_network_statement(union prefixconstptr unet,
269 union prefixconstptr upfx)
270 {
271 const struct prefix *n = unet.p;
272 const struct prefix *p = upfx.p;
273 int offset;
274 int shift;
275 const uint8_t *np, *pp;
276
277 /* Set both prefix's head pointer. */
278 np = n->u.val;
279 pp = p->u.val;
280
281 offset = n->prefixlen / PNBBY;
282 shift = n->prefixlen % PNBBY;
283
284 if (shift)
285 if (maskbit[shift] & (np[offset] ^ pp[offset]))
286 return 0;
287
288 while (offset--)
289 if (np[offset] != pp[offset])
290 return 0;
291 return 1;
292 }
293
294 #ifdef __clang_analyzer__
295 #undef prefix_copy /* cf. prefix.h */
296 #endif
297
298 void prefix_copy(union prefixptr udest, union prefixconstptr usrc)
299 {
300 struct prefix *dest = udest.p;
301 const struct prefix *src = usrc.p;
302
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) {
311 memcpy(&dest->u.prefix_eth, &src->u.prefix_eth,
312 sizeof(struct ethaddr));
313 } else if (src->family == AF_EVPN) {
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;
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;
326 dest->u.prefix_flowspec.family =
327 src->u.prefix_flowspec.family;
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);
333 } else {
334 flog_err(EC_LIB_DEVELOPMENT,
335 "prefix_copy(): Unknown address family %d",
336 src->family);
337 assert(0);
338 }
339 }
340
341 /*
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 */
349 int prefix_same(union prefixconstptr up1, union prefixconstptr up2)
350 {
351 const struct prefix *p1 = up1.p;
352 const struct prefix *p2 = up2.p;
353
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)
362 if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
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)
369 if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth,
370 sizeof(struct ethaddr)))
371 return 1;
372 if (p1->family == AF_EVPN)
373 if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn,
374 sizeof(struct evpn_addr)))
375 return 1;
376 if (p1->family == AF_FLOWSPEC) {
377 if (p1->u.prefix_flowspec.family !=
378 p2->u.prefix_flowspec.family)
379 return 0;
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 }
388 }
389 return 0;
390 }
391
392 /*
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
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 */
403 int prefix_cmp(union prefixconstptr up1, union prefixconstptr up2)
404 {
405 const struct prefix *p1 = up1.p;
406 const struct prefix *p2 = up2.p;
407 int offset;
408 int shift;
409 int i;
410
411 /* Set both prefix's head pointer. */
412 const uint8_t *pp1;
413 const uint8_t *pp2;
414
415 if (p1->family != p2->family)
416 return numcmp(p1->family, p2->family);
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;
420
421 if (p1->u.prefix_flowspec.family !=
422 p2->u.prefix_flowspec.family)
423 return 1;
424
425 if (p1->u.prefix_flowspec.prefixlen !=
426 p2->u.prefix_flowspec.prefixlen)
427 return numcmp(p1->u.prefix_flowspec.prefixlen,
428 p2->u.prefix_flowspec.prefixlen);
429
430 offset = p1->u.prefix_flowspec.prefixlen;
431 while (offset--)
432 if (pp1[offset] != pp2[offset])
433 return numcmp(pp1[offset], pp2[offset]);
434 return 0;
435 }
436 pp1 = p1->u.val;
437 pp2 = p2->u.val;
438
439 if (p1->prefixlen != p2->prefixlen)
440 return numcmp(p1->prefixlen, p2->prefixlen);
441 offset = p1->prefixlen / PNBBY;
442 shift = p1->prefixlen % PNBBY;
443
444 i = memcmp(pp1, pp2, offset);
445 if (i)
446 return i;
447
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;
460 }
461
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 */
468 int prefix_common_bits(union prefixconstptr ua, union prefixconstptr ub)
469 {
470 const struct prefix *p1 = ua.p;
471 const struct prefix *p2 = ub.p;
472 int pos, bit;
473 int length = 0;
474 uint8_t xor ;
475
476 /* Set both prefix's head pointer. */
477 const uint8_t *pp1 = p1->u.val;
478 const uint8_t *pp2 = p2->u.val;
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)
485 length = ETH_ALEN;
486 if (p1->family == AF_EVPN)
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;
504 }
505
506 /* Return prefix family type string. */
507 const char *prefix_family_str(union prefixconstptr pu)
508 {
509 const struct prefix *p = pu.p;
510
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";
517 if (p->family == AF_EVPN)
518 return "evpn";
519 return "unspec";
520 }
521
522 /* Allocate new prefix_ipv4 structure. */
523 struct prefix_ipv4 *prefix_ipv4_new(void)
524 {
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;
534 }
535
536 /* Free prefix_ipv4 structure. */
537 void prefix_ipv4_free(struct prefix_ipv4 **p)
538 {
539 prefix_free((struct prefix **)p);
540 }
541
542 /* If given string is valid return 1 else return 0 */
543 int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p)
544 {
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. */
556 ret = inet_pton(AF_INET, str, &p->prefix);
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);
568 memcpy(cp, str, pnt - str);
569 *(cp + (pnt - str)) = '\0';
570 ret = inet_pton(AF_INET, cp, &p->prefix);
571 XFREE(MTYPE_TMP, cp);
572 if (ret == 0)
573 return 0;
574
575 /* Get prefix length. */
576 plen = (uint8_t)atoi(++pnt);
577 if (plen > IPV4_MAX_BITLEN)
578 return 0;
579
580 p->family = AF_INET;
581 p->prefixlen = plen;
582 }
583
584 return ret;
585 }
586
587 /* When string format is invalid return 0. */
588 int str2prefix_eth(const char *str, struct prefix_eth *p)
589 {
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;
597 bool slash = false;
598
599 if (!strcmp(str, "any")) {
600 memset(p, 0, sizeof(*p));
601 p->family = AF_ETHERNET;
602 return 1;
603 }
604
605 /* Find slash inside string. */
606 pnt = strchr(str, '/');
607
608 if (pnt) {
609 /* Get prefix length. */
610 plen = (uint8_t)atoi(++pnt);
611 if (plen > 48) {
612 ret = 0;
613 goto done;
614 }
615
616 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
617 memcpy(cp, str, pnt - str);
618 *(cp + (pnt - str)) = '\0';
619
620 str_addr = cp;
621 slash = true;
622 }
623
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;
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)))
643 p->prefixlen = 0;
644
645 ret = 1;
646
647 done:
648 XFREE(MTYPE_TMP, cp);
649
650 return ret;
651 }
652
653 /* Convert masklen into IP address's netmask (network byte order). */
654 void masklen2ip(const int masklen, struct in_addr *netmask)
655 {
656 assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
657
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) */
661
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);
667 }
668
669 /* Convert IP address's netmask into integer. We assume netmask is
670 * sequential one. Argument netmask should be network byte order. */
671 uint8_t ip_masklen(struct in_addr netmask)
672 {
673 uint32_t tmp = ~ntohl(netmask.s_addr);
674
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 */
679 return tmp ? __builtin_clz(tmp) : 32;
680 }
681
682 /* Apply mask to IPv4 prefix (network byte order). */
683 void apply_mask_ipv4(struct prefix_ipv4 *p)
684 {
685 struct in_addr mask;
686 masklen2ip(p->prefixlen, &mask);
687 p->prefix.s_addr &= mask.s_addr;
688 }
689
690 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
691 int prefix_ipv4_any(const struct prefix_ipv4 *p)
692 {
693 return (p->prefix.s_addr == INADDR_ANY && p->prefixlen == 0);
694 }
695
696 /* Allocate a new ip version 6 route */
697 struct prefix_ipv6 *prefix_ipv6_new(void)
698 {
699 struct prefix_ipv6 *p;
700
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;
706 }
707
708 /* Free prefix for IPv6. */
709 void prefix_ipv6_free(struct prefix_ipv6 **p)
710 {
711 prefix_free((struct prefix **)p);
712 }
713
714 /* If given string is valid return 1 else return 0 */
715 int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p)
716 {
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);
733 memcpy(cp, str, pnt - str);
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;
739 plen = (uint8_t)atoi(++pnt);
740 if (plen > IPV6_MAX_BITLEN)
741 return 0;
742 p->prefixlen = plen;
743 }
744 p->family = AF_INET6;
745
746 return ret;
747 }
748
749 /* Convert struct in6_addr netmask into integer.
750 * FIXME return uint8_t as ip_maskleni() does. */
751 int ip6_masklen(struct in6_addr netmask)
752 {
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;
763 }
764
765 void masklen2ip6(const int masklen, struct in6_addr *netmask)
766 {
767 assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
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 }
793 }
794
795 void apply_mask_ipv6(struct prefix_ipv6 *p)
796 {
797 uint8_t *pnt;
798 int index;
799 int offset;
800
801 index = p->prefixlen / 8;
802
803 if (index < 16) {
804 pnt = (uint8_t *)&p->prefix;
805 offset = p->prefixlen % 8;
806
807 pnt[index] &= maskbit[offset];
808 index++;
809
810 while (index < 16)
811 pnt[index++] = 0;
812 }
813 }
814
815 void apply_mask(union prefixptr pu)
816 {
817 struct prefix *p = pu.p;
818
819 switch (p->family) {
820 case AF_INET:
821 apply_mask_ipv4(pu.p4);
822 break;
823 case AF_INET6:
824 apply_mask_ipv6(pu.p6);
825 break;
826 default:
827 break;
828 }
829 return;
830 }
831
832 /* Utility function of convert between struct prefix <=> union sockunion. */
833 struct prefix *sockunion2hostprefix(const union sockunion *su,
834 struct prefix *prefix)
835 {
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;
856 }
857
858 void prefix2sockunion(const struct prefix *p, union sockunion *su)
859 {
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));
868 }
869
870 int prefix_blen(union prefixconstptr pu)
871 {
872 const struct prefix *p = pu.p;
873
874 switch (p->family) {
875 case AF_INET:
876 return IPV4_MAX_BYTELEN;
877 case AF_INET6:
878 return IPV6_MAX_BYTELEN;
879 case AF_ETHERNET:
880 return ETH_ALEN;
881 }
882 return 0;
883 }
884
885 /* Generic function for conversion string to struct prefix. */
886 int str2prefix(const char *str, struct prefix *p)
887 {
888 int ret;
889
890 if (!str || !p)
891 return 0;
892
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;
897
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;
902
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;
907
908 return 0;
909 }
910
911 static const char *prefixevpn_ead2str(const struct prefix_evpn *p, char *str,
912 int size)
913 {
914 uint8_t family;
915 char buf[ESI_STR_LEN];
916 char buf1[INET6_ADDRSTRLEN];
917
918 family = IS_IPADDR_V4(&p->prefix.ead_addr.ip) ? AF_INET : AF_INET6;
919 snprintf(str, size, "[%d]:[%u]:[%s]:[%d]:[%s]:[%u]",
920 p->prefix.route_type, p->prefix.ead_addr.eth_tag,
921 esi_to_str(&p->prefix.ead_addr.esi, buf, sizeof(buf)),
922 (family == AF_INET) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN,
923 inet_ntop(family, &p->prefix.ead_addr.ip.ipaddr_v4, buf1,
924 sizeof(buf1)),
925 p->prefix.ead_addr.frag_id);
926 return str;
927 }
928
929 static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str,
930 int size)
931 {
932 uint8_t family;
933 char buf1[ETHER_ADDR_STRLEN];
934 char buf2[PREFIX2STR_BUFFER];
935
936 if (is_evpn_prefix_ipaddr_none(p))
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)));
941 else {
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));
951 }
952 return str;
953 }
954
955 static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str,
956 int size)
957 {
958 uint8_t family;
959 char buf[INET6_ADDRSTRLEN];
960
961 family = IS_IPADDR_V4(&p->prefix.imet_addr.ip) ? AF_INET : AF_INET6;
962 snprintf(str, size, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type,
963 p->prefix.imet_addr.eth_tag,
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)));
967
968 return str;
969 }
970
971 static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str,
972 int size)
973 {
974 uint8_t family;
975 char buf[ESI_STR_LEN];
976 char buf1[INET6_ADDRSTRLEN];
977
978 family = IS_IPADDR_V4(&p->prefix.es_addr.ip) ? AF_INET : AF_INET6;
979 snprintf(str, size, "[%d]:[%s]:[%d]:[%s]", p->prefix.route_type,
980 esi_to_str(&p->prefix.es_addr.esi, buf, sizeof(buf)),
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)));
984
985 return str;
986 }
987
988 static const char *prefixevpn_prefix2str(const struct prefix_evpn *p, char *str,
989 int size)
990 {
991 uint8_t family;
992 char buf[INET6_ADDRSTRLEN];
993
994 family = IS_IPADDR_V4(&p->prefix.prefix_addr.ip) ? AF_INET : AF_INET6;
995 snprintf(str, size, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type,
996 p->prefix.prefix_addr.eth_tag,
997 p->prefix.prefix_addr.ip_prefix_length,
998 inet_ntop(family, &p->prefix.prefix_addr.ip.ipaddr_v4, buf,
999 sizeof(buf)));
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) {
1007 case BGP_EVPN_AD_ROUTE:
1008 return prefixevpn_ead2str(p, str, size);
1009 case BGP_EVPN_MAC_IP_ROUTE:
1010 return prefixevpn_macip2str(p, str, size);
1011 case BGP_EVPN_IMET_ROUTE:
1012 return prefixevpn_imet2str(p, str, size);
1013 case BGP_EVPN_ES_ROUTE:
1014 return prefixevpn_es2str(p, str, size);
1015 case BGP_EVPN_IP_PREFIX_ROUTE:
1016 return prefixevpn_prefix2str(p, str, size);
1017 default:
1018 snprintf(str, size, "Unsupported EVPN prefix");
1019 break;
1020 }
1021 return str;
1022 }
1023
1024 const char *prefix2str(union prefixconstptr pu, char *str, int size)
1025 {
1026 const struct prefix *p = pu.p;
1027 char buf[PREFIX2STR_BUFFER];
1028 int byte, tmp, a, b;
1029 bool z = false;
1030 size_t l;
1031
1032 switch (p->family) {
1033 case AF_INET:
1034 case AF_INET6:
1035 inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf));
1036 l = strlen(buf);
1037 buf[l++] = '/';
1038 byte = p->prefixlen;
1039 tmp = p->prefixlen - 100;
1040 if (tmp >= 0) {
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);
1052 break;
1053
1054 case AF_ETHERNET:
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:
1061 prefixevpn2str((const struct prefix_evpn *)p, str, size);
1062 break;
1063
1064 case AF_FLOWSPEC:
1065 strlcpy(str, "FS prefix", size);
1066 break;
1067
1068 default:
1069 strlcpy(str, "UNK prefix", size);
1070 break;
1071 }
1072
1073 return str;
1074 }
1075
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
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)
1102 strlcpy(buf, "*", buf_size);
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
1125 struct prefix *prefix_new(void)
1126 {
1127 struct prefix *p;
1128
1129 p = XCALLOC(MTYPE_PREFIX, sizeof(*p));
1130 return p;
1131 }
1132
1133 void prefix_free_lists(void *arg)
1134 {
1135 struct prefix *p = arg;
1136
1137 prefix_free(&p);
1138 }
1139
1140 /* Free prefix structure. */
1141 void prefix_free(struct prefix **p)
1142 {
1143 XFREE(MTYPE_PREFIX, *p);
1144 }
1145
1146 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1147 void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
1148 {
1149
1150 uint32_t destination;
1151
1152 destination = ntohl(p->prefix.s_addr);
1153
1154 if (p->prefixlen == IPV4_MAX_BITLEN)
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 }
1167 }
1168
1169 in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
1170 {
1171 struct in_addr mask;
1172
1173 masklen2ip(masklen, &mask);
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);
1181 }
1182
1183 /* Utility function to convert ipv4 netmask to prefixes
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" */
1186 int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1187 char *prefix_str, size_t prefix_str_len)
1188 {
1189 struct in_addr network;
1190 struct in_addr mask;
1191 uint8_t prefixlen;
1192 uint32_t destination;
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
1208 if (network.s_addr == INADDR_ANY)
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 }
1219
1220 snprintf(prefix_str, prefix_str_len, "%s/%d", net_str, prefixlen);
1221
1222 return 1;
1223 }
1224
1225 /* converts to internal representation of mac address
1226 * returns 1 on success, 0 otherwise
1227 * format accepted: AA:BB:CC:DD:EE:FF
1228 * if mac parameter is null, then check only
1229 */
1230 int prefix_str2mac(const char *str, struct ethaddr *mac)
1231 {
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;
1250 }
1251
1252 char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
1253 {
1254 char *ptr;
1255
1256 if (!mac)
1257 return NULL;
1258 if (!buf)
1259 ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char));
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;
1269 }
1270
1271 unsigned prefix_hash_key(const void *pp)
1272 {
1273 struct prefix copy;
1274
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(&copy, 0, sizeof(copy));
1284 prefix_copy(&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 }
1293 /* make sure *all* unused bits are zero, particularly including
1294 * alignment /
1295 * padding and unused prefix bytes. */
1296 memset(&copy, 0, sizeof(copy));
1297 prefix_copy(&copy, (struct prefix *)pp);
1298 return jhash(&copy,
1299 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1300 0x55aa5a5a);
1301 }
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)
1340 ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char));
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 }
1354
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
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
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) {
1432 case BGP_EVPN_MAC_IP_ROUTE:
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;
1441 case BGP_EVPN_IP_PREFIX_ROUTE:
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
1452 printfrr_ext_autoreg_p("EA", printfrr_ea);
1453 static ssize_t printfrr_ea(struct fbuf *buf, struct printfrr_eargs *ea,
1454 const void *ptr)
1455 {
1456 const struct ethaddr *mac = ptr;
1457 char cbuf[ETHER_ADDR_STRLEN];
1458
1459 if (!mac)
1460 return bputs(buf, "(null)");
1461
1462 /* need real length even if buffer is too short */
1463 prefix_mac2str(mac, cbuf, sizeof(cbuf));
1464 return bputs(buf, cbuf);
1465 }
1466
1467 printfrr_ext_autoreg_p("IA", printfrr_ia);
1468 static ssize_t printfrr_ia(struct fbuf *buf, struct printfrr_eargs *ea,
1469 const void *ptr)
1470 {
1471 const struct ipaddr *ipa = ptr;
1472 char cbuf[INET6_ADDRSTRLEN];
1473 bool use_star = false;
1474
1475 if (ea->fmt[0] == 's') {
1476 use_star = true;
1477 ea->fmt++;
1478 }
1479
1480 if (!ipa || !ipa->ipa_type)
1481 return bputs(buf, "(null)");
1482
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
1498 case IPADDR_NONE:
1499 break;
1500 }
1501 }
1502
1503 ipaddr2str(ipa, cbuf, sizeof(cbuf));
1504 return bputs(buf, cbuf);
1505 }
1506
1507 printfrr_ext_autoreg_p("I4", printfrr_i4);
1508 static ssize_t printfrr_i4(struct fbuf *buf, struct printfrr_eargs *ea,
1509 const void *ptr)
1510 {
1511 char cbuf[INET_ADDRSTRLEN];
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 }
1519
1520 if (!ptr)
1521 return bputs(buf, "(null)");
1522
1523 if (use_star && !memcmp(ptr, &zero, sizeof(zero)))
1524 return bputch(buf, '*');
1525
1526 inet_ntop(AF_INET, ptr, cbuf, sizeof(cbuf));
1527 return bputs(buf, cbuf);
1528 }
1529
1530 printfrr_ext_autoreg_p("I6", printfrr_i6);
1531 static ssize_t printfrr_i6(struct fbuf *buf, struct printfrr_eargs *ea,
1532 const void *ptr)
1533 {
1534 char cbuf[INET6_ADDRSTRLEN];
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 }
1542
1543 if (!ptr)
1544 return bputs(buf, "(null)");
1545
1546 if (use_star && !memcmp(ptr, &zero, sizeof(zero)))
1547 return bputch(buf, '*');
1548
1549 inet_ntop(AF_INET6, ptr, cbuf, sizeof(cbuf));
1550 return bputs(buf, cbuf);
1551 }
1552
1553 printfrr_ext_autoreg_p("FX", printfrr_pfx);
1554 static ssize_t printfrr_pfx(struct fbuf *buf, struct printfrr_eargs *ea,
1555 const void *ptr)
1556 {
1557 bool host_only = false;
1558
1559 if (ea->fmt[0] == 'h') {
1560 ea->fmt++;
1561 host_only = true;
1562 }
1563
1564 if (!ptr)
1565 return bputs(buf, "(null)");
1566
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 }
1575 }
1576
1577 printfrr_ext_autoreg_p("PSG4", printfrr_psg);
1578 static ssize_t printfrr_psg(struct fbuf *buf, struct printfrr_eargs *ea,
1579 const void *ptr)
1580 {
1581 const struct prefix_sg *sg = ptr;
1582 ssize_t ret = 0;
1583
1584 if (!sg)
1585 return bputs(buf, "(null)");
1586
1587 if (sg->src.s_addr == INADDR_ANY)
1588 ret += bputs(buf, "(*,");
1589 else
1590 ret += bprintfrr(buf, "(%pI4,", &sg->src);
1591
1592 if (sg->grp.s_addr == INADDR_ANY)
1593 ret += bputs(buf, "*)");
1594 else
1595 ret += bprintfrr(buf, "%pI4)", &sg->grp);
1596
1597 return ret;
1598 }