bgpd: tighten bounds checking in RR ORF msg reader
[jleu-quagga.git] / lib / zebra.h
blob2dc84514295046e8fdf7b3558b5b11a7e5eb5a13
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
9 later version.
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
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #ifndef _ZEBRA_H
22 #define _ZEBRA_H
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif /* HAVE_CONFIG_H */
28 #ifdef SUNOS_5
29 #define _XPG4_2
30 #define __EXTENSIONS__
31 typedef unsigned int u_int32_t;
32 typedef unsigned short u_int16_t;
33 typedef unsigned char u_int8_t;
34 #endif /* SUNOS_5 */
36 #ifndef HAVE_SOCKLEN_T
37 typedef int socklen_t;
38 #endif /* HAVE_SOCKLEN_T */
40 #include <unistd.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <string.h>
48 #include <pwd.h>
49 #include <grp.h>
50 #ifdef HAVE_STROPTS_H
51 #include <stropts.h>
52 #endif /* HAVE_STROPTS_H */
53 #include <sys/fcntl.h>
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
56 #endif /* HAVE_SYS_SELECT_H */
57 #include <sys/stat.h>
58 #include <sys/types.h>
59 #include <sys/param.h>
60 #ifdef HAVE_SYS_SYSCTL_H
61 #ifdef GNU_LINUX
62 #include <linux/types.h>
63 #endif
64 #include <sys/sysctl.h>
65 #endif /* HAVE_SYS_SYSCTL_H */
66 #include <sys/ioctl.h>
67 #ifdef HAVE_SYS_CONF_H
68 #include <sys/conf.h>
69 #endif /* HAVE_SYS_CONF_H */
70 #ifdef HAVE_SYS_KSYM_H
71 #include <sys/ksym.h>
72 #endif /* HAVE_SYS_KSYM_H */
73 #include <syslog.h>
74 #ifdef TIME_WITH_SYS_TIME
75 # include <sys/time.h>
76 # include <time.h>
77 #else
78 # ifdef HAVE_SYS_TIME_H
79 # include <sys/time.h>
80 # else
81 # include <time.h>
82 # endif
83 #endif /* TIME_WITH_SYS_TIME */
84 #include <sys/uio.h>
85 #include <sys/utsname.h>
86 #ifdef HAVE_RUSAGE
87 #include <sys/resource.h>
88 #endif /* HAVE_RUSAGE */
89 #ifdef HAVE_LIMITS_H
90 #include <limits.h>
91 #endif /* HAVE_LIMITS_H */
92 #ifdef HAVE_INTTYPES_H
93 #include <inttypes.h>
94 #endif /* HAVE_INTTYPES_H */
96 /* machine dependent includes */
97 #ifdef SUNOS_5
98 #include <strings.h>
99 #endif /* SUNOS_5 */
101 /* machine dependent includes */
102 #ifdef HAVE_LINUX_VERSION_H
103 #include <linux/version.h>
104 #endif /* HAVE_LINUX_VERSION_H */
106 #ifdef HAVE_ASM_TYPES_H
107 #include <asm/types.h>
108 #endif /* HAVE_ASM_TYPES_H */
110 /* misc include group */
111 #include <stdarg.h>
112 #if !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
113 /* Not C99; do we need to define va_copy? */
114 #ifndef va_copy
115 #ifdef __va_copy
116 #define va_copy(DST,SRC) __va_copy(DST,SRC)
117 #else
118 /* Now we are desperate; this should work on many typical platforms.
119 But this is slightly dangerous, because the standard does not require
120 va_copy to be a macro. */
121 #define va_copy(DST,SRC) memcpy(&(DST), &(SRC), sizeof(va_list))
122 #warning "Not C99 and no va_copy macro available, falling back to memcpy"
123 #endif /* __va_copy */
124 #endif /* !va_copy */
125 #endif /* !C99 */
128 #ifdef HAVE_LCAPS
129 #include <sys/capability.h>
130 #include <sys/prctl.h>
131 #endif /* HAVE_LCAPS */
133 #ifdef HAVE_SOLARIS_CAPABILITIES
134 #include <priv.h>
135 #endif /* HAVE_SOLARIS_CAPABILITIES */
137 /* network include group */
139 #include <sys/socket.h>
141 #ifdef HAVE_SYS_SOCKIO_H
142 #include <sys/sockio.h>
143 #endif /* HAVE_SYS_SOCKIO_H */
145 #ifdef HAVE_NETINET_IN_H
146 #include <netinet/in.h>
147 #endif /* HAVE_NETINET_IN_H */
148 #include <netinet/in_systm.h>
149 #include <netinet/ip.h>
150 #include <netinet/tcp.h>
152 #ifdef HAVE_NET_NETOPT_H
153 #include <net/netopt.h>
154 #endif /* HAVE_NET_NETOPT_H */
156 #include <net/if.h>
158 #ifdef HAVE_NET_IF_DL_H
159 #include <net/if_dl.h>
160 #endif /* HAVE_NET_IF_DL_H */
162 #ifdef HAVE_NET_IF_VAR_H
163 #include <net/if_var.h>
164 #endif /* HAVE_NET_IF_VAR_H */
166 #ifdef HAVE_NET_ROUTE_H
167 #include <net/route.h>
168 #endif /* HAVE_NET_ROUTE_H */
170 #ifdef HAVE_NETLINK
171 #include <linux/netlink.h>
172 #include <linux/rtnetlink.h>
173 #include <linux/filter.h>
174 #include <stddef.h>
175 #else
176 #define RT_TABLE_MAIN 0
177 #endif /* HAVE_NETLINK */
179 #ifdef HAVE_NETDB_H
180 #include <netdb.h>
181 #endif /* HAVE_NETDB_H */
183 #include <arpa/inet.h>
185 #ifdef HAVE_INET_ND_H
186 #include <inet/nd.h>
187 #endif /* HAVE_INET_ND_H */
189 #ifdef HAVE_NETINET_IN_VAR_H
190 #include <netinet/in_var.h>
191 #endif /* HAVE_NETINET_IN_VAR_H */
193 #ifdef HAVE_NETINET6_IN6_VAR_H
194 #include <netinet6/in6_var.h>
195 #endif /* HAVE_NETINET6_IN6_VAR_H */
197 #ifdef HAVE_NETINET_IN6_VAR_H
198 #include <netinet/in6_var.h>
199 #endif /* HAVE_NETINET_IN6_VAR_H */
201 #ifdef HAVE_NETINET6_IN_H
202 #include <netinet6/in.h>
203 #endif /* HAVE_NETINET6_IN_H */
206 #ifdef HAVE_NETINET6_IP6_H
207 #include <netinet6/ip6.h>
208 #endif /* HAVE_NETINET6_IP6_H */
210 #ifdef HAVE_NETINET_ICMP6_H
211 #include <netinet/icmp6.h>
212 #endif /* HAVE_NETINET_ICMP6_H */
214 #ifdef HAVE_NETINET6_ND6_H
215 #include <netinet6/nd6.h>
216 #endif /* HAVE_NETINET6_ND6_H */
218 /* Some systems do not define UINT32_MAX, etc.. from inttypes.h
219 * e.g. this makes life easier for FBSD 4.11 users.
221 #ifndef INT8_MAX
222 #define INT8_MAX (127)
223 #endif
224 #ifndef INT16_MAX
225 #define INT16_MAX (32767)
226 #endif
227 #ifndef INT32_MAX
228 #define INT32_MAX (2147483647)
229 #endif
230 #ifndef UINT8_MAX
231 #define UINT8_MAX (255U)
232 #endif
233 #ifndef UINT16_MAX
234 #define UINT16_MAX (65535U)
235 #endif
236 #ifndef UINT32_MAX
237 #define UINT32_MAX (4294967295U)
238 #endif
240 #ifdef HAVE_LIBUTIL_H
241 #include <libutil.h>
242 #endif /* HAVE_LIBUTIL_H */
244 #ifdef HAVE_GLIBC_BACKTRACE
245 #include <execinfo.h>
246 #endif /* HAVE_GLIBC_BACKTRACE */
248 #ifdef BSDI_NRL
250 #ifdef HAVE_NETINET6_IN6_H
251 #include <netinet6/in6.h>
252 #endif /* HAVE_NETINET6_IN6_H */
254 #ifdef NRL
255 #include <netinet6/in6.h>
256 #endif /* NRL */
258 #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
260 #endif /* BSDI_NRL */
262 /* Local includes: */
263 #if !(defined(__GNUC__) || defined(VTYSH_EXTRACT_PL))
264 #define __attribute__(x)
265 #endif /* !__GNUC__ || VTYSH_EXTRACT_PL */
267 #include "zassert.h"
268 #include "str.h"
271 #ifdef HAVE_BROKEN_CMSG_FIRSTHDR
272 /* This bug is present in Solaris 8 and pre-patch Solaris 9 <sys/socket.h>;
273 please refer to http://bugzilla.quagga.net/show_bug.cgi?id=142 */
275 /* Check that msg_controllen is large enough. */
276 #define ZCMSG_FIRSTHDR(mhdr) \
277 (((size_t)((mhdr)->msg_controllen) >= sizeof(struct cmsghdr)) ? \
278 CMSG_FIRSTHDR(mhdr) : (struct cmsghdr *)NULL)
280 #warning "CMSG_FIRSTHDR is broken on this platform, using a workaround"
282 #else /* HAVE_BROKEN_CMSG_FIRSTHDR */
283 #define ZCMSG_FIRSTHDR(M) CMSG_FIRSTHDR(M)
284 #endif /* HAVE_BROKEN_CMSG_FIRSTHDR */
289 * RFC 3542 defines several macros for using struct cmsghdr.
290 * Here, we define those that are not present
294 * Internal defines, for use only in this file.
295 * These are likely wrong on other than ILP32 machines, so warn.
297 #ifndef _CMSG_DATA_ALIGN
298 #define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
299 #endif /* _CMSG_DATA_ALIGN */
301 #ifndef _CMSG_HDR_ALIGN
302 #define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
303 #endif /* _CMSG_HDR_ALIGN */
306 * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
307 * version.
309 #ifndef CMSG_SPACE
310 #define CMSG_SPACE(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + \
311 _CMSG_HDR_ALIGN(l))
312 #warning "assuming 4-byte alignment for CMSG_SPACE"
313 #endif /* CMSG_SPACE */
316 #ifndef CMSG_LEN
317 #define CMSG_LEN(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + (l))
318 #warning "assuming 4-byte alignment for CMSG_LEN"
319 #endif /* CMSG_LEN */
322 /* The definition of struct in_pktinfo is missing in old version of
323 GLIBC 2.1 (Redhat 6.1). */
324 #if defined (GNU_LINUX) && ! defined (HAVE_STRUCT_IN_PKTINFO)
325 struct in_pktinfo
327 int ipi_ifindex;
328 struct in_addr ipi_spec_dst;
329 struct in_addr ipi_addr;
331 #endif
334 * OSPF Fragmentation / fragmented writes
336 * ospfd can support writing fragmented packets, for cases where
337 * kernel will not fragment IP_HDRINCL and/or multicast destined
338 * packets (ie TTBOMK all kernels, BSD, SunOS, Linux). However,
339 * SunOS, probably BSD too, clobber the user supplied IP ID and IP
340 * flags fields, hence user-space fragmentation will not work.
341 * Only Linux is known to leave IP header unmolested.
342 * Further, fragmentation really should be done the kernel, which already
343 * supports it, and which avoids nasty IP ID state problems.
345 * Fragmentation of OSPF packets can be required on networks with router
346 * with many many interfaces active in one area, or on networks with links
347 * with low MTUs.
349 #ifdef GNU_LINUX
350 #define WANT_OSPF_WRITE_FRAGMENT
351 #endif
354 * IP_HDRINCL / struct ip byte order
356 * Linux: network byte order
357 * *BSD: network, except for length and offset. (cf Stevens)
358 * SunOS: nominally as per BSD. but bug: network order on LE.
359 * OpenBSD: network byte order, apart from older versions which are as per
360 * *BSD
362 #if defined(__NetBSD__) || defined(__FreeBSD__) \
363 || (defined(__OpenBSD__) && (OpenBSD < 200311)) \
364 || (defined(SUNOS_5) && defined(WORDS_BIGENDIAN))
365 #define HAVE_IP_HDRINCL_BSD_ORDER
366 #endif
368 /* Define BYTE_ORDER, if not defined. Useful for compiler conditional
369 * code, rather than preprocessor conditional.
370 * Not all the world has this BSD define.
372 #ifndef BYTE_ORDER
373 #define BIG_ENDIAN 4321 /* least-significant byte first (vax, pc) */
374 #define LITTLE_ENDIAN 1234 /* most-significant byte first (IBM, net) */
375 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
377 #if defined(WORDS_BIGENDIAN)
378 #define BYTE_ORDER BIG_ENDIAN
379 #else /* !WORDS_BIGENDIAN */
380 #define BYTE_ORDER LITTLE_ENDIAN
381 #endif /* WORDS_BIGENDIAN */
383 #endif /* ndef BYTE_ORDER */
385 /* MAX / MIN are not commonly defined, but useful */
386 #ifndef MAX
387 #define MAX(a, b) ((a) > (b) ? (a) : (b))
388 #endif
389 #ifndef MIN
390 #define MIN(a, b) ((a) < (b) ? (a) : (b))
391 #endif
393 /* For old definition. */
394 #ifndef IN6_ARE_ADDR_EQUAL
395 #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
396 #endif /* IN6_ARE_ADDR_EQUAL */
398 /* default zebra TCP port for zclient */
399 #define ZEBRA_PORT 2600
401 /* Zebra message types. */
402 #define ZEBRA_INTERFACE_ADD 1
403 #define ZEBRA_INTERFACE_DELETE 2
404 #define ZEBRA_INTERFACE_ADDRESS_ADD 3
405 #define ZEBRA_INTERFACE_ADDRESS_DELETE 4
406 #define ZEBRA_INTERFACE_UP 5
407 #define ZEBRA_INTERFACE_DOWN 6
408 #define ZEBRA_IPV4_ROUTE_ADD 7
409 #define ZEBRA_IPV4_ROUTE_DELETE 8
410 #define ZEBRA_IPV6_ROUTE_ADD 9
411 #define ZEBRA_IPV6_ROUTE_DELETE 10
412 #define ZEBRA_REDISTRIBUTE_ADD 11
413 #define ZEBRA_REDISTRIBUTE_DELETE 12
414 #define ZEBRA_REDISTRIBUTE_DEFAULT_ADD 13
415 #define ZEBRA_REDISTRIBUTE_DEFAULT_DELETE 14
416 #define ZEBRA_IPV4_NEXTHOP_LOOKUP 15
417 #define ZEBRA_IPV6_NEXTHOP_LOOKUP 16
418 #define ZEBRA_IPV4_IMPORT_LOOKUP 17
419 #define ZEBRA_IPV6_IMPORT_LOOKUP 18
420 #define ZEBRA_INTERFACE_RENAME 19
421 #define ZEBRA_ROUTER_ID_ADD 20
422 #define ZEBRA_ROUTER_ID_DELETE 21
423 #define ZEBRA_ROUTER_ID_UPDATE 22
424 #define ZEBRA_MESSAGE_MAX 23
426 /* Marker value used in new Zserv, in the byte location corresponding
427 * the command value in the old zserv header. To allow old and new
428 * Zserv headers to be distinguished from each other.
430 #define ZEBRA_HEADER_MARKER 255
432 /* Zebra route's types. */
433 #define ZEBRA_ROUTE_SYSTEM 0
434 #define ZEBRA_ROUTE_KERNEL 1
435 #define ZEBRA_ROUTE_CONNECT 2
436 #define ZEBRA_ROUTE_STATIC 3
437 #define ZEBRA_ROUTE_RIP 4
438 #define ZEBRA_ROUTE_RIPNG 5
439 #define ZEBRA_ROUTE_OSPF 6
440 #define ZEBRA_ROUTE_OSPF6 7
441 #define ZEBRA_ROUTE_ISIS 8
442 #define ZEBRA_ROUTE_BGP 9
443 #define ZEBRA_ROUTE_HSLS 10
444 #define ZEBRA_ROUTE_MAX 11
446 /* Note: whenever a new route-type or zserv-command is added the
447 * corresponding {command,route}_types[] table in lib/log.c MUST be
448 * updated! */
450 /* Map a route type to a string. For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
451 extern const char *zebra_route_string(unsigned int route_type);
452 /* Map a route type to a char. For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
453 extern char zebra_route_char(unsigned int route_type);
454 /* Map a zserv command type to the same string,
455 * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
456 /* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
457 extern int proto_name2num(const char *s);
459 extern const char *zserv_command_string (unsigned int command);
461 /* Zebra's family types. */
462 #define ZEBRA_FAMILY_IPV4 1
463 #define ZEBRA_FAMILY_IPV6 2
464 #define ZEBRA_FAMILY_MAX 3
466 /* Error codes of zebra. */
467 #define ZEBRA_ERR_NOERROR 0
468 #define ZEBRA_ERR_RTEXIST -1
469 #define ZEBRA_ERR_RTUNREACH -2
470 #define ZEBRA_ERR_EPERM -3
471 #define ZEBRA_ERR_RTNOEXIST -4
472 #define ZEBRA_ERR_KERNEL -5
474 /* Zebra message flags */
475 #define ZEBRA_FLAG_INTERNAL 0x01
476 #define ZEBRA_FLAG_SELFROUTE 0x02
477 #define ZEBRA_FLAG_BLACKHOLE 0x04
478 #define ZEBRA_FLAG_IBGP 0x08
479 #define ZEBRA_FLAG_SELECTED 0x10
480 #define ZEBRA_FLAG_CHANGED 0x20
481 #define ZEBRA_FLAG_STATIC 0x40
482 #define ZEBRA_FLAG_REJECT 0x80
484 /* Zebra nexthop flags. */
485 #define ZEBRA_NEXTHOP_IFINDEX 1
486 #define ZEBRA_NEXTHOP_IFNAME 2
487 #define ZEBRA_NEXTHOP_IPV4 3
488 #define ZEBRA_NEXTHOP_IPV4_IFINDEX 4
489 #define ZEBRA_NEXTHOP_IPV4_IFNAME 5
490 #define ZEBRA_NEXTHOP_IPV6 6
491 #define ZEBRA_NEXTHOP_IPV6_IFINDEX 7
492 #define ZEBRA_NEXTHOP_IPV6_IFNAME 8
493 #define ZEBRA_NEXTHOP_BLACKHOLE 9
495 #ifndef INADDR_LOOPBACK
496 #define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
497 #endif
499 /* Address family numbers from RFC1700. */
500 #define AFI_IP 1
501 #define AFI_IP6 2
502 #define AFI_MAX 3
504 /* Subsequent Address Family Identifier. */
505 #define SAFI_UNICAST 1
506 #define SAFI_MULTICAST 2
507 #define SAFI_UNICAST_MULTICAST 3
508 #define SAFI_MPLS_VPN 4
509 #define SAFI_MAX 5
511 /* Filter direction. */
512 #define FILTER_IN 0
513 #define FILTER_OUT 1
514 #define FILTER_MAX 2
516 /* Default Administrative Distance of each protocol. */
517 #define ZEBRA_KERNEL_DISTANCE_DEFAULT 0
518 #define ZEBRA_CONNECT_DISTANCE_DEFAULT 0
519 #define ZEBRA_STATIC_DISTANCE_DEFAULT 1
520 #define ZEBRA_RIP_DISTANCE_DEFAULT 120
521 #define ZEBRA_RIPNG_DISTANCE_DEFAULT 120
522 #define ZEBRA_OSPF_DISTANCE_DEFAULT 110
523 #define ZEBRA_OSPF6_DISTANCE_DEFAULT 110
524 #define ZEBRA_ISIS_DISTANCE_DEFAULT 115
525 #define ZEBRA_IBGP_DISTANCE_DEFAULT 200
526 #define ZEBRA_EBGP_DISTANCE_DEFAULT 20
528 /* Flag manipulation macros. */
529 #define CHECK_FLAG(V,F) ((V) & (F))
530 #define SET_FLAG(V,F) (V) |= (F)
531 #define UNSET_FLAG(V,F) (V) &= ~(F)
533 /* AFI and SAFI type. */
534 typedef u_int16_t afi_t;
535 typedef u_int8_t safi_t;
537 /* Zebra types. Used in Zserv message header. */
538 typedef u_int16_t zebra_size_t;
539 typedef u_int16_t zebra_command_t;
541 /* FIFO -- first in first out structure and macros. */
542 struct fifo
544 struct fifo *next;
545 struct fifo *prev;
548 #define FIFO_INIT(F) \
549 do { \
550 struct fifo *Xfifo = (struct fifo *)(F); \
551 Xfifo->next = Xfifo->prev = Xfifo; \
552 } while (0)
554 #define FIFO_ADD(F,N) \
555 do { \
556 struct fifo *Xfifo = (struct fifo *)(F); \
557 struct fifo *Xnode = (struct fifo *)(N); \
558 Xnode->next = Xfifo; \
559 Xnode->prev = Xfifo->prev; \
560 Xfifo->prev = Xfifo->prev->next = Xnode; \
561 } while (0)
563 #define FIFO_DEL(N) \
564 do { \
565 struct fifo *Xnode = (struct fifo *)(N); \
566 Xnode->prev->next = Xnode->next; \
567 Xnode->next->prev = Xnode->prev; \
568 } while (0)
570 #define FIFO_HEAD(F) \
571 ((((struct fifo *)(F))->next == (struct fifo *)(F)) \
572 ? NULL : (F)->next)
574 #define FIFO_EMPTY(F) \
575 (((struct fifo *)(F))->next == (struct fifo *)(F))
577 #define FIFO_TOP(F) \
578 (FIFO_EMPTY(F) ? NULL : ((struct fifo *)(F))->next)
580 #endif /* _ZEBRA_H */