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