ip_fib.h 6.8 KB

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