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>
47 #include <linux/module.h>
48 #include <linux/types.h>
49 #include <linux/kernel.h>
50 #include <linux/netfilter_ipv4.h>
51 #include <linux/netfilter_ipv4/ip_nat.h>
52 #include <linux/netfilter_ipv4/ip_nat_helper.h>
53 #include <linux/brlock.h>
54 #include <linux/types.h>
56 #include <net/checksum.h>
58 #include <asm/uaccess.h>
63 #define SNMP_TRAP_PORT 162
64 #define NOCT1(n) (u_int8_t )((n) & 0xff)
67 static spinlock_t snmp_lock = SPIN_LOCK_UNLOCKED;
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))
223 cnt = (unsigned char) (ch & 0x7F);
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))
254 if (!asn1_length_decode(ctx, &def, &len))
257 /* primitive shall be definite, indefinite shall be constructed */
258 if (*con == ASN1_PRI && !def)
262 *eoc = ctx->pointer + len;
268 static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
273 if (!asn1_octet_decode(ctx, &ch))
277 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
281 if (!asn1_octet_decode(ctx, &ch))
285 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
290 if (ctx->pointer != eoc) {
291 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
298 static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
304 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
311 if (!asn1_octet_decode(ctx, &ch))
314 *integer = (signed char) ch;
317 while (ctx->pointer < eoc) {
318 if (++len > sizeof (long)) {
319 ctx->error = ASN1_ERR_DEC_BADVALUE;
323 if (!asn1_octet_decode(ctx, &ch))
332 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
334 unsigned int *integer)
339 if (!asn1_octet_decode(ctx, &ch))
343 if (ch == 0) len = 0;
346 while (ctx->pointer < eoc) {
347 if (++len > sizeof (unsigned int)) {
348 ctx->error = ASN1_ERR_DEC_BADVALUE;
352 if (!asn1_octet_decode(ctx, &ch))
361 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
363 unsigned long *integer)
368 if (!asn1_octet_decode(ctx, &ch))
372 if (ch == 0) len = 0;
375 while (ctx->pointer < eoc) {
376 if (++len > sizeof (unsigned long)) {
377 ctx->error = ASN1_ERR_DEC_BADVALUE;
381 if (!asn1_octet_decode(ctx, &ch))
390 static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
392 unsigned char **octets,
399 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
400 if (*octets == NULL) {
402 printk("OOM in bsalg (%d)\n", __LINE__);
407 while (ctx->pointer < eoc) {
408 if (!asn1_octet_decode(ctx, (unsigned char *)ptr++)) {
418 static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
419 unsigned long *subid)
426 if (!asn1_octet_decode(ctx, &ch))
431 } while ((ch & 0x80) == 0x80);
435 static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
444 size = eoc - ctx->pointer + 1;
446 /* first subid actually encodes first two subids */
447 if (size < 2 || size > ULONG_MAX/sizeof(unsigned long))
450 *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
453 printk("OOM in bsalg (%d)\n", __LINE__);
459 if (!asn1_subid_decode(ctx, &subid)) {
468 } else if (subid < 80) {
470 optr [1] = subid - 40;
473 optr [1] = subid - 80;
479 while (ctx->pointer < eoc) {
480 if (++(*len) > size) {
481 ctx->error = ASN1_ERR_DEC_BADVALUE;
487 if (!asn1_subid_decode(ctx, optr++)) {
496 /*****************************************************************************
498 * SNMP decoding routines (gxsnmp author Dirk Wisse)
500 *****************************************************************************/
509 #define SNMP_SIZE_COMM 256
510 #define SNMP_SIZE_OBJECTID 128
511 #define SNMP_SIZE_BUFCHR 256
512 #define SNMP_SIZE_BUFINT 128
513 #define SNMP_SIZE_SMALLOBJECTID 16
516 #define SNMP_PDU_GET 0
517 #define SNMP_PDU_NEXT 1
518 #define SNMP_PDU_RESPONSE 2
519 #define SNMP_PDU_SET 3
520 #define SNMP_PDU_TRAP1 4
521 #define SNMP_PDU_BULK 5
522 #define SNMP_PDU_INFORM 6
523 #define SNMP_PDU_TRAP2 7
526 #define SNMP_NOERROR 0
527 #define SNMP_TOOBIG 1
528 #define SNMP_NOSUCHNAME 2
529 #define SNMP_BADVALUE 3
530 #define SNMP_READONLY 4
531 #define SNMP_GENERROR 5
532 #define SNMP_NOACCESS 6
533 #define SNMP_WRONGTYPE 7
534 #define SNMP_WRONGLENGTH 8
535 #define SNMP_WRONGENCODING 9
536 #define SNMP_WRONGVALUE 10
537 #define SNMP_NOCREATION 11
538 #define SNMP_INCONSISTENTVALUE 12
539 #define SNMP_RESOURCEUNAVAILABLE 13
540 #define SNMP_COMMITFAILED 14
541 #define SNMP_UNDOFAILED 15
542 #define SNMP_AUTHORIZATIONERROR 16
543 #define SNMP_NOTWRITABLE 17
544 #define SNMP_INCONSISTENTNAME 18
546 /* General SNMP V1 Traps */
547 #define SNMP_TRAP_COLDSTART 0
548 #define SNMP_TRAP_WARMSTART 1
549 #define SNMP_TRAP_LINKDOWN 2
550 #define SNMP_TRAP_LINKUP 3
551 #define SNMP_TRAP_AUTFAILURE 4
552 #define SNMP_TRAP_EQPNEIGHBORLOSS 5
553 #define SNMP_TRAP_ENTSPECIFIC 6
557 #define SNMP_INTEGER 1 /* l */
558 #define SNMP_OCTETSTR 2 /* c */
559 #define SNMP_DISPLAYSTR 2 /* c */
560 #define SNMP_OBJECTID 3 /* ul */
561 #define SNMP_IPADDR 4 /* uc */
562 #define SNMP_COUNTER 5 /* ul */
563 #define SNMP_GAUGE 6 /* ul */
564 #define SNMP_TIMETICKS 7 /* ul */
565 #define SNMP_OPAQUE 8 /* c */
567 /* Additional SNMPv2 Types */
568 #define SNMP_UINTEGER 5 /* ul */
569 #define SNMP_BITSTR 9 /* uc */
570 #define SNMP_NSAP 10 /* uc */
571 #define SNMP_COUNTER64 11 /* ul */
572 #define SNMP_NOSUCHOBJECT 12
573 #define SNMP_NOSUCHINSTANCE 13
574 #define SNMP_ENDOFMIBVIEW 14
578 unsigned char uc[0]; /* 8 bit unsigned */
579 char c[0]; /* 8 bit signed */
580 unsigned long ul[0]; /* 32 bit unsigned */
581 long l[0]; /* 32 bit signed */
589 unsigned int syntax_len;
590 union snmp_syntax syntax;
596 unsigned int error_status;
597 unsigned int error_index;
604 unsigned long ip_address; /* pointer */
605 unsigned int general;
606 unsigned int specific;
623 static void inline mangle_address(unsigned char *begin,
625 const struct oct1_map *map,
634 static 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 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) {
712 if (!snmp_tag_cls2syntax(tag, cls, &type)) {
720 if (!asn1_long_decode(ctx, end, &l)) {
724 *obj = kmalloc(sizeof(struct snmp_object) + len,
729 printk("OOM in bsalg (%d)\n", __LINE__);
732 (*obj)->syntax.l[0] = l;
736 if (!asn1_octets_decode(ctx, end, &p, &len)) {
740 *obj = kmalloc(sizeof(struct snmp_object) + len,
745 printk("OOM in bsalg (%d)\n", __LINE__);
748 memcpy((*obj)->syntax.c, p, len);
752 case SNMP_NOSUCHOBJECT:
753 case SNMP_NOSUCHINSTANCE:
754 case SNMP_ENDOFMIBVIEW:
756 *obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
760 printk("OOM in bsalg (%d)\n", __LINE__);
763 if (!asn1_null_decode(ctx, end)) {
771 if (!asn1_oid_decode(ctx, end, (unsigned long **)&lp, &len)) {
775 len *= sizeof(unsigned long);
776 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
780 printk("OOM in bsalg (%d)\n", __LINE__);
783 memcpy((*obj)->syntax.ul, lp, len);
787 if (!asn1_octets_decode(ctx, end, &p, &len)) {
796 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
801 printk("OOM in bsalg (%d)\n", __LINE__);
804 memcpy((*obj)->syntax.uc, p, len);
810 len = sizeof(unsigned long);
811 if (!asn1_ulong_decode(ctx, end, &ul)) {
815 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
819 printk("OOM in bsalg (%d)\n", __LINE__);
822 (*obj)->syntax.ul[0] = ul;
829 (*obj)->syntax_len = len;
832 (*obj)->id_len = idlen;
834 if (!asn1_eoc_decode(ctx, eoc)) {
843 static unsigned char snmp_request_decode(struct asn1_ctx *ctx,
844 struct snmp_request *request)
846 unsigned int cls, con, tag;
849 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
852 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
855 if (!asn1_ulong_decode(ctx, end, &request->id))
858 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
861 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
864 if (!asn1_uint_decode(ctx, end, &request->error_status))
867 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
870 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
873 if (!asn1_uint_decode(ctx, end, &request->error_index))
880 * Fast checksum update for possibly oddly-aligned UDP byte, from the
881 * code example in the draft.
883 static void fast_csum(unsigned char *csum,
884 const unsigned char *optr,
885 const unsigned char *nptr,
890 x = csum[0] * 256 + csum[1];
894 if (odd) old = optr[0] * 256;
903 if (odd) new = nptr[0] * 256;
919 * - begin points to the start of the snmp messgae
920 * - addr points to the start of the address
922 static void inline mangle_address(unsigned char *begin,
924 const struct oct1_map *map,
927 if (map->from == NOCT1(*addr)) {
931 memcpy(&old, (unsigned char *)addr, sizeof(old));
935 /* Update UDP checksum if being used */
937 unsigned char odd = !((addr - begin) % 2);
939 fast_csum((unsigned char *)check,
940 &map->from, &map->to, odd);
945 printk(KERN_DEBUG "bsalg: mapped %u.%u.%u.%u to "
946 "%u.%u.%u.%u\n", NIPQUAD(old), NIPQUAD(*addr));
950 static unsigned char snmp_trap_decode(struct asn1_ctx *ctx,
951 struct snmp_v1_trap *trap,
952 const struct oct1_map *map,
955 unsigned int cls, con, tag, len;
958 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
961 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
964 if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
967 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
970 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
971 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS)))
974 if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
981 mangle_address(ctx->begin, ctx->pointer - 4, map, check);
983 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
986 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
989 if (!asn1_uint_decode(ctx, end, &trap->general))
992 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
995 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
998 if (!asn1_uint_decode(ctx, end, &trap->specific))
1001 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
1004 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
1005 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT)))
1008 if (!asn1_ulong_decode(ctx, end, &trap->time))
1014 kfree((unsigned long *)trap->ip_address);
1022 /*****************************************************************************
1026 *****************************************************************************/
1028 static void hex_dump(unsigned char *buf, size_t len)
1032 for (i = 0; i < len; i++) {
1035 printk("%02x ", *(buf + i));
1041 * Parse and mangle SNMP message according to mapping.
1042 * (And this is the fucking 'basic' method).
1044 static int snmp_parse_mangle(unsigned char *msg,
1046 const struct oct1_map *map,
1049 unsigned char *eoc, *end;
1050 unsigned int cls, con, tag, vers, pdutype;
1051 struct asn1_ctx ctx;
1052 struct asn1_octstr comm;
1053 struct snmp_object **obj;
1058 asn1_open(&ctx, msg, len);
1061 * Start of SNMP message.
1063 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1065 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1069 * Version 1 or 2 handled.
1071 if (!asn1_header_decode(&ctx, &end, &cls, &con, &tag))
1073 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
1075 if (!asn1_uint_decode (&ctx, end, &vers))
1078 printk(KERN_DEBUG "bsalg: snmp version: %u\n", vers + 1);
1085 if (!asn1_header_decode (&ctx, &end, &cls, &con, &tag))
1087 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OTS)
1089 if (!asn1_octets_decode(&ctx, end, &comm.data, &comm.len))
1094 printk(KERN_DEBUG "bsalg: community: ");
1095 for (i = 0; i < comm.len; i++)
1096 printk("%c", comm.data[i]);
1104 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &pdutype))
1106 if (cls != ASN1_CTX || con != ASN1_CON)
1109 unsigned char *pdus[] = {
1110 [SNMP_PDU_GET] = "get",
1111 [SNMP_PDU_NEXT] = "get-next",
1112 [SNMP_PDU_RESPONSE] = "response",
1113 [SNMP_PDU_SET] = "set",
1114 [SNMP_PDU_TRAP1] = "trapv1",
1115 [SNMP_PDU_BULK] = "bulk",
1116 [SNMP_PDU_INFORM] = "inform",
1117 [SNMP_PDU_TRAP2] = "trapv2"
1120 if (pdutype > SNMP_PDU_TRAP2)
1121 printk(KERN_DEBUG "bsalg: bad pdu type %u\n", pdutype);
1123 printk(KERN_DEBUG "bsalg: pdu: %s\n", pdus[pdutype]);
1125 if (pdutype != SNMP_PDU_RESPONSE &&
1126 pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
1130 * Request header or v1 trap
1132 if (pdutype == SNMP_PDU_TRAP1) {
1133 struct snmp_v1_trap trap;
1134 unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
1138 kfree((unsigned long *)trap.ip_address);
1143 struct snmp_request req;
1145 if (!snmp_request_decode(&ctx, &req))
1149 printk(KERN_DEBUG "bsalg: request: id=0x%lx error_status=%u "
1150 "error_index=%u\n", req.id, req.error_status,
1155 * Loop through objects, look for IP addresses to mangle.
1157 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1160 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1163 obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
1165 if (net_ratelimit())
1166 printk(KERN_WARNING "OOM in bsalg(%d)\n", __LINE__);
1170 while (!asn1_eoc_decode(&ctx, eoc)) {
1173 if (!snmp_object_decode(&ctx, obj)) {
1184 printk(KERN_DEBUG "bsalg: object: ");
1185 for (i = 0; i < (*obj)->id_len; i++) {
1188 printk("%lu", (*obj)->id[i]);
1190 printk(": type=%u\n", (*obj)->type);
1194 if ((*obj)->type == SNMP_IPADDR)
1195 mangle_address(ctx.begin, ctx.pointer - 4 , map, check);
1202 if (!asn1_eoc_decode(&ctx, eoc))
1208 /*****************************************************************************
1212 *****************************************************************************/
1215 * SNMP translation routine.
1217 static int snmp_translate(struct ip_conntrack *ct,
1218 struct ip_nat_info *info,
1219 enum ip_conntrack_info ctinfo,
1220 unsigned int hooknum,
1221 struct sk_buff **pskb)
1223 struct iphdr *iph = (*pskb)->nh.iph;
1224 struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
1225 u_int16_t udplen = ntohs(udph->len);
1226 u_int16_t paylen = udplen - sizeof(struct udphdr);
1227 int dir = CTINFO2DIR(ctinfo);
1228 struct oct1_map map;
1231 * Determine mappping for application layer addresses based
1232 * on NAT manipulations for the packet.
1234 if (dir == IP_CT_DIR_ORIGINAL) {
1236 map.from = NOCT1(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip);
1237 map.to = NOCT1(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip);
1240 map.from = NOCT1(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
1241 map.to = NOCT1(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip);
1244 if (map.from == map.to)
1247 if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
1248 paylen, &map, &udph->check)) {
1249 printk(KERN_WARNING "bsalg: parser failed\n");
1256 * NAT helper function, packets arrive here from NAT code.
1258 static unsigned int nat_help(struct ip_conntrack *ct,
1259 struct ip_conntrack_expect *exp,
1260 struct ip_nat_info *info,
1261 enum ip_conntrack_info ctinfo,
1262 unsigned int hooknum,
1263 struct sk_buff **pskb)
1265 int dir = CTINFO2DIR(ctinfo);
1266 struct iphdr *iph = (*pskb)->nh.iph;
1267 struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
1269 spin_lock_bh(&snmp_lock);
1272 * Translate snmp replies on pre-routing (DNAT) and snmp traps
1273 * on post routing (SNAT).
1275 if (!((dir == IP_CT_DIR_REPLY && hooknum == NF_IP_PRE_ROUTING &&
1276 udph->source == ntohs(SNMP_PORT)) ||
1277 (dir == IP_CT_DIR_ORIGINAL && hooknum == NF_IP_POST_ROUTING &&
1278 udph->dest == ntohs(SNMP_TRAP_PORT)))) {
1279 spin_unlock_bh(&snmp_lock);
1284 printk(KERN_DEBUG "bsalg: dir=%s hook=%d manip=%s len=%d "
1285 "src=%u.%u.%u.%u:%u dst=%u.%u.%u.%u:%u "
1286 "osrc=%u.%u.%u.%u odst=%u.%u.%u.%u "
1287 "rsrc=%u.%u.%u.%u rdst=%u.%u.%u.%u "
1289 dir == IP_CT_DIR_REPLY ? "reply" : "orig", hooknum,
1290 HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC ? "snat" :
1291 "dnat", (*pskb)->len,
1292 NIPQUAD(iph->saddr), ntohs(udph->source),
1293 NIPQUAD(iph->daddr), ntohs(udph->dest),
1294 NIPQUAD(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip),
1295 NIPQUAD(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip),
1296 NIPQUAD(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip),
1297 NIPQUAD(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip));
1301 * Make sure the packet length is ok. So far, we were only guaranteed
1302 * to have a valid length IP header plus 8 bytes, which means we have
1303 * enough room for a UDP header. Just verify the UDP length field so we
1304 * can mess around with the payload.
1306 if (ntohs(udph->len) == (*pskb)->len - (iph->ihl << 2)) {
1307 int ret = snmp_translate(ct, info, ctinfo, hooknum, pskb);
1308 spin_unlock_bh(&snmp_lock);
1312 if (net_ratelimit())
1313 printk(KERN_WARNING "bsalg: dropping malformed packet "
1314 "src=%u.%u.%u.%u dst=%u.%u.%u.%u\n",
1315 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1316 spin_unlock_bh(&snmp_lock);
1320 static struct ip_nat_helper snmp = {
1323 IP_NAT_HELPER_F_STANDALONE,
1325 { { 0, { .udp = { __constant_htons(SNMP_PORT) } } },
1326 { 0, { 0 }, IPPROTO_UDP } },
1327 { { 0, { .udp = { 0xFFFF } } },
1328 { 0, { 0 }, 0xFFFF } },
1331 static struct ip_nat_helper snmp_trap = {
1334 IP_NAT_HELPER_F_STANDALONE,
1336 { { 0, { .udp = { __constant_htons(SNMP_TRAP_PORT) } } },
1337 { 0, { 0 }, IPPROTO_UDP } },
1338 { { 0, { .udp = { 0xFFFF } } },
1339 { 0, { 0 }, 0xFFFF } },
1342 /*****************************************************************************
1346 *****************************************************************************/
1348 static int __init init(void)
1352 ret = ip_nat_helper_register(&snmp);
1355 ret = ip_nat_helper_register(&snmp_trap);
1357 ip_nat_helper_unregister(&snmp);
1363 static void __exit fini(void)
1365 ip_nat_helper_unregister(&snmp);
1366 ip_nat_helper_unregister(&snmp_trap);
1367 br_write_lock_bh(BR_NETPROTO_LOCK);
1368 br_write_unlock_bh(BR_NETPROTO_LOCK);
1374 MODULE_PARM(debug, "i");
1375 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
1376 MODULE_LICENSE("GPL");