ip_fib.h 8.2 KB

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