ip_fib.h 9.3 KB

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