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