rib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Routing Information Base header
  3. * Copyright (C) 1997 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #ifndef _ZEBRA_RIB_H
  23. #define _ZEBRA_RIB_H
  24. #include "prefix.h"
  25. #include "table.h"
  26. #include "queue.h"
  27. #define DISTANCE_INFINITY 255
  28. /* Routing information base. */
  29. union g_addr {
  30. struct in_addr ipv4;
  31. #ifdef HAVE_IPV6
  32. struct in6_addr ipv6;
  33. #endif /* HAVE_IPV6 */
  34. };
  35. struct rib
  36. {
  37. /* Link list. */
  38. struct rib *next;
  39. struct rib *prev;
  40. /* Nexthop structure */
  41. struct nexthop *nexthop;
  42. /* Refrence count. */
  43. unsigned long refcnt;
  44. /* Uptime. */
  45. time_t uptime;
  46. /* Type fo this route. */
  47. int type;
  48. /* Which routing table */
  49. int table;
  50. /* Metric */
  51. u_int32_t metric;
  52. /* Distance. */
  53. u_char distance;
  54. /* Flags of this route.
  55. * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
  56. * to clients via Zserv
  57. */
  58. u_char flags;
  59. /* RIB internal status */
  60. u_char status;
  61. #define RIB_ENTRY_REMOVED (1 << 0)
  62. /* Nexthop information. */
  63. u_char nexthop_num;
  64. u_char nexthop_active_num;
  65. u_char nexthop_fib_num;
  66. };
  67. /* meta-queue structure:
  68. * sub-queue 0: connected, kernel
  69. * sub-queue 1: static
  70. * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
  71. * sub-queue 3: iBGP, eBGP
  72. * sub-queue 4: any other origin (if any)
  73. */
  74. #define MQ_SIZE 5
  75. struct meta_queue
  76. {
  77. struct list *subq[MQ_SIZE];
  78. u_int32_t size; /* sum of lengths of all subqueues */
  79. };
  80. /*
  81. * Structure that represents a single destination (prefix).
  82. */
  83. typedef struct rib_dest_t_
  84. {
  85. /*
  86. * Back pointer to the route node for this destination. This helps
  87. * us get to the prefix that this structure is for.
  88. */
  89. struct route_node *rnode;
  90. /*
  91. * Doubly-linked list of routes for this prefix.
  92. */
  93. struct rib *routes;
  94. /*
  95. * Flags, see below.
  96. */
  97. u_int32_t flags;
  98. /*
  99. * Linkage to put dest on the FPM processing queue.
  100. */
  101. TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
  102. } rib_dest_t;
  103. #define RIB_ROUTE_QUEUED(x) (1 << (x))
  104. /*
  105. * The maximum qindex that can be used.
  106. */
  107. #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
  108. /*
  109. * This flag indicates that a given prefix has been 'advertised' to
  110. * the FPM to be installed in the forwarding plane.
  111. */
  112. #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
  113. /*
  114. * This flag is set when we need to send an update to the FPM about a
  115. * dest.
  116. */
  117. #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
  118. /*
  119. * Macro to iterate over each route for a destination (prefix).
  120. */
  121. #define RIB_DEST_FOREACH_ROUTE(dest, rib) \
  122. for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
  123. /*
  124. * Same as above, but allows the current node to be unlinked.
  125. */
  126. #define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
  127. for ((rib) = (dest) ? (dest)->routes : NULL; \
  128. (rib) && ((next) = (rib)->next, 1); \
  129. (rib) = (next))
  130. #define RNODE_FOREACH_RIB(rn, rib) \
  131. RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)
  132. #define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
  133. RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)
  134. /* Static route information. */
  135. struct static_ipv4
  136. {
  137. /* For linked list. */
  138. struct static_ipv4 *prev;
  139. struct static_ipv4 *next;
  140. /* Administrative distance. */
  141. u_char distance;
  142. /* Flag for this static route's type. */
  143. u_char type;
  144. #define STATIC_IPV4_GATEWAY 1
  145. #define STATIC_IPV4_IFNAME 2
  146. #define STATIC_IPV4_BLACKHOLE 3
  147. /* Nexthop value. */
  148. union
  149. {
  150. struct in_addr ipv4;
  151. char *ifname;
  152. } gate;
  153. /* bit flags */
  154. u_char flags;
  155. /*
  156. see ZEBRA_FLAG_REJECT
  157. ZEBRA_FLAG_BLACKHOLE
  158. */
  159. };
  160. #ifdef HAVE_IPV6
  161. /* Static route information. */
  162. struct static_ipv6
  163. {
  164. /* For linked list. */
  165. struct static_ipv6 *prev;
  166. struct static_ipv6 *next;
  167. /* Administrative distance. */
  168. u_char distance;
  169. /* Flag for this static route's type. */
  170. u_char type;
  171. #define STATIC_IPV6_GATEWAY 1
  172. #define STATIC_IPV6_GATEWAY_IFNAME 2
  173. #define STATIC_IPV6_IFNAME 3
  174. /* Nexthop value. */
  175. struct in6_addr ipv6;
  176. char *ifname;
  177. /* bit flags */
  178. u_char flags;
  179. /*
  180. see ZEBRA_FLAG_REJECT
  181. ZEBRA_FLAG_BLACKHOLE
  182. */
  183. };
  184. #endif /* HAVE_IPV6 */
  185. enum nexthop_types_t
  186. {
  187. NEXTHOP_TYPE_IFINDEX = 1, /* Directly connected. */
  188. NEXTHOP_TYPE_IFNAME, /* Interface route. */
  189. NEXTHOP_TYPE_IPV4, /* IPv4 nexthop. */
  190. NEXTHOP_TYPE_IPV4_IFINDEX, /* IPv4 nexthop with ifindex. */
  191. NEXTHOP_TYPE_IPV4_IFNAME, /* IPv4 nexthop with ifname. */
  192. NEXTHOP_TYPE_IPV6, /* IPv6 nexthop. */
  193. NEXTHOP_TYPE_IPV6_IFINDEX, /* IPv6 nexthop with ifindex. */
  194. NEXTHOP_TYPE_IPV6_IFNAME, /* IPv6 nexthop with ifname. */
  195. NEXTHOP_TYPE_BLACKHOLE, /* Null0 nexthop. */
  196. };
  197. /* Nexthop structure. */
  198. struct nexthop
  199. {
  200. struct nexthop *next;
  201. struct nexthop *prev;
  202. /* Interface index. */
  203. char *ifname;
  204. unsigned int ifindex;
  205. enum nexthop_types_t type;
  206. u_char flags;
  207. #define NEXTHOP_FLAG_ACTIVE (1 << 0) /* This nexthop is alive. */
  208. #define NEXTHOP_FLAG_FIB (1 << 1) /* FIB nexthop. */
  209. #define NEXTHOP_FLAG_RECURSIVE (1 << 2) /* Recursive nexthop. */
  210. /* Nexthop address or interface name. */
  211. union g_addr gate;
  212. /* Recursive lookup nexthop. */
  213. u_char rtype;
  214. unsigned int rifindex;
  215. union g_addr rgate;
  216. union g_addr src;
  217. };
  218. /* Routing table instance. */
  219. struct vrf
  220. {
  221. /* Identifier. This is same as routing table vector index. */
  222. u_int32_t id;
  223. /* Routing table name. */
  224. char *name;
  225. /* Description. */
  226. char *desc;
  227. /* FIB identifier. */
  228. u_char fib_id;
  229. /* Routing table. */
  230. struct route_table *table[AFI_MAX][SAFI_MAX];
  231. /* Static route configuration. */
  232. struct route_table *stable[AFI_MAX][SAFI_MAX];
  233. };
  234. /*
  235. * rib_table_info_t
  236. *
  237. * Structure that is hung off of a route_table that holds information about
  238. * the table.
  239. */
  240. typedef struct rib_table_info_t_
  241. {
  242. /*
  243. * Back pointer to vrf.
  244. */
  245. struct vrf *vrf;
  246. afi_t afi;
  247. safi_t safi;
  248. } rib_table_info_t;
  249. typedef enum
  250. {
  251. RIB_TABLES_ITER_S_INIT,
  252. RIB_TABLES_ITER_S_ITERATING,
  253. RIB_TABLES_ITER_S_DONE
  254. } rib_tables_iter_state_t;
  255. /*
  256. * Structure that holds state for iterating over all tables in the
  257. * Routing Information Base.
  258. */
  259. typedef struct rib_tables_iter_t_
  260. {
  261. uint32_t vrf_id;
  262. int afi_safi_ix;
  263. rib_tables_iter_state_t state;
  264. } rib_tables_iter_t;
  265. extern const char *nexthop_type_to_str (enum nexthop_types_t nh_type);
  266. extern struct nexthop *nexthop_ifindex_add (struct rib *, unsigned int);
  267. extern struct nexthop *nexthop_ifname_add (struct rib *, char *);
  268. extern struct nexthop *nexthop_blackhole_add (struct rib *);
  269. extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
  270. struct in_addr *);
  271. extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
  272. struct in_addr *,
  273. struct in_addr *,
  274. unsigned int);
  275. extern void rib_lookup_and_dump (struct prefix_ipv4 *);
  276. extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
  277. extern void rib_dump (const char *, const struct prefix_ipv4 *, const struct rib *);
  278. extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *);
  279. #define ZEBRA_RIB_LOOKUP_ERROR -1
  280. #define ZEBRA_RIB_FOUND_EXACT 0
  281. #define ZEBRA_RIB_FOUND_NOGATE 1
  282. #define ZEBRA_RIB_FOUND_CONNECTED 2
  283. #define ZEBRA_RIB_NOTFOUND 3
  284. #ifdef HAVE_IPV6
  285. extern struct nexthop *nexthop_ipv6_add (struct rib *, struct in6_addr *);
  286. #endif /* HAVE_IPV6 */
  287. extern struct vrf *vrf_lookup (u_int32_t);
  288. extern struct route_table *vrf_table (afi_t afi, safi_t safi, u_int32_t id);
  289. extern struct route_table *vrf_static_table (afi_t afi, safi_t safi, u_int32_t id);
  290. /* NOTE:
  291. * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
  292. * also implicitly withdraw equal prefix of same type. */
  293. extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  294. struct in_addr *gate, struct in_addr *src,
  295. unsigned int ifindex, u_int32_t vrf_id,
  296. u_int32_t, u_char, safi_t);
  297. extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
  298. extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  299. struct in_addr *gate, unsigned int ifindex,
  300. u_int32_t, safi_t safi);
  301. extern struct rib *rib_match_ipv4 (struct in_addr);
  302. extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *);
  303. extern void rib_update (void);
  304. extern void rib_weed_tables (void);
  305. extern void rib_sweep_route (void);
  306. extern void rib_close (void);
  307. extern void rib_init (void);
  308. extern unsigned long rib_score_proto (u_char proto);
  309. extern int
  310. static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
  311. u_char flags, u_char distance, u_int32_t vrf_id);
  312. extern int
  313. static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
  314. u_char distance, u_int32_t vrf_id);
  315. #ifdef HAVE_IPV6
  316. extern int
  317. rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  318. struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
  319. u_int32_t metric, u_char distance, safi_t safi);
  320. extern int
  321. rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  322. struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi);
  323. extern struct rib *rib_lookup_ipv6 (struct in6_addr *);
  324. extern struct rib *rib_match_ipv6 (struct in6_addr *);
  325. extern struct route_table *rib_table_ipv6;
  326. extern int
  327. static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  328. const char *ifname, u_char flags, u_char distance,
  329. u_int32_t vrf_id);
  330. extern int
  331. static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  332. const char *ifname, u_char distance, u_int32_t vrf_id);
  333. #endif /* HAVE_IPV6 */
  334. extern int rib_gc_dest (struct route_node *rn);
  335. extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);
  336. /*
  337. * Inline functions.
  338. */
  339. /*
  340. * rib_table_info
  341. */
  342. static inline rib_table_info_t *
  343. rib_table_info (struct route_table *table)
  344. {
  345. return (rib_table_info_t *) table->info;
  346. }
  347. /*
  348. * rib_dest_from_rnode
  349. */
  350. static inline rib_dest_t *
  351. rib_dest_from_rnode (struct route_node *rn)
  352. {
  353. return (rib_dest_t *) rn->info;
  354. }
  355. /*
  356. * rnode_to_ribs
  357. *
  358. * Returns a pointer to the list of routes corresponding to the given
  359. * route_node.
  360. */
  361. static inline struct rib *
  362. rnode_to_ribs (struct route_node *rn)
  363. {
  364. rib_dest_t *dest;
  365. dest = rib_dest_from_rnode (rn);
  366. if (!dest)
  367. return NULL;
  368. return dest->routes;
  369. }
  370. /*
  371. * rib_dest_prefix
  372. */
  373. static inline struct prefix *
  374. rib_dest_prefix (rib_dest_t *dest)
  375. {
  376. return &dest->rnode->p;
  377. }
  378. /*
  379. * rib_dest_af
  380. *
  381. * Returns the address family that the destination is for.
  382. */
  383. static inline u_char
  384. rib_dest_af (rib_dest_t *dest)
  385. {
  386. return dest->rnode->p.family;
  387. }
  388. /*
  389. * rib_dest_table
  390. */
  391. static inline struct route_table *
  392. rib_dest_table (rib_dest_t *dest)
  393. {
  394. return dest->rnode->table;
  395. }
  396. /*
  397. * rib_dest_vrf
  398. */
  399. static inline struct vrf *
  400. rib_dest_vrf (rib_dest_t *dest)
  401. {
  402. return rib_table_info (rib_dest_table (dest))->vrf;
  403. }
  404. /*
  405. * rib_tables_iter_init
  406. */
  407. static inline void
  408. rib_tables_iter_init (rib_tables_iter_t *iter)
  409. {
  410. memset (iter, 0, sizeof (*iter));
  411. iter->state = RIB_TABLES_ITER_S_INIT;
  412. }
  413. /*
  414. * rib_tables_iter_started
  415. *
  416. * Returns TRUE if this iterator has started iterating over the set of
  417. * tables.
  418. */
  419. static inline int
  420. rib_tables_iter_started (rib_tables_iter_t *iter)
  421. {
  422. return iter->state != RIB_TABLES_ITER_S_INIT;
  423. }
  424. /*
  425. * rib_tables_iter_cleanup
  426. */
  427. static inline void
  428. rib_tables_iter_cleanup (rib_tables_iter_t *iter)
  429. {
  430. iter->state = RIB_TABLES_ITER_S_DONE;
  431. }
  432. #endif /*_ZEBRA_RIB_H */