ip6_fib.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <linux/notifier.h>
  18. #include <net/dst.h>
  19. #include <net/flow.h>
  20. #include <net/netlink.h>
  21. #include <net/inetpeer.h>
  22. #include <net/fib_notifier.h>
  23. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  24. #define FIB6_TABLE_HASHSZ 256
  25. #else
  26. #define FIB6_TABLE_HASHSZ 1
  27. #endif
  28. struct rt6_info;
  29. struct fib6_config {
  30. u32 fc_table;
  31. u32 fc_metric;
  32. int fc_dst_len;
  33. int fc_src_len;
  34. int fc_ifindex;
  35. u32 fc_flags;
  36. u32 fc_protocol;
  37. u16 fc_type; /* only 8 bits are used */
  38. u16 fc_delete_all_nh : 1,
  39. __unused : 15;
  40. struct in6_addr fc_dst;
  41. struct in6_addr fc_src;
  42. struct in6_addr fc_prefsrc;
  43. struct in6_addr fc_gateway;
  44. unsigned long fc_expires;
  45. struct nlattr *fc_mx;
  46. int fc_mx_len;
  47. int fc_mp_len;
  48. struct nlattr *fc_mp;
  49. struct nl_info fc_nlinfo;
  50. struct nlattr *fc_encap;
  51. u16 fc_encap_type;
  52. };
  53. struct fib6_node {
  54. struct fib6_node *parent;
  55. struct fib6_node *left;
  56. struct fib6_node *right;
  57. #ifdef CONFIG_IPV6_SUBTREES
  58. struct fib6_node *subtree;
  59. #endif
  60. struct rt6_info *leaf;
  61. __u16 fn_bit; /* bit key */
  62. __u16 fn_flags;
  63. int fn_sernum;
  64. struct rt6_info *rr_ptr;
  65. struct rcu_head rcu;
  66. };
  67. #ifndef CONFIG_IPV6_SUBTREES
  68. #define FIB6_SUBTREE(fn) NULL
  69. #else
  70. #define FIB6_SUBTREE(fn) ((fn)->subtree)
  71. #endif
  72. struct mx6_config {
  73. const u32 *mx;
  74. DECLARE_BITMAP(mx_valid, RTAX_MAX);
  75. };
  76. /*
  77. * routing information
  78. *
  79. */
  80. struct rt6key {
  81. struct in6_addr addr;
  82. int plen;
  83. };
  84. struct fib6_table;
  85. struct rt6_info {
  86. struct dst_entry dst;
  87. /*
  88. * Tail elements of dst_entry (__refcnt etc.)
  89. * and these elements (rarely used in hot path) are in
  90. * the same cache line.
  91. */
  92. struct fib6_table *rt6i_table;
  93. struct fib6_node __rcu *rt6i_node;
  94. struct in6_addr rt6i_gateway;
  95. /* Multipath routes:
  96. * siblings is a list of rt6_info that have the the same metric/weight,
  97. * destination, but not the same gateway. nsiblings is just a cache
  98. * to speed up lookup.
  99. */
  100. struct list_head rt6i_siblings;
  101. unsigned int rt6i_nsiblings;
  102. atomic_t rt6i_ref;
  103. unsigned int rt6i_nh_flags;
  104. /* These are in a separate cache line. */
  105. struct rt6key rt6i_dst ____cacheline_aligned_in_smp;
  106. u32 rt6i_flags;
  107. struct rt6key rt6i_src;
  108. struct rt6key rt6i_prefsrc;
  109. struct list_head rt6i_uncached;
  110. struct uncached_list *rt6i_uncached_list;
  111. struct inet6_dev *rt6i_idev;
  112. struct rt6_info * __percpu *rt6i_pcpu;
  113. u32 rt6i_metric;
  114. u32 rt6i_pmtu;
  115. /* more non-fragment space at head required */
  116. unsigned short rt6i_nfheader_len;
  117. u8 rt6i_protocol;
  118. };
  119. static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
  120. {
  121. return ((struct rt6_info *)dst)->rt6i_idev;
  122. }
  123. static inline void rt6_clean_expires(struct rt6_info *rt)
  124. {
  125. rt->rt6i_flags &= ~RTF_EXPIRES;
  126. rt->dst.expires = 0;
  127. }
  128. static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
  129. {
  130. rt->dst.expires = expires;
  131. rt->rt6i_flags |= RTF_EXPIRES;
  132. }
  133. static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
  134. {
  135. struct rt6_info *rt;
  136. for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES);
  137. rt = (struct rt6_info *)rt->dst.from);
  138. if (rt && rt != rt0)
  139. rt0->dst.expires = rt->dst.expires;
  140. dst_set_expires(&rt0->dst, timeout);
  141. rt0->rt6i_flags |= RTF_EXPIRES;
  142. }
  143. /* Function to safely get fn->sernum for passed in rt
  144. * and store result in passed in cookie.
  145. * Return true if we can get cookie safely
  146. * Return false if not
  147. */
  148. static inline bool rt6_get_cookie_safe(const struct rt6_info *rt,
  149. u32 *cookie)
  150. {
  151. struct fib6_node *fn;
  152. bool status = false;
  153. rcu_read_lock();
  154. fn = rcu_dereference(rt->rt6i_node);
  155. if (fn) {
  156. *cookie = fn->fn_sernum;
  157. status = true;
  158. }
  159. rcu_read_unlock();
  160. return status;
  161. }
  162. static inline u32 rt6_get_cookie(const struct rt6_info *rt)
  163. {
  164. u32 cookie = 0;
  165. if (rt->dst.from)
  166. rt = (struct rt6_info *)(rt->dst.from);
  167. rt6_get_cookie_safe(rt, &cookie);
  168. return cookie;
  169. }
  170. static inline void ip6_rt_put(struct rt6_info *rt)
  171. {
  172. /* dst_release() accepts a NULL parameter.
  173. * We rely on dst being first structure in struct rt6_info
  174. */
  175. BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
  176. dst_release(&rt->dst);
  177. }
  178. void rt6_free_pcpu(struct rt6_info *non_pcpu_rt);
  179. static inline void rt6_hold(struct rt6_info *rt)
  180. {
  181. atomic_inc(&rt->rt6i_ref);
  182. }
  183. static inline void rt6_release(struct rt6_info *rt)
  184. {
  185. if (atomic_dec_and_test(&rt->rt6i_ref)) {
  186. rt6_free_pcpu(rt);
  187. dst_dev_put(&rt->dst);
  188. dst_release(&rt->dst);
  189. }
  190. }
  191. enum fib6_walk_state {
  192. #ifdef CONFIG_IPV6_SUBTREES
  193. FWS_S,
  194. #endif
  195. FWS_L,
  196. FWS_R,
  197. FWS_C,
  198. FWS_U
  199. };
  200. struct fib6_walker {
  201. struct list_head lh;
  202. struct fib6_node *root, *node;
  203. struct rt6_info *leaf;
  204. enum fib6_walk_state state;
  205. bool prune;
  206. unsigned int skip;
  207. unsigned int count;
  208. int (*func)(struct fib6_walker *);
  209. void *args;
  210. };
  211. struct rt6_statistics {
  212. __u32 fib_nodes;
  213. __u32 fib_route_nodes;
  214. __u32 fib_rt_alloc; /* permanent routes */
  215. __u32 fib_rt_entries; /* rt entries in table */
  216. __u32 fib_rt_cache; /* cache routes */
  217. __u32 fib_discarded_routes;
  218. };
  219. #define RTN_TL_ROOT 0x0001
  220. #define RTN_ROOT 0x0002 /* tree root node */
  221. #define RTN_RTINFO 0x0004 /* node with valid routing info */
  222. /*
  223. * priority levels (or metrics)
  224. *
  225. */
  226. struct fib6_table {
  227. struct hlist_node tb6_hlist;
  228. u32 tb6_id;
  229. rwlock_t tb6_lock;
  230. struct fib6_node tb6_root;
  231. struct inet_peer_base tb6_peers;
  232. unsigned int flags;
  233. unsigned int fib_seq;
  234. #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
  235. };
  236. #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
  237. #define RT6_TABLE_MAIN RT_TABLE_MAIN
  238. #define RT6_TABLE_DFLT RT6_TABLE_MAIN
  239. #define RT6_TABLE_INFO RT6_TABLE_MAIN
  240. #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
  241. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  242. #define FIB6_TABLE_MIN 1
  243. #define FIB6_TABLE_MAX RT_TABLE_MAX
  244. #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
  245. #else
  246. #define FIB6_TABLE_MIN RT_TABLE_MAIN
  247. #define FIB6_TABLE_MAX FIB6_TABLE_MIN
  248. #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
  249. #endif
  250. typedef struct rt6_info *(*pol_lookup_t)(struct net *,
  251. struct fib6_table *,
  252. struct flowi6 *, int);
  253. struct fib6_entry_notifier_info {
  254. struct fib_notifier_info info; /* must be first */
  255. struct rt6_info *rt;
  256. };
  257. /*
  258. * exported functions
  259. */
  260. struct fib6_table *fib6_get_table(struct net *net, u32 id);
  261. struct fib6_table *fib6_new_table(struct net *net, u32 id);
  262. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
  263. int flags, pol_lookup_t lookup);
  264. struct fib6_node *fib6_lookup(struct fib6_node *root,
  265. const struct in6_addr *daddr,
  266. const struct in6_addr *saddr);
  267. struct fib6_node *fib6_locate(struct fib6_node *root,
  268. const struct in6_addr *daddr, int dst_len,
  269. const struct in6_addr *saddr, int src_len);
  270. void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
  271. void *arg);
  272. int fib6_add(struct fib6_node *root, struct rt6_info *rt,
  273. struct nl_info *info, struct mx6_config *mxc,
  274. struct netlink_ext_ack *extack);
  275. int fib6_del(struct rt6_info *rt, struct nl_info *info);
  276. void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
  277. unsigned int flags);
  278. void fib6_run_gc(unsigned long expires, struct net *net, bool force);
  279. void fib6_gc_cleanup(void);
  280. int fib6_init(void);
  281. int ipv6_route_open(struct inode *inode, struct file *file);
  282. int call_fib6_notifier(struct notifier_block *nb, struct net *net,
  283. enum fib_event_type event_type,
  284. struct fib_notifier_info *info);
  285. int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
  286. struct fib_notifier_info *info);
  287. int __net_init fib6_notifier_init(struct net *net);
  288. void __net_exit fib6_notifier_exit(struct net *net);
  289. unsigned int fib6_tables_seq_read(struct net *net);
  290. int fib6_tables_dump(struct net *net, struct notifier_block *nb);
  291. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  292. int fib6_rules_init(void);
  293. void fib6_rules_cleanup(void);
  294. bool fib6_rule_default(const struct fib_rule *rule);
  295. int fib6_rules_dump(struct net *net, struct notifier_block *nb);
  296. unsigned int fib6_rules_seq_read(struct net *net);
  297. #else
  298. static inline int fib6_rules_init(void)
  299. {
  300. return 0;
  301. }
  302. static inline void fib6_rules_cleanup(void)
  303. {
  304. return ;
  305. }
  306. static inline bool fib6_rule_default(const struct fib_rule *rule)
  307. {
  308. return true;
  309. }
  310. static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb)
  311. {
  312. return 0;
  313. }
  314. static inline unsigned int fib6_rules_seq_read(struct net *net)
  315. {
  316. return 0;
  317. }
  318. #endif
  319. #endif