diff -urp v3.13-before-nf_reroute/linux/include/net/flow.h linux/include/net/flow.h
--- v3.13-before-nf_reroute/linux/include/net/flow.h	2014-01-21 11:18:21.000000000 +0200
+++ linux/include/net/flow.h	2014-01-25 10:55:46.123832164 +0200
@@ -72,6 +72,7 @@ struct flowi4 {
 #define fl4_ipsec_spi		uli.spi
 #define fl4_mh_type		uli.mht.type
 #define fl4_gre_key		uli.gre_key
+	__be32			fl4_gw;
 } __attribute__((__aligned__(BITS_PER_LONG/8)));
 
 static inline void flowi4_init_output(struct flowi4 *fl4, int oif,
@@ -92,6 +93,7 @@ static inline void flowi4_init_output(st
 	fl4->saddr = saddr;
 	fl4->fl4_dport = dport;
 	fl4->fl4_sport = sport;
+	fl4->fl4_gw = 0;
 }
 
 /* Reset some input parameters after previous lookup */
diff -urp v3.13-before-nf_reroute/linux/include/net/netfilter/nf_nat.h linux/include/net/netfilter/nf_nat.h
--- v3.13-before-nf_reroute/linux/include/net/netfilter/nf_nat.h	2014-01-21 11:18:21.000000000 +0200
+++ linux/include/net/netfilter/nf_nat.h	2014-01-25 10:59:05.322244268 +0200
@@ -40,6 +40,13 @@ struct nf_conn_nat {
 #endif
 };
 
+/* Call input routing for SNAT-ed traffic */
+unsigned int ip_nat_route_input(const struct nf_hook_ops *ops,
+				struct sk_buff *skb,
+				const struct net_device *in,
+				const struct net_device *out,
+				int (*okfn)(struct sk_buff *));
+
 /* Set up the info structure to map into this range. */
 unsigned int nf_nat_setup_info(struct nf_conn *ct,
 			       const struct nf_nat_range *range,
diff -urp v3.13-before-nf_reroute/linux/include/net/route.h linux/include/net/route.h
--- v3.13-before-nf_reroute/linux/include/net/route.h	2014-01-21 11:18:21.000000000 +0200
+++ linux/include/net/route.h	2014-01-25 10:57:16.214668796 +0200
@@ -189,6 +189,8 @@ void ip_rt_multicast_event(struct in_dev
 int ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg);
 void ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt);
 int ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb);
+int ip_route_input_lookup(struct sk_buff*, __be32 dst, __be32 src, u8 tos,
+			  struct net_device *devin, __be32 lsrc);
 
 struct in_ifaddr;
 void fib_add_ifaddr(struct in_ifaddr *);
