1 diff -ur v2.4.29/linux/include/linux/netfilter_ipv4/ip_nat.h linux/include/linux/netfilter_ipv4/ip_nat.h
2 --- v2.4.29/linux/include/linux/netfilter_ipv4/ip_nat.h 2005-01-20 09:25:34.000000000 +0200
3 +++ linux/include/linux/netfilter_ipv4/ip_nat.h 2005-01-20 09:55:46.998651976 +0200
5 extern u_int16_t ip_nat_cheat_check(u_int32_t oldvalinv,
9 +/* Call input routing for SNAT-ed traffic */
10 +extern unsigned int ip_nat_route_input(unsigned int hooknum,
11 + struct sk_buff **pskb,
12 + const struct net_device *in,
13 + const struct net_device *out,
14 + int (*okfn)(struct sk_buff *));
18 diff -ur v2.4.29/linux/include/linux/rtnetlink.h linux/include/linux/rtnetlink.h
19 --- v2.4.29/linux/include/linux/rtnetlink.h 2004-08-08 10:56:48.000000000 +0300
20 +++ linux/include/linux/rtnetlink.h 2005-01-20 09:55:33.431714464 +0200
22 #define RTNH_F_DEAD 1 /* Nexthop is dead (used by multipath) */
23 #define RTNH_F_PERVASIVE 2 /* Do recursive gateway lookup */
24 #define RTNH_F_ONLINK 4 /* Gateway is forced on link */
25 +#define RTNH_F_SUSPECT 8 /* We don't know the real state */
26 +#define RTNH_F_BADSTATE (RTNH_F_DEAD | RTNH_F_SUSPECT)
28 /* Macros to handle hexthops */
30 diff -ur v2.4.29/linux/include/net/ip_fib.h linux/include/net/ip_fib.h
31 --- v2.4.29/linux/include/net/ip_fib.h 2001-11-13 03:24:05.000000000 +0200
32 +++ linux/include/net/ip_fib.h 2005-01-20 09:55:33.432714312 +0200
35 static inline void fib_select_default(const struct rt_key *key, struct fib_result *res)
37 - if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
38 + if ((FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) ||
39 + FIB_RES_NH(*res).nh_scope == RT_SCOPE_HOST)
40 main_table->tb_select_default(main_table, key, res);
44 extern int fib_lookup(const struct rt_key *key, struct fib_result *res);
45 extern struct fib_table *__fib_new_table(int id);
46 extern void fib_rule_put(struct fib_rule *r);
47 +extern int fib_result_table(struct fib_result *res);
49 static inline struct fib_table *fib_get_table(int id)
55 +extern rwlock_t fib_nhflags_lock;
57 #endif /* _NET_FIB_H */
58 diff -ur v2.4.29/linux/include/net/route.h linux/include/net/route.h
59 --- v2.4.29/linux/include/net/route.h 2003-08-25 22:06:13.000000000 +0300
60 +++ linux/include/net/route.h 2005-01-20 09:55:46.999651824 +0200
69 #ifdef CONFIG_IP_ROUTE_FWMARK
71 extern void rt_cache_flush(int how);
72 extern int ip_route_output_key(struct rtable **, const struct rt_key *key);
73 extern int ip_route_input(struct sk_buff*, u32 dst, u32 src, u8 tos, struct net_device *devin);
74 +extern int ip_route_input_lookup(struct sk_buff*, u32 dst, u32 src, u8 tos, struct net_device *devin, u32 lsrc);
75 extern unsigned short ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu);
76 extern void ip_rt_update_pmtu(struct dst_entry *dst, unsigned mtu);
77 extern void ip_rt_send_redirect(struct sk_buff *skb);
83 +ip_route_output_lookup(struct rtable **rp,
84 + u32 daddr, u32 saddr, u32 tos, int oif, u32 gw)
86 + struct rt_key key = { dst:daddr, src:saddr, gw:gw, oif:oif, tos:tos };
88 + return ip_route_output_key(rp, &key);
91 static inline void ip_rt_put(struct rtable * rt)
94 diff -ur v2.4.29/linux/net/ipv4/fib_frontend.c linux/net/ipv4/fib_frontend.c
95 --- v2.4.29/linux/net/ipv4/fib_frontend.c 2003-08-25 22:06:13.000000000 +0300
96 +++ linux/net/ipv4/fib_frontend.c 2005-01-20 09:55:46.999651824 +0200
98 struct fib_table *local_table;
99 struct fib_table *main_table;
101 +#define FIB_RES_TABLE(r) (RT_TABLE_MAIN)
105 #define RT_TABLE_MIN 1
110 +#define FIB_RES_TABLE(r) (fib_result_table(r))
112 #endif /* CONFIG_IP_MULTIPLE_TABLES */
115 struct in_device *in_dev;
117 struct fib_result res;
119 + unsigned char prefixlen;
120 + unsigned char scope;
130 key.scope = RT_SCOPE_UNIVERSE;
132 @@ -237,31 +244,35 @@
134 *spec_dst = FIB_RES_PREFSRC(res);
135 fib_combine_itag(itag, &res);
136 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
137 - if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1)
139 if (FIB_RES_DEV(res) == dev)
142 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
146 + table = FIB_RES_TABLE(&res);
147 + prefixlen = res.prefixlen;
154 key.oif = dev->ifindex;
157 if (fib_lookup(&key, &res) == 0) {
158 - if (res.type == RTN_UNICAST) {
159 + if (res.type == RTN_UNICAST &&
160 + ((table == FIB_RES_TABLE(&res) &&
161 + res.prefixlen >= prefixlen && res.scope >= scope) ||
163 *spec_dst = FIB_RES_PREFSRC(res);
164 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
179 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
180 fib_sync_up(ifa->ifa_dev->dev);
188 } endfor_ifa(in_dev);
189 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
195 diff -ur v2.4.29/linux/net/ipv4/fib_hash.c linux/net/ipv4/fib_hash.c
196 --- v2.4.29/linux/net/ipv4/fib_hash.c 2003-08-25 22:06:13.000000000 +0300
197 +++ linux/net/ipv4/fib_hash.c 2005-01-20 09:55:47.000651672 +0200
199 struct fib_info *fn_info;
200 #define FIB_INFO(f) ((f)->fn_info)
206 @@ -336,72 +337,123 @@
210 -static int fn_hash_last_dflt=-1;
212 -static int fib_detect_death(struct fib_info *fi, int order,
213 - struct fib_info **last_resort, int *last_idx)
214 +static int fib_detect_death(struct fib_info *fi, int order, int last_dflt,
215 + struct fib_info **last_resort, int *last_idx,
216 + int *last_nhsel, const struct rt_key *key)
219 - int state = NUD_NONE;
222 + struct fib_nh * nh;
224 + int flag, dead = 1;
226 + /* change_nexthops(fi) { */
227 + for (nhsel = 0, nh = fi->fib_nh; nhsel < fi->fib_nhs; nh++, nhsel++) {
228 + if (key->oif && key->oif != nh->nh_oif)
230 + if (key->gw && key->gw != nh->nh_gw && nh->nh_gw &&
231 + nh->nh_scope == RT_SCOPE_LINK)
233 + if (nh->nh_flags & RTNH_F_DEAD)
236 - n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
238 - state = n->nud_state;
241 + if (nh->nh_dev->flags & IFF_NOARP) {
247 + if (!nh->nh_gw || nh->nh_scope != RT_SCOPE_LINK)
251 + n = neigh_lookup(&arp_tbl, &dst, nh->nh_dev);
253 + state = n->nud_state;
256 + if (state==NUD_REACHABLE ||
257 + ((state&NUD_VALID) && order != last_dflt)) {
261 + if (!(state&NUD_VALID))
265 + if ((state&NUD_VALID) ||
266 + (*last_idx<0 && order >= last_dflt)) {
269 + *last_nhsel = nhsel;
274 + read_lock_bh(&fib_nhflags_lock);
276 + nh->nh_flags |= RTNH_F_SUSPECT;
278 + nh->nh_flags &= ~RTNH_F_SUSPECT;
279 + read_unlock_bh(&fib_nhflags_lock);
281 - if (state==NUD_REACHABLE)
283 - if ((state&NUD_VALID) && order != fn_hash_last_dflt)
285 - if ((state&NUD_VALID) ||
286 - (*last_idx<0 && order > fn_hash_last_dflt)) {
291 + /* } endfor_nexthops(fi) */
297 fn_hash_select_default(struct fib_table *tb, const struct rt_key *key, struct fib_result *res)
299 - int order, last_idx;
300 - struct fib_node *f;
301 + int order, last_idx, last_dflt, last_nhsel;
302 + struct fib_node *f, *first_node;
303 struct fib_info *fi = NULL;
304 struct fib_info *last_resort;
305 struct fn_hash *t = (struct fn_hash*)tb->tb_data;
306 - struct fn_zone *fz = t->fn_zones[0];
307 + struct fn_zone *fz = t->fn_zones[res->prefixlen];
313 + k = fz_key(key->dst, fz);
321 read_lock(&fib_hash_lock);
322 - for (f = fz->fz_hash[0]; f; f = f->fn_next) {
323 + for (f = fz_chain(k, fz); f; f = f->fn_next) {
324 struct fib_info *next_fi = FIB_INFO(f);
326 - if ((f->fn_state&FN_S_ZOMBIE) ||
327 + if (!fn_key_eq(k, f->fn_key) ||
328 + (f->fn_state&FN_S_ZOMBIE) ||
329 f->fn_scope != res->scope ||
330 +#ifdef CONFIG_IP_ROUTE_TOS
331 + (f->fn_tos && f->fn_tos != key->tos) ||
333 f->fn_type != RTN_UNICAST)
336 if (next_fi->fib_priority > res->fi->fib_priority)
338 - if (!next_fi->fib_nh[0].nh_gw || next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
340 f->fn_state |= FN_S_ACCESSED;
343 - if (next_fi != res->fi)
345 - } else if (!fib_detect_death(fi, order, &last_resort, &last_idx)) {
347 + last_dflt = f->fn_last_dflt;
350 + if (fi && !fib_detect_death(fi, order, last_dflt,
351 + &last_resort, &last_idx, &last_nhsel, key)) {
353 fib_info_put(res->fi);
355 atomic_inc(&fi->fib_clntref);
356 - fn_hash_last_dflt = order;
357 + first_node->fn_last_dflt = order;
361 @@ -409,16 +461,25 @@
364 if (order<=0 || fi==NULL) {
365 - fn_hash_last_dflt = -1;
366 + if (fi && fi->fib_nhs > 1 &&
367 + fib_detect_death(fi, order, last_dflt,
368 + &last_resort, &last_idx, &last_nhsel, key) &&
369 + last_resort == fi) {
370 + read_lock_bh(&fib_nhflags_lock);
371 + fi->fib_nh[last_nhsel].nh_flags &= ~RTNH_F_SUSPECT;
372 + read_unlock_bh(&fib_nhflags_lock);
374 + if (first_node) first_node->fn_last_dflt = -1;
378 - if (!fib_detect_death(fi, order, &last_resort, &last_idx)) {
379 + if (!fib_detect_death(fi, order, last_dflt, &last_resort, &last_idx,
380 + &last_nhsel, key)) {
382 fib_info_put(res->fi);
384 atomic_inc(&fi->fib_clntref);
385 - fn_hash_last_dflt = order;
386 + first_node->fn_last_dflt = order;
391 res->fi = last_resort;
393 atomic_inc(&last_resort->fib_clntref);
394 + read_lock_bh(&fib_nhflags_lock);
395 + last_resort->fib_nh[last_nhsel].nh_flags &= ~RTNH_F_SUSPECT;
396 + read_unlock_bh(&fib_nhflags_lock);
397 + first_node->fn_last_dflt = last_idx;
399 - fn_hash_last_dflt = last_idx;
401 read_unlock(&fib_hash_lock);
405 memset(new_f, 0, sizeof(struct fib_node));
407 + new_f->fn_last_dflt = -1;
409 #ifdef CONFIG_IP_ROUTE_TOS
411 diff -ur v2.4.29/linux/net/ipv4/fib_rules.c linux/net/ipv4/fib_rules.c
412 --- v2.4.29/linux/net/ipv4/fib_rules.c 2004-02-19 00:23:39.000000000 +0200
413 +++ linux/net/ipv4/fib_rules.c 2005-01-20 09:55:33.433714160 +0200
418 +int fib_result_table(struct fib_result *res)
420 + return res->r->r_table;
423 int fib_lookup(const struct rt_key *key, struct fib_result *res)
428 void fib_select_default(const struct rt_key *key, struct fib_result *res)
430 - if (res->r && res->r->r_action == RTN_UNICAST &&
431 - FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
433 + (res->r->r_action == RTN_UNICAST || res->r->r_action == RTN_NAT) &&
434 + ((FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) ||
435 + FIB_RES_NH(*res).nh_scope == RT_SCOPE_HOST)) {
436 struct fib_table *tb;
437 if ((tb = fib_get_table(res->r->r_table)) != NULL)
438 tb->tb_select_default(tb, key, res);
439 diff -ur v2.4.29/linux/net/ipv4/fib_semantics.c linux/net/ipv4/fib_semantics.c
440 --- v2.4.29/linux/net/ipv4/fib_semantics.c 2003-08-25 22:06:13.000000000 +0300
441 +++ linux/net/ipv4/fib_semantics.c 2005-01-20 09:55:47.000651672 +0200
443 static struct fib_info *fib_info_list;
444 static rwlock_t fib_info_lock = RW_LOCK_UNLOCKED;
446 +rwlock_t fib_nhflags_lock = RW_LOCK_UNLOCKED;
448 #define for_fib_info() { struct fib_info *fi; \
449 for (fi = fib_info_list; fi; fi = fi->fib_next)
451 #ifdef CONFIG_NET_CLS_ROUTE
452 nh->nh_tclassid != onh->nh_tclassid ||
454 - ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
455 + ((nh->nh_flags^onh->nh_flags)&~RTNH_F_BADSTATE))
458 } endfor_nexthops(fi);
460 nfi->fib_prefsrc == fi->fib_prefsrc &&
461 nfi->fib_priority == fi->fib_priority &&
462 memcmp(nfi->fib_metrics, fi->fib_metrics, sizeof(fi->fib_metrics)) == 0 &&
463 - ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
464 + ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_BADSTATE) == 0 &&
465 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
470 if ((dev = __dev_get_by_index(nh->nh_oif)) == NULL)
472 - if (!(dev->flags&IFF_UP))
474 + if (!(dev->flags&IFF_UP)) {
475 + if (fi->fib_protocol != RTPROT_STATIC)
477 + nh->nh_flags |= RTNH_F_DEAD;
481 nh->nh_scope = RT_SCOPE_LINK;
482 @@ -380,23 +384,48 @@
483 /* It is not necessary, but requires a bit of thinking */
484 if (key.scope < RT_SCOPE_LINK)
485 key.scope = RT_SCOPE_LINK;
486 - if ((err = fib_lookup(&key, &res)) != 0)
489 - if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
491 - nh->nh_scope = res.scope;
492 - nh->nh_oif = FIB_RES_OIF(res);
493 - if ((nh->nh_dev = FIB_RES_DEV(res)) == NULL)
495 - dev_hold(nh->nh_dev);
497 - if (!(nh->nh_dev->flags & IFF_UP))
501 + err = fib_lookup(&key, &res);
503 + struct in_device *in_dev;
505 + if (err != -ENETUNREACH ||
506 + fi->fib_protocol != RTPROT_STATIC)
509 + in_dev = inetdev_by_index(nh->nh_oif);
510 + if (in_dev == NULL ||
511 + in_dev->dev->flags & IFF_UP) {
513 + in_dev_put(in_dev);
516 + nh->nh_flags |= RTNH_F_DEAD;
517 + nh->nh_scope = RT_SCOPE_LINK;
518 + nh->nh_dev = in_dev->dev;
519 + dev_hold(nh->nh_dev);
520 + in_dev_put(in_dev);
523 + if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
525 + nh->nh_scope = res.scope;
526 + nh->nh_oif = FIB_RES_OIF(res);
527 + if ((nh->nh_dev = FIB_RES_DEV(res)) == NULL)
529 + dev_hold(nh->nh_dev);
530 + if (!(nh->nh_dev->flags & IFF_UP)) {
531 + if (fi->fib_protocol != RTPROT_STATIC) {
535 + nh->nh_flags |= RTNH_F_DEAD;
545 struct in_device *in_dev;
550 if (!(in_dev->dev->flags&IFF_UP)) {
551 - in_dev_put(in_dev);
553 + if (fi->fib_protocol != RTPROT_STATIC) {
554 + in_dev_put(in_dev);
557 + nh->nh_flags |= RTNH_F_DEAD;
559 nh->nh_dev = in_dev->dev;
560 dev_hold(nh->nh_dev);
563 if (nh->nh_flags&RTNH_F_DEAD)
565 - if (!key->oif || key->oif == nh->nh_oif)
567 + if (key->oif && key->oif != nh->nh_oif)
569 + if (key->gw && key->gw != nh->nh_gw &&
570 + nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
574 #ifdef CONFIG_IP_ROUTE_MULTIPATH
575 if (nhsel < fi->fib_nhs) {
576 @@ -870,22 +906,35 @@
577 if (local && fi->fib_prefsrc == local) {
578 fi->fib_flags |= RTNH_F_DEAD;
580 - } else if (dev && fi->fib_nhs) {
581 + } else if (fi->fib_nhs) {
584 change_nexthops(fi) {
585 - if (nh->nh_flags&RTNH_F_DEAD)
587 - else if (nh->nh_dev == dev &&
588 - nh->nh_scope != scope) {
589 - nh->nh_flags |= RTNH_F_DEAD;
590 + if (nh->nh_flags&RTNH_F_DEAD) {
591 + if (fi->fib_protocol!=RTPROT_STATIC ||
592 + nh->nh_dev == NULL ||
593 + !__in_dev_get(nh->nh_dev) ||
594 + nh->nh_dev->flags&IFF_UP)
596 + } else if ((nh->nh_dev == dev && dev &&
597 + nh->nh_scope != scope) ||
598 + (local == nh->nh_gw && local &&
600 + write_lock_bh(&fib_nhflags_lock);
601 #ifdef CONFIG_IP_ROUTE_MULTIPATH
602 - spin_lock_bh(&fib_multipath_lock);
603 + spin_lock(&fib_multipath_lock);
604 + nh->nh_flags |= RTNH_F_DEAD;
605 fi->fib_power -= nh->nh_power;
607 - spin_unlock_bh(&fib_multipath_lock);
608 + spin_unlock(&fib_multipath_lock);
610 + nh->nh_flags |= RTNH_F_DEAD;
613 + write_unlock_bh(&fib_nhflags_lock);
614 + if (fi->fib_protocol!=RTPROT_STATIC ||
616 + (dev && __in_dev_get(dev) == NULL))
619 #ifdef CONFIG_IP_ROUTE_MULTIPATH
620 if (force > 1 && nh->nh_dev == dev) {
621 @@ -903,37 +952,55 @@
625 -#ifdef CONFIG_IP_ROUTE_MULTIPATH
628 - Dead device goes up. We wake up dead nexthops.
629 - It takes sense only on multipath routes.
630 + Dead device goes up or new address is added. We wake up dead nexthops.
633 int fib_sync_up(struct net_device *dev)
637 + struct fib_result res;
641 if (!(dev->flags&IFF_UP))
649 change_nexthops(fi) {
650 - if (!(nh->nh_flags&RTNH_F_DEAD)) {
652 + if (!(nh->nh_flags&RTNH_F_DEAD))
655 if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
657 if (nh->nh_dev != dev || __in_dev_get(dev) == NULL)
659 + if (nh->nh_gw && fi->fib_protocol == RTPROT_STATIC) {
660 + memset(&key, 0, sizeof(key));
661 + key.dst = nh->nh_gw;
662 + key.oif = nh->nh_oif;
663 + key.scope = nh->nh_scope;
664 + if (fib_lookup(&key, &res) != 0)
666 + if (res.type != RTN_UNICAST &&
667 + res.type != RTN_LOCAL) {
671 + nh->nh_scope = res.scope;
676 +#ifdef CONFIG_IP_ROUTE_MULTIPATH
677 spin_lock_bh(&fib_multipath_lock);
679 nh->nh_flags &= ~RTNH_F_DEAD;
680 spin_unlock_bh(&fib_multipath_lock);
682 } endfor_nexthops(fi)
685 @@ -941,9 +1008,13 @@
694 +#ifdef CONFIG_IP_ROUTE_MULTIPATH
697 The algorithm is suboptimal, but it provides really
698 fair weighted route distribution.
699 @@ -952,24 +1023,45 @@
700 void fib_select_multipath(const struct rt_key *key, struct fib_result *res)
702 struct fib_info *fi = res->fi;
706 spin_lock_bh(&fib_multipath_lock);
710 + change_nexthops(fi) {
711 + if (key->oif != nh->nh_oif)
713 + if (key->gw && key->gw != nh->nh_gw &&
714 + nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
716 + if (!(nh->nh_flags&RTNH_F_BADSTATE)) {
717 + if (nh->nh_power > w) {
722 + } endfor_nexthops(fi);
724 + spin_unlock_bh(&fib_multipath_lock);
732 if (fi->fib_power <= 0) {
734 change_nexthops(fi) {
735 - if (!(nh->nh_flags&RTNH_F_DEAD)) {
736 + if (!(nh->nh_flags&RTNH_F_BADSTATE)) {
737 power += nh->nh_weight;
738 nh->nh_power = nh->nh_weight;
740 } endfor_nexthops(fi);
741 fi->fib_power = power;
743 - spin_unlock_bh(&fib_multipath_lock);
744 - /* Race condition: route has just become dead. */
753 @@ -979,20 +1071,40 @@
755 w = jiffies % fi->fib_power;
758 change_nexthops(fi) {
759 - if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
760 + if (!(nh->nh_flags&RTNH_F_BADSTATE) && nh->nh_power) {
761 if ((w -= nh->nh_power) <= 0) {
764 - res->nh_sel = nhsel;
765 spin_unlock_bh(&fib_multipath_lock);
766 + res->nh_sel = nhsel;
771 + } endfor_nexthops(fi);
780 + if (!(nh->nh_flags&RTNH_F_DEAD)) {
781 + if (key->oif && key->oif != nh->nh_oif)
783 + if (key->gw && key->gw != nh->nh_gw &&
784 + nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
786 + spin_unlock_bh(&fib_multipath_lock);
787 + res->nh_sel = nhsel;
790 } endfor_nexthops(fi);
792 /* Race condition: route has just become dead. */
794 spin_unlock_bh(&fib_multipath_lock);
797 diff -ur v2.4.29/linux/net/ipv4/ip_nat_dumb.c linux/net/ipv4/ip_nat_dumb.c
798 --- v2.4.29/linux/net/ipv4/ip_nat_dumb.c 2001-11-13 03:25:26.000000000 +0200
799 +++ linux/net/ipv4/ip_nat_dumb.c 2005-01-20 09:55:47.001651520 +0200
801 key.dst = ciph->saddr;
802 key.iif = skb->dev->ifindex;
805 #ifdef CONFIG_IP_ROUTE_TOS
806 key.tos = RT_TOS(ciph->tos);
808 diff -ur v2.4.29/linux/net/ipv4/netfilter/ip_fw_compat_masq.c linux/net/ipv4/netfilter/ip_fw_compat_masq.c
809 --- v2.4.29/linux/net/ipv4/netfilter/ip_fw_compat_masq.c 2005-01-20 09:25:34.000000000 +0200
810 +++ linux/net/ipv4/netfilter/ip_fw_compat_masq.c 2005-01-20 09:55:47.001651520 +0200
812 enum ip_conntrack_info ctinfo;
813 struct ip_conntrack *ct;
815 + struct rtable *rt, *skb_rt;
816 + struct net_device *skb_dev;
820 /* Sorry, only ICMP, TCP and UDP. */
821 if (iph->protocol != IPPROTO_ICMP
825 info = &ct->nat.info;
826 + iph = (*pskb)->nh.iph;
827 + saddr = iph->saddr;
830 WRITE_LOCK(&ip_nat_lock);
831 /* Setup the masquerade, if not already */
832 if (!info->initialized) {
835 struct ip_nat_multi_range range;
837 + skb_rt = (struct rtable *) (*pskb)->dst;
838 + skb_dev = skb_rt->u.dst.dev;
839 /* Pass 0 instead of saddr, since it's going to be changed
841 - if (ip_route_output(&rt, iph->daddr, 0, 0, 0) != 0) {
842 + if (ip_route_output_lookup(&rt, iph->daddr, 0, RT_TOS(iph->tos),
843 + skb_dev? skb_dev->ifindex : 0,
844 + skb_dev? skb_rt->rt_gateway : 0) != 0) {
845 + WRITE_UNLOCK(&ip_nat_lock);
846 DEBUGP("ipnat_rule_masquerade: Can't reroute.\n");
849 - newsrc = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
850 - RT_SCOPE_UNIVERSE);
851 + newsrc = rt->rt_src;
853 range = ((struct ip_nat_multi_range)
856 WRITE_UNLOCK(&ip_nat_lock);
861 DEBUGP("Masquerading already done on this conn.\n");
862 WRITE_UNLOCK(&ip_nat_lock);
864 - return do_bindings(ct, ctinfo, info, NF_IP_POST_ROUTING, pskb);
865 + ret = do_bindings(ct, ctinfo, info, NF_IP_POST_ROUTING, pskb);
866 + if (ret != NF_ACCEPT || saddr == (*pskb)->nh.iph->saddr || new)
869 + iph = (*pskb)->nh.iph;
870 + if (ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), 0) != 0)
873 + skb_rt = (struct rtable *) (*pskb)->dst;
874 + skb_dev = skb_rt->u.dst.dev;
875 + if (skb_dev != rt->u.dst.dev || rt->rt_gateway != skb_rt->rt_gateway) {
876 + if (skb_dev != rt->u.dst.dev) {
877 + /* TODO: check the new mtu and reply FRAG_NEEDED */
879 + dst_release((*pskb)->dst);
880 + (*pskb)->dst = &rt->u.dst;
888 diff -ur v2.4.29/linux/net/ipv4/netfilter/ip_nat_core.c linux/net/ipv4/netfilter/ip_nat_core.c
889 --- v2.4.29/linux/net/ipv4/netfilter/ip_nat_core.c 2005-01-20 09:25:34.000000000 +0200
890 +++ linux/net/ipv4/netfilter/ip_nat_core.c 2005-01-20 09:55:47.002651368 +0200
896 +ip_nat_route_input(unsigned int hooknum,
897 + struct sk_buff **pskb,
898 + const struct net_device *in,
899 + const struct net_device *out,
900 + int (*okfn)(struct sk_buff *))
902 + struct sk_buff *skb = *pskb;
904 + struct ip_conntrack *ct;
905 + enum ip_conntrack_info ctinfo;
906 + struct ip_nat_info *info;
907 + enum ip_conntrack_dir dir;
911 + if (!(ct = ip_conntrack_get(skb, &ctinfo)))
914 + info = &ct->nat.info;
915 + if (!info->initialized)
921 + if (skb->len < sizeof(struct iphdr))
925 + saddr = iph->saddr;
926 + hooknum = NF_IP_POST_ROUTING;
927 + dir = CTINFO2DIR(ctinfo);
929 + READ_LOCK(&ip_nat_lock);
930 + for (i = 0; i < info->num_manips; i++) {
931 + if (info->manips[i].direction == dir
932 + && info->manips[i].hooknum == hooknum
933 + && info->manips[i].maniptype == IP_NAT_MANIP_SRC) {
934 + saddr = info->manips[i].manip.ip;
937 + READ_UNLOCK(&ip_nat_lock);
939 + if (saddr == iph->saddr)
942 + if (ip_route_input_lookup(skb, iph->daddr, iph->saddr, iph->tos,
949 int __init ip_nat_init(void)
952 diff -ur v2.4.29/linux/net/ipv4/netfilter/ip_nat_standalone.c linux/net/ipv4/netfilter/ip_nat_standalone.c
953 --- v2.4.29/linux/net/ipv4/netfilter/ip_nat_standalone.c 2005-01-20 09:25:34.000000000 +0200
954 +++ linux/net/ipv4/netfilter/ip_nat_standalone.c 2005-01-20 09:55:47.002651368 +0200
956 /* Before packet filtering, change destination */
957 static struct nf_hook_ops ip_nat_in_ops
958 = { { NULL, NULL }, ip_nat_in, PF_INET, NF_IP_PRE_ROUTING, NF_IP_PRI_NAT_DST };
959 +/* Before routing, route before mangling */
960 +static struct nf_hook_ops ip_nat_inr_ops
961 += { { NULL, NULL }, ip_nat_route_input, PF_INET, NF_IP_PRE_ROUTING, NF_IP_PRI_LAST-1 };
962 /* After packet filtering, change source */
963 static struct nf_hook_ops ip_nat_out_ops
964 = { { NULL, NULL }, ip_nat_out, PF_INET, NF_IP_POST_ROUTING, NF_IP_PRI_NAT_SRC};
965 @@ -309,10 +312,15 @@
966 printk("ip_nat_init: can't register in hook.\n");
969 + ret = nf_register_hook(&ip_nat_inr_ops);
971 + printk("ip_nat_init: can't register inr hook.\n");
972 + goto cleanup_inops;
974 ret = nf_register_hook(&ip_nat_out_ops);
976 printk("ip_nat_init: can't register out hook.\n");
977 - goto cleanup_inops;
978 + goto cleanup_inrops;
980 ret = nf_register_hook(&ip_nat_local_out_ops);
983 nf_unregister_hook(&ip_nat_local_out_ops);
985 nf_unregister_hook(&ip_nat_out_ops);
987 + nf_unregister_hook(&ip_nat_inr_ops);
989 nf_unregister_hook(&ip_nat_in_ops);
991 diff -ur v2.4.29/linux/net/ipv4/netfilter/ipt_MASQUERADE.c linux/net/ipv4/netfilter/ipt_MASQUERADE.c
992 --- v2.4.29/linux/net/ipv4/netfilter/ipt_MASQUERADE.c 2005-01-20 09:25:34.000000000 +0200
993 +++ linux/net/ipv4/netfilter/ipt_MASQUERADE.c 2005-01-20 09:55:47.003651216 +0200
995 key.dst = (*pskb)->nh.iph->daddr;
996 key.src = 0; /* Unknown: that's what we're trying to establish */
997 key.tos = RT_TOS((*pskb)->nh.iph->tos)|RTO_CONN;
999 + key.oif = out->ifindex;
1000 + key.gw = ((struct rtable *) (*pskb)->dst)->rt_gateway;
1001 #ifdef CONFIG_IP_ROUTE_FWMARK
1002 key.fwmark = (*pskb)->nfmark;
1005 " No route: Rusty's brain broke!\n");
1008 - if (rt->u.dst.dev != out) {
1009 - if (net_ratelimit())
1010 - printk("MASQUERADE:"
1011 - " Route sent us somewhere else.\n");
1016 newsrc = rt->rt_src;
1017 DEBUGP("newsrc = %u.%u.%u.%u\n", NIPQUAD(newsrc));
1018 diff -ur v2.4.29/linux/net/ipv4/route.c linux/net/ipv4/route.c
1019 --- v2.4.29/linux/net/ipv4/route.c 2004-11-18 08:30:33.000000000 +0200
1020 +++ linux/net/ipv4/route.c 2005-01-20 09:55:47.004651064 +0200
1023 /* Gateway is different ... */
1024 rt->rt_gateway = new_gw;
1025 + if (rt->key.gw) rt->key.gw = new_gw;
1027 /* Redirect received -> path was valid */
1028 dst_confirm(&rth->u.dst);
1029 @@ -1343,6 +1344,7 @@
1030 rth->key.fwmark = skb->nfmark;
1032 rth->key.src = saddr;
1033 + rth->key.lsrc = 0;
1034 rth->rt_src = saddr;
1035 #ifdef CONFIG_IP_ROUTE_NAT
1036 rth->rt_dst_map = daddr;
1037 @@ -1356,6 +1358,7 @@
1038 rth->u.dst.dev = &loopback_dev;
1039 dev_hold(rth->u.dst.dev);
1042 rth->rt_gateway = daddr;
1043 rth->rt_spec_dst= spec_dst;
1044 rth->rt_type = RTN_MULTICAST;
1045 @@ -1395,7 +1398,7 @@
1048 int ip_route_input_slow(struct sk_buff *skb, u32 daddr, u32 saddr,
1049 - u8 tos, struct net_device *dev)
1050 + u8 tos, struct net_device *dev, u32 lsrc)
1053 struct fib_result res;
1054 @@ -1415,16 +1418,17 @@
1059 + key.src = lsrc? : saddr;
1061 #ifdef CONFIG_IP_ROUTE_FWMARK
1062 key.fwmark = skb->nfmark;
1064 - key.iif = dev->ifindex;
1065 + key.iif = lsrc? loopback_dev.ifindex : dev->ifindex;
1068 key.scope = RT_SCOPE_UNIVERSE;
1070 - hash = rt_hash_code(daddr, saddr ^ (key.iif << 5), tos);
1071 + hash = rt_hash_code(daddr, saddr ^ (dev->ifindex << 5), tos);
1073 /* Check for the most weird martians, which can be not detected
1075 @@ -1445,6 +1449,12 @@
1076 if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
1077 goto martian_destination;
1080 + if (MULTICAST(lsrc) || BADCLASS(lsrc) ||
1081 + ZERONET(lsrc) || LOOPBACK(lsrc))
1086 * Now we are ready to route packet.
1088 @@ -1454,6 +1464,10 @@
1092 + if (lsrc && res.type != RTN_UNICAST && res.type != RTN_NAT)
1094 + key.iif = dev->ifindex;
1097 rt_cache_stat[smp_processor_id()].in_slow_tot++;
1099 @@ -1464,7 +1478,7 @@
1102 u32 src_map = saddr;
1104 + if (res.r && !lsrc)
1105 src_map = fib_rules_policy(saddr, &res, &flags);
1107 if (res.type == RTN_NAT) {
1108 @@ -1503,8 +1517,9 @@
1109 if (res.type != RTN_UNICAST)
1110 goto martian_destination;
1112 + fib_select_default(&key, &res);
1113 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1114 - if (res.fi->fib_nhs > 1 && key.oif == 0)
1115 + if (res.fi->fib_nhs > 1)
1116 fib_select_multipath(&key, &res);
1118 out_dev = in_dev_get(FIB_RES_DEV(res));
1119 @@ -1524,6 +1539,7 @@
1120 flags |= RTCF_DIRECTSRC;
1122 if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
1124 (IN_DEV_SHARED_MEDIA(out_dev) ||
1125 inet_addr_onlink(out_dev, saddr, FIB_RES_GW(res))))
1126 flags |= RTCF_DOREDIRECT;
1127 @@ -1550,6 +1566,7 @@
1129 rth->key.src = saddr;
1130 rth->rt_src = saddr;
1131 + rth->key.lsrc = lsrc;
1132 rth->rt_gateway = daddr;
1133 #ifdef CONFIG_IP_ROUTE_NAT
1134 rth->rt_src_map = key.src;
1135 @@ -1562,6 +1579,7 @@
1136 rth->u.dst.dev = out_dev->dev;
1137 dev_hold(rth->u.dst.dev);
1140 rth->rt_spec_dst= spec_dst;
1142 rth->u.dst.input = ip_forward;
1143 @@ -1572,7 +1590,8 @@
1144 rth->rt_flags = flags;
1146 #ifdef CONFIG_NET_FASTROUTE
1147 - if (netdev_fastroute && !(flags&(RTCF_NAT|RTCF_MASQ|RTCF_DOREDIRECT))) {
1148 + if (netdev_fastroute && !(flags&(RTCF_NAT|RTCF_MASQ|RTCF_DOREDIRECT)) &&
1150 struct net_device *odev = rth->u.dst.dev;
1152 dev->accept_fastpath &&
1153 @@ -1595,6 +1614,8 @@
1155 if (skb->protocol != htons(ETH_P_IP))
1161 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
1162 @@ -1627,6 +1648,7 @@
1164 rth->key.src = saddr;
1165 rth->rt_src = saddr;
1166 + rth->key.lsrc = 0;
1167 #ifdef CONFIG_IP_ROUTE_NAT
1168 rth->rt_dst_map = key.dst;
1169 rth->rt_src_map = key.src;
1170 @@ -1639,6 +1661,7 @@
1171 rth->u.dst.dev = &loopback_dev;
1172 dev_hold(rth->u.dst.dev);
1175 rth->rt_gateway = daddr;
1176 rth->rt_spec_dst= spec_dst;
1177 rth->u.dst.input= ip_local_deliver;
1178 @@ -1704,8 +1727,9 @@
1182 -int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
1183 - u8 tos, struct net_device *dev)
1185 +ip_route_input_cached(struct sk_buff *skb, u32 daddr, u32 saddr,
1186 + u8 tos, struct net_device *dev, u32 lsrc)
1188 struct rtable * rth;
1190 @@ -1719,6 +1743,7 @@
1191 if (rth->key.dst == daddr &&
1192 rth->key.src == saddr &&
1193 rth->key.iif == iif &&
1194 + rth->key.lsrc == lsrc &&
1195 rth->key.oif == 0 &&
1196 #ifdef CONFIG_IP_ROUTE_FWMARK
1197 rth->key.fwmark == skb->nfmark &&
1198 @@ -1766,9 +1791,21 @@
1199 read_unlock(&inetdev_lock);
1202 - return ip_route_input_slow(skb, daddr, saddr, tos, dev);
1203 + return ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
1206 +int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
1207 + u8 tos, struct net_device *dev)
1209 + return ip_route_input_cached(skb, daddr, saddr, tos, dev, 0);
1212 +int ip_route_input_lookup(struct sk_buff *skb, u32 daddr, u32 saddr,
1213 + u8 tos, struct net_device *dev, u32 lsrc)
1215 + return ip_route_input_cached(skb, daddr, saddr, tos, dev, lsrc);
1219 * Major route resolver routine.
1221 @@ -1791,6 +1828,7 @@
1222 key.tos = tos & IPTOS_RT_MASK;
1223 key.iif = loopback_dev.ifindex;
1224 key.oif = oldkey->oif;
1225 + key.gw = oldkey->gw;
1226 #ifdef CONFIG_IP_ROUTE_FWMARK
1227 key.fwmark = oldkey->fwmark;
1229 @@ -1880,6 +1918,7 @@
1230 dev_out = &loopback_dev;
1232 key.oif = loopback_dev.ifindex;
1234 res.type = RTN_LOCAL;
1235 flags |= RTCF_LOCAL;
1237 @@ -1887,7 +1926,7 @@
1239 if (fib_lookup(&key, &res)) {
1241 - if (oldkey->oif) {
1242 + if (oldkey->oif && dev_out->flags&IFF_UP) {
1243 /* Apparently, routing tables are wrong. Assume,
1244 that the destination is on link.
1246 @@ -1930,6 +1969,7 @@
1247 dev_out = &loopback_dev;
1249 key.oif = dev_out->ifindex;
1252 fib_info_put(res.fi);
1254 @@ -1937,13 +1977,12 @@
1258 + if (res.type == RTN_UNICAST)
1259 + fib_select_default(&key, &res);
1260 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1261 - if (res.fi->fib_nhs > 1 && key.oif == 0)
1262 + if (res.fi->fib_nhs > 1)
1263 fib_select_multipath(&key, &res);
1266 - if (!res.prefixlen && res.type == RTN_UNICAST && !key.oif)
1267 - fib_select_default(&key, &res);
1270 key.src = FIB_RES_PREFSRC(res);
1271 @@ -2001,7 +2040,9 @@
1273 rth->key.src = oldkey->src;
1275 + rth->key.lsrc = 0;
1276 rth->key.oif = oldkey->oif;
1277 + rth->key.gw = oldkey->gw;
1278 #ifdef CONFIG_IP_ROUTE_FWMARK
1279 rth->key.fwmark = oldkey->fwmark;
1281 @@ -2080,6 +2121,7 @@
1282 rth->key.src == key->src &&
1283 rth->key.iif == 0 &&
1284 rth->key.oif == key->oif &&
1285 + rth->key.gw == key->gw &&
1286 #ifdef CONFIG_IP_ROUTE_FWMARK
1287 rth->key.fwmark == key->fwmark &&
1289 diff -ur v2.4.29/linux/net/netsyms.c linux/net/netsyms.c
1290 --- v2.4.29/linux/net/netsyms.c 2005-01-20 09:25:34.000000000 +0200
1291 +++ linux/net/netsyms.c 2005-01-20 09:55:47.005650912 +0200
1293 EXPORT_SYMBOL(inet_unregister_protosw);
1294 EXPORT_SYMBOL(ip_route_output_key);
1295 EXPORT_SYMBOL(ip_route_input);
1296 +EXPORT_SYMBOL(ip_route_input_lookup);
1297 EXPORT_SYMBOL(icmp_send);
1298 EXPORT_SYMBOL(icmp_statistics);
1299 EXPORT_SYMBOL(icmp_err_convert);