rib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 "linklist.h"
  25. #include "prefix.h"
  26. #include "table.h"
  27. #include "queue.h"
  28. #include "nexthop.h"
  29. #define DISTANCE_INFINITY 255
  30. struct rib
  31. {
  32. /* Link list. */
  33. struct rib *next;
  34. struct rib *prev;
  35. /* Nexthop structure */
  36. struct nexthop *nexthop;
  37. /* Refrence count. */
  38. unsigned long refcnt;
  39. /* Uptime. */
  40. time_t uptime;
  41. /* Type fo this route. */
  42. int type;
  43. /* VRF identifier. */
  44. vrf_id_t vrf_id;
  45. /* Which routing table */
  46. int table;
  47. /* Metric */
  48. u_int32_t metric;
  49. /* MTU */
  50. u_int32_t mtu;
  51. u_int32_t nexthop_mtu;
  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. #define RIB_ENTRY_CHANGED (1 << 1)
  63. #define RIB_ENTRY_SELECTED_FIB (1 << 2)
  64. /* Nexthop information. */
  65. u_char nexthop_num;
  66. u_char nexthop_active_num;
  67. u_char nexthop_fib_num;
  68. };
  69. /* meta-queue structure:
  70. * sub-queue 0: connected, kernel
  71. * sub-queue 1: static
  72. * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
  73. * sub-queue 3: iBGP, eBGP
  74. * sub-queue 4: any other origin (if any)
  75. */
  76. #define MQ_SIZE 5
  77. struct meta_queue
  78. {
  79. struct list *subq[MQ_SIZE];
  80. u_int32_t size; /* sum of lengths of all subqueues */
  81. };
  82. /*
  83. * Structure that represents a single destination (prefix).
  84. */
  85. typedef struct rib_dest_t_
  86. {
  87. /*
  88. * Back pointer to the route node for this destination. This helps
  89. * us get to the prefix that this structure is for.
  90. */
  91. struct route_node *rnode;
  92. /*
  93. * Doubly-linked list of routes for this prefix.
  94. */
  95. struct rib *routes;
  96. /*
  97. * Flags, see below.
  98. */
  99. u_int32_t flags;
  100. /*
  101. * Linkage to put dest on the FPM processing queue.
  102. */
  103. TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
  104. } rib_dest_t;
  105. #define RIB_ROUTE_QUEUED(x) (1 << (x))
  106. /*
  107. * The maximum qindex that can be used.
  108. */
  109. #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
  110. /*
  111. * This flag indicates that a given prefix has been 'advertised' to
  112. * the FPM to be installed in the forwarding plane.
  113. */
  114. #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
  115. /*
  116. * This flag is set when we need to send an update to the FPM about a
  117. * dest.
  118. */
  119. #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
  120. /*
  121. * Macro to iterate over each route for a destination (prefix).
  122. */
  123. #define RIB_DEST_FOREACH_ROUTE(dest, rib) \
  124. for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
  125. /*
  126. * Same as above, but allows the current node to be unlinked.
  127. */
  128. #define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
  129. for ((rib) = (dest) ? (dest)->routes : NULL; \
  130. (rib) && ((next) = (rib)->next, 1); \
  131. (rib) = (next))
  132. #define RNODE_FOREACH_RIB(rn, rib) \
  133. RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)
  134. #define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
  135. RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)
  136. /* Static route information. */
  137. struct static_route
  138. {
  139. /* For linked list. */
  140. struct static_route *prev;
  141. struct static_route *next;
  142. /* VRF identifier. */
  143. vrf_id_t vrf_id;
  144. /* Administrative distance. */
  145. u_char distance;
  146. /* Flag for this static route's type. */
  147. u_char type;
  148. #define STATIC_IPV4_GATEWAY 1
  149. #define STATIC_IPV4_IFNAME 2
  150. #define STATIC_IPV4_BLACKHOLE 3
  151. #define STATIC_IPV6_GATEWAY 4
  152. #define STATIC_IPV6_GATEWAY_IFNAME 5
  153. #define STATIC_IPV6_IFNAME 6
  154. /* Nexthop value. */
  155. union g_addr addr;
  156. char *ifname;
  157. /* bit flags */
  158. u_char flags;
  159. /*
  160. see ZEBRA_FLAG_REJECT
  161. ZEBRA_FLAG_BLACKHOLE
  162. */
  163. };
  164. /* The following for loop allows to iterate over the nexthop
  165. * structure of routes.
  166. *
  167. * We have to maintain quite a bit of state:
  168. *
  169. * nexthop: The pointer to the current nexthop, either in the
  170. * top-level chain or in the resolved chain of ni.
  171. * tnexthop: The pointer to the current nexthop in the top-level
  172. * nexthop chain.
  173. * recursing: Information if nh currently is in the top-level chain
  174. * (0) or in a resolved chain (1).
  175. *
  176. * Initialization: Set `nexthop' and `tnexthop' to the head of the
  177. * top-level chain. As nexthop is in the top level chain, set recursing
  178. * to 0.
  179. *
  180. * Iteration check: Check that the `nexthop' pointer is not NULL.
  181. *
  182. * Iteration step: This is the tricky part. Check if `nexthop' has
  183. * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' is in
  184. * the top level chain and has at least one nexthop attached to
  185. * `nexthop->resolved'. As we want to descend into `nexthop->resolved',
  186. * set `recursing' to 1 and set `nexthop' to `nexthop->resolved'.
  187. * `tnexthop' is left alone in that case so we can remember which nexthop
  188. * in the top level chain we are currently handling.
  189. *
  190. * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
  191. * current chain. If we are recursing, `nexthop' will be set to
  192. * `nexthop->next' and `tnexthop' will be left alone. If we are not
  193. * recursing, both `tnexthop' and `nexthop' will be set to `nexthop->next'
  194. * as we are progressing in the top level chain.
  195. * If we encounter `nexthop->next == NULL', we will clear the `recursing'
  196. * flag as we arived either at the end of the resolved chain or at the end
  197. * of the top level chain. In both cases, we set `tnexthop' and `nexthop'
  198. * to `tnexthop->next', progressing to the next position in the top-level
  199. * chain and possibly to its end marked by NULL.
  200. */
  201. #define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
  202. (tnexthop) = (nexthop) = (head), (recursing) = 0; \
  203. (nexthop); \
  204. (nexthop) = CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \
  205. ? (((recursing) = 1), (nexthop)->resolved) \
  206. : ((nexthop)->next ? ((recursing) ? (nexthop)->next \
  207. : ((tnexthop) = (nexthop)->next)) \
  208. : (((recursing) = 0),((tnexthop) = (tnexthop)->next)))
  209. /* Structure holding nexthop & VRF identifier,
  210. * used for applying the route-map. */
  211. struct nexthop_vrfid
  212. {
  213. struct nexthop *nexthop;
  214. vrf_id_t vrf_id;
  215. };
  216. #if defined (HAVE_RTADV)
  217. /* Structure which hold status of router advertisement. */
  218. struct rtadv
  219. {
  220. int sock;
  221. int adv_if_count;
  222. int adv_msec_if_count;
  223. struct thread *ra_read;
  224. struct thread *ra_timer;
  225. };
  226. #endif /* HAVE_RTADV */
  227. #ifdef HAVE_NETLINK
  228. /* Socket interface to kernel */
  229. struct nlsock
  230. {
  231. int sock;
  232. int seq;
  233. struct sockaddr_nl snl;
  234. const char *name;
  235. };
  236. #endif
  237. /* Routing table instance. */
  238. struct zebra_vrf
  239. {
  240. /* Identifier. */
  241. vrf_id_t vrf_id;
  242. /* Routing table name. */
  243. char *name;
  244. /* Description. */
  245. char *desc;
  246. /* FIB identifier. */
  247. u_char fib_id;
  248. /* Routing table. */
  249. struct route_table *table[AFI_MAX][SAFI_MAX];
  250. /* Static route configuration. */
  251. struct route_table *stable[AFI_MAX][SAFI_MAX];
  252. #ifdef HAVE_NETLINK
  253. struct nlsock netlink; /* kernel messages */
  254. struct nlsock netlink_cmd; /* command channel */
  255. struct thread *t_netlink;
  256. #endif
  257. /* 2nd pointer type used primarily to quell a warning on
  258. * ALL_LIST_ELEMENTS_RO
  259. */
  260. struct list _rid_all_sorted_list;
  261. struct list _rid_lo_sorted_list;
  262. struct list *rid_all_sorted_list;
  263. struct list *rid_lo_sorted_list;
  264. struct prefix rid_user_assigned;
  265. #if defined (HAVE_RTADV)
  266. struct rtadv rtadv;
  267. #endif /* HAVE_RTADV */
  268. /* Recursive Nexthop table */
  269. struct route_table *rnh_table[AFI_MAX];
  270. };
  271. /*
  272. * rib_table_info_t
  273. *
  274. * Structure that is hung off of a route_table that holds information about
  275. * the table.
  276. */
  277. typedef struct rib_table_info_t_
  278. {
  279. /*
  280. * Back pointer to zebra_vrf.
  281. */
  282. struct zebra_vrf *zvrf;
  283. afi_t afi;
  284. safi_t safi;
  285. } rib_table_info_t;
  286. typedef enum
  287. {
  288. RIB_TABLES_ITER_S_INIT,
  289. RIB_TABLES_ITER_S_ITERATING,
  290. RIB_TABLES_ITER_S_DONE
  291. } rib_tables_iter_state_t;
  292. /*
  293. * Structure that holds state for iterating over all tables in the
  294. * Routing Information Base.
  295. */
  296. typedef struct rib_tables_iter_t_
  297. {
  298. vrf_id_t vrf_id;
  299. int afi_safi_ix;
  300. rib_tables_iter_state_t state;
  301. } rib_tables_iter_t;
  302. /* RPF lookup behaviour */
  303. enum multicast_mode
  304. {
  305. MCAST_NO_CONFIG = 0, /* MIX_MRIB_FIRST, but no show in config write */
  306. MCAST_MRIB_ONLY, /* MRIB only */
  307. MCAST_URIB_ONLY, /* URIB only */
  308. MCAST_MIX_MRIB_FIRST, /* MRIB, if nothing at all then URIB */
  309. MCAST_MIX_DISTANCE, /* MRIB & URIB, lower distance wins */
  310. MCAST_MIX_PFXLEN, /* MRIB & URIB, longer prefix wins */
  311. /* on equal value, MRIB wins for last 2 */
  312. };
  313. extern void multicast_mode_ipv4_set (enum multicast_mode mode);
  314. extern enum multicast_mode multicast_mode_ipv4_get (void);
  315. extern const char *nexthop_type_to_str (enum nexthop_types_t nh_type);
  316. extern struct nexthop *rib_nexthop_ifindex_add (struct rib *, ifindex_t);
  317. extern struct nexthop *rib_nexthop_ifname_add (struct rib *, char *);
  318. extern struct nexthop *rib_nexthop_blackhole_add (struct rib *);
  319. extern struct nexthop *rib_nexthop_ipv4_add (struct rib *, struct in_addr *,
  320. struct in_addr *);
  321. extern struct nexthop *rib_nexthop_ipv4_ifindex_add (struct rib *,
  322. struct in_addr *,
  323. struct in_addr *,
  324. ifindex_t);
  325. extern void rib_nexthop_add (struct rib *rib, struct nexthop *nexthop);
  326. extern int nexthop_has_fib_child(struct nexthop *);
  327. extern void rib_lookup_and_dump (struct prefix_ipv4 *);
  328. #define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib)
  329. extern void _rib_dump (const char *,
  330. union prefix46constptr, const struct rib *);
  331. extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *,
  332. vrf_id_t);
  333. #define ZEBRA_RIB_LOOKUP_ERROR -1
  334. #define ZEBRA_RIB_FOUND_EXACT 0
  335. #define ZEBRA_RIB_FOUND_NOGATE 1
  336. #define ZEBRA_RIB_FOUND_CONNECTED 2
  337. #define ZEBRA_RIB_NOTFOUND 3
  338. extern struct nexthop *rib_nexthop_ipv6_add (struct rib *, struct in6_addr *);
  339. extern struct nexthop *rib_nexthop_ipv6_ifindex_add (struct rib *,
  340. struct in6_addr *,
  341. ifindex_t);
  342. extern struct zebra_vrf *zebra_vrf_lookup (vrf_id_t vrf_id);
  343. extern struct zebra_vrf *zebra_vrf_alloc (vrf_id_t);
  344. extern struct route_table *zebra_vrf_table (afi_t, safi_t, vrf_id_t);
  345. extern struct route_table *zebra_vrf_static_table (afi_t, safi_t, vrf_id_t);
  346. /* NOTE:
  347. * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
  348. * also implicitly withdraw equal prefix of same type. */
  349. extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  350. struct in_addr *gate, struct in_addr *src,
  351. ifindex_t ifindex, vrf_id_t vrf_id, int table_id,
  352. u_int32_t, u_int32_t, u_char, safi_t);
  353. extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
  354. extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  355. struct in_addr *gate, ifindex_t ifindex,
  356. vrf_id_t, safi_t safi);
  357. extern struct rib *rib_match_ipv4_safi (struct in_addr addr, safi_t safi,
  358. int skip_bgp, struct route_node **rn_out,
  359. vrf_id_t);
  360. extern struct rib *rib_match_ipv4_multicast (struct in_addr addr,
  361. struct route_node **rn_out,
  362. vrf_id_t);
  363. extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *, vrf_id_t);
  364. extern void rib_update (vrf_id_t);
  365. extern void rib_weed_tables (void);
  366. extern void rib_sweep_route (void);
  367. extern void rib_close_table (struct route_table *);
  368. extern void rib_close (void);
  369. extern void rib_init (void);
  370. extern unsigned long rib_score_proto (u_char proto);
  371. extern int
  372. static_add_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate,
  373. const char *ifname, u_char flags, u_char distance,
  374. vrf_id_t vrf_id);
  375. extern int
  376. static_delete_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate,
  377. const char *ifname, u_char distance, vrf_id_t vrf_id);
  378. extern int
  379. rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  380. struct in6_addr *gate, ifindex_t ifindex, vrf_id_t vrf_id,
  381. int table_id, u_int32_t metric, u_int32_t mtu,
  382. u_char distance, safi_t safi);
  383. extern int
  384. rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  385. struct in6_addr *gate, ifindex_t ifindex, vrf_id_t vrf_id, safi_t safi);
  386. extern struct rib *rib_lookup_ipv6 (struct in6_addr *, vrf_id_t);
  387. extern struct rib *rib_match_ipv6 (struct in6_addr *, vrf_id_t);
  388. extern struct route_table *rib_table_ipv6;
  389. extern int
  390. static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  391. const char *ifname, u_char flags, u_char distance,
  392. vrf_id_t vrf_id);
  393. extern int
  394. rib_add_ipv6_multipath (struct prefix_ipv6 *, struct rib *, safi_t);
  395. extern int
  396. static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  397. const char *ifname, u_char distance, vrf_id_t vrf_id);
  398. extern int rib_gc_dest (struct route_node *rn);
  399. extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);
  400. /*
  401. * Inline functions.
  402. */
  403. /*
  404. * rib_table_info
  405. */
  406. static inline rib_table_info_t *
  407. rib_table_info (struct route_table *table)
  408. {
  409. return (rib_table_info_t *) table->info;
  410. }
  411. /*
  412. * rib_dest_from_rnode
  413. */
  414. static inline rib_dest_t *
  415. rib_dest_from_rnode (struct route_node *rn)
  416. {
  417. return (rib_dest_t *) rn->info;
  418. }
  419. /*
  420. * rnode_to_ribs
  421. *
  422. * Returns a pointer to the list of routes corresponding to the given
  423. * route_node.
  424. */
  425. static inline struct rib *
  426. rnode_to_ribs (struct route_node *rn)
  427. {
  428. rib_dest_t *dest;
  429. dest = rib_dest_from_rnode (rn);
  430. if (!dest)
  431. return NULL;
  432. return dest->routes;
  433. }
  434. /*
  435. * rib_dest_prefix
  436. */
  437. static inline struct prefix *
  438. rib_dest_prefix (rib_dest_t *dest)
  439. {
  440. return &dest->rnode->p;
  441. }
  442. /*
  443. * rib_dest_af
  444. *
  445. * Returns the address family that the destination is for.
  446. */
  447. static inline u_char
  448. rib_dest_af (rib_dest_t *dest)
  449. {
  450. return dest->rnode->p.family;
  451. }
  452. /*
  453. * rib_dest_table
  454. */
  455. static inline struct route_table *
  456. rib_dest_table (rib_dest_t *dest)
  457. {
  458. return dest->rnode->table;
  459. }
  460. /*
  461. * rib_dest_vrf
  462. */
  463. static inline struct zebra_vrf *
  464. rib_dest_vrf (rib_dest_t *dest)
  465. {
  466. return rib_table_info (rib_dest_table (dest))->zvrf;
  467. }
  468. /*
  469. * rib_tables_iter_init
  470. */
  471. static inline void
  472. rib_tables_iter_init (rib_tables_iter_t *iter)
  473. {
  474. memset (iter, 0, sizeof (*iter));
  475. iter->state = RIB_TABLES_ITER_S_INIT;
  476. }
  477. /*
  478. * rib_tables_iter_started
  479. *
  480. * Returns TRUE if this iterator has started iterating over the set of
  481. * tables.
  482. */
  483. static inline int
  484. rib_tables_iter_started (rib_tables_iter_t *iter)
  485. {
  486. return iter->state != RIB_TABLES_ITER_S_INIT;
  487. }
  488. /*
  489. * rib_tables_iter_cleanup
  490. */
  491. static inline void
  492. rib_tables_iter_cleanup (rib_tables_iter_t *iter)
  493. {
  494. iter->state = RIB_TABLES_ITER_S_DONE;
  495. }
  496. #endif /*_ZEBRA_RIB_H */