[NETFILTER]: nf_conntrack: introduce expectation classes and policies
[deliverable/linux.git] / net / ipv4 / netfilter / nf_nat_snmp_basic.c
CommitLineData
807467c2
PM
1/*
2 * nf_nat_snmp_basic.c
3 *
4 * Basic SNMP Application Layer Gateway
5 *
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.
9 *
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.
13 *
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.
17 *
18 * Currently, only SNMPv1 and SNMPv2 are supported.
19 *
20 * More information on ALG and associated issues can be found in
21 * RFC 2962
22 *
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.
25 *
26 * Copyright (c) 2000 RP Internet (www.rpi.net.au).
27 *
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
39 *
40 * Author: James Morris <jmorris@intercode.com.au>
807467c2
PM
41 */
42#include <linux/module.h>
43#include <linux/moduleparam.h>
44#include <linux/types.h>
45#include <linux/kernel.h>
46#include <linux/in.h>
47#include <linux/ip.h>
48#include <linux/udp.h>
49#include <net/checksum.h>
50#include <net/udp.h>
51
52#include <net/netfilter/nf_nat.h>
6002f266 53#include <net/netfilter/nf_conntrack_expect.h>
807467c2
PM
54#include <net/netfilter/nf_conntrack_helper.h>
55#include <net/netfilter/nf_nat_helper.h>
56
57MODULE_LICENSE("GPL");
58MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
59MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
60MODULE_ALIAS("ip_nat_snmp_basic");
61
62#define SNMP_PORT 161
63#define SNMP_TRAP_PORT 162
e79ec50b 64#define NOCT1(n) (*(u8 *)(n))
807467c2
PM
65
66static int debug;
67static DEFINE_SPINLOCK(snmp_lock);
68
69/*
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).
73 */
74struct oct1_map
75{
76 u_int8_t from;
77 u_int8_t to;
78};
79
80
81/*****************************************************************************
82 *
83 * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
84 *
85 *****************************************************************************/
86
87/* Class */
88#define ASN1_UNI 0 /* Universal */
89#define ASN1_APL 1 /* Application */
90#define ASN1_CTX 2 /* Context */
91#define ASN1_PRV 3 /* Private */
92
93/* Tag */
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 */
115
116/* Primitive / Constructed methods*/
117#define ASN1_PRI 0 /* Primitive */
118#define ASN1_CON 1 /* Constructed */
119
120/*
121 * Error codes.
122 */
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
128
129/*
130 * ASN.1 context.
131 */
132struct asn1_ctx
133{
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 */
138};
139
140/*
141 * Octet string (not null terminated)
142 */
143struct asn1_octstr
144{
145 unsigned char *data;
146 unsigned int len;
147};
148
149static void asn1_open(struct asn1_ctx *ctx,
e905a9ed
YH
150 unsigned char *buf,
151 unsigned int len)
807467c2
PM
152{
153 ctx->begin = buf;
154 ctx->end = buf + len;
155 ctx->pointer = buf;
156 ctx->error = ASN1_ERR_NOERROR;
157}
158
159static unsigned char asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
160{
161 if (ctx->pointer >= ctx->end) {
162 ctx->error = ASN1_ERR_DEC_EMPTY;
163 return 0;
164 }
165 *ch = *(ctx->pointer)++;
166 return 1;
167}
168
169static unsigned char asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
170{
171 unsigned char ch;
172
173 *tag = 0;
174
175 do
176 {
177 if (!asn1_octet_decode(ctx, &ch))
178 return 0;
179 *tag <<= 7;
180 *tag |= ch & 0x7F;
181 } while ((ch & 0x80) == 0x80);
182 return 1;
183}
184
185static unsigned char asn1_id_decode(struct asn1_ctx *ctx,
e905a9ed
YH
186 unsigned int *cls,
187 unsigned int *con,
188 unsigned int *tag)
807467c2
PM
189{
190 unsigned char ch;
191
192 if (!asn1_octet_decode(ctx, &ch))
193 return 0;
194
195 *cls = (ch & 0xC0) >> 6;
196 *con = (ch & 0x20) >> 5;
197 *tag = (ch & 0x1F);
198
199 if (*tag == 0x1F) {
200 if (!asn1_tag_decode(ctx, tag))
201 return 0;
202 }
203 return 1;
204}
205
206static unsigned char asn1_length_decode(struct asn1_ctx *ctx,
e905a9ed
YH
207 unsigned int *def,
208 unsigned int *len)
807467c2
PM
209{
210 unsigned char ch, cnt;
211
212 if (!asn1_octet_decode(ctx, &ch))
213 return 0;
214
215 if (ch == 0x80)
216 *def = 0;
217 else {
218 *def = 1;
219
220 if (ch < 0x80)
221 *len = ch;
222 else {
223 cnt = (unsigned char) (ch & 0x7F);
224 *len = 0;
225
226 while (cnt > 0) {
227 if (!asn1_octet_decode(ctx, &ch))
228 return 0;
229 *len <<= 8;
230 *len |= ch;
231 cnt--;
232 }
233 }
234 }
235 return 1;
236}
237
238static unsigned char asn1_header_decode(struct asn1_ctx *ctx,
e905a9ed
YH
239 unsigned char **eoc,
240 unsigned int *cls,
241 unsigned int *con,
242 unsigned int *tag)
807467c2
PM
243{
244 unsigned int def, len;
245
246 if (!asn1_id_decode(ctx, cls, con, tag))
247 return 0;
248
249 def = len = 0;
250 if (!asn1_length_decode(ctx, &def, &len))
251 return 0;
252
253 if (def)
254 *eoc = ctx->pointer + len;
255 else
256 *eoc = NULL;
257 return 1;
258}
259
260static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
261{
262 unsigned char ch;
263
06aa1072 264 if (eoc == NULL) {
807467c2
PM
265 if (!asn1_octet_decode(ctx, &ch))
266 return 0;
267
268 if (ch != 0x00) {
269 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
270 return 0;
271 }
272
273 if (!asn1_octet_decode(ctx, &ch))
274 return 0;
275
276 if (ch != 0x00) {
277 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
278 return 0;
279 }
280 return 1;
281 } else {
282 if (ctx->pointer != eoc) {
283 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
284 return 0;
285 }
286 return 1;
287 }
288}
289
290static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
291{
292 ctx->pointer = eoc;
293 return 1;
294}
295
296static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
e905a9ed
YH
297 unsigned char *eoc,
298 long *integer)
807467c2
PM
299{
300 unsigned char ch;
301 unsigned int len;
302
303 if (!asn1_octet_decode(ctx, &ch))
304 return 0;
305
306 *integer = (signed char) ch;
307 len = 1;
308
309 while (ctx->pointer < eoc) {
310 if (++len > sizeof (long)) {
311 ctx->error = ASN1_ERR_DEC_BADVALUE;
312 return 0;
313 }
314
315 if (!asn1_octet_decode(ctx, &ch))
316 return 0;
317
318 *integer <<= 8;
319 *integer |= ch;
320 }
321 return 1;
322}
323
324static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
e905a9ed
YH
325 unsigned char *eoc,
326 unsigned int *integer)
807467c2
PM
327{
328 unsigned char ch;
329 unsigned int len;
330
331 if (!asn1_octet_decode(ctx, &ch))
332 return 0;
333
334 *integer = ch;
335 if (ch == 0) len = 0;
336 else len = 1;
337
338 while (ctx->pointer < eoc) {
339 if (++len > sizeof (unsigned int)) {
340 ctx->error = ASN1_ERR_DEC_BADVALUE;
341 return 0;
342 }
343
344 if (!asn1_octet_decode(ctx, &ch))
345 return 0;
346
347 *integer <<= 8;
348 *integer |= ch;
349 }
350 return 1;
351}
352
353static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
e905a9ed
YH
354 unsigned char *eoc,
355 unsigned long *integer)
807467c2
PM
356{
357 unsigned char ch;
358 unsigned int len;
359
360 if (!asn1_octet_decode(ctx, &ch))
361 return 0;
362
363 *integer = ch;
364 if (ch == 0) len = 0;
365 else len = 1;
366
367 while (ctx->pointer < eoc) {
368 if (++len > sizeof (unsigned long)) {
369 ctx->error = ASN1_ERR_DEC_BADVALUE;
370 return 0;
371 }
372
373 if (!asn1_octet_decode(ctx, &ch))
374 return 0;
375
376 *integer <<= 8;
377 *integer |= ch;
378 }
379 return 1;
380}
381
382static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
e905a9ed
YH
383 unsigned char *eoc,
384 unsigned char **octets,
385 unsigned int *len)
807467c2
PM
386{
387 unsigned char *ptr;
388
389 *len = 0;
390
391 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
392 if (*octets == NULL) {
393 if (net_ratelimit())
394 printk("OOM in bsalg (%d)\n", __LINE__);
395 return 0;
396 }
397
398 ptr = *octets;
399 while (ctx->pointer < eoc) {
400 if (!asn1_octet_decode(ctx, (unsigned char *)ptr++)) {
401 kfree(*octets);
402 *octets = NULL;
403 return 0;
404 }
405 (*len)++;
406 }
407 return 1;
408}
409
410static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
e905a9ed 411 unsigned long *subid)
807467c2
PM
412{
413 unsigned char ch;
414
415 *subid = 0;
416
417 do {
418 if (!asn1_octet_decode(ctx, &ch))
419 return 0;
420
421 *subid <<= 7;
422 *subid |= ch & 0x7F;
423 } while ((ch & 0x80) == 0x80);
424 return 1;
425}
426
427static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
e905a9ed
YH
428 unsigned char *eoc,
429 unsigned long **oid,
430 unsigned int *len)
807467c2
PM
431{
432 unsigned long subid;
433 unsigned int size;
434 unsigned long *optr;
435
436 size = eoc - ctx->pointer + 1;
437 *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
438 if (*oid == NULL) {
439 if (net_ratelimit())
440 printk("OOM in bsalg (%d)\n", __LINE__);
441 return 0;
442 }
443
444 optr = *oid;
445
446 if (!asn1_subid_decode(ctx, &subid)) {
447 kfree(*oid);
448 *oid = NULL;
449 return 0;
450 }
451
452 if (subid < 40) {
453 optr [0] = 0;
454 optr [1] = subid;
455 } else if (subid < 80) {
456 optr [0] = 1;
457 optr [1] = subid - 40;
458 } else {
459 optr [0] = 2;
460 optr [1] = subid - 80;
461 }
462
463 *len = 2;
464 optr += 2;
465
466 while (ctx->pointer < eoc) {
467 if (++(*len) > size) {
468 ctx->error = ASN1_ERR_DEC_BADVALUE;
469 kfree(*oid);
470 *oid = NULL;
471 return 0;
472 }
473
474 if (!asn1_subid_decode(ctx, optr++)) {
475 kfree(*oid);
476 *oid = NULL;
477 return 0;
478 }
479 }
480 return 1;
481}
482
483/*****************************************************************************
484 *
485 * SNMP decoding routines (gxsnmp author Dirk Wisse)
486 *
487 *****************************************************************************/
488
489/* SNMP Versions */
490#define SNMP_V1 0
491#define SNMP_V2C 1
492#define SNMP_V2 2
493#define SNMP_V3 3
494
495/* Default Sizes */
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
501
502/* Requests */
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
511
512/* Errors */
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
532
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
541
542/* SNMPv1 Types */
543#define SNMP_NULL 0
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 */
553
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
562
563union snmp_syntax
564{
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 */
569};
570
571struct snmp_object
572{
573 unsigned long *id;
574 unsigned int id_len;
575 unsigned short type;
576 unsigned int syntax_len;
577 union snmp_syntax syntax;
578};
579
580struct snmp_request
581{
582 unsigned long id;
583 unsigned int error_status;
584 unsigned int error_index;
585};
586
587struct snmp_v1_trap
588{
589 unsigned long *id;
590 unsigned int id_len;
591 unsigned long ip_address; /* pointer */
592 unsigned int general;
593 unsigned int specific;
594 unsigned long time;
595};
596
597/* SNMP types */
598#define SNMP_IPA 0
599#define SNMP_CNT 1
600#define SNMP_GGE 2
601#define SNMP_TIT 3
602#define SNMP_OPQ 4
603#define SNMP_C64 6
604
605/* SNMP errors */
606#define SERR_NSO 0
607#define SERR_NSI 1
608#define SERR_EOM 2
609
610static inline void mangle_address(unsigned char *begin,
e905a9ed
YH
611 unsigned char *addr,
612 const struct oct1_map *map,
613 __sum16 *check);
807467c2
PM
614struct snmp_cnv
615{
616 unsigned int class;
617 unsigned int tag;
618 int syntax;
619};
620
621static struct snmp_cnv snmp_conv [] =
622{
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},
633
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},
640 {0, 0, -1}
641};
642
643static unsigned char snmp_tag_cls2syntax(unsigned int tag,
e905a9ed
YH
644 unsigned int cls,
645 unsigned short *syntax)
807467c2
PM
646{
647 struct snmp_cnv *cnv;
648
649 cnv = snmp_conv;
650
651 while (cnv->syntax != -1) {
652 if (cnv->tag == tag && cnv->class == cls) {
653 *syntax = cnv->syntax;
654 return 1;
655 }
656 cnv++;
657 }
658 return 0;
659}
660
661static unsigned char snmp_object_decode(struct asn1_ctx *ctx,
e905a9ed 662 struct snmp_object **obj)
807467c2
PM
663{
664 unsigned int cls, con, tag, len, idlen;
665 unsigned short type;
666 unsigned char *eoc, *end, *p;
667 unsigned long *lp, *id;
668 unsigned long ul;
669 long l;
670
671 *obj = NULL;
672 id = NULL;
673
674 if (!asn1_header_decode(ctx, &eoc, &cls, &con, &tag))
675 return 0;
676
677 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
678 return 0;
679
680 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
681 return 0;
682
683 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
684 return 0;
685
686 if (!asn1_oid_decode(ctx, end, &id, &idlen))
687 return 0;
688
689 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag)) {
690 kfree(id);
691 return 0;
692 }
693
694 if (con != ASN1_PRI) {
695 kfree(id);
696 return 0;
697 }
698
699 type = 0;
700 if (!snmp_tag_cls2syntax(tag, cls, &type)) {
701 kfree(id);
702 return 0;
703 }
704
705 l = 0;
706 switch (type) {
707 case SNMP_INTEGER:
708 len = sizeof(long);
709 if (!asn1_long_decode(ctx, end, &l)) {
710 kfree(id);
711 return 0;
712 }
713 *obj = kmalloc(sizeof(struct snmp_object) + len,
e905a9ed 714 GFP_ATOMIC);
807467c2
PM
715 if (*obj == NULL) {
716 kfree(id);
717 if (net_ratelimit())
718 printk("OOM in bsalg (%d)\n", __LINE__);
719 return 0;
720 }
721 (*obj)->syntax.l[0] = l;
722 break;
723 case SNMP_OCTETSTR:
724 case SNMP_OPAQUE:
725 if (!asn1_octets_decode(ctx, end, &p, &len)) {
726 kfree(id);
727 return 0;
728 }
729 *obj = kmalloc(sizeof(struct snmp_object) + len,
e905a9ed 730 GFP_ATOMIC);
807467c2
PM
731 if (*obj == NULL) {
732 kfree(id);
733 if (net_ratelimit())
734 printk("OOM in bsalg (%d)\n", __LINE__);
735 return 0;
736 }
737 memcpy((*obj)->syntax.c, p, len);
738 kfree(p);
739 break;
740 case SNMP_NULL:
741 case SNMP_NOSUCHOBJECT:
742 case SNMP_NOSUCHINSTANCE:
743 case SNMP_ENDOFMIBVIEW:
744 len = 0;
745 *obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
746 if (*obj == NULL) {
747 kfree(id);
748 if (net_ratelimit())
749 printk("OOM in bsalg (%d)\n", __LINE__);
750 return 0;
751 }
752 if (!asn1_null_decode(ctx, end)) {
753 kfree(id);
754 kfree(*obj);
755 *obj = NULL;
756 return 0;
757 }
758 break;
759 case SNMP_OBJECTID:
760 if (!asn1_oid_decode(ctx, end, (unsigned long **)&lp, &len)) {
761 kfree(id);
762 return 0;
763 }
764 len *= sizeof(unsigned long);
765 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
766 if (*obj == NULL) {
767 kfree(lp);
768 kfree(id);
769 if (net_ratelimit())
770 printk("OOM in bsalg (%d)\n", __LINE__);
771 return 0;
772 }
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;
780 }
781 if (len != 4) {
782 kfree(p);
783 kfree(id);
784 return 0;
785 }
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;
793 }
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;
804 }
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;
811 }
812 (*obj)->syntax.ul[0] = ul;
813 break;
814 default:
815 kfree(id);
816 return 0;
817 }
818
819 (*obj)->syntax_len = len;
820 (*obj)->type = type;
821 (*obj)->id = id;
822 (*obj)->id_len = idlen;
823
824 if (!asn1_eoc_decode(ctx, eoc)) {
825 kfree(id);
826 kfree(*obj);
827 *obj = NULL;
828 return 0;
829 }
830 return 1;
831}
832
833static unsigned char snmp_request_decode(struct asn1_ctx *ctx,
e905a9ed 834 struct snmp_request *request)
807467c2
PM
835{
836 unsigned int cls, con, tag;
837 unsigned char *end;
838
839 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
840 return 0;
841
842 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
843 return 0;
844
845 if (!asn1_ulong_decode(ctx, end, &request->id))
846 return 0;
847
848 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
849 return 0;
850
851 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
852 return 0;
853
854 if (!asn1_uint_decode(ctx, end, &request->error_status))
855 return 0;
856
857 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
858 return 0;
859
860 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
861 return 0;
862
863 if (!asn1_uint_decode(ctx, end, &request->error_index))
864 return 0;
865
866 return 1;
867}
868
869/*
870 * Fast checksum update for possibly oddly-aligned UDP byte, from the
871 * code example in the draft.
872 */
873static void fast_csum(__sum16 *csum,
e905a9ed
YH
874 const unsigned char *optr,
875 const unsigned char *nptr,
876 int offset)
807467c2
PM
877{
878 unsigned char s[4];
879
880 if (offset & 1) {
881 s[0] = s[2] = 0;
882 s[1] = ~*optr;
883 s[3] = *nptr;
884 } else {
885 s[1] = s[3] = 0;
886 s[0] = ~*optr;
887 s[2] = *nptr;
888 }
889
890 *csum = csum_fold(csum_partial(s, 4, ~csum_unfold(*csum)));
891}
892
893/*
894 * Mangle IP address.
895 * - begin points to the start of the snmp messgae
896 * - addr points to the start of the address
897 */
898static inline void mangle_address(unsigned char *begin,
e905a9ed
YH
899 unsigned char *addr,
900 const struct oct1_map *map,
901 __sum16 *check)
807467c2
PM
902{
903 if (map->from == NOCT1(addr)) {
904 u_int32_t old;
905
906 if (debug)
907 memcpy(&old, (unsigned char *)addr, sizeof(old));
908
909 *addr = map->to;
910
911 /* Update UDP checksum if being used */
912 if (*check) {
913 fast_csum(check,
e905a9ed 914 &map->from, &map->to, addr - begin);
807467c2
PM
915
916 }
917
918 if (debug)
919 printk(KERN_DEBUG "bsalg: mapped %u.%u.%u.%u to "
920 "%u.%u.%u.%u\n", NIPQUAD(old), NIPQUAD(*addr));
921 }
922}
923
924static unsigned char snmp_trap_decode(struct asn1_ctx *ctx,
e905a9ed
YH
925 struct snmp_v1_trap *trap,
926 const struct oct1_map *map,
927 __sum16 *check)
807467c2
PM
928{
929 unsigned int cls, con, tag, len;
930 unsigned char *end;
931
932 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
933 return 0;
934
935 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
936 return 0;
937
938 if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
939 return 0;
940
941 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
942 goto err_id_free;
943
944 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
945 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS)))
946 goto err_id_free;
947
948 if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
949 goto err_id_free;
950
951 /* IPv4 only */
952 if (len != 4)
953 goto err_addr_free;
954
955 mangle_address(ctx->begin, ctx->pointer - 4, map, check);
956
957 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
958 goto err_addr_free;
959
960 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
961 goto err_addr_free;
962
963 if (!asn1_uint_decode(ctx, end, &trap->general))
964 goto err_addr_free;
965
966 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
967 goto err_addr_free;
968
969 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
970 goto err_addr_free;
971
972 if (!asn1_uint_decode(ctx, end, &trap->specific))
973 goto err_addr_free;
974
975 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
976 goto err_addr_free;
977
978 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
979 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT)))
980 goto err_addr_free;
981
982 if (!asn1_ulong_decode(ctx, end, &trap->time))
983 goto err_addr_free;
984
985 return 1;
986
987err_addr_free:
988 kfree((unsigned long *)trap->ip_address);
989
990err_id_free:
991 kfree(trap->id);
992
993 return 0;
994}
995
996/*****************************************************************************
997 *
998 * Misc. routines
999 *
1000 *****************************************************************************/
1001
1002static void hex_dump(unsigned char *buf, size_t len)
1003{
1004 size_t i;
1005
1006 for (i = 0; i < len; i++) {
1007 if (i && !(i % 16))
1008 printk("\n");
1009 printk("%02x ", *(buf + i));
1010 }
1011 printk("\n");
1012}
1013
1014/*
1015 * Parse and mangle SNMP message according to mapping.
1016 * (And this is the fucking 'basic' method).
1017 */
1018static int snmp_parse_mangle(unsigned char *msg,
e905a9ed
YH
1019 u_int16_t len,
1020 const struct oct1_map *map,
1021 __sum16 *check)
807467c2
PM
1022{
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;
1028
1029 if (debug > 1)
1030 hex_dump(msg, len);
1031
1032 asn1_open(&ctx, msg, len);
1033
1034 /*
1035 * Start of SNMP message.
1036 */
1037 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1038 return 0;
1039 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1040 return 0;
1041
1042 /*
1043 * Version 1 or 2 handled.
1044 */
1045 if (!asn1_header_decode(&ctx, &end, &cls, &con, &tag))
1046 return 0;
1047 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
1048 return 0;
1049 if (!asn1_uint_decode (&ctx, end, &vers))
1050 return 0;
1051 if (debug > 1)
1052 printk(KERN_DEBUG "bsalg: snmp version: %u\n", vers + 1);
1053 if (vers > 1)
1054 return 1;
1055
1056 /*
1057 * Community.
1058 */
1059 if (!asn1_header_decode (&ctx, &end, &cls, &con, &tag))
1060 return 0;
1061 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OTS)
1062 return 0;
1063 if (!asn1_octets_decode(&ctx, end, &comm.data, &comm.len))
1064 return 0;
1065 if (debug > 1) {
1066 unsigned int i;
1067
1068 printk(KERN_DEBUG "bsalg: community: ");
1069 for (i = 0; i < comm.len; i++)
1070 printk("%c", comm.data[i]);
1071 printk("\n");
1072 }
1073 kfree(comm.data);
1074
1075 /*
1076 * PDU type
1077 */
1078 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &pdutype))
1079 return 0;
1080 if (cls != ASN1_CTX || con != ASN1_CON)
1081 return 0;
1082 if (debug > 1) {
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"
1092 };
1093
1094 if (pdutype > SNMP_PDU_TRAP2)
1095 printk(KERN_DEBUG "bsalg: bad pdu type %u\n", pdutype);
1096 else
1097 printk(KERN_DEBUG "bsalg: pdu: %s\n", pdus[pdutype]);
1098 }
1099 if (pdutype != SNMP_PDU_RESPONSE &&
1100 pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
1101 return 1;
1102
1103 /*
1104 * Request header or v1 trap
1105 */
1106 if (pdutype == SNMP_PDU_TRAP1) {
1107 struct snmp_v1_trap trap;
1108 unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
1109
1110 if (ret) {
1111 kfree(trap.id);
1112 kfree((unsigned long *)trap.ip_address);
1113 } else
1114 return ret;
1115
1116 } else {
1117 struct snmp_request req;
1118
1119 if (!snmp_request_decode(&ctx, &req))
1120 return 0;
1121
1122 if (debug > 1)
1123 printk(KERN_DEBUG "bsalg: request: id=0x%lx error_status=%u "
1124 "error_index=%u\n", req.id, req.error_status,
1125 req.error_index);
1126 }
1127
1128 /*
1129 * Loop through objects, look for IP addresses to mangle.
1130 */
1131 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1132 return 0;
1133
1134 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1135 return 0;
1136
1137 obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
1138 if (obj == NULL) {
1139 if (net_ratelimit())
1140 printk(KERN_WARNING "OOM in bsalg(%d)\n", __LINE__);
1141 return 0;
1142 }
1143
1144 while (!asn1_eoc_decode(&ctx, eoc)) {
1145 unsigned int i;
1146
1147 if (!snmp_object_decode(&ctx, obj)) {
1148 if (*obj) {
1149 kfree((*obj)->id);
1150 kfree(*obj);
1151 }
1152 kfree(obj);
1153 return 0;
1154 }
1155
1156 if (debug > 1) {
1157 printk(KERN_DEBUG "bsalg: object: ");
1158 for (i = 0; i < (*obj)->id_len; i++) {
1159 if (i > 0)
1160 printk(".");
1161 printk("%lu", (*obj)->id[i]);
1162 }
1163 printk(": type=%u\n", (*obj)->type);
1164
1165 }
1166
1167 if ((*obj)->type == SNMP_IPADDR)
1168 mangle_address(ctx.begin, ctx.pointer - 4 , map, check);
1169
1170 kfree((*obj)->id);
1171 kfree(*obj);
1172 }
1173 kfree(obj);
1174
1175 if (!asn1_eoc_decode(&ctx, eoc))
1176 return 0;
1177
1178 return 1;
1179}
1180
1181/*****************************************************************************
1182 *
1183 * NAT routines.
1184 *
1185 *****************************************************************************/
1186
1187/*
1188 * SNMP translation routine.
1189 */
1190static int snmp_translate(struct nf_conn *ct,
e905a9ed 1191 enum ip_conntrack_info ctinfo,
3db05fea 1192 struct sk_buff *skb)
807467c2 1193{
3db05fea 1194 struct iphdr *iph = ip_hdr(skb);
807467c2
PM
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;
1200
1201 /*
1202 * Determine mappping for application layer addresses based
1203 * on NAT manipulations for the packet.
1204 */
1205 if (dir == IP_CT_DIR_ORIGINAL) {
1206 /* SNAT traps */
1207 map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
1208 map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
1209 } else {
1210 /* DNAT replies */
1211 map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
1212 map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
1213 }
1214
1215 if (map.from == map.to)
1216 return NF_ACCEPT;
1217
1218 if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
e905a9ed 1219 paylen, &map, &udph->check)) {
807467c2
PM
1220 if (net_ratelimit())
1221 printk(KERN_WARNING "bsalg: parser failed\n");
1222 return NF_DROP;
1223 }
1224 return NF_ACCEPT;
1225}
1226
1227/* We don't actually set up expectations, just adjust internal IP
1228 * addresses if this is being NATted */
3db05fea 1229static int help(struct sk_buff *skb, unsigned int protoff,
807467c2
PM
1230 struct nf_conn *ct,
1231 enum ip_conntrack_info ctinfo)
1232{
1233 int dir = CTINFO2DIR(ctinfo);
1234 unsigned int ret;
3db05fea 1235 struct iphdr *iph = ip_hdr(skb);
807467c2
PM
1236 struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
1237
1238 /* SNMP replies and originating SNMP traps get mangled */
1239 if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
1240 return NF_ACCEPT;
1241 if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
1242 return NF_ACCEPT;
1243
1244 /* No NAT? */
1245 if (!(ct->status & IPS_NAT_MASK))
1246 return NF_ACCEPT;
1247
1248 /*
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.
1253 */
3db05fea 1254 if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
807467c2
PM
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));
1259 return NF_DROP;
1260 }
1261
3db05fea 1262 if (!skb_make_writable(skb, skb->len))
807467c2
PM
1263 return NF_DROP;
1264
1265 spin_lock_bh(&snmp_lock);
3db05fea 1266 ret = snmp_translate(ct, ctinfo, skb);
807467c2
PM
1267 spin_unlock_bh(&snmp_lock);
1268 return ret;
1269}
1270
6002f266
PM
1271static const struct nf_conntrack_expect_policy snmp_exp_policy = {
1272 .max_expected = 0,
1273 .timeout = 180,
1274};
1275
807467c2 1276static struct nf_conntrack_helper snmp_helper __read_mostly = {
807467c2
PM
1277 .me = THIS_MODULE,
1278 .help = help,
6002f266 1279 .expect_policy = &snmp_exp_policy,
807467c2
PM
1280 .name = "snmp",
1281 .tuple.src.l3num = AF_INET,
1282 .tuple.src.u.udp.port = __constant_htons(SNMP_PORT),
1283 .tuple.dst.protonum = IPPROTO_UDP,
807467c2
PM
1284};
1285
1286static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
807467c2
PM
1287 .me = THIS_MODULE,
1288 .help = help,
6002f266 1289 .expect_policy = &snmp_exp_policy,
807467c2
PM
1290 .name = "snmp_trap",
1291 .tuple.src.l3num = AF_INET,
1292 .tuple.src.u.udp.port = __constant_htons(SNMP_TRAP_PORT),
1293 .tuple.dst.protonum = IPPROTO_UDP,
807467c2
PM
1294};
1295
1296/*****************************************************************************
1297 *
1298 * Module stuff.
1299 *
1300 *****************************************************************************/
1301
1302static int __init nf_nat_snmp_basic_init(void)
1303{
1304 int ret = 0;
1305
1306 ret = nf_conntrack_helper_register(&snmp_helper);
1307 if (ret < 0)
1308 return ret;
1309 ret = nf_conntrack_helper_register(&snmp_trap_helper);
1310 if (ret < 0) {
1311 nf_conntrack_helper_unregister(&snmp_helper);
1312 return ret;
1313 }
1314 return ret;
1315}
1316
1317static void __exit nf_nat_snmp_basic_fini(void)
1318{
1319 nf_conntrack_helper_unregister(&snmp_helper);
1320 nf_conntrack_helper_unregister(&snmp_trap_helper);
1321}
1322
1323module_init(nf_nat_snmp_basic_init);
1324module_exit(nf_nat_snmp_basic_fini);
1325
1326module_param(debug, int, 0600);
This page took 0.227633 seconds and 5 git commands to generate.