ip_fib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Definitions for the Forwarding Information Base.
  7. *
  8. * Authors: A.N.Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #ifndef _NET_IP_FIB_H
  16. #define _NET_IP_FIB_H
  17. #include <net/flow.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/rcupdate.h>
  20. #include <net/fib_notifier.h>
  21. #include <net/fib_rules.h>
  22. #include <net/inetpeer.h>
  23. #include <linux/percpu.h>
  24. #include <linux/notifier.h>
  25. #include <linux/refcount.h>
  26. struct fib_config {
  27. u8 fc_dst_len;
  28. u8 fc_tos;
  29. u8 fc_protocol;
  30. u8 fc_scope;
  31. u8 fc_type;
  32. /* 3 bytes unused */
  33. u32 fc_table;
  34. __be32 fc_dst;
  35. __be32 fc_gw;
  36. int fc_oif;
  37. u32 fc_flags;
  38. u32 fc_priority;
  39. __be32 fc_prefsrc;
  40. struct nlattr *fc_mx;
  41. struct rtnexthop *fc_mp;
  42. int fc_mx_len;
  43. int fc_mp_len;
  44. u32 fc_flow;
  45. u32 fc_nlflags;
  46. struct nl_info fc_nlinfo;
  47. struct nlattr *fc_encap;
  48. u16 fc_encap_type;
  49. };
  50. struct fib_info;
  51. struct rtable;
  52. struct fib_nh_exception {
  53. struct fib_nh_exception __rcu *fnhe_next;
  54. int fnhe_genid;
  55. __be32 fnhe_daddr;
  56. u32 fnhe_pmtu;
  57. bool fnhe_mtu_locked;
  58. __be32 fnhe_gw;
  59. unsigned long fnhe_expires;
  60. struct rtable __rcu *fnhe_rth_input;
  61. struct rtable __rcu *fnhe_rth_output;
  62. unsigned long fnhe_stamp;
  63. struct rcu_head rcu;
  64. };
  65. struct fnhe_hash_bucket {
  66. struct fib_nh_exception __rcu *chain;
  67. };
  68. #define FNHE_HASH_SHIFT 11
  69. #define FNHE_HASH_SIZE (1 << FNHE_HASH_SHIFT)
  70. #define FNHE_RECLAIM_DEPTH 5
  71. struct fib_nh {
  72. struct net_device *nh_dev;
  73. struct hlist_node nh_hash;
  74. struct fib_info *nh_parent;
  75. unsigned int nh_flags;
  76. unsigned char nh_scope;
  77. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  78. int nh_weight;
  79. atomic_t nh_upper_bound;
  80. #endif
  81. #ifdef CONFIG_IP_ROUTE_CLASSID
  82. __u32 nh_tclassid;
  83. #endif
  84. int nh_oif;
  85. __be32 nh_gw;
  86. __be32 nh_saddr;
  87. int nh_saddr_genid;
  88. struct rtable __rcu * __percpu *nh_pcpu_rth_output;
  89. struct rtable __rcu *nh_rth_input;
  90. struct fnhe_hash_bucket __rcu *nh_exceptions;
  91. struct lwtunnel_state *nh_lwtstate;
  92. };
  93. /*
  94. * This structure contains data shared by many of routes.
  95. */
  96. struct fib_info {
  97. struct hlist_node fib_hash;
  98. struct hlist_node fib_lhash;
  99. struct net *fib_net;
  100. int fib_treeref;
  101. refcount_t fib_clntref;
  102. unsigned int fib_flags;
  103. unsigned char fib_dead;
  104. unsigned char fib_protocol;
  105. unsigned char fib_scope;
  106. unsigned char fib_type;
  107. __be32 fib_prefsrc;
  108. u32 fib_tb_id;
  109. u32 fib_priority;
  110. struct dst_metrics *fib_metrics;
  111. #define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
  112. #define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
  113. #define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
  114. #define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
  115. int fib_nhs;
  116. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  117. int fib_weight;
  118. #endif
  119. struct rcu_head rcu;
  120. struct fib_nh fib_nh[0];
  121. #define fib_dev fib_nh[0].nh_dev
  122. };
  123. #ifdef CONFIG_IP_MULTIPLE_TABLES
  124. struct fib_rule;
  125. #endif
  126. struct fib_table;
  127. struct fib_result {
  128. __be32 prefix;
  129. unsigned char prefixlen;
  130. unsigned char nh_sel;
  131. unsigned char type;
  132. unsigned char scope;
  133. u32 tclassid;
  134. struct fib_info *fi;
  135. struct fib_table *table;
  136. struct hlist_head *fa_head;
  137. };
  138. struct fib_result_nl {
  139. __be32 fl_addr; /* To be looked up*/
  140. u32 fl_mark;
  141. unsigned char fl_tos;
  142. unsigned char fl_scope;
  143. unsigned char tb_id_in;
  144. unsigned char tb_id; /* Results */
  145. unsigned char prefixlen;
  146. unsigned char nh_sel;
  147. unsigned char type;
  148. unsigned char scope;
  149. int err;
  150. };
  151. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  152. #define FIB_RES_NH(res) ((res).fi->fib_nh[(res).nh_sel])
  153. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  154. #define FIB_RES_NH(res) ((res).fi->fib_nh[0])
  155. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  156. #ifdef CONFIG_IP_MULTIPLE_TABLES
  157. #define FIB_TABLE_HASHSZ 256
  158. #else
  159. #define FIB_TABLE_HASHSZ 2
  160. #endif
  161. __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh);
  162. #define FIB_RES_SADDR(net, res) \
  163. ((FIB_RES_NH(res).nh_saddr_genid == \
  164. atomic_read(&(net)->ipv4.dev_addr_genid)) ? \
  165. FIB_RES_NH(res).nh_saddr : \
  166. fib_info_update_nh_saddr((net), &FIB_RES_NH(res)))
  167. #define FIB_RES_GW(res) (FIB_RES_NH(res).nh_gw)
  168. #define FIB_RES_DEV(res) (FIB_RES_NH(res).nh_dev)
  169. #define FIB_RES_OIF(res) (FIB_RES_NH(res).nh_oif)
  170. #define FIB_RES_PREFSRC(net, res) ((res).fi->fib_prefsrc ? : \
  171. FIB_RES_SADDR(net, res))
  172. struct fib_entry_notifier_info {
  173. struct fib_notifier_info info; /* must be first */
  174. u32 dst;
  175. int dst_len;
  176. struct fib_info *fi;
  177. u8 tos;
  178. u8 type;
  179. u32 tb_id;
  180. };
  181. struct fib_nh_notifier_info {
  182. struct fib_notifier_info info; /* must be first */
  183. struct fib_nh *fib_nh;
  184. };
  185. int call_fib4_notifier(struct notifier_block *nb, struct net *net,
  186. enum fib_event_type event_type,
  187. struct fib_notifier_info *info);
  188. int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
  189. struct fib_notifier_info *info);
  190. int __net_init fib4_notifier_init(struct net *net);
  191. void __net_exit fib4_notifier_exit(struct net *net);
  192. void fib_notify(struct net *net, struct notifier_block *nb);
  193. struct fib_table {
  194. struct hlist_node tb_hlist;
  195. u32 tb_id;
  196. int tb_num_default;
  197. struct rcu_head rcu;
  198. unsigned long *tb_data;
  199. unsigned long __data[0];
  200. };
  201. int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
  202. struct fib_result *res, int fib_flags);
  203. int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
  204. struct netlink_ext_ack *extack);
  205. int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
  206. struct netlink_ext_ack *extack);
  207. int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
  208. struct netlink_callback *cb);
  209. int fib_table_flush(struct net *net, struct fib_table *table, bool flush_all);
  210. struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
  211. void fib_table_flush_external(struct fib_table *table);
  212. void fib_free_table(struct fib_table *tb);
  213. #ifndef CONFIG_IP_MULTIPLE_TABLES
  214. #define TABLE_LOCAL_INDEX (RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
  215. #define TABLE_MAIN_INDEX (RT_TABLE_MAIN & (FIB_TABLE_HASHSZ - 1))
  216. static inline struct fib_table *fib_get_table(struct net *net, u32 id)
  217. {
  218. struct hlist_node *tb_hlist;
  219. struct hlist_head *ptr;
  220. ptr = id == RT_TABLE_LOCAL ?
  221. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
  222. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
  223. tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
  224. return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
  225. }
  226. static inline struct fib_table *fib_new_table(struct net *net, u32 id)
  227. {
  228. return fib_get_table(net, id);
  229. }
  230. static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
  231. struct fib_result *res, unsigned int flags)
  232. {
  233. struct fib_table *tb;
  234. int err = -ENETUNREACH;
  235. rcu_read_lock();
  236. tb = fib_get_table(net, RT_TABLE_MAIN);
  237. if (tb)
  238. err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
  239. if (err == -EAGAIN)
  240. err = -ENETUNREACH;
  241. rcu_read_unlock();
  242. return err;
  243. }
  244. static inline bool fib4_rule_default(const struct fib_rule *rule)
  245. {
  246. return true;
  247. }
  248. static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb)
  249. {
  250. return 0;
  251. }
  252. static inline unsigned int fib4_rules_seq_read(struct net *net)
  253. {
  254. return 0;
  255. }
  256. #else /* CONFIG_IP_MULTIPLE_TABLES */
  257. int __net_init fib4_rules_init(struct net *net);
  258. void __net_exit fib4_rules_exit(struct net *net);
  259. struct fib_table *fib_new_table(struct net *net, u32 id);
  260. struct fib_table *fib_get_table(struct net *net, u32 id);
  261. int __fib_lookup(struct net *net, struct flowi4 *flp,
  262. struct fib_result *res, unsigned int flags);
  263. static inline int fib_lookup(struct net *net, struct flowi4 *flp,
  264. struct fib_result *res, unsigned int flags)
  265. {
  266. struct fib_table *tb;
  267. int err = -ENETUNREACH;
  268. flags |= FIB_LOOKUP_NOREF;
  269. if (net->ipv4.fib_has_custom_rules)
  270. return __fib_lookup(net, flp, res, flags);
  271. rcu_read_lock();
  272. res->tclassid = 0;
  273. tb = rcu_dereference_rtnl(net->ipv4.fib_main);
  274. if (tb)
  275. err = fib_table_lookup(tb, flp, res, flags);
  276. if (!err)
  277. goto out;
  278. tb = rcu_dereference_rtnl(net->ipv4.fib_default);
  279. if (tb)
  280. err = fib_table_lookup(tb, flp, res, flags);
  281. out:
  282. if (err == -EAGAIN)
  283. err = -ENETUNREACH;
  284. rcu_read_unlock();
  285. return err;
  286. }
  287. bool fib4_rule_default(const struct fib_rule *rule);
  288. int fib4_rules_dump(struct net *net, struct notifier_block *nb);
  289. unsigned int fib4_rules_seq_read(struct net *net);
  290. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  291. /* Exported by fib_frontend.c */
  292. extern const struct nla_policy rtm_ipv4_policy[];
  293. void ip_fib_init(void);
  294. __be32 fib_compute_spec_dst(struct sk_buff *skb);
  295. int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
  296. u8 tos, int oif, struct net_device *dev,
  297. struct in_device *idev, u32 *itag);
  298. #ifdef CONFIG_IP_ROUTE_CLASSID
  299. static inline int fib_num_tclassid_users(struct net *net)
  300. {
  301. return net->ipv4.fib_num_tclassid_users;
  302. }
  303. #else
  304. static inline int fib_num_tclassid_users(struct net *net)
  305. {
  306. return 0;
  307. }
  308. #endif
  309. int fib_unmerge(struct net *net);
  310. /* Exported by fib_semantics.c */
  311. int ip_fib_check_default(__be32 gw, struct net_device *dev);
  312. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
  313. int fib_sync_down_addr(struct net_device *dev, __be32 local);
  314. int fib_sync_up(struct net_device *dev, unsigned int nh_flags);
  315. void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
  316. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  317. int fib_multipath_hash(const struct fib_info *fi, const struct flowi4 *fl4,
  318. const struct sk_buff *skb);
  319. #endif
  320. void fib_select_multipath(struct fib_result *res, int hash);
  321. void fib_select_path(struct net *net, struct fib_result *res,
  322. struct flowi4 *fl4, const struct sk_buff *skb);
  323. /* Exported by fib_trie.c */
  324. void fib_trie_init(void);
  325. struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
  326. static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
  327. {
  328. #ifdef CONFIG_IP_ROUTE_CLASSID
  329. #ifdef CONFIG_IP_MULTIPLE_TABLES
  330. u32 rtag;
  331. #endif
  332. *itag = FIB_RES_NH(*res).nh_tclassid<<16;
  333. #ifdef CONFIG_IP_MULTIPLE_TABLES
  334. rtag = res->tclassid;
  335. if (*itag == 0)
  336. *itag = (rtag<<16);
  337. *itag |= (rtag>>16);
  338. #endif
  339. #endif
  340. }
  341. void free_fib_info(struct fib_info *fi);
  342. static inline void fib_info_hold(struct fib_info *fi)
  343. {
  344. refcount_inc(&fi->fib_clntref);
  345. }
  346. static inline void fib_info_put(struct fib_info *fi)
  347. {
  348. if (refcount_dec_and_test(&fi->fib_clntref))
  349. free_fib_info(fi);
  350. }
  351. #ifdef CONFIG_PROC_FS
  352. int __net_init fib_proc_init(struct net *net);
  353. void __net_exit fib_proc_exit(struct net *net);
  354. #else
  355. static inline int fib_proc_init(struct net *net)
  356. {
  357. return 0;
  358. }
  359. static inline void fib_proc_exit(struct net *net)
  360. {
  361. }
  362. #endif
  363. #endif /* _NET_FIB_H */