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