xref: /linux-6.15/include/net/ip_fib.h (revision 85b91b03)
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>
21e1ef4bf2SThomas Graf #include <net/fib_rules.h>
228e773277SDavid S. Miller #include <net/inetpeer.h>
231da177e4SLinus Torvalds 
244e902c57SThomas Graf struct fib_config {
254e902c57SThomas Graf 	u8			fc_dst_len;
264e902c57SThomas Graf 	u8			fc_tos;
274e902c57SThomas Graf 	u8			fc_protocol;
284e902c57SThomas Graf 	u8			fc_scope;
294e902c57SThomas Graf 	u8			fc_type;
30b52f070cSThomas Graf 	/* 3 bytes unused */
314e902c57SThomas Graf 	u32			fc_table;
326d85c10aSAl Viro 	__be32			fc_dst;
336d85c10aSAl Viro 	__be32			fc_gw;
344e902c57SThomas Graf 	int			fc_oif;
354e902c57SThomas Graf 	u32			fc_flags;
364e902c57SThomas Graf 	u32			fc_priority;
376d85c10aSAl Viro 	__be32			fc_prefsrc;
384e902c57SThomas Graf 	struct nlattr		*fc_mx;
394e902c57SThomas Graf 	struct rtnexthop	*fc_mp;
404e902c57SThomas Graf 	int			fc_mx_len;
414e902c57SThomas Graf 	int			fc_mp_len;
424e902c57SThomas Graf 	u32			fc_flow;
434e902c57SThomas Graf 	u32			fc_nlflags;
444e902c57SThomas Graf 	struct nl_info		fc_nlinfo;
451da177e4SLinus Torvalds  };
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds struct fib_info;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds struct fib_nh {
501da177e4SLinus Torvalds 	struct net_device	*nh_dev;
511da177e4SLinus Torvalds 	struct hlist_node	nh_hash;
521da177e4SLinus Torvalds 	struct fib_info		*nh_parent;
5395c96174SEric Dumazet 	unsigned int		nh_flags;
541da177e4SLinus Torvalds 	unsigned char		nh_scope;
551da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
561da177e4SLinus Torvalds 	int			nh_weight;
571da177e4SLinus Torvalds 	int			nh_power;
581da177e4SLinus Torvalds #endif
59c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
601da177e4SLinus Torvalds 	__u32			nh_tclassid;
611da177e4SLinus Torvalds #endif
621da177e4SLinus Torvalds 	int			nh_oif;
63ed49e3caSAl Viro 	__be32			nh_gw;
641fc050a1SDavid S. Miller 	__be32			nh_saddr;
65436c3b66SDavid S. Miller 	int			nh_saddr_genid;
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  * This structure contains data shared by many of routes.
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds struct fib_info {
731da177e4SLinus Torvalds 	struct hlist_node	fib_hash;
741da177e4SLinus Torvalds 	struct hlist_node	fib_lhash;
757462bd74SDenis V. Lunev 	struct net		*fib_net;
761da177e4SLinus Torvalds 	int			fib_treeref;
771da177e4SLinus Torvalds 	atomic_t		fib_clntref;
7895c96174SEric Dumazet 	unsigned int		fib_flags;
7937e826c5SDavid S. Miller 	unsigned char		fib_dead;
8037e826c5SDavid S. Miller 	unsigned char		fib_protocol;
8137e826c5SDavid S. Miller 	unsigned char		fib_scope;
82b83738aeSAl Viro 	__be32			fib_prefsrc;
831da177e4SLinus Torvalds 	u32			fib_priority;
849c150e82SDavid S. Miller 	u32			*fib_metrics;
851da177e4SLinus Torvalds #define fib_mtu fib_metrics[RTAX_MTU-1]
861da177e4SLinus Torvalds #define fib_window fib_metrics[RTAX_WINDOW-1]
871da177e4SLinus Torvalds #define fib_rtt fib_metrics[RTAX_RTT-1]
881da177e4SLinus Torvalds #define fib_advmss fib_metrics[RTAX_ADVMSS-1]
891da177e4SLinus Torvalds 	int			fib_nhs;
901da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
911da177e4SLinus Torvalds 	int			fib_power;
921da177e4SLinus Torvalds #endif
93ebc0ffaeSEric Dumazet 	struct rcu_head		rcu;
941da177e4SLinus Torvalds 	struct fib_nh		fib_nh[0];
951da177e4SLinus Torvalds #define fib_dev		fib_nh[0].nh_dev
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
1001da177e4SLinus Torvalds struct fib_rule;
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
1035b470441SDavid S. Miller struct fib_table;
1041da177e4SLinus Torvalds struct fib_result {
1051da177e4SLinus Torvalds 	unsigned char	prefixlen;
1061da177e4SLinus Torvalds 	unsigned char	nh_sel;
1071da177e4SLinus Torvalds 	unsigned char	type;
1081da177e4SLinus Torvalds 	unsigned char	scope;
109*85b91b03SDavid S. Miller 	u32		tclassid;
1101da177e4SLinus Torvalds 	struct fib_info *fi;
1115b470441SDavid S. Miller 	struct fib_table *table;
1125b470441SDavid S. Miller 	struct list_head *fa_head;
1131da177e4SLinus Torvalds };
1141da177e4SLinus Torvalds 
115246955feSRobert Olsson struct fib_result_nl {
11680e856e1SAl Viro 	__be32		fl_addr;   /* To be looked up*/
1175f300893SThomas Graf 	u32		fl_mark;
118246955feSRobert Olsson 	unsigned char	fl_tos;
119246955feSRobert Olsson 	unsigned char   fl_scope;
120246955feSRobert Olsson 	unsigned char   tb_id_in;
121246955feSRobert Olsson 
122246955feSRobert Olsson 	unsigned char   tb_id;      /* Results */
123246955feSRobert Olsson 	unsigned char	prefixlen;
124246955feSRobert Olsson 	unsigned char	nh_sel;
125246955feSRobert Olsson 	unsigned char	type;
126246955feSRobert Olsson 	unsigned char	scope;
127246955feSRobert Olsson 	int             err;
128246955feSRobert Olsson };
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds #define FIB_RES_NH(res)		((res).fi->fib_nh[(res).nh_sel])
1331da177e4SLinus Torvalds 
13493456b6dSDenis V. Lunev #define FIB_TABLE_HASHSZ 2
13593456b6dSDenis V. Lunev 
1361da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #define FIB_RES_NH(res)		((res).fi->fib_nh[0])
1391da177e4SLinus Torvalds 
14093456b6dSDenis V. Lunev #define FIB_TABLE_HASHSZ 256
14193456b6dSDenis V. Lunev 
1421da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */
1431da177e4SLinus Torvalds 
144436c3b66SDavid S. Miller extern __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh);
145436c3b66SDavid S. Miller 
146436c3b66SDavid S. Miller #define FIB_RES_SADDR(net, res)				\
147436c3b66SDavid S. Miller 	((FIB_RES_NH(res).nh_saddr_genid ==		\
148436c3b66SDavid S. Miller 	  atomic_read(&(net)->ipv4.dev_addr_genid)) ?	\
149436c3b66SDavid S. Miller 	 FIB_RES_NH(res).nh_saddr :			\
150436c3b66SDavid S. Miller 	 fib_info_update_nh_saddr((net), &FIB_RES_NH(res)))
1511da177e4SLinus Torvalds #define FIB_RES_GW(res)			(FIB_RES_NH(res).nh_gw)
1521da177e4SLinus Torvalds #define FIB_RES_DEV(res)		(FIB_RES_NH(res).nh_dev)
1531da177e4SLinus Torvalds #define FIB_RES_OIF(res)		(FIB_RES_NH(res).nh_oif)
1541da177e4SLinus Torvalds 
155436c3b66SDavid S. Miller #define FIB_RES_PREFSRC(net, res)	((res).fi->fib_prefsrc ? : \
156436c3b66SDavid S. Miller 					 FIB_RES_SADDR(net, res))
1571fc050a1SDavid S. Miller 
1581da177e4SLinus Torvalds struct fib_table {
1591af5a8c4SPatrick McHardy 	struct hlist_node	tb_hlist;
1602dfe55b4SPatrick McHardy 	u32			tb_id;
161971b893eSDenis V. Lunev 	int			tb_default;
16221d8c49eSDavid S. Miller 	int			tb_num_default;
16321d8c49eSDavid S. Miller 	unsigned long		tb_data[0];
1641da177e4SLinus Torvalds };
1651da177e4SLinus Torvalds 
16622bd5b9bSDavid S. Miller extern int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
167ebc0ffaeSEric Dumazet 			    struct fib_result *res, int fib_flags);
16816c6cf8bSStephen Hemminger extern int fib_table_insert(struct fib_table *, struct fib_config *);
16916c6cf8bSStephen Hemminger extern int fib_table_delete(struct fib_table *, struct fib_config *);
17016c6cf8bSStephen Hemminger extern int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
17116c6cf8bSStephen Hemminger 			  struct netlink_callback *cb);
17216c6cf8bSStephen Hemminger extern int fib_table_flush(struct fib_table *table);
1734aa2c466SPavel Emelyanov extern void fib_free_table(struct fib_table *tb);
1744aa2c466SPavel Emelyanov 
17516c6cf8bSStephen Hemminger 
17616c6cf8bSStephen Hemminger 
1771da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTIPLE_TABLES
1781da177e4SLinus Torvalds 
17993456b6dSDenis V. Lunev #define TABLE_LOCAL_INDEX	0
18093456b6dSDenis V. Lunev #define TABLE_MAIN_INDEX	1
1811da177e4SLinus Torvalds 
1828ad4942cSDenis V. Lunev static inline struct fib_table *fib_get_table(struct net *net, u32 id)
1831da177e4SLinus Torvalds {
18493456b6dSDenis V. Lunev 	struct hlist_head *ptr;
18593456b6dSDenis V. Lunev 
18693456b6dSDenis V. Lunev 	ptr = id == RT_TABLE_LOCAL ?
187e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
188e4aef8aeSDenis V. Lunev 		&net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
18993456b6dSDenis V. Lunev 	return hlist_entry(ptr->first, struct fib_table, tb_hlist);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1928ad4942cSDenis V. Lunev static inline struct fib_table *fib_new_table(struct net *net, u32 id)
1931da177e4SLinus Torvalds {
1948ad4942cSDenis V. Lunev 	return fib_get_table(net, id);
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
19722bd5b9bSDavid S. Miller static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
198da0e28cbSDenis V. Lunev 			     struct fib_result *res)
1991da177e4SLinus Torvalds {
20093456b6dSDenis V. Lunev 	struct fib_table *table;
20193456b6dSDenis V. Lunev 
202da0e28cbSDenis V. Lunev 	table = fib_get_table(net, RT_TABLE_LOCAL);
203ebc0ffaeSEric Dumazet 	if (!fib_table_lookup(table, flp, res, FIB_LOOKUP_NOREF))
2041da177e4SLinus Torvalds 		return 0;
20593456b6dSDenis V. Lunev 
206da0e28cbSDenis V. Lunev 	table = fib_get_table(net, RT_TABLE_MAIN);
207ebc0ffaeSEric Dumazet 	if (!fib_table_lookup(table, flp, res, FIB_LOOKUP_NOREF))
20893456b6dSDenis V. Lunev 		return 0;
20993456b6dSDenis V. Lunev 	return -ENETUNREACH;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds #else /* CONFIG_IP_MULTIPLE_TABLES */
2137b1a74fdSDenis V. Lunev extern int __net_init fib4_rules_init(struct net *net);
2147b1a74fdSDenis V. Lunev extern void __net_exit fib4_rules_exit(struct net *net);
215c3e9a353SPavel Emelyanov 
2168ad4942cSDenis V. Lunev extern struct fib_table *fib_new_table(struct net *net, u32 id);
2178ad4942cSDenis V. Lunev extern struct fib_table *fib_get_table(struct net *net, u32 id);
2181da177e4SLinus Torvalds 
219f4530fa5SDavid S. Miller extern int __fib_lookup(struct net *net, struct flowi4 *flp,
220f4530fa5SDavid S. Miller 			struct fib_result *res);
221f4530fa5SDavid S. Miller 
222f4530fa5SDavid S. Miller static inline int fib_lookup(struct net *net, struct flowi4 *flp,
223f4530fa5SDavid S. Miller 			     struct fib_result *res)
224f4530fa5SDavid S. Miller {
225f4530fa5SDavid S. Miller 	if (!net->ipv4.fib_has_custom_rules) {
226*85b91b03SDavid S. Miller 		res->tclassid = 0;
227f4530fa5SDavid S. Miller 		if (net->ipv4.fib_local &&
228f4530fa5SDavid S. Miller 		    !fib_table_lookup(net->ipv4.fib_local, flp, res,
229f4530fa5SDavid S. Miller 				      FIB_LOOKUP_NOREF))
230f4530fa5SDavid S. Miller 			return 0;
231f4530fa5SDavid S. Miller 		if (net->ipv4.fib_main &&
232f4530fa5SDavid S. Miller 		    !fib_table_lookup(net->ipv4.fib_main, flp, res,
233f4530fa5SDavid S. Miller 				      FIB_LOOKUP_NOREF))
234f4530fa5SDavid S. Miller 			return 0;
235f4530fa5SDavid S. Miller 		if (net->ipv4.fib_default &&
236f4530fa5SDavid S. Miller 		    !fib_table_lookup(net->ipv4.fib_default, flp, res,
237f4530fa5SDavid S. Miller 				      FIB_LOOKUP_NOREF))
238f4530fa5SDavid S. Miller 			return 0;
239f4530fa5SDavid S. Miller 		return -ENETUNREACH;
240f4530fa5SDavid S. Miller 	}
241f4530fa5SDavid S. Miller 	return __fib_lookup(net, flp, res);
242f4530fa5SDavid S. Miller }
243f4530fa5SDavid S. Miller 
2441da177e4SLinus Torvalds #endif /* CONFIG_IP_MULTIPLE_TABLES */
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds /* Exported by fib_frontend.c */
247ef7c79edSPatrick McHardy extern const struct nla_policy rtm_ipv4_policy[];
2481da177e4SLinus Torvalds extern void		ip_fib_init(void);
24935ebf65eSDavid S. Miller extern __be32 fib_compute_spec_dst(struct sk_buff *skb);
2505c04c819SMichael Smith extern int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
2515c04c819SMichael Smith 			       u8 tos, int oif, struct net_device *dev,
2529e56e380SDavid S. Miller 			       struct in_device *idev, u32 *itag);
2530c838ff1SDavid S. Miller extern void fib_select_default(struct fib_result *res);
2547a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
255f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
256f4530fa5SDavid S. Miller {
257f4530fa5SDavid S. Miller 	return net->ipv4.fib_num_tclassid_users;
258f4530fa5SDavid S. Miller }
2597a9bc9b8SDavid S. Miller #else
260f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
261f4530fa5SDavid S. Miller {
262f4530fa5SDavid S. Miller 	return 0;
263f4530fa5SDavid S. Miller }
2647a9bc9b8SDavid S. Miller #endif
26514c85021SArnaldo Carvalho de Melo 
2661da177e4SLinus Torvalds /* Exported by fib_semantics.c */
267d878e72eSAl Viro extern int ip_fib_check_default(__be32 gw, struct net_device *dev);
26885326fa5SDenis V. Lunev extern int fib_sync_down_dev(struct net_device *dev, int force);
2694814bdbdSDenis V. Lunev extern int fib_sync_down_addr(struct net *net, __be32 local);
2701fc050a1SDavid S. Miller extern void fib_update_nh_saddrs(struct net_device *dev);
2711da177e4SLinus Torvalds extern int fib_sync_up(struct net_device *dev);
2721b7fe593SDavid S. Miller extern void fib_select_multipath(struct fib_result *res);
2731da177e4SLinus Torvalds 
2745348ba85SDavid S. Miller /* Exported by fib_trie.c */
2755348ba85SDavid S. Miller extern void fib_trie_init(void);
2765348ba85SDavid S. Miller extern struct fib_table *fib_trie_table(u32 id);
2771da177e4SLinus Torvalds 
278b6bf3ca0SDavid S. Miller static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
2791da177e4SLinus Torvalds {
280c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2811da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
2821da177e4SLinus Torvalds 	u32 rtag;
2831da177e4SLinus Torvalds #endif
2841da177e4SLinus Torvalds 	*itag = FIB_RES_NH(*res).nh_tclassid<<16;
2851da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
286*85b91b03SDavid S. Miller 	rtag = res->tclassid;
2871da177e4SLinus Torvalds 	if (*itag == 0)
2881da177e4SLinus Torvalds 		*itag = (rtag<<16);
2891da177e4SLinus Torvalds 	*itag |= (rtag>>16);
2901da177e4SLinus Torvalds #endif
2911da177e4SLinus Torvalds #endif
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds extern void free_fib_info(struct fib_info *fi);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static inline void fib_info_put(struct fib_info *fi)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	if (atomic_dec_and_test(&fi->fib_clntref))
2991da177e4SLinus Torvalds 		free_fib_info(fi);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
30220380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
30361a02653SDenis V. Lunev extern int __net_init  fib_proc_init(struct net *net);
30461a02653SDenis V. Lunev extern void __net_exit fib_proc_exit(struct net *net);
305cc8274f5SLi Zefan #else
306cc8274f5SLi Zefan static inline int fib_proc_init(struct net *net)
307cc8274f5SLi Zefan {
308cc8274f5SLi Zefan 	return 0;
309cc8274f5SLi Zefan }
310cc8274f5SLi Zefan static inline void fib_proc_exit(struct net *net)
311cc8274f5SLi Zefan {
312cc8274f5SLi Zefan }
31320380731SArnaldo Carvalho de Melo #endif
31420380731SArnaldo Carvalho de Melo 
3151da177e4SLinus Torvalds #endif  /* _NET_FIB_H */
316