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
27 #include "sockunion.h"
31 #include "lib_errors.h"
35 DEFINE_MTYPE_STATIC(LIB
, PREFIX
, "Prefix")
36 DEFINE_MTYPE_STATIC(LIB
, PREFIX_FLOWSPEC
, "Prefix Flowspec")
39 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
40 0xf8, 0xfc, 0xfe, 0xff};
42 /* Number of bits in prefix type. */
47 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
49 int is_zero_mac(const struct ethaddr
*mac
)
53 for (i
= 0; i
< ETH_ALEN
; i
++) {
61 bool is_bcast_mac(const struct ethaddr
*mac
)
65 for (i
= 0; i
< ETH_ALEN
; i
++)
66 if (mac
->octet
[i
] != 0xFF)
72 bool is_mcast_mac(const struct ethaddr
*mac
)
74 if ((mac
->octet
[0] & 0x01) == 0x01)
80 unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t bit_index
)
82 unsigned int offset
= bit_index
/ 8;
83 unsigned int shift
= 7 - (bit_index
% 8);
85 return (prefix
[offset
] >> shift
) & 1;
88 int str2family(const char *string
)
90 if (!strcmp("ipv4", string
))
92 else if (!strcmp("ipv6", string
))
94 else if (!strcmp("ethernet", string
))
96 else if (!strcmp("evpn", string
))
101 const char *family2str(int family
)
116 /* Address Famiy Identifier to Address Family converter. */
117 int afi2family(afi_t afi
)
121 else if (afi
== AFI_IP6
)
123 else if (afi
== AFI_L2VPN
)
125 /* NOTE: EVPN code should NOT use this interface. */
129 afi_t
family2afi(int family
)
131 if (family
== AF_INET
)
133 else if (family
== AF_INET6
)
135 else if (family
== AF_ETHERNET
|| family
== AF_EVPN
)
140 const char *afi2str(afi_t afi
)
157 const char *safi2str(safi_t safi
)
170 case SAFI_LABELED_UNICAST
:
171 return "labeled-unicast";
179 /* If n includes p prefix then return 1 else return 0. */
180 int prefix_match(const struct prefix
*n
, const struct prefix
*p
)
184 const uint8_t *np
, *pp
;
186 /* If n's prefix is longer than p's one return 0. */
187 if (n
->prefixlen
> p
->prefixlen
)
190 if (n
->family
== AF_FLOWSPEC
) {
191 /* prefixlen is unused. look at fs prefix len */
192 if (n
->u
.prefix_flowspec
.family
!=
193 p
->u
.prefix_flowspec
.family
)
196 if (n
->u
.prefix_flowspec
.prefixlen
>
197 p
->u
.prefix_flowspec
.prefixlen
)
200 /* Set both prefix's head pointer. */
201 np
= (const uint8_t *)&n
->u
.prefix_flowspec
.ptr
;
202 pp
= (const uint8_t *)&p
->u
.prefix_flowspec
.ptr
;
204 offset
= n
->u
.prefix_flowspec
.prefixlen
;
207 if (np
[offset
] != pp
[offset
])
212 /* Set both prefix's head pointer. */
216 offset
= n
->prefixlen
/ PNBBY
;
217 shift
= n
->prefixlen
% PNBBY
;
220 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
224 if (np
[offset
] != pp
[offset
])
231 * n is a type5 evpn prefix. This function tries to see if there is an
232 * ip-prefix within n which matches prefix p
233 * If n includes p prefix then return 1 else return 0.
235 int evpn_type5_prefix_match(const struct prefix
*n
, const struct prefix
*p
)
240 const uint8_t *np
, *pp
;
241 struct prefix_evpn
*evp
;
243 if (n
->family
!= AF_EVPN
)
246 evp
= (struct prefix_evpn
*)n
;
249 if ((evp
->prefix
.route_type
!= 5) ||
250 (p
->family
== AF_INET6
&& !is_evpn_prefix_ipaddr_v6(evp
)) ||
251 (p
->family
== AF_INET
&& !is_evpn_prefix_ipaddr_v4(evp
)) ||
252 (is_evpn_prefix_ipaddr_none(evp
)))
255 prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
256 np
= &evp
->prefix
.prefix_addr
.ip
.ip
.addr
;
258 /* If n's prefix is longer than p's one return 0. */
259 if (prefixlen
> p
->prefixlen
)
262 offset
= prefixlen
/ PNBBY
;
263 shift
= prefixlen
% PNBBY
;
266 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
270 if (np
[offset
] != pp
[offset
])
276 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
277 int prefix_match_network_statement(const struct prefix
*n
,
278 const struct prefix
*p
)
282 const uint8_t *np
, *pp
;
284 /* Set both prefix's head pointer. */
288 offset
= n
->prefixlen
/ PNBBY
;
289 shift
= n
->prefixlen
% PNBBY
;
292 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
296 if (np
[offset
] != pp
[offset
])
301 #ifdef __clang_analyzer__
302 #undef prefix_copy /* cf. prefix.h */
305 void prefix_copy(union prefixptr udest
, union prefixconstptr usrc
)
307 struct prefix
*dest
= udest
.p
;
308 const struct prefix
*src
= usrc
.p
;
310 dest
->family
= src
->family
;
311 dest
->prefixlen
= src
->prefixlen
;
313 if (src
->family
== AF_INET
)
314 dest
->u
.prefix4
= src
->u
.prefix4
;
315 else if (src
->family
== AF_INET6
)
316 dest
->u
.prefix6
= src
->u
.prefix6
;
317 else if (src
->family
== AF_ETHERNET
) {
318 memcpy(&dest
->u
.prefix_eth
, &src
->u
.prefix_eth
,
319 sizeof(struct ethaddr
));
320 } else if (src
->family
== AF_EVPN
) {
321 memcpy(&dest
->u
.prefix_evpn
, &src
->u
.prefix_evpn
,
322 sizeof(struct evpn_addr
));
323 } else if (src
->family
== AF_UNSPEC
) {
324 dest
->u
.lp
.id
= src
->u
.lp
.id
;
325 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
326 } else if (src
->family
== AF_FLOWSPEC
) {
330 len
= src
->u
.prefix_flowspec
.prefixlen
;
331 dest
->u
.prefix_flowspec
.prefixlen
=
332 src
->u
.prefix_flowspec
.prefixlen
;
333 dest
->u
.prefix_flowspec
.family
=
334 src
->u
.prefix_flowspec
.family
;
335 dest
->family
= src
->family
;
336 temp
= XCALLOC(MTYPE_PREFIX_FLOWSPEC
, len
);
337 dest
->u
.prefix_flowspec
.ptr
= (uintptr_t)temp
;
338 memcpy((void *)dest
->u
.prefix_flowspec
.ptr
,
339 (void *)src
->u
.prefix_flowspec
.ptr
, len
);
341 flog_err(EC_LIB_DEVELOPMENT
,
342 "prefix_copy(): Unknown address family %d",
349 * Return 1 if the address/netmask contained in the prefix structure
350 * is the same, and else return 0. For this routine, 'same' requires
351 * that not only the prefix length and the network part be the same,
352 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
353 * the same. Note that this routine has the same return value sense
354 * as '==' (which is different from prefix_cmp).
356 int prefix_same(union prefixconstptr up1
, union prefixconstptr up2
)
358 const struct prefix
*p1
= up1
.p
;
359 const struct prefix
*p2
= up2
.p
;
361 if ((p1
&& !p2
) || (!p1
&& p2
))
367 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
) {
368 if (p1
->family
== AF_INET
)
369 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
371 if (p1
->family
== AF_INET6
)
372 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
.s6_addr
,
373 &p2
->u
.prefix6
.s6_addr
))
375 if (p1
->family
== AF_ETHERNET
)
376 if (!memcmp(&p1
->u
.prefix_eth
, &p2
->u
.prefix_eth
,
377 sizeof(struct ethaddr
)))
379 if (p1
->family
== AF_EVPN
)
380 if (!memcmp(&p1
->u
.prefix_evpn
, &p2
->u
.prefix_evpn
,
381 sizeof(struct evpn_addr
)))
383 if (p1
->family
== AF_FLOWSPEC
) {
384 if (p1
->u
.prefix_flowspec
.family
!=
385 p2
->u
.prefix_flowspec
.family
)
387 if (p1
->u
.prefix_flowspec
.prefixlen
!=
388 p2
->u
.prefix_flowspec
.prefixlen
)
390 if (!memcmp(&p1
->u
.prefix_flowspec
.ptr
,
391 &p2
->u
.prefix_flowspec
.ptr
,
392 p2
->u
.prefix_flowspec
.prefixlen
))
400 * Return -1/0/1 comparing the prefixes in a way that gives a full/linear
403 * Network prefixes are considered the same if the prefix lengths are equal
404 * and the network parts are the same. Host bits (which are considered masked
405 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
406 * 10.0.0.2/8 are considered equivalent by this routine. Note that
407 * this routine has the same return sense as strcmp (which is different
410 int prefix_cmp(union prefixconstptr up1
, union prefixconstptr up2
)
412 const struct prefix
*p1
= up1
.p
;
413 const struct prefix
*p2
= up2
.p
;
418 /* Set both prefix's head pointer. */
422 if (p1
->family
!= p2
->family
)
423 return numcmp(p1
->family
, p2
->family
);
424 if (p1
->family
== AF_FLOWSPEC
) {
425 pp1
= (const uint8_t *)p1
->u
.prefix_flowspec
.ptr
;
426 pp2
= (const uint8_t *)p2
->u
.prefix_flowspec
.ptr
;
428 if (p1
->u
.prefix_flowspec
.family
!=
429 p2
->u
.prefix_flowspec
.family
)
432 if (p1
->u
.prefix_flowspec
.prefixlen
!=
433 p2
->u
.prefix_flowspec
.prefixlen
)
434 return numcmp(p1
->u
.prefix_flowspec
.prefixlen
,
435 p2
->u
.prefix_flowspec
.prefixlen
);
437 offset
= p1
->u
.prefix_flowspec
.prefixlen
;
439 if (pp1
[offset
] != pp2
[offset
])
440 return numcmp(pp1
[offset
], pp2
[offset
]);
446 if (p1
->prefixlen
!= p2
->prefixlen
)
447 return numcmp(p1
->prefixlen
, p2
->prefixlen
);
448 offset
= p1
->prefixlen
/ PNBBY
;
449 shift
= p1
->prefixlen
% PNBBY
;
451 i
= memcmp(pp1
, pp2
, offset
);
456 * At this point offset was the same, if we have shift
457 * that means we still have data to compare, if shift is
458 * 0 then we are at the end of the data structure
459 * and should just return, as that we will be accessing
460 * memory beyond the end of the party zone
463 return numcmp(pp1
[offset
] & maskbit
[shift
],
464 pp2
[offset
] & maskbit
[shift
]);
470 * Count the number of common bits in 2 prefixes. The prefix length is
471 * ignored for this function; the whole prefix is compared. If the prefix
472 * address families don't match, return -1; otherwise the return value is
473 * in range 0 ... maximum prefix length for the address family.
475 int prefix_common_bits(const struct prefix
*p1
, const struct prefix
*p2
)
481 /* Set both prefix's head pointer. */
482 const uint8_t *pp1
= p1
->u
.val
;
483 const uint8_t *pp2
= p2
->u
.val
;
485 if (p1
->family
== AF_INET
)
486 length
= IPV4_MAX_BYTELEN
;
487 if (p1
->family
== AF_INET6
)
488 length
= IPV6_MAX_BYTELEN
;
489 if (p1
->family
== AF_ETHERNET
)
491 if (p1
->family
== AF_EVPN
)
492 length
= 8 * sizeof(struct evpn_addr
);
494 if (p1
->family
!= p2
->family
|| !length
)
497 for (pos
= 0; pos
< length
; pos
++)
498 if (pp1
[pos
] != pp2
[pos
])
503 xor = pp1
[pos
] ^ pp2
[pos
];
504 for (bit
= 0; bit
< 8; bit
++)
505 if (xor&(1 << (7 - bit
)))
508 return pos
* 8 + bit
;
511 /* Return prefix family type string. */
512 const char *prefix_family_str(const struct prefix
*p
)
514 if (p
->family
== AF_INET
)
516 if (p
->family
== AF_INET6
)
518 if (p
->family
== AF_ETHERNET
)
520 if (p
->family
== AF_EVPN
)
525 /* Allocate new prefix_ipv4 structure. */
526 struct prefix_ipv4
*prefix_ipv4_new(void)
528 struct prefix_ipv4
*p
;
530 /* Call prefix_new to allocate a full-size struct prefix to avoid
532 where the struct prefix_ipv4 is cast to struct prefix and unallocated
533 bytes were being referenced (e.g. in structure assignments). */
534 p
= (struct prefix_ipv4
*)prefix_new();
539 /* Free prefix_ipv4 structure. */
540 void prefix_ipv4_free(struct prefix_ipv4
**p
)
542 prefix_free((struct prefix
**)p
);
545 /* If given string is valid return 1 else return 0 */
546 int str2prefix_ipv4(const char *str
, struct prefix_ipv4
*p
)
553 /* Find slash inside string. */
554 pnt
= strchr(str
, '/');
556 /* String doesn't contail slash. */
558 /* Convert string to prefix. */
559 ret
= inet_pton(AF_INET
, str
, &p
->prefix
);
563 /* If address doesn't contain slash we assume it host address.
566 p
->prefixlen
= IPV4_MAX_BITLEN
;
570 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
571 memcpy(cp
, str
, pnt
- str
);
572 *(cp
+ (pnt
- str
)) = '\0';
573 ret
= inet_pton(AF_INET
, cp
, &p
->prefix
);
574 XFREE(MTYPE_TMP
, cp
);
578 /* Get prefix length. */
579 plen
= (uint8_t)atoi(++pnt
);
580 if (plen
> IPV4_MAX_PREFIXLEN
)
590 /* When string format is invalid return 0. */
591 int str2prefix_eth(const char *str
, struct prefix_eth
*p
)
597 const char *str_addr
= str
;
602 if (!strcmp(str
, "any")) {
603 memset(p
, 0, sizeof(*p
));
604 p
->family
= AF_ETHERNET
;
608 /* Find slash inside string. */
609 pnt
= strchr(str
, '/');
612 /* Get prefix length. */
613 plen
= (uint8_t)atoi(++pnt
);
619 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
620 memcpy(cp
, str
, pnt
- str
);
621 *(cp
+ (pnt
- str
)) = '\0';
627 /* Convert string to prefix. */
628 if (sscanf(str_addr
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2,
634 for (i
= 0; i
< 6; ++i
) {
635 p
->eth_addr
.octet
[i
] = a
[i
] & 0xff;
638 p
->family
= AF_ETHERNET
;
641 * special case to allow old configurations to work
642 * Since all zero's is implicitly meant to allow
643 * a comparison to zero, let's assume
645 if (!slash
&& is_zero_mac(&(p
->eth_addr
)))
651 XFREE(MTYPE_TMP
, cp
);
656 /* Convert masklen into IP address's netmask (network byte order). */
657 void masklen2ip(const int masklen
, struct in_addr
*netmask
)
659 assert(masklen
>= 0 && masklen
<= IPV4_MAX_BITLEN
);
661 /* left shift is only defined for less than the size of the type.
662 * we unconditionally use long long in case the target platform
663 * has defined behaviour for << 32 (or has a 64-bit left shift) */
665 if (sizeof(unsigned long long) > 4)
666 netmask
->s_addr
= htonl(0xffffffffULL
<< (32 - masklen
));
669 htonl(masklen
? 0xffffffffU
<< (32 - masklen
) : 0);
672 /* Convert IP address's netmask into integer. We assume netmask is
673 * sequential one. Argument netmask should be network byte order. */
674 uint8_t ip_masklen(struct in_addr netmask
)
676 uint32_t tmp
= ~ntohl(netmask
.s_addr
);
679 * clz: count leading zeroes. sadly, the behaviour of this builtin is
680 * undefined for a 0 argument, even though most CPUs give 32
682 return tmp
? __builtin_clz(tmp
) : 32;
685 /* Apply mask to IPv4 prefix (network byte order). */
686 void apply_mask_ipv4(struct prefix_ipv4
*p
)
689 masklen2ip(p
->prefixlen
, &mask
);
690 p
->prefix
.s_addr
&= mask
.s_addr
;
693 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
694 int prefix_ipv4_any(const struct prefix_ipv4
*p
)
696 return (p
->prefix
.s_addr
== INADDR_ANY
&& p
->prefixlen
== 0);
699 /* Allocate a new ip version 6 route */
700 struct prefix_ipv6
*prefix_ipv6_new(void)
702 struct prefix_ipv6
*p
;
704 /* Allocate a full-size struct prefix to avoid problems with structure
706 p
= (struct prefix_ipv6
*)prefix_new();
707 p
->family
= AF_INET6
;
711 /* Free prefix for IPv6. */
712 void prefix_ipv6_free(struct prefix_ipv6
**p
)
714 prefix_free((struct prefix
**)p
);
717 /* If given string is valid return 1 else return 0 */
718 int str2prefix_ipv6(const char *str
, struct prefix_ipv6
*p
)
724 pnt
= strchr(str
, '/');
726 /* If string doesn't contain `/' treat it as host route. */
728 ret
= inet_pton(AF_INET6
, str
, &p
->prefix
);
731 p
->prefixlen
= IPV6_MAX_BITLEN
;
735 cp
= XMALLOC(MTYPE_TMP
, (pnt
- str
) + 1);
736 memcpy(cp
, str
, pnt
- str
);
737 *(cp
+ (pnt
- str
)) = '\0';
738 ret
= inet_pton(AF_INET6
, cp
, &p
->prefix
);
739 XFREE(MTYPE_TMP
, cp
);
742 plen
= (uint8_t)atoi(++pnt
);
743 if (plen
> IPV6_MAX_BITLEN
)
747 p
->family
= AF_INET6
;
752 /* Convert struct in6_addr netmask into integer.
753 * FIXME return uint8_t as ip_maskleni() does. */
754 int ip6_masklen(struct in6_addr netmask
)
756 if (netmask
.s6_addr32
[0] != 0xffffffffU
)
757 return __builtin_clz(~ntohl(netmask
.s6_addr32
[0]));
758 if (netmask
.s6_addr32
[1] != 0xffffffffU
)
759 return __builtin_clz(~ntohl(netmask
.s6_addr32
[1])) + 32;
760 if (netmask
.s6_addr32
[2] != 0xffffffffU
)
761 return __builtin_clz(~ntohl(netmask
.s6_addr32
[2])) + 64;
762 if (netmask
.s6_addr32
[3] != 0xffffffffU
)
763 return __builtin_clz(~ntohl(netmask
.s6_addr32
[3])) + 96;
764 /* note __builtin_clz(0) is undefined */
768 void masklen2ip6(const int masklen
, struct in6_addr
*netmask
)
770 assert(masklen
>= 0 && masklen
<= IPV6_MAX_BITLEN
);
773 /* note << 32 is undefined */
774 memset(netmask
, 0, sizeof(*netmask
));
775 } else if (masklen
<= 32) {
776 netmask
->s6_addr32
[0] = htonl(0xffffffffU
<< (32 - masklen
));
777 netmask
->s6_addr32
[1] = 0;
778 netmask
->s6_addr32
[2] = 0;
779 netmask
->s6_addr32
[3] = 0;
780 } else if (masklen
<= 64) {
781 netmask
->s6_addr32
[0] = 0xffffffffU
;
782 netmask
->s6_addr32
[1] = htonl(0xffffffffU
<< (64 - masklen
));
783 netmask
->s6_addr32
[2] = 0;
784 netmask
->s6_addr32
[3] = 0;
785 } else if (masklen
<= 96) {
786 netmask
->s6_addr32
[0] = 0xffffffffU
;
787 netmask
->s6_addr32
[1] = 0xffffffffU
;
788 netmask
->s6_addr32
[2] = htonl(0xffffffffU
<< (96 - masklen
));
789 netmask
->s6_addr32
[3] = 0;
791 netmask
->s6_addr32
[0] = 0xffffffffU
;
792 netmask
->s6_addr32
[1] = 0xffffffffU
;
793 netmask
->s6_addr32
[2] = 0xffffffffU
;
794 netmask
->s6_addr32
[3] = htonl(0xffffffffU
<< (128 - masklen
));
798 void apply_mask_ipv6(struct prefix_ipv6
*p
)
804 index
= p
->prefixlen
/ 8;
807 pnt
= (uint8_t *)&p
->prefix
;
808 offset
= p
->prefixlen
% 8;
810 pnt
[index
] &= maskbit
[offset
];
818 void apply_mask(struct prefix
*p
)
822 apply_mask_ipv4((struct prefix_ipv4
*)p
);
825 apply_mask_ipv6((struct prefix_ipv6
*)p
);
833 /* Utility function of convert between struct prefix <=> union sockunion. */
834 struct prefix
*sockunion2hostprefix(const union sockunion
*su
,
835 struct prefix
*prefix
)
837 if (su
->sa
.sa_family
== AF_INET
) {
838 struct prefix_ipv4
*p
;
840 p
= prefix
? (struct prefix_ipv4
*)prefix
: prefix_ipv4_new();
842 p
->prefix
= su
->sin
.sin_addr
;
843 p
->prefixlen
= IPV4_MAX_BITLEN
;
844 return (struct prefix
*)p
;
846 if (su
->sa
.sa_family
== AF_INET6
) {
847 struct prefix_ipv6
*p
;
849 p
= prefix
? (struct prefix_ipv6
*)prefix
: prefix_ipv6_new();
850 p
->family
= AF_INET6
;
851 p
->prefixlen
= IPV6_MAX_BITLEN
;
852 memcpy(&p
->prefix
, &su
->sin6
.sin6_addr
,
853 sizeof(struct in6_addr
));
854 return (struct prefix
*)p
;
859 void prefix2sockunion(const struct prefix
*p
, union sockunion
*su
)
861 memset(su
, 0, sizeof(*su
));
863 su
->sa
.sa_family
= p
->family
;
864 if (p
->family
== AF_INET
)
865 su
->sin
.sin_addr
= p
->u
.prefix4
;
866 if (p
->family
== AF_INET6
)
867 memcpy(&su
->sin6
.sin6_addr
, &p
->u
.prefix6
,
868 sizeof(struct in6_addr
));
871 int prefix_blen(const struct prefix
*p
)
875 return IPV4_MAX_BYTELEN
;
877 return IPV6_MAX_BYTELEN
;
884 /* Generic function for conversion string to struct prefix. */
885 int str2prefix(const char *str
, struct prefix
*p
)
892 /* First we try to convert string to struct prefix_ipv4. */
893 ret
= str2prefix_ipv4(str
, (struct prefix_ipv4
*)p
);
897 /* Next we try to convert string to struct prefix_ipv6. */
898 ret
= str2prefix_ipv6(str
, (struct prefix_ipv6
*)p
);
902 /* Next we try to convert string to struct prefix_eth. */
903 ret
= str2prefix_eth(str
, (struct prefix_eth
*)p
);
910 static const char *prefixevpn_ead2str(const struct prefix_evpn
*p
, char *str
,
914 char buf
[ESI_STR_LEN
];
915 char buf1
[INET6_ADDRSTRLEN
];
917 family
= IS_IPADDR_V4(&p
->prefix
.ead_addr
.ip
) ? AF_INET
: AF_INET6
;
918 snprintf(str
, size
, "[%d]:[%u]:[%s]:[%d]:[%s]", p
->prefix
.route_type
,
919 p
->prefix
.ead_addr
.eth_tag
,
920 esi_to_str(&p
->prefix
.ead_addr
.esi
, buf
, sizeof(buf
)),
921 (family
== AF_INET
) ? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
,
922 inet_ntop(family
, &p
->prefix
.ead_addr
.ip
.ipaddr_v4
, buf1
,
927 static const char *prefixevpn_macip2str(const struct prefix_evpn
*p
, char *str
,
931 char buf1
[ETHER_ADDR_STRLEN
];
932 char buf2
[PREFIX2STR_BUFFER
];
934 if (is_evpn_prefix_ipaddr_none(p
))
935 snprintf(str
, size
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
936 p
->prefix
.macip_addr
.eth_tag
, 8 * ETH_ALEN
,
937 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
940 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
941 snprintf(str
, size
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
942 p
->prefix
.route_type
, p
->prefix
.macip_addr
.eth_tag
,
944 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
946 family
== AF_INET
? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
,
947 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
948 buf2
, PREFIX2STR_BUFFER
));
953 static const char *prefixevpn_imet2str(const struct prefix_evpn
*p
, char *str
,
957 char buf
[INET6_ADDRSTRLEN
];
959 family
= IS_IPADDR_V4(&p
->prefix
.imet_addr
.ip
) ? AF_INET
: AF_INET6
;
960 snprintf(str
, size
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
961 p
->prefix
.imet_addr
.eth_tag
,
962 (family
== AF_INET
) ? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
,
963 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
, buf
,
969 static const char *prefixevpn_es2str(const struct prefix_evpn
*p
, char *str
,
973 char buf
[ESI_STR_LEN
];
974 char buf1
[INET6_ADDRSTRLEN
];
976 family
= IS_IPADDR_V4(&p
->prefix
.es_addr
.ip
) ? AF_INET
: AF_INET6
;
977 snprintf(str
, size
, "[%d]:[%s]:[%d]:[%s]", p
->prefix
.route_type
,
978 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
979 (family
== AF_INET
) ? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
,
980 inet_ntop(family
, &p
->prefix
.es_addr
.ip
.ipaddr_v4
, buf1
,
986 static const char *prefixevpn_prefix2str(const struct prefix_evpn
*p
, char *str
,
990 char buf
[INET6_ADDRSTRLEN
];
992 family
= IS_IPADDR_V4(&p
->prefix
.prefix_addr
.ip
) ? AF_INET
: AF_INET6
;
993 snprintf(str
, size
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
994 p
->prefix
.prefix_addr
.eth_tag
,
995 p
->prefix
.prefix_addr
.ip_prefix_length
,
996 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ipaddr_v4
, buf
,
1001 static const char *prefixevpn2str(const struct prefix_evpn
*p
, char *str
,
1004 switch (p
->prefix
.route_type
) {
1005 case BGP_EVPN_AD_ROUTE
:
1006 return prefixevpn_ead2str(p
, str
, size
);
1007 case BGP_EVPN_MAC_IP_ROUTE
:
1008 return prefixevpn_macip2str(p
, str
, size
);
1009 case BGP_EVPN_IMET_ROUTE
:
1010 return prefixevpn_imet2str(p
, str
, size
);
1011 case BGP_EVPN_ES_ROUTE
:
1012 return prefixevpn_es2str(p
, str
, size
);
1013 case BGP_EVPN_IP_PREFIX_ROUTE
:
1014 return prefixevpn_prefix2str(p
, str
, size
);
1016 snprintf(str
, size
, "Unsupported EVPN prefix");
1022 const char *prefix2str(union prefixconstptr pu
, char *str
, int size
)
1024 const struct prefix
*p
= pu
.p
;
1025 char buf
[PREFIX2STR_BUFFER
];
1026 int byte
, tmp
, a
, b
;
1030 switch (p
->family
) {
1033 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
));
1036 byte
= p
->prefixlen
;
1037 if ((tmp
= p
->prefixlen
- 100) >= 0) {
1048 strlcpy(str
, buf
, size
);
1052 snprintf(str
, size
, "%s/%d",
1053 prefix_mac2str(&p
->u
.prefix_eth
, buf
, sizeof(buf
)),
1058 prefixevpn2str((const struct prefix_evpn
*)p
, str
, size
);
1062 strlcpy(str
, "FS prefix", size
);
1066 strlcpy(str
, "UNK prefix", size
);
1073 void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
1074 char *buf
, int buf_size
)
1076 int save_errno
= errno
;
1078 if (addr
.s_addr
== INADDR_ANY
)
1079 strlcpy(buf
, "*", buf_size
);
1081 if (!inet_ntop(AF_INET
, &addr
, buf
, buf_size
)) {
1083 snprintf(buf
, buf_size
, "%s", onfail
);
1090 const char *prefix_sg2str(const struct prefix_sg
*sg
, char *sg_str
)
1092 char src_str
[INET_ADDRSTRLEN
];
1093 char grp_str
[INET_ADDRSTRLEN
];
1095 prefix_mcast_inet4_dump("<src?>", sg
->src
, src_str
, sizeof(src_str
));
1096 prefix_mcast_inet4_dump("<grp?>", sg
->grp
, grp_str
, sizeof(grp_str
));
1097 snprintf(sg_str
, PREFIX_SG_STR_LEN
, "(%s,%s)", src_str
, grp_str
);
1102 struct prefix
*prefix_new(void)
1106 p
= XCALLOC(MTYPE_PREFIX
, sizeof(*p
));
1110 void prefix_free_lists(void *arg
)
1112 struct prefix
*p
= arg
;
1117 /* Free prefix structure. */
1118 void prefix_free(struct prefix
**p
)
1120 XFREE(MTYPE_PREFIX
, *p
);
1123 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1124 void apply_classful_mask_ipv4(struct prefix_ipv4
*p
)
1127 uint32_t destination
;
1129 destination
= ntohl(p
->prefix
.s_addr
);
1131 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
)
1133 /* do nothing for host routes */
1134 else if (IN_CLASSC(destination
)) {
1137 } else if (IN_CLASSB(destination
)) {
1146 in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
)
1148 struct in_addr mask
;
1150 masklen2ip(masklen
, &mask
);
1151 return (masklen
!= IPV4_MAX_PREFIXLEN
- 1) ?
1153 (hostaddr
| ~mask
.s_addr
)
1155 /* For prefix 31 return 255.255.255.255 (RFC3021) */
1159 /* Utility function to convert ipv4 netmask to prefixes
1160 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1161 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
1162 int netmask_str2prefix_str(const char *net_str
, const char *mask_str
,
1165 struct in_addr network
;
1166 struct in_addr mask
;
1168 uint32_t destination
;
1171 ret
= inet_aton(net_str
, &network
);
1176 ret
= inet_aton(mask_str
, &mask
);
1180 prefixlen
= ip_masklen(mask
);
1182 destination
= ntohl(network
.s_addr
);
1184 if (network
.s_addr
== INADDR_ANY
)
1186 else if (IN_CLASSC(destination
))
1188 else if (IN_CLASSB(destination
))
1190 else if (IN_CLASSA(destination
))
1196 sprintf(prefix_str
, "%s/%d", net_str
, prefixlen
);
1201 /* Utility function for making IPv6 address string. */
1202 const char *inet6_ntoa(struct in6_addr addr
)
1204 static char buf
[INET6_ADDRSTRLEN
];
1206 inet_ntop(AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
1210 /* converts to internal representation of mac address
1211 * returns 1 on success, 0 otherwise
1212 * format accepted: AA:BB:CC:DD:EE:FF
1213 * if mac parameter is null, then check only
1215 int prefix_str2mac(const char *str
, struct ethaddr
*mac
)
1223 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x", a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1226 /* error in incoming str length */
1229 /* valid mac address */
1232 for (i
= 0; i
< 6; ++i
)
1233 mac
->octet
[i
] = a
[i
] & 0xff;
1237 char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
)
1244 ptr
= XMALLOC(MTYPE_TMP
, ETHER_ADDR_STRLEN
* sizeof(char));
1246 assert(size
>= ETHER_ADDR_STRLEN
);
1249 snprintf(ptr
, (ETHER_ADDR_STRLEN
), "%02x:%02x:%02x:%02x:%02x:%02x",
1250 (uint8_t)mac
->octet
[0], (uint8_t)mac
->octet
[1],
1251 (uint8_t)mac
->octet
[2], (uint8_t)mac
->octet
[3],
1252 (uint8_t)mac
->octet
[4], (uint8_t)mac
->octet
[5]);
1256 unsigned prefix_hash_key(const void *pp
)
1260 if (((struct prefix
*)pp
)->family
== AF_FLOWSPEC
) {
1264 /* make sure *all* unused bits are zero,
1265 * particularly including alignment /
1266 * padding and unused prefix bytes.
1268 memset(©
, 0, sizeof(copy
));
1269 prefix_copy(©
, (struct prefix
*)pp
);
1270 len
= jhash((void *)copy
.u
.prefix_flowspec
.ptr
,
1271 copy
.u
.prefix_flowspec
.prefixlen
,
1273 temp
= (void *)copy
.u
.prefix_flowspec
.ptr
;
1274 XFREE(MTYPE_PREFIX_FLOWSPEC
, temp
);
1275 copy
.u
.prefix_flowspec
.ptr
= (uintptr_t)NULL
;
1278 /* make sure *all* unused bits are zero, particularly including
1280 * padding and unused prefix bytes. */
1281 memset(©
, 0, sizeof(copy
));
1282 prefix_copy(©
, (struct prefix
*)pp
);
1284 offsetof(struct prefix
, u
.prefix
) + PSIZE(copy
.prefixlen
),
1288 /* converts to internal representation of esi
1289 * returns 1 on success, 0 otherwise
1290 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1291 * if esi parameter is null, then check only
1293 int str_to_esi(const char *str
, esi_t
*esi
)
1296 unsigned int a
[ESI_BYTES
];
1301 if (sscanf(str
, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1302 a
+ 0, a
+ 1, a
+ 2, a
+ 3,
1303 a
+ 4, a
+ 5, a
+ 6, a
+ 7,
1306 /* error in incoming str length */
1313 for (i
= 0; i
< ESI_BYTES
; ++i
)
1314 esi
->val
[i
] = a
[i
] & 0xff;
1318 char *esi_to_str(const esi_t
*esi
, char *buf
, int size
)
1325 ptr
= XMALLOC(MTYPE_TMP
, ESI_STR_LEN
* sizeof(char));
1327 assert(size
>= ESI_STR_LEN
);
1331 snprintf(ptr
, ESI_STR_LEN
,
1332 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1333 esi
->val
[0], esi
->val
[1], esi
->val
[2],
1334 esi
->val
[3], esi
->val
[4], esi
->val
[5],
1335 esi
->val
[6], esi
->val
[7], esi
->val
[8],
1340 char *evpn_es_df_alg2str(uint8_t df_alg
, char *buf
, int buf_len
)
1343 case EVPN_MH_DF_ALG_SERVICE_CARVING
:
1344 snprintf(buf
, buf_len
, "service-carving");
1347 case EVPN_MH_DF_ALG_HRW
:
1348 snprintf(buf
, buf_len
, "HRW");
1351 case EVPN_MH_DF_ALG_PREF
:
1352 snprintf(buf
, buf_len
, "preference");
1356 snprintf(buf
, buf_len
, "unknown %u", df_alg
);
1363 printfrr_ext_autoreg_p("EA", printfrr_ea
)
1364 static ssize_t
printfrr_ea(char *buf
, size_t bsz
, const char *fmt
,
1365 int prec
, const void *ptr
)
1367 const struct ethaddr
*mac
= ptr
;
1369 prefix_mac2str(mac
, buf
, bsz
);
1373 printfrr_ext_autoreg_p("IA", printfrr_ia
)
1374 static ssize_t
printfrr_ia(char *buf
, size_t bsz
, const char *fmt
,
1375 int prec
, const void *ptr
)
1377 const struct ipaddr
*ipa
= ptr
;
1379 ipaddr2str(ipa
, buf
, bsz
);
1383 printfrr_ext_autoreg_p("I4", printfrr_i4
)
1384 static ssize_t
printfrr_i4(char *buf
, size_t bsz
, const char *fmt
,
1385 int prec
, const void *ptr
)
1387 inet_ntop(AF_INET
, ptr
, buf
, bsz
);
1391 printfrr_ext_autoreg_p("I6", printfrr_i6
)
1392 static ssize_t
printfrr_i6(char *buf
, size_t bsz
, const char *fmt
,
1393 int prec
, const void *ptr
)
1395 inet_ntop(AF_INET6
, ptr
, buf
, bsz
);
1399 printfrr_ext_autoreg_p("FX", printfrr_pfx
)
1400 static ssize_t
printfrr_pfx(char *buf
, size_t bsz
, const char *fmt
,
1401 int prec
, const void *ptr
)
1403 prefix2str(ptr
, buf
, bsz
);
1407 printfrr_ext_autoreg_p("SG4", printfrr_psg
)
1408 static ssize_t
printfrr_psg(char *buf
, size_t bsz
, const char *fmt
,
1409 int prec
, const void *ptr
)
1411 const struct prefix_sg
*sg
= ptr
;
1412 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= bsz
- 1 };
1414 if (sg
->src
.s_addr
== INADDR_ANY
)
1415 bprintfrr(&fb
, "(*,");
1417 bprintfrr(&fb
, "(%pI4,", &sg
->src
);
1419 if (sg
->grp
.s_addr
== INADDR_ANY
)
1420 bprintfrr(&fb
, "*)");
1422 bprintfrr(&fb
, "%pI4)", &sg
->grp
);