2006-12-03 06:10:34 +00:00
|
|
|
/*
|
|
|
|
* nf_nat_snmp_basic.c
|
|
|
|
*
|
|
|
|
* Basic SNMP Application Layer Gateway
|
|
|
|
*
|
|
|
|
* This IP NAT module is intended for use with SNMP network
|
|
|
|
* discovery and monitoring applications where target networks use
|
|
|
|
* conflicting private address realms.
|
|
|
|
*
|
|
|
|
* Static NAT is used to remap the networks from the view of the network
|
|
|
|
* management system at the IP layer, and this module remaps some application
|
|
|
|
* layer addresses to match.
|
|
|
|
*
|
|
|
|
* The simplest form of ALG is performed, where only tagged IP addresses
|
|
|
|
* are modified. The module does not need to be MIB aware and only scans
|
|
|
|
* messages at the ASN.1/BER level.
|
|
|
|
*
|
|
|
|
* Currently, only SNMPv1 and SNMPv2 are supported.
|
|
|
|
*
|
|
|
|
* More information on ALG and associated issues can be found in
|
|
|
|
* RFC 2962
|
|
|
|
*
|
|
|
|
* The ASB.1/BER parsing code is derived from the gxsnmp package by Gregory
|
|
|
|
* McLean & Jochen Friedrich, stripped down for use in the kernel.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2000 RP Internet (www.rpi.net.au).
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2013-12-06 17:13:39 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2006-12-03 06:10:34 +00:00
|
|
|
*
|
|
|
|
* Author: James Morris <jmorris@intercode.com.au>
|
2013-04-06 13:24:29 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
|
2006-12-03 06:10:34 +00:00
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2006-12-03 06:10:34 +00:00
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/udp.h>
|
|
|
|
#include <net/checksum.h>
|
|
|
|
#include <net/udp.h>
|
|
|
|
|
|
|
|
#include <net/netfilter/nf_nat.h>
|
2008-03-26 03:09:15 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_expect.h>
|
2006-12-03 06:10:34 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_helper.h>
|
|
|
|
#include <net/netfilter/nf_nat_helper.h>
|
2011-01-18 17:12:24 +00:00
|
|
|
#include <linux/netfilter/nf_conntrack_snmp.h>
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
|
|
|
|
MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
|
|
|
|
MODULE_ALIAS("ip_nat_snmp_basic");
|
|
|
|
|
|
|
|
#define SNMP_PORT 161
|
|
|
|
#define SNMP_TRAP_PORT 162
|
2007-12-18 06:44:06 +00:00
|
|
|
#define NOCT1(n) (*(u8 *)(n))
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
static int debug;
|
|
|
|
static DEFINE_SPINLOCK(snmp_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Application layer address mapping mimics the NAT mapping, but
|
|
|
|
* only for the first octet in this case (a more flexible system
|
|
|
|
* can be implemented if needed).
|
|
|
|
*/
|
|
|
|
struct oct1_map
|
|
|
|
{
|
|
|
|
u_int8_t from;
|
|
|
|
u_int8_t to;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/* Class */
|
|
|
|
#define ASN1_UNI 0 /* Universal */
|
|
|
|
#define ASN1_APL 1 /* Application */
|
|
|
|
#define ASN1_CTX 2 /* Context */
|
|
|
|
#define ASN1_PRV 3 /* Private */
|
|
|
|
|
|
|
|
/* Tag */
|
|
|
|
#define ASN1_EOC 0 /* End Of Contents */
|
|
|
|
#define ASN1_BOL 1 /* Boolean */
|
|
|
|
#define ASN1_INT 2 /* Integer */
|
|
|
|
#define ASN1_BTS 3 /* Bit String */
|
|
|
|
#define ASN1_OTS 4 /* Octet String */
|
|
|
|
#define ASN1_NUL 5 /* Null */
|
|
|
|
#define ASN1_OJI 6 /* Object Identifier */
|
|
|
|
#define ASN1_OJD 7 /* Object Description */
|
|
|
|
#define ASN1_EXT 8 /* External */
|
|
|
|
#define ASN1_SEQ 16 /* Sequence */
|
|
|
|
#define ASN1_SET 17 /* Set */
|
|
|
|
#define ASN1_NUMSTR 18 /* Numerical String */
|
|
|
|
#define ASN1_PRNSTR 19 /* Printable String */
|
|
|
|
#define ASN1_TEXSTR 20 /* Teletext String */
|
|
|
|
#define ASN1_VIDSTR 21 /* Video String */
|
|
|
|
#define ASN1_IA5STR 22 /* IA5 String */
|
|
|
|
#define ASN1_UNITIM 23 /* Universal Time */
|
|
|
|
#define ASN1_GENTIM 24 /* General Time */
|
|
|
|
#define ASN1_GRASTR 25 /* Graphical String */
|
|
|
|
#define ASN1_VISSTR 26 /* Visible String */
|
|
|
|
#define ASN1_GENSTR 27 /* General String */
|
|
|
|
|
|
|
|
/* Primitive / Constructed methods*/
|
|
|
|
#define ASN1_PRI 0 /* Primitive */
|
|
|
|
#define ASN1_CON 1 /* Constructed */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Error codes.
|
|
|
|
*/
|
|
|
|
#define ASN1_ERR_NOERROR 0
|
|
|
|
#define ASN1_ERR_DEC_EMPTY 2
|
|
|
|
#define ASN1_ERR_DEC_EOC_MISMATCH 3
|
|
|
|
#define ASN1_ERR_DEC_LENGTH_MISMATCH 4
|
|
|
|
#define ASN1_ERR_DEC_BADVALUE 5
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ASN.1 context.
|
|
|
|
*/
|
|
|
|
struct asn1_ctx
|
|
|
|
{
|
|
|
|
int error; /* Error condition */
|
|
|
|
unsigned char *pointer; /* Octet just to be decoded */
|
|
|
|
unsigned char *begin; /* First octet */
|
|
|
|
unsigned char *end; /* Octet after last octet */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Octet string (not null terminated)
|
|
|
|
*/
|
|
|
|
struct asn1_octstr
|
|
|
|
{
|
|
|
|
unsigned char *data;
|
|
|
|
unsigned int len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void asn1_open(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char *buf,
|
|
|
|
unsigned int len)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
ctx->begin = buf;
|
|
|
|
ctx->end = buf + len;
|
|
|
|
ctx->pointer = buf;
|
|
|
|
ctx->error = ASN1_ERR_NOERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
|
|
|
|
{
|
|
|
|
if (ctx->pointer >= ctx->end) {
|
|
|
|
ctx->error = ASN1_ERR_DEC_EMPTY;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*ch = *(ctx->pointer)++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
|
|
|
|
{
|
|
|
|
unsigned char ch;
|
|
|
|
|
|
|
|
*tag = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
*tag <<= 7;
|
|
|
|
*tag |= ch & 0x7F;
|
|
|
|
} while ((ch & 0x80) == 0x80);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_id_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned int *cls,
|
|
|
|
unsigned int *con,
|
|
|
|
unsigned int *tag)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char ch;
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*cls = (ch & 0xC0) >> 6;
|
|
|
|
*con = (ch & 0x20) >> 5;
|
|
|
|
*tag = (ch & 0x1F);
|
|
|
|
|
|
|
|
if (*tag == 0x1F) {
|
|
|
|
if (!asn1_tag_decode(ctx, tag))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_length_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned int *def,
|
|
|
|
unsigned int *len)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char ch, cnt;
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ch == 0x80)
|
|
|
|
*def = 0;
|
|
|
|
else {
|
|
|
|
*def = 1;
|
|
|
|
|
|
|
|
if (ch < 0x80)
|
|
|
|
*len = ch;
|
|
|
|
else {
|
2008-04-14 09:15:42 +00:00
|
|
|
cnt = ch & 0x7F;
|
2006-12-03 06:10:34 +00:00
|
|
|
*len = 0;
|
|
|
|
|
|
|
|
while (cnt > 0) {
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
*len <<= 8;
|
|
|
|
*len |= ch;
|
|
|
|
cnt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-04 16:16:33 +00:00
|
|
|
|
|
|
|
/* don't trust len bigger than ctx buffer */
|
|
|
|
if (*len > ctx->end - ctx->pointer)
|
|
|
|
return 0;
|
|
|
|
|
2006-12-03 06:10:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_header_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char **eoc,
|
|
|
|
unsigned int *cls,
|
|
|
|
unsigned int *con,
|
|
|
|
unsigned int *tag)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned int def, len;
|
|
|
|
|
|
|
|
if (!asn1_id_decode(ctx, cls, con, tag))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
def = len = 0;
|
|
|
|
if (!asn1_length_decode(ctx, &def, &len))
|
|
|
|
return 0;
|
|
|
|
|
2008-06-04 16:16:33 +00:00
|
|
|
/* primitive shall be definite, indefinite shall be constructed */
|
|
|
|
if (*con == ASN1_PRI && !def)
|
|
|
|
return 0;
|
|
|
|
|
2006-12-03 06:10:34 +00:00
|
|
|
if (def)
|
|
|
|
*eoc = ctx->pointer + len;
|
|
|
|
else
|
|
|
|
*eoc = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
|
|
|
|
{
|
|
|
|
unsigned char ch;
|
|
|
|
|
2008-01-31 12:07:08 +00:00
|
|
|
if (eoc == NULL) {
|
2006-12-03 06:10:34 +00:00
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ch != 0x00) {
|
|
|
|
ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ch != 0x00) {
|
|
|
|
ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (ctx->pointer != eoc) {
|
|
|
|
ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
|
|
|
|
{
|
|
|
|
ctx->pointer = eoc;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char *eoc,
|
|
|
|
long *integer)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char ch;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*integer = (signed char) ch;
|
|
|
|
len = 1;
|
|
|
|
|
|
|
|
while (ctx->pointer < eoc) {
|
|
|
|
if (++len > sizeof (long)) {
|
|
|
|
ctx->error = ASN1_ERR_DEC_BADVALUE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*integer <<= 8;
|
|
|
|
*integer |= ch;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char *eoc,
|
|
|
|
unsigned int *integer)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char ch;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*integer = ch;
|
|
|
|
if (ch == 0) len = 0;
|
|
|
|
else len = 1;
|
|
|
|
|
|
|
|
while (ctx->pointer < eoc) {
|
|
|
|
if (++len > sizeof (unsigned int)) {
|
|
|
|
ctx->error = ASN1_ERR_DEC_BADVALUE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*integer <<= 8;
|
|
|
|
*integer |= ch;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char *eoc,
|
|
|
|
unsigned long *integer)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char ch;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*integer = ch;
|
|
|
|
if (ch == 0) len = 0;
|
|
|
|
else len = 1;
|
|
|
|
|
|
|
|
while (ctx->pointer < eoc) {
|
|
|
|
if (++len > sizeof (unsigned long)) {
|
|
|
|
ctx->error = ASN1_ERR_DEC_BADVALUE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*integer <<= 8;
|
|
|
|
*integer |= ch;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char *eoc,
|
|
|
|
unsigned char **octets,
|
|
|
|
unsigned int *len)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char *ptr;
|
|
|
|
|
|
|
|
*len = 0;
|
|
|
|
|
|
|
|
*octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
|
2011-08-29 21:17:25 +00:00
|
|
|
if (*octets == NULL)
|
2006-12-03 06:10:34 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
ptr = *octets;
|
|
|
|
while (ctx->pointer < eoc) {
|
2012-06-03 17:41:40 +00:00
|
|
|
if (!asn1_octet_decode(ctx, ptr++)) {
|
2006-12-03 06:10:34 +00:00
|
|
|
kfree(*octets);
|
|
|
|
*octets = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
(*len)++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned long *subid)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char ch;
|
|
|
|
|
|
|
|
*subid = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!asn1_octet_decode(ctx, &ch))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*subid <<= 7;
|
|
|
|
*subid |= ch & 0x7F;
|
|
|
|
} while ((ch & 0x80) == 0x80);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char *eoc,
|
|
|
|
unsigned long **oid,
|
|
|
|
unsigned int *len)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned long subid;
|
|
|
|
unsigned long *optr;
|
2008-07-09 22:06:45 +00:00
|
|
|
size_t size;
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
size = eoc - ctx->pointer + 1;
|
2008-06-04 16:16:33 +00:00
|
|
|
|
|
|
|
/* first subid actually encodes first two subids */
|
|
|
|
if (size < 2 || size > ULONG_MAX/sizeof(unsigned long))
|
|
|
|
return 0;
|
|
|
|
|
2006-12-03 06:10:34 +00:00
|
|
|
*oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
|
2011-08-29 21:17:25 +00:00
|
|
|
if (*oid == NULL)
|
2006-12-03 06:10:34 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
optr = *oid;
|
|
|
|
|
|
|
|
if (!asn1_subid_decode(ctx, &subid)) {
|
|
|
|
kfree(*oid);
|
|
|
|
*oid = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subid < 40) {
|
2013-12-23 06:37:27 +00:00
|
|
|
optr[0] = 0;
|
|
|
|
optr[1] = subid;
|
2006-12-03 06:10:34 +00:00
|
|
|
} else if (subid < 80) {
|
2013-12-23 06:37:27 +00:00
|
|
|
optr[0] = 1;
|
|
|
|
optr[1] = subid - 40;
|
2006-12-03 06:10:34 +00:00
|
|
|
} else {
|
2013-12-23 06:37:27 +00:00
|
|
|
optr[0] = 2;
|
|
|
|
optr[1] = subid - 80;
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*len = 2;
|
|
|
|
optr += 2;
|
|
|
|
|
|
|
|
while (ctx->pointer < eoc) {
|
|
|
|
if (++(*len) > size) {
|
|
|
|
ctx->error = ASN1_ERR_DEC_BADVALUE;
|
|
|
|
kfree(*oid);
|
|
|
|
*oid = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!asn1_subid_decode(ctx, optr++)) {
|
|
|
|
kfree(*oid);
|
|
|
|
*oid = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* SNMP decoding routines (gxsnmp author Dirk Wisse)
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/* SNMP Versions */
|
|
|
|
#define SNMP_V1 0
|
|
|
|
#define SNMP_V2C 1
|
|
|
|
#define SNMP_V2 2
|
|
|
|
#define SNMP_V3 3
|
|
|
|
|
|
|
|
/* Default Sizes */
|
|
|
|
#define SNMP_SIZE_COMM 256
|
|
|
|
#define SNMP_SIZE_OBJECTID 128
|
|
|
|
#define SNMP_SIZE_BUFCHR 256
|
|
|
|
#define SNMP_SIZE_BUFINT 128
|
|
|
|
#define SNMP_SIZE_SMALLOBJECTID 16
|
|
|
|
|
|
|
|
/* Requests */
|
|
|
|
#define SNMP_PDU_GET 0
|
|
|
|
#define SNMP_PDU_NEXT 1
|
|
|
|
#define SNMP_PDU_RESPONSE 2
|
|
|
|
#define SNMP_PDU_SET 3
|
|
|
|
#define SNMP_PDU_TRAP1 4
|
|
|
|
#define SNMP_PDU_BULK 5
|
|
|
|
#define SNMP_PDU_INFORM 6
|
|
|
|
#define SNMP_PDU_TRAP2 7
|
|
|
|
|
|
|
|
/* Errors */
|
|
|
|
#define SNMP_NOERROR 0
|
|
|
|
#define SNMP_TOOBIG 1
|
|
|
|
#define SNMP_NOSUCHNAME 2
|
|
|
|
#define SNMP_BADVALUE 3
|
|
|
|
#define SNMP_READONLY 4
|
|
|
|
#define SNMP_GENERROR 5
|
|
|
|
#define SNMP_NOACCESS 6
|
|
|
|
#define SNMP_WRONGTYPE 7
|
|
|
|
#define SNMP_WRONGLENGTH 8
|
|
|
|
#define SNMP_WRONGENCODING 9
|
|
|
|
#define SNMP_WRONGVALUE 10
|
|
|
|
#define SNMP_NOCREATION 11
|
|
|
|
#define SNMP_INCONSISTENTVALUE 12
|
|
|
|
#define SNMP_RESOURCEUNAVAILABLE 13
|
|
|
|
#define SNMP_COMMITFAILED 14
|
|
|
|
#define SNMP_UNDOFAILED 15
|
|
|
|
#define SNMP_AUTHORIZATIONERROR 16
|
|
|
|
#define SNMP_NOTWRITABLE 17
|
|
|
|
#define SNMP_INCONSISTENTNAME 18
|
|
|
|
|
|
|
|
/* General SNMP V1 Traps */
|
|
|
|
#define SNMP_TRAP_COLDSTART 0
|
|
|
|
#define SNMP_TRAP_WARMSTART 1
|
|
|
|
#define SNMP_TRAP_LINKDOWN 2
|
|
|
|
#define SNMP_TRAP_LINKUP 3
|
|
|
|
#define SNMP_TRAP_AUTFAILURE 4
|
|
|
|
#define SNMP_TRAP_EQPNEIGHBORLOSS 5
|
|
|
|
#define SNMP_TRAP_ENTSPECIFIC 6
|
|
|
|
|
|
|
|
/* SNMPv1 Types */
|
|
|
|
#define SNMP_NULL 0
|
|
|
|
#define SNMP_INTEGER 1 /* l */
|
|
|
|
#define SNMP_OCTETSTR 2 /* c */
|
|
|
|
#define SNMP_DISPLAYSTR 2 /* c */
|
|
|
|
#define SNMP_OBJECTID 3 /* ul */
|
|
|
|
#define SNMP_IPADDR 4 /* uc */
|
|
|
|
#define SNMP_COUNTER 5 /* ul */
|
|
|
|
#define SNMP_GAUGE 6 /* ul */
|
|
|
|
#define SNMP_TIMETICKS 7 /* ul */
|
|
|
|
#define SNMP_OPAQUE 8 /* c */
|
|
|
|
|
|
|
|
/* Additional SNMPv2 Types */
|
|
|
|
#define SNMP_UINTEGER 5 /* ul */
|
|
|
|
#define SNMP_BITSTR 9 /* uc */
|
|
|
|
#define SNMP_NSAP 10 /* uc */
|
|
|
|
#define SNMP_COUNTER64 11 /* ul */
|
|
|
|
#define SNMP_NOSUCHOBJECT 12
|
|
|
|
#define SNMP_NOSUCHINSTANCE 13
|
|
|
|
#define SNMP_ENDOFMIBVIEW 14
|
|
|
|
|
|
|
|
union snmp_syntax
|
|
|
|
{
|
|
|
|
unsigned char uc[0]; /* 8 bit unsigned */
|
|
|
|
char c[0]; /* 8 bit signed */
|
|
|
|
unsigned long ul[0]; /* 32 bit unsigned */
|
|
|
|
long l[0]; /* 32 bit signed */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct snmp_object
|
|
|
|
{
|
|
|
|
unsigned long *id;
|
|
|
|
unsigned int id_len;
|
|
|
|
unsigned short type;
|
|
|
|
unsigned int syntax_len;
|
|
|
|
union snmp_syntax syntax;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct snmp_request
|
|
|
|
{
|
|
|
|
unsigned long id;
|
|
|
|
unsigned int error_status;
|
|
|
|
unsigned int error_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct snmp_v1_trap
|
|
|
|
{
|
|
|
|
unsigned long *id;
|
|
|
|
unsigned int id_len;
|
|
|
|
unsigned long ip_address; /* pointer */
|
|
|
|
unsigned int general;
|
|
|
|
unsigned int specific;
|
|
|
|
unsigned long time;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* SNMP types */
|
|
|
|
#define SNMP_IPA 0
|
|
|
|
#define SNMP_CNT 1
|
|
|
|
#define SNMP_GGE 2
|
|
|
|
#define SNMP_TIT 3
|
|
|
|
#define SNMP_OPQ 4
|
|
|
|
#define SNMP_C64 6
|
|
|
|
|
|
|
|
/* SNMP errors */
|
|
|
|
#define SERR_NSO 0
|
|
|
|
#define SERR_NSI 1
|
|
|
|
#define SERR_EOM 2
|
|
|
|
|
|
|
|
static inline void mangle_address(unsigned char *begin,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char *addr,
|
|
|
|
const struct oct1_map *map,
|
|
|
|
__sum16 *check);
|
2006-12-03 06:10:34 +00:00
|
|
|
struct snmp_cnv
|
|
|
|
{
|
|
|
|
unsigned int class;
|
|
|
|
unsigned int tag;
|
|
|
|
int syntax;
|
|
|
|
};
|
|
|
|
|
2008-04-14 09:15:42 +00:00
|
|
|
static const struct snmp_cnv snmp_conv[] = {
|
2006-12-03 06:10:34 +00:00
|
|
|
{ASN1_UNI, ASN1_NUL, SNMP_NULL},
|
|
|
|
{ASN1_UNI, ASN1_INT, SNMP_INTEGER},
|
|
|
|
{ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR},
|
|
|
|
{ASN1_UNI, ASN1_OTS, SNMP_DISPLAYSTR},
|
|
|
|
{ASN1_UNI, ASN1_OJI, SNMP_OBJECTID},
|
|
|
|
{ASN1_APL, SNMP_IPA, SNMP_IPADDR},
|
|
|
|
{ASN1_APL, SNMP_CNT, SNMP_COUNTER}, /* Counter32 */
|
|
|
|
{ASN1_APL, SNMP_GGE, SNMP_GAUGE}, /* Gauge32 == Unsigned32 */
|
|
|
|
{ASN1_APL, SNMP_TIT, SNMP_TIMETICKS},
|
|
|
|
{ASN1_APL, SNMP_OPQ, SNMP_OPAQUE},
|
|
|
|
|
|
|
|
/* SNMPv2 data types and errors */
|
|
|
|
{ASN1_UNI, ASN1_BTS, SNMP_BITSTR},
|
|
|
|
{ASN1_APL, SNMP_C64, SNMP_COUNTER64},
|
|
|
|
{ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT},
|
|
|
|
{ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE},
|
|
|
|
{ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW},
|
|
|
|
{0, 0, -1}
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned char snmp_tag_cls2syntax(unsigned int tag,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned int cls,
|
|
|
|
unsigned short *syntax)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
2008-04-14 09:15:42 +00:00
|
|
|
const struct snmp_cnv *cnv;
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
cnv = snmp_conv;
|
|
|
|
|
|
|
|
while (cnv->syntax != -1) {
|
|
|
|
if (cnv->tag == tag && cnv->class == cls) {
|
|
|
|
*syntax = cnv->syntax;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
cnv++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char snmp_object_decode(struct asn1_ctx *ctx,
|
2007-02-09 14:24:47 +00:00
|
|
|
struct snmp_object **obj)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned int cls, con, tag, len, idlen;
|
|
|
|
unsigned short type;
|
|
|
|
unsigned char *eoc, *end, *p;
|
|
|
|
unsigned long *lp, *id;
|
|
|
|
unsigned long ul;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
*obj = NULL;
|
|
|
|
id = NULL;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &eoc, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_oid_decode(ctx, end, &id, &idlen))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag)) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con != ASN1_PRI) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = 0;
|
|
|
|
if (!snmp_tag_cls2syntax(tag, cls, &type)) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
switch (type) {
|
2011-07-01 09:43:06 +00:00
|
|
|
case SNMP_INTEGER:
|
|
|
|
len = sizeof(long);
|
|
|
|
if (!asn1_long_decode(ctx, end, &l)) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
|
|
|
|
if (*obj == NULL) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
(*obj)->syntax.l[0] = l;
|
|
|
|
break;
|
|
|
|
case SNMP_OCTETSTR:
|
|
|
|
case SNMP_OPAQUE:
|
|
|
|
if (!asn1_octets_decode(ctx, end, &p, &len)) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
|
|
|
|
if (*obj == NULL) {
|
2006-12-03 06:10:34 +00:00
|
|
|
kfree(p);
|
2011-07-01 09:43:06 +00:00
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy((*obj)->syntax.c, p, len);
|
|
|
|
kfree(p);
|
|
|
|
break;
|
|
|
|
case SNMP_NULL:
|
|
|
|
case SNMP_NOSUCHOBJECT:
|
|
|
|
case SNMP_NOSUCHINSTANCE:
|
|
|
|
case SNMP_ENDOFMIBVIEW:
|
|
|
|
len = 0;
|
|
|
|
*obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
|
|
|
|
if (*obj == NULL) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!asn1_null_decode(ctx, end)) {
|
|
|
|
kfree(id);
|
|
|
|
kfree(*obj);
|
|
|
|
*obj = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SNMP_OBJECTID:
|
2012-06-03 17:41:40 +00:00
|
|
|
if (!asn1_oid_decode(ctx, end, &lp, &len)) {
|
2011-07-01 09:43:06 +00:00
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len *= sizeof(unsigned long);
|
|
|
|
*obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
|
|
|
|
if (*obj == NULL) {
|
2006-12-03 06:10:34 +00:00
|
|
|
kfree(lp);
|
2011-07-01 09:43:06 +00:00
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy((*obj)->syntax.ul, lp, len);
|
|
|
|
kfree(lp);
|
|
|
|
break;
|
|
|
|
case SNMP_IPADDR:
|
|
|
|
if (!asn1_octets_decode(ctx, end, &p, &len)) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (len != 4) {
|
2006-12-03 06:10:34 +00:00
|
|
|
kfree(p);
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
2011-07-01 09:43:06 +00:00
|
|
|
}
|
|
|
|
*obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
|
|
|
|
if (*obj == NULL) {
|
|
|
|
kfree(p);
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy((*obj)->syntax.uc, p, len);
|
|
|
|
kfree(p);
|
|
|
|
break;
|
|
|
|
case SNMP_COUNTER:
|
|
|
|
case SNMP_GAUGE:
|
|
|
|
case SNMP_TIMETICKS:
|
|
|
|
len = sizeof(unsigned long);
|
|
|
|
if (!asn1_ulong_decode(ctx, end, &ul)) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
|
|
|
|
if (*obj == NULL) {
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
(*obj)->syntax.ul[0] = ul;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
kfree(id);
|
|
|
|
return 0;
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(*obj)->syntax_len = len;
|
|
|
|
(*obj)->type = type;
|
|
|
|
(*obj)->id = id;
|
|
|
|
(*obj)->id_len = idlen;
|
|
|
|
|
|
|
|
if (!asn1_eoc_decode(ctx, eoc)) {
|
|
|
|
kfree(id);
|
|
|
|
kfree(*obj);
|
|
|
|
*obj = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-27 14:39:43 +00:00
|
|
|
static unsigned char noinline_for_stack
|
|
|
|
snmp_request_decode(struct asn1_ctx *ctx, struct snmp_request *request)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned int cls, con, tag;
|
|
|
|
unsigned char *end;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_ulong_decode(ctx, end, &request->id))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_uint_decode(ctx, end, &request->error_status))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_uint_decode(ctx, end, &request->error_index))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fast checksum update for possibly oddly-aligned UDP byte, from the
|
|
|
|
* code example in the draft.
|
|
|
|
*/
|
|
|
|
static void fast_csum(__sum16 *csum,
|
2007-02-09 14:24:47 +00:00
|
|
|
const unsigned char *optr,
|
|
|
|
const unsigned char *nptr,
|
|
|
|
int offset)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char s[4];
|
|
|
|
|
|
|
|
if (offset & 1) {
|
2010-09-21 21:17:33 +00:00
|
|
|
s[0] = ~0;
|
2006-12-03 06:10:34 +00:00
|
|
|
s[1] = ~*optr;
|
2010-09-21 21:17:33 +00:00
|
|
|
s[2] = 0;
|
2006-12-03 06:10:34 +00:00
|
|
|
s[3] = *nptr;
|
|
|
|
} else {
|
|
|
|
s[0] = ~*optr;
|
2010-09-21 21:17:33 +00:00
|
|
|
s[1] = ~0;
|
2006-12-03 06:10:34 +00:00
|
|
|
s[2] = *nptr;
|
2010-09-21 21:17:33 +00:00
|
|
|
s[3] = 0;
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*csum = csum_fold(csum_partial(s, 4, ~csum_unfold(*csum)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mangle IP address.
|
|
|
|
* - begin points to the start of the snmp messgae
|
|
|
|
* - addr points to the start of the address
|
|
|
|
*/
|
|
|
|
static inline void mangle_address(unsigned char *begin,
|
2007-02-09 14:24:47 +00:00
|
|
|
unsigned char *addr,
|
|
|
|
const struct oct1_map *map,
|
|
|
|
__sum16 *check)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
if (map->from == NOCT1(addr)) {
|
|
|
|
u_int32_t old;
|
|
|
|
|
|
|
|
if (debug)
|
2008-04-14 09:15:42 +00:00
|
|
|
memcpy(&old, addr, sizeof(old));
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
*addr = map->to;
|
|
|
|
|
|
|
|
/* Update UDP checksum if being used */
|
|
|
|
if (*check) {
|
|
|
|
fast_csum(check,
|
2007-02-09 14:24:47 +00:00
|
|
|
&map->from, &map->to, addr - begin);
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug)
|
2008-10-31 07:53:08 +00:00
|
|
|
printk(KERN_DEBUG "bsalg: mapped %pI4 to %pI4\n",
|
|
|
|
&old, addr);
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 14:39:43 +00:00
|
|
|
static unsigned char noinline_for_stack
|
|
|
|
snmp_trap_decode(struct asn1_ctx *ctx, struct snmp_v1_trap *trap,
|
|
|
|
const struct oct1_map *map,
|
|
|
|
__sum16 *check)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned int cls, con, tag, len;
|
|
|
|
unsigned char *end;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
goto err_id_free;
|
|
|
|
|
|
|
|
if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
|
|
|
|
(cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS)))
|
|
|
|
goto err_id_free;
|
|
|
|
|
|
|
|
if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
|
|
|
|
goto err_id_free;
|
|
|
|
|
|
|
|
/* IPv4 only */
|
|
|
|
if (len != 4)
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
mangle_address(ctx->begin, ctx->pointer - 4, map, check);
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
if (!asn1_uint_decode(ctx, end, &trap->general))
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
if (!asn1_uint_decode(ctx, end, &trap->specific))
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
|
|
|
|
(cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT)))
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
if (!asn1_ulong_decode(ctx, end, &trap->time))
|
|
|
|
goto err_addr_free;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err_addr_free:
|
|
|
|
kfree((unsigned long *)trap->ip_address);
|
|
|
|
|
|
|
|
err_id_free:
|
|
|
|
kfree(trap->id);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Misc. routines
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse and mangle SNMP message according to mapping.
|
|
|
|
* (And this is the fucking 'basic' method).
|
|
|
|
*/
|
|
|
|
static int snmp_parse_mangle(unsigned char *msg,
|
2007-02-09 14:24:47 +00:00
|
|
|
u_int16_t len,
|
|
|
|
const struct oct1_map *map,
|
|
|
|
__sum16 *check)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
|
|
|
unsigned char *eoc, *end;
|
|
|
|
unsigned int cls, con, tag, vers, pdutype;
|
|
|
|
struct asn1_ctx ctx;
|
|
|
|
struct asn1_octstr comm;
|
2010-01-04 14:21:31 +00:00
|
|
|
struct snmp_object *obj;
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
if (debug > 1)
|
2017-02-28 22:09:24 +00:00
|
|
|
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 16, 1,
|
|
|
|
msg, len, 0);
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
asn1_open(&ctx, msg, len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start of SNMP message.
|
|
|
|
*/
|
|
|
|
if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version 1 or 2 handled.
|
|
|
|
*/
|
|
|
|
if (!asn1_header_decode(&ctx, &end, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
|
|
|
|
return 0;
|
|
|
|
if (!asn1_uint_decode (&ctx, end, &vers))
|
|
|
|
return 0;
|
|
|
|
if (debug > 1)
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_debug("bsalg: snmp version: %u\n", vers + 1);
|
2006-12-03 06:10:34 +00:00
|
|
|
if (vers > 1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Community.
|
|
|
|
*/
|
|
|
|
if (!asn1_header_decode (&ctx, &end, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OTS)
|
|
|
|
return 0;
|
|
|
|
if (!asn1_octets_decode(&ctx, end, &comm.data, &comm.len))
|
|
|
|
return 0;
|
|
|
|
if (debug > 1) {
|
|
|
|
unsigned int i;
|
|
|
|
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_debug("bsalg: community: ");
|
2006-12-03 06:10:34 +00:00
|
|
|
for (i = 0; i < comm.len; i++)
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_cont("%c", comm.data[i]);
|
|
|
|
pr_cont("\n");
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
kfree(comm.data);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PDU type
|
|
|
|
*/
|
|
|
|
if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &pdutype))
|
|
|
|
return 0;
|
|
|
|
if (cls != ASN1_CTX || con != ASN1_CON)
|
|
|
|
return 0;
|
|
|
|
if (debug > 1) {
|
2008-04-14 09:15:42 +00:00
|
|
|
static const unsigned char *const pdus[] = {
|
2006-12-03 06:10:34 +00:00
|
|
|
[SNMP_PDU_GET] = "get",
|
|
|
|
[SNMP_PDU_NEXT] = "get-next",
|
|
|
|
[SNMP_PDU_RESPONSE] = "response",
|
|
|
|
[SNMP_PDU_SET] = "set",
|
|
|
|
[SNMP_PDU_TRAP1] = "trapv1",
|
|
|
|
[SNMP_PDU_BULK] = "bulk",
|
|
|
|
[SNMP_PDU_INFORM] = "inform",
|
|
|
|
[SNMP_PDU_TRAP2] = "trapv2"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (pdutype > SNMP_PDU_TRAP2)
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_debug("bsalg: bad pdu type %u\n", pdutype);
|
2006-12-03 06:10:34 +00:00
|
|
|
else
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_debug("bsalg: pdu: %s\n", pdus[pdutype]);
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
if (pdutype != SNMP_PDU_RESPONSE &&
|
|
|
|
pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request header or v1 trap
|
|
|
|
*/
|
|
|
|
if (pdutype == SNMP_PDU_TRAP1) {
|
|
|
|
struct snmp_v1_trap trap;
|
|
|
|
unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
kfree(trap.id);
|
|
|
|
kfree((unsigned long *)trap.ip_address);
|
|
|
|
} else
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
struct snmp_request req;
|
|
|
|
|
|
|
|
if (!snmp_request_decode(&ctx, &req))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (debug > 1)
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_debug("bsalg: request: id=0x%lx error_status=%u "
|
2006-12-03 06:10:34 +00:00
|
|
|
"error_index=%u\n", req.id, req.error_status,
|
|
|
|
req.error_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through objects, look for IP addresses to mangle.
|
|
|
|
*/
|
|
|
|
if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (!asn1_eoc_decode(&ctx, eoc)) {
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-01-04 14:21:31 +00:00
|
|
|
if (!snmp_object_decode(&ctx, &obj)) {
|
|
|
|
if (obj) {
|
|
|
|
kfree(obj->id);
|
|
|
|
kfree(obj);
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug > 1) {
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_debug("bsalg: object: ");
|
2010-01-04 14:21:31 +00:00
|
|
|
for (i = 0; i < obj->id_len; i++) {
|
2006-12-03 06:10:34 +00:00
|
|
|
if (i > 0)
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_cont(".");
|
|
|
|
pr_cont("%lu", obj->id[i]);
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
2015-11-13 10:40:34 +00:00
|
|
|
pr_cont(": type=%u\n", obj->type);
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-01-04 14:21:31 +00:00
|
|
|
if (obj->type == SNMP_IPADDR)
|
2015-10-14 22:17:08 +00:00
|
|
|
mangle_address(ctx.begin, ctx.pointer - 4, map, check);
|
2006-12-03 06:10:34 +00:00
|
|
|
|
2010-01-04 14:21:31 +00:00
|
|
|
kfree(obj->id);
|
|
|
|
kfree(obj);
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!asn1_eoc_decode(&ctx, eoc))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* NAT routines.
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SNMP translation routine.
|
|
|
|
*/
|
|
|
|
static int snmp_translate(struct nf_conn *ct,
|
2007-02-09 14:24:47 +00:00
|
|
|
enum ip_conntrack_info ctinfo,
|
2007-10-15 07:53:15 +00:00
|
|
|
struct sk_buff *skb)
|
2006-12-03 06:10:34 +00:00
|
|
|
{
|
2007-10-15 07:53:15 +00:00
|
|
|
struct iphdr *iph = ip_hdr(skb);
|
2006-12-03 06:10:34 +00:00
|
|
|
struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
|
|
|
|
u_int16_t udplen = ntohs(udph->len);
|
|
|
|
u_int16_t paylen = udplen - sizeof(struct udphdr);
|
|
|
|
int dir = CTINFO2DIR(ctinfo);
|
|
|
|
struct oct1_map map;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine mappping for application layer addresses based
|
|
|
|
* on NAT manipulations for the packet.
|
|
|
|
*/
|
|
|
|
if (dir == IP_CT_DIR_ORIGINAL) {
|
|
|
|
/* SNAT traps */
|
|
|
|
map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
|
|
|
|
map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
|
|
|
|
} else {
|
|
|
|
/* DNAT replies */
|
2014-02-11 14:49:25 +00:00
|
|
|
map.from = NOCT1(&ct->tuplehash[!dir].tuple.src.u3.ip);
|
|
|
|
map.to = NOCT1(&ct->tuplehash[dir].tuple.dst.u3.ip);
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (map.from == map.to)
|
|
|
|
return NF_ACCEPT;
|
|
|
|
|
|
|
|
if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
|
2007-02-09 14:24:47 +00:00
|
|
|
paylen, &map, &udph->check)) {
|
2012-05-13 21:56:26 +00:00
|
|
|
net_warn_ratelimited("bsalg: parser failed\n");
|
2006-12-03 06:10:34 +00:00
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't actually set up expectations, just adjust internal IP
|
|
|
|
* addresses if this is being NATted */
|
2007-10-15 07:53:15 +00:00
|
|
|
static int help(struct sk_buff *skb, unsigned int protoff,
|
2006-12-03 06:10:34 +00:00
|
|
|
struct nf_conn *ct,
|
|
|
|
enum ip_conntrack_info ctinfo)
|
|
|
|
{
|
|
|
|
int dir = CTINFO2DIR(ctinfo);
|
|
|
|
unsigned int ret;
|
2008-04-14 09:15:42 +00:00
|
|
|
const struct iphdr *iph = ip_hdr(skb);
|
|
|
|
const struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
|
2006-12-03 06:10:34 +00:00
|
|
|
|
|
|
|
/* SNMP replies and originating SNMP traps get mangled */
|
|
|
|
if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
|
|
|
|
return NF_ACCEPT;
|
|
|
|
if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
|
|
|
|
return NF_ACCEPT;
|
|
|
|
|
|
|
|
/* No NAT? */
|
|
|
|
if (!(ct->status & IPS_NAT_MASK))
|
|
|
|
return NF_ACCEPT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the packet length is ok. So far, we were only guaranteed
|
|
|
|
* to have a valid length IP header plus 8 bytes, which means we have
|
|
|
|
* enough room for a UDP header. Just verify the UDP length field so we
|
|
|
|
* can mess around with the payload.
|
|
|
|
*/
|
2007-10-15 07:53:15 +00:00
|
|
|
if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
|
2012-05-13 21:56:26 +00:00
|
|
|
net_warn_ratelimited("SNMP: dropping malformed packet src=%pI4 dst=%pI4\n",
|
|
|
|
&iph->saddr, &iph->daddr);
|
2006-12-03 06:10:34 +00:00
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
|
2007-10-15 07:53:15 +00:00
|
|
|
if (!skb_make_writable(skb, skb->len))
|
2006-12-03 06:10:34 +00:00
|
|
|
return NF_DROP;
|
|
|
|
|
|
|
|
spin_lock_bh(&snmp_lock);
|
2007-10-15 07:53:15 +00:00
|
|
|
ret = snmp_translate(ct, ctinfo, skb);
|
2006-12-03 06:10:34 +00:00
|
|
|
spin_unlock_bh(&snmp_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-26 03:09:15 +00:00
|
|
|
static const struct nf_conntrack_expect_policy snmp_exp_policy = {
|
|
|
|
.max_expected = 0,
|
|
|
|
.timeout = 180,
|
|
|
|
};
|
|
|
|
|
2006-12-03 06:10:34 +00:00
|
|
|
static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
.help = help,
|
2008-03-26 03:09:15 +00:00
|
|
|
.expect_policy = &snmp_exp_policy,
|
2006-12-03 06:10:34 +00:00
|
|
|
.name = "snmp_trap",
|
|
|
|
.tuple.src.l3num = AF_INET,
|
2009-02-01 08:45:17 +00:00
|
|
|
.tuple.src.u.udp.port = cpu_to_be16(SNMP_TRAP_PORT),
|
2006-12-03 06:10:34 +00:00
|
|
|
.tuple.dst.protonum = IPPROTO_UDP,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Module stuff.
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
static int __init nf_nat_snmp_basic_init(void)
|
|
|
|
{
|
2011-01-18 17:12:24 +00:00
|
|
|
BUG_ON(nf_nat_snmp_hook != NULL);
|
2011-08-01 16:19:00 +00:00
|
|
|
RCU_INIT_POINTER(nf_nat_snmp_hook, help);
|
2011-01-18 17:12:24 +00:00
|
|
|
|
2017-03-25 10:24:36 +00:00
|
|
|
return nf_conntrack_helper_register(&snmp_trap_helper);
|
2006-12-03 06:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit nf_nat_snmp_basic_fini(void)
|
|
|
|
{
|
2011-08-01 16:19:00 +00:00
|
|
|
RCU_INIT_POINTER(nf_nat_snmp_hook, NULL);
|
netfilter: invoke synchronize_rcu after set the _hook_ to NULL
Otherwise, another CPU may access the invalid pointer. For example:
CPU0 CPU1
- rcu_read_lock();
- pfunc = _hook_;
_hook_ = NULL; -
mod unload -
- pfunc(); // invalid, panic
- rcu_read_unlock();
So we must call synchronize_rcu() to wait the rcu reader to finish.
Also note, in nf_nat_snmp_basic_fini, synchronize_rcu() will be invoked
by later nf_conntrack_helper_unregister, but I'm inclined to add a
explicit synchronize_rcu after set the nf_nat_snmp_hook to NULL. Depend
on such obscure assumptions is not a good idea.
Last, in nfnetlink_cttimeout, we use kfree_rcu to free the time object,
so in cttimeout_exit, invoking rcu_barrier() is not necessary at all,
remove it too.
Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2017-03-25 00:53:12 +00:00
|
|
|
synchronize_rcu();
|
2006-12-03 06:10:34 +00:00
|
|
|
nf_conntrack_helper_unregister(&snmp_trap_helper);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(nf_nat_snmp_basic_init);
|
|
|
|
module_exit(nf_nat_snmp_basic_fini);
|
|
|
|
|
|
|
|
module_param(debug, int, 0600);
|