]> git.proxmox.com Git - mirror_frr.git/blob - lib/zebra.h
Merge pull request #2764 from opensourcerouting/isis-srcdest
[mirror_frr.git] / lib / zebra.h
1 /* Zebra common header.
2 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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.
10 *
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.
15 *
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
19 */
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 #include "compiler.h"
29
30 #ifdef SUNOS_5
31 #define _XPG4_2
32 typedef unsigned int uint32_t;
33 typedef unsigned short uint16_t;
34 typedef unsigned char uint8_t;
35 #endif /* SUNOS_5 */
36
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stddef.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <signal.h>
45 #include <string.h>
46 #include <pwd.h>
47 #include <grp.h>
48 #ifdef HAVE_STROPTS_H
49 #include <stropts.h>
50 #endif /* HAVE_STROPTS_H */
51 #include <sys/select.h>
52 #include <sys/stat.h>
53 #include <sys/types.h>
54 #include <sys/param.h>
55 #ifdef HAVE_SYS_SYSCTL_H
56 #ifdef GNU_LINUX
57 #include <linux/types.h>
58 #endif
59 #include <sys/sysctl.h>
60 #endif /* HAVE_SYS_SYSCTL_H */
61 #include <sys/ioctl.h>
62 #ifdef HAVE_SYS_CONF_H
63 #include <sys/conf.h>
64 #endif /* HAVE_SYS_CONF_H */
65 #ifdef HAVE_SYS_KSYM_H
66 #include <sys/ksym.h>
67 #endif /* HAVE_SYS_KSYM_H */
68 #include <syslog.h>
69 #include <sys/time.h>
70 #include <time.h>
71 #include <sys/uio.h>
72 #include <sys/utsname.h>
73 #include <sys/resource.h>
74 #include <limits.h>
75 #include <inttypes.h>
76 #include <stdbool.h>
77
78 /* machine dependent includes */
79 #ifdef SUNOS_5
80 #include <strings.h>
81 #endif /* SUNOS_5 */
82
83 /* machine dependent includes */
84 #ifdef HAVE_LINUX_VERSION_H
85 #include <linux/version.h>
86 #endif /* HAVE_LINUX_VERSION_H */
87
88 #ifdef HAVE_ASM_TYPES_H
89 #include <asm/types.h>
90 #endif /* HAVE_ASM_TYPES_H */
91
92 /* misc include group */
93 #include <stdarg.h>
94 #if !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
95 /* Not C99; do we need to define va_copy? */
96 #ifndef va_copy
97 #ifdef __va_copy
98 #define va_copy(DST,SRC) __va_copy(DST,SRC)
99 #else
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 */
107 #endif /* !C99 */
108
109
110 #ifdef HAVE_LCAPS
111 #include <sys/capability.h>
112 #include <sys/prctl.h>
113 #endif /* HAVE_LCAPS */
114
115 #ifdef HAVE_SOLARIS_CAPABILITIES
116 #include <priv.h>
117 #endif /* HAVE_SOLARIS_CAPABILITIES */
118
119 /* network include group */
120
121 #include <sys/socket.h>
122
123 #ifdef HAVE_SYS_SOCKIO_H
124 #include <sys/sockio.h>
125 #endif /* HAVE_SYS_SOCKIO_H */
126
127 #ifdef __APPLE__
128 #define __APPLE_USE_RFC_3542
129 #endif
130
131 #ifndef HAVE_LIBCRYPT
132 #ifdef HAVE_LIBCRYPTO
133 #include <openssl/des.h>
134 # define crypt DES_crypt
135 #endif
136 #endif
137
138 #include "openbsd-tree.h"
139
140 #include <netinet/in.h>
141 #include <netinet/in_systm.h>
142 #include <netinet/ip.h>
143 #include <netinet/tcp.h>
144
145 #ifdef HAVE_NET_NETOPT_H
146 #include <net/netopt.h>
147 #endif /* HAVE_NET_NETOPT_H */
148
149 #include <net/if.h>
150
151 #ifdef HAVE_NET_IF_DL_H
152 #include <net/if_dl.h>
153 #endif /* HAVE_NET_IF_DL_H */
154
155 #ifdef HAVE_NET_IF_VAR_H
156 #include <net/if_var.h>
157 #endif /* HAVE_NET_IF_VAR_H */
158
159 #include <net/route.h>
160
161 #ifdef HAVE_NETLINK
162 #include <linux/netlink.h>
163 #include <linux/rtnetlink.h>
164 #include <linux/filter.h>
165 #else
166 #define RT_TABLE_MAIN 0
167 #endif /* HAVE_NETLINK */
168
169 #include <netdb.h>
170 #include <arpa/inet.h>
171
172 #ifdef HAVE_INET_ND_H
173 #include <inet/nd.h>
174 #endif /* HAVE_INET_ND_H */
175
176 #ifdef HAVE_NETINET_IN_VAR_H
177 #include <netinet/in_var.h>
178 #endif /* HAVE_NETINET_IN_VAR_H */
179
180 #ifdef HAVE_NETINET6_IN6_VAR_H
181 #include <netinet6/in6_var.h>
182 #endif /* HAVE_NETINET6_IN6_VAR_H */
183
184 #ifdef HAVE_NETINET_IN6_VAR_H
185 #include <netinet/in6_var.h>
186 #endif /* HAVE_NETINET_IN6_VAR_H */
187
188 #ifdef HAVE_NETINET6_IN_H
189 #include <netinet6/in.h>
190 #endif /* HAVE_NETINET6_IN_H */
191
192
193 #ifdef HAVE_NETINET6_IP6_H
194 #include <netinet6/ip6.h>
195 #endif /* HAVE_NETINET6_IP6_H */
196
197 #include <netinet/icmp6.h>
198
199 #ifdef HAVE_NETINET6_ND6_H
200 #include <netinet6/nd6.h>
201 #endif /* HAVE_NETINET6_ND6_H */
202
203 /* Some systems do not define UINT32_MAX, etc.. from inttypes.h
204 * e.g. this makes life easier for FBSD 4.11 users.
205 */
206 #ifndef INT8_MAX
207 #define INT8_MAX (127)
208 #endif
209 #ifndef INT16_MAX
210 #define INT16_MAX (32767)
211 #endif
212 #ifndef INT32_MAX
213 #define INT32_MAX (2147483647)
214 #endif
215 #ifndef UINT8_MAX
216 #define UINT8_MAX (255U)
217 #endif
218 #ifndef UINT16_MAX
219 #define UINT16_MAX (65535U)
220 #endif
221 #ifndef UINT32_MAX
222 #define UINT32_MAX (4294967295U)
223 #endif
224
225 #ifdef HAVE_GLIBC_BACKTRACE
226 #include <execinfo.h>
227 #endif /* HAVE_GLIBC_BACKTRACE */
228
229 /* Local includes: */
230 #if !(defined(__GNUC__) || defined(VTYSH_EXTRACT_PL))
231 #define __attribute__(x)
232 #endif /* !__GNUC__ || VTYSH_EXTRACT_PL */
233
234 #include "zassert.h"
235
236 #ifndef HAVE_STRLCAT
237 size_t strlcat(char *__restrict dest,
238 const char *__restrict src, size_t destsize);
239 #endif
240 #ifndef HAVE_STRLCPY
241 size_t strlcpy(char *__restrict dest,
242 const char *__restrict src, size_t destsize);
243 #endif
244
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 */
248
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)
254
255 #warning "CMSG_FIRSTHDR is broken on this platform, using a workaround"
256
257 #else /* HAVE_BROKEN_CMSG_FIRSTHDR */
258 #define ZCMSG_FIRSTHDR(M) CMSG_FIRSTHDR(M)
259 #endif /* HAVE_BROKEN_CMSG_FIRSTHDR */
260
261
262 /* GCC have printf type attribute check. */
263 #ifdef __GNUC__
264 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
265 #else
266 #define PRINTF_ATTRIBUTE(a,b)
267 #endif /* __GNUC__ */
268
269 /*
270 * RFC 3542 defines several macros for using struct cmsghdr.
271 * Here, we define those that are not present
272 */
273
274 /*
275 * Internal defines, for use only in this file.
276 * These are likely wrong on other than ILP32 machines, so warn.
277 */
278 #ifndef _CMSG_DATA_ALIGN
279 #define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
280 #endif /* _CMSG_DATA_ALIGN */
281
282 #ifndef _CMSG_HDR_ALIGN
283 #define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
284 #endif /* _CMSG_HDR_ALIGN */
285
286 /*
287 * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
288 * version.
289 */
290 #ifndef CMSG_SPACE
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 */
295
296
297 #ifndef CMSG_LEN
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 */
301
302
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)
306 struct in_pktinfo {
307 int ipi_ifindex;
308 struct in_addr ipi_spec_dst;
309 struct in_addr ipi_addr;
310 };
311 #endif
312
313 /*
314 * IP_HDRINCL / struct ip byte order
315 *
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
320 * *BSD
321 */
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
328 #endif
329
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.
333 */
334 #ifndef BYTE_ORDER
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) */
338
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 */
344
345 #endif /* ndef BYTE_ORDER */
346
347 /* MAX / MIN are not commonly defined, but useful */
348 /* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
349 #ifdef MAX
350 #undef MAX
351 #endif
352 #define MAX(a, b) \
353 ({ \
354 typeof(a) _a = (a); \
355 typeof(b) _b = (b); \
356 _a > _b ? _a : _b; \
357 })
358 #ifdef MIN
359 #undef MIN
360 #endif
361 #define MIN(a, b) \
362 ({ \
363 typeof(a) _a = (a); \
364 typeof(b) _b = (b); \
365 _a < _b ? _a : _b; \
366 })
367
368 #ifndef offsetof
369 #ifdef __compiler_offsetof
370 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
371 #else
372 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
373 #endif
374 #endif
375
376 #ifndef container_of
377 #define container_of(ptr, type, member) \
378 ({ \
379 const typeof(((type *)0)->member) *__mptr = (ptr); \
380 (type *)((char *)__mptr - offsetof(type, member)); \
381 })
382 #endif
383
384 #define ZEBRA_NUM_OF(x) (sizeof (x) / sizeof (x[0]))
385
386 /* For old definition. */
387 #ifndef IN6_ARE_ADDR_EQUAL
388 #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
389 #endif /* IN6_ARE_ADDR_EQUAL */
390
391 /* default zebra TCP port for zclient */
392 #define ZEBRA_PORT 2600
393
394 /* Marker value used in new Zserv, in the byte location corresponding
395 * the command value in the old zserv header. To allow old and new
396 * Zserv headers to be distinguished from each other.
397 */
398 #define ZEBRA_HEADER_MARKER 254
399
400 /* Zebra route's types are defined in route_types.h */
401 #include "route_types.h"
402
403 /* Note: whenever a new route-type or zserv-command is added the
404 * corresponding {command,route}_types[] table in lib/log.c MUST be
405 * updated! */
406
407 /* Map a route type to a string. For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
408 extern const char *zebra_route_string(unsigned int route_type);
409 /* Map a route type to a char. For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
410 extern char zebra_route_char(unsigned int route_type);
411 /* Map a zserv command type to the same string,
412 * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
413 /* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
414 extern int proto_name2num(const char *s);
415 /* Map redistribute X argument to protocol number.
416 * unlike proto_name2num, this accepts shorthands and takes
417 * an AFI value to restrict input */
418 extern int proto_redistnum(int afi, const char *s);
419
420 extern const char *zserv_command_string(unsigned int command);
421
422 #define strmatch(a,b) (!strcmp((a), (b)))
423
424 /* Zebra message flags */
425 #define ZEBRA_FLAG_ALLOW_RECURSION 0x01
426 #define ZEBRA_FLAG_SELFROUTE 0x02
427 #define ZEBRA_FLAG_IBGP 0x08
428 #define ZEBRA_FLAG_SELECTED 0x10
429 #define ZEBRA_FLAG_STATIC 0x40
430 #define ZEBRA_FLAG_SCOPE_LINK 0x100
431 #define ZEBRA_FLAG_FIB_OVERRIDE 0x200
432 #define ZEBRA_FLAG_EVPN_ROUTE 0x400
433 /* ZEBRA_FLAG_BLACKHOLE was 0x04 */
434 /* ZEBRA_FLAG_REJECT was 0x80 */
435
436 /* Zebra FEC flags. */
437 #define ZEBRA_FEC_REGISTER_LABEL_INDEX 0x1
438
439 #ifndef INADDR_LOOPBACK
440 #define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
441 #endif
442
443 /* Address family numbers from RFC1700. */
444 typedef enum { AFI_IP = 1, AFI_IP6 = 2, AFI_L2VPN = 3, AFI_MAX = 4 } afi_t;
445
446 /* Subsequent Address Family Identifier. */
447 typedef enum {
448 SAFI_UNICAST = 1,
449 SAFI_MULTICAST = 2,
450 SAFI_MPLS_VPN = 3,
451 SAFI_ENCAP = 4,
452 SAFI_EVPN = 5,
453 SAFI_LABELED_UNICAST = 6,
454 SAFI_FLOWSPEC = 7,
455 SAFI_MAX = 8
456 } safi_t;
457
458 /*
459 * The above AFI and SAFI definitions are for internal use. The protocol
460 * definitions (IANA values) as for example used in BGP protocol packets
461 * are defined below and these will get mapped to/from the internal values
462 * in the appropriate places.
463 * The rationale is that the protocol (IANA) values may be sparse and are
464 * not optimal for use in data-structure sizing.
465 * Note: Only useful (i.e., supported) values are defined below.
466 */
467 typedef enum {
468 IANA_AFI_RESERVED = 0,
469 IANA_AFI_IPV4 = 1,
470 IANA_AFI_IPV6 = 2,
471 IANA_AFI_L2VPN = 25,
472 IANA_AFI_IPMR = 128,
473 IANA_AFI_IP6MR = 129
474 } iana_afi_t;
475
476 typedef enum {
477 IANA_SAFI_RESERVED = 0,
478 IANA_SAFI_UNICAST = 1,
479 IANA_SAFI_MULTICAST = 2,
480 IANA_SAFI_LABELED_UNICAST = 4,
481 IANA_SAFI_ENCAP = 7,
482 IANA_SAFI_EVPN = 70,
483 IANA_SAFI_MPLS_VPN = 128,
484 IANA_SAFI_FLOWSPEC = 133
485 } iana_safi_t;
486
487 /* Default Administrative Distance of each protocol. */
488 #define ZEBRA_KERNEL_DISTANCE_DEFAULT 0
489 #define ZEBRA_CONNECT_DISTANCE_DEFAULT 0
490 #define ZEBRA_STATIC_DISTANCE_DEFAULT 1
491 #define ZEBRA_RIP_DISTANCE_DEFAULT 120
492 #define ZEBRA_RIPNG_DISTANCE_DEFAULT 120
493 #define ZEBRA_OSPF_DISTANCE_DEFAULT 110
494 #define ZEBRA_OSPF6_DISTANCE_DEFAULT 110
495 #define ZEBRA_ISIS_DISTANCE_DEFAULT 115
496 #define ZEBRA_IBGP_DISTANCE_DEFAULT 200
497 #define ZEBRA_EBGP_DISTANCE_DEFAULT 20
498 #define ZEBRA_TABLE_DISTANCE_DEFAULT 15
499
500 /* Flag manipulation macros. */
501 #define CHECK_FLAG(V,F) ((V) & (F))
502 #define SET_FLAG(V,F) (V) |= (F)
503 #define UNSET_FLAG(V,F) (V) &= ~(F)
504 #define RESET_FLAG(V) (V) = 0
505 #define COND_FLAG(V, F, C) ((C) ? (SET_FLAG(V, F)) : (UNSET_FLAG(V, F)))
506
507 /* Atomic flag manipulation macros. */
508 #define CHECK_FLAG_ATOMIC(PV, F) \
509 ((atomic_load_explicit(PV, memory_order_seq_cst)) & (F))
510 #define SET_FLAG_ATOMIC(PV, F) \
511 ((atomic_fetch_or_explicit(PV, (F), memory_order_seq_cst)))
512 #define UNSET_FLAG_ATOMIC(PV, F) \
513 ((atomic_fetch_and_explicit(PV, ~(F), memory_order_seq_cst)))
514 #define RESET_FLAG_ATOMIC(PV) \
515 ((atomic_store_explicit(PV, 0, memory_order_seq_cst)))
516
517 /* Zebra types. Used in Zserv message header. */
518 typedef uint16_t zebra_size_t;
519 typedef uint16_t zebra_command_t;
520
521 /* VRF ID type. */
522 typedef uint32_t vrf_id_t;
523
524 typedef uint32_t route_tag_t;
525 #define ROUTE_TAG_MAX UINT32_MAX
526 #define ROUTE_TAG_PRI PRIu32
527
528 static inline afi_t afi_iana2int(iana_afi_t afi)
529 {
530 switch (afi) {
531 case IANA_AFI_IPV4:
532 return AFI_IP;
533 case IANA_AFI_IPV6:
534 return AFI_IP6;
535 case IANA_AFI_L2VPN:
536 return AFI_L2VPN;
537 default:
538 return AFI_MAX;
539 }
540 }
541
542 static inline iana_afi_t afi_int2iana(afi_t afi)
543 {
544 switch (afi) {
545 case AFI_IP:
546 return IANA_AFI_IPV4;
547 case AFI_IP6:
548 return IANA_AFI_IPV6;
549 case AFI_L2VPN:
550 return IANA_AFI_L2VPN;
551 default:
552 return IANA_AFI_RESERVED;
553 }
554 }
555
556 static inline safi_t safi_iana2int(iana_safi_t safi)
557 {
558 switch (safi) {
559 case IANA_SAFI_UNICAST:
560 return SAFI_UNICAST;
561 case IANA_SAFI_MULTICAST:
562 return SAFI_MULTICAST;
563 case IANA_SAFI_MPLS_VPN:
564 return SAFI_MPLS_VPN;
565 case IANA_SAFI_ENCAP:
566 return SAFI_ENCAP;
567 case IANA_SAFI_EVPN:
568 return SAFI_EVPN;
569 case IANA_SAFI_LABELED_UNICAST:
570 return SAFI_LABELED_UNICAST;
571 case IANA_SAFI_FLOWSPEC:
572 return SAFI_FLOWSPEC;
573 default:
574 return SAFI_MAX;
575 }
576 }
577
578 static inline iana_safi_t safi_int2iana(safi_t safi)
579 {
580 switch (safi) {
581 case SAFI_UNICAST:
582 return IANA_SAFI_UNICAST;
583 case SAFI_MULTICAST:
584 return IANA_SAFI_MULTICAST;
585 case SAFI_MPLS_VPN:
586 return IANA_SAFI_MPLS_VPN;
587 case SAFI_ENCAP:
588 return IANA_SAFI_ENCAP;
589 case SAFI_EVPN:
590 return IANA_SAFI_EVPN;
591 case SAFI_LABELED_UNICAST:
592 return IANA_SAFI_LABELED_UNICAST;
593 case SAFI_FLOWSPEC:
594 return IANA_SAFI_FLOWSPEC;
595 default:
596 return IANA_SAFI_RESERVED;
597 }
598 }
599
600 #endif /* _ZEBRA_H */