ip6_fib.h 6.8 KB

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