Skip to contentSkip to conversation

New standalone nat helper module which help function is never called

DISCUSSION tuXit 3 participants 9 messages Nov 17, 2003 – Nov 20, 2003 netfilter

lore
Share & tools
lore

tuXit

author
#0 lore raw

Hi all,

I am new in netfilter development, and I'm working on creating a module in order to modify tcp packets "on the fly", for some ASCII protocols (I first develop using the NNTP one). The final goal is to say in iptables which pattern of the packet toward a port to match, and with what to replace it.

Anyway, my problem is not yet in iptables, but in the nat helper module. I created one, defined as standalone ('cause no related connection is expected), defining that it uses the tcp protocol, the NNTP port (119), the masks... I inspired myself from the ftp nat helper and the snmp one (that is also standalone).

In the end, I insmod my module (which is marked as unused), and the logs show that it is properly registered. I said in iptables to accept the packets from/toward the NNTP port (119), and I also said to masquerade from one network to the other. When trying to contact the NNTP server on the other network from a client machine in the first network and using the machine on which I inserted the module as gate, the help function of my module is never called. There must be something I forgot, or I wrongly understood, but after having read several times the kernel hacking guide and searched inside this mailing-list archive, I can't find any reason.

I somebody has an idea about it, I would be glad to know it :) Thanks in advance,
tuXit.

Signature · 4 lines
Florian Pouradier
echo '|^H_!Xn|^H_' | less 
EpX Member - Debian GNU/Linux user
GPG FP: B0D7 1F53 94DA 0BCA C11E  F417 094B 49F3 D5DD F5EC 
#1 lore raw
Quoted 11 lines · tuXit

In the end, I insmod my module (which is marked as unused), and the logs show that it is properly registered. I said in iptables to accept the packets from/toward the NNTP port (119), and I also said to masquerade from one network to the other. When trying to contact the NNTP server on the other network from a client machine in the first network and using the machine on which I inserted the module as gate, the help function of my module is never called. There must be something I forgot, or I wrongly understood, but after having read several times the kernel hacking guide and searched inside this mailing-list archive, I can't find any reason.

I somebody has an idea about it, I would be glad to know it :)

if you could send us the current code, it woulde be easier to figure out what's going on.

Quoted 2 lines

Thanks in advance,
tuXit.

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
author
#2 lore raw
Quoted 12 lines · On Mon, 17 Nov 2003 23:31:30 +0100 Harald Welte <[email protected]> wrote:
Quoted 10 lines · tuXit

In the end, I insmod my module (which is marked as unused), and the logs show that it is properly registered. I said in iptables to accept the packets from/toward the NNTP port (119), and I also said to masquerade from one network to the other. When trying to contact the NNTP server on the other network from a client machine in the first network and using the machine on which I inserted the module as gate, the help function of my module is never called. There must be something I forgot, or I wrongly understood, but after having read several times the kernel hacking guide and searched inside this mailing-list archive, I can't find any reason.

Quoted 4 lines
Quoted 1 line

I somebody has an idea about it, I would be glad to know it :)

if you could send us the current code, it woulde be easier to figure out what's going on.

Of course...

----------8<----------8<----------8<----------

/* ASCII extension for TCP NAT alteration. */ #include <linux/module.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/tcp.h>
#include <linux/netfilter_ipv4/ip_nat.h> #include <linux/netfilter_ipv4/ip_nat_helper.h> #include <linux/netfilter_ipv4/ip_nat_rule.h>
/* #include <linux/netfilter_ipv4/ip_conntrack_ascii.h>*/

#if 1
#define DEBUGP printk
#else
#define DEBUGP(format, args...)
#endif

/* Modified by <[email protected]> at Sun Nov 16 23:38:44 2003 */ /* Used to declare the port on which to listen */
/* Later on, it will be an argument of the module */

#define ASCII_PORT 119
static int port;
DECLARE_LOCK(ip_ascii_lock);

static void mangle_ascii_packet(struct sk_buff **pskb,

				enum ip_conntrack_info ctinfo){

}

