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