4 * Basic SNMP Application Layer Gateway
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.
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.
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.
18 * Currently, only SNMPv1 and SNMPv2 are supported.
20 * More information on ALG and associated issues can be found in
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.
26 * Copyright (c) 2000 RP Internet (www.rpi.net.au).
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/>.
39 * Author: James Morris <jmorris@intercode.com.au>
41 * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/types.h>
46 #include <linux/kernel.h>
49 #include <linux/udp.h>
50 #include <net/checksum.h>
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"
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");
65 #define SNMP_TRAP_PORT 162
67 static DEFINE_SPINLOCK(snmp_lock);
76 static void fast_csum(struct snmp_ctx *ctx, unsigned char offset)
78 unsigned char s[12] = {0,};
82 memcpy(&s[1], &ctx->from, 4);
83 memcpy(&s[7], &ctx->to, 4);
92 memcpy(&s[0], &ctx->from, 4);
93 memcpy(&s[4], &ctx->to, 4);
100 *ctx->check = csum_fold(csum_partial(s, size,
101 ~csum_unfold(*ctx->check)));
104 int snmp_version(void *context, size_t hdrlen, unsigned char tag,
105 const void *data, size_t datalen)
109 if (*(unsigned char *)data > 1)
114 int snmp_helper(void *context, size_t hdrlen, unsigned char tag,
115 const void *data, size_t datalen)
117 struct snmp_ctx *ctx = (struct snmp_ctx *)context;
122 pdata = (__be32 *)data;
123 if (*pdata == ctx->from) {
124 pr_debug("%s: %pI4 to %pI4\n", __func__,
125 (void *)&ctx->from, (void *)&ctx->to);
128 fast_csum(ctx, (unsigned char *)data - ctx->begin);
135 static int snmp_translate(struct nf_conn *ct, int dir, struct sk_buff *skb)
137 struct iphdr *iph = ip_hdr(skb);
138 struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
139 u16 datalen = ntohs(udph->len) - sizeof(struct udphdr);
140 char *data = (unsigned char *)udph + sizeof(struct udphdr);
144 if (dir == IP_CT_DIR_ORIGINAL) {
145 ctx.from = ct->tuplehash[dir].tuple.src.u3.ip;
146 ctx.to = ct->tuplehash[!dir].tuple.dst.u3.ip;
148 ctx.from = ct->tuplehash[!dir].tuple.src.u3.ip;
149 ctx.to = ct->tuplehash[dir].tuple.dst.u3.ip;
152 if (ctx.from == ctx.to)
155 ctx.begin = (unsigned char *)udph + sizeof(struct udphdr);
156 ctx.check = &udph->check;
157 ret = asn1_ber_decoder(&nf_nat_snmp_basic_decoder, &ctx, data, datalen);
159 nf_ct_helper_log(skb, ct, "parser failed\n");
166 /* We don't actually set up expectations, just adjust internal IP
167 * addresses if this is being NATted
169 static int help(struct sk_buff *skb, unsigned int protoff,
171 enum ip_conntrack_info ctinfo)
173 int dir = CTINFO2DIR(ctinfo);
175 const struct iphdr *iph = ip_hdr(skb);
176 const struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
178 /* SNMP replies and originating SNMP traps get mangled */
179 if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
181 if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
185 if (!(ct->status & IPS_NAT_MASK))
188 /* Make sure the packet length is ok. So far, we were only guaranteed
189 * to have a valid length IP header plus 8 bytes, which means we have
190 * enough room for a UDP header. Just verify the UDP length field so we
191 * can mess around with the payload.
193 if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
194 nf_ct_helper_log(skb, ct, "dropping malformed packet\n");
198 if (!skb_make_writable(skb, skb->len)) {
199 nf_ct_helper_log(skb, ct, "cannot mangle packet");
203 spin_lock_bh(&snmp_lock);
204 ret = snmp_translate(ct, dir, skb);
205 spin_unlock_bh(&snmp_lock);
209 static const struct nf_conntrack_expect_policy snmp_exp_policy = {
214 static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
217 .expect_policy = &snmp_exp_policy,
219 .tuple.src.l3num = AF_INET,
220 .tuple.src.u.udp.port = cpu_to_be16(SNMP_TRAP_PORT),
221 .tuple.dst.protonum = IPPROTO_UDP,
224 static int __init nf_nat_snmp_basic_init(void)
226 BUG_ON(nf_nat_snmp_hook != NULL);
227 RCU_INIT_POINTER(nf_nat_snmp_hook, help);
229 return nf_conntrack_helper_register(&snmp_trap_helper);
232 static void __exit nf_nat_snmp_basic_fini(void)
234 RCU_INIT_POINTER(nf_nat_snmp_hook, NULL);
236 nf_conntrack_helper_unregister(&snmp_trap_helper);
239 module_init(nf_nat_snmp_basic_init);
240 module_exit(nf_nat_snmp_basic_fini);