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