diff -urp v3.13-before-nf_reroute/linux/net/bridge/br_netfilter.c linux/net/bridge/br_netfilter.c
--- v3.13-before-nf_reroute/linux/net/bridge/br_netfilter.c	2014-01-21 11:18:21.000000000 +0200
+++ linux/net/bridge/br_netfilter.c	2014-01-25 10:55:46.126832073 +0200
@@ -460,6 +460,9 @@ static int br_nf_pre_routing_finish(stru
 	struct rtable *rt;
 	int err;
 
+	/* Old skb->dst is not expected, it is lost in all cases */
+	skb_dst_drop(skb);
+
 	if (nf_bridge->mask & BRNF_PKT_TYPE) {
 		skb->pkt_type = PACKET_OTHERHOST;
 		nf_bridge->mask ^= BRNF_PKT_TYPE;
diff -urp v3.13-before-nf_reroute/linux/net/ipv4/fib_frontend.c linux/net/ipv4/fib_frontend.c
--- v3.13-before-nf_reroute/linux/net/ipv4/fib_frontend.c	2014-01-25 10:55:22.695135669 +0200
+++ linux/net/ipv4/fib_frontend.c	2014-01-25 10:55:46.126832073 +0200
@@ -226,6 +226,7 @@ __be32 fib_compute_spec_dst(struct sk_bu
 		fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
 		fl4.flowi4_scope = scope;
 		fl4.flowi4_mark = IN_DEV_SRC_VMARK(in_dev) ? skb->mark : 0;
+		fl4.fl4_gw = 0;
 		if (!fib_lookup(net, &fl4, &res))
 			return FIB_RES_PREFSRC(net, res);
 	} else {
@@ -262,6 +263,7 @@ static int __fib_validate_source(struct
 	fl4.saddr = dst;
 	fl4.flowi4_tos = tos;
 	fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
+	fl4.fl4_gw = 0;
 
 	no_addr = idev->ifa_list == NULL;
 
diff -urp v3.13-before-nf_reroute/linux/net/ipv4/fib_semantics.c linux/net/ipv4/fib_semantics.c
--- v3.13-before-nf_reroute/linux/net/ipv4/fib_semantics.c	2014-01-25 10:55:22.696135603 +0200
+++ linux/net/ipv4/fib_semantics.c	2014-01-25 10:55:46.127832038 +0200
@@ -442,6 +442,9 @@ int fib_detect_death(struct fib_info *fi
 	for (nhsel = 0, nh = fi->fib_nh; nhsel < fi->fib_nhs; nh++, nhsel++) {
 		if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
 			continue;
+		if (flp->fl4_gw && flp->fl4_gw != nh->nh_gw && nh->nh_gw &&
+		    nh->nh_scope == RT_SCOPE_LINK)
+			continue;
 		if (nh->nh_flags & RTNH_F_DEAD)
 			continue;
 
@@ -1411,6 +1414,10 @@ void fib_select_multipath(const struct f
 		change_nexthops(fi) {
 			if (flp->flowi4_oif != nexthop_nh->nh_oif)
 				continue;
+			if (flp->fl4_gw && flp->fl4_gw != nexthop_nh->nh_gw &&
+			    nexthop_nh->nh_gw &&
+			    nexthop_nh->nh_scope == RT_SCOPE_LINK)
+				continue;
 			if (!(nexthop_nh->nh_flags & RTNH_F_BADSTATE)) {
 				if (nexthop_nh->nh_power > w) {
 					w = nexthop_nh->nh_power;
@@ -1472,6 +1479,9 @@ last_resort:
 		if (!(nh->nh_flags & RTNH_F_DEAD)) {
 			if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
 				continue;
+			if (flp->fl4_gw && flp->fl4_gw != nh->nh_gw &&
+			    nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
+				continue;
 			spin_unlock_bh(&fib_multipath_lock);
 			res->nh_sel = nhsel;
 			return;
diff -urp v3.13-before-nf_reroute/linux/net/ipv4/fib_trie.c linux/net/ipv4/fib_trie.c
--- v3.13-before-nf_reroute/linux/net/ipv4/fib_trie.c	2014-01-25 10:55:22.697135537 +0200
+++ linux/net/ipv4/fib_trie.c	2014-01-25 10:55:46.129831952 +0200
@@ -1371,6 +1371,9 @@ static int check_leaf(struct fib_table *
 					continue;
 				if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
 					continue;
+				if (flp->fl4_gw && flp->fl4_gw != nh->nh_gw &&
+				    nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
+					continue;
 
 #ifdef CONFIG_IP_FIB_TRIE_STATS
 				t->stats.semantic_match_passed++;
diff -urp v3.13-before-nf_reroute/linux/net/ipv4/netfilter/ipt_MASQUERADE.c linux/net/ipv4/netfilter/ipt_MASQUERADE.c
--- v3.13-before-nf_reroute/linux/net/ipv4/netfilter/ipt_MASQUERADE.c	2013-11-04 01:41:51.000000000 +0200
+++ linux/net/ipv4/netfilter/ipt_MASQUERADE.c	2014-01-25 10:55:46.129831952 +0200
@@ -51,8 +51,8 @@ masquerade_tg(struct sk_buff *skb, const
 	enum ip_conntrack_info ctinfo;
 	struct nf_nat_range newrange;
 	const struct nf_nat_ipv4_multi_range_compat *mr;
-	const struct rtable *rt;
-	__be32 newsrc, nh;
+	struct rtable *rt;
+	__be32 newsrc;
 
 	NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);
 
@@ -69,12 +69,24 @@ masquerade_tg(struct sk_buff *skb, const
 		return NF_ACCEPT;
 
 	mr = par->targinfo;
-	rt = skb_rtable(skb);
-	nh = rt_nexthop(rt, ip_hdr(skb)->daddr);
-	newsrc = inet_select_addr(par->out, nh, RT_SCOPE_UNIVERSE);
-	if (!newsrc) {
-		pr_info("%s ate my IP address\n", par->out->name);
-		return NF_DROP;
+
+	{
+		struct flowi4 fl4 = { .flowi4_tos = RT_TOS(ip_hdr(skb)->tos),
+				      .flowi4_mark = skb->mark,
+				      .flowi4_oif = par->out->ifindex,
+				      .daddr = ip_hdr(skb)->daddr,
+				      .fl4_gw = skb_rtable(skb)->rt_gateway };
+		rt = ip_route_output_key(dev_net(par->out), &fl4);
+		if (IS_ERR(rt)) {
+			/* Funky routing can do this. */
+			if (net_ratelimit())
+				pr_info("%s:"
+				       " No route: Rusty's brain broke!\n",
+				       par->out->name);
+			return NF_DROP;
+		}
+		newsrc = fl4.saddr;
+		ip_rt_put(rt);
 	}
 
 	nat->masq_index = par->out->ifindex;
diff -urp v3.13-before-nf_reroute/linux/net/ipv4/netfilter/iptable_nat.c linux/net/ipv4/netfilter/iptable_nat.c
--- v3.13-before-nf_reroute/linux/net/ipv4/netfilter/iptable_nat.c	2014-01-21 11:18:21.000000000 +0200
+++ linux/net/ipv4/netfilter/iptable_nat.c	2014-01-25 10:55:46.130831912 +0200
@@ -273,6 +273,14 @@ static struct nf_hook_ops nf_nat_ipv4_op
 		.hooknum	= NF_INET_LOCAL_OUT,
 		.priority	= NF_IP_PRI_NAT_DST,
 	},
+	/* Before routing, route before mangling */
+	{
+		.hook		= ip_nat_route_input,
+		.owner		= THIS_MODULE,
+		.pf		= NFPROTO_IPV4,
+		.hooknum	= NF_INET_PRE_ROUTING,
+		.priority	= NF_IP_PRI_LAST-1,
+	},
 	/* After packet filtering, change source */
 	{
 		.hook		= nf_nat_ipv4_fn,
diff -urp v3.13-before-nf_reroute/linux/net/ipv4/route.c linux/net/ipv4/route.c
--- v3.13-before-nf_reroute/linux/net/ipv4/route.c	2014-01-25 10:55:22.698135473 +0200
+++ linux/net/ipv4/route.c	2014-01-25 10:55:46.132831845 +0200
@@ -1521,7 +1521,7 @@ static void ip_handle_martian_source(str
 static int __mkroute_input(struct sk_buff *skb,
 			   const struct fib_result *res,
 			   struct in_device *in_dev,
-			   __be32 daddr, __be32 saddr, u32 tos)
+			   __be32 daddr, __be32 saddr, u32 tos, __be32 lsrc)
 {
 	struct fib_nh_exception *fnhe;
 	struct rtable *rth;
@@ -1549,6 +1549,7 @@ static int __mkroute_input(struct sk_buf
 
 	do_cache = res->fi && !itag;
 	if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) &&
+	    !lsrc &&
 	    (IN_DEV_SHARED_MEDIA(out_dev) ||
 	     inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res)))) {
 		flags |= RTCF_DOREDIRECT;
@@ -1617,7 +1618,7 @@ static int ip_mkroute_input(struct sk_bu
 			    struct net *net,
 			    const struct flowi4 *fl4,
 			    struct in_device *in_dev,
-			    __be32 daddr, __be32 saddr, u32 tos)
+			    __be32 daddr, __be32 saddr, u32 tos, __be32 lsrc)
 {
 	fib_select_default(fl4, res);
 #ifdef CONFIG_IP_ROUTE_MULTIPATH
@@ -1626,7 +1627,7 @@ static int ip_mkroute_input(struct sk_bu
 #endif
 
 	/* create a routing cache entry */
-	return __mkroute_input(skb, res, in_dev, daddr, saddr, tos);
+	return __mkroute_input(skb, res, in_dev, daddr, saddr, tos, lsrc);
 }
 
 /*
@@ -1641,7 +1642,7 @@ static int ip_mkroute_input(struct sk_bu
  */
 
 static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
-			       u8 tos, struct net_device *dev)
+			       u8 tos, struct net_device *dev, __be32 lsrc)
 {
 	struct fib_result res;
 	struct in_device *in_dev = __in_dev_get_rcu(dev);
@@ -1689,17 +1690,27 @@ static int ip_route_input_slow(struct sk
 			goto martian_source;
 	}
 
+	if (lsrc) {
+		if (ipv4_is_multicast(lsrc) || ipv4_is_lbcast(lsrc) ||
+		    ipv4_is_zeronet(lsrc) || ipv4_is_loopback(lsrc))
+			goto martian_source;
+	}
+
 	/*
 	 *	Now we are ready to route packet.
 	 */
 	fl4.flowi4_oif = 0;
-	fl4.flowi4_iif = dev->ifindex;
+	fl4.flowi4_iif = lsrc ?
+			 LOOPBACK_IFINDEX : dev->ifindex;
 	fl4.flowi4_mark = skb->mark;
 	fl4.flowi4_tos = tos;
 	fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
 	fl4.daddr = daddr;
-	fl4.saddr = saddr;
+	fl4.saddr = lsrc? : saddr;
+	fl4.fl4_gw = 0;
 	err = fib_lookup(net, &fl4, &res);
+	fl4.flowi4_iif = dev->ifindex;
+	fl4.saddr = saddr;
 	if (err != 0)
 		goto no_route;
 
@@ -1722,12 +1733,15 @@ static int ip_route_input_slow(struct sk
 	if (res.type != RTN_UNICAST)
 		goto martian_destination;
 
-	err = ip_mkroute_input(skb, &res, net, &fl4, in_dev, daddr, saddr, tos);
+	err = ip_mkroute_input(skb, &res, net, &fl4, in_dev, daddr, saddr,
+		tos, lsrc);
 out:	return err;
 
 brd_input:
 	if (skb->protocol != htons(ETH_P_IP))
 		goto e_inval;
+	if (lsrc)
+		goto e_inval;
 
 	if (!ipv4_is_zeronet(saddr)) {
 		err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
@@ -1821,8 +1835,9 @@ martian_source_keep_err:
 	goto out;
 }
 
-int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,
-			 u8 tos, struct net_device *dev)
+int ip_route_input_noref_common(struct sk_buff *skb, __be32 daddr,
+				__be32 saddr, u8 tos, struct net_device *dev,
+				__be32 lsrc)
 {
 	int res;
 
@@ -1861,12 +1876,25 @@ int ip_route_input_noref(struct sk_buff
 		rcu_read_unlock();
 		return -EINVAL;
 	}
-	res = ip_route_input_slow(skb, daddr, saddr, tos, dev);
+	res = ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
 	rcu_read_unlock();
 	return res;
 }
+
+int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,
+			 u8 tos, struct net_device *dev)
+{
+	return ip_route_input_noref_common(skb, daddr, saddr, tos, dev, 0);
+}
 EXPORT_SYMBOL(ip_route_input_noref);
 
+int ip_route_input_lookup(struct sk_buff *skb, __be32 daddr, __be32 saddr,
+			  u8 tos, struct net_device *dev, __be32 lsrc)
+{
+	return ip_route_input_noref_common(skb, daddr, saddr, tos, dev, lsrc);
+}
+EXPORT_SYMBOL(ip_route_input_lookup);
+
 /* called with rcu_read_lock() */
 static struct rtable *__mkroute_output(const struct fib_result *res,
 				       const struct flowi4 *fl4, int orig_oif,
@@ -2098,6 +2126,7 @@ struct rtable *__ip_route_output_key(str
 			fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
 		dev_out = net->loopback_dev;
 		fl4->flowi4_oif = LOOPBACK_IFINDEX;
+		fl4->fl4_gw = 0;
 		res.type = RTN_LOCAL;
 		flags |= RTCF_LOCAL;
 		goto make_route;
@@ -2144,6 +2173,7 @@ struct rtable *__ip_route_output_key(str
 		}
 		dev_out = net->loopback_dev;
 		fl4->flowi4_oif = dev_out->ifindex;
+		fl4->fl4_gw = 0;
 		flags |= RTCF_LOCAL;
 		goto make_route;
 	}
diff -urp v3.13-before-nf_reroute/linux/net/netfilter/nf_nat_core.c linux/net/netfilter/nf_nat_core.c
--- v3.13-before-nf_reroute/linux/net/netfilter/nf_nat_core.c	2014-01-21 11:18:21.000000000 +0200
+++ linux/net/netfilter/nf_nat_core.c	2014-01-25 10:58:12.061946052 +0200
@@ -788,6 +788,51 @@ static struct nf_ct_helper_expectfn foll
 	.expectfn	= nf_nat_follow_master,
 };
 
+unsigned int ip_nat_route_input(const struct nf_hook_ops *ops,
+				struct sk_buff *skb,
+				const struct net_device *in,
+				const struct net_device *out,
+				int (*okfn)(struct sk_buff *))
+{
+	struct iphdr *iph;
+	struct nf_conn *conn;
+	enum ip_conntrack_info ctinfo;
+	enum ip_conntrack_dir dir;
+	unsigned long statusbit;
+	__be32 saddr;
+
+	if (!(conn = nf_ct_get(skb, &ctinfo)))
+		return NF_ACCEPT;
+
+	if (!(conn->status & IPS_NAT_DONE_MASK))
+		return NF_ACCEPT;
+	dir = CTINFO2DIR(ctinfo);
+	statusbit = IPS_SRC_NAT;
+	if (dir == IP_CT_DIR_REPLY)
+		statusbit ^= IPS_NAT_MASK;
+	if (!(conn->status & statusbit))
+		return NF_ACCEPT;
+
+	if (skb_dst(skb))
+		return NF_ACCEPT;
+
+	if (skb->len < sizeof(struct iphdr))
+		return NF_ACCEPT;
+
+	/* use daddr in other direction as masquerade address (lsrc) */
+	iph = ip_hdr(skb);
+	saddr = conn->tuplehash[!dir].tuple.dst.u3.ip;
+	if (saddr == iph->saddr)
+		return NF_ACCEPT;
+
+	if (ip_route_input_lookup(skb, iph->daddr, iph->saddr, iph->tos,
+	    skb->dev, saddr))
+		return NF_DROP;
+
+	return NF_ACCEPT;
+}
+EXPORT_SYMBOL_GPL(ip_nat_route_input);
+
 static int __init nf_nat_init(void)
 {
 	int ret;
