ip6_fib.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. struct nl_info fc_nlinfo;
  44. };
  45. struct fib6_node {
  46. struct fib6_node *parent;
  47. struct fib6_node *left;
  48. struct fib6_node *right;
  49. #ifdef CONFIG_IPV6_SUBTREES
  50. struct fib6_node *subtree;
  51. #endif
  52. struct rt6_info *leaf;
  53. __u16 fn_bit; /* bit key */
  54. __u16 fn_flags;
  55. __u32 fn_sernum;
  56. struct rt6_info *rr_ptr;
  57. };
  58. #ifndef CONFIG_IPV6_SUBTREES
  59. #define FIB6_SUBTREE(fn) NULL
  60. #else
  61. #define FIB6_SUBTREE(fn) ((fn)->subtree)
  62. #endif
  63. /*
  64. * routing information
  65. *
  66. */
  67. struct rt6key {
  68. struct in6_addr addr;
  69. int plen;
  70. };
  71. struct fib6_table;
  72. struct rt6_info {
  73. struct dst_entry dst;
  74. struct neighbour *n;
  75. /*
  76. * Tail elements of dst_entry (__refcnt etc.)
  77. * and these elements (rarely used in hot path) are in
  78. * the same cache line.
  79. */
  80. struct fib6_table *rt6i_table;
  81. struct fib6_node *rt6i_node;
  82. struct in6_addr rt6i_gateway;
  83. atomic_t rt6i_ref;
  84. /* These are in a separate cache line. */
  85. struct rt6key rt6i_dst ____cacheline_aligned_in_smp;
  86. u32 rt6i_flags;
  87. struct rt6key rt6i_src;
  88. struct rt6key rt6i_prefsrc;
  89. u32 rt6i_metric;
  90. u32 rt6i_peer_genid;
  91. struct inet6_dev *rt6i_idev;
  92. unsigned long _rt6i_peer;
  93. #ifdef CONFIG_XFRM
  94. u32 rt6i_flow_cache_genid;
  95. #endif
  96. /* more non-fragment space at head required */
  97. unsigned short rt6i_nfheader_len;
  98. u8 rt6i_protocol;
  99. };
  100. static inline struct inet_peer *rt6_peer_ptr(struct rt6_info *rt)
  101. {
  102. return inetpeer_ptr(rt->_rt6i_peer);
  103. }
  104. static inline bool rt6_has_peer(struct rt6_info *rt)
  105. {
  106. return inetpeer_ptr_is_peer(rt->_rt6i_peer);
  107. }
  108. static inline void __rt6_set_peer(struct rt6_info *rt, struct inet_peer *peer)
  109. {
  110. __inetpeer_ptr_set_peer(&rt->_rt6i_peer, peer);
  111. }
  112. static inline bool rt6_set_peer(struct rt6_info *rt, struct inet_peer *peer)
  113. {
  114. return inetpeer_ptr_set_peer(&rt->_rt6i_peer, peer);
  115. }
  116. static inline void rt6_init_peer(struct rt6_info *rt, struct inet_peer_base *base)
  117. {
  118. inetpeer_init_ptr(&rt->_rt6i_peer, base);
  119. }
  120. static inline void rt6_transfer_peer(struct rt6_info *rt, struct rt6_info *ort)
  121. {
  122. inetpeer_transfer_peer(&rt->_rt6i_peer, &ort->_rt6i_peer);
  123. }
  124. static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
  125. {
  126. return ((struct rt6_info *)dst)->rt6i_idev;
  127. }
  128. static inline void rt6_clean_expires(struct rt6_info *rt)
  129. {
  130. if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from)
  131. dst_release(rt->dst.from);
  132. rt->rt6i_flags &= ~RTF_EXPIRES;
  133. rt->dst.from = NULL;
  134. }
  135. static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
  136. {
  137. if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from)
  138. dst_release(rt->dst.from);
  139. rt->rt6i_flags |= RTF_EXPIRES;
  140. rt->dst.expires = expires;
  141. }
  142. static inline void rt6_update_expires(struct rt6_info *rt, int timeout)
  143. {
  144. if (!(rt->rt6i_flags & RTF_EXPIRES)) {
  145. if (rt->dst.from)
  146. dst_release(rt->dst.from);
  147. /* dst_set_expires relies on expires == 0
  148. * if it has not been set previously.
  149. */
  150. rt->dst.expires = 0;
  151. }
  152. dst_set_expires(&rt->dst, timeout);
  153. rt->rt6i_flags |= RTF_EXPIRES;
  154. }
  155. static inline void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
  156. {
  157. struct dst_entry *new = (struct dst_entry *) from;
  158. if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from) {
  159. if (new == rt->dst.from)
  160. return;
  161. dst_release(rt->dst.from);
  162. }
  163. rt->rt6i_flags &= ~RTF_EXPIRES;
  164. rt->dst.from = new;
  165. dst_hold(new);
  166. }
  167. struct fib6_walker_t {
  168. struct list_head lh;
  169. struct fib6_node *root, *node;
  170. struct rt6_info *leaf;
  171. unsigned char state;
  172. unsigned char prune;
  173. unsigned int skip;
  174. unsigned int count;
  175. int (*func)(struct fib6_walker_t *);
  176. void *args;
  177. };
  178. struct rt6_statistics {
  179. __u32 fib_nodes;
  180. __u32 fib_route_nodes;
  181. __u32 fib_rt_alloc; /* permanent routes */
  182. __u32 fib_rt_entries; /* rt entries in table */
  183. __u32 fib_rt_cache; /* cache routes */
  184. __u32 fib_discarded_routes;
  185. };
  186. #define RTN_TL_ROOT 0x0001
  187. #define RTN_ROOT 0x0002 /* tree root node */
  188. #define RTN_RTINFO 0x0004 /* node with valid routing info */
  189. /*
  190. * priority levels (or metrics)
  191. *
  192. */
  193. struct fib6_table {
  194. struct hlist_node tb6_hlist;
  195. u32 tb6_id;
  196. rwlock_t tb6_lock;
  197. struct fib6_node tb6_root;
  198. struct inet_peer_base tb6_peers;
  199. };
  200. #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
  201. #define RT6_TABLE_MAIN RT_TABLE_MAIN
  202. #define RT6_TABLE_DFLT RT6_TABLE_MAIN
  203. #define RT6_TABLE_INFO RT6_TABLE_MAIN
  204. #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
  205. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  206. #define FIB6_TABLE_MIN 1
  207. #define FIB6_TABLE_MAX RT_TABLE_MAX
  208. #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
  209. #else
  210. #define FIB6_TABLE_MIN RT_TABLE_MAIN
  211. #define FIB6_TABLE_MAX FIB6_TABLE_MIN
  212. #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
  213. #endif
  214. typedef struct rt6_info *(*pol_lookup_t)(struct net *,
  215. struct fib6_table *,
  216. struct flowi6 *, int);
  217. /*
  218. * exported functions
  219. */
  220. extern struct fib6_table *fib6_get_table(struct net *net, u32 id);
  221. extern struct fib6_table *fib6_new_table(struct net *net, u32 id);
  222. extern struct dst_entry *fib6_rule_lookup(struct net *net,
  223. struct flowi6 *fl6, int flags,
  224. pol_lookup_t lookup);
  225. extern struct fib6_node *fib6_lookup(struct fib6_node *root,
  226. const struct in6_addr *daddr,
  227. const struct in6_addr *saddr);
  228. struct fib6_node *fib6_locate(struct fib6_node *root,
  229. const struct in6_addr *daddr, int dst_len,
  230. const struct in6_addr *saddr, int src_len);
  231. extern void fib6_clean_all_ro(struct net *net,
  232. int (*func)(struct rt6_info *, void *arg),
  233. int prune, void *arg);
  234. extern void fib6_clean_all(struct net *net,
  235. int (*func)(struct rt6_info *, void *arg),
  236. int prune, void *arg);
  237. extern int fib6_add(struct fib6_node *root,
  238. struct rt6_info *rt,
  239. struct nl_info *info);
  240. extern int fib6_del(struct rt6_info *rt,
  241. struct nl_info *info);
  242. extern void inet6_rt_notify(int event, struct rt6_info *rt,
  243. struct nl_info *info);
  244. extern void fib6_run_gc(unsigned long expires,
  245. struct net *net);
  246. extern void fib6_gc_cleanup(void);
  247. extern int fib6_init(void);
  248. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  249. extern int fib6_rules_init(void);
  250. extern void fib6_rules_cleanup(void);
  251. #else
  252. static inline int fib6_rules_init(void)
  253. {
  254. return 0;
  255. }
  256. static inline void fib6_rules_cleanup(void)
  257. {
  258. return ;
  259. }
  260. #endif
  261. #endif