zebra.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* Zebra common header.
  2. Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #ifndef _ZEBRA_H
  17. #define _ZEBRA_H
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif /* HAVE_CONFIG_H */
  21. #ifdef GNU_LINUX
  22. #define _GNU_SOURCE
  23. #endif /* GNU_LINUX */
  24. #ifdef SUNOS_5
  25. #define _XPG4_2
  26. #define __EXTENSIONS__
  27. typedef unsigned int u_int32_t;
  28. typedef unsigned short u_int16_t;
  29. typedef unsigned char u_int8_t;
  30. #endif /* SUNOS_5 */
  31. #ifndef HAVE_SOCKLEN_T
  32. typedef int socklen_t;
  33. #endif /* HAVE_SOCKLEN_T */
  34. #include <unistd.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <ctype.h>
  38. #include <errno.h>
  39. #include <fcntl.h>
  40. #include <signal.h>
  41. #include <string.h>
  42. #include <pwd.h>
  43. #include <grp.h>
  44. #ifdef HAVE_STROPTS_H
  45. #include <stropts.h>
  46. #endif /* HAVE_STROPTS_H */
  47. #include <sys/fcntl.h>
  48. #ifdef HAVE_SYS_SELECT_H
  49. #include <sys/select.h>
  50. #endif /* HAVE_SYS_SELECT_H */
  51. #include <sys/stat.h>
  52. #include <sys/time.h>
  53. #include <sys/types.h>
  54. #include <sys/param.h>
  55. #ifdef HAVE_SYS_SYSCTL_H
  56. #include <sys/sysctl.h>
  57. #endif /* HAVE_SYS_SYSCTL_H */
  58. #include <sys/ioctl.h>
  59. #ifdef HAVE_SYS_CONF_H
  60. #include <sys/conf.h>
  61. #endif /* HAVE_SYS_CONF_H */
  62. #ifdef HAVE_SYS_KSYM_H
  63. #include <sys/ksym.h>
  64. #endif /* HAVE_SYS_KSYM_H */
  65. #include <syslog.h>
  66. #include <time.h>
  67. #include <sys/uio.h>
  68. #include <sys/utsname.h>
  69. #ifdef HAVE_RUSAGE
  70. #include <sys/resource.h>
  71. #endif /* HAVE_RUSAGE */
  72. #ifdef HAVE_LIMITS_H
  73. #include <limits.h>
  74. #endif /* HAVE_LIMITS_H */
  75. /* machine dependent includes */
  76. #ifdef SUNOS_5
  77. #include <strings.h>
  78. #endif /* SUNOS_5 */
  79. /* machine dependent includes */
  80. #ifdef HAVE_LINUX_VERSION_H
  81. #include <linux/version.h>
  82. #endif /* HAVE_LINUX_VERSION_H */
  83. #ifdef HAVE_ASM_TYPES_H
  84. #include <asm/types.h>
  85. #endif /* HAVE_ASM_TYPES_H */
  86. /* misc include group */
  87. #include <stdarg.h>
  88. #if !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
  89. /* Not C99; do we need to define va_copy? */
  90. #ifndef va_copy
  91. #ifdef __va_copy
  92. #define va_copy(DST,SRC) __va_copy(DST,SRC)
  93. #else
  94. /* Now we are desperate; this should work on many typical platforms.
  95. But this is slightly dangerous, because the standard does not require
  96. va_copy to be a macro. */
  97. #define va_copy(DST,SRC) memcpy(&(DST), &(SRC), sizeof(va_list))
  98. #warning "Not C99 and no va_copy macro available, falling back to memcpy"
  99. #endif /* __va_copy */
  100. #endif /* !va_copy */
  101. #endif /* !C99 */
  102. #ifdef HAVE_LCAPS
  103. #include <sys/capability.h>
  104. #include <sys/prctl.h>
  105. #endif /* HAVE_LCAPS */
  106. #ifdef HAVE_SOLARIS_CAPABILITIES
  107. #include <priv.h>
  108. #endif /* HAVE_SOLARIS_CAPABILITIES */
  109. /* network include group */
  110. #include <sys/socket.h>
  111. #ifdef HAVE_SYS_SOCKIO_H
  112. #include <sys/sockio.h>
  113. #endif /* HAVE_SYS_SOCKIO_H */
  114. #ifdef HAVE_NETINET_IN_H
  115. #include <netinet/in.h>
  116. #endif /* HAVE_NETINET_IN_H */
  117. #include <netinet/in_systm.h>
  118. #include <netinet/ip.h>
  119. #include <netinet/tcp.h>
  120. #ifdef HAVE_NET_NETOPT_H
  121. #include <net/netopt.h>
  122. #endif /* HAVE_NET_NETOPT_H */
  123. #include <net/if.h>
  124. #ifdef HAVE_NET_IF_DL_H
  125. #include <net/if_dl.h>
  126. #endif /* HAVE_NET_IF_DL_H */
  127. #ifdef HAVE_NET_IF_VAR_H
  128. #include <net/if_var.h>
  129. #endif /* HAVE_NET_IF_VAR_H */
  130. #ifdef HAVE_NET_ROUTE_H
  131. #include <net/route.h>
  132. #endif /* HAVE_NET_ROUTE_H */
  133. #ifdef HAVE_NETLINK
  134. #include <linux/netlink.h>
  135. #include <linux/rtnetlink.h>
  136. #else
  137. #define RT_TABLE_MAIN 0
  138. #endif /* HAVE_NETLINK */
  139. #ifdef HAVE_NETDB_H
  140. #include <netdb.h>
  141. #endif /* HAVE_NETDB_H */
  142. #include <arpa/inet.h>
  143. #ifdef HAVE_INET_ND_H
  144. #include <inet/nd.h>
  145. #endif /* HAVE_INET_ND_H */
  146. #ifdef HAVE_NETINET_IN_VAR_H
  147. #include <netinet/in_var.h>
  148. #endif /* HAVE_NETINET_IN_VAR_H */
  149. #ifdef HAVE_NETINET6_IN6_VAR_H
  150. #include <netinet6/in6_var.h>
  151. #endif /* HAVE_NETINET6_IN6_VAR_H */
  152. #ifdef HAVE_NETINET_IN6_VAR_H
  153. #include <netinet/in6_var.h>
  154. #endif /* HAVE_NETINET_IN6_VAR_H */
  155. #ifdef HAVE_NETINET6_IN_H
  156. #include <netinet6/in.h>
  157. #endif /* HAVE_NETINET6_IN_H */
  158. #ifdef HAVE_NETINET6_IP6_H
  159. #include <netinet6/ip6.h>
  160. #endif /* HAVE_NETINET6_IP6_H */
  161. #ifdef HAVE_NETINET_ICMP6_H
  162. #include <netinet/icmp6.h>
  163. #endif /* HAVE_NETINET_ICMP6_H */
  164. #ifdef HAVE_NETINET6_ND6_H
  165. #include <netinet6/nd6.h>
  166. #endif /* HAVE_NETINET6_ND6_H */
  167. /* Some systems do not define UINT32_MAX */
  168. #ifndef UINT32_MAX
  169. #define UINT32_MAX 0xFFFFFFFFU
  170. #endif /* UINT32_MAX */
  171. #ifdef HAVE_LIBUTIL_H
  172. #include <libutil.h>
  173. #endif /* HAVE_LIBUTIL_H */
  174. #ifdef HAVE_GLIBC_BACKTRACE
  175. #include <execinfo.h>
  176. #endif /* HAVE_GLIBC_BACKTRACE */
  177. #ifdef BSDI_NRL
  178. #ifdef HAVE_NETINET6_IN6_H
  179. #include <netinet6/in6.h>
  180. #endif /* HAVE_NETINET6_IN6_H */
  181. #ifdef NRL
  182. #include <netinet6/in6.h>
  183. #endif /* NRL */
  184. #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
  185. #endif /* BSDI_NRL */
  186. /* Local includes: */
  187. #if !(defined(__GNUC__) || defined(VTYSH_EXTRACT_PL))
  188. #define __attribute__(x)
  189. #endif /* !__GNUC__ || VTYSH_EXTRACT_PL */
  190. #include "zassert.h"
  191. #include "str.h"
  192. #ifdef HAVE_BROKEN_CMSG_FIRSTHDR
  193. /* This bug is present in Solaris 8 and pre-patch Solaris 9 <sys/socket.h>;
  194. please refer to http://bugzilla.quagga.net/show_bug.cgi?id=142 */
  195. /* Check that msg_controllen is large enough. */
  196. #define ZCMSG_FIRSTHDR(mhdr) \
  197. (((size_t)((mhdr)->msg_controllen) >= sizeof(struct cmsghdr)) ? \
  198. CMSG_FIRSTHDR(mhdr) : (struct cmsghdr *)NULL)
  199. #warning "CMSG_FIRSTHDR is broken on this platform, using a workaround"
  200. #else /* HAVE_BROKEN_CMSG_FIRSTHDR */
  201. #define ZCMSG_FIRSTHDR(M) CMSG_FIRSTHDR(M)
  202. #endif /* HAVE_BROKEN_CMSG_FIRSTHDR */
  203. /*
  204. * RFC 3542 defines several macros for using struct cmsghdr.
  205. * Here, we define those that are not present
  206. */
  207. /*
  208. * Internal defines, for use only in this file.
  209. * These are likely wrong on other than ILP32 machines, so warn.
  210. */
  211. #ifndef _CMSG_DATA_ALIGN
  212. #define _CMSG_DATA_ALIGN(n) (((n) + 3) & ~3)
  213. #endif /* _CMSG_DATA_ALIGN */
  214. #ifndef _CMSG_HDR_ALIGN
  215. #define _CMSG_HDR_ALIGN(n) (((n) + 3) & ~3)
  216. #endif /* _CMSG_HDR_ALIGN */
  217. /*
  218. * CMSG_SPACE and CMSG_LEN are required in RFC3542, but were new in that
  219. * version.
  220. */
  221. #ifndef CMSG_SPACE
  222. #define CMSG_SPACE(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + \
  223. _CMSG_HDR_ALIGN(l))
  224. #warning "assuming 4-byte alignment for CMSG_SPACE"
  225. #endif /* CMSG_SPACE */
  226. #ifndef CMSG_LEN
  227. #define CMSG_LEN(l) (_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)) + (l))
  228. #warning "assuming 4-byte alignment for CMSG_LEN"
  229. #endif /* CMSG_LEN */
  230. /* The definition of struct in_pktinfo is missing in old version of
  231. GLIBC 2.1 (Redhat 6.1). */
  232. #if defined (GNU_LINUX) && ! defined (HAVE_INPKTINFO)
  233. struct in_pktinfo
  234. {
  235. int ipi_ifindex;
  236. struct in_addr ipi_spec_dst;
  237. struct in_addr ipi_addr;
  238. };
  239. #endif
  240. /*
  241. * OSPF Fragmentation / fragmented writes
  242. *
  243. * ospfd can support writing fragmented packets, for cases where
  244. * kernel will not fragment IP_HDRINCL and/or multicast destined
  245. * packets (ie TTBOMK all kernels, BSD, SunOS, Linux). However,
  246. * SunOS, probably BSD too, clobber the user supplied IP ID and IP
  247. * flags fields, hence user-space fragmentation will not work.
  248. * Only Linux is known to leave IP header unmolested.
  249. * Further, fragmentation really should be done the kernel, which already
  250. * supports it, and which avoids nasty IP ID state problems.
  251. *
  252. * Fragmentation of OSPF packets can be required on networks with router
  253. * with many many interfaces active in one area, or on networks with links
  254. * with low MTUs.
  255. */
  256. #ifdef GNU_LINUX
  257. #define WANT_OSPF_WRITE_FRAGMENT
  258. #endif
  259. /*
  260. * IP_HDRINCL / struct ip byte order
  261. *
  262. * Linux: network byte order
  263. * *BSD: network, except for length and offset. (cf Stevens)
  264. * SunOS: nominally as per BSD. but bug: network order on LE.
  265. * OpenBSD: network byte order, apart from older versions which are as per
  266. * *BSD
  267. */
  268. #if defined(__NetBSD__) || defined(__FreeBSD__) \
  269. || (defined(__OpenBSD__) && (OpenBSD < 200311)) \
  270. || (defined(SUNOS_5) && defined(WORDS_BIGENDIAN))
  271. #define HAVE_IP_HDRINCL_BSD_ORDER
  272. #endif
  273. /* MAX / MIN are not commonly defined, but useful */
  274. #ifndef MAX
  275. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  276. #endif
  277. #ifndef MIN
  278. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  279. #endif
  280. /* For old definition. */
  281. #ifndef IN6_ARE_ADDR_EQUAL
  282. #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL
  283. #endif /* IN6_ARE_ADDR_EQUAL */
  284. /* default zebra TCP port for zclient */
  285. #define ZEBRA_PORT 2600
  286. /* Zebra message types. */
  287. #define ZEBRA_INTERFACE_ADD 1
  288. #define ZEBRA_INTERFACE_DELETE 2
  289. #define ZEBRA_INTERFACE_ADDRESS_ADD 3
  290. #define ZEBRA_INTERFACE_ADDRESS_DELETE 4
  291. #define ZEBRA_INTERFACE_UP 5
  292. #define ZEBRA_INTERFACE_DOWN 6
  293. #define ZEBRA_IPV4_ROUTE_ADD 7
  294. #define ZEBRA_IPV4_ROUTE_DELETE 8
  295. #define ZEBRA_IPV6_ROUTE_ADD 9
  296. #define ZEBRA_IPV6_ROUTE_DELETE 10
  297. #define ZEBRA_REDISTRIBUTE_ADD 11
  298. #define ZEBRA_REDISTRIBUTE_DELETE 12
  299. #define ZEBRA_REDISTRIBUTE_DEFAULT_ADD 13
  300. #define ZEBRA_REDISTRIBUTE_DEFAULT_DELETE 14
  301. #define ZEBRA_IPV4_NEXTHOP_LOOKUP 15
  302. #define ZEBRA_IPV6_NEXTHOP_LOOKUP 16
  303. #define ZEBRA_IPV4_IMPORT_LOOKUP 17
  304. #define ZEBRA_IPV6_IMPORT_LOOKUP 18
  305. #define ZEBRA_INTERFACE_RENAME 19
  306. #define ZEBRA_ROUTER_ID_ADD 20
  307. #define ZEBRA_ROUTER_ID_DELETE 21
  308. #define ZEBRA_ROUTER_ID_UPDATE 22
  309. #define ZEBRA_MESSAGE_MAX 23
  310. /* Zebra route's types. */
  311. #define ZEBRA_ROUTE_SYSTEM 0
  312. #define ZEBRA_ROUTE_KERNEL 1
  313. #define ZEBRA_ROUTE_CONNECT 2
  314. #define ZEBRA_ROUTE_STATIC 3
  315. #define ZEBRA_ROUTE_RIP 4
  316. #define ZEBRA_ROUTE_RIPNG 5
  317. #define ZEBRA_ROUTE_OSPF 6
  318. #define ZEBRA_ROUTE_OSPF6 7
  319. #define ZEBRA_ROUTE_ISIS 8
  320. #define ZEBRA_ROUTE_BGP 9
  321. #define ZEBRA_ROUTE_HSLS 10
  322. #define ZEBRA_ROUTE_MAX 11
  323. /* Note: whenever a new route type is added (or the numbering is changed),
  324. the route_types[] table in lib/log.c must be updated! */
  325. /* Map a route type to a string. For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
  326. extern const char *zebra_route_string(u_int route_type);
  327. /* Map a route type to a char. For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
  328. extern char zebra_route_char(u_int route_type);
  329. /* Zebra's family types. */
  330. #define ZEBRA_FAMILY_IPV4 1
  331. #define ZEBRA_FAMILY_IPV6 2
  332. #define ZEBRA_FAMILY_MAX 3
  333. /* Error codes of zebra. */
  334. #define ZEBRA_ERR_RTEXIST -1
  335. #define ZEBRA_ERR_RTUNREACH -2
  336. #define ZEBRA_ERR_EPERM -3
  337. #define ZEBRA_ERR_RTNOEXIST -4
  338. /* Zebra message flags */
  339. #define ZEBRA_FLAG_INTERNAL 0x01
  340. #define ZEBRA_FLAG_SELFROUTE 0x02
  341. #define ZEBRA_FLAG_BLACKHOLE 0x04
  342. #define ZEBRA_FLAG_IBGP 0x08
  343. #define ZEBRA_FLAG_SELECTED 0x10
  344. #define ZEBRA_FLAG_CHANGED 0x20
  345. #define ZEBRA_FLAG_STATIC 0x40
  346. #define ZEBRA_FLAG_REJECT 0x80
  347. /* Zebra nexthop flags. */
  348. #define ZEBRA_NEXTHOP_IFINDEX 1
  349. #define ZEBRA_NEXTHOP_IFNAME 2
  350. #define ZEBRA_NEXTHOP_IPV4 3
  351. #define ZEBRA_NEXTHOP_IPV4_IFINDEX 4
  352. #define ZEBRA_NEXTHOP_IPV4_IFNAME 5
  353. #define ZEBRA_NEXTHOP_IPV6 6
  354. #define ZEBRA_NEXTHOP_IPV6_IFINDEX 7
  355. #define ZEBRA_NEXTHOP_IPV6_IFNAME 8
  356. #define ZEBRA_NEXTHOP_BLACKHOLE 9
  357. #ifndef INADDR_LOOPBACK
  358. #define INADDR_LOOPBACK 0x7f000001 /* Internet address 127.0.0.1. */
  359. #endif
  360. /* Address family numbers from RFC1700. */
  361. #define AFI_IP 1
  362. #define AFI_IP6 2
  363. #define AFI_MAX 3
  364. /* Subsequent Address Family Identifier. */
  365. #define SAFI_UNICAST 1
  366. #define SAFI_MULTICAST 2
  367. #define SAFI_UNICAST_MULTICAST 3
  368. #define SAFI_MPLS_VPN 4
  369. #define SAFI_MAX 5
  370. /* Filter direction. */
  371. #define FILTER_IN 0
  372. #define FILTER_OUT 1
  373. #define FILTER_MAX 2
  374. /* Default Administrative Distance of each protocol. */
  375. #define ZEBRA_KERNEL_DISTANCE_DEFAULT 0
  376. #define ZEBRA_CONNECT_DISTANCE_DEFAULT 0
  377. #define ZEBRA_STATIC_DISTANCE_DEFAULT 1
  378. #define ZEBRA_RIP_DISTANCE_DEFAULT 120
  379. #define ZEBRA_RIPNG_DISTANCE_DEFAULT 120
  380. #define ZEBRA_OSPF_DISTANCE_DEFAULT 110
  381. #define ZEBRA_OSPF6_DISTANCE_DEFAULT 110
  382. #define ZEBRA_ISIS_DISTANCE_DEFAULT 115
  383. #define ZEBRA_IBGP_DISTANCE_DEFAULT 200
  384. #define ZEBRA_EBGP_DISTANCE_DEFAULT 20
  385. /* Flag manipulation macros. */
  386. #define CHECK_FLAG(V,F) ((V) & (F))
  387. #define SET_FLAG(V,F) (V) |= (F)
  388. #define UNSET_FLAG(V,F) (V) &= ~(F)
  389. /* AFI and SAFI type. */
  390. typedef u_int16_t afi_t;
  391. typedef u_int8_t safi_t;
  392. /* Zebra types. */
  393. typedef u_int16_t zebra_size_t;
  394. typedef u_int8_t zebra_command_t;
  395. /* FIFO -- first in first out structure and macros. */
  396. struct fifo
  397. {
  398. struct fifo *next;
  399. struct fifo *prev;
  400. };
  401. #define FIFO_INIT(F) \
  402. do { \
  403. struct fifo *Xfifo = (struct fifo *)(F); \
  404. Xfifo->next = Xfifo->prev = Xfifo; \
  405. } while (0)
  406. #define FIFO_ADD(F,N) \
  407. do { \
  408. struct fifo *Xfifo = (struct fifo *)(F); \
  409. struct fifo *Xnode = (struct fifo *)(N); \
  410. Xnode->next = Xfifo; \
  411. Xnode->prev = Xfifo->prev; \
  412. Xfifo->prev = Xfifo->prev->next = Xnode; \
  413. } while (0)
  414. #define FIFO_DEL(N) \
  415. do { \
  416. struct fifo *Xnode = (struct fifo *)(N); \
  417. Xnode->prev->next = Xnode->next; \
  418. Xnode->next->prev = Xnode->prev; \
  419. } while (0)
  420. #define FIFO_HEAD(F) \
  421. ((((struct fifo *)(F))->next == (struct fifo *)(F)) \
  422. ? NULL : (F)->next)
  423. #define FIFO_EMPTY(F) \
  424. (((struct fifo *)(F))->next == (struct fifo *)(F))
  425. #define FIFO_TOP(F) \
  426. (FIFO_EMPTY(F) ? NULL : ((struct fifo *)(F))->next)
  427. #endif /* _ZEBRA_H */