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