From: Gregory Nietsky <gregory@distrotech.co.za>
To: netfilter@vger.kernel.org
Subject: libnetfilter_conntrack userspace nat via NFQUEUE
Date: Sun, 03 Jun 2012 23:05:19 +0200 [thread overview]
Message-ID: <4FCBD18F.2020607@distrotech.co.za> (raw)
Greetings
I have been working on userspace nat via NFQUEUE i have it working but
something does not make
sense to me.
the code below is to build the conntrack and attach the nat attributes.
i cannot get it working unless i use the following
nfct_set_attr_u8(ct, ATTR_TCP_STATE, TCP_CONNTRACK_ESTABLISHED);
the documentation and examples suggest this is not correct however this way
it works no other options function.
as the documentation is not extensive perhaps someone will be able to
comment on this.
am i correct to only use this for TCP connections.
the code for this is available @
http://pbx.distrotech.co.za/svn/taploop/trunk/ in the framework directory.
Regards
Greg
extern struct nf_conntrack *nf_ctrack_buildct(uint8_t *pkt) {
struct nf_conntrack *ct;
struct iphdr *ip = (struct iphdr*)pkt;
union l4hdr *l4 = (union l4hdr*)(pkt + (ip->ihl * 4));
if (!(ct = nfct_new())) {
return (NULL);
};
/*Build tuple*/
nfct_set_attr_u8(ct, ATTR_L3PROTO, PF_INET);
nfct_set_attr_u32(ct, ATTR_IPV4_SRC, ip->saddr);
nfct_set_attr_u32(ct, ATTR_IPV4_DST, ip->daddr);
nfct_set_attr_u8(ct, ATTR_L4PROTO, ip->protocol);
switch(ip->protocol) {
case IPPROTO_TCP:
nfct_set_attr_u16(ct, ATTR_PORT_SRC,
l4->tcp.source);
nfct_set_attr_u16(ct, ATTR_PORT_DST, l4->tcp.dest);
break;
case IPPROTO_UDP:
nfct_set_attr_u16(ct, ATTR_PORT_SRC,
l4->udp.source);
nfct_set_attr_u16(ct, ATTR_PORT_DST, l4->udp.dest);
break;
case IPPROTO_ICMP:
nfct_set_attr_u8(ct, ATTR_ICMP_TYPE,
l4->icmp.type);
nfct_set_attr_u8(ct, ATTR_ICMP_CODE,
l4->icmp.code);
nfct_set_attr_u16(ct, ATTR_ICMP_ID,
l4->icmp.un.echo.id);
/* no break */
default:
break;
};
return (ct);
}
extern uint8_t nf_ctrack_nat(uint8_t *pkt, uint32_t addr, uint16_t port,
uint8_t dnat) {
struct iphdr *ip = (struct iphdr*)pkt;
struct nf_conntrack *ct;
uint8_t unref = 0;
uint8_t ret = 0;
if (!ctrack) {
if (nf_ctrack_init()) {
return (-1);
}
unref = 1;
}
ct = nf_ctrack_buildct(pkt);
nfct_setobjopt(ct, NFCT_SOPT_SETUP_REPLY);
nfct_set_attr_u32(ct, ATTR_TIMEOUT, 120);
nfct_set_attr_u32(ct, (dnat) ? ATTR_DNAT_IPV4 : ATTR_SNAT_IPV4,
addr);
switch(ip->protocol) {
case IPPROTO_TCP:
nfct_set_attr_u8(ct, ATTR_TCP_STATE,
TCP_CONNTRACK_ESTABLISHED);
/* no break */
case IPPROTO_UDP:
if (port) {
nfct_set_attr_u16(ct, (dnat) ?
ATTR_DNAT_PORT : ATTR_SNAT_PORT, port);
}
break;
}
objlock(ctrack);
if (nfct_query(ctrack->nfct, NFCT_Q_CREATE_UPDATE, ct) < 0) {
ret = -1;
}
objunlock(ctrack);
nfct_destroy(ct);
if (unref) {
nf_ctrack_close();
}
return (ret);
}
--
This message has been scanned for viruses and
dangerous content by Distrotech Solutions,
it is believed to be clean.
http://www.distrotech.co.za
next reply other threads:[~2012-06-03 21:05 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-03 21:05 Gregory Nietsky [this message]
2012-06-04 12:20 ` libnetfilter_conntrack userspace nat via NFQUEUE Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4FCBD18F.2020607@distrotech.co.za \
--to=gregory@distrotech.co.za \
--cc=netfilter@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.