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