]> git.proxmox.com Git - mirror_frr.git/blame - lib/zebra.h
build: massively remove needless checks
[mirror_frr.git] / lib / zebra.h
CommitLineData
718e3744 1/* Zebra common header.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#ifndef _ZEBRA_H
22#define _ZEBRA_H
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif /* HAVE_CONFIG_H */
27
28#ifdef SUNOS_5
29#define _XPG4_2
9bcdb638 30typedef unsigned int u_int32_t;
31typedef unsigned short u_int16_t;
32typedef unsigned char u_int8_t;
718e3744 33#endif /* SUNOS_5 */
34
35#include <unistd.h>
36#include <stdio.h>
37#include <stdlib.h>
c423d413 38#include <stddef.h>
718e3744 39#include <ctype.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <signal.h>
43#include <string.h>
edd7c245 44#include <pwd.h>
45#include <grp.h>
718e3744 46#ifdef HAVE_STROPTS_H
47#include <stropts.h>
48#endif /* HAVE_STROPTS_H */
718e3744 49#include <sys/select.h>
718e3744 50#include <sys/stat.h>
718e3744 51#include <sys/types.h>
52#include <sys/param.h>
53#ifdef HAVE_SYS_SYSCTL_H
0dc0b708
AS
54#ifdef GNU_LINUX
55#include <linux/types.h>
56#endif
718e3744 57#include <sys/sysctl.h>
58#endif /* HAVE_SYS_SYSCTL_H */
59#include <sys/ioctl.h>
60#ifdef HAVE_SYS_CONF_H
61#include <sys/conf.h>
62#endif /* HAVE_SYS_CONF_H */
63#ifdef HAVE_SYS_KSYM_H
64#include <sys/ksym.h>
65#endif /* HAVE_SYS_KSYM_H */
66#include <syslog.h>
24f5e2fc
DL
67#include <sys/time.h>
68#include <time.h>
718e3744 69#include <sys/uio.h>
70#include <sys/utsname.h>
718e3744 71#include <sys/resource.h>
a58c25b6 72#include <limits.h>
35cfc90c 73#include <inttypes.h>
63a13864 74#include <stdbool.h>
718e3744 75
76/* machine dependent includes */
77#ifdef SUNOS_5
718e3744 78#include <strings.h>
79#endif /* SUNOS_5 */
80
81/* machine dependent includes */
82#ifdef HAVE_LINUX_VERSION_H
83#include <linux/version.h>
84#endif /* HAVE_LINUX_VERSION_H */
85
ba965c6c 86#ifdef HAVE_ASM_TYPES_H
87#include <asm/types.h>
88#endif /* HAVE_ASM_TYPES_H */
89
718e3744 90/* misc include group */
91#include <stdarg.h>
4cf0d0df 92#if !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
93/* Not C99; do we need to define va_copy? */
e22f5516 94#ifndef va_copy
95#ifdef __va_copy
4cf0d0df 96#define va_copy(DST,SRC) __va_copy(DST,SRC)
e22f5516 97#else
98/* Now we are desperate; this should work on many typical platforms.
99 But this is slightly dangerous, because the standard does not require
100 va_copy to be a macro. */
101#define va_copy(DST,SRC) memcpy(&(DST), &(SRC), sizeof(va_list))
102#warning "Not C99 and no va_copy macro available, falling back to memcpy"
103#endif /* __va_copy */
104#endif /* !va_copy */
4cf0d0df 105#endif /* !C99 */
db8eaac6 106
718e3744 107
ba3a0bc5 108#ifdef HAVE_LCAPS
109#include <sys/capability.h>
110#include <sys/prctl.h>
111#endif /* HAVE_LCAPS */
112
ceacedba 113#ifdef HAVE_SOLARIS_CAPABILITIES
114#include <priv.h>
115#endif /* HAVE_SOLARIS_CAPABILITIES */
116
718e3744 117/* network include group */
118
119#include <sys/socket.h>
120
121#ifdef HAVE_SYS_SOCKIO_H
122#include <sys/sockio.h>
123#endif /* HAVE_SYS_SOCKIO_H */
124
ea057677
HT
125#ifdef __APPLE__
126#define __APPLE_USE_RFC_3542
127#endif
128
718e3744 129#include <netinet/in.h>
718e3744 130#include <netinet/in_systm.h>
131#include <netinet/ip.h>
132#include <netinet/tcp.h>
133
134#ifdef HAVE_NET_NETOPT_H
135#include <net/netopt.h>
136#endif /* HAVE_NET_NETOPT_H */
137
138#include <net/if.h>
139
140#ifdef HAVE_NET_IF_DL_H
141#include <net/if_dl.h>
142#endif /* HAVE_NET_IF_DL_H */
143
144#ifdef HAVE_NET_IF_VAR_H
145#include <net/if_var.h>
146#endif /* HAVE_NET_IF_VAR_H */
147
148#include <net/route.h>
149
150#ifdef HAVE_NETLINK
151#include <linux/netlink.h>
152#include <linux/rtnetlink.h>
768a27ea 153#include <linux/filter.h>
718e3744 154#else
155#define RT_TABLE_MAIN 0
156#endif /* HAVE_NETLINK */
157
718e3744 158#include <netdb.h>
718e3744 159#include <arpa/inet.h>
718e3744 160
161#ifdef HAVE_INET_ND_H
162#include <inet/nd.h>
163#endif /* HAVE_INET_ND_H */
164
165#ifdef HAVE_NETINET_IN_VAR_H
166#include <netinet/in_var.h>
167#endif /* HAVE_NETINET_IN_VAR_H */
168
726f9b2b 169#ifdef HAVE_NETINET6_IN6_VAR_H
170#include <netinet6/in6_var.h>
171#endif /* HAVE_NETINET6_IN6_VAR_H */
172
718e3744 173#ifdef HAVE_NETINET_IN6_VAR_H
174#include <netinet/in6_var.h>
175#endif /* HAVE_NETINET_IN6_VAR_H */
176
177#ifdef HAVE_NETINET6_IN_H
178#include <netinet6/in.h>
179#endif /* HAVE_NETINET6_IN_H */
180
181
182#ifdef HAVE_NETINET6_IP6_H
183#include <netinet6/ip6.h>
184#endif /* HAVE_NETINET6_IP6_H */
185
718e3744 186#include <netinet/icmp6.h>
718e3744 187
188#ifdef HAVE_NETINET6_ND6_H
189#include <netinet6/nd6.h>
190#endif /* HAVE_NETINET6_ND6_H */
191
35cfc90c
PJ
192/* Some systems do not define UINT32_MAX, etc.. from inttypes.h
193 * e.g. this makes life easier for FBSD 4.11 users.
194 */
195#ifndef INT8_MAX
196#define INT8_MAX (127)
197#endif
198#ifndef INT16_MAX
199#define INT16_MAX (32767)
200#endif
201#ifndef INT32_MAX
202#define INT32_MAX (2147483647)
203#endif
204#ifndef UINT8_MAX
205#define UINT8_MAX (255U)
206#endif
207#ifndef UINT16_MAX
208#define UINT16_MAX (65535U)
209#endif
3b424979 210#ifndef UINT32_MAX
35cfc90c
PJ
211#define UINT32_MAX (4294967295U)
212#endif
3b424979 213
fb2d1502 214#ifdef HAVE_GLIBC_BACKTRACE
215#include <execinfo.h>
216#endif /* HAVE_GLIBC_BACKTRACE */
217
db8eaac6 218/* Local includes: */
219#if !(defined(__GNUC__) || defined(VTYSH_EXTRACT_PL))
220#define __attribute__(x)
221#endif /* !__GNUC__ || VTYSH_EXTRACT_PL */
222
223#include "zassert.h"
3cb98dee 224#include "str.h"
db8eaac6 225
226
b99760ab 227#ifdef HAVE_BROKEN_CMSG_FIRSTHDR
228/* This bug is present in Solaris 8 and pre-patch Solaris 9 <sys/socket.h>;
229 please refer to http://bugzilla.quagga.net/show_bug.cgi?id=142 */
230
231/* Check that msg_controllen is large enough. */
232#define ZCMSG_FIRSTHDR(mhdr) \
233 (((size_t)((mhdr)->msg_controllen) >= sizeof(struct cmsghdr)) ? \
234 CMSG_FIRSTHDR(mhdr) : (struct cmsghdr *)NULL)
235
236#warning "CMSG_FIRSTHDR is broken on this platform, using a workaround"
237
238#else /* HAVE_BROKEN_CMSG_FIRSTHDR */
239#define ZCMSG_FIRSTHDR(M) CMSG_FIRSTHDR(M)
240#endif /* HAVE_BROKEN_CMSG_FIRSTHDR */
241
242
243
02ff83c5 244/*
69e1325f 245 * RFC 3542 defines several macros for using struct cmsghdr.
246 * Here, we define those that are not present
247 */
248
249/*
250 * Internal defines, for use only in this file.
251 * These are likely wrong on other than ILP32 machines, so warn.
02ff83c5 252 */
253#ifndef _CMSG_DATA_ALIGN
254#define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
255#endif /* _CMSG_DATA_ALIGN */
256
257#ifndef _CMSG_HDR_ALIGN
258#define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
259#endif /* _CMSG_HDR_ALIGN */
260
69e1325f 261/*
262 * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
263 * version.
264 */
02ff83c5 265#ifndef CMSG_SPACE
266#define CMSG_SPACE(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + \
267 _CMSG_HDR_ALIGN(l))
69e1325f 268#warning "assuming 4-byte alignment for CMSG_SPACE"
02ff83c5 269#endif /* CMSG_SPACE */
270
271
272#ifndef CMSG_LEN
273#define CMSG_LEN(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + (l))
69e1325f 274#warning "assuming 4-byte alignment for CMSG_LEN"
02ff83c5 275#endif /* CMSG_LEN */
276
69e1325f 277
718e3744 278/* The definition of struct in_pktinfo is missing in old version of
279 GLIBC 2.1 (Redhat 6.1). */
6f0e3f6e 280#if defined (GNU_LINUX) && ! defined (HAVE_STRUCT_IN_PKTINFO)
718e3744 281struct in_pktinfo
282{
283 int ipi_ifindex;
284 struct in_addr ipi_spec_dst;
285 struct in_addr ipi_addr;
286};
287#endif
288
9172ee0e 289/*
290 * IP_HDRINCL / struct ip byte order
291 *
292 * Linux: network byte order
293 * *BSD: network, except for length and offset. (cf Stevens)
294 * SunOS: nominally as per BSD. but bug: network order on LE.
295 * OpenBSD: network byte order, apart from older versions which are as per
296 * *BSD
297 */
618969ea
OCL
298#if defined(__NetBSD__) \
299 || (defined(__FreeBSD__) && (__FreeBSD_version < 1100030)) \
9172ee0e 300 || (defined(__OpenBSD__) && (OpenBSD < 200311)) \
3f0bfc9e 301 || (defined(__APPLE__)) \
9172ee0e 302 || (defined(SUNOS_5) && defined(WORDS_BIGENDIAN))
303#define HAVE_IP_HDRINCL_BSD_ORDER
304#endif
305
34204aac 306/* Define BYTE_ORDER, if not defined. Useful for compiler conditional
307 * code, rather than preprocessor conditional.
308 * Not all the world has this BSD define.
309 */
310#ifndef BYTE_ORDER
311#define BIG_ENDIAN 4321 /* least-significant byte first (vax, pc) */
312#define LITTLE_ENDIAN 1234 /* most-significant byte first (IBM, net) */
313#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
314
315#if defined(WORDS_BIGENDIAN)
316#define BYTE_ORDER BIG_ENDIAN
317#else /* !WORDS_BIGENDIAN */
318#define BYTE_ORDER LITTLE_ENDIAN
319#endif /* WORDS_BIGENDIAN */
320
321#endif /* ndef BYTE_ORDER */
322
efba6ce9 323/* MAX / MIN are not commonly defined, but useful */
6b143a68
DL
324/* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
325#ifdef MAX
326#undef MAX
327#endif
8a0bebfc
DL
328#define MAX(a, b) \
329 ({ typeof (a) _a = (a); \
330 typeof (b) _b = (b); \
331 _a > _b ? _a : _b; })
6b143a68
DL
332#ifdef MIN
333#undef MIN
8a0bebfc 334#endif
8a0bebfc
DL
335#define MIN(a, b) \
336 ({ typeof (a) _a = (a); \
337 typeof (b) _b = (b); \
338 _a < _b ? _a : _b; })
efba6ce9 339
0915bb0c
AS
340#define ZEBRA_NUM_OF(x) (sizeof (x) / sizeof (x[0]))
341
718e3744 342/* For old definition. */
343#ifndef IN6_ARE_ADDR_EQUAL
344#define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
345#endif /* IN6_ARE_ADDR_EQUAL */
346
8cc4198f 347/* default zebra TCP port for zclient */
348#define ZEBRA_PORT 2600
349
718e3744 350/* Zebra message types. */
b3d307ba
DS
351typedef enum {
352 ZEBRA_INTERFACE_ADD,
353 ZEBRA_INTERFACE_DELETE,
354 ZEBRA_INTERFACE_ADDRESS_ADD,
355 ZEBRA_INTERFACE_ADDRESS_DELETE,
356 ZEBRA_INTERFACE_UP,
357 ZEBRA_INTERFACE_DOWN,
358 ZEBRA_IPV4_ROUTE_ADD,
359 ZEBRA_IPV4_ROUTE_DELETE,
360 ZEBRA_IPV6_ROUTE_ADD,
361 ZEBRA_IPV6_ROUTE_DELETE,
362 ZEBRA_REDISTRIBUTE_ADD,
363 ZEBRA_REDISTRIBUTE_DELETE,
364 ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
365 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
b3d307ba
DS
366 ZEBRA_ROUTER_ID_ADD,
367 ZEBRA_ROUTER_ID_DELETE,
368 ZEBRA_ROUTER_ID_UPDATE,
369 ZEBRA_HELLO,
370 ZEBRA_NEXTHOP_REGISTER,
371 ZEBRA_NEXTHOP_UNREGISTER,
372 ZEBRA_NEXTHOP_UPDATE,
373 ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
374 ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
375 ZEBRA_INTERFACE_BFD_DEST_UPDATE,
376 ZEBRA_IMPORT_ROUTE_REGISTER,
377 ZEBRA_IMPORT_ROUTE_UNREGISTER,
378 ZEBRA_IMPORT_CHECK_UPDATE,
379 ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD,
380 ZEBRA_BFD_DEST_REGISTER,
381 ZEBRA_BFD_DEST_DEREGISTER,
382 ZEBRA_BFD_DEST_UPDATE,
383 ZEBRA_BFD_DEST_REPLAY,
384 ZEBRA_REDISTRIBUTE_IPV4_ADD,
385 ZEBRA_REDISTRIBUTE_IPV4_DEL,
386 ZEBRA_REDISTRIBUTE_IPV6_ADD,
387 ZEBRA_REDISTRIBUTE_IPV6_DEL,
388 ZEBRA_VRF_UNREGISTER,
389 ZEBRA_VRF_ADD,
390 ZEBRA_VRF_DELETE,
391 ZEBRA_INTERFACE_VRF_UPDATE,
392 ZEBRA_BFD_CLIENT_REGISTER,
393 ZEBRA_INTERFACE_ENABLE_RADV,
394 ZEBRA_INTERFACE_DISABLE_RADV,
395 ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB,
396 ZEBRA_INTERFACE_LINK_PARAMS,
ce549947
RW
397 ZEBRA_MPLS_LABELS_ADD,
398 ZEBRA_MPLS_LABELS_DELETE,
65efcfce
LB
399 ZEBRA_IPV4_NEXTHOP_ADD,
400 ZEBRA_IPV4_NEXTHOP_DELETE,
401 ZEBRA_IPV6_NEXTHOP_ADD,
402 ZEBRA_IPV6_NEXTHOP_DELETE,
b3d307ba 403} zebra_message_types_t;
718e3744 404
c1b9800a 405/* Marker value used in new Zserv, in the byte location corresponding
406 * the command value in the old zserv header. To allow old and new
407 * Zserv headers to be distinguished from each other.
408 */
409#define ZEBRA_HEADER_MARKER 255
410
e0ca5fde
DL
411/* Zebra route's types are defined in route_types.h */
412#include "route_types.h"
718e3744 413
d6d672aa
PJ
414/* Note: whenever a new route-type or zserv-command is added the
415 * corresponding {command,route}_types[] table in lib/log.c MUST be
416 * updated! */
f52d13cb 417
418/* Map a route type to a string. For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
b6026073 419extern const char *zebra_route_string(unsigned int route_type);
f52d13cb 420/* Map a route type to a char. For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
b6026073 421extern char zebra_route_char(unsigned int route_type);
d6d672aa
PJ
422/* Map a zserv command type to the same string,
423 * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
7514fb77
PJ
424/* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
425extern int proto_name2num(const char *s);
e0ca5fde
DL
426/* Map redistribute X argument to protocol number.
427 * unlike proto_name2num, this accepts shorthands and takes
428 * an AFI value to restrict input */
429extern int proto_redistnum(int afi, const char *s);
7514fb77 430
d6d672aa 431extern const char *zserv_command_string (unsigned int command);
f52d13cb 432
718e3744 433/* Error codes of zebra. */
dc95824a 434#define ZEBRA_ERR_NOERROR 0
718e3744 435#define ZEBRA_ERR_RTEXIST -1
436#define ZEBRA_ERR_RTUNREACH -2
437#define ZEBRA_ERR_EPERM -3
438#define ZEBRA_ERR_RTNOEXIST -4
dc95824a 439#define ZEBRA_ERR_KERNEL -5
718e3744 440
441/* Zebra message flags */
442#define ZEBRA_FLAG_INTERNAL 0x01
443#define ZEBRA_FLAG_SELFROUTE 0x02
444#define ZEBRA_FLAG_BLACKHOLE 0x04
445#define ZEBRA_FLAG_IBGP 0x08
446#define ZEBRA_FLAG_SELECTED 0x10
718e3744 447#define ZEBRA_FLAG_STATIC 0x40
81dfcaa2 448#define ZEBRA_FLAG_REJECT 0x80
525c1839 449#define ZEBRA_FLAG_SCOPE_LINK 0x100
446bb95e 450#define ZEBRA_FLAG_FIB_OVERRIDE 0x200
718e3744 451
718e3744 452#ifndef INADDR_LOOPBACK
453#define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
454#endif
455
456/* Address family numbers from RFC1700. */
e978e1de
DS
457typedef enum {
458 AFI_IP = 1,
459 AFI_IP6 = 2,
32ac65d9
LB
460 AFI_ETHER = 3, /* RFC 1700 has "6" for 802.* */
461 AFI_MAX = 4
e978e1de 462} afi_t;
718e3744 463
464/* Subsequent Address Family Identifier. */
465#define SAFI_UNICAST 1
466#define SAFI_MULTICAST 2
0a28130d 467#define SAFI_RESERVED_3 3
718e3744 468#define SAFI_MPLS_VPN 4
1ec23d90
LB
469#define SAFI_ENCAP 7 /* per IANA */
470#define SAFI_MAX 8
718e3744 471
718e3744 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
9e867fe6 480#define ZEBRA_ISIS_DISTANCE_DEFAULT 115
718e3744 481#define ZEBRA_IBGP_DISTANCE_DEFAULT 200
482#define ZEBRA_EBGP_DISTANCE_DEFAULT 20
7ab5795a 483#define ZEBRA_TABLE_DISTANCE_DEFAULT 15
718e3744 484
485/* Flag manipulation macros. */
486#define CHECK_FLAG(V,F) ((V) & (F))
548e6f7d 487#define SET_FLAG(V,F) (V) |= (F)
488#define UNSET_FLAG(V,F) (V) &= ~(F)
fb018d25 489#define RESET_FLAG(V) (V) = 0
718e3744 490
5228ad27 491typedef u_int8_t safi_t;
718e3744 492
c1b9800a 493/* Zebra types. Used in Zserv message header. */
718e3744 494typedef u_int16_t zebra_size_t;
c1b9800a 495typedef u_int16_t zebra_command_t;
718e3744 496
b72ede27
FL
497/* VRF ID type. */
498typedef u_int16_t vrf_id_t;
499
dc9ffce8
CF
500typedef uint32_t route_tag_t;
501#define ROUTE_TAG_MAX UINT32_MAX
502#define ROUTE_TAG_PRI PRIu32
503
718e3744 504#endif /* _ZEBRA_H */