xref: /openbmc/linux/include/net/ip_fib.h (revision e868d61272caa648214046a096e5a6bfc068dc8c)
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET  is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		Definitions for the Forwarding Information Base.
7  *
8  * Authors:	A.N.Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *		This program is free software; you can redistribute it and/or
11  *		modify it under the terms of the GNU General Public License
12  *		as published by the Free Software Foundation; either version
13  *		2 of the License, or (at your option) any later version.
14  */
15 
16 #ifndef _NET_IP_FIB_H
17 #define _NET_IP_FIB_H
18 
19 #include <net/flow.h>
20 #include <linux/seq_file.h>
21 #include <net/fib_rules.h>
22 
23 struct fib_config {
24 	u8			fc_dst_len;
25 	u8			fc_tos;
26 	u8			fc_protocol;
27 	u8			fc_scope;
28 	u8			fc_type;
29 	/* 3 bytes unused */
30 	u32			fc_table;
31 	__be32			fc_dst;
32 	__be32			fc_gw;
33 	int			fc_oif;
34 	u32			fc_flags;
35 	u32			fc_priority;
36 	__be32			fc_prefsrc;
37 	struct nlattr		*fc_mx;
38 	struct rtnexthop	*fc_mp;
39 	int			fc_mx_len;
40 	int			fc_mp_len;
41 	u32			fc_flow;
42 	u32			fc_mp_alg;
43 	u32			fc_nlflags;
44 	struct nl_info		fc_nlinfo;
45  };
46 
47 struct fib_info;
48 
49 struct fib_nh {
50 	struct net_device	*nh_dev;
51 	struct hlist_node	nh_hash;
52 	struct fib_info		*nh_parent;
53 	unsigned		nh_flags;
54 	unsigned char		nh_scope;
55 #ifdef CONFIG_IP_ROUTE_MULTIPATH
56 	int			nh_weight;
57 	int			nh_power;
58 #endif
59 #ifdef CONFIG_NET_CLS_ROUTE
60 	__u32			nh_tclassid;
61 #endif
62 	int			nh_oif;
63 	__be32			nh_gw;
64 };
65 
66 /*
67  * This structure contains data shared by many of routes.
68  */
69 
70 struct fib_info {
71 	struct hlist_node	fib_hash;
72 	struct hlist_node	fib_lhash;
73 	int			fib_treeref;
74 	atomic_t		fib_clntref;
75 	int			fib_dead;
76 	unsigned		fib_flags;
77 	int			fib_protocol;
78 	__be32			fib_prefsrc;
79 	u32			fib_priority;
80 	u32			fib_metrics[RTAX_MAX];
81 #define fib_mtu fib_metrics[RTAX_MTU-1]
82 #define fib_window fib_metrics[RTAX_WINDOW-1]
83 #define fib_rtt fib_metrics[RTAX_RTT-1]
84 #define fib_advmss fib_metrics[RTAX_ADVMSS-1]
85 	int			fib_nhs;
86 #ifdef CONFIG_IP_ROUTE_MULTIPATH
87 	int			fib_power;
88 #endif
89 #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
90 	u32			fib_mp_alg;
91 #endif
92 	struct fib_nh		fib_nh[0];
93 #define fib_dev		fib_nh[0].nh_dev
94 };
95 
96 
97 #ifdef CONFIG_IP_MULTIPLE_TABLES
98 struct fib_rule;
99 #endif
100 
101 struct fib_result {
102 	unsigned char	prefixlen;
103 	unsigned char	nh_sel;
104 	unsigned char	type;
105 	unsigned char	scope;
106 #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
107 	__be32          network;
108 	__be32          netmask;
109 #endif
110 	struct fib_info *fi;
111 #ifdef CONFIG_IP_MULTIPLE_TABLES
112 	struct fib_rule	*r;
113 #endif
114 };
115 
116 struct fib_result_nl {
117 	__be32		fl_addr;   /* To be looked up*/
118 	u32		fl_mark;
119 	unsigned char	fl_tos;
120 	unsigned char   fl_scope;
121 	unsigned char   tb_id_in;
122 
123 	unsigned char   tb_id;      /* Results */
124 	unsigned char	prefixlen;
125 	unsigned char	nh_sel;
126 	unsigned char	type;
127 	unsigned char	scope;
128 	int             err;
129 };
130 
131 #ifdef CONFIG_IP_ROUTE_MULTIPATH
132 
133 #define FIB_RES_NH(res)		((res).fi->fib_nh[(res).nh_sel])
134 #define FIB_RES_RESET(res)	((res).nh_sel = 0)
135 
136 #else /* CONFIG_IP_ROUTE_MULTIPATH */
137 
138 #define FIB_RES_NH(res)		((res).fi->fib_nh[0])
139 #define FIB_RES_RESET(res)
140 
141 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
142 
143 #define FIB_RES_PREFSRC(res)		((res).fi->fib_prefsrc ? : __fib_res_prefsrc(&res))
144 #define FIB_RES_GW(res)			(FIB_RES_NH(res).nh_gw)
145 #define FIB_RES_DEV(res)		(FIB_RES_NH(res).nh_dev)
146 #define FIB_RES_OIF(res)		(FIB_RES_NH(res).nh_oif)
147 
148 #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
149 #define FIB_RES_NETWORK(res)		((res).network)
150 #define FIB_RES_NETMASK(res)	        ((res).netmask)
151 #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
152 #define FIB_RES_NETWORK(res)		(0)
153 #define FIB_RES_NETMASK(res)	        (0)
154 #endif /* CONFIG_IP_ROUTE_MULTIPATH_WRANDOM */
155 
156 struct fib_table {
157 	struct hlist_node tb_hlist;
158 	u32		tb_id;
159 	unsigned	tb_stamp;
160 	int		(*tb_lookup)(struct fib_table *tb, const struct flowi *flp, struct fib_result *res);
161 	int		(*tb_insert)(struct fib_table *, struct fib_config *);
162 	int		(*tb_delete)(struct fib_table *, struct fib_config *);
163 	int		(*tb_dump)(struct fib_table *table, struct sk_buff *skb,
164 				     struct netlink_callback *cb);
165 	int		(*tb_flush)(struct fib_table *table);
166 	void		(*tb_select_default)(struct fib_table *table,
167 					     const struct flowi *flp, struct fib_result *res);
168 
169 	unsigned char	tb_data[0];
170 };
171 
172 #ifndef CONFIG_IP_MULTIPLE_TABLES
173 
174 extern struct fib_table *ip_fib_local_table;
175 extern struct fib_table *ip_fib_main_table;
176 
177 static inline struct fib_table *fib_get_table(u32 id)
178 {
179 	if (id != RT_TABLE_LOCAL)
180 		return ip_fib_main_table;
181 	return ip_fib_local_table;
182 }
183 
184 static inline struct fib_table *fib_new_table(u32 id)
185 {
186 	return fib_get_table(id);
187 }
188 
189 static inline int fib_lookup(const struct flowi *flp, struct fib_result *res)
190 {
191 	if (ip_fib_local_table->tb_lookup(ip_fib_local_table, flp, res) &&
192 	    ip_fib_main_table->tb_lookup(ip_fib_main_table, flp, res))
193 		return -ENETUNREACH;
194 	return 0;
195 }
196 
197 static inline void fib_select_default(const struct flowi *flp, struct fib_result *res)
198 {
199 	if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
200 		ip_fib_main_table->tb_select_default(ip_fib_main_table, flp, res);
201 }
202 
203 #else /* CONFIG_IP_MULTIPLE_TABLES */
204 #define ip_fib_local_table fib_get_table(RT_TABLE_LOCAL)
205 #define ip_fib_main_table fib_get_table(RT_TABLE_MAIN)
206 
207 extern int fib_lookup(struct flowi *flp, struct fib_result *res);
208 
209 extern struct fib_table *fib_new_table(u32 id);
210 extern struct fib_table *fib_get_table(u32 id);
211 extern void fib_select_default(const struct flowi *flp, struct fib_result *res);
212 
213 #endif /* CONFIG_IP_MULTIPLE_TABLES */
214 
215 /* Exported by fib_frontend.c */
216 extern struct nla_policy rtm_ipv4_policy[];
217 extern void		ip_fib_init(void);
218 extern int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
219 			       struct net_device *dev, __be32 *spec_dst, u32 *itag);
220 extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
221 
222 struct rtentry;
223 
224 /* Exported by fib_semantics.c */
225 extern int ip_fib_check_default(__be32 gw, struct net_device *dev);
226 extern int fib_sync_down(__be32 local, struct net_device *dev, int force);
227 extern int fib_sync_up(struct net_device *dev);
228 extern __be32  __fib_res_prefsrc(struct fib_result *res);
229 
230 /* Exported by fib_hash.c */
231 extern struct fib_table *fib_hash_init(u32 id);
232 
233 #ifdef CONFIG_IP_MULTIPLE_TABLES
234 extern void __init fib4_rules_init(void);
235 
236 #ifdef CONFIG_NET_CLS_ROUTE
237 extern u32 fib_rules_tclass(struct fib_result *res);
238 #endif
239 
240 #endif
241 
242 static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
243 {
244 #ifdef CONFIG_NET_CLS_ROUTE
245 #ifdef CONFIG_IP_MULTIPLE_TABLES
246 	u32 rtag;
247 #endif
248 	*itag = FIB_RES_NH(*res).nh_tclassid<<16;
249 #ifdef CONFIG_IP_MULTIPLE_TABLES
250 	rtag = fib_rules_tclass(res);
251 	if (*itag == 0)
252 		*itag = (rtag<<16);
253 	*itag |= (rtag>>16);
254 #endif
255 #endif
256 }
257 
258 extern void free_fib_info(struct fib_info *fi);
259 
260 static inline void fib_info_put(struct fib_info *fi)
261 {
262 	if (atomic_dec_and_test(&fi->fib_clntref))
263 		free_fib_info(fi);
264 }
265 
266 static inline void fib_res_put(struct fib_result *res)
267 {
268 	if (res->fi)
269 		fib_info_put(res->fi);
270 #ifdef CONFIG_IP_MULTIPLE_TABLES
271 	if (res->r)
272 		fib_rule_put(res->r);
273 #endif
274 }
275 
276 #ifdef CONFIG_PROC_FS
277 extern int  fib_proc_init(void);
278 extern void fib_proc_exit(void);
279 #endif
280 
281 #endif  /* _NET_FIB_H */
282