]>
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 bool is_bcast_mac(const struct ethaddr
*mac
)
63 for (i
= 0; i
< ETH_ALEN
; i
++)
64 if (mac
->octet
[i
] != 0xFF)
70 bool is_mcast_mac(const struct ethaddr
*mac
)
72 if ((mac
->octet
[0] & 0x01) == 0x01)
78 unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t prefixlen
)
80 unsigned int offset
= prefixlen
/ 8;
81 unsigned int shift
= 7 - (prefixlen
% 8);
83 return (prefix
[offset
] >> shift
) & 1;
86 int str2family(const char *string
)
88 if (!strcmp("ipv4", string
))
90 else if (!strcmp("ipv6", string
))
92 else if (!strcmp("ethernet", string
))
94 else if (!strcmp("evpn", string
))
99 const char *family2str(int family
)
114 /* Address Famiy Identifier to Address Family converter. */
115 int afi2family(afi_t afi
)
119 else if (afi
== AFI_IP6
)
121 else if (afi
== AFI_L2VPN
)
123 /* NOTE: EVPN code should NOT use this interface. */
127 afi_t
family2afi(int family
)
129 if (family
== AF_INET
)
131 else if (family
== AF_INET6
)
133 else if (family
== AF_ETHERNET
|| family
== AF_EVPN
)
138 const char *afi2str(afi_t afi
)
155 const char *safi2str(safi_t safi
)
168 case SAFI_LABELED_UNICAST
:
169 return "labeled-unicast";
177 /* If n includes p prefix then return 1 else return 0. */
178 int prefix_match(const struct prefix
*n
, const struct prefix
*p
)
182 const uint8_t *np
, *pp
;
184 /* If n's prefix is longer than p's one return 0. */
185 if (n
->prefixlen
> p
->prefixlen
)
188 if (n
->family
== AF_FLOWSPEC
) {
189 /* prefixlen is unused. look at fs prefix len */
190 if (n
->u
.prefix_flowspec
.prefixlen
>
191 p
->u
.prefix_flowspec
.prefixlen
)
194 /* Set both prefix's head pointer. */
195 np
= (const uint8_t *)&n
->u
.prefix_flowspec
.ptr
;
196 pp
= (const uint8_t *)&p
->u
.prefix_flowspec
.ptr
;
198 offset
= n
->u
.prefix_flowspec
.prefixlen
;
201 if (np
[offset
] != pp
[offset
])
206 /* Set both prefix's head pointer. */
210 offset
= n
->prefixlen
/ PNBBY
;
211 shift
= n
->prefixlen
% PNBBY
;
214 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
218 if (np
[offset
] != pp
[offset
])
225 * n is a type5 evpn prefix. This function tries to see if there is an
226 * ip-prefix within n which matches prefix p
227 * If n includes p prefix then return 1 else return 0.
229 int evpn_type5_prefix_match(const struct prefix
*n
, const struct prefix
*p
)
234 const uint8_t *np
, *pp
;
235 struct prefix_evpn
*evp
;
237 if (n
->family
!= AF_EVPN
)
240 evp
= (struct prefix_evpn
*)n
;
243 if ((evp
->prefix
.route_type
!= 5) ||
244 (p
->family
== AF_INET6
&& !is_evpn_prefix_ipaddr_v6(evp
)) ||
245 (p
->family
== AF_INET
&& !is_evpn_prefix_ipaddr_v4(evp
)) ||
246 (is_evpn_prefix_ipaddr_none(evp
)))
249 prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
250 np
= &evp
->prefix
.prefix_addr
.ip
.ip
.addr
;
252 /* If n's prefix is longer than p's one return 0. */
253 if (prefixlen
> p
->prefixlen
)
256 offset
= prefixlen
/ PNBBY
;
257 shift
= prefixlen
% PNBBY
;
260 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
264 if (np
[offset
] != pp
[offset
])
270 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
271 int prefix_match_network_statement(const struct prefix
*n
,
272 const struct prefix
*p
)
276 const uint8_t *np
, *pp
;
278 /* Set both prefix's head pointer. */
282 offset
= n
->prefixlen
/ PNBBY
;
283 shift
= n
->prefixlen
% PNBBY
;
286 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
290 if (np
[offset
] != pp
[offset
])
295 #ifdef __clang_analyzer__
296 #undef prefix_copy /* cf. prefix.h */
299 void prefix_copy(union prefixptr udest
, union prefixconstptr usrc
)
301 struct prefix
*dest
= udest
.p
;
302 const struct prefix
*src
= usrc
.p
;
304 dest
->family
= src
->family
;
305 dest
->prefixlen
= src
->prefixlen
;
307 if (src
->family
== AF_INET
)
308 dest
->u
.prefix4
= src
->u
.prefix4
;
309 else if (src
->family
== AF_INET6
)
310 dest
->u
.prefix6
= src
->u
.prefix6
;
311 else if (src
->family
== AF_ETHERNET
) {
312 memcpy(&dest
->u
.prefix_eth
, &src
->u
.prefix_eth
,
313 sizeof(struct ethaddr
));
314 } else if (src
->family
== AF_EVPN
) {
315 memcpy(&dest
->u
.prefix_evpn
, &src
->u
.prefix_evpn
,
316 sizeof(struct evpn_addr
));
317 } else if (src
->family
== AF_UNSPEC
) {
318 dest
->u
.lp
.id
= src
->u
.lp
.id
;
319 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
320 } else if (src
->family
== AF_FLOWSPEC
) {
324 len
= src
->u
.prefix_flowspec
.prefixlen
;
325 dest
->u
.prefix_flowspec
.prefixlen
=
326 src
->u
.prefix_flowspec
.prefixlen
;
327 dest
->family
= src
->family
;
328 temp
= XCALLOC(MTYPE_PREFIX_FLOWSPEC
, len
);
329 dest
->u
.prefix_flowspec
.ptr
= (uintptr_t)temp
;
330 memcpy((void *)dest
->u
.prefix_flowspec
.ptr
,
331 (void *)src
->u
.prefix_flowspec
.ptr
, len
);
333 flog_err(EC_LIB_DEVELOPMENT
,
334 "prefix_copy(): Unknown address family %d",
341 * Return 1 if the address/netmask contained in the prefix structure
342 * is the same, and else return 0. For this routine, 'same' requires
343 * that not only the prefix length and the network part be the same,
344 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
345 * the same. Note that this routine has the same return value sense
346 * as '==' (which is different from prefix_cmp).
348 int prefix_same(union prefixconstptr up1
, union prefixconstptr up2
)
350 const struct prefix
*p1
= up1
.p
;
351 const struct prefix
*p2
= up2
.p
;
353 if ((p1
&& !p2
) || (!p1
&& p2
))
359 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
) {
360 if (p1
->family
== AF_INET
)
361 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
363 if (p1
->family
== AF_INET6
)
364 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
365 &p2
->u
.prefix6
.s6_addr
))
367 if (p1
->family
== AF_ETHERNET
)
368 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
369 sizeof(struct ethaddr
)))
371 if (p1
->family
== AF_EVPN
)
372 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
373 sizeof(struct evpn_addr
)))
375 if (p1
->family
== AF_FLOWSPEC
) {
376 if (p1
->u
.prefix_flowspec
.prefixlen
!=
377 p2
->u
.prefix_flowspec
.prefixlen
)
379 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
380 &p2
->u
.prefix_flowspec
.ptr
,
381 p2
->u
.prefix_flowspec
.prefixlen
))
389 * Return -1/0/1 comparing the prefixes in a way that gives a full/linear
392 * Network prefixes are considered the same if the prefix lengths are equal
393 * and the network parts are the same. Host bits (which are considered masked
394 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
395 * 10.0.0.2/8 are considered equivalent by this routine. Note that
396 * this routine has the same return sense as strcmp (which is different
399 int prefix_cmp(union prefixconstptr up1
, union prefixconstptr up2
)
401 const struct prefix
*p1
= up1
.p
;
402 const struct prefix
*p2
= up2
.p
;
407 /* Set both prefix's head pointer. */
411 if (p1
->family
!= p2
->family
)
412 return numcmp(p1
->family
, p2
->family
);
413 if (p1
->family
== AF_FLOWSPEC
) {
414 pp1
= (const uint8_t *)p1
->u
.prefix_flowspec
.ptr
;
415 pp2
= (const uint8_t *)p2
->u
.prefix_flowspec
.ptr
;
417 if (p1
->u
.prefix_flowspec
.prefixlen
!=
418 p2
->u
.prefix_flowspec
.prefixlen
)
419 return numcmp(p1
->u
.prefix_flowspec
.prefixlen
,
420 p2
->u
.prefix_flowspec
.prefixlen
);
422 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
424 if (pp1
[offset
] != pp2
[offset
])
425 return numcmp(pp1
[offset
], pp2
[offset
]);
431 if (p1
->prefixlen
!= p2
->prefixlen
)
432 return numcmp(p1
->prefixlen
, p2
->prefixlen
);
433 offset
= p1
->prefixlen
/ PNBBY
;
434 shift
= p1
->prefixlen
% PNBBY
;
436 i
= memcmp(pp1
, pp2
, offset
);
441 * At this point offset was the same, if we have shift
442 * that means we still have data to compare, if shift is
443 * 0 then we are at the end of the data structure
444 * and should just return, as that we will be accessing
445 * memory beyond the end of the party zone
448 return numcmp(pp1
[offset
] & maskbit
[shift
],
449 pp2
[offset
] & maskbit
[shift
]);
455 * Count the number of common bits in 2 prefixes. The prefix length is
456 * ignored for this function; the whole prefix is compared. If the prefix
457 * address families don't match, return -1; otherwise the return value is
458 * in range 0 ... maximum prefix length for the address family.
460 int prefix_common_bits(const struct prefix
*p1
, const struct prefix
*p2
)
466 /* Set both prefix's head pointer. */
467 const uint8_t *pp1
= p1
->u
.val
;
468 const uint8_t *pp2
= p2
->u
.val
;
470 if (p1
->family
== AF_INET
)
471 length
= IPV4_MAX_BYTELEN
;
472 if (p1
->family
== AF_INET6
)
473 length
= IPV6_MAX_BYTELEN
;
474 if (p1
->family
== AF_ETHERNET
)
476 if (p1
->family
== AF_EVPN
)
477 length
= 8 * sizeof(struct evpn_addr
);
479 if (p1
->family
!= p2
->family
|| !length
)
482 for (pos
= 0; pos
< length
; pos
++)
483 if (pp1
[pos
] != pp2
[pos
])
488 xor = pp1
[pos
] ^ pp2
[pos
];
489 for (bit
= 0; bit
< 8; bit
++)
490 if (xor&(1 << (7 - bit
)))
493 return pos
* 8 + bit
;
496 /* Return prefix family type string. */
497 const char *prefix_family_str(const struct prefix
*p
)
499 if (p
->family
== AF_INET
)
501 if (p
->family
== AF_INET6
)
503 if (p
->family
== AF_ETHERNET
)
505 if (p
->family
== AF_EVPN
)
510 /* Allocate new prefix_ipv4 structure. */
511 struct prefix_ipv4
*prefix_ipv4_new(void)
513 struct prefix_ipv4
*p
;
515 /* Call prefix_new to allocate a full-size struct prefix to avoid
517 where the struct prefix_ipv4 is cast to struct prefix and unallocated
518 bytes were being referenced (e.g. in structure assignments). */
519 p
= (struct prefix_ipv4
*)prefix_new();
524 /* Free prefix_ipv4 structure. */
525 void prefix_ipv4_free(struct prefix_ipv4
**p
)
527 prefix_free((struct prefix
**)p
);
530 /* If given string is valid return 1 else return 0 */
531 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
538 /* Find slash inside string. */
539 pnt
= strchr(str
, '/');
541 /* String doesn't contail slash. */
543 /* Convert string to prefix. */
544 ret
= inet_pton(AF_INET
, str
, &p
->prefix
);
548 /* If address doesn't contain slash we assume it host address.
551 p
->prefixlen
= IPV4_MAX_BITLEN
;
555 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
556 memcpy(cp
, str
, pnt
- str
);
557 *(cp
+ (pnt
- str
)) = '\0';
558 ret
= inet_pton(AF_INET
, cp
, &p
->prefix
);
559 XFREE(MTYPE_TMP
, cp
);
563 /* Get prefix length. */
564 plen
= (uint8_t)atoi(++pnt
);
565 if (plen
> IPV4_MAX_PREFIXLEN
)
575 /* When string format is invalid return 0. */
576 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
582 const char *str_addr
= str
;
587 if (!strcmp(str
, "any")) {
588 memset(p
, 0, sizeof(*p
));
589 p
->family
= AF_ETHERNET
;
593 /* Find slash inside string. */
594 pnt
= strchr(str
, '/');
597 /* Get prefix length. */
598 plen
= (uint8_t)atoi(++pnt
);
604 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
605 memcpy(cp
, str
, pnt
- str
);
606 *(cp
+ (pnt
- str
)) = '\0';
612 /* Convert string to prefix. */
613 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
619 for (i
= 0; i
< 6; ++i
) {
620 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
623 p
->family
= AF_ETHERNET
;
626 * special case to allow old configurations to work
627 * Since all zero's is implicitly meant to allow
628 * a comparison to zero, let's assume
630 if (!slash
&& is_zero_mac(&(p
->eth_addr
)))
636 XFREE(MTYPE_TMP
, cp
);
641 /* Convert masklen into IP address's netmask (network byte order). */
642 void masklen2ip(const int masklen
, struct in_addr
*netmask
)
644 assert(masklen
>= 0 && masklen
<= IPV4_MAX_BITLEN
);
646 /* left shift is only defined for less than the size of the type.
647 * we unconditionally use long long in case the target platform
648 * has defined behaviour for << 32 (or has a 64-bit left shift) */
650 if (sizeof(unsigned long long) > 4)
651 netmask
->s_addr
= htonl(0xffffffffULL
<< (32 - masklen
));
654 htonl(masklen
? 0xffffffffU
<< (32 - masklen
) : 0);
657 /* Convert IP address's netmask into integer. We assume netmask is
658 * sequential one. Argument netmask should be network byte order. */
659 uint8_t ip_masklen(struct in_addr netmask
)
661 uint32_t tmp
= ~ntohl(netmask
.s_addr
);
664 * clz: count leading zeroes. sadly, the behaviour of this builtin is
665 * undefined for a 0 argument, even though most CPUs give 32
667 return tmp
? __builtin_clz(tmp
) : 32;
670 /* Apply mask to IPv4 prefix (network byte order). */
671 void apply_mask_ipv4(struct prefix_ipv4
*p
)
674 masklen2ip(p
->prefixlen
, &mask
);
675 p
->prefix
.s_addr
&= mask
.s_addr
;
678 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
679 int prefix_ipv4_any(const struct prefix_ipv4
*p
)
681 return (p
->prefix
.s_addr
== INADDR_ANY
&& p
->prefixlen
== 0);
684 /* Allocate a new ip version 6 route */
685 struct prefix_ipv6
*prefix_ipv6_new(void)
687 struct prefix_ipv6
*p
;
689 /* Allocate a full-size struct prefix to avoid problems with structure
691 p
= (struct prefix_ipv6
*)prefix_new();
692 p
->family
= AF_INET6
;
696 /* Free prefix for IPv6. */
697 void prefix_ipv6_free(struct prefix_ipv6
**p
)
699 prefix_free((struct prefix
**)p
);
702 /* If given string is valid return 1 else return 0 */
703 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
709 pnt
= strchr(str
, '/');
711 /* If string doesn't contain `/' treat it as host route. */
713 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
716 p
->prefixlen
= IPV6_MAX_BITLEN
;
720 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
721 memcpy(cp
, str
, pnt
- str
);
722 *(cp
+ (pnt
- str
)) = '\0';
723 ret
= inet_pton(AF_INET6
, cp
, &p
->prefix
);
724 XFREE(MTYPE_TMP
, cp
);
727 plen
= (uint8_t)atoi(++pnt
);
728 if (plen
> IPV6_MAX_BITLEN
)
732 p
->family
= AF_INET6
;
737 /* Convert struct in6_addr netmask into integer.
738 * FIXME return uint8_t as ip_maskleni() does. */
739 int ip6_masklen(struct in6_addr netmask
)
741 if (netmask
.s6_addr32
[0] != 0xffffffffU
)
742 return __builtin_clz(~ntohl(netmask
.s6_addr32
[0]));
743 if (netmask
.s6_addr32
[1] != 0xffffffffU
)
744 return __builtin_clz(~ntohl(netmask
.s6_addr32
[1])) + 32;
745 if (netmask
.s6_addr32
[2] != 0xffffffffU
)
746 return __builtin_clz(~ntohl(netmask
.s6_addr32
[2])) + 64;
747 if (netmask
.s6_addr32
[3] != 0xffffffffU
)
748 return __builtin_clz(~ntohl(netmask
.s6_addr32
[3])) + 96;
749 /* note __builtin_clz(0) is undefined */
753 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
755 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
758 /* note << 32 is undefined */
759 memset(netmask
, 0, sizeof(*netmask
));
760 } else if (masklen
<= 32) {
761 netmask
->s6_addr32
[0] = htonl(0xffffffffU
<< (32 - masklen
));
762 netmask
->s6_addr32
[1] = 0;
763 netmask
->s6_addr32
[2] = 0;
764 netmask
->s6_addr32
[3] = 0;
765 } else if (masklen
<= 64) {
766 netmask
->s6_addr32
[0] = 0xffffffffU
;
767 netmask
->s6_addr32
[1] = htonl(0xffffffffU
<< (64 - masklen
));
768 netmask
->s6_addr32
[2] = 0;
769 netmask
->s6_addr32
[3] = 0;
770 } else if (masklen
<= 96) {
771 netmask
->s6_addr32
[0] = 0xffffffffU
;
772 netmask
->s6_addr32
[1] = 0xffffffffU
;
773 netmask
->s6_addr32
[2] = htonl(0xffffffffU
<< (96 - masklen
));
774 netmask
->s6_addr32
[3] = 0;
776 netmask
->s6_addr32
[0] = 0xffffffffU
;
777 netmask
->s6_addr32
[1] = 0xffffffffU
;
778 netmask
->s6_addr32
[2] = 0xffffffffU
;
779 netmask
->s6_addr32
[3] = htonl(0xffffffffU
<< (128 - masklen
));
783 void apply_mask_ipv6(struct prefix_ipv6
*p
)
789 index
= p
->prefixlen
/ 8;
792 pnt
= (uint8_t *)&p
->prefix
;
793 offset
= p
->prefixlen
% 8;
795 pnt
[index
] &= maskbit
[offset
];
803 void apply_mask(struct prefix
*p
)
807 apply_mask_ipv4((struct prefix_ipv4
*)p
);
810 apply_mask_ipv6((struct prefix_ipv6
*)p
);
818 /* Utility function of convert between struct prefix <=> union sockunion. */
819 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
820 struct prefix
*prefix
)
822 if (su
->sa
.sa_family
== AF_INET
) {
823 struct prefix_ipv4
*p
;
825 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
827 p
->prefix
= su
->sin
.sin_addr
;
828 p
->prefixlen
= IPV4_MAX_BITLEN
;
829 return (struct prefix
*)p
;
831 if (su
->sa
.sa_family
== AF_INET6
) {
832 struct prefix_ipv6
*p
;
834 p
= prefix
? (struct prefix_ipv6
*)prefix
: prefix_ipv6_new();
835 p
->family
= AF_INET6
;
836 p
->prefixlen
= IPV6_MAX_BITLEN
;
837 memcpy(&p
->prefix
, &su
->sin6
.sin6_addr
,
838 sizeof(struct in6_addr
));
839 return (struct prefix
*)p
;
844 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
846 memset(su
, 0, sizeof(*su
));
848 su
->sa
.sa_family
= p
->family
;
849 if (p
->family
== AF_INET
)
850 su
->sin
.sin_addr
= p
->u
.prefix4
;
851 if (p
->family
== AF_INET6
)
852 memcpy(&su
->sin6
.sin6_addr
, &p
->u
.prefix6
,
853 sizeof(struct in6_addr
));
856 int prefix_blen(const struct prefix
*p
)
860 return IPV4_MAX_BYTELEN
;
862 return IPV6_MAX_BYTELEN
;
869 /* Generic function for conversion string to struct prefix. */
870 int str2prefix(const char *str
, struct prefix
*p
)
877 /* First we try to convert string to struct prefix_ipv4. */
878 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
882 /* Next we try to convert string to struct prefix_ipv6. */
883 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
887 /* Next we try to convert string to struct prefix_eth. */
888 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
895 static const char *prefixevpn_ead2str(const struct prefix_evpn
*p
, char *str
,
898 snprintf(str
, size
, "Unsupported EVPN prefix");
902 static const char *prefixevpn_macip2str(const struct prefix_evpn
*p
, char *str
,
906 char buf
[PREFIX2STR_BUFFER
];
907 char buf2
[ETHER_ADDR_STRLEN
];
909 if (is_evpn_prefix_ipaddr_none(p
))
910 snprintf(str
, size
, "[%d]:[%s]/%d",
911 p
->prefix
.route_type
,
912 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
916 family
= is_evpn_prefix_ipaddr_v4(p
)
919 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d",
920 p
->prefix
.route_type
,
921 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
924 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
925 buf
, PREFIX2STR_BUFFER
),
931 static const char *prefixevpn_imet2str(const struct prefix_evpn
*p
, char *str
,
935 char buf
[PREFIX2STR_BUFFER
];
937 family
= is_evpn_prefix_ipaddr_v4(p
)
940 snprintf(str
, size
, "[%d]:[%s]/%d", p
->prefix
.route_type
,
942 &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf
,
948 static const char *prefixevpn_es2str(const struct prefix_evpn
*p
, char *str
,
951 char buf
[ESI_STR_LEN
];
953 snprintf(str
, size
, "[%d]:[%s]:[%s]/%d", p
->prefix
.route_type
,
954 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
955 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
),
960 static const char *prefixevpn_prefix2str(const struct prefix_evpn
*p
, char *str
,
964 char buf
[PREFIX2STR_BUFFER
];
966 family
= is_evpn_prefix_ipaddr_v4(p
)
969 snprintf(str
, size
, "[%d]:[%u][%s/%d]/%d",
970 p
->prefix
.route_type
,
971 p
->prefix
.prefix_addr
.eth_tag
,
973 &p
->prefix
.prefix_addr
.ip
.ip
.addr
, buf
,
975 p
->prefix
.prefix_addr
.ip_prefix_length
,
980 static const char *prefixevpn2str(const struct prefix_evpn
*p
, char *str
,
983 switch (p
->prefix
.route_type
) {
985 return prefixevpn_ead2str(p
, str
, size
);
987 return prefixevpn_macip2str(p
, str
, size
);
989 return prefixevpn_imet2str(p
, str
, size
);
991 return prefixevpn_es2str(p
, str
, size
);
993 return prefixevpn_prefix2str(p
, str
, size
);
995 snprintf(str
, size
, "Unsupported EVPN prefix");
1001 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
1003 const struct prefix
*p
= pu
.p
;
1004 char buf
[PREFIX2STR_BUFFER
];
1005 int byte
, tmp
, a
, b
;
1009 switch (p
->family
) {
1012 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
));
1015 byte
= p
->prefixlen
;
1016 if ((tmp
= p
->prefixlen
- 100) >= 0) {
1027 strlcpy(str
, buf
, size
);
1031 snprintf(str
, size
, "%s/%d",
1032 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1037 prefixevpn2str((const struct prefix_evpn
*)p
, str
, size
);
1041 strlcpy(str
, "FS prefix", size
);
1045 strlcpy(str
, "UNK prefix", size
);
1052 void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
1053 char *buf
, int buf_size
)
1055 int save_errno
= errno
;
1057 if (addr
.s_addr
== INADDR_ANY
)
1058 strlcpy(buf
, "*", buf_size
);
1060 if (!inet_ntop(AF_INET
, &addr
, buf
, buf_size
)) {
1062 snprintf(buf
, buf_size
, "%s", onfail
);
1069 const char *prefix_sg2str(const struct prefix_sg
*sg
, char *sg_str
)
1071 char src_str
[INET_ADDRSTRLEN
];
1072 char grp_str
[INET_ADDRSTRLEN
];
1074 prefix_mcast_inet4_dump("<src?>", sg
->src
, src_str
, sizeof(src_str
));
1075 prefix_mcast_inet4_dump("<grp?>", sg
->grp
, grp_str
, sizeof(grp_str
));
1076 snprintf(sg_str
, PREFIX_SG_STR_LEN
, "(%s,%s)", src_str
, grp_str
);
1081 struct prefix
*prefix_new(void)
1085 p
= XCALLOC(MTYPE_PREFIX
, sizeof(*p
));
1089 void prefix_free_lists(void *arg
)
1091 struct prefix
*p
= arg
;
1096 /* Free prefix structure. */
1097 void prefix_free(struct prefix
**p
)
1099 XFREE(MTYPE_PREFIX
, *p
);
1102 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1103 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1106 uint32_t destination
;
1108 destination
= ntohl(p
->prefix
.s_addr
);
1110 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
)
1112 /* do nothing for host routes */
1113 else if (IN_CLASSC(destination
)) {
1116 } else if (IN_CLASSB(destination
)) {
1125 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1127 struct in_addr mask
;
1129 masklen2ip(masklen
, &mask
);
1130 return (masklen
!= IPV4_MAX_PREFIXLEN
- 1) ?
1132 (hostaddr
| ~mask
.s_addr
)
1134 /* For prefix 31 return 255.255.255.255 (RFC3021) */
1138 /* Utility function to convert ipv4 netmask to prefixes
1139 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1140 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1141 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1144 struct in_addr network
;
1145 struct in_addr mask
;
1147 uint32_t destination
;
1150 ret
= inet_aton(net_str
, &network
);
1155 ret
= inet_aton(mask_str
, &mask
);
1159 prefixlen
= ip_masklen(mask
);
1161 destination
= ntohl(network
.s_addr
);
1163 if (network
.s_addr
== INADDR_ANY
)
1165 else if (IN_CLASSC(destination
))
1167 else if (IN_CLASSB(destination
))
1169 else if (IN_CLASSA(destination
))
1175 sprintf(prefix_str
, "%s/%d", net_str
, prefixlen
);
1180 /* Utility function for making IPv6 address string. */
1181 const char *inet6_ntoa(struct in6_addr addr
)
1183 static char buf
[INET6_ADDRSTRLEN
];
1185 inet_ntop(AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
1189 /* converts to internal representation of mac address
1190 * returns 1 on success, 0 otherwise
1191 * format accepted: AA:BB:CC:DD:EE:FF
1192 * if mac parameter is null, then check only
1194 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1202 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1205 /* error in incoming str length */
1208 /* valid mac address */
1211 for (i
= 0; i
< 6; ++i
)
1212 mac
->octet
[i
] = a
[i
] & 0xff;
1216 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1223 ptr
= XMALLOC(MTYPE_TMP
, ETHER_ADDR_STRLEN
* sizeof(char));
1225 assert(size
>= ETHER_ADDR_STRLEN
);
1228 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1229 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1230 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1231 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1235 unsigned prefix_hash_key(const void *pp
)
1239 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1243 /* make sure *all* unused bits are zero,
1244 * particularly including alignment /
1245 * padding and unused prefix bytes.
1247 memset(©
, 0, sizeof(copy
));
1248 prefix_copy(©
, (struct prefix
*)pp
);
1249 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1250 copy
.u
.prefix_flowspec
.prefixlen
,
1252 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1253 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1254 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1257 /* make sure *all* unused bits are zero, particularly including
1259 * padding and unused prefix bytes. */
1260 memset(©
, 0, sizeof(copy
));
1261 prefix_copy(©
, (struct prefix
*)pp
);
1263 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
1267 /* converts to internal representation of esi
1268 * returns 1 on success, 0 otherwise
1269 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1270 * if esi parameter is null, then check only
1272 int str_to_esi(const char *str
, esi_t
*esi
)
1275 unsigned int a
[ESI_BYTES
];
1280 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1281 a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1282 a
+ 4, a
+ 5, a
+ 6, a
+ 7,
1285 /* error in incoming str length */
1292 for (i
= 0; i
< ESI_BYTES
; ++i
)
1293 esi
->val
[i
] = a
[i
] & 0xff;
1297 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1304 ptr
= XMALLOC(MTYPE_TMP
, ESI_STR_LEN
* sizeof(char));
1306 assert(size
>= ESI_STR_LEN
);
1310 snprintf(ptr
, ESI_STR_LEN
,
1311 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1312 esi
->val
[0], esi
->val
[1], esi
->val
[2],
1313 esi
->val
[3], esi
->val
[4], esi
->val
[5],
1314 esi
->val
[6], esi
->val
[7], esi
->val
[8],
1319 printfrr_ext_autoreg_p("I4", printfrr_i4
)
1320 static ssize_t
printfrr_i4(char *buf
, size_t bsz
, const char *fmt
,
1321 int prec
, const void *ptr
)
1323 inet_ntop(AF_INET
, ptr
, buf
, bsz
);
1327 printfrr_ext_autoreg_p("I6", printfrr_i6
)
1328 static ssize_t
printfrr_i6(char *buf
, size_t bsz
, const char *fmt
,
1329 int prec
, const void *ptr
)
1331 inet_ntop(AF_INET6
, ptr
, buf
, bsz
);
1335 printfrr_ext_autoreg_p("FX", printfrr_pfx
)
1336 static ssize_t
printfrr_pfx(char *buf
, size_t bsz
, const char *fmt
,
1337 int prec
, const void *ptr
)
1339 prefix2str(ptr
, buf
, bsz
);
1343 printfrr_ext_autoreg_p("SG4", printfrr_psg
)
1344 static ssize_t
printfrr_psg(char *buf
, size_t bsz
, const char *fmt
,
1345 int prec
, const void *ptr
)
1347 const struct prefix_sg
*sg
= ptr
;
1348 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= bsz
- 1 };
1350 if (sg
->src
.s_addr
== INADDR_ANY
)
1351 bprintfrr(&fb
, "(*,");
1353 bprintfrr(&fb
, "(%pI4,", &sg
->src
);
1355 if (sg
->grp
.s_addr
== INADDR_ANY
)
1356 bprintfrr(&fb
, "*)");
1358 bprintfrr(&fb
, "%pI4)", &sg
->grp
);