ip_fib.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_NET_CLS_ROUTE
  54. __u32 nh_tclassid;
  55. #endif
  56. int nh_oif;
  57. __be32 nh_gw;
  58. };
  59. /*
  60. * This structure contains data shared by many of routes.
  61. */
  62. struct fib_info {
  63. struct hlist_node fib_hash;
  64. struct hlist_node fib_lhash;
  65. struct net *fib_net;
  66. int fib_treeref;
  67. atomic_t fib_clntref;
  68. int fib_dead;
  69. unsigned fib_flags;
  70. int fib_protocol;
  71. __be32 fib_prefsrc;
  72. u32 fib_priority;
  73. u32 fib_metrics[RTAX_MAX];
  74. #define fib_mtu fib_metrics[RTAX_MTU-1]
  75. #define fib_window fib_metrics[RTAX_WINDOW-1]
  76. #define fib_rtt fib_metrics[RTAX_RTT-1]
  77. #define fib_advmss fib_metrics[RTAX_ADVMSS-1]
  78. int fib_nhs;
  79. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  80. int fib_power;
  81. #endif
  82. struct fib_nh fib_nh[0];
  83. #define fib_dev fib_nh[0].nh_dev
  84. };
  85. #ifdef CONFIG_IP_MULTIPLE_TABLES
  86. struct fib_rule;
  87. #endif
  88. struct fib_result {
  89. unsigned char prefixlen;
  90. unsigned char nh_sel;
  91. unsigned char type;
  92. unsigned char scope;
  93. struct fib_info *fi;
  94. #ifdef CONFIG_IP_MULTIPLE_TABLES
  95. struct fib_rule *r;
  96. #endif
  97. };
  98. struct fib_result_nl {
  99. __be32 fl_addr; /* To be looked up*/
  100. u32 fl_mark;
  101. unsigned char fl_tos;
  102. unsigned char fl_scope;
  103. unsigned char tb_id_in;
  104. unsigned char tb_id; /* Results */
  105. unsigned char prefixlen;
  106. unsigned char nh_sel;
  107. unsigned char type;
  108. unsigned char scope;
  109. int err;
  110. };
  111. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  112. #define FIB_RES_NH(res) ((res).fi->fib_nh[(res).nh_sel])
  113. #define FIB_TABLE_HASHSZ 2
  114. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  115. #define FIB_RES_NH(res) ((res).fi->fib_nh[0])
  116. #define FIB_TABLE_HASHSZ 256
  117. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  118. #define FIB_RES_PREFSRC(res) ((res).fi->fib_prefsrc ? : __fib_res_prefsrc(&res))
  119. #define FIB_RES_GW(res) (FIB_RES_NH(res).nh_gw)
  120. #define FIB_RES_DEV(res) (FIB_RES_NH(res).nh_dev)
  121. #define FIB_RES_OIF(res) (FIB_RES_NH(res).nh_oif)
  122. struct fib_table {
  123. struct hlist_node tb_hlist;
  124. u32 tb_id;
  125. int tb_default;
  126. unsigned char tb_data[0];
  127. };
  128. extern int fib_table_lookup(struct fib_table *tb, const struct flowi *flp,
  129. struct fib_result *res);
  130. extern int fib_table_insert(struct fib_table *, struct fib_config *);
  131. extern int fib_table_delete(struct fib_table *, struct fib_config *);
  132. extern int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
  133. struct netlink_callback *cb);
  134. extern int fib_table_flush(struct fib_table *table);
  135. extern void fib_table_select_default(struct fib_table *table,
  136. const struct flowi *flp,
  137. struct fib_result *res);
  138. #ifndef CONFIG_IP_MULTIPLE_TABLES
  139. #define TABLE_LOCAL_INDEX 0
  140. #define TABLE_MAIN_INDEX 1
  141. static inline struct fib_table *fib_get_table(struct net *net, u32 id)
  142. {
  143. struct hlist_head *ptr;
  144. ptr = id == RT_TABLE_LOCAL ?
  145. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
  146. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
  147. return hlist_entry(ptr->first, struct fib_table, tb_hlist);
  148. }
  149. static inline struct fib_table *fib_new_table(struct net *net, u32 id)
  150. {
  151. return fib_get_table(net, id);
  152. }
  153. static inline int fib_lookup(struct net *net, const struct flowi *flp,
  154. struct fib_result *res)
  155. {
  156. struct fib_table *table;
  157. table = fib_get_table(net, RT_TABLE_LOCAL);
  158. if (!fib_table_lookup(table, flp, res))
  159. return 0;
  160. table = fib_get_table(net, RT_TABLE_MAIN);
  161. if (!fib_table_lookup(table, flp, res))
  162. return 0;
  163. return -ENETUNREACH;
  164. }
  165. #else /* CONFIG_IP_MULTIPLE_TABLES */
  166. extern int __net_init fib4_rules_init(struct net *net);
  167. extern void __net_exit fib4_rules_exit(struct net *net);
  168. #ifdef CONFIG_NET_CLS_ROUTE
  169. extern u32 fib_rules_tclass(struct fib_result *res);
  170. #endif
  171. extern int fib_lookup(struct net *n, struct flowi *flp, struct fib_result *res);
  172. extern struct fib_table *fib_new_table(struct net *net, u32 id);
  173. extern struct fib_table *fib_get_table(struct net *net, u32 id);
  174. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  175. /* Exported by fib_frontend.c */
  176. extern const struct nla_policy rtm_ipv4_policy[];
  177. extern void ip_fib_init(void);
  178. extern int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
  179. struct net_device *dev, __be32 *spec_dst,
  180. u32 *itag, u32 mark);
  181. extern void fib_select_default(struct net *net, const struct flowi *flp,
  182. struct fib_result *res);
  183. /* Exported by fib_semantics.c */
  184. extern int ip_fib_check_default(__be32 gw, struct net_device *dev);
  185. extern int fib_sync_down_dev(struct net_device *dev, int force);
  186. extern int fib_sync_down_addr(struct net *net, __be32 local);
  187. extern int fib_sync_up(struct net_device *dev);
  188. extern __be32 __fib_res_prefsrc(struct fib_result *res);
  189. extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
  190. /* Exported by fib_{hash|trie}.c */
  191. extern void fib_hash_init(void);
  192. extern struct fib_table *fib_hash_table(u32 id);
  193. static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
  194. {
  195. #ifdef CONFIG_NET_CLS_ROUTE
  196. #ifdef CONFIG_IP_MULTIPLE_TABLES
  197. u32 rtag;
  198. #endif
  199. *itag = FIB_RES_NH(*res).nh_tclassid<<16;
  200. #ifdef CONFIG_IP_MULTIPLE_TABLES
  201. rtag = fib_rules_tclass(res);
  202. if (*itag == 0)
  203. *itag = (rtag<<16);
  204. *itag |= (rtag>>16);
  205. #endif
  206. #endif
  207. }
  208. extern void free_fib_info(struct fib_info *fi);
  209. static inline void fib_info_put(struct fib_info *fi)
  210. {
  211. if (atomic_dec_and_test(&fi->fib_clntref))
  212. free_fib_info(fi);
  213. }
  214. static inline void fib_res_put(struct fib_result *res)
  215. {
  216. if (res->fi)
  217. fib_info_put(res->fi);
  218. #ifdef CONFIG_IP_MULTIPLE_TABLES
  219. if (res->r)
  220. fib_rule_put(res->r);
  221. #endif
  222. }
  223. #ifdef CONFIG_PROC_FS
  224. extern int __net_init fib_proc_init(struct net *net);
  225. extern void __net_exit fib_proc_exit(struct net *net);
  226. #else
  227. static inline int fib_proc_init(struct net *net)
  228. {
  229. return 0;
  230. }
  231. static inline void fib_proc_exit(struct net *net)
  232. {
  233. }
  234. #endif
  235. #endif /* _NET_FIB_H */