rib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. #define NEXTHOP_FLAG_ONLINK (1 << 3) /* Nexthop should be installed onlink. */
  211. /* Nexthop address */
  212. union g_addr gate;
  213. union g_addr src;
  214. /* Nexthops obtained by recursive resolution.
  215. *
  216. * If the nexthop struct needs to be resolved recursively,
  217. * NEXTHOP_FLAG_RECURSIVE will be set in flags and the nexthops
  218. * obtained by recursive resolution will be added to `resolved'.
  219. * Only one level of recursive resolution is currently supported. */
  220. struct nexthop *resolved;
  221. };
  222. /* The following for loop allows to iterate over the nexthop
  223. * structure of routes.
  224. *
  225. * We have to maintain quite a bit of state:
  226. *
  227. * nexthop: The pointer to the current nexthop, either in the
  228. * top-level chain or in the resolved chain of ni.
  229. * tnexthop: The pointer to the current nexthop in the top-level
  230. * nexthop chain.
  231. * recursing: Information if nh currently is in the top-level chain
  232. * (0) or in a resolved chain (1).
  233. *
  234. * Initialization: Set `nexthop' and `tnexthop' to the head of the
  235. * top-level chain. As nexthop is in the top level chain, set recursing
  236. * to 0.
  237. *
  238. * Iteration check: Check that the `nexthop' pointer is not NULL.
  239. *
  240. * Iteration step: This is the tricky part. Check if `nexthop' has
  241. * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' is in
  242. * the top level chain and has at least one nexthop attached to
  243. * `nexthop->resolved'. As we want to descend into `nexthop->resolved',
  244. * set `recursing' to 1 and set `nexthop' to `nexthop->resolved'.
  245. * `tnexthop' is left alone in that case so we can remember which nexthop
  246. * in the top level chain we are currently handling.
  247. *
  248. * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
  249. * current chain. If we are recursing, `nexthop' will be set to
  250. * `nexthop->next' and `tnexthop' will be left alone. If we are not
  251. * recursing, both `tnexthop' and `nexthop' will be set to `nexthop->next'
  252. * as we are progressing in the top level chain.
  253. * If we encounter `nexthop->next == NULL', we will clear the `recursing'
  254. * flag as we arived either at the end of the resolved chain or at the end
  255. * of the top level chain. In both cases, we set `tnexthop' and `nexthop'
  256. * to `tnexthop->next', progressing to the next position in the top-level
  257. * chain and possibly to its end marked by NULL.
  258. */
  259. #define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
  260. (tnexthop) = (nexthop) = (head), (recursing) = 0; \
  261. (nexthop); \
  262. (nexthop) = CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \
  263. ? (((recursing) = 1), (nexthop)->resolved) \
  264. : ((nexthop)->next ? ((recursing) ? (nexthop)->next \
  265. : ((tnexthop) = (nexthop)->next)) \
  266. : (((recursing) = 0),((tnexthop) = (tnexthop)->next)))
  267. /* Routing table instance. */
  268. struct vrf
  269. {
  270. /* Identifier. This is same as routing table vector index. */
  271. u_int32_t id;
  272. /* Routing table name. */
  273. char *name;
  274. /* Description. */
  275. char *desc;
  276. /* FIB identifier. */
  277. u_char fib_id;
  278. /* Routing table. */
  279. struct route_table *table[AFI_MAX][SAFI_MAX];
  280. /* Static route configuration. */
  281. struct route_table *stable[AFI_MAX][SAFI_MAX];
  282. };
  283. /*
  284. * rib_table_info_t
  285. *
  286. * Structure that is hung off of a route_table that holds information about
  287. * the table.
  288. */
  289. typedef struct rib_table_info_t_
  290. {
  291. /*
  292. * Back pointer to vrf.
  293. */
  294. struct vrf *vrf;
  295. afi_t afi;
  296. safi_t safi;
  297. } rib_table_info_t;
  298. typedef enum
  299. {
  300. RIB_TABLES_ITER_S_INIT,
  301. RIB_TABLES_ITER_S_ITERATING,
  302. RIB_TABLES_ITER_S_DONE
  303. } rib_tables_iter_state_t;
  304. /*
  305. * Structure that holds state for iterating over all tables in the
  306. * Routing Information Base.
  307. */
  308. typedef struct rib_tables_iter_t_
  309. {
  310. uint32_t vrf_id;
  311. int afi_safi_ix;
  312. rib_tables_iter_state_t state;
  313. } rib_tables_iter_t;
  314. extern const char *nexthop_type_to_str (enum nexthop_types_t nh_type);
  315. extern struct nexthop *nexthop_ifindex_add (struct rib *, unsigned int);
  316. extern struct nexthop *nexthop_ifname_add (struct rib *, char *);
  317. extern struct nexthop *nexthop_blackhole_add (struct rib *);
  318. extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
  319. struct in_addr *);
  320. extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
  321. struct in_addr *,
  322. struct in_addr *,
  323. unsigned int);
  324. extern int nexthop_has_fib_child(struct nexthop *);
  325. extern void rib_lookup_and_dump (struct prefix_ipv4 *);
  326. extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
  327. #define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib)
  328. extern void _rib_dump (const char *,
  329. union prefix46constptr, const struct rib *);
  330. extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *);
  331. #define ZEBRA_RIB_LOOKUP_ERROR -1
  332. #define ZEBRA_RIB_FOUND_EXACT 0
  333. #define ZEBRA_RIB_FOUND_NOGATE 1
  334. #define ZEBRA_RIB_FOUND_CONNECTED 2
  335. #define ZEBRA_RIB_NOTFOUND 3
  336. #ifdef HAVE_IPV6
  337. extern struct nexthop *nexthop_ipv6_add (struct rib *, struct in6_addr *);
  338. #endif /* HAVE_IPV6 */
  339. extern struct vrf *vrf_lookup (u_int32_t);
  340. extern struct route_table *vrf_table (afi_t afi, safi_t safi, u_int32_t id);
  341. extern struct route_table *vrf_static_table (afi_t afi, safi_t safi, u_int32_t id);
  342. /* NOTE:
  343. * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
  344. * also implicitly withdraw equal prefix of same type. */
  345. extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  346. struct in_addr *gate, struct in_addr *src,
  347. unsigned int ifindex, u_int32_t vrf_id,
  348. u_int32_t, u_char, safi_t);
  349. extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
  350. extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  351. struct in_addr *gate, unsigned int ifindex,
  352. u_int32_t, safi_t safi);
  353. extern struct rib *rib_match_ipv4_safi (struct in_addr addr, safi_t safi, int skip_bgp);
  354. extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *);
  355. extern void rib_update (void);
  356. extern void rib_weed_tables (void);
  357. extern void rib_sweep_route (void);
  358. extern void rib_close (void);
  359. extern void rib_init (void);
  360. extern unsigned long rib_score_proto (u_char proto);
  361. extern int
  362. static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
  363. u_char flags, u_char distance, u_int32_t vrf_id);
  364. extern int
  365. static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
  366. u_char distance, u_int32_t vrf_id);
  367. #ifdef HAVE_IPV6
  368. extern int
  369. rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  370. struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
  371. u_int32_t metric, u_char distance, safi_t safi);
  372. extern int
  373. rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  374. struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi);
  375. extern struct rib *rib_lookup_ipv6 (struct in6_addr *);
  376. extern struct rib *rib_match_ipv6 (struct in6_addr *);
  377. extern struct route_table *rib_table_ipv6;
  378. extern int
  379. static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  380. const char *ifname, u_char flags, u_char distance,
  381. u_int32_t vrf_id);
  382. extern int
  383. static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  384. const char *ifname, u_char distance, u_int32_t vrf_id);
  385. #endif /* HAVE_IPV6 */
  386. extern int rib_gc_dest (struct route_node *rn);
  387. extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);
  388. /*
  389. * Inline functions.
  390. */
  391. /*
  392. * rib_table_info
  393. */
  394. static inline rib_table_info_t *
  395. rib_table_info (struct route_table *table)
  396. {
  397. return (rib_table_info_t *) table->info;
  398. }
  399. /*
  400. * rib_dest_from_rnode
  401. */
  402. static inline rib_dest_t *
  403. rib_dest_from_rnode (struct route_node *rn)
  404. {
  405. return (rib_dest_t *) rn->info;
  406. }
  407. /*
  408. * rnode_to_ribs
  409. *
  410. * Returns a pointer to the list of routes corresponding to the given
  411. * route_node.
  412. */
  413. static inline struct rib *
  414. rnode_to_ribs (struct route_node *rn)
  415. {
  416. rib_dest_t *dest;
  417. dest = rib_dest_from_rnode (rn);
  418. if (!dest)
  419. return NULL;
  420. return dest->routes;
  421. }
  422. /*
  423. * rib_dest_prefix
  424. */
  425. static inline struct prefix *
  426. rib_dest_prefix (rib_dest_t *dest)
  427. {
  428. return &dest->rnode->p;
  429. }
  430. /*
  431. * rib_dest_af
  432. *
  433. * Returns the address family that the destination is for.
  434. */
  435. static inline u_char
  436. rib_dest_af (rib_dest_t *dest)
  437. {
  438. return dest->rnode->p.family;
  439. }
  440. /*
  441. * rib_dest_table
  442. */
  443. static inline struct route_table *
  444. rib_dest_table (rib_dest_t *dest)
  445. {
  446. return dest->rnode->table;
  447. }
  448. /*
  449. * rib_dest_vrf
  450. */
  451. static inline struct vrf *
  452. rib_dest_vrf (rib_dest_t *dest)
  453. {
  454. return rib_table_info (rib_dest_table (dest))->vrf;
  455. }
  456. /*
  457. * rib_tables_iter_init
  458. */
  459. static inline void
  460. rib_tables_iter_init (rib_tables_iter_t *iter)
  461. {
  462. memset (iter, 0, sizeof (*iter));
  463. iter->state = RIB_TABLES_ITER_S_INIT;
  464. }
  465. /*
  466. * rib_tables_iter_started
  467. *
  468. * Returns TRUE if this iterator has started iterating over the set of
  469. * tables.
  470. */
  471. static inline int
  472. rib_tables_iter_started (rib_tables_iter_t *iter)
  473. {
  474. return iter->state != RIB_TABLES_ITER_S_INIT;
  475. }
  476. /*
  477. * rib_tables_iter_cleanup
  478. */
  479. static inline void
  480. rib_tables_iter_cleanup (rib_tables_iter_t *iter)
  481. {
  482. iter->state = RIB_TABLES_ITER_S_DONE;
  483. }
  484. #endif /*_ZEBRA_RIB_H */