1 /* Zebra common header.
2 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #endif /* HAVE_CONFIG_H */
31 typedef unsigned int uint32_t;
32 typedef unsigned short uint16_t;
33 typedef unsigned char uint8_t;
49 #endif /* HAVE_STROPTS_H */
50 #include <sys/select.h>
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #ifdef HAVE_SYS_SYSCTL_H
56 #include <linux/types.h>
58 #include <sys/sysctl.h>
59 #endif /* HAVE_SYS_SYSCTL_H */
60 #include <sys/ioctl.h>
61 #ifdef HAVE_SYS_CONF_H
63 #endif /* HAVE_SYS_CONF_H */
64 #ifdef HAVE_SYS_KSYM_H
66 #endif /* HAVE_SYS_KSYM_H */
71 #include <sys/utsname.h>
72 #include <sys/resource.h>
77 /* machine dependent includes */
82 /* machine dependent includes */
83 #ifdef HAVE_LINUX_VERSION_H
84 #include <linux/version.h>
85 #endif /* HAVE_LINUX_VERSION_H */
87 #ifdef HAVE_ASM_TYPES_H
88 #include <asm/types.h>
89 #endif /* HAVE_ASM_TYPES_H */
91 /* misc include group */
93 #if !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
94 /* Not C99; do we need to define va_copy? */
97 #define va_copy(DST,SRC) __va_copy(DST,SRC)
99 /* Now we are desperate; this should work on many typical platforms.
100 But this is slightly dangerous, because the standard does not require
101 va_copy to be a macro. */
102 #define va_copy(DST,SRC) memcpy(&(DST), &(SRC), sizeof(va_list))
103 #warning "Not C99 and no va_copy macro available, falling back to memcpy"
104 #endif /* __va_copy */
105 #endif /* !va_copy */
110 #include <sys/capability.h>
111 #include <sys/prctl.h>
112 #endif /* HAVE_LCAPS */
114 #ifdef HAVE_SOLARIS_CAPABILITIES
116 #endif /* HAVE_SOLARIS_CAPABILITIES */
118 /* network include group */
120 #include <sys/socket.h>
122 #ifdef HAVE_SYS_SOCKIO_H
123 #include <sys/sockio.h>
124 #endif /* HAVE_SYS_SOCKIO_H */
127 #define __APPLE_USE_RFC_3542
130 #ifndef HAVE_LIBCRYPT
131 #ifdef HAVE_LIBCRYPTO
132 #include <openssl/des.h>
133 # define crypt DES_crypt
137 #include "openbsd-tree.h"
139 #include <netinet/in.h>
140 #include <netinet/in_systm.h>
141 #include <netinet/ip.h>
142 #include <netinet/tcp.h>
144 #ifdef HAVE_NET_NETOPT_H
145 #include <net/netopt.h>
146 #endif /* HAVE_NET_NETOPT_H */
150 #ifdef HAVE_NET_IF_DL_H
151 #include <net/if_dl.h>
152 #endif /* HAVE_NET_IF_DL_H */
154 #ifdef HAVE_NET_IF_VAR_H
155 #include <net/if_var.h>
156 #endif /* HAVE_NET_IF_VAR_H */
158 #include <net/route.h>
161 #include <linux/netlink.h>
162 #include <linux/rtnetlink.h>
163 #include <linux/filter.h>
165 #define RT_TABLE_MAIN 0
166 #endif /* HAVE_NETLINK */
169 #include <arpa/inet.h>
171 #ifdef HAVE_INET_ND_H
173 #endif /* HAVE_INET_ND_H */
175 #ifdef HAVE_NETINET_IN_VAR_H
176 #include <netinet/in_var.h>
177 #endif /* HAVE_NETINET_IN_VAR_H */
179 #ifdef HAVE_NETINET6_IN6_VAR_H
180 #include <netinet6/in6_var.h>
181 #endif /* HAVE_NETINET6_IN6_VAR_H */
183 #ifdef HAVE_NETINET_IN6_VAR_H
184 #include <netinet/in6_var.h>
185 #endif /* HAVE_NETINET_IN6_VAR_H */
187 #ifdef HAVE_NETINET6_IN_H
188 #include <netinet6/in.h>
189 #endif /* HAVE_NETINET6_IN_H */
192 #ifdef HAVE_NETINET6_IP6_H
193 #include <netinet6/ip6.h>
194 #endif /* HAVE_NETINET6_IP6_H */
196 #include <netinet/icmp6.h>
198 #ifdef HAVE_NETINET6_ND6_H
199 #include <netinet6/nd6.h>
200 #endif /* HAVE_NETINET6_ND6_H */
202 /* Some systems do not define UINT32_MAX, etc.. from inttypes.h
203 * e.g. this makes life easier for FBSD 4.11 users.
206 #define INT8_MAX (127)
209 #define INT16_MAX (32767)
212 #define INT32_MAX (2147483647)
215 #define UINT8_MAX (255U)
218 #define UINT16_MAX (65535U)
221 #define UINT32_MAX (4294967295U)
224 #ifdef HAVE_GLIBC_BACKTRACE
225 #include <execinfo.h>
226 #endif /* HAVE_GLIBC_BACKTRACE */
228 /* Local includes: */
229 #if !(defined(__GNUC__) || defined(VTYSH_EXTRACT_PL))
230 #define __attribute__(x)
231 #endif /* !__GNUC__ || VTYSH_EXTRACT_PL */
236 * Add explicit static cast only when using a C++ compiler.
239 #define static_cast(l, r) static_cast<decltype(l)>((r))
241 #define static_cast(l, r) (r)
245 size_t strlcat(char *__restrict dest
,
246 const char *__restrict src
, size_t destsize
);
249 size_t strlcpy(char *__restrict dest
,
250 const char *__restrict src
, size_t destsize
);
253 /* GCC have printf type attribute check. */
255 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
257 #define PRINTF_ATTRIBUTE(a,b)
258 #endif /* __GNUC__ */
261 * RFC 3542 defines several macros for using struct cmsghdr.
262 * Here, we define those that are not present
266 * Internal defines, for use only in this file.
267 * These are likely wrong on other than ILP32 machines, so warn.
269 #ifndef _CMSG_DATA_ALIGN
270 #define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
271 #endif /* _CMSG_DATA_ALIGN */
273 #ifndef _CMSG_HDR_ALIGN
274 #define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
275 #endif /* _CMSG_HDR_ALIGN */
278 * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
282 #define CMSG_SPACE(l) \
283 (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + _CMSG_HDR_ALIGN(l))
284 #warning "assuming 4-byte alignment for CMSG_SPACE"
285 #endif /* CMSG_SPACE */
289 #define CMSG_LEN(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + (l))
290 #warning "assuming 4-byte alignment for CMSG_LEN"
291 #endif /* CMSG_LEN */
294 /* The definition of struct in_pktinfo is missing in old version of
295 GLIBC 2.1 (Redhat 6.1). */
296 #if defined(GNU_LINUX) && !defined(HAVE_STRUCT_IN_PKTINFO)
299 struct in_addr ipi_spec_dst
;
300 struct in_addr ipi_addr
;
305 * IP_HDRINCL / struct ip byte order
307 * Linux: network byte order
308 * *BSD: network, except for length and offset. (cf Stevens)
309 * SunOS: nominally as per BSD. but bug: network order on LE.
310 * OpenBSD: network byte order, apart from older versions which are as per
313 #if defined(__NetBSD__) \
314 || (defined(__FreeBSD__) && (__FreeBSD_version < 1100030)) \
315 || (defined(__OpenBSD__) && (OpenBSD < 200311)) \
316 || (defined(__APPLE__)) \
317 || (defined(SUNOS_5) && defined(WORDS_BIGENDIAN))
318 #define HAVE_IP_HDRINCL_BSD_ORDER
321 /* Define BYTE_ORDER, if not defined. Useful for compiler conditional
322 * code, rather than preprocessor conditional.
323 * Not all the world has this BSD define.
326 #define BIG_ENDIAN 4321 /* least-significant byte first (vax, pc) */
327 #define LITTLE_ENDIAN 1234 /* most-significant byte first (IBM, net) */
328 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
330 #if defined(WORDS_BIGENDIAN)
331 #define BYTE_ORDER BIG_ENDIAN
332 #else /* !WORDS_BIGENDIAN */
333 #define BYTE_ORDER LITTLE_ENDIAN
334 #endif /* WORDS_BIGENDIAN */
336 #endif /* ndef BYTE_ORDER */
338 /* For old definition. */
339 #ifndef IN6_ARE_ADDR_EQUAL
340 #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
341 #endif /* IN6_ARE_ADDR_EQUAL */
343 /* default zebra TCP port for zclient */
344 #define ZEBRA_PORT 2600
346 /* Marker value used in new Zserv, in the byte location corresponding
347 * the command value in the old zserv header. To allow old and new
348 * Zserv headers to be distinguished from each other.
350 #define ZEBRA_HEADER_MARKER 254
353 * The compiler.h header is used for anyone using the CPP_NOTICE
354 * since this is universally needed, let's add it to zebra.h
356 #include "compiler.h"
358 /* Zebra route's types are defined in route_types.h */
359 #include "route_types.h"
361 /* Note: whenever a new route-type or zserv-command is added the
362 * corresponding {command,route}_types[] table in lib/log.c MUST be
365 /* Map a route type to a string. For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
366 extern const char *zebra_route_string(unsigned int route_type
);
367 /* Map a route type to a char. For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
368 extern char zebra_route_char(unsigned int route_type
);
369 /* Map a zserv command type to the same string,
370 * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
371 /* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
372 extern int proto_name2num(const char *s
);
373 /* Map redistribute X argument to protocol number.
374 * unlike proto_name2num, this accepts shorthands and takes
375 * an AFI value to restrict input */
376 extern int proto_redistnum(int afi
, const char *s
);
378 extern const char *zserv_command_string(unsigned int command
);
380 #define strmatch(a,b) (!strcmp((a), (b)))
382 /* Zebra message flags */
385 * Cause Zebra to consider this routes nexthops recursively
387 #define ZEBRA_FLAG_ALLOW_RECURSION 0x01
389 * This is a route that is read in on startup that was left around
390 * from a previous run of FRR
392 #define ZEBRA_FLAG_SELFROUTE 0x02
394 * This flag is used to tell Zebra that the BGP route being passed
395 * down is a IBGP route
397 #define ZEBRA_FLAG_IBGP 0x04
399 * This is a route that has been selected for FIB installation.
400 * This flag is set in zebra and can be passed up to routing daemons
402 #define ZEBRA_FLAG_SELECTED 0x08
404 * This is a route that we are telling Zebra that this route *must*
405 * win and will be installed even over ZEBRA_FLAG_SELECTED
407 #define ZEBRA_FLAG_FIB_OVERRIDE 0x10
409 * This flag tells Zebra that the route is a EVPN route and should
410 * be treated specially
412 #define ZEBRA_FLAG_EVPN_ROUTE 0x20
414 * This flag tells Zebra that it should treat the distance passed
415 * down as an additional discriminator for route selection of the
416 * route entry. This mainly is used for backup static routes.
418 #define ZEBRA_FLAG_RR_USE_DISTANCE 0x40
420 #ifndef INADDR_LOOPBACK
421 #define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
424 /* Address family numbers from RFC1700. */
433 /* Subsequent Address Family Identifier. */
440 SAFI_LABELED_UNICAST
= 6,
446 * The above AFI and SAFI definitions are for internal use. The protocol
447 * definitions (IANA values) as for example used in BGP protocol packets
448 * are defined below and these will get mapped to/from the internal values
449 * in the appropriate places.
450 * The rationale is that the protocol (IANA) values may be sparse and are
451 * not optimal for use in data-structure sizing.
452 * Note: Only useful (i.e., supported) values are defined below.
455 IANA_AFI_RESERVED
= 0,
462 IANA_SAFI_RESERVED
= 0,
463 IANA_SAFI_UNICAST
= 1,
464 IANA_SAFI_MULTICAST
= 2,
465 IANA_SAFI_LABELED_UNICAST
= 4,
468 IANA_SAFI_MPLS_VPN
= 128,
469 IANA_SAFI_FLOWSPEC
= 133
472 /* Default Administrative Distance of each protocol. */
473 #define ZEBRA_KERNEL_DISTANCE_DEFAULT 0
474 #define ZEBRA_CONNECT_DISTANCE_DEFAULT 0
475 #define ZEBRA_STATIC_DISTANCE_DEFAULT 1
476 #define ZEBRA_RIP_DISTANCE_DEFAULT 120
477 #define ZEBRA_RIPNG_DISTANCE_DEFAULT 120
478 #define ZEBRA_OSPF_DISTANCE_DEFAULT 110
479 #define ZEBRA_OSPF6_DISTANCE_DEFAULT 110
480 #define ZEBRA_ISIS_DISTANCE_DEFAULT 115
481 #define ZEBRA_IBGP_DISTANCE_DEFAULT 200
482 #define ZEBRA_EBGP_DISTANCE_DEFAULT 20
483 #define ZEBRA_TABLE_DISTANCE_DEFAULT 15
485 /* Flag manipulation macros. */
486 #define CHECK_FLAG(V,F) ((V) & (F))
487 #define SET_FLAG(V,F) (V) |= (F)
488 #define UNSET_FLAG(V,F) (V) &= ~(F)
489 #define RESET_FLAG(V) (V) = 0
490 #define COND_FLAG(V, F, C) ((C) ? (SET_FLAG(V, F)) : (UNSET_FLAG(V, F)))
492 /* Atomic flag manipulation macros. */
493 #define CHECK_FLAG_ATOMIC(PV, F) \
494 ((atomic_load_explicit(PV, memory_order_seq_cst)) & (F))
495 #define SET_FLAG_ATOMIC(PV, F) \
496 ((atomic_fetch_or_explicit(PV, (F), memory_order_seq_cst)))
497 #define UNSET_FLAG_ATOMIC(PV, F) \
498 ((atomic_fetch_and_explicit(PV, ~(F), memory_order_seq_cst)))
499 #define RESET_FLAG_ATOMIC(PV) \
500 ((atomic_store_explicit(PV, 0, memory_order_seq_cst)))
502 /* Zebra types. Used in Zserv message header. */
503 typedef uint16_t zebra_size_t
;
504 typedef uint16_t zebra_command_t
;
507 typedef uint32_t vrf_id_t
;
509 typedef uint32_t route_tag_t
;
510 #define ROUTE_TAG_MAX UINT32_MAX
511 #define ROUTE_TAG_PRI PRIu32
513 static inline const char *iana_afi2str(iana_afi_t afi
)
516 case IANA_AFI_RESERVED
:
529 static inline afi_t
afi_iana2int(iana_afi_t afi
)
543 static inline iana_afi_t
afi_int2iana(afi_t afi
)
547 return IANA_AFI_IPV4
;
549 return IANA_AFI_IPV6
;
551 return IANA_AFI_L2VPN
;
553 return IANA_AFI_RESERVED
;
557 static inline const char *iana_safi2str(iana_safi_t safi
)
560 case IANA_SAFI_RESERVED
:
562 case IANA_SAFI_UNICAST
:
564 case IANA_SAFI_MULTICAST
:
566 case IANA_SAFI_LABELED_UNICAST
:
567 return "Labeled Unicast";
568 case IANA_SAFI_ENCAP
:
572 case IANA_SAFI_MPLS_VPN
:
574 case IANA_SAFI_FLOWSPEC
:
581 static inline safi_t
safi_iana2int(iana_safi_t safi
)
584 case IANA_SAFI_UNICAST
:
586 case IANA_SAFI_MULTICAST
:
587 return SAFI_MULTICAST
;
588 case IANA_SAFI_MPLS_VPN
:
589 return SAFI_MPLS_VPN
;
590 case IANA_SAFI_ENCAP
:
594 case IANA_SAFI_LABELED_UNICAST
:
595 return SAFI_LABELED_UNICAST
;
596 case IANA_SAFI_FLOWSPEC
:
597 return SAFI_FLOWSPEC
;
603 static inline iana_safi_t
safi_int2iana(safi_t safi
)
607 return IANA_SAFI_UNICAST
;
609 return IANA_SAFI_MULTICAST
;
611 return IANA_SAFI_MPLS_VPN
;
613 return IANA_SAFI_ENCAP
;
615 return IANA_SAFI_EVPN
;
616 case SAFI_LABELED_UNICAST
:
617 return IANA_SAFI_LABELED_UNICAST
;
619 return IANA_SAFI_FLOWSPEC
;
621 return IANA_SAFI_RESERVED
;
625 #endif /* _ZEBRA_H */