fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / include / net / route.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 IP router.
7  *
8  * Version:     @(#)route.h     1.0.4   05/27/93
9  *
10  * Authors:     Ross Biro
11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12  * Fixes:
13  *              Alan Cox        :       Reformatted. Added ip_rt_local()
14  *              Alan Cox        :       Support for TCP parameters.
15  *              Alexey Kuznetsov:       Major changes for new routing code.
16  *              Mike McLagan    :       Routing by source
17  *              Robert Olsson   :       Added rt_cache statistics
18  *
19  *              This program is free software; you can redistribute it and/or
20  *              modify it under the terms of the GNU General Public License
21  *              as published by the Free Software Foundation; either version
22  *              2 of the License, or (at your option) any later version.
23  */
24 #ifndef _ROUTE_H
25 #define _ROUTE_H
26
27 #include <net/dst.h>
28 #include <net/inetpeer.h>
29 #include <net/flow.h>
30 #include <net/inet_sock.h>
31 #include <linux/in_route.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/route.h>
34 #include <linux/ip.h>
35 #include <linux/cache.h>
36 #include <linux/security.h>
37 #include <linux/vs_base.h>
38 #include <linux/vs_network.h>
39 #include <linux/in.h>
40
41 #ifndef __KERNEL__
42 #warning This file is not supposed to be used outside of kernel.
43 #endif
44
45 #define RTO_ONLINK      0x01
46
47 #define RTO_CONN        0
48 /* RTO_CONN is not used (being alias for 0), but preserved not to break
49  * some modules referring to it. */
50
51 #define RT_CONN_FLAGS(sk)   (RT_TOS(inet_sk(sk)->tos) | sock_flag(sk, SOCK_LOCALROUTE))
52
53 struct fib_nh;
54 struct inet_peer;
55 struct rtable
56 {
57         union
58         {
59                 struct dst_entry        dst;
60                 struct rtable           *rt_next;
61         } u;
62
63         struct in_device        *idev;
64         
65         unsigned                rt_flags;
66         __u16                   rt_type;
67         __u16                   rt_multipath_alg;
68
69         __be32                  rt_dst; /* Path destination     */
70         __be32                  rt_src; /* Path source          */
71         int                     rt_iif;
72
73         /* Info on neighbour */
74         __be32                  rt_gateway;
75
76         /* Cache lookup keys */
77         struct flowi            fl;
78
79         /* Miscellaneous cached information */
80         __be32                  rt_spec_dst; /* RFC1122 specific destination */
81         struct inet_peer        *peer; /* long-living peer info */
82 };
83
84 struct ip_rt_acct
85 {
86         __u32   o_bytes;
87         __u32   o_packets;
88         __u32   i_bytes;
89         __u32   i_packets;
90 };
91
92 struct rt_cache_stat 
93 {
94         unsigned int in_hit;
95         unsigned int in_slow_tot;
96         unsigned int in_slow_mc;
97         unsigned int in_no_route;
98         unsigned int in_brd;
99         unsigned int in_martian_dst;
100         unsigned int in_martian_src;
101         unsigned int out_hit;
102         unsigned int out_slow_tot;
103         unsigned int out_slow_mc;
104         unsigned int gc_total;
105         unsigned int gc_ignored;
106         unsigned int gc_goal_miss;
107         unsigned int gc_dst_overflow;
108         unsigned int in_hlist_search;
109         unsigned int out_hlist_search;
110 };
111
112 extern struct ip_rt_acct *ip_rt_acct;
113
114 struct in_device;
115 extern int              ip_rt_init(void);
116 extern void             ip_rt_redirect(__be32 old_gw, __be32 dst, __be32 new_gw,
117                                        __be32 src, struct net_device *dev);
118 extern void             ip_rt_advice(struct rtable **rp, int advice);
119 extern void             rt_cache_flush(int how);
120 extern int              __ip_route_output_key(struct rtable **, const struct flowi *flp);
121 extern int              ip_route_output_key(struct rtable **, struct flowi *flp);
122 extern int              ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk, int flags);
123 extern int              ip_route_input(struct sk_buff*, __be32 dst, __be32 src, u8 tos, struct net_device *devin);
124 extern unsigned short   ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu);
125 extern void             ip_rt_send_redirect(struct sk_buff *skb);
126
127 extern unsigned         inet_addr_type(__be32 addr);
128 extern void             ip_rt_multicast_event(struct in_device *);
129 extern int              ip_rt_ioctl(unsigned int cmd, void __user *arg);
130 extern void             ip_rt_get_source(u8 *src, struct rtable *rt);
131 extern int              ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb);
132
133 struct in_ifaddr;
134 extern void fib_add_ifaddr(struct in_ifaddr *);
135
136 static inline void ip_rt_put(struct rtable * rt)
137 {
138         if (rt)
139                 dst_release(&rt->u.dst);
140 }
141
142 #define IPTOS_RT_MASK   (IPTOS_TOS_MASK & ~3)
143
144 extern __u8 ip_tos2prio[16];
145
146 static inline char rt_tos2priority(u8 tos)
147 {
148         return ip_tos2prio[IPTOS_TOS(tos)>>1];
149 }
150
151 #define IPI_LOOPBACK    htonl(INADDR_LOOPBACK)
152
153 static inline int ip_find_src(struct nx_info *nxi, struct rtable **rp, struct flowi *fl)
154 {
155         int err;
156         int i, n = nxi->nbipv4;
157         u32 ipv4root = nxi->ipv4[0];
158
159         if (ipv4root == 0)
160                 return 0;
161
162         if (fl->fl4_src == 0) {
163                 if (n > 1) {
164                         u32 foundsrc;
165
166                         err = __ip_route_output_key(rp, fl);
167                         if (err) {
168                                 fl->fl4_src = ipv4root;
169                                 err = __ip_route_output_key(rp, fl);
170                         }
171                         if (err)
172                                 return err;
173
174                         foundsrc = (*rp)->rt_src;
175                         ip_rt_put(*rp);
176
177                         for (i=0; i<n; i++){
178                                 u32 mask = nxi->mask[i];
179                                 u32 ipv4 = nxi->ipv4[i];
180                                 u32 net4 = ipv4 & mask;
181
182                                 if (foundsrc == ipv4) {
183                                         fl->fl4_src = ipv4;
184                                         break;
185                                 }
186                                 if (!fl->fl4_src && (foundsrc & mask) == net4)
187                                         fl->fl4_src = ipv4;
188                         }
189                 }
190                 if (fl->fl4_src == 0)
191                         fl->fl4_src = (fl->fl4_dst == IPI_LOOPBACK)
192                                 ? IPI_LOOPBACK : ipv4root;
193         } else {
194                 for (i=0; i<n; i++) {
195                         if (nxi->ipv4[i] == fl->fl4_src)
196                                 break;
197                 }
198                 if (i == n)
199                         return -EPERM;
200         }
201         return 0;
202 }
203
204 static inline int ip_route_connect(struct rtable **rp, __be32 dst,
205                                    __be32 src, u32 tos, int oif, u8 protocol,
206                                    __be16 sport, __be16 dport, struct sock *sk)
207 {
208         struct flowi fl = { .oif = oif,
209                             .nl_u = { .ip4_u = { .daddr = dst,
210                                                  .saddr = src,
211                                                  .tos   = tos } },
212                             .proto = protocol,
213                             .uli_u = { .ports =
214                                        { .sport = sport,
215                                          .dport = dport } } };
216
217         int err;
218         struct nx_info *nx_info = current->nx_info;
219
220         if (sk)
221                 nx_info = sk->sk_nx_info;
222         vxdprintk(VXD_CBIT(net, 4),
223                 "ip_route_connect(%p) %p,%p;%lx",
224                 sk, nx_info, sk->sk_socket,
225                 (sk->sk_socket?sk->sk_socket->flags:0));
226
227         if (nx_info) {
228                 err = ip_find_src(nx_info, rp, &fl);
229                 if (err)
230                         return err;
231                 if (fl.fl4_dst == IPI_LOOPBACK && !nx_check(0, VS_ADMIN))
232                         fl.fl4_dst = nx_info->ipv4[0];
233 #ifdef CONFIG_VSERVER_REMAP_SADDR
234                 if (fl.fl4_src == IPI_LOOPBACK && !nx_check(0, VS_ADMIN))
235                         fl.fl4_src = nx_info->ipv4[0];
236 #endif
237         }
238         if (!fl.fl4_dst || !fl.fl4_src) {
239                 err = __ip_route_output_key(rp, &fl);
240                 if (err)
241                         return err;
242                 fl.fl4_dst = (*rp)->rt_dst;
243                 fl.fl4_src = (*rp)->rt_src;
244                 ip_rt_put(*rp);
245                 *rp = NULL;
246         }
247         security_sk_classify_flow(sk, &fl);
248         return ip_route_output_flow(rp, &fl, sk, 0);
249 }
250
251 static inline int ip_route_newports(struct rtable **rp, u8 protocol,
252                                     __be16 sport, __be16 dport, struct sock *sk)
253 {
254         if (sport != (*rp)->fl.fl_ip_sport ||
255             dport != (*rp)->fl.fl_ip_dport) {
256                 struct flowi fl;
257
258                 memcpy(&fl, &(*rp)->fl, sizeof(fl));
259                 fl.fl_ip_sport = sport;
260                 fl.fl_ip_dport = dport;
261                 fl.proto = protocol;
262                 ip_rt_put(*rp);
263                 *rp = NULL;
264                 security_sk_classify_flow(sk, &fl);
265                 return ip_route_output_flow(rp, &fl, sk, 0);
266         }
267         return 0;
268 }
269
270 extern void rt_bind_peer(struct rtable *rt, int create);
271
272 static inline struct inet_peer *rt_get_peer(struct rtable *rt)
273 {
274         if (rt->peer)
275                 return rt->peer;
276
277         rt_bind_peer(rt, 0);
278         return rt->peer;
279 }
280
281 extern ctl_table ipv4_route_table[];
282
283 #endif  /* _ROUTE_H */