[IPv4]: FIB configuration using struct fib_config
[pandora-kernel.git] / include / net / ip_fib.h
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_family;
25         u8                      fc_dst_len;
26         u8                      fc_src_len;
27         u8                      fc_tos;
28         u8                      fc_protocol;
29         u8                      fc_scope;
30         u8                      fc_type;
31         /* 1 byte unused */
32         u32                     fc_table;
33         u32                     fc_dst;
34         u32                     fc_src;
35         u32                     fc_gw;
36         int                     fc_oif;
37         u32                     fc_flags;
38         u32                     fc_priority;
39         u32                     fc_prefsrc;
40         struct nlattr           *fc_mx;
41         struct rtnexthop        *fc_mp;
42         int                     fc_mx_len;
43         int                     fc_mp_len;
44         u32                     fc_flow;
45         u32                     fc_mp_alg;
46         u32                     fc_nlflags;
47         struct nl_info          fc_nlinfo;
48  };
49
50 struct fib_info;
51
52 struct fib_nh {
53         struct net_device       *nh_dev;
54         struct hlist_node       nh_hash;
55         struct fib_info         *nh_parent;
56         unsigned                nh_flags;
57         unsigned char           nh_scope;
58 #ifdef CONFIG_IP_ROUTE_MULTIPATH
59         int                     nh_weight;
60         int                     nh_power;
61 #endif
62 #ifdef CONFIG_NET_CLS_ROUTE
63         __u32                   nh_tclassid;
64 #endif
65         int                     nh_oif;
66         u32                     nh_gw;
67 };
68
69 /*
70  * This structure contains data shared by many of routes.
71  */
72
73 struct fib_info {
74         struct hlist_node       fib_hash;
75         struct hlist_node       fib_lhash;
76         int                     fib_treeref;
77         atomic_t                fib_clntref;
78         int                     fib_dead;
79         unsigned                fib_flags;
80         int                     fib_protocol;
81         u32                     fib_prefsrc;
82         u32                     fib_priority;
83         u32                     fib_metrics[RTAX_MAX];
84 #define fib_mtu fib_metrics[RTAX_MTU-1]
85 #define fib_window fib_metrics[RTAX_WINDOW-1]
86 #define fib_rtt fib_metrics[RTAX_RTT-1]
87 #define fib_advmss fib_metrics[RTAX_ADVMSS-1]
88         int                     fib_nhs;
89 #ifdef CONFIG_IP_ROUTE_MULTIPATH
90         int                     fib_power;
91 #endif
92 #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
93         u32                     fib_mp_alg;
94 #endif
95         struct fib_nh           fib_nh[0];
96 #define fib_dev         fib_nh[0].nh_dev
97 };
98
99
100 #ifdef CONFIG_IP_MULTIPLE_TABLES
101 struct fib_rule;
102 #endif
103
104 struct fib_result {
105         unsigned char   prefixlen;
106         unsigned char   nh_sel;
107         unsigned char   type;
108         unsigned char   scope;
109 #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
110         __u32           network;
111         __u32           netmask;
112 #endif
113         struct fib_info *fi;
114 #ifdef CONFIG_IP_MULTIPLE_TABLES
115         struct fib_rule *r;
116 #endif
117 };
118
119 struct fib_result_nl {
120         u32             fl_addr;   /* To be looked up*/ 
121         u32             fl_fwmark; 
122         unsigned char   fl_tos;
123         unsigned char   fl_scope;
124         unsigned char   tb_id_in;
125
126         unsigned char   tb_id;      /* Results */
127         unsigned char   prefixlen;
128         unsigned char   nh_sel;
129         unsigned char   type;
130         unsigned char   scope;
131         int             err;      
132 };
133
134 #ifdef CONFIG_IP_ROUTE_MULTIPATH
135
136 #define FIB_RES_NH(res)         ((res).fi->fib_nh[(res).nh_sel])
137 #define FIB_RES_RESET(res)      ((res).nh_sel = 0)
138
139 #else /* CONFIG_IP_ROUTE_MULTIPATH */
140
141 #define FIB_RES_NH(res)         ((res).fi->fib_nh[0])
142 #define FIB_RES_RESET(res)
143
144 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
145
146 #define FIB_RES_PREFSRC(res)            ((res).fi->fib_prefsrc ? : __fib_res_prefsrc(&res))
147 #define FIB_RES_GW(res)                 (FIB_RES_NH(res).nh_gw)
148 #define FIB_RES_DEV(res)                (FIB_RES_NH(res).nh_dev)
149 #define FIB_RES_OIF(res)                (FIB_RES_NH(res).nh_oif)
150
151 #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
152 #define FIB_RES_NETWORK(res)            ((res).network)
153 #define FIB_RES_NETMASK(res)            ((res).netmask)
154 #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
155 #define FIB_RES_NETWORK(res)            (0)
156 #define FIB_RES_NETMASK(res)            (0)
157 #endif /* CONFIG_IP_ROUTE_MULTIPATH_WRANDOM */
158
159 struct fib_table {
160         struct hlist_node tb_hlist;
161         u32             tb_id;
162         unsigned        tb_stamp;
163         int             (*tb_lookup)(struct fib_table *tb, const struct flowi *flp, struct fib_result *res);
164         int             (*tb_insert)(struct fib_table *, struct fib_config *);
165         int             (*tb_delete)(struct fib_table *, struct fib_config *);
166         int             (*tb_dump)(struct fib_table *table, struct sk_buff *skb,
167                                      struct netlink_callback *cb);
168         int             (*tb_flush)(struct fib_table *table);
169         void            (*tb_select_default)(struct fib_table *table,
170                                              const struct flowi *flp, struct fib_result *res);
171
172         unsigned char   tb_data[0];
173 };
174
175 #ifndef CONFIG_IP_MULTIPLE_TABLES
176
177 extern struct fib_table *ip_fib_local_table;
178 extern struct fib_table *ip_fib_main_table;
179
180 static inline struct fib_table *fib_get_table(u32 id)
181 {
182         if (id != RT_TABLE_LOCAL)
183                 return ip_fib_main_table;
184         return ip_fib_local_table;
185 }
186
187 static inline struct fib_table *fib_new_table(u32 id)
188 {
189         return fib_get_table(id);
190 }
191
192 static inline int fib_lookup(const struct flowi *flp, struct fib_result *res)
193 {
194         if (ip_fib_local_table->tb_lookup(ip_fib_local_table, flp, res) &&
195             ip_fib_main_table->tb_lookup(ip_fib_main_table, flp, res))
196                 return -ENETUNREACH;
197         return 0;
198 }
199
200 static inline void fib_select_default(const struct flowi *flp, struct fib_result *res)
201 {
202         if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
203                 ip_fib_main_table->tb_select_default(ip_fib_main_table, flp, res);
204 }
205
206 #else /* CONFIG_IP_MULTIPLE_TABLES */
207 #define ip_fib_local_table fib_get_table(RT_TABLE_LOCAL)
208 #define ip_fib_main_table fib_get_table(RT_TABLE_MAIN)
209
210 extern int fib_lookup(struct flowi *flp, struct fib_result *res);
211
212 extern struct fib_table *fib_new_table(u32 id);
213 extern struct fib_table *fib_get_table(u32 id);
214 extern void fib_select_default(const struct flowi *flp, struct fib_result *res);
215
216 #endif /* CONFIG_IP_MULTIPLE_TABLES */
217
218 /* Exported by fib_frontend.c */
219 extern void             ip_fib_init(void);
220 extern int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
221 extern int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
222 extern int inet_rtm_getroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
223 extern int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb);
224 extern int fib_validate_source(u32 src, u32 dst, u8 tos, int oif,
225                                struct net_device *dev, u32 *spec_dst, u32 *itag);
226 extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
227
228 struct rtentry;
229
230 /* Exported by fib_semantics.c */
231 extern int ip_fib_check_default(u32 gw, struct net_device *dev);
232 extern int fib_sync_down(u32 local, struct net_device *dev, int force);
233 extern int fib_sync_up(struct net_device *dev);
234 extern u32  __fib_res_prefsrc(struct fib_result *res);
235
236 /* Exported by fib_hash.c */
237 extern struct fib_table *fib_hash_init(u32 id);
238
239 #ifdef CONFIG_IP_MULTIPLE_TABLES
240 extern int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb);
241
242 extern void __init fib4_rules_init(void);
243
244 #ifdef CONFIG_NET_CLS_ROUTE
245 extern u32 fib_rules_tclass(struct fib_result *res);
246 #endif
247
248 #endif
249
250 static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
251 {
252 #ifdef CONFIG_NET_CLS_ROUTE
253 #ifdef CONFIG_IP_MULTIPLE_TABLES
254         u32 rtag;
255 #endif
256         *itag = FIB_RES_NH(*res).nh_tclassid<<16;
257 #ifdef CONFIG_IP_MULTIPLE_TABLES
258         rtag = fib_rules_tclass(res);
259         if (*itag == 0)
260                 *itag = (rtag<<16);
261         *itag |= (rtag>>16);
262 #endif
263 #endif
264 }
265
266 extern void free_fib_info(struct fib_info *fi);
267
268 static inline void fib_info_put(struct fib_info *fi)
269 {
270         if (atomic_dec_and_test(&fi->fib_clntref))
271                 free_fib_info(fi);
272 }
273
274 static inline void fib_res_put(struct fib_result *res)
275 {
276         if (res->fi)
277                 fib_info_put(res->fi);
278 #ifdef CONFIG_IP_MULTIPLE_TABLES
279         if (res->r)
280                 fib_rule_put(res->r);
281 #endif
282 }
283
284 #ifdef CONFIG_PROC_FS
285 extern int  fib_proc_init(void);
286 extern void fib_proc_exit(void);
287 #endif
288
289 #endif  /* _NET_FIB_H */