ip_fib.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_rules.h>
  21. #include <net/inetpeer.h>
  22. #include <linux/percpu.h>
  23. struct fib_config {
  24. u8 fc_dst_len;
  25. u8 fc_tos;
  26. u8 fc_protocol;
  27. u8 fc_scope;
  28. u8 fc_type;
  29. /* 3 bytes unused */
  30. u32 fc_table;
  31. __be32 fc_dst;
  32. __be32 fc_gw;
  33. int fc_oif;
  34. u32 fc_flags;
  35. u32 fc_priority;
  36. __be32 fc_prefsrc;
  37. struct nlattr *fc_mx;
  38. struct rtnexthop *fc_mp;
  39. int fc_mx_len;
  40. int fc_mp_len;
  41. u32 fc_flow;
  42. u32 fc_nlflags;
  43. struct nl_info fc_nlinfo;
  44. struct nlattr *fc_encap;
  45. u16 fc_encap_type;
  46. };
  47. struct fib_info;
  48. struct rtable;
  49. struct fib_nh_exception {
  50. struct fib_nh_exception __rcu *fnhe_next;
  51. int fnhe_genid;
  52. __be32 fnhe_daddr;
  53. u32 fnhe_pmtu;
  54. bool fnhe_mtu_locked;
  55. __be32 fnhe_gw;
  56. unsigned long fnhe_expires;
  57. struct rtable __rcu *fnhe_rth_input;
  58. struct rtable __rcu *fnhe_rth_output;
  59. unsigned long fnhe_stamp;
  60. struct rcu_head rcu;
  61. };
  62. struct fnhe_hash_bucket {
  63. struct fib_nh_exception __rcu *chain;
  64. };
  65. #define FNHE_HASH_SHIFT 11
  66. #define FNHE_HASH_SIZE (1 << FNHE_HASH_SHIFT)
  67. #define FNHE_RECLAIM_DEPTH 5
  68. struct fib_nh {
  69. struct net_device *nh_dev;
  70. struct hlist_node nh_hash;
  71. struct fib_info *nh_parent;
  72. unsigned int nh_flags;
  73. unsigned char nh_scope;
  74. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  75. int nh_weight;
  76. atomic_t nh_upper_bound;
  77. #endif
  78. #ifdef CONFIG_IP_ROUTE_CLASSID
  79. __u32 nh_tclassid;
  80. #endif
  81. int nh_oif;
  82. __be32 nh_gw;
  83. __be32 nh_saddr;
  84. int nh_saddr_genid;
  85. struct rtable __rcu * __percpu *nh_pcpu_rth_output;
  86. struct rtable __rcu *nh_rth_input;
  87. struct fnhe_hash_bucket __rcu *nh_exceptions;
  88. struct lwtunnel_state *nh_lwtstate;
  89. };
  90. /*
  91. * This structure contains data shared by many of routes.
  92. */
  93. struct fib_info {
  94. struct hlist_node fib_hash;
  95. struct hlist_node fib_lhash;
  96. struct net *fib_net;
  97. int fib_treeref;
  98. atomic_t fib_clntref;
  99. unsigned int fib_flags;
  100. unsigned char fib_dead;
  101. unsigned char fib_protocol;
  102. unsigned char fib_scope;
  103. unsigned char fib_type;
  104. __be32 fib_prefsrc;
  105. u32 fib_priority;
  106. struct dst_metrics *fib_metrics;
  107. #define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
  108. #define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
  109. #define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
  110. #define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
  111. int fib_nhs;
  112. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  113. int fib_weight;
  114. #endif
  115. struct rcu_head rcu;
  116. struct fib_nh fib_nh[0];
  117. #define fib_dev fib_nh[0].nh_dev
  118. };
  119. #ifdef CONFIG_IP_MULTIPLE_TABLES
  120. struct fib_rule;
  121. #endif
  122. struct fib_table;
  123. struct fib_result {
  124. unsigned char prefixlen;
  125. unsigned char nh_sel;
  126. unsigned char type;
  127. unsigned char scope;
  128. u32 tclassid;
  129. struct fib_info *fi;
  130. struct fib_table *table;
  131. struct hlist_head *fa_head;
  132. };
  133. struct fib_result_nl {
  134. __be32 fl_addr; /* To be looked up*/
  135. u32 fl_mark;
  136. unsigned char fl_tos;
  137. unsigned char fl_scope;
  138. unsigned char tb_id_in;
  139. unsigned char tb_id; /* Results */
  140. unsigned char prefixlen;
  141. unsigned char nh_sel;
  142. unsigned char type;
  143. unsigned char scope;
  144. int err;
  145. };
  146. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  147. #define FIB_RES_NH(res) ((res).fi->fib_nh[(res).nh_sel])
  148. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  149. #define FIB_RES_NH(res) ((res).fi->fib_nh[0])
  150. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  151. #ifdef CONFIG_IP_MULTIPLE_TABLES
  152. #define FIB_TABLE_HASHSZ 256
  153. #else
  154. #define FIB_TABLE_HASHSZ 2
  155. #endif
  156. __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh);
  157. #define FIB_RES_SADDR(net, res) \
  158. ((FIB_RES_NH(res).nh_saddr_genid == \
  159. atomic_read(&(net)->ipv4.dev_addr_genid)) ? \
  160. FIB_RES_NH(res).nh_saddr : \
  161. fib_info_update_nh_saddr((net), &FIB_RES_NH(res)))
  162. #define FIB_RES_GW(res) (FIB_RES_NH(res).nh_gw)
  163. #define FIB_RES_DEV(res) (FIB_RES_NH(res).nh_dev)
  164. #define FIB_RES_OIF(res) (FIB_RES_NH(res).nh_oif)
  165. #define FIB_RES_PREFSRC(net, res) ((res).fi->fib_prefsrc ? : \
  166. FIB_RES_SADDR(net, res))
  167. struct fib_table {
  168. struct hlist_node tb_hlist;
  169. u32 tb_id;
  170. int tb_num_default;
  171. struct rcu_head rcu;
  172. unsigned long *tb_data;
  173. unsigned long __data[0];
  174. };
  175. int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
  176. struct fib_result *res, int fib_flags);
  177. int fib_table_insert(struct fib_table *, struct fib_config *);
  178. int fib_table_delete(struct fib_table *, struct fib_config *);
  179. int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
  180. struct netlink_callback *cb);
  181. int fib_table_flush(struct fib_table *table, bool flush_all);
  182. struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
  183. void fib_table_flush_external(struct fib_table *table);
  184. void fib_free_table(struct fib_table *tb);
  185. #ifndef CONFIG_IP_MULTIPLE_TABLES
  186. #define TABLE_LOCAL_INDEX (RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
  187. #define TABLE_MAIN_INDEX (RT_TABLE_MAIN & (FIB_TABLE_HASHSZ - 1))
  188. static inline struct fib_table *fib_get_table(struct net *net, u32 id)
  189. {
  190. struct hlist_node *tb_hlist;
  191. struct hlist_head *ptr;
  192. ptr = id == RT_TABLE_LOCAL ?
  193. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
  194. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
  195. tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
  196. return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
  197. }
  198. static inline struct fib_table *fib_new_table(struct net *net, u32 id)
  199. {
  200. return fib_get_table(net, id);
  201. }
  202. static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
  203. struct fib_result *res, unsigned int flags)
  204. {
  205. struct fib_table *tb;
  206. int err = -ENETUNREACH;
  207. rcu_read_lock();
  208. tb = fib_get_table(net, RT_TABLE_MAIN);
  209. if (tb)
  210. err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
  211. if (err == -EAGAIN)
  212. err = -ENETUNREACH;
  213. rcu_read_unlock();
  214. return err;
  215. }
  216. #else /* CONFIG_IP_MULTIPLE_TABLES */
  217. int __net_init fib4_rules_init(struct net *net);
  218. void __net_exit fib4_rules_exit(struct net *net);
  219. struct fib_table *fib_new_table(struct net *net, u32 id);
  220. struct fib_table *fib_get_table(struct net *net, u32 id);
  221. int __fib_lookup(struct net *net, struct flowi4 *flp,
  222. struct fib_result *res, unsigned int flags);
  223. static inline int fib_lookup(struct net *net, struct flowi4 *flp,
  224. struct fib_result *res, unsigned int flags)
  225. {
  226. struct fib_table *tb;
  227. int err = -ENETUNREACH;
  228. flags |= FIB_LOOKUP_NOREF;
  229. if (net->ipv4.fib_has_custom_rules)
  230. return __fib_lookup(net, flp, res, flags);
  231. rcu_read_lock();
  232. res->tclassid = 0;
  233. tb = rcu_dereference_rtnl(net->ipv4.fib_main);
  234. if (tb)
  235. err = fib_table_lookup(tb, flp, res, flags);
  236. if (!err)
  237. goto out;
  238. tb = rcu_dereference_rtnl(net->ipv4.fib_default);
  239. if (tb)
  240. err = fib_table_lookup(tb, flp, res, flags);
  241. out:
  242. if (err == -EAGAIN)
  243. err = -ENETUNREACH;
  244. rcu_read_unlock();
  245. return err;
  246. }
  247. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  248. /* Exported by fib_frontend.c */
  249. extern const struct nla_policy rtm_ipv4_policy[];
  250. void ip_fib_init(void);
  251. __be32 fib_compute_spec_dst(struct sk_buff *skb);
  252. int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
  253. u8 tos, int oif, struct net_device *dev,
  254. struct in_device *idev, u32 *itag);
  255. void fib_select_default(const struct flowi4 *flp, struct fib_result *res);
  256. #ifdef CONFIG_IP_ROUTE_CLASSID
  257. static inline int fib_num_tclassid_users(struct net *net)
  258. {
  259. return net->ipv4.fib_num_tclassid_users;
  260. }
  261. #else
  262. static inline int fib_num_tclassid_users(struct net *net)
  263. {
  264. return 0;
  265. }
  266. #endif
  267. int fib_unmerge(struct net *net);
  268. void fib_flush_external(struct net *net);
  269. /* Exported by fib_semantics.c */
  270. int ip_fib_check_default(__be32 gw, struct net_device *dev);
  271. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
  272. int fib_sync_down_addr(struct net *net, __be32 local);
  273. int fib_sync_up(struct net_device *dev, unsigned int nh_flags);
  274. void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
  275. extern u32 fib_multipath_secret __read_mostly;
  276. static inline int fib_multipath_hash(__be32 saddr, __be32 daddr)
  277. {
  278. return jhash_2words(saddr, daddr, fib_multipath_secret) >> 1;
  279. }
  280. void fib_select_multipath(struct fib_result *res, int hash);
  281. void fib_select_path(struct net *net, struct fib_result *res,
  282. struct flowi4 *fl4, int mp_hash);
  283. /* Exported by fib_trie.c */
  284. void fib_trie_init(void);
  285. struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
  286. static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
  287. {
  288. #ifdef CONFIG_IP_ROUTE_CLASSID
  289. #ifdef CONFIG_IP_MULTIPLE_TABLES
  290. u32 rtag;
  291. #endif
  292. *itag = FIB_RES_NH(*res).nh_tclassid<<16;
  293. #ifdef CONFIG_IP_MULTIPLE_TABLES
  294. rtag = res->tclassid;
  295. if (*itag == 0)
  296. *itag = (rtag<<16);
  297. *itag |= (rtag>>16);
  298. #endif
  299. #endif
  300. }
  301. void free_fib_info(struct fib_info *fi);
  302. static inline void fib_info_put(struct fib_info *fi)
  303. {
  304. if (atomic_dec_and_test(&fi->fib_clntref))
  305. free_fib_info(fi);
  306. }
  307. #ifdef CONFIG_PROC_FS
  308. int __net_init fib_proc_init(struct net *net);
  309. void __net_exit fib_proc_exit(struct net *net);
  310. #else
  311. static inline int fib_proc_init(struct net *net)
  312. {
  313. return 0;
  314. }
  315. static inline void fib_proc_exit(struct net *net)
  316. {
  317. }
  318. #endif
  319. #endif /* _NET_FIB_H */