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