1 /* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
2 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
13 #include <linux/udp.h>
14 #include <linux/netfilter.h>
16 #include <net/netfilter/nf_conntrack.h>
17 #include <net/netfilter/nf_conntrack_tuple.h>
18 #include <net/netfilter/nf_conntrack_expect.h>
19 #include <net/netfilter/nf_conntrack_ecache.h>
20 #include <net/netfilter/nf_conntrack_helper.h>
21 #include <linux/netfilter/nf_conntrack_tftp.h>
23 #define HELPER_NAME "tftp"
25 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
26 MODULE_DESCRIPTION("TFTP connection tracking helper");
27 MODULE_LICENSE("GPL");
28 MODULE_ALIAS("ip_conntrack_tftp");
29 MODULE_ALIAS_NFCT_HELPER(HELPER_NAME);
32 static unsigned short ports[MAX_PORTS];
33 static unsigned int ports_c;
34 module_param_array(ports, ushort, &ports_c, 0400);
35 MODULE_PARM_DESC(ports, "Port numbers of TFTP servers");
37 unsigned int (*nf_nat_tftp_hook)(struct sk_buff *skb,
38 enum ip_conntrack_info ctinfo,
39 struct nf_conntrack_expect *exp) __read_mostly;
40 EXPORT_SYMBOL_GPL(nf_nat_tftp_hook);
42 static int tftp_help(struct sk_buff *skb,
45 enum ip_conntrack_info ctinfo)
47 const struct tftphdr *tfh;
48 struct tftphdr _tftph;
49 struct nf_conntrack_expect *exp;
50 struct nf_conntrack_tuple *tuple;
51 unsigned int ret = NF_ACCEPT;
52 typeof(nf_nat_tftp_hook) nf_nat_tftp;
54 tfh = skb_header_pointer(skb, protoff + sizeof(struct udphdr),
55 sizeof(_tftph), &_tftph);
59 switch (ntohs(tfh->opcode)) {
60 case TFTP_OPCODE_READ:
61 case TFTP_OPCODE_WRITE:
62 /* RRQ and WRQ works the same way */
63 nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
64 nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
66 exp = nf_ct_expect_alloc(ct);
68 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
71 tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
72 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
74 &tuple->src.u3, &tuple->dst.u3,
75 IPPROTO_UDP, NULL, &tuple->dst.u.udp.port);
78 nf_ct_dump_tuple(&exp->tuple);
80 nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook);
81 if (nf_nat_tftp && ct->status & IPS_NAT_MASK)
82 ret = nf_nat_tftp(skb, ctinfo, exp);
83 else if (nf_ct_expect_related(exp) != 0) {
84 nf_ct_helper_log(skb, ct, "cannot add expectation");
87 nf_ct_expect_put(exp);
89 case TFTP_OPCODE_DATA:
91 pr_debug("Data/ACK opcode\n");
93 case TFTP_OPCODE_ERROR:
94 pr_debug("Error opcode\n");
97 pr_debug("Unknown opcode\n");
102 static struct nf_conntrack_helper tftp[MAX_PORTS * 2] __read_mostly;
104 static const struct nf_conntrack_expect_policy tftp_exp_policy = {
109 static void __exit nf_conntrack_tftp_fini(void)
111 nf_conntrack_helpers_unregister(tftp, ports_c * 2);
114 static int __init nf_conntrack_tftp_init(void)
118 NF_CT_HELPER_BUILD_BUG_ON(0);
121 ports[ports_c++] = TFTP_PORT;
123 for (i = 0; i < ports_c; i++) {
124 nf_ct_helper_init(&tftp[2 * i], AF_INET, IPPROTO_UDP,
125 HELPER_NAME, TFTP_PORT, ports[i], i,
126 &tftp_exp_policy, 0, tftp_help, NULL,
128 nf_ct_helper_init(&tftp[2 * i + 1], AF_INET6, IPPROTO_UDP,
129 HELPER_NAME, TFTP_PORT, ports[i], i,
130 &tftp_exp_policy, 0, tftp_help, NULL,
134 ret = nf_conntrack_helpers_register(tftp, ports_c * 2);
136 pr_err("failed to register helpers\n");
142 module_init(nf_conntrack_tftp_init);
143 module_exit(nf_conntrack_tftp_fini);