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