ip6_fib.h 7.2 KB

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