ip6_fib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. u32 fib6_metric;
  135. u8 fib6_protocol;
  136. u8 fib6_type;
  137. u8 exception_bucket_flushed:1,
  138. should_flush:1,
  139. dst_nocount:1,
  140. dst_nopolicy:1,
  141. dst_host:1,
  142. unused:3;
  143. struct fib6_nh fib6_nh;
  144. };
  145. struct rt6_info {
  146. struct dst_entry dst;
  147. struct fib6_info __rcu *from;
  148. struct rt6key rt6i_dst;
  149. struct rt6key rt6i_src;
  150. struct in6_addr rt6i_gateway;
  151. struct inet6_dev *rt6i_idev;
  152. u32 rt6i_flags;
  153. struct rt6key rt6i_prefsrc;
  154. struct list_head rt6i_uncached;
  155. struct uncached_list *rt6i_uncached_list;
  156. /* more non-fragment space at head required */
  157. unsigned short rt6i_nfheader_len;
  158. };
  159. #define for_each_fib6_node_rt_rcu(fn) \
  160. for (rt = rcu_dereference((fn)->leaf); rt; \
  161. rt = rcu_dereference(rt->fib6_next))
  162. #define for_each_fib6_walker_rt(w) \
  163. for (rt = (w)->leaf; rt; \
  164. rt = rcu_dereference_protected(rt->fib6_next, 1))
  165. static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
  166. {
  167. return ((struct rt6_info *)dst)->rt6i_idev;
  168. }
  169. static inline void fib6_clean_expires(struct fib6_info *f6i)
  170. {
  171. f6i->fib6_flags &= ~RTF_EXPIRES;
  172. f6i->expires = 0;
  173. }
  174. static inline void fib6_set_expires(struct fib6_info *f6i,
  175. unsigned long expires)
  176. {
  177. f6i->expires = expires;
  178. f6i->fib6_flags |= RTF_EXPIRES;
  179. }
  180. static inline bool fib6_check_expired(const struct fib6_info *f6i)
  181. {
  182. if (f6i->fib6_flags & RTF_EXPIRES)
  183. return time_after(jiffies, f6i->expires);
  184. return false;
  185. }
  186. /* Function to safely get fn->sernum for passed in rt
  187. * and store result in passed in cookie.
  188. * Return true if we can get cookie safely
  189. * Return false if not
  190. */
  191. static inline bool fib6_get_cookie_safe(const struct fib6_info *f6i,
  192. u32 *cookie)
  193. {
  194. struct fib6_node *fn;
  195. bool status = false;
  196. fn = rcu_dereference(f6i->fib6_node);
  197. if (fn) {
  198. *cookie = fn->fn_sernum;
  199. /* pairs with smp_wmb() in fib6_update_sernum_upto_root() */
  200. smp_rmb();
  201. status = true;
  202. }
  203. return status;
  204. }
  205. static inline u32 rt6_get_cookie(const struct rt6_info *rt)
  206. {
  207. struct fib6_info *from;
  208. u32 cookie = 0;
  209. rcu_read_lock();
  210. from = rcu_dereference(rt->from);
  211. if (from && (rt->rt6i_flags & RTF_PCPU ||
  212. unlikely(!list_empty(&rt->rt6i_uncached))))
  213. fib6_get_cookie_safe(from, &cookie);
  214. rcu_read_unlock();
  215. return cookie;
  216. }
  217. static inline void ip6_rt_put(struct rt6_info *rt)
  218. {
  219. /* dst_release() accepts a NULL parameter.
  220. * We rely on dst being first structure in struct rt6_info
  221. */
  222. BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
  223. dst_release(&rt->dst);
  224. }
  225. struct fib6_info *fib6_info_alloc(gfp_t gfp_flags);
  226. void fib6_info_destroy(struct fib6_info *f6i);
  227. static inline void fib6_info_hold(struct fib6_info *f6i)
  228. {
  229. atomic_inc(&f6i->fib6_ref);
  230. }
  231. static inline void fib6_info_release(struct fib6_info *f6i)
  232. {
  233. if (f6i && atomic_dec_and_test(&f6i->fib6_ref))
  234. fib6_info_destroy(f6i);
  235. }
  236. enum fib6_walk_state {
  237. #ifdef CONFIG_IPV6_SUBTREES
  238. FWS_S,
  239. #endif
  240. FWS_L,
  241. FWS_R,
  242. FWS_C,
  243. FWS_U
  244. };
  245. struct fib6_walker {
  246. struct list_head lh;
  247. struct fib6_node *root, *node;
  248. struct fib6_info *leaf;
  249. enum fib6_walk_state state;
  250. unsigned int skip;
  251. unsigned int count;
  252. int (*func)(struct fib6_walker *);
  253. void *args;
  254. };
  255. struct rt6_statistics {
  256. __u32 fib_nodes; /* all fib6 nodes */
  257. __u32 fib_route_nodes; /* intermediate nodes */
  258. __u32 fib_rt_entries; /* rt entries in fib table */
  259. __u32 fib_rt_cache; /* cached rt entries in exception table */
  260. __u32 fib_discarded_routes; /* total number of routes delete */
  261. /* The following stats are not protected by any lock */
  262. atomic_t fib_rt_alloc; /* total number of routes alloced */
  263. atomic_t fib_rt_uncache; /* rt entries in uncached list */
  264. };
  265. #define RTN_TL_ROOT 0x0001
  266. #define RTN_ROOT 0x0002 /* tree root node */
  267. #define RTN_RTINFO 0x0004 /* node with valid routing info */
  268. /*
  269. * priority levels (or metrics)
  270. *
  271. */
  272. struct fib6_table {
  273. struct hlist_node tb6_hlist;
  274. u32 tb6_id;
  275. spinlock_t tb6_lock;
  276. struct fib6_node tb6_root;
  277. struct inet_peer_base tb6_peers;
  278. unsigned int flags;
  279. unsigned int fib_seq;
  280. #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
  281. };
  282. #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
  283. #define RT6_TABLE_MAIN RT_TABLE_MAIN
  284. #define RT6_TABLE_DFLT RT6_TABLE_MAIN
  285. #define RT6_TABLE_INFO RT6_TABLE_MAIN
  286. #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
  287. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  288. #define FIB6_TABLE_MIN 1
  289. #define FIB6_TABLE_MAX RT_TABLE_MAX
  290. #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
  291. #else
  292. #define FIB6_TABLE_MIN RT_TABLE_MAIN
  293. #define FIB6_TABLE_MAX FIB6_TABLE_MIN
  294. #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
  295. #endif
  296. typedef struct rt6_info *(*pol_lookup_t)(struct net *,
  297. struct fib6_table *,
  298. struct flowi6 *,
  299. const struct sk_buff *, int);
  300. struct fib6_entry_notifier_info {
  301. struct fib_notifier_info info; /* must be first */
  302. struct fib6_info *rt;
  303. };
  304. /*
  305. * exported functions
  306. */
  307. struct fib6_table *fib6_get_table(struct net *net, u32 id);
  308. struct fib6_table *fib6_new_table(struct net *net, u32 id);
  309. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
  310. const struct sk_buff *skb,
  311. int flags, pol_lookup_t lookup);
  312. /* called with rcu lock held; can return error pointer
  313. * caller needs to select path
  314. */
  315. struct fib6_info *fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
  316. int flags);
  317. /* called with rcu lock held; caller needs to select path */
  318. struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
  319. int oif, struct flowi6 *fl6, int strict);
  320. struct fib6_info *fib6_multipath_select(const struct net *net,
  321. struct fib6_info *match,
  322. struct flowi6 *fl6, int oif,
  323. const struct sk_buff *skb, int strict);
  324. struct fib6_node *fib6_node_lookup(struct fib6_node *root,
  325. const struct in6_addr *daddr,
  326. const struct in6_addr *saddr);
  327. struct fib6_node *fib6_locate(struct fib6_node *root,
  328. const struct in6_addr *daddr, int dst_len,
  329. const struct in6_addr *saddr, int src_len,
  330. bool exact_match);
  331. void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *arg),
  332. void *arg);
  333. int fib6_add(struct fib6_node *root, struct fib6_info *rt,
  334. struct nl_info *info, struct netlink_ext_ack *extack);
  335. int fib6_del(struct fib6_info *rt, struct nl_info *info);
  336. static inline struct net_device *fib6_info_nh_dev(const struct fib6_info *f6i)
  337. {
  338. return f6i->fib6_nh.nh_dev;
  339. }
  340. static inline
  341. struct lwtunnel_state *fib6_info_nh_lwt(const struct fib6_info *f6i)
  342. {
  343. return f6i->fib6_nh.nh_lwtstate;
  344. }
  345. void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
  346. unsigned int flags);
  347. void fib6_run_gc(unsigned long expires, struct net *net, bool force);
  348. void fib6_gc_cleanup(void);
  349. int fib6_init(void);
  350. struct ipv6_route_iter {
  351. struct seq_net_private p;
  352. struct fib6_walker w;
  353. loff_t skip;
  354. struct fib6_table *tbl;
  355. int sernum;
  356. };
  357. extern const struct seq_operations ipv6_route_seq_ops;
  358. int call_fib6_notifier(struct notifier_block *nb, struct net *net,
  359. enum fib_event_type event_type,
  360. struct fib_notifier_info *info);
  361. int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
  362. struct fib_notifier_info *info);
  363. int __net_init fib6_notifier_init(struct net *net);
  364. void __net_exit fib6_notifier_exit(struct net *net);
  365. unsigned int fib6_tables_seq_read(struct net *net);
  366. int fib6_tables_dump(struct net *net, struct notifier_block *nb);
  367. void fib6_update_sernum(struct net *net, struct fib6_info *rt);
  368. void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt);
  369. void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val);
  370. static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
  371. {
  372. return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
  373. }
  374. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  375. int fib6_rules_init(void);
  376. void fib6_rules_cleanup(void);
  377. bool fib6_rule_default(const struct fib_rule *rule);
  378. int fib6_rules_dump(struct net *net, struct notifier_block *nb);
  379. unsigned int fib6_rules_seq_read(struct net *net);
  380. static inline bool fib6_rules_early_flow_dissect(struct net *net,
  381. struct sk_buff *skb,
  382. struct flowi6 *fl6,
  383. struct flow_keys *flkeys)
  384. {
  385. unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
  386. if (!net->ipv6.fib6_rules_require_fldissect)
  387. return false;
  388. skb_flow_dissect_flow_keys(skb, flkeys, flag);
  389. fl6->fl6_sport = flkeys->ports.src;
  390. fl6->fl6_dport = flkeys->ports.dst;
  391. fl6->flowi6_proto = flkeys->basic.ip_proto;
  392. return true;
  393. }
  394. #else
  395. static inline int fib6_rules_init(void)
  396. {
  397. return 0;
  398. }
  399. static inline void fib6_rules_cleanup(void)
  400. {
  401. return ;
  402. }
  403. static inline bool fib6_rule_default(const struct fib_rule *rule)
  404. {
  405. return true;
  406. }
  407. static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb)
  408. {
  409. return 0;
  410. }
  411. static inline unsigned int fib6_rules_seq_read(struct net *net)
  412. {
  413. return 0;
  414. }
  415. static inline bool fib6_rules_early_flow_dissect(struct net *net,
  416. struct sk_buff *skb,
  417. struct flowi6 *fl6,
  418. struct flow_keys *flkeys)
  419. {
  420. return false;
  421. }
  422. #endif
  423. #endif