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