]> err.no Git - linux-2.6/commitdiff
[NETFILTER]: nf_conntrack/nf_nat: add TFTP helper port
authorPatrick McHardy <kaber@trash.net>
Sun, 3 Dec 2006 06:10:18 +0000 (22:10 -0800)
committerDavid S. Miller <davem@davemloft.net>
Sun, 3 Dec 2006 06:10:18 +0000 (22:10 -0800)
Add IPv4 and IPv6 capable nf_conntrack port of the TFTP conntrack/NAT helper.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/netfilter/nf_conntrack_tftp.h [new file with mode: 0644]
net/ipv4/netfilter/Kconfig
net/ipv4/netfilter/Makefile
net/ipv4/netfilter/nf_nat_tftp.c [new file with mode: 0644]
net/netfilter/Kconfig
net/netfilter/Makefile
net/netfilter/nf_conntrack_tftp.c [new file with mode: 0644]

diff --git a/include/linux/netfilter/nf_conntrack_tftp.h b/include/linux/netfilter/nf_conntrack_tftp.h
new file mode 100644 (file)
index 0000000..0d79b7a
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef _NF_CONNTRACK_TFTP_H
+#define _NF_CONNTRACK_TFTP_H
+
+#define TFTP_PORT 69
+
+struct tftphdr {
+       __be16 opcode;
+};
+
+#define TFTP_OPCODE_READ       1
+#define TFTP_OPCODE_WRITE      2
+#define TFTP_OPCODE_DATA       3
+#define TFTP_OPCODE_ACK                4
+#define TFTP_OPCODE_ERROR      5
+
+extern unsigned int (*nf_nat_tftp_hook)(struct sk_buff **pskb,
+                                       enum ip_conntrack_info ctinfo,
+                                       struct nf_conntrack_expect *exp);
+
+#endif /* _NF_CONNTRACK_TFTP_H */
index 83e83f553ccb9ea446affe60b024268a9711a107..1c0018b6bea44c3d545ebe54e1eaf9a68196206f 100644 (file)
@@ -515,6 +515,11 @@ config IP_NF_NAT_TFTP
        default IP_NF_NAT if IP_NF_TFTP=y
        default m if IP_NF_TFTP=m
 
+config NF_NAT_TFTP
+       tristate
+       depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
+       default NF_NAT && NF_CONNTRACK_TFTP
+
 config IP_NF_NAT_AMANDA
        tristate
        depends on IP_NF_IPTABLES!=n && IP_NF_CONNTRACK!=n && IP_NF_NAT!=n
index 167b65e89acae969a62daf2c30164fd004593ea8..3ca0af0ed5f0342ae28c9f5b261c94b8bf5aebfe 100644 (file)
@@ -56,6 +56,7 @@ obj-$(CONFIG_NF_NAT_H323) += nf_nat_h323.o
 obj-$(CONFIG_NF_NAT_IRC) += nf_nat_irc.o
 obj-$(CONFIG_NF_NAT_PPTP) += nf_nat_pptp.o
 obj-$(CONFIG_NF_NAT_SIP) += nf_nat_sip.o
+obj-$(CONFIG_NF_NAT_TFTP) += nf_nat_tftp.o
 
 # NAT protocols (nf_nat)
 obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o
