enable multicast routing for linux 2.4 (#6037)
[oweals/openwrt.git] / target / linux / generic-2.4 / patches / 700-multiple_default_gateways.patch
1 --- a/include/linux/netfilter_ipv4/ip_nat.h
2 +++ b/include/linux/netfilter_ipv4/ip_nat.h
3 @@ -121,5 +121,13 @@ extern int ip_nat_used_tuple(const struc
4  extern u_int16_t ip_nat_cheat_check(u_int32_t oldvalinv,
5                                     u_int32_t newval,
6                                     u_int16_t oldcheck);
7 +
8 +/* Call input routing for SNAT-ed traffic */
9 +extern unsigned int ip_nat_route_input(unsigned int hooknum,
10 +                                      struct sk_buff **pskb,
11 +                                      const struct net_device *in,
12 +                                      const struct net_device *out,
13 +                                      int (*okfn)(struct sk_buff *));
14 +
15  #endif /*__KERNEL__*/
16  #endif
17 --- a/include/linux/rtnetlink.h
18 +++ b/include/linux/rtnetlink.h
19 @@ -234,6 +234,8 @@ struct rtnexthop
20  #define RTNH_F_DEAD            1       /* Nexthop is dead (used by multipath)  */
21  #define RTNH_F_PERVASIVE       2       /* Do recursive gateway lookup  */
22  #define RTNH_F_ONLINK          4       /* Gateway is forced on link    */
23 +#define RTNH_F_SUSPECT         8       /* We don't know the real state */
24 +#define RTNH_F_BADSTATE                (RTNH_F_DEAD | RTNH_F_SUSPECT)
25  
26  /* Macros to handle hexthops */
27  
28 --- a/include/net/ip_fib.h
29 +++ b/include/net/ip_fib.h
30 @@ -162,7 +162,8 @@ static inline int fib_lookup(const struc
31  
32  static inline void fib_select_default(const struct rt_key *key, struct fib_result *res)
33  {
34 -       if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
35 +       if ((FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) ||
36 +           FIB_RES_NH(*res).nh_scope == RT_SCOPE_HOST)
37                 main_table->tb_select_default(main_table, key, res);
38  }
39  
40 @@ -174,6 +175,7 @@ extern struct fib_table * fib_tables[RT_
41  extern int fib_lookup(const struct rt_key *key, struct fib_result *res);
42  extern struct fib_table *__fib_new_table(int id);
43  extern void fib_rule_put(struct fib_rule *r);
44 +extern int fib_result_table(struct fib_result *res);
45  
46  static inline struct fib_table *fib_get_table(int id)
47  {
48 @@ -275,5 +277,6 @@ static inline void fib_res_put(struct fi
49  #endif
50  }
51  
52 +extern rwlock_t fib_nhflags_lock;
53  
54  #endif  /* _NET_FIB_H */
55 --- a/include/net/route.h
56 +++ b/include/net/route.h
57 @@ -49,6 +49,8 @@ struct rt_key
58  {
59         __u32                   dst;
60         __u32                   src;
61 +       __u32                   lsrc;
62 +       __u32                   gw;
63         int                     iif;
64         int                     oif;
65  #ifdef CONFIG_IP_ROUTE_FWMARK
66 @@ -128,6 +130,7 @@ extern void         ip_rt_advice(struct rtable 
67  extern void            rt_cache_flush(int how);
68  extern int             ip_route_output_key(struct rtable **, const struct rt_key *key);
69  extern int             ip_route_input(struct sk_buff*, u32 dst, u32 src, u8 tos, struct net_device *devin);
70 +extern int             ip_route_input_lookup(struct sk_buff*, u32 dst, u32 src, u8 tos, struct net_device *devin, u32 lsrc);
71  extern unsigned short  ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu);
72  extern void            ip_rt_update_pmtu(struct dst_entry *dst, unsigned mtu);
73  extern void            ip_rt_send_redirect(struct sk_buff *skb);
74 @@ -148,6 +151,15 @@ static inline int ip_route_output(struct
75  }
76  
77  
78 +static inline int
79 +ip_route_output_lookup(struct rtable **rp,
80 +                      u32 daddr, u32 saddr, u32 tos, int oif, u32 gw)
81 +{
82 +       struct rt_key key = { dst:daddr, src:saddr, gw:gw, oif:oif, tos:tos };
83 +
84 +       return ip_route_output_key(rp, &key);
85 +}
86 +
87  static inline void ip_rt_put(struct rtable * rt)
88  {
89         if (rt)
90 --- a/net/ipv4/fib_frontend.c
91 +++ b/net/ipv4/fib_frontend.c
92 @@ -54,6 +54,8 @@
93  struct fib_table *local_table;
94  struct fib_table *main_table;
95  
96 +#define FIB_RES_TABLE(r) (RT_TABLE_MAIN)
97 +
98  #else
99  
100  #define RT_TABLE_MIN 1
101 @@ -71,6 +73,7 @@ struct fib_table *__fib_new_table(int id
102         return tb;
103  }
104  
105 +#define FIB_RES_TABLE(r) (fib_result_table(r))
106  
107  #endif /* CONFIG_IP_MULTIPLE_TABLES */
108  
109 @@ -209,6 +212,9 @@ int fib_validate_source(u32 src, u32 dst
110         struct in_device *in_dev;
111         struct rt_key key;
112         struct fib_result res;
113 +       int table;
114 +       unsigned char prefixlen;
115 +       unsigned char scope;
116         int no_addr, rpf;
117         int ret;
118  
119 @@ -216,6 +222,7 @@ int fib_validate_source(u32 src, u32 dst
120         key.src = dst;
121         key.tos = tos;
122         key.oif = 0;
123 +       key.gw  = 0;
124         key.iif = oif;
125         key.scope = RT_SCOPE_UNIVERSE;
126  
127 @@ -237,31 +244,35 @@ int fib_validate_source(u32 src, u32 dst
128                 goto e_inval_res;
129         *spec_dst = FIB_RES_PREFSRC(res);
130         fib_combine_itag(itag, &res);
131 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
132 -       if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1)
133 -#else
134         if (FIB_RES_DEV(res) == dev)
135 -#endif
136         {
137                 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
138                 fib_res_put(&res);
139                 return ret;
140         }
141 +       table = FIB_RES_TABLE(&res);
142 +       prefixlen = res.prefixlen;
143 +       scope = res.scope;
144         fib_res_put(&res);
145         if (no_addr)
146                 goto last_resort;
147 -       if (rpf)
148 -               goto e_inval;
149         key.oif = dev->ifindex;
150  
151         ret = 0;
152         if (fib_lookup(&key, &res) == 0) {
153 -               if (res.type == RTN_UNICAST) {
154 +               if (res.type == RTN_UNICAST &&
155 +                   ((table == FIB_RES_TABLE(&res) &&
156 +                     res.prefixlen >= prefixlen && res.scope >= scope) ||
157 +                    !rpf)) {
158                         *spec_dst = FIB_RES_PREFSRC(res);
159                         ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
160 +                       fib_res_put(&res);
161 +                       return ret;
162                 }
163                 fib_res_put(&res);
164         }
165 +       if (rpf)
166 +               goto e_inval;
167         return ret;
168  
169  last_resort:
170 @@ -579,9 +590,7 @@ static int fib_inetaddr_event(struct not
171         switch (event) {
172         case NETDEV_UP:
173                 fib_add_ifaddr(ifa);
174 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
175                 fib_sync_up(ifa->ifa_dev->dev);
176 -#endif
177                 rt_cache_flush(-1);
178                 break;
179         case NETDEV_DOWN:
180 @@ -617,9 +626,7 @@ static int fib_netdev_event(struct notif
181                 for_ifa(in_dev) {
182                         fib_add_ifaddr(ifa);
183                 } endfor_ifa(in_dev);
184 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
185                 fib_sync_up(dev);
186 -#endif
187                 rt_cache_flush(-1);
188                 break;
189         case NETDEV_DOWN:
190 --- a/net/ipv4/fib_hash.c
191 +++ b/net/ipv4/fib_hash.c
192 @@ -71,6 +71,7 @@ struct fib_node
193         struct fib_info         *fn_info;
194  #define FIB_INFO(f)    ((f)->fn_info)
195         fn_key_t                fn_key;
196 +       int                     fn_last_dflt;
197         u8                      fn_tos;
198         u8                      fn_type;
199         u8                      fn_scope;
200 @@ -336,72 +337,123 @@ out:
201         return err;
202  }
203  
204 -static int fn_hash_last_dflt=-1;
205 -
206 -static int fib_detect_death(struct fib_info *fi, int order,
207 -                           struct fib_info **last_resort, int *last_idx)
208 +static int fib_detect_death(struct fib_info *fi, int order, int last_dflt,
209 +                           struct fib_info **last_resort, int *last_idx,
210 +                           int *last_nhsel, const struct rt_key *key)
211  {
212         struct neighbour *n;
213 -       int state = NUD_NONE;
214 +       int nhsel;
215 +       int state;
216 +       struct fib_nh * nh;
217 +       u32 dst;
218 +       int flag, dead = 1;
219 +
220 +       /* change_nexthops(fi) { */
221 +       for (nhsel = 0, nh = fi->fib_nh; nhsel < fi->fib_nhs; nh++, nhsel++) {
222 +               if (key->oif && key->oif != nh->nh_oif)
223 +                       continue;
224 +               if (key->gw && key->gw != nh->nh_gw && nh->nh_gw &&
225 +                   nh->nh_scope == RT_SCOPE_LINK)
226 +                       continue;
227 +               if (nh->nh_flags & RTNH_F_DEAD)
228 +                       continue;
229  
230 -       n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
231 -       if (n) {
232 -               state = n->nud_state;
233 -               neigh_release(n);
234 +               flag = 0;
235 +               if (nh->nh_dev->flags & IFF_NOARP) {
236 +                       dead = 0;
237 +                       goto setfl;
238 +               }
239 +
240 +               dst = nh->nh_gw;
241 +               if (!nh->nh_gw || nh->nh_scope != RT_SCOPE_LINK)
242 +                       dst = key->dst;
243 +
244 +               state = NUD_NONE;
245 +               n = neigh_lookup(&arp_tbl, &dst, nh->nh_dev);
246 +               if (n) {
247 +                       state = n->nud_state;
248 +                       neigh_release(n);
249 +               }
250 +               if (state==NUD_REACHABLE ||
251 +                       ((state&NUD_VALID) && order != last_dflt)) {
252 +                       dead = 0;
253 +                       goto setfl;
254 +               }
255 +               if (!(state&NUD_VALID))
256 +                       flag = 1;
257 +               if (!dead)
258 +                       goto setfl;
259 +               if ((state&NUD_VALID) ||
260 +                   (*last_idx<0 && order >= last_dflt)) {
261 +                       *last_resort = fi;
262 +                       *last_idx = order;
263 +                       *last_nhsel = nhsel;
264 +               }
265 +
266 +               setfl:
267 +
268 +               read_lock_bh(&fib_nhflags_lock);
269 +               if (flag)
270 +                       nh->nh_flags |= RTNH_F_SUSPECT;
271 +               else
272 +                       nh->nh_flags &= ~RTNH_F_SUSPECT;
273 +               read_unlock_bh(&fib_nhflags_lock);
274         }
275 -       if (state==NUD_REACHABLE)
276 -               return 0;
277 -       if ((state&NUD_VALID) && order != fn_hash_last_dflt)
278 -               return 0;
279 -       if ((state&NUD_VALID) ||
280 -           (*last_idx<0 && order > fn_hash_last_dflt)) {
281 -               *last_resort = fi;
282 -               *last_idx = order;
283 -       }
284 -       return 1;
285 +       /* } endfor_nexthops(fi) */
286 +
287 +       return dead;
288  }
289  
290  static void
291  fn_hash_select_default(struct fib_table *tb, const struct rt_key *key, struct fib_result *res)
292  {
293 -       int order, last_idx;
294 -       struct fib_node *f;
295 +       int order, last_idx, last_dflt, last_nhsel;
296 +       struct fib_node *f, *first_node;
297         struct fib_info *fi = NULL;
298         struct fib_info *last_resort;
299         struct fn_hash *t = (struct fn_hash*)tb->tb_data;
300 -       struct fn_zone *fz = t->fn_zones[0];
301 +       struct fn_zone *fz = t->fn_zones[res->prefixlen];
302 +       fn_key_t k;
303  
304         if (fz == NULL)
305                 return;
306  
307 +       k = fz_key(key->dst, fz);
308 +       last_dflt = -2;
309 +       first_node = NULL;
310         last_idx = -1;
311         last_resort = NULL;
312 +       last_nhsel = 0;
313         order = -1;
314  
315         read_lock(&fib_hash_lock);
316 -       for (f = fz->fz_hash[0]; f; f = f->fn_next) {
317 +       for (f = fz_chain(k, fz); f; f = f->fn_next) {
318                 struct fib_info *next_fi = FIB_INFO(f);
319  
320 -               if ((f->fn_state&FN_S_ZOMBIE) ||
321 +               if (!fn_key_eq(k, f->fn_key) ||
322 +                   (f->fn_state&FN_S_ZOMBIE) ||
323                     f->fn_scope != res->scope ||
324 +#ifdef CONFIG_IP_ROUTE_TOS
325 +                   (f->fn_tos && f->fn_tos != key->tos) ||
326 +#endif
327                     f->fn_type != RTN_UNICAST)
328                         continue;
329  
330                 if (next_fi->fib_priority > res->fi->fib_priority)
331                         break;
332 -               if (!next_fi->fib_nh[0].nh_gw || next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
333 -                       continue;
334                 f->fn_state |= FN_S_ACCESSED;
335  
336 -               if (fi == NULL) {
337 -                       if (next_fi != res->fi)
338 -                               break;
339 -               } else if (!fib_detect_death(fi, order, &last_resort, &last_idx)) {
340 +               if (!first_node) {
341 +                       last_dflt = f->fn_last_dflt;
342 +                       first_node = f;
343 +               }
344 +               if (fi && !fib_detect_death(fi, order, last_dflt,
345 +                               &last_resort, &last_idx, &last_nhsel, key)) {
346                         if (res->fi)
347                                 fib_info_put(res->fi);
348                         res->fi = fi;
349                         atomic_inc(&fi->fib_clntref);
350 -                       fn_hash_last_dflt = order;
351 +                       first_node->fn_last_dflt = order;
352                         goto out;
353                 }
354                 fi = next_fi;
355 @@ -409,16 +461,25 @@ fn_hash_select_default(struct fib_table 
356         }
357  
358         if (order<=0 || fi==NULL) {
359 -               fn_hash_last_dflt = -1;
360 +               if (fi && fi->fib_nhs > 1 &&
361 +                   fib_detect_death(fi, order, last_dflt,
362 +                       &last_resort, &last_idx, &last_nhsel, key) &&
363 +                   last_resort == fi) {
364 +                       read_lock_bh(&fib_nhflags_lock);
365 +                       fi->fib_nh[last_nhsel].nh_flags &= ~RTNH_F_SUSPECT;
366 +                       read_unlock_bh(&fib_nhflags_lock);
367 +               }
368 +               if (first_node) first_node->fn_last_dflt = -1;
369                 goto out;
370         }
371  
372 -       if (!fib_detect_death(fi, order, &last_resort, &last_idx)) {
373 +       if (!fib_detect_death(fi, order, last_dflt, &last_resort, &last_idx,
374 +                             &last_nhsel, key)) {
375                 if (res->fi)
376                         fib_info_put(res->fi);
377                 res->fi = fi;
378                 atomic_inc(&fi->fib_clntref);
379 -               fn_hash_last_dflt = order;
380 +               first_node->fn_last_dflt = order;
381                 goto out;
382         }
383  
384 @@ -428,8 +489,11 @@ fn_hash_select_default(struct fib_table 
385                 res->fi = last_resort;
386                 if (last_resort)
387                         atomic_inc(&last_resort->fib_clntref);
388 +               read_lock_bh(&fib_nhflags_lock);
389 +               last_resort->fib_nh[last_nhsel].nh_flags &= ~RTNH_F_SUSPECT;
390 +               read_unlock_bh(&fib_nhflags_lock);
391 +               first_node->fn_last_dflt = last_idx;
392         }
393 -       fn_hash_last_dflt = last_idx;
394  out:
395         read_unlock(&fib_hash_lock);
396  }
397 @@ -589,6 +653,7 @@ replace:
398  
399         memset(new_f, 0, sizeof(struct fib_node));
400  
401 +       new_f->fn_last_dflt = -1;
402         new_f->fn_key = key;
403  #ifdef CONFIG_IP_ROUTE_TOS
404         new_f->fn_tos = tos;
405 --- a/net/ipv4/fib_rules.c
406 +++ b/net/ipv4/fib_rules.c
407 @@ -307,6 +307,11 @@ static void fib_rules_attach(struct net_
408         }
409  }
410  
411 +int fib_result_table(struct fib_result *res)
412 +{
413 +       return res->r->r_table;
414 +}
415 +
416  int fib_lookup(const struct rt_key *key, struct fib_result *res)
417  {
418         int err;
419 @@ -371,8 +376,10 @@ FRprintk("FAILURE\n");
420  
421  void fib_select_default(const struct rt_key *key, struct fib_result *res)
422  {
423 -       if (res->r && res->r->r_action == RTN_UNICAST &&
424 -           FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
425 +       if (res->r &&
426 +           (res->r->r_action == RTN_UNICAST || res->r->r_action == RTN_NAT) &&
427 +           ((FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) ||
428 +            FIB_RES_NH(*res).nh_scope == RT_SCOPE_HOST)) {
429                 struct fib_table *tb;
430                 if ((tb = fib_get_table(res->r->r_table)) != NULL)
431                         tb->tb_select_default(tb, key, res);
432 --- a/net/ipv4/fib_semantics.c
433 +++ b/net/ipv4/fib_semantics.c
434 @@ -48,6 +48,7 @@
435  static struct fib_info         *fib_info_list;
436  static rwlock_t fib_info_lock = RW_LOCK_UNLOCKED;
437  int fib_info_cnt;
438 +rwlock_t fib_nhflags_lock = RW_LOCK_UNLOCKED;
439  
440  #define for_fib_info() { struct fib_info *fi; \
441         for (fi = fib_info_list; fi; fi = fi->fib_next)
442 @@ -150,7 +151,7 @@ static __inline__ int nh_comp(const stru
443  #ifdef CONFIG_NET_CLS_ROUTE
444                     nh->nh_tclassid != onh->nh_tclassid ||
445  #endif
446 -                   ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
447 +                   ((nh->nh_flags^onh->nh_flags)&~RTNH_F_BADSTATE))
448                         return -1;
449                 onh++;
450         } endfor_nexthops(fi);
451 @@ -166,7 +167,7 @@ static __inline__ struct fib_info * fib_
452                     nfi->fib_prefsrc == fi->fib_prefsrc &&
453                     nfi->fib_priority == fi->fib_priority &&
454                     memcmp(nfi->fib_metrics, fi->fib_metrics, sizeof(fi->fib_metrics)) == 0 &&
455 -                   ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
456 +                   ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_BADSTATE) == 0 &&
457                     (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
458                         return fi;
459         } endfor_fib_info();
460 @@ -365,8 +366,11 @@ static int fib_check_nh(const struct rtm
461                                 return -EINVAL;
462                         if ((dev = __dev_get_by_index(nh->nh_oif)) == NULL)
463                                 return -ENODEV;
464 -                       if (!(dev->flags&IFF_UP))
465 -                               return -ENETDOWN;
466 +                       if (!(dev->flags&IFF_UP)) {
467 +                               if (fi->fib_protocol != RTPROT_STATIC)
468 +                                       return -ENETDOWN;
469 +                               nh->nh_flags |= RTNH_F_DEAD;
470 +                       }
471                         nh->nh_dev = dev;
472                         dev_hold(dev);
473                         nh->nh_scope = RT_SCOPE_LINK;
474 @@ -380,23 +384,48 @@ static int fib_check_nh(const struct rtm
475                 /* It is not necessary, but requires a bit of thinking */
476                 if (key.scope < RT_SCOPE_LINK)
477                         key.scope = RT_SCOPE_LINK;
478 -               if ((err = fib_lookup(&key, &res)) != 0)
479 -                       return err;
480 -               err = -EINVAL;
481 -               if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
482 -                       goto out;
483 -               nh->nh_scope = res.scope;
484 -               nh->nh_oif = FIB_RES_OIF(res);
485 -               if ((nh->nh_dev = FIB_RES_DEV(res)) == NULL)
486 -                       goto out;
487 -               dev_hold(nh->nh_dev);
488 -               err = -ENETDOWN;
489 -               if (!(nh->nh_dev->flags & IFF_UP))
490 -                       goto out;
491 -               err = 0;
492 +
493 +               err = fib_lookup(&key, &res);
494 +               if (err) {
495 +                       struct in_device *in_dev;
496 +
497 +                       if (err != -ENETUNREACH ||
498 +                           fi->fib_protocol != RTPROT_STATIC)
499 +                               return err;
500 +
501 +                       in_dev = inetdev_by_index(nh->nh_oif);
502 +                       if (in_dev == NULL ||
503 +                           in_dev->dev->flags & IFF_UP) {
504 +                               if (in_dev)
505 +                                       in_dev_put(in_dev);
506 +                               return err;
507 +                       }
508 +                       nh->nh_flags |= RTNH_F_DEAD;
509 +                       nh->nh_scope = RT_SCOPE_LINK;
510 +                       nh->nh_dev = in_dev->dev;
511 +                       dev_hold(nh->nh_dev);
512 +                       in_dev_put(in_dev);
513 +               } else {
514 +                       err = -EINVAL;
515 +                       if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
516 +                               goto out;
517 +                       nh->nh_scope = res.scope;
518 +                       nh->nh_oif = FIB_RES_OIF(res);
519 +                       if ((nh->nh_dev = FIB_RES_DEV(res)) == NULL)
520 +                               goto out;
521 +                       dev_hold(nh->nh_dev);
522 +                       if (!(nh->nh_dev->flags & IFF_UP)) {
523 +                               if (fi->fib_protocol != RTPROT_STATIC) {
524 +                                       err = -ENETDOWN;
525 +                                       goto out;
526 +                               }
527 +                               nh->nh_flags |= RTNH_F_DEAD;
528 +                       }
529 +                       err = 0;
530  out:
531 -               fib_res_put(&res);
532 -               return err;
533 +                       fib_res_put(&res);
534 +                       return err;
535 +               }
536         } else {
537                 struct in_device *in_dev;
538  
539 @@ -407,8 +436,11 @@ out:
540                 if (in_dev == NULL)
541                         return -ENODEV;
542                 if (!(in_dev->dev->flags&IFF_UP)) {
543 -                       in_dev_put(in_dev);
544 -                       return -ENETDOWN;
545 +                       if (fi->fib_protocol != RTPROT_STATIC) {
546 +                               in_dev_put(in_dev);
547 +                               return -ENETDOWN;
548 +                       }
549 +                       nh->nh_flags |= RTNH_F_DEAD;
550                 }
551                 nh->nh_dev = in_dev->dev;
552                 dev_hold(nh->nh_dev);
553 @@ -606,8 +638,12 @@ fib_semantic_match(int type, struct fib_
554                         for_nexthops(fi) {
555                                 if (nh->nh_flags&RTNH_F_DEAD)
556                                         continue;
557 -                               if (!key->oif || key->oif == nh->nh_oif)
558 -                                       break;
559 +                               if (key->oif && key->oif != nh->nh_oif)
560 +                                       continue;
561 +                               if (key->gw && key->gw != nh->nh_gw &&
562 +                                   nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
563 +                                       continue;
564 +                               break;
565                         }
566  #ifdef CONFIG_IP_ROUTE_MULTIPATH
567                         if (nhsel < fi->fib_nhs) {
568 @@ -873,22 +909,35 @@ int fib_sync_down(u32 local, struct net_
569                 if (local && fi->fib_prefsrc == local) {
570                         fi->fib_flags |= RTNH_F_DEAD;
571                         ret++;
572 -               } else if (dev && fi->fib_nhs) {
573 +               } else if (fi->fib_nhs) {
574                         int dead = 0;
575  
576                         change_nexthops(fi) {
577 -                               if (nh->nh_flags&RTNH_F_DEAD)
578 -                                       dead++;
579 -                               else if (nh->nh_dev == dev &&
580 -                                        nh->nh_scope != scope) {
581 -                                       nh->nh_flags |= RTNH_F_DEAD;
582 +                               if (nh->nh_flags&RTNH_F_DEAD) {
583 +                                       if (fi->fib_protocol!=RTPROT_STATIC ||
584 +                                           nh->nh_dev == NULL ||
585 +                                           !__in_dev_get(nh->nh_dev) ||
586 +                                           nh->nh_dev->flags&IFF_UP)
587 +                                               dead++;
588 +                               } else if ((nh->nh_dev == dev && dev &&
589 +                                           nh->nh_scope != scope) ||
590 +                                           (local == nh->nh_gw && local &&
591 +                                            nh->nh_oif)) {
592 +                                       write_lock_bh(&fib_nhflags_lock);
593  #ifdef CONFIG_IP_ROUTE_MULTIPATH
594 -                                       spin_lock_bh(&fib_multipath_lock);
595 +                                       spin_lock(&fib_multipath_lock);
596 +                                       nh->nh_flags |= RTNH_F_DEAD;
597                                         fi->fib_power -= nh->nh_power;
598                                         nh->nh_power = 0;
599 -                                       spin_unlock_bh(&fib_multipath_lock);
600 +                                       spin_unlock(&fib_multipath_lock);
601 +#else
602 +                                       nh->nh_flags |= RTNH_F_DEAD;
603  #endif
604 -                                       dead++;
605 +                                       write_unlock_bh(&fib_nhflags_lock);
606 +                                       if (fi->fib_protocol!=RTPROT_STATIC ||
607 +                                           force ||
608 +                                           (dev && __in_dev_get(dev) == NULL))
609 +                                               dead++;
610                                 }
611  #ifdef CONFIG_IP_ROUTE_MULTIPATH
612                                 if (force > 1 && nh->nh_dev == dev) {
613 @@ -906,37 +955,55 @@ int fib_sync_down(u32 local, struct net_
614         return ret;
615  }
616  
617 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
618 -
619  /*
620 -   Dead device goes up. We wake up dead nexthops.
621 -   It takes sense only on multipath routes.
622 +   Dead device goes up or new address is added. We wake up dead nexthops.
623   */
624  
625  int fib_sync_up(struct net_device *dev)
626  {
627 -       int ret = 0;
628 +       struct rt_key key;
629 +       struct fib_result res;
630 +       int ret, rep;
631  
632 +repeat:
633         if (!(dev->flags&IFF_UP))
634                 return 0;
635  
636 +       ret = 0;
637 +       rep = 0;
638         for_fib_info() {
639                 int alive = 0;
640  
641                 change_nexthops(fi) {
642 -                       if (!(nh->nh_flags&RTNH_F_DEAD)) {
643 -                               alive++;
644 +                       if (!(nh->nh_flags&RTNH_F_DEAD))
645                                 continue;
646 -                       }
647                         if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
648                                 continue;
649                         if (nh->nh_dev != dev || __in_dev_get(dev) == NULL)
650                                 continue;
651 +                       if (nh->nh_gw && fi->fib_protocol == RTPROT_STATIC) {
652 +                               memset(&key, 0, sizeof(key));
653 +                               key.dst = nh->nh_gw;
654 +                               key.oif = nh->nh_oif;
655 +                               key.scope = nh->nh_scope;
656 +                               if (fib_lookup(&key, &res) != 0)
657 +                                       continue;
658 +                               if (res.type != RTN_UNICAST &&
659 +                                   res.type != RTN_LOCAL) {
660 +                                       fib_res_put(&res);
661 +                                       continue;
662 +                               }
663 +                               nh->nh_scope = res.scope;
664 +                               fib_res_put(&res);
665 +                               rep = 1;
666 +                       }
667                         alive++;
668 +#ifdef CONFIG_IP_ROUTE_MULTIPATH
669                         spin_lock_bh(&fib_multipath_lock);
670                         nh->nh_power = 0;
671                         nh->nh_flags &= ~RTNH_F_DEAD;
672                         spin_unlock_bh(&fib_multipath_lock);
673 +#endif
674                 } endfor_nexthops(fi)
675  
676                 if (alive > 0) {
677 @@ -944,9 +1011,13 @@ int fib_sync_up(struct net_device *dev)
678                         ret++;
679                 }
680         } endfor_fib_info();
681 +       if (rep)
682 +               goto repeat;
683         return ret;
684  }
685  
686 +#ifdef CONFIG_IP_ROUTE_MULTIPATH
687 +
688  /*
689     The algorithm is suboptimal, but it provides really
690     fair weighted route distribution.
691 @@ -955,24 +1026,45 @@ int fib_sync_up(struct net_device *dev)
692  void fib_select_multipath(const struct rt_key *key, struct fib_result *res)
693  {
694         struct fib_info *fi = res->fi;
695 -       int w;
696 +       int w, alive;
697  
698         spin_lock_bh(&fib_multipath_lock);
699 +       if (key->oif) {
700 +               int sel = -1;
701 +               w = -1;
702 +               change_nexthops(fi) {
703 +                       if (key->oif != nh->nh_oif)
704 +                               continue;
705 +                       if (key->gw && key->gw != nh->nh_gw &&
706 +                           nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
707 +                               continue;
708 +                       if (!(nh->nh_flags&RTNH_F_BADSTATE)) {
709 +                               if (nh->nh_power > w) {
710 +                                       w = nh->nh_power;
711 +                                       sel = nhsel;
712 +                               }
713 +                       }
714 +               } endfor_nexthops(fi);
715 +               if (sel >= 0) {
716 +                       spin_unlock_bh(&fib_multipath_lock);
717 +                       res->nh_sel = sel;
718 +                       return;
719 +               }
720 +               goto last_resort;
721 +       }
722 +
723 +repeat:
724         if (fi->fib_power <= 0) {
725                 int power = 0;
726                 change_nexthops(fi) {
727 -                       if (!(nh->nh_flags&RTNH_F_DEAD)) {
728 +                       if (!(nh->nh_flags&RTNH_F_BADSTATE)) {
729                                 power += nh->nh_weight;
730                                 nh->nh_power = nh->nh_weight;
731                         }
732                 } endfor_nexthops(fi);
733                 fi->fib_power = power;
734 -               if (power <= 0) {
735 -                       spin_unlock_bh(&fib_multipath_lock);
736 -                       /* Race condition: route has just become dead. */
737 -                       res->nh_sel = 0;
738 -                       return;
739 -               }
740 +               if (power <= 0)
741 +                       goto last_resort;
742         }
743  
744  
745 @@ -982,20 +1074,40 @@ void fib_select_multipath(const struct r
746  
747         w = jiffies % fi->fib_power;
748  
749 +       alive = 0;
750         change_nexthops(fi) {
751 -               if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
752 +               if (!(nh->nh_flags&RTNH_F_BADSTATE) && nh->nh_power) {
753                         if ((w -= nh->nh_power) <= 0) {
754                                 nh->nh_power--;
755                                 fi->fib_power--;
756 -                               res->nh_sel = nhsel;
757                                 spin_unlock_bh(&fib_multipath_lock);
758 +                               res->nh_sel = nhsel;
759                                 return;
760                         }
761 +                       alive = 1;
762 +               }
763 +       } endfor_nexthops(fi);
764 +       if (alive) {
765 +               fi->fib_power = 0;
766 +               goto repeat;
767 +       }
768 +
769 +last_resort:
770 +
771 +       for_nexthops(fi) {
772 +               if (!(nh->nh_flags&RTNH_F_DEAD)) {
773 +                       if (key->oif && key->oif != nh->nh_oif)
774 +                               continue;
775 +                       if (key->gw && key->gw != nh->nh_gw &&
776 +                           nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
777 +                               continue;
778 +                       spin_unlock_bh(&fib_multipath_lock);
779 +                       res->nh_sel = nhsel;
780 +                       return;
781                 }
782         } endfor_nexthops(fi);
783  
784         /* Race condition: route has just become dead. */
785 -       res->nh_sel = 0;
786         spin_unlock_bh(&fib_multipath_lock);
787  }
788  #endif
789 --- a/net/ipv4/ip_nat_dumb.c
790 +++ b/net/ipv4/ip_nat_dumb.c
791 @@ -124,6 +124,7 @@ ip_do_nat(struct sk_buff *skb)
792                                         key.dst = ciph->saddr;
793                                         key.iif = skb->dev->ifindex;
794                                         key.oif = 0;
795 +                                       key.gw  = 0;
796  #ifdef CONFIG_IP_ROUTE_TOS
797                                         key.tos = RT_TOS(ciph->tos);
798  #endif
799 --- a/net/ipv4/netfilter/ip_fw_compat_masq.c
800 +++ b/net/ipv4/netfilter/ip_fw_compat_masq.c
801 @@ -41,6 +41,10 @@ do_masquerade(struct sk_buff **pskb, con
802         enum ip_conntrack_info ctinfo;
803         struct ip_conntrack *ct;
804         unsigned int ret;
805 +       struct rtable *rt, *skb_rt;
806 +       struct net_device *skb_dev;
807 +       __u32 saddr;
808 +       int new;
809  
810         /* Sorry, only ICMP, TCP and UDP. */
811         if (iph->protocol != IPPROTO_ICMP
812 @@ -64,22 +68,28 @@ do_masquerade(struct sk_buff **pskb, con
813         }
814  
815         info = &ct->nat.info;
816 +       iph = (*pskb)->nh.iph;
817 +       saddr = iph->saddr;
818 +       new = 0;
819  
820         WRITE_LOCK(&ip_nat_lock);
821         /* Setup the masquerade, if not already */
822         if (!info->initialized) {
823                 u_int32_t newsrc;
824 -               struct rtable *rt;
825                 struct ip_nat_multi_range range;
826  
827 +               skb_rt = (struct rtable *) (*pskb)->dst;
828 +               skb_dev = skb_rt->u.dst.dev;
829                 /* Pass 0 instead of saddr, since it's going to be changed
830                    anyway. */
831 -               if (ip_route_output(&rt, iph->daddr, 0, 0, 0) != 0) {
832 +               if (ip_route_output_lookup(&rt, iph->daddr, 0, RT_TOS(iph->tos),
833 +                   skb_dev? skb_dev->ifindex : 0,
834 +                   skb_dev? skb_rt->rt_gateway : 0) != 0) {
835 +                       WRITE_UNLOCK(&ip_nat_lock);
836                         DEBUGP("ipnat_rule_masquerade: Can't reroute.\n");
837                         return NF_DROP;
838                 }
839 -               newsrc = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
840 -                                         RT_SCOPE_UNIVERSE);
841 +               newsrc = rt->rt_src;
842                 ip_rt_put(rt);
843                 range = ((struct ip_nat_multi_range)
844                          { 1,
845 @@ -92,11 +102,31 @@ do_masquerade(struct sk_buff **pskb, con
846                         WRITE_UNLOCK(&ip_nat_lock);
847                         return ret;
848                 }
849 +               new = 1;
850         } else
851                 DEBUGP("Masquerading already done on this conn.\n");
852         WRITE_UNLOCK(&ip_nat_lock);
853  
854 -       return do_bindings(ct, ctinfo, info, NF_IP_POST_ROUTING, pskb);
855 +       ret = do_bindings(ct, ctinfo, info, NF_IP_POST_ROUTING, pskb);
856 +       if (ret != NF_ACCEPT || saddr == (*pskb)->nh.iph->saddr || new)
857 +               return ret;
858 +
859 +       iph = (*pskb)->nh.iph;
860 +       if (ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), 0) != 0)
861 +               return NF_DROP;
862 +       
863 +       skb_rt = (struct rtable *) (*pskb)->dst;
864 +       skb_dev = skb_rt->u.dst.dev;
865 +       if (skb_dev != rt->u.dst.dev || rt->rt_gateway != skb_rt->rt_gateway) {
866 +               if (skb_dev != rt->u.dst.dev) {
867 +                       /* TODO: check the new mtu and reply FRAG_NEEDED */
868 +               }
869 +               dst_release((*pskb)->dst);
870 +               (*pskb)->dst = &rt->u.dst;
871 +       } else {
872 +               ip_rt_put(rt);
873 +       }
874 +       return NF_ACCEPT;
875  }
876  
877  void
878 --- a/net/ipv4/netfilter/ip_nat_core.c
879 +++ b/net/ipv4/netfilter/ip_nat_core.c
880 @@ -994,6 +994,60 @@ icmp_reply_translation(struct sk_buff *s
881         return NF_ACCEPT;
882  }
883  
884 +unsigned int
885 +ip_nat_route_input(unsigned int hooknum,
886 +               struct sk_buff **pskb,
887 +               const struct net_device *in,
888 +               const struct net_device *out,
889 +               int (*okfn)(struct sk_buff *))
890 +{
891 +       struct sk_buff *skb = *pskb;
892 +       struct iphdr *iph;
893 +       struct ip_conntrack *ct;
894 +       enum ip_conntrack_info ctinfo;
895 +       struct ip_nat_info *info;
896 +       enum ip_conntrack_dir dir;
897 +       __u32 saddr;
898 +       int i;
899 +
900 +       if (!(ct = ip_conntrack_get(skb, &ctinfo)))
901 +               return NF_ACCEPT;
902 +
903 +       info = &ct->nat.info;
904 +       if (!info->initialized)
905 +               return NF_ACCEPT;
906 +
907 +       if (skb->dst)
908 +               return NF_ACCEPT;
909 +
910 +       if (skb->len < sizeof(struct iphdr))
911 +               return NF_ACCEPT;
912 +
913 +       iph = skb->nh.iph;
914 +       saddr = iph->saddr;
915 +       hooknum = NF_IP_POST_ROUTING;
916 +       dir = CTINFO2DIR(ctinfo);
917 +
918 +       READ_LOCK(&ip_nat_lock);
919 +       for (i = 0; i < info->num_manips; i++) {
920 +               if (info->manips[i].direction == dir
921 +                   && info->manips[i].hooknum == hooknum
922 +                   && info->manips[i].maniptype == IP_NAT_MANIP_SRC) {
923 +                       saddr = info->manips[i].manip.ip;
924 +               }
925 +       }
926 +       READ_UNLOCK(&ip_nat_lock);
927 +
928 +       if (saddr == iph->saddr)
929 +               return NF_ACCEPT;
930 +
931 +       if (ip_route_input_lookup(skb, iph->daddr, iph->saddr, iph->tos,
932 +           skb->dev, saddr))
933 +               return NF_DROP;
934 +
935 +       return NF_ACCEPT;
936 +}
937 +
938  int __init ip_nat_init(void)
939  {
940         size_t i;
941 --- a/net/ipv4/netfilter/ip_nat_standalone.c
942 +++ b/net/ipv4/netfilter/ip_nat_standalone.c
943 @@ -245,6 +245,9 @@ ip_nat_local_fn(unsigned int hooknum,
944  /* Before packet filtering, change destination */
945  static struct nf_hook_ops ip_nat_in_ops
946  = { { NULL, NULL }, ip_nat_in, PF_INET, NF_IP_PRE_ROUTING, NF_IP_PRI_NAT_DST };
947 +/* Before routing, route before mangling */
948 +static struct nf_hook_ops ip_nat_inr_ops
949 += { { NULL, NULL }, ip_nat_route_input, PF_INET, NF_IP_PRE_ROUTING, NF_IP_PRI_LAST-1 };
950  /* After packet filtering, change source */
951  static struct nf_hook_ops ip_nat_out_ops
952  = { { NULL, NULL }, ip_nat_out, PF_INET, NF_IP_POST_ROUTING, NF_IP_PRI_NAT_SRC};
953 @@ -313,10 +316,15 @@ static int init_or_cleanup(int init)
954                 printk("ip_nat_init: can't register in hook.\n");
955                 goto cleanup_nat;
956         }
957 +       ret = nf_register_hook(&ip_nat_inr_ops);
958 +       if (ret < 0) {
959 +               printk("ip_nat_init: can't register inr hook.\n");
960 +               goto cleanup_inops;
961 +       }
962         ret = nf_register_hook(&ip_nat_out_ops);
963         if (ret < 0) {
964                 printk("ip_nat_init: can't register out hook.\n");
965 -               goto cleanup_inops;
966 +               goto cleanup_inrops;
967         }
968         ret = nf_register_hook(&ip_nat_local_out_ops);
969         if (ret < 0) {
970 @@ -336,6 +344,8 @@ static int init_or_cleanup(int init)
971         nf_unregister_hook(&ip_nat_local_out_ops);
972   cleanup_outops:
973         nf_unregister_hook(&ip_nat_out_ops);
974 + cleanup_inrops:
975 +       nf_unregister_hook(&ip_nat_inr_ops);
976   cleanup_inops:
977         nf_unregister_hook(&ip_nat_in_ops);
978   cleanup_nat:
979 --- a/net/ipv4/netfilter/ipt_MASQUERADE.c
980 +++ b/net/ipv4/netfilter/ipt_MASQUERADE.c
981 @@ -87,7 +87,8 @@ masquerade_target(struct sk_buff **pskb,
982         key.dst = (*pskb)->nh.iph->daddr;
983         key.src = 0; /* Unknown: that's what we're trying to establish */
984         key.tos = RT_TOS((*pskb)->nh.iph->tos)|RTO_CONN;
985 -       key.oif = 0;
986 +       key.oif = out->ifindex;
987 +       key.gw  = ((struct rtable *) (*pskb)->dst)->rt_gateway;
988  #ifdef CONFIG_IP_ROUTE_FWMARK
989         key.fwmark = (*pskb)->nfmark;
990  #endif
991 @@ -98,13 +99,6 @@ masquerade_target(struct sk_buff **pskb,
992                                 " No route: Rusty's brain broke!\n");
993                  return NF_DROP;
994          }
995 -        if (rt->u.dst.dev != out) {
996 -                if (net_ratelimit())
997 -                        printk("MASQUERADE:"
998 -                               " Route sent us somewhere else.\n");
999 -                       ip_rt_put(rt);
1000 -               return NF_DROP;
1001 -       }
1002  
1003         newsrc = rt->rt_src;
1004         DEBUGP("newsrc = %u.%u.%u.%u\n", NIPQUAD(newsrc));
1005 --- a/net/ipv4/route.c
1006 +++ b/net/ipv4/route.c
1007 @@ -919,6 +919,7 @@ void ip_rt_redirect(u32 old_gw, u32 dadd
1008  
1009                                 /* Gateway is different ... */
1010                                 rt->rt_gateway          = new_gw;
1011 +                               if (rt->key.gw) rt->key.gw = new_gw;
1012  
1013                                 /* Redirect received -> path was valid */
1014                                 dst_confirm(&rth->u.dst);
1015 @@ -1343,6 +1344,7 @@ static int ip_route_input_mc(struct sk_b
1016         rth->key.fwmark = skb->nfmark;
1017  #endif
1018         rth->key.src    = saddr;
1019 +       rth->key.lsrc   = 0;
1020         rth->rt_src     = saddr;
1021  #ifdef CONFIG_IP_ROUTE_NAT
1022         rth->rt_dst_map = daddr;
1023 @@ -1356,6 +1358,7 @@ static int ip_route_input_mc(struct sk_b
1024         rth->u.dst.dev  = &loopback_dev;
1025         dev_hold(rth->u.dst.dev);
1026         rth->key.oif    = 0;
1027 +       rth->key.gw     = 0;
1028         rth->rt_gateway = daddr;
1029         rth->rt_spec_dst= spec_dst;
1030         rth->rt_type    = RTN_MULTICAST;
1031 @@ -1395,7 +1398,7 @@ e_inval:
1032   */
1033  
1034  int ip_route_input_slow(struct sk_buff *skb, u32 daddr, u32 saddr,
1035 -                       u8 tos, struct net_device *dev)
1036 +                       u8 tos, struct net_device *dev, u32 lsrc)
1037  {
1038         struct rt_key   key;
1039         struct fib_result res;
1040 @@ -1415,16 +1418,17 @@ int ip_route_input_slow(struct sk_buff *
1041                 goto out;
1042  
1043         key.dst         = daddr;
1044 -       key.src         = saddr;
1045 +       key.src         = lsrc? : saddr;
1046         key.tos         = tos;
1047  #ifdef CONFIG_IP_ROUTE_FWMARK
1048         key.fwmark      = skb->nfmark;
1049  #endif
1050 -       key.iif         = dev->ifindex;
1051 +       key.iif         = lsrc? loopback_dev.ifindex : dev->ifindex;
1052         key.oif         = 0;
1053 +       key.gw          = 0;
1054         key.scope       = RT_SCOPE_UNIVERSE;
1055  
1056 -       hash = rt_hash_code(daddr, saddr ^ (key.iif << 5), tos);
1057 +       hash = rt_hash_code(daddr, saddr ^ (dev->ifindex << 5), tos);
1058  
1059         /* Check for the most weird martians, which can be not detected
1060            by fib_lookup.
1061 @@ -1445,6 +1449,12 @@ int ip_route_input_slow(struct sk_buff *
1062         if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
1063                 goto martian_destination;
1064  
1065 +       if (lsrc) {
1066 +               if (MULTICAST(lsrc) || BADCLASS(lsrc) ||
1067 +                   ZERONET(lsrc) || LOOPBACK(lsrc))
1068 +                       goto e_inval;
1069 +       }
1070 +
1071         /*
1072          *      Now we are ready to route packet.
1073          */
1074 @@ -1454,6 +1464,10 @@ int ip_route_input_slow(struct sk_buff *
1075                 goto no_route;
1076         }
1077         free_res = 1;
1078 +       if (lsrc && res.type != RTN_UNICAST && res.type != RTN_NAT)
1079 +               goto e_inval;
1080 +       key.iif = dev->ifindex;
1081 +       key.src = saddr;
1082  
1083         rt_cache_stat[smp_processor_id()].in_slow_tot++;
1084  
1085 @@ -1464,7 +1478,7 @@ int ip_route_input_slow(struct sk_buff *
1086  
1087         if (1) {
1088                 u32 src_map = saddr;
1089 -               if (res.r)
1090 +               if (res.r && !lsrc)
1091                         src_map = fib_rules_policy(saddr, &res, &flags);
1092  
1093                 if (res.type == RTN_NAT) {
1094 @@ -1503,8 +1517,9 @@ int ip_route_input_slow(struct sk_buff *
1095         if (res.type != RTN_UNICAST)
1096                 goto martian_destination;
1097  
1098 +       fib_select_default(&key, &res);
1099  #ifdef CONFIG_IP_ROUTE_MULTIPATH
1100 -       if (res.fi->fib_nhs > 1 && key.oif == 0)
1101 +       if (res.fi->fib_nhs > 1)
1102                 fib_select_multipath(&key, &res);
1103  #endif
1104         out_dev = in_dev_get(FIB_RES_DEV(res));
1105 @@ -1524,6 +1539,7 @@ int ip_route_input_slow(struct sk_buff *
1106                 flags |= RTCF_DIRECTSRC;
1107  
1108         if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
1109 +           !lsrc &&
1110             (IN_DEV_SHARED_MEDIA(out_dev) ||
1111              inet_addr_onlink(out_dev, saddr, FIB_RES_GW(res))))
1112                 flags |= RTCF_DOREDIRECT;
1113 @@ -1550,6 +1566,7 @@ int ip_route_input_slow(struct sk_buff *
1114  #endif
1115         rth->key.src    = saddr;
1116         rth->rt_src     = saddr;
1117 +       rth->key.lsrc   = lsrc;
1118         rth->rt_gateway = daddr;
1119  #ifdef CONFIG_IP_ROUTE_NAT
1120         rth->rt_src_map = key.src;
1121 @@ -1562,6 +1579,7 @@ int ip_route_input_slow(struct sk_buff *
1122         rth->u.dst.dev  = out_dev->dev;
1123         dev_hold(rth->u.dst.dev);
1124         rth->key.oif    = 0;
1125 +       rth->key.gw     = 0;
1126         rth->rt_spec_dst= spec_dst;
1127  
1128         rth->u.dst.input = ip_forward;
1129 @@ -1572,7 +1590,8 @@ int ip_route_input_slow(struct sk_buff *
1130         rth->rt_flags = flags;
1131  
1132  #ifdef CONFIG_NET_FASTROUTE
1133 -       if (netdev_fastroute && !(flags&(RTCF_NAT|RTCF_MASQ|RTCF_DOREDIRECT))) {
1134 +       if (netdev_fastroute && !(flags&(RTCF_NAT|RTCF_MASQ|RTCF_DOREDIRECT)) &&
1135 +           !lsrc) {
1136                 struct net_device *odev = rth->u.dst.dev;
1137                 if (odev != dev &&
1138                     dev->accept_fastpath &&
1139 @@ -1595,6 +1614,8 @@ out:      return err;
1140  brd_input:
1141         if (skb->protocol != htons(ETH_P_IP))
1142                 goto e_inval;
1143 +       if (lsrc)
1144 +               goto e_inval;
1145  
1146         if (ZERONET(saddr))
1147                 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
1148 @@ -1627,6 +1648,7 @@ local_input:
1149  #endif
1150         rth->key.src    = saddr;
1151         rth->rt_src     = saddr;
1152 +       rth->key.lsrc   = 0;
1153  #ifdef CONFIG_IP_ROUTE_NAT
1154         rth->rt_dst_map = key.dst;
1155         rth->rt_src_map = key.src;
1156 @@ -1639,6 +1661,7 @@ local_input:
1157         rth->u.dst.dev  = &loopback_dev;
1158         dev_hold(rth->u.dst.dev);
1159         rth->key.oif    = 0;
1160 +       rth->key.gw     = 0;
1161         rth->rt_gateway = daddr;
1162         rth->rt_spec_dst= spec_dst;
1163         rth->u.dst.input= ip_local_deliver;
1164 @@ -1704,8 +1727,9 @@ martian_source:
1165         goto e_inval;
1166  }
1167  
1168 -int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
1169 -                  u8 tos, struct net_device *dev)
1170 +static inline int
1171 +ip_route_input_cached(struct sk_buff *skb, u32 daddr, u32 saddr,
1172 +                     u8 tos, struct net_device *dev, u32 lsrc)
1173  {
1174         struct rtable * rth;
1175         unsigned        hash;
1176 @@ -1719,6 +1743,7 @@ int ip_route_input(struct sk_buff *skb, 
1177                 if (rth->key.dst == daddr &&
1178                     rth->key.src == saddr &&
1179                     rth->key.iif == iif &&
1180 +                   rth->key.lsrc == lsrc &&
1181                     rth->key.oif == 0 &&
1182  #ifdef CONFIG_IP_ROUTE_FWMARK
1183                     rth->key.fwmark == skb->nfmark &&
1184 @@ -1766,9 +1791,21 @@ int ip_route_input(struct sk_buff *skb, 
1185                 read_unlock(&inetdev_lock);
1186                 return -EINVAL;
1187         }
1188 -       return ip_route_input_slow(skb, daddr, saddr, tos, dev);
1189 +       return ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
1190 +}
1191 +
1192 +int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
1193 +                  u8 tos, struct net_device *dev)
1194 +{
1195 +       return ip_route_input_cached(skb, daddr, saddr, tos, dev, 0);
1196  }
1197  
1198 +int ip_route_input_lookup(struct sk_buff *skb, u32 daddr, u32 saddr,
1199 +                         u8 tos, struct net_device *dev, u32 lsrc)
1200 +{
1201 +       return ip_route_input_cached(skb, daddr, saddr, tos, dev, lsrc);
1202 +}
1203
1204  /*
1205   * Major route resolver routine.
1206   */
1207 @@ -1791,6 +1828,7 @@ int ip_route_output_slow(struct rtable *
1208         key.tos         = tos & IPTOS_RT_MASK;
1209         key.iif         = loopback_dev.ifindex;
1210         key.oif         = oldkey->oif;
1211 +       key.gw          = oldkey->gw;
1212  #ifdef CONFIG_IP_ROUTE_FWMARK
1213         key.fwmark      = oldkey->fwmark;
1214  #endif
1215 @@ -1880,6 +1918,7 @@ int ip_route_output_slow(struct rtable *
1216                 dev_out = &loopback_dev;
1217                 dev_hold(dev_out);
1218                 key.oif = loopback_dev.ifindex;
1219 +               key.gw = 0;
1220                 res.type = RTN_LOCAL;
1221                 flags |= RTCF_LOCAL;
1222                 goto make_route;
1223 @@ -1887,7 +1926,7 @@ int ip_route_output_slow(struct rtable *
1224  
1225         if (fib_lookup(&key, &res)) {
1226                 res.fi = NULL;
1227 -               if (oldkey->oif) {
1228 +               if (oldkey->oif && dev_out->flags&IFF_UP) {
1229                         /* Apparently, routing tables are wrong. Assume,
1230                            that the destination is on link.
1231  
1232 @@ -1930,6 +1969,7 @@ int ip_route_output_slow(struct rtable *
1233                 dev_out = &loopback_dev;
1234                 dev_hold(dev_out);
1235                 key.oif = dev_out->ifindex;
1236 +               key.gw = 0;
1237                 if (res.fi)
1238                         fib_info_put(res.fi);
1239                 res.fi = NULL;
1240 @@ -1937,13 +1977,12 @@ int ip_route_output_slow(struct rtable *
1241                 goto make_route;
1242         }
1243  
1244 +       if (res.type == RTN_UNICAST)
1245 +               fib_select_default(&key, &res);
1246  #ifdef CONFIG_IP_ROUTE_MULTIPATH
1247 -       if (res.fi->fib_nhs > 1 && key.oif == 0)
1248 +       if (res.fi->fib_nhs > 1)
1249                 fib_select_multipath(&key, &res);
1250 -       else
1251  #endif
1252 -       if (!res.prefixlen && res.type == RTN_UNICAST && !key.oif)
1253 -               fib_select_default(&key, &res);
1254  
1255         if (!key.src)
1256                 key.src = FIB_RES_PREFSRC(res);
1257 @@ -2001,7 +2040,9 @@ make_route:
1258         rth->key.tos    = tos;
1259         rth->key.src    = oldkey->src;
1260         rth->key.iif    = 0;
1261 +       rth->key.lsrc   = 0;
1262         rth->key.oif    = oldkey->oif;
1263 +       rth->key.gw     = oldkey->gw;
1264  #ifdef CONFIG_IP_ROUTE_FWMARK
1265         rth->key.fwmark = oldkey->fwmark;
1266  #endif
1267 @@ -2080,6 +2121,7 @@ int ip_route_output_key(struct rtable **
1268                     rth->key.src == key->src &&
1269                     rth->key.iif == 0 &&
1270                     rth->key.oif == key->oif &&
1271 +                   rth->key.gw == key->gw &&
1272  #ifdef CONFIG_IP_ROUTE_FWMARK
1273                     rth->key.fwmark == key->fwmark &&
1274  #endif
1275 --- a/net/netsyms.c
1276 +++ b/net/netsyms.c
1277 @@ -260,6 +260,7 @@ EXPORT_SYMBOL(inet_register_protosw);
1278  EXPORT_SYMBOL(inet_unregister_protosw);
1279  EXPORT_SYMBOL(ip_route_output_key);
1280  EXPORT_SYMBOL(ip_route_input);
1281 +EXPORT_SYMBOL(ip_route_input_lookup);
1282  EXPORT_SYMBOL(icmp_send);
1283  EXPORT_SYMBOL(icmp_statistics);
1284  EXPORT_SYMBOL(icmp_err_convert);