xref: /linux-6.15/include/net/ip_fib.h (revision dcb1ecb5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET  is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Definitions for the Forwarding Information Base.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	A.N.Kuznetsov, <[email protected]>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #ifndef _NET_IP_FIB_H
171da177e4SLinus Torvalds #define _NET_IP_FIB_H
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <net/flow.h>
201da177e4SLinus Torvalds #include <linux/seq_file.h>
214895c771SDavid S. Miller #include <linux/rcupdate.h>
2204b1d4e5SIdo Schimmel #include <net/fib_notifier.h>
23e1ef4bf2SThomas Graf #include <net/fib_rules.h>
248e773277SDavid S. Miller #include <net/inetpeer.h>
25d26b3a7cSEric Dumazet #include <linux/percpu.h>
26b90eb754SJiri Pirko #include <linux/notifier.h>
270029c0deSReshetova, Elena #include <linux/refcount.h>
281da177e4SLinus Torvalds 
294e902c57SThomas Graf struct fib_config {
304e902c57SThomas Graf 	u8			fc_dst_len;
314e902c57SThomas Graf 	u8			fc_tos;
324e902c57SThomas Graf 	u8			fc_protocol;
334e902c57SThomas Graf 	u8			fc_scope;
344e902c57SThomas Graf 	u8			fc_type;
35f35b794bSDavid Ahern 	u8			fc_gw_family;
36f35b794bSDavid Ahern 	/* 2 bytes unused */
374e902c57SThomas Graf 	u32			fc_table;
386d85c10aSAl Viro 	__be32			fc_dst;
39a4ea5d43SDavid Ahern 	union {
40f35b794bSDavid Ahern 		__be32		fc_gw4;
41a4ea5d43SDavid Ahern 		struct in6_addr	fc_gw6;
42a4ea5d43SDavid Ahern 	};
434e902c57SThomas Graf 	int			fc_oif;
444e902c57SThomas Graf 	u32			fc_flags;
454e902c57SThomas Graf 	u32			fc_priority;
466d85c10aSAl Viro 	__be32			fc_prefsrc;
474e902c57SThomas Graf 	struct nlattr		*fc_mx;
484e902c57SThomas Graf 	struct rtnexthop	*fc_mp;
494e902c57SThomas Graf 	int			fc_mx_len;
504e902c57SThomas Graf 	int			fc_mp_len;
514e902c57SThomas Graf 	u32			fc_flow;
524e902c57SThomas Graf 	u32			fc_nlflags;
534e902c57SThomas Graf 	struct nl_info		fc_nlinfo;
54571e7226SRoopa Prabhu 	struct nlattr		*fc_encap;
55571e7226SRoopa Prabhu 	u16			fc_encap_type;
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds struct fib_info;
59f2bb4bedSDavid S. Miller struct rtable;
601da177e4SLinus Torvalds 
614895c771SDavid S. Miller struct fib_nh_exception {
624895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*fnhe_next;
635aad1de5STimo Teräs 	int				fnhe_genid;
644895c771SDavid S. Miller 	__be32				fnhe_daddr;
654895c771SDavid S. Miller 	u32				fnhe_pmtu;
66d52e5a7eSSabrina Dubroca 	bool				fnhe_mtu_locked;
67aee06da6SJulian Anastasov 	__be32				fnhe_gw;
684895c771SDavid S. Miller 	unsigned long			fnhe_expires;
692ffae99dSTimo Teräs 	struct rtable __rcu		*fnhe_rth_input;
702ffae99dSTimo Teräs 	struct rtable __rcu		*fnhe_rth_output;
714895c771SDavid S. Miller 	unsigned long			fnhe_stamp;
72deed49dfSXin Long 	struct rcu_head			rcu;
734895c771SDavid S. Miller };
744895c771SDavid S. Miller 
754895c771SDavid S. Miller struct fnhe_hash_bucket {
764895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*chain;
774895c771SDavid S. Miller };
784895c771SDavid S. Miller 
79d546c621SEric Dumazet #define FNHE_HASH_SHIFT		11
80d546c621SEric Dumazet #define FNHE_HASH_SIZE		(1 << FNHE_HASH_SHIFT)
814895c771SDavid S. Miller #define FNHE_RECLAIM_DEPTH	5
824895c771SDavid S. Miller 
83f1741730SDavid Ahern struct fib_nh_common {
84f1741730SDavid Ahern 	struct net_device	*nhc_dev;
85f1741730SDavid Ahern 	int			nhc_oif;
86f1741730SDavid Ahern 	unsigned char		nhc_scope;
87f1741730SDavid Ahern 	u8			nhc_family;
88bdf00467SDavid Ahern 	u8			nhc_gw_family;
89ecc5663cSDavid Ahern 	unsigned char		nhc_flags;
90ecc5663cSDavid Ahern 	struct lwtunnel_state	*nhc_lwtstate;
91bdf00467SDavid Ahern 
92f1741730SDavid Ahern 	union {
93f1741730SDavid Ahern 		__be32          ipv4;
94f1741730SDavid Ahern 		struct in6_addr ipv6;
95f1741730SDavid Ahern 	} nhc_gw;
96f1741730SDavid Ahern 
97f1741730SDavid Ahern 	int			nhc_weight;
98f1741730SDavid Ahern 	atomic_t		nhc_upper_bound;
990f457a36SDavid Ahern 
1000f457a36SDavid Ahern 	/* v4 specific, but allows fib6_nh with v4 routes */
1010f457a36SDavid Ahern 	struct rtable __rcu * __percpu *nhc_pcpu_rth_output;
1020f457a36SDavid Ahern 	struct rtable __rcu     *nhc_rth_input;
103a5995e71SDavid Ahern 	struct fnhe_hash_bucket	__rcu *nhc_exceptions;
104f1741730SDavid Ahern };
105f1741730SDavid Ahern 
1061da177e4SLinus Torvalds struct fib_nh {
107f1741730SDavid Ahern 	struct fib_nh_common	nh_common;
1081da177e4SLinus Torvalds 	struct hlist_node	nh_hash;
1091da177e4SLinus Torvalds 	struct fib_info		*nh_parent;
110c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1111da177e4SLinus Torvalds 	__u32			nh_tclassid;
1121da177e4SLinus Torvalds #endif
1131fc050a1SDavid S. Miller 	__be32			nh_saddr;
114436c3b66SDavid S. Miller 	int			nh_saddr_genid;
115f1741730SDavid Ahern #define fib_nh_family		nh_common.nhc_family
116f1741730SDavid Ahern #define fib_nh_dev		nh_common.nhc_dev
117f1741730SDavid Ahern #define fib_nh_oif		nh_common.nhc_oif
118f1741730SDavid Ahern #define fib_nh_flags		nh_common.nhc_flags
119f1741730SDavid Ahern #define fib_nh_lws		nh_common.nhc_lwtstate
120f1741730SDavid Ahern #define fib_nh_scope		nh_common.nhc_scope
121bdf00467SDavid Ahern #define fib_nh_gw_family	nh_common.nhc_gw_family
122f1741730SDavid Ahern #define fib_nh_gw4		nh_common.nhc_gw.ipv4
123f1741730SDavid Ahern #define fib_nh_gw6		nh_common.nhc_gw.ipv6
124f1741730SDavid Ahern #define fib_nh_weight		nh_common.nhc_weight
125f1741730SDavid Ahern #define fib_nh_upper_bound	nh_common.nhc_upper_bound
1261da177e4SLinus Torvalds };
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds  * This structure contains data shared by many of routes.
1301da177e4SLinus Torvalds  */
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds struct fib_info {
1331da177e4SLinus Torvalds 	struct hlist_node	fib_hash;
1341da177e4SLinus Torvalds 	struct hlist_node	fib_lhash;
1357462bd74SDenis V. Lunev 	struct net		*fib_net;
1361da177e4SLinus Torvalds 	int			fib_treeref;
1370029c0deSReshetova, Elena 	refcount_t		fib_clntref;
13895c96174SEric Dumazet 	unsigned int		fib_flags;
13937e826c5SDavid S. Miller 	unsigned char		fib_dead;
14037e826c5SDavid S. Miller 	unsigned char		fib_protocol;
14137e826c5SDavid S. Miller 	unsigned char		fib_scope;
142f4ef85bbSEric Dumazet 	unsigned char		fib_type;
143b83738aeSAl Viro 	__be32			fib_prefsrc;
1445a56a0b3SMark Tomlinson 	u32			fib_tb_id;
1451da177e4SLinus Torvalds 	u32			fib_priority;
1463fb07dafSEric Dumazet 	struct dst_metrics	*fib_metrics;
1473fb07dafSEric Dumazet #define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
1483fb07dafSEric Dumazet #define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
1493fb07dafSEric Dumazet #define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
1503fb07dafSEric Dumazet #define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
1511da177e4SLinus Torvalds 	int			fib_nhs;
15219a9d136SDavid Ahern 	bool			fib_nh_is_v6;
1531bff1a0cSDavid Ahern 	bool			nh_updated;
154ebc0ffaeSEric Dumazet 	struct rcu_head		rcu;
1551da177e4SLinus Torvalds 	struct fib_nh		fib_nh[0];
1561da177e4SLinus Torvalds };
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
1601da177e4SLinus Torvalds struct fib_rule;
1611da177e4SLinus Torvalds #endif
1621da177e4SLinus Torvalds 
1635b470441SDavid S. Miller struct fib_table;
1641da177e4SLinus Torvalds struct fib_result {
1656ffd9034SDavid Ahern 	__be32			prefix;
1661da177e4SLinus Torvalds 	unsigned char		prefixlen;
1671da177e4SLinus Torvalds 	unsigned char		nh_sel;
1681da177e4SLinus Torvalds 	unsigned char		type;
1691da177e4SLinus Torvalds 	unsigned char		scope;
17085b91b03SDavid S. Miller 	u32			tclassid;
171eba618abSDavid Ahern 	struct fib_nh_common	*nhc;
1721da177e4SLinus Torvalds 	struct fib_info		*fi;
1735b470441SDavid S. Miller 	struct fib_table	*table;
17456315f9eSAlexander Duyck 	struct hlist_head	*fa_head;
1751da177e4SLinus Torvalds };
1761da177e4SLinus Torvalds 
177246955feSRobert Olsson struct fib_result_nl {
17880e856e1SAl Viro 	__be32		fl_addr;   /* To be looked up*/
1795f300893SThomas Graf 	u32		fl_mark;
180246955feSRobert Olsson 	unsigned char	fl_tos;
181246955feSRobert Olsson 	unsigned char   fl_scope;
182246955feSRobert Olsson 	unsigned char   tb_id_in;
183246955feSRobert Olsson 
184246955feSRobert Olsson 	unsigned char   tb_id;      /* Results */
185246955feSRobert Olsson 	unsigned char	prefixlen;
186246955feSRobert Olsson 	unsigned char	nh_sel;
187246955feSRobert Olsson 	unsigned char	type;
188246955feSRobert Olsson 	unsigned char	scope;
189246955feSRobert Olsson 	int             err;
190246955feSRobert Olsson };
1911da177e4SLinus Torvalds 
1925b9e12dbSDenis V. Lunev #ifdef CONFIG_IP_MULTIPLE_TABLES
1935b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 256
1945b9e12dbSDenis V. Lunev #else
1955b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 2
1965b9e12dbSDenis V. Lunev #endif
1975b9e12dbSDenis V. Lunev 
198*dcb1ecb5SDavid Ahern __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
199c3669486SDavid Ahern 				 unsigned char scope);
200eba618abSDavid Ahern __be32 fib_result_prefsrc(struct net *net, struct fib_result *res);
201436c3b66SDavid S. Miller 
202eba618abSDavid Ahern #define FIB_RES_NHC(res)		((res).nhc)
203eba618abSDavid Ahern #define FIB_RES_DEV(res)	(FIB_RES_NHC(res)->nhc_dev)
204eba618abSDavid Ahern #define FIB_RES_OIF(res)	(FIB_RES_NHC(res)->nhc_oif)
2051fc050a1SDavid S. Miller 
206b90eb754SJiri Pirko struct fib_entry_notifier_info {
207b90eb754SJiri Pirko 	struct fib_notifier_info info; /* must be first */
208b90eb754SJiri Pirko 	u32 dst;
209b90eb754SJiri Pirko 	int dst_len;
210b90eb754SJiri Pirko 	struct fib_info *fi;
211b90eb754SJiri Pirko 	u8 tos;
212b90eb754SJiri Pirko 	u8 type;
213b90eb754SJiri Pirko 	u32 tb_id;
214b90eb754SJiri Pirko };
215b90eb754SJiri Pirko 
216982acb97SIdo Schimmel struct fib_nh_notifier_info {
217982acb97SIdo Schimmel 	struct fib_notifier_info info; /* must be first */
218982acb97SIdo Schimmel 	struct fib_nh *fib_nh;
219982acb97SIdo Schimmel };
220982acb97SIdo Schimmel 
22104b1d4e5SIdo Schimmel int call_fib4_notifier(struct notifier_block *nb, struct net *net,
222c0243892SIdo Schimmel 		       enum fib_event_type event_type,
223c0243892SIdo Schimmel 		       struct fib_notifier_info *info);
22404b1d4e5SIdo Schimmel int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
225b90eb754SJiri Pirko 			struct fib_notifier_info *info);
226b90eb754SJiri Pirko 
22704b1d4e5SIdo Schimmel int __net_init fib4_notifier_init(struct net *net);
22804b1d4e5SIdo Schimmel void __net_exit fib4_notifier_exit(struct net *net);
22904b1d4e5SIdo Schimmel 
2301bff1a0cSDavid Ahern void fib_info_notify_update(struct net *net, struct nl_info *info);
231d05f7a7dSIdo Schimmel void fib_notify(struct net *net, struct notifier_block *nb);
232c0243892SIdo Schimmel 
2331da177e4SLinus Torvalds struct fib_table {
2341af5a8c4SPatrick McHardy 	struct hlist_node	tb_hlist;
2352dfe55b4SPatrick McHardy 	u32			tb_id;
23621d8c49eSDavid S. Miller 	int			tb_num_default;
237a7e53531SAlexander Duyck 	struct rcu_head		rcu;
2380ddcf43dSAlexander Duyck 	unsigned long 		*tb_data;
2390ddcf43dSAlexander Duyck 	unsigned long		__data[0];
2401da177e4SLinus Torvalds };
2411da177e4SLinus Torvalds 
2424724676dSDavid Ahern struct fib_dump_filter {
2434724676dSDavid Ahern 	u32			table_id;
2444724676dSDavid Ahern 	/* filter_set is an optimization that an entry is set */
2454724676dSDavid Ahern 	bool			filter_set;
246ae677bbbSDavid Ahern 	bool			dump_all_families;
2474724676dSDavid Ahern 	unsigned char		protocol;
2484724676dSDavid Ahern 	unsigned char		rt_type;
2494724676dSDavid Ahern 	unsigned int		flags;
2504724676dSDavid Ahern 	struct net_device	*dev;
2514724676dSDavid Ahern };
2524724676dSDavid Ahern 
2535c3a0fd7SJoe Perches int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
254ebc0ffaeSEric Dumazet 		     struct fib_result *res, int fib_flags);
2556d8422a1SDavid Ahern int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
2566d8422a1SDavid Ahern 		     struct netlink_ext_ack *extack);
25778055998SDavid Ahern int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
25878055998SDavid Ahern 		     struct netlink_ext_ack *extack);
2595c3a0fd7SJoe Perches int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
26018a8021aSDavid Ahern 		   struct netlink_callback *cb, struct fib_dump_filter *filter);
261f97f4dd8SIdo Schimmel int fib_table_flush(struct net *net, struct fib_table *table, bool flush_all);
2620ddcf43dSAlexander Duyck struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
2633b709334SAlexander Duyck void fib_table_flush_external(struct fib_table *table);
2645c3a0fd7SJoe Perches void fib_free_table(struct fib_table *tb);
2654aa2c466SPavel Emelyanov 
2661da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTIPLE_TABLES
2671da177e4SLinus Torvalds 
268a5a519b2SAlexander Duyck #define TABLE_LOCAL_INDEX	(RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
269a5a519b2SAlexander Duyck #define TABLE_MAIN_INDEX	(RT_TABLE_MAIN  & (FIB_TABLE_HASHSZ - 1))
2701da177e4SLinus Torvalds 
2718ad4942cSDenis V. Lunev static inline struct fib_table *fib_get_table(struct net *net, u32 id)
2721da177e4SLinus Torvalds {
273a7e53531SAlexander Duyck 	struct hlist_node *tb_hlist;
27493456b6dSDenis V. Lunev 	struct hlist_head *ptr;
27593456b6dSDenis V. Lunev 
27693456b6dSDenis V. Lunev 	ptr = id == RT_TABLE_LOCAL ?
277e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
278e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
279a7e53531SAlexander Duyck 
280a7e53531SAlexander Duyck 	tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
281a7e53531SAlexander Duyck 
282a7e53531SAlexander Duyck 	return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2858ad4942cSDenis V. Lunev static inline struct fib_table *fib_new_table(struct net *net, u32 id)
2861da177e4SLinus Torvalds {
2878ad4942cSDenis V. Lunev 	return fib_get_table(net, id);
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
29022bd5b9bSDavid S. Miller static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
2910eeb075fSAndy Gospodarek 			     struct fib_result *res, unsigned int flags)
2921da177e4SLinus Torvalds {
293a7e53531SAlexander Duyck 	struct fib_table *tb;
2940ddcf43dSAlexander Duyck 	int err = -ENETUNREACH;
29593456b6dSDenis V. Lunev 
296345e9b54SAlexander Duyck 	rcu_read_lock();
29793456b6dSDenis V. Lunev 
298a7e53531SAlexander Duyck 	tb = fib_get_table(net, RT_TABLE_MAIN);
2990315e382SNikola Forró 	if (tb)
3000315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
3010315e382SNikola Forró 
3020315e382SNikola Forró 	if (err == -EAGAIN)
3030315e382SNikola Forró 		err = -ENETUNREACH;
304345e9b54SAlexander Duyck 
305345e9b54SAlexander Duyck 	rcu_read_unlock();
306345e9b54SAlexander Duyck 
307345e9b54SAlexander Duyck 	return err;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3103c71006dSIdo Schimmel static inline bool fib4_rule_default(const struct fib_rule *rule)
3113c71006dSIdo Schimmel {
3123c71006dSIdo Schimmel 	return true;
3133c71006dSIdo Schimmel }
3143c71006dSIdo Schimmel 
3151b2a4440SIdo Schimmel static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb)
3161b2a4440SIdo Schimmel {
3171b2a4440SIdo Schimmel 	return 0;
3181b2a4440SIdo Schimmel }
3191b2a4440SIdo Schimmel 
3201b2a4440SIdo Schimmel static inline unsigned int fib4_rules_seq_read(struct net *net)
3211b2a4440SIdo Schimmel {
3221b2a4440SIdo Schimmel 	return 0;
3231b2a4440SIdo Schimmel }
3241b2a4440SIdo Schimmel 
325e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
326e37b1e97SRoopa Prabhu 						 struct sk_buff *skb,
327e37b1e97SRoopa Prabhu 						 struct flowi4 *fl4,
328e37b1e97SRoopa Prabhu 						 struct flow_keys *flkeys)
329e37b1e97SRoopa Prabhu {
330e37b1e97SRoopa Prabhu 	return false;
331e37b1e97SRoopa Prabhu }
3321da177e4SLinus Torvalds #else /* CONFIG_IP_MULTIPLE_TABLES */
3335c3a0fd7SJoe Perches int __net_init fib4_rules_init(struct net *net);
3345c3a0fd7SJoe Perches void __net_exit fib4_rules_exit(struct net *net);
335c3e9a353SPavel Emelyanov 
3365c3a0fd7SJoe Perches struct fib_table *fib_new_table(struct net *net, u32 id);
3375c3a0fd7SJoe Perches struct fib_table *fib_get_table(struct net *net, u32 id);
3381da177e4SLinus Torvalds 
3390eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp,
3400eeb075fSAndy Gospodarek 		 struct fib_result *res, unsigned int flags);
341f4530fa5SDavid S. Miller 
342f4530fa5SDavid S. Miller static inline int fib_lookup(struct net *net, struct flowi4 *flp,
3430eeb075fSAndy Gospodarek 			     struct fib_result *res, unsigned int flags)
344f4530fa5SDavid S. Miller {
345a7e53531SAlexander Duyck 	struct fib_table *tb;
3460315e382SNikola Forró 	int err = -ENETUNREACH;
347a7e53531SAlexander Duyck 
3480eeb075fSAndy Gospodarek 	flags |= FIB_LOOKUP_NOREF;
349a7e53531SAlexander Duyck 	if (net->ipv4.fib_has_custom_rules)
3500eeb075fSAndy Gospodarek 		return __fib_lookup(net, flp, res, flags);
351345e9b54SAlexander Duyck 
352345e9b54SAlexander Duyck 	rcu_read_lock();
353345e9b54SAlexander Duyck 
35485b91b03SDavid S. Miller 	res->tclassid = 0;
355a7e53531SAlexander Duyck 
356a7e53531SAlexander Duyck 	tb = rcu_dereference_rtnl(net->ipv4.fib_main);
3570315e382SNikola Forró 	if (tb)
3580315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags);
3590315e382SNikola Forró 
3600315e382SNikola Forró 	if (!err)
3610315e382SNikola Forró 		goto out;
362a7e53531SAlexander Duyck 
363a7e53531SAlexander Duyck 	tb = rcu_dereference_rtnl(net->ipv4.fib_default);
3640315e382SNikola Forró 	if (tb)
3650315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags);
3660315e382SNikola Forró 
3670315e382SNikola Forró out:
3680315e382SNikola Forró 	if (err == -EAGAIN)
3690315e382SNikola Forró 		err = -ENETUNREACH;
370345e9b54SAlexander Duyck 
371345e9b54SAlexander Duyck 	rcu_read_unlock();
372345e9b54SAlexander Duyck 
373345e9b54SAlexander Duyck 	return err;
374f4530fa5SDavid S. Miller }
375f4530fa5SDavid S. Miller 
3763c71006dSIdo Schimmel bool fib4_rule_default(const struct fib_rule *rule);
3771b2a4440SIdo Schimmel int fib4_rules_dump(struct net *net, struct notifier_block *nb);
3781b2a4440SIdo Schimmel unsigned int fib4_rules_seq_read(struct net *net);
3793c71006dSIdo Schimmel 
380e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
381e37b1e97SRoopa Prabhu 						 struct sk_buff *skb,
382e37b1e97SRoopa Prabhu 						 struct flowi4 *fl4,
383e37b1e97SRoopa Prabhu 						 struct flow_keys *flkeys)
384e37b1e97SRoopa Prabhu {
385e37b1e97SRoopa Prabhu 	unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
386e37b1e97SRoopa Prabhu 
387e37b1e97SRoopa Prabhu 	if (!net->ipv4.fib_rules_require_fldissect)
388e37b1e97SRoopa Prabhu 		return false;
389e37b1e97SRoopa Prabhu 
390e37b1e97SRoopa Prabhu 	skb_flow_dissect_flow_keys(skb, flkeys, flag);
391e37b1e97SRoopa Prabhu 	fl4->fl4_sport = flkeys->ports.src;
392e37b1e97SRoopa Prabhu 	fl4->fl4_dport = flkeys->ports.dst;
393e37b1e97SRoopa Prabhu 	fl4->flowi4_proto = flkeys->basic.ip_proto;
394e37b1e97SRoopa Prabhu 
395e37b1e97SRoopa Prabhu 	return true;
396e37b1e97SRoopa Prabhu }
397e37b1e97SRoopa Prabhu 
3981da177e4SLinus Torvalds #endif /* CONFIG_IP_MULTIPLE_TABLES */
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /* Exported by fib_frontend.c */
401ef7c79edSPatrick McHardy extern const struct nla_policy rtm_ipv4_policy[];
4025c3a0fd7SJoe Perches void ip_fib_init(void);
403d1566268SDavid Ahern int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
404d1566268SDavid Ahern 		    struct netlink_ext_ack *extack);
4055c3a0fd7SJoe Perches __be32 fib_compute_spec_dst(struct sk_buff *skb);
40678f2756cSDavid Ahern bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
4075c3a0fd7SJoe Perches int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
4085c04c819SMichael Smith 			u8 tos, int oif, struct net_device *dev,
4099e56e380SDavid S. Miller 			struct in_device *idev, u32 *itag);
4107a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
411f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
412f4530fa5SDavid S. Miller {
413f4530fa5SDavid S. Miller 	return net->ipv4.fib_num_tclassid_users;
414f4530fa5SDavid S. Miller }
4157a9bc9b8SDavid S. Miller #else
416f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
417f4530fa5SDavid S. Miller {
418f4530fa5SDavid S. Miller 	return 0;
419f4530fa5SDavid S. Miller }
4207a9bc9b8SDavid S. Miller #endif
4210ddcf43dSAlexander Duyck int fib_unmerge(struct net *net);
42214c85021SArnaldo Carvalho de Melo 
4231da177e4SLinus Torvalds /* Exported by fib_semantics.c */
4245c3a0fd7SJoe Perches int ip_fib_check_default(__be32 gw, struct net_device *dev);
4254f823defSJulian Anastasov int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
4265a56a0b3SMark Tomlinson int fib_sync_down_addr(struct net_device *dev, __be32 local);
427ecc5663cSDavid Ahern int fib_sync_up(struct net_device *dev, unsigned char nh_flags);
428af7d6cceSSabrina Dubroca void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
42906c77c3eSDavid Ahern void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
4300e884c78SPeter Nørlund 
431bf4e0a3dSNikolay Aleksandrov #ifdef CONFIG_IP_ROUTE_MULTIPATH
4327efc0b6bSDavid Ahern int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
433e37b1e97SRoopa Prabhu 		       const struct sk_buff *skb, struct flow_keys *flkeys);
434bf4e0a3dSNikolay Aleksandrov #endif
435ac1fab2dSDavid Ahern int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
436ac1fab2dSDavid Ahern 		 struct netlink_ext_ack *extack);
4370e884c78SPeter Nørlund void fib_select_multipath(struct fib_result *res, int hash);
4383ce58d84SDavid Ahern void fib_select_path(struct net *net, struct fib_result *res,
439bf4e0a3dSNikolay Aleksandrov 		     struct flowi4 *fl4, const struct sk_buff *skb);
4401da177e4SLinus Torvalds 
441e4516ef6SDavid Ahern int fib_nh_init(struct net *net, struct fib_nh *fib_nh,
442e4516ef6SDavid Ahern 		struct fib_config *cfg, int nh_weight,
443e4516ef6SDavid Ahern 		struct netlink_ext_ack *extack);
444faa041a4SDavid Ahern void fib_nh_release(struct net *net, struct fib_nh *fib_nh);
445979e276eSDavid Ahern int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *fc_encap,
446979e276eSDavid Ahern 		       u16 fc_encap_type, void *cfg, gfp_t gfp_flags,
447979e276eSDavid Ahern 		       struct netlink_ext_ack *extack);
448979e276eSDavid Ahern void fib_nh_common_release(struct fib_nh_common *nhc);
449e4516ef6SDavid Ahern 
4505348ba85SDavid S. Miller /* Exported by fib_trie.c */
4515c3a0fd7SJoe Perches void fib_trie_init(void);
4520ddcf43dSAlexander Duyck struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
4531da177e4SLinus Torvalds 
454b6bf3ca0SDavid S. Miller static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
4551da177e4SLinus Torvalds {
456c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
457eba618abSDavid Ahern 	struct fib_nh_common *nhc = res->nhc;
4581da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
4591da177e4SLinus Torvalds 	u32 rtag;
4601da177e4SLinus Torvalds #endif
461*dcb1ecb5SDavid Ahern 	if (nhc->nhc_family == AF_INET) {
462*dcb1ecb5SDavid Ahern 		struct fib_nh *nh;
463*dcb1ecb5SDavid Ahern 
464*dcb1ecb5SDavid Ahern 		nh = container_of(nhc, struct fib_nh, nh_common);
465eba618abSDavid Ahern 		*itag = nh->nh_tclassid << 16;
466*dcb1ecb5SDavid Ahern 	} else {
467*dcb1ecb5SDavid Ahern 		*itag = 0;
468*dcb1ecb5SDavid Ahern 	}
469*dcb1ecb5SDavid Ahern 
4701da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
47185b91b03SDavid S. Miller 	rtag = res->tclassid;
4721da177e4SLinus Torvalds 	if (*itag == 0)
4731da177e4SLinus Torvalds 		*itag = (rtag<<16);
4741da177e4SLinus Torvalds 	*itag |= (rtag>>16);
4751da177e4SLinus Torvalds #endif
4761da177e4SLinus Torvalds #endif
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4799bd83667SDavid Ahern void fib_flush(struct net *net);
4805c3a0fd7SJoe Perches void free_fib_info(struct fib_info *fi);
4811da177e4SLinus Torvalds 
4821c677b3dSIdo Schimmel static inline void fib_info_hold(struct fib_info *fi)
4831c677b3dSIdo Schimmel {
4840029c0deSReshetova, Elena 	refcount_inc(&fi->fib_clntref);
4851c677b3dSIdo Schimmel }
4861c677b3dSIdo Schimmel 
4871da177e4SLinus Torvalds static inline void fib_info_put(struct fib_info *fi)
4881da177e4SLinus Torvalds {
4890029c0deSReshetova, Elena 	if (refcount_dec_and_test(&fi->fib_clntref))
4901da177e4SLinus Torvalds 		free_fib_info(fi);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
49320380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
4945c3a0fd7SJoe Perches int __net_init fib_proc_init(struct net *net);
4955c3a0fd7SJoe Perches void __net_exit fib_proc_exit(struct net *net);
496cc8274f5SLi Zefan #else
497cc8274f5SLi Zefan static inline int fib_proc_init(struct net *net)
498cc8274f5SLi Zefan {
499cc8274f5SLi Zefan 	return 0;
500cc8274f5SLi Zefan }
501cc8274f5SLi Zefan static inline void fib_proc_exit(struct net *net)
502cc8274f5SLi Zefan {
503cc8274f5SLi Zefan }
50420380731SArnaldo Carvalho de Melo #endif
50520380731SArnaldo Carvalho de Melo 
50650d889b1SDavid Ahern u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr);
50750d889b1SDavid Ahern 
5084724676dSDavid Ahern int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
5094724676dSDavid Ahern 			  struct fib_dump_filter *filter,
510effe6792SDavid Ahern 			  struct netlink_callback *cb);
511c0a72077SDavid Ahern 
512c0a72077SDavid Ahern int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nh,
513ecc5663cSDavid Ahern 		     unsigned char *flags, bool skip_oif);
514c0a72077SDavid Ahern int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nh,
515c0a72077SDavid Ahern 		    int nh_weight);
5161da177e4SLinus Torvalds #endif  /* _NET_FIB_H */
517