Merge branch 'for-5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/dennis/percpu
[linux-2.6-microblaze.git] / net / netfilter / nf_conntrack_tftp.c
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.
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/in.h>
13 #include <linux/udp.h>
14 #include <linux/netfilter.h>
15
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
23 #define HELPER_NAME "tftp"
24
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);
30
31 #define MAX_PORTS 8
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");
36
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);
41
42 static int tftp_help(struct sk_buff *skb,
43                      unsigned int protoff,
44                      struct nf_conn *ct,
45                      enum ip_conntrack_info ctinfo)
46 {
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;
53
54         tfh = skb_header_pointer(skb, protoff + sizeof(struct udphdr),
55                                  sizeof(_tftph), &_tftph);
56         if (tfh == NULL)
57                 return NF_ACCEPT;
58
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);
65
66                 exp = nf_ct_expect_alloc(ct);
67                 if (exp == NULL) {
68                         nf_ct_helper_log(skb, ct, "cannot alloc expectation");
69                         return NF_DROP;
70                 }
71                 tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
72                 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
73                                   nf_ct_l3num(ct),
74                                   &tuple->src.u3, &tuple->dst.u3,
75                                   IPPROTO_UDP, NULL, &tuple->dst.u.udp.port);
76
77                 pr_debug("expect: ");
78                 nf_ct_dump_tuple(&exp->tuple);
79
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");
85                         ret = NF_DROP;
86                 }
87                 nf_ct_expect_put(exp);
88                 break;
89         case TFTP_OPCODE_DATA:
90         case TFTP_OPCODE_ACK:
91                 pr_debug("Data/ACK opcode\n");
92                 break;
93         case TFTP_OPCODE_ERROR:
94                 pr_debug("Error opcode\n");
95                 break;
96         default:
97                 pr_debug("Unknown opcode\n");
98         }
99         return ret;
100 }
101
102 static struct nf_conntrack_helper tftp[MAX_PORTS * 2] __read_mostly;
103
104 static const struct nf_conntrack_expect_policy tftp_exp_policy = {
105         .max_expected   = 1,
106         .timeout        = 5 * 60,
107 };
108
109 static void __exit nf_conntrack_tftp_fini(void)
110 {
111         nf_conntrack_helpers_unregister(tftp, ports_c * 2);
112 }
113
114 static int __init nf_conntrack_tftp_init(void)
115 {
116         int i, ret;
117
118         NF_CT_HELPER_BUILD_BUG_ON(0);
119
120         if (ports_c == 0)
121                 ports[ports_c++] = TFTP_PORT;
122
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,
127                                   THIS_MODULE);
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,
131                                   THIS_MODULE);
132         }
133
134         ret = nf_conntrack_helpers_register(tftp, ports_c * 2);
135         if (ret < 0) {
136                 pr_err("failed to register helpers\n");
137                 return ret;
138         }
139         return 0;
140 }
141
142 module_init(nf_conntrack_tftp_init);
143 module_exit(nf_conntrack_tftp_fini);