nf_nat_snmp_basic.c 28 KB

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