]> git.proxmox.com Git - mirror_frr.git/blame - lib/prefix.h
Merge pull request #8978 from kssoman/ospf_new
[mirror_frr.git] / lib / prefix.h
CommitLineData
718e3744 1/*
2 * Prefix structure.
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * later version.
11 *
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.
16 *
896014f4
DL
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
718e3744 20 */
21
22#ifndef _ZEBRA_PREFIX_H
23#define _ZEBRA_PREFIX_H
24
d62a17ae 25#ifdef GNU_LINUX
26#include <net/ethernet.h>
27#else
28#include <netinet/if_ether.h>
29#endif
8cc4198f 30#include "sockunion.h"
86f1ef44 31#include "ipaddr.h"
de1a880c 32#include "compiler.h"
8cc4198f 33
5e244469
RW
34#ifdef __cplusplus
35extern "C" {
36#endif
37
7628d862
DS
38#ifndef ETH_ALEN
39#define ETH_ALEN 6
40#endif
41
2ff12693
AK
42/* EVPN route types. */
43typedef enum {
44 BGP_EVPN_AD_ROUTE = 1, /* Ethernet Auto-Discovery (A-D) route */
45 BGP_EVPN_MAC_IP_ROUTE, /* MAC/IP Advertisement route */
46 BGP_EVPN_IMET_ROUTE, /* Inclusive Multicast Ethernet Tag route */
47 BGP_EVPN_ES_ROUTE, /* Ethernet Segment route */
48 BGP_EVPN_IP_PREFIX_ROUTE, /* IP Prefix route */
49} bgp_evpn_route_type;
50
19bfb274
AK
51/* value of first byte of ESI */
52#define ESI_TYPE_ARBITRARY 0 /* */
53#define ESI_TYPE_LACP 1 /* <> */
54#define ESI_TYPE_BRIDGE 2 /* <Root bridge Mac-6B>:<Root Br Priority-2B>:00 */
55#define ESI_TYPE_MAC 3 /* <Syst Mac Add-6B>:<Local Discriminator Value-3B> */
56#define ESI_TYPE_ROUTER 4 /* <RouterId-4B>:<Local Discriminator Value-4B> */
57#define ESI_TYPE_AS 5 /* <AS-4B>:<Local Discriminator Value-4B> */
58
59#define MAX_ESI {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
60
61
c44ab6f1 62#define EVPN_ETH_TAG_BYTES 4
50f74cf1 63#define ESI_BYTES 10
2bb9eff4 64#define ESI_STR_LEN (3 * ESI_BYTES)
74e2bd89 65#define EVPN_DF_ALG_STR_LEN 24
50f74cf1 66
19bfb274
AK
67/* Maximum number of VTEPs per-ES -
68 * XXX - temporary limit for allocating strings etc.
69 */
70#define ES_VTEP_MAX_CNT 10
71#define ES_VTEP_LIST_STR_SZ (ES_VTEP_MAX_CNT * 16)
72
7628d862 73#define ETHER_ADDR_STRLEN (3*ETH_ALEN)
32ac65d9
LB
74/*
75 * there isn't a portable ethernet address type. We define our
76 * own to simplify internal handling
77 */
78struct ethaddr {
d7c0a89a 79 uint8_t octet[ETH_ALEN];
d62a17ae 80} __attribute__((packed));
32ac65d9
LB
81
82
d62a17ae 83/* length is the number of valuable bits of prefix structure
9d303b37
DL
84* 18 bytes is current length in structure, if address is ipv4
85* 30 bytes is in case of ipv6
86*/
a440846e 87#define PREFIX_LEN_ROUTE_TYPE_5_IPV4 (18*8)
88#define PREFIX_LEN_ROUTE_TYPE_5_IPV6 (30*8)
89
3714a385 90typedef struct esi_t_ {
19bfb274 91 uint8_t val[ESI_BYTES];
3714a385 92} esi_t;
93
94struct evpn_ead_addr {
95 esi_t esi;
96 uint32_t eth_tag;
19bfb274 97 struct ipaddr ip;
3714a385 98};
99
100struct evpn_macip_addr {
101 uint32_t eth_tag;
d7c0a89a 102 uint8_t ip_prefix_length;
d62a17ae 103 struct ethaddr mac;
3714a385 104 struct ipaddr ip;
105};
106
107struct evpn_imet_addr {
d62a17ae 108 uint32_t eth_tag;
3714a385 109 uint8_t ip_prefix_length;
110 struct ipaddr ip;
111};
112
113struct evpn_es_addr {
114 esi_t esi;
115 uint8_t ip_prefix_length;
d62a17ae 116 struct ipaddr ip;
a440846e 117};
118
3714a385 119struct evpn_prefix_addr {
120 uint32_t eth_tag;
121 uint8_t ip_prefix_length;
122 struct ipaddr ip;
123};
124
125/* EVPN address (RFC 7432) */
126struct evpn_addr {
127 uint8_t route_type;
128 union {
129 struct evpn_ead_addr _ead_addr;
130 struct evpn_macip_addr _macip_addr;
131 struct evpn_imet_addr _imet_addr;
132 struct evpn_es_addr _es_addr;
133 struct evpn_prefix_addr _prefix_addr;
134 } u;
135#define ead_addr u._ead_addr
136#define macip_addr u._macip_addr
137#define imet_addr u._imet_addr
138#define es_addr u._es_addr
139#define prefix_addr u._prefix_addr
140};
a440846e 141
9d24baaa 142/*
143 * A struct prefix contains an address family, a prefix length, and an
144 * address. This can represent either a 'network prefix' as defined
145 * by CIDR, where the 'host bits' of the prefix are 0
146 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
147 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
148 * interface.
149 */
150
32ac65d9
LB
151/* different OSes use different names */
152#if defined(AF_PACKET)
153#define AF_ETHERNET AF_PACKET
154#else
155#if defined(AF_LINK)
156#define AF_ETHERNET AF_LINK
157#endif
158#endif
159
b03b8898
DS
160/* The 'family' in the prefix structure is internal to FRR and need not
161 * map to standard OS AF_ definitions except where needed for interacting
162 * with the kernel. However, AF_ definitions are currently in use and
163 * prevalent across the code. Define a new FRR-specific AF for EVPN to
164 * distinguish between 'ethernet' (MAC-only) and 'evpn' prefixes and
165 * ensure it does not conflict with any OS AF_ definition.
166 */
167#if !defined(AF_EVPN)
168#define AF_EVPN (AF_MAX + 1)
169#endif
170
9a14899b
PG
171#if !defined(AF_FLOWSPEC)
172#define AF_FLOWSPEC (AF_MAX + 2)
173#endif
174
175struct flowspec_prefix {
e4552d66 176 uint8_t family;
9a14899b
PG
177 uint16_t prefixlen; /* length in bytes */
178 uintptr_t ptr;
179};
180
b03b8898 181/* FRR generic prefix structure. */
d62a17ae 182struct prefix {
d7c0a89a 183 uint8_t family;
f93eee44 184 uint16_t prefixlen;
d62a17ae 185 union {
d7c0a89a 186 uint8_t prefix;
d62a17ae 187 struct in_addr prefix4;
188 struct in6_addr prefix6;
189 struct {
190 struct in_addr id;
191 struct in_addr adv_router;
192 } lp;
193 struct ethaddr prefix_eth; /* AF_ETHERNET */
d7c0a89a 194 uint8_t val[16];
6a923ca4 195 uint32_t val32[4];
d62a17ae 196 uintptr_t ptr;
b03b8898 197 struct evpn_addr prefix_evpn; /* AF_EVPN */
9a14899b 198 struct flowspec_prefix prefix_flowspec; /* AF_FLOWSPEC */
d62a17ae 199 } u __attribute__((aligned(8)));
718e3744 200};
201
202/* IPv4 prefix structure. */
d62a17ae 203struct prefix_ipv4 {
d7c0a89a 204 uint8_t family;
f93eee44 205 uint16_t prefixlen;
d62a17ae 206 struct in_addr prefix __attribute__((aligned(8)));
718e3744 207};
208
209/* IPv6 prefix structure. */
d62a17ae 210struct prefix_ipv6 {
d7c0a89a 211 uint8_t family;
f93eee44 212 uint16_t prefixlen;
d62a17ae 213 struct in6_addr prefix __attribute__((aligned(8)));
718e3744 214};
718e3744 215
d62a17ae 216struct prefix_ls {
d7c0a89a 217 uint8_t family;
f93eee44 218 uint16_t prefixlen;
d62a17ae 219 struct in_addr id __attribute__((aligned(8)));
220 struct in_addr adv_router;
718e3744 221};
222
223/* Prefix for routing distinguisher. */
d62a17ae 224struct prefix_rd {
d7c0a89a 225 uint8_t family;
f93eee44 226 uint16_t prefixlen;
d7c0a89a 227 uint8_t val[8] __attribute__((aligned(8)));
718e3744 228};
229
32ac65d9 230/* Prefix for ethernet. */
d62a17ae 231struct prefix_eth {
d7c0a89a 232 uint8_t family;
f93eee44 233 uint16_t prefixlen;
d62a17ae 234 struct ethaddr eth_addr __attribute__((aligned(8))); /* AF_ETHERNET */
32ac65d9
LB
235};
236
86f1ef44 237/* EVPN prefix structure. */
d62a17ae 238struct prefix_evpn {
d7c0a89a 239 uint8_t family;
f93eee44 240 uint16_t prefixlen;
d62a17ae 241 struct evpn_addr prefix __attribute__((aligned(8)));
86f1ef44 242};
243
3714a385 244static inline int is_evpn_prefix_ipaddr_none(const struct prefix_evpn *evp)
245{
2ff12693 246 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
19bfb274 247 return IS_IPADDR_NONE(&(evp)->prefix.ead_addr.ip);
2ff12693 248 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 249 return IS_IPADDR_NONE(&(evp)->prefix.macip_addr.ip);
2ff12693 250 if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
3714a385 251 return IS_IPADDR_NONE(&(evp)->prefix.imet_addr.ip);
2ff12693 252 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
50f74cf1 253 return IS_IPADDR_NONE(&(evp)->prefix.es_addr.ip);
2ff12693 254 if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
3714a385 255 return IS_IPADDR_NONE(&(evp)->prefix.prefix_addr.ip);
256 return 0;
257}
258
259static inline int is_evpn_prefix_ipaddr_v4(const struct prefix_evpn *evp)
260{
2ff12693 261 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
19bfb274 262 return IS_IPADDR_V4(&(evp)->prefix.ead_addr.ip);
2ff12693 263 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 264 return IS_IPADDR_V4(&(evp)->prefix.macip_addr.ip);
2ff12693 265 if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
3714a385 266 return IS_IPADDR_V4(&(evp)->prefix.imet_addr.ip);
2ff12693 267 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
50f74cf1 268 return IS_IPADDR_V4(&(evp)->prefix.es_addr.ip);
2ff12693 269 if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
3714a385 270 return IS_IPADDR_V4(&(evp)->prefix.prefix_addr.ip);
271 return 0;
272}
273
274static inline int is_evpn_prefix_ipaddr_v6(const struct prefix_evpn *evp)
275{
2ff12693 276 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
19bfb274 277 return IS_IPADDR_V6(&(evp)->prefix.ead_addr.ip);
2ff12693 278 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 279 return IS_IPADDR_V6(&(evp)->prefix.macip_addr.ip);
2ff12693 280 if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
3714a385 281 return IS_IPADDR_V6(&(evp)->prefix.imet_addr.ip);
2ff12693 282 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
50f74cf1 283 return IS_IPADDR_V6(&(evp)->prefix.es_addr.ip);
2ff12693 284 if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
3714a385 285 return IS_IPADDR_V6(&(evp)->prefix.prefix_addr.ip);
286 return 0;
287}
288
b4b359a2 289/* Prefix for a generic pointer */
d62a17ae 290struct prefix_ptr {
d7c0a89a 291 uint8_t family;
f93eee44 292 uint16_t prefixlen;
d62a17ae 293 uintptr_t prefix __attribute__((aligned(8)));
b4b359a2
PM
294};
295
9a14899b
PG
296/* Prefix for a Flowspec entry */
297struct prefix_fs {
298 uint8_t family;
f93eee44 299 uint16_t prefixlen; /* unused */
9a14899b
PG
300 struct flowspec_prefix prefix __attribute__((aligned(8)));
301};
302
d62a17ae 303struct prefix_sg {
d7c0a89a 304 uint8_t family;
f93eee44 305 uint16_t prefixlen;
d62a17ae 306 struct in_addr src __attribute__((aligned(8)));
307 struct in_addr grp;
e9219290
DS
308};
309
f7bf4153
DL
310/* helper to get type safety/avoid casts on calls
311 * (w/o this, functions accepting all prefix types need casts on the caller
312 * side, which strips type safety since the cast will accept any pointer
313 * type.)
314 */
be566e4e
DL
315#ifndef __cplusplus
316#define prefixtype(uname, typename, fieldname) \
317 typename *fieldname;
5d387792 318#define TRANSPARENT_UNION __attribute__((transparent_union))
be566e4e
DL
319#else
320#define prefixtype(uname, typename, fieldname) \
321 typename *fieldname; \
322 uname(typename *x) { this->fieldname = x; }
5d387792 323#define TRANSPARENT_UNION
be566e4e
DL
324#endif
325
d62a17ae 326union prefixptr {
be566e4e
DL
327 prefixtype(prefixptr, struct prefix, p)
328 prefixtype(prefixptr, struct prefix_ipv4, p4)
329 prefixtype(prefixptr, struct prefix_ipv6, p6)
330 prefixtype(prefixptr, struct prefix_evpn, evp)
331 prefixtype(prefixptr, struct prefix_fs, fs)
132b6a91 332 prefixtype(prefixptr, struct prefix_rd, rd)
5d387792 333} TRANSPARENT_UNION;
d62a17ae 334
335union prefixconstptr {
be566e4e
DL
336 prefixtype(prefixconstptr, const struct prefix, p)
337 prefixtype(prefixconstptr, const struct prefix_ipv4, p4)
338 prefixtype(prefixconstptr, const struct prefix_ipv6, p6)
339 prefixtype(prefixconstptr, const struct prefix_evpn, evp)
340 prefixtype(prefixconstptr, const struct prefix_fs, fs)
132b6a91 341 prefixtype(prefixconstptr, const struct prefix_rd, rd)
5d387792
DL
342} TRANSPARENT_UNION;
343
344#undef prefixtype
345#undef TRANSPARENT_UNION
f7bf4153 346
718e3744 347#ifndef INET_ADDRSTRLEN
348#define INET_ADDRSTRLEN 16
349#endif /* INET_ADDRSTRLEN */
350
351#ifndef INET6_ADDRSTRLEN
f93eee44 352/* dead:beef:dead:beef:dead:beef:dead:beef + \0 */
718e3744 353#define INET6_ADDRSTRLEN 46
354#endif /* INET6_ADDRSTRLEN */
355
356#ifndef INET6_BUFSIZ
f93eee44 357#define INET6_BUFSIZ 53
718e3744 358#endif /* INET6_BUFSIZ */
359
61be6e94
QY
360/* Maximum string length of the result of prefix2str */
361#define PREFIX_STRLEN 80
855110bb 362
c6b6b53b
AK
363/*
364 * Longest possible length of a (S,G) string is 36 bytes
fef32740 365 * 123.123.123.123 = 15 * 2
c6b6b53b
AK
366 * (,) = 3
367 * NULL Character at end = 1
368 * (123.123.123.123,123.123.123.123)
369 */
fef32740 370#define PREFIX_SG_STR_LEN 34
c6b6b53b 371
718e3744 372/* Max bit/byte length of IPv4 address. */
373#define IPV4_MAX_BYTELEN 4
374#define IPV4_MAX_BITLEN 32
718e3744 375#define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
19aad877
JB
376
377static inline bool ipv4_addr_same(const struct in_addr *a,
378 const struct in_addr *b)
379{
380 return (a->s_addr == b->s_addr);
381}
382#define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
383
384static inline void ipv4_addr_copy(struct in_addr *dst,
385 const struct in_addr *src)
386{
387 dst->s_addr = src->s_addr;
388}
389#define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
718e3744 390
d7c0a89a
QY
391#define IPV4_NET0(a) ((((uint32_t)(a)) & 0xff000000) == 0x00000000)
392#define IPV4_NET127(a) ((((uint32_t)(a)) & 0xff000000) == 0x7f000000)
393#define IPV4_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffff0000) == 0xa9fe0000)
394#define IPV4_CLASS_DE(a) ((((uint32_t)(a)) & 0xe0000000) == 0xe0000000)
395#define IPV4_MC_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffffff00) == 0xe0000000)
718e3744 396
397/* Max bit/byte length of IPv6 address. */
398#define IPV6_MAX_BYTELEN 16
399#define IPV6_MAX_BITLEN 128
718e3744 400#define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
401#define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
402#define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
403
404/* Count prefix size from mask length */
405#define PSIZE(a) (((a) + 7) / (8))
406
cd1964ff
DS
407#define BSIZE(a) ((a) * (8))
408
718e3744 409/* Prefix's family member. */
410#define PREFIX_FAMILY(p) ((p)->family)
411
5c6ed111
DL
412/* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
413#ifndef s6_addr32
5c6ed111 414#define s6_addr32 __u6_addr.__u6_addr32
5c6ed111
DL
415#endif /*s6_addr32*/
416
718e3744 417/* Prototypes. */
f3ccedaa 418extern int str2family(const char *);
d62a17ae 419extern int afi2family(afi_t);
420extern afi_t family2afi(int);
db2fde34 421extern const char *family2str(int family);
1ec23d90 422extern const char *safi2str(safi_t safi);
32ac65d9 423extern const char *afi2str(afi_t afi);
8cc4198f 424
adeb0672
QY
425/*
426 * Check bit of the prefix.
427 *
428 * prefix
429 * byte buffer
430 *
431 * bit_index
432 * which bit to fetch from byte buffer, 0 indexed.
433 */
434extern unsigned int prefix_bit(const uint8_t *prefix, const uint16_t bit_index);
f63f06da 435
d62a17ae 436extern struct prefix *prefix_new(void);
63265b5c
DS
437extern void prefix_free(struct prefix **p);
438/*
439 * Function to handle prefix_free being used as a del function.
440 */
441extern void prefix_free_lists(void *arg);
d62a17ae 442extern const char *prefix_family_str(const struct prefix *);
443extern int prefix_blen(const struct prefix *);
444extern int str2prefix(const char *, struct prefix *);
4690c7d7 445
a9ea959f 446#define PREFIX2STR_BUFFER PREFIX_STRLEN
447
c6b6b53b
AK
448extern void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr,
449 char *buf, int buf_size);
450extern const char *prefix_sg2str(const struct prefix_sg *sg, char *str);
d62a17ae 451extern const char *prefix2str(union prefixconstptr, char *, int);
44c69747
LK
452extern int evpn_type5_prefix_match(const struct prefix *evpn_pfx,
453 const struct prefix *match_pfx);
d62a17ae 454extern int prefix_match(const struct prefix *, const struct prefix *);
455extern int prefix_match_network_statement(const struct prefix *,
456 const struct prefix *);
9c3a2171
DL
457extern int prefix_same(union prefixconstptr, union prefixconstptr);
458extern int prefix_cmp(union prefixconstptr, union prefixconstptr);
d62a17ae 459extern int prefix_common_bits(const struct prefix *, const struct prefix *);
9c3a2171 460extern void prefix_copy(union prefixptr, union prefixconstptr);
d62a17ae 461extern void apply_mask(struct prefix *);
462
4937287f
DL
463#ifdef __clang_analyzer__
464/* clang-SA doesn't understand transparent unions, making it think that the
465 * target of prefix_copy is uninitialized. So just memset the target.
466 * cf. https://bugs.llvm.org/show_bug.cgi?id=42811
467 */
468#define prefix_copy(a, b) ({ memset(a, 0, sizeof(*a)); prefix_copy(a, b); })
469#endif
470
d62a17ae 471extern struct prefix *sockunion2hostprefix(const union sockunion *,
472 struct prefix *p);
473extern void prefix2sockunion(const struct prefix *, union sockunion *);
474
475extern int str2prefix_eth(const char *, struct prefix_eth *);
476
477extern struct prefix_ipv4 *prefix_ipv4_new(void);
63265b5c 478extern void prefix_ipv4_free(struct prefix_ipv4 **p);
d62a17ae 479extern int str2prefix_ipv4(const char *, struct prefix_ipv4 *);
480extern void apply_mask_ipv4(struct prefix_ipv4 *);
481
d62a17ae 482extern int prefix_ipv4_any(const struct prefix_ipv4 *);
483extern void apply_classful_mask_ipv4(struct prefix_ipv4 *);
8cc4198f 484
d7c0a89a 485extern uint8_t ip_masklen(struct in_addr);
d62a17ae 486extern void masklen2ip(const int, struct in_addr *);
3fb9cd6e 487/* given the address of a host on a network and the network mask length,
488 * calculate the broadcast address for that network;
2d48474e 489 * special treatment for /31 according to RFC3021 section 3.3 */
d62a17ae 490extern in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen);
3fb9cd6e 491
7533cad7 492extern int netmask_str2prefix_str(const char *, const char *, char *, size_t);
718e3744 493
d62a17ae 494extern struct prefix_ipv6 *prefix_ipv6_new(void);
63265b5c 495extern void prefix_ipv6_free(struct prefix_ipv6 **p);
d62a17ae 496extern int str2prefix_ipv6(const char *, struct prefix_ipv6 *);
497extern void apply_mask_ipv6(struct prefix_ipv6 *);
718e3744 498
d62a17ae 499extern int ip6_masklen(struct in6_addr);
500extern void masklen2ip6(const int, struct in6_addr *);
b04c699e 501
a9e08ebc 502extern int is_zero_mac(const struct ethaddr *mac);
c6ec0c74
KA
503extern bool is_mcast_mac(const struct ethaddr *mac);
504extern bool is_bcast_mac(const struct ethaddr *mac);
db42a173
PG
505extern int prefix_str2mac(const char *str, struct ethaddr *mac);
506extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
c215ecaf 507
62b4b3b6 508extern unsigned prefix_hash_key(const void *pp);
7a7761d2 509
50f74cf1 510extern int str_to_esi(const char *str, esi_t *esi);
511extern char *esi_to_str(const esi_t *esi, char *buf, int size);
74e2bd89 512extern char *evpn_es_df_alg2str(uint8_t df_alg, char *buf, int buf_len);
50f74cf1 513extern void prefix_evpn_hexdump(const struct prefix_evpn *p);
514
d62a17ae 515static inline int ipv6_martian(struct in6_addr *addr)
d914d5ff 516{
d62a17ae 517 struct in6_addr localhost_addr;
d914d5ff 518
d62a17ae 519 inet_pton(AF_INET6, "::1", &localhost_addr);
d914d5ff 520
d62a17ae 521 if (IPV6_ADDR_SAME(&localhost_addr, addr))
522 return 1;
d914d5ff 523
d62a17ae 524 return 0;
d914d5ff
DS
525}
526
d37ba549 527extern int macstr2prefix_evpn(const char *str, struct prefix_evpn *p);
718e3744 528
d914d5ff 529/* NOTE: This routine expects the address argument in network byte order. */
d62a17ae 530static inline int ipv4_martian(struct in_addr *addr)
6ee06fa9 531{
d62a17ae 532 in_addr_t ip = ntohl(addr->s_addr);
6ee06fa9 533
d62a17ae 534 if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) {
535 return 1;
536 }
537 return 0;
6ee06fa9
PM
538}
539
f229873a 540static inline int is_default_prefix(const struct prefix *p)
18ff3edd 541{
d62a17ae 542 if (!p)
543 return 0;
18ff3edd 544
996c9314
LB
545 if ((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY)
546 && (p->prefixlen == 0))
f229873a
DS
547 return 1;
548
996c9314
LB
549 if ((p->family == AF_INET6) && (p->prefixlen == 0)
550 && (!memcmp(&p->u.prefix6, &in6addr_any, sizeof(struct in6_addr))))
d62a17ae 551 return 1;
18ff3edd 552
d62a17ae 553 return 0;
18ff3edd
DS
554}
555
0b700537 556static inline int is_host_route(const struct prefix *p)
408b00c4
MK
557{
558 if (p->family == AF_INET)
559 return (p->prefixlen == IPV4_MAX_BITLEN);
560 else if (p->family == AF_INET6)
561 return (p->prefixlen == IPV6_MAX_BITLEN);
562 return 0;
563}
5e244469 564
35518f54 565static inline int is_default_host_route(const struct prefix *p)
1eb6c3ea
CS
566{
567 if (p->family == AF_INET) {
568 return (p->u.prefix4.s_addr == INADDR_ANY &&
569 p->prefixlen == IPV4_MAX_BITLEN);
570 } else if (p->family == AF_INET6) {
571 return ((!memcmp(&p->u.prefix6, &in6addr_any,
572 sizeof(struct in6_addr))) &&
573 p->prefixlen == IPV6_MAX_BITLEN);
574 }
575 return 0;
576}
577
07ef3e34 578#ifdef _FRR_ATTRIBUTE_PRINTFRR
bd0ab4d8
DL
579#pragma FRR printfrr_ext "%pEA" (struct ethaddr *)
580
07ef3e34
DL
581#pragma FRR printfrr_ext "%pI4" (struct in_addr *)
582#pragma FRR printfrr_ext "%pI4" (in_addr_t *)
583
584#pragma FRR printfrr_ext "%pI6" (struct in6_addr *)
585
586#pragma FRR printfrr_ext "%pFX" (struct prefix *)
587#pragma FRR printfrr_ext "%pFX" (struct prefix_ipv4 *)
588#pragma FRR printfrr_ext "%pFX" (struct prefix_ipv6 *)
589#pragma FRR printfrr_ext "%pFX" (struct prefix_eth *)
590#pragma FRR printfrr_ext "%pFX" (struct prefix_evpn *)
591#pragma FRR printfrr_ext "%pFX" (struct prefix_fs *)
592
593#pragma FRR printfrr_ext "%pSG4" (struct prefix_sg *)
594#endif
595
5e244469
RW
596#ifdef __cplusplus
597}
598#endif
599
718e3744 600#endif /* _ZEBRA_PREFIX_H */