ip6_fib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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_info;
  36. struct fib6_config {
  37. u32 fc_table;
  38. u32 fc_metric;
  39. int fc_dst_len;
  40. int fc_src_len;
  41. int fc_ifindex;
  42. u32 fc_flags;
  43. u32 fc_protocol;
  44. u16 fc_type; /* only 8 bits are used */
  45. u16 fc_delete_all_nh : 1,
  46. __unused : 15;
  47. struct in6_addr fc_dst;
  48. struct in6_addr fc_src;
  49. struct in6_addr fc_prefsrc;
  50. struct in6_addr fc_gateway;
  51. unsigned long fc_expires;
  52. struct nlattr *fc_mx;
  53. int fc_mx_len;
  54. int fc_mp_len;
  55. struct nlattr *fc_mp;
  56. struct nl_info fc_nlinfo;
  57. struct nlattr *fc_encap;
  58. u16 fc_encap_type;
  59. };
  60. struct fib6_node {
  61. struct fib6_node __rcu *parent;
  62. struct fib6_node __rcu *left;
  63. struct fib6_node __rcu *right;
  64. #ifdef CONFIG_IPV6_SUBTREES
  65. struct fib6_node __rcu *subtree;
  66. #endif
  67. struct fib6_info __rcu *leaf;
  68. __u16 fn_bit; /* bit key */
  69. __u16 fn_flags;
  70. int fn_sernum;
  71. struct fib6_info __rcu *rr_ptr;
  72. struct rcu_head rcu;
  73. };
  74. struct fib6_gc_args {
  75. int timeout;
  76. int more;
  77. };
  78. #ifndef CONFIG_IPV6_SUBTREES
  79. #define FIB6_SUBTREE(fn) NULL
  80. #else
  81. #define FIB6_SUBTREE(fn) (rcu_dereference_protected((fn)->subtree, 1))
  82. #endif
  83. /*
  84. * routing information
  85. *
  86. */
  87. struct rt6key {
  88. struct in6_addr addr;
  89. int plen;
  90. };
  91. struct fib6_table;
  92. struct rt6_exception_bucket {
  93. struct hlist_head chain;
  94. int depth;
  95. };
  96. struct rt6_exception {
  97. struct hlist_node hlist;
  98. struct rt6_info *rt6i;
  99. unsigned long stamp;
  100. struct rcu_head rcu;
  101. };
  102. #define FIB6_EXCEPTION_BUCKET_SIZE_SHIFT 10
  103. #define FIB6_EXCEPTION_BUCKET_SIZE (1 << FIB6_EXCEPTION_BUCKET_SIZE_SHIFT)
  104. #define FIB6_MAX_DEPTH 5
  105. struct fib6_nh {
  106. struct in6_addr nh_gw;
  107. struct net_device *nh_dev;
  108. struct lwtunnel_state *nh_lwtstate;
  109. unsigned int nh_flags;
  110. atomic_t nh_upper_bound;
  111. int nh_weight;
  112. };
  113. struct fib6_info {
  114. struct fib6_table *fib6_table;
  115. struct fib6_info __rcu *fib6_next;
  116. struct fib6_node __rcu *fib6_node;
  117. /* Multipath routes:
  118. * siblings is a list of fib6_info that have the the same metric/weight,
  119. * destination, but not the same gateway. nsiblings is just a cache
  120. * to speed up lookup.
  121. */
  122. struct list_head fib6_siblings;
  123. unsigned int fib6_nsiblings;
  124. atomic_t fib6_ref;
  125. unsigned long expires;
  126. struct dst_metrics *fib6_metrics;
  127. #define fib6_pmtu fib6_metrics->metrics[RTAX_MTU-1]
  128. struct rt6key fib6_dst;
  129. u32 fib6_flags;
  130. struct rt6key fib6_src;
  131. struct rt6key fib6_prefsrc;
  132. struct rt6_info * __percpu *rt6i_pcpu;
  133. struct rt6_exception_bucket __rcu *rt6i_exception_bucket;
  134. #ifdef CONFIG_IPV6_ROUTER_PREF
  135. unsigned long last_probe;
  136. #endif
  137. u32 fib6_metric;
  138. u8 fib6_protocol;
  139. u8 fib6_type;
  140. u8 exception_bucket_flushed:1,
  141. should_flush:1,
  142. dst_nocount:1,
  143. dst_nopolicy:1,
  144. dst_host:1,
  145. fib6_destroying:1,
  146. unused:2;
  147. struct fib6_nh fib6_nh;
  148. struct rcu_head rcu;
  149. };
  150. struct rt6_info {
  151. struct dst_entry dst;
  152. struct fib6_info __rcu *from;
  153. struct rt6key rt6i_dst;
  154. struct rt6key rt6i_src;
  155. struct in6_addr rt6i_gateway;
  156. struct inet6_dev *rt6i_idev;
  157. u32 rt6i_flags;
  158. struct rt6key rt6i_prefsrc;
  159. struct list_head rt6i_uncached;
  160. struct uncached_list *rt6i_uncached_list;
  161. /* more non-fragment space at head required */
  162. unsigned short rt6i_nfheader_len;
  163. };
  164. #define for_each_fib6_node_rt_rcu(fn) \
  165. for (rt = rcu_dereference((fn)->leaf); rt; \
  166. rt = rcu_dereference(rt->fib6_next))
  167. #define for_each_fib6_walker_rt(w) \
  168. for (rt = (w)->leaf; rt; \
  169. rt = rcu_dereference_protected(rt->fib6_next, 1))
  170. static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
  171. {
  172. return ((struct rt6_info *)dst)->rt6i_idev;
  173. }
  174. static inline void fib6_clean_expires(struct fib6_info *f6i)
  175. {
  176. f6i->fib6_flags &= ~RTF_EXPIRES;
  177. f6i->expires = 0;
  178. }
  179. static inline void fib6_set_expires(struct fib6_info *f6i,
  180. unsigned long expires)
  181. {
  182. f6i->expires = expires;
  183. f6i->fib6_flags |= RTF_EXPIRES;
  184. }
  185. static inline bool fib6_check_expired(const struct fib6_info *f6i)
  186. {
  187. if (f6i->fib6_flags & RTF_EXPIRES)
  188. return time_after(jiffies, f6i->expires);
  189. return false;
  190. }
  191. /* Function to safely get fn->sernum for passed in rt
  192. * and store result in passed in cookie.
  193. * Return true if we can get cookie safely
  194. * Return false if not
  195. */
  196. static inline bool fib6_get_cookie_safe(const struct fib6_info *f6i,
  197. u32 *cookie)
  198. {
  199. struct fib6_node *fn;
  200. bool status = false;
  201. fn = rcu_dereference(f6i->fib6_node);
  202. if (fn) {
  203. *cookie = fn->fn_sernum;
  204. /* pairs with smp_wmb() in fib6_update_sernum_upto_root() */
  205. smp_rmb();
  206. status = true;
  207. }
  208. return status;
  209. }
  210. static inline u32 rt6_get_cookie(const struct rt6_info *rt)
  211. {
  212. struct fib6_info *from;
  213. u32 cookie = 0;
  214. rcu_read_lock();
  215. from = rcu_dereference(rt->from);
  216. if (from)
  217. fib6_get_cookie_safe(from, &cookie);
  218. rcu_read_unlock();
  219. return cookie;
  220. }
  221. static inline void ip6_rt_put(struct rt6_info *rt)
  222. {
  223. /* dst_release() accepts a NULL parameter.
  224. * We rely on dst being first structure in struct rt6_info
  225. */
  226. BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
  227. dst_release(&rt->dst);
  228. }
  229. struct fib6_info *fib6_info_alloc(gfp_t gfp_flags);
  230. void fib6_info_destroy_rcu(struct rcu_head *head);
  231. static inline void fib6_info_hold(struct fib6_info *f6i)
  232. {
  233. atomic_inc(&f6i->fib6_ref);
  234. }
  235. static inline bool fib6_info_hold_safe(struct fib6_info *f6i)
  236. {
  237. return atomic_inc_not_zero(&f6i->fib6_ref);
  238. }
  239. static inline void fib6_info_release(struct fib6_info *f6i)
  240. {
  241. if (f6i && atomic_dec_and_test(&f6i->fib6_ref))
  242. call_rcu(&f6i->rcu, fib6_info_destroy_rcu);
  243. }
  244. enum fib6_walk_state {
  245. #ifdef CONFIG_IPV6_SUBTREES
  246. FWS_S,
  247. #endif
  248. FWS_L,
  249. FWS_R,
  250. FWS_C,
  251. FWS_U
  252. };
  253. struct fib6_walker {
  254. struct list_head lh;
  255. struct fib6_node *root, *node;
  256. struct fib6_info *leaf;
  257. enum fib6_walk_state state;
  258. unsigned int skip;
  259. unsigned int count;
  260. int (*func)(struct fib6_walker *);
  261. void *args;
  262. };
  263. struct rt6_statistics {
  264. __u32 fib_nodes; /* all fib6 nodes */
  265. __u32 fib_route_nodes; /* intermediate nodes */
  266. __u32 fib_rt_entries; /* rt entries in fib table */
  267. __u32 fib_rt_cache; /* cached rt entries in exception table */
  268. __u32 fib_discarded_routes; /* total number of routes delete */
  269. /* The following stats are not protected by any lock */
  270. atomic_t fib_rt_alloc; /* total number of routes alloced */
  271. atomic_t fib_rt_uncache; /* rt entries in uncached list */
  272. };
  273. #define RTN_TL_ROOT 0x0001
  274. #define RTN_ROOT 0x0002 /* tree root node */
  275. #define RTN_RTINFO 0x0004 /* node with valid routing info */
  276. /*
  277. * priority levels (or metrics)
  278. *
  279. */
  280. struct fib6_table {
  281. struct hlist_node tb6_hlist;
  282. u32 tb6_id;
  283. spinlock_t tb6_lock;
  284. struct fib6_node tb6_root;
  285. struct inet_peer_base tb6_peers;
  286. unsigned int flags;
  287. unsigned int fib_seq;
  288. #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
  289. };
  290. #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
  291. #define RT6_TABLE_MAIN RT_TABLE_MAIN
  292. #define RT6_TABLE_DFLT RT6_TABLE_MAIN
  293. #define RT6_TABLE_INFO RT6_TABLE_MAIN
  294. #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
  295. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  296. #define FIB6_TABLE_MIN 1
  297. #define FIB6_TABLE_MAX RT_TABLE_MAX
  298. #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
  299. #else
  300. #define FIB6_TABLE_MIN RT_TABLE_MAIN
  301. #define FIB6_TABLE_MAX FIB6_TABLE_MIN
  302. #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
  303. #endif
  304. typedef struct rt6_info *(*pol_lookup_t)(struct net *,
  305. struct fib6_table *,
  306. struct flowi6 *,
  307. const struct sk_buff *, int);
  308. struct fib6_entry_notifier_info {
  309. struct fib_notifier_info info; /* must be first */
  310. struct fib6_info *rt;
  311. };
  312. /*
  313. * exported functions
  314. */
  315. struct fib6_table *fib6_get_table(struct net *net, u32 id);
  316. struct fib6_table *fib6_new_table(struct net *net, u32 id);
  317. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
  318. const struct sk_buff *skb,
  319. int flags, pol_lookup_t lookup);
  320. /* called with rcu lock held; can return error pointer
  321. * caller needs to select path
  322. */
  323. struct fib6_info *fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
  324. int flags);
  325. /* called with rcu lock held; caller needs to select path */
  326. struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
  327. int oif, struct flowi6 *fl6, int strict);
  328. struct fib6_info *fib6_multipath_select(const struct net *net,
  329. struct fib6_info *match,
  330. struct flowi6 *fl6, int oif,
  331. const struct sk_buff *skb, int strict);
  332. struct fib6_node *fib6_node_lookup(struct fib6_node *root,
  333. const struct in6_addr *daddr,
  334. const struct in6_addr *saddr);
  335. struct fib6_node *fib6_locate(struct fib6_node *root,
  336. const struct in6_addr *daddr, int dst_len,
  337. const struct in6_addr *saddr, int src_len,
  338. bool exact_match);
  339. void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *arg),
  340. void *arg);
  341. int fib6_add(struct fib6_node *root, struct fib6_info *rt,
  342. struct nl_info *info, struct netlink_ext_ack *extack);
  343. int fib6_del(struct fib6_info *rt, struct nl_info *info);
  344. static inline struct net_device *fib6_info_nh_dev(const struct fib6_info *f6i)
  345. {
  346. return f6i->fib6_nh.nh_dev;
  347. }
  348. static inline
  349. struct lwtunnel_state *fib6_info_nh_lwt(const struct fib6_info *f6i)
  350. {
  351. return f6i->fib6_nh.nh_lwtstate;
  352. }
  353. void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
  354. unsigned int flags);
  355. void fib6_run_gc(unsigned long expires, struct net *net, bool force);
  356. void fib6_gc_cleanup(void);
  357. int fib6_init(void);
  358. struct ipv6_route_iter {
  359. struct seq_net_private p;
  360. struct fib6_walker w;
  361. loff_t skip;
  362. struct fib6_table *tbl;
  363. int sernum;
  364. };
  365. extern const struct seq_operations ipv6_route_seq_ops;
  366. int call_fib6_notifier(struct notifier_block *nb, struct net *net,
  367. enum fib_event_type event_type,
  368. struct fib_notifier_info *info);
  369. int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
  370. struct fib_notifier_info *info);
  371. int __net_init fib6_notifier_init(struct net *net);
  372. void __net_exit fib6_notifier_exit(struct net *net);
  373. unsigned int fib6_tables_seq_read(struct net *net);
  374. int fib6_tables_dump(struct net *net, struct notifier_block *nb);
  375. void fib6_update_sernum(struct net *net, struct fib6_info *rt);
  376. void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt);
  377. void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val);
  378. static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
  379. {
  380. return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
  381. }
  382. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  383. int fib6_rules_init(void);
  384. void fib6_rules_cleanup(void);
  385. bool fib6_rule_default(const struct fib_rule *rule);
  386. int fib6_rules_dump(struct net *net, struct notifier_block *nb);
  387. unsigned int fib6_rules_seq_read(struct net *net);
  388. static inline bool fib6_rules_early_flow_dissect(struct net *net,
  389. struct sk_buff *skb,
  390. struct flowi6 *fl6,
  391. struct flow_keys *flkeys)
  392. {
  393. unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
  394. if (!net->ipv6.fib6_rules_require_fldissect)
  395. return false;
  396. skb_flow_dissect_flow_keys(skb, flkeys, flag);
  397. fl6->fl6_sport = flkeys->ports.src;
  398. fl6->fl6_dport = flkeys->ports.dst;
  399. fl6->flowi6_proto = flkeys->basic.ip_proto;
  400. return true;
  401. }
  402. #else
  403. static inline int fib6_rules_init(void)
  404. {
  405. return 0;
  406. }
  407. static inline void fib6_rules_cleanup(void)
  408. {
  409. return ;
  410. }
  411. static inline bool fib6_rule_default(const struct fib_rule *rule)
  412. {
  413. return true;
  414. }
  415. static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb)
  416. {
  417. return 0;
  418. }
  419. static inline unsigned int fib6_rules_seq_read(struct net *net)
  420. {
  421. return 0;
  422. }
  423. static inline bool fib6_rules_early_flow_dissect(struct net *net,
  424. struct sk_buff *skb,
  425. struct flowi6 *fl6,
  426. struct flow_keys *flkeys)
  427. {
  428. return false;
  429. }
  430. #endif
  431. #endif