[PATCH] NETFILTER: SNMP NAT: fix memory corruption (CVE-2006-2444)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / netfilter / ip_nat_snmp_basic.c
blobdf57e7a117dbb4c46605e44dc349ca818e58171e
1 /*
2 * ip_nat_snmp_basic.c
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
21 * RFC 2962
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 * Updates:
43 * 2000-08-06: Convert to new helper API (Harald Welte).
46 #include <linux/config.h>
47 #include <linux/in.h>
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/kernel.h>
51 #include <linux/moduleparam.h>
52 #include <linux/netfilter_ipv4.h>
53 #include <linux/netfilter_ipv4/ip_nat.h>
54 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
55 #include <linux/netfilter_ipv4/ip_nat_helper.h>
56 #include <linux/ip.h>
57 #include <linux/udp.h>
58 #include <net/checksum.h>
59 #include <net/udp.h>
60 #include <asm/uaccess.h>
62 MODULE_LICENSE("GPL");
63 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
64 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
66 #define SNMP_PORT 161
67 #define SNMP_TRAP_PORT 162
68 #define NOCT1(n) (u_int8_t )((n) & 0xff)
70 static int debug;
71 static DEFINE_SPINLOCK(snmp_lock);
73 /*
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).
78 struct oct1_map
80 u_int8_t from;
81 u_int8_t to;
85 /*****************************************************************************
87 * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
89 *****************************************************************************/
91 /* Class */
92 #define ASN1_UNI 0 /* Universal */
93 #define ASN1_APL 1 /* Application */
94 #define ASN1_CTX 2 /* Context */
95 #define ASN1_PRV 3 /* Private */
97 /* Tag */
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 */
125 * Error codes.
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
134 * ASN.1 context.
136 struct asn1_ctx
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)
147 struct asn1_octstr
149 unsigned char *data;
150 unsigned int len;
153 static void asn1_open(struct asn1_ctx *ctx,
154 unsigned char *buf,
155 unsigned int len)
157 ctx->begin = buf;
158 ctx->end = buf + len;
159 ctx->pointer = buf;
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;
167 return 0;
169 *ch = *(ctx->pointer)++;
170 return 1;
173 static unsigned char asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
175 unsigned char ch;
177 *tag = 0;
181 if (!asn1_octet_decode(ctx, &ch))
182 return 0;
183 *tag <<= 7;
184 *tag |= ch & 0x7F;
185 } while ((ch & 0x80) == 0x80);
186 return 1;
189 static unsigned char asn1_id_decode(struct asn1_ctx *ctx,
190 unsigned int *cls,
191 unsigned int *con,
192 unsigned int *tag)
194 unsigned char ch;
196 if (!asn1_octet_decode(ctx, &ch))
197 return 0;
199 *cls = (ch & 0xC0) >> 6;
200 *con = (ch & 0x20) >> 5;
201 *tag = (ch & 0x1F);
203 if (*tag == 0x1F) {
204 if (!asn1_tag_decode(ctx, tag))
205 return 0;
207 return 1;
210 static unsigned char asn1_length_decode(struct asn1_ctx *ctx,
211 unsigned int *def,
212 unsigned int *len)
214 unsigned char ch, cnt;
216 if (!asn1_octet_decode(ctx, &ch))
217 return 0;
219 if (ch == 0x80)
220 *def = 0;
221 else {
222 *def = 1;
224 if (ch < 0x80)
225 *len = ch;
226 else {
227 cnt = (unsigned char) (ch & 0x7F);
228 *len = 0;
230 while (cnt > 0) {
231 if (!asn1_octet_decode(ctx, &ch))
232 return 0;
233 *len <<= 8;
234 *len |= ch;
235 cnt--;
239 return 1;
242 static unsigned char asn1_header_decode(struct asn1_ctx *ctx,
243 unsigned char **eoc,
244 unsigned int *cls,
245 unsigned int *con,
246 unsigned int *tag)
248 unsigned int def, len;
250 if (!asn1_id_decode(ctx, cls, con, tag))
251 return 0;
253 if (!asn1_length_decode(ctx, &def, &len))
254 return 0;
256 if (def)
257 *eoc = ctx->pointer + len;
258 else
259 *eoc = NULL;
260 return 1;
263 static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
265 unsigned char ch;
267 if (eoc == 0) {
268 if (!asn1_octet_decode(ctx, &ch))
269 return 0;
271 if (ch != 0x00) {
272 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
273 return 0;
276 if (!asn1_octet_decode(ctx, &ch))
277 return 0;
279 if (ch != 0x00) {
280 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
281 return 0;
283 return 1;
284 } else {
285 if (ctx->pointer != eoc) {
286 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
287 return 0;
289 return 1;
293 static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
295 ctx->pointer = eoc;
296 return 1;
299 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
300 unsigned char *eoc,
301 long *integer)
303 unsigned char ch;
304 unsigned int len;
306 if (!asn1_octet_decode(ctx, &ch))
307 return 0;
309 *integer = (signed char) ch;
310 len = 1;
312 while (ctx->pointer < eoc) {
313 if (++len > sizeof (long)) {
314 ctx->error = ASN1_ERR_DEC_BADVALUE;
315 return 0;
318 if (!asn1_octet_decode(ctx, &ch))
319 return 0;
321 *integer <<= 8;
322 *integer |= ch;
324 return 1;
327 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
328 unsigned char *eoc,
329 unsigned int *integer)
331 unsigned char ch;
332 unsigned int len;
334 if (!asn1_octet_decode(ctx, &ch))
335 return 0;
337 *integer = ch;
338 if (ch == 0) len = 0;
339 else len = 1;
341 while (ctx->pointer < eoc) {
342 if (++len > sizeof (unsigned int)) {
343 ctx->error = ASN1_ERR_DEC_BADVALUE;
344 return 0;
347 if (!asn1_octet_decode(ctx, &ch))
348 return 0;
350 *integer <<= 8;
351 *integer |= ch;
353 return 1;
356 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
357 unsigned char *eoc,
358 unsigned long *integer)
360 unsigned char ch;
361 unsigned int len;
363 if (!asn1_octet_decode(ctx, &ch))
364 return 0;
366 *integer = ch;
367 if (ch == 0) len = 0;
368 else len = 1;
370 while (ctx->pointer < eoc) {
371 if (++len > sizeof (unsigned long)) {
372 ctx->error = ASN1_ERR_DEC_BADVALUE;
373 return 0;
376 if (!asn1_octet_decode(ctx, &ch))
377 return 0;
379 *integer <<= 8;
380 *integer |= ch;
382 return 1;
385 static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
386 unsigned char *eoc,
387 unsigned char **octets,
388 unsigned int *len)
390 unsigned char *ptr;
392 *len = 0;
394 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
395 if (*octets == NULL) {
396 if (net_ratelimit())
397 printk("OOM in bsalg (%d)\n", __LINE__);
398 return 0;
401 ptr = *octets;
402 while (ctx->pointer < eoc) {
403 if (!asn1_octet_decode(ctx, (unsigned char *)ptr++)) {
404 kfree(*octets);
405 *octets = NULL;
406 return 0;
408 (*len)++;
410 return 1;
413 static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
414 unsigned long *subid)
416 unsigned char ch;
418 *subid = 0;
420 do {
421 if (!asn1_octet_decode(ctx, &ch))
422 return 0;
424 *subid <<= 7;
425 *subid |= ch & 0x7F;
426 } while ((ch & 0x80) == 0x80);
427 return 1;
430 static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
431 unsigned char *eoc,
432 unsigned long **oid,
433 unsigned int *len)
435 unsigned long subid;
436 unsigned int size;
437 unsigned long *optr;
439 size = eoc - ctx->pointer + 1;
440 *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
441 if (*oid == NULL) {
442 if (net_ratelimit())
443 printk("OOM in bsalg (%d)\n", __LINE__);
444 return 0;
447 optr = *oid;
449 if (!asn1_subid_decode(ctx, &subid)) {
450 kfree(*oid);
451 *oid = NULL;
452 return 0;
455 if (subid < 40) {
456 optr [0] = 0;
457 optr [1] = subid;
458 } else if (subid < 80) {
459 optr [0] = 1;
460 optr [1] = subid - 40;
461 } else {
462 optr [0] = 2;
463 optr [1] = subid - 80;
466 *len = 2;
467 optr += 2;
469 while (ctx->pointer < eoc) {
470 if (++(*len) > size) {
471 ctx->error = ASN1_ERR_DEC_BADVALUE;
472 kfree(*oid);
473 *oid = NULL;
474 return 0;
477 if (!asn1_subid_decode(ctx, optr++)) {
478 kfree(*oid);
479 *oid = NULL;
480 return 0;
483 return 1;
486 /*****************************************************************************
488 * SNMP decoding routines (gxsnmp author Dirk Wisse)
490 *****************************************************************************/
492 /* SNMP Versions */
493 #define SNMP_V1 0
494 #define SNMP_V2C 1
495 #define SNMP_V2 2
496 #define SNMP_V3 3
498 /* Default Sizes */
499 #define SNMP_SIZE_COMM 256
500 #define SNMP_SIZE_OBJECTID 128
501 #define SNMP_SIZE_BUFCHR 256
502 #define SNMP_SIZE_BUFINT 128
503 #define SNMP_SIZE_SMALLOBJECTID 16
505 /* Requests */
506 #define SNMP_PDU_GET 0
507 #define SNMP_PDU_NEXT 1
508 #define SNMP_PDU_RESPONSE 2
509 #define SNMP_PDU_SET 3
510 #define SNMP_PDU_TRAP1 4
511 #define SNMP_PDU_BULK 5
512 #define SNMP_PDU_INFORM 6
513 #define SNMP_PDU_TRAP2 7
515 /* Errors */
516 #define SNMP_NOERROR 0
517 #define SNMP_TOOBIG 1
518 #define SNMP_NOSUCHNAME 2
519 #define SNMP_BADVALUE 3
520 #define SNMP_READONLY 4
521 #define SNMP_GENERROR 5
522 #define SNMP_NOACCESS 6
523 #define SNMP_WRONGTYPE 7
524 #define SNMP_WRONGLENGTH 8
525 #define SNMP_WRONGENCODING 9
526 #define SNMP_WRONGVALUE 10
527 #define SNMP_NOCREATION 11
528 #define SNMP_INCONSISTENTVALUE 12
529 #define SNMP_RESOURCEUNAVAILABLE 13
530 #define SNMP_COMMITFAILED 14
531 #define SNMP_UNDOFAILED 15
532 #define SNMP_AUTHORIZATIONERROR 16
533 #define SNMP_NOTWRITABLE 17
534 #define SNMP_INCONSISTENTNAME 18
536 /* General SNMP V1 Traps */
537 #define SNMP_TRAP_COLDSTART 0
538 #define SNMP_TRAP_WARMSTART 1
539 #define SNMP_TRAP_LINKDOWN 2
540 #define SNMP_TRAP_LINKUP 3
541 #define SNMP_TRAP_AUTFAILURE 4
542 #define SNMP_TRAP_EQPNEIGHBORLOSS 5
543 #define SNMP_TRAP_ENTSPECIFIC 6
545 /* SNMPv1 Types */
546 #define SNMP_NULL 0
547 #define SNMP_INTEGER 1 /* l */
548 #define SNMP_OCTETSTR 2 /* c */
549 #define SNMP_DISPLAYSTR 2 /* c */
550 #define SNMP_OBJECTID 3 /* ul */
551 #define SNMP_IPADDR 4 /* uc */
552 #define SNMP_COUNTER 5 /* ul */
553 #define SNMP_GAUGE 6 /* ul */
554 #define SNMP_TIMETICKS 7 /* ul */
555 #define SNMP_OPAQUE 8 /* c */
557 /* Additional SNMPv2 Types */
558 #define SNMP_UINTEGER 5 /* ul */
559 #define SNMP_BITSTR 9 /* uc */
560 #define SNMP_NSAP 10 /* uc */
561 #define SNMP_COUNTER64 11 /* ul */
562 #define SNMP_NOSUCHOBJECT 12
563 #define SNMP_NOSUCHINSTANCE 13
564 #define SNMP_ENDOFMIBVIEW 14
566 union snmp_syntax
568 unsigned char uc[0]; /* 8 bit unsigned */
569 char c[0]; /* 8 bit signed */
570 unsigned long ul[0]; /* 32 bit unsigned */
571 long l[0]; /* 32 bit signed */
574 struct snmp_object
576 unsigned long *id;
577 unsigned int id_len;
578 unsigned short type;
579 unsigned int syntax_len;
580 union snmp_syntax syntax;
583 struct snmp_request
585 unsigned long id;
586 unsigned int error_status;
587 unsigned int error_index;
590 struct snmp_v1_trap
592 unsigned long *id;
593 unsigned int id_len;
594 unsigned long ip_address; /* pointer */
595 unsigned int general;
596 unsigned int specific;
597 unsigned long time;
600 /* SNMP types */
601 #define SNMP_IPA 0
602 #define SNMP_CNT 1
603 #define SNMP_GGE 2
604 #define SNMP_TIT 3
605 #define SNMP_OPQ 4
606 #define SNMP_C64 6
608 /* SNMP errors */
609 #define SERR_NSO 0
610 #define SERR_NSI 1
611 #define SERR_EOM 2
613 static inline void mangle_address(unsigned char *begin,
614 unsigned char *addr,
615 const struct oct1_map *map,
616 u_int16_t *check);
617 struct snmp_cnv
619 unsigned int class;
620 unsigned int tag;
621 int syntax;
624 static struct snmp_cnv snmp_conv [] =
626 {ASN1_UNI, ASN1_NUL, SNMP_NULL},
627 {ASN1_UNI, ASN1_INT, SNMP_INTEGER},
628 {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR},
629 {ASN1_UNI, ASN1_OTS, SNMP_DISPLAYSTR},
630 {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID},
631 {ASN1_APL, SNMP_IPA, SNMP_IPADDR},
632 {ASN1_APL, SNMP_CNT, SNMP_COUNTER}, /* Counter32 */
633 {ASN1_APL, SNMP_GGE, SNMP_GAUGE}, /* Gauge32 == Unsigned32 */
634 {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS},
635 {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE},
637 /* SNMPv2 data types and errors */
638 {ASN1_UNI, ASN1_BTS, SNMP_BITSTR},
639 {ASN1_APL, SNMP_C64, SNMP_COUNTER64},
640 {ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT},
641 {ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE},
642 {ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW},
643 {0, 0, -1}
646 static unsigned char snmp_tag_cls2syntax(unsigned int tag,
647 unsigned int cls,
648 unsigned short *syntax)
650 struct snmp_cnv *cnv;
652 cnv = snmp_conv;
654 while (cnv->syntax != -1) {
655 if (cnv->tag == tag && cnv->class == cls) {
656 *syntax = cnv->syntax;
657 return 1;
659 cnv++;
661 return 0;
664 static unsigned char snmp_object_decode(struct asn1_ctx *ctx,
665 struct snmp_object **obj)
667 unsigned int cls, con, tag, len, idlen;
668 unsigned short type;
669 unsigned char *eoc, *end, *p;
670 unsigned long *lp, *id;
671 unsigned long ul;
672 long l;
674 *obj = NULL;
675 id = NULL;
677 if (!asn1_header_decode(ctx, &eoc, &cls, &con, &tag))
678 return 0;
680 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
681 return 0;
683 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
684 return 0;
686 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
687 return 0;
689 if (!asn1_oid_decode(ctx, end, &id, &idlen))
690 return 0;
692 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag)) {
693 kfree(id);
694 return 0;
697 if (con != ASN1_PRI) {
698 kfree(id);
699 return 0;
702 if (!snmp_tag_cls2syntax(tag, cls, &type)) {
703 kfree(id);
704 return 0;
707 switch (type) {
708 case SNMP_INTEGER:
709 len = sizeof(long);
710 if (!asn1_long_decode(ctx, end, &l)) {
711 kfree(id);
712 return 0;
714 *obj = kmalloc(sizeof(struct snmp_object) + len,
715 GFP_ATOMIC);
716 if (*obj == NULL) {
717 kfree(id);
718 if (net_ratelimit())
719 printk("OOM in bsalg (%d)\n", __LINE__);
720 return 0;
722 (*obj)->syntax.l[0] = l;
723 break;
724 case SNMP_OCTETSTR:
725 case SNMP_OPAQUE:
726 if (!asn1_octets_decode(ctx, end, &p, &len)) {
727 kfree(id);
728 return 0;
730 *obj = kmalloc(sizeof(struct snmp_object) + len,
731 GFP_ATOMIC);
732 if (*obj == NULL) {
733 kfree(id);
734 if (net_ratelimit())
735 printk("OOM in bsalg (%d)\n", __LINE__);
736 return 0;
738 memcpy((*obj)->syntax.c, p, len);
739 kfree(p);
740 break;
741 case SNMP_NULL:
742 case SNMP_NOSUCHOBJECT:
743 case SNMP_NOSUCHINSTANCE:
744 case SNMP_ENDOFMIBVIEW:
745 len = 0;
746 *obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
747 if (*obj == NULL) {
748 kfree(id);
749 if (net_ratelimit())
750 printk("OOM in bsalg (%d)\n", __LINE__);
751 return 0;
753 if (!asn1_null_decode(ctx, end)) {
754 kfree(id);
755 kfree(*obj);
756 *obj = NULL;
757 return 0;
759 break;
760 case SNMP_OBJECTID:
761 if (!asn1_oid_decode(ctx, end, (unsigned long **)&lp, &len)) {
762 kfree(id);
763 return 0;
765 len *= sizeof(unsigned long);
766 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
767 if (*obj == NULL) {
768 kfree(id);
769 if (net_ratelimit())
770 printk("OOM in bsalg (%d)\n", __LINE__);
771 return 0;
773 memcpy((*obj)->syntax.ul, lp, len);
774 kfree(lp);
775 break;
776 case SNMP_IPADDR:
777 if (!asn1_octets_decode(ctx, end, &p, &len)) {
778 kfree(id);
779 return 0;
781 if (len != 4) {
782 kfree(p);
783 kfree(id);
784 return 0;
786 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
787 if (*obj == NULL) {
788 kfree(p);
789 kfree(id);
790 if (net_ratelimit())
791 printk("OOM in bsalg (%d)\n", __LINE__);
792 return 0;
794 memcpy((*obj)->syntax.uc, p, len);
795 kfree(p);
796 break;
797 case SNMP_COUNTER:
798 case SNMP_GAUGE:
799 case SNMP_TIMETICKS:
800 len = sizeof(unsigned long);
801 if (!asn1_ulong_decode(ctx, end, &ul)) {
802 kfree(id);
803 return 0;
805 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
806 if (*obj == NULL) {
807 kfree(id);
808 if (net_ratelimit())
809 printk("OOM in bsalg (%d)\n", __LINE__);
810 return 0;
812 (*obj)->syntax.ul[0] = ul;
813 break;
814 default:
815 kfree(id);
816 return 0;
819 (*obj)->syntax_len = len;
820 (*obj)->type = type;
821 (*obj)->id = id;
822 (*obj)->id_len = idlen;
824 if (!asn1_eoc_decode(ctx, eoc)) {
825 kfree(id);
826 kfree(*obj);
827 *obj = NULL;
828 return 0;
830 return 1;
833 static unsigned char snmp_request_decode(struct asn1_ctx *ctx,
834 struct snmp_request *request)
836 unsigned int cls, con, tag;
837 unsigned char *end;
839 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
840 return 0;
842 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
843 return 0;
845 if (!asn1_ulong_decode(ctx, end, &request->id))
846 return 0;
848 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
849 return 0;
851 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
852 return 0;
854 if (!asn1_uint_decode(ctx, end, &request->error_status))
855 return 0;
857 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
858 return 0;
860 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
861 return 0;
863 if (!asn1_uint_decode(ctx, end, &request->error_index))
864 return 0;
866 return 1;
870 * Fast checksum update for possibly oddly-aligned UDP byte, from the
871 * code example in the draft.
873 static void fast_csum(unsigned char *csum,
874 const unsigned char *optr,
875 const unsigned char *nptr,
876 int odd)
878 long x, old, new;
880 x = csum[0] * 256 + csum[1];
882 x =~ x & 0xFFFF;
884 if (odd) old = optr[0] * 256;
885 else old = optr[0];
887 x -= old & 0xFFFF;
888 if (x <= 0) {
889 x--;
890 x &= 0xFFFF;
893 if (odd) new = nptr[0] * 256;
894 else new = nptr[0];
896 x += new & 0xFFFF;
897 if (x & 0x10000) {
898 x++;
899 x &= 0xFFFF;
902 x =~ x & 0xFFFF;
903 csum[0] = x / 256;
904 csum[1] = x & 0xFF;
908 * Mangle IP address.
909 * - begin points to the start of the snmp messgae
910 * - addr points to the start of the address
912 static inline void mangle_address(unsigned char *begin,
913 unsigned char *addr,
914 const struct oct1_map *map,
915 u_int16_t *check)
917 if (map->from == NOCT1(*addr)) {
918 u_int32_t old;
920 if (debug)
921 memcpy(&old, (unsigned char *)addr, sizeof(old));
923 *addr = map->to;
925 /* Update UDP checksum if being used */
926 if (*check) {
927 unsigned char odd = !((addr - begin) % 2);
929 fast_csum((unsigned char *)check,
930 &map->from, &map->to, odd);
934 if (debug)
935 printk(KERN_DEBUG "bsalg: mapped %u.%u.%u.%u to "
936 "%u.%u.%u.%u\n", NIPQUAD(old), NIPQUAD(*addr));
940 static unsigned char snmp_trap_decode(struct asn1_ctx *ctx,
941 struct snmp_v1_trap *trap,
942 const struct oct1_map *map,
943 u_int16_t *check)
945 unsigned int cls, con, tag, len;
946 unsigned char *end;
948 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
949 return 0;
951 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
952 return 0;
954 if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
955 return 0;
957 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
958 goto err_id_free;
960 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
961 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS)))
962 goto err_id_free;
964 if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
965 goto err_id_free;
967 /* IPv4 only */
968 if (len != 4)
969 goto err_addr_free;
971 mangle_address(ctx->begin, ctx->pointer - 4, map, check);
973 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
974 goto err_addr_free;
976 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
977 goto err_addr_free;
979 if (!asn1_uint_decode(ctx, end, &trap->general))
980 goto err_addr_free;
982 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
983 goto err_addr_free;
985 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
986 goto err_addr_free;
988 if (!asn1_uint_decode(ctx, end, &trap->specific))
989 goto err_addr_free;
991 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
992 goto err_addr_free;
994 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
995 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT)))
996 goto err_addr_free;
998 if (!asn1_ulong_decode(ctx, end, &trap->time))
999 goto err_addr_free;
1001 return 1;
1003 err_addr_free:
1004 kfree((unsigned long *)trap->ip_address);
1006 err_id_free:
1007 kfree(trap->id);
1009 return 0;
1012 /*****************************************************************************
1014 * Misc. routines
1016 *****************************************************************************/
1018 static void hex_dump(unsigned char *buf, size_t len)
1020 size_t i;
1022 for (i = 0; i < len; i++) {
1023 if (i && !(i % 16))
1024 printk("\n");
1025 printk("%02x ", *(buf + i));
1027 printk("\n");
1031 * Parse and mangle SNMP message according to mapping.
1032 * (And this is the fucking 'basic' method).
1034 static int snmp_parse_mangle(unsigned char *msg,
1035 u_int16_t len,
1036 const struct oct1_map *map,
1037 u_int16_t *check)
1039 unsigned char *eoc, *end;
1040 unsigned int cls, con, tag, vers, pdutype;
1041 struct asn1_ctx ctx;
1042 struct asn1_octstr comm;
1043 struct snmp_object **obj;
1045 if (debug > 1)
1046 hex_dump(msg, len);
1048 asn1_open(&ctx, msg, len);
1051 * Start of SNMP message.
1053 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1054 return 0;
1055 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1056 return 0;
1059 * Version 1 or 2 handled.
1061 if (!asn1_header_decode(&ctx, &end, &cls, &con, &tag))
1062 return 0;
1063 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
1064 return 0;
1065 if (!asn1_uint_decode (&ctx, end, &vers))
1066 return 0;
1067 if (debug > 1)
1068 printk(KERN_DEBUG "bsalg: snmp version: %u\n", vers + 1);
1069 if (vers > 1)
1070 return 1;
1073 * Community.
1075 if (!asn1_header_decode (&ctx, &end, &cls, &con, &tag))
1076 return 0;
1077 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OTS)
1078 return 0;
1079 if (!asn1_octets_decode(&ctx, end, &comm.data, &comm.len))
1080 return 0;
1081 if (debug > 1) {
1082 unsigned int i;
1084 printk(KERN_DEBUG "bsalg: community: ");
1085 for (i = 0; i < comm.len; i++)
1086 printk("%c", comm.data[i]);
1087 printk("\n");
1089 kfree(comm.data);
1092 * PDU type
1094 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &pdutype))
1095 return 0;
1096 if (cls != ASN1_CTX || con != ASN1_CON)
1097 return 0;
1098 if (debug > 1) {
1099 unsigned char *pdus[] = {
1100 [SNMP_PDU_GET] = "get",
1101 [SNMP_PDU_NEXT] = "get-next",
1102 [SNMP_PDU_RESPONSE] = "response",
1103 [SNMP_PDU_SET] = "set",
1104 [SNMP_PDU_TRAP1] = "trapv1",
1105 [SNMP_PDU_BULK] = "bulk",
1106 [SNMP_PDU_INFORM] = "inform",
1107 [SNMP_PDU_TRAP2] = "trapv2"
1110 if (pdutype > SNMP_PDU_TRAP2)
1111 printk(KERN_DEBUG "bsalg: bad pdu type %u\n", pdutype);
1112 else
1113 printk(KERN_DEBUG "bsalg: pdu: %s\n", pdus[pdutype]);
1115 if (pdutype != SNMP_PDU_RESPONSE &&
1116 pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
1117 return 1;
1120 * Request header or v1 trap
1122 if (pdutype == SNMP_PDU_TRAP1) {
1123 struct snmp_v1_trap trap;
1124 unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
1126 if (ret) {
1127 kfree(trap.id);
1128 kfree((unsigned long *)trap.ip_address);
1129 } else
1130 return ret;
1132 } else {
1133 struct snmp_request req;
1135 if (!snmp_request_decode(&ctx, &req))
1136 return 0;
1138 if (debug > 1)
1139 printk(KERN_DEBUG "bsalg: request: id=0x%lx error_status=%u "
1140 "error_index=%u\n", req.id, req.error_status,
1141 req.error_index);
1145 * Loop through objects, look for IP addresses to mangle.
1147 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1148 return 0;
1150 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1151 return 0;
1153 obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
1154 if (obj == NULL) {
1155 if (net_ratelimit())
1156 printk(KERN_WARNING "OOM in bsalg(%d)\n", __LINE__);
1157 return 0;
1160 while (!asn1_eoc_decode(&ctx, eoc)) {
1161 unsigned int i;
1163 if (!snmp_object_decode(&ctx, obj)) {
1164 if (*obj) {
1165 kfree((*obj)->id);
1166 kfree(*obj);
1168 kfree(obj);
1169 return 0;
1172 if (debug > 1) {
1173 printk(KERN_DEBUG "bsalg: object: ");
1174 for (i = 0; i < (*obj)->id_len; i++) {
1175 if (i > 0)
1176 printk(".");
1177 printk("%lu", (*obj)->id[i]);
1179 printk(": type=%u\n", (*obj)->type);
1183 if ((*obj)->type == SNMP_IPADDR)
1184 mangle_address(ctx.begin, ctx.pointer - 4 , map, check);
1186 kfree((*obj)->id);
1187 kfree(*obj);
1189 kfree(obj);
1191 if (!asn1_eoc_decode(&ctx, eoc))
1192 return 0;
1194 return 1;
1197 /*****************************************************************************
1199 * NAT routines.
1201 *****************************************************************************/
1204 * SNMP translation routine.
1206 static int snmp_translate(struct ip_conntrack *ct,
1207 enum ip_conntrack_info ctinfo,
1208 struct sk_buff **pskb)
1210 struct iphdr *iph = (*pskb)->nh.iph;
1211 struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
1212 u_int16_t udplen = ntohs(udph->len);
1213 u_int16_t paylen = udplen - sizeof(struct udphdr);
1214 int dir = CTINFO2DIR(ctinfo);
1215 struct oct1_map map;
1218 * Determine mappping for application layer addresses based
1219 * on NAT manipulations for the packet.
1221 if (dir == IP_CT_DIR_ORIGINAL) {
1222 /* SNAT traps */
1223 map.from = NOCT1(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip);
1224 map.to = NOCT1(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip);
1225 } else {
1226 /* DNAT replies */
1227 map.from = NOCT1(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
1228 map.to = NOCT1(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip);
1231 if (map.from == map.to)
1232 return NF_ACCEPT;
1234 if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
1235 paylen, &map, &udph->check)) {
1236 if (net_ratelimit())
1237 printk(KERN_WARNING "bsalg: parser failed\n");
1238 return NF_DROP;
1240 return NF_ACCEPT;
1243 /* We don't actually set up expectations, just adjust internal IP
1244 * addresses if this is being NATted */
1245 static int help(struct sk_buff **pskb,
1246 struct ip_conntrack *ct,
1247 enum ip_conntrack_info ctinfo)
1249 int dir = CTINFO2DIR(ctinfo);
1250 unsigned int ret;
1251 struct iphdr *iph = (*pskb)->nh.iph;
1252 struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
1254 /* SNMP replies and originating SNMP traps get mangled */
1255 if (udph->source == ntohs(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
1256 return NF_ACCEPT;
1257 if (udph->dest == ntohs(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
1258 return NF_ACCEPT;
1260 /* No NAT? */
1261 if (!(ct->status & IPS_NAT_MASK))
1262 return NF_ACCEPT;
1265 * Make sure the packet length is ok. So far, we were only guaranteed
1266 * to have a valid length IP header plus 8 bytes, which means we have
1267 * enough room for a UDP header. Just verify the UDP length field so we
1268 * can mess around with the payload.
1270 if (ntohs(udph->len) != (*pskb)->len - (iph->ihl << 2)) {
1271 if (net_ratelimit())
1272 printk(KERN_WARNING "SNMP: dropping malformed packet "
1273 "src=%u.%u.%u.%u dst=%u.%u.%u.%u\n",
1274 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1275 return NF_DROP;
1278 if (!skb_make_writable(pskb, (*pskb)->len))
1279 return NF_DROP;
1281 spin_lock_bh(&snmp_lock);
1282 ret = snmp_translate(ct, ctinfo, pskb);
1283 spin_unlock_bh(&snmp_lock);
1284 return ret;
1287 static struct ip_conntrack_helper snmp_helper = {
1288 .max_expected = 0,
1289 .timeout = 180,
1290 .me = THIS_MODULE,
1291 .help = help,
1292 .name = "snmp",
1294 .tuple = { .src = { .u = { __constant_htons(SNMP_PORT) } },
1295 .dst = { .protonum = IPPROTO_UDP },
1297 .mask = { .src = { .u = { 0xFFFF } },
1298 .dst = { .protonum = 0xFF },
1302 static struct ip_conntrack_helper snmp_trap_helper = {
1303 .max_expected = 0,
1304 .timeout = 180,
1305 .me = THIS_MODULE,
1306 .help = help,
1307 .name = "snmp_trap",
1309 .tuple = { .src = { .u = { __constant_htons(SNMP_TRAP_PORT) } },
1310 .dst = { .protonum = IPPROTO_UDP },
1312 .mask = { .src = { .u = { 0xFFFF } },
1313 .dst = { .protonum = 0xFF },
1317 /*****************************************************************************
1319 * Module stuff.
1321 *****************************************************************************/
1323 static int __init init(void)
1325 int ret = 0;
1327 ret = ip_conntrack_helper_register(&snmp_helper);
1328 if (ret < 0)
1329 return ret;
1330 ret = ip_conntrack_helper_register(&snmp_trap_helper);
1331 if (ret < 0) {
1332 ip_conntrack_helper_unregister(&snmp_helper);
1333 return ret;
1335 return ret;
1338 static void __exit fini(void)
1340 ip_conntrack_helper_unregister(&snmp_helper);
1341 ip_conntrack_helper_unregister(&snmp_trap_helper);
1344 module_init(init);
1345 module_exit(fini);
1347 module_param(debug, bool, 0600);