xref: /linux-6.15/include/net/ip_fib.h (revision f4ef85bb)
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>
22e1ef4bf2SThomas Graf #include <net/fib_rules.h>
238e773277SDavid S. Miller #include <net/inetpeer.h>
24d26b3a7cSEric Dumazet #include <linux/percpu.h>
251da177e4SLinus Torvalds 
264e902c57SThomas Graf struct fib_config {
274e902c57SThomas Graf 	u8			fc_dst_len;
284e902c57SThomas Graf 	u8			fc_tos;
294e902c57SThomas Graf 	u8			fc_protocol;
304e902c57SThomas Graf 	u8			fc_scope;
314e902c57SThomas Graf 	u8			fc_type;
32b52f070cSThomas Graf 	/* 3 bytes unused */
334e902c57SThomas Graf 	u32			fc_table;
346d85c10aSAl Viro 	__be32			fc_dst;
356d85c10aSAl Viro 	__be32			fc_gw;
364e902c57SThomas Graf 	int			fc_oif;
374e902c57SThomas Graf 	u32			fc_flags;
384e902c57SThomas Graf 	u32			fc_priority;
396d85c10aSAl Viro 	__be32			fc_prefsrc;
404e902c57SThomas Graf 	struct nlattr		*fc_mx;
414e902c57SThomas Graf 	struct rtnexthop	*fc_mp;
424e902c57SThomas Graf 	int			fc_mx_len;
434e902c57SThomas Graf 	int			fc_mp_len;
444e902c57SThomas Graf 	u32			fc_flow;
454e902c57SThomas Graf 	u32			fc_nlflags;
464e902c57SThomas Graf 	struct nl_info		fc_nlinfo;
471da177e4SLinus Torvalds  };
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds struct fib_info;
50f2bb4bedSDavid S. Miller struct rtable;
511da177e4SLinus Torvalds 
524895c771SDavid S. Miller struct fib_nh_exception {
534895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*fnhe_next;
544895c771SDavid S. Miller 	__be32				fnhe_daddr;
554895c771SDavid S. Miller 	u32				fnhe_pmtu;
56aee06da6SJulian Anastasov 	__be32				fnhe_gw;
574895c771SDavid S. Miller 	unsigned long			fnhe_expires;
58c5038a83SDavid S. Miller 	struct rtable __rcu		*fnhe_rth;
594895c771SDavid S. Miller 	unsigned long			fnhe_stamp;
604895c771SDavid S. Miller };
614895c771SDavid S. Miller 
624895c771SDavid S. Miller struct fnhe_hash_bucket {
634895c771SDavid S. Miller 	struct fib_nh_exception __rcu	*chain;
644895c771SDavid S. Miller };
654895c771SDavid S. Miller 
664895c771SDavid S. Miller #define FNHE_HASH_SIZE		2048
674895c771SDavid S. Miller #define FNHE_RECLAIM_DEPTH	5
684895c771SDavid S. Miller 
691da177e4SLinus Torvalds struct fib_nh {
701da177e4SLinus Torvalds 	struct net_device	*nh_dev;
711da177e4SLinus Torvalds 	struct hlist_node	nh_hash;
721da177e4SLinus Torvalds 	struct fib_info		*nh_parent;
7395c96174SEric Dumazet 	unsigned int		nh_flags;
741da177e4SLinus Torvalds 	unsigned char		nh_scope;
751da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
761da177e4SLinus Torvalds 	int			nh_weight;
771da177e4SLinus Torvalds 	int			nh_power;
781da177e4SLinus Torvalds #endif
79c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
801da177e4SLinus Torvalds 	__u32			nh_tclassid;
811da177e4SLinus Torvalds #endif
821da177e4SLinus Torvalds 	int			nh_oif;
83ed49e3caSAl Viro 	__be32			nh_gw;
841fc050a1SDavid S. Miller 	__be32			nh_saddr;
85436c3b66SDavid S. Miller 	int			nh_saddr_genid;
86d26b3a7cSEric Dumazet 	struct rtable __rcu * __percpu *nh_pcpu_rth_output;
8754764bb6SEric Dumazet 	struct rtable __rcu	*nh_rth_input;
884895c771SDavid S. Miller 	struct fnhe_hash_bucket	*nh_exceptions;
891da177e4SLinus Torvalds };
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /*
921da177e4SLinus Torvalds  * This structure contains data shared by many of routes.
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds struct fib_info {
961da177e4SLinus Torvalds 	struct hlist_node	fib_hash;
971da177e4SLinus Torvalds 	struct hlist_node	fib_lhash;
987462bd74SDenis V. Lunev 	struct net		*fib_net;
991da177e4SLinus Torvalds 	int			fib_treeref;
1001da177e4SLinus Torvalds 	atomic_t		fib_clntref;
10195c96174SEric Dumazet 	unsigned int		fib_flags;
10237e826c5SDavid S. Miller 	unsigned char		fib_dead;
10337e826c5SDavid S. Miller 	unsigned char		fib_protocol;
10437e826c5SDavid S. Miller 	unsigned char		fib_scope;
105*f4ef85bbSEric Dumazet 	unsigned char		fib_type;
106b83738aeSAl Viro 	__be32			fib_prefsrc;
1071da177e4SLinus Torvalds 	u32			fib_priority;
1089c150e82SDavid S. Miller 	u32			*fib_metrics;
1091da177e4SLinus Torvalds #define fib_mtu fib_metrics[RTAX_MTU-1]
1101da177e4SLinus Torvalds #define fib_window fib_metrics[RTAX_WINDOW-1]
1111da177e4SLinus Torvalds #define fib_rtt fib_metrics[RTAX_RTT-1]
1121da177e4SLinus Torvalds #define fib_advmss fib_metrics[RTAX_ADVMSS-1]
1131da177e4SLinus Torvalds 	int			fib_nhs;
1141da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
1151da177e4SLinus Torvalds 	int			fib_power;
1161da177e4SLinus Torvalds #endif
117ebc0ffaeSEric Dumazet 	struct rcu_head		rcu;
1181da177e4SLinus Torvalds 	struct fib_nh		fib_nh[0];
1191da177e4SLinus Torvalds #define fib_dev		fib_nh[0].nh_dev
1201da177e4SLinus Torvalds };
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
1241da177e4SLinus Torvalds struct fib_rule;
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds 
1275b470441SDavid S. Miller struct fib_table;
1281da177e4SLinus Torvalds struct fib_result {
1291da177e4SLinus Torvalds 	unsigned char	prefixlen;
1301da177e4SLinus Torvalds 	unsigned char	nh_sel;
1311da177e4SLinus Torvalds 	unsigned char	type;
1321da177e4SLinus Torvalds 	unsigned char	scope;
13385b91b03SDavid S. Miller 	u32		tclassid;
1341da177e4SLinus Torvalds 	struct fib_info *fi;
1355b470441SDavid S. Miller 	struct fib_table *table;
1365b470441SDavid S. Miller 	struct list_head *fa_head;
1371da177e4SLinus Torvalds };
1381da177e4SLinus Torvalds 
139246955feSRobert Olsson struct fib_result_nl {
14080e856e1SAl Viro 	__be32		fl_addr;   /* To be looked up*/
1415f300893SThomas Graf 	u32		fl_mark;
142246955feSRobert Olsson 	unsigned char	fl_tos;
143246955feSRobert Olsson 	unsigned char   fl_scope;
144246955feSRobert Olsson 	unsigned char   tb_id_in;
145246955feSRobert Olsson 
146246955feSRobert Olsson 	unsigned char   tb_id;      /* Results */
147246955feSRobert Olsson 	unsigned char	prefixlen;
148246955feSRobert Olsson 	unsigned char	nh_sel;
149246955feSRobert Olsson 	unsigned char	type;
150246955feSRobert Olsson 	unsigned char	scope;
151246955feSRobert Olsson 	int             err;
152246955feSRobert Olsson };
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds #define FIB_RES_NH(res)		((res).fi->fib_nh[(res).nh_sel])
1571da177e4SLinus Torvalds 
15893456b6dSDenis V. Lunev #define FIB_TABLE_HASHSZ 2
15993456b6dSDenis V. Lunev 
1601da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds #define FIB_RES_NH(res)		((res).fi->fib_nh[0])
1631da177e4SLinus Torvalds 
16493456b6dSDenis V. Lunev #define FIB_TABLE_HASHSZ 256
16593456b6dSDenis V. Lunev 
1661da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */
1671da177e4SLinus Torvalds 
168436c3b66SDavid S. Miller extern __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh);
169436c3b66SDavid S. Miller 
170436c3b66SDavid S. Miller #define FIB_RES_SADDR(net, res)				\
171436c3b66SDavid S. Miller 	((FIB_RES_NH(res).nh_saddr_genid ==		\
172436c3b66SDavid S. Miller 	  atomic_read(&(net)->ipv4.dev_addr_genid)) ?	\
173436c3b66SDavid S. Miller 	 FIB_RES_NH(res).nh_saddr :			\
174436c3b66SDavid S. Miller 	 fib_info_update_nh_saddr((net), &FIB_RES_NH(res)))
1751da177e4SLinus Torvalds #define FIB_RES_GW(res)			(FIB_RES_NH(res).nh_gw)
1761da177e4SLinus Torvalds #define FIB_RES_DEV(res)		(FIB_RES_NH(res).nh_dev)
1771da177e4SLinus Torvalds #define FIB_RES_OIF(res)		(FIB_RES_NH(res).nh_oif)
1781da177e4SLinus Torvalds 
179436c3b66SDavid S. Miller #define FIB_RES_PREFSRC(net, res)	((res).fi->fib_prefsrc ? : \
180436c3b66SDavid S. Miller 					 FIB_RES_SADDR(net, res))
1811fc050a1SDavid S. Miller 
1821da177e4SLinus Torvalds struct fib_table {
1831af5a8c4SPatrick McHardy 	struct hlist_node	tb_hlist;
1842dfe55b4SPatrick McHardy 	u32			tb_id;
185971b893eSDenis V. Lunev 	int			tb_default;
18621d8c49eSDavid S. Miller 	int			tb_num_default;
18721d8c49eSDavid S. Miller 	unsigned long		tb_data[0];
1881da177e4SLinus Torvalds };
1891da177e4SLinus Torvalds 
19022bd5b9bSDavid S. Miller extern int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
191ebc0ffaeSEric Dumazet 			    struct fib_result *res, int fib_flags);
19216c6cf8bSStephen Hemminger extern int fib_table_insert(struct fib_table *, struct fib_config *);
19316c6cf8bSStephen Hemminger extern int fib_table_delete(struct fib_table *, struct fib_config *);
19416c6cf8bSStephen Hemminger extern int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
19516c6cf8bSStephen Hemminger 			  struct netlink_callback *cb);
19616c6cf8bSStephen Hemminger extern int fib_table_flush(struct fib_table *table);
1974aa2c466SPavel Emelyanov extern void fib_free_table(struct fib_table *tb);
1984aa2c466SPavel Emelyanov 
19916c6cf8bSStephen Hemminger 
20016c6cf8bSStephen Hemminger 
2011da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTIPLE_TABLES
2021da177e4SLinus Torvalds 
20393456b6dSDenis V. Lunev #define TABLE_LOCAL_INDEX	0
20493456b6dSDenis V. Lunev #define TABLE_MAIN_INDEX	1
2051da177e4SLinus Torvalds 
2068ad4942cSDenis V. Lunev static inline struct fib_table *fib_get_table(struct net *net, u32 id)
2071da177e4SLinus Torvalds {
20893456b6dSDenis V. Lunev 	struct hlist_head *ptr;
20993456b6dSDenis V. Lunev 
21093456b6dSDenis V. Lunev 	ptr = id == RT_TABLE_LOCAL ?
211e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
212e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
21393456b6dSDenis V. Lunev 	return hlist_entry(ptr->first, struct fib_table, tb_hlist);
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2168ad4942cSDenis V. Lunev static inline struct fib_table *fib_new_table(struct net *net, u32 id)
2171da177e4SLinus Torvalds {
2188ad4942cSDenis V. Lunev 	return fib_get_table(net, id);
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
22122bd5b9bSDavid S. Miller static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
222da0e28cbSDenis V. Lunev 			     struct fib_result *res)
2231da177e4SLinus Torvalds {
22493456b6dSDenis V. Lunev 	struct fib_table *table;
22593456b6dSDenis V. Lunev 
226da0e28cbSDenis V. Lunev 	table = fib_get_table(net, RT_TABLE_LOCAL);
227ebc0ffaeSEric Dumazet 	if (!fib_table_lookup(table, flp, res, FIB_LOOKUP_NOREF))
2281da177e4SLinus Torvalds 		return 0;
22993456b6dSDenis V. Lunev 
230da0e28cbSDenis V. Lunev 	table = fib_get_table(net, RT_TABLE_MAIN);
231ebc0ffaeSEric Dumazet 	if (!fib_table_lookup(table, flp, res, FIB_LOOKUP_NOREF))
23293456b6dSDenis V. Lunev 		return 0;
23393456b6dSDenis V. Lunev 	return -ENETUNREACH;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds #else /* CONFIG_IP_MULTIPLE_TABLES */
2377b1a74fdSDenis V. Lunev extern int __net_init fib4_rules_init(struct net *net);
2387b1a74fdSDenis V. Lunev extern void __net_exit fib4_rules_exit(struct net *net);
239c3e9a353SPavel Emelyanov 
2408ad4942cSDenis V. Lunev extern struct fib_table *fib_new_table(struct net *net, u32 id);
2418ad4942cSDenis V. Lunev extern struct fib_table *fib_get_table(struct net *net, u32 id);
2421da177e4SLinus Torvalds 
243f4530fa5SDavid S. Miller extern int __fib_lookup(struct net *net, struct flowi4 *flp,
244f4530fa5SDavid S. Miller 			struct fib_result *res);
245f4530fa5SDavid S. Miller 
246f4530fa5SDavid S. Miller static inline int fib_lookup(struct net *net, struct flowi4 *flp,
247f4530fa5SDavid S. Miller 			     struct fib_result *res)
248f4530fa5SDavid S. Miller {
249f4530fa5SDavid S. Miller 	if (!net->ipv4.fib_has_custom_rules) {
25085b91b03SDavid S. Miller 		res->tclassid = 0;
251f4530fa5SDavid S. Miller 		if (net->ipv4.fib_local &&
252f4530fa5SDavid S. Miller 		    !fib_table_lookup(net->ipv4.fib_local, flp, res,
253f4530fa5SDavid S. Miller 				      FIB_LOOKUP_NOREF))
254f4530fa5SDavid S. Miller 			return 0;
255f4530fa5SDavid S. Miller 		if (net->ipv4.fib_main &&
256f4530fa5SDavid S. Miller 		    !fib_table_lookup(net->ipv4.fib_main, flp, res,
257f4530fa5SDavid S. Miller 				      FIB_LOOKUP_NOREF))
258f4530fa5SDavid S. Miller 			return 0;
259f4530fa5SDavid S. Miller 		if (net->ipv4.fib_default &&
260f4530fa5SDavid S. Miller 		    !fib_table_lookup(net->ipv4.fib_default, flp, res,
261f4530fa5SDavid S. Miller 				      FIB_LOOKUP_NOREF))
262f4530fa5SDavid S. Miller 			return 0;
263f4530fa5SDavid S. Miller 		return -ENETUNREACH;
264f4530fa5SDavid S. Miller 	}
265f4530fa5SDavid S. Miller 	return __fib_lookup(net, flp, res);
266f4530fa5SDavid S. Miller }
267f4530fa5SDavid S. Miller 
2681da177e4SLinus Torvalds #endif /* CONFIG_IP_MULTIPLE_TABLES */
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /* Exported by fib_frontend.c */
271ef7c79edSPatrick McHardy extern const struct nla_policy rtm_ipv4_policy[];
2721da177e4SLinus Torvalds extern void		ip_fib_init(void);
27335ebf65eSDavid S. Miller extern __be32 fib_compute_spec_dst(struct sk_buff *skb);
2745c04c819SMichael Smith extern int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
2755c04c819SMichael Smith 			       u8 tos, int oif, struct net_device *dev,
2769e56e380SDavid S. Miller 			       struct in_device *idev, u32 *itag);
2770c838ff1SDavid S. Miller extern void fib_select_default(struct fib_result *res);
2787a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
279f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
280f4530fa5SDavid S. Miller {
281f4530fa5SDavid S. Miller 	return net->ipv4.fib_num_tclassid_users;
282f4530fa5SDavid S. Miller }
2837a9bc9b8SDavid S. Miller #else
284f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
285f4530fa5SDavid S. Miller {
286f4530fa5SDavid S. Miller 	return 0;
287f4530fa5SDavid S. Miller }
2887a9bc9b8SDavid S. Miller #endif
28914c85021SArnaldo Carvalho de Melo 
2901da177e4SLinus Torvalds /* Exported by fib_semantics.c */
291d878e72eSAl Viro extern int ip_fib_check_default(__be32 gw, struct net_device *dev);
29285326fa5SDenis V. Lunev extern int fib_sync_down_dev(struct net_device *dev, int force);
2934814bdbdSDenis V. Lunev extern int fib_sync_down_addr(struct net *net, __be32 local);
2941fc050a1SDavid S. Miller extern void fib_update_nh_saddrs(struct net_device *dev);
2951da177e4SLinus Torvalds extern int fib_sync_up(struct net_device *dev);
2961b7fe593SDavid S. Miller extern void fib_select_multipath(struct fib_result *res);
2971da177e4SLinus Torvalds 
2985348ba85SDavid S. Miller /* Exported by fib_trie.c */
2995348ba85SDavid S. Miller extern void fib_trie_init(void);
3005348ba85SDavid S. Miller extern struct fib_table *fib_trie_table(u32 id);
3011da177e4SLinus Torvalds 
302b6bf3ca0SDavid S. Miller static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
3031da177e4SLinus Torvalds {
304c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
3051da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
3061da177e4SLinus Torvalds 	u32 rtag;
3071da177e4SLinus Torvalds #endif
3081da177e4SLinus Torvalds 	*itag = FIB_RES_NH(*res).nh_tclassid<<16;
3091da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
31085b91b03SDavid S. Miller 	rtag = res->tclassid;
3111da177e4SLinus Torvalds 	if (*itag == 0)
3121da177e4SLinus Torvalds 		*itag = (rtag<<16);
3131da177e4SLinus Torvalds 	*itag |= (rtag>>16);
3141da177e4SLinus Torvalds #endif
3151da177e4SLinus Torvalds #endif
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds extern void free_fib_info(struct fib_info *fi);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds static inline void fib_info_put(struct fib_info *fi)
3211da177e4SLinus Torvalds {
3221da177e4SLinus Torvalds 	if (atomic_dec_and_test(&fi->fib_clntref))
3231da177e4SLinus Torvalds 		free_fib_info(fi);
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
32620380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
32761a02653SDenis V. Lunev extern int __net_init  fib_proc_init(struct net *net);
32861a02653SDenis V. Lunev extern void __net_exit fib_proc_exit(struct net *net);
329cc8274f5SLi Zefan #else
330cc8274f5SLi Zefan static inline int fib_proc_init(struct net *net)
331cc8274f5SLi Zefan {
332cc8274f5SLi Zefan 	return 0;
333cc8274f5SLi Zefan }
334cc8274f5SLi Zefan static inline void fib_proc_exit(struct net *net)
335cc8274f5SLi Zefan {
336cc8274f5SLi Zefan }
33720380731SArnaldo Carvalho de Melo #endif
33820380731SArnaldo Carvalho de Melo 
3391da177e4SLinus Torvalds #endif  /* _NET_FIB_H */
340