Merge branch 'linus' into x86/cleanups
[deliverable/linux.git] / net / ipv4 / netfilter / nf_nat_snmp_basic.c
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>
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>
53 #include <net/netfilter/nf_conntrack_expect.h>
54 #include <net/netfilter/nf_conntrack_helper.h>
55 #include <net/netfilter/nf_nat_helper.h>
56
57 MODULE_LICENSE("GPL");
58 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
59 MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
60 MODULE_ALIAS("ip_nat_snmp_basic");
61
62 #define SNMP_PORT 161
63 #define SNMP_TRAP_PORT 162
64 #define NOCT1(n) (*(u8 *)(n))
65
66 static int debug;
67 static 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 */
74 struct 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 */
132 struct 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 */
143 struct asn1_octstr
144 {
145 unsigned char *data;
146 unsigned int len;
147 };
148
149 static void asn1_open(struct asn1_ctx *ctx,
150 unsigned char *buf,
151 unsigned int len)
152 {
153 ctx->begin = buf;
154 ctx->end = buf + len;
155 ctx->pointer = buf;
156 ctx->error = ASN1_ERR_NOERROR;
157 }
158
159 static 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
169 static 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
185 static unsigned char asn1_id_decode(struct asn1_ctx *ctx,
186 unsigned int *cls,
187 unsigned int *con,
188 unsigned int *tag)
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
206 static unsigned char asn1_length_decode(struct asn1_ctx *ctx,
207 unsigned int *def,
208 unsigned int *len)
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 = 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
236 /* don't trust len bigger than ctx buffer */
237 if (*len > ctx->end - ctx->pointer)
238 return 0;
239
240 return 1;
241 }
242
243 static unsigned char asn1_header_decode(struct asn1_ctx *ctx,
244 unsigned char **eoc,
245 unsigned int *cls,
246 unsigned int *con,
247 unsigned int *tag)
248 {
249 unsigned int def, len;
250
251 if (!asn1_id_decode(ctx, cls, con, tag))
252 return 0;
253
254 def = len = 0;
255 if (!asn1_length_decode(ctx, &def, &len))
256 return 0;
257
258 /* primitive shall be definite, indefinite shall be constructed */
259 if (*con == ASN1_PRI && !def)
260 return 0;
261
262 if (def)
263 *eoc = ctx->pointer + len;
264 else
265 *eoc = NULL;
266 return 1;
267 }
268
269 static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
270 {
271 unsigned char ch;
272
273 if (eoc == NULL) {
274 if (!asn1_octet_decode(ctx, &ch))
275 return 0;
276
277 if (ch != 0x00) {
278 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
279 return 0;
280 }
281
282 if (!asn1_octet_decode(ctx, &ch))
283 return 0;
284
285 if (ch != 0x00) {
286 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
287 return 0;
288 }
289 return 1;
290 } else {
291 if (ctx->pointer != eoc) {
292 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
293 return 0;
294 }
295 return 1;
296 }
297 }
298
299 static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
300 {
301 ctx->pointer = eoc;
302 return 1;
303 }
304
305 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
306 unsigned char *eoc,
307 long *integer)
308 {
309 unsigned char ch;
310 unsigned int len;
311
312 if (!asn1_octet_decode(ctx, &ch))
313 return 0;
314
315 *integer = (signed char) ch;
316 len = 1;
317
318 while (ctx->pointer < eoc) {
319 if (++len > sizeof (long)) {
320 ctx->error = ASN1_ERR_DEC_BADVALUE;
321 return 0;
322 }
323
324 if (!asn1_octet_decode(ctx, &ch))
325 return 0;
326
327 *integer <<= 8;
328 *integer |= ch;
329 }
330 return 1;
331 }
332
333 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
334 unsigned char *eoc,
335 unsigned int *integer)
336 {
337 unsigned char ch;
338 unsigned int len;
339
340 if (!asn1_octet_decode(ctx, &ch))
341 return 0;
342
343 *integer = ch;
344 if (ch == 0) len = 0;
345 else len = 1;
346
347 while (ctx->pointer < eoc) {
348 if (++len > sizeof (unsigned int)) {
349 ctx->error = ASN1_ERR_DEC_BADVALUE;
350 return 0;
351 }
352
353 if (!asn1_octet_decode(ctx, &ch))
354 return 0;
355
356 *integer <<= 8;
357 *integer |= ch;
358 }
359 return 1;
360 }
361
362 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
363 unsigned char *eoc,
364 unsigned long *integer)
365 {
366 unsigned char ch;
367 unsigned int len;
368
369 if (!asn1_octet_decode(ctx, &ch))
370 return 0;
371
372 *integer = ch;
373 if (ch == 0) len = 0;
374 else len = 1;
375
376 while (ctx->pointer < eoc) {
377 if (++len > sizeof (unsigned long)) {
378 ctx->error = ASN1_ERR_DEC_BADVALUE;
379 return 0;
380 }
381
382 if (!asn1_octet_decode(ctx, &ch))
383 return 0;
384
385 *integer <<= 8;
386 *integer |= ch;
387 }
388 return 1;
389 }
390
391 static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
392 unsigned char *eoc,
393 unsigned char **octets,
394 unsigned int *len)
395 {
396 unsigned char *ptr;
397
398 *len = 0;
399
400 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
401 if (*octets == NULL) {
402 if (net_ratelimit())
403 printk("OOM in bsalg (%d)\n", __LINE__);
404 return 0;
405 }
406
407 ptr = *octets;
408 while (ctx->pointer < eoc) {
409 if (!asn1_octet_decode(ctx, (unsigned char *)ptr++)) {
410 kfree(*octets);
411 *octets = NULL;
412 return 0;
413 }
414 (*len)++;
415 }
416 return 1;
417 }
418
419 static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
420 unsigned long *subid)
421 {
422 unsigned char ch;
423
424 *subid = 0;
425
426 do {
427 if (!asn1_octet_decode(ctx, &ch))
428 return 0;
429
430 *subid <<= 7;
431 *subid |= ch & 0x7F;
432 } while ((ch & 0x80) == 0x80);
433 return 1;
434 }
435
436 static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
437 unsigned char *eoc,
438 unsigned long **oid,
439 unsigned int *len)
440 {
441 unsigned long subid;
442 unsigned long *optr;
443 size_t size;
444
445 size = eoc - ctx->pointer + 1;
446
447 /* first subid actually encodes first two subids */
448 if (size < 2 || size > ULONG_MAX/sizeof(unsigned long))
449 return 0;
450
451 *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
452 if (*oid == NULL) {
453 if (net_ratelimit())
454 printk("OOM in bsalg (%d)\n", __LINE__);
455 return 0;
456 }
457
458 optr = *oid;
459
460 if (!asn1_subid_decode(ctx, &subid)) {
461 kfree(*oid);
462 *oid = NULL;
463 return 0;
464 }
465
466 if (subid < 40) {
467 optr [0] = 0;
468 optr [1] = subid;
469 } else if (subid < 80) {
470 optr [0] = 1;
471 optr [1] = subid - 40;
472 } else {
473 optr [0] = 2;
474 optr [1] = subid - 80;
475 }
476
477 *len = 2;
478 optr += 2;
479
480 while (ctx->pointer < eoc) {
481 if (++(*len) > size) {
482 ctx->error = ASN1_ERR_DEC_BADVALUE;
483 kfree(*oid);
484 *oid = NULL;
485 return 0;
486 }
487
488 if (!asn1_subid_decode(ctx, optr++)) {
489 kfree(*oid);
490 *oid = NULL;
491 return 0;
492 }
493 }
494 return 1;
495 }
496
497 /*****************************************************************************
498 *
499 * SNMP decoding routines (gxsnmp author Dirk Wisse)
500 *
501 *****************************************************************************/
502
503 /* SNMP Versions */
504 #define SNMP_V1 0
505 #define SNMP_V2C 1
506 #define SNMP_V2 2
507 #define SNMP_V3 3
508
509 /* Default Sizes */
510 #define SNMP_SIZE_COMM 256
511 #define SNMP_SIZE_OBJECTID 128
512 #define SNMP_SIZE_BUFCHR 256
513 #define SNMP_SIZE_BUFINT 128
514 #define SNMP_SIZE_SMALLOBJECTID 16
515
516 /* Requests */
517 #define SNMP_PDU_GET 0
518 #define SNMP_PDU_NEXT 1
519 #define SNMP_PDU_RESPONSE 2
520 #define SNMP_PDU_SET 3
521 #define SNMP_PDU_TRAP1 4
522 #define SNMP_PDU_BULK 5
523 #define SNMP_PDU_INFORM 6
524 #define SNMP_PDU_TRAP2 7
525
526 /* Errors */
527 #define SNMP_NOERROR 0
528 #define SNMP_TOOBIG 1
529 #define SNMP_NOSUCHNAME 2
530 #define SNMP_BADVALUE 3
531 #define SNMP_READONLY 4
532 #define SNMP_GENERROR 5
533 #define SNMP_NOACCESS 6
534 #define SNMP_WRONGTYPE 7
535 #define SNMP_WRONGLENGTH 8
536 #define SNMP_WRONGENCODING 9
537 #define SNMP_WRONGVALUE 10
538 #define SNMP_NOCREATION 11
539 #define SNMP_INCONSISTENTVALUE 12
540 #define SNMP_RESOURCEUNAVAILABLE 13
541 #define SNMP_COMMITFAILED 14
542 #define SNMP_UNDOFAILED 15
543 #define SNMP_AUTHORIZATIONERROR 16
544 #define SNMP_NOTWRITABLE 17
545 #define SNMP_INCONSISTENTNAME 18
546
547 /* General SNMP V1 Traps */
548 #define SNMP_TRAP_COLDSTART 0
549 #define SNMP_TRAP_WARMSTART 1
550 #define SNMP_TRAP_LINKDOWN 2
551 #define SNMP_TRAP_LINKUP 3
552 #define SNMP_TRAP_AUTFAILURE 4
553 #define SNMP_TRAP_EQPNEIGHBORLOSS 5
554 #define SNMP_TRAP_ENTSPECIFIC 6
555
556 /* SNMPv1 Types */
557 #define SNMP_NULL 0
558 #define SNMP_INTEGER 1 /* l */
559 #define SNMP_OCTETSTR 2 /* c */
560 #define SNMP_DISPLAYSTR 2 /* c */
561 #define SNMP_OBJECTID 3 /* ul */
562 #define SNMP_IPADDR 4 /* uc */
563 #define SNMP_COUNTER 5 /* ul */
564 #define SNMP_GAUGE 6 /* ul */
565 #define SNMP_TIMETICKS 7 /* ul */
566 #define SNMP_OPAQUE 8 /* c */
567
568 /* Additional SNMPv2 Types */
569 #define SNMP_UINTEGER 5 /* ul */
570 #define SNMP_BITSTR 9 /* uc */
571 #define SNMP_NSAP 10 /* uc */
572 #define SNMP_COUNTER64 11 /* ul */
573 #define SNMP_NOSUCHOBJECT 12
574 #define SNMP_NOSUCHINSTANCE 13
575 #define SNMP_ENDOFMIBVIEW 14
576
577 union snmp_syntax
578 {
579 unsigned char uc[0]; /* 8 bit unsigned */
580 char c[0]; /* 8 bit signed */
581 unsigned long ul[0]; /* 32 bit unsigned */
582 long l[0]; /* 32 bit signed */
583 };
584
585 struct snmp_object
586 {
587 unsigned long *id;
588 unsigned int id_len;
589 unsigned short type;
590 unsigned int syntax_len;
591 union snmp_syntax syntax;
592 };
593
594 struct snmp_request
595 {
596 unsigned long id;
597 unsigned int error_status;
598 unsigned int error_index;
599 };
600
601 struct snmp_v1_trap
602 {
603 unsigned long *id;
604 unsigned int id_len;
605 unsigned long ip_address; /* pointer */
606 unsigned int general;
607 unsigned int specific;
608 unsigned long time;
609 };
610
611 /* SNMP types */
612 #define SNMP_IPA 0
613 #define SNMP_CNT 1
614 #define SNMP_GGE 2
615 #define SNMP_TIT 3
616 #define SNMP_OPQ 4
617 #define SNMP_C64 6
618
619 /* SNMP errors */
620 #define SERR_NSO 0
621 #define SERR_NSI 1
622 #define SERR_EOM 2
623
624 static inline void mangle_address(unsigned char *begin,
625 unsigned char *addr,
626 const struct oct1_map *map,
627 __sum16 *check);
628 struct snmp_cnv
629 {
630 unsigned int class;
631 unsigned int tag;
632 int syntax;
633 };
634
635 static const struct snmp_cnv snmp_conv[] = {
636 {ASN1_UNI, ASN1_NUL, SNMP_NULL},
637 {ASN1_UNI, ASN1_INT, SNMP_INTEGER},
638 {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR},
639 {ASN1_UNI, ASN1_OTS, SNMP_DISPLAYSTR},
640 {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID},
641 {ASN1_APL, SNMP_IPA, SNMP_IPADDR},
642 {ASN1_APL, SNMP_CNT, SNMP_COUNTER}, /* Counter32 */
643 {ASN1_APL, SNMP_GGE, SNMP_GAUGE}, /* Gauge32 == Unsigned32 */
644 {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS},
645 {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE},
646
647 /* SNMPv2 data types and errors */
648 {ASN1_UNI, ASN1_BTS, SNMP_BITSTR},
649 {ASN1_APL, SNMP_C64, SNMP_COUNTER64},
650 {ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT},
651 {ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE},
652 {ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW},
653 {0, 0, -1}
654 };
655
656 static unsigned char snmp_tag_cls2syntax(unsigned int tag,
657 unsigned int cls,
658 unsigned short *syntax)
659 {
660 const struct snmp_cnv *cnv;
661
662 cnv = snmp_conv;
663
664 while (cnv->syntax != -1) {
665 if (cnv->tag == tag && cnv->class == cls) {
666 *syntax = cnv->syntax;
667 return 1;
668 }
669 cnv++;
670 }
671 return 0;
672 }
673
674 static unsigned char snmp_object_decode(struct asn1_ctx *ctx,
675 struct snmp_object **obj)
676 {
677 unsigned int cls, con, tag, len, idlen;
678 unsigned short type;
679 unsigned char *eoc, *end, *p;
680 unsigned long *lp, *id;
681 unsigned long ul;
682 long l;
683
684 *obj = NULL;
685 id = NULL;
686
687 if (!asn1_header_decode(ctx, &eoc, &cls, &con, &tag))
688 return 0;
689
690 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
691 return 0;
692
693 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
694 return 0;
695
696 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
697 return 0;
698
699 if (!asn1_oid_decode(ctx, end, &id, &idlen))
700 return 0;
701
702 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag)) {
703 kfree(id);
704 return 0;
705 }
706
707 if (con != ASN1_PRI) {
708 kfree(id);
709 return 0;
710 }
711
712 type = 0;
713 if (!snmp_tag_cls2syntax(tag, cls, &type)) {
714 kfree(id);
715 return 0;
716 }
717
718 l = 0;
719 switch (type) {
720 case SNMP_INTEGER:
721 len = sizeof(long);
722 if (!asn1_long_decode(ctx, end, &l)) {
723 kfree(id);
724 return 0;
725 }
726 *obj = kmalloc(sizeof(struct snmp_object) + len,
727 GFP_ATOMIC);
728 if (*obj == NULL) {
729 kfree(id);
730 if (net_ratelimit())
731 printk("OOM in bsalg (%d)\n", __LINE__);
732 return 0;
733 }
734 (*obj)->syntax.l[0] = l;
735 break;
736 case SNMP_OCTETSTR:
737 case SNMP_OPAQUE:
738 if (!asn1_octets_decode(ctx, end, &p, &len)) {
739 kfree(id);
740 return 0;
741 }
742 *obj = kmalloc(sizeof(struct snmp_object) + len,
743 GFP_ATOMIC);
744 if (*obj == NULL) {
745 kfree(p);
746 kfree(id);
747 if (net_ratelimit())
748 printk("OOM in bsalg (%d)\n", __LINE__);
749 return 0;
750 }
751 memcpy((*obj)->syntax.c, p, len);
752 kfree(p);
753 break;
754 case SNMP_NULL:
755 case SNMP_NOSUCHOBJECT:
756 case SNMP_NOSUCHINSTANCE:
757 case SNMP_ENDOFMIBVIEW:
758 len = 0;
759 *obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
760 if (*obj == NULL) {
761 kfree(id);
762 if (net_ratelimit())
763 printk("OOM in bsalg (%d)\n", __LINE__);
764 return 0;
765 }
766 if (!asn1_null_decode(ctx, end)) {
767 kfree(id);
768 kfree(*obj);
769 *obj = NULL;
770 return 0;
771 }
772 break;
773 case SNMP_OBJECTID:
774 if (!asn1_oid_decode(ctx, end, (unsigned long **)&lp, &len)) {
775 kfree(id);
776 return 0;
777 }
778 len *= sizeof(unsigned long);
779 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
780 if (*obj == NULL) {
781 kfree(lp);
782 kfree(id);
783 if (net_ratelimit())
784 printk("OOM in bsalg (%d)\n", __LINE__);
785 return 0;
786 }
787 memcpy((*obj)->syntax.ul, lp, len);
788 kfree(lp);
789 break;
790 case SNMP_IPADDR:
791 if (!asn1_octets_decode(ctx, end, &p, &len)) {
792 kfree(id);
793 return 0;
794 }
795 if (len != 4) {
796 kfree(p);
797 kfree(id);
798 return 0;
799 }
800 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
801 if (*obj == NULL) {
802 kfree(p);
803 kfree(id);
804 if (net_ratelimit())
805 printk("OOM in bsalg (%d)\n", __LINE__);
806 return 0;
807 }
808 memcpy((*obj)->syntax.uc, p, len);
809 kfree(p);
810 break;
811 case SNMP_COUNTER:
812 case SNMP_GAUGE:
813 case SNMP_TIMETICKS:
814 len = sizeof(unsigned long);
815 if (!asn1_ulong_decode(ctx, end, &ul)) {
816 kfree(id);
817 return 0;
818 }
819 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
820 if (*obj == NULL) {
821 kfree(id);
822 if (net_ratelimit())
823 printk("OOM in bsalg (%d)\n", __LINE__);
824 return 0;
825 }
826 (*obj)->syntax.ul[0] = ul;
827 break;
828 default:
829 kfree(id);
830 return 0;
831 }
832
833 (*obj)->syntax_len = len;
834 (*obj)->type = type;
835 (*obj)->id = id;
836 (*obj)->id_len = idlen;
837
838 if (!asn1_eoc_decode(ctx, eoc)) {
839 kfree(id);
840 kfree(*obj);
841 *obj = NULL;
842 return 0;
843 }
844 return 1;
845 }
846
847 static unsigned char snmp_request_decode(struct asn1_ctx *ctx,
848 struct snmp_request *request)
849 {
850 unsigned int cls, con, tag;
851 unsigned char *end;
852
853 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
854 return 0;
855
856 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
857 return 0;
858
859 if (!asn1_ulong_decode(ctx, end, &request->id))
860 return 0;
861
862 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
863 return 0;
864
865 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
866 return 0;
867
868 if (!asn1_uint_decode(ctx, end, &request->error_status))
869 return 0;
870
871 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
872 return 0;
873
874 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
875 return 0;
876
877 if (!asn1_uint_decode(ctx, end, &request->error_index))
878 return 0;
879
880 return 1;
881 }
882
883 /*
884 * Fast checksum update for possibly oddly-aligned UDP byte, from the
885 * code example in the draft.
886 */
887 static void fast_csum(__sum16 *csum,
888 const unsigned char *optr,
889 const unsigned char *nptr,
890 int offset)
891 {
892 unsigned char s[4];
893
894 if (offset & 1) {
895 s[0] = s[2] = 0;
896 s[1] = ~*optr;
897 s[3] = *nptr;
898 } else {
899 s[1] = s[3] = 0;
900 s[0] = ~*optr;
901 s[2] = *nptr;
902 }
903
904 *csum = csum_fold(csum_partial(s, 4, ~csum_unfold(*csum)));
905 }
906
907 /*
908 * Mangle IP address.
909 * - begin points to the start of the snmp messgae
910 * - addr points to the start of the address
911 */
912 static inline void mangle_address(unsigned char *begin,
913 unsigned char *addr,
914 const struct oct1_map *map,
915 __sum16 *check)
916 {
917 if (map->from == NOCT1(addr)) {
918 u_int32_t old;
919
920 if (debug)
921 memcpy(&old, addr, sizeof(old));
922
923 *addr = map->to;
924
925 /* Update UDP checksum if being used */
926 if (*check) {
927 fast_csum(check,
928 &map->from, &map->to, addr - begin);
929
930 }
931
932 if (debug)
933 printk(KERN_DEBUG "bsalg: mapped %pI4 to %pI4\n",
934 &old, addr);
935 }
936 }
937
938 static unsigned char snmp_trap_decode(struct asn1_ctx *ctx,
939 struct snmp_v1_trap *trap,
940 const struct oct1_map *map,
941 __sum16 *check)
942 {
943 unsigned int cls, con, tag, len;
944 unsigned char *end;
945
946 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
947 return 0;
948
949 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
950 return 0;
951
952 if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
953 return 0;
954
955 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
956 goto err_id_free;
957
958 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
959 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS)))
960 goto err_id_free;
961
962 if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
963 goto err_id_free;
964
965 /* IPv4 only */
966 if (len != 4)
967 goto err_addr_free;
968
969 mangle_address(ctx->begin, ctx->pointer - 4, map, check);
970
971 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
972 goto err_addr_free;
973
974 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
975 goto err_addr_free;
976
977 if (!asn1_uint_decode(ctx, end, &trap->general))
978 goto err_addr_free;
979
980 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
981 goto err_addr_free;
982
983 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
984 goto err_addr_free;
985
986 if (!asn1_uint_decode(ctx, end, &trap->specific))
987 goto err_addr_free;
988
989 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
990 goto err_addr_free;
991
992 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
993 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT)))
994 goto err_addr_free;
995
996 if (!asn1_ulong_decode(ctx, end, &trap->time))
997 goto err_addr_free;
998
999 return 1;
1000
1001 err_addr_free:
1002 kfree((unsigned long *)trap->ip_address);
1003
1004 err_id_free:
1005 kfree(trap->id);
1006
1007 return 0;
1008 }
1009
1010 /*****************************************************************************
1011 *
1012 * Misc. routines
1013 *
1014 *****************************************************************************/
1015
1016 static void hex_dump(const unsigned char *buf, size_t len)
1017 {
1018 size_t i;
1019
1020 for (i = 0; i < len; i++) {
1021 if (i && !(i % 16))
1022 printk("\n");
1023 printk("%02x ", *(buf + i));
1024 }
1025 printk("\n");
1026 }
1027
1028 /*
1029 * Parse and mangle SNMP message according to mapping.
1030 * (And this is the fucking 'basic' method).
1031 */
1032 static int snmp_parse_mangle(unsigned char *msg,
1033 u_int16_t len,
1034 const struct oct1_map *map,
1035 __sum16 *check)
1036 {
1037 unsigned char *eoc, *end;
1038 unsigned int cls, con, tag, vers, pdutype;
1039 struct asn1_ctx ctx;
1040 struct asn1_octstr comm;
1041 struct snmp_object **obj;
1042
1043 if (debug > 1)
1044 hex_dump(msg, len);
1045
1046 asn1_open(&ctx, msg, len);
1047
1048 /*
1049 * Start of SNMP message.
1050 */
1051 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1052 return 0;
1053 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1054 return 0;
1055
1056 /*
1057 * Version 1 or 2 handled.
1058 */
1059 if (!asn1_header_decode(&ctx, &end, &cls, &con, &tag))
1060 return 0;
1061 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
1062 return 0;
1063 if (!asn1_uint_decode (&ctx, end, &vers))
1064 return 0;
1065 if (debug > 1)
1066 printk(KERN_DEBUG "bsalg: snmp version: %u\n", vers + 1);
1067 if (vers > 1)
1068 return 1;
1069
1070 /*
1071 * Community.
1072 */
1073 if (!asn1_header_decode (&ctx, &end, &cls, &con, &tag))
1074 return 0;
1075 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OTS)
1076 return 0;
1077 if (!asn1_octets_decode(&ctx, end, &comm.data, &comm.len))
1078 return 0;
1079 if (debug > 1) {
1080 unsigned int i;
1081
1082 printk(KERN_DEBUG "bsalg: community: ");
1083 for (i = 0; i < comm.len; i++)
1084 printk("%c", comm.data[i]);
1085 printk("\n");
1086 }
1087 kfree(comm.data);
1088
1089 /*
1090 * PDU type
1091 */
1092 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &pdutype))
1093 return 0;
1094 if (cls != ASN1_CTX || con != ASN1_CON)
1095 return 0;
1096 if (debug > 1) {
1097 static const unsigned char *const pdus[] = {
1098 [SNMP_PDU_GET] = "get",
1099 [SNMP_PDU_NEXT] = "get-next",
1100 [SNMP_PDU_RESPONSE] = "response",
1101 [SNMP_PDU_SET] = "set",
1102 [SNMP_PDU_TRAP1] = "trapv1",
1103 [SNMP_PDU_BULK] = "bulk",
1104 [SNMP_PDU_INFORM] = "inform",
1105 [SNMP_PDU_TRAP2] = "trapv2"
1106 };
1107
1108 if (pdutype > SNMP_PDU_TRAP2)
1109 printk(KERN_DEBUG "bsalg: bad pdu type %u\n", pdutype);
1110 else
1111 printk(KERN_DEBUG "bsalg: pdu: %s\n", pdus[pdutype]);
1112 }
1113 if (pdutype != SNMP_PDU_RESPONSE &&
1114 pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
1115 return 1;
1116
1117 /*
1118 * Request header or v1 trap
1119 */
1120 if (pdutype == SNMP_PDU_TRAP1) {
1121 struct snmp_v1_trap trap;
1122 unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
1123
1124 if (ret) {
1125 kfree(trap.id);
1126 kfree((unsigned long *)trap.ip_address);
1127 } else
1128 return ret;
1129
1130 } else {
1131 struct snmp_request req;
1132
1133 if (!snmp_request_decode(&ctx, &req))
1134 return 0;
1135
1136 if (debug > 1)
1137 printk(KERN_DEBUG "bsalg: request: id=0x%lx error_status=%u "
1138 "error_index=%u\n", req.id, req.error_status,
1139 req.error_index);
1140 }
1141
1142 /*
1143 * Loop through objects, look for IP addresses to mangle.
1144 */
1145 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1146 return 0;
1147
1148 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1149 return 0;
1150
1151 obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
1152 if (obj == NULL) {
1153 if (net_ratelimit())
1154 printk(KERN_WARNING "OOM in bsalg(%d)\n", __LINE__);
1155 return 0;
1156 }
1157
1158 while (!asn1_eoc_decode(&ctx, eoc)) {
1159 unsigned int i;
1160
1161 if (!snmp_object_decode(&ctx, obj)) {
1162 if (*obj) {
1163 kfree((*obj)->id);
1164 kfree(*obj);
1165 }
1166 kfree(obj);
1167 return 0;
1168 }
1169
1170 if (debug > 1) {
1171 printk(KERN_DEBUG "bsalg: object: ");
1172 for (i = 0; i < (*obj)->id_len; i++) {
1173 if (i > 0)
1174 printk(".");
1175 printk("%lu", (*obj)->id[i]);
1176 }
1177 printk(": type=%u\n", (*obj)->type);
1178
1179 }
1180
1181 if ((*obj)->type == SNMP_IPADDR)
1182 mangle_address(ctx.begin, ctx.pointer - 4 , map, check);
1183
1184 kfree((*obj)->id);
1185 kfree(*obj);
1186 }
1187 kfree(obj);
1188
1189 if (!asn1_eoc_decode(&ctx, eoc))
1190 return 0;
1191
1192 return 1;
1193 }
1194
1195 /*****************************************************************************
1196 *
1197 * NAT routines.
1198 *
1199 *****************************************************************************/
1200
1201 /*
1202 * SNMP translation routine.
1203 */
1204 static int snmp_translate(struct nf_conn *ct,
1205 enum ip_conntrack_info ctinfo,
1206 struct sk_buff *skb)
1207 {
1208 struct iphdr *iph = ip_hdr(skb);
1209 struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
1210 u_int16_t udplen = ntohs(udph->len);
1211 u_int16_t paylen = udplen - sizeof(struct udphdr);
1212 int dir = CTINFO2DIR(ctinfo);
1213 struct oct1_map map;
1214
1215 /*
1216 * Determine mappping for application layer addresses based
1217 * on NAT manipulations for the packet.
1218 */
1219 if (dir == IP_CT_DIR_ORIGINAL) {
1220 /* SNAT traps */
1221 map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
1222 map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
1223 } else {
1224 /* DNAT replies */
1225 map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
1226 map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
1227 }
1228
1229 if (map.from == map.to)
1230 return NF_ACCEPT;
1231
1232 if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
1233 paylen, &map, &udph->check)) {
1234 if (net_ratelimit())
1235 printk(KERN_WARNING "bsalg: parser failed\n");
1236 return NF_DROP;
1237 }
1238 return NF_ACCEPT;
1239 }
1240
1241 /* We don't actually set up expectations, just adjust internal IP
1242 * addresses if this is being NATted */
1243 static int help(struct sk_buff *skb, unsigned int protoff,
1244 struct nf_conn *ct,
1245 enum ip_conntrack_info ctinfo)
1246 {
1247 int dir = CTINFO2DIR(ctinfo);
1248 unsigned int ret;
1249 const struct iphdr *iph = ip_hdr(skb);
1250 const struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
1251
1252 /* SNMP replies and originating SNMP traps get mangled */
1253 if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
1254 return NF_ACCEPT;
1255 if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
1256 return NF_ACCEPT;
1257
1258 /* No NAT? */
1259 if (!(ct->status & IPS_NAT_MASK))
1260 return NF_ACCEPT;
1261
1262 /*
1263 * Make sure the packet length is ok. So far, we were only guaranteed
1264 * to have a valid length IP header plus 8 bytes, which means we have
1265 * enough room for a UDP header. Just verify the UDP length field so we
1266 * can mess around with the payload.
1267 */
1268 if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
1269 if (net_ratelimit())
1270 printk(KERN_WARNING "SNMP: dropping malformed packet src=%pI4 dst=%pI4\n",
1271 &iph->saddr, &iph->daddr);
1272 return NF_DROP;
1273 }
1274
1275 if (!skb_make_writable(skb, skb->len))
1276 return NF_DROP;
1277
1278 spin_lock_bh(&snmp_lock);
1279 ret = snmp_translate(ct, ctinfo, skb);
1280 spin_unlock_bh(&snmp_lock);
1281 return ret;
1282 }
1283
1284 static const struct nf_conntrack_expect_policy snmp_exp_policy = {
1285 .max_expected = 0,
1286 .timeout = 180,
1287 };
1288
1289 static struct nf_conntrack_helper snmp_helper __read_mostly = {
1290 .me = THIS_MODULE,
1291 .help = help,
1292 .expect_policy = &snmp_exp_policy,
1293 .name = "snmp",
1294 .tuple.src.l3num = AF_INET,
1295 .tuple.src.u.udp.port = __constant_htons(SNMP_PORT),
1296 .tuple.dst.protonum = IPPROTO_UDP,
1297 };
1298
1299 static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
1300 .me = THIS_MODULE,
1301 .help = help,
1302 .expect_policy = &snmp_exp_policy,
1303 .name = "snmp_trap",
1304 .tuple.src.l3num = AF_INET,
1305 .tuple.src.u.udp.port = __constant_htons(SNMP_TRAP_PORT),
1306 .tuple.dst.protonum = IPPROTO_UDP,
1307 };
1308
1309 /*****************************************************************************
1310 *
1311 * Module stuff.
1312 *
1313 *****************************************************************************/
1314
1315 static int __init nf_nat_snmp_basic_init(void)
1316 {
1317 int ret = 0;
1318
1319 ret = nf_conntrack_helper_register(&snmp_helper);
1320 if (ret < 0)
1321 return ret;
1322 ret = nf_conntrack_helper_register(&snmp_trap_helper);
1323 if (ret < 0) {
1324 nf_conntrack_helper_unregister(&snmp_helper);
1325 return ret;
1326 }
1327 return ret;
1328 }
1329
1330 static void __exit nf_nat_snmp_basic_fini(void)
1331 {
1332 nf_conntrack_helper_unregister(&snmp_helper);
1333 nf_conntrack_helper_unregister(&snmp_trap_helper);
1334 }
1335
1336 module_init(nf_nat_snmp_basic_init);
1337 module_exit(nf_nat_snmp_basic_fini);
1338
1339 module_param(debug, int, 0600);
This page took 0.191998 seconds and 6 git commands to generate.