xref: /linux-6.15/include/net/ip_fib.h (revision 1fa3314c)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET  is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		Definitions for the Forwarding Information Base.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	A.N.Kuznetsov, <[email protected]>
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #ifndef _NET_IP_FIB_H
131da177e4SLinus Torvalds #define _NET_IP_FIB_H
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <net/flow.h>
161da177e4SLinus Torvalds #include <linux/seq_file.h>
174895c771SDavid S. Miller #include <linux/rcupdate.h>
1804b1d4e5SIdo Schimmel #include <net/fib_notifier.h>
19e1ef4bf2SThomas Graf #include <net/fib_rules.h>
20f55fbb6aSGuillaume Nault #include <net/inet_dscp.h>
218e773277SDavid S. Miller #include <net/inetpeer.h>
22d26b3a7cSEric Dumazet #include <linux/percpu.h>
23b90eb754SJiri Pirko #include <linux/notifier.h>
240029c0deSReshetova, Elena #include <linux/refcount.h>
25*1fa3314cSIdo Schimmel #include <linux/in_route.h>
261da177e4SLinus Torvalds 
274e902c57SThomas Graf struct fib_config {
284e902c57SThomas Graf 	u8			fc_dst_len;
29f55fbb6aSGuillaume Nault 	dscp_t			fc_dscp;
304e902c57SThomas Graf 	u8			fc_protocol;
314e902c57SThomas Graf 	u8			fc_scope;
324e902c57SThomas Graf 	u8			fc_type;
33f35b794bSDavid Ahern 	u8			fc_gw_family;
34f35b794bSDavid Ahern 	/* 2 bytes unused */
354e902c57SThomas Graf 	u32			fc_table;
366d85c10aSAl Viro 	__be32			fc_dst;
37a4ea5d43SDavid Ahern 	union {
38f35b794bSDavid Ahern 		__be32		fc_gw4;
39a4ea5d43SDavid Ahern 		struct in6_addr	fc_gw6;
40a4ea5d43SDavid Ahern 	};
414e902c57SThomas Graf 	int			fc_oif;
424e902c57SThomas Graf 	u32			fc_flags;
434e902c57SThomas Graf 	u32			fc_priority;
446d85c10aSAl Viro 	__be32			fc_prefsrc;
45493ced1aSDavid Ahern 	u32			fc_nh_id;
464e902c57SThomas Graf 	struct nlattr		*fc_mx;
474e902c57SThomas Graf 	struct rtnexthop	*fc_mp;
484e902c57SThomas Graf 	int			fc_mx_len;
494e902c57SThomas Graf 	int			fc_mp_len;
504e902c57SThomas Graf 	u32			fc_flow;
514e902c57SThomas Graf 	u32			fc_nlflags;
524e902c57SThomas Graf 	struct nl_info		fc_nlinfo;
53571e7226SRoopa Prabhu 	struct nlattr		*fc_encap;
54571e7226SRoopa Prabhu 	u16			fc_encap_type;
551da177e4SLinus Torvalds };
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds struct fib_info;
58f2bb4bedSDavid S. Miller struct rtable;
591da177e4SLinus Torvalds 
604895c771SDavid S. Miller struct fib_nh_exception {
614895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*fnhe_next;
625aad1de5STimo Teräs 	int				fnhe_genid;
634895c771SDavid S. Miller 	__be32				fnhe_daddr;
644895c771SDavid S. Miller 	u32				fnhe_pmtu;
65d52e5a7eSSabrina Dubroca 	bool				fnhe_mtu_locked;
66aee06da6SJulian Anastasov 	__be32				fnhe_gw;
674895c771SDavid S. Miller 	unsigned long			fnhe_expires;
682ffae99dSTimo Teräs 	struct rtable __rcu		*fnhe_rth_input;
692ffae99dSTimo Teräs 	struct rtable __rcu		*fnhe_rth_output;
704895c771SDavid S. Miller 	unsigned long			fnhe_stamp;
71deed49dfSXin Long 	struct rcu_head			rcu;
724895c771SDavid S. Miller };
734895c771SDavid S. Miller 
744895c771SDavid S. Miller struct fnhe_hash_bucket {
754895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*chain;
764895c771SDavid S. Miller };
774895c771SDavid S. Miller 
78d546c621SEric Dumazet #define FNHE_HASH_SHIFT		11
79d546c621SEric Dumazet #define FNHE_HASH_SIZE		(1 << FNHE_HASH_SHIFT)
804895c771SDavid S. Miller #define FNHE_RECLAIM_DEPTH	5
814895c771SDavid S. Miller 
82f1741730SDavid Ahern struct fib_nh_common {
83f1741730SDavid Ahern 	struct net_device	*nhc_dev;
84e44b14ebSEric Dumazet 	netdevice_tracker	nhc_dev_tracker;
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
117e44b14ebSEric Dumazet #define fib_nh_dev_tracker	nh_common.nhc_dev_tracker
118f1741730SDavid Ahern #define fib_nh_oif		nh_common.nhc_oif
119f1741730SDavid Ahern #define fib_nh_flags		nh_common.nhc_flags
120f1741730SDavid Ahern #define fib_nh_lws		nh_common.nhc_lwtstate
121f1741730SDavid Ahern #define fib_nh_scope		nh_common.nhc_scope
122bdf00467SDavid Ahern #define fib_nh_gw_family	nh_common.nhc_gw_family
123f1741730SDavid Ahern #define fib_nh_gw4		nh_common.nhc_gw.ipv4
124f1741730SDavid Ahern #define fib_nh_gw6		nh_common.nhc_gw.ipv6
125f1741730SDavid Ahern #define fib_nh_weight		nh_common.nhc_weight
126f1741730SDavid Ahern #define fib_nh_upper_bound	nh_common.nhc_upper_bound
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
1301da177e4SLinus Torvalds  * This structure contains data shared by many of routes.
1311da177e4SLinus Torvalds  */
1321da177e4SLinus Torvalds 
1334c7e8084SDavid Ahern struct nexthop;
1344c7e8084SDavid Ahern 
1351da177e4SLinus Torvalds struct fib_info {
1361da177e4SLinus Torvalds 	struct hlist_node	fib_hash;
1371da177e4SLinus Torvalds 	struct hlist_node	fib_lhash;
1384c7e8084SDavid Ahern 	struct list_head	nh_list;
1397462bd74SDenis V. Lunev 	struct net		*fib_net;
14079976892SYajun Deng 	refcount_t		fib_treeref;
1410029c0deSReshetova, Elena 	refcount_t		fib_clntref;
14295c96174SEric Dumazet 	unsigned int		fib_flags;
14337e826c5SDavid S. Miller 	unsigned char		fib_dead;
14437e826c5SDavid S. Miller 	unsigned char		fib_protocol;
14537e826c5SDavid S. Miller 	unsigned char		fib_scope;
146f4ef85bbSEric Dumazet 	unsigned char		fib_type;
147b83738aeSAl Viro 	__be32			fib_prefsrc;
1485a56a0b3SMark Tomlinson 	u32			fib_tb_id;
1491da177e4SLinus Torvalds 	u32			fib_priority;
1503fb07dafSEric Dumazet 	struct dst_metrics	*fib_metrics;
1513fb07dafSEric Dumazet #define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
1523fb07dafSEric Dumazet #define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
1533fb07dafSEric Dumazet #define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
1543fb07dafSEric Dumazet #define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
1551da177e4SLinus Torvalds 	int			fib_nhs;
15619a9d136SDavid Ahern 	bool			fib_nh_is_v6;
1571bff1a0cSDavid Ahern 	bool			nh_updated;
1584b2b6060SHangbin Liu 	bool			pfsrc_removed;
1594c7e8084SDavid Ahern 	struct nexthop		*nh;
160ebc0ffaeSEric Dumazet 	struct rcu_head		rcu;
1615b98fd5dSKees Cook 	struct fib_nh		fib_nh[] __counted_by(fib_nhs);
1621da177e4SLinus Torvalds };
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
1661da177e4SLinus Torvalds struct fib_rule;
1671da177e4SLinus Torvalds #endif
1681da177e4SLinus Torvalds 
1695b470441SDavid S. Miller struct fib_table;
1701da177e4SLinus Torvalds struct fib_result {
1716ffd9034SDavid Ahern 	__be32			prefix;
1721da177e4SLinus Torvalds 	unsigned char		prefixlen;
1731da177e4SLinus Torvalds 	unsigned char		nh_sel;
1741da177e4SLinus Torvalds 	unsigned char		type;
1751da177e4SLinus Torvalds 	unsigned char		scope;
17685b91b03SDavid S. Miller 	u32			tclassid;
177338bb57eSIdo Schimmel 	dscp_t			dscp;
178eba618abSDavid Ahern 	struct fib_nh_common	*nhc;
1791da177e4SLinus Torvalds 	struct fib_info		*fi;
1805b470441SDavid S. Miller 	struct fib_table	*table;
18156315f9eSAlexander Duyck 	struct hlist_head	*fa_head;
1821da177e4SLinus Torvalds };
1831da177e4SLinus Torvalds 
184246955feSRobert Olsson struct fib_result_nl {
18580e856e1SAl Viro 	__be32		fl_addr;   /* To be looked up*/
1865f300893SThomas Graf 	u32		fl_mark;
187246955feSRobert Olsson 	unsigned char	fl_tos;
188246955feSRobert Olsson 	unsigned char   fl_scope;
189246955feSRobert Olsson 	unsigned char   tb_id_in;
190246955feSRobert Olsson 
191246955feSRobert Olsson 	unsigned char   tb_id;      /* Results */
192246955feSRobert Olsson 	unsigned char	prefixlen;
193246955feSRobert Olsson 	unsigned char	nh_sel;
194246955feSRobert Olsson 	unsigned char	type;
195246955feSRobert Olsson 	unsigned char	scope;
196246955feSRobert Olsson 	int             err;
197246955feSRobert Olsson };
1981da177e4SLinus Torvalds 
1995b9e12dbSDenis V. Lunev #ifdef CONFIG_IP_MULTIPLE_TABLES
2005b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 256
2015b9e12dbSDenis V. Lunev #else
2025b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 2
2035b9e12dbSDenis V. Lunev #endif
2045b9e12dbSDenis V. Lunev 
205dcb1ecb5SDavid Ahern __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
206c3669486SDavid Ahern 				 unsigned char scope);
207eba618abSDavid Ahern __be32 fib_result_prefsrc(struct net *net, struct fib_result *res);
208436c3b66SDavid S. Miller 
209eba618abSDavid Ahern #define FIB_RES_NHC(res)		((res).nhc)
210eba618abSDavid Ahern #define FIB_RES_DEV(res)	(FIB_RES_NHC(res)->nhc_dev)
211eba618abSDavid Ahern #define FIB_RES_OIF(res)	(FIB_RES_NHC(res)->nhc_oif)
2121fc050a1SDavid S. Miller 
2131e301fd0SIdo Schimmel struct fib_rt_info {
2141e301fd0SIdo Schimmel 	struct fib_info		*fi;
2151e301fd0SIdo Schimmel 	u32			tb_id;
2161e301fd0SIdo Schimmel 	__be32			dst;
2171e301fd0SIdo Schimmel 	int			dst_len;
218888ade8fSGuillaume Nault 	dscp_t			dscp;
2191e301fd0SIdo Schimmel 	u8			type;
22090b93f1bSIdo Schimmel 	u8			offload:1,
22190b93f1bSIdo Schimmel 				trap:1,
22236c5100eSAmit Cohen 				offload_failed:1,
22336c5100eSAmit Cohen 				unused:5;
2241e301fd0SIdo Schimmel };
2251e301fd0SIdo Schimmel 
226b90eb754SJiri Pirko struct fib_entry_notifier_info {
227b90eb754SJiri Pirko 	struct fib_notifier_info info; /* must be first */
228b90eb754SJiri Pirko 	u32 dst;
229b90eb754SJiri Pirko 	int dst_len;
230b90eb754SJiri Pirko 	struct fib_info *fi;
231568a3f33SGuillaume Nault 	dscp_t dscp;
232b90eb754SJiri Pirko 	u8 type;
233b90eb754SJiri Pirko 	u32 tb_id;
234b90eb754SJiri Pirko };
235b90eb754SJiri Pirko 
236982acb97SIdo Schimmel struct fib_nh_notifier_info {
237982acb97SIdo Schimmel 	struct fib_notifier_info info; /* must be first */
238982acb97SIdo Schimmel 	struct fib_nh *fib_nh;
239982acb97SIdo Schimmel };
240982acb97SIdo Schimmel 
2417c550dafSJiri Pirko int call_fib4_notifier(struct notifier_block *nb,
242c0243892SIdo Schimmel 		       enum fib_event_type event_type,
243c0243892SIdo Schimmel 		       struct fib_notifier_info *info);
24404b1d4e5SIdo Schimmel int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
245b90eb754SJiri Pirko 			struct fib_notifier_info *info);
246b90eb754SJiri Pirko 
24704b1d4e5SIdo Schimmel int __net_init fib4_notifier_init(struct net *net);
24804b1d4e5SIdo Schimmel void __net_exit fib4_notifier_exit(struct net *net);
24904b1d4e5SIdo Schimmel 
2501bff1a0cSDavid Ahern void fib_info_notify_update(struct net *net, struct nl_info *info);
251b7a59557SJiri Pirko int fib_notify(struct net *net, struct notifier_block *nb,
252b7a59557SJiri Pirko 	       struct netlink_ext_ack *extack);
253c0243892SIdo Schimmel 
2541da177e4SLinus Torvalds struct fib_table {
2551af5a8c4SPatrick McHardy 	struct hlist_node	tb_hlist;
2562dfe55b4SPatrick McHardy 	u32			tb_id;
25721d8c49eSDavid S. Miller 	int			tb_num_default;
258a7e53531SAlexander Duyck 	struct rcu_head		rcu;
2590ddcf43dSAlexander Duyck 	unsigned long 		*tb_data;
260a5311060SGustavo A. R. Silva 	unsigned long		__data[];
2611da177e4SLinus Torvalds };
2621da177e4SLinus Torvalds 
2634724676dSDavid Ahern struct fib_dump_filter {
2644724676dSDavid Ahern 	u32			table_id;
2654724676dSDavid Ahern 	/* filter_set is an optimization that an entry is set */
2664724676dSDavid Ahern 	bool			filter_set;
267564c91f7SStefano Brivio 	bool			dump_routes;
268564c91f7SStefano Brivio 	bool			dump_exceptions;
26922e36ea9SEric Dumazet 	bool			rtnl_held;
2704724676dSDavid Ahern 	unsigned char		protocol;
2714724676dSDavid Ahern 	unsigned char		rt_type;
2724724676dSDavid Ahern 	unsigned int		flags;
2734724676dSDavid Ahern 	struct net_device	*dev;
2744724676dSDavid Ahern };
2754724676dSDavid Ahern 
2765c3a0fd7SJoe Perches int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
277ebc0ffaeSEric Dumazet 		     struct fib_result *res, int fib_flags);
2786d8422a1SDavid Ahern int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
2796d8422a1SDavid Ahern 		     struct netlink_ext_ack *extack);
28078055998SDavid Ahern int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
28178055998SDavid Ahern 		     struct netlink_ext_ack *extack);
2825c3a0fd7SJoe Perches int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
28318a8021aSDavid Ahern 		   struct netlink_callback *cb, struct fib_dump_filter *filter);
284f97f4dd8SIdo Schimmel int fib_table_flush(struct net *net, struct fib_table *table, bool flush_all);
2850ddcf43dSAlexander Duyck struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
2863b709334SAlexander Duyck void fib_table_flush_external(struct fib_table *table);
2875c3a0fd7SJoe Perches void fib_free_table(struct fib_table *tb);
2884aa2c466SPavel Emelyanov 
2891da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTIPLE_TABLES
2901da177e4SLinus Torvalds 
291a5a519b2SAlexander Duyck #define TABLE_LOCAL_INDEX	(RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
292a5a519b2SAlexander Duyck #define TABLE_MAIN_INDEX	(RT_TABLE_MAIN  & (FIB_TABLE_HASHSZ - 1))
2931da177e4SLinus Torvalds 
2948ad4942cSDenis V. Lunev static inline struct fib_table *fib_get_table(struct net *net, u32 id)
2951da177e4SLinus Torvalds {
296a7e53531SAlexander Duyck 	struct hlist_node *tb_hlist;
29793456b6dSDenis V. Lunev 	struct hlist_head *ptr;
29893456b6dSDenis V. Lunev 
29993456b6dSDenis V. Lunev 	ptr = id == RT_TABLE_LOCAL ?
300e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
301e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
302a7e53531SAlexander Duyck 
303a7e53531SAlexander Duyck 	tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
304a7e53531SAlexander Duyck 
305a7e53531SAlexander Duyck 	return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3088ad4942cSDenis V. Lunev static inline struct fib_table *fib_new_table(struct net *net, u32 id)
3091da177e4SLinus Torvalds {
3108ad4942cSDenis V. Lunev 	return fib_get_table(net, id);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
31322bd5b9bSDavid S. Miller static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
3140eeb075fSAndy Gospodarek 			     struct fib_result *res, unsigned int flags)
3151da177e4SLinus Torvalds {
316a7e53531SAlexander Duyck 	struct fib_table *tb;
3170ddcf43dSAlexander Duyck 	int err = -ENETUNREACH;
31893456b6dSDenis V. Lunev 
319345e9b54SAlexander Duyck 	rcu_read_lock();
32093456b6dSDenis V. Lunev 
321a7e53531SAlexander Duyck 	tb = fib_get_table(net, RT_TABLE_MAIN);
3220315e382SNikola Forró 	if (tb)
3230315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
3240315e382SNikola Forró 
3250315e382SNikola Forró 	if (err == -EAGAIN)
3260315e382SNikola Forró 		err = -ENETUNREACH;
327345e9b54SAlexander Duyck 
328345e9b54SAlexander Duyck 	rcu_read_unlock();
329345e9b54SAlexander Duyck 
330345e9b54SAlexander Duyck 	return err;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
333c43c3d76SPaolo Abeni static inline bool fib4_has_custom_rules(const struct net *net)
334c43c3d76SPaolo Abeni {
335c43c3d76SPaolo Abeni 	return false;
336c43c3d76SPaolo Abeni }
337c43c3d76SPaolo Abeni 
3383c71006dSIdo Schimmel static inline bool fib4_rule_default(const struct fib_rule *rule)
3393c71006dSIdo Schimmel {
3403c71006dSIdo Schimmel 	return true;
3413c71006dSIdo Schimmel }
3423c71006dSIdo Schimmel 
343b7a59557SJiri Pirko static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb,
344b7a59557SJiri Pirko 				  struct netlink_ext_ack *extack)
3451b2a4440SIdo Schimmel {
3461b2a4440SIdo Schimmel 	return 0;
3471b2a4440SIdo Schimmel }
3481b2a4440SIdo Schimmel 
3491b2a4440SIdo Schimmel static inline unsigned int fib4_rules_seq_read(struct net *net)
3501b2a4440SIdo Schimmel {
3511b2a4440SIdo Schimmel 	return 0;
3521b2a4440SIdo Schimmel }
3531b2a4440SIdo Schimmel 
354e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
355e37b1e97SRoopa Prabhu 						 struct sk_buff *skb,
356e37b1e97SRoopa Prabhu 						 struct flowi4 *fl4,
357e37b1e97SRoopa Prabhu 						 struct flow_keys *flkeys)
358e37b1e97SRoopa Prabhu {
359e37b1e97SRoopa Prabhu 	return false;
360e37b1e97SRoopa Prabhu }
3611da177e4SLinus Torvalds #else /* CONFIG_IP_MULTIPLE_TABLES */
3625c3a0fd7SJoe Perches int __net_init fib4_rules_init(struct net *net);
3635c3a0fd7SJoe Perches void __net_exit fib4_rules_exit(struct net *net);
364c3e9a353SPavel Emelyanov 
3655c3a0fd7SJoe Perches struct fib_table *fib_new_table(struct net *net, u32 id);
3665c3a0fd7SJoe Perches struct fib_table *fib_get_table(struct net *net, u32 id);
3671da177e4SLinus Torvalds 
3680eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp,
3690eeb075fSAndy Gospodarek 		 struct fib_result *res, unsigned int flags);
370f4530fa5SDavid S. Miller 
371f4530fa5SDavid S. Miller static inline int fib_lookup(struct net *net, struct flowi4 *flp,
3720eeb075fSAndy Gospodarek 			     struct fib_result *res, unsigned int flags)
373f4530fa5SDavid S. Miller {
374a7e53531SAlexander Duyck 	struct fib_table *tb;
3750315e382SNikola Forró 	int err = -ENETUNREACH;
376a7e53531SAlexander Duyck 
3770eeb075fSAndy Gospodarek 	flags |= FIB_LOOKUP_NOREF;
378a7e53531SAlexander Duyck 	if (net->ipv4.fib_has_custom_rules)
3790eeb075fSAndy Gospodarek 		return __fib_lookup(net, flp, res, flags);
380345e9b54SAlexander Duyck 
381345e9b54SAlexander Duyck 	rcu_read_lock();
382345e9b54SAlexander Duyck 
38385b91b03SDavid S. Miller 	res->tclassid = 0;
384a7e53531SAlexander Duyck 
385a7e53531SAlexander Duyck 	tb = rcu_dereference_rtnl(net->ipv4.fib_main);
3860315e382SNikola Forró 	if (tb)
3870315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags);
3880315e382SNikola Forró 
3890315e382SNikola Forró 	if (!err)
3900315e382SNikola Forró 		goto out;
391a7e53531SAlexander Duyck 
392a7e53531SAlexander Duyck 	tb = rcu_dereference_rtnl(net->ipv4.fib_default);
3930315e382SNikola Forró 	if (tb)
3940315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags);
3950315e382SNikola Forró 
3960315e382SNikola Forró out:
3970315e382SNikola Forró 	if (err == -EAGAIN)
3980315e382SNikola Forró 		err = -ENETUNREACH;
399345e9b54SAlexander Duyck 
400345e9b54SAlexander Duyck 	rcu_read_unlock();
401345e9b54SAlexander Duyck 
402345e9b54SAlexander Duyck 	return err;
403f4530fa5SDavid S. Miller }
404f4530fa5SDavid S. Miller 
405c43c3d76SPaolo Abeni static inline bool fib4_has_custom_rules(const struct net *net)
406c43c3d76SPaolo Abeni {
407c43c3d76SPaolo Abeni 	return net->ipv4.fib_has_custom_rules;
408c43c3d76SPaolo Abeni }
409c43c3d76SPaolo Abeni 
4103c71006dSIdo Schimmel bool fib4_rule_default(const struct fib_rule *rule);
411b7a59557SJiri Pirko int fib4_rules_dump(struct net *net, struct notifier_block *nb,
412b7a59557SJiri Pirko 		    struct netlink_ext_ack *extack);
4131b2a4440SIdo Schimmel unsigned int fib4_rules_seq_read(struct net *net);
4143c71006dSIdo Schimmel 
415e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
416e37b1e97SRoopa Prabhu 						 struct sk_buff *skb,
417e37b1e97SRoopa Prabhu 						 struct flowi4 *fl4,
418e37b1e97SRoopa Prabhu 						 struct flow_keys *flkeys)
419e37b1e97SRoopa Prabhu {
420e37b1e97SRoopa Prabhu 	unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
421e37b1e97SRoopa Prabhu 
422e37b1e97SRoopa Prabhu 	if (!net->ipv4.fib_rules_require_fldissect)
423e37b1e97SRoopa Prabhu 		return false;
424e37b1e97SRoopa Prabhu 
4258aae7625SFlorian Westphal 	memset(flkeys, 0, sizeof(*flkeys));
4268aae7625SFlorian Westphal 	__skb_flow_dissect(net, skb, &flow_keys_dissector,
4278aae7625SFlorian Westphal 			   flkeys, NULL, 0, 0, 0, flag);
4288aae7625SFlorian Westphal 
429e37b1e97SRoopa Prabhu 	fl4->fl4_sport = flkeys->ports.src;
430e37b1e97SRoopa Prabhu 	fl4->fl4_dport = flkeys->ports.dst;
431e37b1e97SRoopa Prabhu 	fl4->flowi4_proto = flkeys->basic.ip_proto;
432e37b1e97SRoopa Prabhu 
433e37b1e97SRoopa Prabhu 	return true;
434e37b1e97SRoopa Prabhu }
435e37b1e97SRoopa Prabhu 
4361da177e4SLinus Torvalds #endif /* CONFIG_IP_MULTIPLE_TABLES */
4371da177e4SLinus Torvalds 
438*1fa3314cSIdo Schimmel static inline bool fib_dscp_masked_match(dscp_t dscp, const struct flowi4 *fl4)
439*1fa3314cSIdo Schimmel {
440*1fa3314cSIdo Schimmel 	return dscp == inet_dsfield_to_dscp(RT_TOS(fl4->flowi4_tos));
441*1fa3314cSIdo Schimmel }
442*1fa3314cSIdo Schimmel 
4431da177e4SLinus Torvalds /* Exported by fib_frontend.c */
444ef7c79edSPatrick McHardy extern const struct nla_policy rtm_ipv4_policy[];
4455c3a0fd7SJoe Perches void ip_fib_init(void);
446d1566268SDavid Ahern int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
447d1566268SDavid Ahern 		    struct netlink_ext_ack *extack);
4485c3a0fd7SJoe Perches __be32 fib_compute_spec_dst(struct sk_buff *skb);
44978f2756cSDavid Ahern bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
4505c3a0fd7SJoe Perches int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
4515c04c819SMichael Smith 			u8 tos, int oif, struct net_device *dev,
4529e56e380SDavid S. Miller 			struct in_device *idev, u32 *itag);
4537a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
454f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
455f4530fa5SDavid S. Miller {
456213f5f8fSEric Dumazet 	return atomic_read(&net->ipv4.fib_num_tclassid_users);
457f4530fa5SDavid S. Miller }
4587a9bc9b8SDavid S. Miller #else
459f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
460f4530fa5SDavid S. Miller {
461f4530fa5SDavid S. Miller 	return 0;
462f4530fa5SDavid S. Miller }
4637a9bc9b8SDavid S. Miller #endif
4640ddcf43dSAlexander Duyck int fib_unmerge(struct net *net);
46514c85021SArnaldo Carvalho de Melo 
4661fd1c768SDavid Ahern static inline bool nhc_l3mdev_matches_dev(const struct fib_nh_common *nhc,
4671fd1c768SDavid Ahern const struct net_device *dev)
4681fd1c768SDavid Ahern {
4691fd1c768SDavid Ahern 	if (nhc->nhc_dev == dev ||
4701fd1c768SDavid Ahern 	    l3mdev_master_ifindex_rcu(nhc->nhc_dev) == dev->ifindex)
4711fd1c768SDavid Ahern 		return true;
4721fd1c768SDavid Ahern 
4731fd1c768SDavid Ahern 	return false;
4741fd1c768SDavid Ahern }
4751fd1c768SDavid Ahern 
4761da177e4SLinus Torvalds /* Exported by fib_semantics.c */
4775c3a0fd7SJoe Perches int ip_fib_check_default(__be32 gw, struct net_device *dev);
4784f823defSJulian Anastasov int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
4795a56a0b3SMark Tomlinson int fib_sync_down_addr(struct net_device *dev, __be32 local);
480ecc5663cSDavid Ahern int fib_sync_up(struct net_device *dev, unsigned char nh_flags);
481af7d6cceSSabrina Dubroca void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
48206c77c3eSDavid Ahern void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
4830e884c78SPeter Nørlund 
484ce5c9c20SIdo Schimmel /* Fields used for sysctl_fib_multipath_hash_fields.
485ce5c9c20SIdo Schimmel  * Common to IPv4 and IPv6.
486ce5c9c20SIdo Schimmel  *
487ce5c9c20SIdo Schimmel  * Add new fields at the end. This is user API.
488ce5c9c20SIdo Schimmel  */
489ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_SRC_IP			BIT(0)
490ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DST_IP			BIT(1)
491ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_IP_PROTO		BIT(2)
492ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_FLOWLABEL		BIT(3)
493ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_SRC_PORT		BIT(4)
494ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DST_PORT		BIT(5)
495ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP		BIT(6)
496ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP		BIT(7)
497ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO		BIT(8)
498ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL	BIT(9)
499ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT		BIT(10)
500ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT		BIT(11)
501ce5c9c20SIdo Schimmel 
502ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_OUTER_MASK		\
503ce5c9c20SIdo Schimmel 	(FIB_MULTIPATH_HASH_FIELD_SRC_IP |		\
504ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_DST_IP |		\
505ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_IP_PROTO |		\
506ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_FLOWLABEL |		\
507ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_SRC_PORT |		\
508ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_DST_PORT)
509ce5c9c20SIdo Schimmel 
510ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_MASK		\
511ce5c9c20SIdo Schimmel 	(FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP |	\
512ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP |	\
513ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO |	\
514ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL |	\
515ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT |	\
516ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
517ce5c9c20SIdo Schimmel 
518ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_ALL_MASK		\
519ce5c9c20SIdo Schimmel 	(FIB_MULTIPATH_HASH_FIELD_OUTER_MASK |		\
520ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_MASK)
521ce5c9c20SIdo Schimmel 
522ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK		\
523ce5c9c20SIdo Schimmel 	(FIB_MULTIPATH_HASH_FIELD_SRC_IP |		\
524ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_DST_IP |		\
525ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
526ce5c9c20SIdo Schimmel 
527bf4e0a3dSNikolay Aleksandrov #ifdef CONFIG_IP_ROUTE_MULTIPATH
5287efc0b6bSDavid Ahern int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
529e37b1e97SRoopa Prabhu 		       const struct sk_buff *skb, struct flow_keys *flkeys);
5303e453ca1SPetr Machata 
5314ee2a8caSPetr Machata static void
5324ee2a8caSPetr Machata fib_multipath_hash_construct_key(siphash_key_t *key, u32 mp_seed)
5334ee2a8caSPetr Machata {
5344ee2a8caSPetr Machata 	u64 mp_seed_64 = mp_seed;
5354ee2a8caSPetr Machata 
5364ee2a8caSPetr Machata 	key->key[0] = (mp_seed_64 << 32) | mp_seed_64;
5374ee2a8caSPetr Machata 	key->key[1] = key->key[0];
5384ee2a8caSPetr Machata }
5394ee2a8caSPetr Machata 
5404ee2a8caSPetr Machata static inline u32 fib_multipath_hash_from_keys(const struct net *net,
5414ee2a8caSPetr Machata 					       struct flow_keys *keys)
5424ee2a8caSPetr Machata {
5434ee2a8caSPetr Machata 	siphash_aligned_key_t hash_key;
5444ee2a8caSPetr Machata 	u32 mp_seed;
5454ee2a8caSPetr Machata 
5464ee2a8caSPetr Machata 	mp_seed = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_seed).mp_seed;
5474ee2a8caSPetr Machata 	fib_multipath_hash_construct_key(&hash_key, mp_seed);
5484ee2a8caSPetr Machata 
5494ee2a8caSPetr Machata 	return flow_hash_from_keys_seed(keys, &hash_key);
5504ee2a8caSPetr Machata }
5514ee2a8caSPetr Machata #else
5523e453ca1SPetr Machata static inline u32 fib_multipath_hash_from_keys(const struct net *net,
5533e453ca1SPetr Machata 					       struct flow_keys *keys)
5543e453ca1SPetr Machata {
5553e453ca1SPetr Machata 	return flow_hash_from_keys(keys);
5563e453ca1SPetr Machata }
5574ee2a8caSPetr Machata #endif
5583e453ca1SPetr Machata 
559ac1fab2dSDavid Ahern int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
560ac1fab2dSDavid Ahern 		 struct netlink_ext_ack *extack);
5610e884c78SPeter Nørlund void fib_select_multipath(struct fib_result *res, int hash);
5623ce58d84SDavid Ahern void fib_select_path(struct net *net, struct fib_result *res,
563bf4e0a3dSNikolay Aleksandrov 		     struct flowi4 *fl4, const struct sk_buff *skb);
5641da177e4SLinus Torvalds 
565e4516ef6SDavid Ahern int fib_nh_init(struct net *net, struct fib_nh *fib_nh,
566e4516ef6SDavid Ahern 		struct fib_config *cfg, int nh_weight,
567e4516ef6SDavid Ahern 		struct netlink_ext_ack *extack);
568faa041a4SDavid Ahern void fib_nh_release(struct net *net, struct fib_nh *fib_nh);
569faee6769SAlexander Aring int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
570faee6769SAlexander Aring 		       struct nlattr *fc_encap, u16 fc_encap_type,
571faee6769SAlexander Aring 		       void *cfg, gfp_t gfp_flags,
572979e276eSDavid Ahern 		       struct netlink_ext_ack *extack);
573979e276eSDavid Ahern void fib_nh_common_release(struct fib_nh_common *nhc);
574e4516ef6SDavid Ahern 
5755348ba85SDavid S. Miller /* Exported by fib_trie.c */
57690b93f1bSIdo Schimmel void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri);
5775c3a0fd7SJoe Perches void fib_trie_init(void);
5780ddcf43dSAlexander Duyck struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
579af7888adSDavid Ahern bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
580af7888adSDavid Ahern 			 const struct flowi4 *flp);
5811da177e4SLinus Torvalds 
582b6bf3ca0SDavid S. Miller static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
5831da177e4SLinus Torvalds {
584c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
585eba618abSDavid Ahern 	struct fib_nh_common *nhc = res->nhc;
5861da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
5871da177e4SLinus Torvalds 	u32 rtag;
5881da177e4SLinus Torvalds #endif
589dcb1ecb5SDavid Ahern 	if (nhc->nhc_family == AF_INET) {
590dcb1ecb5SDavid Ahern 		struct fib_nh *nh;
591dcb1ecb5SDavid Ahern 
592dcb1ecb5SDavid Ahern 		nh = container_of(nhc, struct fib_nh, nh_common);
593eba618abSDavid Ahern 		*itag = nh->nh_tclassid << 16;
594dcb1ecb5SDavid Ahern 	} else {
595dcb1ecb5SDavid Ahern 		*itag = 0;
596dcb1ecb5SDavid Ahern 	}
597dcb1ecb5SDavid Ahern 
5981da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
59985b91b03SDavid S. Miller 	rtag = res->tclassid;
6001da177e4SLinus Torvalds 	if (*itag == 0)
6011da177e4SLinus Torvalds 		*itag = (rtag<<16);
6021da177e4SLinus Torvalds 	*itag |= (rtag>>16);
6031da177e4SLinus Torvalds #endif
6041da177e4SLinus Torvalds #endif
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6079bd83667SDavid Ahern void fib_flush(struct net *net);
6085c3a0fd7SJoe Perches void free_fib_info(struct fib_info *fi);
6091da177e4SLinus Torvalds 
6101c677b3dSIdo Schimmel static inline void fib_info_hold(struct fib_info *fi)
6111c677b3dSIdo Schimmel {
6120029c0deSReshetova, Elena 	refcount_inc(&fi->fib_clntref);
6131c677b3dSIdo Schimmel }
6141c677b3dSIdo Schimmel 
6151da177e4SLinus Torvalds static inline void fib_info_put(struct fib_info *fi)
6161da177e4SLinus Torvalds {
6170029c0deSReshetova, Elena 	if (refcount_dec_and_test(&fi->fib_clntref))
6181da177e4SLinus Torvalds 		free_fib_info(fi);
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
62120380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
6225c3a0fd7SJoe Perches int __net_init fib_proc_init(struct net *net);
6235c3a0fd7SJoe Perches void __net_exit fib_proc_exit(struct net *net);
624cc8274f5SLi Zefan #else
625cc8274f5SLi Zefan static inline int fib_proc_init(struct net *net)
626cc8274f5SLi Zefan {
627cc8274f5SLi Zefan 	return 0;
628cc8274f5SLi Zefan }
629cc8274f5SLi Zefan static inline void fib_proc_exit(struct net *net)
630cc8274f5SLi Zefan {
631cc8274f5SLi Zefan }
63220380731SArnaldo Carvalho de Melo #endif
63320380731SArnaldo Carvalho de Melo 
63450d889b1SDavid Ahern u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr);
63550d889b1SDavid Ahern 
6364724676dSDavid Ahern int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
6374724676dSDavid Ahern 			  struct fib_dump_filter *filter,
638effe6792SDavid Ahern 			  struct netlink_callback *cb);
639c0a72077SDavid Ahern 
640c0a72077SDavid Ahern int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nh,
6417bdf4de1SDonald Sharp 		     u8 rt_family, unsigned char *flags, bool skip_oif);
642c0a72077SDavid Ahern int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nh,
643597aa16cSXiao Liang 		    int nh_weight, u8 rt_family, u32 nh_tclassid);
6441da177e4SLinus Torvalds #endif  /* _NET_FIB_H */
645