]>
git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
2 * Prefix related functions.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "sockunion.h"
30 #include "lib_errors.h"
33 DEFINE_MTYPE_STATIC(LIB
, PREFIX
, "Prefix")
34 DEFINE_MTYPE_STATIC(LIB
, PREFIX_FLOWSPEC
, "Prefix Flowspec")
37 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
38 0xf8, 0xfc, 0xfe, 0xff};
40 /* Number of bits in prefix type. */
45 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
47 int is_zero_mac(const struct ethaddr
*mac
)
51 for (i
= 0; i
< ETH_ALEN
; i
++) {
59 unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t prefixlen
)
61 unsigned int offset
= prefixlen
/ 8;
62 unsigned int shift
= 7 - (prefixlen
% 8);
64 return (prefix
[offset
] >> shift
) & 1;
67 int str2family(const char *string
)
69 if (!strcmp("ipv4", string
))
71 else if (!strcmp("ipv6", string
))
73 else if (!strcmp("ethernet", string
))
75 else if (!strcmp("evpn", string
))
80 const char *family2str(int family
)
95 /* Address Famiy Identifier to Address Family converter. */
96 int afi2family(afi_t afi
)
100 else if (afi
== AFI_IP6
)
102 else if (afi
== AFI_L2VPN
)
104 /* NOTE: EVPN code should NOT use this interface. */
108 afi_t
family2afi(int family
)
110 if (family
== AF_INET
)
112 else if (family
== AF_INET6
)
114 else if (family
== AF_ETHERNET
|| family
== AF_EVPN
)
119 const char *afi2str(afi_t afi
)
136 const char *safi2str(safi_t safi
)
149 case SAFI_LABELED_UNICAST
:
150 return "labeled-unicast";
158 /* If n includes p prefix then return 1 else return 0. */
159 int prefix_match(const struct prefix
*n
, const struct prefix
*p
)
163 const uint8_t *np
, *pp
;
165 /* If n's prefix is longer than p's one return 0. */
166 if (n
->prefixlen
> p
->prefixlen
)
169 if (n
->family
== AF_FLOWSPEC
) {
170 /* prefixlen is unused. look at fs prefix len */
171 if (n
->u
.prefix_flowspec
.prefixlen
>
172 p
->u
.prefix_flowspec
.prefixlen
)
175 /* Set both prefix's head pointer. */
176 np
= (const uint8_t *)&n
->u
.prefix_flowspec
.ptr
;
177 pp
= (const uint8_t *)&p
->u
.prefix_flowspec
.ptr
;
179 offset
= n
->u
.prefix_flowspec
.prefixlen
;
182 if (np
[offset
] != pp
[offset
])
187 /* Set both prefix's head pointer. */
191 offset
= n
->prefixlen
/ PNBBY
;
192 shift
= n
->prefixlen
% PNBBY
;
195 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
199 if (np
[offset
] != pp
[offset
])
206 * n is a type5 evpn prefix. This function tries to see if there is an
207 * ip-prefix within n which matches prefix p
208 * If n includes p prefix then return 1 else return 0.
210 int evpn_type5_prefix_match(const struct prefix
*n
, const struct prefix
*p
)
215 const uint8_t *np
, *pp
;
216 struct prefix_evpn
*evp
;
218 if (n
->family
!= AF_EVPN
)
221 evp
= (struct prefix_evpn
*)n
;
224 if ((evp
->prefix
.route_type
!= 5) ||
225 (p
->family
== AF_INET6
&& !is_evpn_prefix_ipaddr_v6(evp
)) ||
226 (p
->family
== AF_INET
&& !is_evpn_prefix_ipaddr_v4(evp
)) ||
227 (is_evpn_prefix_ipaddr_none(evp
)))
230 prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
231 np
= &evp
->prefix
.prefix_addr
.ip
.ip
.addr
;
233 /* If n's prefix is longer than p's one return 0. */
234 if (prefixlen
> p
->prefixlen
)
237 offset
= prefixlen
/ PNBBY
;
238 shift
= prefixlen
% PNBBY
;
241 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
245 if (np
[offset
] != pp
[offset
])
251 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
252 int prefix_match_network_statement(const struct prefix
*n
,
253 const struct prefix
*p
)
257 const uint8_t *np
, *pp
;
259 /* Set both prefix's head pointer. */
263 offset
= n
->prefixlen
/ PNBBY
;
264 shift
= n
->prefixlen
% PNBBY
;
267 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
271 if (np
[offset
] != pp
[offset
])
276 #ifdef __clang_analyzer__
277 #undef prefix_copy /* cf. prefix.h */
280 void prefix_copy(union prefixptr udest
, union prefixconstptr usrc
)
282 struct prefix
*dest
= udest
.p
;
283 const struct prefix
*src
= usrc
.p
;
285 dest
->family
= src
->family
;
286 dest
->prefixlen
= src
->prefixlen
;
288 if (src
->family
== AF_INET
)
289 dest
->u
.prefix4
= src
->u
.prefix4
;
290 else if (src
->family
== AF_INET6
)
291 dest
->u
.prefix6
= src
->u
.prefix6
;
292 else if (src
->family
== AF_ETHERNET
) {
293 memcpy(&dest
->u
.prefix_eth
, &src
->u
.prefix_eth
,
294 sizeof(struct ethaddr
));
295 } else if (src
->family
== AF_EVPN
) {
296 memcpy(&dest
->u
.prefix_evpn
, &src
->u
.prefix_evpn
,
297 sizeof(struct evpn_addr
));
298 } else if (src
->family
== AF_UNSPEC
) {
299 dest
->u
.lp
.id
= src
->u
.lp
.id
;
300 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
301 } else if (src
->family
== AF_FLOWSPEC
) {
305 len
= src
->u
.prefix_flowspec
.prefixlen
;
306 dest
->u
.prefix_flowspec
.prefixlen
=
307 src
->u
.prefix_flowspec
.prefixlen
;
308 dest
->family
= src
->family
;
309 temp
= XCALLOC(MTYPE_PREFIX_FLOWSPEC
, len
);
310 dest
->u
.prefix_flowspec
.ptr
= (uintptr_t)temp
;
311 memcpy((void *)dest
->u
.prefix_flowspec
.ptr
,
312 (void *)src
->u
.prefix_flowspec
.ptr
, len
);
314 flog_err(EC_LIB_DEVELOPMENT
,
315 "prefix_copy(): Unknown address family %d",
322 * Return 1 if the address/netmask contained in the prefix structure
323 * is the same, and else return 0. For this routine, 'same' requires
324 * that not only the prefix length and the network part be the same,
325 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
326 * the same. Note that this routine has the same return value sense
327 * as '==' (which is different from prefix_cmp).
329 int prefix_same(union prefixconstptr up1
, union prefixconstptr up2
)
331 const struct prefix
*p1
= up1
.p
;
332 const struct prefix
*p2
= up2
.p
;
334 if ((p1
&& !p2
) || (!p1
&& p2
))
340 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
) {
341 if (p1
->family
== AF_INET
)
342 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
344 if (p1
->family
== AF_INET6
)
345 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
346 &p2
->u
.prefix6
.s6_addr
))
348 if (p1
->family
== AF_ETHERNET
)
349 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
350 sizeof(struct ethaddr
)))
352 if (p1
->family
== AF_EVPN
)
353 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
354 sizeof(struct evpn_addr
)))
356 if (p1
->family
== AF_FLOWSPEC
) {
357 if (p1
->u
.prefix_flowspec
.prefixlen
!=
358 p2
->u
.prefix_flowspec
.prefixlen
)
360 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
361 &p2
->u
.prefix_flowspec
.ptr
,
362 p2
->u
.prefix_flowspec
.prefixlen
))
370 * Return -1/0/1 comparing the prefixes in a way that gives a full/linear
373 * Network prefixes are considered the same if the prefix lengths are equal
374 * and the network parts are the same. Host bits (which are considered masked
375 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
376 * 10.0.0.2/8 are considered equivalent by this routine. Note that
377 * this routine has the same return sense as strcmp (which is different
380 int prefix_cmp(union prefixconstptr up1
, union prefixconstptr up2
)
382 const struct prefix
*p1
= up1
.p
;
383 const struct prefix
*p2
= up2
.p
;
388 /* Set both prefix's head pointer. */
392 if (p1
->family
!= p2
->family
)
393 return numcmp(p1
->family
, p2
->family
);
394 if (p1
->family
== AF_FLOWSPEC
) {
395 pp1
= (const uint8_t *)p1
->u
.prefix_flowspec
.ptr
;
396 pp2
= (const uint8_t *)p2
->u
.prefix_flowspec
.ptr
;
398 if (p1
->u
.prefix_flowspec
.prefixlen
!=
399 p2
->u
.prefix_flowspec
.prefixlen
)
400 return numcmp(p1
->u
.prefix_flowspec
.prefixlen
,
401 p2
->u
.prefix_flowspec
.prefixlen
);
403 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
405 if (pp1
[offset
] != pp2
[offset
])
406 return numcmp(pp1
[offset
], pp2
[offset
]);
412 if (p1
->prefixlen
!= p2
->prefixlen
)
413 return numcmp(p1
->prefixlen
, p2
->prefixlen
);
414 offset
= p1
->prefixlen
/ PNBBY
;
415 shift
= p1
->prefixlen
% PNBBY
;
417 i
= memcmp(pp1
, pp2
, offset
);
422 * At this point offset was the same, if we have shift
423 * that means we still have data to compare, if shift is
424 * 0 then we are at the end of the data structure
425 * and should just return, as that we will be accessing
426 * memory beyond the end of the party zone
429 return numcmp(pp1
[offset
] & maskbit
[shift
],
430 pp2
[offset
] & maskbit
[shift
]);
436 * Count the number of common bits in 2 prefixes. The prefix length is
437 * ignored for this function; the whole prefix is compared. If the prefix
438 * address families don't match, return -1; otherwise the return value is
439 * in range 0 ... maximum prefix length for the address family.
441 int prefix_common_bits(const struct prefix
*p1
, const struct prefix
*p2
)
447 /* Set both prefix's head pointer. */
448 const uint8_t *pp1
= p1
->u
.val
;
449 const uint8_t *pp2
= p2
->u
.val
;
451 if (p1
->family
== AF_INET
)
452 length
= IPV4_MAX_BYTELEN
;
453 if (p1
->family
== AF_INET6
)
454 length
= IPV6_MAX_BYTELEN
;
455 if (p1
->family
== AF_ETHERNET
)
457 if (p1
->family
== AF_EVPN
)
458 length
= 8 * sizeof(struct evpn_addr
);
460 if (p1
->family
!= p2
->family
|| !length
)
463 for (pos
= 0; pos
< length
; pos
++)
464 if (pp1
[pos
] != pp2
[pos
])
469 xor = pp1
[pos
] ^ pp2
[pos
];
470 for (bit
= 0; bit
< 8; bit
++)
471 if (xor&(1 << (7 - bit
)))
474 return pos
* 8 + bit
;
477 /* Return prefix family type string. */
478 const char *prefix_family_str(const struct prefix
*p
)
480 if (p
->family
== AF_INET
)
482 if (p
->family
== AF_INET6
)
484 if (p
->family
== AF_ETHERNET
)
486 if (p
->family
== AF_EVPN
)
491 /* Allocate new prefix_ipv4 structure. */
492 struct prefix_ipv4
*prefix_ipv4_new(void)
494 struct prefix_ipv4
*p
;
496 /* Call prefix_new to allocate a full-size struct prefix to avoid
498 where the struct prefix_ipv4 is cast to struct prefix and unallocated
499 bytes were being referenced (e.g. in structure assignments). */
500 p
= (struct prefix_ipv4
*)prefix_new();
505 /* Free prefix_ipv4 structure. */
506 void prefix_ipv4_free(struct prefix_ipv4
**p
)
508 prefix_free((struct prefix
**)p
);
511 /* If given string is valid return 1 else return 0 */
512 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
519 /* Find slash inside string. */
520 pnt
= strchr(str
, '/');
522 /* String doesn't contail slash. */
524 /* Convert string to prefix. */
525 ret
= inet_pton(AF_INET
, str
, &p
->prefix
);
529 /* If address doesn't contain slash we assume it host address.
532 p
->prefixlen
= IPV4_MAX_BITLEN
;
536 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
537 memcpy(cp
, str
, pnt
- str
);
538 *(cp
+ (pnt
- str
)) = '\0';
539 ret
= inet_pton(AF_INET
, cp
, &p
->prefix
);
540 XFREE(MTYPE_TMP
, cp
);
544 /* Get prefix length. */
545 plen
= (uint8_t)atoi(++pnt
);
546 if (plen
> IPV4_MAX_PREFIXLEN
)
556 /* When string format is invalid return 0. */
557 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
563 const char *str_addr
= str
;
568 if (!strcmp(str
, "any")) {
569 memset(p
, 0, sizeof(*p
));
570 p
->family
= AF_ETHERNET
;
574 /* Find slash inside string. */
575 pnt
= strchr(str
, '/');
578 /* Get prefix length. */
579 plen
= (uint8_t)atoi(++pnt
);
585 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
586 memcpy(cp
, str
, pnt
- str
);
587 *(cp
+ (pnt
- str
)) = '\0';
593 /* Convert string to prefix. */
594 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
600 for (i
= 0; i
< 6; ++i
) {
601 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
604 p
->family
= AF_ETHERNET
;
607 * special case to allow old configurations to work
608 * Since all zero's is implicitly meant to allow
609 * a comparison to zero, let's assume
611 if (!slash
&& is_zero_mac(&(p
->eth_addr
)))
617 XFREE(MTYPE_TMP
, cp
);
622 /* Convert masklen into IP address's netmask (network byte order). */
623 void masklen2ip(const int masklen
, struct in_addr
*netmask
)
625 assert(masklen
>= 0 && masklen
<= IPV4_MAX_BITLEN
);
627 /* left shift is only defined for less than the size of the type.
628 * we unconditionally use long long in case the target platform
629 * has defined behaviour for << 32 (or has a 64-bit left shift) */
631 if (sizeof(unsigned long long) > 4)
632 netmask
->s_addr
= htonl(0xffffffffULL
<< (32 - masklen
));
635 htonl(masklen
? 0xffffffffU
<< (32 - masklen
) : 0);
638 /* Convert IP address's netmask into integer. We assume netmask is
639 * sequential one. Argument netmask should be network byte order. */
640 uint8_t ip_masklen(struct in_addr netmask
)
642 uint32_t tmp
= ~ntohl(netmask
.s_addr
);
645 * clz: count leading zeroes. sadly, the behaviour of this builtin is
646 * undefined for a 0 argument, even though most CPUs give 32
648 return tmp
? __builtin_clz(tmp
) : 32;
651 /* Apply mask to IPv4 prefix (network byte order). */
652 void apply_mask_ipv4(struct prefix_ipv4
*p
)
655 masklen2ip(p
->prefixlen
, &mask
);
656 p
->prefix
.s_addr
&= mask
.s_addr
;
659 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
660 int prefix_ipv4_any(const struct prefix_ipv4
*p
)
662 return (p
->prefix
.s_addr
== 0 && p
->prefixlen
== 0);
665 /* Allocate a new ip version 6 route */
666 struct prefix_ipv6
*prefix_ipv6_new(void)
668 struct prefix_ipv6
*p
;
670 /* Allocate a full-size struct prefix to avoid problems with structure
672 p
= (struct prefix_ipv6
*)prefix_new();
673 p
->family
= AF_INET6
;
677 /* Free prefix for IPv6. */
678 void prefix_ipv6_free(struct prefix_ipv6
**p
)
680 prefix_free((struct prefix
**)p
);
683 /* If given string is valid return 1 else return 0 */
684 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
690 pnt
= strchr(str
, '/');
692 /* If string doesn't contain `/' treat it as host route. */
694 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
697 p
->prefixlen
= IPV6_MAX_BITLEN
;
701 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
702 memcpy(cp
, str
, pnt
- str
);
703 *(cp
+ (pnt
- str
)) = '\0';
704 ret
= inet_pton(AF_INET6
, cp
, &p
->prefix
);
705 XFREE(MTYPE_TMP
, cp
);
708 plen
= (uint8_t)atoi(++pnt
);
709 if (plen
> IPV6_MAX_BITLEN
)
713 p
->family
= AF_INET6
;
718 /* Convert struct in6_addr netmask into integer.
719 * FIXME return uint8_t as ip_maskleni() does. */
720 int ip6_masklen(struct in6_addr netmask
)
722 if (netmask
.s6_addr32
[0] != 0xffffffffU
)
723 return __builtin_clz(~ntohl(netmask
.s6_addr32
[0]));
724 if (netmask
.s6_addr32
[1] != 0xffffffffU
)
725 return __builtin_clz(~ntohl(netmask
.s6_addr32
[1])) + 32;
726 if (netmask
.s6_addr32
[2] != 0xffffffffU
)
727 return __builtin_clz(~ntohl(netmask
.s6_addr32
[2])) + 64;
728 if (netmask
.s6_addr32
[3] != 0xffffffffU
)
729 return __builtin_clz(~ntohl(netmask
.s6_addr32
[3])) + 96;
730 /* note __builtin_clz(0) is undefined */
734 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
736 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
739 /* note << 32 is undefined */
740 memset(netmask
, 0, sizeof(*netmask
));
741 } else if (masklen
<= 32) {
742 netmask
->s6_addr32
[0] = htonl(0xffffffffU
<< (32 - masklen
));
743 netmask
->s6_addr32
[1] = 0;
744 netmask
->s6_addr32
[2] = 0;
745 netmask
->s6_addr32
[3] = 0;
746 } else if (masklen
<= 64) {
747 netmask
->s6_addr32
[0] = 0xffffffffU
;
748 netmask
->s6_addr32
[1] = htonl(0xffffffffU
<< (64 - masklen
));
749 netmask
->s6_addr32
[2] = 0;
750 netmask
->s6_addr32
[3] = 0;
751 } else if (masklen
<= 96) {
752 netmask
->s6_addr32
[0] = 0xffffffffU
;
753 netmask
->s6_addr32
[1] = 0xffffffffU
;
754 netmask
->s6_addr32
[2] = htonl(0xffffffffU
<< (96 - masklen
));
755 netmask
->s6_addr32
[3] = 0;
757 netmask
->s6_addr32
[0] = 0xffffffffU
;
758 netmask
->s6_addr32
[1] = 0xffffffffU
;
759 netmask
->s6_addr32
[2] = 0xffffffffU
;
760 netmask
->s6_addr32
[3] = htonl(0xffffffffU
<< (128 - masklen
));
764 void apply_mask_ipv6(struct prefix_ipv6
*p
)
770 index
= p
->prefixlen
/ 8;
773 pnt
= (uint8_t *)&p
->prefix
;
774 offset
= p
->prefixlen
% 8;
776 pnt
[index
] &= maskbit
[offset
];
784 void apply_mask(struct prefix
*p
)
788 apply_mask_ipv4((struct prefix_ipv4
*)p
);
791 apply_mask_ipv6((struct prefix_ipv6
*)p
);
799 /* Utility function of convert between struct prefix <=> union sockunion. */
800 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
801 struct prefix
*prefix
)
803 if (su
->sa
.sa_family
== AF_INET
) {
804 struct prefix_ipv4
*p
;
806 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
808 p
->prefix
= su
->sin
.sin_addr
;
809 p
->prefixlen
= IPV4_MAX_BITLEN
;
810 return (struct prefix
*)p
;
812 if (su
->sa
.sa_family
== AF_INET6
) {
813 struct prefix_ipv6
*p
;
815 p
= prefix
? (struct prefix_ipv6
*)prefix
: prefix_ipv6_new();
816 p
->family
= AF_INET6
;
817 p
->prefixlen
= IPV6_MAX_BITLEN
;
818 memcpy(&p
->prefix
, &su
->sin6
.sin6_addr
,
819 sizeof(struct in6_addr
));
820 return (struct prefix
*)p
;
825 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
827 memset(su
, 0, sizeof(*su
));
829 su
->sa
.sa_family
= p
->family
;
830 if (p
->family
== AF_INET
)
831 su
->sin
.sin_addr
= p
->u
.prefix4
;
832 if (p
->family
== AF_INET6
)
833 memcpy(&su
->sin6
.sin6_addr
, &p
->u
.prefix6
,
834 sizeof(struct in6_addr
));
837 int prefix_blen(const struct prefix
*p
)
841 return IPV4_MAX_BYTELEN
;
844 return IPV6_MAX_BYTELEN
;
853 /* Generic function for conversion string to struct prefix. */
854 int str2prefix(const char *str
, struct prefix
*p
)
861 /* First we try to convert string to struct prefix_ipv4. */
862 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
866 /* Next we try to convert string to struct prefix_ipv6. */
867 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
871 /* Next we try to convert string to struct prefix_eth. */
872 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
879 static const char *prefixevpn_ead2str(const struct prefix_evpn
*p
, char *str
,
882 snprintf(str
, size
, "Unsupported EVPN prefix");
886 static const char *prefixevpn_macip2str(const struct prefix_evpn
*p
, char *str
,
890 char buf
[PREFIX2STR_BUFFER
];
891 char buf2
[ETHER_ADDR_STRLEN
];
893 if (is_evpn_prefix_ipaddr_none(p
))
894 snprintf(str
, size
, "[%d]:[%s]/%d",
895 p
->prefix
.route_type
,
896 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
900 family
= is_evpn_prefix_ipaddr_v4(p
)
903 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d",
904 p
->prefix
.route_type
,
905 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
908 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
909 buf
, PREFIX2STR_BUFFER
),
915 static const char *prefixevpn_imet2str(const struct prefix_evpn
*p
, char *str
,
919 char buf
[PREFIX2STR_BUFFER
];
921 family
= is_evpn_prefix_ipaddr_v4(p
)
924 snprintf(str
, size
, "[%d]:[%s]/%d", p
->prefix
.route_type
,
926 &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf
,
932 static const char *prefixevpn_es2str(const struct prefix_evpn
*p
, char *str
,
935 char buf
[ESI_STR_LEN
];
937 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d", p
->prefix
.route_type
,
938 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
939 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
),
944 static const char *prefixevpn_prefix2str(const struct prefix_evpn
*p
, char *str
,
948 char buf
[PREFIX2STR_BUFFER
];
950 family
= is_evpn_prefix_ipaddr_v4(p
)
953 snprintf(str
, size
, "[%d]:[%u][%s/%d]/%d",
954 p
->prefix
.route_type
,
955 p
->prefix
.prefix_addr
.eth_tag
,
957 &p
->prefix
.prefix_addr
.ip
.ip
.addr
, buf
,
959 p
->prefix
.prefix_addr
.ip_prefix_length
,
964 static const char *prefixevpn2str(const struct prefix_evpn
*p
, char *str
,
967 switch (p
->prefix
.route_type
) {
969 return prefixevpn_ead2str(p
, str
, size
);
971 return prefixevpn_macip2str(p
, str
, size
);
973 return prefixevpn_imet2str(p
, str
, size
);
975 return prefixevpn_es2str(p
, str
, size
);
977 return prefixevpn_prefix2str(p
, str
, size
);
979 snprintf(str
, size
, "Unsupported EVPN prefix");
985 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
987 const struct prefix
*p
= pu
.p
;
988 char buf
[PREFIX2STR_BUFFER
];
996 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
));
1000 if ((tmp
= p
->prefixlen
- 100) >= 0) {
1011 strlcpy(str
, buf
, size
);
1015 snprintf(str
, size
, "%s/%d",
1016 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1021 prefixevpn2str((const struct prefix_evpn
*)p
, str
, size
);
1025 strlcpy(str
, "FS prefix", size
);
1029 strlcpy(str
, "UNK prefix", size
);
1036 void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
1037 char *buf
, int buf_size
)
1039 int save_errno
= errno
;
1041 if (addr
.s_addr
== INADDR_ANY
)
1042 strlcpy(buf
, "*", buf_size
);
1044 if (!inet_ntop(AF_INET
, &addr
, buf
, buf_size
)) {
1046 snprintf(buf
, buf_size
, "%s", onfail
);
1053 const char *prefix_sg2str(const struct prefix_sg
*sg
, char *sg_str
)
1055 char src_str
[INET_ADDRSTRLEN
];
1056 char grp_str
[INET_ADDRSTRLEN
];
1058 prefix_mcast_inet4_dump("<src?>", sg
->src
, src_str
, sizeof(src_str
));
1059 prefix_mcast_inet4_dump("<grp?>", sg
->grp
, grp_str
, sizeof(grp_str
));
1060 snprintf(sg_str
, PREFIX_SG_STR_LEN
, "(%s,%s)", src_str
, grp_str
);
1065 struct prefix
*prefix_new(void)
1069 p
= XCALLOC(MTYPE_PREFIX
, sizeof *p
);
1073 void prefix_free_lists(void *arg
)
1075 struct prefix
*p
= arg
;
1080 /* Free prefix structure. */
1081 void prefix_free(struct prefix
**p
)
1083 XFREE(MTYPE_PREFIX
, *p
);
1087 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1088 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1091 uint32_t destination
;
1093 destination
= ntohl(p
->prefix
.s_addr
);
1095 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
)
1097 /* do nothing for host routes */
1098 else if (IN_CLASSC(destination
)) {
1101 } else if (IN_CLASSB(destination
)) {
1110 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1112 struct in_addr mask
;
1114 masklen2ip(masklen
, &mask
);
1115 return (masklen
!= IPV4_MAX_PREFIXLEN
- 1) ?
1117 (hostaddr
| ~mask
.s_addr
)
1119 /* special case for /31 */
1120 (hostaddr
^ ~mask
.s_addr
);
1123 /* Utility function to convert ipv4 netmask to prefixes
1124 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1125 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1126 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1129 struct in_addr network
;
1130 struct in_addr mask
;
1132 uint32_t destination
;
1135 ret
= inet_aton(net_str
, &network
);
1140 ret
= inet_aton(mask_str
, &mask
);
1144 prefixlen
= ip_masklen(mask
);
1146 destination
= ntohl(network
.s_addr
);
1148 if (network
.s_addr
== 0)
1150 else if (IN_CLASSC(destination
))
1152 else if (IN_CLASSB(destination
))
1154 else if (IN_CLASSA(destination
))
1160 sprintf(prefix_str
, "%s/%d", net_str
, prefixlen
);
1165 /* Utility function for making IPv6 address string. */
1166 const char *inet6_ntoa(struct in6_addr addr
)
1168 static char buf
[INET6_ADDRSTRLEN
];
1170 inet_ntop(AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
1174 /* converts to internal representation of mac address
1175 * returns 1 on success, 0 otherwise
1176 * format accepted: AA:BB:CC:DD:EE:FF
1177 * if mac parameter is null, then check only
1179 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1187 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1190 /* error in incoming str length */
1193 /* valid mac address */
1196 for (i
= 0; i
< 6; ++i
)
1197 mac
->octet
[i
] = a
[i
] & 0xff;
1201 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1208 ptr
= XMALLOC(MTYPE_TMP
, ETHER_ADDR_STRLEN
* sizeof(char));
1210 assert(size
>= ETHER_ADDR_STRLEN
);
1213 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1214 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1215 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1216 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1220 unsigned prefix_hash_key(const void *pp
)
1224 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1228 /* make sure *all* unused bits are zero,
1229 * particularly including alignment /
1230 * padding and unused prefix bytes.
1232 memset(©
, 0, sizeof(copy
));
1233 prefix_copy(©
, (struct prefix
*)pp
);
1234 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1235 copy
.u
.prefix_flowspec
.prefixlen
,
1237 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1238 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1239 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1242 /* make sure *all* unused bits are zero, particularly including
1244 * padding and unused prefix bytes. */
1245 memset(©
, 0, sizeof(copy
));
1246 prefix_copy(©
, (struct prefix
*)pp
);
1248 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
1252 /* converts to internal representation of esi
1253 * returns 1 on success, 0 otherwise
1254 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1255 * if esi parameter is null, then check only
1257 int str_to_esi(const char *str
, esi_t
*esi
)
1260 unsigned int a
[ESI_BYTES
];
1265 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1266 a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1267 a
+ 4, a
+ 5, a
+ 6, a
+ 7,
1270 /* error in incoming str length */
1277 for (i
= 0; i
< ESI_BYTES
; ++i
)
1278 esi
->val
[i
] = a
[i
] & 0xff;
1282 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1289 ptr
= XMALLOC(MTYPE_TMP
, ESI_STR_LEN
* sizeof(char));
1291 assert(size
>= ESI_STR_LEN
);
1295 snprintf(ptr
, ESI_STR_LEN
,
1296 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1297 esi
->val
[0], esi
->val
[1], esi
->val
[2],
1298 esi
->val
[3], esi
->val
[4], esi
->val
[5],
1299 esi
->val
[6], esi
->val
[7], esi
->val
[8],
1304 printfrr_ext_autoreg_p("I4", printfrr_i4
)
1305 static ssize_t
printfrr_i4(char *buf
, size_t bsz
, const char *fmt
,
1306 int prec
, const void *ptr
)
1308 inet_ntop(AF_INET
, ptr
, buf
, bsz
);
1312 printfrr_ext_autoreg_p("I6", printfrr_i6
)
1313 static ssize_t
printfrr_i6(char *buf
, size_t bsz
, const char *fmt
,
1314 int prec
, const void *ptr
)
1316 inet_ntop(AF_INET6
, ptr
, buf
, bsz
);
1320 printfrr_ext_autoreg_p("FX", printfrr_pfx
)
1321 static ssize_t
printfrr_pfx(char *buf
, size_t bsz
, const char *fmt
,
1322 int prec
, const void *ptr
)
1324 prefix2str(ptr
, buf
, bsz
);
1328 printfrr_ext_autoreg_p("SG4", printfrr_psg
)
1329 static ssize_t
printfrr_psg(char *buf
, size_t bsz
, const char *fmt
,
1330 int prec
, const void *ptr
)
1332 const struct prefix_sg
*sg
= ptr
;
1333 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= bsz
- 1 };
1335 if (sg
->src
.s_addr
== INADDR_ANY
)
1336 bprintfrr(&fb
, "(*,");
1338 bprintfrr(&fb
, "(%pI4,", &sg
->src
);
1340 if (sg
->grp
.s_addr
== INADDR_ANY
)
1341 bprintfrr(&fb
, "*)");
1343 bprintfrr(&fb
, "%pI4)", &sg
->grp
);