ip6_fib.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Linux INET6 implementation
  3. *
  4. * Authors:
  5. * Pedro Roque <roque@di.fc.ul.pt>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #ifndef _IP6_FIB_H
  13. #define _IP6_FIB_H
  14. #include <linux/ipv6_route.h>
  15. #include <linux/rtnetlink.h>
  16. #include <linux/spinlock.h>
  17. #include <net/dst.h>
  18. #include <net/flow.h>
  19. #include <net/netlink.h>
  20. #include <net/inetpeer.h>
  21. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  22. #define FIB6_TABLE_HASHSZ 256
  23. #else
  24. #define FIB6_TABLE_HASHSZ 1
  25. #endif
  26. struct rt6_info;
  27. struct fib6_config {
  28. u32 fc_table;
  29. u32 fc_metric;
  30. int fc_dst_len;
  31. int fc_src_len;
  32. int fc_ifindex;
  33. u32 fc_flags;
  34. u32 fc_protocol;
  35. struct in6_addr fc_dst;
  36. struct in6_addr fc_src;
  37. struct in6_addr fc_prefsrc;
  38. struct in6_addr fc_gateway;
  39. unsigned long fc_expires;
  40. struct nlattr *fc_mx;
  41. int fc_mx_len;
  42. struct nl_info fc_nlinfo;
  43. };
  44. struct fib6_node {
  45. struct fib6_node *parent;
  46. struct fib6_node *left;
  47. struct fib6_node *right;
  48. #ifdef CONFIG_IPV6_SUBTREES
  49. struct fib6_node *subtree;
  50. #endif
  51. struct rt6_info *leaf;
  52. __u16 fn_bit; /* bit key */
  53. __u16 fn_flags;
  54. __u32 fn_sernum;
  55. struct rt6_info *rr_ptr;
  56. };
  57. #ifndef CONFIG_IPV6_SUBTREES
  58. #define FIB6_SUBTREE(fn) NULL
  59. #else
  60. #define FIB6_SUBTREE(fn) ((fn)->subtree)
  61. #endif
  62. /*
  63. * routing information
  64. *
  65. */
  66. struct rt6key {
  67. struct in6_addr addr;
  68. int plen;
  69. };
  70. struct fib6_table;
  71. struct rt6_info {
  72. struct dst_entry dst;
  73. /*
  74. * Tail elements of dst_entry (__refcnt etc.)
  75. * and these elements (rarely used in hot path) are in
  76. * the same cache line.
  77. */
  78. struct fib6_table *rt6i_table;
  79. struct fib6_node *rt6i_node;
  80. struct in6_addr rt6i_gateway;
  81. atomic_t rt6i_ref;
  82. /* These are in a separate cache line. */
  83. struct rt6key rt6i_dst ____cacheline_aligned_in_smp;
  84. u32 rt6i_flags;
  85. struct rt6key rt6i_src;
  86. struct rt6key rt6i_prefsrc;
  87. u32 rt6i_metric;
  88. u32 rt6i_peer_genid;
  89. struct inet6_dev *rt6i_idev;
  90. struct inet_peer *rt6i_peer;
  91. #ifdef CONFIG_XFRM
  92. u32 rt6i_flow_cache_genid;
  93. #endif
  94. /* more non-fragment space at head required */
  95. unsigned short rt6i_nfheader_len;
  96. u8 rt6i_protocol;
  97. };
  98. static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
  99. {
  100. return ((struct rt6_info *)dst)->rt6i_idev;
  101. }
  102. static inline void rt6_clean_expires(struct rt6_info *rt)
  103. {
  104. if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from)
  105. dst_release(rt->dst.from);
  106. rt->rt6i_flags &= ~RTF_EXPIRES;
  107. rt->dst.from = NULL;
  108. }
  109. static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
  110. {
  111. if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from)
  112. dst_release(rt->dst.from);
  113. rt->rt6i_flags |= RTF_EXPIRES;
  114. rt->dst.expires = expires;
  115. }
  116. static inline void rt6_update_expires(struct rt6_info *rt, int timeout)
  117. {
  118. if (!(rt->rt6i_flags & RTF_EXPIRES)) {
  119. if (rt->dst.from)
  120. dst_release(rt->dst.from);
  121. /* dst_set_expires relies on expires == 0
  122. * if it has not been set previously.
  123. */
  124. rt->dst.expires = 0;
  125. }
  126. dst_set_expires(&rt->dst, timeout);
  127. rt->rt6i_flags |= RTF_EXPIRES;
  128. }
  129. static inline void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
  130. {
  131. struct dst_entry *new = (struct dst_entry *) from;
  132. if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from) {
  133. if (new == rt->dst.from)
  134. return;
  135. dst_release(rt->dst.from);
  136. }
  137. rt->rt6i_flags &= ~RTF_EXPIRES;
  138. rt->dst.from = new;
  139. dst_hold(new);
  140. }
  141. struct fib6_walker_t {
  142. struct list_head lh;
  143. struct fib6_node *root, *node;
  144. struct rt6_info *leaf;
  145. unsigned char state;
  146. unsigned char prune;
  147. unsigned int skip;
  148. unsigned int count;
  149. int (*func)(struct fib6_walker_t *);
  150. void *args;
  151. };
  152. struct rt6_statistics {
  153. __u32 fib_nodes;
  154. __u32 fib_route_nodes;
  155. __u32 fib_rt_alloc; /* permanent routes */
  156. __u32 fib_rt_entries; /* rt entries in table */
  157. __u32 fib_rt_cache; /* cache routes */
  158. __u32 fib_discarded_routes;
  159. };
  160. #define RTN_TL_ROOT 0x0001
  161. #define RTN_ROOT 0x0002 /* tree root node */
  162. #define RTN_RTINFO 0x0004 /* node with valid routing info */
  163. /*
  164. * priority levels (or metrics)
  165. *
  166. */
  167. struct fib6_table {
  168. struct hlist_node tb6_hlist;
  169. u32 tb6_id;
  170. rwlock_t tb6_lock;
  171. struct fib6_node tb6_root;
  172. };
  173. #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
  174. #define RT6_TABLE_MAIN RT_TABLE_MAIN
  175. #define RT6_TABLE_DFLT RT6_TABLE_MAIN
  176. #define RT6_TABLE_INFO RT6_TABLE_MAIN
  177. #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
  178. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  179. #define FIB6_TABLE_MIN 1
  180. #define FIB6_TABLE_MAX RT_TABLE_MAX
  181. #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
  182. #else
  183. #define FIB6_TABLE_MIN RT_TABLE_MAIN
  184. #define FIB6_TABLE_MAX FIB6_TABLE_MIN
  185. #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
  186. #endif
  187. typedef struct rt6_info *(*pol_lookup_t)(struct net *,
  188. struct fib6_table *,
  189. struct flowi6 *, int);
  190. /*
  191. * exported functions
  192. */
  193. extern struct fib6_table *fib6_get_table(struct net *net, u32 id);
  194. extern struct fib6_table *fib6_new_table(struct net *net, u32 id);
  195. extern struct dst_entry *fib6_rule_lookup(struct net *net,
  196. struct flowi6 *fl6, int flags,
  197. pol_lookup_t lookup);
  198. extern struct fib6_node *fib6_lookup(struct fib6_node *root,
  199. const struct in6_addr *daddr,
  200. const struct in6_addr *saddr);
  201. struct fib6_node *fib6_locate(struct fib6_node *root,
  202. const struct in6_addr *daddr, int dst_len,
  203. const struct in6_addr *saddr, int src_len);
  204. extern void fib6_clean_all_ro(struct net *net,
  205. int (*func)(struct rt6_info *, void *arg),
  206. int prune, void *arg);
  207. extern void fib6_clean_all(struct net *net,
  208. int (*func)(struct rt6_info *, void *arg),
  209. int prune, void *arg);
  210. extern int fib6_add(struct fib6_node *root,
  211. struct rt6_info *rt,
  212. struct nl_info *info);
  213. extern int fib6_del(struct rt6_info *rt,
  214. struct nl_info *info);
  215. extern void inet6_rt_notify(int event, struct rt6_info *rt,
  216. struct nl_info *info);
  217. extern void fib6_run_gc(unsigned long expires,
  218. struct net *net);
  219. extern void fib6_gc_cleanup(void);
  220. extern int fib6_init(void);
  221. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  222. extern int fib6_rules_init(void);
  223. extern void fib6_rules_cleanup(void);
  224. #else
  225. static inline int fib6_rules_init(void)
  226. {
  227. return 0;
  228. }
  229. static inline void fib6_rules_cleanup(void)
  230. {
  231. return ;
  232. }
  233. #endif
  234. #endif