Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / net / ipv4 / netfilter / nf_nat_snmp_basic_main.c
1 /*
2  * nf_nat_snmp_basic.c
3  *
4  * Basic SNMP Application Layer Gateway
5  *
6  * This IP NAT module is intended for use with SNMP network
7  * discovery and monitoring applications where target networks use
8  * conflicting private address realms.
9  *
10  * Static NAT is used to remap the networks from the view of the network
11  * management system at the IP layer, and this module remaps some application
12  * layer addresses to match.
13  *
14  * The simplest form of ALG is performed, where only tagged IP addresses
15  * are modified.  The module does not need to be MIB aware and only scans
16  * messages at the ASN.1/BER level.
17  *
18  * Currently, only SNMPv1 and SNMPv2 are supported.
19  *
20  * More information on ALG and associated issues can be found in
21  * RFC 2962
22  *
23  * The ASB.1/BER parsing code is derived from the gxsnmp package by Gregory
24  * McLean & Jochen Friedrich, stripped down for use in the kernel.
25  *
26  * Copyright (c) 2000 RP Internet (www.rpi.net.au).
27  *
28  * This program is free software; you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation; either version 2 of the License, or
31  * (at your option) any later version.
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  * GNU General Public License for more details.
36  * You should have received a copy of the GNU General Public License
37  * along with this program; if not, see <http://www.gnu.org/licenses/>.
38  *
39  * Author: James Morris <jmorris@intercode.com.au>
40  *
41  * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
42  */
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/types.h>
46 #include <linux/kernel.h>
47 #include <linux/in.h>
48 #include <linux/ip.h>
49 #include <linux/udp.h>
50 #include <net/checksum.h>
51 #include <net/udp.h>
52
53 #include <net/netfilter/nf_nat.h>
54 #include <net/netfilter/nf_conntrack_expect.h>
55 #include <net/netfilter/nf_conntrack_helper.h>
56 #include <linux/netfilter/nf_conntrack_snmp.h>
57 #include "nf_nat_snmp_basic.asn1.h"
58
59 MODULE_LICENSE("GPL");
60 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
61 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
62 MODULE_ALIAS("ip_nat_snmp_basic");
63 MODULE_ALIAS_NFCT_HELPER("snmp_trap");
64
65 #define SNMP_PORT 161
66 #define SNMP_TRAP_PORT 162
67
68 static DEFINE_SPINLOCK(snmp_lock);
69
70 struct snmp_ctx {
71         unsigned char *begin;
72         __sum16 *check;
73         __be32 from;
74         __be32 to;
75 };
76
77 static void fast_csum(struct snmp_ctx *ctx, unsigned char offset)
78 {
79         unsigned char s[12] = {0,};
80         int size;
81
82         if (offset & 1) {
83                 memcpy(&s[1], &ctx->from, 4);
84                 memcpy(&s[7], &ctx->to, 4);
85                 s[0] = ~0;
86                 s[1] = ~s[1];
87                 s[2] = ~s[2];
88                 s[3] = ~s[3];
89                 s[4] = ~s[4];
90                 s[5] = ~0;
91                 size = 12;
92         } else {
93                 memcpy(&s[0], &ctx->from, 4);
94                 memcpy(&s[4], &ctx->to, 4);
95                 s[0] = ~s[0];
96                 s[1] = ~s[1];
97                 s[2] = ~s[2];
98                 s[3] = ~s[3];
99                 size = 8;
100         }
101         *ctx->check = csum_fold(csum_partial(s, size,
102                                              ~csum_unfold(*ctx->check)));
103 }
104
105 int snmp_version(void *context, size_t hdrlen, unsigned char tag,
106                  const void *data, size_t datalen)
107 {
108         if (*(unsigned char *)data > 1)
109                 return -ENOTSUPP;
110         return 1;
111 }
112
113 int snmp_helper(void *context, size_t hdrlen, unsigned char tag,
114                 const void *data, size_t datalen)
115 {
116         struct snmp_ctx *ctx = (struct snmp_ctx *)context;
117         __be32 *pdata = (__be32 *)data;
118
119         if (*pdata == ctx->from) {
120                 pr_debug("%s: %pI4 to %pI4\n", __func__,
121                          (void *)&ctx->from, (void *)&ctx->to);
122
123                 if (*ctx->check)
124                         fast_csum(ctx, (unsigned char *)data - ctx->begin);
125                 *pdata = ctx->to;
126         }
127
128         return 1;
129 }
130
131 static int snmp_translate(struct nf_conn *ct, int dir, struct sk_buff *skb)
132 {
133         struct iphdr *iph = ip_hdr(skb);
134         struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
135         u16 datalen = ntohs(udph->len) - sizeof(struct udphdr);
136         char *data = (unsigned char *)udph + sizeof(struct udphdr);
137         struct snmp_ctx ctx;
138         int ret;
139
140         if (dir == IP_CT_DIR_ORIGINAL) {
141                 ctx.from = ct->tuplehash[dir].tuple.src.u3.ip;
142                 ctx.to = ct->tuplehash[!dir].tuple.dst.u3.ip;
143         } else {
144                 ctx.from = ct->tuplehash[!dir].tuple.src.u3.ip;
145                 ctx.to = ct->tuplehash[dir].tuple.dst.u3.ip;
146         }
147
148         if (ctx.from == ctx.to)
149                 return NF_ACCEPT;
150
151         ctx.begin = (unsigned char *)udph + sizeof(struct udphdr);
152         ctx.check = &udph->check;
153         ret = asn1_ber_decoder(&nf_nat_snmp_basic_decoder, &ctx, data, datalen);
154         if (ret < 0) {
155                 nf_ct_helper_log(skb, ct, "parser failed\n");
156                 return NF_DROP;
157         }
158
159         return NF_ACCEPT;
160 }
161
162 /* We don't actually set up expectations, just adjust internal IP
163  * addresses if this is being NATted
164  */
165 static int help(struct sk_buff *skb, unsigned int protoff,
166                 struct nf_conn *ct,
167                 enum ip_conntrack_info ctinfo)
168 {
169         int dir = CTINFO2DIR(ctinfo);
170         unsigned int ret;
171         const struct iphdr *iph = ip_hdr(skb);
172         const struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
173
174         /* SNMP replies and originating SNMP traps get mangled */
175         if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
176                 return NF_ACCEPT;
177         if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
178                 return NF_ACCEPT;
179
180         /* No NAT? */
181         if (!(ct->status & IPS_NAT_MASK))
182                 return NF_ACCEPT;
183
184         /* Make sure the packet length is ok.  So far, we were only guaranteed
185          * to have a valid length IP header plus 8 bytes, which means we have
186          * enough room for a UDP header.  Just verify the UDP length field so we
187          * can mess around with the payload.
188          */
189         if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
190                 nf_ct_helper_log(skb, ct, "dropping malformed packet\n");
191                 return NF_DROP;
192         }
193
194         if (!skb_make_writable(skb, skb->len)) {
195                 nf_ct_helper_log(skb, ct, "cannot mangle packet");
196                 return NF_DROP;
197         }
198
199         spin_lock_bh(&snmp_lock);
200         ret = snmp_translate(ct, dir, skb);
201         spin_unlock_bh(&snmp_lock);
202         return ret;
203 }
204
205 static const struct nf_conntrack_expect_policy snmp_exp_policy = {
206         .max_expected   = 0,
207         .timeout        = 180,
208 };
209
210 static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
211         .me                     = THIS_MODULE,
212         .help                   = help,
213         .expect_policy          = &snmp_exp_policy,
214         .name                   = "snmp_trap",
215         .tuple.src.l3num        = AF_INET,
216         .tuple.src.u.udp.port   = cpu_to_be16(SNMP_TRAP_PORT),
217         .tuple.dst.protonum     = IPPROTO_UDP,
218 };
219
220 static int __init nf_nat_snmp_basic_init(void)
221 {
222         BUG_ON(nf_nat_snmp_hook != NULL);
223         RCU_INIT_POINTER(nf_nat_snmp_hook, help);
224
225         return nf_conntrack_helper_register(&snmp_trap_helper);
226 }
227
228 static void __exit nf_nat_snmp_basic_fini(void)
229 {
230         RCU_INIT_POINTER(nf_nat_snmp_hook, NULL);
231         synchronize_rcu();
232         nf_conntrack_helper_unregister(&snmp_trap_helper);
233 }
234
235 module_init(nf_nat_snmp_basic_init);
236 module_exit(nf_nat_snmp_basic_fini);