1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Basic SNMP Application Layer Gateway
7 * This IP NAT module is intended for use with SNMP network
8 * discovery and monitoring applications where target networks use
9 * conflicting private address realms.
11 * Static NAT is used to remap the networks from the view of the network
12 * management system at the IP layer, and this module remaps some application
13 * layer addresses to match.
15 * The simplest form of ALG is performed, where only tagged IP addresses
16 * are modified. The module does not need to be MIB aware and only scans
17 * messages at the ASN.1/BER level.
19 * Currently, only SNMPv1 and SNMPv2 are supported.
21 * More information on ALG and associated issues can be found in
24 * The ASB.1/BER parsing code is derived from the gxsnmp package by Gregory
25 * McLean & Jochen Friedrich, stripped down for use in the kernel.
27 * Copyright (c) 2000 RP Internet (www.rpi.net.au).
29 * Author: James Morris <jmorris@intercode.com.au>
31 * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/types.h>
36 #include <linux/kernel.h>
39 #include <linux/udp.h>
40 #include <net/checksum.h>
43 #include <net/netfilter/nf_nat.h>
44 #include <net/netfilter/nf_conntrack_expect.h>
45 #include <net/netfilter/nf_conntrack_helper.h>
46 #include <linux/netfilter/nf_conntrack_snmp.h>
47 #include "nf_nat_snmp_basic.asn1.h"
49 MODULE_LICENSE("GPL");
50 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
51 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
52 MODULE_ALIAS("ip_nat_snmp_basic");
53 MODULE_ALIAS_NFCT_HELPER("snmp_trap");
56 #define SNMP_TRAP_PORT 162
58 static DEFINE_SPINLOCK(snmp_lock);
67 static void fast_csum(struct snmp_ctx *ctx, unsigned char offset)
69 unsigned char s[12] = {0,};
73 memcpy(&s[1], &ctx->from, 4);
74 memcpy(&s[7], &ctx->to, 4);
83 memcpy(&s[0], &ctx->from, 4);
84 memcpy(&s[4], &ctx->to, 4);
91 *ctx->check = csum_fold(csum_partial(s, size,
92 ~csum_unfold(*ctx->check)));
95 int snmp_version(void *context, size_t hdrlen, unsigned char tag,
96 const void *data, size_t datalen)
100 if (*(unsigned char *)data > 1)
105 int snmp_helper(void *context, size_t hdrlen, unsigned char tag,
106 const void *data, size_t datalen)
108 struct snmp_ctx *ctx = (struct snmp_ctx *)context;
113 pdata = (__be32 *)data;
114 if (*pdata == ctx->from) {
115 pr_debug("%s: %pI4 to %pI4\n", __func__,
116 (void *)&ctx->from, (void *)&ctx->to);
119 fast_csum(ctx, (unsigned char *)data - ctx->begin);
126 static int snmp_translate(struct nf_conn *ct, int dir, struct sk_buff *skb)
128 struct iphdr *iph = ip_hdr(skb);
129 struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
130 u16 datalen = ntohs(udph->len) - sizeof(struct udphdr);
131 char *data = (unsigned char *)udph + sizeof(struct udphdr);
135 if (dir == IP_CT_DIR_ORIGINAL) {
136 ctx.from = ct->tuplehash[dir].tuple.src.u3.ip;
137 ctx.to = ct->tuplehash[!dir].tuple.dst.u3.ip;
139 ctx.from = ct->tuplehash[!dir].tuple.src.u3.ip;
140 ctx.to = ct->tuplehash[dir].tuple.dst.u3.ip;
143 if (ctx.from == ctx.to)
146 ctx.begin = (unsigned char *)udph + sizeof(struct udphdr);
147 ctx.check = &udph->check;
148 ret = asn1_ber_decoder(&nf_nat_snmp_basic_decoder, &ctx, data, datalen);
150 nf_ct_helper_log(skb, ct, "parser failed\n");
157 /* We don't actually set up expectations, just adjust internal IP
158 * addresses if this is being NATted
160 static int help(struct sk_buff *skb, unsigned int protoff,
162 enum ip_conntrack_info ctinfo)
164 int dir = CTINFO2DIR(ctinfo);
166 const struct iphdr *iph = ip_hdr(skb);
167 const struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
169 /* SNMP replies and originating SNMP traps get mangled */
170 if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
172 if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
176 if (!(ct->status & IPS_NAT_MASK))
179 /* Make sure the packet length is ok. So far, we were only guaranteed
180 * to have a valid length IP header plus 8 bytes, which means we have
181 * enough room for a UDP header. Just verify the UDP length field so we
182 * can mess around with the payload.
184 if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
185 nf_ct_helper_log(skb, ct, "dropping malformed packet\n");
189 if (skb_ensure_writable(skb, skb->len)) {
190 nf_ct_helper_log(skb, ct, "cannot mangle packet");
194 spin_lock_bh(&snmp_lock);
195 ret = snmp_translate(ct, dir, skb);
196 spin_unlock_bh(&snmp_lock);
200 static const struct nf_conntrack_expect_policy snmp_exp_policy = {
205 static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
208 .expect_policy = &snmp_exp_policy,
210 .tuple.src.l3num = AF_INET,
211 .tuple.src.u.udp.port = cpu_to_be16(SNMP_TRAP_PORT),
212 .tuple.dst.protonum = IPPROTO_UDP,
215 static int __init nf_nat_snmp_basic_init(void)
217 BUG_ON(nf_nat_snmp_hook != NULL);
218 RCU_INIT_POINTER(nf_nat_snmp_hook, help);
220 return nf_conntrack_helper_register(&snmp_trap_helper);
223 static void __exit nf_nat_snmp_basic_fini(void)
225 RCU_INIT_POINTER(nf_nat_snmp_hook, NULL);
227 nf_conntrack_helper_unregister(&snmp_trap_helper);
230 module_init(nf_nat_snmp_basic_init);
231 module_exit(nf_nat_snmp_basic_fini);