8e47264473ff31691d2c49f05f984b20a40dfb47
[openwrt/staging/florian.git] / target / linux / generic-2.6 / patches-2.6.22 / 201-multiple_default_gateways.patch
1 Index: linux-2.6.22.19/include/linux/rtnetlink.h
2 ===================================================================
3 --- linux-2.6.22.19.orig/include/linux/rtnetlink.h
4 +++ linux-2.6.22.19/include/linux/rtnetlink.h
5 @@ -293,6 +293,8 @@ struct rtnexthop
6 #define RTNH_F_DEAD 1 /* Nexthop is dead (used by multipath) */
7 #define RTNH_F_PERVASIVE 2 /* Do recursive gateway lookup */
8 #define RTNH_F_ONLINK 4 /* Gateway is forced on link */
9 +#define RTNH_F_SUSPECT 8 /* We don't know the real state */
10 +#define RTNH_F_BADSTATE (RTNH_F_DEAD | RTNH_F_SUSPECT)
11
12 /* Macros to handle hexthops */
13
14 Index: linux-2.6.22.19/include/net/flow.h
15 ===================================================================
16 --- linux-2.6.22.19.orig/include/net/flow.h
17 +++ linux-2.6.22.19/include/net/flow.h
18 @@ -19,6 +19,8 @@ struct flowi {
19 struct {
20 __be32 daddr;
21 __be32 saddr;
22 + __u32 lsrc;
23 + __u32 gw;
24 __u8 tos;
25 __u8 scope;
26 } ip4_u;
27 @@ -43,6 +45,8 @@ struct flowi {
28 #define fl6_flowlabel nl_u.ip6_u.flowlabel
29 #define fl4_dst nl_u.ip4_u.daddr
30 #define fl4_src nl_u.ip4_u.saddr
31 +#define fl4_lsrc nl_u.ip4_u.lsrc
32 +#define fl4_gw nl_u.ip4_u.gw
33 #define fl4_tos nl_u.ip4_u.tos
34 #define fl4_scope nl_u.ip4_u.scope
35
36 Index: linux-2.6.22.19/net/ipv4/route.c
37 ===================================================================
38 --- linux-2.6.22.19.orig/net/ipv4/route.c
39 +++ linux-2.6.22.19/net/ipv4/route.c
40 @@ -1208,6 +1208,7 @@ void ip_rt_redirect(__be32 old_gw, __be3
41
42 /* Gateway is different ... */
43 rt->rt_gateway = new_gw;
44 + if (rt->fl.fl4_gw) rt->fl.fl4_gw = new_gw;
45
46 /* Redirect received -> path was valid */
47 dst_confirm(&rth->u.dst);
48 @@ -1643,6 +1644,7 @@ static int ip_route_input_mc(struct sk_b
49 rth->fl.fl4_tos = tos;
50 rth->fl.mark = skb->mark;
51 rth->fl.fl4_src = saddr;
52 + rth->fl.fl4_lsrc = 0;
53 rth->rt_src = saddr;
54 #ifdef CONFIG_NET_CLS_ROUTE
55 rth->u.dst.tclassid = itag;
56 @@ -1653,6 +1655,7 @@ static int ip_route_input_mc(struct sk_b
57 dev_hold(rth->u.dst.dev);
58 rth->idev = in_dev_get(rth->u.dst.dev);
59 rth->fl.oif = 0;
60 + rth->fl.fl4_gw = 0;
61 rth->rt_gateway = daddr;
62 rth->rt_spec_dst= spec_dst;
63 rth->rt_type = RTN_MULTICAST;
64 @@ -1716,7 +1719,7 @@ static void ip_handle_martian_source(str
65 static inline int __mkroute_input(struct sk_buff *skb,
66 struct fib_result* res,
67 struct in_device *in_dev,
68 - __be32 daddr, __be32 saddr, u32 tos,
69 + __be32 daddr, __be32 saddr, u32 tos, u32 lsrc,
70 struct rtable **result)
71 {
72
73 @@ -1751,6 +1754,7 @@ static inline int __mkroute_input(struct
74 flags |= RTCF_DIRECTSRC;
75
76 if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
77 + !lsrc &&
78 (IN_DEV_SHARED_MEDIA(out_dev) ||
79 inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
80 flags |= RTCF_DOREDIRECT;
81 @@ -1788,6 +1792,7 @@ static inline int __mkroute_input(struct
82 rth->fl.mark = skb->mark;
83 rth->fl.fl4_src = saddr;
84 rth->rt_src = saddr;
85 + rth->fl.fl4_lsrc = lsrc;
86 rth->rt_gateway = daddr;
87 rth->rt_iif =
88 rth->fl.iif = in_dev->dev->ifindex;
89 @@ -1795,6 +1800,7 @@ static inline int __mkroute_input(struct
90 dev_hold(rth->u.dst.dev);
91 rth->idev = in_dev_get(rth->u.dst.dev);
92 rth->fl.oif = 0;
93 + rth->fl.fl4_gw = 0;
94 rth->rt_spec_dst= spec_dst;
95
96 rth->u.dst.input = ip_forward;
97 @@ -1816,19 +1822,21 @@ static inline int ip_mkroute_input_def(s
98 struct fib_result* res,
99 const struct flowi *fl,
100 struct in_device *in_dev,
101 - __be32 daddr, __be32 saddr, u32 tos)
102 + __be32 daddr, __be32 saddr, u32 tos,
103 + u32 lsrc)
104 {
105 struct rtable* rth = NULL;
106 int err;
107 unsigned hash;
108
109 + fib_select_default(fl, res);
110 #ifdef CONFIG_IP_ROUTE_MULTIPATH
111 - if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
112 + if (res->fi && res->fi->fib_nhs > 1)
113 fib_select_multipath(fl, res);
114 #endif
115
116 /* create a routing cache entry */
117 - err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
118 + err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, lsrc, &rth);
119 if (err)
120 return err;
121
122 @@ -1841,7 +1849,8 @@ static inline int ip_mkroute_input(struc
123 struct fib_result* res,
124 const struct flowi *fl,
125 struct in_device *in_dev,
126 - __be32 daddr, __be32 saddr, u32 tos)
127 + __be32 daddr, __be32 saddr, u32 tos,
128 + u32 lsrc)
129 {
130 #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
131 struct rtable* rth = NULL, *rtres;
132 @@ -1857,7 +1866,7 @@ static inline int ip_mkroute_input(struc
133 /* distinguish between multipath and singlepath */
134 if (hopcount < 2)
135 return ip_mkroute_input_def(skb, res, fl, in_dev, daddr,
136 - saddr, tos);
137 + saddr, tos, 0);
138
139 /* add all alternatives to the routing cache */
140 for (hop = 0; hop < hopcount; hop++) {
141 @@ -1869,7 +1878,7 @@ static inline int ip_mkroute_input(struc
142
143 /* create a routing cache entry */
144 err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos,
145 - &rth);
146 + 0, &rth);
147 if (err)
148 return err;
149
150 @@ -1889,7 +1898,7 @@ static inline int ip_mkroute_input(struc
151 skb->dst = &rtres->u.dst;
152 return err;
153 #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
154 - return ip_mkroute_input_def(skb, res, fl, in_dev, daddr, saddr, tos);
155 + return ip_mkroute_input_def(skb, res, fl, in_dev, daddr, saddr, tos, lsrc);
156 #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
157 }
158
159 @@ -1905,18 +1914,18 @@ static inline int ip_mkroute_input(struc
160 */
161
162 static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
163 - u8 tos, struct net_device *dev)
164 + u8 tos, struct net_device *dev, u32 lsrc)
165 {
166 struct fib_result res;
167 struct in_device *in_dev = in_dev_get(dev);
168 struct flowi fl = { .nl_u = { .ip4_u =
169 { .daddr = daddr,
170 - .saddr = saddr,
171 + .saddr = lsrc ? : saddr,
172 .tos = tos,
173 .scope = RT_SCOPE_UNIVERSE,
174 } },
175 .mark = skb->mark,
176 - .iif = dev->ifindex };
177 + .iif = lsrc? loopback_dev.ifindex : dev->ifindex };
178 unsigned flags = 0;
179 u32 itag = 0;
180 struct rtable * rth;
181 @@ -1949,6 +1958,12 @@ static int ip_route_input_slow(struct sk
182 if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
183 goto martian_destination;
184
185 + if (lsrc) {
186 + if (MULTICAST(lsrc) || BADCLASS(lsrc) ||
187 + ZERONET(lsrc) || LOOPBACK(lsrc))
188 + goto e_inval;
189 + }
190 +
191 /*
192 * Now we are ready to route packet.
193 */
194 @@ -1958,6 +1973,10 @@ static int ip_route_input_slow(struct sk
195 goto no_route;
196 }
197 free_res = 1;
198 + if (lsrc && res.type != RTN_UNICAST && res.type != RTN_NAT)
199 + goto e_inval;
200 + fl.iif = dev->ifindex;
201 + fl.fl4_src = saddr;
202
203 RT_CACHE_STAT_INC(in_slow_tot);
204
205 @@ -1982,7 +2001,7 @@ static int ip_route_input_slow(struct sk
206 if (res.type != RTN_UNICAST)
207 goto martian_destination;
208
209 - err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
210 + err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos, lsrc);
211 if (err == -ENOBUFS)
212 goto e_nobufs;
213 if (err == -EINVAL)
214 @@ -1997,6 +2016,8 @@ out: return err;
215 brd_input:
216 if (skb->protocol != htons(ETH_P_IP))
217 goto e_inval;
218 + if (lsrc)
219 + goto e_inval;
220
221 if (ZERONET(saddr))
222 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
223 @@ -2037,6 +2058,7 @@ local_input:
224 rth->u.dst.dev = &loopback_dev;
225 dev_hold(rth->u.dst.dev);
226 rth->idev = in_dev_get(rth->u.dst.dev);
227 + rth->fl.fl4_gw = 0;
228 rth->rt_gateway = daddr;
229 rth->rt_spec_dst= spec_dst;
230 rth->u.dst.input= ip_local_deliver;
231 @@ -2086,8 +2108,9 @@ martian_source:
232 goto e_inval;
233 }
234
235 -int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
236 - u8 tos, struct net_device *dev)
237 +static inline int
238 +ip_route_input_cached(struct sk_buff *skb, __be32 daddr, __be32 saddr,
239 + u8 tos, struct net_device *dev, u32 lsrc)
240 {
241 struct rtable * rth;
242 unsigned hash;
243 @@ -2102,6 +2125,7 @@ int ip_route_input(struct sk_buff *skb,
244 if (rth->fl.fl4_dst == daddr &&
245 rth->fl.fl4_src == saddr &&
246 rth->fl.iif == iif &&
247 + rth->fl.fl4_lsrc == lsrc &&
248 rth->fl.oif == 0 &&
249 rth->fl.mark == skb->mark &&
250 rth->fl.fl4_tos == tos) {
251 @@ -2148,7 +2172,19 @@ int ip_route_input(struct sk_buff *skb,
252 rcu_read_unlock();
253 return -EINVAL;
254 }
255 - return ip_route_input_slow(skb, daddr, saddr, tos, dev);
256 + return ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
257 +}
258 +
259 +int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
260 + u8 tos, struct net_device *dev)
261 +{
262 + return ip_route_input_cached(skb, daddr, saddr, tos, dev, 0);
263 +}
264 +
265 +int ip_route_input_lookup(struct sk_buff *skb, u32 daddr, u32 saddr,
266 + u8 tos, struct net_device *dev, u32 lsrc)
267 +{
268 + return ip_route_input_cached(skb, daddr, saddr, tos, dev, lsrc);
269 }
270
271 static inline int __mkroute_output(struct rtable **result,
272 @@ -2227,6 +2263,7 @@ static inline int __mkroute_output(struc
273 rth->fl.fl4_tos = tos;
274 rth->fl.fl4_src = oldflp->fl4_src;
275 rth->fl.oif = oldflp->oif;
276 + rth->fl.fl4_gw = oldflp->fl4_gw;
277 rth->fl.mark = oldflp->mark;
278 rth->rt_dst = fl->fl4_dst;
279 rth->rt_src = fl->fl4_src;
280 @@ -2367,6 +2404,7 @@ static int ip_route_output_slow(struct r
281 struct flowi fl = { .nl_u = { .ip4_u =
282 { .daddr = oldflp->fl4_dst,
283 .saddr = oldflp->fl4_src,
284 + .gw = oldflp->fl4_gw,
285 .tos = tos & IPTOS_RT_MASK,
286 .scope = ((tos & RTO_ONLINK) ?
287 RT_SCOPE_LINK :
288 @@ -2470,6 +2508,7 @@ static int ip_route_output_slow(struct r
289 dev_out = &loopback_dev;
290 dev_hold(dev_out);
291 fl.oif = loopback_dev.ifindex;
292 + fl.fl4_gw = 0;
293 res.type = RTN_LOCAL;
294 flags |= RTCF_LOCAL;
295 goto make_route;
296 @@ -2477,7 +2516,7 @@ static int ip_route_output_slow(struct r
297
298 if (fib_lookup(&fl, &res)) {
299 res.fi = NULL;
300 - if (oldflp->oif) {
301 + if (oldflp->oif && dev_out->flags & IFF_UP) {
302 /* Apparently, routing tables are wrong. Assume,
303 that the destination is on link.
304
305 @@ -2517,6 +2556,7 @@ static int ip_route_output_slow(struct r
306 dev_out = &loopback_dev;
307 dev_hold(dev_out);
308 fl.oif = dev_out->ifindex;
309 + fl.fl4_gw = 0;
310 if (res.fi)
311 fib_info_put(res.fi);
312 res.fi = NULL;
313 @@ -2524,13 +2564,12 @@ static int ip_route_output_slow(struct r
314 goto make_route;
315 }
316
317 + if (res.type == RTN_UNICAST)
318 + fib_select_default(&fl, &res);
319 #ifdef CONFIG_IP_ROUTE_MULTIPATH
320 - if (res.fi->fib_nhs > 1 && fl.oif == 0)
321 + if (res.fi->fib_nhs > 1)
322 fib_select_multipath(&fl, &res);
323 - else
324 #endif
325 - if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
326 - fib_select_default(&fl, &res);
327
328 if (!fl.fl4_src)
329 fl.fl4_src = FIB_RES_PREFSRC(res);
330 @@ -2567,6 +2606,7 @@ int __ip_route_output_key(struct rtable
331 rth->fl.fl4_src == flp->fl4_src &&
332 rth->fl.iif == 0 &&
333 rth->fl.oif == flp->oif &&
334 + rth->fl.fl4_gw == flp->fl4_gw &&
335 rth->fl.mark == flp->mark &&
336 !((rth->fl.fl4_tos ^ flp->fl4_tos) &
337 (IPTOS_RT_MASK | RTO_ONLINK))) {
338 @@ -3267,3 +3307,4 @@ int __init ip_rt_init(void)
339 EXPORT_SYMBOL(__ip_select_ident);
340 EXPORT_SYMBOL(ip_route_input);
341 EXPORT_SYMBOL(ip_route_output_key);
342 +EXPORT_SYMBOL(ip_route_input_lookup);