ip_fib.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <linux/rcupdate.h>
  20. #include <net/fib_rules.h>
  21. #include <net/inetpeer.h>
  22. #include <linux/percpu.h>
  23. #include <linux/notifier.h>
  24. struct fib_config {
  25. u8 fc_dst_len;
  26. u8 fc_tos;
  27. u8 fc_protocol;
  28. u8 fc_scope;
  29. u8 fc_type;
  30. /* 3 bytes unused */
  31. u32 fc_table;
  32. __be32 fc_dst;
  33. __be32 fc_gw;
  34. int fc_oif;
  35. u32 fc_flags;
  36. u32 fc_priority;
  37. __be32 fc_prefsrc;
  38. struct nlattr *fc_mx;
  39. struct rtnexthop *fc_mp;
  40. int fc_mx_len;
  41. int fc_mp_len;
  42. u32 fc_flow;
  43. u32 fc_nlflags;
  44. struct nl_info fc_nlinfo;
  45. struct nlattr *fc_encap;
  46. u16 fc_encap_type;
  47. };
  48. struct fib_info;
  49. struct rtable;
  50. struct fib_nh_exception {
  51. struct fib_nh_exception __rcu *fnhe_next;
  52. int fnhe_genid;
  53. __be32 fnhe_daddr;
  54. u32 fnhe_pmtu;
  55. bool fnhe_mtu_locked;
  56. __be32 fnhe_gw;
  57. unsigned long fnhe_expires;
  58. struct rtable __rcu *fnhe_rth_input;
  59. struct rtable __rcu *fnhe_rth_output;
  60. unsigned long fnhe_stamp;
  61. struct rcu_head rcu;
  62. };
  63. struct fnhe_hash_bucket {
  64. struct fib_nh_exception __rcu *chain;
  65. };
  66. #define FNHE_HASH_SHIFT 11
  67. #define FNHE_HASH_SIZE (1 << FNHE_HASH_SHIFT)
  68. #define FNHE_RECLAIM_DEPTH 5
  69. struct fib_nh {
  70. struct net_device *nh_dev;
  71. struct hlist_node nh_hash;
  72. struct fib_info *nh_parent;
  73. unsigned int nh_flags;
  74. unsigned char nh_scope;
  75. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  76. int nh_weight;
  77. atomic_t nh_upper_bound;
  78. #endif
  79. #ifdef CONFIG_IP_ROUTE_CLASSID
  80. __u32 nh_tclassid;
  81. #endif
  82. int nh_oif;
  83. __be32 nh_gw;
  84. __be32 nh_saddr;
  85. int nh_saddr_genid;
  86. struct rtable __rcu * __percpu *nh_pcpu_rth_output;
  87. struct rtable __rcu *nh_rth_input;
  88. struct fnhe_hash_bucket __rcu *nh_exceptions;
  89. struct lwtunnel_state *nh_lwtstate;
  90. };
  91. /*
  92. * This structure contains data shared by many of routes.
  93. */
  94. struct fib_info {
  95. struct hlist_node fib_hash;
  96. struct hlist_node fib_lhash;
  97. struct net *fib_net;
  98. int fib_treeref;
  99. atomic_t fib_clntref;
  100. unsigned int fib_flags;
  101. unsigned char fib_dead;
  102. unsigned char fib_protocol;
  103. unsigned char fib_scope;
  104. unsigned char fib_type;
  105. __be32 fib_prefsrc;
  106. u32 fib_tb_id;
  107. u32 fib_priority;
  108. struct dst_metrics *fib_metrics;
  109. #define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
  110. #define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
  111. #define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
  112. #define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
  113. int fib_nhs;
  114. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  115. int fib_weight;
  116. #endif
  117. unsigned int fib_offload_cnt;
  118. struct rcu_head rcu;
  119. struct fib_nh fib_nh[0];
  120. #define fib_dev fib_nh[0].nh_dev
  121. };
  122. #ifdef CONFIG_IP_MULTIPLE_TABLES
  123. struct fib_rule;
  124. #endif
  125. struct fib_table;
  126. struct fib_result {
  127. unsigned char prefixlen;
  128. unsigned char nh_sel;
  129. unsigned char type;
  130. unsigned char scope;
  131. u32 tclassid;
  132. struct fib_info *fi;
  133. struct fib_table *table;
  134. struct hlist_head *fa_head;
  135. };
  136. struct fib_result_nl {
  137. __be32 fl_addr; /* To be looked up*/
  138. u32 fl_mark;
  139. unsigned char fl_tos;
  140. unsigned char fl_scope;
  141. unsigned char tb_id_in;
  142. unsigned char tb_id; /* Results */
  143. unsigned char prefixlen;
  144. unsigned char nh_sel;
  145. unsigned char type;
  146. unsigned char scope;
  147. int err;
  148. };
  149. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  150. #define FIB_RES_NH(res) ((res).fi->fib_nh[(res).nh_sel])
  151. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  152. #define FIB_RES_NH(res) ((res).fi->fib_nh[0])
  153. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  154. #ifdef CONFIG_IP_MULTIPLE_TABLES
  155. #define FIB_TABLE_HASHSZ 256
  156. #else
  157. #define FIB_TABLE_HASHSZ 2
  158. #endif
  159. __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh);
  160. static inline void fib_info_offload_inc(struct fib_info *fi)
  161. {
  162. fi->fib_offload_cnt++;
  163. fi->fib_flags |= RTNH_F_OFFLOAD;
  164. }
  165. static inline void fib_info_offload_dec(struct fib_info *fi)
  166. {
  167. if (--fi->fib_offload_cnt == 0)
  168. fi->fib_flags &= ~RTNH_F_OFFLOAD;
  169. }
  170. #define FIB_RES_SADDR(net, res) \
  171. ((FIB_RES_NH(res).nh_saddr_genid == \
  172. atomic_read(&(net)->ipv4.dev_addr_genid)) ? \
  173. FIB_RES_NH(res).nh_saddr : \
  174. fib_info_update_nh_saddr((net), &FIB_RES_NH(res)))
  175. #define FIB_RES_GW(res) (FIB_RES_NH(res).nh_gw)
  176. #define FIB_RES_DEV(res) (FIB_RES_NH(res).nh_dev)
  177. #define FIB_RES_OIF(res) (FIB_RES_NH(res).nh_oif)
  178. #define FIB_RES_PREFSRC(net, res) ((res).fi->fib_prefsrc ? : \
  179. FIB_RES_SADDR(net, res))
  180. struct fib_notifier_info {
  181. struct net *net;
  182. };
  183. struct fib_entry_notifier_info {
  184. struct fib_notifier_info info; /* must be first */
  185. u32 dst;
  186. int dst_len;
  187. struct fib_info *fi;
  188. u8 tos;
  189. u8 type;
  190. u32 tb_id;
  191. u32 nlflags;
  192. };
  193. enum fib_event_type {
  194. FIB_EVENT_ENTRY_ADD,
  195. FIB_EVENT_ENTRY_DEL,
  196. FIB_EVENT_RULE_ADD,
  197. FIB_EVENT_RULE_DEL,
  198. };
  199. int register_fib_notifier(struct notifier_block *nb);
  200. int unregister_fib_notifier(struct notifier_block *nb);
  201. int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
  202. struct fib_notifier_info *info);
  203. struct fib_table {
  204. struct hlist_node tb_hlist;
  205. u32 tb_id;
  206. int tb_num_default;
  207. struct rcu_head rcu;
  208. unsigned long *tb_data;
  209. unsigned long __data[0];
  210. };
  211. int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
  212. struct fib_result *res, int fib_flags);
  213. int fib_table_insert(struct net *, struct fib_table *, struct fib_config *);
  214. int fib_table_delete(struct net *, struct fib_table *, struct fib_config *);
  215. int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
  216. struct netlink_callback *cb);
  217. int fib_table_flush(struct net *net, struct fib_table *table);
  218. struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
  219. void fib_table_flush_external(struct fib_table *table);
  220. void fib_free_table(struct fib_table *tb);
  221. #ifndef CONFIG_IP_MULTIPLE_TABLES
  222. #define TABLE_LOCAL_INDEX (RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
  223. #define TABLE_MAIN_INDEX (RT_TABLE_MAIN & (FIB_TABLE_HASHSZ - 1))
  224. static inline struct fib_table *fib_get_table(struct net *net, u32 id)
  225. {
  226. struct hlist_node *tb_hlist;
  227. struct hlist_head *ptr;
  228. ptr = id == RT_TABLE_LOCAL ?
  229. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
  230. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
  231. tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
  232. return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
  233. }
  234. static inline struct fib_table *fib_new_table(struct net *net, u32 id)
  235. {
  236. return fib_get_table(net, id);
  237. }
  238. static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
  239. struct fib_result *res, unsigned int flags)
  240. {
  241. struct fib_table *tb;
  242. int err = -ENETUNREACH;
  243. rcu_read_lock();
  244. tb = fib_get_table(net, RT_TABLE_MAIN);
  245. if (tb)
  246. err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
  247. if (err == -EAGAIN)
  248. err = -ENETUNREACH;
  249. rcu_read_unlock();
  250. return err;
  251. }
  252. #else /* CONFIG_IP_MULTIPLE_TABLES */
  253. int __net_init fib4_rules_init(struct net *net);
  254. void __net_exit fib4_rules_exit(struct net *net);
  255. struct fib_table *fib_new_table(struct net *net, u32 id);
  256. struct fib_table *fib_get_table(struct net *net, u32 id);
  257. int __fib_lookup(struct net *net, struct flowi4 *flp,
  258. struct fib_result *res, unsigned int flags);
  259. static inline int fib_lookup(struct net *net, struct flowi4 *flp,
  260. struct fib_result *res, unsigned int flags)
  261. {
  262. struct fib_table *tb;
  263. int err = -ENETUNREACH;
  264. flags |= FIB_LOOKUP_NOREF;
  265. if (net->ipv4.fib_has_custom_rules)
  266. return __fib_lookup(net, flp, res, flags);
  267. rcu_read_lock();
  268. res->tclassid = 0;
  269. tb = rcu_dereference_rtnl(net->ipv4.fib_main);
  270. if (tb)
  271. err = fib_table_lookup(tb, flp, res, flags);
  272. if (!err)
  273. goto out;
  274. tb = rcu_dereference_rtnl(net->ipv4.fib_default);
  275. if (tb)
  276. err = fib_table_lookup(tb, flp, res, flags);
  277. out:
  278. if (err == -EAGAIN)
  279. err = -ENETUNREACH;
  280. rcu_read_unlock();
  281. return err;
  282. }
  283. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  284. /* Exported by fib_frontend.c */
  285. extern const struct nla_policy rtm_ipv4_policy[];
  286. void ip_fib_init(void);
  287. __be32 fib_compute_spec_dst(struct sk_buff *skb);
  288. int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
  289. u8 tos, int oif, struct net_device *dev,
  290. struct in_device *idev, u32 *itag);
  291. void fib_select_default(const struct flowi4 *flp, struct fib_result *res);
  292. #ifdef CONFIG_IP_ROUTE_CLASSID
  293. static inline int fib_num_tclassid_users(struct net *net)
  294. {
  295. return net->ipv4.fib_num_tclassid_users;
  296. }
  297. #else
  298. static inline int fib_num_tclassid_users(struct net *net)
  299. {
  300. return 0;
  301. }
  302. #endif
  303. int fib_unmerge(struct net *net);
  304. /* Exported by fib_semantics.c */
  305. int ip_fib_check_default(__be32 gw, struct net_device *dev);
  306. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
  307. int fib_sync_down_addr(struct net_device *dev, __be32 local);
  308. int fib_sync_up(struct net_device *dev, unsigned int nh_flags);
  309. extern u32 fib_multipath_secret __read_mostly;
  310. static inline int fib_multipath_hash(__be32 saddr, __be32 daddr)
  311. {
  312. return jhash_2words((__force u32)saddr, (__force u32)daddr,
  313. fib_multipath_secret) >> 1;
  314. }
  315. void fib_select_multipath(struct fib_result *res, int hash);
  316. void fib_select_path(struct net *net, struct fib_result *res,
  317. struct flowi4 *fl4, int mp_hash);
  318. /* Exported by fib_trie.c */
  319. void fib_trie_init(void);
  320. struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
  321. static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
  322. {
  323. #ifdef CONFIG_IP_ROUTE_CLASSID
  324. #ifdef CONFIG_IP_MULTIPLE_TABLES
  325. u32 rtag;
  326. #endif
  327. *itag = FIB_RES_NH(*res).nh_tclassid<<16;
  328. #ifdef CONFIG_IP_MULTIPLE_TABLES
  329. rtag = res->tclassid;
  330. if (*itag == 0)
  331. *itag = (rtag<<16);
  332. *itag |= (rtag>>16);
  333. #endif
  334. #endif
  335. }
  336. void free_fib_info(struct fib_info *fi);
  337. static inline void fib_info_put(struct fib_info *fi)
  338. {
  339. if (atomic_dec_and_test(&fi->fib_clntref))
  340. free_fib_info(fi);
  341. }
  342. #ifdef CONFIG_PROC_FS
  343. int __net_init fib_proc_init(struct net *net);
  344. void __net_exit fib_proc_exit(struct net *net);
  345. #else
  346. static inline int fib_proc_init(struct net *net)
  347. {
  348. return 0;
  349. }
  350. static inline void fib_proc_exit(struct net *net)
  351. {
  352. }
  353. #endif
  354. #endif /* _NET_FIB_H */