ip_fib.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_mp_alg;
  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 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_NET_CLS_ROUTE
  55. __u32 nh_tclassid;
  56. #endif
  57. int nh_oif;
  58. __be32 nh_gw;
  59. };
  60. /*
  61. * This structure contains data shared by many of routes.
  62. */
  63. struct fib_info {
  64. struct hlist_node fib_hash;
  65. struct hlist_node fib_lhash;
  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. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  83. u32 fib_mp_alg;
  84. #endif
  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_result {
  92. unsigned char prefixlen;
  93. unsigned char nh_sel;
  94. unsigned char type;
  95. unsigned char scope;
  96. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  97. __be32 network;
  98. __be32 netmask;
  99. #endif
  100. struct fib_info *fi;
  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_fwmark;
  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_RES_RESET(res) ((res).nh_sel = 0)
  121. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  122. #define FIB_RES_NH(res) ((res).fi->fib_nh[0])
  123. #define FIB_RES_RESET(res)
  124. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  125. #define FIB_RES_PREFSRC(res) ((res).fi->fib_prefsrc ? : __fib_res_prefsrc(&res))
  126. #define FIB_RES_GW(res) (FIB_RES_NH(res).nh_gw)
  127. #define FIB_RES_DEV(res) (FIB_RES_NH(res).nh_dev)
  128. #define FIB_RES_OIF(res) (FIB_RES_NH(res).nh_oif)
  129. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  130. #define FIB_RES_NETWORK(res) ((res).network)
  131. #define FIB_RES_NETMASK(res) ((res).netmask)
  132. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  133. #define FIB_RES_NETWORK(res) (0)
  134. #define FIB_RES_NETMASK(res) (0)
  135. #endif /* CONFIG_IP_ROUTE_MULTIPATH_WRANDOM */
  136. struct fib_table {
  137. struct hlist_node tb_hlist;
  138. u32 tb_id;
  139. unsigned tb_stamp;
  140. int (*tb_lookup)(struct fib_table *tb, const struct flowi *flp, struct fib_result *res);
  141. int (*tb_insert)(struct fib_table *, struct fib_config *);
  142. int (*tb_delete)(struct fib_table *, struct fib_config *);
  143. int (*tb_dump)(struct fib_table *table, struct sk_buff *skb,
  144. struct netlink_callback *cb);
  145. int (*tb_flush)(struct fib_table *table);
  146. void (*tb_select_default)(struct fib_table *table,
  147. const struct flowi *flp, struct fib_result *res);
  148. unsigned char tb_data[0];
  149. };
  150. #ifndef CONFIG_IP_MULTIPLE_TABLES
  151. extern struct fib_table *ip_fib_local_table;
  152. extern struct fib_table *ip_fib_main_table;
  153. static inline struct fib_table *fib_get_table(u32 id)
  154. {
  155. if (id != RT_TABLE_LOCAL)
  156. return ip_fib_main_table;
  157. return ip_fib_local_table;
  158. }
  159. static inline struct fib_table *fib_new_table(u32 id)
  160. {
  161. return fib_get_table(id);
  162. }
  163. static inline int fib_lookup(const struct flowi *flp, struct fib_result *res)
  164. {
  165. if (ip_fib_local_table->tb_lookup(ip_fib_local_table, flp, res) &&
  166. ip_fib_main_table->tb_lookup(ip_fib_main_table, flp, res))
  167. return -ENETUNREACH;
  168. return 0;
  169. }
  170. static inline void fib_select_default(const struct flowi *flp, struct fib_result *res)
  171. {
  172. if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  173. ip_fib_main_table->tb_select_default(ip_fib_main_table, flp, res);
  174. }
  175. #else /* CONFIG_IP_MULTIPLE_TABLES */
  176. #define ip_fib_local_table fib_get_table(RT_TABLE_LOCAL)
  177. #define ip_fib_main_table fib_get_table(RT_TABLE_MAIN)
  178. extern int fib_lookup(struct flowi *flp, struct fib_result *res);
  179. extern struct fib_table *fib_new_table(u32 id);
  180. extern struct fib_table *fib_get_table(u32 id);
  181. extern void fib_select_default(const struct flowi *flp, struct fib_result *res);
  182. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  183. /* Exported by fib_frontend.c */
  184. extern struct nla_policy rtm_ipv4_policy[];
  185. extern void ip_fib_init(void);
  186. extern int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  187. extern int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  188. extern int inet_rtm_getroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  189. extern int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb);
  190. extern int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
  191. struct net_device *dev, __be32 *spec_dst, u32 *itag);
  192. extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
  193. struct rtentry;
  194. /* Exported by fib_semantics.c */
  195. extern int ip_fib_check_default(__be32 gw, struct net_device *dev);
  196. extern int fib_sync_down(__be32 local, struct net_device *dev, int force);
  197. extern int fib_sync_up(struct net_device *dev);
  198. extern __be32 __fib_res_prefsrc(struct fib_result *res);
  199. /* Exported by fib_hash.c */
  200. extern struct fib_table *fib_hash_init(u32 id);
  201. #ifdef CONFIG_IP_MULTIPLE_TABLES
  202. extern int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb);
  203. extern void __init fib4_rules_init(void);
  204. #ifdef CONFIG_NET_CLS_ROUTE
  205. extern u32 fib_rules_tclass(struct fib_result *res);
  206. #endif
  207. #endif
  208. static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
  209. {
  210. #ifdef CONFIG_NET_CLS_ROUTE
  211. #ifdef CONFIG_IP_MULTIPLE_TABLES
  212. u32 rtag;
  213. #endif
  214. *itag = FIB_RES_NH(*res).nh_tclassid<<16;
  215. #ifdef CONFIG_IP_MULTIPLE_TABLES
  216. rtag = fib_rules_tclass(res);
  217. if (*itag == 0)
  218. *itag = (rtag<<16);
  219. *itag |= (rtag>>16);
  220. #endif
  221. #endif
  222. }
  223. extern void free_fib_info(struct fib_info *fi);
  224. static inline void fib_info_put(struct fib_info *fi)
  225. {
  226. if (atomic_dec_and_test(&fi->fib_clntref))
  227. free_fib_info(fi);
  228. }
  229. static inline void fib_res_put(struct fib_result *res)
  230. {
  231. if (res->fi)
  232. fib_info_put(res->fi);
  233. #ifdef CONFIG_IP_MULTIPLE_TABLES
  234. if (res->r)
  235. fib_rule_put(res->r);
  236. #endif
  237. }
  238. #ifdef CONFIG_PROC_FS
  239. extern int fib_proc_init(void);
  240. extern void fib_proc_exit(void);
  241. #endif
  242. #endif /* _NET_FIB_H */