diff --git a/net/ipv4/netfilter/nf_nat_tftp.c b/net/ipv4/netfilter/nf_nat_tftp.c
new file mode 100644 (file)
index 0000000..2566b79
--- /dev/null
@@ -0,0 +1,52 @@
+/* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/udp.h>
+
+#include <net/netfilter/nf_nat_helper.h>
+#include <net/netfilter/nf_nat_rule.h>
+#include <net/netfilter/nf_conntrack_helper.h>
+#include <net/netfilter/nf_conntrack_expect.h>
+#include <linux/netfilter/nf_conntrack_tftp.h>
+
+MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
+MODULE_DESCRIPTION("TFTP NAT helper");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("ip_nat_tftp");
+
+static unsigned int help(struct sk_buff **pskb,
+                        enum ip_conntrack_info ctinfo,
+                        struct nf_conntrack_expect *exp)
+{
+       struct nf_conn *ct = exp->master;
+
+       exp->saved_proto.udp.port
+               = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
+       exp->dir = IP_CT_DIR_REPLY;
+       exp->expectfn = nf_nat_follow_master;
+       if (nf_conntrack_expect_related(exp) != 0)
+               return NF_DROP;
+       return NF_ACCEPT;
+}
+
+static void __exit nf_nat_tftp_fini(void)
+{
+       rcu_assign_pointer(nf_nat_tftp_hook, NULL);
+       synchronize_rcu();
+}
+
+static int __init nf_nat_tftp_init(void)
+{
+       BUG_ON(rcu_dereference(nf_nat_tftp_hook));
+       rcu_assign_pointer(nf_nat_tftp_hook, help);
+       return 0;
+}
+
+module_init(nf_nat_tftp_init);
+module_exit(nf_nat_tftp_fini);
index e73891194b315f75ad62dec748345790a1e5d408..e0deac17df12d163bf4442b856fdf9aded3ce781 100644 (file)
@@ -248,6 +248,17 @@ config NF_CONNTRACK_SIP
 
          To compile it as a module, choose M here.  If unsure, say N.
 
+config NF_CONNTRACK_TFTP
+       tristate "TFTP protocol support (EXPERIMENTAL)"
+       depends on EXPERIMENTAL && NF_CONNTRACK
+       help
+         TFTP connection tracking helper, this is required depending
+         on how restrictive your ruleset is.
+         If you are using a tftp client behind -j SNAT or -j MASQUERADING
+         you will need this.
+
+         To compile it as a module, choose M here.  If unsure, say N.
+
 config NF_CT_NETLINK
        tristate 'Connection tracking netlink interface (EXPERIMENTAL)'
        depends on EXPERIMENTAL && NF_CONNTRACK && NETFILTER_NETLINK
index 86da21700aa3c9f169ba2bdac9de1ce526de53c7..5dc5574f7e99ea79be21c416405aad456622659e 100644 (file)
@@ -30,6 +30,7 @@ obj-$(CONFIG_NF_CONNTRACK_IRC) += nf_conntrack_irc.o
 obj-$(CONFIG_NF_CONNTRACK_NETBIOS_NS) += nf_conntrack_netbios_ns.o
 obj-$(CONFIG_NF_CONNTRACK_PPTP) += nf_conntrack_pptp.o
 obj-$(CONFIG_NF_CONNTRACK_SIP) += nf_conntrack_sip.o
+obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
 
 # generic X tables 
 obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
diff --git a/net/netfilter/nf_conntrack_tftp.c b/net/netfilter/nf_conntrack_tftp.c
new file mode 100644 (file)
index 0000000..d46a126
--- /dev/null
@@ -0,0 +1,159 @@
+/* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/in.h>
+#include <linux/udp.h>
+
+#include <net/netfilter/nf_conntrack.h>
+#include <net/netfilter/nf_conntrack_tuple.h>
+#include <net/netfilter/nf_conntrack_expect.h>
+#include <net/netfilter/nf_conntrack_ecache.h>
+#include <net/netfilter/nf_conntrack_helper.h>
+#include <linux/netfilter/nf_conntrack_tftp.h>
+
+MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
+MODULE_DESCRIPTION("TFTP connection tracking helper");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("ip_conntrack_tftp");
+
+#define MAX_PORTS 8
+static unsigned short ports[MAX_PORTS];
+static int ports_c;
+module_param_array(ports, ushort, &ports_c, 0400);
+MODULE_PARM_DESC(ports, "Port numbers of TFTP servers");
+
+#if 0
+#define DEBUGP(format, args...) printk("%s:%s:" format, \
+                                       __FILE__, __FUNCTION__ , ## args)
+#else
+#define DEBUGP(format, args...)
+#endif
+
+unsigned int (*nf_nat_tftp_hook)(struct sk_buff **pskb,
+                                enum ip_conntrack_info ctinfo,
+                                struct nf_conntrack_expect *exp) __read_mostly;
+EXPORT_SYMBOL_GPL(nf_nat_tftp_hook);
+
+static int tftp_help(struct sk_buff **pskb,
+                    unsigned int protoff,
+                    struct nf_conn *ct,
+                    enum ip_conntrack_info ctinfo)
+{
+       struct tftphdr _tftph, *tfh;
+       struct nf_conntrack_expect *exp;
+       struct nf_conntrack_tuple *tuple;
+       unsigned int ret = NF_ACCEPT;
+       int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
+       typeof(nf_nat_tftp_hook) nf_nat_tftp;
+
+       tfh = skb_header_pointer(*pskb, protoff + sizeof(struct udphdr),
+                                sizeof(_tftph), &_tftph);
+       if (tfh == NULL)
+               return NF_ACCEPT;
+
+       switch (ntohs(tfh->opcode)) {
+       case TFTP_OPCODE_READ:
+       case TFTP_OPCODE_WRITE:
+               /* RRQ and WRQ works the same way */
+               DEBUGP("");
+               NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
+               NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
+
+               exp = nf_conntrack_expect_alloc(ct);
+               if (exp == NULL)
+                       return NF_DROP;
+               tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
+               nf_conntrack_expect_init(exp, family,
+                                        &tuple->src.u3, &tuple->dst.u3,
+                                        IPPROTO_UDP,
+                                        NULL, &tuple->dst.u.udp.port);
+
+               DEBUGP("expect: ");
+               NF_CT_DUMP_TUPLE(&exp->tuple);
+               NF_CT_DUMP_TUPLE(&exp->mask);
+
+               nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook);
+               if (nf_nat_tftp && ct->status & IPS_NAT_MASK)
+                       ret = nf_nat_tftp(pskb, ctinfo, exp);
+               else if (nf_conntrack_expect_related(exp) != 0)
+                       ret = NF_DROP;
+               nf_conntrack_expect_put(exp);
+               break;
+       case TFTP_OPCODE_DATA:
+       case TFTP_OPCODE_ACK:
+               DEBUGP("Data/ACK opcode\n");
+               break;
+       case TFTP_OPCODE_ERROR:
+               DEBUGP("Error opcode\n");
+               break;
+       default:
+               DEBUGP("Unknown opcode\n");
+       }
+       return ret;
+}
+
+static struct nf_conntrack_helper tftp[MAX_PORTS][2] __read_mostly;
+static char tftp_names[MAX_PORTS][2][sizeof("tftp-65535")] __read_mostly;
+
+static void nf_conntrack_tftp_fini(void)
+{
+       int i, j;
+
+       for (i = 0; i < ports_c; i++) {
+               for (j = 0; j < 2; j++)
+                       nf_conntrack_helper_unregister(&tftp[i][j]);
+       }
+}
+
+static int __init nf_conntrack_tftp_init(void)
+{
+       int i, j, ret;
+       char *tmpname;
+
+       if (ports_c == 0)
+               ports[ports_c++] = TFTP_PORT;
+
+       for (i = 0; i < ports_c; i++) {
+               memset(&tftp[i], 0, sizeof(tftp[i]));
+
+               tftp[i][0].tuple.src.l3num = AF_INET;
+               tftp[i][1].tuple.src.l3num = AF_INET6;
+               for (j = 0; j < 2; j++) {
+                       tftp[i][j].tuple.dst.protonum = IPPROTO_UDP;
+                       tftp[i][j].tuple.src.u.udp.port = htons(ports[i]);
+                       tftp[i][j].mask.src.l3num = 0xFFFF;
+                       tftp[i][j].mask.dst.protonum = 0xFF;
+                       tftp[i][j].mask.src.u.udp.port = htons(0xFFFF);
+                       tftp[i][j].max_expected = 1;
+                       tftp[i][j].timeout = 5 * 60; /* 5 minutes */
+                       tftp[i][j].me = THIS_MODULE;
+                       tftp[i][j].help = tftp_help;
+
+                       tmpname = &tftp_names[i][j][0];
+                       if (ports[i] == TFTP_PORT)
+                               sprintf(tmpname, "tftp");
+                       else
+                               sprintf(tmpname, "tftp-%u", i);
+                       tftp[i][j].name = tmpname;
+
+                       ret = nf_conntrack_helper_register(&tftp[i][j]);
+                       if (ret) {
+                               printk("nf_ct_tftp: failed to register helper "
+                                      "for pf: %u port: %u\n",
+                                       tftp[i][j].tuple.src.l3num, ports[i]);
+                               nf_conntrack_tftp_fini();
+                               return ret;
+                       }
+               }
+       }
+       return 0;
+}
+
+module_init(nf_conntrack_tftp_init);
+module_exit(nf_conntrack_tftp_fini);