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(afi_t afi
)
140 assert(!"Reached end of function we should never reach");
143 const char *safi2str(safi_t safi
)
156 case SAFI_LABELED_UNICAST
:
157 return "labeled-unicast";
165 assert(!"Reached end of function we should never reach");
168 /* If n includes p prefix then return 1 else return 0. */
169 int prefix_match(union prefixconstptr unet
, union prefixconstptr upfx
)
171 const struct prefix
*n
= unet
.p
;
172 const struct prefix
*p
= upfx
.p
;
175 const uint8_t *np
, *pp
;
177 /* If n's prefix is longer than p's one return 0. */
178 if (n
->prefixlen
> p
->prefixlen
)
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
)
187 if (n
->u
.prefix_flowspec
.prefixlen
>
188 p
->u
.prefix_flowspec
.prefixlen
)
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
;
195 offset
= n
->u
.prefix_flowspec
.prefixlen
;
198 if (np
[offset
] != pp
[offset
])
203 /* Set both prefix's head pointer. */
207 offset
= n
->prefixlen
/ PNBBY
;
208 shift
= n
->prefixlen
% PNBBY
;
211 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
215 if (np
[offset
] != pp
[offset
])
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.
226 int evpn_type5_prefix_match(const struct prefix
*n
, const struct prefix
*p
)
231 const uint8_t *np
, *pp
;
232 struct prefix_evpn
*evp
;
234 if (n
->family
!= AF_EVPN
)
237 evp
= (struct prefix_evpn
*)n
;
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
)))
246 prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
247 np
= &evp
->prefix
.prefix_addr
.ip
.ip
.addr
;
249 /* If n's prefix is longer than p's one return 0. */
250 if (prefixlen
> p
->prefixlen
)
253 offset
= prefixlen
/ PNBBY
;
254 shift
= prefixlen
% PNBBY
;
257 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
261 if (np
[offset
] != pp
[offset
])
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
)
271 const struct prefix
*n
= unet
.p
;
272 const struct prefix
*p
= upfx
.p
;
275 const uint8_t *np
, *pp
;
277 /* Set both prefix's head pointer. */
281 offset
= n
->prefixlen
/ PNBBY
;
282 shift
= n
->prefixlen
% PNBBY
;
285 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
289 if (np
[offset
] != pp
[offset
])
294 #ifdef __clang_analyzer__
295 #undef prefix_copy /* cf. prefix.h */
298 void prefix_copy(union prefixptr udest
, union prefixconstptr usrc
)
300 struct prefix
*dest
= udest
.p
;
301 const struct prefix
*src
= usrc
.p
;
303 dest
->family
= src
->family
;
304 dest
->prefixlen
= src
->prefixlen
;
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
) {
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
);
334 flog_err(EC_LIB_DEVELOPMENT
,
335 "prefix_copy(): Unknown address family %d",
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).
349 int prefix_same(union prefixconstptr up1
, union prefixconstptr up2
)
351 const struct prefix
*p1
= up1
.p
;
352 const struct prefix
*p2
= up2
.p
;
354 if ((p1
&& !p2
) || (!p1
&& p2
))
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
))
364 if (p1
->family
== AF_INET6
)
365 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
366 &p2
->u
.prefix6
.s6_addr
))
368 if (p1
->family
== AF_ETHERNET
)
369 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
370 sizeof(struct ethaddr
)))
372 if (p1
->family
== AF_EVPN
)
373 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
374 sizeof(struct evpn_addr
)))
376 if (p1
->family
== AF_FLOWSPEC
) {
377 if (p1
->u
.prefix_flowspec
.family
!=
378 p2
->u
.prefix_flowspec
.family
)
380 if (p1
->u
.prefix_flowspec
.prefixlen
!=
381 p2
->u
.prefix_flowspec
.prefixlen
)
383 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
384 &p2
->u
.prefix_flowspec
.ptr
,
385 p2
->u
.prefix_flowspec
.prefixlen
))
393 * Return -1/0/1 comparing the prefixes in a way that gives a full/linear
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
403 int prefix_cmp(union prefixconstptr up1
, union prefixconstptr up2
)
405 const struct prefix
*p1
= up1
.p
;
406 const struct prefix
*p2
= up2
.p
;
411 /* Set both prefix's head pointer. */
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
;
421 if (p1
->u
.prefix_flowspec
.family
!=
422 p2
->u
.prefix_flowspec
.family
)
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
);
430 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
432 if (pp1
[offset
] != pp2
[offset
])
433 return numcmp(pp1
[offset
], pp2
[offset
]);
439 if (p1
->prefixlen
!= p2
->prefixlen
)
440 return numcmp(p1
->prefixlen
, p2
->prefixlen
);
441 offset
= p1
->prefixlen
/ PNBBY
;
442 shift
= p1
->prefixlen
% PNBBY
;
444 i
= memcmp(pp1
, pp2
, offset
);
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
456 return numcmp(pp1
[offset
] & maskbit
[shift
],
457 pp2
[offset
] & maskbit
[shift
]);
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.
468 int prefix_common_bits(union prefixconstptr ua
, union prefixconstptr ub
)
470 const struct prefix
*p1
= ua
.p
;
471 const struct prefix
*p2
= ub
.p
;
476 /* Set both prefix's head pointer. */
477 const uint8_t *pp1
= p1
->u
.val
;
478 const uint8_t *pp2
= p2
->u
.val
;
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
)
486 if (p1
->family
== AF_EVPN
)
487 length
= 8 * sizeof(struct evpn_addr
);
489 if (p1
->family
!= p2
->family
|| !length
)
492 for (pos
= 0; pos
< length
; pos
++)
493 if (pp1
[pos
] != pp2
[pos
])
498 xor = pp1
[pos
] ^ pp2
[pos
];
499 for (bit
= 0; bit
< 8; bit
++)
500 if (xor&(1 << (7 - bit
)))
503 return pos
* 8 + bit
;
506 /* Return prefix family type string. */
507 const char *prefix_family_str(union prefixconstptr pu
)
509 const struct prefix
*p
= pu
.p
;
511 if (p
->family
== AF_INET
)
513 if (p
->family
== AF_INET6
)
515 if (p
->family
== AF_ETHERNET
)
517 if (p
->family
== AF_EVPN
)
522 /* Allocate new prefix_ipv4 structure. */
523 struct prefix_ipv4
*prefix_ipv4_new(void)
525 struct prefix_ipv4
*p
;
527 /* Call prefix_new to allocate a full-size struct prefix to avoid
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();
536 /* Free prefix_ipv4 structure. */
537 void prefix_ipv4_free(struct prefix_ipv4
**p
)
539 prefix_free((struct prefix
**)p
);
542 /* If given string is valid return 1 else return 0 */
543 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
550 /* Find slash inside string. */
551 pnt
= strchr(str
, '/');
553 /* String doesn't contail slash. */
555 /* Convert string to prefix. */
556 ret
= inet_pton(AF_INET
, str
, &p
->prefix
);
560 /* If address doesn't contain slash we assume it host address.
563 p
->prefixlen
= IPV4_MAX_BITLEN
;
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
);
575 /* Get prefix length. */
576 plen
= (uint8_t)atoi(++pnt
);
577 if (plen
> IPV4_MAX_BITLEN
)
587 /* When string format is invalid return 0. */
588 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
594 const char *str_addr
= str
;
599 if (!strcmp(str
, "any")) {
600 memset(p
, 0, sizeof(*p
));
601 p
->family
= AF_ETHERNET
;
605 /* Find slash inside string. */
606 pnt
= strchr(str
, '/');
609 /* Get prefix length. */
610 plen
= (uint8_t)atoi(++pnt
);
616 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
617 memcpy(cp
, str
, pnt
- str
);
618 *(cp
+ (pnt
- str
)) = '\0';
624 /* Convert string to prefix. */
625 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
631 for (i
= 0; i
< 6; ++i
) {
632 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
635 p
->family
= AF_ETHERNET
;
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
642 if (!slash
&& is_zero_mac(&(p
->eth_addr
)))
648 XFREE(MTYPE_TMP
, cp
);
653 /* Convert masklen into IP address's netmask (network byte order). */
654 void masklen2ip(const int masklen
, struct in_addr
*netmask
)
656 assert(masklen
>= 0 && masklen
<= IPV4_MAX_BITLEN
);
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) */
662 if (sizeof(unsigned long long) > 4)
663 netmask
->s_addr
= htonl(0xffffffffULL
<< (32 - masklen
));
666 htonl(masklen
? 0xffffffffU
<< (32 - masklen
) : 0);
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
)
673 uint32_t tmp
= ~ntohl(netmask
.s_addr
);
676 * clz: count leading zeroes. sadly, the behaviour of this builtin is
677 * undefined for a 0 argument, even though most CPUs give 32
679 return tmp
? __builtin_clz(tmp
) : 32;
682 /* Apply mask to IPv4 prefix (network byte order). */
683 void apply_mask_ipv4(struct prefix_ipv4
*p
)
686 masklen2ip(p
->prefixlen
, &mask
);
687 p
->prefix
.s_addr
&= mask
.s_addr
;
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
)
693 return (p
->prefix
.s_addr
== INADDR_ANY
&& p
->prefixlen
== 0);
696 /* Allocate a new ip version 6 route */
697 struct prefix_ipv6
*prefix_ipv6_new(void)
699 struct prefix_ipv6
*p
;
701 /* Allocate a full-size struct prefix to avoid problems with structure
703 p
= (struct prefix_ipv6
*)prefix_new();
704 p
->family
= AF_INET6
;
708 /* Free prefix for IPv6. */
709 void prefix_ipv6_free(struct prefix_ipv6
**p
)
711 prefix_free((struct prefix
**)p
);
714 /* If given string is valid return 1 else return 0 */
715 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
721 pnt
= strchr(str
, '/');
723 /* If string doesn't contain `/' treat it as host route. */
725 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
728 p
->prefixlen
= IPV6_MAX_BITLEN
;
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
);
739 plen
= (uint8_t)atoi(++pnt
);
740 if (plen
> IPV6_MAX_BITLEN
)
744 p
->family
= AF_INET6
;
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
)
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 */
765 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
767 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
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;
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
));
795 void apply_mask_ipv6(struct prefix_ipv6
*p
)
801 index
= p
->prefixlen
/ 8;
804 pnt
= (uint8_t *)&p
->prefix
;
805 offset
= p
->prefixlen
% 8;
807 pnt
[index
] &= maskbit
[offset
];
815 void apply_mask(union prefixptr pu
)
817 struct prefix
*p
= pu
.p
;
821 apply_mask_ipv4(pu
.p4
);
824 apply_mask_ipv6(pu
.p6
);
832 /* Utility function of convert between struct prefix <=> union sockunion. */
833 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
834 struct prefix
*prefix
)
836 if (su
->sa
.sa_family
== AF_INET
) {
837 struct prefix_ipv4
*p
;
839 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
841 p
->prefix
= su
->sin
.sin_addr
;
842 p
->prefixlen
= IPV4_MAX_BITLEN
;
843 return (struct prefix
*)p
;
845 if (su
->sa
.sa_family
== AF_INET6
) {
846 struct prefix_ipv6
*p
;
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
;
858 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
860 memset(su
, 0, sizeof(*su
));
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
));
870 int prefix_blen(union prefixconstptr pu
)
872 const struct prefix
*p
= pu
.p
;
876 return IPV4_MAX_BYTELEN
;
878 return IPV6_MAX_BYTELEN
;
885 /* Generic function for conversion string to struct prefix. */
886 int str2prefix(const char *str
, struct prefix
*p
)
893 /* First we try to convert string to struct prefix_ipv4. */
894 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
898 /* Next we try to convert string to struct prefix_ipv6. */
899 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
903 /* Next we try to convert string to struct prefix_eth. */
904 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
911 static const char *prefixevpn_ead2str(const struct prefix_evpn
*p
, char *str
,
915 char buf
[ESI_STR_LEN
];
916 char buf1
[INET6_ADDRSTRLEN
];
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
,
925 p
->prefix
.ead_addr
.frag_id
);
929 static const char *prefixevpn_macip2str(const struct prefix_evpn
*p
, char *str
,
933 char buf1
[ETHER_ADDR_STRLEN
];
934 char buf2
[PREFIX2STR_BUFFER
];
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
,
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
,
946 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, 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
));
955 static const char *prefixevpn_imet2str(const struct prefix_evpn
*p
, char *str
,
959 char buf
[INET6_ADDRSTRLEN
];
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
,
971 static const char *prefixevpn_es2str(const struct prefix_evpn
*p
, char *str
,
975 char buf
[ESI_STR_LEN
];
976 char buf1
[INET6_ADDRSTRLEN
];
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
,
988 static const char *prefixevpn_prefix2str(const struct prefix_evpn
*p
, char *str
,
992 char buf
[INET6_ADDRSTRLEN
];
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
,
1003 static const char *prefixevpn2str(const struct prefix_evpn
*p
, char *str
,
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
);
1018 snprintf(str
, size
, "Unsupported EVPN prefix");
1024 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
1026 const struct prefix
*p
= pu
.p
;
1027 char buf
[PREFIX2STR_BUFFER
];
1028 int byte
, tmp
, a
, b
;
1032 switch (p
->family
) {
1035 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
));
1038 byte
= p
->prefixlen
;
1039 tmp
= p
->prefixlen
- 100;
1051 strlcpy(str
, buf
, size
);
1055 snprintf(str
, size
, "%s/%d",
1056 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1061 prefixevpn2str((const struct prefix_evpn
*)p
, str
, size
);
1065 strlcpy(str
, "FS prefix", size
);
1069 strlcpy(str
, "UNK prefix", size
);
1076 static ssize_t
prefixhost2str(struct fbuf
*fbuf
, union prefixconstptr pu
)
1078 const struct prefix
*p
= pu
.p
;
1079 char buf
[PREFIX2STR_BUFFER
];
1081 switch (p
->family
) {
1084 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
));
1085 return bputs(fbuf
, buf
);
1088 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
));
1089 return bputs(fbuf
, buf
);
1092 return bprintfrr(fbuf
, "{prefix.af=%dPF}", p
->family
);
1096 void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
1097 char *buf
, int buf_size
)
1099 int save_errno
= errno
;
1101 if (addr
.s_addr
== INADDR_ANY
)
1102 strlcpy(buf
, "*", buf_size
);
1104 if (!inet_ntop(AF_INET
, &addr
, buf
, buf_size
)) {
1106 snprintf(buf
, buf_size
, "%s", onfail
);
1113 const char *prefix_sg2str(const struct prefix_sg
*sg
, char *sg_str
)
1115 char src_str
[INET_ADDRSTRLEN
];
1116 char grp_str
[INET_ADDRSTRLEN
];
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
);
1125 struct prefix
*prefix_new(void)
1129 p
= XCALLOC(MTYPE_PREFIX
, sizeof(*p
));
1133 void prefix_free_lists(void *arg
)
1135 struct prefix
*p
= arg
;
1140 /* Free prefix structure. */
1141 void prefix_free(struct prefix
**p
)
1143 XFREE(MTYPE_PREFIX
, *p
);
1146 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1147 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1150 uint32_t destination
;
1152 destination
= ntohl(p
->prefix
.s_addr
);
1154 if (p
->prefixlen
== IPV4_MAX_BITLEN
)
1156 /* do nothing for host routes */
1157 else if (IN_CLASSC(destination
)) {
1160 } else if (IN_CLASSB(destination
)) {
1169 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1171 struct in_addr mask
;
1173 masklen2ip(masklen
, &mask
);
1174 return (masklen
!= IPV4_MAX_BITLEN
- 1)
1177 (hostaddr
| ~mask
.s_addr
)
1179 /* For prefix 31 return 255.255.255.255 (RFC3021) */
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
)
1189 struct in_addr network
;
1190 struct in_addr mask
;
1192 uint32_t destination
;
1195 ret
= inet_aton(net_str
, &network
);
1200 ret
= inet_aton(mask_str
, &mask
);
1204 prefixlen
= ip_masklen(mask
);
1206 destination
= ntohl(network
.s_addr
);
1208 if (network
.s_addr
== INADDR_ANY
)
1210 else if (IN_CLASSC(destination
))
1212 else if (IN_CLASSB(destination
))
1214 else if (IN_CLASSA(destination
))
1220 snprintf(prefix_str
, prefix_str_len
, "%s/%d", net_str
, prefixlen
);
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
1230 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1238 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1241 /* error in incoming str length */
1244 /* valid mac address */
1247 for (i
= 0; i
< 6; ++i
)
1248 mac
->octet
[i
] = a
[i
] & 0xff;
1252 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1259 ptr
= XMALLOC(MTYPE_TMP
, ETHER_ADDR_STRLEN
* sizeof(char));
1261 assert(size
>= ETHER_ADDR_STRLEN
);
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]);
1271 unsigned prefix_hash_key(const void *pp
)
1275 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1279 /* make sure *all* unused bits are zero,
1280 * particularly including alignment /
1281 * padding and unused prefix bytes.
1283 memset(©
, 0, sizeof(copy
));
1284 prefix_copy(©
, (struct prefix
*)pp
);
1285 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1286 copy
.u
.prefix_flowspec
.prefixlen
,
1288 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1289 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1290 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1293 /* make sure *all* unused bits are zero, particularly including
1295 * padding and unused prefix bytes. */
1296 memset(©
, 0, sizeof(copy
));
1297 prefix_copy(©
, (struct prefix
*)pp
);
1299 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
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
1308 int str_to_esi(const char *str
, esi_t
*esi
)
1311 unsigned int a
[ESI_BYTES
];
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,
1321 /* error in incoming str length */
1328 for (i
= 0; i
< ESI_BYTES
; ++i
)
1329 esi
->val
[i
] = a
[i
] & 0xff;
1333 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1340 ptr
= XMALLOC(MTYPE_TMP
, ESI_STR_LEN
* sizeof(char));
1342 assert(size
>= ESI_STR_LEN
);
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],
1355 char *evpn_es_df_alg2str(uint8_t df_alg
, char *buf
, int buf_len
)
1358 case EVPN_MH_DF_ALG_SERVICE_CARVING
:
1359 snprintf(buf
, buf_len
, "service-carving");
1362 case EVPN_MH_DF_ALG_HRW
:
1363 snprintf(buf
, buf_len
, "HRW");
1366 case EVPN_MH_DF_ALG_PREF
:
1367 snprintf(buf
, buf_len
, "preference");
1371 snprintf(buf
, buf_len
, "unknown %u", df_alg
);
1378 bool ipv4_unicast_valid(const struct in_addr
*addr
)
1380 in_addr_t ip
= ntohl(addr
->s_addr
);
1382 if (IPV4_CLASS_D(ip
))
1385 if (IPV4_CLASS_E(ip
)) {
1386 if (cmd_allow_reserved_ranges_get())
1395 static int ipaddr2prefix(const struct ipaddr
*ip
, uint16_t prefixlen
,
1398 switch (ip
->ipa_type
) {
1400 p
->family
= AF_INET
;
1401 p
->u
.prefix4
= ip
->ipaddr_v4
;
1402 p
->prefixlen
= prefixlen
;
1405 p
->family
= AF_INET6
;
1406 p
->u
.prefix6
= ip
->ipaddr_v6
;
1407 p
->prefixlen
= prefixlen
;
1410 p
->family
= AF_UNSPEC
;
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
1422 int evpn_prefix2prefix(const struct prefix
*evpn
, struct prefix
*to
)
1424 const struct evpn_addr
*addr
;
1426 if (evpn
->family
!= AF_EVPN
)
1429 addr
= &evpn
->u
.prefix_evpn
;
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
);
1438 return -1; /* mac only? */
1441 case BGP_EVPN_IP_PREFIX_ROUTE
:
1442 ipaddr2prefix(&addr
->prefix_addr
.ip
,
1443 addr
->prefix_addr
.ip_prefix_length
, to
);
1452 printfrr_ext_autoreg_p("EA", printfrr_ea
);
1453 static ssize_t
printfrr_ea(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1456 const struct ethaddr
*mac
= ptr
;
1457 char cbuf
[ETHER_ADDR_STRLEN
];
1460 return bputs(buf
, "(null)");
1462 /* need real length even if buffer is too short */
1463 prefix_mac2str(mac
, cbuf
, sizeof(cbuf
));
1464 return bputs(buf
, cbuf
);
1467 printfrr_ext_autoreg_p("IA", printfrr_ia
);
1468 static ssize_t
printfrr_ia(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1471 const struct ipaddr
*ipa
= ptr
;
1472 char cbuf
[INET6_ADDRSTRLEN
];
1473 bool use_star
= false;
1475 if (ea
->fmt
[0] == 's') {
1480 if (!ipa
|| !ipa
->ipa_type
)
1481 return bputs(buf
, "(null)");
1484 struct in_addr zero4
= {};
1485 struct in6_addr zero6
= {};
1487 switch (ipa
->ipa_type
) {
1489 if (!memcmp(&ipa
->ip
.addr
, &zero4
, sizeof(zero4
)))
1490 return bputch(buf
, '*');
1494 if (!memcmp(&ipa
->ip
.addr
, &zero6
, sizeof(zero6
)))
1495 return bputch(buf
, '*');
1503 ipaddr2str(ipa
, cbuf
, sizeof(cbuf
));
1504 return bputs(buf
, cbuf
);
1507 printfrr_ext_autoreg_p("I4", printfrr_i4
);
1508 static ssize_t
printfrr_i4(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1511 char cbuf
[INET_ADDRSTRLEN
];
1512 bool use_star
= false;
1513 struct in_addr zero
= {};
1515 if (ea
->fmt
[0] == 's') {
1521 return bputs(buf
, "(null)");
1523 if (use_star
&& !memcmp(ptr
, &zero
, sizeof(zero
)))
1524 return bputch(buf
, '*');
1526 inet_ntop(AF_INET
, ptr
, cbuf
, sizeof(cbuf
));
1527 return bputs(buf
, cbuf
);
1530 printfrr_ext_autoreg_p("I6", printfrr_i6
);
1531 static ssize_t
printfrr_i6(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1534 char cbuf
[INET6_ADDRSTRLEN
];
1535 bool use_star
= false;
1536 struct in6_addr zero
= {};
1538 if (ea
->fmt
[0] == 's') {
1544 return bputs(buf
, "(null)");
1546 if (use_star
&& !memcmp(ptr
, &zero
, sizeof(zero
)))
1547 return bputch(buf
, '*');
1549 inet_ntop(AF_INET6
, ptr
, cbuf
, sizeof(cbuf
));
1550 return bputs(buf
, cbuf
);
1553 printfrr_ext_autoreg_p("FX", printfrr_pfx
);
1554 static ssize_t
printfrr_pfx(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1557 bool host_only
= false;
1559 if (ea
->fmt
[0] == 'h') {
1565 return bputs(buf
, "(null)");
1568 return prefixhost2str(buf
, (struct prefix
*)ptr
);
1570 char cbuf
[PREFIX_STRLEN
];
1572 prefix2str(ptr
, cbuf
, sizeof(cbuf
));
1573 return bputs(buf
, cbuf
);
1577 printfrr_ext_autoreg_p("PSG4", printfrr_psg
);
1578 static ssize_t
printfrr_psg(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
1581 const struct prefix_sg
*sg
= ptr
;
1585 return bputs(buf
, "(null)");
1587 if (sg
->src
.s_addr
== INADDR_ANY
)
1588 ret
+= bputs(buf
, "(*,");
1590 ret
+= bprintfrr(buf
, "(%pI4,", &sg
->src
);
1592 if (sg
->grp
.s_addr
== INADDR_ANY
)
1593 ret
+= bputs(buf
, "*)");
1595 ret
+= bprintfrr(buf
, "%pI4)", &sg
->grp
);