]> git.proxmox.com Git - mirror_frr.git/blob - lib/zebra.h
Merge pull request #2994 from opensourcerouting/sa-warnings
[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 /* GCC have printf type attribute check. */
246 #ifdef __GNUC__
247 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
248 #else
249 #define PRINTF_ATTRIBUTE(a,b)
250 #endif /* __GNUC__ */
251
252 /*
253 * RFC 3542 defines several macros for using struct cmsghdr.
254 * Here, we define those that are not present
255 */
256
257 /*
258 * Internal defines, for use only in this file.
259 * These are likely wrong on other than ILP32 machines, so warn.
260 */
261 #ifndef _CMSG_DATA_ALIGN
262 #define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
263 #endif /* _CMSG_DATA_ALIGN */
264
265 #ifndef _CMSG_HDR_ALIGN
266 #define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
267 #endif /* _CMSG_HDR_ALIGN */
268
269 /*
270 * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
271 * version.
272 */
273 #ifndef CMSG_SPACE
274 #define CMSG_SPACE(l) \
275 (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + _CMSG_HDR_ALIGN(l))
276 #warning "assuming 4-byte alignment for CMSG_SPACE"
277 #endif /* CMSG_SPACE */
278
279
280 #ifndef CMSG_LEN
281 #define CMSG_LEN(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + (l))
282 #warning "assuming 4-byte alignment for CMSG_LEN"
283 #endif /* CMSG_LEN */
284
285
286 /* The definition of struct in_pktinfo is missing in old version of
287 GLIBC 2.1 (Redhat 6.1). */
288 #if defined(GNU_LINUX) && !defined(HAVE_STRUCT_IN_PKTINFO)
289 struct in_pktinfo {
290 int ipi_ifindex;
291 struct in_addr ipi_spec_dst;
292 struct in_addr ipi_addr;
293 };
294 #endif
295
296 /*
297 * IP_HDRINCL / struct ip byte order
298 *
299 * Linux: network byte order
300 * *BSD: network, except for length and offset. (cf Stevens)
301 * SunOS: nominally as per BSD. but bug: network order on LE.
302 * OpenBSD: network byte order, apart from older versions which are as per
303 * *BSD
304 */
305 #if defined(__NetBSD__) \
306 || (defined(__FreeBSD__) && (__FreeBSD_version < 1100030)) \
307 || (defined(__OpenBSD__) && (OpenBSD < 200311)) \
308 || (defined(__APPLE__)) \
309 || (defined(SUNOS_5) && defined(WORDS_BIGENDIAN))
310 #define HAVE_IP_HDRINCL_BSD_ORDER
311 #endif
312
313 /* Define BYTE_ORDER, if not defined. Useful for compiler conditional
314 * code, rather than preprocessor conditional.
315 * Not all the world has this BSD define.
316 */
317 #ifndef BYTE_ORDER
318 #define BIG_ENDIAN 4321 /* least-significant byte first (vax, pc) */
319 #define LITTLE_ENDIAN 1234 /* most-significant byte first (IBM, net) */
320 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
321
322 #if defined(WORDS_BIGENDIAN)
323 #define BYTE_ORDER BIG_ENDIAN
324 #else /* !WORDS_BIGENDIAN */
325 #define BYTE_ORDER LITTLE_ENDIAN
326 #endif /* WORDS_BIGENDIAN */
327
328 #endif /* ndef BYTE_ORDER */
329
330 /* MAX / MIN are not commonly defined, but useful */
331 /* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
332 #ifdef MAX
333 #undef MAX
334 #endif
335 #define MAX(a, b) \
336 ({ \
337 typeof(a) _a = (a); \
338 typeof(b) _b = (b); \
339 _a > _b ? _a : _b; \
340 })
341 #ifdef MIN
342 #undef MIN
343 #endif
344 #define MIN(a, b) \
345 ({ \
346 typeof(a) _a = (a); \
347 typeof(b) _b = (b); \
348 _a < _b ? _a : _b; \
349 })
350
351 #ifndef offsetof
352 #ifdef __compiler_offsetof
353 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
354 #else
355 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
356 #endif
357 #endif
358
359 #ifndef container_of
360 #define container_of(ptr, type, member) \
361 ({ \
362 const typeof(((type *)0)->member) *__mptr = (ptr); \
363 (type *)((char *)__mptr - offsetof(type, member)); \
364 })
365 #endif
366
367 #define ZEBRA_NUM_OF(x) (sizeof (x) / sizeof (x[0]))
368
369 /* For old definition. */
370 #ifndef IN6_ARE_ADDR_EQUAL
371 #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
372 #endif /* IN6_ARE_ADDR_EQUAL */
373
374 /* default zebra TCP port for zclient */
375 #define ZEBRA_PORT 2600
376
377 /* Marker value used in new Zserv, in the byte location corresponding
378 * the command value in the old zserv header. To allow old and new
379 * Zserv headers to be distinguished from each other.
380 */
381 #define ZEBRA_HEADER_MARKER 254
382
383 /* Zebra route's types are defined in route_types.h */
384 #include "route_types.h"
385
386 /* Note: whenever a new route-type or zserv-command is added the
387 * corresponding {command,route}_types[] table in lib/log.c MUST be
388 * updated! */
389
390 /* Map a route type to a string. For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
391 extern const char *zebra_route_string(unsigned int route_type);
392 /* Map a route type to a char. For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
393 extern char zebra_route_char(unsigned int route_type);
394 /* Map a zserv command type to the same string,
395 * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
396 /* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
397 extern int proto_name2num(const char *s);
398 /* Map redistribute X argument to protocol number.
399 * unlike proto_name2num, this accepts shorthands and takes
400 * an AFI value to restrict input */
401 extern int proto_redistnum(int afi, const char *s);
402
403 extern const char *zserv_command_string(unsigned int command);
404
405 #define strmatch(a,b) (!strcmp((a), (b)))
406
407 /* Zebra message flags */
408 #define ZEBRA_FLAG_ALLOW_RECURSION 0x01
409 #define ZEBRA_FLAG_SELFROUTE 0x02
410 #define ZEBRA_FLAG_IBGP 0x08
411 #define ZEBRA_FLAG_SELECTED 0x10
412 #define ZEBRA_FLAG_STATIC 0x40
413 #define ZEBRA_FLAG_SCOPE_LINK 0x100
414 #define ZEBRA_FLAG_FIB_OVERRIDE 0x200
415 #define ZEBRA_FLAG_EVPN_ROUTE 0x400
416 #define ZEBRA_FLAG_RR_USE_DISTANCE 0x800
417 #define ZEBRA_FLAG_ONLINK 0x1000
418 /* ZEBRA_FLAG_BLACKHOLE was 0x04 */
419 /* ZEBRA_FLAG_REJECT was 0x80 */
420
421 /* Zebra FEC flags. */
422 #define ZEBRA_FEC_REGISTER_LABEL_INDEX 0x1
423
424 #ifndef INADDR_LOOPBACK
425 #define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
426 #endif
427
428 /* Address family numbers from RFC1700. */
429 typedef enum { AFI_IP = 1, AFI_IP6 = 2, AFI_L2VPN = 3, AFI_MAX = 4 } afi_t;
430
431 /* Subsequent Address Family Identifier. */
432 typedef enum {
433 SAFI_UNICAST = 1,
434 SAFI_MULTICAST = 2,
435 SAFI_MPLS_VPN = 3,
436 SAFI_ENCAP = 4,
437 SAFI_EVPN = 5,
438 SAFI_LABELED_UNICAST = 6,
439 SAFI_FLOWSPEC = 7,
440 SAFI_MAX = 8
441 } safi_t;
442
443 /*
444 * The above AFI and SAFI definitions are for internal use. The protocol
445 * definitions (IANA values) as for example used in BGP protocol packets
446 * are defined below and these will get mapped to/from the internal values
447 * in the appropriate places.
448 * The rationale is that the protocol (IANA) values may be sparse and are
449 * not optimal for use in data-structure sizing.
450 * Note: Only useful (i.e., supported) values are defined below.
451 */
452 typedef enum {
453 IANA_AFI_RESERVED = 0,
454 IANA_AFI_IPV4 = 1,
455 IANA_AFI_IPV6 = 2,
456 IANA_AFI_L2VPN = 25,
457 IANA_AFI_IPMR = 128,
458 IANA_AFI_IP6MR = 129
459 } iana_afi_t;
460
461 typedef enum {
462 IANA_SAFI_RESERVED = 0,
463 IANA_SAFI_UNICAST = 1,
464 IANA_SAFI_MULTICAST = 2,
465 IANA_SAFI_LABELED_UNICAST = 4,
466 IANA_SAFI_ENCAP = 7,
467 IANA_SAFI_EVPN = 70,
468 IANA_SAFI_MPLS_VPN = 128,
469 IANA_SAFI_FLOWSPEC = 133
470 } iana_safi_t;
471
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
480 #define ZEBRA_ISIS_DISTANCE_DEFAULT 115
481 #define ZEBRA_IBGP_DISTANCE_DEFAULT 200
482 #define ZEBRA_EBGP_DISTANCE_DEFAULT 20
483 #define ZEBRA_TABLE_DISTANCE_DEFAULT 15
484
485 /* Flag manipulation macros. */
486 #define CHECK_FLAG(V,F) ((V) & (F))
487 #define SET_FLAG(V,F) (V) |= (F)
488 #define UNSET_FLAG(V,F) (V) &= ~(F)
489 #define RESET_FLAG(V) (V) = 0
490 #define COND_FLAG(V, F, C) ((C) ? (SET_FLAG(V, F)) : (UNSET_FLAG(V, F)))
491
492 /* Atomic flag manipulation macros. */
493 #define CHECK_FLAG_ATOMIC(PV, F) \
494 ((atomic_load_explicit(PV, memory_order_seq_cst)) & (F))
495 #define SET_FLAG_ATOMIC(PV, F) \
496 ((atomic_fetch_or_explicit(PV, (F), memory_order_seq_cst)))
497 #define UNSET_FLAG_ATOMIC(PV, F) \
498 ((atomic_fetch_and_explicit(PV, ~(F), memory_order_seq_cst)))
499 #define RESET_FLAG_ATOMIC(PV) \
500 ((atomic_store_explicit(PV, 0, memory_order_seq_cst)))
501
502 /* Zebra types. Used in Zserv message header. */
503 typedef uint16_t zebra_size_t;
504 typedef uint16_t zebra_command_t;
505
506 /* VRF ID type. */
507 typedef uint32_t vrf_id_t;
508
509 typedef uint32_t route_tag_t;
510 #define ROUTE_TAG_MAX UINT32_MAX
511 #define ROUTE_TAG_PRI PRIu32
512
513 static inline afi_t afi_iana2int(iana_afi_t afi)
514 {
515 switch (afi) {
516 case IANA_AFI_IPV4:
517 return AFI_IP;
518 case IANA_AFI_IPV6:
519 return AFI_IP6;
520 case IANA_AFI_L2VPN:
521 return AFI_L2VPN;
522 default:
523 return AFI_MAX;
524 }
525 }
526
527 static inline iana_afi_t afi_int2iana(afi_t afi)
528 {
529 switch (afi) {
530 case AFI_IP:
531 return IANA_AFI_IPV4;
532 case AFI_IP6:
533 return IANA_AFI_IPV6;
534 case AFI_L2VPN:
535 return IANA_AFI_L2VPN;
536 default:
537 return IANA_AFI_RESERVED;
538 }
539 }
540
541 static inline safi_t safi_iana2int(iana_safi_t safi)
542 {
543 switch (safi) {
544 case IANA_SAFI_UNICAST:
545 return SAFI_UNICAST;
546 case IANA_SAFI_MULTICAST:
547 return SAFI_MULTICAST;
548 case IANA_SAFI_MPLS_VPN:
549 return SAFI_MPLS_VPN;
550 case IANA_SAFI_ENCAP:
551 return SAFI_ENCAP;
552 case IANA_SAFI_EVPN:
553 return SAFI_EVPN;
554 case IANA_SAFI_LABELED_UNICAST:
555 return SAFI_LABELED_UNICAST;
556 case IANA_SAFI_FLOWSPEC:
557 return SAFI_FLOWSPEC;
558 default:
559 return SAFI_MAX;
560 }
561 }
562
563 static inline iana_safi_t safi_int2iana(safi_t safi)
564 {
565 switch (safi) {
566 case SAFI_UNICAST:
567 return IANA_SAFI_UNICAST;
568 case SAFI_MULTICAST:
569 return IANA_SAFI_MULTICAST;
570 case SAFI_MPLS_VPN:
571 return IANA_SAFI_MPLS_VPN;
572 case SAFI_ENCAP:
573 return IANA_SAFI_ENCAP;
574 case SAFI_EVPN:
575 return IANA_SAFI_EVPN;
576 case SAFI_LABELED_UNICAST:
577 return IANA_SAFI_LABELED_UNICAST;
578 case SAFI_FLOWSPEC:
579 return IANA_SAFI_FLOWSPEC;
580 default:
581 return IANA_SAFI_RESERVED;
582 }
583 }
584
585 #endif /* _ZEBRA_H */