ip_fib.h 8.5 KB

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