ip_fib.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_family;
  22. u8 fc_dst_len;
  23. u8 fc_src_len;
  24. u8 fc_tos;
  25. u8 fc_protocol;
  26. u8 fc_scope;
  27. u8 fc_type;
  28. /* 1 byte unused */
  29. u32 fc_table;
  30. u32 fc_dst;
  31. u32 fc_src;
  32. u32 fc_gw;
  33. int fc_oif;
  34. u32 fc_flags;
  35. u32 fc_priority;
  36. u32 fc_prefsrc;
  37. struct nlattr *fc_mx;
  38. struct rtnexthop *fc_mp;
  39. int fc_mx_len;
  40. int fc_mp_len;
  41. u32 fc_flow;
  42. u32 fc_mp_alg;
  43. u32 fc_nlflags;
  44. struct nl_info fc_nlinfo;
  45. };
  46. struct fib_info;
  47. struct fib_nh {
  48. struct net_device *nh_dev;
  49. struct hlist_node nh_hash;
  50. struct fib_info *nh_parent;
  51. unsigned nh_flags;
  52. unsigned char nh_scope;
  53. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  54. int nh_weight;
  55. int nh_power;
  56. #endif
  57. #ifdef CONFIG_NET_CLS_ROUTE
  58. __u32 nh_tclassid;
  59. #endif
  60. int nh_oif;
  61. u32 nh_gw;
  62. };
  63. /*
  64. * This structure contains data shared by many of routes.
  65. */
  66. struct fib_info {
  67. struct hlist_node fib_hash;
  68. struct hlist_node fib_lhash;
  69. int fib_treeref;
  70. atomic_t fib_clntref;
  71. int fib_dead;
  72. unsigned fib_flags;
  73. int fib_protocol;
  74. u32 fib_prefsrc;
  75. u32 fib_priority;
  76. u32 fib_metrics[RTAX_MAX];
  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. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  86. u32 fib_mp_alg;
  87. #endif
  88. struct fib_nh fib_nh[0];
  89. #define fib_dev fib_nh[0].nh_dev
  90. };
  91. #ifdef CONFIG_IP_MULTIPLE_TABLES
  92. struct fib_rule;
  93. #endif
  94. struct fib_result {
  95. unsigned char prefixlen;
  96. unsigned char nh_sel;
  97. unsigned char type;
  98. unsigned char scope;
  99. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  100. __u32 network;
  101. __u32 netmask;
  102. #endif
  103. struct fib_info *fi;
  104. #ifdef CONFIG_IP_MULTIPLE_TABLES
  105. struct fib_rule *r;
  106. #endif
  107. };
  108. struct fib_result_nl {
  109. u32 fl_addr; /* To be looked up*/
  110. u32 fl_fwmark;
  111. unsigned char fl_tos;
  112. unsigned char fl_scope;
  113. unsigned char tb_id_in;
  114. unsigned char tb_id; /* Results */
  115. unsigned char prefixlen;
  116. unsigned char nh_sel;
  117. unsigned char type;
  118. unsigned char scope;
  119. int err;
  120. };
  121. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  122. #define FIB_RES_NH(res) ((res).fi->fib_nh[(res).nh_sel])
  123. #define FIB_RES_RESET(res) ((res).nh_sel = 0)
  124. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  125. #define FIB_RES_NH(res) ((res).fi->fib_nh[0])
  126. #define FIB_RES_RESET(res)
  127. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  128. #define FIB_RES_PREFSRC(res) ((res).fi->fib_prefsrc ? : __fib_res_prefsrc(&res))
  129. #define FIB_RES_GW(res) (FIB_RES_NH(res).nh_gw)
  130. #define FIB_RES_DEV(res) (FIB_RES_NH(res).nh_dev)
  131. #define FIB_RES_OIF(res) (FIB_RES_NH(res).nh_oif)
  132. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  133. #define FIB_RES_NETWORK(res) ((res).network)
  134. #define FIB_RES_NETMASK(res) ((res).netmask)
  135. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  136. #define FIB_RES_NETWORK(res) (0)
  137. #define FIB_RES_NETMASK(res) (0)
  138. #endif /* CONFIG_IP_ROUTE_MULTIPATH_WRANDOM */
  139. struct fib_table {
  140. struct hlist_node tb_hlist;
  141. u32 tb_id;
  142. unsigned tb_stamp;
  143. int (*tb_lookup)(struct fib_table *tb, const struct flowi *flp, struct fib_result *res);
  144. int (*tb_insert)(struct fib_table *, struct fib_config *);
  145. int (*tb_delete)(struct fib_table *, struct fib_config *);
  146. int (*tb_dump)(struct fib_table *table, struct sk_buff *skb,
  147. struct netlink_callback *cb);
  148. int (*tb_flush)(struct fib_table *table);
  149. void (*tb_select_default)(struct fib_table *table,
  150. const struct flowi *flp, struct fib_result *res);
  151. unsigned char tb_data[0];
  152. };
  153. #ifndef CONFIG_IP_MULTIPLE_TABLES
  154. extern struct fib_table *ip_fib_local_table;
  155. extern struct fib_table *ip_fib_main_table;
  156. static inline struct fib_table *fib_get_table(u32 id)
  157. {
  158. if (id != RT_TABLE_LOCAL)
  159. return ip_fib_main_table;
  160. return ip_fib_local_table;
  161. }
  162. static inline struct fib_table *fib_new_table(u32 id)
  163. {
  164. return fib_get_table(id);
  165. }
  166. static inline int fib_lookup(const struct flowi *flp, struct fib_result *res)
  167. {
  168. if (ip_fib_local_table->tb_lookup(ip_fib_local_table, flp, res) &&
  169. ip_fib_main_table->tb_lookup(ip_fib_main_table, flp, res))
  170. return -ENETUNREACH;
  171. return 0;
  172. }
  173. static inline void fib_select_default(const struct flowi *flp, struct fib_result *res)
  174. {
  175. if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  176. ip_fib_main_table->tb_select_default(ip_fib_main_table, flp, res);
  177. }
  178. #else /* CONFIG_IP_MULTIPLE_TABLES */
  179. #define ip_fib_local_table fib_get_table(RT_TABLE_LOCAL)
  180. #define ip_fib_main_table fib_get_table(RT_TABLE_MAIN)
  181. extern int fib_lookup(struct flowi *flp, struct fib_result *res);
  182. extern struct fib_table *fib_new_table(u32 id);
  183. extern struct fib_table *fib_get_table(u32 id);
  184. extern void fib_select_default(const struct flowi *flp, struct fib_result *res);
  185. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  186. /* Exported by fib_frontend.c */
  187. extern struct nla_policy rtm_ipv4_policy[];
  188. extern void ip_fib_init(void);
  189. extern int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  190. extern int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  191. extern int inet_rtm_getroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  192. extern int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb);
  193. extern int fib_validate_source(u32 src, u32 dst, u8 tos, int oif,
  194. struct net_device *dev, u32 *spec_dst, u32 *itag);
  195. extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
  196. struct rtentry;
  197. /* Exported by fib_semantics.c */
  198. extern int ip_fib_check_default(u32 gw, struct net_device *dev);
  199. extern int fib_sync_down(u32 local, struct net_device *dev, int force);
  200. extern int fib_sync_up(struct net_device *dev);
  201. extern u32 __fib_res_prefsrc(struct fib_result *res);
  202. /* Exported by fib_hash.c */
  203. extern struct fib_table *fib_hash_init(u32 id);
  204. #ifdef CONFIG_IP_MULTIPLE_TABLES
  205. extern int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb);
  206. extern void __init fib4_rules_init(void);
  207. #ifdef CONFIG_NET_CLS_ROUTE
  208. extern u32 fib_rules_tclass(struct fib_result *res);
  209. #endif
  210. #endif
  211. static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
  212. {
  213. #ifdef CONFIG_NET_CLS_ROUTE
  214. #ifdef CONFIG_IP_MULTIPLE_TABLES
  215. u32 rtag;
  216. #endif
  217. *itag = FIB_RES_NH(*res).nh_tclassid<<16;
  218. #ifdef CONFIG_IP_MULTIPLE_TABLES
  219. rtag = fib_rules_tclass(res);
  220. if (*itag == 0)
  221. *itag = (rtag<<16);
  222. *itag |= (rtag>>16);
  223. #endif
  224. #endif
  225. }
  226. extern void free_fib_info(struct fib_info *fi);
  227. static inline void fib_info_put(struct fib_info *fi)
  228. {
  229. if (atomic_dec_and_test(&fi->fib_clntref))
  230. free_fib_info(fi);
  231. }
  232. static inline void fib_res_put(struct fib_result *res)
  233. {
  234. if (res->fi)
  235. fib_info_put(res->fi);
  236. #ifdef CONFIG_IP_MULTIPLE_TABLES
  237. if (res->r)
  238. fib_rule_put(res->r);
  239. #endif
  240. }
  241. #ifdef CONFIG_PROC_FS
  242. extern int fib_proc_init(void);
  243. extern void fib_proc_exit(void);
  244. #endif
  245. #endif /* _NET_FIB_H */