]> git.proxmox.com Git - mirror_frr.git/blob - lib/zebra.h
bgpd, zebra: auto assign labels from label pool to regular prefixes in BGP labeled...
[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 typedef unsigned int uint32_t;
32 typedef unsigned short uint16_t;
33 typedef unsigned char uint8_t;
34 #endif /* SUNOS_5 */
35
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stddef.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <string.h>
45 #include <pwd.h>
46 #include <grp.h>
47 #ifdef HAVE_STROPTS_H
48 #include <stropts.h>
49 #endif /* HAVE_STROPTS_H */
50 #include <sys/select.h>
51 #include <sys/stat.h>
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #ifdef HAVE_SYS_SYSCTL_H
55 #ifdef GNU_LINUX
56 #include <linux/types.h>
57 #endif
58 #include <sys/sysctl.h>
59 #endif /* HAVE_SYS_SYSCTL_H */
60 #include <sys/ioctl.h>
61 #ifdef HAVE_SYS_CONF_H
62 #include <sys/conf.h>
63 #endif /* HAVE_SYS_CONF_H */
64 #ifdef HAVE_SYS_KSYM_H
65 #include <sys/ksym.h>
66 #endif /* HAVE_SYS_KSYM_H */
67 #include <syslog.h>
68 #include <sys/time.h>
69 #include <time.h>
70 #include <sys/uio.h>
71 #include <sys/utsname.h>
72 #include <sys/resource.h>
73 #include <limits.h>
74 #include <inttypes.h>
75 #include <stdbool.h>
76
77 /* machine dependent includes */
78 #ifdef SUNOS_5
79 #include <strings.h>
80 #endif /* SUNOS_5 */
81
82 /* machine dependent includes */
83 #ifdef HAVE_LINUX_VERSION_H
84 #include <linux/version.h>
85 #endif /* HAVE_LINUX_VERSION_H */
86
87 #ifdef HAVE_ASM_TYPES_H
88 #include <asm/types.h>
89 #endif /* HAVE_ASM_TYPES_H */
90
91 /* misc include group */
92 #include <stdarg.h>
93 #if !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
94 /* Not C99; do we need to define va_copy? */
95 #ifndef va_copy
96 #ifdef __va_copy
97 #define va_copy(DST,SRC) __va_copy(DST,SRC)
98 #else
99 /* Now we are desperate; this should work on many typical platforms.
100 But this is slightly dangerous, because the standard does not require
101 va_copy to be a macro. */
102 #define va_copy(DST,SRC) memcpy(&(DST), &(SRC), sizeof(va_list))
103 #warning "Not C99 and no va_copy macro available, falling back to memcpy"
104 #endif /* __va_copy */
105 #endif /* !va_copy */
106 #endif /* !C99 */
107
108
109 #ifdef HAVE_LCAPS
110 #include <sys/capability.h>
111 #include <sys/prctl.h>
112 #endif /* HAVE_LCAPS */
113
114 #ifdef HAVE_SOLARIS_CAPABILITIES
115 #include <priv.h>
116 #endif /* HAVE_SOLARIS_CAPABILITIES */
117
118 /* network include group */
119
120 #include <sys/socket.h>
121
122 #ifdef HAVE_SYS_SOCKIO_H
123 #include <sys/sockio.h>
124 #endif /* HAVE_SYS_SOCKIO_H */
125
126 #ifdef __APPLE__
127 #define __APPLE_USE_RFC_3542
128 #endif
129
130 #ifndef HAVE_LIBCRYPT
131 #ifdef HAVE_LIBCRYPTO
132 #include <openssl/des.h>
133 # define crypt DES_crypt
134 #endif
135 #endif
136
137 #include "openbsd-tree.h"
138
139 #include <netinet/in.h>
140 #include <netinet/in_systm.h>
141 #include <netinet/ip.h>
142 #include <netinet/tcp.h>
143
144 #ifdef HAVE_NET_NETOPT_H
145 #include <net/netopt.h>
146 #endif /* HAVE_NET_NETOPT_H */
147
148 #include <net/if.h>
149
150 #ifdef HAVE_NET_IF_DL_H
151 #include <net/if_dl.h>
152 #endif /* HAVE_NET_IF_DL_H */
153
154 #ifdef HAVE_NET_IF_VAR_H
155 #include <net/if_var.h>
156 #endif /* HAVE_NET_IF_VAR_H */
157
158 #include <net/route.h>
159
160 #ifdef HAVE_NETLINK
161 #include <linux/netlink.h>
162 #include <linux/rtnetlink.h>
163 #include <linux/filter.h>
164 #else
165 #define RT_TABLE_MAIN 0
166 #endif /* HAVE_NETLINK */
167
168 #include <netdb.h>
169 #include <arpa/inet.h>
170
171 #ifdef HAVE_INET_ND_H
172 #include <inet/nd.h>
173 #endif /* HAVE_INET_ND_H */
174
175 #ifdef HAVE_NETINET_IN_VAR_H
176 #include <netinet/in_var.h>
177 #endif /* HAVE_NETINET_IN_VAR_H */
178
179 #ifdef HAVE_NETINET6_IN6_VAR_H
180 #include <netinet6/in6_var.h>
181 #endif /* HAVE_NETINET6_IN6_VAR_H */
182
183 #ifdef HAVE_NETINET_IN6_VAR_H
184 #include <netinet/in6_var.h>
185 #endif /* HAVE_NETINET_IN6_VAR_H */
186
187 #ifdef HAVE_NETINET6_IN_H
188 #include <netinet6/in.h>
189 #endif /* HAVE_NETINET6_IN_H */
190
191
192 #ifdef HAVE_NETINET6_IP6_H
193 #include <netinet6/ip6.h>
194 #endif /* HAVE_NETINET6_IP6_H */
195
196 #include <netinet/icmp6.h>
197
198 #ifdef HAVE_NETINET6_ND6_H
199 #include <netinet6/nd6.h>
200 #endif /* HAVE_NETINET6_ND6_H */
201
202 /* Some systems do not define UINT32_MAX, etc.. from inttypes.h
203 * e.g. this makes life easier for FBSD 4.11 users.
204 */
205 #ifndef INT8_MAX
206 #define INT8_MAX (127)
207 #endif
208 #ifndef INT16_MAX
209 #define INT16_MAX (32767)
210 #endif
211 #ifndef INT32_MAX
212 #define INT32_MAX (2147483647)
213 #endif
214 #ifndef UINT8_MAX
215 #define UINT8_MAX (255U)
216 #endif
217 #ifndef UINT16_MAX
218 #define UINT16_MAX (65535U)
219 #endif
220 #ifndef UINT32_MAX
221 #define UINT32_MAX (4294967295U)
222 #endif
223
224 #ifdef HAVE_GLIBC_BACKTRACE
225 #include <execinfo.h>
226 #endif /* HAVE_GLIBC_BACKTRACE */
227
228 /* Local includes: */
229 #if !(defined(__GNUC__) || defined(VTYSH_EXTRACT_PL))
230 #define __attribute__(x)
231 #endif /* !__GNUC__ || VTYSH_EXTRACT_PL */
232
233 #include "zassert.h"
234
235 #ifndef HAVE_STRLCAT
236 size_t strlcat(char *__restrict dest,
237 const char *__restrict src, size_t destsize);
238 #endif
239 #ifndef HAVE_STRLCPY
240 size_t strlcpy(char *__restrict dest,
241 const char *__restrict src, size_t destsize);
242 #endif
243
244 /* GCC have printf type attribute check. */
245 #ifdef __GNUC__
246 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
247 #else
248 #define PRINTF_ATTRIBUTE(a,b)
249 #endif /* __GNUC__ */
250
251 /*
252 * RFC 3542 defines several macros for using struct cmsghdr.
253 * Here, we define those that are not present
254 */
255
256 /*
257 * Internal defines, for use only in this file.
258 * These are likely wrong on other than ILP32 machines, so warn.
259 */
260 #ifndef _CMSG_DATA_ALIGN
261 #define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
262 #endif /* _CMSG_DATA_ALIGN */
263
264 #ifndef _CMSG_HDR_ALIGN
265 #define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
266 #endif /* _CMSG_HDR_ALIGN */
267
268 /*
269 * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
270 * version.
271 */
272 #ifndef CMSG_SPACE
273 #define CMSG_SPACE(l) \
274 (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + _CMSG_HDR_ALIGN(l))
275 #warning "assuming 4-byte alignment for CMSG_SPACE"
276 #endif /* CMSG_SPACE */
277
278
279 #ifndef CMSG_LEN
280 #define CMSG_LEN(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + (l))
281 #warning "assuming 4-byte alignment for CMSG_LEN"
282 #endif /* CMSG_LEN */
283
284
285 /* The definition of struct in_pktinfo is missing in old version of
286 GLIBC 2.1 (Redhat 6.1). */
287 #if defined(GNU_LINUX) && !defined(HAVE_STRUCT_IN_PKTINFO)
288 struct in_pktinfo {
289 int ipi_ifindex;
290 struct in_addr ipi_spec_dst;
291 struct in_addr ipi_addr;
292 };
293 #endif
294
295 /*
296 * IP_HDRINCL / struct ip byte order
297 *
298 * Linux: network byte order
299 * *BSD: network, except for length and offset. (cf Stevens)
300 * SunOS: nominally as per BSD. but bug: network order on LE.
301 * OpenBSD: network byte order, apart from older versions which are as per
302 * *BSD
303 */
304 #if defined(__NetBSD__) \
305 || (defined(__FreeBSD__) && (__FreeBSD_version < 1100030)) \
306 || (defined(__OpenBSD__) && (OpenBSD < 200311)) \
307 || (defined(__APPLE__)) \
308 || (defined(SUNOS_5) && defined(WORDS_BIGENDIAN))
309 #define HAVE_IP_HDRINCL_BSD_ORDER
310 #endif
311
312 /* Define BYTE_ORDER, if not defined. Useful for compiler conditional
313 * code, rather than preprocessor conditional.
314 * Not all the world has this BSD define.
315 */
316 #ifndef BYTE_ORDER
317 #define BIG_ENDIAN 4321 /* least-significant byte first (vax, pc) */
318 #define LITTLE_ENDIAN 1234 /* most-significant byte first (IBM, net) */
319 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
320
321 #if defined(WORDS_BIGENDIAN)
322 #define BYTE_ORDER BIG_ENDIAN
323 #else /* !WORDS_BIGENDIAN */
324 #define BYTE_ORDER LITTLE_ENDIAN
325 #endif /* WORDS_BIGENDIAN */
326
327 #endif /* ndef BYTE_ORDER */
328
329 /* MAX / MIN are not commonly defined, but useful */
330 /* note: glibc sys/param.h has #define MIN(a,b) (((a)<(b))?(a):(b)) */
331 #ifdef MAX
332 #undef MAX
333 #endif
334 #define MAX(a, b) \
335 ({ \
336 typeof(a) _max_a = (a); \
337 typeof(b) _max_b = (b); \
338 _max_a > _max_b ? _max_a : _max_b; \
339 })
340 #ifdef MIN
341 #undef MIN
342 #endif
343 #define MIN(a, b) \
344 ({ \
345 typeof(a) _min_a = (a); \
346 typeof(b) _min_b = (b); \
347 _min_a < _min_b ? _min_a : _min_b; \
348 })
349
350 #ifndef offsetof
351 #ifdef __compiler_offsetof
352 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
353 #else
354 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
355 #endif
356 #endif
357
358 #ifndef container_of
359 #define container_of(ptr, type, member) \
360 ({ \
361 const typeof(((type *)0)->member) *__mptr = (ptr); \
362 (type *)((char *)__mptr - offsetof(type, member)); \
363 })
364 #endif
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 /*
383 * The compiler.h header is used for anyone using the CPP_NOTICE
384 * since this is universally needed, let's add it to zebra.h
385 */
386 #include "compiler.h"
387
388 /* Zebra route's types are defined in route_types.h */
389 #include "route_types.h"
390
391 /* Note: whenever a new route-type or zserv-command is added the
392 * corresponding {command,route}_types[] table in lib/log.c MUST be
393 * updated! */
394
395 /* Map a route type to a string. For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
396 extern const char *zebra_route_string(unsigned int route_type);
397 /* Map a route type to a char. For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
398 extern char zebra_route_char(unsigned int route_type);
399 /* Map a zserv command type to the same string,
400 * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
401 /* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
402 extern int proto_name2num(const char *s);
403 /* Map redistribute X argument to protocol number.
404 * unlike proto_name2num, this accepts shorthands and takes
405 * an AFI value to restrict input */
406 extern int proto_redistnum(int afi, const char *s);
407
408 extern const char *zserv_command_string(unsigned int command);
409
410 #define strmatch(a,b) (!strcmp((a), (b)))
411
412 /* Zebra message flags */
413
414 /*
415 * Cause Zebra to consider this routes nexthops recursively
416 */
417 #define ZEBRA_FLAG_ALLOW_RECURSION 0x01
418 /*
419 * This is a route that is read in on startup that was left around
420 * from a previous run of FRR
421 */
422 #define ZEBRA_FLAG_SELFROUTE 0x02
423 /*
424 * This flag is used to tell Zebra that the BGP route being passed
425 * down is a IBGP route
426 */
427 #define ZEBRA_FLAG_IBGP 0x04
428 /*
429 * This is a route that has been selected for FIB installation.
430 * This flag is set in zebra and can be passed up to routing daemons
431 */
432 #define ZEBRA_FLAG_SELECTED 0x08
433 /*
434 * This is a route that we are telling Zebra that this route *must*
435 * win and will be installed even over ZEBRA_FLAG_SELECTED
436 */
437 #define ZEBRA_FLAG_FIB_OVERRIDE 0x10
438 /*
439 * This flag tells Zebra that the route is a EVPN route and should
440 * be treated specially
441 */
442 #define ZEBRA_FLAG_EVPN_ROUTE 0x20
443 /*
444 * This flag tells Zebra that it should treat the distance passed
445 * down as an additional discriminator for route selection of the
446 * route entry. This mainly is used for backup static routes.
447 */
448 #define ZEBRA_FLAG_RR_USE_DISTANCE 0x40
449 /*
450 * This flag tells Zebra that the passed down route is ONLINK and the
451 * kernel install flag for it should be turned on
452 */
453 #define ZEBRA_FLAG_ONLINK 0x80
454
455 #ifndef INADDR_LOOPBACK
456 #define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
457 #endif
458
459 /* Address family numbers from RFC1700. */
460 typedef enum { AFI_IP = 1, AFI_IP6 = 2, AFI_L2VPN = 3, AFI_MAX = 4 } afi_t;
461
462 /* Subsequent Address Family Identifier. */
463 typedef enum {
464 SAFI_UNICAST = 1,
465 SAFI_MULTICAST = 2,
466 SAFI_MPLS_VPN = 3,
467 SAFI_ENCAP = 4,
468 SAFI_EVPN = 5,
469 SAFI_LABELED_UNICAST = 6,
470 SAFI_FLOWSPEC = 7,
471 SAFI_MAX = 8
472 } safi_t;
473
474 /*
475 * The above AFI and SAFI definitions are for internal use. The protocol
476 * definitions (IANA values) as for example used in BGP protocol packets
477 * are defined below and these will get mapped to/from the internal values
478 * in the appropriate places.
479 * The rationale is that the protocol (IANA) values may be sparse and are
480 * not optimal for use in data-structure sizing.
481 * Note: Only useful (i.e., supported) values are defined below.
482 */
483 typedef enum {
484 IANA_AFI_RESERVED = 0,
485 IANA_AFI_IPV4 = 1,
486 IANA_AFI_IPV6 = 2,
487 IANA_AFI_L2VPN = 25,
488 IANA_AFI_IPMR = 128,
489 IANA_AFI_IP6MR = 129
490 } iana_afi_t;
491
492 typedef enum {
493 IANA_SAFI_RESERVED = 0,
494 IANA_SAFI_UNICAST = 1,
495 IANA_SAFI_MULTICAST = 2,
496 IANA_SAFI_LABELED_UNICAST = 4,
497 IANA_SAFI_ENCAP = 7,
498 IANA_SAFI_EVPN = 70,
499 IANA_SAFI_MPLS_VPN = 128,
500 IANA_SAFI_FLOWSPEC = 133
501 } iana_safi_t;
502
503 /* Default Administrative Distance of each protocol. */
504 #define ZEBRA_KERNEL_DISTANCE_DEFAULT 0
505 #define ZEBRA_CONNECT_DISTANCE_DEFAULT 0
506 #define ZEBRA_STATIC_DISTANCE_DEFAULT 1
507 #define ZEBRA_RIP_DISTANCE_DEFAULT 120
508 #define ZEBRA_RIPNG_DISTANCE_DEFAULT 120
509 #define ZEBRA_OSPF_DISTANCE_DEFAULT 110
510 #define ZEBRA_OSPF6_DISTANCE_DEFAULT 110
511 #define ZEBRA_ISIS_DISTANCE_DEFAULT 115
512 #define ZEBRA_IBGP_DISTANCE_DEFAULT 200
513 #define ZEBRA_EBGP_DISTANCE_DEFAULT 20
514 #define ZEBRA_TABLE_DISTANCE_DEFAULT 15
515
516 /* Flag manipulation macros. */
517 #define CHECK_FLAG(V,F) ((V) & (F))
518 #define SET_FLAG(V,F) (V) |= (F)
519 #define UNSET_FLAG(V,F) (V) &= ~(F)
520 #define RESET_FLAG(V) (V) = 0
521 #define COND_FLAG(V, F, C) ((C) ? (SET_FLAG(V, F)) : (UNSET_FLAG(V, F)))
522
523 /* Atomic flag manipulation macros. */
524 #define CHECK_FLAG_ATOMIC(PV, F) \
525 ((atomic_load_explicit(PV, memory_order_seq_cst)) & (F))
526 #define SET_FLAG_ATOMIC(PV, F) \
527 ((atomic_fetch_or_explicit(PV, (F), memory_order_seq_cst)))
528 #define UNSET_FLAG_ATOMIC(PV, F) \
529 ((atomic_fetch_and_explicit(PV, ~(F), memory_order_seq_cst)))
530 #define RESET_FLAG_ATOMIC(PV) \
531 ((atomic_store_explicit(PV, 0, memory_order_seq_cst)))
532
533 /* Zebra types. Used in Zserv message header. */
534 typedef uint16_t zebra_size_t;
535 typedef uint16_t zebra_command_t;
536
537 /* VRF ID type. */
538 typedef uint32_t vrf_id_t;
539
540 typedef uint32_t route_tag_t;
541 #define ROUTE_TAG_MAX UINT32_MAX
542 #define ROUTE_TAG_PRI PRIu32
543
544 static inline afi_t afi_iana2int(iana_afi_t afi)
545 {
546 switch (afi) {
547 case IANA_AFI_IPV4:
548 return AFI_IP;
549 case IANA_AFI_IPV6:
550 return AFI_IP6;
551 case IANA_AFI_L2VPN:
552 return AFI_L2VPN;
553 default:
554 return AFI_MAX;
555 }
556 }
557
558 static inline iana_afi_t afi_int2iana(afi_t afi)
559 {
560 switch (afi) {
561 case AFI_IP:
562 return IANA_AFI_IPV4;
563 case AFI_IP6:
564 return IANA_AFI_IPV6;
565 case AFI_L2VPN:
566 return IANA_AFI_L2VPN;
567 default:
568 return IANA_AFI_RESERVED;
569 }
570 }
571
572 static inline safi_t safi_iana2int(iana_safi_t safi)
573 {
574 switch (safi) {
575 case IANA_SAFI_UNICAST:
576 return SAFI_UNICAST;
577 case IANA_SAFI_MULTICAST:
578 return SAFI_MULTICAST;
579 case IANA_SAFI_MPLS_VPN:
580 return SAFI_MPLS_VPN;
581 case IANA_SAFI_ENCAP:
582 return SAFI_ENCAP;
583 case IANA_SAFI_EVPN:
584 return SAFI_EVPN;
585 case IANA_SAFI_LABELED_UNICAST:
586 return SAFI_LABELED_UNICAST;
587 case IANA_SAFI_FLOWSPEC:
588 return SAFI_FLOWSPEC;
589 default:
590 return SAFI_MAX;
591 }
592 }
593
594 static inline iana_safi_t safi_int2iana(safi_t safi)
595 {
596 switch (safi) {
597 case SAFI_UNICAST:
598 return IANA_SAFI_UNICAST;
599 case SAFI_MULTICAST:
600 return IANA_SAFI_MULTICAST;
601 case SAFI_MPLS_VPN:
602 return IANA_SAFI_MPLS_VPN;
603 case SAFI_ENCAP:
604 return IANA_SAFI_ENCAP;
605 case SAFI_EVPN:
606 return IANA_SAFI_EVPN;
607 case SAFI_LABELED_UNICAST:
608 return IANA_SAFI_LABELED_UNICAST;
609 case SAFI_FLOWSPEC:
610 return IANA_SAFI_FLOWSPEC;
611 default:
612 return IANA_SAFI_RESERVED;
613 }
614 }
615
616 #endif /* _ZEBRA_H */