ip6_fib.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. u32 fc_type; /* only 8 bits are used */
  36. struct in6_addr fc_dst;
  37. struct in6_addr fc_src;
  38. struct in6_addr fc_prefsrc;
  39. struct in6_addr fc_gateway;
  40. unsigned long fc_expires;
  41. struct nlattr *fc_mx;
  42. int fc_mx_len;
  43. int fc_mp_len;
  44. struct nlattr *fc_mp;
  45. struct nl_info fc_nlinfo;
  46. };
  47. struct fib6_node {
  48. struct fib6_node *parent;
  49. struct fib6_node *left;
  50. struct fib6_node *right;
  51. #ifdef CONFIG_IPV6_SUBTREES
  52. struct fib6_node *subtree;
  53. #endif
  54. struct rt6_info *leaf;
  55. __u16 fn_bit; /* bit key */
  56. __u16 fn_flags;
  57. int fn_sernum;
  58. struct rt6_info *rr_ptr;
  59. };
  60. #ifndef CONFIG_IPV6_SUBTREES
  61. #define FIB6_SUBTREE(fn) NULL
  62. #else
  63. #define FIB6_SUBTREE(fn) ((fn)->subtree)
  64. #endif
  65. struct mx6_config {
  66. const u32 *mx;
  67. DECLARE_BITMAP(mx_valid, RTAX_MAX);
  68. };
  69. /*
  70. * routing information
  71. *
  72. */
  73. struct rt6key {
  74. struct in6_addr addr;
  75. int plen;
  76. };
  77. struct fib6_table;
  78. struct rt6_info {
  79. struct dst_entry dst;
  80. /*
  81. * Tail elements of dst_entry (__refcnt etc.)
  82. * and these elements (rarely used in hot path) are in
  83. * the same cache line.
  84. */
  85. struct fib6_table *rt6i_table;
  86. struct fib6_node *rt6i_node;
  87. struct in6_addr rt6i_gateway;
  88. /* Multipath routes:
  89. * siblings is a list of rt6_info that have the the same metric/weight,
  90. * destination, but not the same gateway. nsiblings is just a cache
  91. * to speed up lookup.
  92. */
  93. struct list_head rt6i_siblings;
  94. unsigned int rt6i_nsiblings;
  95. atomic_t rt6i_ref;
  96. /* These are in a separate cache line. */
  97. struct rt6key rt6i_dst ____cacheline_aligned_in_smp;
  98. u32 rt6i_flags;
  99. struct rt6key rt6i_src;
  100. struct rt6key rt6i_prefsrc;
  101. struct list_head rt6i_uncached;
  102. struct uncached_list *rt6i_uncached_list;
  103. struct inet6_dev *rt6i_idev;
  104. struct rt6_info * __percpu *rt6i_pcpu;
  105. u32 rt6i_metric;
  106. u32 rt6i_pmtu;
  107. /* more non-fragment space at head required */
  108. unsigned short rt6i_nfheader_len;
  109. u8 rt6i_protocol;
  110. };
  111. static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
  112. {
  113. return ((struct rt6_info *)dst)->rt6i_idev;
  114. }
  115. static inline void rt6_clean_expires(struct rt6_info *rt)
  116. {
  117. rt->rt6i_flags &= ~RTF_EXPIRES;
  118. rt->dst.expires = 0;
  119. }
  120. static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
  121. {
  122. rt->dst.expires = expires;
  123. rt->rt6i_flags |= RTF_EXPIRES;
  124. }
  125. static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
  126. {
  127. struct rt6_info *rt;
  128. for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES);
  129. rt = (struct rt6_info *)rt->dst.from);
  130. if (rt && rt != rt0)
  131. rt0->dst.expires = rt->dst.expires;
  132. dst_set_expires(&rt0->dst, timeout);
  133. rt0->rt6i_flags |= RTF_EXPIRES;
  134. }
  135. static inline u32 rt6_get_cookie(const struct rt6_info *rt)
  136. {
  137. if (rt->rt6i_flags & RTF_PCPU || unlikely(rt->dst.flags & DST_NOCACHE))
  138. rt = (struct rt6_info *)(rt->dst.from);
  139. return rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
  140. }
  141. static inline void ip6_rt_put(struct rt6_info *rt)
  142. {
  143. /* dst_release() accepts a NULL parameter.
  144. * We rely on dst being first structure in struct rt6_info
  145. */
  146. BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
  147. dst_release(&rt->dst);
  148. }
  149. enum fib6_walk_state {
  150. #ifdef CONFIG_IPV6_SUBTREES
  151. FWS_S,
  152. #endif
  153. FWS_L,
  154. FWS_R,
  155. FWS_C,
  156. FWS_U
  157. };
  158. struct fib6_walker {
  159. struct list_head lh;
  160. struct fib6_node *root, *node;
  161. struct rt6_info *leaf;
  162. enum fib6_walk_state state;
  163. bool prune;
  164. unsigned int skip;
  165. unsigned int count;
  166. int (*func)(struct fib6_walker *);
  167. void *args;
  168. };
  169. struct rt6_statistics {
  170. __u32 fib_nodes;
  171. __u32 fib_route_nodes;
  172. __u32 fib_rt_alloc; /* permanent routes */
  173. __u32 fib_rt_entries; /* rt entries in table */
  174. __u32 fib_rt_cache; /* cache routes */
  175. __u32 fib_discarded_routes;
  176. };
  177. #define RTN_TL_ROOT 0x0001
  178. #define RTN_ROOT 0x0002 /* tree root node */
  179. #define RTN_RTINFO 0x0004 /* node with valid routing info */
  180. /*
  181. * priority levels (or metrics)
  182. *
  183. */
  184. struct fib6_table {
  185. struct hlist_node tb6_hlist;
  186. u32 tb6_id;
  187. rwlock_t tb6_lock;
  188. struct fib6_node tb6_root;
  189. struct inet_peer_base tb6_peers;
  190. };
  191. #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
  192. #define RT6_TABLE_MAIN RT_TABLE_MAIN
  193. #define RT6_TABLE_DFLT RT6_TABLE_MAIN
  194. #define RT6_TABLE_INFO RT6_TABLE_MAIN
  195. #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
  196. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  197. #define FIB6_TABLE_MIN 1
  198. #define FIB6_TABLE_MAX RT_TABLE_MAX
  199. #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
  200. #else
  201. #define FIB6_TABLE_MIN RT_TABLE_MAIN
  202. #define FIB6_TABLE_MAX FIB6_TABLE_MIN
  203. #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
  204. #endif
  205. typedef struct rt6_info *(*pol_lookup_t)(struct net *,
  206. struct fib6_table *,
  207. struct flowi6 *, int);
  208. /*
  209. * exported functions
  210. */
  211. struct fib6_table *fib6_get_table(struct net *net, u32 id);
  212. struct fib6_table *fib6_new_table(struct net *net, u32 id);
  213. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
  214. int flags, pol_lookup_t lookup);
  215. struct fib6_node *fib6_lookup(struct fib6_node *root,
  216. const struct in6_addr *daddr,
  217. const struct in6_addr *saddr);
  218. struct fib6_node *fib6_locate(struct fib6_node *root,
  219. const struct in6_addr *daddr, int dst_len,
  220. const struct in6_addr *saddr, int src_len);
  221. void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
  222. void *arg);
  223. int fib6_add(struct fib6_node *root, struct rt6_info *rt,
  224. struct nl_info *info, struct mx6_config *mxc);
  225. int fib6_del(struct rt6_info *rt, struct nl_info *info);
  226. void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info);
  227. void fib6_run_gc(unsigned long expires, struct net *net, bool force);
  228. void fib6_gc_cleanup(void);
  229. int fib6_init(void);
  230. int ipv6_route_open(struct inode *inode, struct file *file);
  231. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  232. int fib6_rules_init(void);
  233. void fib6_rules_cleanup(void);
  234. #else
  235. static inline int fib6_rules_init(void)
  236. {
  237. return 0;
  238. }
  239. static inline void fib6_rules_cleanup(void)
  240. {
  241. return ;
  242. }
  243. #endif
  244. #endif