ip_fib.h 9.0 KB

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