]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.h
*: use C99 standard fixed-width integer types
[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 /* EVPN address (RFC 7432) */
60 struct evpn_addr {
61 uint8_t route_type;
62 uint8_t ip_prefix_length;
63 struct ethaddr mac;
64 uint32_t eth_tag;
65 struct ipaddr ip;
66 #if 0
67 union
68 {
69 uint8_t addr;
70 struct in_addr v4_addr;
71 struct in6_addr v6_addr;
72 } ip;
73 #endif
74 };
75
76 #define IS_EVPN_PREFIX_IPADDR_NONE(evp) IS_IPADDR_NONE(&(evp)->prefix.ip)
77 #define IS_EVPN_PREFIX_IPADDR_V4(evp) IS_IPADDR_V4(&(evp)->prefix.ip)
78 #define IS_EVPN_PREFIX_IPADDR_V6(evp) IS_IPADDR_V6(&(evp)->prefix.ip)
79
80 /*
81 * A struct prefix contains an address family, a prefix length, and an
82 * address. This can represent either a 'network prefix' as defined
83 * by CIDR, where the 'host bits' of the prefix are 0
84 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
85 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
86 * interface.
87 */
88
89 /* different OSes use different names */
90 #if defined(AF_PACKET)
91 #define AF_ETHERNET AF_PACKET
92 #else
93 #if defined(AF_LINK)
94 #define AF_ETHERNET AF_LINK
95 #endif
96 #endif
97
98 /* The 'family' in the prefix structure is internal to FRR and need not
99 * map to standard OS AF_ definitions except where needed for interacting
100 * with the kernel. However, AF_ definitions are currently in use and
101 * prevalent across the code. Define a new FRR-specific AF for EVPN to
102 * distinguish between 'ethernet' (MAC-only) and 'evpn' prefixes and
103 * ensure it does not conflict with any OS AF_ definition.
104 */
105 #if !defined(AF_EVPN)
106 #define AF_EVPN (AF_MAX + 1)
107 #endif
108
109 /* FRR generic prefix structure. */
110 struct prefix {
111 uint8_t family;
112 uint8_t prefixlen;
113 union {
114 uint8_t prefix;
115 struct in_addr prefix4;
116 struct in6_addr prefix6;
117 struct {
118 struct in_addr id;
119 struct in_addr adv_router;
120 } lp;
121 struct ethaddr prefix_eth; /* AF_ETHERNET */
122 uint8_t val[16];
123 uintptr_t ptr;
124 struct evpn_addr prefix_evpn; /* AF_EVPN */
125 } u __attribute__((aligned(8)));
126 };
127
128 /* IPv4 prefix structure. */
129 struct prefix_ipv4 {
130 uint8_t family;
131 uint8_t prefixlen;
132 struct in_addr prefix __attribute__((aligned(8)));
133 };
134
135 /* IPv6 prefix structure. */
136 struct prefix_ipv6 {
137 uint8_t family;
138 uint8_t prefixlen;
139 struct in6_addr prefix __attribute__((aligned(8)));
140 };
141
142 struct prefix_ls {
143 uint8_t family;
144 uint8_t prefixlen;
145 struct in_addr id __attribute__((aligned(8)));
146 struct in_addr adv_router;
147 };
148
149 /* Prefix for routing distinguisher. */
150 struct prefix_rd {
151 uint8_t family;
152 uint8_t prefixlen;
153 uint8_t val[8] __attribute__((aligned(8)));
154 };
155
156 /* Prefix for ethernet. */
157 struct prefix_eth {
158 uint8_t family;
159 uint8_t prefixlen;
160 struct ethaddr eth_addr __attribute__((aligned(8))); /* AF_ETHERNET */
161 };
162
163 /* EVPN prefix structure. */
164 struct prefix_evpn {
165 uint8_t family;
166 uint8_t prefixlen;
167 struct evpn_addr prefix __attribute__((aligned(8)));
168 };
169
170 /* Prefix for a generic pointer */
171 struct prefix_ptr {
172 uint8_t family;
173 uint8_t prefixlen;
174 uintptr_t prefix __attribute__((aligned(8)));
175 };
176
177 struct prefix_sg {
178 uint8_t family;
179 uint8_t prefixlen;
180 struct in_addr src __attribute__((aligned(8)));
181 struct in_addr grp;
182 };
183
184 /* helper to get type safety/avoid casts on calls
185 * (w/o this, functions accepting all prefix types need casts on the caller
186 * side, which strips type safety since the cast will accept any pointer
187 * type.)
188 */
189 union prefixptr {
190 struct prefix *p;
191 struct prefix_ipv4 *p4;
192 struct prefix_ipv6 *p6;
193 struct prefix_evpn *evp;
194 } __attribute__((transparent_union));
195
196 union prefixconstptr {
197 const struct prefix *p;
198 const struct prefix_ipv4 *p4;
199 const struct prefix_ipv6 *p6;
200 const struct prefix_evpn *evp;
201 } __attribute__((transparent_union));
202
203 #ifndef INET_ADDRSTRLEN
204 #define INET_ADDRSTRLEN 16
205 #endif /* INET_ADDRSTRLEN */
206
207 #ifndef INET6_ADDRSTRLEN
208 #define INET6_ADDRSTRLEN 46
209 #endif /* INET6_ADDRSTRLEN */
210
211 #ifndef INET6_BUFSIZ
212 #define INET6_BUFSIZ 51
213 #endif /* INET6_BUFSIZ */
214
215 /* Maximum prefix string length (IPv6) */
216 #define PREFIX_STRLEN 51
217
218 /* Max bit/byte length of IPv4 address. */
219 #define IPV4_MAX_BYTELEN 4
220 #define IPV4_MAX_BITLEN 32
221 #define IPV4_MAX_PREFIXLEN 32
222 #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
223
224 static inline bool ipv4_addr_same(const struct in_addr *a,
225 const struct in_addr *b)
226 {
227 return (a->s_addr == b->s_addr);
228 }
229 #define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
230
231 static inline void ipv4_addr_copy(struct in_addr *dst,
232 const struct in_addr *src)
233 {
234 dst->s_addr = src->s_addr;
235 }
236 #define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
237
238 #define IPV4_NET0(a) ((((uint32_t)(a)) & 0xff000000) == 0x00000000)
239 #define IPV4_NET127(a) ((((uint32_t)(a)) & 0xff000000) == 0x7f000000)
240 #define IPV4_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffff0000) == 0xa9fe0000)
241 #define IPV4_CLASS_DE(a) ((((uint32_t)(a)) & 0xe0000000) == 0xe0000000)
242 #define IPV4_MC_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffffff00) == 0xe0000000)
243
244 /* Max bit/byte length of IPv6 address. */
245 #define IPV6_MAX_BYTELEN 16
246 #define IPV6_MAX_BITLEN 128
247 #define IPV6_MAX_PREFIXLEN 128
248 #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
249 #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
250 #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
251
252 /* Count prefix size from mask length */
253 #define PSIZE(a) (((a) + 7) / (8))
254
255 #define BSIZE(a) ((a) * (8))
256
257 /* Prefix's family member. */
258 #define PREFIX_FAMILY(p) ((p)->family)
259
260 /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
261 #ifndef s6_addr32
262 #if defined(SUNOS_5)
263 /* Some SunOS define s6_addr32 only to kernel */
264 #define s6_addr32 _S6_un._S6_u32
265 #else
266 #define s6_addr32 __u6_addr.__u6_addr32
267 #endif /* SUNOS_5 */
268 #endif /*s6_addr32*/
269
270 /* Prototypes. */
271 extern int str2family(const char *);
272 extern int afi2family(afi_t);
273 extern afi_t family2afi(int);
274 extern const char *safi2str(safi_t safi);
275 extern const char *afi2str(afi_t afi);
276
277 /* Check bit of the prefix. */
278 extern unsigned int prefix_bit(const uint8_t *prefix, const uint8_t prefixlen);
279 extern unsigned int prefix6_bit(const struct in6_addr *prefix,
280 const uint8_t prefixlen);
281
282 extern struct prefix *prefix_new(void);
283 extern void prefix_free(struct prefix *);
284 extern const char *prefix_family_str(const struct prefix *);
285 extern int prefix_blen(const struct prefix *);
286 extern int str2prefix(const char *, struct prefix *);
287
288 #define PREFIX2STR_BUFFER PREFIX_STRLEN
289
290 extern const char *prefix2str(union prefixconstptr, char *, int);
291 extern int prefix_match(const struct prefix *, const struct prefix *);
292 extern int prefix_match_network_statement(const struct prefix *,
293 const struct prefix *);
294 extern int prefix_same(const struct prefix *, const struct prefix *);
295 extern int prefix_cmp(const struct prefix *, const struct prefix *);
296 extern int prefix_common_bits(const struct prefix *, const struct prefix *);
297 extern void prefix_copy(struct prefix *dest, const struct prefix *src);
298 extern void apply_mask(struct prefix *);
299
300 extern struct prefix *sockunion2prefix(const union sockunion *dest,
301 const union sockunion *mask);
302 extern struct prefix *sockunion2hostprefix(const union sockunion *,
303 struct prefix *p);
304 extern void prefix2sockunion(const struct prefix *, union sockunion *);
305
306 extern int str2prefix_eth(const char *, struct prefix_eth *);
307
308 extern struct prefix_ipv4 *prefix_ipv4_new(void);
309 extern void prefix_ipv4_free(struct prefix_ipv4 *);
310 extern int str2prefix_ipv4(const char *, struct prefix_ipv4 *);
311 extern void apply_mask_ipv4(struct prefix_ipv4 *);
312
313 #define PREFIX_COPY(DST, SRC) \
314 *((struct prefix *)(DST)) = *((const struct prefix *)(SRC))
315 #define PREFIX_COPY_IPV4(DST, SRC) \
316 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
317
318 extern int prefix_ipv4_any(const struct prefix_ipv4 *);
319 extern void apply_classful_mask_ipv4(struct prefix_ipv4 *);
320
321 extern uint8_t ip_masklen(struct in_addr);
322 extern void masklen2ip(const int, struct in_addr *);
323 /* returns the network portion of the host address */
324 extern in_addr_t ipv4_network_addr(in_addr_t hostaddr, int masklen);
325 /* given the address of a host on a network and the network mask length,
326 * calculate the broadcast address for that network;
327 * special treatment for /31: returns the address of the other host
328 * on the network by flipping the host bit */
329 extern in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen);
330
331 extern int netmask_str2prefix_str(const char *, const char *, char *);
332
333 extern struct prefix_ipv6 *prefix_ipv6_new(void);
334 extern void prefix_ipv6_free(struct prefix_ipv6 *);
335 extern int str2prefix_ipv6(const char *, struct prefix_ipv6 *);
336 extern void apply_mask_ipv6(struct prefix_ipv6 *);
337
338 #define PREFIX_COPY_IPV6(DST, SRC) \
339 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
340
341 extern int ip6_masklen(struct in6_addr);
342 extern void masklen2ip6(const int, struct in6_addr *);
343
344 extern const char *inet6_ntoa(struct in6_addr);
345
346 extern int is_zero_mac(struct ethaddr *mac);
347 extern int prefix_str2mac(const char *str, struct ethaddr *mac);
348 extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
349
350 extern unsigned prefix_hash_key(void *pp);
351
352 static inline int ipv6_martian(struct in6_addr *addr)
353 {
354 struct in6_addr localhost_addr;
355
356 inet_pton(AF_INET6, "::1", &localhost_addr);
357
358 if (IPV6_ADDR_SAME(&localhost_addr, addr))
359 return 1;
360
361 return 0;
362 }
363
364 extern int all_digit(const char *);
365 extern int macstr2prefix_evpn(const char *str, struct prefix_evpn *p);
366
367 /* NOTE: This routine expects the address argument in network byte order. */
368 static inline int ipv4_martian(struct in_addr *addr)
369 {
370 in_addr_t ip = ntohl(addr->s_addr);
371
372 if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) {
373 return 1;
374 }
375 return 0;
376 }
377
378 static inline int is_default_prefix(const struct prefix *p)
379 {
380 if (!p)
381 return 0;
382
383 if ((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY)
384 && (p->prefixlen == 0))
385 return 1;
386
387 if ((p->family == AF_INET6) && (p->prefixlen == 0)
388 && (!memcmp(&p->u.prefix6, &in6addr_any, sizeof(struct in6_addr))))
389 return 1;
390
391 return 0;
392 }
393
394 static inline int is_host_route(struct prefix *p)
395 {
396 if (p->family == AF_INET)
397 return (p->prefixlen == IPV4_MAX_BITLEN);
398 else if (p->family == AF_INET6)
399 return (p->prefixlen == IPV6_MAX_BITLEN);
400 return 0;
401 }
402
403 #endif /* _ZEBRA_PREFIX_H */