Skip to contentSkip to conversation

Change masquerade code to use first IP address of interface.

PATCH In review Rusty Russell 5 participants 10 messages Oct 20, 2004 – Oct 21, 2004

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Change masquerade code to use first IP address of interface.

As discussed previously, let's try this in 2.6.10, and see who screams.

Cheers,
Rusty.

Name: Change MASQUERADE to Use Device Address Directly
Status: Untested

Instead of doing a dubious route lookup, just use the first IP address of the (dynamic) interface. Also, reset assured bit so after a device goes down, masq connections can be cleaned up if memory pressure.

net/ipv4/netfilter/ipt_MASQUERADE.c +18 -29
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .2156-linux-2.6.9-rc1-bk12/net/ipv4/netfilter/ipt_MASQUERADE.c .2156-linux-2.6.9-rc1-bk12.updated/net/ipv4/netfilter/ipt_MASQUERADE.c
@@ -82,7 +82,6 @@ masquerade_target(struct sk_buff **pskb,
 	const struct ip_nat_multi_range *mr;
 	struct ip_nat_multi_range newrange;
 	u_int32_t newsrc;
-	struct rtable *rt;
 
 	IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
 
@@ -96,36 +95,12 @@ masquerade_target(struct sk_buff **pskb,
 	                    || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
 
 	mr = targinfo;
-
-	{
-		struct flowi fl = { .nl_u = { .ip4_u =
-					      { .daddr = (*pskb)->nh.iph->daddr,
-						.tos = (RT_TOS((*pskb)->nh.iph->tos) |
-							RTO_CONN),
-#ifdef CONFIG_IP_ROUTE_FWMARK
-						.fwmark = (*pskb)->nfmark
-#endif
-					      } } };
-		if (ip_route_output_key(&rt, &fl) != 0) {
-			/* Funky routing can do this. */
-			if (net_ratelimit())
-				printk("MASQUERADE:"
-				       " No route: Rusty's brain broke!\n");
-			return NF_DROP;
-		}
-		if (rt->u.dst.dev != out) {
-			if (net_ratelimit())
-				printk("MASQUERADE:"
-				       " Route sent us somewhere else.\n");
-			ip_rt_put(rt);
-			return NF_DROP;
-		}
+	newsrc = inet_select_addr(out, 0, RT_SCOPE_UNIVERSE);
+	if (!newsrc) {
+		printk("MASQUERADE: %s ate my IP address\n", out->name);
+		return NF_DROP;
 	}
 
-	newsrc = rt->rt_src;
-	DEBUGP("newsrc = %u.%u.%u.%u\n", NIPQUAD(newsrc));
-	ip_rt_put(rt);
-
 	WRITE_LOCK(&masq_lock);
 	ct->nat.masq_index = out->ifindex;
 	WRITE_UNLOCK(&masq_lock);
@@ -157,6 +132,18 @@ device_cmp(const struct ip_conntrack *i,
 	return ret;
 }
 
+static inline int
+connect_unassure(const struct ip_conntrack *i, void *_ina)
+{
+	struct in_ifaddr *ina = _ina;
+
+	/* We reset the ASSURED bit on all connections, so they will
+	 * get reaped under memory pressure. */
+	if (i->nat.masq_index == ina->ifa_dev->dev->ifindex)
+		clear_bit(IPS_ASSURED_BIT, (unsigned long *)&i->status);
+	return 0;
+}
+
 static int masq_inet_event(struct notifier_block *this,
 			   unsigned long event,
 			   void *ptr)
@@ -166,6 +153,8 @@ static int masq_inet_event(struct notifi
 	 * entries. */
 	if (event == NETDEV_UP)
 		ip_ct_selective_cleanup(device_cmp, ptr);
+	else if (event == NETDEV_DOWN)
+		ip_ct_selective_cleanup(connect_unassure, ptr);
 
 	return NOTIFY_DONE;
 }
Signature · 1 line
Anyone who quotes me in their signature is an idiot -- Rusty Russell
#1 lore raw
Quoted 1 line · Rusty Russell

As discussed previously, let's try this in 2.6.10, and see who screams.

Rather than screaming, I'm going to thank you :)

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw
In reply to Rusty Russell

net/ipv4/netfilter/ipt_MASQUERADE.c masquerade_target(struct sk_buff **pskb, · line 101

Quoted 5 lines · Rusty Russell
net/ipv4/netfilter/ipt_MASQUERADE.c +4 -0
+       newsrc = inet_select_addr(out, 0, RT_SCOPE_UNIVERSE);
+       if (!newsrc) {
+               printk("MASQUERADE: %s ate my IP address\n", out->name);
+               return NF_DROP;

Wouldn't it be better to select the address for the gateway that we're sending towards?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 9 lines · Herbert Xu

net/ipv4/netfilter/ipt_MASQUERADE.c masquerade_target(struct sk_buff **pskb, · line 101

Quoted 5 lines · Rusty Russell
net/ipv4/netfilter/ipt_MASQUERADE.c +4 -0
+       newsrc = inet_select_addr(out, 0, RT_SCOPE_UNIVERSE);
+       if (!newsrc) {
+               printk("MASQUERADE: %s ate my IP address\n", out->name);
+               return NF_DROP;

Wouldn't it be better to select the address for the gateway that we're sending towards?

What you mean?

It would surely not be sane to masquerade as the gateway we are sending towards.

Regards
Henrik

#4 lore raw
Quoted 9 lines · Henrik Nordstrom
Quoted 7 lines · Herbert Xu
Quoted 5 lines · Rusty Russell
net/ipv4/netfilter/ipt_MASQUERADE.c +4 -0
+       newsrc = inet_select_addr(out, 0, RT_SCOPE_UNIVERSE);
+       if (!newsrc) {
+               printk("MASQUERADE: %s ate my IP address\n", out->name);
+               return NF_DROP;
Quoted 4 lines
Quoted 2 lines

Wouldn't it be better to select the address for the gateway that we're sending towards?

What you mean?

I mean

	rt = (struct rtable *)(*pskb)->dst;
	newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE);

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#5 lore raw
Quoted 4 lines · Herbert Xu

I mean

	rt = (struct rtable *)(*pskb)->dst;
	newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE);

IIRC we are not allowed to access this here, but I may be wrong.. If we are allowe to access the rtable like this then this is obviously better, and should also solve most policy routing setups (and allow the others to be solved).

Regards
Henrik

#6 lore raw
Quoted 8 lines · Henrik Nordstrom
Quoted 4 lines · Herbert Xu

I mean

	rt = (struct rtable *)(*pskb)->dst;
	newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE);

IIRC we are not allowed to access this here, but I may be wrong.. If we

Why? POST_ROUTING is called in three places in ip_output.c and they all give a valid dst. In fact ip_refrag in ip_conntrack_standalone.c is also accessing skb->dst.

Quoted 3 lines

are allowe to access the rtable like this then this is obviously better, and should also solve most policy routing setups (and allow the others to be solved).

Well it isn't really equivalent to full policy routing since we've lost the preferred source field.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#7 lore raw
Quoted 5 lines · Herbert Xu
Quoted 4 lines · Henrik Nordstrom
Quoted 2 lines · Herbert Xu

I mean

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
	rt = (struct rtable *)(*pskb)->dst;
	newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE);

IIRC we are not allowed to access this here, but I may be wrong.. If we

Why? POST_ROUTING is called in three places in ip_output.c and they all give a valid dst. In fact ip_refrag in ip_conntrack_standalone.c is also accessing skb->dst.

Yeah, I like it. Please send patch on top of mine to Dave, and we'll see what blows up.

Rusty.

Signature · 1 line
Anyone who quotes me in their signature is an idiot -- Rusty Russell
maintainer
#8 lore raw
Quoted 9 lines · On Thu, 21 Oct 2004 10:50:49 +1000 Rusty Russell <[email protected]> wrote:
Quoted 8 lines · Herbert Xu
Quoted 7 lines · Henrik Nordstrom
Quoted 4 lines · Herbert Xu

I mean

	rt = (struct rtable *)(*pskb)->dst;
	newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE);
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

IIRC we are not allowed to access this here, but I may be wrong.. If we

Why? POST_ROUTING is called in three places in ip_output.c and they all give a valid dst. In fact ip_refrag in ip_conntrack_standalone.c is also accessing skb->dst.

Yeah, I like it. Please send patch on top of mine to Dave, and we'll see what blows up.

Patrick feel free to send this along to me whenever you like.

#9 lore raw
In reply to Herbert Xu
Quoted 10 lines · Herbert Xu

net/ipv4/netfilter/ipt_MASQUERADE.c masquerade_target(struct sk_buff **pskb, · line 101

Quoted 6 lines · Rusty Russell
net/ipv4/netfilter/ipt_MASQUERADE.c +4 -0
+       newsrc = inet_select_addr(out, 0, RT_SCOPE_UNIVERSE);
+       if (!newsrc) {
+               printk("MASQUERADE: %s ate my IP address\n", out->name);
+               return NF_DROP;
Quoted 4 lines

Wouldn't it be better to select the address for the gateway that we're sending towards?

I agree. Rusty, I already have your patch queued in a post-2.6.9 tree, I'm going to add Herbert's patch on top.

Regards
Patrick