]> git.proxmox.com Git - mirror_frr.git/blame - lib/prefix.h
lib: revert table.h change for C++
[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
32ac65d9 25#ifdef SUNOS_5
d62a17ae 26#include <sys/ethernet.h>
32ac65d9 27#else
d62a17ae 28#ifdef GNU_LINUX
29#include <net/ethernet.h>
30#else
31#include <netinet/if_ether.h>
32#endif
32ac65d9 33#endif
8cc4198f 34#include "sockunion.h"
86f1ef44 35#include "ipaddr.h"
de1a880c 36#include "compiler.h"
8cc4198f 37
7628d862
DS
38#ifndef ETH_ALEN
39#define ETH_ALEN 6
40#endif
41
50f74cf1 42#define ESI_BYTES 10
2bb9eff4 43#define ESI_STR_LEN (3 * ESI_BYTES)
50f74cf1 44
7628d862 45#define ETHER_ADDR_STRLEN (3*ETH_ALEN)
32ac65d9
LB
46/*
47 * there isn't a portable ethernet address type. We define our
48 * own to simplify internal handling
49 */
50struct ethaddr {
d7c0a89a 51 uint8_t octet[ETH_ALEN];
d62a17ae 52} __attribute__((packed));
32ac65d9
LB
53
54
d62a17ae 55/* length is the number of valuable bits of prefix structure
9d303b37
DL
56* 18 bytes is current length in structure, if address is ipv4
57* 30 bytes is in case of ipv6
58*/
a440846e 59#define PREFIX_LEN_ROUTE_TYPE_5_IPV4 (18*8)
60#define PREFIX_LEN_ROUTE_TYPE_5_IPV6 (30*8)
61
3714a385 62typedef struct esi_t_ {
63 uint8_t val[10];
64} esi_t;
65
66struct evpn_ead_addr {
67 esi_t esi;
68 uint32_t eth_tag;
69};
70
71struct evpn_macip_addr {
72 uint32_t eth_tag;
d7c0a89a 73 uint8_t ip_prefix_length;
d62a17ae 74 struct ethaddr mac;
3714a385 75 struct ipaddr ip;
76};
77
78struct evpn_imet_addr {
d62a17ae 79 uint32_t eth_tag;
3714a385 80 uint8_t ip_prefix_length;
81 struct ipaddr ip;
82};
83
84struct evpn_es_addr {
85 esi_t esi;
86 uint8_t ip_prefix_length;
d62a17ae 87 struct ipaddr ip;
a440846e 88};
89
3714a385 90struct evpn_prefix_addr {
91 uint32_t eth_tag;
92 uint8_t ip_prefix_length;
93 struct ipaddr ip;
94};
95
96/* EVPN address (RFC 7432) */
97struct evpn_addr {
98 uint8_t route_type;
99 union {
100 struct evpn_ead_addr _ead_addr;
101 struct evpn_macip_addr _macip_addr;
102 struct evpn_imet_addr _imet_addr;
103 struct evpn_es_addr _es_addr;
104 struct evpn_prefix_addr _prefix_addr;
105 } u;
106#define ead_addr u._ead_addr
107#define macip_addr u._macip_addr
108#define imet_addr u._imet_addr
109#define es_addr u._es_addr
110#define prefix_addr u._prefix_addr
111};
a440846e 112
9d24baaa 113/*
114 * A struct prefix contains an address family, a prefix length, and an
115 * address. This can represent either a 'network prefix' as defined
116 * by CIDR, where the 'host bits' of the prefix are 0
117 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
118 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
119 * interface.
120 */
121
32ac65d9
LB
122/* different OSes use different names */
123#if defined(AF_PACKET)
124#define AF_ETHERNET AF_PACKET
125#else
126#if defined(AF_LINK)
127#define AF_ETHERNET AF_LINK
128#endif
129#endif
130
b03b8898
DS
131/* The 'family' in the prefix structure is internal to FRR and need not
132 * map to standard OS AF_ definitions except where needed for interacting
133 * with the kernel. However, AF_ definitions are currently in use and
134 * prevalent across the code. Define a new FRR-specific AF for EVPN to
135 * distinguish between 'ethernet' (MAC-only) and 'evpn' prefixes and
136 * ensure it does not conflict with any OS AF_ definition.
137 */
138#if !defined(AF_EVPN)
139#define AF_EVPN (AF_MAX + 1)
140#endif
141
9a14899b
PG
142#if !defined(AF_FLOWSPEC)
143#define AF_FLOWSPEC (AF_MAX + 2)
144#endif
145
146struct flowspec_prefix {
147 uint16_t prefixlen; /* length in bytes */
148 uintptr_t ptr;
149};
150
b03b8898 151/* FRR generic prefix structure. */
d62a17ae 152struct prefix {
d7c0a89a 153 uint8_t family;
f93eee44 154 uint16_t prefixlen;
d62a17ae 155 union {
d7c0a89a 156 uint8_t prefix;
d62a17ae 157 struct in_addr prefix4;
158 struct in6_addr prefix6;
159 struct {
160 struct in_addr id;
161 struct in_addr adv_router;
162 } lp;
163 struct ethaddr prefix_eth; /* AF_ETHERNET */
d7c0a89a 164 uint8_t val[16];
6a923ca4 165 uint32_t val32[4];
d62a17ae 166 uintptr_t ptr;
b03b8898 167 struct evpn_addr prefix_evpn; /* AF_EVPN */
9a14899b 168 struct flowspec_prefix prefix_flowspec; /* AF_FLOWSPEC */
d62a17ae 169 } u __attribute__((aligned(8)));
718e3744 170};
171
172/* IPv4 prefix structure. */
d62a17ae 173struct prefix_ipv4 {
d7c0a89a 174 uint8_t family;
f93eee44 175 uint16_t prefixlen;
d62a17ae 176 struct in_addr prefix __attribute__((aligned(8)));
718e3744 177};
178
179/* IPv6 prefix structure. */
d62a17ae 180struct prefix_ipv6 {
d7c0a89a 181 uint8_t family;
f93eee44 182 uint16_t prefixlen;
d62a17ae 183 struct in6_addr prefix __attribute__((aligned(8)));
718e3744 184};
718e3744 185
d62a17ae 186struct prefix_ls {
d7c0a89a 187 uint8_t family;
f93eee44 188 uint16_t prefixlen;
d62a17ae 189 struct in_addr id __attribute__((aligned(8)));
190 struct in_addr adv_router;
718e3744 191};
192
193/* Prefix for routing distinguisher. */
d62a17ae 194struct prefix_rd {
d7c0a89a 195 uint8_t family;
f93eee44 196 uint16_t prefixlen;
d7c0a89a 197 uint8_t val[8] __attribute__((aligned(8)));
718e3744 198};
199
32ac65d9 200/* Prefix for ethernet. */
d62a17ae 201struct prefix_eth {
d7c0a89a 202 uint8_t family;
f93eee44 203 uint16_t prefixlen;
d62a17ae 204 struct ethaddr eth_addr __attribute__((aligned(8))); /* AF_ETHERNET */
32ac65d9
LB
205};
206
86f1ef44 207/* EVPN prefix structure. */
d62a17ae 208struct prefix_evpn {
d7c0a89a 209 uint8_t family;
f93eee44 210 uint16_t prefixlen;
d62a17ae 211 struct evpn_addr prefix __attribute__((aligned(8)));
86f1ef44 212};
213
3714a385 214static inline int is_evpn_prefix_ipaddr_none(const struct prefix_evpn *evp)
215{
216 if (evp->prefix.route_type == 2)
217 return IS_IPADDR_NONE(&(evp)->prefix.macip_addr.ip);
218 if (evp->prefix.route_type == 3)
219 return IS_IPADDR_NONE(&(evp)->prefix.imet_addr.ip);
50f74cf1 220 if (evp->prefix.route_type == 4)
221 return IS_IPADDR_NONE(&(evp)->prefix.es_addr.ip);
3714a385 222 if (evp->prefix.route_type == 5)
223 return IS_IPADDR_NONE(&(evp)->prefix.prefix_addr.ip);
224 return 0;
225}
226
227static inline int is_evpn_prefix_ipaddr_v4(const struct prefix_evpn *evp)
228{
229 if (evp->prefix.route_type == 2)
230 return IS_IPADDR_V4(&(evp)->prefix.macip_addr.ip);
231 if (evp->prefix.route_type == 3)
232 return IS_IPADDR_V4(&(evp)->prefix.imet_addr.ip);
50f74cf1 233 if (evp->prefix.route_type == 4)
234 return IS_IPADDR_V4(&(evp)->prefix.es_addr.ip);
3714a385 235 if (evp->prefix.route_type == 5)
236 return IS_IPADDR_V4(&(evp)->prefix.prefix_addr.ip);
237 return 0;
238}
239
240static inline int is_evpn_prefix_ipaddr_v6(const struct prefix_evpn *evp)
241{
242 if (evp->prefix.route_type == 2)
243 return IS_IPADDR_V6(&(evp)->prefix.macip_addr.ip);
244 if (evp->prefix.route_type == 3)
245 return IS_IPADDR_V6(&(evp)->prefix.imet_addr.ip);
50f74cf1 246 if (evp->prefix.route_type == 4)
247 return IS_IPADDR_V6(&(evp)->prefix.es_addr.ip);
3714a385 248 if (evp->prefix.route_type == 5)
249 return IS_IPADDR_V6(&(evp)->prefix.prefix_addr.ip);
250 return 0;
251}
252
b4b359a2 253/* Prefix for a generic pointer */
d62a17ae 254struct prefix_ptr {
d7c0a89a 255 uint8_t family;
f93eee44 256 uint16_t prefixlen;
d62a17ae 257 uintptr_t prefix __attribute__((aligned(8)));
b4b359a2
PM
258};
259
9a14899b
PG
260/* Prefix for a Flowspec entry */
261struct prefix_fs {
262 uint8_t family;
f93eee44 263 uint16_t prefixlen; /* unused */
9a14899b
PG
264 struct flowspec_prefix prefix __attribute__((aligned(8)));
265};
266
d62a17ae 267struct prefix_sg {
d7c0a89a 268 uint8_t family;
f93eee44 269 uint16_t prefixlen;
d62a17ae 270 struct in_addr src __attribute__((aligned(8)));
271 struct in_addr grp;
e9219290
DS
272};
273
f7bf4153
DL
274/* helper to get type safety/avoid casts on calls
275 * (w/o this, functions accepting all prefix types need casts on the caller
276 * side, which strips type safety since the cast will accept any pointer
277 * type.)
278 */
d62a17ae 279union prefixptr {
280 struct prefix *p;
281 struct prefix_ipv4 *p4;
282 struct prefix_ipv6 *p6;
283 struct prefix_evpn *evp;
9a14899b 284 const struct prefix_fs *fs;
d62a17ae 285} __attribute__((transparent_union));
286
287union prefixconstptr {
288 const struct prefix *p;
289 const struct prefix_ipv4 *p4;
290 const struct prefix_ipv6 *p6;
291 const struct prefix_evpn *evp;
9a14899b 292 const struct prefix_fs *fs;
d62a17ae 293} __attribute__((transparent_union));
f7bf4153 294
718e3744 295#ifndef INET_ADDRSTRLEN
296#define INET_ADDRSTRLEN 16
297#endif /* INET_ADDRSTRLEN */
298
299#ifndef INET6_ADDRSTRLEN
f93eee44 300/* dead:beef:dead:beef:dead:beef:dead:beef + \0 */
718e3744 301#define INET6_ADDRSTRLEN 46
302#endif /* INET6_ADDRSTRLEN */
303
304#ifndef INET6_BUFSIZ
f93eee44 305#define INET6_BUFSIZ 53
718e3744 306#endif /* INET6_BUFSIZ */
307
61be6e94
QY
308/* Maximum string length of the result of prefix2str */
309#define PREFIX_STRLEN 80
855110bb 310
718e3744 311/* Max bit/byte length of IPv4 address. */
312#define IPV4_MAX_BYTELEN 4
313#define IPV4_MAX_BITLEN 32
314#define IPV4_MAX_PREFIXLEN 32
315#define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
19aad877
JB
316
317static inline bool ipv4_addr_same(const struct in_addr *a,
318 const struct in_addr *b)
319{
320 return (a->s_addr == b->s_addr);
321}
322#define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
323
324static inline void ipv4_addr_copy(struct in_addr *dst,
325 const struct in_addr *src)
326{
327 dst->s_addr = src->s_addr;
328}
329#define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
718e3744 330
d7c0a89a
QY
331#define IPV4_NET0(a) ((((uint32_t)(a)) & 0xff000000) == 0x00000000)
332#define IPV4_NET127(a) ((((uint32_t)(a)) & 0xff000000) == 0x7f000000)
333#define IPV4_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffff0000) == 0xa9fe0000)
334#define IPV4_CLASS_DE(a) ((((uint32_t)(a)) & 0xe0000000) == 0xe0000000)
335#define IPV4_MC_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffffff00) == 0xe0000000)
718e3744 336
337/* Max bit/byte length of IPv6 address. */
338#define IPV6_MAX_BYTELEN 16
339#define IPV6_MAX_BITLEN 128
340#define IPV6_MAX_PREFIXLEN 128
341#define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
342#define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
343#define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
344
345/* Count prefix size from mask length */
346#define PSIZE(a) (((a) + 7) / (8))
347
cd1964ff
DS
348#define BSIZE(a) ((a) * (8))
349
718e3744 350/* Prefix's family member. */
351#define PREFIX_FAMILY(p) ((p)->family)
352
5c6ed111
DL
353/* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
354#ifndef s6_addr32
355#if defined(SUNOS_5)
356/* Some SunOS define s6_addr32 only to kernel */
357#define s6_addr32 _S6_un._S6_u32
358#else
359#define s6_addr32 __u6_addr.__u6_addr32
360#endif /* SUNOS_5 */
361#endif /*s6_addr32*/
362
718e3744 363/* Prototypes. */
f3ccedaa 364extern int str2family(const char *);
d62a17ae 365extern int afi2family(afi_t);
366extern afi_t family2afi(int);
db2fde34 367extern const char *family2str(int family);
1ec23d90 368extern const char *safi2str(safi_t safi);
32ac65d9 369extern const char *afi2str(afi_t afi);
8cc4198f 370
f63f06da 371/* Check bit of the prefix. */
f93eee44 372extern unsigned int prefix_bit(const uint8_t *prefix, const uint16_t prefixlen);
d62a17ae 373extern unsigned int prefix6_bit(const struct in6_addr *prefix,
f93eee44 374 const uint16_t prefixlen);
f63f06da 375
d62a17ae 376extern struct prefix *prefix_new(void);
377extern void prefix_free(struct prefix *);
378extern const char *prefix_family_str(const struct prefix *);
379extern int prefix_blen(const struct prefix *);
380extern int str2prefix(const char *, struct prefix *);
4690c7d7 381
a9ea959f 382#define PREFIX2STR_BUFFER PREFIX_STRLEN
383
d62a17ae 384extern const char *prefix2str(union prefixconstptr, char *, int);
385extern int prefix_match(const struct prefix *, const struct prefix *);
386extern int prefix_match_network_statement(const struct prefix *,
387 const struct prefix *);
388extern int prefix_same(const struct prefix *, const struct prefix *);
389extern int prefix_cmp(const struct prefix *, const struct prefix *);
390extern int prefix_common_bits(const struct prefix *, const struct prefix *);
391extern void prefix_copy(struct prefix *dest, const struct prefix *src);
392extern void apply_mask(struct prefix *);
393
394extern struct prefix *sockunion2prefix(const union sockunion *dest,
395 const union sockunion *mask);
396extern struct prefix *sockunion2hostprefix(const union sockunion *,
397 struct prefix *p);
398extern void prefix2sockunion(const struct prefix *, union sockunion *);
399
400extern int str2prefix_eth(const char *, struct prefix_eth *);
401
402extern struct prefix_ipv4 *prefix_ipv4_new(void);
403extern void prefix_ipv4_free(struct prefix_ipv4 *);
404extern int str2prefix_ipv4(const char *, struct prefix_ipv4 *);
405extern void apply_mask_ipv4(struct prefix_ipv4 *);
406
996c9314 407#define PREFIX_COPY(DST, SRC) \
72a1b201 408 *((struct prefix *)(DST)) = *((const struct prefix *)(SRC))
996c9314 409#define PREFIX_COPY_IPV4(DST, SRC) \
e4529636
AS
410 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
411
d62a17ae 412extern int prefix_ipv4_any(const struct prefix_ipv4 *);
413extern void apply_classful_mask_ipv4(struct prefix_ipv4 *);
8cc4198f 414
d7c0a89a 415extern uint8_t ip_masklen(struct in_addr);
d62a17ae 416extern void masklen2ip(const int, struct in_addr *);
3fb9cd6e 417/* returns the network portion of the host address */
d62a17ae 418extern in_addr_t ipv4_network_addr(in_addr_t hostaddr, int masklen);
3fb9cd6e 419/* given the address of a host on a network and the network mask length,
420 * calculate the broadcast address for that network;
421 * special treatment for /31: returns the address of the other host
422 * on the network by flipping the host bit */
d62a17ae 423extern in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen);
3fb9cd6e 424
d62a17ae 425extern int netmask_str2prefix_str(const char *, const char *, char *);
718e3744 426
d62a17ae 427extern struct prefix_ipv6 *prefix_ipv6_new(void);
428extern void prefix_ipv6_free(struct prefix_ipv6 *);
429extern int str2prefix_ipv6(const char *, struct prefix_ipv6 *);
430extern void apply_mask_ipv6(struct prefix_ipv6 *);
718e3744 431
d62a17ae 432#define PREFIX_COPY_IPV6(DST, SRC) \
e4529636
AS
433 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
434
d62a17ae 435extern int ip6_masklen(struct in6_addr);
436extern void masklen2ip6(const int, struct in6_addr *);
b04c699e 437
d62a17ae 438extern const char *inet6_ntoa(struct in6_addr);
5920990f 439
523cafc4 440extern int is_zero_mac(struct ethaddr *mac);
db42a173
PG
441extern int prefix_str2mac(const char *str, struct ethaddr *mac);
442extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
c215ecaf 443
7a7761d2
CF
444extern unsigned prefix_hash_key(void *pp);
445
50f74cf1 446extern int str_to_esi(const char *str, esi_t *esi);
447extern char *esi_to_str(const esi_t *esi, char *buf, int size);
448extern void prefix_hexdump(const struct prefix *p);
449extern void prefix_evpn_hexdump(const struct prefix_evpn *p);
450
d62a17ae 451static inline int ipv6_martian(struct in6_addr *addr)
d914d5ff 452{
d62a17ae 453 struct in6_addr localhost_addr;
d914d5ff 454
d62a17ae 455 inet_pton(AF_INET6, "::1", &localhost_addr);
d914d5ff 456
d62a17ae 457 if (IPV6_ADDR_SAME(&localhost_addr, addr))
458 return 1;
d914d5ff 459
d62a17ae 460 return 0;
d914d5ff
DS
461}
462
d37ba549 463extern int macstr2prefix_evpn(const char *str, struct prefix_evpn *p);
718e3744 464
d914d5ff 465/* NOTE: This routine expects the address argument in network byte order. */
d62a17ae 466static inline int ipv4_martian(struct in_addr *addr)
6ee06fa9 467{
d62a17ae 468 in_addr_t ip = ntohl(addr->s_addr);
6ee06fa9 469
d62a17ae 470 if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) {
471 return 1;
472 }
473 return 0;
6ee06fa9
PM
474}
475
f229873a 476static inline int is_default_prefix(const struct prefix *p)
18ff3edd 477{
d62a17ae 478 if (!p)
479 return 0;
18ff3edd 480
996c9314
LB
481 if ((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY)
482 && (p->prefixlen == 0))
f229873a
DS
483 return 1;
484
996c9314
LB
485 if ((p->family == AF_INET6) && (p->prefixlen == 0)
486 && (!memcmp(&p->u.prefix6, &in6addr_any, sizeof(struct in6_addr))))
d62a17ae 487 return 1;
18ff3edd 488
d62a17ae 489 return 0;
18ff3edd
DS
490}
491
408b00c4
MK
492static inline int is_host_route(struct prefix *p)
493{
494 if (p->family == AF_INET)
495 return (p->prefixlen == IPV4_MAX_BITLEN);
496 else if (p->family == AF_INET6)
497 return (p->prefixlen == IPV6_MAX_BITLEN);
498 return 0;
499}
718e3744 500#endif /* _ZEBRA_PREFIX_H */