xref: /linux-6.15/include/net/ip_fib.h (revision f1741730)
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;
35b52f070cSThomas Graf 	/* 3 bytes unused */
364e902c57SThomas Graf 	u32			fc_table;
376d85c10aSAl Viro 	__be32			fc_dst;
386d85c10aSAl Viro 	__be32			fc_gw;
394e902c57SThomas Graf 	int			fc_oif;
404e902c57SThomas Graf 	u32			fc_flags;
414e902c57SThomas Graf 	u32			fc_priority;
426d85c10aSAl Viro 	__be32			fc_prefsrc;
434e902c57SThomas Graf 	struct nlattr		*fc_mx;
444e902c57SThomas Graf 	struct rtnexthop	*fc_mp;
454e902c57SThomas Graf 	int			fc_mx_len;
464e902c57SThomas Graf 	int			fc_mp_len;
474e902c57SThomas Graf 	u32			fc_flow;
484e902c57SThomas Graf 	u32			fc_nlflags;
494e902c57SThomas Graf 	struct nl_info		fc_nlinfo;
50571e7226SRoopa Prabhu 	struct nlattr		*fc_encap;
51571e7226SRoopa Prabhu 	u16			fc_encap_type;
521da177e4SLinus Torvalds };
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds struct fib_info;
55f2bb4bedSDavid S. Miller struct rtable;
561da177e4SLinus Torvalds 
574895c771SDavid S. Miller struct fib_nh_exception {
584895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*fnhe_next;
595aad1de5STimo Teräs 	int				fnhe_genid;
604895c771SDavid S. Miller 	__be32				fnhe_daddr;
614895c771SDavid S. Miller 	u32				fnhe_pmtu;
62d52e5a7eSSabrina Dubroca 	bool				fnhe_mtu_locked;
63aee06da6SJulian Anastasov 	__be32				fnhe_gw;
644895c771SDavid S. Miller 	unsigned long			fnhe_expires;
652ffae99dSTimo Teräs 	struct rtable __rcu		*fnhe_rth_input;
662ffae99dSTimo Teräs 	struct rtable __rcu		*fnhe_rth_output;
674895c771SDavid S. Miller 	unsigned long			fnhe_stamp;
68deed49dfSXin Long 	struct rcu_head			rcu;
694895c771SDavid S. Miller };
704895c771SDavid S. Miller 
714895c771SDavid S. Miller struct fnhe_hash_bucket {
724895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*chain;
734895c771SDavid S. Miller };
744895c771SDavid S. Miller 
75d546c621SEric Dumazet #define FNHE_HASH_SHIFT		11
76d546c621SEric Dumazet #define FNHE_HASH_SIZE		(1 << FNHE_HASH_SHIFT)
774895c771SDavid S. Miller #define FNHE_RECLAIM_DEPTH	5
784895c771SDavid S. Miller 
79*f1741730SDavid Ahern struct fib_nh_common {
80*f1741730SDavid Ahern 	struct net_device	*nhc_dev;
81*f1741730SDavid Ahern 	int			nhc_oif;
82*f1741730SDavid Ahern 	unsigned int		nhc_flags;
83*f1741730SDavid Ahern 	struct lwtunnel_state	*nhc_lwtstate;
84*f1741730SDavid Ahern 	unsigned char		nhc_scope;
85*f1741730SDavid Ahern 	u8			nhc_family;
86*f1741730SDavid Ahern 	u8			nhc_has_gw:1,
87*f1741730SDavid Ahern 				unused:7;
88*f1741730SDavid Ahern 	union {
89*f1741730SDavid Ahern 		__be32          ipv4;
90*f1741730SDavid Ahern 		struct in6_addr ipv6;
91*f1741730SDavid Ahern 	} nhc_gw;
92*f1741730SDavid Ahern 
93*f1741730SDavid Ahern 	int			nhc_weight;
94*f1741730SDavid Ahern 	atomic_t		nhc_upper_bound;
95*f1741730SDavid Ahern };
96*f1741730SDavid Ahern 
971da177e4SLinus Torvalds struct fib_nh {
98*f1741730SDavid Ahern 	struct fib_nh_common	nh_common;
991da177e4SLinus Torvalds 	struct hlist_node	nh_hash;
1001da177e4SLinus Torvalds 	struct fib_info		*nh_parent;
101c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1021da177e4SLinus Torvalds 	__u32			nh_tclassid;
1031da177e4SLinus Torvalds #endif
1041fc050a1SDavid S. Miller 	__be32			nh_saddr;
105436c3b66SDavid S. Miller 	int			nh_saddr_genid;
106d26b3a7cSEric Dumazet 	struct rtable __rcu * __percpu *nh_pcpu_rth_output;
10754764bb6SEric Dumazet 	struct rtable __rcu	*nh_rth_input;
108caa41527SEric Dumazet 	struct fnhe_hash_bucket	__rcu *nh_exceptions;
109*f1741730SDavid Ahern #define fib_nh_family		nh_common.nhc_family
110*f1741730SDavid Ahern #define fib_nh_dev		nh_common.nhc_dev
111*f1741730SDavid Ahern #define fib_nh_oif		nh_common.nhc_oif
112*f1741730SDavid Ahern #define fib_nh_flags		nh_common.nhc_flags
113*f1741730SDavid Ahern #define fib_nh_lws		nh_common.nhc_lwtstate
114*f1741730SDavid Ahern #define fib_nh_scope		nh_common.nhc_scope
115*f1741730SDavid Ahern #define fib_nh_family		nh_common.nhc_family
116*f1741730SDavid Ahern #define fib_nh_has_gw		nh_common.nhc_has_gw
117*f1741730SDavid Ahern #define fib_nh_gw4		nh_common.nhc_gw.ipv4
118*f1741730SDavid Ahern #define fib_nh_gw6		nh_common.nhc_gw.ipv6
119*f1741730SDavid Ahern #define fib_nh_weight		nh_common.nhc_weight
120*f1741730SDavid Ahern #define fib_nh_upper_bound	nh_common.nhc_upper_bound
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /*
1241da177e4SLinus Torvalds  * This structure contains data shared by many of routes.
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds struct fib_info {
1281da177e4SLinus Torvalds 	struct hlist_node	fib_hash;
1291da177e4SLinus Torvalds 	struct hlist_node	fib_lhash;
1307462bd74SDenis V. Lunev 	struct net		*fib_net;
1311da177e4SLinus Torvalds 	int			fib_treeref;
1320029c0deSReshetova, Elena 	refcount_t		fib_clntref;
13395c96174SEric Dumazet 	unsigned int		fib_flags;
13437e826c5SDavid S. Miller 	unsigned char		fib_dead;
13537e826c5SDavid S. Miller 	unsigned char		fib_protocol;
13637e826c5SDavid S. Miller 	unsigned char		fib_scope;
137f4ef85bbSEric Dumazet 	unsigned char		fib_type;
138b83738aeSAl Viro 	__be32			fib_prefsrc;
1395a56a0b3SMark Tomlinson 	u32			fib_tb_id;
1401da177e4SLinus Torvalds 	u32			fib_priority;
1413fb07dafSEric Dumazet 	struct dst_metrics	*fib_metrics;
1423fb07dafSEric Dumazet #define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
1433fb07dafSEric Dumazet #define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
1443fb07dafSEric Dumazet #define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
1453fb07dafSEric Dumazet #define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
1461da177e4SLinus Torvalds 	int			fib_nhs;
147ebc0ffaeSEric Dumazet 	struct rcu_head		rcu;
1481da177e4SLinus Torvalds 	struct fib_nh		fib_nh[0];
149b75ed8b1SDavid Ahern #define fib_dev		fib_nh[0].fib_nh_dev
1501da177e4SLinus Torvalds };
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
1541da177e4SLinus Torvalds struct fib_rule;
1551da177e4SLinus Torvalds #endif
1561da177e4SLinus Torvalds 
1575b470441SDavid S. Miller struct fib_table;
1581da177e4SLinus Torvalds struct fib_result {
1596ffd9034SDavid Ahern 	__be32		prefix;
1601da177e4SLinus Torvalds 	unsigned char	prefixlen;
1611da177e4SLinus Torvalds 	unsigned char	nh_sel;
1621da177e4SLinus Torvalds 	unsigned char	type;
1631da177e4SLinus Torvalds 	unsigned char	scope;
16485b91b03SDavid S. Miller 	u32		tclassid;
1651da177e4SLinus Torvalds 	struct fib_info *fi;
1665b470441SDavid S. Miller 	struct fib_table *table;
16756315f9eSAlexander Duyck 	struct hlist_head *fa_head;
1681da177e4SLinus Torvalds };
1691da177e4SLinus Torvalds 
170246955feSRobert Olsson struct fib_result_nl {
17180e856e1SAl Viro 	__be32		fl_addr;   /* To be looked up*/
1725f300893SThomas Graf 	u32		fl_mark;
173246955feSRobert Olsson 	unsigned char	fl_tos;
174246955feSRobert Olsson 	unsigned char   fl_scope;
175246955feSRobert Olsson 	unsigned char   tb_id_in;
176246955feSRobert Olsson 
177246955feSRobert Olsson 	unsigned char   tb_id;      /* Results */
178246955feSRobert Olsson 	unsigned char	prefixlen;
179246955feSRobert Olsson 	unsigned char	nh_sel;
180246955feSRobert Olsson 	unsigned char	type;
181246955feSRobert Olsson 	unsigned char	scope;
182246955feSRobert Olsson 	int             err;
183246955feSRobert Olsson };
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
1861da177e4SLinus Torvalds #define FIB_RES_NH(res)		((res).fi->fib_nh[(res).nh_sel])
1871da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */
1881da177e4SLinus Torvalds #define FIB_RES_NH(res)		((res).fi->fib_nh[0])
1891da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */
1901da177e4SLinus Torvalds 
1915b9e12dbSDenis V. Lunev #ifdef CONFIG_IP_MULTIPLE_TABLES
1925b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 256
1935b9e12dbSDenis V. Lunev #else
1945b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 2
1955b9e12dbSDenis V. Lunev #endif
1965b9e12dbSDenis V. Lunev 
1975c3a0fd7SJoe Perches __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh);
198436c3b66SDavid S. Miller 
199436c3b66SDavid S. Miller #define FIB_RES_SADDR(net, res)				\
200436c3b66SDavid S. Miller 	((FIB_RES_NH(res).nh_saddr_genid ==		\
201436c3b66SDavid S. Miller 	  atomic_read(&(net)->ipv4.dev_addr_genid)) ?	\
202436c3b66SDavid S. Miller 	 FIB_RES_NH(res).nh_saddr :			\
203436c3b66SDavid S. Miller 	 fib_info_update_nh_saddr((net), &FIB_RES_NH(res)))
204b75ed8b1SDavid Ahern #define FIB_RES_GW(res)			(FIB_RES_NH(res).fib_nh_gw4)
205b75ed8b1SDavid Ahern #define FIB_RES_DEV(res)		(FIB_RES_NH(res).fib_nh_dev)
206b75ed8b1SDavid Ahern #define FIB_RES_OIF(res)		(FIB_RES_NH(res).fib_nh_oif)
2071da177e4SLinus Torvalds 
208436c3b66SDavid S. Miller #define FIB_RES_PREFSRC(net, res)	((res).fi->fib_prefsrc ? : \
209436c3b66SDavid S. Miller 					 FIB_RES_SADDR(net, res))
2101fc050a1SDavid S. Miller 
211b90eb754SJiri Pirko struct fib_entry_notifier_info {
212b90eb754SJiri Pirko 	struct fib_notifier_info info; /* must be first */
213b90eb754SJiri Pirko 	u32 dst;
214b90eb754SJiri Pirko 	int dst_len;
215b90eb754SJiri Pirko 	struct fib_info *fi;
216b90eb754SJiri Pirko 	u8 tos;
217b90eb754SJiri Pirko 	u8 type;
218b90eb754SJiri Pirko 	u32 tb_id;
219b90eb754SJiri Pirko };
220b90eb754SJiri Pirko 
221982acb97SIdo Schimmel struct fib_nh_notifier_info {
222982acb97SIdo Schimmel 	struct fib_notifier_info info; /* must be first */
223982acb97SIdo Schimmel 	struct fib_nh *fib_nh;
224982acb97SIdo Schimmel };
225982acb97SIdo Schimmel 
22604b1d4e5SIdo Schimmel int call_fib4_notifier(struct notifier_block *nb, struct net *net,
227c0243892SIdo Schimmel 		       enum fib_event_type event_type,
228c0243892SIdo Schimmel 		       struct fib_notifier_info *info);
22904b1d4e5SIdo Schimmel int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
230b90eb754SJiri Pirko 			struct fib_notifier_info *info);
231b90eb754SJiri Pirko 
23204b1d4e5SIdo Schimmel int __net_init fib4_notifier_init(struct net *net);
23304b1d4e5SIdo Schimmel void __net_exit fib4_notifier_exit(struct net *net);
23404b1d4e5SIdo Schimmel 
235d05f7a7dSIdo Schimmel void fib_notify(struct net *net, struct notifier_block *nb);
236c0243892SIdo Schimmel 
2371da177e4SLinus Torvalds struct fib_table {
2381af5a8c4SPatrick McHardy 	struct hlist_node	tb_hlist;
2392dfe55b4SPatrick McHardy 	u32			tb_id;
24021d8c49eSDavid S. Miller 	int			tb_num_default;
241a7e53531SAlexander Duyck 	struct rcu_head		rcu;
2420ddcf43dSAlexander Duyck 	unsigned long 		*tb_data;
2430ddcf43dSAlexander Duyck 	unsigned long		__data[0];
2441da177e4SLinus Torvalds };
2451da177e4SLinus Torvalds 
2464724676dSDavid Ahern struct fib_dump_filter {
2474724676dSDavid Ahern 	u32			table_id;
2484724676dSDavid Ahern 	/* filter_set is an optimization that an entry is set */
2494724676dSDavid Ahern 	bool			filter_set;
250ae677bbbSDavid Ahern 	bool			dump_all_families;
2514724676dSDavid Ahern 	unsigned char		protocol;
2524724676dSDavid Ahern 	unsigned char		rt_type;
2534724676dSDavid Ahern 	unsigned int		flags;
2544724676dSDavid Ahern 	struct net_device	*dev;
2554724676dSDavid Ahern };
2564724676dSDavid Ahern 
2575c3a0fd7SJoe Perches int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
258ebc0ffaeSEric Dumazet 		     struct fib_result *res, int fib_flags);
2596d8422a1SDavid Ahern int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
2606d8422a1SDavid Ahern 		     struct netlink_ext_ack *extack);
26178055998SDavid Ahern int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
26278055998SDavid Ahern 		     struct netlink_ext_ack *extack);
2635c3a0fd7SJoe Perches int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
26418a8021aSDavid Ahern 		   struct netlink_callback *cb, struct fib_dump_filter *filter);
265f97f4dd8SIdo Schimmel int fib_table_flush(struct net *net, struct fib_table *table, bool flush_all);
2660ddcf43dSAlexander Duyck struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
2673b709334SAlexander Duyck void fib_table_flush_external(struct fib_table *table);
2685c3a0fd7SJoe Perches void fib_free_table(struct fib_table *tb);
2694aa2c466SPavel Emelyanov 
2701da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTIPLE_TABLES
2711da177e4SLinus Torvalds 
272a5a519b2SAlexander Duyck #define TABLE_LOCAL_INDEX	(RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
273a5a519b2SAlexander Duyck #define TABLE_MAIN_INDEX	(RT_TABLE_MAIN  & (FIB_TABLE_HASHSZ - 1))
2741da177e4SLinus Torvalds 
2758ad4942cSDenis V. Lunev static inline struct fib_table *fib_get_table(struct net *net, u32 id)
2761da177e4SLinus Torvalds {
277a7e53531SAlexander Duyck 	struct hlist_node *tb_hlist;
27893456b6dSDenis V. Lunev 	struct hlist_head *ptr;
27993456b6dSDenis V. Lunev 
28093456b6dSDenis V. Lunev 	ptr = id == RT_TABLE_LOCAL ?
281e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
282e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
283a7e53531SAlexander Duyck 
284a7e53531SAlexander Duyck 	tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
285a7e53531SAlexander Duyck 
286a7e53531SAlexander Duyck 	return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2898ad4942cSDenis V. Lunev static inline struct fib_table *fib_new_table(struct net *net, u32 id)
2901da177e4SLinus Torvalds {
2918ad4942cSDenis V. Lunev 	return fib_get_table(net, id);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
29422bd5b9bSDavid S. Miller static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
2950eeb075fSAndy Gospodarek 			     struct fib_result *res, unsigned int flags)
2961da177e4SLinus Torvalds {
297a7e53531SAlexander Duyck 	struct fib_table *tb;
2980ddcf43dSAlexander Duyck 	int err = -ENETUNREACH;
29993456b6dSDenis V. Lunev 
300345e9b54SAlexander Duyck 	rcu_read_lock();
30193456b6dSDenis V. Lunev 
302a7e53531SAlexander Duyck 	tb = fib_get_table(net, RT_TABLE_MAIN);
3030315e382SNikola Forró 	if (tb)
3040315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
3050315e382SNikola Forró 
3060315e382SNikola Forró 	if (err == -EAGAIN)
3070315e382SNikola Forró 		err = -ENETUNREACH;
308345e9b54SAlexander Duyck 
309345e9b54SAlexander Duyck 	rcu_read_unlock();
310345e9b54SAlexander Duyck 
311345e9b54SAlexander Duyck 	return err;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
3143c71006dSIdo Schimmel static inline bool fib4_rule_default(const struct fib_rule *rule)
3153c71006dSIdo Schimmel {
3163c71006dSIdo Schimmel 	return true;
3173c71006dSIdo Schimmel }
3183c71006dSIdo Schimmel 
3191b2a4440SIdo Schimmel static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb)
3201b2a4440SIdo Schimmel {
3211b2a4440SIdo Schimmel 	return 0;
3221b2a4440SIdo Schimmel }
3231b2a4440SIdo Schimmel 
3241b2a4440SIdo Schimmel static inline unsigned int fib4_rules_seq_read(struct net *net)
3251b2a4440SIdo Schimmel {
3261b2a4440SIdo Schimmel 	return 0;
3271b2a4440SIdo Schimmel }
3281b2a4440SIdo Schimmel 
329e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
330e37b1e97SRoopa Prabhu 						 struct sk_buff *skb,
331e37b1e97SRoopa Prabhu 						 struct flowi4 *fl4,
332e37b1e97SRoopa Prabhu 						 struct flow_keys *flkeys)
333e37b1e97SRoopa Prabhu {
334e37b1e97SRoopa Prabhu 	return false;
335e37b1e97SRoopa Prabhu }
3361da177e4SLinus Torvalds #else /* CONFIG_IP_MULTIPLE_TABLES */
3375c3a0fd7SJoe Perches int __net_init fib4_rules_init(struct net *net);
3385c3a0fd7SJoe Perches void __net_exit fib4_rules_exit(struct net *net);
339c3e9a353SPavel Emelyanov 
3405c3a0fd7SJoe Perches struct fib_table *fib_new_table(struct net *net, u32 id);
3415c3a0fd7SJoe Perches struct fib_table *fib_get_table(struct net *net, u32 id);
3421da177e4SLinus Torvalds 
3430eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp,
3440eeb075fSAndy Gospodarek 		 struct fib_result *res, unsigned int flags);
345f4530fa5SDavid S. Miller 
346f4530fa5SDavid S. Miller static inline int fib_lookup(struct net *net, struct flowi4 *flp,
3470eeb075fSAndy Gospodarek 			     struct fib_result *res, unsigned int flags)
348f4530fa5SDavid S. Miller {
349a7e53531SAlexander Duyck 	struct fib_table *tb;
3500315e382SNikola Forró 	int err = -ENETUNREACH;
351a7e53531SAlexander Duyck 
3520eeb075fSAndy Gospodarek 	flags |= FIB_LOOKUP_NOREF;
353a7e53531SAlexander Duyck 	if (net->ipv4.fib_has_custom_rules)
3540eeb075fSAndy Gospodarek 		return __fib_lookup(net, flp, res, flags);
355345e9b54SAlexander Duyck 
356345e9b54SAlexander Duyck 	rcu_read_lock();
357345e9b54SAlexander Duyck 
35885b91b03SDavid S. Miller 	res->tclassid = 0;
359a7e53531SAlexander Duyck 
360a7e53531SAlexander Duyck 	tb = rcu_dereference_rtnl(net->ipv4.fib_main);
3610315e382SNikola Forró 	if (tb)
3620315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags);
3630315e382SNikola Forró 
3640315e382SNikola Forró 	if (!err)
3650315e382SNikola Forró 		goto out;
366a7e53531SAlexander Duyck 
367a7e53531SAlexander Duyck 	tb = rcu_dereference_rtnl(net->ipv4.fib_default);
3680315e382SNikola Forró 	if (tb)
3690315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags);
3700315e382SNikola Forró 
3710315e382SNikola Forró out:
3720315e382SNikola Forró 	if (err == -EAGAIN)
3730315e382SNikola Forró 		err = -ENETUNREACH;
374345e9b54SAlexander Duyck 
375345e9b54SAlexander Duyck 	rcu_read_unlock();
376345e9b54SAlexander Duyck 
377345e9b54SAlexander Duyck 	return err;
378f4530fa5SDavid S. Miller }
379f4530fa5SDavid S. Miller 
3803c71006dSIdo Schimmel bool fib4_rule_default(const struct fib_rule *rule);
3811b2a4440SIdo Schimmel int fib4_rules_dump(struct net *net, struct notifier_block *nb);
3821b2a4440SIdo Schimmel unsigned int fib4_rules_seq_read(struct net *net);
3833c71006dSIdo Schimmel 
384e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
385e37b1e97SRoopa Prabhu 						 struct sk_buff *skb,
386e37b1e97SRoopa Prabhu 						 struct flowi4 *fl4,
387e37b1e97SRoopa Prabhu 						 struct flow_keys *flkeys)
388e37b1e97SRoopa Prabhu {
389e37b1e97SRoopa Prabhu 	unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
390e37b1e97SRoopa Prabhu 
391e37b1e97SRoopa Prabhu 	if (!net->ipv4.fib_rules_require_fldissect)
392e37b1e97SRoopa Prabhu 		return false;
393e37b1e97SRoopa Prabhu 
394e37b1e97SRoopa Prabhu 	skb_flow_dissect_flow_keys(skb, flkeys, flag);
395e37b1e97SRoopa Prabhu 	fl4->fl4_sport = flkeys->ports.src;
396e37b1e97SRoopa Prabhu 	fl4->fl4_dport = flkeys->ports.dst;
397e37b1e97SRoopa Prabhu 	fl4->flowi4_proto = flkeys->basic.ip_proto;
398e37b1e97SRoopa Prabhu 
399e37b1e97SRoopa Prabhu 	return true;
400e37b1e97SRoopa Prabhu }
401e37b1e97SRoopa Prabhu 
4021da177e4SLinus Torvalds #endif /* CONFIG_IP_MULTIPLE_TABLES */
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds /* Exported by fib_frontend.c */
405ef7c79edSPatrick McHardy extern const struct nla_policy rtm_ipv4_policy[];
4065c3a0fd7SJoe Perches void ip_fib_init(void);
4075c3a0fd7SJoe Perches __be32 fib_compute_spec_dst(struct sk_buff *skb);
40878f2756cSDavid Ahern bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
4095c3a0fd7SJoe Perches int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
4105c04c819SMichael Smith 			u8 tos, int oif, struct net_device *dev,
4119e56e380SDavid S. Miller 			struct in_device *idev, u32 *itag);
4127a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
413f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
414f4530fa5SDavid S. Miller {
415f4530fa5SDavid S. Miller 	return net->ipv4.fib_num_tclassid_users;
416f4530fa5SDavid S. Miller }
4177a9bc9b8SDavid S. Miller #else
418f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
419f4530fa5SDavid S. Miller {
420f4530fa5SDavid S. Miller 	return 0;
421f4530fa5SDavid S. Miller }
4227a9bc9b8SDavid S. Miller #endif
4230ddcf43dSAlexander Duyck int fib_unmerge(struct net *net);
42414c85021SArnaldo Carvalho de Melo 
4251da177e4SLinus Torvalds /* Exported by fib_semantics.c */
4265c3a0fd7SJoe Perches int ip_fib_check_default(__be32 gw, struct net_device *dev);
4274f823defSJulian Anastasov int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
4285a56a0b3SMark Tomlinson int fib_sync_down_addr(struct net_device *dev, __be32 local);
4298a3d0316SAndy Gospodarek int fib_sync_up(struct net_device *dev, unsigned int nh_flags);
430af7d6cceSSabrina Dubroca void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
4310e884c78SPeter Nørlund 
432bf4e0a3dSNikolay Aleksandrov #ifdef CONFIG_IP_ROUTE_MULTIPATH
4337efc0b6bSDavid Ahern int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
434e37b1e97SRoopa Prabhu 		       const struct sk_buff *skb, struct flow_keys *flkeys);
435bf4e0a3dSNikolay Aleksandrov #endif
4360e884c78SPeter Nørlund void fib_select_multipath(struct fib_result *res, int hash);
4373ce58d84SDavid Ahern void fib_select_path(struct net *net, struct fib_result *res,
438bf4e0a3dSNikolay Aleksandrov 		     struct flowi4 *fl4, const struct sk_buff *skb);
4391da177e4SLinus Torvalds 
440e4516ef6SDavid Ahern int fib_nh_init(struct net *net, struct fib_nh *fib_nh,
441e4516ef6SDavid Ahern 		struct fib_config *cfg, int nh_weight,
442e4516ef6SDavid Ahern 		struct netlink_ext_ack *extack);
443faa041a4SDavid Ahern void fib_nh_release(struct net *net, struct fib_nh *fib_nh);
444e4516ef6SDavid Ahern 
4455348ba85SDavid S. Miller /* Exported by fib_trie.c */
4465c3a0fd7SJoe Perches void fib_trie_init(void);
4470ddcf43dSAlexander Duyck struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
4481da177e4SLinus Torvalds 
449b6bf3ca0SDavid S. Miller static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
4501da177e4SLinus Torvalds {
451c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
4521da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
4531da177e4SLinus Torvalds 	u32 rtag;
4541da177e4SLinus Torvalds #endif
4551da177e4SLinus Torvalds 	*itag = FIB_RES_NH(*res).nh_tclassid<<16;
4561da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
45785b91b03SDavid S. Miller 	rtag = res->tclassid;
4581da177e4SLinus Torvalds 	if (*itag == 0)
4591da177e4SLinus Torvalds 		*itag = (rtag<<16);
4601da177e4SLinus Torvalds 	*itag |= (rtag>>16);
4611da177e4SLinus Torvalds #endif
4621da177e4SLinus Torvalds #endif
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4655c3a0fd7SJoe Perches void free_fib_info(struct fib_info *fi);
4661da177e4SLinus Torvalds 
4671c677b3dSIdo Schimmel static inline void fib_info_hold(struct fib_info *fi)
4681c677b3dSIdo Schimmel {
4690029c0deSReshetova, Elena 	refcount_inc(&fi->fib_clntref);
4701c677b3dSIdo Schimmel }
4711c677b3dSIdo Schimmel 
4721da177e4SLinus Torvalds static inline void fib_info_put(struct fib_info *fi)
4731da177e4SLinus Torvalds {
4740029c0deSReshetova, Elena 	if (refcount_dec_and_test(&fi->fib_clntref))
4751da177e4SLinus Torvalds 		free_fib_info(fi);
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
47820380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
4795c3a0fd7SJoe Perches int __net_init fib_proc_init(struct net *net);
4805c3a0fd7SJoe Perches void __net_exit fib_proc_exit(struct net *net);
481cc8274f5SLi Zefan #else
482cc8274f5SLi Zefan static inline int fib_proc_init(struct net *net)
483cc8274f5SLi Zefan {
484cc8274f5SLi Zefan 	return 0;
485cc8274f5SLi Zefan }
486cc8274f5SLi Zefan static inline void fib_proc_exit(struct net *net)
487cc8274f5SLi Zefan {
488cc8274f5SLi Zefan }
48920380731SArnaldo Carvalho de Melo #endif
49020380731SArnaldo Carvalho de Melo 
49150d889b1SDavid Ahern u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr);
49250d889b1SDavid Ahern 
4934724676dSDavid Ahern int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
4944724676dSDavid Ahern 			  struct fib_dump_filter *filter,
495effe6792SDavid Ahern 			  struct netlink_callback *cb);
4961da177e4SLinus Torvalds #endif  /* _NET_FIB_H */
497