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,
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 *));
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)
26 /* Macros to handle hexthops */
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
32 static inline void fib_select_default(const struct rt_key *key, struct fib_result *res)
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);
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);
46 static inline struct fib_table *fib_get_table(int id)
48 @@ -275,5 +277,6 @@ static inline void fib_res_put(struct fi
52 +extern rwlock_t fib_nhflags_lock;
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
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
79 +ip_route_output_lookup(struct rtable **rp,
80 + u32 daddr, u32 saddr, u32 tos, int oif, u32 gw)
82 + struct rt_key key = { dst:daddr, src:saddr, gw:gw, oif:oif, tos:tos };
84 + return ip_route_output_key(rp, &key);
87 static inline void ip_rt_put(struct rtable * rt)
90 --- a/net/ipv4/fib_frontend.c
91 +++ b/net/ipv4/fib_frontend.c
93 struct fib_table *local_table;
94 struct fib_table *main_table;
96 +#define FIB_RES_TABLE(r) (RT_TABLE_MAIN)
100 #define RT_TABLE_MIN 1
101 @@ -71,6 +73,7 @@ struct fib_table *__fib_new_table(int id
105 +#define FIB_RES_TABLE(r) (fib_result_table(r))
107 #endif /* CONFIG_IP_MULTIPLE_TABLES */
109 @@ -209,6 +212,9 @@ int fib_validate_source(u32 src, u32 dst
110 struct in_device *in_dev;
112 struct fib_result res;
114 + unsigned char prefixlen;
115 + unsigned char scope;
119 @@ -216,6 +222,7 @@ int fib_validate_source(u32 src, u32 dst
125 key.scope = RT_SCOPE_UNIVERSE;
127 @@ -237,31 +244,35 @@ int fib_validate_source(u32 src, u32 dst
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)
134 if (FIB_RES_DEV(res) == dev)
137 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
141 + table = FIB_RES_TABLE(&res);
142 + prefixlen = res.prefixlen;
149 key.oif = dev->ifindex;
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) ||
158 *spec_dst = FIB_RES_PREFSRC(res);
159 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
170 @@ -579,9 +590,7 @@ static int fib_inetaddr_event(struct not
174 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
175 fib_sync_up(ifa->ifa_dev->dev);
180 @@ -617,9 +626,7 @@ static int fib_netdev_event(struct notif
183 } endfor_ifa(in_dev);
184 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
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)
200 @@ -336,72 +337,123 @@ out:
204 -static int fn_hash_last_dflt=-1;
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)
213 - int state = NUD_NONE;
216 + struct fib_nh * nh;
218 + int flag, dead = 1;
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)
224 + if (key->gw && key->gw != nh->nh_gw && nh->nh_gw &&
225 + nh->nh_scope == RT_SCOPE_LINK)
227 + if (nh->nh_flags & RTNH_F_DEAD)
230 - n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
232 - state = n->nud_state;
235 + if (nh->nh_dev->flags & IFF_NOARP) {
241 + if (!nh->nh_gw || nh->nh_scope != RT_SCOPE_LINK)
245 + n = neigh_lookup(&arp_tbl, &dst, nh->nh_dev);
247 + state = n->nud_state;
250 + if (state==NUD_REACHABLE ||
251 + ((state&NUD_VALID) && order != last_dflt)) {
255 + if (!(state&NUD_VALID))
259 + if ((state&NUD_VALID) ||
260 + (*last_idx<0 && order >= last_dflt)) {
263 + *last_nhsel = nhsel;
268 + read_lock_bh(&fib_nhflags_lock);
270 + nh->nh_flags |= RTNH_F_SUSPECT;
272 + nh->nh_flags &= ~RTNH_F_SUSPECT;
273 + read_unlock_bh(&fib_nhflags_lock);
275 - if (state==NUD_REACHABLE)
277 - if ((state&NUD_VALID) && order != fn_hash_last_dflt)
279 - if ((state&NUD_VALID) ||
280 - (*last_idx<0 && order > fn_hash_last_dflt)) {
285 + /* } endfor_nexthops(fi) */
291 fn_hash_select_default(struct fib_table *tb, const struct rt_key *key, struct fib_result *res)
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];
307 + k = fz_key(key->dst, fz);
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);
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) ||
327 f->fn_type != RTN_UNICAST)
330 if (next_fi->fib_priority > res->fi->fib_priority)
332 - if (!next_fi->fib_nh[0].nh_gw || next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
334 f->fn_state |= FN_S_ACCESSED;
337 - if (next_fi != res->fi)
339 - } else if (!fib_detect_death(fi, order, &last_resort, &last_idx)) {
341 + last_dflt = f->fn_last_dflt;
344 + if (fi && !fib_detect_death(fi, order, last_dflt,
345 + &last_resort, &last_idx, &last_nhsel, key)) {
347 fib_info_put(res->fi);
349 atomic_inc(&fi->fib_clntref);
350 - fn_hash_last_dflt = order;
351 + first_node->fn_last_dflt = order;
355 @@ -409,16 +461,25 @@ fn_hash_select_default(struct fib_table
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);
368 + if (first_node) first_node->fn_last_dflt = -1;
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)) {
376 fib_info_put(res->fi);
378 atomic_inc(&fi->fib_clntref);
379 - fn_hash_last_dflt = order;
380 + first_node->fn_last_dflt = order;
384 @@ -428,8 +489,11 @@ fn_hash_select_default(struct fib_table
385 res->fi = 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;
393 - fn_hash_last_dflt = last_idx;
395 read_unlock(&fib_hash_lock);
397 @@ -589,6 +653,7 @@ replace:
399 memset(new_f, 0, sizeof(struct fib_node));
401 + new_f->fn_last_dflt = -1;
403 #ifdef CONFIG_IP_ROUTE_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_
411 +int fib_result_table(struct fib_result *res)
413 + return res->r->r_table;
416 int fib_lookup(const struct rt_key *key, struct fib_result *res)
419 @@ -371,8 +376,10 @@ FRprintk("FAILURE\n");
421 void fib_select_default(const struct rt_key *key, struct fib_result *res)
423 - if (res->r && res->r->r_action == RTN_UNICAST &&
424 - FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
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
435 static struct fib_info *fib_info_list;
436 static rwlock_t fib_info_lock = RW_LOCK_UNLOCKED;
438 +rwlock_t fib_nhflags_lock = RW_LOCK_UNLOCKED;
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 ||
446 - ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
447 + ((nh->nh_flags^onh->nh_flags)&~RTNH_F_BADSTATE))
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))
460 @@ -365,8 +366,11 @@ static int fib_check_nh(const struct rtm
462 if ((dev = __dev_get_by_index(nh->nh_oif)) == NULL)
464 - if (!(dev->flags&IFF_UP))
466 + if (!(dev->flags&IFF_UP)) {
467 + if (fi->fib_protocol != RTPROT_STATIC)
469 + nh->nh_flags |= RTNH_F_DEAD;
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)
481 - if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
483 - nh->nh_scope = res.scope;
484 - nh->nh_oif = FIB_RES_OIF(res);
485 - if ((nh->nh_dev = FIB_RES_DEV(res)) == NULL)
487 - dev_hold(nh->nh_dev);
489 - if (!(nh->nh_dev->flags & IFF_UP))
493 + err = fib_lookup(&key, &res);
495 + struct in_device *in_dev;
497 + if (err != -ENETUNREACH ||
498 + fi->fib_protocol != RTPROT_STATIC)
501 + in_dev = inetdev_by_index(nh->nh_oif);
502 + if (in_dev == NULL ||
503 + in_dev->dev->flags & IFF_UP) {
505 + in_dev_put(in_dev);
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);
515 + if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
517 + nh->nh_scope = res.scope;
518 + nh->nh_oif = FIB_RES_OIF(res);
519 + if ((nh->nh_dev = FIB_RES_DEV(res)) == NULL)
521 + dev_hold(nh->nh_dev);
522 + if (!(nh->nh_dev->flags & IFF_UP)) {
523 + if (fi->fib_protocol != RTPROT_STATIC) {
527 + nh->nh_flags |= RTNH_F_DEAD;
537 struct in_device *in_dev;
539 @@ -407,8 +436,11 @@ out:
542 if (!(in_dev->dev->flags&IFF_UP)) {
543 - in_dev_put(in_dev);
545 + if (fi->fib_protocol != RTPROT_STATIC) {
546 + in_dev_put(in_dev);
549 + nh->nh_flags |= RTNH_F_DEAD;
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_
555 if (nh->nh_flags&RTNH_F_DEAD)
557 - if (!key->oif || key->oif == nh->nh_oif)
559 + if (key->oif && key->oif != nh->nh_oif)
561 + if (key->gw && key->gw != nh->nh_gw &&
562 + nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
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;
572 - } else if (dev && fi->fib_nhs) {
573 + } else if (fi->fib_nhs) {
576 change_nexthops(fi) {
577 - if (nh->nh_flags&RTNH_F_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)
588 + } else if ((nh->nh_dev == dev && dev &&
589 + nh->nh_scope != scope) ||
590 + (local == nh->nh_gw && local &&
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;
599 - spin_unlock_bh(&fib_multipath_lock);
600 + spin_unlock(&fib_multipath_lock);
602 + nh->nh_flags |= RTNH_F_DEAD;
605 + write_unlock_bh(&fib_nhflags_lock);
606 + if (fi->fib_protocol!=RTPROT_STATIC ||
608 + (dev && __in_dev_get(dev) == NULL))
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_
617 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
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.
625 int fib_sync_up(struct net_device *dev)
629 + struct fib_result res;
633 if (!(dev->flags&IFF_UP))
641 change_nexthops(fi) {
642 - if (!(nh->nh_flags&RTNH_F_DEAD)) {
644 + if (!(nh->nh_flags&RTNH_F_DEAD))
647 if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
649 if (nh->nh_dev != dev || __in_dev_get(dev) == NULL)
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)
658 + if (res.type != RTN_UNICAST &&
659 + res.type != RTN_LOCAL) {
663 + nh->nh_scope = res.scope;
668 +#ifdef CONFIG_IP_ROUTE_MULTIPATH
669 spin_lock_bh(&fib_multipath_lock);
671 nh->nh_flags &= ~RTNH_F_DEAD;
672 spin_unlock_bh(&fib_multipath_lock);
674 } endfor_nexthops(fi)
677 @@ -944,9 +1011,13 @@ int fib_sync_up(struct net_device *dev)
686 +#ifdef CONFIG_IP_ROUTE_MULTIPATH
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)
694 struct fib_info *fi = res->fi;
698 spin_lock_bh(&fib_multipath_lock);
702 + change_nexthops(fi) {
703 + if (key->oif != nh->nh_oif)
705 + if (key->gw && key->gw != nh->nh_gw &&
706 + nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
708 + if (!(nh->nh_flags&RTNH_F_BADSTATE)) {
709 + if (nh->nh_power > w) {
714 + } endfor_nexthops(fi);
716 + spin_unlock_bh(&fib_multipath_lock);
724 if (fi->fib_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;
732 } endfor_nexthops(fi);
733 fi->fib_power = power;
735 - spin_unlock_bh(&fib_multipath_lock);
736 - /* Race condition: route has just become dead. */
745 @@ -982,20 +1074,40 @@ void fib_select_multipath(const struct r
747 w = jiffies % fi->fib_power;
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) {
756 - res->nh_sel = nhsel;
757 spin_unlock_bh(&fib_multipath_lock);
758 + res->nh_sel = nhsel;
763 + } endfor_nexthops(fi);
772 + if (!(nh->nh_flags&RTNH_F_DEAD)) {
773 + if (key->oif && key->oif != nh->nh_oif)
775 + if (key->gw && key->gw != nh->nh_gw &&
776 + nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
778 + spin_unlock_bh(&fib_multipath_lock);
779 + res->nh_sel = nhsel;
782 } endfor_nexthops(fi);
784 /* Race condition: route has just become dead. */
786 spin_unlock_bh(&fib_multipath_lock);
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;
796 #ifdef CONFIG_IP_ROUTE_TOS
797 key.tos = RT_TOS(ciph->tos);
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;
805 + struct rtable *rt, *skb_rt;
806 + struct net_device *skb_dev;
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
815 info = &ct->nat.info;
816 + iph = (*pskb)->nh.iph;
817 + saddr = iph->saddr;
820 WRITE_LOCK(&ip_nat_lock);
821 /* Setup the masquerade, if not already */
822 if (!info->initialized) {
825 struct ip_nat_multi_range range;
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
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");
839 - newsrc = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
840 - RT_SCOPE_UNIVERSE);
841 + newsrc = rt->rt_src;
843 range = ((struct ip_nat_multi_range)
845 @@ -92,11 +102,31 @@ do_masquerade(struct sk_buff **pskb, con
846 WRITE_UNLOCK(&ip_nat_lock);
851 DEBUGP("Masquerading already done on this conn.\n");
852 WRITE_UNLOCK(&ip_nat_lock);
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)
859 + iph = (*pskb)->nh.iph;
860 + if (ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), 0) != 0)
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 */
869 + dst_release((*pskb)->dst);
870 + (*pskb)->dst = &rt->u.dst;
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
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 *))
891 + struct sk_buff *skb = *pskb;
893 + struct ip_conntrack *ct;
894 + enum ip_conntrack_info ctinfo;
895 + struct ip_nat_info *info;
896 + enum ip_conntrack_dir dir;
900 + if (!(ct = ip_conntrack_get(skb, &ctinfo)))
903 + info = &ct->nat.info;
904 + if (!info->initialized)
910 + if (skb->len < sizeof(struct iphdr))
914 + saddr = iph->saddr;
915 + hooknum = NF_IP_POST_ROUTING;
916 + dir = CTINFO2DIR(ctinfo);
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;
926 + READ_UNLOCK(&ip_nat_lock);
928 + if (saddr == iph->saddr)
931 + if (ip_route_input_lookup(skb, iph->daddr, iph->saddr, iph->tos,
938 int __init ip_nat_init(void)
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");
957 + ret = nf_register_hook(&ip_nat_inr_ops);
959 + printk("ip_nat_init: can't register inr hook.\n");
960 + goto cleanup_inops;
962 ret = nf_register_hook(&ip_nat_out_ops);
964 printk("ip_nat_init: can't register out hook.\n");
965 - goto cleanup_inops;
966 + goto cleanup_inrops;
968 ret = nf_register_hook(&ip_nat_local_out_ops);
970 @@ -336,6 +344,8 @@ static int init_or_cleanup(int init)
971 nf_unregister_hook(&ip_nat_local_out_ops);
973 nf_unregister_hook(&ip_nat_out_ops);
975 + nf_unregister_hook(&ip_nat_inr_ops);
977 nf_unregister_hook(&ip_nat_in_ops);
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;
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;
991 @@ -98,13 +99,6 @@ masquerade_target(struct sk_buff **pskb,
992 " No route: Rusty's brain broke!\n");
995 - if (rt->u.dst.dev != out) {
996 - if (net_ratelimit())
997 - printk("MASQUERADE:"
998 - " Route sent us somewhere else.\n");
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
1009 /* Gateway is different ... */
1010 rt->rt_gateway = new_gw;
1011 + if (rt->key.gw) rt->key.gw = new_gw;
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;
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);
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:
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)
1039 struct fib_result res;
1040 @@ -1415,16 +1418,17 @@ int ip_route_input_slow(struct sk_buff *
1045 + key.src = lsrc? : saddr;
1047 #ifdef CONFIG_IP_ROUTE_FWMARK
1048 key.fwmark = skb->nfmark;
1050 - key.iif = dev->ifindex;
1051 + key.iif = lsrc? loopback_dev.ifindex : dev->ifindex;
1054 key.scope = RT_SCOPE_UNIVERSE;
1056 - hash = rt_hash_code(daddr, saddr ^ (key.iif << 5), tos);
1057 + hash = rt_hash_code(daddr, saddr ^ (dev->ifindex << 5), tos);
1059 /* Check for the most weird martians, which can be not detected
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;
1066 + if (MULTICAST(lsrc) || BADCLASS(lsrc) ||
1067 + ZERONET(lsrc) || LOOPBACK(lsrc))
1072 * Now we are ready to route packet.
1074 @@ -1454,6 +1464,10 @@ int ip_route_input_slow(struct sk_buff *
1078 + if (lsrc && res.type != RTN_UNICAST && res.type != RTN_NAT)
1080 + key.iif = dev->ifindex;
1083 rt_cache_stat[smp_processor_id()].in_slow_tot++;
1085 @@ -1464,7 +1478,7 @@ int ip_route_input_slow(struct sk_buff *
1088 u32 src_map = saddr;
1090 + if (res.r && !lsrc)
1091 src_map = fib_rules_policy(saddr, &res, &flags);
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;
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);
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;
1108 if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
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 *
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);
1126 rth->rt_spec_dst= spec_dst;
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;
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)) &&
1136 struct net_device *odev = rth->u.dst.dev;
1138 dev->accept_fastpath &&
1139 @@ -1595,6 +1614,8 @@ out: return err;
1141 if (skb->protocol != htons(ETH_P_IP))
1147 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
1148 @@ -1627,6 +1648,7 @@ local_input:
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);
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:
1168 -int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
1169 - u8 tos, struct net_device *dev)
1171 +ip_route_input_cached(struct sk_buff *skb, u32 daddr, u32 saddr,
1172 + u8 tos, struct net_device *dev, u32 lsrc)
1174 struct rtable * rth;
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);
1188 - return ip_route_input_slow(skb, daddr, saddr, tos, dev);
1189 + return ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
1192 +int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
1193 + u8 tos, struct net_device *dev)
1195 + return ip_route_input_cached(skb, daddr, saddr, tos, dev, 0);
1198 +int ip_route_input_lookup(struct sk_buff *skb, u32 daddr, u32 saddr,
1199 + u8 tos, struct net_device *dev, u32 lsrc)
1201 + return ip_route_input_cached(skb, daddr, saddr, tos, dev, lsrc);
1205 * Major route resolver routine.
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;
1215 @@ -1880,6 +1918,7 @@ int ip_route_output_slow(struct rtable *
1216 dev_out = &loopback_dev;
1218 key.oif = loopback_dev.ifindex;
1220 res.type = RTN_LOCAL;
1221 flags |= RTCF_LOCAL;
1223 @@ -1887,7 +1926,7 @@ int ip_route_output_slow(struct rtable *
1225 if (fib_lookup(&key, &res)) {
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.
1232 @@ -1930,6 +1969,7 @@ int ip_route_output_slow(struct rtable *
1233 dev_out = &loopback_dev;
1235 key.oif = dev_out->ifindex;
1238 fib_info_put(res.fi);
1240 @@ -1937,13 +1977,12 @@ int ip_route_output_slow(struct rtable *
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);
1252 - if (!res.prefixlen && res.type == RTN_UNICAST && !key.oif)
1253 - fib_select_default(&key, &res);
1256 key.src = FIB_RES_PREFSRC(res);
1257 @@ -2001,7 +2040,9 @@ make_route:
1259 rth->key.src = oldkey->src;
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;
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 &&
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);