xref: /linux-6.15/include/net/ip_fib.h (revision 597aa16c)
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>
208e773277SDavid S. Miller #include <net/inetpeer.h>
21d26b3a7cSEric Dumazet #include <linux/percpu.h>
22b90eb754SJiri Pirko #include <linux/notifier.h>
230029c0deSReshetova, Elena #include <linux/refcount.h>
241da177e4SLinus Torvalds 
254e902c57SThomas Graf struct fib_config {
264e902c57SThomas Graf 	u8			fc_dst_len;
274e902c57SThomas Graf 	u8			fc_tos;
284e902c57SThomas Graf 	u8			fc_protocol;
294e902c57SThomas Graf 	u8			fc_scope;
304e902c57SThomas Graf 	u8			fc_type;
31f35b794bSDavid Ahern 	u8			fc_gw_family;
32f35b794bSDavid Ahern 	/* 2 bytes unused */
334e902c57SThomas Graf 	u32			fc_table;
346d85c10aSAl Viro 	__be32			fc_dst;
35a4ea5d43SDavid Ahern 	union {
36f35b794bSDavid Ahern 		__be32		fc_gw4;
37a4ea5d43SDavid Ahern 		struct in6_addr	fc_gw6;
38a4ea5d43SDavid Ahern 	};
394e902c57SThomas Graf 	int			fc_oif;
404e902c57SThomas Graf 	u32			fc_flags;
414e902c57SThomas Graf 	u32			fc_priority;
426d85c10aSAl Viro 	__be32			fc_prefsrc;
43493ced1aSDavid Ahern 	u32			fc_nh_id;
444e902c57SThomas Graf 	struct nlattr		*fc_mx;
454e902c57SThomas Graf 	struct rtnexthop	*fc_mp;
464e902c57SThomas Graf 	int			fc_mx_len;
474e902c57SThomas Graf 	int			fc_mp_len;
484e902c57SThomas Graf 	u32			fc_flow;
494e902c57SThomas Graf 	u32			fc_nlflags;
504e902c57SThomas Graf 	struct nl_info		fc_nlinfo;
51571e7226SRoopa Prabhu 	struct nlattr		*fc_encap;
52571e7226SRoopa Prabhu 	u16			fc_encap_type;
531da177e4SLinus Torvalds };
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds struct fib_info;
56f2bb4bedSDavid S. Miller struct rtable;
571da177e4SLinus Torvalds 
584895c771SDavid S. Miller struct fib_nh_exception {
594895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*fnhe_next;
605aad1de5STimo Teräs 	int				fnhe_genid;
614895c771SDavid S. Miller 	__be32				fnhe_daddr;
624895c771SDavid S. Miller 	u32				fnhe_pmtu;
63d52e5a7eSSabrina Dubroca 	bool				fnhe_mtu_locked;
64aee06da6SJulian Anastasov 	__be32				fnhe_gw;
654895c771SDavid S. Miller 	unsigned long			fnhe_expires;
662ffae99dSTimo Teräs 	struct rtable __rcu		*fnhe_rth_input;
672ffae99dSTimo Teräs 	struct rtable __rcu		*fnhe_rth_output;
684895c771SDavid S. Miller 	unsigned long			fnhe_stamp;
69deed49dfSXin Long 	struct rcu_head			rcu;
704895c771SDavid S. Miller };
714895c771SDavid S. Miller 
724895c771SDavid S. Miller struct fnhe_hash_bucket {
734895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*chain;
744895c771SDavid S. Miller };
754895c771SDavid S. Miller 
76d546c621SEric Dumazet #define FNHE_HASH_SHIFT		11
77d546c621SEric Dumazet #define FNHE_HASH_SIZE		(1 << FNHE_HASH_SHIFT)
784895c771SDavid S. Miller #define FNHE_RECLAIM_DEPTH	5
794895c771SDavid S. Miller 
80f1741730SDavid Ahern struct fib_nh_common {
81f1741730SDavid Ahern 	struct net_device	*nhc_dev;
82f1741730SDavid Ahern 	int			nhc_oif;
83f1741730SDavid Ahern 	unsigned char		nhc_scope;
84f1741730SDavid Ahern 	u8			nhc_family;
85bdf00467SDavid Ahern 	u8			nhc_gw_family;
86ecc5663cSDavid Ahern 	unsigned char		nhc_flags;
87ecc5663cSDavid Ahern 	struct lwtunnel_state	*nhc_lwtstate;
88bdf00467SDavid Ahern 
89f1741730SDavid Ahern 	union {
90f1741730SDavid Ahern 		__be32          ipv4;
91f1741730SDavid Ahern 		struct in6_addr ipv6;
92f1741730SDavid Ahern 	} nhc_gw;
93f1741730SDavid Ahern 
94f1741730SDavid Ahern 	int			nhc_weight;
95f1741730SDavid Ahern 	atomic_t		nhc_upper_bound;
960f457a36SDavid Ahern 
970f457a36SDavid Ahern 	/* v4 specific, but allows fib6_nh with v4 routes */
980f457a36SDavid Ahern 	struct rtable __rcu * __percpu *nhc_pcpu_rth_output;
990f457a36SDavid Ahern 	struct rtable __rcu     *nhc_rth_input;
100a5995e71SDavid Ahern 	struct fnhe_hash_bucket	__rcu *nhc_exceptions;
101f1741730SDavid Ahern };
102f1741730SDavid Ahern 
1031da177e4SLinus Torvalds struct fib_nh {
104f1741730SDavid Ahern 	struct fib_nh_common	nh_common;
1051da177e4SLinus Torvalds 	struct hlist_node	nh_hash;
1061da177e4SLinus Torvalds 	struct fib_info		*nh_parent;
107c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1081da177e4SLinus Torvalds 	__u32			nh_tclassid;
1091da177e4SLinus Torvalds #endif
1101fc050a1SDavid S. Miller 	__be32			nh_saddr;
111436c3b66SDavid S. Miller 	int			nh_saddr_genid;
112f1741730SDavid Ahern #define fib_nh_family		nh_common.nhc_family
113f1741730SDavid Ahern #define fib_nh_dev		nh_common.nhc_dev
114f1741730SDavid Ahern #define fib_nh_oif		nh_common.nhc_oif
115f1741730SDavid Ahern #define fib_nh_flags		nh_common.nhc_flags
116f1741730SDavid Ahern #define fib_nh_lws		nh_common.nhc_lwtstate
117f1741730SDavid Ahern #define fib_nh_scope		nh_common.nhc_scope
118bdf00467SDavid Ahern #define fib_nh_gw_family	nh_common.nhc_gw_family
119f1741730SDavid Ahern #define fib_nh_gw4		nh_common.nhc_gw.ipv4
120f1741730SDavid Ahern #define fib_nh_gw6		nh_common.nhc_gw.ipv6
121f1741730SDavid Ahern #define fib_nh_weight		nh_common.nhc_weight
122f1741730SDavid Ahern #define fib_nh_upper_bound	nh_common.nhc_upper_bound
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds  * This structure contains data shared by many of routes.
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds 
1294c7e8084SDavid Ahern struct nexthop;
1304c7e8084SDavid Ahern 
1311da177e4SLinus Torvalds struct fib_info {
1321da177e4SLinus Torvalds 	struct hlist_node	fib_hash;
1331da177e4SLinus Torvalds 	struct hlist_node	fib_lhash;
1344c7e8084SDavid Ahern 	struct list_head	nh_list;
1357462bd74SDenis V. Lunev 	struct net		*fib_net;
13679976892SYajun Deng 	refcount_t		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;
1544c7e8084SDavid Ahern 	struct nexthop		*nh;
155ebc0ffaeSEric Dumazet 	struct rcu_head		rcu;
156a5311060SGustavo A. R. Silva 	struct fib_nh		fib_nh[];
1571da177e4SLinus Torvalds };
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
1611da177e4SLinus Torvalds struct fib_rule;
1621da177e4SLinus Torvalds #endif
1631da177e4SLinus Torvalds 
1645b470441SDavid S. Miller struct fib_table;
1651da177e4SLinus Torvalds struct fib_result {
1666ffd9034SDavid Ahern 	__be32			prefix;
1671da177e4SLinus Torvalds 	unsigned char		prefixlen;
1681da177e4SLinus Torvalds 	unsigned char		nh_sel;
1691da177e4SLinus Torvalds 	unsigned char		type;
1701da177e4SLinus Torvalds 	unsigned char		scope;
17185b91b03SDavid S. Miller 	u32			tclassid;
172eba618abSDavid Ahern 	struct fib_nh_common	*nhc;
1731da177e4SLinus Torvalds 	struct fib_info		*fi;
1745b470441SDavid S. Miller 	struct fib_table	*table;
17556315f9eSAlexander Duyck 	struct hlist_head	*fa_head;
1761da177e4SLinus Torvalds };
1771da177e4SLinus Torvalds 
178246955feSRobert Olsson struct fib_result_nl {
17980e856e1SAl Viro 	__be32		fl_addr;   /* To be looked up*/
1805f300893SThomas Graf 	u32		fl_mark;
181246955feSRobert Olsson 	unsigned char	fl_tos;
182246955feSRobert Olsson 	unsigned char   fl_scope;
183246955feSRobert Olsson 	unsigned char   tb_id_in;
184246955feSRobert Olsson 
185246955feSRobert Olsson 	unsigned char   tb_id;      /* Results */
186246955feSRobert Olsson 	unsigned char	prefixlen;
187246955feSRobert Olsson 	unsigned char	nh_sel;
188246955feSRobert Olsson 	unsigned char	type;
189246955feSRobert Olsson 	unsigned char	scope;
190246955feSRobert Olsson 	int             err;
191246955feSRobert Olsson };
1921da177e4SLinus Torvalds 
1935b9e12dbSDenis V. Lunev #ifdef CONFIG_IP_MULTIPLE_TABLES
1945b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 256
1955b9e12dbSDenis V. Lunev #else
1965b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 2
1975b9e12dbSDenis V. Lunev #endif
1985b9e12dbSDenis V. Lunev 
199dcb1ecb5SDavid Ahern __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
200c3669486SDavid Ahern 				 unsigned char scope);
201eba618abSDavid Ahern __be32 fib_result_prefsrc(struct net *net, struct fib_result *res);
202436c3b66SDavid S. Miller 
203eba618abSDavid Ahern #define FIB_RES_NHC(res)		((res).nhc)
204eba618abSDavid Ahern #define FIB_RES_DEV(res)	(FIB_RES_NHC(res)->nhc_dev)
205eba618abSDavid Ahern #define FIB_RES_OIF(res)	(FIB_RES_NHC(res)->nhc_oif)
2061fc050a1SDavid S. Miller 
2071e301fd0SIdo Schimmel struct fib_rt_info {
2081e301fd0SIdo Schimmel 	struct fib_info		*fi;
2091e301fd0SIdo Schimmel 	u32			tb_id;
2101e301fd0SIdo Schimmel 	__be32			dst;
2111e301fd0SIdo Schimmel 	int			dst_len;
2121e301fd0SIdo Schimmel 	u8			tos;
2131e301fd0SIdo Schimmel 	u8			type;
21490b93f1bSIdo Schimmel 	u8			offload:1,
21590b93f1bSIdo Schimmel 				trap:1,
21636c5100eSAmit Cohen 				offload_failed:1,
21736c5100eSAmit Cohen 				unused:5;
2181e301fd0SIdo Schimmel };
2191e301fd0SIdo Schimmel 
220b90eb754SJiri Pirko struct fib_entry_notifier_info {
221b90eb754SJiri Pirko 	struct fib_notifier_info info; /* must be first */
222b90eb754SJiri Pirko 	u32 dst;
223b90eb754SJiri Pirko 	int dst_len;
224b90eb754SJiri Pirko 	struct fib_info *fi;
225b90eb754SJiri Pirko 	u8 tos;
226b90eb754SJiri Pirko 	u8 type;
227b90eb754SJiri Pirko 	u32 tb_id;
228b90eb754SJiri Pirko };
229b90eb754SJiri Pirko 
230982acb97SIdo Schimmel struct fib_nh_notifier_info {
231982acb97SIdo Schimmel 	struct fib_notifier_info info; /* must be first */
232982acb97SIdo Schimmel 	struct fib_nh *fib_nh;
233982acb97SIdo Schimmel };
234982acb97SIdo Schimmel 
2357c550dafSJiri Pirko int call_fib4_notifier(struct notifier_block *nb,
236c0243892SIdo Schimmel 		       enum fib_event_type event_type,
237c0243892SIdo Schimmel 		       struct fib_notifier_info *info);
23804b1d4e5SIdo Schimmel int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
239b90eb754SJiri Pirko 			struct fib_notifier_info *info);
240b90eb754SJiri Pirko 
24104b1d4e5SIdo Schimmel int __net_init fib4_notifier_init(struct net *net);
24204b1d4e5SIdo Schimmel void __net_exit fib4_notifier_exit(struct net *net);
24304b1d4e5SIdo Schimmel 
2441bff1a0cSDavid Ahern void fib_info_notify_update(struct net *net, struct nl_info *info);
245b7a59557SJiri Pirko int fib_notify(struct net *net, struct notifier_block *nb,
246b7a59557SJiri Pirko 	       struct netlink_ext_ack *extack);
247c0243892SIdo Schimmel 
2481da177e4SLinus Torvalds struct fib_table {
2491af5a8c4SPatrick McHardy 	struct hlist_node	tb_hlist;
2502dfe55b4SPatrick McHardy 	u32			tb_id;
25121d8c49eSDavid S. Miller 	int			tb_num_default;
252a7e53531SAlexander Duyck 	struct rcu_head		rcu;
2530ddcf43dSAlexander Duyck 	unsigned long 		*tb_data;
254a5311060SGustavo A. R. Silva 	unsigned long		__data[];
2551da177e4SLinus Torvalds };
2561da177e4SLinus Torvalds 
2574724676dSDavid Ahern struct fib_dump_filter {
2584724676dSDavid Ahern 	u32			table_id;
2594724676dSDavid Ahern 	/* filter_set is an optimization that an entry is set */
2604724676dSDavid Ahern 	bool			filter_set;
261564c91f7SStefano Brivio 	bool			dump_routes;
262564c91f7SStefano Brivio 	bool			dump_exceptions;
2634724676dSDavid Ahern 	unsigned char		protocol;
2644724676dSDavid Ahern 	unsigned char		rt_type;
2654724676dSDavid Ahern 	unsigned int		flags;
2664724676dSDavid Ahern 	struct net_device	*dev;
2674724676dSDavid Ahern };
2684724676dSDavid Ahern 
2695c3a0fd7SJoe Perches int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
270ebc0ffaeSEric Dumazet 		     struct fib_result *res, int fib_flags);
2716d8422a1SDavid Ahern int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
2726d8422a1SDavid Ahern 		     struct netlink_ext_ack *extack);
27378055998SDavid Ahern int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
27478055998SDavid Ahern 		     struct netlink_ext_ack *extack);
2755c3a0fd7SJoe Perches int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
27618a8021aSDavid Ahern 		   struct netlink_callback *cb, struct fib_dump_filter *filter);
277f97f4dd8SIdo Schimmel int fib_table_flush(struct net *net, struct fib_table *table, bool flush_all);
2780ddcf43dSAlexander Duyck struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
2793b709334SAlexander Duyck void fib_table_flush_external(struct fib_table *table);
2805c3a0fd7SJoe Perches void fib_free_table(struct fib_table *tb);
2814aa2c466SPavel Emelyanov 
2821da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTIPLE_TABLES
2831da177e4SLinus Torvalds 
284a5a519b2SAlexander Duyck #define TABLE_LOCAL_INDEX	(RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
285a5a519b2SAlexander Duyck #define TABLE_MAIN_INDEX	(RT_TABLE_MAIN  & (FIB_TABLE_HASHSZ - 1))
2861da177e4SLinus Torvalds 
2878ad4942cSDenis V. Lunev static inline struct fib_table *fib_get_table(struct net *net, u32 id)
2881da177e4SLinus Torvalds {
289a7e53531SAlexander Duyck 	struct hlist_node *tb_hlist;
29093456b6dSDenis V. Lunev 	struct hlist_head *ptr;
29193456b6dSDenis V. Lunev 
29293456b6dSDenis V. Lunev 	ptr = id == RT_TABLE_LOCAL ?
293e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
294e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
295a7e53531SAlexander Duyck 
296a7e53531SAlexander Duyck 	tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
297a7e53531SAlexander Duyck 
298a7e53531SAlexander Duyck 	return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3018ad4942cSDenis V. Lunev static inline struct fib_table *fib_new_table(struct net *net, u32 id)
3021da177e4SLinus Torvalds {
3038ad4942cSDenis V. Lunev 	return fib_get_table(net, id);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
30622bd5b9bSDavid S. Miller static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
3070eeb075fSAndy Gospodarek 			     struct fib_result *res, unsigned int flags)
3081da177e4SLinus Torvalds {
309a7e53531SAlexander Duyck 	struct fib_table *tb;
3100ddcf43dSAlexander Duyck 	int err = -ENETUNREACH;
31193456b6dSDenis V. Lunev 
312345e9b54SAlexander Duyck 	rcu_read_lock();
31393456b6dSDenis V. Lunev 
314a7e53531SAlexander Duyck 	tb = fib_get_table(net, RT_TABLE_MAIN);
3150315e382SNikola Forró 	if (tb)
3160315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
3170315e382SNikola Forró 
3180315e382SNikola Forró 	if (err == -EAGAIN)
3190315e382SNikola Forró 		err = -ENETUNREACH;
320345e9b54SAlexander Duyck 
321345e9b54SAlexander Duyck 	rcu_read_unlock();
322345e9b54SAlexander Duyck 
323345e9b54SAlexander Duyck 	return err;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
326c43c3d76SPaolo Abeni static inline bool fib4_has_custom_rules(const struct net *net)
327c43c3d76SPaolo Abeni {
328c43c3d76SPaolo Abeni 	return false;
329c43c3d76SPaolo Abeni }
330c43c3d76SPaolo Abeni 
3313c71006dSIdo Schimmel static inline bool fib4_rule_default(const struct fib_rule *rule)
3323c71006dSIdo Schimmel {
3333c71006dSIdo Schimmel 	return true;
3343c71006dSIdo Schimmel }
3353c71006dSIdo Schimmel 
336b7a59557SJiri Pirko static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb,
337b7a59557SJiri Pirko 				  struct netlink_ext_ack *extack)
3381b2a4440SIdo Schimmel {
3391b2a4440SIdo Schimmel 	return 0;
3401b2a4440SIdo Schimmel }
3411b2a4440SIdo Schimmel 
3421b2a4440SIdo Schimmel static inline unsigned int fib4_rules_seq_read(struct net *net)
3431b2a4440SIdo Schimmel {
3441b2a4440SIdo Schimmel 	return 0;
3451b2a4440SIdo Schimmel }
3461b2a4440SIdo Schimmel 
347e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
348e37b1e97SRoopa Prabhu 						 struct sk_buff *skb,
349e37b1e97SRoopa Prabhu 						 struct flowi4 *fl4,
350e37b1e97SRoopa Prabhu 						 struct flow_keys *flkeys)
351e37b1e97SRoopa Prabhu {
352e37b1e97SRoopa Prabhu 	return false;
353e37b1e97SRoopa Prabhu }
3541da177e4SLinus Torvalds #else /* CONFIG_IP_MULTIPLE_TABLES */
3555c3a0fd7SJoe Perches int __net_init fib4_rules_init(struct net *net);
3565c3a0fd7SJoe Perches void __net_exit fib4_rules_exit(struct net *net);
357c3e9a353SPavel Emelyanov 
3585c3a0fd7SJoe Perches struct fib_table *fib_new_table(struct net *net, u32 id);
3595c3a0fd7SJoe Perches struct fib_table *fib_get_table(struct net *net, u32 id);
3601da177e4SLinus Torvalds 
3610eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp,
3620eeb075fSAndy Gospodarek 		 struct fib_result *res, unsigned int flags);
363f4530fa5SDavid S. Miller 
364f4530fa5SDavid S. Miller static inline int fib_lookup(struct net *net, struct flowi4 *flp,
3650eeb075fSAndy Gospodarek 			     struct fib_result *res, unsigned int flags)
366f4530fa5SDavid S. Miller {
367a7e53531SAlexander Duyck 	struct fib_table *tb;
3680315e382SNikola Forró 	int err = -ENETUNREACH;
369a7e53531SAlexander Duyck 
3700eeb075fSAndy Gospodarek 	flags |= FIB_LOOKUP_NOREF;
371a7e53531SAlexander Duyck 	if (net->ipv4.fib_has_custom_rules)
3720eeb075fSAndy Gospodarek 		return __fib_lookup(net, flp, res, flags);
373345e9b54SAlexander Duyck 
374345e9b54SAlexander Duyck 	rcu_read_lock();
375345e9b54SAlexander Duyck 
37685b91b03SDavid S. Miller 	res->tclassid = 0;
377a7e53531SAlexander Duyck 
378a7e53531SAlexander Duyck 	tb = rcu_dereference_rtnl(net->ipv4.fib_main);
3790315e382SNikola Forró 	if (tb)
3800315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags);
3810315e382SNikola Forró 
3820315e382SNikola Forró 	if (!err)
3830315e382SNikola Forró 		goto out;
384a7e53531SAlexander Duyck 
385a7e53531SAlexander Duyck 	tb = rcu_dereference_rtnl(net->ipv4.fib_default);
3860315e382SNikola Forró 	if (tb)
3870315e382SNikola Forró 		err = fib_table_lookup(tb, flp, res, flags);
3880315e382SNikola Forró 
3890315e382SNikola Forró out:
3900315e382SNikola Forró 	if (err == -EAGAIN)
3910315e382SNikola Forró 		err = -ENETUNREACH;
392345e9b54SAlexander Duyck 
393345e9b54SAlexander Duyck 	rcu_read_unlock();
394345e9b54SAlexander Duyck 
395345e9b54SAlexander Duyck 	return err;
396f4530fa5SDavid S. Miller }
397f4530fa5SDavid S. Miller 
398c43c3d76SPaolo Abeni static inline bool fib4_has_custom_rules(const struct net *net)
399c43c3d76SPaolo Abeni {
400c43c3d76SPaolo Abeni 	return net->ipv4.fib_has_custom_rules;
401c43c3d76SPaolo Abeni }
402c43c3d76SPaolo Abeni 
4033c71006dSIdo Schimmel bool fib4_rule_default(const struct fib_rule *rule);
404b7a59557SJiri Pirko int fib4_rules_dump(struct net *net, struct notifier_block *nb,
405b7a59557SJiri Pirko 		    struct netlink_ext_ack *extack);
4061b2a4440SIdo Schimmel unsigned int fib4_rules_seq_read(struct net *net);
4073c71006dSIdo Schimmel 
408e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
409e37b1e97SRoopa Prabhu 						 struct sk_buff *skb,
410e37b1e97SRoopa Prabhu 						 struct flowi4 *fl4,
411e37b1e97SRoopa Prabhu 						 struct flow_keys *flkeys)
412e37b1e97SRoopa Prabhu {
413e37b1e97SRoopa Prabhu 	unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
414e37b1e97SRoopa Prabhu 
415e37b1e97SRoopa Prabhu 	if (!net->ipv4.fib_rules_require_fldissect)
416e37b1e97SRoopa Prabhu 		return false;
417e37b1e97SRoopa Prabhu 
418e37b1e97SRoopa Prabhu 	skb_flow_dissect_flow_keys(skb, flkeys, flag);
419e37b1e97SRoopa Prabhu 	fl4->fl4_sport = flkeys->ports.src;
420e37b1e97SRoopa Prabhu 	fl4->fl4_dport = flkeys->ports.dst;
421e37b1e97SRoopa Prabhu 	fl4->flowi4_proto = flkeys->basic.ip_proto;
422e37b1e97SRoopa Prabhu 
423e37b1e97SRoopa Prabhu 	return true;
424e37b1e97SRoopa Prabhu }
425e37b1e97SRoopa Prabhu 
4261da177e4SLinus Torvalds #endif /* CONFIG_IP_MULTIPLE_TABLES */
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds /* Exported by fib_frontend.c */
429ef7c79edSPatrick McHardy extern const struct nla_policy rtm_ipv4_policy[];
4305c3a0fd7SJoe Perches void ip_fib_init(void);
431d1566268SDavid Ahern int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
432d1566268SDavid Ahern 		    struct netlink_ext_ack *extack);
4335c3a0fd7SJoe Perches __be32 fib_compute_spec_dst(struct sk_buff *skb);
43478f2756cSDavid Ahern bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
4355c3a0fd7SJoe Perches int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
4365c04c819SMichael Smith 			u8 tos, int oif, struct net_device *dev,
4379e56e380SDavid S. Miller 			struct in_device *idev, u32 *itag);
4387a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
439f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
440f4530fa5SDavid S. Miller {
441f4530fa5SDavid S. Miller 	return net->ipv4.fib_num_tclassid_users;
442f4530fa5SDavid S. Miller }
4437a9bc9b8SDavid S. Miller #else
444f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
445f4530fa5SDavid S. Miller {
446f4530fa5SDavid S. Miller 	return 0;
447f4530fa5SDavid S. Miller }
4487a9bc9b8SDavid S. Miller #endif
4490ddcf43dSAlexander Duyck int fib_unmerge(struct net *net);
45014c85021SArnaldo Carvalho de Melo 
4511fd1c768SDavid Ahern static inline bool nhc_l3mdev_matches_dev(const struct fib_nh_common *nhc,
4521fd1c768SDavid Ahern const struct net_device *dev)
4531fd1c768SDavid Ahern {
4541fd1c768SDavid Ahern 	if (nhc->nhc_dev == dev ||
4551fd1c768SDavid Ahern 	    l3mdev_master_ifindex_rcu(nhc->nhc_dev) == dev->ifindex)
4561fd1c768SDavid Ahern 		return true;
4571fd1c768SDavid Ahern 
4581fd1c768SDavid Ahern 	return false;
4591fd1c768SDavid Ahern }
4601fd1c768SDavid Ahern 
4611da177e4SLinus Torvalds /* Exported by fib_semantics.c */
4625c3a0fd7SJoe Perches int ip_fib_check_default(__be32 gw, struct net_device *dev);
4634f823defSJulian Anastasov int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
4645a56a0b3SMark Tomlinson int fib_sync_down_addr(struct net_device *dev, __be32 local);
465ecc5663cSDavid Ahern int fib_sync_up(struct net_device *dev, unsigned char nh_flags);
466af7d6cceSSabrina Dubroca void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
46706c77c3eSDavid Ahern void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
4680e884c78SPeter Nørlund 
469ce5c9c20SIdo Schimmel /* Fields used for sysctl_fib_multipath_hash_fields.
470ce5c9c20SIdo Schimmel  * Common to IPv4 and IPv6.
471ce5c9c20SIdo Schimmel  *
472ce5c9c20SIdo Schimmel  * Add new fields at the end. This is user API.
473ce5c9c20SIdo Schimmel  */
474ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_SRC_IP			BIT(0)
475ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DST_IP			BIT(1)
476ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_IP_PROTO		BIT(2)
477ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_FLOWLABEL		BIT(3)
478ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_SRC_PORT		BIT(4)
479ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DST_PORT		BIT(5)
480ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP		BIT(6)
481ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP		BIT(7)
482ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO		BIT(8)
483ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL	BIT(9)
484ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT		BIT(10)
485ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT		BIT(11)
486ce5c9c20SIdo Schimmel 
487ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_OUTER_MASK		\
488ce5c9c20SIdo Schimmel 	(FIB_MULTIPATH_HASH_FIELD_SRC_IP |		\
489ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_DST_IP |		\
490ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_IP_PROTO |		\
491ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_FLOWLABEL |		\
492ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_SRC_PORT |		\
493ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_DST_PORT)
494ce5c9c20SIdo Schimmel 
495ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_MASK		\
496ce5c9c20SIdo Schimmel 	(FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP |	\
497ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP |	\
498ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO |	\
499ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL |	\
500ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT |	\
501ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
502ce5c9c20SIdo Schimmel 
503ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_ALL_MASK		\
504ce5c9c20SIdo Schimmel 	(FIB_MULTIPATH_HASH_FIELD_OUTER_MASK |		\
505ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_INNER_MASK)
506ce5c9c20SIdo Schimmel 
507ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK		\
508ce5c9c20SIdo Schimmel 	(FIB_MULTIPATH_HASH_FIELD_SRC_IP |		\
509ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_DST_IP |		\
510ce5c9c20SIdo Schimmel 	 FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
511ce5c9c20SIdo Schimmel 
512bf4e0a3dSNikolay Aleksandrov #ifdef CONFIG_IP_ROUTE_MULTIPATH
5137efc0b6bSDavid Ahern int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
514e37b1e97SRoopa Prabhu 		       const struct sk_buff *skb, struct flow_keys *flkeys);
515bf4e0a3dSNikolay Aleksandrov #endif
516ac1fab2dSDavid Ahern int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
517ac1fab2dSDavid Ahern 		 struct netlink_ext_ack *extack);
5180e884c78SPeter Nørlund void fib_select_multipath(struct fib_result *res, int hash);
5193ce58d84SDavid Ahern void fib_select_path(struct net *net, struct fib_result *res,
520bf4e0a3dSNikolay Aleksandrov 		     struct flowi4 *fl4, const struct sk_buff *skb);
5211da177e4SLinus Torvalds 
522e4516ef6SDavid Ahern int fib_nh_init(struct net *net, struct fib_nh *fib_nh,
523e4516ef6SDavid Ahern 		struct fib_config *cfg, int nh_weight,
524e4516ef6SDavid Ahern 		struct netlink_ext_ack *extack);
525faa041a4SDavid Ahern void fib_nh_release(struct net *net, struct fib_nh *fib_nh);
526faee6769SAlexander Aring int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
527faee6769SAlexander Aring 		       struct nlattr *fc_encap, u16 fc_encap_type,
528faee6769SAlexander Aring 		       void *cfg, gfp_t gfp_flags,
529979e276eSDavid Ahern 		       struct netlink_ext_ack *extack);
530979e276eSDavid Ahern void fib_nh_common_release(struct fib_nh_common *nhc);
531e4516ef6SDavid Ahern 
5325348ba85SDavid S. Miller /* Exported by fib_trie.c */
53390b93f1bSIdo Schimmel void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri);
5345c3a0fd7SJoe Perches void fib_trie_init(void);
5350ddcf43dSAlexander Duyck struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
536af7888adSDavid Ahern bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
537af7888adSDavid Ahern 			 const struct flowi4 *flp);
5381da177e4SLinus Torvalds 
539b6bf3ca0SDavid S. Miller static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
5401da177e4SLinus Torvalds {
541c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
542eba618abSDavid Ahern 	struct fib_nh_common *nhc = res->nhc;
5431da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
5441da177e4SLinus Torvalds 	u32 rtag;
5451da177e4SLinus Torvalds #endif
546dcb1ecb5SDavid Ahern 	if (nhc->nhc_family == AF_INET) {
547dcb1ecb5SDavid Ahern 		struct fib_nh *nh;
548dcb1ecb5SDavid Ahern 
549dcb1ecb5SDavid Ahern 		nh = container_of(nhc, struct fib_nh, nh_common);
550eba618abSDavid Ahern 		*itag = nh->nh_tclassid << 16;
551dcb1ecb5SDavid Ahern 	} else {
552dcb1ecb5SDavid Ahern 		*itag = 0;
553dcb1ecb5SDavid Ahern 	}
554dcb1ecb5SDavid Ahern 
5551da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
55685b91b03SDavid S. Miller 	rtag = res->tclassid;
5571da177e4SLinus Torvalds 	if (*itag == 0)
5581da177e4SLinus Torvalds 		*itag = (rtag<<16);
5591da177e4SLinus Torvalds 	*itag |= (rtag>>16);
5601da177e4SLinus Torvalds #endif
5611da177e4SLinus Torvalds #endif
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5649bd83667SDavid Ahern void fib_flush(struct net *net);
5655c3a0fd7SJoe Perches void free_fib_info(struct fib_info *fi);
5661da177e4SLinus Torvalds 
5671c677b3dSIdo Schimmel static inline void fib_info_hold(struct fib_info *fi)
5681c677b3dSIdo Schimmel {
5690029c0deSReshetova, Elena 	refcount_inc(&fi->fib_clntref);
5701c677b3dSIdo Schimmel }
5711c677b3dSIdo Schimmel 
5721da177e4SLinus Torvalds static inline void fib_info_put(struct fib_info *fi)
5731da177e4SLinus Torvalds {
5740029c0deSReshetova, Elena 	if (refcount_dec_and_test(&fi->fib_clntref))
5751da177e4SLinus Torvalds 		free_fib_info(fi);
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
57820380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
5795c3a0fd7SJoe Perches int __net_init fib_proc_init(struct net *net);
5805c3a0fd7SJoe Perches void __net_exit fib_proc_exit(struct net *net);
581cc8274f5SLi Zefan #else
582cc8274f5SLi Zefan static inline int fib_proc_init(struct net *net)
583cc8274f5SLi Zefan {
584cc8274f5SLi Zefan 	return 0;
585cc8274f5SLi Zefan }
586cc8274f5SLi Zefan static inline void fib_proc_exit(struct net *net)
587cc8274f5SLi Zefan {
588cc8274f5SLi Zefan }
58920380731SArnaldo Carvalho de Melo #endif
59020380731SArnaldo Carvalho de Melo 
59150d889b1SDavid Ahern u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr);
59250d889b1SDavid Ahern 
5934724676dSDavid Ahern int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
5944724676dSDavid Ahern 			  struct fib_dump_filter *filter,
595effe6792SDavid Ahern 			  struct netlink_callback *cb);
596c0a72077SDavid Ahern 
597c0a72077SDavid Ahern int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nh,
5987bdf4de1SDonald Sharp 		     u8 rt_family, unsigned char *flags, bool skip_oif);
599c0a72077SDavid Ahern int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nh,
600*597aa16cSXiao Liang 		    int nh_weight, u8 rt_family, u32 nh_tclassid);
6011da177e4SLinus Torvalds #endif  /* _NET_FIB_H */
602