PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ipv4/netfilter/nf_nat_snmp_basic.c

https://github.com/mstsirkin/kvm
C | 1332 lines | 985 code | 199 blank | 148 comment | 296 complexity | ef6f300462e7072ced4a55021154dee9 MD5 | raw file
  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/slab.h>
  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. #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. #include <linux/netfilter/nf_conntrack_snmp.h>
  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. #define SNMP_PORT 161
  62. #define SNMP_TRAP_PORT 162
  63. #define NOCT1(n) (*(u8 *)(n))
  64. static int debug;
  65. static DEFINE_SPINLOCK(snmp_lock);
  66. /*
  67. * Application layer address mapping mimics the NAT mapping, but
  68. * only for the first octet in this case (a more flexible system
  69. * can be implemented if needed).
  70. */
  71. struct oct1_map
  72. {
  73. u_int8_t from;
  74. u_int8_t to;
  75. };
  76. /*****************************************************************************
  77. *
  78. * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
  79. *
  80. *****************************************************************************/
  81. /* Class */
  82. #define ASN1_UNI 0 /* Universal */
  83. #define ASN1_APL 1 /* Application */
  84. #define ASN1_CTX 2 /* Context */
  85. #define ASN1_PRV 3 /* Private */
  86. /* Tag */
  87. #define ASN1_EOC 0 /* End Of Contents */
  88. #define ASN1_BOL 1 /* Boolean */
  89. #define ASN1_INT 2 /* Integer */
  90. #define ASN1_BTS 3 /* Bit String */
  91. #define ASN1_OTS 4 /* Octet String */
  92. #define ASN1_NUL 5 /* Null */
  93. #define ASN1_OJI 6 /* Object Identifier */
  94. #define ASN1_OJD 7 /* Object Description */
  95. #define ASN1_EXT 8 /* External */
  96. #define ASN1_SEQ 16 /* Sequence */
  97. #define ASN1_SET 17 /* Set */
  98. #define ASN1_NUMSTR 18 /* Numerical String */
  99. #define ASN1_PRNSTR 19 /* Printable String */
  100. #define ASN1_TEXSTR 20 /* Teletext String */
  101. #define ASN1_VIDSTR 21 /* Video String */
  102. #define ASN1_IA5STR 22 /* IA5 String */
  103. #define ASN1_UNITIM 23 /* Universal Time */
  104. #define ASN1_GENTIM 24 /* General Time */
  105. #define ASN1_GRASTR 25 /* Graphical String */
  106. #define ASN1_VISSTR 26 /* Visible String */
  107. #define ASN1_GENSTR 27 /* General String */
  108. /* Primitive / Constructed methods*/
  109. #define ASN1_PRI 0 /* Primitive */
  110. #define ASN1_CON 1 /* Constructed */
  111. /*
  112. * Error codes.
  113. */
  114. #define ASN1_ERR_NOERROR 0
  115. #define ASN1_ERR_DEC_EMPTY 2
  116. #define ASN1_ERR_DEC_EOC_MISMATCH 3
  117. #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
  118. #define ASN1_ERR_DEC_BADVALUE 5
  119. /*
  120. * ASN.1 context.
  121. */
  122. struct asn1_ctx
  123. {
  124. int error; /* Error condition */
  125. unsigned char *pointer; /* Octet just to be decoded */
  126. unsigned char *begin; /* First octet */
  127. unsigned char *end; /* Octet after last octet */
  128. };
  129. /*
  130. * Octet string (not null terminated)
  131. */
  132. struct asn1_octstr
  133. {
  134. unsigned char *data;
  135. unsigned int len;
  136. };
  137. static void asn1_open(struct asn1_ctx *ctx,
  138. unsigned char *buf,
  139. unsigned int len)
  140. {
  141. ctx->begin = buf;
  142. ctx->end = buf + len;
  143. ctx->pointer = buf;
  144. ctx->error = ASN1_ERR_NOERROR;
  145. }
  146. static unsigned char asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
  147. {
  148. if (ctx->pointer >= ctx->end) {
  149. ctx->error = ASN1_ERR_DEC_EMPTY;
  150. return 0;
  151. }
  152. *ch = *(ctx->pointer)++;
  153. return 1;
  154. }
  155. static unsigned char asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
  156. {
  157. unsigned char ch;
  158. *tag = 0;
  159. do
  160. {
  161. if (!asn1_octet_decode(ctx, &ch))
  162. return 0;
  163. *tag <<= 7;
  164. *tag |= ch & 0x7F;
  165. } while ((ch & 0x80) == 0x80);
  166. return 1;
  167. }
  168. static unsigned char asn1_id_decode(struct asn1_ctx *ctx,
  169. unsigned int *cls,
  170. unsigned int *con,
  171. unsigned int *tag)
  172. {
  173. unsigned char ch;
  174. if (!asn1_octet_decode(ctx, &ch))
  175. return 0;
  176. *cls = (ch & 0xC0) >> 6;
  177. *con = (ch & 0x20) >> 5;
  178. *tag = (ch & 0x1F);
  179. if (*tag == 0x1F) {
  180. if (!asn1_tag_decode(ctx, tag))
  181. return 0;
  182. }
  183. return 1;
  184. }
  185. static unsigned char asn1_length_decode(struct asn1_ctx *ctx,
  186. unsigned int *def,
  187. unsigned int *len)
  188. {
  189. unsigned char ch, cnt;
  190. if (!asn1_octet_decode(ctx, &ch))
  191. return 0;
  192. if (ch == 0x80)
  193. *def = 0;
  194. else {
  195. *def = 1;
  196. if (ch < 0x80)
  197. *len = ch;
  198. else {
  199. cnt = ch & 0x7F;
  200. *len = 0;
  201. while (cnt > 0) {
  202. if (!asn1_octet_decode(ctx, &ch))
  203. return 0;
  204. *len <<= 8;
  205. *len |= ch;
  206. cnt--;
  207. }
  208. }
  209. }
  210. /* don't trust len bigger than ctx buffer */
  211. if (*len > ctx->end - ctx->pointer)
  212. return 0;
  213. return 1;
  214. }
  215. static unsigned char asn1_header_decode(struct asn1_ctx *ctx,
  216. unsigned char **eoc,
  217. unsigned int *cls,
  218. unsigned int *con,
  219. unsigned int *tag)
  220. {
  221. unsigned int def, len;
  222. if (!asn1_id_decode(ctx, cls, con, tag))
  223. return 0;
  224. def = len = 0;
  225. if (!asn1_length_decode(ctx, &def, &len))
  226. return 0;
  227. /* primitive shall be definite, indefinite shall be constructed */
  228. if (*con == ASN1_PRI && !def)
  229. return 0;
  230. if (def)
  231. *eoc = ctx->pointer + len;
  232. else
  233. *eoc = NULL;
  234. return 1;
  235. }
  236. static unsigned char asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
  237. {
  238. unsigned char ch;
  239. if (eoc == NULL) {
  240. if (!asn1_octet_decode(ctx, &ch))
  241. return 0;
  242. if (ch != 0x00) {
  243. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  244. return 0;
  245. }
  246. if (!asn1_octet_decode(ctx, &ch))
  247. return 0;
  248. if (ch != 0x00) {
  249. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  250. return 0;
  251. }
  252. return 1;
  253. } else {
  254. if (ctx->pointer != eoc) {
  255. ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
  256. return 0;
  257. }
  258. return 1;
  259. }
  260. }
  261. static unsigned char asn1_null_decode(struct asn1_ctx *ctx, unsigned char *eoc)
  262. {
  263. ctx->pointer = eoc;
  264. return 1;
  265. }
  266. static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
  267. unsigned char *eoc,
  268. long *integer)
  269. {
  270. unsigned char ch;
  271. unsigned int len;
  272. if (!asn1_octet_decode(ctx, &ch))
  273. return 0;
  274. *integer = (signed char) ch;
  275. len = 1;
  276. while (ctx->pointer < eoc) {
  277. if (++len > sizeof (long)) {
  278. ctx->error = ASN1_ERR_DEC_BADVALUE;
  279. return 0;
  280. }
  281. if (!asn1_octet_decode(ctx, &ch))
  282. return 0;
  283. *integer <<= 8;
  284. *integer |= ch;
  285. }
  286. return 1;
  287. }
  288. static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
  289. unsigned char *eoc,
  290. unsigned int *integer)
  291. {
  292. unsigned char ch;
  293. unsigned int len;
  294. if (!asn1_octet_decode(ctx, &ch))
  295. return 0;
  296. *integer = ch;
  297. if (ch == 0) len = 0;
  298. else len = 1;
  299. while (ctx->pointer < eoc) {
  300. if (++len > sizeof (unsigned int)) {
  301. ctx->error = ASN1_ERR_DEC_BADVALUE;
  302. return 0;
  303. }
  304. if (!asn1_octet_decode(ctx, &ch))
  305. return 0;
  306. *integer <<= 8;
  307. *integer |= ch;
  308. }
  309. return 1;
  310. }
  311. static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
  312. unsigned char *eoc,
  313. unsigned long *integer)
  314. {
  315. unsigned char ch;
  316. unsigned int len;
  317. if (!asn1_octet_decode(ctx, &ch))
  318. return 0;
  319. *integer = ch;
  320. if (ch == 0) len = 0;
  321. else len = 1;
  322. while (ctx->pointer < eoc) {
  323. if (++len > sizeof (unsigned long)) {
  324. ctx->error = ASN1_ERR_DEC_BADVALUE;
  325. return 0;
  326. }
  327. if (!asn1_octet_decode(ctx, &ch))
  328. return 0;
  329. *integer <<= 8;
  330. *integer |= ch;
  331. }
  332. return 1;
  333. }
  334. static unsigned char asn1_octets_decode(struct asn1_ctx *ctx,
  335. unsigned char *eoc,
  336. unsigned char **octets,
  337. unsigned int *len)
  338. {
  339. unsigned char *ptr;
  340. *len = 0;
  341. *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
  342. if (*octets == NULL) {
  343. if (net_ratelimit())
  344. pr_notice("OOM in bsalg (%d)\n", __LINE__);
  345. return 0;
  346. }
  347. ptr = *octets;
  348. while (ctx->pointer < eoc) {
  349. if (!asn1_octet_decode(ctx, (unsigned char *)ptr++)) {
  350. kfree(*octets);
  351. *octets = NULL;
  352. return 0;
  353. }
  354. (*len)++;
  355. }
  356. return 1;
  357. }
  358. static unsigned char asn1_subid_decode(struct asn1_ctx *ctx,
  359. unsigned long *subid)
  360. {
  361. unsigned char ch;
  362. *subid = 0;
  363. do {
  364. if (!asn1_octet_decode(ctx, &ch))
  365. return 0;
  366. *subid <<= 7;
  367. *subid |= ch & 0x7F;
  368. } while ((ch & 0x80) == 0x80);
  369. return 1;
  370. }
  371. static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,
  372. unsigned char *eoc,
  373. unsigned long **oid,
  374. unsigned int *len)
  375. {
  376. unsigned long subid;
  377. unsigned long *optr;
  378. size_t size;
  379. size = eoc - ctx->pointer + 1;
  380. /* first subid actually encodes first two subids */
  381. if (size < 2 || size > ULONG_MAX/sizeof(unsigned long))
  382. return 0;
  383. *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
  384. if (*oid == NULL) {
  385. if (net_ratelimit())
  386. pr_notice("OOM in bsalg (%d)\n", __LINE__);
  387. return 0;
  388. }
  389. optr = *oid;
  390. if (!asn1_subid_decode(ctx, &subid)) {
  391. kfree(*oid);
  392. *oid = NULL;
  393. return 0;
  394. }
  395. if (subid < 40) {
  396. optr [0] = 0;
  397. optr [1] = subid;
  398. } else if (subid < 80) {
  399. optr [0] = 1;
  400. optr [1] = subid - 40;
  401. } else {
  402. optr [0] = 2;
  403. optr [1] = subid - 80;
  404. }
  405. *len = 2;
  406. optr += 2;
  407. while (ctx->pointer < eoc) {
  408. if (++(*len) > size) {
  409. ctx->error = ASN1_ERR_DEC_BADVALUE;
  410. kfree(*oid);
  411. *oid = NULL;
  412. return 0;
  413. }
  414. if (!asn1_subid_decode(ctx, optr++)) {
  415. kfree(*oid);
  416. *oid = NULL;
  417. return 0;
  418. }
  419. }
  420. return 1;
  421. }
  422. /*****************************************************************************
  423. *
  424. * SNMP decoding routines (gxsnmp author Dirk Wisse)
  425. *
  426. *****************************************************************************/
  427. /* SNMP Versions */
  428. #define SNMP_V1 0
  429. #define SNMP_V2C 1
  430. #define SNMP_V2 2
  431. #define SNMP_V3 3
  432. /* Default Sizes */
  433. #define SNMP_SIZE_COMM 256
  434. #define SNMP_SIZE_OBJECTID 128
  435. #define SNMP_SIZE_BUFCHR 256
  436. #define SNMP_SIZE_BUFINT 128
  437. #define SNMP_SIZE_SMALLOBJECTID 16
  438. /* Requests */
  439. #define SNMP_PDU_GET 0
  440. #define SNMP_PDU_NEXT 1
  441. #define SNMP_PDU_RESPONSE 2
  442. #define SNMP_PDU_SET 3
  443. #define SNMP_PDU_TRAP1 4
  444. #define SNMP_PDU_BULK 5
  445. #define SNMP_PDU_INFORM 6
  446. #define SNMP_PDU_TRAP2 7
  447. /* Errors */
  448. #define SNMP_NOERROR 0
  449. #define SNMP_TOOBIG 1
  450. #define SNMP_NOSUCHNAME 2
  451. #define SNMP_BADVALUE 3
  452. #define SNMP_READONLY 4
  453. #define SNMP_GENERROR 5
  454. #define SNMP_NOACCESS 6
  455. #define SNMP_WRONGTYPE 7
  456. #define SNMP_WRONGLENGTH 8
  457. #define SNMP_WRONGENCODING 9
  458. #define SNMP_WRONGVALUE 10
  459. #define SNMP_NOCREATION 11
  460. #define SNMP_INCONSISTENTVALUE 12
  461. #define SNMP_RESOURCEUNAVAILABLE 13
  462. #define SNMP_COMMITFAILED 14
  463. #define SNMP_UNDOFAILED 15
  464. #define SNMP_AUTHORIZATIONERROR 16
  465. #define SNMP_NOTWRITABLE 17
  466. #define SNMP_INCONSISTENTNAME 18
  467. /* General SNMP V1 Traps */
  468. #define SNMP_TRAP_COLDSTART 0
  469. #define SNMP_TRAP_WARMSTART 1
  470. #define SNMP_TRAP_LINKDOWN 2
  471. #define SNMP_TRAP_LINKUP 3
  472. #define SNMP_TRAP_AUTFAILURE 4
  473. #define SNMP_TRAP_EQPNEIGHBORLOSS 5
  474. #define SNMP_TRAP_ENTSPECIFIC 6
  475. /* SNMPv1 Types */
  476. #define SNMP_NULL 0
  477. #define SNMP_INTEGER 1 /* l */
  478. #define SNMP_OCTETSTR 2 /* c */
  479. #define SNMP_DISPLAYSTR 2 /* c */
  480. #define SNMP_OBJECTID 3 /* ul */
  481. #define SNMP_IPADDR 4 /* uc */
  482. #define SNMP_COUNTER 5 /* ul */
  483. #define SNMP_GAUGE 6 /* ul */
  484. #define SNMP_TIMETICKS 7 /* ul */
  485. #define SNMP_OPAQUE 8 /* c */
  486. /* Additional SNMPv2 Types */
  487. #define SNMP_UINTEGER 5 /* ul */
  488. #define SNMP_BITSTR 9 /* uc */
  489. #define SNMP_NSAP 10 /* uc */
  490. #define SNMP_COUNTER64 11 /* ul */
  491. #define SNMP_NOSUCHOBJECT 12
  492. #define SNMP_NOSUCHINSTANCE 13
  493. #define SNMP_ENDOFMIBVIEW 14
  494. union snmp_syntax
  495. {
  496. unsigned char uc[0]; /* 8 bit unsigned */
  497. char c[0]; /* 8 bit signed */
  498. unsigned long ul[0]; /* 32 bit unsigned */
  499. long l[0]; /* 32 bit signed */
  500. };
  501. struct snmp_object
  502. {
  503. unsigned long *id;
  504. unsigned int id_len;
  505. unsigned short type;
  506. unsigned int syntax_len;
  507. union snmp_syntax syntax;
  508. };
  509. struct snmp_request
  510. {
  511. unsigned long id;
  512. unsigned int error_status;
  513. unsigned int error_index;
  514. };
  515. struct snmp_v1_trap
  516. {
  517. unsigned long *id;
  518. unsigned int id_len;
  519. unsigned long ip_address; /* pointer */
  520. unsigned int general;
  521. unsigned int specific;
  522. unsigned long time;
  523. };
  524. /* SNMP types */
  525. #define SNMP_IPA 0
  526. #define SNMP_CNT 1
  527. #define SNMP_GGE 2
  528. #define SNMP_TIT 3
  529. #define SNMP_OPQ 4
  530. #define SNMP_C64 6
  531. /* SNMP errors */
  532. #define SERR_NSO 0
  533. #define SERR_NSI 1
  534. #define SERR_EOM 2
  535. static inline void mangle_address(unsigned char *begin,
  536. unsigned char *addr,
  537. const struct oct1_map *map,
  538. __sum16 *check);
  539. struct snmp_cnv
  540. {
  541. unsigned int class;
  542. unsigned int tag;
  543. int syntax;
  544. };
  545. static const struct snmp_cnv snmp_conv[] = {
  546. {ASN1_UNI, ASN1_NUL, SNMP_NULL},
  547. {ASN1_UNI, ASN1_INT, SNMP_INTEGER},
  548. {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR},
  549. {ASN1_UNI, ASN1_OTS, SNMP_DISPLAYSTR},
  550. {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID},
  551. {ASN1_APL, SNMP_IPA, SNMP_IPADDR},
  552. {ASN1_APL, SNMP_CNT, SNMP_COUNTER}, /* Counter32 */
  553. {ASN1_APL, SNMP_GGE, SNMP_GAUGE}, /* Gauge32 == Unsigned32 */
  554. {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS},
  555. {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE},
  556. /* SNMPv2 data types and errors */
  557. {ASN1_UNI, ASN1_BTS, SNMP_BITSTR},
  558. {ASN1_APL, SNMP_C64, SNMP_COUNTER64},
  559. {ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT},
  560. {ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE},
  561. {ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW},
  562. {0, 0, -1}
  563. };
  564. static unsigned char snmp_tag_cls2syntax(unsigned int tag,
  565. unsigned int cls,
  566. unsigned short *syntax)
  567. {
  568. const struct snmp_cnv *cnv;
  569. cnv = snmp_conv;
  570. while (cnv->syntax != -1) {
  571. if (cnv->tag == tag && cnv->class == cls) {
  572. *syntax = cnv->syntax;
  573. return 1;
  574. }
  575. cnv++;
  576. }
  577. return 0;
  578. }
  579. static unsigned char snmp_object_decode(struct asn1_ctx *ctx,
  580. struct snmp_object **obj)
  581. {
  582. unsigned int cls, con, tag, len, idlen;
  583. unsigned short type;
  584. unsigned char *eoc, *end, *p;
  585. unsigned long *lp, *id;
  586. unsigned long ul;
  587. long l;
  588. *obj = NULL;
  589. id = NULL;
  590. if (!asn1_header_decode(ctx, &eoc, &cls, &con, &tag))
  591. return 0;
  592. if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
  593. return 0;
  594. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  595. return 0;
  596. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
  597. return 0;
  598. if (!asn1_oid_decode(ctx, end, &id, &idlen))
  599. return 0;
  600. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag)) {
  601. kfree(id);
  602. return 0;
  603. }
  604. if (con != ASN1_PRI) {
  605. kfree(id);
  606. return 0;
  607. }
  608. type = 0;
  609. if (!snmp_tag_cls2syntax(tag, cls, &type)) {
  610. kfree(id);
  611. return 0;
  612. }
  613. l = 0;
  614. switch (type) {
  615. case SNMP_INTEGER:
  616. len = sizeof(long);
  617. if (!asn1_long_decode(ctx, end, &l)) {
  618. kfree(id);
  619. return 0;
  620. }
  621. *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
  622. if (*obj == NULL) {
  623. kfree(id);
  624. if (net_ratelimit())
  625. pr_notice("OOM in bsalg (%d)\n", __LINE__);
  626. return 0;
  627. }
  628. (*obj)->syntax.l[0] = l;
  629. break;
  630. case SNMP_OCTETSTR:
  631. case SNMP_OPAQUE:
  632. if (!asn1_octets_decode(ctx, end, &p, &len)) {
  633. kfree(id);
  634. return 0;
  635. }
  636. *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
  637. if (*obj == NULL) {
  638. kfree(p);
  639. kfree(id);
  640. if (net_ratelimit())
  641. pr_notice("OOM in bsalg (%d)\n", __LINE__);
  642. return 0;
  643. }
  644. memcpy((*obj)->syntax.c, p, len);
  645. kfree(p);
  646. break;
  647. case SNMP_NULL:
  648. case SNMP_NOSUCHOBJECT:
  649. case SNMP_NOSUCHINSTANCE:
  650. case SNMP_ENDOFMIBVIEW:
  651. len = 0;
  652. *obj = kmalloc(sizeof(struct snmp_object), GFP_ATOMIC);
  653. if (*obj == NULL) {
  654. kfree(id);
  655. if (net_ratelimit())
  656. pr_notice("OOM in bsalg (%d)\n", __LINE__);
  657. return 0;
  658. }
  659. if (!asn1_null_decode(ctx, end)) {
  660. kfree(id);
  661. kfree(*obj);
  662. *obj = NULL;
  663. return 0;
  664. }
  665. break;
  666. case SNMP_OBJECTID:
  667. if (!asn1_oid_decode(ctx, end, (unsigned long **)&lp, &len)) {
  668. kfree(id);
  669. return 0;
  670. }
  671. len *= sizeof(unsigned long);
  672. *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
  673. if (*obj == NULL) {
  674. kfree(lp);
  675. kfree(id);
  676. if (net_ratelimit())
  677. pr_notice("OOM in bsalg (%d)\n", __LINE__);
  678. return 0;
  679. }
  680. memcpy((*obj)->syntax.ul, lp, len);
  681. kfree(lp);
  682. break;
  683. case SNMP_IPADDR:
  684. if (!asn1_octets_decode(ctx, end, &p, &len)) {
  685. kfree(id);
  686. return 0;
  687. }
  688. if (len != 4) {
  689. kfree(p);
  690. kfree(id);
  691. return 0;
  692. }
  693. *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
  694. if (*obj == NULL) {
  695. kfree(p);
  696. kfree(id);
  697. if (net_ratelimit())
  698. pr_notice("OOM in bsalg (%d)\n", __LINE__);
  699. return 0;
  700. }
  701. memcpy((*obj)->syntax.uc, p, len);
  702. kfree(p);
  703. break;
  704. case SNMP_COUNTER:
  705. case SNMP_GAUGE:
  706. case SNMP_TIMETICKS:
  707. len = sizeof(unsigned long);
  708. if (!asn1_ulong_decode(ctx, end, &ul)) {
  709. kfree(id);
  710. return 0;
  711. }
  712. *obj = kmalloc(sizeof(struct snmp_object) + len, GFP_ATOMIC);
  713. if (*obj == NULL) {
  714. kfree(id);
  715. if (net_ratelimit())
  716. pr_notice("OOM in bsalg (%d)\n", __LINE__);
  717. return 0;
  718. }
  719. (*obj)->syntax.ul[0] = ul;
  720. break;
  721. default:
  722. kfree(id);
  723. return 0;
  724. }
  725. (*obj)->syntax_len = len;
  726. (*obj)->type = type;
  727. (*obj)->id = id;
  728. (*obj)->id_len = idlen;
  729. if (!asn1_eoc_decode(ctx, eoc)) {
  730. kfree(id);
  731. kfree(*obj);
  732. *obj = NULL;
  733. return 0;
  734. }
  735. return 1;
  736. }
  737. static unsigned char snmp_request_decode(struct asn1_ctx *ctx,
  738. struct snmp_request *request)
  739. {
  740. unsigned int cls, con, tag;
  741. unsigned char *end;
  742. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  743. return 0;
  744. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
  745. return 0;
  746. if (!asn1_ulong_decode(ctx, end, &request->id))
  747. return 0;
  748. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  749. return 0;
  750. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
  751. return 0;
  752. if (!asn1_uint_decode(ctx, end, &request->error_status))
  753. return 0;
  754. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  755. return 0;
  756. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
  757. return 0;
  758. if (!asn1_uint_decode(ctx, end, &request->error_index))
  759. return 0;
  760. return 1;
  761. }
  762. /*
  763. * Fast checksum update for possibly oddly-aligned UDP byte, from the
  764. * code example in the draft.
  765. */
  766. static void fast_csum(__sum16 *csum,
  767. const unsigned char *optr,
  768. const unsigned char *nptr,
  769. int offset)
  770. {
  771. unsigned char s[4];
  772. if (offset & 1) {
  773. s[0] = ~0;
  774. s[1] = ~*optr;
  775. s[2] = 0;
  776. s[3] = *nptr;
  777. } else {
  778. s[0] = ~*optr;
  779. s[1] = ~0;
  780. s[2] = *nptr;
  781. s[3] = 0;
  782. }
  783. *csum = csum_fold(csum_partial(s, 4, ~csum_unfold(*csum)));
  784. }
  785. /*
  786. * Mangle IP address.
  787. * - begin points to the start of the snmp messgae
  788. * - addr points to the start of the address
  789. */
  790. static inline void mangle_address(unsigned char *begin,
  791. unsigned char *addr,
  792. const struct oct1_map *map,
  793. __sum16 *check)
  794. {
  795. if (map->from == NOCT1(addr)) {
  796. u_int32_t old;
  797. if (debug)
  798. memcpy(&old, addr, sizeof(old));
  799. *addr = map->to;
  800. /* Update UDP checksum if being used */
  801. if (*check) {
  802. fast_csum(check,
  803. &map->from, &map->to, addr - begin);
  804. }
  805. if (debug)
  806. printk(KERN_DEBUG "bsalg: mapped %pI4 to %pI4\n",
  807. &old, addr);
  808. }
  809. }
  810. static unsigned char snmp_trap_decode(struct asn1_ctx *ctx,
  811. struct snmp_v1_trap *trap,
  812. const struct oct1_map *map,
  813. __sum16 *check)
  814. {
  815. unsigned int cls, con, tag, len;
  816. unsigned char *end;
  817. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  818. return 0;
  819. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OJI)
  820. return 0;
  821. if (!asn1_oid_decode(ctx, end, &trap->id, &trap->id_len))
  822. return 0;
  823. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  824. goto err_id_free;
  825. if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
  826. (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS)))
  827. goto err_id_free;
  828. if (!asn1_octets_decode(ctx, end, (unsigned char **)&trap->ip_address, &len))
  829. goto err_id_free;
  830. /* IPv4 only */
  831. if (len != 4)
  832. goto err_addr_free;
  833. mangle_address(ctx->begin, ctx->pointer - 4, map, check);
  834. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  835. goto err_addr_free;
  836. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
  837. goto err_addr_free;
  838. if (!asn1_uint_decode(ctx, end, &trap->general))
  839. goto err_addr_free;
  840. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  841. goto err_addr_free;
  842. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
  843. goto err_addr_free;
  844. if (!asn1_uint_decode(ctx, end, &trap->specific))
  845. goto err_addr_free;
  846. if (!asn1_header_decode(ctx, &end, &cls, &con, &tag))
  847. goto err_addr_free;
  848. if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
  849. (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT)))
  850. goto err_addr_free;
  851. if (!asn1_ulong_decode(ctx, end, &trap->time))
  852. goto err_addr_free;
  853. return 1;
  854. err_addr_free:
  855. kfree((unsigned long *)trap->ip_address);
  856. err_id_free:
  857. kfree(trap->id);
  858. return 0;
  859. }
  860. /*****************************************************************************
  861. *
  862. * Misc. routines
  863. *
  864. *****************************************************************************/
  865. static void hex_dump(const unsigned char *buf, size_t len)
  866. {
  867. size_t i;
  868. for (i = 0; i < len; i++) {
  869. if (i && !(i % 16))
  870. printk("\n");
  871. printk("%02x ", *(buf + i));
  872. }
  873. printk("\n");
  874. }
  875. /*
  876. * Parse and mangle SNMP message according to mapping.
  877. * (And this is the fucking 'basic' method).
  878. */
  879. static int snmp_parse_mangle(unsigned char *msg,
  880. u_int16_t len,
  881. const struct oct1_map *map,
  882. __sum16 *check)
  883. {
  884. unsigned char *eoc, *end;
  885. unsigned int cls, con, tag, vers, pdutype;
  886. struct asn1_ctx ctx;
  887. struct asn1_octstr comm;
  888. struct snmp_object *obj;
  889. if (debug > 1)
  890. hex_dump(msg, len);
  891. asn1_open(&ctx, msg, len);
  892. /*
  893. * Start of SNMP message.
  894. */
  895. if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
  896. return 0;
  897. if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
  898. return 0;
  899. /*
  900. * Version 1 or 2 handled.
  901. */
  902. if (!asn1_header_decode(&ctx, &end, &cls, &con, &tag))
  903. return 0;
  904. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_INT)
  905. return 0;
  906. if (!asn1_uint_decode (&ctx, end, &vers))
  907. return 0;
  908. if (debug > 1)
  909. printk(KERN_DEBUG "bsalg: snmp version: %u\n", vers + 1);
  910. if (vers > 1)
  911. return 1;
  912. /*
  913. * Community.
  914. */
  915. if (!asn1_header_decode (&ctx, &end, &cls, &con, &tag))
  916. return 0;
  917. if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_OTS)
  918. return 0;
  919. if (!asn1_octets_decode(&ctx, end, &comm.data, &comm.len))
  920. return 0;
  921. if (debug > 1) {
  922. unsigned int i;
  923. printk(KERN_DEBUG "bsalg: community: ");
  924. for (i = 0; i < comm.len; i++)
  925. printk("%c", comm.data[i]);
  926. printk("\n");
  927. }
  928. kfree(comm.data);
  929. /*
  930. * PDU type
  931. */
  932. if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &pdutype))
  933. return 0;
  934. if (cls != ASN1_CTX || con != ASN1_CON)
  935. return 0;
  936. if (debug > 1) {
  937. static const unsigned char *const pdus[] = {
  938. [SNMP_PDU_GET] = "get",
  939. [SNMP_PDU_NEXT] = "get-next",
  940. [SNMP_PDU_RESPONSE] = "response",
  941. [SNMP_PDU_SET] = "set",
  942. [SNMP_PDU_TRAP1] = "trapv1",
  943. [SNMP_PDU_BULK] = "bulk",
  944. [SNMP_PDU_INFORM] = "inform",
  945. [SNMP_PDU_TRAP2] = "trapv2"
  946. };
  947. if (pdutype > SNMP_PDU_TRAP2)
  948. printk(KERN_DEBUG "bsalg: bad pdu type %u\n", pdutype);
  949. else
  950. printk(KERN_DEBUG "bsalg: pdu: %s\n", pdus[pdutype]);
  951. }
  952. if (pdutype != SNMP_PDU_RESPONSE &&
  953. pdutype != SNMP_PDU_TRAP1 && pdutype != SNMP_PDU_TRAP2)
  954. return 1;
  955. /*
  956. * Request header or v1 trap
  957. */
  958. if (pdutype == SNMP_PDU_TRAP1) {
  959. struct snmp_v1_trap trap;
  960. unsigned char ret = snmp_trap_decode(&ctx, &trap, map, check);
  961. if (ret) {
  962. kfree(trap.id);
  963. kfree((unsigned long *)trap.ip_address);
  964. } else
  965. return ret;
  966. } else {
  967. struct snmp_request req;
  968. if (!snmp_request_decode(&ctx, &req))
  969. return 0;
  970. if (debug > 1)
  971. printk(KERN_DEBUG "bsalg: request: id=0x%lx error_status=%u "
  972. "error_index=%u\n", req.id, req.error_status,
  973. req.error_index);
  974. }
  975. /*
  976. * Loop through objects, look for IP addresses to mangle.
  977. */
  978. if (!asn1_header_decode(&ctx, &eoc, &cls, &con, &tag))
  979. return 0;
  980. if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
  981. return 0;
  982. while (!asn1_eoc_decode(&ctx, eoc)) {
  983. unsigned int i;
  984. if (!snmp_object_decode(&ctx, &obj)) {
  985. if (obj) {
  986. kfree(obj->id);
  987. kfree(obj);
  988. }
  989. return 0;
  990. }
  991. if (debug > 1) {
  992. printk(KERN_DEBUG "bsalg: object: ");
  993. for (i = 0; i < obj->id_len; i++) {
  994. if (i > 0)
  995. printk(".");
  996. printk("%lu", obj->id[i]);
  997. }
  998. printk(": type=%u\n", obj->type);
  999. }
  1000. if (obj->type == SNMP_IPADDR)
  1001. mangle_address(ctx.begin, ctx.pointer - 4 , map, check);
  1002. kfree(obj->id);
  1003. kfree(obj);
  1004. }
  1005. if (!asn1_eoc_decode(&ctx, eoc))
  1006. return 0;
  1007. return 1;
  1008. }
  1009. /*****************************************************************************
  1010. *
  1011. * NAT routines.
  1012. *
  1013. *****************************************************************************/
  1014. /*
  1015. * SNMP translation routine.
  1016. */
  1017. static int snmp_translate(struct nf_conn *ct,
  1018. enum ip_conntrack_info ctinfo,
  1019. struct sk_buff *skb)
  1020. {
  1021. struct iphdr *iph = ip_hdr(skb);
  1022. struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
  1023. u_int16_t udplen = ntohs(udph->len);
  1024. u_int16_t paylen = udplen - sizeof(struct udphdr);
  1025. int dir = CTINFO2DIR(ctinfo);
  1026. struct oct1_map map;
  1027. /*
  1028. * Determine mappping for application layer addresses based
  1029. * on NAT manipulations for the packet.
  1030. */
  1031. if (dir == IP_CT_DIR_ORIGINAL) {
  1032. /* SNAT traps */
  1033. map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
  1034. map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
  1035. } else {
  1036. /* DNAT replies */
  1037. map.from = NOCT1(&ct->tuplehash[dir].tuple.src.u3.ip);
  1038. map.to = NOCT1(&ct->tuplehash[!dir].tuple.dst.u3.ip);
  1039. }
  1040. if (map.from == map.to)
  1041. return NF_ACCEPT;
  1042. if (!snmp_parse_mangle((unsigned char *)udph + sizeof(struct udphdr),
  1043. paylen, &map, &udph->check)) {
  1044. if (net_ratelimit())
  1045. printk(KERN_WARNING "bsalg: parser failed\n");
  1046. return NF_DROP;
  1047. }
  1048. return NF_ACCEPT;
  1049. }
  1050. /* We don't actually set up expectations, just adjust internal IP
  1051. * addresses if this is being NATted */
  1052. static int help(struct sk_buff *skb, unsigned int protoff,
  1053. struct nf_conn *ct,
  1054. enum ip_conntrack_info ctinfo)
  1055. {
  1056. int dir = CTINFO2DIR(ctinfo);
  1057. unsigned int ret;
  1058. const struct iphdr *iph = ip_hdr(skb);
  1059. const struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);
  1060. /* SNMP replies and originating SNMP traps get mangled */
  1061. if (udph->source == htons(SNMP_PORT) && dir != IP_CT_DIR_REPLY)
  1062. return NF_ACCEPT;
  1063. if (udph->dest == htons(SNMP_TRAP_PORT) && dir != IP_CT_DIR_ORIGINAL)
  1064. return NF_ACCEPT;
  1065. /* No NAT? */
  1066. if (!(ct->status & IPS_NAT_MASK))
  1067. return NF_ACCEPT;
  1068. /*
  1069. * Make sure the packet length is ok. So far, we were only guaranteed
  1070. * to have a valid length IP header plus 8 bytes, which means we have
  1071. * enough room for a UDP header. Just verify the UDP length field so we
  1072. * can mess around with the payload.
  1073. */
  1074. if (ntohs(udph->len) != skb->len - (iph->ihl << 2)) {
  1075. if (net_ratelimit())
  1076. printk(KERN_WARNING "SNMP: dropping malformed packet src=%pI4 dst=%pI4\n",
  1077. &iph->saddr, &iph->daddr);
  1078. return NF_DROP;
  1079. }
  1080. if (!skb_make_writable(skb, skb->len))
  1081. return NF_DROP;
  1082. spin_lock_bh(&snmp_lock);
  1083. ret = snmp_translate(ct, ctinfo, skb);
  1084. spin_unlock_bh(&snmp_lock);
  1085. return ret;
  1086. }
  1087. static const struct nf_conntrack_expect_policy snmp_exp_policy = {
  1088. .max_expected = 0,
  1089. .timeout = 180,
  1090. };
  1091. static struct nf_conntrack_helper snmp_helper __read_mostly = {
  1092. .me = THIS_MODULE,
  1093. .help = help,
  1094. .expect_policy = &snmp_exp_policy,
  1095. .name = "snmp",
  1096. .tuple.src.l3num = AF_INET,
  1097. .tuple.src.u.udp.port = cpu_to_be16(SNMP_PORT),
  1098. .tuple.dst.protonum = IPPROTO_UDP,
  1099. };
  1100. static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {
  1101. .me = THIS_MODULE,
  1102. .help = help,
  1103. .expect_policy = &snmp_exp_policy,
  1104. .name = "snmp_trap",
  1105. .tuple.src.l3num = AF_INET,
  1106. .tuple.src.u.udp.port = cpu_to_be16(SNMP_TRAP_PORT),
  1107. .tuple.dst.protonum = IPPROTO_UDP,
  1108. };
  1109. /*****************************************************************************
  1110. *
  1111. * Module stuff.
  1112. *
  1113. *****************************************************************************/
  1114. static int __init nf_nat_snmp_basic_init(void)
  1115. {
  1116. int ret = 0;
  1117. BUG_ON(nf_nat_snmp_hook != NULL);
  1118. rcu_assign_pointer(nf_nat_snmp_hook, help);
  1119. ret = nf_conntrack_helper_register(&snmp_trap_helper);
  1120. if (ret < 0) {
  1121. nf_conntrack_helper_unregister(&snmp_helper);
  1122. return ret;
  1123. }
  1124. return ret;
  1125. }
  1126. static void __exit nf_nat_snmp_basic_fini(void)
  1127. {
  1128. rcu_assign_pointer(nf_nat_snmp_hook, NULL);
  1129. nf_conntrack_helper_unregister(&snmp_trap_helper);
  1130. }
  1131. module_init(nf_nat_snmp_basic_init);
  1132. module_exit(nf_nat_snmp_basic_fini);
  1133. module_param(debug, int, 0600);