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, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 * Author: James Morris <jmorris@intercode.com.au>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
48 #include <linux/udp.h>
49 #include <net/checksum.h>
52 #include <net/netfilter/nf_nat.h>
53 #include <net/netfilter/nf_conntrack_expect.h>
54 #include <net/netfilter/nf_conntrack_helper.h>
55 #include <net/netfilter/nf_nat_helper.h>
57 MODULE_LICENSE("GPL");
58 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
59 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
60 MODULE_ALIAS("ip_nat_snmp_basic");
63 #define SNMP_TRAP_PORT 162
64 #define NOCT1(n) (*(u8 *)(n))
67 static DEFINE_SPINLOCK(snmp_lock);
70 * Application layer address mapping mimics the NAT mapping, but
71 * only for the first octet in this case (a more flexible system
72 * can be implemented if needed).
81 /*****************************************************************************
83 * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
85 *****************************************************************************/
88 #define ASN1_UNI 0 /* Universal */
89 #define ASN1_APL 1 /* Application */
90 #define ASN1_CTX 2 /* Context */
91 #define ASN1_PRV 3 /* Private */
94 #define ASN1_EOC 0 /* End Of Contents */
95 #define ASN1_BOL 1 /* Boolean */
96 #define ASN1_INT 2 /* Integer */
97 #define ASN1_BTS 3 /* Bit String */
98 #define ASN1_OTS 4 /* Octet String */
99 #define ASN1_NUL 5 /* Null */
100 #define ASN1_OJI 6 /* Object Identifier */
101 #define ASN1_OJD 7 /* Object Description */
102 #define ASN1_EXT 8 /* External */
103 #define ASN1_SEQ 16 /* Sequence */
104 #define ASN1_SET 17 /* Set */
105 #define ASN1_NUMSTR 18 /* Numerical String */
106 #define ASN1_PRNSTR 19 /* Printable String */
107 #define ASN1_TEXSTR 20 /* Teletext String */
108 #define ASN1_VIDSTR 21 /* Video String */
109 #define ASN1_IA5STR 22 /* IA5 String */
110 #define ASN1_UNITIM 23 /* Universal Time */
111 #define ASN1_GENTIM 24 /* General Time */
112 #define ASN1_GRASTR 25 /* Graphical String */
113 #define ASN1_VISSTR 26 /* Visible String */
114 #define ASN1_GENSTR 27 /* General String */
116 /* Primitive / Constructed methods*/
117 #define ASN1_PRI 0 /* Primitive */
118 #define ASN1_CON 1 /* Constructed */
123 #define ASN1_ERR_NOERROR 0
124 #define ASN1_ERR_DEC_EMPTY 2
125 #define ASN1_ERR_DEC_EOC_MISMATCH 3
126 #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
127 #define ASN1_ERR_DEC_BADVALUE 5
134 int error; /* Error condition */
135 unsigned char *pointer; /* Octet just to be decoded */
136 unsigned char *begin; /* First octet */
137 unsigned char *end; /* Octet after last octet */
141 * Octet string (not null terminated)
149 static void asn1_open(struct asn1_ctx *ctx,
154 ctx->end = buf + len;
156 ctx->error = ASN1_ERR_NOERROR;
159 static unsigned char asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
161 if (ctx->pointer >= ctx->end) {
162 ctx->error = ASN1_ERR_DEC_EMPTY;
165 *ch = *(ctx->pointer)++;
169 static unsigned char asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
177 if (!asn1_octet_decode(ctx, &ch))
181 } while ((ch & 0x80) == 0x80);
185 static unsigned char asn1_id_decode(struct asn1_ctx *ctx,
192 if (!asn1_octet_decode(ctx, &ch))
195 *cls = (ch & 0xC0) >> 6;
196 *con = (ch & 0x20) >> 5;
200 if (!asn1_tag_decode(ctx, tag))
206 static unsigned char asn1_length_decode(struct asn1_ctx *ctx,
210 unsigned char ch, cnt;
212 if (!asn1_octet_decode(ctx, &ch))
227 if (!asn1_octet_decode(ctx, &ch))
236 /* don't trust len bigger than ctx buffer */
237 if (*len > ctx->end - ctx->pointer)
243 static unsigned char asn1_header_decode(struct asn1_ctx *ctx,
249 unsigned int def, len;
251 if (!asn1_id_decode(ctx, cls, con, tag))
255 if (!asn1_length_decode(ctx, &def, &len))
258 /* primitive shall be definite, indefinite shall be constructed */
259 if (*con == ASN1_PRI && !def)
263 *eoc = ctx->pointer + len;
269 static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
274 if (!asn1_octet_decode(ctx, &ch))
278 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
282 if (!asn1_octet_decode(ctx, &ch))
286 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
291 if (ctx->pointer != eoc) {
292 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
299 static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
305 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
312 if (!asn1_octet_decode(ctx, &ch))
315 *integer = (signed char) ch;
318 while (ctx->pointer < eoc) {
319 if (++len > sizeof (long)) {
320 ctx->error = ASN1_ERR_DEC_BADVALUE;
324 if (!asn1_octet_decode(ctx, &ch))
333 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
335 unsigned int *integer)
340 if (!asn1_octet_decode(ctx, &ch))
344 if (ch == 0) len = 0;
347 while (ctx->pointer < eoc) {
348 if (++len > sizeof (unsigned int)) {
349 ctx->error = ASN1_ERR_DEC_BADVALUE;
353 if (!asn1_octet_decode(ctx, &ch))
362 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
364 unsigned long *integer)
369 if (!asn1_octet_decode(ctx, &ch))
373 if (ch == 0) len = 0;
376 while (ctx->pointer < eoc) {
377 if (++len > sizeof (unsigned long)) {
378 ctx->error = ASN1_ERR_DEC_BADVALUE;
382 if (!asn1_octet_decode(ctx, &ch))
391 static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
393 unsigned char **octets,
400 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
401 if (*octets == NULL) {
403 printk("OOM in bsalg (%d)\n", __LINE__);
408 while (ctx->pointer < eoc) {
409 if (!asn1_octet_decode(ctx, (unsigned char *)ptr++)) {
419 static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
420 unsigned long *subid)
427 if (!asn1_octet_decode(ctx, &ch))
432 } while ((ch & 0x80) == 0x80);
436 static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
445 size = eoc - ctx->pointer + 1;
447 /* first subid actually encodes first two subids */
448 if (size < 2 || size > ULONG_MAX/sizeof(unsigned long))
451 *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
454 printk("OOM in bsalg (%d)\n", __LINE__);
460 if (!asn1_subid_decode(ctx, &subid)) {
469 } else if (subid < 80) {
471 optr [1] = subid - 40;
474 optr [1] = subid - 80;
480 while (ctx->pointer < eoc) {
481 if (++(*len) > size) {
482 ctx->error = ASN1_ERR_DEC_BADVALUE;
488 if (!asn1_subid_decode(ctx, optr++)) {
497 /*****************************************************************************
499 * SNMP decoding routines (gxsnmp author Dirk Wisse)
501 *****************************************************************************/
510 #define SNMP_SIZE_COMM 256
511 #define SNMP_SIZE_OBJECTID 128
512 #define SNMP_SIZE_BUFCHR 256
513 #define SNMP_SIZE_BUFINT 128
514 #define SNMP_SIZE_SMALLOBJECTID 16
517 #define SNMP_PDU_GET 0
518 #define SNMP_PDU_NEXT 1
519 #define SNMP_PDU_RESPONSE 2
520 #define SNMP_PDU_SET 3
521 #define SNMP_PDU_TRAP1 4
522 #define SNMP_PDU_BULK 5
523 #define SNMP_PDU_INFORM 6
524 #define SNMP_PDU_TRAP2 7
527 #define SNMP_NOERROR 0
528 #define SNMP_TOOBIG 1
529 #define SNMP_NOSUCHNAME 2
530 #define SNMP_BADVALUE 3
531 #define SNMP_READONLY 4
532 #define SNMP_GENERROR 5
533 #define SNMP_NOACCESS 6
534 #define SNMP_WRONGTYPE 7
535 #define SNMP_WRONGLENGTH 8
536 #define SNMP_WRONGENCODING 9
537 #define SNMP_WRONGVALUE 10
538 #define SNMP_NOCREATION 11
539 #define SNMP_INCONSISTENTVALUE 12
540 #define SNMP_RESOURCEUNAVAILABLE 13
541 #define SNMP_COMMITFAILED 14
542 #define SNMP_UNDOFAILED 15
543 #define SNMP_AUTHORIZATIONERROR 16
544 #define SNMP_NOTWRITABLE 17
545 #define SNMP_INCONSISTENTNAME 18
547 /* General SNMP V1 Traps */
548 #define SNMP_TRAP_COLDSTART 0
549 #define SNMP_TRAP_WARMSTART 1
550 #define SNMP_TRAP_LINKDOWN 2
551 #define SNMP_TRAP_LINKUP 3
552 #define SNMP_TRAP_AUTFAILURE 4
553 #define SNMP_TRAP_EQPNEIGHBORLOSS 5
554 #define SNMP_TRAP_ENTSPECIFIC 6
558 #define SNMP_INTEGER 1 /* l */
559 #define SNMP_OCTETSTR 2 /* c */
560 #define SNMP_DISPLAYSTR 2 /* c */
561 #define SNMP_OBJECTID 3 /* ul */
562 #define SNMP_IPADDR 4 /* uc */
563 #define SNMP_COUNTER 5 /* ul */
564 #define SNMP_GAUGE 6 /* ul */
565 #define SNMP_TIMETICKS 7 /* ul */
566 #define SNMP_OPAQUE 8 /* c */
568 /* Additional SNMPv2 Types */
569 #define SNMP_UINTEGER 5 /* ul */
570 #define SNMP_BITSTR 9 /* uc */
571 #define SNMP_NSAP 10 /* uc */
572 #define SNMP_COUNTER64 11 /* ul */
573 #define SNMP_NOSUCHOBJECT 12
574 #define SNMP_NOSUCHINSTANCE 13
575 #define SNMP_ENDOFMIBVIEW 14
579 unsigned char uc[0]; /* 8 bit unsigned */
580 char c[0]; /* 8 bit signed */
581 unsigned long ul[0]; /* 32 bit unsigned */
582 long l[0]; /* 32 bit signed */
590 unsigned int syntax_len;
591 union snmp_syntax syntax;
597 unsigned int error_status;
598 unsigned int error_index;
605 unsigned long ip_address; /* pointer */
606 unsigned int general;
607 unsigned int specific;
624 static inline void mangle_address(unsigned char *begin,
626 const struct oct1_map *map,
635 static const struct snmp_cnv snmp_conv[] = {
636 {ASN1_UNI, ASN1_NUL, SNMP_NULL},
637 {ASN1_UNI, ASN1_INT, SNMP_INTEGER},
638 {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR},
639 {ASN1_UNI, ASN1_OTS, SNMP_DISPLAYSTR},
640 {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID},
641 {ASN1_APL, SNMP_IPA, SNMP_IPADDR},
642 {ASN1_APL, SNMP_CNT, SNMP_COUNTER}, /* Counter32 */
643 {ASN1_APL, SNMP_GGE, SNMP_GAUGE}, /* Gauge32 == Unsigned32 */
644 {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS},
645 {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE},
647 /* SNMPv2 data types and errors */
648 {ASN1_UNI, ASN1_BTS, SNMP_BITSTR},
649 {ASN1_APL, SNMP_C64, SNMP_COUNTER64},
650 {ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT},
651 {ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE},
652 {ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW},
656 static unsigned char snmp_tag_cls2syntax(unsigned int tag,
658 unsigned short *syntax)
660 const struct snmp_cnv *cnv;
664 while (cnv->syntax != -1) {
665 if (cnv->tag == tag && cnv->class == cls) {
666 *syntax = cnv->syntax;
674 static unsigned char snmp_object_decode(struct asn1_ctx *ctx,
675 struct snmp_object **obj)
677 unsigned int cls, con, tag, len, idlen;
679 unsigned char *eoc, *end, *p;
680 unsigned long *lp, *id;
687 if (!asn1_header_decode(ctx, &eoc, &cls, &con, &tag))
690 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
693 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
696 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
699 if (!asn1_oid_decode(ctx, end, &id, &idlen))
702 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag)) {
707 if (con != ASN1_PRI) {
713 if (!snmp_tag_cls2syntax(tag, cls, &type)) {
722 if (!asn1_long_decode(ctx, end, &l)) {
726 *obj = kmalloc(sizeof(struct snmp_object) + len,
731 printk("OOM in bsalg (%d)\n", __LINE__);
734 (*obj)->syntax.l[0] = l;
738 if (!asn1_octets_decode(ctx, end, &p, &len)) {
742 *obj = kmalloc(sizeof(struct snmp_object) + len,
747 printk("OOM in bsalg (%d)\n", __LINE__);
750 memcpy((*obj)->syntax.c, p, len);
754 case SNMP_NOSUCHOBJECT:
755 case SNMP_NOSUCHINSTANCE:
756 case SNMP_ENDOFMIBVIEW:
758 *obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
762 printk("OOM in bsalg (%d)\n", __LINE__);
765 if (!asn1_null_decode(ctx, end)) {
773 if (!asn1_oid_decode(ctx, end, (unsigned long **)&lp, &len)) {
777 len *= sizeof(unsigned long);
778 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
783 printk("OOM in bsalg (%d)\n", __LINE__);
786 memcpy((*obj)->syntax.ul, lp, len);
790 if (!asn1_octets_decode(ctx, end, &p, &len)) {
799 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
804 printk("OOM in bsalg (%d)\n", __LINE__);
807 memcpy((*obj)->syntax.uc, p, len);
813 len = sizeof(unsigned long);
814 if (!asn1_ulong_decode(ctx, end, &ul)) {
818 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
822 printk("OOM in bsalg (%d)\n", __LINE__);
825 (*obj)->syntax.ul[0] = ul;
832 (*obj)->syntax_len = len;
835 (*obj)->id_len = idlen;
837 if (!asn1_eoc_decode(ctx, eoc)) {
846 static unsigned char snmp_request_decode(struct asn1_ctx *ctx,
847 struct snmp_request *request)
849 unsigned int cls, con, tag;
852 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
855 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
858 if (!asn1_ulong_decode(ctx, end, &request->id))
861 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
864 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
867 if (!asn1_uint_decode(ctx, end, &request->error_status))
870 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
873 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
876 if (!asn1_uint_decode(ctx, end, &request->error_index))
883 * Fast checksum update for possibly oddly-aligned UDP byte, from the
884 * code example in the draft.
886 static void fast_csum(__sum16 *csum,
887 const unsigned char *optr,
888 const unsigned char *nptr,
903 *csum = csum_fold(csum_partial(s, 4, ~csum_unfold(*csum)));
908 * - begin points to the start of the snmp messgae
909 * - addr points to the start of the address
911 static inline void mangle_address(unsigned char *begin,
913 const struct oct1_map *map,
916 if (map->from == NOCT1(addr)) {
920 memcpy(&old, addr, sizeof(old));
924 /* Update UDP checksum if being used */
927 &map->from, &map->to, addr - begin);
932 printk(KERN_DEBUG "bsalg: mapped %u.%u.%u.%u to "
933 "%u.%u.%u.%u\n", NIPQUAD(old), NIPQUAD(*addr));
937 static unsigned char snmp_trap_decode(struct asn1_ctx *ctx,
938 struct snmp_v1_trap *trap,
939 const struct oct1_map *map,
942 unsigned int cls, con, tag, len;
945 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
948 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
951 if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
954 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
957 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
958 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS)))
961 if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
968 mangle_address(ctx->begin, ctx->pointer - 4, map, check);
970 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
973 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
976 if (!asn1_uint_decode(ctx, end, &trap->general))
979 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
982 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
985 if (!asn1_uint_decode(ctx, end, &trap->specific))
988 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
991 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
992 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT)))
995 if (!asn1_ulong_decode(ctx, end, &trap->time))
1001 kfree((unsigned long *)trap->ip_address);
1009 /*****************************************************************************
1013 *****************************************************************************/
1015 static void hex_dump(const unsigned char *buf, size_t len)
1019 for (i = 0; i < len; i++) {
1022 printk("%02x ", *(buf + i));
1028 * Parse and mangle SNMP message according to mapping.
1029 * (And this is the fucking 'basic' method).
1031 static int snmp_parse_mangle(unsigned char *msg,
1033 const struct oct1_map *map,
1036 unsigned char *eoc, *end;
1037 unsigned int cls, con, tag, vers, pdutype;
1038 struct asn1_ctx ctx;
1039 struct asn1_octstr comm;
1040 struct snmp_object **obj;
1045 asn1_open(&ctx, msg, len);
1048 * Start of SNMP message.
1050 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1052 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1056 * Version 1 or 2 handled.
1058 if (!asn1_header_decode(&ctx, &end, &cls, &con, &tag))
1060 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
1062 if (!asn1_uint_decode (&ctx, end, &vers))
1065 printk(KERN_DEBUG "bsalg: snmp version: %u\n", vers + 1);
1072 if (!asn1_header_decode (&ctx, &end, &cls, &con, &tag))
1074 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OTS)
1076 if (!asn1_octets_decode(&ctx, end, &comm.data, &comm.len))
1081 printk(KERN_DEBUG "bsalg: community: ");
1082 for (i = 0; i < comm.len; i++)
1083 printk("%c", comm.data[i]);
1091 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &pdutype))
1093 if (cls != ASN1_CTX || con != ASN1_CON)
1096 static const unsigned char *const pdus[] = {
1097 [SNMP_PDU_GET] = "get",
1098 [SNMP_PDU_NEXT] = "get-next",
1099 [SNMP_PDU_RESPONSE] = "response",
1100 [SNMP_PDU_SET] = "set",
1101 [SNMP_PDU_TRAP1] = "trapv1",
1102 [SNMP_PDU_BULK] = "bulk",
1103 [SNMP_PDU_INFORM] = "inform",
1104 [SNMP_PDU_TRAP2] = "trapv2"
1107 if (pdutype > SNMP_PDU_TRAP2)
1108 printk(KERN_DEBUG "bsalg: bad pdu type %u\n", pdutype);
1110 printk(KERN_DEBUG "bsalg: pdu: %s\n", pdus[pdutype]);
1112 if (pdutype != SNMP_PDU_RESPONSE &&
1113 pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
1117 * Request header or v1 trap
1119 if (pdutype == SNMP_PDU_TRAP1) {
1120 struct snmp_v1_trap trap;
1121 unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
1125 kfree((unsigned long *)trap.ip_address);
1130 struct snmp_request req;
1132 if (!snmp_request_decode(&ctx, &req))
1136 printk(KERN_DEBUG "bsalg: request: id=0x%lx error_status=%u "
1137 "error_index=%u\n", req.id, req.error_status,
1142 * Loop through objects, look for IP addresses to mangle.
1144 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1147 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1150 obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
1152 if (net_ratelimit())
1153 printk(KERN_WARNING "OOM in bsalg(%d)\n", __LINE__);
1157 while (!asn1_eoc_decode(&ctx, eoc)) {
1160 if (!snmp_object_decode(&ctx, obj)) {
1170 printk(KERN_DEBUG "bsalg: object: ");
1171 for (i = 0; i < (*obj)->id_len; i++) {
1174 printk("%lu", (*obj)->id[i]);
1176 printk(": type=%u\n", (*obj)->type);
1180 if ((*obj)->type == SNMP_IPADDR)
1181 mangle_address(ctx.begin, ctx.pointer - 4 , map, check);
1188 if (!asn1_eoc_decode(&ctx, eoc))
1194 /*****************************************************************************
1198 *****************************************************************************/
1201 * SNMP translation routine.
1203 static int snmp_translate(struct nf_conn *ct,
1204 enum ip_conntrack_info ctinfo,
1205 struct sk_buff *skb)
1207 struct iphdr *iph = ip_hdr(skb);
1208 struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
1209 u_int16_t udplen = ntohs(udph->len);
1210 u_int16_t paylen = udplen - sizeof(struct udphdr);
1211 int dir = CTINFO2DIR(ctinfo);
1212 struct oct1_map map;
1215 * Determine mappping for application layer addresses based
1216 * on NAT manipulations for the packet.
1218 if (dir == IP_CT_DIR_ORIGINAL) {
1220 map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
1221 map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
1224 map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
1225 map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
1228 if (map.from == map.to)
1231 if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
1232 paylen, &map, &udph->check)) {
1233 if (net_ratelimit())
1234 printk(KERN_WARNING "bsalg: parser failed\n");
1240 /* We don't actually set up expectations, just adjust internal IP
1241 * addresses if this is being NATted */
1242 static int help(struct sk_buff *skb, unsigned int protoff,
1244 enum ip_conntrack_info ctinfo)
1246 int dir = CTINFO2DIR(ctinfo);
1248 const struct iphdr *iph = ip_hdr(skb);
1249 const struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
1251 /* SNMP replies and originating SNMP traps get mangled */
1252 if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
1254 if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
1258 if (!(ct->status & IPS_NAT_MASK))
1262 * Make sure the packet length is ok. So far, we were only guaranteed
1263 * to have a valid length IP header plus 8 bytes, which means we have
1264 * enough room for a UDP header. Just verify the UDP length field so we
1265 * can mess around with the payload.
1267 if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
1268 if (net_ratelimit())
1269 printk(KERN_WARNING "SNMP: dropping malformed packet "
1270 "src=%u.%u.%u.%u dst=%u.%u.%u.%u\n",
1271 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1275 if (!skb_make_writable(skb, skb->len))
1278 spin_lock_bh(&snmp_lock);
1279 ret = snmp_translate(ct, ctinfo, skb);
1280 spin_unlock_bh(&snmp_lock);
1284 static const struct nf_conntrack_expect_policy snmp_exp_policy = {
1289 static struct nf_conntrack_helper snmp_helper __read_mostly = {
1292 .expect_policy = &snmp_exp_policy,
1294 .tuple.src.l3num = AF_INET,
1295 .tuple.src.u.udp.port = __constant_htons(SNMP_PORT),
1296 .tuple.dst.protonum = IPPROTO_UDP,
1299 static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
1302 .expect_policy = &snmp_exp_policy,
1303 .name = "snmp_trap",
1304 .tuple.src.l3num = AF_INET,
1305 .tuple.src.u.udp.port = __constant_htons(SNMP_TRAP_PORT),
1306 .tuple.dst.protonum = IPPROTO_UDP,
1309 /*****************************************************************************
1313 *****************************************************************************/
1315 static int __init nf_nat_snmp_basic_init(void)
1319 ret = nf_conntrack_helper_register(&snmp_helper);
1322 ret = nf_conntrack_helper_register(&snmp_trap_helper);
1324 nf_conntrack_helper_unregister(&snmp_helper);
1330 static void __exit nf_nat_snmp_basic_fini(void)
1332 nf_conntrack_helper_unregister(&snmp_helper);
1333 nf_conntrack_helper_unregister(&snmp_trap_helper);
1336 module_init(nf_nat_snmp_basic_init);
1337 module_exit(nf_nat_snmp_basic_fini);
1339 module_param(debug, int, 0600);