blob: 949b932d2f08c8f72017f8d61ce1f773192f55e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/flow.h>
20#include <linux/seq_file.h>
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070021#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Thomas Graf4e902c52006-08-17 18:14:52 -070023struct fib_config {
Thomas Graf4e902c52006-08-17 18:14:52 -070024 u8 fc_dst_len;
Thomas Graf4e902c52006-08-17 18:14:52 -070025 u8 fc_tos;
26 u8 fc_protocol;
27 u8 fc_scope;
28 u8 fc_type;
Thomas Grafb52f0702006-10-18 20:26:36 -070029 /* 3 bytes unused */
Thomas Graf4e902c52006-08-17 18:14:52 -070030 u32 fc_table;
Al Viro6d85c102006-09-26 22:15:46 -070031 __be32 fc_dst;
Al Viro6d85c102006-09-26 22:15:46 -070032 __be32 fc_gw;
Thomas Graf4e902c52006-08-17 18:14:52 -070033 int fc_oif;
34 u32 fc_flags;
35 u32 fc_priority;
Al Viro6d85c102006-09-26 22:15:46 -070036 __be32 fc_prefsrc;
Thomas Graf4e902c52006-08-17 18:14:52 -070037 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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47struct fib_info;
48
49struct 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;
Al Viroed49e3c2006-09-26 22:13:54 -070063 __be32 nh_gw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66/*
67 * This structure contains data shared by many of routes.
68 */
69
70struct 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;
Al Virob83738a2006-09-26 22:14:15 -070078 __be32 fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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
98struct fib_rule;
99#endif
100
101struct 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
Al Viro80e856e2006-09-26 22:19:36 -0700107 __be32 network;
108 __be32 netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
110 struct fib_info *fi;
111#ifdef CONFIG_IP_MULTIPLE_TABLES
112 struct fib_rule *r;
113#endif
114};
115
Robert Olsson246955f2005-06-20 13:36:39 -0700116struct fib_result_nl {
Al Viro80e856e2006-09-26 22:19:36 -0700117 __be32 fl_addr; /* To be looked up*/
Robert Olsson246955f2005-06-20 13:36:39 -0700118 u32 fl_fwmark;
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};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
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
156struct fib_table {
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700157 struct hlist_node tb_hlist;
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700158 u32 tb_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 unsigned tb_stamp;
160 int (*tb_lookup)(struct fib_table *tb, const struct flowi *flp, struct fib_result *res);
Thomas Graf4e902c52006-08-17 18:14:52 -0700161 int (*tb_insert)(struct fib_table *, struct fib_config *);
162 int (*tb_delete)(struct fib_table *, struct fib_config *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 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
174extern struct fib_table *ip_fib_local_table;
175extern struct fib_table *ip_fib_main_table;
176
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700177static inline struct fib_table *fib_get_table(u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 if (id != RT_TABLE_LOCAL)
180 return ip_fib_main_table;
181 return ip_fib_local_table;
182}
183
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700184static inline struct fib_table *fib_new_table(u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 return fib_get_table(id);
187}
188
189static 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
197static 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 */
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700204#define ip_fib_local_table fib_get_table(RT_TABLE_LOCAL)
205#define ip_fib_main_table fib_get_table(RT_TABLE_MAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700207extern int fib_lookup(struct flowi *flp, struct fib_result *res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700209extern struct fib_table *fib_new_table(u32 id);
210extern struct fib_table *fib_get_table(u32 id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211extern 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 */
Thomas Grafd889ce32006-08-17 18:15:44 -0700216extern struct nla_policy rtm_ipv4_policy[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217extern void ip_fib_init(void);
218extern int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
219extern int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
220extern int inet_rtm_getroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
221extern int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb);
Al Virod9c9df82006-09-26 21:28:14 -0700222extern int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
223 struct net_device *dev, __be32 *spec_dst, u32 *itag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
225
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200226struct rtentry;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* Exported by fib_semantics.c */
Al Virod878e72e2006-09-26 22:18:13 -0700229extern int ip_fib_check_default(__be32 gw, struct net_device *dev);
Al Viro81f7bf62006-09-27 18:40:00 -0700230extern int fib_sync_down(__be32 local, struct net_device *dev, int force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231extern int fib_sync_up(struct net_device *dev);
Al Virob83738a2006-09-26 22:14:15 -0700232extern __be32 __fib_res_prefsrc(struct fib_result *res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/* Exported by fib_hash.c */
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700235extern struct fib_table *fib_hash_init(u32 id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237#ifdef CONFIG_IP_MULTIPLE_TABLES
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700238extern int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700240extern void __init fib4_rules_init(void);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#ifdef CONFIG_NET_CLS_ROUTE
243extern u32 fib_rules_tclass(struct fib_result *res);
244#endif
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#endif
247
248static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
249{
250#ifdef CONFIG_NET_CLS_ROUTE
251#ifdef CONFIG_IP_MULTIPLE_TABLES
252 u32 rtag;
253#endif
254 *itag = FIB_RES_NH(*res).nh_tclassid<<16;
255#ifdef CONFIG_IP_MULTIPLE_TABLES
256 rtag = fib_rules_tclass(res);
257 if (*itag == 0)
258 *itag = (rtag<<16);
259 *itag |= (rtag>>16);
260#endif
261#endif
262}
263
264extern void free_fib_info(struct fib_info *fi);
265
266static inline void fib_info_put(struct fib_info *fi)
267{
268 if (atomic_dec_and_test(&fi->fib_clntref))
269 free_fib_info(fi);
270}
271
272static inline void fib_res_put(struct fib_result *res)
273{
274 if (res->fi)
275 fib_info_put(res->fi);
276#ifdef CONFIG_IP_MULTIPLE_TABLES
277 if (res->r)
278 fib_rule_put(res->r);
279#endif
280}
281
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300282#ifdef CONFIG_PROC_FS
283extern int fib_proc_init(void);
284extern void fib_proc_exit(void);
285#endif
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#endif /* _NET_FIB_H */