zebra.h 14 KB

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