]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv4/netfilter/nf_nat_snmp_basic.c
rcu: convert uses of rcu_assign_pointer(x, NULL) to RCU_INIT_POINTER
[mirror_ubuntu-bionic-kernel.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>
5a0e3ad6 46#include <linux/slab.h>
807467c2
PM
47#include <linux/in.h>
48#include <linux/ip.h>
49#include <linux/udp.h>
50#include <net/checksum.h>
51#include <net/udp.h>
52
53#include <net/netfilter/nf_nat.h>
6002f266 54#include <net/netfilter/nf_conntrack_expect.h>
807467c2
PM
55#include <net/netfilter/nf_conntrack_helper.h>
56#include <net/netfilter/nf_nat_helper.h>
93557f53 57#include <linux/netfilter/nf_conntrack_snmp.h>
807467c2
PM
58
59MODULE_LICENSE("GPL");
60MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
61MODULE_DESCRIPTION("Basic SNMP Application Layer Gateway");
62MODULE_ALIAS("ip_nat_snmp_basic");
63
64#define SNMP_PORT 161
65#define SNMP_TRAP_PORT 162
e79ec50b 66#define NOCT1(n) (*(u8 *)(n))
807467c2
PM
67
68static int debug;
69static DEFINE_SPINLOCK(snmp_lock);
70
71/*
72 * Application layer address mapping mimics the NAT mapping, but
73 * only for the first octet in this case (a more flexible system
74 * can be implemented if needed).
75 */
76struct oct1_map
77{
78 u_int8_t from;
79 u_int8_t to;
80};
81
82
83/*****************************************************************************
84 *
85 * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
86 *
87 *****************************************************************************/
88
89/* Class */
90#define ASN1_UNI 0 /* Universal */
91#define ASN1_APL 1 /* Application */
92#define ASN1_CTX 2 /* Context */
93#define ASN1_PRV 3 /* Private */
94
95/* Tag */
96#define ASN1_EOC 0 /* End Of Contents */
97#define ASN1_BOL 1 /* Boolean */
98#define ASN1_INT 2 /* Integer */
99#define ASN1_BTS 3 /* Bit String */
100#define ASN1_OTS 4 /* Octet String */
101#define ASN1_NUL 5 /* Null */
102#define ASN1_OJI 6 /* Object Identifier */
103#define ASN1_OJD 7 /* Object Description */
104#define ASN1_EXT 8 /* External */
105#define ASN1_SEQ 16 /* Sequence */
106#define ASN1_SET 17 /* Set */
107#define ASN1_NUMSTR 18 /* Numerical String */
108#define ASN1_PRNSTR 19 /* Printable String */
109#define ASN1_TEXSTR 20 /* Teletext String */
110#define ASN1_VIDSTR 21 /* Video String */
111#define ASN1_IA5STR 22 /* IA5 String */
112#define ASN1_UNITIM 23 /* Universal Time */
113#define ASN1_GENTIM 24 /* General Time */
114#define ASN1_GRASTR 25 /* Graphical String */
115#define ASN1_VISSTR 26 /* Visible String */
116#define ASN1_GENSTR 27 /* General String */
117
118/* Primitive / Constructed methods*/
119#define ASN1_PRI 0 /* Primitive */
120#define ASN1_CON 1 /* Constructed */
121
122/*
123 * Error codes.
124 */
125#define ASN1_ERR_NOERROR 0
126#define ASN1_ERR_DEC_EMPTY 2
127#define ASN1_ERR_DEC_EOC_MISMATCH 3
128#define ASN1_ERR_DEC_LENGTH_MISMATCH 4
129#define ASN1_ERR_DEC_BADVALUE 5
130
131/*
132 * ASN.1 context.
133 */
134struct asn1_ctx
135{
136 int error; /* Error condition */
137 unsigned char *pointer; /* Octet just to be decoded */
138 unsigned char *begin; /* First octet */
139 unsigned char *end; /* Octet after last octet */
140};
141
142/*
143 * Octet string (not null terminated)
144 */
145struct asn1_octstr
146{
147 unsigned char *data;
148 unsigned int len;
149};
150
151static void asn1_open(struct asn1_ctx *ctx,
e905a9ed
YH
152 unsigned char *buf,
153 unsigned int len)
807467c2
PM
154{
155 ctx->begin = buf;
156 ctx->end = buf + len;
157 ctx->pointer = buf;
158 ctx->error = ASN1_ERR_NOERROR;
159}
160
161static unsigned char asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
162{
163 if (ctx->pointer >= ctx->end) {
164 ctx->error = ASN1_ERR_DEC_EMPTY;
165 return 0;
166 }
167 *ch = *(ctx->pointer)++;
168 return 1;
169}
170
171static unsigned char asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
172{
173 unsigned char ch;
174
175 *tag = 0;
176
177 do
178 {
179 if (!asn1_octet_decode(ctx, &ch))
180 return 0;
181 *tag <<= 7;
182 *tag |= ch & 0x7F;
183 } while ((ch & 0x80) == 0x80);
184 return 1;
185}
186
187static unsigned char asn1_id_decode(struct asn1_ctx *ctx,
e905a9ed
YH
188 unsigned int *cls,
189 unsigned int *con,
190 unsigned int *tag)
807467c2
PM
191{
192 unsigned char ch;
193
194 if (!asn1_octet_decode(ctx, &ch))
195 return 0;
196
197 *cls = (ch & 0xC0) >> 6;
198 *con = (ch & 0x20) >> 5;
199 *tag = (ch & 0x1F);
200
201 if (*tag == 0x1F) {
202 if (!asn1_tag_decode(ctx, tag))
203 return 0;
204 }
205 return 1;
206}
207
208static unsigned char asn1_length_decode(struct asn1_ctx *ctx,
e905a9ed
YH
209 unsigned int *def,
210 unsigned int *len)
807467c2
PM
211{
212 unsigned char ch, cnt;
213
214 if (!asn1_octet_decode(ctx, &ch))
215 return 0;
216
217 if (ch == 0x80)
218 *def = 0;
219 else {
220 *def = 1;
221
222 if (ch < 0x80)
223 *len = ch;
224 else {
72b72949 225 cnt = ch & 0x7F;
807467c2
PM
226 *len = 0;
227
228 while (cnt > 0) {
229 if (!asn1_octet_decode(ctx, &ch))
230 return 0;
231 *len <<= 8;
232 *len |= ch;
233 cnt--;
234 }
235 }
236 }
ddb2c435
CW
237
238 /* don't trust len bigger than ctx buffer */
239 if (*len > ctx->end - ctx->pointer)
240 return 0;
241
807467c2
PM
242 return 1;
243}
244
245static unsigned char asn1_header_decode(struct asn1_ctx *ctx,
e905a9ed
YH
246 unsigned char **eoc,
247 unsigned int *cls,
248 unsigned int *con,
249 unsigned int *tag)
807467c2
PM
250{
251 unsigned int def, len;
252
253 if (!asn1_id_decode(ctx, cls, con, tag))
254 return 0;
255
256 def = len = 0;
257 if (!asn1_length_decode(ctx, &def, &len))
258 return 0;
259
ddb2c435
CW
260 /* primitive shall be definite, indefinite shall be constructed */
261 if (*con == ASN1_PRI && !def)
262 return 0;
263
807467c2
PM
264 if (def)
265 *eoc = ctx->pointer + len;
266 else
267 *eoc = NULL;
268 return 1;
269}
270
271static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
272{
273 unsigned char ch;
274
06aa1072 275 if (eoc == NULL) {
807467c2
PM
276 if (!asn1_octet_decode(ctx, &ch))
277 return 0;
278
279 if (ch != 0x00) {
280 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
281 return 0;
282 }
283
284 if (!asn1_octet_decode(ctx, &ch))
285 return 0;
286
287 if (ch != 0x00) {
288 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
289 return 0;
290 }
291 return 1;
292 } else {
293 if (ctx->pointer != eoc) {
294 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
295 return 0;
296 }
297 return 1;
298 }
299}
300
301static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
302{
303 ctx->pointer = eoc;
304 return 1;
305}
306
307static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
e905a9ed
YH
308 unsigned char *eoc,
309 long *integer)
807467c2
PM
310{
311 unsigned char ch;
312 unsigned int len;
313
314 if (!asn1_octet_decode(ctx, &ch))
315 return 0;
316
317 *integer = (signed char) ch;
318 len = 1;
319
320 while (ctx->pointer < eoc) {
321 if (++len > sizeof (long)) {
322 ctx->error = ASN1_ERR_DEC_BADVALUE;
323 return 0;
324 }
325
326 if (!asn1_octet_decode(ctx, &ch))
327 return 0;
328
329 *integer <<= 8;
330 *integer |= ch;
331 }
332 return 1;
333}
334
335static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
e905a9ed
YH
336 unsigned char *eoc,
337 unsigned int *integer)
807467c2
PM
338{
339 unsigned char ch;
340 unsigned int len;
341
342 if (!asn1_octet_decode(ctx, &ch))
343 return 0;
344
345 *integer = ch;
346 if (ch == 0) len = 0;
347 else len = 1;
348
349 while (ctx->pointer < eoc) {
350 if (++len > sizeof (unsigned int)) {
351 ctx->error = ASN1_ERR_DEC_BADVALUE;
352 return 0;
353 }
354
355 if (!asn1_octet_decode(ctx, &ch))
356 return 0;
357
358 *integer <<= 8;
359 *integer |= ch;
360 }
361 return 1;
362}
363
364static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
e905a9ed
YH
365 unsigned char *eoc,
366 unsigned long *integer)
807467c2
PM
367{
368 unsigned char ch;
369 unsigned int len;
370
371 if (!asn1_octet_decode(ctx, &ch))
372 return 0;
373
374 *integer = ch;
375 if (ch == 0) len = 0;
376 else len = 1;
377
378 while (ctx->pointer < eoc) {
379 if (++len > sizeof (unsigned long)) {
380 ctx->error = ASN1_ERR_DEC_BADVALUE;
381 return 0;
382 }
383
384 if (!asn1_octet_decode(ctx, &ch))
385 return 0;
386
387 *integer <<= 8;
388 *integer |= ch;
389 }
390 return 1;
391}
392
393static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
e905a9ed
YH
394 unsigned char *eoc,
395 unsigned char **octets,
396 unsigned int *len)
807467c2
PM
397{
398 unsigned char *ptr;
399
400 *len = 0;
401
402 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
403 if (*octets == NULL) {
404 if (net_ratelimit())
654d0fbd 405 pr_notice("OOM in bsalg (%d)\n", __LINE__);
807467c2
PM
406 return 0;
407 }
408
409 ptr = *octets;
410 while (ctx->pointer < eoc) {
411 if (!asn1_octet_decode(ctx, (unsigned char *)ptr++)) {
412 kfree(*octets);
413 *octets = NULL;
414 return 0;
415 }
416 (*len)++;
417 }
418 return 1;
419}
420
421static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
e905a9ed 422 unsigned long *subid)
807467c2
PM
423{
424 unsigned char ch;
425
426 *subid = 0;
427
428 do {
429 if (!asn1_octet_decode(ctx, &ch))
430 return 0;
431
432 *subid <<= 7;
433 *subid |= ch & 0x7F;
434 } while ((ch & 0x80) == 0x80);
435 return 1;
436}
437
438static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
e905a9ed
YH
439 unsigned char *eoc,
440 unsigned long **oid,
441 unsigned int *len)
807467c2
PM
442{
443 unsigned long subid;
807467c2 444 unsigned long *optr;
252815b0 445 size_t size;
807467c2
PM
446
447 size = eoc - ctx->pointer + 1;
ddb2c435
CW
448
449 /* first subid actually encodes first two subids */
450 if (size < 2 || size > ULONG_MAX/sizeof(unsigned long))
451 return 0;
452
807467c2
PM
453 *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
454 if (*oid == NULL) {
455 if (net_ratelimit())
654d0fbd 456 pr_notice("OOM in bsalg (%d)\n", __LINE__);
807467c2
PM
457 return 0;
458 }
459
460 optr = *oid;
461
462 if (!asn1_subid_decode(ctx, &subid)) {
463 kfree(*oid);
464 *oid = NULL;
465 return 0;
466 }
467
468 if (subid < 40) {
469 optr [0] = 0;
470 optr [1] = subid;
471 } else if (subid < 80) {
472 optr [0] = 1;
473 optr [1] = subid - 40;
474 } else {
475 optr [0] = 2;
476 optr [1] = subid - 80;
477 }
478
479 *len = 2;
480 optr += 2;
481
482 while (ctx->pointer < eoc) {
483 if (++(*len) > size) {
484 ctx->error = ASN1_ERR_DEC_BADVALUE;
485 kfree(*oid);
486 *oid = NULL;
487 return 0;
488 }
489
490 if (!asn1_subid_decode(ctx, optr++)) {
491 kfree(*oid);
492 *oid = NULL;
493 return 0;
494 }
495 }
496 return 1;
497}
498
499/*****************************************************************************
500 *
501 * SNMP decoding routines (gxsnmp author Dirk Wisse)
502 *
503 *****************************************************************************/
504
505/* SNMP Versions */
506#define SNMP_V1 0
507#define SNMP_V2C 1
508#define SNMP_V2 2
509#define SNMP_V3 3
510
511/* Default Sizes */
512#define SNMP_SIZE_COMM 256
513#define SNMP_SIZE_OBJECTID 128
514#define SNMP_SIZE_BUFCHR 256
515#define SNMP_SIZE_BUFINT 128
516#define SNMP_SIZE_SMALLOBJECTID 16
517
518/* Requests */
519#define SNMP_PDU_GET 0
520#define SNMP_PDU_NEXT 1
521#define SNMP_PDU_RESPONSE 2
522#define SNMP_PDU_SET 3
523#define SNMP_PDU_TRAP1 4
524#define SNMP_PDU_BULK 5
525#define SNMP_PDU_INFORM 6
526#define SNMP_PDU_TRAP2 7
527
528/* Errors */
529#define SNMP_NOERROR 0
530#define SNMP_TOOBIG 1
531#define SNMP_NOSUCHNAME 2
532#define SNMP_BADVALUE 3
533#define SNMP_READONLY 4
534#define SNMP_GENERROR 5
535#define SNMP_NOACCESS 6
536#define SNMP_WRONGTYPE 7
537#define SNMP_WRONGLENGTH 8
538#define SNMP_WRONGENCODING 9
539#define SNMP_WRONGVALUE 10
540#define SNMP_NOCREATION 11
541#define SNMP_INCONSISTENTVALUE 12
542#define SNMP_RESOURCEUNAVAILABLE 13
543#define SNMP_COMMITFAILED 14
544#define SNMP_UNDOFAILED 15
545#define SNMP_AUTHORIZATIONERROR 16
546#define SNMP_NOTWRITABLE 17
547#define SNMP_INCONSISTENTNAME 18
548
549/* General SNMP V1 Traps */
550#define SNMP_TRAP_COLDSTART 0
551#define SNMP_TRAP_WARMSTART 1
552#define SNMP_TRAP_LINKDOWN 2
553#define SNMP_TRAP_LINKUP 3
554#define SNMP_TRAP_AUTFAILURE 4
555#define SNMP_TRAP_EQPNEIGHBORLOSS 5
556#define SNMP_TRAP_ENTSPECIFIC 6
557
558/* SNMPv1 Types */
559#define SNMP_NULL 0
560#define SNMP_INTEGER 1 /* l */
561#define SNMP_OCTETSTR 2 /* c */
562#define SNMP_DISPLAYSTR 2 /* c */
563#define SNMP_OBJECTID 3 /* ul */
564#define SNMP_IPADDR 4 /* uc */
565#define SNMP_COUNTER 5 /* ul */
566#define SNMP_GAUGE 6 /* ul */
567#define SNMP_TIMETICKS 7 /* ul */
568#define SNMP_OPAQUE 8 /* c */
569
570/* Additional SNMPv2 Types */
571#define SNMP_UINTEGER 5 /* ul */
572#define SNMP_BITSTR 9 /* uc */
573#define SNMP_NSAP 10 /* uc */
574#define SNMP_COUNTER64 11 /* ul */
575#define SNMP_NOSUCHOBJECT 12
576#define SNMP_NOSUCHINSTANCE 13
577#define SNMP_ENDOFMIBVIEW 14
578
579union snmp_syntax
580{
581 unsigned char uc[0]; /* 8 bit unsigned */
582 char c[0]; /* 8 bit signed */
583 unsigned long ul[0]; /* 32 bit unsigned */
584 long l[0]; /* 32 bit signed */
585};
586
587struct snmp_object
588{
589 unsigned long *id;
590 unsigned int id_len;
591 unsigned short type;
592 unsigned int syntax_len;
593 union snmp_syntax syntax;
594};
595
596struct snmp_request
597{
598 unsigned long id;
599 unsigned int error_status;
600 unsigned int error_index;
601};
602
603struct snmp_v1_trap
604{
605 unsigned long *id;
606 unsigned int id_len;
607 unsigned long ip_address; /* pointer */
608 unsigned int general;
609 unsigned int specific;
610 unsigned long time;
611};
612
613/* SNMP types */
614#define SNMP_IPA 0
615#define SNMP_CNT 1
616#define SNMP_GGE 2
617#define SNMP_TIT 3
618#define SNMP_OPQ 4
619#define SNMP_C64 6
620
621/* SNMP errors */
622#define SERR_NSO 0
623#define SERR_NSI 1
624#define SERR_EOM 2
625
626static inline void mangle_address(unsigned char *begin,
e905a9ed
YH
627 unsigned char *addr,
628 const struct oct1_map *map,
629 __sum16 *check);
807467c2
PM
630struct snmp_cnv
631{
632 unsigned int class;
633 unsigned int tag;
634 int syntax;
635};
636
72b72949 637static const struct snmp_cnv snmp_conv[] = {
807467c2
PM
638 {ASN1_UNI, ASN1_NUL, SNMP_NULL},
639 {ASN1_UNI, ASN1_INT, SNMP_INTEGER},
640 {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR},
641 {ASN1_UNI, ASN1_OTS, SNMP_DISPLAYSTR},
642 {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID},
643 {ASN1_APL, SNMP_IPA, SNMP_IPADDR},
644 {ASN1_APL, SNMP_CNT, SNMP_COUNTER}, /* Counter32 */
645 {ASN1_APL, SNMP_GGE, SNMP_GAUGE}, /* Gauge32 == Unsigned32 */
646 {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS},
647 {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE},
648
649 /* SNMPv2 data types and errors */
650 {ASN1_UNI, ASN1_BTS, SNMP_BITSTR},
651 {ASN1_APL, SNMP_C64, SNMP_COUNTER64},
652 {ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT},
653 {ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE},
654 {ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW},
655 {0, 0, -1}
656};
657
658static unsigned char snmp_tag_cls2syntax(unsigned int tag,
e905a9ed
YH
659 unsigned int cls,
660 unsigned short *syntax)
807467c2 661{
72b72949 662 const struct snmp_cnv *cnv;
807467c2
PM
663
664 cnv = snmp_conv;
665
666 while (cnv->syntax != -1) {
667 if (cnv->tag == tag && cnv->class == cls) {
668 *syntax = cnv->syntax;
669 return 1;
670 }
671 cnv++;
672 }
673 return 0;
674}
675
676static unsigned char snmp_object_decode(struct asn1_ctx *ctx,
e905a9ed 677 struct snmp_object **obj)
807467c2
PM
678{
679 unsigned int cls, con, tag, len, idlen;
680 unsigned short type;
681 unsigned char *eoc, *end, *p;
682 unsigned long *lp, *id;
683 unsigned long ul;
684 long l;
685
686 *obj = NULL;
687 id = NULL;
688
689 if (!asn1_header_decode(ctx, &eoc, &cls, &con, &tag))
690 return 0;
691
692 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
693 return 0;
694
695 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
696 return 0;
697
698 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
699 return 0;
700
701 if (!asn1_oid_decode(ctx, end, &id, &idlen))
702 return 0;
703
704 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag)) {
705 kfree(id);
706 return 0;
707 }
708
709 if (con != ASN1_PRI) {
710 kfree(id);
711 return 0;
712 }
713
714 type = 0;
715 if (!snmp_tag_cls2syntax(tag, cls, &type)) {
716 kfree(id);
717 return 0;
718 }
719
720 l = 0;
721 switch (type) {
181b1e9c
JP
722 case SNMP_INTEGER:
723 len = sizeof(long);
724 if (!asn1_long_decode(ctx, end, &l)) {
725 kfree(id);
726 return 0;
727 }
728 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
729 if (*obj == NULL) {
730 kfree(id);
731 if (net_ratelimit())
732 pr_notice("OOM in bsalg (%d)\n", __LINE__);
733 return 0;
734 }
735 (*obj)->syntax.l[0] = l;
736 break;
737 case SNMP_OCTETSTR:
738 case SNMP_OPAQUE:
739 if (!asn1_octets_decode(ctx, end, &p, &len)) {
740 kfree(id);
741 return 0;
742 }
743 *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
744 if (*obj == NULL) {
807467c2 745 kfree(p);
181b1e9c
JP
746 kfree(id);
747 if (net_ratelimit())
748 pr_notice("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 pr_notice("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) {
807467c2 781 kfree(lp);
181b1e9c
JP
782 kfree(id);
783 if (net_ratelimit())
784 pr_notice("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) {
807467c2 796 kfree(p);
807467c2
PM
797 kfree(id);
798 return 0;
181b1e9c
JP
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 pr_notice("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 pr_notice("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;
807467c2
PM
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
847static unsigned char snmp_request_decode(struct asn1_ctx *ctx,
e905a9ed 848 struct snmp_request *request)
807467c2
PM
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 */
887static void fast_csum(__sum16 *csum,
e905a9ed
YH
888 const unsigned char *optr,
889 const unsigned char *nptr,
890 int offset)
807467c2
PM
891{
892 unsigned char s[4];
893
894 if (offset & 1) {
d6120b8a 895 s[0] = ~0;
807467c2 896 s[1] = ~*optr;
d6120b8a 897 s[2] = 0;
807467c2
PM
898 s[3] = *nptr;
899 } else {
807467c2 900 s[0] = ~*optr;
d6120b8a 901 s[1] = ~0;
807467c2 902 s[2] = *nptr;
d6120b8a 903 s[3] = 0;
807467c2
PM
904 }
905
906 *csum = csum_fold(csum_partial(s, 4, ~csum_unfold(*csum)));
907}
908
909/*
910 * Mangle IP address.
911 * - begin points to the start of the snmp messgae
912 * - addr points to the start of the address
913 */
914static inline void mangle_address(unsigned char *begin,
e905a9ed
YH
915 unsigned char *addr,
916 const struct oct1_map *map,
917 __sum16 *check)
807467c2
PM
918{
919 if (map->from == NOCT1(addr)) {
920 u_int32_t old;
921
922 if (debug)
72b72949 923 memcpy(&old, addr, sizeof(old));
807467c2
PM
924
925 *addr = map->to;
926
927 /* Update UDP checksum if being used */
928 if (*check) {
929 fast_csum(check,
e905a9ed 930 &map->from, &map->to, addr - begin);
807467c2
PM
931
932 }
933
934 if (debug)
cffee385
HH
935 printk(KERN_DEBUG "bsalg: mapped %pI4 to %pI4\n",
936 &old, addr);
807467c2
PM
937 }
938}
939
940static unsigned char snmp_trap_decode(struct asn1_ctx *ctx,
e905a9ed
YH
941 struct snmp_v1_trap *trap,
942 const struct oct1_map *map,
943 __sum16 *check)
807467c2
PM
944{
945 unsigned int cls, con, tag, len;
946 unsigned char *end;
947
948 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
949 return 0;
950
951 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
952 return 0;
953
954 if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
955 return 0;
956
957 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
958 goto err_id_free;
959
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;
963
964 if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
965 goto err_id_free;
966
967 /* IPv4 only */
968 if (len != 4)
969 goto err_addr_free;
970
971 mangle_address(ctx->begin, ctx->pointer - 4, map, check);
972
973 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
974 goto err_addr_free;
975
976 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
977 goto err_addr_free;
978
979 if (!asn1_uint_decode(ctx, end, &trap->general))
980 goto err_addr_free;
981
982 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
983 goto err_addr_free;
984
985 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
986 goto err_addr_free;
987
988 if (!asn1_uint_decode(ctx, end, &trap->specific))
989 goto err_addr_free;
990
991 if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
992 goto err_addr_free;
993
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;
997
998 if (!asn1_ulong_decode(ctx, end, &trap->time))
999 goto err_addr_free;
1000
1001 return 1;
1002
1003err_addr_free:
1004 kfree((unsigned long *)trap->ip_address);
1005
1006err_id_free:
1007 kfree(trap->id);
1008
1009 return 0;
1010}
1011
1012/*****************************************************************************
1013 *
1014 * Misc. routines
1015 *
1016 *****************************************************************************/
1017
72b72949 1018static void hex_dump(const unsigned char *buf, size_t len)
807467c2
PM
1019{
1020 size_t i;
1021
1022 for (i = 0; i < len; i++) {
1023 if (i && !(i % 16))
1024 printk("\n");
1025 printk("%02x ", *(buf + i));
1026 }
1027 printk("\n");
1028}
1029
1030/*
1031 * Parse and mangle SNMP message according to mapping.
1032 * (And this is the fucking 'basic' method).
1033 */
1034static int snmp_parse_mangle(unsigned char *msg,
e905a9ed
YH
1035 u_int16_t len,
1036 const struct oct1_map *map,
1037 __sum16 *check)
807467c2
PM
1038{
1039 unsigned char *eoc, *end;
1040 unsigned int cls, con, tag, vers, pdutype;
1041 struct asn1_ctx ctx;
1042 struct asn1_octstr comm;
71c3ebfd 1043 struct snmp_object *obj;
807467c2
PM
1044
1045 if (debug > 1)
1046 hex_dump(msg, len);
1047
1048 asn1_open(&ctx, msg, len);
1049
1050 /*
1051 * Start of SNMP message.
1052 */
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;
1057
1058 /*
1059 * Version 1 or 2 handled.
1060 */
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;
1071
1072 /*
1073 * Community.
1074 */
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;
1083
1084 printk(KERN_DEBUG "bsalg: community: ");
1085 for (i = 0; i < comm.len; i++)
1086 printk("%c", comm.data[i]);
1087 printk("\n");
1088 }
1089 kfree(comm.data);
1090
1091 /*
1092 * PDU type
1093 */
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) {
72b72949 1099 static const unsigned char *const pdus[] = {
807467c2
PM
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"
1108 };
1109
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]);
1114 }
1115 if (pdutype != SNMP_PDU_RESPONSE &&
1116 pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
1117 return 1;
1118
1119 /*
1120 * Request header or v1 trap
1121 */
1122 if (pdutype == SNMP_PDU_TRAP1) {
1123 struct snmp_v1_trap trap;
1124 unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
1125
1126 if (ret) {
1127 kfree(trap.id);
1128 kfree((unsigned long *)trap.ip_address);
1129 } else
1130 return ret;
1131
1132 } else {
1133 struct snmp_request req;
1134
1135 if (!snmp_request_decode(&ctx, &req))
1136 return 0;
1137
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);
1142 }
1143
1144 /*
1145 * Loop through objects, look for IP addresses to mangle.
1146 */
1147 if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
1148 return 0;
1149
1150 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
1151 return 0;
1152
807467c2
PM
1153 while (!asn1_eoc_decode(&ctx, eoc)) {
1154 unsigned int i;
1155
71c3ebfd
JL
1156 if (!snmp_object_decode(&ctx, &obj)) {
1157 if (obj) {
1158 kfree(obj->id);
1159 kfree(obj);
807467c2 1160 }
807467c2
PM
1161 return 0;
1162 }
1163
1164 if (debug > 1) {
1165 printk(KERN_DEBUG "bsalg: object: ");
71c3ebfd 1166 for (i = 0; i < obj->id_len; i++) {
807467c2
PM
1167 if (i > 0)
1168 printk(".");
71c3ebfd 1169 printk("%lu", obj->id[i]);
807467c2 1170 }
71c3ebfd 1171 printk(": type=%u\n", obj->type);
807467c2
PM
1172
1173 }
1174
71c3ebfd 1175 if (obj->type == SNMP_IPADDR)
807467c2
PM
1176 mangle_address(ctx.begin, ctx.pointer - 4 , map, check);
1177
71c3ebfd
JL
1178 kfree(obj->id);
1179 kfree(obj);
807467c2 1180 }
807467c2
PM
1181
1182 if (!asn1_eoc_decode(&ctx, eoc))
1183 return 0;
1184
1185 return 1;
1186}
1187
1188/*****************************************************************************
1189 *
1190 * NAT routines.
1191 *
1192 *****************************************************************************/
1193
1194/*
1195 * SNMP translation routine.
1196 */
1197static int snmp_translate(struct nf_conn *ct,
e905a9ed 1198 enum ip_conntrack_info ctinfo,
3db05fea 1199 struct sk_buff *skb)
807467c2 1200{
3db05fea 1201 struct iphdr *iph = ip_hdr(skb);
807467c2
PM
1202 struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
1203 u_int16_t udplen = ntohs(udph->len);
1204 u_int16_t paylen = udplen - sizeof(struct udphdr);
1205 int dir = CTINFO2DIR(ctinfo);
1206 struct oct1_map map;
1207
1208 /*
1209 * Determine mappping for application layer addresses based
1210 * on NAT manipulations for the packet.
1211 */
1212 if (dir == IP_CT_DIR_ORIGINAL) {
1213 /* SNAT traps */
1214 map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
1215 map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
1216 } else {
1217 /* DNAT replies */
1218 map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
1219 map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
1220 }
1221
1222 if (map.from == map.to)
1223 return NF_ACCEPT;
1224
1225 if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
e905a9ed 1226 paylen, &map, &udph->check)) {
807467c2
PM
1227 if (net_ratelimit())
1228 printk(KERN_WARNING "bsalg: parser failed\n");
1229 return NF_DROP;
1230 }
1231 return NF_ACCEPT;
1232}
1233
1234/* We don't actually set up expectations, just adjust internal IP
1235 * addresses if this is being NATted */
3db05fea 1236static int help(struct sk_buff *skb, unsigned int protoff,
807467c2
PM
1237 struct nf_conn *ct,
1238 enum ip_conntrack_info ctinfo)
1239{
1240 int dir = CTINFO2DIR(ctinfo);
1241 unsigned int ret;
72b72949
JE
1242 const struct iphdr *iph = ip_hdr(skb);
1243 const struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
807467c2
PM
1244
1245 /* SNMP replies and originating SNMP traps get mangled */
1246 if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
1247 return NF_ACCEPT;
1248 if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
1249 return NF_ACCEPT;
1250
1251 /* No NAT? */
1252 if (!(ct->status & IPS_NAT_MASK))
1253 return NF_ACCEPT;
1254
1255 /*
1256 * Make sure the packet length is ok. So far, we were only guaranteed
1257 * to have a valid length IP header plus 8 bytes, which means we have
1258 * enough room for a UDP header. Just verify the UDP length field so we
1259 * can mess around with the payload.
1260 */
3db05fea 1261 if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
807467c2 1262 if (net_ratelimit())
cffee385
HH
1263 printk(KERN_WARNING "SNMP: dropping malformed packet src=%pI4 dst=%pI4\n",
1264 &iph->saddr, &iph->daddr);
807467c2
PM
1265 return NF_DROP;
1266 }
1267
3db05fea 1268 if (!skb_make_writable(skb, skb->len))
807467c2
PM
1269 return NF_DROP;
1270
1271 spin_lock_bh(&snmp_lock);
3db05fea 1272 ret = snmp_translate(ct, ctinfo, skb);
807467c2
PM
1273 spin_unlock_bh(&snmp_lock);
1274 return ret;
1275}
1276
6002f266
PM
1277static const struct nf_conntrack_expect_policy snmp_exp_policy = {
1278 .max_expected = 0,
1279 .timeout = 180,
1280};
1281
807467c2 1282static struct nf_conntrack_helper snmp_helper __read_mostly = {
807467c2
PM
1283 .me = THIS_MODULE,
1284 .help = help,
6002f266 1285 .expect_policy = &snmp_exp_policy,
807467c2
PM
1286 .name = "snmp",
1287 .tuple.src.l3num = AF_INET,
09640e63 1288 .tuple.src.u.udp.port = cpu_to_be16(SNMP_PORT),
807467c2 1289 .tuple.dst.protonum = IPPROTO_UDP,
807467c2
PM
1290};
1291
1292static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
807467c2
PM
1293 .me = THIS_MODULE,
1294 .help = help,
6002f266 1295 .expect_policy = &snmp_exp_policy,
807467c2
PM
1296 .name = "snmp_trap",
1297 .tuple.src.l3num = AF_INET,
09640e63 1298 .tuple.src.u.udp.port = cpu_to_be16(SNMP_TRAP_PORT),
807467c2 1299 .tuple.dst.protonum = IPPROTO_UDP,
807467c2
PM
1300};
1301
1302/*****************************************************************************
1303 *
1304 * Module stuff.
1305 *
1306 *****************************************************************************/
1307
1308static int __init nf_nat_snmp_basic_init(void)
1309{
1310 int ret = 0;
1311
93557f53 1312 BUG_ON(nf_nat_snmp_hook != NULL);
a9b3cd7f 1313 RCU_INIT_POINTER(nf_nat_snmp_hook, help);
93557f53 1314
807467c2
PM
1315 ret = nf_conntrack_helper_register(&snmp_trap_helper);
1316 if (ret < 0) {
1317 nf_conntrack_helper_unregister(&snmp_helper);
1318 return ret;
1319 }
1320 return ret;
1321}
1322
1323static void __exit nf_nat_snmp_basic_fini(void)
1324{
a9b3cd7f 1325 RCU_INIT_POINTER(nf_nat_snmp_hook, NULL);
807467c2
PM
1326 nf_conntrack_helper_unregister(&snmp_trap_helper);
1327}
1328
1329module_init(nf_nat_snmp_basic_init);
1330module_exit(nf_nat_snmp_basic_fini);
1331
1332module_param(debug, int, 0600);