ip_fib.h 11 KB

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