ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 <linux/config.h>
20 #include <net/flow.h>
21 #include <linux/seq_file.h>
22
23 /* WARNING: The ordering of these elements must match ordering
24  *          of RTA_* rtnetlink attribute numbers.
25  */
26 struct kern_rta
27 {
28         void            *rta_dst;
29         void            *rta_src;
30         int             *rta_iif;
31         int             *rta_oif;
32         void            *rta_gw;
33         u32             *rta_priority;
34         void            *rta_prefsrc;
35         struct rtattr   *rta_mx;
36         struct rtattr   *rta_mp;
37         unsigned char   *rta_protoinfo;
38         u32             *rta_flow;
39         struct rta_cacheinfo *rta_ci;
40         struct rta_session *rta_sess;
41 };
42
43 struct fib_nh
44 {
45         struct net_device               *nh_dev;
46         unsigned                nh_flags;
47         unsigned char           nh_scope;
48 #ifdef CONFIG_IP_ROUTE_MULTIPATH
49         int                     nh_weight;
50         int                     nh_power;
51 #endif
52 #ifdef CONFIG_NET_CLS_ROUTE
53         __u32                   nh_tclassid;
54 #endif
55         int                     nh_oif;
56         u32                     nh_gw;
57 };
58
59 /*
60  * This structure contains data shared by many of routes.
61  */
62
63 struct fib_info
64 {
65         struct fib_info         *fib_next;
66         struct fib_info         *fib_prev;
67         int                     fib_treeref;
68         atomic_t                fib_clntref;
69         int                     fib_dead;
70         unsigned                fib_flags;
71         int                     fib_protocol;
72         u32                     fib_prefsrc;
73         u32                     fib_priority;
74         u32                     fib_metrics[RTAX_MAX];
75 #define fib_mtu fib_metrics[RTAX_MTU-1]
76 #define fib_window fib_metrics[RTAX_WINDOW-1]
77 #define fib_rtt fib_metrics[RTAX_RTT-1]
78 #define fib_advmss fib_metrics[RTAX_ADVMSS-1]
79         int                     fib_nhs;
80 #ifdef CONFIG_IP_ROUTE_MULTIPATH
81         int                     fib_power;
82 #endif
83         struct fib_nh           fib_nh[0];
84 #define fib_dev         fib_nh[0].nh_dev
85 };
86
87
88 #ifdef CONFIG_IP_MULTIPLE_TABLES
89 struct fib_rule;
90 #endif
91
92 struct fib_result
93 {
94         unsigned char   prefixlen;
95         unsigned char   nh_sel;
96         unsigned char   type;
97         unsigned char   scope;
98         struct fib_info *fi;
99 #ifdef CONFIG_IP_MULTIPLE_TABLES
100         struct fib_rule *r;
101 #endif
102 };
103
104
105 #ifdef CONFIG_IP_ROUTE_MULTIPATH
106
107 #define FIB_RES_NH(res)         ((res).fi->fib_nh[(res).nh_sel])
108 #define FIB_RES_RESET(res)      ((res).nh_sel = 0)
109
110 #else /* CONFIG_IP_ROUTE_MULTIPATH */
111
112 #define FIB_RES_NH(res)         ((res).fi->fib_nh[0])
113 #define FIB_RES_RESET(res)
114
115 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
116
117 #define FIB_RES_PREFSRC(res)            ((res).fi->fib_prefsrc ? : __fib_res_prefsrc(&res))
118 #define FIB_RES_GW(res)                 (FIB_RES_NH(res).nh_gw)
119 #define FIB_RES_DEV(res)                (FIB_RES_NH(res).nh_dev)
120 #define FIB_RES_OIF(res)                (FIB_RES_NH(res).nh_oif)
121
122 struct fib_table
123 {
124         unsigned char   tb_id;
125         unsigned        tb_stamp;
126         int             (*tb_lookup)(struct fib_table *tb, const struct flowi *flp, struct fib_result *res);
127         int             (*tb_insert)(struct fib_table *table, struct rtmsg *r,
128                                      struct kern_rta *rta, struct nlmsghdr *n,
129                                      struct netlink_skb_parms *req);
130         int             (*tb_delete)(struct fib_table *table, struct rtmsg *r,
131                                      struct kern_rta *rta, struct nlmsghdr *n,
132                                      struct netlink_skb_parms *req);
133         int             (*tb_dump)(struct fib_table *table, struct sk_buff *skb,
134                                      struct netlink_callback *cb);
135         int             (*tb_flush)(struct fib_table *table);
136         void            (*tb_select_default)(struct fib_table *table,
137                                              const struct flowi *flp, struct fib_result *res);
138
139         unsigned char   tb_data[0];
140 };
141
142 #ifndef CONFIG_IP_MULTIPLE_TABLES
143
144 extern struct fib_table *ip_fib_local_table;
145 extern struct fib_table *ip_fib_main_table;
146
147 static inline struct fib_table *fib_get_table(int id)
148 {
149         if (id != RT_TABLE_LOCAL)
150                 return ip_fib_main_table;
151         return ip_fib_local_table;
152 }
153
154 static inline struct fib_table *fib_new_table(int id)
155 {
156         return fib_get_table(id);
157 }
158
159 static inline int fib_lookup(const struct flowi *flp, struct fib_result *res)
160 {
161         if (ip_fib_local_table->tb_lookup(ip_fib_local_table, flp, res) &&
162             ip_fib_main_table->tb_lookup(ip_fib_main_table, flp, res))
163                 return -ENETUNREACH;
164         return 0;
165 }
166
167 static inline void fib_select_default(const struct flowi *flp, struct fib_result *res)
168 {
169         if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
170                 ip_fib_main_table->tb_select_default(ip_fib_main_table, flp, res);
171 }
172
173 #else /* CONFIG_IP_MULTIPLE_TABLES */
174 #define ip_fib_local_table (fib_tables[RT_TABLE_LOCAL])
175 #define ip_fib_main_table (fib_tables[RT_TABLE_MAIN])
176
177 extern struct fib_table * fib_tables[RT_TABLE_MAX+1];
178 extern int fib_lookup(const struct flowi *flp, struct fib_result *res);
179 extern struct fib_table *__fib_new_table(int id);
180 extern void fib_rule_put(struct fib_rule *r);
181
182 static inline struct fib_table *fib_get_table(int id)
183 {
184         if (id == 0)
185                 id = RT_TABLE_MAIN;
186
187         return fib_tables[id];
188 }
189
190 static inline struct fib_table *fib_new_table(int id)
191 {
192         if (id == 0)
193                 id = RT_TABLE_MAIN;
194
195         return fib_tables[id] ? : __fib_new_table(id);
196 }
197
198 extern void fib_select_default(const struct flowi *flp, struct fib_result *res);
199
200 #endif /* CONFIG_IP_MULTIPLE_TABLES */
201
202 /* Exported by fib_frontend.c */
203 extern void             ip_fib_init(void);
204 extern void             fib_flush(void);
205 extern int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
206 extern int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
207 extern int inet_rtm_getroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
208 extern int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb);
209 extern int fib_validate_source(u32 src, u32 dst, u8 tos, int oif,
210                                struct net_device *dev, u32 *spec_dst, u32 *itag);
211 extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
212
213 /* Exported by fib_semantics.c */
214 extern int              ip_fib_check_default(u32 gw, struct net_device *dev);
215 extern void             fib_release_info(struct fib_info *);
216 extern int              fib_semantic_match(int type, struct fib_info *,
217                                            const struct flowi *, struct fib_result*);
218 extern struct fib_info  *fib_create_info(const struct rtmsg *r, struct kern_rta *rta,
219                                          const struct nlmsghdr *, int *err);
220 extern int fib_nh_match(struct rtmsg *r, struct nlmsghdr *, struct kern_rta *rta, struct fib_info *fi);
221 extern int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
222                          u8 tb_id, u8 type, u8 scope, void *dst, int dst_len, u8 tos,
223                          struct fib_info *fi);
224 extern int fib_sync_down(u32 local, struct net_device *dev, int force);
225 extern int fib_sync_up(struct net_device *dev);
226 extern int fib_convert_rtentry(int cmd, struct nlmsghdr *nl, struct rtmsg *rtm,
227                                struct kern_rta *rta, struct rtentry *r);
228 extern void fib_node_seq_show(struct seq_file *seq, int type, int dead,
229                               struct fib_info *fi, u32 prefix, u32 mask);
230 extern u32  __fib_res_prefsrc(struct fib_result *res);
231
232 /* Exported by fib_hash.c */
233 extern struct fib_table *fib_hash_init(int id);
234
235 #ifdef CONFIG_IP_MULTIPLE_TABLES
236 /* Exported by fib_rules.c */
237
238 extern int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
239 extern int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
240 extern int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb);
241 extern u32 fib_rules_map_destination(u32 daddr, struct fib_result *res);
242 #ifdef CONFIG_NET_CLS_ROUTE
243 extern u32 fib_rules_tclass(struct fib_result *res);
244 #endif
245 extern u32 fib_rules_policy(u32 saddr, struct fib_result *res, unsigned *flags);
246 extern void fib_rules_init(void);
247 #endif
248
249 static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
250 {
251 #ifdef CONFIG_NET_CLS_ROUTE
252 #ifdef CONFIG_IP_MULTIPLE_TABLES
253         u32 rtag;
254 #endif
255         *itag = FIB_RES_NH(*res).nh_tclassid<<16;
256 #ifdef CONFIG_IP_MULTIPLE_TABLES
257         rtag = fib_rules_tclass(res);
258         if (*itag == 0)
259                 *itag = (rtag<<16);
260         *itag |= (rtag>>16);
261 #endif
262 #endif
263 }
264
265 extern void free_fib_info(struct fib_info *fi);
266
267 static inline void fib_info_put(struct fib_info *fi)
268 {
269         if (atomic_dec_and_test(&fi->fib_clntref))
270                 free_fib_info(fi);
271 }
272
273 static inline void fib_res_put(struct fib_result *res)
274 {
275         if (res->fi)
276                 fib_info_put(res->fi);
277 #ifdef CONFIG_IP_MULTIPLE_TABLES
278         if (res->r)
279                 fib_rule_put(res->r);
280 #endif
281 }
282
283 #endif  /* _NET_FIB_H */