static unsigned int help(struct ip_conntrack *ct,
struct ip_conntrack_expect *exp, struct ip_nat_info *info, enum ip_conntrack_info ctinfo, unsigned int hooknum, struct sk_buff **pskb)

{
	struct iphdr *iph = (*pskb)->nh.iph;
	struct tcphdr *tcph = (void *)iph + iph->ihl*4;
	unsigned int datalen;
	int dir;

	DEBUGP("nat_ascii: ctinfo value is %i\n",
	       ctinfo);
	return NF_ACCEPT;
}

static struct ip_nat_helper ascii;

/* Not __exit: called from init() */
static void fini(void)

{
	DEBUGP("ip_nat_ascii: unregistering port %d\n", port);
	ip_nat_helper_unregister(&ascii);
}

static int __init init(void)

{
	int ret = 0;
	char *tmpname;
	char ascii_names[10];

	port = ASCII_PORT;

	ascii.list.next = 0;
	ascii.list.prev = 0;
	tmpname = &ascii_names[0];
	sprintf(tmpname, "ascii");
	ascii.name = tmpname;	
	ascii.flags = IP_NAT_HELPER_F_STANDALONE;
	ascii.me = THIS_MODULE;
	ascii.tuple.src.ip = 0;
	ascii.tuple.src.u.tcp.port = 0;
	ascii.tuple.dst.ip = 0;
	ascii.tuple.dst.u.tcp.port = __constant_htons(port);
	ascii.tuple.dst.protonum = IPPROTO_TCP;
	ascii.mask.src.ip = 0;
	ascii.mask.src.u.tcp.port = 0xFFFF;
	ascii.mask.dst.ip = 0;
	ascii.mask.dst.u.tcp.port = 0;
	ascii.mask.dst.protonum = 0xFFFF;
	ascii.help = help;
	ascii.expect = NULL;	
	
	DEBUGP("ip_nat_ascii: Trying to register for port %d\n",
	       port);
	ret = ip_nat_helper_register(&ascii);
	
	if (ret) {
	  printk("ip_nat_ascii: error registering "
		 "helper for port %d\n", port);
	  fini();
	  return ret;
	}
	return ret;
}

EXPORT_SYMBOL(ip_ascii_lock);

module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");

----------8<----------8<----------8<----------

