1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Prefix related functions.
4 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
13 #include "sockunion.h"
17 #include "lib_errors.h"
21 DEFINE_MTYPE_STATIC(LIB
, PREFIX
, "Prefix");
22 DEFINE_MTYPE_STATIC(LIB
, PREFIX_FLOWSPEC
, "Prefix Flowspec");
25 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
26 0xf8, 0xfc, 0xfe, 0xff};
28 /* Number of bits in prefix type. */
33 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
35 int is_zero_mac(const struct ethaddr
*mac
)
39 for (i
= 0; i
< ETH_ALEN
; i
++) {
47 bool is_bcast_mac(const struct ethaddr
*mac
)
51 for (i
= 0; i
< ETH_ALEN
; i
++)
52 if (mac
->octet
[i
] != 0xFF)
58 bool is_mcast_mac(const struct ethaddr
*mac
)
60 if ((mac
->octet
[0] & 0x01) == 0x01)
66 unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t bit_index
)
68 unsigned int offset
= bit_index
/ 8;
69 unsigned int shift
= 7 - (bit_index
% 8);
71 return (prefix
[offset
] >> shift
) & 1;
74 int str2family(const char *string
)
76 if (!strcmp("ipv4", string
))
78 else if (!strcmp("ipv6", string
))
80 else if (!strcmp("ethernet", string
))
82 else if (!strcmp("evpn", string
))
87 const char *family2str(int family
)
102 /* Address Family Identifier to Address Family converter. */
103 int afi2family(afi_t afi
)
107 else if (afi
== AFI_IP6
)
109 else if (afi
== AFI_L2VPN
)
111 /* NOTE: EVPN code should NOT use this interface. */
115 afi_t
family2afi(int family
)
117 if (family
== AF_INET
)
119 else if (family
== AF_INET6
)
121 else if (family
== AF_ETHERNET
|| family
== AF_EVPN
)
126 const char *afi2str_lower(afi_t afi
)
140 assert(!"Reached end of function we should never reach");
143 const char *afi2str(afi_t afi
)
157 assert(!"Reached end of function we should never reach");
160 const char *safi2str(safi_t safi
)
173 case SAFI_LABELED_UNICAST
:
174 return "labeled-unicast";
182 assert(!"Reached end of function we should never reach");
185 /* If n includes p prefix then return 1 else return 0. */
186 int prefix_match(union prefixconstptr unet
, union prefixconstptr upfx
)
188 const struct prefix
*n
= unet
.p
;
189 const struct prefix
*p
= upfx
.p
;
192 const uint8_t *np
, *pp
;
194 /* If n's prefix is longer than p's one return 0. */
195 if (n
->prefixlen
> p
->prefixlen
)
198 if (n
->family
== AF_FLOWSPEC
) {
199 /* prefixlen is unused. look at fs prefix len */
200 if (n
->u
.prefix_flowspec
.family
!=
201 p
->u
.prefix_flowspec
.family
)
204 if (n
->u
.prefix_flowspec
.prefixlen
>
205 p
->u
.prefix_flowspec
.prefixlen
)
208 /* Set both prefix's head pointer. */
209 np
= (const uint8_t *)&n
->u
.prefix_flowspec
.ptr
;
210 pp
= (const uint8_t *)&p
->u
.prefix_flowspec
.ptr
;
212 offset
= n
->u
.prefix_flowspec
.prefixlen
;
215 if (np
[offset
] != pp
[offset
])
220 /* Set both prefix's head pointer. */
224 offset
= n
->prefixlen
/ PNBBY
;
225 shift
= n
->prefixlen
% PNBBY
;
228 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
232 if (np
[offset
] != pp
[offset
])
239 * n is a type5 evpn prefix. This function tries to see if there is an
240 * ip-prefix within n which matches prefix p
241 * If n includes p prefix then return 1 else return 0.
243 int evpn_type5_prefix_match(const struct prefix
*n
, const struct prefix
*p
)
248 const uint8_t *np
, *pp
;
249 struct prefix_evpn
*evp
;
251 if (n
->family
!= AF_EVPN
)
254 evp
= (struct prefix_evpn
*)n
;
257 if ((evp
->prefix
.route_type
!= 5) ||
258 (p
->family
== AF_INET6
&& !is_evpn_prefix_ipaddr_v6(evp
)) ||
259 (p
->family
== AF_INET
&& !is_evpn_prefix_ipaddr_v4(evp
)) ||
260 (is_evpn_prefix_ipaddr_none(evp
)))
263 prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
264 np
= &evp
->prefix
.prefix_addr
.ip
.ip
.addr
;
266 /* If n's prefix is longer than p's one return 0. */
267 if (prefixlen
> p
->prefixlen
)
270 offset
= prefixlen
/ PNBBY
;
271 shift
= prefixlen
% PNBBY
;
274 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
278 if (np
[offset
] != pp
[offset
])
284 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
285 int prefix_match_network_statement(union prefixconstptr unet
,
286 union prefixconstptr upfx
)
288 const struct prefix
*n
= unet
.p
;
289 const struct prefix
*p
= upfx
.p
;
292 const uint8_t *np
, *pp
;
294 /* Set both prefix's head pointer. */
298 offset
= n
->prefixlen
/ PNBBY
;
299 shift
= n
->prefixlen
% PNBBY
;
302 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
306 if (np
[offset
] != pp
[offset
])
311 #ifdef __clang_analyzer__
312 #undef prefix_copy /* cf. prefix.h */
315 void prefix_copy(union prefixptr udest
, union prefixconstptr usrc
)
317 struct prefix
*dest
= udest
.p
;
318 const struct prefix
*src
= usrc
.p
;
320 dest
->family
= src
->family
;
321 dest
->prefixlen
= src
->prefixlen
;
323 if (src
->family
== AF_INET
)
324 dest
->u
.prefix4
= src
->u
.prefix4
;
325 else if (src
->family
== AF_INET6
)
326 dest
->u
.prefix6
= src
->u
.prefix6
;
327 else if (src
->family
== AF_ETHERNET
) {
328 memcpy(&dest
->u
.prefix_eth
, &src
->u
.prefix_eth
,
329 sizeof(struct ethaddr
));
330 } else if (src
->family
== AF_EVPN
) {
331 memcpy(&dest
->u
.prefix_evpn
, &src
->u
.prefix_evpn
,
332 sizeof(struct evpn_addr
));
333 } else if (src
->family
== AF_UNSPEC
) {
334 dest
->u
.lp
.id
= src
->u
.lp
.id
;
335 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
336 } else if (src
->family
== AF_FLOWSPEC
) {
340 len
= src
->u
.prefix_flowspec
.prefixlen
;
341 dest
->u
.prefix_flowspec
.prefixlen
=
342 src
->u
.prefix_flowspec
.prefixlen
;
343 dest
->u
.prefix_flowspec
.family
=
344 src
->u
.prefix_flowspec
.family
;
345 dest
->family
= src
->family
;
346 temp
= XCALLOC(MTYPE_PREFIX_FLOWSPEC
, len
);
347 dest
->u
.prefix_flowspec
.ptr
= (uintptr_t)temp
;
348 memcpy((void *)dest
->u
.prefix_flowspec
.ptr
,
349 (void *)src
->u
.prefix_flowspec
.ptr
, len
);
351 flog_err(EC_LIB_DEVELOPMENT
,
352 "prefix_copy(): Unknown address family %d",
359 * Return 1 if the address/netmask contained in the prefix structure
360 * is the same, and else return 0. For this routine, 'same' requires
361 * that not only the prefix length and the network part be the same,
362 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
363 * the same. Note that this routine has the same return value sense
364 * as '==' (which is different from prefix_cmp).
366 int prefix_same(union prefixconstptr up1
, union prefixconstptr up2
)
368 const struct prefix
*p1
= up1
.p
;
369 const struct prefix
*p2
= up2
.p
;
371 if ((p1
&& !p2
) || (!p1
&& p2
))
377 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
) {
378 if (p1
->family
== AF_INET
)
379 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
381 if (p1
->family
== AF_INET6
)
382 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
383 &p2
->u
.prefix6
.s6_addr
))
385 if (p1
->family
== AF_ETHERNET
)
386 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
387 sizeof(struct ethaddr
)))
389 if (p1
->family
== AF_EVPN
)
390 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
391 sizeof(struct evpn_addr
)))
393 if (p1
->family
== AF_FLOWSPEC
) {
394 if (p1
->u
.prefix_flowspec
.family
!=
395 p2
->u
.prefix_flowspec
.family
)
397 if (p1
->u
.prefix_flowspec
.prefixlen
!=
398 p2
->u
.prefix_flowspec
.prefixlen
)
400 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
401 &p2
->u
.prefix_flowspec
.ptr
,
402 p2
->u
.prefix_flowspec
.prefixlen
))
410 * Return -1/0/1 comparing the prefixes in a way that gives a full/linear
413 * Network prefixes are considered the same if the prefix lengths are equal
414 * and the network parts are the same. Host bits (which are considered masked
415 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
416 * 10.0.0.2/8 are considered equivalent by this routine. Note that
417 * this routine has the same return sense as strcmp (which is different
420 int prefix_cmp(union prefixconstptr up1
, union prefixconstptr up2
)
422 const struct prefix
*p1
= up1
.p
;
423 const struct prefix
*p2
= up2
.p
;
428 /* Set both prefix's head pointer. */
432 if (p1
->family
!= p2
->family
)
433 return numcmp(p1
->family
, p2
->family
);
434 if (p1
->family
== AF_FLOWSPEC
) {
435 pp1
= (const uint8_t *)p1
->u
.prefix_flowspec
.ptr
;
436 pp2
= (const uint8_t *)p2
->u
.prefix_flowspec
.ptr
;
438 if (p1
->u
.prefix_flowspec
.family
!=
439 p2
->u
.prefix_flowspec
.family
)
442 if (p1
->u
.prefix_flowspec
.prefixlen
!=
443 p2
->u
.prefix_flowspec
.prefixlen
)
444 return numcmp(p1
->u
.prefix_flowspec
.prefixlen
,
445 p2
->u
.prefix_flowspec
.prefixlen
);
447 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
449 if (pp1
[offset
] != pp2
[offset
])
450 return numcmp(pp1
[offset
], pp2
[offset
]);
456 if (p1
->prefixlen
!= p2
->prefixlen
)
457 return numcmp(p1
->prefixlen
, p2
->prefixlen
);
458 offset
= p1
->prefixlen
/ PNBBY
;
459 shift
= p1
->prefixlen
% PNBBY
;
461 i
= memcmp(pp1
, pp2
, offset
);
466 * At this point offset was the same, if we have shift
467 * that means we still have data to compare, if shift is
468 * 0 then we are at the end of the data structure
469 * and should just return, as that we will be accessing
470 * memory beyond the end of the party zone
473 return numcmp(pp1
[offset
] & maskbit
[shift
],
474 pp2
[offset
] & maskbit
[shift
]);
480 * Count the number of common bits in 2 prefixes. The prefix length is
481 * ignored for this function; the whole prefix is compared. If the prefix
482 * address families don't match, return -1; otherwise the return value is
483 * in range 0 ... maximum prefix length for the address family.
485 int prefix_common_bits(union prefixconstptr ua
, union prefixconstptr ub
)
487 const struct prefix
*p1
= ua
.p
;
488 const struct prefix
*p2
= ub
.p
;
493 /* Set both prefix's head pointer. */
494 const uint8_t *pp1
= p1
->u
.val
;
495 const uint8_t *pp2
= p2
->u
.val
;
497 if (p1
->family
== AF_INET
)
498 length
= IPV4_MAX_BYTELEN
;
499 if (p1
->family
== AF_INET6
)
500 length
= IPV6_MAX_BYTELEN
;
501 if (p1
->family
== AF_ETHERNET
)
503 if (p1
->family
== AF_EVPN
)
504 length
= 8 * sizeof(struct evpn_addr
);
506 if (p1
->family
!= p2
->family
|| !length
)
509 for (pos
= 0; pos
< length
; pos
++)
510 if (pp1
[pos
] != pp2
[pos
])
515 xor = pp1
[pos
] ^ pp2
[pos
];
516 for (bit
= 0; bit
< 8; bit
++)
517 if (xor&(1 << (7 - bit
)))
520 return pos
* 8 + bit
;
523 /* Return prefix family type string. */
524 const char *prefix_family_str(union prefixconstptr pu
)
526 const struct prefix
*p
= pu
.p
;
528 if (p
->family
== AF_INET
)
530 if (p
->family
== AF_INET6
)
532 if (p
->family
== AF_ETHERNET
)
534 if (p
->family
== AF_EVPN
)
539 /* Allocate new prefix_ipv4 structure. */
540 struct prefix_ipv4
*prefix_ipv4_new(void)
542 struct prefix_ipv4
*p
;
544 /* Call prefix_new to allocate a full-size struct prefix to avoid
546 where the struct prefix_ipv4 is cast to struct prefix and unallocated
547 bytes were being referenced (e.g. in structure assignments). */
548 p
= (struct prefix_ipv4
*)prefix_new();
553 /* Free prefix_ipv4 structure. */
554 void prefix_ipv4_free(struct prefix_ipv4
**p
)
556 prefix_free((struct prefix
**)p
);
559 /* If given string is valid return 1 else return 0 */
560 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
567 /* Find slash inside string. */
568 pnt
= strchr(str
, '/');
570 /* String doesn't contail slash. */
572 /* Convert string to prefix. */
573 ret
= inet_pton(AF_INET
, str
, &p
->prefix
);
577 /* If address doesn't contain slash we assume it host address.
580 p
->prefixlen
= IPV4_MAX_BITLEN
;
584 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
585 memcpy(cp
, str
, pnt
- str
);
586 *(cp
+ (pnt
- str
)) = '\0';
587 ret
= inet_pton(AF_INET
, cp
, &p
->prefix
);
588 XFREE(MTYPE_TMP
, cp
);
592 /* Get prefix length. */
593 plen
= (uint8_t)atoi(++pnt
);
594 if (plen
> IPV4_MAX_BITLEN
)
604 /* When string format is invalid return 0. */
605 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
611 const char *str_addr
= str
;
616 if (!strcmp(str
, "any")) {
617 memset(p
, 0, sizeof(*p
));
618 p
->family
= AF_ETHERNET
;
622 /* Find slash inside string. */
623 pnt
= strchr(str
, '/');
626 /* Get prefix length. */
627 plen
= (uint8_t)atoi(++pnt
);
633 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
634 memcpy(cp
, str
, pnt
- str
);
635 *(cp
+ (pnt
- str
)) = '\0';
641 /* Convert string to prefix. */
642 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
648 for (i
= 0; i
< 6; ++i
) {
649 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
652 p
->family
= AF_ETHERNET
;
655 * special case to allow old configurations to work
656 * Since all zero's is implicitly meant to allow
657 * a comparison to zero, let's assume
659 if (!slash
&& is_zero_mac(&(p
->eth_addr
)))
665 XFREE(MTYPE_TMP
, cp
);
670 /* Convert masklen into IP address's netmask (network byte order). */
671 void masklen2ip(const int masklen
, struct in_addr
*netmask
)
673 assert(masklen
>= 0 && masklen
<= IPV4_MAX_BITLEN
);
675 /* left shift is only defined for less than the size of the type.
676 * we unconditionally use long long in case the target platform
677 * has defined behaviour for << 32 (or has a 64-bit left shift) */
679 if (sizeof(unsigned long long) > 4)
680 netmask
->s_addr
= htonl(0xffffffffULL
<< (32 - masklen
));
683 htonl(masklen
? 0xffffffffU
<< (32 - masklen
) : 0);
686 /* Convert IP address's netmask into integer. We assume netmask is
687 * sequential one. Argument netmask should be network byte order. */
688 uint8_t ip_masklen(struct in_addr netmask
)
690 uint32_t tmp
= ~ntohl(netmask
.s_addr
);
693 * clz: count leading zeroes. sadly, the behaviour of this builtin is
694 * undefined for a 0 argument, even though most CPUs give 32
696 return tmp
? __builtin_clz(tmp
) : 32;
699 /* Apply mask to IPv4 prefix (network byte order). */
700 void apply_mask_ipv4(struct prefix_ipv4
*p
)
703 masklen2ip(p
->prefixlen
, &mask
);
704 p
->prefix
.s_addr
&= mask
.s_addr
;
707 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
708 int prefix_ipv4_any(const struct prefix_ipv4
*p
)
710 return (p
->prefix
.s_addr
== INADDR_ANY
&& p
->prefixlen
== 0);
713 /* Allocate a new ip version 6 route */
714 struct prefix_ipv6
*prefix_ipv6_new(void)
716 struct prefix_ipv6
*p
;
718 /* Allocate a full-size struct prefix to avoid problems with structure
720 p
= (struct prefix_ipv6
*)prefix_new();
721 p
->family
= AF_INET6
;
725 /* Free prefix for IPv6. */
726 void prefix_ipv6_free(struct prefix_ipv6
**p
)
728 prefix_free((struct prefix
**)p
);
731 /* If given string is valid return 1 else return 0 */
732 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
738 pnt
= strchr(str
, '/');
740 /* If string doesn't contain `/' treat it as host route. */
742 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
745 p
->prefixlen
= IPV6_MAX_BITLEN
;
749 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
750 memcpy(cp
, str
, pnt
- str
);
751 *(cp
+ (pnt
- str
)) = '\0';
752 ret
= inet_pton(AF_INET6
, cp
, &p
->prefix
);
753 XFREE(MTYPE_TMP
, cp
);
756 plen
= (uint8_t)atoi(++pnt
);
757 if (plen
> IPV6_MAX_BITLEN
)
761 p
->family
= AF_INET6
;
766 /* Convert struct in6_addr netmask into integer.
767 * FIXME return uint8_t as ip_maskleni() does. */
768 int ip6_masklen(struct in6_addr netmask
)
770 if (netmask
.s6_addr32
[0] != 0xffffffffU
)
771 return __builtin_clz(~ntohl(netmask
.s6_addr32
[0]));
772 if (netmask
.s6_addr32
[1] != 0xffffffffU
)
773 return __builtin_clz(~ntohl(netmask
.s6_addr32
[1])) + 32;
774 if (netmask
.s6_addr32
[2] != 0xffffffffU
)
775 return __builtin_clz(~ntohl(netmask
.s6_addr32
[2])) + 64;
776 if (netmask
.s6_addr32
[3] != 0xffffffffU
)
777 return __builtin_clz(~ntohl(netmask
.s6_addr32
[3])) + 96;
778 /* note __builtin_clz(0) is undefined */
782 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
784 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
787 /* note << 32 is undefined */
788 memset(netmask
, 0, sizeof(*netmask
));
789 } else if (masklen
<= 32) {
790 netmask
->s6_addr32
[0] = htonl(0xffffffffU
<< (32 - masklen
));
791 netmask
->s6_addr32
[1] = 0;
792 netmask
->s6_addr32
[2] = 0;
793 netmask
->s6_addr32
[3] = 0;
794 } else if (masklen
<= 64) {
795 netmask
->s6_addr32
[0] = 0xffffffffU
;
796 netmask
->s6_addr32
[1] = htonl(0xffffffffU
<< (64 - masklen
));
797 netmask
->s6_addr32
[2] = 0;
798 netmask
->s6_addr32
[3] = 0;
799 } else if (masklen
<= 96) {
800 netmask
->s6_addr32
[0] = 0xffffffffU
;
801 netmask
->s6_addr32
[1] = 0xffffffffU
;
802 netmask
->s6_addr32
[2] = htonl(0xffffffffU
<< (96 - masklen
));
803 netmask
->s6_addr32
[3] = 0;
805 netmask
->s6_addr32
[0] = 0xffffffffU
;
806 netmask
->s6_addr32
[1] = 0xffffffffU
;
807 netmask
->s6_addr32
[2] = 0xffffffffU
;
808 netmask
->s6_addr32
[3] = htonl(0xffffffffU
<< (128 - masklen
));
812 void apply_mask_ipv6(struct prefix_ipv6
*p
)
818 index
= p
->prefixlen
/ 8;
821 pnt
= (uint8_t *)&p
->prefix
;
822 offset
= p
->prefixlen
% 8;
824 pnt
[index
] &= maskbit
[offset
];
832 void apply_mask(union prefixptr pu
)
834 struct prefix
*p
= pu
.p
;
838 apply_mask_ipv4(pu
.p4
);
841 apply_mask_ipv6(pu
.p6
);
849 /* Utility function of convert between struct prefix <=> union sockunion. */
850 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
851 struct prefix
*prefix
)
853 if (su
->sa
.sa_family
== AF_INET
) {
854 struct prefix_ipv4
*p
;
856 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
858 p
->prefix
= su
->sin
.sin_addr
;
859 p
->prefixlen
= IPV4_MAX_BITLEN
;
860 return (struct prefix
*)p
;
862 if (su
->sa
.sa_family
== AF_INET6
) {
863 struct prefix_ipv6
*p
;
865 p
= prefix
? (struct prefix_ipv6
*)prefix
: prefix_ipv6_new();
866 p
->family
= AF_INET6
;
867 p
->prefixlen
= IPV6_MAX_BITLEN
;
868 memcpy(&p
->prefix
, &su
->sin6
.sin6_addr
,
869 sizeof(struct in6_addr
));
870 return (struct prefix
*)p
;
875 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
877 memset(su
, 0, sizeof(*su
));
879 su
->sa
.sa_family
= p
->family
;
880 if (p
->family
== AF_INET
)
881 su
->sin
.sin_addr
= p
->u
.prefix4
;
882 if (p
->family
== AF_INET6
)
883 memcpy(&su
->sin6
.sin6_addr
, &p
->u
.prefix6
,
884 sizeof(struct in6_addr
));
887 int prefix_blen(union prefixconstptr pu
)
889 const struct prefix
*p
= pu
.p
;
893 return IPV4_MAX_BYTELEN
;
895 return IPV6_MAX_BYTELEN
;
902 /* Generic function for conversion string to struct prefix. */
903 int str2prefix(const char *str
, struct prefix
*p
)
910 /* First we try to convert string to struct prefix_ipv4. */
911 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
915 /* Next we try to convert string to struct prefix_ipv6. */
916 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
920 /* Next we try to convert string to struct prefix_eth. */
921 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
928 static const char *prefixevpn_ead2str(const struct prefix_evpn
*p
, char *str
,
932 char buf
[ESI_STR_LEN
];
933 char buf1
[INET6_ADDRSTRLEN
];
935 family
= IS_IPADDR_V4(&p
->prefix
.ead_addr
.ip
) ? AF_INET
: AF_INET6
;
936 snprintf(str
, size
, "[%d]:[%u]:[%s]:[%d]:[%s]:[%u]",
937 p
->prefix
.route_type
, p
->prefix
.ead_addr
.eth_tag
,
938 esi_to_str(&p
->prefix
.ead_addr
.esi
, buf
, sizeof(buf
)),
939 (family
== AF_INET
) ? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
,
940 inet_ntop(family
, &p
->prefix
.ead_addr
.ip
.ipaddr_v4
, buf1
,
942 p
->prefix
.ead_addr
.frag_id
);
946 static const char *prefixevpn_macip2str(const struct prefix_evpn
*p
, char *str
,
950 char buf1
[ETHER_ADDR_STRLEN
];
951 char buf2
[PREFIX2STR_BUFFER
];
953 if (is_evpn_prefix_ipaddr_none(p
))
954 snprintf(str
, size
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
955 p
->prefix
.macip_addr
.eth_tag
, 8 * ETH_ALEN
,
956 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
959 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
960 snprintf(str
, size
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
961 p
->prefix
.route_type
, p
->prefix
.macip_addr
.eth_tag
,
963 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
965 family
== AF_INET
? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
,
966 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
967 buf2
, PREFIX2STR_BUFFER
));
972 static const char *prefixevpn_imet2str(const struct prefix_evpn
*p
, char *str
,
976 char buf
[INET6_ADDRSTRLEN
];
978 family
= IS_IPADDR_V4(&p
->prefix
.imet_addr
.ip
) ? AF_INET
: AF_INET6
;
979 snprintf(str
, size
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
980 p
->prefix
.imet_addr
.eth_tag
,
981 (family
== AF_INET
) ? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
,
982 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
, buf
,
988 static const char *prefixevpn_es2str(const struct prefix_evpn
*p
, char *str
,
992 char buf
[ESI_STR_LEN
];
993 char buf1
[INET6_ADDRSTRLEN
];
995 family
= IS_IPADDR_V4(&p
->prefix
.es_addr
.ip
) ? AF_INET
: AF_INET6
;
996 snprintf(str
, size
, "[%d]:[%s]:[%d]:[%s]", p
->prefix
.route_type
,
997 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
998 (family
== AF_INET
) ? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
,
999 inet_ntop(family
, &p
->prefix
.es_addr
.ip
.ipaddr_v4
, buf1
,
1005 static const char *prefixevpn_prefix2str(const struct prefix_evpn
*p
, char *str
,
1009 char buf
[INET6_ADDRSTRLEN
];
1011 family
= IS_IPADDR_V4(&p
->prefix
.prefix_addr
.ip
) ? AF_INET
: AF_INET6
;
1012 snprintf(str
, size
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
1013 p
->prefix
.prefix_addr
.eth_tag
,
1014 p
->prefix
.prefix_addr
.ip_prefix_length
,
1015 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ipaddr_v4
, buf
,
1020 static const char *prefixevpn2str(const struct prefix_evpn
*p
, char *str
,
1023 switch (p
->prefix
.route_type
) {
1024 case BGP_EVPN_AD_ROUTE
:
1025 return prefixevpn_ead2str(p
, str
, size
);
1026 case BGP_EVPN_MAC_IP_ROUTE
:
1027 return prefixevpn_macip2str(p
, str
, size
);
1028 case BGP_EVPN_IMET_ROUTE
:
1029 return prefixevpn_imet2str(p
, str
, size
);
1030 case BGP_EVPN_ES_ROUTE
:
1031 return prefixevpn_es2str(p
, str
, size
);
1032 case BGP_EVPN_IP_PREFIX_ROUTE
:
1033 return prefixevpn_prefix2str(p
, str
, size
);
1035 snprintf(str
, size
, "Unsupported EVPN prefix");
1041 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
1043 const struct prefix
*p
= pu
.p
;
1044 char buf
[PREFIX2STR_BUFFER
];
1045 int byte
, tmp
, a
, b
;
1049 switch (p
->family
) {
1052 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
));
1055 byte
= p
->prefixlen
;
1056 tmp
= p
->prefixlen
- 100;
1068 strlcpy(str
, buf
, size
);
1072 snprintf(str
, size
, "%s/%d",
1073 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1078 prefixevpn2str((const struct prefix_evpn
*)p
, str
, size
);
1082 strlcpy(str
, "FS prefix", size
);
1086 strlcpy(str
, "UNK prefix", size
);
1093 static ssize_t
prefixhost2str(struct fbuf
*fbuf
, union prefixconstptr pu
)
1095 const struct prefix
*p
= pu
.p
;
1096 char buf
[PREFIX2STR_BUFFER
];
1098 switch (p
->family
) {
1101 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
));
1102 return bputs(fbuf
, buf
);
1105 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
));
1106 return bputs(fbuf
, buf
);
1109 return bprintfrr(fbuf
, "{prefix.af=%dPF}", p
->family
);
1113 void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
1114 char *buf
, int buf_size
)
1116 int save_errno
= errno
;
1118 if (addr
.s_addr
== INADDR_ANY
)
1119 strlcpy(buf
, "*", buf_size
);
1121 if (!inet_ntop(AF_INET
, &addr
, buf
, buf_size
)) {
1123 snprintf(buf
, buf_size
, "%s", onfail
);
1130 const char *prefix_sg2str(const struct prefix_sg
*sg
, char *sg_str
)
1132 char src_str
[INET_ADDRSTRLEN
];
1133 char grp_str
[INET_ADDRSTRLEN
];
1135 prefix_mcast_inet4_dump("<src?>", sg
->src
, src_str
, sizeof(src_str
));
1136 prefix_mcast_inet4_dump("<grp?>", sg
->grp
, grp_str
, sizeof(grp_str
));
1137 snprintf(sg_str
, PREFIX_SG_STR_LEN
, "(%s,%s)", src_str
, grp_str
);
1142 struct prefix
*prefix_new(void)
1146 p
= XCALLOC(MTYPE_PREFIX
, sizeof(*p
));
1150 void prefix_free_lists(void *arg
)
1152 struct prefix
*p
= arg
;
1157 /* Free prefix structure. */
1158 void prefix_free(struct prefix
**p
)
1160 XFREE(MTYPE_PREFIX
, *p
);
1163 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1164 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1167 uint32_t destination
;
1169 destination
= ntohl(p
->prefix
.s_addr
);
1171 if (p
->prefixlen
== IPV4_MAX_BITLEN
)
1173 /* do nothing for host routes */
1174 else if (IN_CLASSC(destination
)) {
1177 } else if (IN_CLASSB(destination
)) {
1186 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1188 struct in_addr mask
;
1190 masklen2ip(masklen
, &mask
);
1191 return (masklen
!= IPV4_MAX_BITLEN
- 1)
1194 (hostaddr
| ~mask
.s_addr
)
1196 /* For prefix 31 return 255.255.255.255 (RFC3021) */
1200 /* Utility function to convert ipv4 netmask to prefixes
1201 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1202 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1203 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1204 char *prefix_str
, size_t prefix_str_len
)
1206 struct in_addr network
;
1207 struct in_addr mask
;
1209 uint32_t destination
;
1212 ret
= inet_aton(net_str
, &network
);
1217 ret
= inet_aton(mask_str
, &mask
);
1221 prefixlen
= ip_masklen(mask
);
1223 destination
= ntohl(network
.s_addr
);
1225 if (network
.s_addr
== INADDR_ANY
)
1227 else if (IN_CLASSC(destination
))
1229 else if (IN_CLASSB(destination
))
1231 else if (IN_CLASSA(destination
))
1237 snprintf(prefix_str
, prefix_str_len
, "%s/%d", net_str
, prefixlen
);
1242 /* converts to internal representation of mac address
1243 * returns 1 on success, 0 otherwise
1244 * format accepted: AA:BB:CC:DD:EE:FF
1245 * if mac parameter is null, then check only
1247 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1255 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1258 /* error in incoming str length */
1261 /* valid mac address */
1264 for (i
= 0; i
< 6; ++i
)
1265 mac
->octet
[i
] = a
[i
] & 0xff;
1269 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1276 ptr
= XMALLOC(MTYPE_TMP
, ETHER_ADDR_STRLEN
* sizeof(char));
1278 assert(size
>= ETHER_ADDR_STRLEN
);
1281 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1282 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1283 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1284 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1288 unsigned prefix_hash_key(const void *pp
)
1292 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1296 /* make sure *all* unused bits are zero,
1297 * particularly including alignment /
1298 * padding and unused prefix bytes.
1300 memset(©
, 0, sizeof(copy
));
1301 prefix_copy(©
, (struct prefix
*)pp
);
1302 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1303 copy
.u
.prefix_flowspec
.prefixlen
,
1305 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1306 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1307 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1310 /* make sure *all* unused bits are zero, particularly including
1312 * padding and unused prefix bytes. */
1313 memset(©
, 0, sizeof(copy
));
1314 prefix_copy(©
, (struct prefix
*)pp
);
1316 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
1320 /* converts to internal representation of esi
1321 * returns 1 on success, 0 otherwise
1322 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1323 * if esi parameter is null, then check only
1325 int str_to_esi(const char *str
, esi_t
*esi
)
1328 unsigned int a
[ESI_BYTES
];
1333 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1334 a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1335 a
+ 4, a
+ 5, a
+ 6, a
+ 7,
1338 /* error in incoming str length */
1345 for (i
= 0; i
< ESI_BYTES
; ++i
)
1346 esi
->val
[i
] = a
[i
] & 0xff;
1350 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1357 ptr
= XMALLOC(MTYPE_TMP
, ESI_STR_LEN
* sizeof(char));
1359 assert(size
>= ESI_STR_LEN
);
1363 snprintf(ptr
, ESI_STR_LEN
,
1364 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1365 esi
->val
[0], esi
->val
[1], esi
->val
[2],
1366 esi
->val
[3], esi
->val
[4], esi
->val
[5],
1367 esi
->val
[6], esi
->val
[7], esi
->val
[8],
1372 char *evpn_es_df_alg2str(uint8_t df_alg
, char *buf
, int buf_len
)
1375 case EVPN_MH_DF_ALG_SERVICE_CARVING
:
1376 snprintf(buf
, buf_len
, "service-carving");
1379 case EVPN_MH_DF_ALG_HRW
:
1380 snprintf(buf
, buf_len
, "HRW");
1383 case EVPN_MH_DF_ALG_PREF
:
1384 snprintf(buf
, buf_len
, "preference");
1388 snprintf(buf
, buf_len
, "unknown %u", df_alg
);
1395 bool ipv4_unicast_valid(const struct in_addr
*addr
)
1397 in_addr_t ip
= ntohl(addr
->s_addr
);
1399 if (IPV4_CLASS_D(ip
))
1402 if (IPV4_CLASS_E(ip
)) {
1403 if (cmd_allow_reserved_ranges_get())
1412 static int ipaddr2prefix(const struct ipaddr
*ip
, uint16_t prefixlen
,
1415 switch (ip
->ipa_type
) {
1417 p
->family
= AF_INET
;
1418 p
->u
.prefix4
= ip
->ipaddr_v4
;
1419 p
->prefixlen
= prefixlen
;
1422 p
->family
= AF_INET6
;
1423 p
->u
.prefix6
= ip
->ipaddr_v6
;
1424 p
->prefixlen
= prefixlen
;
1427 p
->family
= AF_UNSPEC
;
1435 * Convert type-2 and type-5 evpn route prefixes into the more
1436 * general ipv4/ipv6 prefix types so we can match prefix lists
1439 int evpn_prefix2prefix(const struct prefix
*evpn
, struct prefix
*to
)
1441 const struct evpn_addr
*addr
;
1443 if (evpn
->family
!= AF_EVPN
)
1446 addr
= &evpn
->u
.prefix_evpn
;
1448 switch (addr
->route_type
) {
1449 case BGP_EVPN_MAC_IP_ROUTE
:
1450 if (IS_IPADDR_V4(&addr
->macip_addr
.ip
))
1451 ipaddr2prefix(&addr
->macip_addr
.ip
, IPV4_MAX_BITLEN
,
1453 else if (IS_IPADDR_V6(&addr
->macip_addr
.ip
))
1454 ipaddr2prefix(&addr
->macip_addr
.ip
, IPV6_MAX_BITLEN
,
1457 return -1; /* mac only? */
1460 case BGP_EVPN_IP_PREFIX_ROUTE
:
1461 ipaddr2prefix(&addr
->prefix_addr
.ip
,
1462 addr
->prefix_addr
.ip_prefix_length
, to
);
1471 printfrr_ext_autoreg_p("EA", printfrr_ea
);
1472 static ssize_t
printfrr_ea(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1475 const struct ethaddr
*mac
= ptr
;
1476 char cbuf
[ETHER_ADDR_STRLEN
];
1479 return bputs(buf
, "(null)");
1481 /* need real length even if buffer is too short */
1482 prefix_mac2str(mac
, cbuf
, sizeof(cbuf
));
1483 return bputs(buf
, cbuf
);
1486 printfrr_ext_autoreg_p("IA", printfrr_ia
);
1487 static ssize_t
printfrr_ia(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1490 const struct ipaddr
*ipa
= ptr
;
1491 char cbuf
[INET6_ADDRSTRLEN
];
1492 bool use_star
= false;
1494 if (ea
->fmt
[0] == 's') {
1499 if (!ipa
|| !ipa
->ipa_type
)
1500 return bputs(buf
, "(null)");
1503 struct in_addr zero4
= {};
1504 struct in6_addr zero6
= {};
1506 switch (ipa
->ipa_type
) {
1508 if (!memcmp(&ipa
->ip
.addr
, &zero4
, sizeof(zero4
)))
1509 return bputch(buf
, '*');
1513 if (!memcmp(&ipa
->ip
.addr
, &zero6
, sizeof(zero6
)))
1514 return bputch(buf
, '*');
1522 ipaddr2str(ipa
, cbuf
, sizeof(cbuf
));
1523 return bputs(buf
, cbuf
);
1526 printfrr_ext_autoreg_p("I4", printfrr_i4
);
1527 static ssize_t
printfrr_i4(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1530 char cbuf
[INET_ADDRSTRLEN
];
1531 bool use_star
= false;
1532 struct in_addr zero
= {};
1534 if (ea
->fmt
[0] == 's') {
1540 return bputs(buf
, "(null)");
1542 if (use_star
&& !memcmp(ptr
, &zero
, sizeof(zero
)))
1543 return bputch(buf
, '*');
1545 inet_ntop(AF_INET
, ptr
, cbuf
, sizeof(cbuf
));
1546 return bputs(buf
, cbuf
);
1549 printfrr_ext_autoreg_p("I6", printfrr_i6
);
1550 static ssize_t
printfrr_i6(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1553 char cbuf
[INET6_ADDRSTRLEN
];
1554 bool use_star
= false;
1555 struct in6_addr zero
= {};
1557 if (ea
->fmt
[0] == 's') {
1563 return bputs(buf
, "(null)");
1565 if (use_star
&& !memcmp(ptr
, &zero
, sizeof(zero
)))
1566 return bputch(buf
, '*');
1568 inet_ntop(AF_INET6
, ptr
, cbuf
, sizeof(cbuf
));
1569 return bputs(buf
, cbuf
);
1572 printfrr_ext_autoreg_p("FX", printfrr_pfx
);
1573 static ssize_t
printfrr_pfx(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1576 bool host_only
= false;
1578 if (ea
->fmt
[0] == 'h') {
1584 return bputs(buf
, "(null)");
1587 return prefixhost2str(buf
, (struct prefix
*)ptr
);
1589 char cbuf
[PREFIX_STRLEN
];
1591 prefix2str(ptr
, cbuf
, sizeof(cbuf
));
1592 return bputs(buf
, cbuf
);
1596 printfrr_ext_autoreg_p("PSG4", printfrr_psg
);
1597 static ssize_t
printfrr_psg(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1600 const struct prefix_sg
*sg
= ptr
;
1604 return bputs(buf
, "(null)");
1606 if (sg
->src
.s_addr
== INADDR_ANY
)
1607 ret
+= bputs(buf
, "(*,");
1609 ret
+= bprintfrr(buf
, "(%pI4,", &sg
->src
);
1611 if (sg
->grp
.s_addr
== INADDR_ANY
)
1612 ret
+= bputs(buf
, "*)");
1614 ret
+= bprintfrr(buf
, "%pI4)", &sg
->grp
);