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 */
32 typedef unsigned int uint32_t;
33 typedef unsigned short uint16_t;
34 typedef unsigned char uint8_t;
50 #endif /* HAVE_STROPTS_H */
51 #include <sys/select.h>
53 #include <sys/types.h>
54 #include <sys/param.h>
55 #ifdef HAVE_SYS_SYSCTL_H
57 #include <linux/types.h>
59 #include <sys/sysctl.h>
60 #endif /* HAVE_SYS_SYSCTL_H */
61 #include <sys/ioctl.h>
62 #ifdef HAVE_SYS_CONF_H
64 #endif /* HAVE_SYS_CONF_H */
65 #ifdef HAVE_SYS_KSYM_H
67 #endif /* HAVE_SYS_KSYM_H */
72 #include <sys/utsname.h>
73 #include <sys/resource.h>
78 /* machine dependent includes */
83 /* machine dependent includes */
84 #ifdef HAVE_LINUX_VERSION_H
85 #include <linux/version.h>
86 #endif /* HAVE_LINUX_VERSION_H */
88 #ifdef HAVE_ASM_TYPES_H
89 #include <asm/types.h>
90 #endif /* HAVE_ASM_TYPES_H */
92 /* misc include group */
94 #if !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
95 /* Not C99; do we need to define va_copy? */
98 #define va_copy(DST,SRC) __va_copy(DST,SRC)
100 /* Now we are desperate; this should work on many typical platforms.
101 But this is slightly dangerous, because the standard does not require
102 va_copy to be a macro. */
103 #define va_copy(DST,SRC) memcpy(&(DST), &(SRC), sizeof(va_list))
104 #warning "Not C99 and no va_copy macro available, falling back to memcpy"
105 #endif /* __va_copy */
106 #endif /* !va_copy */
111 #include <sys/capability.h>
112 #include <sys/prctl.h>
113 #endif /* HAVE_LCAPS */
115 #ifdef HAVE_SOLARIS_CAPABILITIES
117 #endif /* HAVE_SOLARIS_CAPABILITIES */
119 /* network include group */
121 #include <sys/socket.h>
123 #ifdef HAVE_SYS_SOCKIO_H
124 #include <sys/sockio.h>
125 #endif /* HAVE_SYS_SOCKIO_H */
128 #define __APPLE_USE_RFC_3542
131 #ifndef HAVE_LIBCRYPT
132 #ifdef HAVE_LIBCRYPTO
133 #include <openssl/des.h>
134 # define crypt DES_crypt
138 #include "openbsd-tree.h"
140 #include <netinet/in.h>
141 #include <netinet/in_systm.h>
142 #include <netinet/ip.h>
143 #include <netinet/tcp.h>
145 #ifdef HAVE_NET_NETOPT_H
146 #include <net/netopt.h>
147 #endif /* HAVE_NET_NETOPT_H */
151 #ifdef HAVE_NET_IF_DL_H
152 #include <net/if_dl.h>
153 #endif /* HAVE_NET_IF_DL_H */
155 #ifdef HAVE_NET_IF_VAR_H
156 #include <net/if_var.h>
157 #endif /* HAVE_NET_IF_VAR_H */
159 #include <net/route.h>
162 #include <linux/netlink.h>
163 #include <linux/rtnetlink.h>
164 #include <linux/filter.h>
166 #define RT_TABLE_MAIN 0
167 #endif /* HAVE_NETLINK */
170 #include <arpa/inet.h>
172 #ifdef HAVE_INET_ND_H
174 #endif /* HAVE_INET_ND_H */
176 #ifdef HAVE_NETINET_IN_VAR_H
177 #include <netinet/in_var.h>
178 #endif /* HAVE_NETINET_IN_VAR_H */
180 #ifdef HAVE_NETINET6_IN6_VAR_H
181 #include <netinet6/in6_var.h>
182 #endif /* HAVE_NETINET6_IN6_VAR_H */
184 #ifdef HAVE_NETINET_IN6_VAR_H
185 #include <netinet/in6_var.h>
186 #endif /* HAVE_NETINET_IN6_VAR_H */
188 #ifdef HAVE_NETINET6_IN_H
189 #include <netinet6/in.h>
190 #endif /* HAVE_NETINET6_IN_H */
193 #ifdef HAVE_NETINET6_IP6_H
194 #include <netinet6/ip6.h>
195 #endif /* HAVE_NETINET6_IP6_H */
197 #include <netinet/icmp6.h>
199 #ifdef HAVE_NETINET6_ND6_H
200 #include <netinet6/nd6.h>
201 #endif /* HAVE_NETINET6_ND6_H */
203 /* Some systems do not define UINT32_MAX, etc.. from inttypes.h
204 * e.g. this makes life easier for FBSD 4.11 users.
207 #define INT8_MAX (127)
210 #define INT16_MAX (32767)
213 #define INT32_MAX (2147483647)
216 #define UINT8_MAX (255U)
219 #define UINT16_MAX (65535U)
222 #define UINT32_MAX (4294967295U)
225 #ifdef HAVE_GLIBC_BACKTRACE
226 #include <execinfo.h>
227 #endif /* HAVE_GLIBC_BACKTRACE */
229 /* Local includes: */
230 #if !(defined(__GNUC__) || defined(VTYSH_EXTRACT_PL))
231 #define __attribute__(x)
232 #endif /* !__GNUC__ || VTYSH_EXTRACT_PL */
237 size_t strlcat(char *__restrict dest
,
238 const char *__restrict src
, size_t destsize
);
241 size_t strlcpy(char *__restrict dest
,
242 const char *__restrict src
, size_t destsize
);
245 #ifdef HAVE_BROKEN_CMSG_FIRSTHDR
246 /* This bug is present in Solaris 8 and pre-patch Solaris 9 <sys/socket.h>;
247 please refer to http://bugzilla.quagga.net/show_bug.cgi?id=142 */
249 /* Check that msg_controllen is large enough. */
250 #define ZCMSG_FIRSTHDR(mhdr) \
251 (((size_t)((mhdr)->msg_controllen) >= sizeof(struct cmsghdr)) \
252 ? CMSG_FIRSTHDR(mhdr) \
253 : (struct cmsghdr *)NULL)
255 #warning "CMSG_FIRSTHDR is broken on this platform, using a workaround"
257 #else /* HAVE_BROKEN_CMSG_FIRSTHDR */
258 #define ZCMSG_FIRSTHDR(M) CMSG_FIRSTHDR(M)
259 #endif /* HAVE_BROKEN_CMSG_FIRSTHDR */
262 /* GCC have printf type attribute check. */
264 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
266 #define PRINTF_ATTRIBUTE(a,b)
267 #endif /* __GNUC__ */
270 * RFC 3542 defines several macros for using struct cmsghdr.
271 * Here, we define those that are not present
275 * Internal defines, for use only in this file.
276 * These are likely wrong on other than ILP32 machines, so warn.
278 #ifndef _CMSG_DATA_ALIGN
279 #define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
280 #endif /* _CMSG_DATA_ALIGN */
282 #ifndef _CMSG_HDR_ALIGN
283 #define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
284 #endif /* _CMSG_HDR_ALIGN */
287 * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
291 #define CMSG_SPACE(l) \
292 (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + _CMSG_HDR_ALIGN(l))
293 #warning "assuming 4-byte alignment for CMSG_SPACE"
294 #endif /* CMSG_SPACE */
298 #define CMSG_LEN(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + (l))
299 #warning "assuming 4-byte alignment for CMSG_LEN"
300 #endif /* CMSG_LEN */
303 /* The definition of struct in_pktinfo is missing in old version of
304 GLIBC 2.1 (Redhat 6.1). */
305 #if defined(GNU_LINUX) && !defined(HAVE_STRUCT_IN_PKTINFO)
308 struct in_addr ipi_spec_dst
;
309 struct in_addr ipi_addr
;
314 * IP_HDRINCL / struct ip byte order
316 * Linux: network byte order
317 * *BSD: network, except for length and offset. (cf Stevens)
318 * SunOS: nominally as per BSD. but bug: network order on LE.
319 * OpenBSD: network byte order, apart from older versions which are as per
322 #if defined(__NetBSD__) \
323 || (defined(__FreeBSD__) && (__FreeBSD_version < 1100030)) \
324 || (defined(__OpenBSD__) && (OpenBSD < 200311)) \
325 || (defined(__APPLE__)) \
326 || (defined(SUNOS_5) && defined(WORDS_BIGENDIAN))
327 #define HAVE_IP_HDRINCL_BSD_ORDER
330 /* Define BYTE_ORDER, if not defined. Useful for compiler conditional
331 * code, rather than preprocessor conditional.
332 * Not all the world has this BSD define.
335 #define BIG_ENDIAN 4321 /* least-significant byte first (vax, pc) */
336 #define LITTLE_ENDIAN 1234 /* most-significant byte first (IBM, net) */
337 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
339 #if defined(WORDS_BIGENDIAN)
340 #define BYTE_ORDER BIG_ENDIAN
341 #else /* !WORDS_BIGENDIAN */
342 #define BYTE_ORDER LITTLE_ENDIAN
343 #endif /* WORDS_BIGENDIAN */
345 #endif /* ndef BYTE_ORDER */
347 /* MAX / MIN are not commonly defined, but useful */
348 /* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
354 typeof(a) _a = (a); \
355 typeof(b) _b = (b); \
363 typeof(a) _a = (a); \
364 typeof(b) _b = (b); \
368 #define ZEBRA_NUM_OF(x) (sizeof (x) / sizeof (x[0]))
370 /* For old definition. */
371 #ifndef IN6_ARE_ADDR_EQUAL
372 #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
373 #endif /* IN6_ARE_ADDR_EQUAL */
375 /* default zebra TCP port for zclient */
376 #define ZEBRA_PORT 2600
378 /* Marker value used in new Zserv, in the byte location corresponding
379 * the command value in the old zserv header. To allow old and new
380 * Zserv headers to be distinguished from each other.
382 #define ZEBRA_HEADER_MARKER 254
384 /* Zebra route's types are defined in route_types.h */
385 #include "route_types.h"
387 /* Note: whenever a new route-type or zserv-command is added the
388 * corresponding {command,route}_types[] table in lib/log.c MUST be
391 /* Map a route type to a string. For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
392 extern const char *zebra_route_string(unsigned int route_type
);
393 /* Map a route type to a char. For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
394 extern char zebra_route_char(unsigned int route_type
);
395 /* Map a zserv command type to the same string,
396 * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
397 /* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
398 extern int proto_name2num(const char *s
);
399 /* Map redistribute X argument to protocol number.
400 * unlike proto_name2num, this accepts shorthands and takes
401 * an AFI value to restrict input */
402 extern int proto_redistnum(int afi
, const char *s
);
404 extern const char *zserv_command_string(unsigned int command
);
406 #define strmatch(a,b) (!strcmp((a), (b)))
408 /* Zebra message flags */
409 #define ZEBRA_FLAG_ALLOW_RECURSION 0x01
410 #define ZEBRA_FLAG_SELFROUTE 0x02
411 #define ZEBRA_FLAG_IBGP 0x08
412 #define ZEBRA_FLAG_SELECTED 0x10
413 #define ZEBRA_FLAG_STATIC 0x40
414 #define ZEBRA_FLAG_SCOPE_LINK 0x100
415 #define ZEBRA_FLAG_FIB_OVERRIDE 0x200
416 #define ZEBRA_FLAG_EVPN_ROUTE 0x400
417 /* ZEBRA_FLAG_BLACKHOLE was 0x04 */
418 /* ZEBRA_FLAG_REJECT was 0x80 */
420 /* Zebra FEC flags. */
421 #define ZEBRA_FEC_REGISTER_LABEL_INDEX 0x1
423 #ifndef INADDR_LOOPBACK
424 #define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
427 /* Address family numbers from RFC1700. */
428 typedef enum { AFI_IP
= 1, AFI_IP6
= 2, AFI_L2VPN
= 3, AFI_MAX
= 4 } afi_t
;
430 /* Subsequent Address Family Identifier. */
437 SAFI_LABELED_UNICAST
= 6,
443 * The above AFI and SAFI definitions are for internal use. The protocol
444 * definitions (IANA values) as for example used in BGP protocol packets
445 * are defined below and these will get mapped to/from the internal values
446 * in the appropriate places.
447 * The rationale is that the protocol (IANA) values may be sparse and are
448 * not optimal for use in data-structure sizing.
449 * Note: Only useful (i.e., supported) values are defined below.
452 IANA_AFI_RESERVED
= 0,
461 IANA_SAFI_RESERVED
= 0,
462 IANA_SAFI_UNICAST
= 1,
463 IANA_SAFI_MULTICAST
= 2,
464 IANA_SAFI_LABELED_UNICAST
= 4,
467 IANA_SAFI_MPLS_VPN
= 128,
468 IANA_SAFI_FLOWSPEC
= 133
471 /* Default Administrative Distance of each protocol. */
472 #define ZEBRA_KERNEL_DISTANCE_DEFAULT 0
473 #define ZEBRA_CONNECT_DISTANCE_DEFAULT 0
474 #define ZEBRA_STATIC_DISTANCE_DEFAULT 1
475 #define ZEBRA_RIP_DISTANCE_DEFAULT 120
476 #define ZEBRA_RIPNG_DISTANCE_DEFAULT 120
477 #define ZEBRA_OSPF_DISTANCE_DEFAULT 110
478 #define ZEBRA_OSPF6_DISTANCE_DEFAULT 110
479 #define ZEBRA_ISIS_DISTANCE_DEFAULT 115
480 #define ZEBRA_IBGP_DISTANCE_DEFAULT 200
481 #define ZEBRA_EBGP_DISTANCE_DEFAULT 20
482 #define ZEBRA_TABLE_DISTANCE_DEFAULT 15
484 /* Flag manipulation macros. */
485 #define CHECK_FLAG(V,F) ((V) & (F))
486 #define SET_FLAG(V,F) (V) |= (F)
487 #define UNSET_FLAG(V,F) (V) &= ~(F)
488 #define RESET_FLAG(V) (V) = 0
489 #define COND_FLAG(V, F, C) ((C) ? (SET_FLAG(V, F)) : (UNSET_FLAG(V, F)))
491 /* Atomic flag manipulation macros. */
492 #define CHECK_FLAG_ATOMIC(PV, F) \
493 ((atomic_load_explicit(PV, memory_order_seq_cst)) & (F))
494 #define SET_FLAG_ATOMIC(PV, F) \
495 ((atomic_fetch_or_explicit(PV, (F), memory_order_seq_cst)))
496 #define UNSET_FLAG_ATOMIC(PV, F) \
497 ((atomic_fetch_and_explicit(PV, ~(F), memory_order_seq_cst)))
498 #define RESET_FLAG_ATOMIC(PV) \
499 ((atomic_store_explicit(PV, 0, memory_order_seq_cst)))
501 /* Zebra types. Used in Zserv message header. */
502 typedef uint16_t zebra_size_t
;
503 typedef uint16_t zebra_command_t
;
506 typedef uint32_t vrf_id_t
;
508 typedef uint32_t route_tag_t
;
509 #define ROUTE_TAG_MAX UINT32_MAX
510 #define ROUTE_TAG_PRI PRIu32
512 static inline afi_t
afi_iana2int(iana_afi_t afi
)
526 static inline iana_afi_t
afi_int2iana(afi_t afi
)
530 return IANA_AFI_IPV4
;
532 return IANA_AFI_IPV6
;
534 return IANA_AFI_L2VPN
;
536 return IANA_AFI_RESERVED
;
540 static inline safi_t
safi_iana2int(iana_safi_t safi
)
543 case IANA_SAFI_UNICAST
:
545 case IANA_SAFI_MULTICAST
:
546 return SAFI_MULTICAST
;
547 case IANA_SAFI_MPLS_VPN
:
548 return SAFI_MPLS_VPN
;
549 case IANA_SAFI_ENCAP
:
553 case IANA_SAFI_LABELED_UNICAST
:
554 return SAFI_LABELED_UNICAST
;
555 case IANA_SAFI_FLOWSPEC
:
556 return SAFI_FLOWSPEC
;
562 static inline iana_safi_t
safi_int2iana(safi_t safi
)
566 return IANA_SAFI_UNICAST
;
568 return IANA_SAFI_MULTICAST
;
570 return IANA_SAFI_MPLS_VPN
;
572 return IANA_SAFI_ENCAP
;
574 return IANA_SAFI_EVPN
;
575 case SAFI_LABELED_UNICAST
:
576 return IANA_SAFI_LABELED_UNICAST
;
578 return IANA_SAFI_FLOWSPEC
;
580 return IANA_SAFI_RESERVED
;
584 #endif /* _ZEBRA_H */