Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * Prefix structure.
4 : * Copyright (C) 1998 Kunihiro Ishiguro
5 : */
6 :
7 : #ifndef _ZEBRA_PREFIX_H
8 : #define _ZEBRA_PREFIX_H
9 :
10 : #ifdef GNU_LINUX
11 : #include <net/ethernet.h>
12 : #else
13 : #include <netinet/if_ether.h>
14 : #endif
15 : #include "sockunion.h"
16 : #include "ipaddr.h"
17 : #include "compiler.h"
18 :
19 : #ifdef __cplusplus
20 : extern "C" {
21 : #endif
22 :
23 : #ifndef ETH_ALEN
24 : #define ETH_ALEN 6
25 : #endif
26 :
27 : /* EVPN route types. */
28 : typedef enum {
29 : BGP_EVPN_AD_ROUTE = 1, /* Ethernet Auto-Discovery (A-D) route */
30 : BGP_EVPN_MAC_IP_ROUTE, /* MAC/IP Advertisement route */
31 : BGP_EVPN_IMET_ROUTE, /* Inclusive Multicast Ethernet Tag route */
32 : BGP_EVPN_ES_ROUTE, /* Ethernet Segment route */
33 : BGP_EVPN_IP_PREFIX_ROUTE, /* IP Prefix route */
34 : } bgp_evpn_route_type;
35 :
36 : /* value of first byte of ESI */
37 : #define ESI_TYPE_ARBITRARY 0 /* */
38 : #define ESI_TYPE_LACP 1 /* <> */
39 : #define ESI_TYPE_BRIDGE 2 /* <Root bridge Mac-6B>:<Root Br Priority-2B>:00 */
40 : #define ESI_TYPE_MAC 3 /* <Syst Mac Add-6B>:<Local Discriminator Value-3B> */
41 : #define ESI_TYPE_ROUTER 4 /* <RouterId-4B>:<Local Discriminator Value-4B> */
42 : #define ESI_TYPE_AS 5 /* <AS-4B>:<Local Discriminator Value-4B> */
43 :
44 : #define MAX_ESI {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
45 :
46 :
47 : #define EVPN_ETH_TAG_BYTES 4
48 : #define ESI_BYTES 10
49 : #define ESI_STR_LEN (3 * ESI_BYTES)
50 : #define EVPN_DF_ALG_STR_LEN 24
51 :
52 : /* Maximum number of VTEPs per-ES -
53 : * XXX - temporary limit for allocating strings etc.
54 : */
55 : #define ES_VTEP_MAX_CNT 10
56 : #define ES_VTEP_LIST_STR_SZ (ES_VTEP_MAX_CNT * 16)
57 :
58 : #define ETHER_ADDR_STRLEN (3*ETH_ALEN)
59 : /*
60 : * there isn't a portable ethernet address type. We define our
61 : * own to simplify internal handling
62 : */
63 : struct ethaddr {
64 : uint8_t octet[ETH_ALEN];
65 : } __attribute__((packed));
66 :
67 :
68 : /* length is the number of valuable bits of prefix structure
69 : * 18 bytes is current length in structure, if address is ipv4
70 : * 30 bytes is in case of ipv6
71 : */
72 : #define PREFIX_LEN_ROUTE_TYPE_5_IPV4 (18*8)
73 : #define PREFIX_LEN_ROUTE_TYPE_5_IPV6 (30*8)
74 :
75 : typedef struct esi_t_ {
76 : uint8_t val[ESI_BYTES];
77 : } esi_t;
78 :
79 : struct evpn_ead_addr {
80 : esi_t esi;
81 : uint32_t eth_tag;
82 : struct ipaddr ip;
83 : uint16_t frag_id;
84 : };
85 :
86 : struct evpn_macip_addr {
87 : uint32_t eth_tag;
88 : uint8_t ip_prefix_length;
89 : struct ethaddr mac;
90 : struct ipaddr ip;
91 : };
92 :
93 : struct evpn_imet_addr {
94 : uint32_t eth_tag;
95 : uint8_t ip_prefix_length;
96 : struct ipaddr ip;
97 : };
98 :
99 : struct evpn_es_addr {
100 : esi_t esi;
101 : uint8_t ip_prefix_length;
102 : struct ipaddr ip;
103 : };
104 :
105 : struct evpn_prefix_addr {
106 : uint32_t eth_tag;
107 : uint8_t ip_prefix_length;
108 : struct ipaddr ip;
109 : };
110 :
111 : /* EVPN address (RFC 7432) */
112 : struct evpn_addr {
113 : uint8_t route_type;
114 : union {
115 : struct evpn_ead_addr _ead_addr;
116 : struct evpn_macip_addr _macip_addr;
117 : struct evpn_imet_addr _imet_addr;
118 : struct evpn_es_addr _es_addr;
119 : struct evpn_prefix_addr _prefix_addr;
120 : } u;
121 : #define ead_addr u._ead_addr
122 : #define macip_addr u._macip_addr
123 : #define imet_addr u._imet_addr
124 : #define es_addr u._es_addr
125 : #define prefix_addr u._prefix_addr
126 : };
127 :
128 : /*
129 : * A struct prefix contains an address family, a prefix length, and an
130 : * address. This can represent either a 'network prefix' as defined
131 : * by CIDR, where the 'host bits' of the prefix are 0
132 : * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
133 : * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
134 : * interface.
135 : */
136 :
137 : /* different OSes use different names */
138 : #if defined(AF_PACKET)
139 : #define AF_ETHERNET AF_PACKET
140 : #else
141 : #if defined(AF_LINK)
142 : #define AF_ETHERNET AF_LINK
143 : #endif
144 : #endif
145 :
146 : /* The 'family' in the prefix structure is internal to FRR and need not
147 : * map to standard OS AF_ definitions except where needed for interacting
148 : * with the kernel. However, AF_ definitions are currently in use and
149 : * prevalent across the code. Define a new FRR-specific AF for EVPN to
150 : * distinguish between 'ethernet' (MAC-only) and 'evpn' prefixes and
151 : * ensure it does not conflict with any OS AF_ definition.
152 : */
153 : #if !defined(AF_EVPN)
154 : #define AF_EVPN (AF_MAX + 1)
155 : #endif
156 :
157 : #if !defined(AF_FLOWSPEC)
158 : #define AF_FLOWSPEC (AF_MAX + 2)
159 : #endif
160 :
161 : struct flowspec_prefix {
162 : uint8_t family;
163 : uint16_t prefixlen; /* length in bytes */
164 : uintptr_t ptr;
165 : };
166 :
167 : /* FRR generic prefix structure. */
168 : struct prefix {
169 : uint8_t family;
170 : uint16_t prefixlen;
171 : union {
172 : uint8_t prefix;
173 : struct in_addr prefix4;
174 : struct in6_addr prefix6;
175 : struct {
176 : struct in_addr id;
177 : struct in_addr adv_router;
178 : } lp;
179 : struct ethaddr prefix_eth; /* AF_ETHERNET */
180 : uint8_t val[16];
181 : uint32_t val32[4];
182 : uintptr_t ptr;
183 : struct evpn_addr prefix_evpn; /* AF_EVPN */
184 : struct flowspec_prefix prefix_flowspec; /* AF_FLOWSPEC */
185 : } u __attribute__((aligned(8)));
186 : };
187 :
188 : /* IPv4 prefix structure. */
189 : struct prefix_ipv4 {
190 : uint8_t family;
191 : uint16_t prefixlen;
192 : struct in_addr prefix __attribute__((aligned(8)));
193 : };
194 :
195 : /* IPv6 prefix structure. */
196 : struct prefix_ipv6 {
197 : uint8_t family;
198 : uint16_t prefixlen;
199 : struct in6_addr prefix __attribute__((aligned(8)));
200 : };
201 :
202 : struct prefix_ls {
203 : uint8_t family;
204 : uint16_t prefixlen;
205 : struct in_addr id __attribute__((aligned(8)));
206 : struct in_addr adv_router;
207 : };
208 :
209 : /* Prefix for routing distinguisher. */
210 : struct prefix_rd {
211 : uint8_t family;
212 : uint16_t prefixlen;
213 : uint8_t val[8] __attribute__((aligned(8)));
214 : };
215 :
216 : /* Prefix for ethernet. */
217 : struct prefix_eth {
218 : uint8_t family;
219 : uint16_t prefixlen;
220 : struct ethaddr eth_addr __attribute__((aligned(8))); /* AF_ETHERNET */
221 : };
222 :
223 : /* EVPN prefix structure. */
224 : struct prefix_evpn {
225 : uint8_t family;
226 : uint16_t prefixlen;
227 : struct evpn_addr prefix __attribute__((aligned(8)));
228 : };
229 :
230 0 : static inline int is_evpn_prefix_ipaddr_none(const struct prefix_evpn *evp)
231 : {
232 0 : if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
233 0 : return IS_IPADDR_NONE(&(evp)->prefix.ead_addr.ip);
234 : if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
235 0 : return IS_IPADDR_NONE(&(evp)->prefix.macip_addr.ip);
236 : if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
237 0 : return IS_IPADDR_NONE(&(evp)->prefix.imet_addr.ip);
238 : if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
239 0 : return IS_IPADDR_NONE(&(evp)->prefix.es_addr.ip);
240 : if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
241 0 : return IS_IPADDR_NONE(&(evp)->prefix.prefix_addr.ip);
242 : return 0;
243 : }
244 :
245 0 : static inline int is_evpn_prefix_ipaddr_v4(const struct prefix_evpn *evp)
246 : {
247 0 : if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
248 0 : return IS_IPADDR_V4(&(evp)->prefix.ead_addr.ip);
249 : if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
250 0 : return IS_IPADDR_V4(&(evp)->prefix.macip_addr.ip);
251 : if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
252 0 : return IS_IPADDR_V4(&(evp)->prefix.imet_addr.ip);
253 : if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
254 0 : return IS_IPADDR_V4(&(evp)->prefix.es_addr.ip);
255 : if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
256 0 : return IS_IPADDR_V4(&(evp)->prefix.prefix_addr.ip);
257 : return 0;
258 : }
259 :
260 0 : static inline int is_evpn_prefix_ipaddr_v6(const struct prefix_evpn *evp)
261 : {
262 0 : if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
263 0 : return IS_IPADDR_V6(&(evp)->prefix.ead_addr.ip);
264 : if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
265 0 : return IS_IPADDR_V6(&(evp)->prefix.macip_addr.ip);
266 : if (evp->prefix.route_type == BGP_EVPN_IMET_ROUTE)
267 0 : return IS_IPADDR_V6(&(evp)->prefix.imet_addr.ip);
268 : if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE)
269 0 : return IS_IPADDR_V6(&(evp)->prefix.es_addr.ip);
270 : if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
271 0 : return IS_IPADDR_V6(&(evp)->prefix.prefix_addr.ip);
272 : return 0;
273 : }
274 :
275 : /* Prefix for a Flowspec entry */
276 : struct prefix_fs {
277 : uint8_t family;
278 : uint16_t prefixlen; /* unused */
279 : struct flowspec_prefix prefix __attribute__((aligned(8)));
280 : };
281 :
282 : struct prefix_sg {
283 : uint8_t family;
284 : uint16_t prefixlen;
285 : struct in_addr src __attribute__((aligned(8)));
286 : struct in_addr grp;
287 : };
288 :
289 : /* clang-format off */
290 : union prefixptr {
291 : uniontype(prefixptr, struct prefix, p)
292 : uniontype(prefixptr, struct prefix_ipv4, p4)
293 : uniontype(prefixptr, struct prefix_ipv6, p6)
294 : uniontype(prefixptr, struct prefix_evpn, evp)
295 : uniontype(prefixptr, struct prefix_fs, fs)
296 : uniontype(prefixptr, struct prefix_rd, rd)
297 : } TRANSPARENT_UNION;
298 :
299 : union prefixconstptr {
300 : uniontype(prefixconstptr, const struct prefix, p)
301 : uniontype(prefixconstptr, const struct prefix_ipv4, p4)
302 : uniontype(prefixconstptr, const struct prefix_ipv6, p6)
303 : uniontype(prefixconstptr, const struct prefix_evpn, evp)
304 : uniontype(prefixconstptr, const struct prefix_fs, fs)
305 : uniontype(prefixconstptr, const struct prefix_rd, rd)
306 : } TRANSPARENT_UNION;
307 : /* clang-format on */
308 :
309 : #ifndef INET_ADDRSTRLEN
310 : #define INET_ADDRSTRLEN 16
311 : #endif /* INET_ADDRSTRLEN */
312 :
313 : #ifndef INET6_ADDRSTRLEN
314 : /* dead:beef:dead:beef:dead:beef:dead:beef + \0 */
315 : #define INET6_ADDRSTRLEN 46
316 : #endif /* INET6_ADDRSTRLEN */
317 :
318 : #ifndef INET6_BUFSIZ
319 : #define INET6_BUFSIZ 53
320 : #endif /* INET6_BUFSIZ */
321 :
322 : /* Maximum string length of the result of prefix2str */
323 : #define PREFIX_STRLEN 80
324 :
325 : /*
326 : * Longest possible length of a (S,G) string is 34 bytes
327 : * 123.123.123.123 = 15 * 2
328 : * (,) = 3
329 : * NULL Character at end = 1
330 : * (123.123.123.123,123.123.123.123)
331 : */
332 : #define PREFIX_SG_STR_LEN 34
333 :
334 : /* Max bit/byte length of IPv4 address. */
335 : #define IPV4_MAX_BYTELEN 4
336 : #define IPV4_MAX_BITLEN 32
337 : #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
338 :
339 108 : static inline bool ipv4_addr_same(const struct in_addr *a,
340 : const struct in_addr *b)
341 : {
342 74 : return (a->s_addr == b->s_addr);
343 : }
344 : #define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
345 :
346 6 : static inline void ipv4_addr_copy(struct in_addr *dst,
347 : const struct in_addr *src)
348 : {
349 6 : dst->s_addr = src->s_addr;
350 0 : }
351 : #define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
352 :
353 : #define IPV4_NET0(a) ((((uint32_t)(a)) & 0xff000000) == 0x00000000)
354 : #define IPV4_NET127(a) ((((uint32_t)(a)) & 0xff000000) == 0x7f000000)
355 : #define IPV4_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffff0000) == 0xa9fe0000)
356 : #define IPV4_CLASS_D(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
357 : #define IPV4_CLASS_E(a) ((((uint32_t)(a)) & 0xf0000000) == 0xf0000000)
358 : #define IPV4_CLASS_DE(a) ((((uint32_t)(a)) & 0xe0000000) == 0xe0000000)
359 : #define IPV4_MC_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffffff00) == 0xe0000000)
360 :
361 : /* Max bit/byte length of IPv6 address. */
362 : #define IPV6_MAX_BYTELEN 16
363 : #define IPV6_MAX_BITLEN 128
364 : #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
365 : #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
366 : #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
367 :
368 : /* Count prefix size from mask length */
369 : #define PSIZE(a) (((a) + 7) / (8))
370 :
371 : #define BSIZE(a) ((a) * (8))
372 :
373 : /* Prefix's family member. */
374 : #define PREFIX_FAMILY(p) ((p)->family)
375 :
376 : /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
377 : #ifndef s6_addr32
378 : #define s6_addr32 __u6_addr.__u6_addr32
379 : #endif /*s6_addr32*/
380 :
381 : /* Prototypes. */
382 : extern int str2family(const char *string);
383 : extern int afi2family(afi_t afi);
384 : extern afi_t family2afi(int family);
385 : extern const char *family2str(int family);
386 : extern const char *safi2str(safi_t safi);
387 : extern const char *afi2str(afi_t afi);
388 : extern const char *afi2str_lower(afi_t afi);
389 :
390 : static inline afi_t prefix_afi(union prefixconstptr pu)
391 : {
392 : return family2afi(pu.p->family);
393 : }
394 :
395 : /*
396 : * Check bit of the prefix.
397 : *
398 : * prefix
399 : * byte buffer
400 : *
401 : * bit_index
402 : * which bit to fetch from byte buffer, 0 indexed.
403 : */
404 : extern unsigned int prefix_bit(const uint8_t *prefix, const uint16_t bit_index);
405 :
406 : extern struct prefix *prefix_new(void);
407 : extern void prefix_free(struct prefix **p);
408 : /*
409 : * Function to handle prefix_free being used as a del function.
410 : */
411 : extern void prefix_free_lists(void *arg);
412 : extern const char *prefix_family_str(union prefixconstptr pu);
413 : extern int prefix_blen(union prefixconstptr pu);
414 : extern int str2prefix(const char *string, struct prefix *prefix);
415 :
416 : #define PREFIX2STR_BUFFER PREFIX_STRLEN
417 :
418 : extern void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr,
419 : char *buf, int buf_size);
420 : extern const char *prefix_sg2str(const struct prefix_sg *sg, char *str);
421 : extern const char *prefix2str(union prefixconstptr upfx, char *buffer,
422 : int size);
423 : extern int evpn_type5_prefix_match(const struct prefix *evpn_pfx,
424 : const struct prefix *match_pfx);
425 : extern int prefix_match(union prefixconstptr unet, union prefixconstptr upfx);
426 : extern int prefix_match_network_statement(union prefixconstptr unet,
427 : union prefixconstptr upfx);
428 : extern int prefix_same(union prefixconstptr ua, union prefixconstptr ub);
429 : extern int prefix_cmp(union prefixconstptr ua, union prefixconstptr ub);
430 : extern int prefix_common_bits(union prefixconstptr ua, union prefixconstptr ub);
431 : extern void prefix_copy(union prefixptr udst, union prefixconstptr usrc);
432 : extern void apply_mask(union prefixptr pu);
433 : extern bool evpn_addr_same(const struct evpn_addr *e1, const struct evpn_addr *e2);
434 :
435 : #ifdef __clang_analyzer__
436 : /* clang-SA doesn't understand transparent unions, making it think that the
437 : * target of prefix_copy is uninitialized. So just memset the target.
438 : * cf. https://bugs.llvm.org/show_bug.cgi?id=42811
439 : */
440 : #define prefix_copy(a, b) ({ memset(a, 0, sizeof(*a)); prefix_copy(a, b); })
441 : #endif
442 :
443 : extern struct prefix *sockunion2hostprefix(const union sockunion *su,
444 : struct prefix *p);
445 : extern void prefix2sockunion(const struct prefix *p, union sockunion *su);
446 :
447 : extern int str2prefix_eth(const char *string, struct prefix_eth *p);
448 :
449 : extern struct prefix_ipv4 *prefix_ipv4_new(void);
450 : extern void prefix_ipv4_free(struct prefix_ipv4 **p);
451 : extern int str2prefix_ipv4(const char *string, struct prefix_ipv4 *p);
452 : extern void apply_mask_ipv4(struct prefix_ipv4 *p);
453 :
454 : extern int prefix_ipv4_any(const struct prefix_ipv4 *p);
455 : extern void apply_classful_mask_ipv4(struct prefix_ipv4 *p);
456 :
457 : extern uint8_t ip_masklen(struct in_addr addr);
458 : extern void masklen2ip(const int length, struct in_addr *addr);
459 : /* given the address of a host on a network and the network mask length,
460 : * calculate the broadcast address for that network;
461 : * special treatment for /31 according to RFC3021 section 3.3 */
462 : extern in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen);
463 :
464 : extern int netmask_str2prefix_str(const char *net_str, const char *mask_str,
465 : char *prefix_str, size_t prefix_str_len);
466 :
467 : extern struct prefix_ipv6 *prefix_ipv6_new(void);
468 : extern void prefix_ipv6_free(struct prefix_ipv6 **p);
469 : extern int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p);
470 : extern void apply_mask_ipv6(struct prefix_ipv6 *p);
471 :
472 : extern int ip6_masklen(struct in6_addr netmask);
473 : extern void masklen2ip6(const int masklen, struct in6_addr *netmask);
474 :
475 : extern int is_zero_mac(const struct ethaddr *mac);
476 : extern bool is_mcast_mac(const struct ethaddr *mac);
477 : extern bool is_bcast_mac(const struct ethaddr *mac);
478 : extern int prefix_str2mac(const char *str, struct ethaddr *mac);
479 : extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
480 :
481 : extern unsigned prefix_hash_key(const void *pp);
482 :
483 : extern int str_to_esi(const char *str, esi_t *esi);
484 : extern char *esi_to_str(const esi_t *esi, char *buf, int size);
485 : extern char *evpn_es_df_alg2str(uint8_t df_alg, char *buf, int buf_len);
486 : extern void prefix_evpn_hexdump(const struct prefix_evpn *p);
487 : extern bool ipv4_unicast_valid(const struct in_addr *addr);
488 : extern int evpn_prefix2prefix(const struct prefix *evpn, struct prefix *to);
489 :
490 16 : static inline int ipv6_martian(const struct in6_addr *addr)
491 : {
492 16 : struct in6_addr localhost_addr;
493 :
494 16 : inet_pton(AF_INET6, "::1", &localhost_addr);
495 :
496 16 : if (IPV6_ADDR_SAME(&localhost_addr, addr))
497 2 : return 1;
498 :
499 : return 0;
500 : }
501 :
502 : extern int macstr2prefix_evpn(const char *str, struct prefix_evpn *p);
503 :
504 : /* NOTE: This routine expects the address argument in network byte order. */
505 6 : static inline bool ipv4_martian(const struct in_addr *addr)
506 : {
507 6 : if (!ipv4_unicast_valid(addr))
508 0 : return true;
509 : return false;
510 : }
511 :
512 14 : static inline bool is_default_prefix4(const struct prefix_ipv4 *p)
513 : {
514 14 : return p && p->family == AF_INET && p->prefixlen == 0
515 0 : && p->prefix.s_addr == INADDR_ANY;
516 : }
517 :
518 30 : static inline bool is_default_prefix6(const struct prefix_ipv6 *p)
519 : {
520 30 : return p && p->family == AF_INET6 && p->prefixlen == 0
521 30 : && memcmp(&p->prefix, &in6addr_any, sizeof(struct in6_addr))
522 : == 0;
523 : }
524 :
525 44 : static inline bool is_default_prefix(const struct prefix *p)
526 : {
527 44 : if (p == NULL)
528 : return false;
529 :
530 44 : switch (p->family) {
531 : case AF_INET:
532 28 : return is_default_prefix4((const struct prefix_ipv4 *)p);
533 30 : case AF_INET6:
534 30 : return is_default_prefix6((const struct prefix_ipv6 *)p);
535 : }
536 :
537 : return false;
538 : }
539 :
540 : static inline int is_host_route(const struct prefix *p)
541 : {
542 : if (p->family == AF_INET)
543 : return (p->prefixlen == IPV4_MAX_BITLEN);
544 : else if (p->family == AF_INET6)
545 : return (p->prefixlen == IPV6_MAX_BITLEN);
546 : return 0;
547 : }
548 :
549 6 : static inline int is_default_host_route(const struct prefix *p)
550 : {
551 6 : if (p->family == AF_INET) {
552 0 : return (p->u.prefix4.s_addr == INADDR_ANY &&
553 0 : p->prefixlen == IPV4_MAX_BITLEN);
554 6 : } else if (p->family == AF_INET6) {
555 6 : return ((!memcmp(&p->u.prefix6, &in6addr_any,
556 6 : sizeof(struct in6_addr))) &&
557 0 : p->prefixlen == IPV6_MAX_BITLEN);
558 : }
559 : return 0;
560 : }
561 :
562 : static inline bool is_ipv6_global_unicast(const struct in6_addr *p)
563 : {
564 : if (IN6_IS_ADDR_UNSPECIFIED(p) || IN6_IS_ADDR_LOOPBACK(p) ||
565 : IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MULTICAST(p))
566 : return false;
567 :
568 : return true;
569 : }
570 :
571 : /* IPv6 scope values, usable for IPv4 too (cf. below) */
572 : /* clang-format off */
573 : enum {
574 : /* 0: reserved */
575 : MCAST_SCOPE_IFACE = 0x1,
576 : MCAST_SCOPE_LINK = 0x2,
577 : MCAST_SCOPE_REALM = 0x3,
578 : MCAST_SCOPE_ADMIN = 0x4,
579 : MCAST_SCOPE_SITE = 0x5,
580 : /* 6-7: unassigned */
581 : MCAST_SCOPE_ORG = 0x8,
582 : /* 9-d: unassigned */
583 : MCAST_SCOPE_GLOBAL = 0xe,
584 : /* f: reserved */
585 : };
586 : /* clang-format on */
587 :
588 : static inline uint8_t ipv6_mcast_scope(const struct in6_addr *addr)
589 : {
590 : return addr->s6_addr[1] & 0xf;
591 : }
592 :
593 : static inline bool ipv6_mcast_nofwd(const struct in6_addr *addr)
594 : {
595 : return (addr->s6_addr[1] & 0xf) <= MCAST_SCOPE_LINK;
596 : }
597 :
598 : static inline bool ipv6_mcast_ssm(const struct in6_addr *addr)
599 : {
600 : uint32_t bits = ntohl(addr->s6_addr32[0]);
601 :
602 : /* ff3x:0000::/32 */
603 : return (bits & 0xfff0ffff) == 0xff300000;
604 : }
605 :
606 : static inline bool ipv6_mcast_reserved(const struct in6_addr *addr)
607 : {
608 : uint32_t bits = ntohl(addr->s6_addr32[0]);
609 :
610 : /* ffx2::/16 */
611 : return (bits & 0xff0fffff) == 0xff020000;
612 : }
613 :
614 : static inline uint8_t ipv4_mcast_scope(const struct in_addr *addr)
615 : {
616 : uint32_t bits = ntohl(addr->s_addr);
617 :
618 : /* 224.0.0.0/24 - link scope */
619 : if ((bits & 0xffffff00) == 0xe0000000)
620 : return MCAST_SCOPE_LINK;
621 : /* 239.0.0.0/8 - org scope */
622 : if ((bits & 0xff000000) == 0xef000000)
623 : return MCAST_SCOPE_ORG;
624 :
625 : return MCAST_SCOPE_GLOBAL;
626 : }
627 :
628 : static inline bool ipv4_mcast_nofwd(const struct in_addr *addr)
629 : {
630 : uint32_t bits = ntohl(addr->s_addr);
631 :
632 : /* 224.0.0.0/24 */
633 : return (bits & 0xffffff00) == 0xe0000000;
634 : }
635 :
636 : static inline bool ipv4_mcast_ssm(const struct in_addr *addr)
637 : {
638 : uint32_t bits = ntohl(addr->s_addr);
639 :
640 : /* 232.0.0.0/8 */
641 : return (bits & 0xff000000) == 0xe8000000;
642 : }
643 :
644 : #ifdef _FRR_ATTRIBUTE_PRINTFRR
645 : #pragma FRR printfrr_ext "%pEA" (struct ethaddr *)
646 :
647 : #pragma FRR printfrr_ext "%pI4" (struct in_addr *)
648 : #pragma FRR printfrr_ext "%pI4" (in_addr_t *)
649 :
650 : #pragma FRR printfrr_ext "%pI6" (struct in6_addr *)
651 :
652 : #pragma FRR printfrr_ext "%pFX" (struct prefix *)
653 : #pragma FRR printfrr_ext "%pFX" (struct prefix_ipv4 *)
654 : #pragma FRR printfrr_ext "%pFX" (struct prefix_ipv6 *)
655 : #pragma FRR printfrr_ext "%pFX" (struct prefix_eth *)
656 : #pragma FRR printfrr_ext "%pFX" (struct prefix_evpn *)
657 : #pragma FRR printfrr_ext "%pFX" (struct prefix_fs *)
658 : #pragma FRR printfrr_ext "%pRDP" (struct prefix_rd *)
659 : /* RD with AS4B with dot and dot+ format */
660 : #pragma FRR printfrr_ext "%pRDD" (struct prefix_rd *)
661 : #pragma FRR printfrr_ext "%pRDE" (struct prefix_rd *)
662 :
663 : #pragma FRR printfrr_ext "%pPSG4" (struct prefix_sg *)
664 : #endif
665 :
666 : #ifdef __cplusplus
667 : }
668 : #endif
669 :
670 : #endif /* _ZEBRA_PREFIX_H */
|