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
))
223 cnt
= (unsigned char) (ch
& 0x7F);
227 if (!asn1_octet_decode(ctx
, &ch
))
238 static unsigned char asn1_header_decode(struct asn1_ctx
*ctx
,
244 unsigned int def
, len
;
246 if (!asn1_id_decode(ctx
, cls
, con
, tag
))
250 if (!asn1_length_decode(ctx
, &def
, &len
))
254 *eoc
= ctx
->pointer
+ len
;
260 static unsigned char asn1_eoc_decode(struct asn1_ctx
*ctx
, unsigned char *eoc
)
265 if (!asn1_octet_decode(ctx
, &ch
))
269 ctx
->error
= ASN1_ERR_DEC_EOC_MISMATCH
;
273 if (!asn1_octet_decode(ctx
, &ch
))
277 ctx
->error
= ASN1_ERR_DEC_EOC_MISMATCH
;
282 if (ctx
->pointer
!= eoc
) {
283 ctx
->error
= ASN1_ERR_DEC_LENGTH_MISMATCH
;
290 static unsigned char asn1_null_decode(struct asn1_ctx
*ctx
, unsigned char *eoc
)
296 static unsigned char asn1_long_decode(struct asn1_ctx
*ctx
,
303 if (!asn1_octet_decode(ctx
, &ch
))
306 *integer
= (signed char) ch
;
309 while (ctx
->pointer
< eoc
) {
310 if (++len
> sizeof (long)) {
311 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
315 if (!asn1_octet_decode(ctx
, &ch
))
324 static unsigned char asn1_uint_decode(struct asn1_ctx
*ctx
,
326 unsigned int *integer
)
331 if (!asn1_octet_decode(ctx
, &ch
))
335 if (ch
== 0) len
= 0;
338 while (ctx
->pointer
< eoc
) {
339 if (++len
> sizeof (unsigned int)) {
340 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
344 if (!asn1_octet_decode(ctx
, &ch
))
353 static unsigned char asn1_ulong_decode(struct asn1_ctx
*ctx
,
355 unsigned long *integer
)
360 if (!asn1_octet_decode(ctx
, &ch
))
364 if (ch
== 0) len
= 0;
367 while (ctx
->pointer
< eoc
) {
368 if (++len
> sizeof (unsigned long)) {
369 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
373 if (!asn1_octet_decode(ctx
, &ch
))
382 static unsigned char asn1_octets_decode(struct asn1_ctx
*ctx
,
384 unsigned char **octets
,
391 *octets
= kmalloc(eoc
- ctx
->pointer
, GFP_ATOMIC
);
392 if (*octets
== NULL
) {
394 printk("OOM in bsalg (%d)\n", __LINE__
);
399 while (ctx
->pointer
< eoc
) {
400 if (!asn1_octet_decode(ctx
, (unsigned char *)ptr
++)) {
410 static unsigned char asn1_subid_decode(struct asn1_ctx
*ctx
,
411 unsigned long *subid
)
418 if (!asn1_octet_decode(ctx
, &ch
))
423 } while ((ch
& 0x80) == 0x80);
427 static unsigned char asn1_oid_decode(struct asn1_ctx
*ctx
,
436 size
= eoc
- ctx
->pointer
+ 1;
437 *oid
= kmalloc(size
* sizeof(unsigned long), GFP_ATOMIC
);
440 printk("OOM in bsalg (%d)\n", __LINE__
);
446 if (!asn1_subid_decode(ctx
, &subid
)) {
455 } else if (subid
< 80) {
457 optr
[1] = subid
- 40;
460 optr
[1] = subid
- 80;
466 while (ctx
->pointer
< eoc
) {
467 if (++(*len
) > size
) {
468 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
474 if (!asn1_subid_decode(ctx
, optr
++)) {
483 /*****************************************************************************
485 * SNMP decoding routines (gxsnmp author Dirk Wisse)
487 *****************************************************************************/
496 #define SNMP_SIZE_COMM 256
497 #define SNMP_SIZE_OBJECTID 128
498 #define SNMP_SIZE_BUFCHR 256
499 #define SNMP_SIZE_BUFINT 128
500 #define SNMP_SIZE_SMALLOBJECTID 16
503 #define SNMP_PDU_GET 0
504 #define SNMP_PDU_NEXT 1
505 #define SNMP_PDU_RESPONSE 2
506 #define SNMP_PDU_SET 3
507 #define SNMP_PDU_TRAP1 4
508 #define SNMP_PDU_BULK 5
509 #define SNMP_PDU_INFORM 6
510 #define SNMP_PDU_TRAP2 7
513 #define SNMP_NOERROR 0
514 #define SNMP_TOOBIG 1
515 #define SNMP_NOSUCHNAME 2
516 #define SNMP_BADVALUE 3
517 #define SNMP_READONLY 4
518 #define SNMP_GENERROR 5
519 #define SNMP_NOACCESS 6
520 #define SNMP_WRONGTYPE 7
521 #define SNMP_WRONGLENGTH 8
522 #define SNMP_WRONGENCODING 9
523 #define SNMP_WRONGVALUE 10
524 #define SNMP_NOCREATION 11
525 #define SNMP_INCONSISTENTVALUE 12
526 #define SNMP_RESOURCEUNAVAILABLE 13
527 #define SNMP_COMMITFAILED 14
528 #define SNMP_UNDOFAILED 15
529 #define SNMP_AUTHORIZATIONERROR 16
530 #define SNMP_NOTWRITABLE 17
531 #define SNMP_INCONSISTENTNAME 18
533 /* General SNMP V1 Traps */
534 #define SNMP_TRAP_COLDSTART 0
535 #define SNMP_TRAP_WARMSTART 1
536 #define SNMP_TRAP_LINKDOWN 2
537 #define SNMP_TRAP_LINKUP 3
538 #define SNMP_TRAP_AUTFAILURE 4
539 #define SNMP_TRAP_EQPNEIGHBORLOSS 5
540 #define SNMP_TRAP_ENTSPECIFIC 6
544 #define SNMP_INTEGER 1 /* l */
545 #define SNMP_OCTETSTR 2 /* c */
546 #define SNMP_DISPLAYSTR 2 /* c */
547 #define SNMP_OBJECTID 3 /* ul */
548 #define SNMP_IPADDR 4 /* uc */
549 #define SNMP_COUNTER 5 /* ul */
550 #define SNMP_GAUGE 6 /* ul */
551 #define SNMP_TIMETICKS 7 /* ul */
552 #define SNMP_OPAQUE 8 /* c */
554 /* Additional SNMPv2 Types */
555 #define SNMP_UINTEGER 5 /* ul */
556 #define SNMP_BITSTR 9 /* uc */
557 #define SNMP_NSAP 10 /* uc */
558 #define SNMP_COUNTER64 11 /* ul */
559 #define SNMP_NOSUCHOBJECT 12
560 #define SNMP_NOSUCHINSTANCE 13
561 #define SNMP_ENDOFMIBVIEW 14
565 unsigned char uc
[0]; /* 8 bit unsigned */
566 char c
[0]; /* 8 bit signed */
567 unsigned long ul
[0]; /* 32 bit unsigned */
568 long l
[0]; /* 32 bit signed */
576 unsigned int syntax_len
;
577 union snmp_syntax syntax
;
583 unsigned int error_status
;
584 unsigned int error_index
;
591 unsigned long ip_address
; /* pointer */
592 unsigned int general
;
593 unsigned int specific
;
610 static inline void mangle_address(unsigned char *begin
,
612 const struct oct1_map
*map
,
621 static struct snmp_cnv snmp_conv
[] =
623 {ASN1_UNI
, ASN1_NUL
, SNMP_NULL
},
624 {ASN1_UNI
, ASN1_INT
, SNMP_INTEGER
},
625 {ASN1_UNI
, ASN1_OTS
, SNMP_OCTETSTR
},
626 {ASN1_UNI
, ASN1_OTS
, SNMP_DISPLAYSTR
},
627 {ASN1_UNI
, ASN1_OJI
, SNMP_OBJECTID
},
628 {ASN1_APL
, SNMP_IPA
, SNMP_IPADDR
},
629 {ASN1_APL
, SNMP_CNT
, SNMP_COUNTER
}, /* Counter32 */
630 {ASN1_APL
, SNMP_GGE
, SNMP_GAUGE
}, /* Gauge32 == Unsigned32 */
631 {ASN1_APL
, SNMP_TIT
, SNMP_TIMETICKS
},
632 {ASN1_APL
, SNMP_OPQ
, SNMP_OPAQUE
},
634 /* SNMPv2 data types and errors */
635 {ASN1_UNI
, ASN1_BTS
, SNMP_BITSTR
},
636 {ASN1_APL
, SNMP_C64
, SNMP_COUNTER64
},
637 {ASN1_CTX
, SERR_NSO
, SNMP_NOSUCHOBJECT
},
638 {ASN1_CTX
, SERR_NSI
, SNMP_NOSUCHINSTANCE
},
639 {ASN1_CTX
, SERR_EOM
, SNMP_ENDOFMIBVIEW
},
643 static unsigned char snmp_tag_cls2syntax(unsigned int tag
,
645 unsigned short *syntax
)
647 struct snmp_cnv
*cnv
;
651 while (cnv
->syntax
!= -1) {
652 if (cnv
->tag
== tag
&& cnv
->class == cls
) {
653 *syntax
= cnv
->syntax
;
661 static unsigned char snmp_object_decode(struct asn1_ctx
*ctx
,
662 struct snmp_object
**obj
)
664 unsigned int cls
, con
, tag
, len
, idlen
;
666 unsigned char *eoc
, *end
, *p
;
667 unsigned long *lp
, *id
;
674 if (!asn1_header_decode(ctx
, &eoc
, &cls
, &con
, &tag
))
677 if (cls
!= ASN1_UNI
|| con
!= ASN1_CON
|| tag
!= ASN1_SEQ
)
680 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
683 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_OJI
)
686 if (!asn1_oid_decode(ctx
, end
, &id
, &idlen
))
689 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
)) {
694 if (con
!= ASN1_PRI
) {
700 if (!snmp_tag_cls2syntax(tag
, cls
, &type
)) {
709 if (!asn1_long_decode(ctx
, end
, &l
)) {
713 *obj
= kmalloc(sizeof(struct snmp_object
) + len
,
718 printk("OOM in bsalg (%d)\n", __LINE__
);
721 (*obj
)->syntax
.l
[0] = l
;
725 if (!asn1_octets_decode(ctx
, end
, &p
, &len
)) {
729 *obj
= kmalloc(sizeof(struct snmp_object
) + len
,
734 printk("OOM in bsalg (%d)\n", __LINE__
);
737 memcpy((*obj
)->syntax
.c
, p
, len
);
741 case SNMP_NOSUCHOBJECT
:
742 case SNMP_NOSUCHINSTANCE
:
743 case SNMP_ENDOFMIBVIEW
:
745 *obj
= kmalloc(sizeof(struct snmp_object
), GFP_ATOMIC
);
749 printk("OOM in bsalg (%d)\n", __LINE__
);
752 if (!asn1_null_decode(ctx
, end
)) {
760 if (!asn1_oid_decode(ctx
, end
, (unsigned long **)&lp
, &len
)) {
764 len
*= sizeof(unsigned long);
765 *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(__sum16
*csum
,
874 const unsigned char *optr
,
875 const unsigned char *nptr
,
890 *csum
= csum_fold(csum_partial(s
, 4, ~csum_unfold(*csum
)));
895 * - begin points to the start of the snmp messgae
896 * - addr points to the start of the address
898 static inline void mangle_address(unsigned char *begin
,
900 const struct oct1_map
*map
,
903 if (map
->from
== NOCT1(addr
)) {
907 memcpy(&old
, (unsigned char *)addr
, sizeof(old
));
911 /* Update UDP checksum if being used */
914 &map
->from
, &map
->to
, addr
- begin
);
919 printk(KERN_DEBUG
"bsalg: mapped %u.%u.%u.%u to "
920 "%u.%u.%u.%u\n", NIPQUAD(old
), NIPQUAD(*addr
));
924 static unsigned char snmp_trap_decode(struct asn1_ctx
*ctx
,
925 struct snmp_v1_trap
*trap
,
926 const struct oct1_map
*map
,
929 unsigned int cls
, con
, tag
, len
;
932 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
935 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_OJI
)
938 if (!asn1_oid_decode(ctx
, end
, &trap
->id
, &trap
->id_len
))
941 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
944 if (!((cls
== ASN1_APL
&& con
== ASN1_PRI
&& tag
== SNMP_IPA
) ||
945 (cls
== ASN1_UNI
&& con
== ASN1_PRI
&& tag
== ASN1_OTS
)))
948 if (!asn1_octets_decode(ctx
, end
, (unsigned char **)&trap
->ip_address
, &len
))
955 mangle_address(ctx
->begin
, ctx
->pointer
- 4, map
, check
);
957 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
960 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
963 if (!asn1_uint_decode(ctx
, end
, &trap
->general
))
966 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
969 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
972 if (!asn1_uint_decode(ctx
, end
, &trap
->specific
))
975 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
978 if (!((cls
== ASN1_APL
&& con
== ASN1_PRI
&& tag
== SNMP_TIT
) ||
979 (cls
== ASN1_UNI
&& con
== ASN1_PRI
&& tag
== ASN1_INT
)))
982 if (!asn1_ulong_decode(ctx
, end
, &trap
->time
))
988 kfree((unsigned long *)trap
->ip_address
);
996 /*****************************************************************************
1000 *****************************************************************************/
1002 static void hex_dump(unsigned char *buf
, size_t len
)
1006 for (i
= 0; i
< len
; i
++) {
1009 printk("%02x ", *(buf
+ i
));
1015 * Parse and mangle SNMP message according to mapping.
1016 * (And this is the fucking 'basic' method).
1018 static int snmp_parse_mangle(unsigned char *msg
,
1020 const struct oct1_map
*map
,
1023 unsigned char *eoc
, *end
;
1024 unsigned int cls
, con
, tag
, vers
, pdutype
;
1025 struct asn1_ctx ctx
;
1026 struct asn1_octstr comm
;
1027 struct snmp_object
**obj
;
1032 asn1_open(&ctx
, msg
, len
);
1035 * Start of SNMP message.
1037 if (!asn1_header_decode(&ctx
, &eoc
, &cls
, &con
, &tag
))
1039 if (cls
!= ASN1_UNI
|| con
!= ASN1_CON
|| tag
!= ASN1_SEQ
)
1043 * Version 1 or 2 handled.
1045 if (!asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
))
1047 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
1049 if (!asn1_uint_decode (&ctx
, end
, &vers
))
1052 printk(KERN_DEBUG
"bsalg: snmp version: %u\n", vers
+ 1);
1059 if (!asn1_header_decode (&ctx
, &end
, &cls
, &con
, &tag
))
1061 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_OTS
)
1063 if (!asn1_octets_decode(&ctx
, end
, &comm
.data
, &comm
.len
))
1068 printk(KERN_DEBUG
"bsalg: community: ");
1069 for (i
= 0; i
< comm
.len
; i
++)
1070 printk("%c", comm
.data
[i
]);
1078 if (!asn1_header_decode(&ctx
, &eoc
, &cls
, &con
, &pdutype
))
1080 if (cls
!= ASN1_CTX
|| con
!= ASN1_CON
)
1083 unsigned char *pdus
[] = {
1084 [SNMP_PDU_GET
] = "get",
1085 [SNMP_PDU_NEXT
] = "get-next",
1086 [SNMP_PDU_RESPONSE
] = "response",
1087 [SNMP_PDU_SET
] = "set",
1088 [SNMP_PDU_TRAP1
] = "trapv1",
1089 [SNMP_PDU_BULK
] = "bulk",
1090 [SNMP_PDU_INFORM
] = "inform",
1091 [SNMP_PDU_TRAP2
] = "trapv2"
1094 if (pdutype
> SNMP_PDU_TRAP2
)
1095 printk(KERN_DEBUG
"bsalg: bad pdu type %u\n", pdutype
);
1097 printk(KERN_DEBUG
"bsalg: pdu: %s\n", pdus
[pdutype
]);
1099 if (pdutype
!= SNMP_PDU_RESPONSE
&&
1100 pdutype
!= SNMP_PDU_TRAP1
&& pdutype
!= SNMP_PDU_TRAP2
)
1104 * Request header or v1 trap
1106 if (pdutype
== SNMP_PDU_TRAP1
) {
1107 struct snmp_v1_trap trap
;
1108 unsigned char ret
= snmp_trap_decode(&ctx
, &trap
, map
, check
);
1112 kfree((unsigned long *)trap
.ip_address
);
1117 struct snmp_request req
;
1119 if (!snmp_request_decode(&ctx
, &req
))
1123 printk(KERN_DEBUG
"bsalg: request: id=0x%lx error_status=%u "
1124 "error_index=%u\n", req
.id
, req
.error_status
,
1129 * Loop through objects, look for IP addresses to mangle.
1131 if (!asn1_header_decode(&ctx
, &eoc
, &cls
, &con
, &tag
))
1134 if (cls
!= ASN1_UNI
|| con
!= ASN1_CON
|| tag
!= ASN1_SEQ
)
1137 obj
= kmalloc(sizeof(struct snmp_object
), GFP_ATOMIC
);
1139 if (net_ratelimit())
1140 printk(KERN_WARNING
"OOM in bsalg(%d)\n", __LINE__
);
1144 while (!asn1_eoc_decode(&ctx
, eoc
)) {
1147 if (!snmp_object_decode(&ctx
, obj
)) {
1157 printk(KERN_DEBUG
"bsalg: object: ");
1158 for (i
= 0; i
< (*obj
)->id_len
; i
++) {
1161 printk("%lu", (*obj
)->id
[i
]);
1163 printk(": type=%u\n", (*obj
)->type
);
1167 if ((*obj
)->type
== SNMP_IPADDR
)
1168 mangle_address(ctx
.begin
, ctx
.pointer
- 4 , map
, check
);
1175 if (!asn1_eoc_decode(&ctx
, eoc
))
1181 /*****************************************************************************
1185 *****************************************************************************/
1188 * SNMP translation routine.
1190 static int snmp_translate(struct nf_conn
*ct
,
1191 enum ip_conntrack_info ctinfo
,
1192 struct sk_buff
*skb
)
1194 struct iphdr
*iph
= ip_hdr(skb
);
1195 struct udphdr
*udph
= (struct udphdr
*)((__be32
*)iph
+ iph
->ihl
);
1196 u_int16_t udplen
= ntohs(udph
->len
);
1197 u_int16_t paylen
= udplen
- sizeof(struct udphdr
);
1198 int dir
= CTINFO2DIR(ctinfo
);
1199 struct oct1_map map
;
1202 * Determine mappping for application layer addresses based
1203 * on NAT manipulations for the packet.
1205 if (dir
== IP_CT_DIR_ORIGINAL
) {
1207 map
.from
= NOCT1(&ct
->tuplehash
[dir
].tuple
.src
.u3
.ip
);
1208 map
.to
= NOCT1(&ct
->tuplehash
[!dir
].tuple
.dst
.u3
.ip
);
1211 map
.from
= NOCT1(&ct
->tuplehash
[dir
].tuple
.src
.u3
.ip
);
1212 map
.to
= NOCT1(&ct
->tuplehash
[!dir
].tuple
.dst
.u3
.ip
);
1215 if (map
.from
== map
.to
)
1218 if (!snmp_parse_mangle((unsigned char *)udph
+ sizeof(struct udphdr
),
1219 paylen
, &map
, &udph
->check
)) {
1220 if (net_ratelimit())
1221 printk(KERN_WARNING
"bsalg: parser failed\n");
1227 /* We don't actually set up expectations, just adjust internal IP
1228 * addresses if this is being NATted */
1229 static int help(struct sk_buff
*skb
, unsigned int protoff
,
1231 enum ip_conntrack_info ctinfo
)
1233 int dir
= CTINFO2DIR(ctinfo
);
1235 struct iphdr
*iph
= ip_hdr(skb
);
1236 struct udphdr
*udph
= (struct udphdr
*)((u_int32_t
*)iph
+ iph
->ihl
);
1238 /* SNMP replies and originating SNMP traps get mangled */
1239 if (udph
->source
== htons(SNMP_PORT
) && dir
!= IP_CT_DIR_REPLY
)
1241 if (udph
->dest
== htons(SNMP_TRAP_PORT
) && dir
!= IP_CT_DIR_ORIGINAL
)
1245 if (!(ct
->status
& IPS_NAT_MASK
))
1249 * Make sure the packet length is ok. So far, we were only guaranteed
1250 * to have a valid length IP header plus 8 bytes, which means we have
1251 * enough room for a UDP header. Just verify the UDP length field so we
1252 * can mess around with the payload.
1254 if (ntohs(udph
->len
) != skb
->len
- (iph
->ihl
<< 2)) {
1255 if (net_ratelimit())
1256 printk(KERN_WARNING
"SNMP: dropping malformed packet "
1257 "src=%u.%u.%u.%u dst=%u.%u.%u.%u\n",
1258 NIPQUAD(iph
->saddr
), NIPQUAD(iph
->daddr
));
1262 if (!skb_make_writable(skb
, skb
->len
))
1265 spin_lock_bh(&snmp_lock
);
1266 ret
= snmp_translate(ct
, ctinfo
, skb
);
1267 spin_unlock_bh(&snmp_lock
);
1271 static const struct nf_conntrack_expect_policy snmp_exp_policy
= {
1276 static struct nf_conntrack_helper snmp_helper __read_mostly
= {
1279 .expect_policy
= &snmp_exp_policy
,
1281 .tuple
.src
.l3num
= AF_INET
,
1282 .tuple
.src
.u
.udp
.port
= __constant_htons(SNMP_PORT
),
1283 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1284 .mask
.src
.l3num
= 0xFFFF,
1285 .mask
.src
.u
.udp
.port
= __constant_htons(0xFFFF),
1286 .mask
.dst
.protonum
= 0xFF,
1289 static struct nf_conntrack_helper snmp_trap_helper __read_mostly
= {
1292 .expect_policy
= &snmp_exp_policy
,
1293 .name
= "snmp_trap",
1294 .tuple
.src
.l3num
= AF_INET
,
1295 .tuple
.src
.u
.udp
.port
= __constant_htons(SNMP_TRAP_PORT
),
1296 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1297 .mask
.src
.l3num
= 0xFFFF,
1298 .mask
.src
.u
.udp
.port
= __constant_htons(0xFFFF),
1299 .mask
.dst
.protonum
= 0xFF,
1302 /*****************************************************************************
1306 *****************************************************************************/
1308 static int __init
nf_nat_snmp_basic_init(void)
1312 ret
= nf_conntrack_helper_register(&snmp_helper
);
1315 ret
= nf_conntrack_helper_register(&snmp_trap_helper
);
1317 nf_conntrack_helper_unregister(&snmp_helper
);
1323 static void __exit
nf_nat_snmp_basic_fini(void)
1325 nf_conntrack_helper_unregister(&snmp_helper
);
1326 nf_conntrack_helper_unregister(&snmp_trap_helper
);
1329 module_init(nf_nat_snmp_basic_init
);
1330 module_exit(nf_nat_snmp_basic_fini
);
1332 module_param(debug
, int, 0600);