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>
43 * 2000-08-06: Convert to new helper API (Harald Welte).
46 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/kernel.h>
51 #include <linux/moduleparam.h>
52 #include <linux/netfilter_ipv4.h>
53 #include <linux/netfilter_ipv4/ip_nat.h>
54 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
55 #include <linux/netfilter_ipv4/ip_nat_helper.h>
57 #include <linux/udp.h>
58 #include <net/checksum.h>
60 #include <asm/uaccess.h>
62 MODULE_LICENSE("GPL");
63 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
64 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
67 #define SNMP_TRAP_PORT 162
68 #define NOCT1(n) (u_int8_t )((n) & 0xff)
71 static DEFINE_SPINLOCK(snmp_lock);
74 * Application layer address mapping mimics the NAT mapping, but
75 * only for the first octet in this case (a more flexible system
76 * can be implemented if needed).
85 /*****************************************************************************
87 * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
89 *****************************************************************************/
92 #define ASN1_UNI 0 /* Universal */
93 #define ASN1_APL 1 /* Application */
94 #define ASN1_CTX 2 /* Context */
95 #define ASN1_PRV 3 /* Private */
98 #define ASN1_EOC 0 /* End Of Contents */
99 #define ASN1_BOL 1 /* Boolean */
100 #define ASN1_INT 2 /* Integer */
101 #define ASN1_BTS 3 /* Bit String */
102 #define ASN1_OTS 4 /* Octet String */
103 #define ASN1_NUL 5 /* Null */
104 #define ASN1_OJI 6 /* Object Identifier */
105 #define ASN1_OJD 7 /* Object Description */
106 #define ASN1_EXT 8 /* External */
107 #define ASN1_SEQ 16 /* Sequence */
108 #define ASN1_SET 17 /* Set */
109 #define ASN1_NUMSTR 18 /* Numerical String */
110 #define ASN1_PRNSTR 19 /* Printable String */
111 #define ASN1_TEXSTR 20 /* Teletext String */
112 #define ASN1_VIDSTR 21 /* Video String */
113 #define ASN1_IA5STR 22 /* IA5 String */
114 #define ASN1_UNITIM 23 /* Universal Time */
115 #define ASN1_GENTIM 24 /* General Time */
116 #define ASN1_GRASTR 25 /* Graphical String */
117 #define ASN1_VISSTR 26 /* Visible String */
118 #define ASN1_GENSTR 27 /* General String */
120 /* Primitive / Constructed methods*/
121 #define ASN1_PRI 0 /* Primitive */
122 #define ASN1_CON 1 /* Constructed */
127 #define ASN1_ERR_NOERROR 0
128 #define ASN1_ERR_DEC_EMPTY 2
129 #define ASN1_ERR_DEC_EOC_MISMATCH 3
130 #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
131 #define ASN1_ERR_DEC_BADVALUE 5
138 int error; /* Error condition */
139 unsigned char *pointer; /* Octet just to be decoded */
140 unsigned char *begin; /* First octet */
141 unsigned char *end; /* Octet after last octet */
145 * Octet string (not null terminated)
153 static void asn1_open(struct asn1_ctx *ctx,
158 ctx->end = buf + len;
160 ctx->error = ASN1_ERR_NOERROR;
163 static unsigned char asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
165 if (ctx->pointer >= ctx->end) {
166 ctx->error = ASN1_ERR_DEC_EMPTY;
169 *ch = *(ctx->pointer)++;
173 static unsigned char asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
181 if (!asn1_octet_decode(ctx, &ch))
185 } while ((ch & 0x80) == 0x80);
189 static unsigned char asn1_id_decode(struct asn1_ctx *ctx,
196 if (!asn1_octet_decode(ctx, &ch))
199 *cls = (ch & 0xC0) >> 6;
200 *con = (ch & 0x20) >> 5;
204 if (!asn1_tag_decode(ctx, tag))
210 static unsigned char asn1_length_decode(struct asn1_ctx *ctx,
214 unsigned char ch, cnt;
216 if (!asn1_octet_decode(ctx, &ch))
227 cnt = (unsigned char) (ch & 0x7F);
231 if (!asn1_octet_decode(ctx, &ch))
242 static unsigned char asn1_header_decode(struct asn1_ctx *ctx,
248 unsigned int def, len;
250 if (!asn1_id_decode(ctx, cls, con, tag))
253 if (!asn1_length_decode(ctx, &def, &len))
257 *eoc = ctx->pointer + len;
263 static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
268 if (!asn1_octet_decode(ctx, &ch))
272 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
276 if (!asn1_octet_decode(ctx, &ch))
280 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
285 if (ctx->pointer != eoc) {
286 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
293 static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
299 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
306 if (!asn1_octet_decode(ctx, &ch))
309 *integer = (signed char) ch;
312 while (ctx->pointer < eoc) {
313 if (++len > sizeof (long)) {
314 ctx->error = ASN1_ERR_DEC_BADVALUE;
318 if (!asn1_octet_decode(ctx, &ch))
327 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
329 unsigned int *integer)
334 if (!asn1_octet_decode(ctx, &ch))
338 if (ch == 0) len = 0;
341 while (ctx->pointer < eoc) {
342 if (++len > sizeof (unsigned int)) {
343 ctx->error = ASN1_ERR_DEC_BADVALUE;
347 if (!asn1_octet_decode(ctx, &ch))
356 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
358 unsigned long *integer)
363 if (!asn1_octet_decode(ctx, &ch))
367 if (ch == 0) len = 0;
370 while (ctx->pointer < eoc) {
371 if (++len > sizeof (unsigned long)) {
372 ctx->error = ASN1_ERR_DEC_BADVALUE;
376 if (!asn1_octet_decode(ctx, &ch))
385 static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
387 unsigned char **octets,
394 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
395 if (*octets == NULL) {
397 printk("OOM in bsalg (%d)\n", __LINE__);
402 while (ctx->pointer < eoc) {
403 if (!asn1_octet_decode(ctx, (unsigned char *)ptr++)) {
413 static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
414 unsigned long *subid)
421 if (!asn1_octet_decode(ctx, &ch))
426 } while ((ch & 0x80) == 0x80);
430 static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
439 size = eoc - ctx->pointer + 1;
440 *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
443 printk("OOM in bsalg (%d)\n", __LINE__);
449 if (!asn1_subid_decode(ctx, &subid)) {
458 } else if (subid < 80) {
460 optr [1] = subid - 40;
463 optr [1] = subid - 80;
469 while (ctx->pointer < eoc) {
470 if (++(*len) > size) {
471 ctx->error = ASN1_ERR_DEC_BADVALUE;
477 if (!asn1_subid_decode(ctx, optr++)) {
486 /*****************************************************************************
488 * SNMP decoding routines (gxsnmp author Dirk Wisse)
490 *****************************************************************************/
499 #define SNMP_SIZE_COMM 256
500 #define SNMP_SIZE_OBJECTID 128
501 #define SNMP_SIZE_BUFCHR 256
502 #define SNMP_SIZE_BUFINT 128
503 #define SNMP_SIZE_SMALLOBJECTID 16
506 #define SNMP_PDU_GET 0
507 #define SNMP_PDU_NEXT 1
508 #define SNMP_PDU_RESPONSE 2
509 #define SNMP_PDU_SET 3
510 #define SNMP_PDU_TRAP1 4
511 #define SNMP_PDU_BULK 5
512 #define SNMP_PDU_INFORM 6
513 #define SNMP_PDU_TRAP2 7
516 #define SNMP_NOERROR 0
517 #define SNMP_TOOBIG 1
518 #define SNMP_NOSUCHNAME 2
519 #define SNMP_BADVALUE 3
520 #define SNMP_READONLY 4
521 #define SNMP_GENERROR 5
522 #define SNMP_NOACCESS 6
523 #define SNMP_WRONGTYPE 7
524 #define SNMP_WRONGLENGTH 8
525 #define SNMP_WRONGENCODING 9
526 #define SNMP_WRONGVALUE 10
527 #define SNMP_NOCREATION 11
528 #define SNMP_INCONSISTENTVALUE 12
529 #define SNMP_RESOURCEUNAVAILABLE 13
530 #define SNMP_COMMITFAILED 14
531 #define SNMP_UNDOFAILED 15
532 #define SNMP_AUTHORIZATIONERROR 16
533 #define SNMP_NOTWRITABLE 17
534 #define SNMP_INCONSISTENTNAME 18
536 /* General SNMP V1 Traps */
537 #define SNMP_TRAP_COLDSTART 0
538 #define SNMP_TRAP_WARMSTART 1
539 #define SNMP_TRAP_LINKDOWN 2
540 #define SNMP_TRAP_LINKUP 3
541 #define SNMP_TRAP_AUTFAILURE 4
542 #define SNMP_TRAP_EQPNEIGHBORLOSS 5
543 #define SNMP_TRAP_ENTSPECIFIC 6
547 #define SNMP_INTEGER 1 /* l */
548 #define SNMP_OCTETSTR 2 /* c */
549 #define SNMP_DISPLAYSTR 2 /* c */
550 #define SNMP_OBJECTID 3 /* ul */
551 #define SNMP_IPADDR 4 /* uc */
552 #define SNMP_COUNTER 5 /* ul */
553 #define SNMP_GAUGE 6 /* ul */
554 #define SNMP_TIMETICKS 7 /* ul */
555 #define SNMP_OPAQUE 8 /* c */
557 /* Additional SNMPv2 Types */
558 #define SNMP_UINTEGER 5 /* ul */
559 #define SNMP_BITSTR 9 /* uc */
560 #define SNMP_NSAP 10 /* uc */
561 #define SNMP_COUNTER64 11 /* ul */
562 #define SNMP_NOSUCHOBJECT 12
563 #define SNMP_NOSUCHINSTANCE 13
564 #define SNMP_ENDOFMIBVIEW 14
568 unsigned char uc[0]; /* 8 bit unsigned */
569 char c[0]; /* 8 bit signed */
570 unsigned long ul[0]; /* 32 bit unsigned */
571 long l[0]; /* 32 bit signed */
579 unsigned int syntax_len;
580 union snmp_syntax syntax;
586 unsigned int error_status;
587 unsigned int error_index;
594 unsigned long ip_address; /* pointer */
595 unsigned int general;
596 unsigned int specific;
613 static inline void mangle_address(unsigned char *begin,
615 const struct oct1_map *map,
624 static struct snmp_cnv snmp_conv [] =
626 {ASN1_UNI, ASN1_NUL, SNMP_NULL},
627 {ASN1_UNI, ASN1_INT, SNMP_INTEGER},
628 {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR},
629 {ASN1_UNI, ASN1_OTS, SNMP_DISPLAYSTR},
630 {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID},
631 {ASN1_APL, SNMP_IPA, SNMP_IPADDR},
632 {ASN1_APL, SNMP_CNT, SNMP_COUNTER}, /* Counter32 */
633 {ASN1_APL, SNMP_GGE, SNMP_GAUGE}, /* Gauge32 == Unsigned32 */
634 {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS},
635 {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE},
637 /* SNMPv2 data types and errors */
638 {ASN1_UNI, ASN1_BTS, SNMP_BITSTR},
639 {ASN1_APL, SNMP_C64, SNMP_COUNTER64},
640 {ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT},
641 {ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE},
642 {ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW},
646 static unsigned char snmp_tag_cls2syntax(unsigned int tag,
648 unsigned short *syntax)
650 struct snmp_cnv *cnv;
654 while (cnv->syntax != -1) {
655 if (cnv->tag == tag && cnv->class == cls) {
656 *syntax = cnv->syntax;
664 static unsigned char snmp_object_decode(struct asn1_ctx *ctx,
665 struct snmp_object **obj)
667 unsigned int cls, con, tag, len, idlen;
669 unsigned char *eoc, *end, *p;
670 unsigned long *lp, *id;
677 if (!asn1_header_decode(ctx, &eoc, &cls, &con, &tag))
680 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
683 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
686 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
689 if (!asn1_oid_decode(ctx, end, &id, &idlen))
692 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag)) {
697 if (con != ASN1_PRI) {
702 if (!snmp_tag_cls2syntax(tag, cls, &type)) {
710 if (!asn1_long_decode(ctx, end, &l)) {
714 *obj = kmalloc(sizeof(struct snmp_object) + len,
719 printk("OOM in bsalg (%d)\n", __LINE__);
722 (*obj)->syntax.l[0] = l;
726 if (!asn1_octets_decode(ctx, end, &p, &len)) {
730 *obj = kmalloc(sizeof(struct snmp_object) + len,
735 printk("OOM in bsalg (%d)\n", __LINE__);
738 memcpy((*obj)->syntax.c, p, len);
742 case SNMP_NOSUCHOBJECT:
743 case SNMP_NOSUCHINSTANCE:
744 case SNMP_ENDOFMIBVIEW:
746 *obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
750 printk("OOM in bsalg (%d)\n", __LINE__);
753 if (!asn1_null_decode(ctx, end)) {
761 if (!asn1_oid_decode(ctx, end, (unsigned long **)&lp, &len)) {
765 len *= sizeof(unsigned long);
766 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
770 printk("OOM in bsalg (%d)\n", __LINE__);
773 memcpy((*obj)->syntax.ul, lp, len);
777 if (!asn1_octets_decode(ctx, end, &p, &len)) {
786 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
791 printk("OOM in bsalg (%d)\n", __LINE__);
794 memcpy((*obj)->syntax.uc, p, len);
800 len = sizeof(unsigned long);
801 if (!asn1_ulong_decode(ctx, end, &ul)) {
805 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
809 printk("OOM in bsalg (%d)\n", __LINE__);
812 (*obj)->syntax.ul[0] = ul;
819 (*obj)->syntax_len = len;
822 (*obj)->id_len = idlen;
824 if (!asn1_eoc_decode(ctx, eoc)) {
833 static unsigned char snmp_request_decode(struct asn1_ctx *ctx,
834 struct snmp_request *request)
836 unsigned int cls, con, tag;
839 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
842 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
845 if (!asn1_ulong_decode(ctx, end, &request->id))
848 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
851 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
854 if (!asn1_uint_decode(ctx, end, &request->error_status))
857 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
860 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
863 if (!asn1_uint_decode(ctx, end, &request->error_index))
870 * Fast checksum update for possibly oddly-aligned UDP byte, from the
871 * code example in the draft.
873 static void fast_csum(unsigned char *csum,
874 const unsigned char *optr,
875 const unsigned char *nptr,
880 x = csum[0] * 256 + csum[1];
884 if (odd) old = optr[0] * 256;
893 if (odd) new = nptr[0] * 256;
909 * - begin points to the start of the snmp messgae
910 * - addr points to the start of the address
912 static inline void mangle_address(unsigned char *begin,
914 const struct oct1_map *map,
917 if (map->from == NOCT1(*addr)) {
921 memcpy(&old, (unsigned char *)addr, sizeof(old));
925 /* Update UDP checksum if being used */
927 unsigned char odd = !((addr - begin) % 2);
929 fast_csum((unsigned char *)check,
930 &map->from, &map->to, odd);
935 printk(KERN_DEBUG "bsalg: mapped %u.%u.%u.%u to "
936 "%u.%u.%u.%u\n", NIPQUAD(old), NIPQUAD(*addr));
940 static unsigned char snmp_trap_decode(struct asn1_ctx *ctx,
941 struct snmp_v1_trap *trap,
942 const struct oct1_map *map,
945 unsigned int cls, con, tag, len;
948 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
951 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
954 if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
957 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
960 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
961 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS)))
964 if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
971 mangle_address(ctx->begin, ctx->pointer - 4, map, check);
973 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
976 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
979 if (!asn1_uint_decode(ctx, end, &trap->general))
982 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
985 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
988 if (!asn1_uint_decode(ctx, end, &trap->specific))
991 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
994 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
995 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT)))
998 if (!asn1_ulong_decode(ctx, end, &trap->time))
1007 kfree((unsigned long *)trap->ip_address);
1012 /*****************************************************************************
1016 *****************************************************************************/
1018 static void hex_dump(unsigned char *buf, size_t len)
1022 for (i = 0; i < len; i++) {
1025 printk("%02x ", *(buf + i));
1031 * Parse and mangle SNMP message according to mapping.
1032 * (And this is the fucking 'basic' method).
1034 static int snmp_parse_mangle(unsigned char *msg,
1036 const struct oct1_map *map,
1039 unsigned char *eoc, *end;
1040 unsigned int cls, con, tag, vers, pdutype;
1041 struct asn1_ctx ctx;
1042 struct asn1_octstr comm;
1043 struct snmp_object **obj;
1048 asn1_open(&ctx, msg, len);
1051 * Start of SNMP message.
1053 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1055 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1059 * Version 1 or 2 handled.
1061 if (!asn1_header_decode(&ctx, &end, &cls, &con, &tag))
1063 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
1065 if (!asn1_uint_decode (&ctx, end, &vers))
1068 printk(KERN_DEBUG "bsalg: snmp version: %u\n", vers + 1);
1075 if (!asn1_header_decode (&ctx, &end, &cls, &con, &tag))
1077 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OTS)
1079 if (!asn1_octets_decode(&ctx, end, &comm.data, &comm.len))
1084 printk(KERN_DEBUG "bsalg: community: ");
1085 for (i = 0; i < comm.len; i++)
1086 printk("%c", comm.data[i]);
1094 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &pdutype))
1096 if (cls != ASN1_CTX || con != ASN1_CON)
1099 unsigned char *pdus[] = {
1100 [SNMP_PDU_GET] = "get",
1101 [SNMP_PDU_NEXT] = "get-next",
1102 [SNMP_PDU_RESPONSE] = "response",
1103 [SNMP_PDU_SET] = "set",
1104 [SNMP_PDU_TRAP1] = "trapv1",
1105 [SNMP_PDU_BULK] = "bulk",
1106 [SNMP_PDU_INFORM] = "inform",
1107 [SNMP_PDU_TRAP2] = "trapv2"
1110 if (pdutype > SNMP_PDU_TRAP2)
1111 printk(KERN_DEBUG "bsalg: bad pdu type %u\n", pdutype);
1113 printk(KERN_DEBUG "bsalg: pdu: %s\n", pdus[pdutype]);
1115 if (pdutype != SNMP_PDU_RESPONSE &&
1116 pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
1120 * Request header or v1 trap
1122 if (pdutype == SNMP_PDU_TRAP1) {
1123 struct snmp_v1_trap trap;
1124 unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
1126 /* Discard trap allocations regardless */
1128 kfree((unsigned long *)trap.ip_address);
1134 struct snmp_request req;
1136 if (!snmp_request_decode(&ctx, &req))
1140 printk(KERN_DEBUG "bsalg: request: id=0x%lx error_status=%u "
1141 "error_index=%u\n", req.id, req.error_status,
1146 * Loop through objects, look for IP addresses to mangle.
1148 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1151 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1154 obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
1156 if (net_ratelimit())
1157 printk(KERN_WARNING "OOM in bsalg(%d)\n", __LINE__);
1161 while (!asn1_eoc_decode(&ctx, eoc)) {
1164 if (!snmp_object_decode(&ctx, obj)) {
1174 printk(KERN_DEBUG "bsalg: object: ");
1175 for (i = 0; i < (*obj)->id_len; i++) {
1178 printk("%lu", (*obj)->id[i]);
1180 printk(": type=%u\n", (*obj)->type);
1184 if ((*obj)->type == SNMP_IPADDR)
1185 mangle_address(ctx.begin, ctx.pointer - 4 , map, check);
1192 if (!asn1_eoc_decode(&ctx, eoc))
1198 /*****************************************************************************
1202 *****************************************************************************/
1205 * SNMP translation routine.
1207 static int snmp_translate(struct ip_conntrack *ct,
1208 enum ip_conntrack_info ctinfo,
1209 struct sk_buff **pskb)
1211 struct iphdr *iph = (*pskb)->nh.iph;
1212 struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
1213 u_int16_t udplen = ntohs(udph->len);
1214 u_int16_t paylen = udplen - sizeof(struct udphdr);
1215 int dir = CTINFO2DIR(ctinfo);
1216 struct oct1_map map;
1219 * Determine mappping for application layer addresses based
1220 * on NAT manipulations for the packet.
1222 if (dir == IP_CT_DIR_ORIGINAL) {
1224 map.from = NOCT1(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip);
1225 map.to = NOCT1(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip);
1228 map.from = NOCT1(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
1229 map.to = NOCT1(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip);
1232 if (map.from == map.to)
1235 if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
1236 paylen, &map, &udph->check)) {
1237 if (net_ratelimit())
1238 printk(KERN_WARNING "bsalg: parser failed\n");
1244 /* We don't actually set up expectations, just adjust internal IP
1245 * addresses if this is being NATted */
1246 static int help(struct sk_buff **pskb,
1247 struct ip_conntrack *ct,
1248 enum ip_conntrack_info ctinfo)
1250 int dir = CTINFO2DIR(ctinfo);
1252 struct iphdr *iph = (*pskb)->nh.iph;
1253 struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
1255 /* SNMP replies and originating SNMP traps get mangled */
1256 if (udph->source == ntohs(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
1258 if (udph->dest == ntohs(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
1262 if (!(ct->status & IPS_NAT_MASK))
1266 * Make sure the packet length is ok. So far, we were only guaranteed
1267 * to have a valid length IP header plus 8 bytes, which means we have
1268 * enough room for a UDP header. Just verify the UDP length field so we
1269 * can mess around with the payload.
1271 if (ntohs(udph->len) != (*pskb)->len - (iph->ihl << 2)) {
1272 if (net_ratelimit())
1273 printk(KERN_WARNING "SNMP: dropping malformed packet "
1274 "src=%u.%u.%u.%u dst=%u.%u.%u.%u\n",
1275 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1279 if (!skb_make_writable(pskb, (*pskb)->len))
1282 spin_lock_bh(&snmp_lock);
1283 ret = snmp_translate(ct, ctinfo, pskb);
1284 spin_unlock_bh(&snmp_lock);
1288 static struct ip_conntrack_helper snmp_helper = {
1295 .tuple = { .src = { .u = { __constant_htons(SNMP_PORT) } },
1296 .dst = { .protonum = IPPROTO_UDP },
1298 .mask = { .src = { .u = { 0xFFFF } },
1299 .dst = { .protonum = 0xFF },
1303 static struct ip_conntrack_helper snmp_trap_helper = {
1308 .name = "snmp_trap",
1310 .tuple = { .src = { .u = { __constant_htons(SNMP_TRAP_PORT) } },
1311 .dst = { .protonum = IPPROTO_UDP },
1313 .mask = { .src = { .u = { 0xFFFF } },
1314 .dst = { .protonum = 0xFF },
1318 /*****************************************************************************
1322 *****************************************************************************/
1324 static int __init init(void)
1328 ret = ip_conntrack_helper_register(&snmp_helper);
1331 ret = ip_conntrack_helper_register(&snmp_trap_helper);
1333 ip_conntrack_helper_unregister(&snmp_helper);
1339 static void __exit fini(void)
1341 ip_conntrack_helper_unregister(&snmp_helper);
1342 ip_conntrack_helper_unregister(&snmp_trap_helper);
1348 module_param(debug, bool, 0600);