ip_fib.h 7.4 KB

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