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 * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
44 #include <linux/module.h>
45 #include <linux/moduleparam.h>
46 #include <linux/types.h>
47 #include <linux/kernel.h>
48 #include <linux/slab.h>
51 #include <linux/udp.h>
52 #include <net/checksum.h>
55 #include <net/netfilter/nf_nat.h>
56 #include <net/netfilter/nf_conntrack_expect.h>
57 #include <net/netfilter/nf_conntrack_helper.h>
58 #include <net/netfilter/nf_nat_helper.h>
59 #include <linux/netfilter/nf_conntrack_snmp.h>
61 MODULE_LICENSE("GPL");
62 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
63 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
64 MODULE_ALIAS("ip_nat_snmp_basic");
67 #define SNMP_TRAP_PORT 162
68 #define NOCT1(n) (*(u8 *)(n))
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
))
231 if (!asn1_octet_decode(ctx
, &ch
))
240 /* don't trust len bigger than ctx buffer */
241 if (*len
> ctx
->end
- ctx
->pointer
)
247 static unsigned char asn1_header_decode(struct asn1_ctx
*ctx
,
253 unsigned int def
, len
;
255 if (!asn1_id_decode(ctx
, cls
, con
, tag
))
259 if (!asn1_length_decode(ctx
, &def
, &len
))
262 /* primitive shall be definite, indefinite shall be constructed */
263 if (*con
== ASN1_PRI
&& !def
)
267 *eoc
= ctx
->pointer
+ len
;
273 static unsigned char asn1_eoc_decode(struct asn1_ctx
*ctx
, unsigned char *eoc
)
278 if (!asn1_octet_decode(ctx
, &ch
))
282 ctx
->error
= ASN1_ERR_DEC_EOC_MISMATCH
;
286 if (!asn1_octet_decode(ctx
, &ch
))
290 ctx
->error
= ASN1_ERR_DEC_EOC_MISMATCH
;
295 if (ctx
->pointer
!= eoc
) {
296 ctx
->error
= ASN1_ERR_DEC_LENGTH_MISMATCH
;
303 static unsigned char asn1_null_decode(struct asn1_ctx
*ctx
, unsigned char *eoc
)
309 static unsigned char asn1_long_decode(struct asn1_ctx
*ctx
,
316 if (!asn1_octet_decode(ctx
, &ch
))
319 *integer
= (signed char) ch
;
322 while (ctx
->pointer
< eoc
) {
323 if (++len
> sizeof (long)) {
324 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
328 if (!asn1_octet_decode(ctx
, &ch
))
337 static unsigned char asn1_uint_decode(struct asn1_ctx
*ctx
,
339 unsigned int *integer
)
344 if (!asn1_octet_decode(ctx
, &ch
))
348 if (ch
== 0) len
= 0;
351 while (ctx
->pointer
< eoc
) {
352 if (++len
> sizeof (unsigned int)) {
353 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
357 if (!asn1_octet_decode(ctx
, &ch
))
366 static unsigned char asn1_ulong_decode(struct asn1_ctx
*ctx
,
368 unsigned long *integer
)
373 if (!asn1_octet_decode(ctx
, &ch
))
377 if (ch
== 0) len
= 0;
380 while (ctx
->pointer
< eoc
) {
381 if (++len
> sizeof (unsigned long)) {
382 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
386 if (!asn1_octet_decode(ctx
, &ch
))
395 static unsigned char asn1_octets_decode(struct asn1_ctx
*ctx
,
397 unsigned char **octets
,
404 *octets
= kmalloc(eoc
- ctx
->pointer
, GFP_ATOMIC
);
409 while (ctx
->pointer
< eoc
) {
410 if (!asn1_octet_decode(ctx
, 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
);
458 if (!asn1_subid_decode(ctx
, &subid
)) {
467 } else if (subid
< 80) {
469 optr
[1] = subid
- 40;
472 optr
[1] = subid
- 80;
478 while (ctx
->pointer
< eoc
) {
479 if (++(*len
) > size
) {
480 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
486 if (!asn1_subid_decode(ctx
, optr
++)) {
495 /*****************************************************************************
497 * SNMP decoding routines (gxsnmp author Dirk Wisse)
499 *****************************************************************************/
508 #define SNMP_SIZE_COMM 256
509 #define SNMP_SIZE_OBJECTID 128
510 #define SNMP_SIZE_BUFCHR 256
511 #define SNMP_SIZE_BUFINT 128
512 #define SNMP_SIZE_SMALLOBJECTID 16
515 #define SNMP_PDU_GET 0
516 #define SNMP_PDU_NEXT 1
517 #define SNMP_PDU_RESPONSE 2
518 #define SNMP_PDU_SET 3
519 #define SNMP_PDU_TRAP1 4
520 #define SNMP_PDU_BULK 5
521 #define SNMP_PDU_INFORM 6
522 #define SNMP_PDU_TRAP2 7
525 #define SNMP_NOERROR 0
526 #define SNMP_TOOBIG 1
527 #define SNMP_NOSUCHNAME 2
528 #define SNMP_BADVALUE 3
529 #define SNMP_READONLY 4
530 #define SNMP_GENERROR 5
531 #define SNMP_NOACCESS 6
532 #define SNMP_WRONGTYPE 7
533 #define SNMP_WRONGLENGTH 8
534 #define SNMP_WRONGENCODING 9
535 #define SNMP_WRONGVALUE 10
536 #define SNMP_NOCREATION 11
537 #define SNMP_INCONSISTENTVALUE 12
538 #define SNMP_RESOURCEUNAVAILABLE 13
539 #define SNMP_COMMITFAILED 14
540 #define SNMP_UNDOFAILED 15
541 #define SNMP_AUTHORIZATIONERROR 16
542 #define SNMP_NOTWRITABLE 17
543 #define SNMP_INCONSISTENTNAME 18
545 /* General SNMP V1 Traps */
546 #define SNMP_TRAP_COLDSTART 0
547 #define SNMP_TRAP_WARMSTART 1
548 #define SNMP_TRAP_LINKDOWN 2
549 #define SNMP_TRAP_LINKUP 3
550 #define SNMP_TRAP_AUTFAILURE 4
551 #define SNMP_TRAP_EQPNEIGHBORLOSS 5
552 #define SNMP_TRAP_ENTSPECIFIC 6
556 #define SNMP_INTEGER 1 /* l */
557 #define SNMP_OCTETSTR 2 /* c */
558 #define SNMP_DISPLAYSTR 2 /* c */
559 #define SNMP_OBJECTID 3 /* ul */
560 #define SNMP_IPADDR 4 /* uc */
561 #define SNMP_COUNTER 5 /* ul */
562 #define SNMP_GAUGE 6 /* ul */
563 #define SNMP_TIMETICKS 7 /* ul */
564 #define SNMP_OPAQUE 8 /* c */
566 /* Additional SNMPv2 Types */
567 #define SNMP_UINTEGER 5 /* ul */
568 #define SNMP_BITSTR 9 /* uc */
569 #define SNMP_NSAP 10 /* uc */
570 #define SNMP_COUNTER64 11 /* ul */
571 #define SNMP_NOSUCHOBJECT 12
572 #define SNMP_NOSUCHINSTANCE 13
573 #define SNMP_ENDOFMIBVIEW 14
577 unsigned char uc
[0]; /* 8 bit unsigned */
578 char c
[0]; /* 8 bit signed */
579 unsigned long ul
[0]; /* 32 bit unsigned */
580 long l
[0]; /* 32 bit signed */
588 unsigned int syntax_len
;
589 union snmp_syntax syntax
;
595 unsigned int error_status
;
596 unsigned int error_index
;
603 unsigned long ip_address
; /* pointer */
604 unsigned int general
;
605 unsigned int specific
;
622 static inline void mangle_address(unsigned char *begin
,
624 const struct oct1_map
*map
,
633 static const struct snmp_cnv snmp_conv
[] = {
634 {ASN1_UNI
, ASN1_NUL
, SNMP_NULL
},
635 {ASN1_UNI
, ASN1_INT
, SNMP_INTEGER
},
636 {ASN1_UNI
, ASN1_OTS
, SNMP_OCTETSTR
},
637 {ASN1_UNI
, ASN1_OTS
, SNMP_DISPLAYSTR
},
638 {ASN1_UNI
, ASN1_OJI
, SNMP_OBJECTID
},
639 {ASN1_APL
, SNMP_IPA
, SNMP_IPADDR
},
640 {ASN1_APL
, SNMP_CNT
, SNMP_COUNTER
}, /* Counter32 */
641 {ASN1_APL
, SNMP_GGE
, SNMP_GAUGE
}, /* Gauge32 == Unsigned32 */
642 {ASN1_APL
, SNMP_TIT
, SNMP_TIMETICKS
},
643 {ASN1_APL
, SNMP_OPQ
, SNMP_OPAQUE
},
645 /* SNMPv2 data types and errors */
646 {ASN1_UNI
, ASN1_BTS
, SNMP_BITSTR
},
647 {ASN1_APL
, SNMP_C64
, SNMP_COUNTER64
},
648 {ASN1_CTX
, SERR_NSO
, SNMP_NOSUCHOBJECT
},
649 {ASN1_CTX
, SERR_NSI
, SNMP_NOSUCHINSTANCE
},
650 {ASN1_CTX
, SERR_EOM
, SNMP_ENDOFMIBVIEW
},
654 static unsigned char snmp_tag_cls2syntax(unsigned int tag
,
656 unsigned short *syntax
)
658 const struct snmp_cnv
*cnv
;
662 while (cnv
->syntax
!= -1) {
663 if (cnv
->tag
== tag
&& cnv
->class == cls
) {
664 *syntax
= cnv
->syntax
;
672 static unsigned char snmp_object_decode(struct asn1_ctx
*ctx
,
673 struct snmp_object
**obj
)
675 unsigned int cls
, con
, tag
, len
, idlen
;
677 unsigned char *eoc
, *end
, *p
;
678 unsigned long *lp
, *id
;
685 if (!asn1_header_decode(ctx
, &eoc
, &cls
, &con
, &tag
))
688 if (cls
!= ASN1_UNI
|| con
!= ASN1_CON
|| tag
!= ASN1_SEQ
)
691 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
694 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_OJI
)
697 if (!asn1_oid_decode(ctx
, end
, &id
, &idlen
))
700 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
)) {
705 if (con
!= ASN1_PRI
) {
711 if (!snmp_tag_cls2syntax(tag
, cls
, &type
)) {
720 if (!asn1_long_decode(ctx
, end
, &l
)) {
724 *obj
= kmalloc(sizeof(struct snmp_object
) + len
, GFP_ATOMIC
);
729 (*obj
)->syntax
.l
[0] = l
;
733 if (!asn1_octets_decode(ctx
, end
, &p
, &len
)) {
737 *obj
= kmalloc(sizeof(struct snmp_object
) + len
, GFP_ATOMIC
);
743 memcpy((*obj
)->syntax
.c
, p
, len
);
747 case SNMP_NOSUCHOBJECT
:
748 case SNMP_NOSUCHINSTANCE
:
749 case SNMP_ENDOFMIBVIEW
:
751 *obj
= kmalloc(sizeof(struct snmp_object
), GFP_ATOMIC
);
756 if (!asn1_null_decode(ctx
, end
)) {
764 if (!asn1_oid_decode(ctx
, end
, &lp
, &len
)) {
768 len
*= sizeof(unsigned long);
769 *obj
= kmalloc(sizeof(struct snmp_object
) + len
, GFP_ATOMIC
);
775 memcpy((*obj
)->syntax
.ul
, lp
, len
);
779 if (!asn1_octets_decode(ctx
, end
, &p
, &len
)) {
788 *obj
= kmalloc(sizeof(struct snmp_object
) + len
, GFP_ATOMIC
);
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
);
810 (*obj
)->syntax
.ul
[0] = ul
;
817 (*obj
)->syntax_len
= len
;
820 (*obj
)->id_len
= idlen
;
822 if (!asn1_eoc_decode(ctx
, eoc
)) {
831 static unsigned char snmp_request_decode(struct asn1_ctx
*ctx
,
832 struct snmp_request
*request
)
834 unsigned int cls
, con
, tag
;
837 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
840 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
843 if (!asn1_ulong_decode(ctx
, end
, &request
->id
))
846 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
849 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
852 if (!asn1_uint_decode(ctx
, end
, &request
->error_status
))
855 if (!asn1_header_decode(ctx
, &end
, &cls
, &con
, &tag
))
858 if (cls
!= ASN1_UNI
|| con
!= ASN1_PRI
|| tag
!= ASN1_INT
)
861 if (!asn1_uint_decode(ctx
, end
, &request
->error_index
))
868 * Fast checksum update for possibly oddly-aligned UDP byte, from the
869 * code example in the draft.
871 static void fast_csum(__sum16
*csum
,
872 const unsigned char *optr
,
873 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
, addr
, sizeof(old
));
911 /* Update UDP checksum if being used */
914 &map
->from
, &map
->to
, addr
- begin
);
919 printk(KERN_DEBUG
"bsalg: mapped %pI4 to %pI4\n",
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(const 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 static const unsigned char *const 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 while (!asn1_eoc_decode(&ctx
, eoc
)) {
1140 if (!snmp_object_decode(&ctx
, &obj
)) {
1149 printk(KERN_DEBUG
"bsalg: object: ");
1150 for (i
= 0; i
< obj
->id_len
; i
++) {
1153 printk("%lu", obj
->id
[i
]);
1155 printk(": type=%u\n", obj
->type
);
1159 if (obj
->type
== SNMP_IPADDR
)
1160 mangle_address(ctx
.begin
, ctx
.pointer
- 4 , map
, check
);
1166 if (!asn1_eoc_decode(&ctx
, eoc
))
1172 /*****************************************************************************
1176 *****************************************************************************/
1179 * SNMP translation routine.
1181 static int snmp_translate(struct nf_conn
*ct
,
1182 enum ip_conntrack_info ctinfo
,
1183 struct sk_buff
*skb
)
1185 struct iphdr
*iph
= ip_hdr(skb
);
1186 struct udphdr
*udph
= (struct udphdr
*)((__be32
*)iph
+ iph
->ihl
);
1187 u_int16_t udplen
= ntohs(udph
->len
);
1188 u_int16_t paylen
= udplen
- sizeof(struct udphdr
);
1189 int dir
= CTINFO2DIR(ctinfo
);
1190 struct oct1_map map
;
1193 * Determine mappping for application layer addresses based
1194 * on NAT manipulations for the packet.
1196 if (dir
== IP_CT_DIR_ORIGINAL
) {
1198 map
.from
= NOCT1(&ct
->tuplehash
[dir
].tuple
.src
.u3
.ip
);
1199 map
.to
= NOCT1(&ct
->tuplehash
[!dir
].tuple
.dst
.u3
.ip
);
1202 map
.from
= NOCT1(&ct
->tuplehash
[dir
].tuple
.src
.u3
.ip
);
1203 map
.to
= NOCT1(&ct
->tuplehash
[!dir
].tuple
.dst
.u3
.ip
);
1206 if (map
.from
== map
.to
)
1209 if (!snmp_parse_mangle((unsigned char *)udph
+ sizeof(struct udphdr
),
1210 paylen
, &map
, &udph
->check
)) {
1211 net_warn_ratelimited("bsalg: parser failed\n");
1217 /* We don't actually set up expectations, just adjust internal IP
1218 * addresses if this is being NATted */
1219 static int help(struct sk_buff
*skb
, unsigned int protoff
,
1221 enum ip_conntrack_info ctinfo
)
1223 int dir
= CTINFO2DIR(ctinfo
);
1225 const struct iphdr
*iph
= ip_hdr(skb
);
1226 const struct udphdr
*udph
= (struct udphdr
*)((__be32
*)iph
+ iph
->ihl
);
1228 /* SNMP replies and originating SNMP traps get mangled */
1229 if (udph
->source
== htons(SNMP_PORT
) && dir
!= IP_CT_DIR_REPLY
)
1231 if (udph
->dest
== htons(SNMP_TRAP_PORT
) && dir
!= IP_CT_DIR_ORIGINAL
)
1235 if (!(ct
->status
& IPS_NAT_MASK
))
1239 * Make sure the packet length is ok. So far, we were only guaranteed
1240 * to have a valid length IP header plus 8 bytes, which means we have
1241 * enough room for a UDP header. Just verify the UDP length field so we
1242 * can mess around with the payload.
1244 if (ntohs(udph
->len
) != skb
->len
- (iph
->ihl
<< 2)) {
1245 net_warn_ratelimited("SNMP: dropping malformed packet src=%pI4 dst=%pI4\n",
1246 &iph
->saddr
, &iph
->daddr
);
1250 if (!skb_make_writable(skb
, skb
->len
))
1253 spin_lock_bh(&snmp_lock
);
1254 ret
= snmp_translate(ct
, ctinfo
, skb
);
1255 spin_unlock_bh(&snmp_lock
);
1259 static const struct nf_conntrack_expect_policy snmp_exp_policy
= {
1264 static struct nf_conntrack_helper snmp_helper __read_mostly
= {
1267 .expect_policy
= &snmp_exp_policy
,
1269 .tuple
.src
.l3num
= AF_INET
,
1270 .tuple
.src
.u
.udp
.port
= cpu_to_be16(SNMP_PORT
),
1271 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1274 static struct nf_conntrack_helper snmp_trap_helper __read_mostly
= {
1277 .expect_policy
= &snmp_exp_policy
,
1278 .name
= "snmp_trap",
1279 .tuple
.src
.l3num
= AF_INET
,
1280 .tuple
.src
.u
.udp
.port
= cpu_to_be16(SNMP_TRAP_PORT
),
1281 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1284 /*****************************************************************************
1288 *****************************************************************************/
1290 static int __init
nf_nat_snmp_basic_init(void)
1294 BUG_ON(nf_nat_snmp_hook
!= NULL
);
1295 RCU_INIT_POINTER(nf_nat_snmp_hook
, help
);
1297 ret
= nf_conntrack_helper_register(&snmp_trap_helper
);
1299 nf_conntrack_helper_unregister(&snmp_helper
);
1305 static void __exit
nf_nat_snmp_basic_fini(void)
1307 RCU_INIT_POINTER(nf_nat_snmp_hook
, NULL
);
1308 nf_conntrack_helper_unregister(&snmp_trap_helper
);
1311 module_init(nf_nat_snmp_basic_init
);
1312 module_exit(nf_nat_snmp_basic_fini
);
1314 module_param(debug
, int, 0600);