blob: b1227dc6f75e115c8cb2e88e488c9349686891a0 [file] [log] [blame]
Patrick McHardya536df32006-12-02 22:10:18 -08001/* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02002 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
Patrick McHardya536df32006-12-02 22:10:18 -08003 * 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.
6 */
7
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +01008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Patrick McHardya536df32006-12-02 22:10:18 -080010#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/in.h>
13#include <linux/udp.h>
Yasuyuki Kozakai1863f092006-12-02 22:12:54 -080014#include <linux/netfilter.h>
Patrick McHardya536df32006-12-02 22:10:18 -080015
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>
22
23MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
24MODULE_DESCRIPTION("TFTP connection tracking helper");
25MODULE_LICENSE("GPL");
26MODULE_ALIAS("ip_conntrack_tftp");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010027MODULE_ALIAS_NFCT_HELPER("tftp");
Patrick McHardya536df32006-12-02 22:10:18 -080028
29#define MAX_PORTS 8
30static unsigned short ports[MAX_PORTS];
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -080031static unsigned int ports_c;
Patrick McHardya536df32006-12-02 22:10:18 -080032module_param_array(ports, ushort, &ports_c, 0400);
33MODULE_PARM_DESC(ports, "Port numbers of TFTP servers");
34
Herbert Xu3db05fe2007-10-15 00:53:15 -070035unsigned int (*nf_nat_tftp_hook)(struct sk_buff *skb,
Patrick McHardya536df32006-12-02 22:10:18 -080036 enum ip_conntrack_info ctinfo,
37 struct nf_conntrack_expect *exp) __read_mostly;
38EXPORT_SYMBOL_GPL(nf_nat_tftp_hook);
39
Herbert Xu3db05fe2007-10-15 00:53:15 -070040static int tftp_help(struct sk_buff *skb,
Patrick McHardya536df32006-12-02 22:10:18 -080041 unsigned int protoff,
42 struct nf_conn *ct,
43 enum ip_conntrack_info ctinfo)
44{
Jan Engelhardtde24b4e2008-01-31 04:50:51 -080045 const struct tftphdr *tfh;
46 struct tftphdr _tftph;
Patrick McHardya536df32006-12-02 22:10:18 -080047 struct nf_conntrack_expect *exp;
48 struct nf_conntrack_tuple *tuple;
49 unsigned int ret = NF_ACCEPT;
Patrick McHardya536df32006-12-02 22:10:18 -080050 typeof(nf_nat_tftp_hook) nf_nat_tftp;
51
Herbert Xu3db05fe2007-10-15 00:53:15 -070052 tfh = skb_header_pointer(skb, protoff + sizeof(struct udphdr),
Patrick McHardya536df32006-12-02 22:10:18 -080053 sizeof(_tftph), &_tftph);
54 if (tfh == NULL)
55 return NF_ACCEPT;
56
57 switch (ntohs(tfh->opcode)) {
58 case TFTP_OPCODE_READ:
59 case TFTP_OPCODE_WRITE:
60 /* RRQ and WRQ works the same way */
Jan Engelhardt3c9fba62008-04-14 11:15:54 +020061 nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
62 nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
Patrick McHardya536df32006-12-02 22:10:18 -080063
Patrick McHardy68236452007-07-07 22:30:49 -070064 exp = nf_ct_expect_alloc(ct);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +010065 if (exp == NULL) {
66 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
Patrick McHardya536df32006-12-02 22:10:18 -080067 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +010068 }
Patrick McHardya536df32006-12-02 22:10:18 -080069 tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +020070 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
71 nf_ct_l3num(ct),
Patrick McHardy6002f2662008-03-25 20:09:15 -070072 &tuple->src.u3, &tuple->dst.u3,
Patrick McHardy68236452007-07-07 22:30:49 -070073 IPPROTO_UDP, NULL, &tuple->dst.u.udp.port);
Patrick McHardya536df32006-12-02 22:10:18 -080074
Patrick McHardy0d537782007-07-07 22:39:38 -070075 pr_debug("expect: ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +020076 nf_ct_dump_tuple(&exp->tuple);
Patrick McHardya536df32006-12-02 22:10:18 -080077
78 nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook);
79 if (nf_nat_tftp && ct->status & IPS_NAT_MASK)
Herbert Xu3db05fe2007-10-15 00:53:15 -070080 ret = nf_nat_tftp(skb, ctinfo, exp);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +010081 else if (nf_ct_expect_related(exp) != 0) {
82 nf_ct_helper_log(skb, ct, "cannot add expectation");
Patrick McHardya536df32006-12-02 22:10:18 -080083 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +010084 }
Patrick McHardy68236452007-07-07 22:30:49 -070085 nf_ct_expect_put(exp);
Patrick McHardya536df32006-12-02 22:10:18 -080086 break;
87 case TFTP_OPCODE_DATA:
88 case TFTP_OPCODE_ACK:
Patrick McHardy0d537782007-07-07 22:39:38 -070089 pr_debug("Data/ACK opcode\n");
Patrick McHardya536df32006-12-02 22:10:18 -080090 break;
91 case TFTP_OPCODE_ERROR:
Patrick McHardy0d537782007-07-07 22:39:38 -070092 pr_debug("Error opcode\n");
Patrick McHardya536df32006-12-02 22:10:18 -080093 break;
94 default:
Patrick McHardy0d537782007-07-07 22:39:38 -070095 pr_debug("Unknown opcode\n");
Patrick McHardya536df32006-12-02 22:10:18 -080096 }
97 return ret;
98}
99
Gao Feng82de0be2016-07-18 11:39:23 +0800100static struct nf_conntrack_helper tftp[MAX_PORTS * 2] __read_mostly;
Patrick McHardya536df32006-12-02 22:10:18 -0800101
Patrick McHardy6002f2662008-03-25 20:09:15 -0700102static const struct nf_conntrack_expect_policy tftp_exp_policy = {
103 .max_expected = 1,
104 .timeout = 5 * 60,
105};
106
Patrick McHardya536df32006-12-02 22:10:18 -0800107static void nf_conntrack_tftp_fini(void)
108{
Gao Feng82de0be2016-07-18 11:39:23 +0800109 nf_conntrack_helpers_unregister(tftp, ports_c * 2);
Patrick McHardya536df32006-12-02 22:10:18 -0800110}
111
112static int __init nf_conntrack_tftp_init(void)
113{
Gao Feng82de0be2016-07-18 11:39:23 +0800114 int i, ret;
Patrick McHardya536df32006-12-02 22:10:18 -0800115
116 if (ports_c == 0)
117 ports[ports_c++] = TFTP_PORT;
118
119 for (i = 0; i < ports_c; i++) {
Gao Feng82de0be2016-07-18 11:39:23 +0800120 nf_ct_helper_init(&tftp[2 * i], AF_INET, IPPROTO_UDP, "tftp",
121 TFTP_PORT, ports[i], i, &tftp_exp_policy,
122 0, 0, tftp_help, NULL, THIS_MODULE);
123 nf_ct_helper_init(&tftp[2 * i + 1], AF_INET6, IPPROTO_UDP, "tftp",
124 TFTP_PORT, ports[i], i, &tftp_exp_policy,
125 0, 0, tftp_help, NULL, THIS_MODULE);
126 }
Patrick McHardya536df32006-12-02 22:10:18 -0800127
Gao Feng82de0be2016-07-18 11:39:23 +0800128 ret = nf_conntrack_helpers_register(tftp, ports_c * 2);
129 if (ret < 0) {
130 pr_err("failed to register helpers\n");
131 return ret;
Patrick McHardya536df32006-12-02 22:10:18 -0800132 }
133 return 0;
134}
135
136module_init(nf_conntrack_tftp_init);
137module_exit(nf_conntrack_tftp_fini);