[IPV4]: fib_validate_source() annotations
[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 struct nla_policy rtm_ipv4_policy[];
220 extern void             ip_fib_init(void);
221 extern int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
222 extern int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
223 extern int inet_rtm_getroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
224 extern int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb);
225 extern int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
226                                struct net_device *dev, __be32 *spec_dst, u32 *itag);
227 extern void fib_select_multipath(const struct flowi *flp, struct fib_result *res);
228
229 struct rtentry;
230
231 /* Exported by fib_semantics.c */
232 extern int ip_fib_check_default(u32 gw, struct net_device *dev);
233 extern int fib_sync_down(u32 local, struct net_device *dev, int force);
234 extern int fib_sync_up(struct net_device *dev);
235 extern u32  __fib_res_prefsrc(struct fib_result *res);
236
237 /* Exported by fib_hash.c */
238 extern struct fib_table *fib_hash_init(u32 id);
239
240 #ifdef CONFIG_IP_MULTIPLE_TABLES
241 extern int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb);
242
243 extern void __init fib4_rules_init(void);
244
245 #ifdef CONFIG_NET_CLS_ROUTE
246 extern u32 fib_rules_tclass(struct fib_result *res);
247 #endif
248
249 #endif
250
251 static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
252 {
253 #ifdef CONFIG_NET_CLS_ROUTE
254 #ifdef CONFIG_IP_MULTIPLE_TABLES
255         u32 rtag;
256 #endif
257         *itag = FIB_RES_NH(*res).nh_tclassid<<16;
258 #ifdef CONFIG_IP_MULTIPLE_TABLES
259         rtag = fib_rules_tclass(res);
260         if (*itag == 0)
261                 *itag = (rtag<<16);
262         *itag |= (rtag>>16);
263 #endif
264 #endif
265 }
266
267 extern void free_fib_info(struct fib_info *fi);
268
269 static inline void fib_info_put(struct fib_info *fi)
270 {
271         if (atomic_dec_and_test(&fi->fib_clntref))
272                 free_fib_info(fi);
273 }
274
275 static inline void fib_res_put(struct fib_result *res)
276 {
277         if (res->fi)
278                 fib_info_put(res->fi);
279 #ifdef CONFIG_IP_MULTIPLE_TABLES
280         if (res->r)
281                 fib_rule_put(res->r);
282 #endif
283 }
284
285 #ifdef CONFIG_PROC_FS
286 extern int  fib_proc_init(void);
287 extern void fib_proc_exit(void);
288 #endif
289
290 #endif  /* _NET_FIB_H */