ip_fib.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <linux/config.h>
  18. #include <net/flow.h>
  19. #include <linux/seq_file.h>
  20. /* WARNING: The ordering of these elements must match ordering
  21. * of RTA_* rtnetlink attribute numbers.
  22. */
  23. struct kern_rta {
  24. void *rta_dst;
  25. void *rta_src;
  26. int *rta_iif;
  27. int *rta_oif;
  28. void *rta_gw;
  29. u32 *rta_priority;
  30. void *rta_prefsrc;
  31. struct rtattr *rta_mx;
  32. struct rtattr *rta_mp;
  33. unsigned char *rta_protoinfo;
  34. u32 *rta_flow;
  35. struct rta_cacheinfo *rta_ci;
  36. struct rta_session *rta_sess;
  37. u32 *rta_mp_alg;
  38. };
  39. struct fib_info;
  40. struct fib_nh {
  41. struct net_device *nh_dev;
  42. struct hlist_node nh_hash;
  43. struct fib_info *nh_parent;
  44. unsigned nh_flags;
  45. unsigned char nh_scope;
  46. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  47. int nh_weight;
  48. int nh_power;
  49. #endif
  50. #ifdef CONFIG_NET_CLS_ROUTE
  51. __u32 nh_tclassid;
  52. #endif
  53. int nh_oif;
  54. u32 nh_gw;
  55. };
  56. /*
  57. * This structure contains data shared by many of routes.
  58. */
  59. struct fib_info {
  60. struct hlist_node fib_hash;
  61. struct hlist_node fib_lhash;
  62. int fib_treeref;
  63. atomic_t fib_clntref;
  64. int fib_dead;
  65. unsigned fib_flags;
  66. int fib_protocol;
  67. u32 fib_prefsrc;
  68. u32 fib_priority;
  69. u32 fib_metrics[RTAX_MAX];
  70. #define fib_mtu fib_metrics[RTAX_MTU-1]
  71. #define fib_window fib_metrics[RTAX_WINDOW-1]
  72. #define fib_rtt fib_metrics[RTAX_RTT-1]
  73. #define fib_advmss fib_metrics[RTAX_ADVMSS-1]
  74. int fib_nhs;
  75. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  76. int fib_power;
  77. #endif
  78. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  79. u32 fib_mp_alg;
  80. #endif
  81. struct fib_nh fib_nh[0];
  82. #define fib_dev fib_nh[0].nh_dev
  83. };
  84. #ifdef CONFIG_IP_MULTIPLE_TABLES
  85. struct fib_rule;
  86. #endif
  87. struct fib_result {
  88. unsigned char prefixlen;
  89. unsigned char nh_sel;
  90. unsigned char type;
  91. unsigned char scope;
  92. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  93. __u32 network;
  94. __u32 netmask;
  95. #endif
  96. struct fib_info *fi;
  97. #ifdef CONFIG_IP_MULTIPLE_TABLES
  98. struct fib_rule *r;
  99. #endif
  100. };
  101. struct fib_result_nl {
  102. u32 fl_addr; /* To be looked up*/
  103. u32 fl_fwmark;
  104. unsigned char fl_tos;
  105. unsigned char fl_scope;
  106. unsigned char tb_id_in;
  107. unsigned char tb_id; /* Results */
  108. unsigned char prefixlen;
  109. unsigned char nh_sel;
  110. unsigned char type;
  111. unsigned char scope;
  112. int err;
  113. };
  114. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  115. #define FIB_RES_NH(res) ((res).fi->fib_nh[(res).nh_sel])
  116. #define FIB_RES_RESET(res) ((res).nh_sel = 0)
  117. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  118. #define FIB_RES_NH(res) ((res).fi->fib_nh[0])
  119. #define FIB_RES_RESET(res)
  120. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  121. #define FIB_RES_PREFSRC(res) ((res).fi->fib_prefsrc ? : __fib_res_prefsrc(&res))
  122. #define FIB_RES_GW(res) (FIB_RES_NH(res).nh_gw)
  123. #define FIB_RES_DEV(res) (FIB_RES_NH(res).nh_dev)
  124. #define FIB_RES_OIF(res) (FIB_RES_NH(res).nh_oif)
  125. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  126. #define FIB_RES_NETWORK(res) ((res).network)
  127. #define FIB_RES_NETMASK(res) ((res).netmask)
  128. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  129. #define FIB_RES_NETWORK(res) (0)
  130. #define FIB_RES_NETMASK(res) (0)
  131. #endif /* CONFIG_IP_ROUTE_MULTIPATH_WRANDOM */
  132. struct fib_table {
  133. unsigned char tb_id;
  134. unsigned tb_stamp;
  135. int (*tb_lookup)(struct fib_table *tb, const struct flowi *flp, struct fib_result *res);
  136. int (*tb_insert)(struct fib_table *table, struct rtmsg *r,
  137. struct kern_rta *rta, struct nlmsghdr *n,
  138. struct netlink_skb_parms *req);
  139. int (*tb_delete)(struct fib_table *table, struct rtmsg *r,
  140. struct kern_rta *rta, struct nlmsghdr *n,
  141. struct netlink_skb_parms *req);
  142. int (*tb_dump)(struct fib_table *table, struct sk_buff *skb,
  143. struct netlink_callback *cb);
  144. int (*tb_flush)(struct fib_table *table);
  145. void (*tb_select_default)(struct fib_table *table,
  146. const struct flowi *flp, struct fib_result *res);
  147. unsigned char tb_data[0];
  148. };
  149. #ifndef CONFIG_IP_MULTIPLE_TABLES
  150. extern struct fib_table *ip_fib_local_table;
  151. extern struct fib_table *ip_fib_main_table;
  152. static inline struct fib_table *fib_get_table(int id)
  153. {
  154. if (id != RT_TABLE_LOCAL)
  155. return ip_fib_main_table;
  156. return ip_fib_local_table;
  157. }
  158. static inline struct fib_table *fib_new_table(int id)
  159. {
  160. return fib_get_table(id);
  161. }
  162. static inline int fib_lookup(const struct flowi *flp, struct fib_result *res)
  163. {
  164. if (ip_fib_local_table->tb_lookup(ip_fib_local_table, flp, res) &&
  165. ip_fib_main_table->tb_lookup(ip_fib_main_table, flp, res))
  166. return -ENETUNREACH;
  167. return 0;
  168. }
  169. static inline void fib_select_default(const struct flowi *flp, struct fib_result *res)
  170. {
  171. if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  172. ip_fib_main_table->tb_select_default(ip_fib_main_table, flp, res);
  173. }
  174. #else /* CONFIG_IP_MULTIPLE_TABLES */
  175. #define ip_fib_local_table (fib_tables[RT_TABLE_LOCAL])
  176. #define ip_fib_main_table (fib_tables[RT_TABLE_MAIN])
  177. extern struct fib_table * fib_tables[RT_TABLE_MAX+1];
  178. extern int fib_lookup(const struct flowi *flp, struct fib_result *res);
  179. extern struct fib_table *__fib_new_table(int id);
  180. extern void fib_rule_put(struct fib_rule *r);
  181. static inline struct fib_table *fib_get_table(int id)
  182. {
  183. if (id == 0)
  184. id = RT_TABLE_MAIN;
  185. return fib_tables[id];
  186. }
  187. static inline struct fib_table *fib_new_table(int id)
  188. {
  189. if (id == 0)
  190. id = RT_TABLE_MAIN;
  191. return fib_tables[id] ? : __fib_new_table(id);
  192. }
  193. extern void fib_select_default(const struct flowi *flp, struct fib_result *res);
  194. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  195. /* Exported by fib_frontend.c */
  196. extern void ip_fib_init(void);
  197. extern int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  198. extern int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  199. extern int inet_rtm_getroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  200. extern int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb);
  201. extern int fib_validate_source(u32 src, u32 dst, u8 tos, int oif,
  202. struct net_device *dev, u32 *spec_dst, u32 *itag);
  203. extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
  204. struct rtentry;
  205. /* Exported by fib_semantics.c */
  206. extern int ip_fib_check_default(u32 gw, struct net_device *dev);
  207. extern int fib_sync_down(u32 local, struct net_device *dev, int force);
  208. extern int fib_sync_up(struct net_device *dev);
  209. extern int fib_convert_rtentry(int cmd, struct nlmsghdr *nl, struct rtmsg *rtm,
  210. struct kern_rta *rta, struct rtentry *r);
  211. extern u32 __fib_res_prefsrc(struct fib_result *res);
  212. /* Exported by fib_hash.c */
  213. extern struct fib_table *fib_hash_init(int id);
  214. #ifdef CONFIG_IP_MULTIPLE_TABLES
  215. /* Exported by fib_rules.c */
  216. extern int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  217. extern int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
  218. extern int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb);
  219. #ifdef CONFIG_NET_CLS_ROUTE
  220. extern u32 fib_rules_tclass(struct fib_result *res);
  221. #endif
  222. extern void fib_rules_init(void);
  223. #endif
  224. static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
  225. {
  226. #ifdef CONFIG_NET_CLS_ROUTE
  227. #ifdef CONFIG_IP_MULTIPLE_TABLES
  228. u32 rtag;
  229. #endif
  230. *itag = FIB_RES_NH(*res).nh_tclassid<<16;
  231. #ifdef CONFIG_IP_MULTIPLE_TABLES
  232. rtag = fib_rules_tclass(res);
  233. if (*itag == 0)
  234. *itag = (rtag<<16);
  235. *itag |= (rtag>>16);
  236. #endif
  237. #endif
  238. }
  239. extern void free_fib_info(struct fib_info *fi);
  240. static inline void fib_info_put(struct fib_info *fi)
  241. {
  242. if (atomic_dec_and_test(&fi->fib_clntref))
  243. free_fib_info(fi);
  244. }
  245. static inline void fib_res_put(struct fib_result *res)
  246. {
  247. if (res->fi)
  248. fib_info_put(res->fi);
  249. #ifdef CONFIG_IP_MULTIPLE_TABLES
  250. if (res->r)
  251. fib_rule_put(res->r);
  252. #endif
  253. }
  254. #ifdef CONFIG_PROC_FS
  255. extern int fib_proc_init(void);
  256. extern void fib_proc_exit(void);
  257. #endif
  258. #endif /* _NET_FIB_H */