]> git.proxmox.com Git - mirror_frr.git/blob - lib/zebra.h
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / lib / zebra.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Zebra common header.
3 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Kunihiro Ishiguro
4 */
5
6 #ifndef _ZEBRA_H
7 #define _ZEBRA_H
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #include "compiler.h"
14
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stddef.h>
19 #include <ctype.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <pwd.h>
25 #include <grp.h>
26 #ifdef HAVE_STROPTS_H
27 #include <stropts.h>
28 #endif /* HAVE_STROPTS_H */
29 #include <sys/select.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #ifdef HAVE_SYS_SYSCTL_H
34 #ifdef GNU_LINUX
35 #include <linux/types.h>
36 #else
37 #include <sys/sysctl.h>
38 #endif
39 #endif /* HAVE_SYS_SYSCTL_H */
40 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_CONF_H
42 #include <sys/conf.h>
43 #endif /* HAVE_SYS_CONF_H */
44 #ifdef HAVE_SYS_KSYM_H
45 #include <sys/ksym.h>
46 #endif /* HAVE_SYS_KSYM_H */
47 #include <syslog.h>
48 #include <sys/time.h>
49 #include <time.h>
50 #include <sys/uio.h>
51 #include <sys/utsname.h>
52 #include <sys/resource.h>
53 #include <limits.h>
54 #include <inttypes.h>
55 #include <stdbool.h>
56 #ifdef HAVE_SYS_ENDIAN_H
57 #include <sys/endian.h>
58 #endif
59 #ifdef HAVE_ENDIAN_H
60 #include <endian.h>
61 #endif
62
63 /* misc include group */
64 #include <stdarg.h>
65
66 #ifdef HAVE_LCAPS
67 #include <sys/capability.h>
68 #include <sys/prctl.h>
69 #endif /* HAVE_LCAPS */
70
71 /* network include group */
72
73 #include <sys/socket.h>
74
75 #ifdef HAVE_SYS_SOCKIO_H
76 #include <sys/sockio.h>
77 #endif /* HAVE_SYS_SOCKIO_H */
78
79 #ifdef __APPLE__
80 #define __APPLE_USE_RFC_3542
81 #endif
82
83 #ifndef HAVE_LIBCRYPT
84 #ifdef HAVE_LIBCRYPTO
85 #include <openssl/des.h>
86 # define crypt DES_crypt
87 #endif
88 #endif
89
90 #ifdef CRYPTO_OPENSSL
91 #include <openssl/evp.h>
92 #include <openssl/hmac.h>
93 #endif
94
95 #include "openbsd-tree.h"
96
97 #include <netinet/in.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <netinet/tcp.h>
101
102 #ifdef HAVE_NET_NETOPT_H
103 #include <net/netopt.h>
104 #endif /* HAVE_NET_NETOPT_H */
105
106 #include <net/if.h>
107
108 #ifdef HAVE_NET_IF_DL_H
109 #include <net/if_dl.h>
110 #endif /* HAVE_NET_IF_DL_H */
111
112 #ifdef HAVE_NET_IF_VAR_H
113 #include <net/if_var.h>
114 #endif /* HAVE_NET_IF_VAR_H */
115
116 #include <net/route.h>
117
118 #ifdef HAVE_NETLINK
119 #include <linux/netlink.h>
120 #include <linux/rtnetlink.h>
121 #include <linux/filter.h>
122 #else
123 #define RT_TABLE_MAIN 0
124 #endif /* HAVE_NETLINK */
125
126 #include <netdb.h>
127 #include <arpa/inet.h>
128
129 #ifdef HAVE_INET_ND_H
130 #include <inet/nd.h>
131 #endif /* HAVE_INET_ND_H */
132
133 #ifdef HAVE_NETINET_IN_VAR_H
134 #include <netinet/in_var.h>
135 #endif /* HAVE_NETINET_IN_VAR_H */
136
137 #ifdef HAVE_NETINET6_IN6_VAR_H
138 #include <netinet6/in6_var.h>
139 #endif /* HAVE_NETINET6_IN6_VAR_H */
140
141 #ifdef HAVE_NETINET_IN6_VAR_H
142 #include <netinet/in6_var.h>
143 #endif /* HAVE_NETINET_IN6_VAR_H */
144
145 #ifdef HAVE_NETINET6_IN_H
146 #include <netinet6/in.h>
147 #endif /* HAVE_NETINET6_IN_H */
148
149
150 #ifdef HAVE_NETINET6_IP6_H
151 #include <netinet6/ip6.h>
152 #endif /* HAVE_NETINET6_IP6_H */
153
154 #include <netinet/icmp6.h>
155
156 #ifdef HAVE_NETINET6_ND6_H
157 #include <netinet6/nd6.h>
158 #endif /* HAVE_NETINET6_ND6_H */
159
160 /* Some systems do not define UINT32_MAX, etc.. from inttypes.h
161 * e.g. this makes life easier for FBSD 4.11 users.
162 */
163 #ifndef INT16_MAX
164 #define INT16_MAX (32767)
165 #endif
166 #ifndef INT32_MAX
167 #define INT32_MAX (2147483647)
168 #endif
169 #ifndef UINT16_MAX
170 #define UINT16_MAX (65535U)
171 #endif
172 #ifndef UINT32_MAX
173 #define UINT32_MAX (4294967295U)
174 #endif
175
176 #ifdef HAVE_GLIBC_BACKTRACE
177 #include <execinfo.h>
178 #endif /* HAVE_GLIBC_BACKTRACE */
179
180 /* Local includes: */
181 #if !defined(__GNUC__)
182 #define __attribute__(x)
183 #endif /* !__GNUC__ */
184
185 #include <assert.h>
186
187 /*
188 * Add explicit static cast only when using a C++ compiler.
189 */
190 #ifdef __cplusplus
191 #define static_cast(l, r) static_cast<decltype(l)>((r))
192 #else
193 #define static_cast(l, r) (r)
194 #endif
195
196 #ifdef __cplusplus
197 extern "C" {
198 #endif
199
200 #ifndef HAVE_STRLCAT
201 size_t strlcat(char *__restrict dest,
202 const char *__restrict src, size_t destsize);
203 #endif
204 #ifndef HAVE_STRLCPY
205 size_t strlcpy(char *__restrict dest,
206 const char *__restrict src, size_t destsize);
207 #endif
208
209 #ifndef HAVE_EXPLICIT_BZERO
210 void explicit_bzero(void *buf, size_t len);
211 #endif
212
213 #if !defined(HAVE_STRUCT_MMSGHDR_MSG_HDR) || !defined(HAVE_SENDMMSG)
214 /* avoid conflicts in case we have partial support */
215 #define mmsghdr frr_mmsghdr
216 #define sendmmsg frr_sendmmsg
217
218 struct mmsghdr {
219 struct msghdr msg_hdr;
220 unsigned int msg_len;
221 };
222
223 /* just go 1 at a time here, the loop this is used in will handle the rest */
224 static inline int sendmmsg(int fd, struct mmsghdr *mmh, unsigned int len,
225 int flags)
226 {
227 int rv = sendmsg(fd, &mmh->msg_hdr, 0);
228
229 return rv > 0 ? 1 : rv;
230 }
231 #endif
232
233 /*
234 * RFC 3542 defines several macros for using struct cmsghdr.
235 * Here, we define those that are not present
236 */
237
238 /*
239 * Internal defines, for use only in this file.
240 * These are likely wrong on other than ILP32 machines, so warn.
241 */
242 #ifndef _CMSG_DATA_ALIGN
243 #define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
244 #endif /* _CMSG_DATA_ALIGN */
245
246 #ifndef _CMSG_HDR_ALIGN
247 #define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
248 #endif /* _CMSG_HDR_ALIGN */
249
250 /*
251 * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
252 * version.
253 */
254 #ifndef CMSG_SPACE
255 #define CMSG_SPACE(l) \
256 (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + _CMSG_HDR_ALIGN(l))
257 #warning "assuming 4-byte alignment for CMSG_SPACE"
258 #endif /* CMSG_SPACE */
259
260
261 #ifndef CMSG_LEN
262 #define CMSG_LEN(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + (l))
263 #warning "assuming 4-byte alignment for CMSG_LEN"
264 #endif /* CMSG_LEN */
265
266
267 /* The definition of struct in_pktinfo is missing in old version of
268 GLIBC 2.1 (Redhat 6.1). */
269 #if defined(GNU_LINUX) && !defined(HAVE_STRUCT_IN_PKTINFO)
270 struct in_pktinfo {
271 int ipi_ifindex;
272 struct in_addr ipi_spec_dst;
273 struct in_addr ipi_addr;
274 };
275 #endif
276
277 /*
278 * IP_HDRINCL / struct ip byte order
279 *
280 * Linux: network byte order
281 * *BSD: network, except for length and offset. (cf Stevens)
282 * SunOS: nominally as per BSD. but bug: network order on LE.
283 * OpenBSD: network byte order, apart from older versions which are as per
284 * *BSD
285 */
286 #if defined(__NetBSD__) \
287 || (defined(__FreeBSD__) && (__FreeBSD_version < 1100030)) \
288 || (defined(__OpenBSD__) && (OpenBSD < 200311)) \
289 || (defined(__APPLE__))
290 #define HAVE_IP_HDRINCL_BSD_ORDER
291 #endif
292
293 /* autoconf macros for this are deprecated, just find endian.h */
294 #ifndef BYTE_ORDER
295 #error please locate an endian.h file appropriate to your platform
296 #endif
297
298 /* For old definition. */
299 #ifndef IN6_ARE_ADDR_EQUAL
300 #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
301 #endif /* IN6_ARE_ADDR_EQUAL */
302
303 /* default zebra TCP port for zclient */
304 #define ZEBRA_PORT 2600
305
306 /*
307 * The compiler.h header is used for anyone using the CPP_NOTICE
308 * since this is universally needed, let's add it to zebra.h
309 */
310 #include "compiler.h"
311
312 /* Zebra route's types are defined in route_types.h */
313 #include "lib/route_types.h"
314
315 #define strmatch(a,b) (!strcmp((a), (b)))
316
317 #if BYTE_ORDER == LITTLE_ENDIAN
318 #define htonll(x) (((uint64_t)htonl((x)&0xFFFFFFFF) << 32) | htonl((x) >> 32))
319 #define ntohll(x) (((uint64_t)ntohl((x)&0xFFFFFFFF) << 32) | ntohl((x) >> 32))
320 #else
321 #define htonll(x) (x)
322 #define ntohll(x) (x)
323 #endif
324
325 #ifndef INADDR_LOOPBACK
326 #define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
327 #endif
328
329 /* Address family numbers from RFC1700. */
330 typedef enum {
331 AFI_UNSPEC = 0,
332 AFI_IP = 1,
333 AFI_IP6 = 2,
334 AFI_L2VPN = 3,
335 AFI_MAX = 4
336 } afi_t;
337
338 #define IS_VALID_AFI(a) ((a) > AFI_UNSPEC && (a) < AFI_MAX)
339
340 /* Subsequent Address Family Identifier. */
341 typedef enum {
342 SAFI_UNSPEC = 0,
343 SAFI_UNICAST = 1,
344 SAFI_MULTICAST = 2,
345 SAFI_MPLS_VPN = 3,
346 SAFI_ENCAP = 4,
347 SAFI_EVPN = 5,
348 SAFI_LABELED_UNICAST = 6,
349 SAFI_FLOWSPEC = 7,
350 SAFI_MAX = 8
351 } safi_t;
352
353 #define FOREACH_AFI_SAFI(afi, safi) \
354 for (afi = AFI_IP; afi < AFI_MAX; afi++) \
355 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
356
357 #define FOREACH_AFI_SAFI_NSF(afi, safi) \
358 for (afi = AFI_IP; afi < AFI_MAX; afi++) \
359 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++)
360
361 /* Default Administrative Distance of each protocol. */
362 #define ZEBRA_KERNEL_DISTANCE_DEFAULT 0
363 #define ZEBRA_CONNECT_DISTANCE_DEFAULT 0
364 #define ZEBRA_STATIC_DISTANCE_DEFAULT 1
365 #define ZEBRA_RIP_DISTANCE_DEFAULT 120
366 #define ZEBRA_RIPNG_DISTANCE_DEFAULT 120
367 #define ZEBRA_OSPF_DISTANCE_DEFAULT 110
368 #define ZEBRA_OSPF6_DISTANCE_DEFAULT 110
369 #define ZEBRA_ISIS_DISTANCE_DEFAULT 115
370 #define ZEBRA_IBGP_DISTANCE_DEFAULT 200
371 #define ZEBRA_EBGP_DISTANCE_DEFAULT 20
372 #define ZEBRA_TABLE_DISTANCE_DEFAULT 15
373 #define ZEBRA_EIGRP_DISTANCE_DEFAULT 90
374 #define ZEBRA_NHRP_DISTANCE_DEFAULT 10
375 #define ZEBRA_LDP_DISTANCE_DEFAULT 150
376 #define ZEBRA_BABEL_DISTANCE_DEFAULT 100
377 #define ZEBRA_SHARP_DISTANCE_DEFAULT 150
378 #define ZEBRA_PBR_DISTANCE_DEFAULT 200
379 #define ZEBRA_OPENFABRIC_DISTANCE_DEFAULT 115
380 #define ZEBRA_MAX_DISTANCE_DEFAULT 255
381
382 /* Flag manipulation macros. */
383 #define CHECK_FLAG(V,F) ((V) & (F))
384 #define SET_FLAG(V,F) (V) |= (F)
385 #define UNSET_FLAG(V,F) (V) &= ~(F)
386 #define RESET_FLAG(V) (V) = 0
387 #define COND_FLAG(V, F, C) ((C) ? (SET_FLAG(V, F)) : (UNSET_FLAG(V, F)))
388
389 /* Atomic flag manipulation macros. */
390 #define CHECK_FLAG_ATOMIC(PV, F) \
391 ((atomic_load_explicit(PV, memory_order_seq_cst)) & (F))
392 #define SET_FLAG_ATOMIC(PV, F) \
393 ((atomic_fetch_or_explicit(PV, (F), memory_order_seq_cst)))
394 #define UNSET_FLAG_ATOMIC(PV, F) \
395 ((atomic_fetch_and_explicit(PV, ~(F), memory_order_seq_cst)))
396 #define RESET_FLAG_ATOMIC(PV) \
397 ((atomic_store_explicit(PV, 0, memory_order_seq_cst)))
398
399 /* VRF ID type. */
400 typedef uint32_t vrf_id_t;
401
402 typedef uint32_t route_tag_t;
403 #define ROUTE_TAG_MAX UINT32_MAX
404 #define ROUTE_TAG_PRI PRIu32
405
406 #ifdef __cplusplus
407 }
408 #endif
409
410 #endif /* _ZEBRA_H */