]> git.proxmox.com Git - mirror_frr.git/blame - lib/prefix.h
Merge pull request #11443 from opensourcerouting/fix/duplicate_label
[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;
7b0db0e4 98 uint16_t frag_id;
3714a385 99};
100
101struct evpn_macip_addr {
102 uint32_t eth_tag;
d7c0a89a 103 uint8_t ip_prefix_length;
d62a17ae 104 struct ethaddr mac;
3714a385 105 struct ipaddr ip;
106};
107
108struct evpn_imet_addr {
d62a17ae 109 uint32_t eth_tag;
3714a385 110 uint8_t ip_prefix_length;
111 struct ipaddr ip;
112};
113
114struct evpn_es_addr {
115 esi_t esi;
116 uint8_t ip_prefix_length;
d62a17ae 117 struct ipaddr ip;
a440846e 118};
119
3714a385 120struct evpn_prefix_addr {
121 uint32_t eth_tag;
122 uint8_t ip_prefix_length;
123 struct ipaddr ip;
124};
125
126/* EVPN address (RFC 7432) */
127struct evpn_addr {
128 uint8_t route_type;
129 union {
130 struct evpn_ead_addr _ead_addr;
131 struct evpn_macip_addr _macip_addr;
132 struct evpn_imet_addr _imet_addr;
133 struct evpn_es_addr _es_addr;
134 struct evpn_prefix_addr _prefix_addr;
135 } u;
136#define ead_addr u._ead_addr
137#define macip_addr u._macip_addr
138#define imet_addr u._imet_addr
139#define es_addr u._es_addr
140#define prefix_addr u._prefix_addr
141};
a440846e 142
9d24baaa 143/*
144 * A struct prefix contains an address family, a prefix length, and an
145 * address. This can represent either a 'network prefix' as defined
146 * by CIDR, where the 'host bits' of the prefix are 0
147 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
148 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
149 * interface.
150 */
151
32ac65d9
LB
152/* different OSes use different names */
153#if defined(AF_PACKET)
154#define AF_ETHERNET AF_PACKET
155#else
156#if defined(AF_LINK)
157#define AF_ETHERNET AF_LINK
158#endif
159#endif
160
b03b8898
DS
161/* The 'family' in the prefix structure is internal to FRR and need not
162 * map to standard OS AF_ definitions except where needed for interacting
163 * with the kernel. However, AF_ definitions are currently in use and
164 * prevalent across the code. Define a new FRR-specific AF for EVPN to
165 * distinguish between 'ethernet' (MAC-only) and 'evpn' prefixes and
166 * ensure it does not conflict with any OS AF_ definition.
167 */
168#if !defined(AF_EVPN)
169#define AF_EVPN (AF_MAX + 1)
170#endif
171
9a14899b
PG
172#if !defined(AF_FLOWSPEC)
173#define AF_FLOWSPEC (AF_MAX + 2)
174#endif
175
176struct flowspec_prefix {
e4552d66 177 uint8_t family;
9a14899b
PG
178 uint16_t prefixlen; /* length in bytes */
179 uintptr_t ptr;
180};
181
b03b8898 182/* FRR generic prefix structure. */
d62a17ae 183struct prefix {
d7c0a89a 184 uint8_t family;
f93eee44 185 uint16_t prefixlen;
d62a17ae 186 union {
d7c0a89a 187 uint8_t prefix;
d62a17ae 188 struct in_addr prefix4;
189 struct in6_addr prefix6;
190 struct {
191 struct in_addr id;
192 struct in_addr adv_router;
193 } lp;
194 struct ethaddr prefix_eth; /* AF_ETHERNET */
d7c0a89a 195 uint8_t val[16];
6a923ca4 196 uint32_t val32[4];
d62a17ae 197 uintptr_t ptr;
b03b8898 198 struct evpn_addr prefix_evpn; /* AF_EVPN */
9a14899b 199 struct flowspec_prefix prefix_flowspec; /* AF_FLOWSPEC */
d62a17ae 200 } u __attribute__((aligned(8)));
718e3744 201};
202
203/* IPv4 prefix structure. */
d62a17ae 204struct prefix_ipv4 {
d7c0a89a 205 uint8_t family;
f93eee44 206 uint16_t prefixlen;
d62a17ae 207 struct in_addr prefix __attribute__((aligned(8)));
718e3744 208};
209
210/* IPv6 prefix structure. */
d62a17ae 211struct prefix_ipv6 {
d7c0a89a 212 uint8_t family;
f93eee44 213 uint16_t prefixlen;
d62a17ae 214 struct in6_addr prefix __attribute__((aligned(8)));
718e3744 215};
718e3744 216
d62a17ae 217struct prefix_ls {
d7c0a89a 218 uint8_t family;
f93eee44 219 uint16_t prefixlen;
d62a17ae 220 struct in_addr id __attribute__((aligned(8)));
221 struct in_addr adv_router;
718e3744 222};
223
224/* Prefix for routing distinguisher. */
d62a17ae 225struct prefix_rd {
d7c0a89a 226 uint8_t family;
f93eee44 227 uint16_t prefixlen;
d7c0a89a 228 uint8_t val[8] __attribute__((aligned(8)));
718e3744 229};
230
32ac65d9 231/* Prefix for ethernet. */
d62a17ae 232struct prefix_eth {
d7c0a89a 233 uint8_t family;
f93eee44 234 uint16_t prefixlen;
d62a17ae 235 struct ethaddr eth_addr __attribute__((aligned(8))); /* AF_ETHERNET */
32ac65d9
LB
236};
237
86f1ef44 238/* EVPN prefix structure. */
d62a17ae 239struct prefix_evpn {
d7c0a89a 240 uint8_t family;
f93eee44 241 uint16_t prefixlen;
d62a17ae 242 struct evpn_addr prefix __attribute__((aligned(8)));
86f1ef44 243};
244
3714a385 245static inline int is_evpn_prefix_ipaddr_none(const struct prefix_evpn *evp)
246{
2ff12693 247 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
19bfb274 248 return IS_IPADDR_NONE(&(evp)->prefix.ead_addr.ip);
2ff12693 249 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 250 return IS_IPADDR_NONE(&(evp)->prefix.macip_addr.ip);
2ff12693 251 if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
3714a385 252 return IS_IPADDR_NONE(&(evp)->prefix.imet_addr.ip);
2ff12693 253 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
50f74cf1 254 return IS_IPADDR_NONE(&(evp)->prefix.es_addr.ip);
2ff12693 255 if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
3714a385 256 return IS_IPADDR_NONE(&(evp)->prefix.prefix_addr.ip);
257 return 0;
258}
259
260static inline int is_evpn_prefix_ipaddr_v4(const struct prefix_evpn *evp)
261{
2ff12693 262 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
19bfb274 263 return IS_IPADDR_V4(&(evp)->prefix.ead_addr.ip);
2ff12693 264 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 265 return IS_IPADDR_V4(&(evp)->prefix.macip_addr.ip);
2ff12693 266 if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
3714a385 267 return IS_IPADDR_V4(&(evp)->prefix.imet_addr.ip);
2ff12693 268 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
50f74cf1 269 return IS_IPADDR_V4(&(evp)->prefix.es_addr.ip);
2ff12693 270 if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
3714a385 271 return IS_IPADDR_V4(&(evp)->prefix.prefix_addr.ip);
272 return 0;
273}
274
275static inline int is_evpn_prefix_ipaddr_v6(const struct prefix_evpn *evp)
276{
2ff12693 277 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
19bfb274 278 return IS_IPADDR_V6(&(evp)->prefix.ead_addr.ip);
2ff12693 279 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 280 return IS_IPADDR_V6(&(evp)->prefix.macip_addr.ip);
2ff12693 281 if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
3714a385 282 return IS_IPADDR_V6(&(evp)->prefix.imet_addr.ip);
2ff12693 283 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
50f74cf1 284 return IS_IPADDR_V6(&(evp)->prefix.es_addr.ip);
2ff12693 285 if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
3714a385 286 return IS_IPADDR_V6(&(evp)->prefix.prefix_addr.ip);
287 return 0;
288}
289
9a14899b
PG
290/* Prefix for a Flowspec entry */
291struct prefix_fs {
292 uint8_t family;
f93eee44 293 uint16_t prefixlen; /* unused */
9a14899b
PG
294 struct flowspec_prefix prefix __attribute__((aligned(8)));
295};
296
d62a17ae 297struct prefix_sg {
d7c0a89a 298 uint8_t family;
f93eee44 299 uint16_t prefixlen;
d62a17ae 300 struct in_addr src __attribute__((aligned(8)));
301 struct in_addr grp;
e9219290
DS
302};
303
f7bf4153
DL
304/* helper to get type safety/avoid casts on calls
305 * (w/o this, functions accepting all prefix types need casts on the caller
306 * side, which strips type safety since the cast will accept any pointer
307 * type.)
308 */
be566e4e
DL
309#ifndef __cplusplus
310#define prefixtype(uname, typename, fieldname) \
311 typename *fieldname;
5d387792 312#define TRANSPARENT_UNION __attribute__((transparent_union))
be566e4e
DL
313#else
314#define prefixtype(uname, typename, fieldname) \
315 typename *fieldname; \
316 uname(typename *x) { this->fieldname = x; }
5d387792 317#define TRANSPARENT_UNION
be566e4e
DL
318#endif
319
d62a17ae 320union prefixptr {
be566e4e
DL
321 prefixtype(prefixptr, struct prefix, p)
322 prefixtype(prefixptr, struct prefix_ipv4, p4)
323 prefixtype(prefixptr, struct prefix_ipv6, p6)
324 prefixtype(prefixptr, struct prefix_evpn, evp)
325 prefixtype(prefixptr, struct prefix_fs, fs)
132b6a91 326 prefixtype(prefixptr, struct prefix_rd, rd)
5d387792 327} TRANSPARENT_UNION;
d62a17ae 328
329union prefixconstptr {
be566e4e
DL
330 prefixtype(prefixconstptr, const struct prefix, p)
331 prefixtype(prefixconstptr, const struct prefix_ipv4, p4)
332 prefixtype(prefixconstptr, const struct prefix_ipv6, p6)
333 prefixtype(prefixconstptr, const struct prefix_evpn, evp)
334 prefixtype(prefixconstptr, const struct prefix_fs, fs)
132b6a91 335 prefixtype(prefixconstptr, const struct prefix_rd, rd)
5d387792
DL
336} TRANSPARENT_UNION;
337
718e3744 338#ifndef INET_ADDRSTRLEN
339#define INET_ADDRSTRLEN 16
340#endif /* INET_ADDRSTRLEN */
341
342#ifndef INET6_ADDRSTRLEN
f93eee44 343/* dead:beef:dead:beef:dead:beef:dead:beef + \0 */
718e3744 344#define INET6_ADDRSTRLEN 46
345#endif /* INET6_ADDRSTRLEN */
346
347#ifndef INET6_BUFSIZ
f93eee44 348#define INET6_BUFSIZ 53
718e3744 349#endif /* INET6_BUFSIZ */
350
61be6e94
QY
351/* Maximum string length of the result of prefix2str */
352#define PREFIX_STRLEN 80
855110bb 353
c6b6b53b
AK
354/*
355 * Longest possible length of a (S,G) string is 36 bytes
fef32740 356 * 123.123.123.123 = 15 * 2
c6b6b53b
AK
357 * (,) = 3
358 * NULL Character at end = 1
359 * (123.123.123.123,123.123.123.123)
360 */
fef32740 361#define PREFIX_SG_STR_LEN 34
c6b6b53b 362
718e3744 363/* Max bit/byte length of IPv4 address. */
364#define IPV4_MAX_BYTELEN 4
365#define IPV4_MAX_BITLEN 32
718e3744 366#define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
19aad877
JB
367
368static inline bool ipv4_addr_same(const struct in_addr *a,
369 const struct in_addr *b)
370{
371 return (a->s_addr == b->s_addr);
372}
373#define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
374
375static inline void ipv4_addr_copy(struct in_addr *dst,
376 const struct in_addr *src)
377{
378 dst->s_addr = src->s_addr;
379}
380#define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
718e3744 381
d7c0a89a
QY
382#define IPV4_NET0(a) ((((uint32_t)(a)) & 0xff000000) == 0x00000000)
383#define IPV4_NET127(a) ((((uint32_t)(a)) & 0xff000000) == 0x7f000000)
384#define IPV4_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffff0000) == 0xa9fe0000)
385#define IPV4_CLASS_DE(a) ((((uint32_t)(a)) & 0xe0000000) == 0xe0000000)
386#define IPV4_MC_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffffff00) == 0xe0000000)
718e3744 387
388/* Max bit/byte length of IPv6 address. */
389#define IPV6_MAX_BYTELEN 16
390#define IPV6_MAX_BITLEN 128
718e3744 391#define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
392#define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
393#define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
394
395/* Count prefix size from mask length */
396#define PSIZE(a) (((a) + 7) / (8))
397
cd1964ff
DS
398#define BSIZE(a) ((a) * (8))
399
718e3744 400/* Prefix's family member. */
401#define PREFIX_FAMILY(p) ((p)->family)
402
5c6ed111
DL
403/* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
404#ifndef s6_addr32
5c6ed111 405#define s6_addr32 __u6_addr.__u6_addr32
5c6ed111
DL
406#endif /*s6_addr32*/
407
718e3744 408/* Prototypes. */
f3ccedaa 409extern int str2family(const char *);
d62a17ae 410extern int afi2family(afi_t);
411extern afi_t family2afi(int);
db2fde34 412extern const char *family2str(int family);
1ec23d90 413extern const char *safi2str(safi_t safi);
32ac65d9 414extern const char *afi2str(afi_t afi);
8cc4198f 415
0e8d473b
DL
416static inline afi_t prefix_afi(union prefixconstptr pu)
417{
418 return family2afi(pu.p->family);
419}
420
adeb0672
QY
421/*
422 * Check bit of the prefix.
423 *
424 * prefix
425 * byte buffer
426 *
427 * bit_index
428 * which bit to fetch from byte buffer, 0 indexed.
429 */
430extern unsigned int prefix_bit(const uint8_t *prefix, const uint16_t bit_index);
f63f06da 431
d62a17ae 432extern struct prefix *prefix_new(void);
63265b5c
DS
433extern void prefix_free(struct prefix **p);
434/*
435 * Function to handle prefix_free being used as a del function.
436 */
437extern void prefix_free_lists(void *arg);
3125fa6d
DL
438extern const char *prefix_family_str(union prefixconstptr pu);
439extern int prefix_blen(union prefixconstptr pu);
d62a17ae 440extern int str2prefix(const char *, struct prefix *);
4690c7d7 441
a9ea959f 442#define PREFIX2STR_BUFFER PREFIX_STRLEN
443
c6b6b53b
AK
444extern void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr,
445 char *buf, int buf_size);
446extern const char *prefix_sg2str(const struct prefix_sg *sg, char *str);
d62a17ae 447extern const char *prefix2str(union prefixconstptr, char *, int);
44c69747
LK
448extern int evpn_type5_prefix_match(const struct prefix *evpn_pfx,
449 const struct prefix *match_pfx);
3125fa6d
DL
450extern int prefix_match(union prefixconstptr unet, union prefixconstptr upfx);
451extern int prefix_match_network_statement(union prefixconstptr unet,
452 union prefixconstptr upfx);
453extern int prefix_same(union prefixconstptr ua, union prefixconstptr ub);
454extern int prefix_cmp(union prefixconstptr ua, union prefixconstptr ub);
455extern int prefix_common_bits(union prefixconstptr ua, union prefixconstptr ub);
456extern void prefix_copy(union prefixptr udst, union prefixconstptr usrc);
457extern void apply_mask(union prefixptr pu);
d62a17ae 458
4937287f
DL
459#ifdef __clang_analyzer__
460/* clang-SA doesn't understand transparent unions, making it think that the
461 * target of prefix_copy is uninitialized. So just memset the target.
462 * cf. https://bugs.llvm.org/show_bug.cgi?id=42811
463 */
464#define prefix_copy(a, b) ({ memset(a, 0, sizeof(*a)); prefix_copy(a, b); })
465#endif
466
d62a17ae 467extern struct prefix *sockunion2hostprefix(const union sockunion *,
468 struct prefix *p);
469extern void prefix2sockunion(const struct prefix *, union sockunion *);
470
471extern int str2prefix_eth(const char *, struct prefix_eth *);
472
473extern struct prefix_ipv4 *prefix_ipv4_new(void);
63265b5c 474extern void prefix_ipv4_free(struct prefix_ipv4 **p);
d62a17ae 475extern int str2prefix_ipv4(const char *, struct prefix_ipv4 *);
476extern void apply_mask_ipv4(struct prefix_ipv4 *);
477
d62a17ae 478extern int prefix_ipv4_any(const struct prefix_ipv4 *);
479extern void apply_classful_mask_ipv4(struct prefix_ipv4 *);
8cc4198f 480
d7c0a89a 481extern uint8_t ip_masklen(struct in_addr);
d62a17ae 482extern void masklen2ip(const int, struct in_addr *);
3fb9cd6e 483/* given the address of a host on a network and the network mask length,
484 * calculate the broadcast address for that network;
2d48474e 485 * special treatment for /31 according to RFC3021 section 3.3 */
d62a17ae 486extern in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen);
3fb9cd6e 487
7533cad7 488extern int netmask_str2prefix_str(const char *, const char *, char *, size_t);
718e3744 489
d62a17ae 490extern struct prefix_ipv6 *prefix_ipv6_new(void);
63265b5c 491extern void prefix_ipv6_free(struct prefix_ipv6 **p);
d62a17ae 492extern int str2prefix_ipv6(const char *, struct prefix_ipv6 *);
493extern void apply_mask_ipv6(struct prefix_ipv6 *);
718e3744 494
d62a17ae 495extern int ip6_masklen(struct in6_addr);
496extern void masklen2ip6(const int, struct in6_addr *);
b04c699e 497
a9e08ebc 498extern int is_zero_mac(const struct ethaddr *mac);
c6ec0c74
KA
499extern bool is_mcast_mac(const struct ethaddr *mac);
500extern bool is_bcast_mac(const struct ethaddr *mac);
db42a173
PG
501extern int prefix_str2mac(const char *str, struct ethaddr *mac);
502extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
c215ecaf 503
62b4b3b6 504extern unsigned prefix_hash_key(const void *pp);
7a7761d2 505
50f74cf1 506extern int str_to_esi(const char *str, esi_t *esi);
507extern char *esi_to_str(const esi_t *esi, char *buf, int size);
74e2bd89 508extern char *evpn_es_df_alg2str(uint8_t df_alg, char *buf, int buf_len);
50f74cf1 509extern void prefix_evpn_hexdump(const struct prefix_evpn *p);
510
e24a6977
DA
511static inline bool ipv4_unicast_valid(const struct in_addr *addr)
512{
513
514 in_addr_t ip = ntohl(addr->s_addr);
515
516 if (IPV4_CLASS_DE(ip))
517 return false;
518
519 return true;
520}
521
9c86ee1e 522static inline int ipv6_martian(const struct in6_addr *addr)
d914d5ff 523{
d62a17ae 524 struct in6_addr localhost_addr;
d914d5ff 525
d62a17ae 526 inet_pton(AF_INET6, "::1", &localhost_addr);
d914d5ff 527
d62a17ae 528 if (IPV6_ADDR_SAME(&localhost_addr, addr))
529 return 1;
d914d5ff 530
d62a17ae 531 return 0;
d914d5ff
DS
532}
533
d37ba549 534extern int macstr2prefix_evpn(const char *str, struct prefix_evpn *p);
718e3744 535
d914d5ff 536/* NOTE: This routine expects the address argument in network byte order. */
9c86ee1e 537static inline int ipv4_martian(const struct in_addr *addr)
6ee06fa9 538{
d62a17ae 539 in_addr_t ip = ntohl(addr->s_addr);
6ee06fa9 540
e24a6977 541 if (IPV4_NET0(ip) || IPV4_NET127(ip) || !ipv4_unicast_valid(addr)) {
d62a17ae 542 return 1;
543 }
544 return 0;
6ee06fa9
PM
545}
546
1fe59b44 547static inline bool is_default_prefix4(const struct prefix_ipv4 *p)
18ff3edd 548{
1fe59b44
RZ
549 return p && p->family == AF_INET && p->prefixlen == 0
550 && p->prefix.s_addr == INADDR_ANY;
551}
18ff3edd 552
1fe59b44
RZ
553static inline bool is_default_prefix6(const struct prefix_ipv6 *p)
554{
555 return p && p->family == AF_INET6 && p->prefixlen == 0
556 && memcmp(&p->prefix, &in6addr_any, sizeof(struct in6_addr))
557 == 0;
558}
f229873a 559
1fe59b44
RZ
560static inline bool is_default_prefix(const struct prefix *p)
561{
562 if (p == NULL)
563 return false;
564
565 switch (p->family) {
566 case AF_INET:
567 return is_default_prefix4((const struct prefix_ipv4 *)p);
568 case AF_INET6:
569 return is_default_prefix6((const struct prefix_ipv6 *)p);
570 }
18ff3edd 571
1fe59b44 572 return false;
18ff3edd
DS
573}
574
0b700537 575static inline int is_host_route(const struct prefix *p)
408b00c4
MK
576{
577 if (p->family == AF_INET)
578 return (p->prefixlen == IPV4_MAX_BITLEN);
579 else if (p->family == AF_INET6)
580 return (p->prefixlen == IPV6_MAX_BITLEN);
581 return 0;
582}
5e244469 583
35518f54 584static inline int is_default_host_route(const struct prefix *p)
1eb6c3ea
CS
585{
586 if (p->family == AF_INET) {
587 return (p->u.prefix4.s_addr == INADDR_ANY &&
588 p->prefixlen == IPV4_MAX_BITLEN);
589 } else if (p->family == AF_INET6) {
590 return ((!memcmp(&p->u.prefix6, &in6addr_any,
591 sizeof(struct in6_addr))) &&
592 p->prefixlen == IPV6_MAX_BITLEN);
593 }
594 return 0;
595}
596
eeec4112
DL
597/* IPv6 scope values, usable for IPv4 too (cf. below) */
598/* clang-format off */
599enum {
600 /* 0: reserved */
601 MCAST_SCOPE_IFACE = 0x1,
602 MCAST_SCOPE_LINK = 0x2,
603 MCAST_SCOPE_REALM = 0x3,
604 MCAST_SCOPE_ADMIN = 0x4,
605 MCAST_SCOPE_SITE = 0x5,
606 /* 6-7: unassigned */
607 MCAST_SCOPE_ORG = 0x8,
608 /* 9-d: unassigned */
609 MCAST_SCOPE_GLOBAL = 0xe,
610 /* f: reserved */
611};
612/* clang-format on */
613
614static inline uint8_t ipv6_mcast_scope(const struct in6_addr *addr)
615{
616 return addr->s6_addr[1] & 0xf;
617}
618
619static inline bool ipv6_mcast_nofwd(const struct in6_addr *addr)
620{
621 return (addr->s6_addr[1] & 0xf) <= MCAST_SCOPE_LINK;
622}
623
624static inline bool ipv6_mcast_ssm(const struct in6_addr *addr)
625{
626 uint32_t bits = ntohl(addr->s6_addr32[0]);
627
628 /* ff3x:0000::/32 */
629 return (bits & 0xfff0ffff) == 0xff300000;
630}
631
632static inline uint8_t ipv4_mcast_scope(const struct in_addr *addr)
633{
634 uint32_t bits = ntohl(addr->s_addr);
635
636 /* 224.0.0.0/24 - link scope */
637 if ((bits & 0xffffff00) == 0xe0000000)
638 return MCAST_SCOPE_LINK;
639 /* 239.0.0.0/8 - org scope */
640 if ((bits & 0xff000000) == 0xef000000)
641 return MCAST_SCOPE_ORG;
642
643 return MCAST_SCOPE_GLOBAL;
644}
645
646static inline bool ipv4_mcast_nofwd(const struct in_addr *addr)
647{
648 uint32_t bits = ntohl(addr->s_addr);
649
650 /* 224.0.0.0/24 */
651 return (bits & 0xffffff00) == 0xe0000000;
652}
653
654static inline bool ipv4_mcast_ssm(const struct in_addr *addr)
655{
656 uint32_t bits = ntohl(addr->s_addr);
657
658 /* 232.0.0.0/8 */
659 return (bits & 0xff000000) == 0xe8000000;
660}
661
07ef3e34 662#ifdef _FRR_ATTRIBUTE_PRINTFRR
bd0ab4d8
DL
663#pragma FRR printfrr_ext "%pEA" (struct ethaddr *)
664
07ef3e34
DL
665#pragma FRR printfrr_ext "%pI4" (struct in_addr *)
666#pragma FRR printfrr_ext "%pI4" (in_addr_t *)
667
668#pragma FRR printfrr_ext "%pI6" (struct in6_addr *)
669
670#pragma FRR printfrr_ext "%pFX" (struct prefix *)
671#pragma FRR printfrr_ext "%pFX" (struct prefix_ipv4 *)
672#pragma FRR printfrr_ext "%pFX" (struct prefix_ipv6 *)
673#pragma FRR printfrr_ext "%pFX" (struct prefix_eth *)
674#pragma FRR printfrr_ext "%pFX" (struct prefix_evpn *)
675#pragma FRR printfrr_ext "%pFX" (struct prefix_fs *)
676
d51f8b0f 677#pragma FRR printfrr_ext "%pPSG4" (struct prefix_sg *)
07ef3e34
DL
678#endif
679
5e244469
RW
680#ifdef __cplusplus
681}
682#endif
683
718e3744 684#endif /* _ZEBRA_PREFIX_H */