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>
46 #include <linux/slab.h>
49 #include <linux/udp.h>
50 #include <net/checksum.h>
53 #include <net/netfilter/nf_nat.h>
54 #include <net/netfilter/nf_conntrack_expect.h>
55 #include <net/netfilter/nf_conntrack_helper.h>
56 #include <net/netfilter/nf_nat_helper.h>
58 MODULE_LICENSE("GPL");
59 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
60 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
61 MODULE_ALIAS("ip_nat_snmp_basic");
64 #define SNMP_TRAP_PORT 162
65 #define NOCT1(n) (*(u8 *)(n))
68 static DEFINE_SPINLOCK(snmp_lock
);
71 * Application layer address mapping mimics the NAT mapping, but
72 * only for the first octet in this case (a more flexible system
73 * can be implemented if needed).
82 /*****************************************************************************
84 * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
86 *****************************************************************************/
89 #define ASN1_UNI 0 /* Universal */
90 #define ASN1_APL 1 /* Application */
91 #define ASN1_CTX 2 /* Context */
92 #define ASN1_PRV 3 /* Private */
95 #define ASN1_EOC 0 /* End Of Contents */
96 #define ASN1_BOL 1 /* Boolean */
97 #define ASN1_INT 2 /* Integer */
98 #define ASN1_BTS 3 /* Bit String */
99 #define ASN1_OTS 4 /* Octet String */
100 #define ASN1_NUL 5 /* Null */
101 #define ASN1_OJI 6 /* Object Identifier */
102 #define ASN1_OJD 7 /* Object Description */
103 #define ASN1_EXT 8 /* External */
104 #define ASN1_SEQ 16 /* Sequence */
105 #define ASN1_SET 17 /* Set */
106 #define ASN1_NUMSTR 18 /* Numerical String */
107 #define ASN1_PRNSTR 19 /* Printable String */
108 #define ASN1_TEXSTR 20 /* Teletext String */
109 #define ASN1_VIDSTR 21 /* Video String */
110 #define ASN1_IA5STR 22 /* IA5 String */
111 #define ASN1_UNITIM 23 /* Universal Time */
112 #define ASN1_GENTIM 24 /* General Time */
113 #define ASN1_GRASTR 25 /* Graphical String */
114 #define ASN1_VISSTR 26 /* Visible String */
115 #define ASN1_GENSTR 27 /* General String */
117 /* Primitive / Constructed methods*/
118 #define ASN1_PRI 0 /* Primitive */
119 #define ASN1_CON 1 /* Constructed */
124 #define ASN1_ERR_NOERROR 0
125 #define ASN1_ERR_DEC_EMPTY 2
126 #define ASN1_ERR_DEC_EOC_MISMATCH 3
127 #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
128 #define ASN1_ERR_DEC_BADVALUE 5
135 int error
; /* Error condition */
136 unsigned char *pointer
; /* Octet just to be decoded */
137 unsigned char *begin
; /* First octet */
138 unsigned char *end
; /* Octet after last octet */
142 * Octet string (not null terminated)
150 static void asn1_open(struct asn1_ctx
*ctx
,
155 ctx
->end
= buf
+ len
;
157 ctx
->error
= ASN1_ERR_NOERROR
;
160 static unsigned char asn1_octet_decode(struct asn1_ctx
*ctx
, unsigned char *ch
)
162 if (ctx
->pointer
>= ctx
->end
) {
163 ctx
->error
= ASN1_ERR_DEC_EMPTY
;
166 *ch
= *(ctx
->pointer
)++;
170 static unsigned char asn1_tag_decode(struct asn1_ctx
*ctx
, unsigned int *tag
)
178 if (!asn1_octet_decode(ctx
, &ch
))
182 } while ((ch
& 0x80) == 0x80);
186 static unsigned char asn1_id_decode(struct asn1_ctx
*ctx
,
193 if (!asn1_octet_decode(ctx
, &ch
))
196 *cls
= (ch
& 0xC0) >> 6;
197 *con
= (ch
& 0x20) >> 5;
201 if (!asn1_tag_decode(ctx
, tag
))
207 static unsigned char asn1_length_decode(struct asn1_ctx
*ctx
,
211 unsigned char ch
, cnt
;
213 if (!asn1_octet_decode(ctx
, &ch
))
228 if (!asn1_octet_decode(ctx
, &ch
))
237 /* don't trust len bigger than ctx buffer */
238 if (*len
> ctx
->end
- ctx
->pointer
)
244 static unsigned char asn1_header_decode(struct asn1_ctx
*ctx
,
250 unsigned int def
, len
;
252 if (!asn1_id_decode(ctx
, cls
, con
, tag
))
256 if (!asn1_length_decode(ctx
, &def
, &len
))
259 /* primitive shall be definite, indefinite shall be constructed */
260 if (*con
== ASN1_PRI
&& !def
)
264 *eoc
= ctx
->pointer
+ len
;
270 static unsigned char asn1_eoc_decode(struct asn1_ctx
*ctx
, unsigned char *eoc
)
275 if (!asn1_octet_decode(ctx
, &ch
))
279 ctx
->error
= ASN1_ERR_DEC_EOC_MISMATCH
;
283 if (!asn1_octet_decode(ctx
, &ch
))
287 ctx
->error
= ASN1_ERR_DEC_EOC_MISMATCH
;
292 if (ctx
->pointer
!= eoc
) {
293 ctx
->error
= ASN1_ERR_DEC_LENGTH_MISMATCH
;
300 static unsigned char asn1_null_decode(struct asn1_ctx
*ctx
, unsigned char *eoc
)
306 static unsigned char asn1_long_decode(struct asn1_ctx
*ctx
,
313 if (!asn1_octet_decode(ctx
, &ch
))
316 *integer
= (signed char) ch
;
319 while (ctx
->pointer
< eoc
) {
320 if (++len
> sizeof (long)) {
321 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
325 if (!asn1_octet_decode(ctx
, &ch
))
334 static unsigned char asn1_uint_decode(struct asn1_ctx
*ctx
,
336 unsigned int *integer
)
341 if (!asn1_octet_decode(ctx
, &ch
))
345 if (ch
== 0) len
= 0;
348 while (ctx
->pointer
< eoc
) {
349 if (++len
> sizeof (unsigned int)) {
350 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
354 if (!asn1_octet_decode(ctx
, &ch
))
363 static unsigned char asn1_ulong_decode(struct asn1_ctx
*ctx
,
365 unsigned long *integer
)
370 if (!asn1_octet_decode(ctx
, &ch
))
374 if (ch
== 0) len
= 0;
377 while (ctx
->pointer
< eoc
) {
378 if (++len
> sizeof (unsigned long)) {
379 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
383 if (!asn1_octet_decode(ctx
, &ch
))
392 static unsigned char asn1_octets_decode(struct asn1_ctx
*ctx
,
394 unsigned char **octets
,
401 *octets
= kmalloc(eoc
- ctx
->pointer
, GFP_ATOMIC
);
402 if (*octets
== NULL
) {
404 printk("OOM in bsalg (%d)\n", __LINE__
);
409 while (ctx
->pointer
< eoc
) {
410 if (!asn1_octet_decode(ctx
, (unsigned char *)ptr
++)) {
420 static unsigned char asn1_subid_decode(struct asn1_ctx
*ctx
,
421 unsigned long *subid
)
428 if (!asn1_octet_decode(ctx
, &ch
))
433 } while ((ch
& 0x80) == 0x80);
437 static unsigned char asn1_oid_decode(struct asn1_ctx
*ctx
,
446 size
= eoc
- ctx
->pointer
+ 1;
448 /* first subid actually encodes first two subids */
449 if (size
< 2 || size
> ULONG_MAX
/sizeof(unsigned long))
452 *oid
= kmalloc(size
* sizeof(unsigned long), GFP_ATOMIC
);
455 printk("OOM in bsalg (%d)\n", __LINE__
);
461 if (!asn1_subid_decode(ctx
, &subid
)) {
470 } else if (subid
< 80) {
472 optr
[1] = subid
- 40;
475 optr
[1] = subid
- 80;
481 while (ctx
->pointer
< eoc
) {
482 if (++(*len
) > size
) {
483 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
489 if (!asn1_subid_decode(ctx
, optr
++)) {
498 /*****************************************************************************
500 * SNMP decoding routines (gxsnmp author Dirk Wisse)
502 *****************************************************************************/
511 #define SNMP_SIZE_COMM 256
512 #define SNMP_SIZE_OBJECTID 128
513 #define SNMP_SIZE_BUFCHR 256
514 #define SNMP_SIZE_BUFINT 128
515 #define SNMP_SIZE_SMALLOBJECTID 16
518 #define SNMP_PDU_GET 0
519 #define SNMP_PDU_NEXT 1
520 #define SNMP_PDU_RESPONSE 2
521 #define SNMP_PDU_SET 3
522 #define SNMP_PDU_TRAP1 4
523 #define SNMP_PDU_BULK 5
524 #define SNMP_PDU_INFORM 6
525 #define SNMP_PDU_TRAP2 7
528 #define SNMP_NOERROR 0
529 #define SNMP_TOOBIG 1
530 #define SNMP_NOSUCHNAME 2
531 #define SNMP_BADVALUE 3
532 #define SNMP_READONLY 4
533 #define SNMP_GENERROR 5
534 #define SNMP_NOACCESS 6
535 #define SNMP_WRONGTYPE 7
536 #define SNMP_WRONGLENGTH 8
537 #define SNMP_WRONGENCODING 9
538 #define SNMP_WRONGVALUE 10
539 #define SNMP_NOCREATION 11
540 #define SNMP_INCONSISTENTVALUE 12
541 #define SNMP_RESOURCEUNAVAILABLE 13
542 #define SNMP_COMMITFAILED 14
543 #define SNMP_UNDOFAILED 15
544 #define SNMP_AUTHORIZATIONERROR 16
545 #define SNMP_NOTWRITABLE 17
546 #define SNMP_INCONSISTENTNAME 18
548 /* General SNMP V1 Traps */
549 #define SNMP_TRAP_COLDSTART 0
550 #define SNMP_TRAP_WARMSTART 1
551 #define SNMP_TRAP_LINKDOWN 2
552 #define SNMP_TRAP_LINKUP 3
553 #define SNMP_TRAP_AUTFAILURE 4
554 #define SNMP_TRAP_EQPNEIGHBORLOSS 5
555 #define SNMP_TRAP_ENTSPECIFIC 6
559 #define SNMP_INTEGER 1 /* l */
560 #define SNMP_OCTETSTR 2 /* c */
561 #define SNMP_DISPLAYSTR 2 /* c */
562 #define SNMP_OBJECTID 3 /* ul */
563 #define SNMP_IPADDR 4 /* uc */
564 #define SNMP_COUNTER 5 /* ul */
565 #define SNMP_GAUGE 6 /* ul */
566 #define SNMP_TIMETICKS 7 /* ul */
567 #define SNMP_OPAQUE 8 /* c */
569 /* Additional SNMPv2 Types */
570 #define SNMP_UINTEGER 5 /* ul */
571 #define SNMP_BITSTR 9 /* uc */
572 #define SNMP_NSAP 10 /* uc */
573 #define SNMP_COUNTER64 11 /* ul */
574 #define SNMP_NOSUCHOBJECT 12
575 #define SNMP_NOSUCHINSTANCE 13
576 #define SNMP_ENDOFMIBVIEW 14
580 unsigned char uc
[0]; /* 8 bit unsigned */
581 char c
[0]; /* 8 bit signed */
582 unsigned long ul
[0]; /* 32 bit unsigned */
583 long l
[0]; /* 32 bit signed */
591 unsigned int syntax_len
;
592 union snmp_syntax syntax
;
598 unsigned int error_status
;
599 unsigned int error_index
;
606 unsigned long ip_address
; /* pointer */
607 unsigned int general
;
608 unsigned int specific
;
625 static inline void mangle_address(unsigned char *begin
,
627 const struct oct1_map
*map
,
636 static const struct snmp_cnv snmp_conv
[] = {
637 {ASN1_UNI
, ASN1_NUL
, SNMP_NULL
},
638 {ASN1_UNI
, ASN1_INT
, SNMP_INTEGER
},
639 {ASN1_UNI
, ASN1_OTS
, SNMP_OCTETSTR
},
640 {ASN1_UNI
, ASN1_OTS
, SNMP_DISPLAYSTR
},
641 {ASN1_UNI
, ASN1_OJI
, SNMP_OBJECTID
},
642 {ASN1_APL
, SNMP_IPA
, SNMP_IPADDR
},
643 {ASN1_APL
, SNMP_CNT
, SNMP_COUNTER
}, /* Counter32 */
644 {ASN1_APL
, SNMP_GGE
, SNMP_GAUGE
}, /* Gauge32 == Unsigned32 */
645 {ASN1_APL
, SNMP_TIT
, SNMP_TIMETICKS
},
646 {ASN1_APL
, SNMP_OPQ
, SNMP_OPAQUE
},
648 /* SNMPv2 data types and errors */
649 {ASN1_UNI
, ASN1_BTS
, SNMP_BITSTR
},
650 {ASN1_APL
, SNMP_C64
, SNMP_COUNTER64
},
651 {ASN1_CTX
, SERR_NSO
, SNMP_NOSUCHOBJECT
},
652 {ASN1_CTX
, SERR_NSI
, SNMP_NOSUCHINSTANCE
},
653 {ASN1_CTX
, SERR_EOM
, SNMP_ENDOFMIBVIEW
},
657 static unsigned char snmp_tag_cls2syntax(unsigned int tag
,
659 unsigned short *syntax
)
661 const struct snmp_cnv
*cnv
;
665 while (cnv
->syntax
!= -1) {
666 if (cnv
->tag
== tag
&& cnv
->class == cls
) {
667 *syntax
= cnv
->syntax
;
675 static unsigned char snmp_object_decode(struct asn1_ctx
*ctx
,
676 struct snmp_object
**obj
)
678 unsigned int cls
, con
, tag
, len
, idlen
;
680 unsigned char *eoc
, *end
, *p
;
681 unsigned long *lp
, *id
;
688 if (!asn1_header_decode(ctx
, &eoc
, &cls
, &con
, &tag
))
691 if (cls
!= ASN1_UNI
|| con
!= ASN1_CON
|| tag
!= ASN1_SEQ
)
694 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
697 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_OJI
)
700 if (!asn1_oid_decode(ctx
, end
, &id
, &idlen
))
703 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
)) {
708 if (con
!= ASN1_PRI
) {
714 if (!snmp_tag_cls2syntax(tag
, cls
, &type
)) {
723 if (!asn1_long_decode(ctx
, end
, &l
)) {
727 *obj
= kmalloc(sizeof(struct snmp_object
) + len
,
732 printk("OOM in bsalg (%d)\n", __LINE__
);
735 (*obj
)->syntax
.l
[0] = l
;
739 if (!asn1_octets_decode(ctx
, end
, &p
, &len
)) {
743 *obj
= kmalloc(sizeof(struct snmp_object
) + len
,
749 printk("OOM in bsalg (%d)\n", __LINE__
);
752 memcpy((*obj
)->syntax
.c
, p
, len
);
756 case SNMP_NOSUCHOBJECT
:
757 case SNMP_NOSUCHINSTANCE
:
758 case SNMP_ENDOFMIBVIEW
:
760 *obj
= kmalloc(sizeof(struct snmp_object
), GFP_ATOMIC
);
764 printk("OOM in bsalg (%d)\n", __LINE__
);
767 if (!asn1_null_decode(ctx
, end
)) {
775 if (!asn1_oid_decode(ctx
, end
, (unsigned long **)&lp
, &len
)) {
779 len
*= sizeof(unsigned long);
780 *obj
= kmalloc(sizeof(struct snmp_object
) + len
, GFP_ATOMIC
);
785 printk("OOM in bsalg (%d)\n", __LINE__
);
788 memcpy((*obj
)->syntax
.ul
, lp
, len
);
792 if (!asn1_octets_decode(ctx
, end
, &p
, &len
)) {
801 *obj
= kmalloc(sizeof(struct snmp_object
) + len
, GFP_ATOMIC
);
806 printk("OOM in bsalg (%d)\n", __LINE__
);
809 memcpy((*obj
)->syntax
.uc
, p
, len
);
815 len
= sizeof(unsigned long);
816 if (!asn1_ulong_decode(ctx
, end
, &ul
)) {
820 *obj
= kmalloc(sizeof(struct snmp_object
) + len
, GFP_ATOMIC
);
824 printk("OOM in bsalg (%d)\n", __LINE__
);
827 (*obj
)->syntax
.ul
[0] = ul
;
834 (*obj
)->syntax_len
= len
;
837 (*obj
)->id_len
= idlen
;
839 if (!asn1_eoc_decode(ctx
, eoc
)) {
848 static unsigned char snmp_request_decode(struct asn1_ctx
*ctx
,
849 struct snmp_request
*request
)
851 unsigned int cls
, con
, tag
;
854 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
857 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
860 if (!asn1_ulong_decode(ctx
, end
, &request
->id
))
863 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
866 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
869 if (!asn1_uint_decode(ctx
, end
, &request
->error_status
))
872 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
875 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
878 if (!asn1_uint_decode(ctx
, end
, &request
->error_index
))
885 * Fast checksum update for possibly oddly-aligned UDP byte, from the
886 * code example in the draft.
888 static void fast_csum(__sum16
*csum
,
889 const unsigned char *optr
,
890 const unsigned char *nptr
,
905 *csum
= csum_fold(csum_partial(s
, 4, ~csum_unfold(*csum
)));
910 * - begin points to the start of the snmp messgae
911 * - addr points to the start of the address
913 static inline void mangle_address(unsigned char *begin
,
915 const struct oct1_map
*map
,
918 if (map
->from
== NOCT1(addr
)) {
922 memcpy(&old
, addr
, sizeof(old
));
926 /* Update UDP checksum if being used */
929 &map
->from
, &map
->to
, addr
- begin
);
934 printk(KERN_DEBUG
"bsalg: mapped %pI4 to %pI4\n",
939 static unsigned char snmp_trap_decode(struct asn1_ctx
*ctx
,
940 struct snmp_v1_trap
*trap
,
941 const struct oct1_map
*map
,
944 unsigned int cls
, con
, tag
, len
;
947 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
950 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_OJI
)
953 if (!asn1_oid_decode(ctx
, end
, &trap
->id
, &trap
->id_len
))
956 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
959 if (!((cls
== ASN1_APL
&& con
== ASN1_PRI
&& tag
== SNMP_IPA
) ||
960 (cls
== ASN1_UNI
&& con
== ASN1_PRI
&& tag
== ASN1_OTS
)))
963 if (!asn1_octets_decode(ctx
, end
, (unsigned char **)&trap
->ip_address
, &len
))
970 mangle_address(ctx
->begin
, ctx
->pointer
- 4, map
, check
);
972 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
975 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
978 if (!asn1_uint_decode(ctx
, end
, &trap
->general
))
981 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
984 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
987 if (!asn1_uint_decode(ctx
, end
, &trap
->specific
))
990 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
993 if (!((cls
== ASN1_APL
&& con
== ASN1_PRI
&& tag
== SNMP_TIT
) ||
994 (cls
== ASN1_UNI
&& con
== ASN1_PRI
&& tag
== ASN1_INT
)))
997 if (!asn1_ulong_decode(ctx
, end
, &trap
->time
))
1003 kfree((unsigned long *)trap
->ip_address
);
1011 /*****************************************************************************
1015 *****************************************************************************/
1017 static void hex_dump(const unsigned char *buf
, size_t len
)
1021 for (i
= 0; i
< len
; i
++) {
1024 printk("%02x ", *(buf
+ i
));
1030 * Parse and mangle SNMP message according to mapping.
1031 * (And this is the fucking 'basic' method).
1033 static int snmp_parse_mangle(unsigned char *msg
,
1035 const struct oct1_map
*map
,
1038 unsigned char *eoc
, *end
;
1039 unsigned int cls
, con
, tag
, vers
, pdutype
;
1040 struct asn1_ctx ctx
;
1041 struct asn1_octstr comm
;
1042 struct snmp_object
*obj
;
1047 asn1_open(&ctx
, msg
, len
);
1050 * Start of SNMP message.
1052 if (!asn1_header_decode(&ctx
, &eoc
, &cls
, &con
, &tag
))
1054 if (cls
!= ASN1_UNI
|| con
!= ASN1_CON
|| tag
!= ASN1_SEQ
)
1058 * Version 1 or 2 handled.
1060 if (!asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
))
1062 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
1064 if (!asn1_uint_decode (&ctx
, end
, &vers
))
1067 printk(KERN_DEBUG
"bsalg: snmp version: %u\n", vers
+ 1);
1074 if (!asn1_header_decode (&ctx
, &end
, &cls
, &con
, &tag
))
1076 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_OTS
)
1078 if (!asn1_octets_decode(&ctx
, end
, &comm
.data
, &comm
.len
))
1083 printk(KERN_DEBUG
"bsalg: community: ");
1084 for (i
= 0; i
< comm
.len
; i
++)
1085 printk("%c", comm
.data
[i
]);
1093 if (!asn1_header_decode(&ctx
, &eoc
, &cls
, &con
, &pdutype
))
1095 if (cls
!= ASN1_CTX
|| con
!= ASN1_CON
)
1098 static const unsigned char *const pdus
[] = {
1099 [SNMP_PDU_GET
] = "get",
1100 [SNMP_PDU_NEXT
] = "get-next",
1101 [SNMP_PDU_RESPONSE
] = "response",
1102 [SNMP_PDU_SET
] = "set",
1103 [SNMP_PDU_TRAP1
] = "trapv1",
1104 [SNMP_PDU_BULK
] = "bulk",
1105 [SNMP_PDU_INFORM
] = "inform",
1106 [SNMP_PDU_TRAP2
] = "trapv2"
1109 if (pdutype
> SNMP_PDU_TRAP2
)
1110 printk(KERN_DEBUG
"bsalg: bad pdu type %u\n", pdutype
);
1112 printk(KERN_DEBUG
"bsalg: pdu: %s\n", pdus
[pdutype
]);
1114 if (pdutype
!= SNMP_PDU_RESPONSE
&&
1115 pdutype
!= SNMP_PDU_TRAP1
&& pdutype
!= SNMP_PDU_TRAP2
)
1119 * Request header or v1 trap
1121 if (pdutype
== SNMP_PDU_TRAP1
) {
1122 struct snmp_v1_trap trap
;
1123 unsigned char ret
= snmp_trap_decode(&ctx
, &trap
, map
, check
);
1127 kfree((unsigned long *)trap
.ip_address
);
1132 struct snmp_request req
;
1134 if (!snmp_request_decode(&ctx
, &req
))
1138 printk(KERN_DEBUG
"bsalg: request: id=0x%lx error_status=%u "
1139 "error_index=%u\n", req
.id
, req
.error_status
,
1144 * Loop through objects, look for IP addresses to mangle.
1146 if (!asn1_header_decode(&ctx
, &eoc
, &cls
, &con
, &tag
))
1149 if (cls
!= ASN1_UNI
|| con
!= ASN1_CON
|| tag
!= ASN1_SEQ
)
1152 while (!asn1_eoc_decode(&ctx
, eoc
)) {
1155 if (!snmp_object_decode(&ctx
, &obj
)) {
1164 printk(KERN_DEBUG
"bsalg: object: ");
1165 for (i
= 0; i
< obj
->id_len
; i
++) {
1168 printk("%lu", obj
->id
[i
]);
1170 printk(": type=%u\n", obj
->type
);
1174 if (obj
->type
== SNMP_IPADDR
)
1175 mangle_address(ctx
.begin
, ctx
.pointer
- 4 , map
, check
);
1181 if (!asn1_eoc_decode(&ctx
, eoc
))
1187 /*****************************************************************************
1191 *****************************************************************************/
1194 * SNMP translation routine.
1196 static int snmp_translate(struct nf_conn
*ct
,
1197 enum ip_conntrack_info ctinfo
,
1198 struct sk_buff
*skb
)
1200 struct iphdr
*iph
= ip_hdr(skb
);
1201 struct udphdr
*udph
= (struct udphdr
*)((__be32
*)iph
+ iph
->ihl
);
1202 u_int16_t udplen
= ntohs(udph
->len
);
1203 u_int16_t paylen
= udplen
- sizeof(struct udphdr
);
1204 int dir
= CTINFO2DIR(ctinfo
);
1205 struct oct1_map map
;
1208 * Determine mappping for application layer addresses based
1209 * on NAT manipulations for the packet.
1211 if (dir
== IP_CT_DIR_ORIGINAL
) {
1213 map
.from
= NOCT1(&ct
->tuplehash
[dir
].tuple
.src
.u3
.ip
);
1214 map
.to
= NOCT1(&ct
->tuplehash
[!dir
].tuple
.dst
.u3
.ip
);
1217 map
.from
= NOCT1(&ct
->tuplehash
[dir
].tuple
.src
.u3
.ip
);
1218 map
.to
= NOCT1(&ct
->tuplehash
[!dir
].tuple
.dst
.u3
.ip
);
1221 if (map
.from
== map
.to
)
1224 if (!snmp_parse_mangle((unsigned char *)udph
+ sizeof(struct udphdr
),
1225 paylen
, &map
, &udph
->check
)) {
1226 if (net_ratelimit())
1227 printk(KERN_WARNING
"bsalg: parser failed\n");
1233 /* We don't actually set up expectations, just adjust internal IP
1234 * addresses if this is being NATted */
1235 static int help(struct sk_buff
*skb
, unsigned int protoff
,
1237 enum ip_conntrack_info ctinfo
)
1239 int dir
= CTINFO2DIR(ctinfo
);
1241 const struct iphdr
*iph
= ip_hdr(skb
);
1242 const struct udphdr
*udph
= (struct udphdr
*)((__be32
*)iph
+ iph
->ihl
);
1244 /* SNMP replies and originating SNMP traps get mangled */
1245 if (udph
->source
== htons(SNMP_PORT
) && dir
!= IP_CT_DIR_REPLY
)
1247 if (udph
->dest
== htons(SNMP_TRAP_PORT
) && dir
!= IP_CT_DIR_ORIGINAL
)
1251 if (!(ct
->status
& IPS_NAT_MASK
))
1255 * Make sure the packet length is ok. So far, we were only guaranteed
1256 * to have a valid length IP header plus 8 bytes, which means we have
1257 * enough room for a UDP header. Just verify the UDP length field so we
1258 * can mess around with the payload.
1260 if (ntohs(udph
->len
) != skb
->len
- (iph
->ihl
<< 2)) {
1261 if (net_ratelimit())
1262 printk(KERN_WARNING
"SNMP: dropping malformed packet src=%pI4 dst=%pI4\n",
1263 &iph
->saddr
, &iph
->daddr
);
1267 if (!skb_make_writable(skb
, skb
->len
))
1270 spin_lock_bh(&snmp_lock
);
1271 ret
= snmp_translate(ct
, ctinfo
, skb
);
1272 spin_unlock_bh(&snmp_lock
);
1276 static const struct nf_conntrack_expect_policy snmp_exp_policy
= {
1281 static struct nf_conntrack_helper snmp_helper __read_mostly
= {
1284 .expect_policy
= &snmp_exp_policy
,
1286 .tuple
.src
.l3num
= AF_INET
,
1287 .tuple
.src
.u
.udp
.port
= cpu_to_be16(SNMP_PORT
),
1288 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1291 static struct nf_conntrack_helper snmp_trap_helper __read_mostly
= {
1294 .expect_policy
= &snmp_exp_policy
,
1295 .name
= "snmp_trap",
1296 .tuple
.src
.l3num
= AF_INET
,
1297 .tuple
.src
.u
.udp
.port
= cpu_to_be16(SNMP_TRAP_PORT
),
1298 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1301 /*****************************************************************************
1305 *****************************************************************************/
1307 static int __init
nf_nat_snmp_basic_init(void)
1311 ret
= nf_conntrack_helper_register(&snmp_helper
);
1314 ret
= nf_conntrack_helper_register(&snmp_trap_helper
);
1316 nf_conntrack_helper_unregister(&snmp_helper
);
1322 static void __exit
nf_nat_snmp_basic_fini(void)
1324 nf_conntrack_helper_unregister(&snmp_helper
);
1325 nf_conntrack_helper_unregister(&snmp_trap_helper
);
1328 module_init(nf_nat_snmp_basic_init
);
1329 module_exit(nf_nat_snmp_basic_fini
);
1331 module_param(debug
, int, 0600);