ip_fib.h 8.4 KB

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