ip6_fib.h 10 KB

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