I have put everything I could related to netfilter as a kernel module, in order to avoid rebooting after each change.
The this is that my module is successfully registered, but whenever a natted packet comes, my nat helper function is never called (for the moment, it does nothing but printing a debug message...

After some debugging, I see that the function do_bindings from ip_nat_core.c is called each time a natted packet comes, but the field info->helper is NULL instead of owning the helper function address.

Thanks in advance.

Signature · 4 lines
Florian Pouradier
echo '|^H_!Xn|^H_' | less 
EpX Member - Debian GNU/Linux user
GPG FP: B0D7 1F53 94DA 0BCA C11E  F417 094B 49F3 D5DD F5EC 
#3 lore raw
Quoted 1 line · tuXit
	ascii.flags = IP_NAT_HELPER_F_STANDALONE;

you are missing IP_NAT_HELPER_F_ALWAYS here. otherwise a nat helper will only be called if a connection tracking helper has issued an expectation within that packet.

Quoted 1 line

Florian Pouradier

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
author
#4 lore raw
Quoted 6 lines · On Tue, 18 Nov 2003 01:49:31 +0100 Harald Welte <[email protected]> wrote:
Quoted 1 line · tuXit
	ascii.flags = IP_NAT_HELPER_F_STANDALONE;

you are missing IP_NAT_HELPER_F_ALWAYS here. otherwise a nat helper will only be called if a connection tracking helper has issued an expectation within that packet.

That's true; I added this flag so now, I have :
ascii.flags = IP_NAT_HELPER_F_STANDALONE + IP_NAT_HELPER_F_ALWAYS; I also changed
char ascii_names[10];
that was in my previous post from the function
__init
outside of it (because it is in the stack, so deleted whenever this function is finished).

But it still doesn't help. The function do_bindings from the file ip_nat.core is called for each natted packet that is going toward port 119, but while trying to detect the helper, it value is NULL. I tried to try to debug, modifying the snmp nat helper module. I modified the udp fields into tcp fields, and the udp protocol into tcp protocol, and exchange source and destination. After these modifications in the
struct ip_nat_helper
the helper function is not called anymore whenever a natted packet arrives. So I don't understand what happens...

Signature · 4 lines
Florian Pouradier
echo '|^H_!Xn|^H_' | less 
EpX Member - Debian GNU/Linux user
GPG FP: B0D7 1F53 94DA 0BCA C11E  F417 094B 49F3 D5DD F5EC 
#5 lore raw
Quoted 19 lines · tuXit
Quoted 6 lines · On Tue, 18 Nov 2003 01:49:31 +0100 Harald Welte <[email protected]> wrote:
Quoted 1 line · tuXit
	ascii.flags = IP_NAT_HELPER_F_STANDALONE;

you are missing IP_NAT_HELPER_F_ALWAYS here. otherwise a nat helper will only be called if a connection tracking helper has issued an expectation within that packet.

That's true; I added this flag so now, I have :
ascii.flags = IP_NAT_HELPER_F_STANDALONE + IP_NAT_HELPER_F_ALWAYS; I also changed
char ascii_names[10];
that was in my previous post from the function
__init
outside of it (because it is in the stack, so deleted whenever this function is finished).

Quoted 1 line

But it still doesn't help.

Flags in a bitmask have to be bit-wise or'ed (IP_NAT_HELPER_F_STANDALONE|IP_NAT_HELPER_F_ALWAYS). Please see the existing code.

Quoted 1 line

Florian Pouradier

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
author
#6 lore raw
Quoted 20 lines · On Tue, 18 Nov 2003 17:59:04 +0100 Harald Welte <[email protected]> wrote:
Quoted 19 lines · tuXit
Quoted 6 lines · On Tue, 18 Nov 2003 01:49:31 +0100 Harald Welte <[email protected]> wrote:
Quoted 1 line · tuXit
	ascii.flags = IP_NAT_HELPER_F_STANDALONE;

you are missing IP_NAT_HELPER_F_ALWAYS here. otherwise a nat helper will only be called if a connection tracking helper has issued an expectation within that packet.

That's true; I added this flag so now, I have :
ascii.flags = IP_NAT_HELPER_F_STANDALONE + IP_NAT_HELPER_F_ALWAYS; I also changed
char ascii_names[10];
that was in my previous post from the function
__init
outside of it (because it is in the stack, so deleted whenever this function is finished).

Quoted 5 lines
Quoted 1 line

But it still doesn't help.

Flags in a bitmask have to be bit-wise or'ed (IP_NAT_HELPER_F_STANDALONE|IP_NAT_HELPER_F_ALWAYS). Please see the existing code.

I checked the code from the cuseeme-nat patch from the extra part of the patch-o-matic
(http://cvs.netfilter.org/~checkout~/netfilter/patch-o-matic//extra/cuseeme-n
at.patch), but that's true it is a bitmask...
Anyway, I changed this, and the nat helper function is not called. I put back my code to remind it:

----------8<----------8<----------8<----------8<----------8<----------8<-----

/* ASCII extension for TCP NAT alteration. */
#include <linux/module.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/tcp.h>
#include <linux/netfilter_ipv4/ip_nat.h>
#include <linux/netfilter_ipv4/ip_nat_helper.h>
#include <linux/netfilter_ipv4/ip_nat_rule.h>

#if 1
#define DEBUGP printk
#else
#define DEBUGP(format, args...)
#endif

#define ASCII_PORT 119

static int port;

static unsigned int ascii_help(struct ip_conntrack *ct,
struct ip_conntrack_expect *exp, struct ip_nat_info *info, enum ip_conntrack_info ctinfo, unsigned int hooknum, struct sk_buff **pskb)

{
        DEBUGP("ip_nat_ascii: The nat helper is called\n");
	return NF_ACCEPT;
}

static struct ip_nat_helper ascii;
static char ascii_names[10];

static void fini(void)

{
	DEBUGP("ip_nat_ascii: unregistering port %d\n", port);
	ip_nat_helper_unregister(&ascii);
}

static int __init init(void)

{
	int ret = 0;
	char *tmpname;

	port = ASCII_PORT;
	ascii.list.next = NULL;
	ascii.list.prev = NULL;
	tmpname = &ascii_names[0];
	sprintf(tmpname, "ascii");
	ascii.name = tmpname;
	ascii.flags = (IP_NAT_HELPER_F_STANDALONE | IP_NAT_HELPER_F_ALWAYS);
	ascii.me = THIS_MODULE;
	ascii.tuple.src.ip = 0;
	ascii.tuple.src.u.tcp.port = 0;
	ascii.tuple.dst.ip = 0;
	ascii.tuple.dst.u.tcp.port = __constant_htons(port);
	ascii.tuple.dst.protonum = IPPROTO_TCP;
	ascii.mask.src.ip = 0;
	ascii.mask.src.u.tcp.port = 0xFFFF;
	ascii.mask.dst.ip = 0;
	ascii.mask.dst.u.tcp.port = 0;
	ascii.mask.dst.protonum = 0xFFFF;
	ascii.help = ascii_help;
	ascii.expect = NULL;

	DEBUGP("ip_nat_ascii: Trying to register for port %d\n",
	       port);

	ret = ip_nat_helper_register(&ascii);
	
	if (ret) {
	  printk("ip_nat_ascii: error registering "
		 "helper for port %d\n", port);
	  fini();
	  return ret;
	}
	return ret;
}

----------8<----------8<----------8<----------8<----------8<----------8<-----

The do_bindings function from the ip_nat_core.c file tells me while debugging that info->helper is NULL just before trying to call the helper if it exists. This function seems to be called by ip_nat_fn from the ip_nat_standalone.c file. At the end of ip_nat_fn, the call to do_bindings passes info as a parameter. Just before the call, info->helper is NULL.

Thanks in advance,

Signature · 4 lines
Florian Pouradier
echo '|^H_!Xn|^H_' | less 
EpX Member - Debian GNU/Linux user
GPG FP: B0D7 1F53 94DA 0BCA C11E  F417 094B 49F3 D5DD F5EC 
#7 lore raw
Quoted 10 lines · tuXit
	ascii.tuple.src.ip = 0;
	ascii.tuple.src.u.tcp.port = 0;
	ascii.tuple.dst.ip = 0;
	ascii.tuple.dst.u.tcp.port = __constant_htons(port);
	ascii.tuple.dst.protonum = IPPROTO_TCP;
	ascii.mask.src.ip = 0;
	ascii.mask.src.u.tcp.port = 0xFFFF;
	ascii.mask.dst.ip = 0;
	ascii.mask.dst.u.tcp.port = 0;
	ascii.mask.dst.protonum = 0xFFFF;

The port masks looks a little odd to me.. I think you have got the src/dst masks the wrong way around and because of this no sessions will ever match your helper registration.

Regards
Henrik

#8 lore raw
In reply to tuXit
Quoted 10 lines · tuXit
	ascii.tuple.src.ip = 0;
	ascii.tuple.src.u.tcp.port = 0;
	ascii.tuple.dst.ip = 0;
	ascii.tuple.dst.u.tcp.port = __constant_htons(port);
	ascii.tuple.dst.protonum = IPPROTO_TCP;
	ascii.mask.src.ip = 0;
	ascii.mask.src.u.tcp.port = 0xFFFF;
	ascii.mask.dst.ip = 0;
	ascii.mask.dst.u.tcp.port = 0;
	ascii.mask.dst.protonum = 0xFFFF;

please revert the tupoel/mask part. Like in all existing helper, you need to specify the port as src.u.tcp.port

Quoted 2 lines

Thanks in advance,
Florian Pouradier

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie