ip_fib.h 10 KB

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