ip_fib.h 7.1 KB

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