You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1730 lines
49 KiB

  1. --- /dev/null
  2. +++ b/extensions/rtsp/Kbuild
  3. @@ -0,0 +1,4 @@
  4. +# -*- Makefile -*-
  5. +
  6. +obj-m += nf_nat_rtsp.o
  7. +obj-m += nf_conntrack_rtsp.o
  8. --- /dev/null
  9. +++ b/extensions/rtsp/netfilter_helpers.h
  10. @@ -0,0 +1,133 @@
  11. +/*
  12. + * Helpers for netfiler modules. This file provides implementations for basic
  13. + * functions such as strncasecmp(), etc.
  14. + *
  15. + * gcc will warn for defined but unused functions, so we only include the
  16. + * functions requested. The following macros are used:
  17. + * NF_NEED_STRNCASECMP nf_strncasecmp()
  18. + * NF_NEED_STRTOU16 nf_strtou16()
  19. + * NF_NEED_STRTOU32 nf_strtou32()
  20. + */
  21. +#ifndef _NETFILTER_HELPERS_H
  22. +#define _NETFILTER_HELPERS_H
  23. +
  24. +/* Only include these functions for kernel code. */
  25. +#ifdef __KERNEL__
  26. +
  27. +#include <linux/ctype.h>
  28. +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
  29. +
  30. +/*
  31. + * The standard strncasecmp()
  32. + */
  33. +#ifdef NF_NEED_STRNCASECMP
  34. +static int
  35. +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
  36. +{
  37. + if (s1 == NULL || s2 == NULL)
  38. + {
  39. + if (s1 == NULL && s2 == NULL)
  40. + {
  41. + return 0;
  42. + }
  43. + return (s1 == NULL) ? -1 : 1;
  44. + }
  45. + while (len > 0 && tolower(*s1) == tolower(*s2))
  46. + {
  47. + len--;
  48. + s1++;
  49. + s2++;
  50. + }
  51. + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
  52. +}
  53. +#endif /* NF_NEED_STRNCASECMP */
  54. +
  55. +/*
  56. + * Parse a string containing a 16-bit unsigned integer.
  57. + * Returns the number of chars used, or zero if no number is found.
  58. + */
  59. +#ifdef NF_NEED_STRTOU16
  60. +static int
  61. +nf_strtou16(const char* pbuf, u_int16_t* pval)
  62. +{
  63. + int n = 0;
  64. +
  65. + *pval = 0;
  66. + while (isdigit(pbuf[n]))
  67. + {
  68. + *pval = (*pval * 10) + (pbuf[n] - '0');
  69. + n++;
  70. + }
  71. +
  72. + return n;
  73. +}
  74. +#endif /* NF_NEED_STRTOU16 */
  75. +
  76. +/*
  77. + * Parse a string containing a 32-bit unsigned integer.
  78. + * Returns the number of chars used, or zero if no number is found.
  79. + */
  80. +#ifdef NF_NEED_STRTOU32
  81. +static int
  82. +nf_strtou32(const char* pbuf, u_int32_t* pval)
  83. +{
  84. + int n = 0;
  85. +
  86. + *pval = 0;
  87. + while (pbuf[n] >= '0' && pbuf[n] <= '9')
  88. + {
  89. + *pval = (*pval * 10) + (pbuf[n] - '0');
  90. + n++;
  91. + }
  92. +
  93. + return n;
  94. +}
  95. +#endif /* NF_NEED_STRTOU32 */
  96. +
  97. +/*
  98. + * Given a buffer and length, advance to the next line and mark the current
  99. + * line.
  100. + */
  101. +#ifdef NF_NEED_NEXTLINE
  102. +static int
  103. +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
  104. +{
  105. + uint off = *poff;
  106. + uint physlen = 0;
  107. +
  108. + if (off >= len)
  109. + {
  110. + return 0;
  111. + }
  112. +
  113. + while (p[off] != '\n')
  114. + {
  115. + if (len-off <= 1)
  116. + {
  117. + return 0;
  118. + }
  119. +
  120. + physlen++;
  121. + off++;
  122. + }
  123. +
  124. + /* if we saw a crlf, physlen needs adjusted */
  125. + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
  126. + {
  127. + physlen--;
  128. + }
  129. +
  130. + /* advance past the newline */
  131. + off++;
  132. +
  133. + *plineoff = *poff;
  134. + *plinelen = physlen;
  135. + *poff = off;
  136. +
  137. + return 1;
  138. +}
  139. +#endif /* NF_NEED_NEXTLINE */
  140. +
  141. +#endif /* __KERNEL__ */
  142. +
  143. +#endif /* _NETFILTER_HELPERS_H */
  144. --- /dev/null
  145. +++ b/extensions/rtsp/netfilter_mime.h
  146. @@ -0,0 +1,89 @@
  147. +/*
  148. + * MIME functions for netfilter modules. This file provides implementations
  149. + * for basic MIME parsing. MIME headers are used in many protocols, such as
  150. + * HTTP, RTSP, SIP, etc.
  151. + *
  152. + * gcc will warn for defined but unused functions, so we only include the
  153. + * functions requested. The following macros are used:
  154. + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
  155. + */
  156. +#ifndef _NETFILTER_MIME_H
  157. +#define _NETFILTER_MIME_H
  158. +
  159. +/* Only include these functions for kernel code. */
  160. +#ifdef __KERNEL__
  161. +
  162. +#include <linux/ctype.h>
  163. +
  164. +/*
  165. + * Given a buffer and length, advance to the next line and mark the current
  166. + * line. If the current line is empty, *plinelen will be set to zero. If
  167. + * not, it will be set to the actual line length (including CRLF).
  168. + *
  169. + * 'line' in this context means logical line (includes LWS continuations).
  170. + * Returns 1 on success, 0 on failure.
  171. + */
  172. +#ifdef NF_NEED_MIME_NEXTLINE
  173. +static int
  174. +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
  175. +{
  176. + uint off = *poff;
  177. + uint physlen = 0;
  178. + int is_first_line = 1;
  179. +
  180. + if (off >= len)
  181. + {
  182. + return 0;
  183. + }
  184. +
  185. + do
  186. + {
  187. + while (p[off] != '\n')
  188. + {
  189. + if (len-off <= 1)
  190. + {
  191. + return 0;
  192. + }
  193. +
  194. + physlen++;
  195. + off++;
  196. + }
  197. +
  198. + /* if we saw a crlf, physlen needs adjusted */
  199. + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
  200. + {
  201. + physlen--;
  202. + }
  203. +
  204. + /* advance past the newline */
  205. + off++;
  206. +
  207. + /* check for an empty line */
  208. + if (physlen == 0)
  209. + {
  210. + break;
  211. + }
  212. +
  213. + /* check for colon on the first physical line */
  214. + if (is_first_line)
  215. + {
  216. + is_first_line = 0;
  217. + if (memchr(p+(*poff), ':', physlen) == NULL)
  218. + {
  219. + return 0;
  220. + }
  221. + }
  222. + }
  223. + while (p[off] == ' ' || p[off] == '\t');
  224. +
  225. + *plineoff = *poff;
  226. + *plinelen = (physlen == 0) ? 0 : (off - *poff);
  227. + *poff = off;
  228. +
  229. + return 1;
  230. +}
  231. +#endif /* NF_NEED_MIME_NEXTLINE */
  232. +
  233. +#endif /* __KERNEL__ */
  234. +
  235. +#endif /* _NETFILTER_MIME_H */
  236. --- /dev/null
  237. +++ b/extensions/rtsp/nf_conntrack_rtsp.c
  238. @@ -0,0 +1,761 @@
  239. +/*
  240. + * RTSP extension for IP connection tracking
  241. + * (C) 2003 by Tom Marshall <tmarshall at real.com>
  242. + *
  243. + * 2005-02-13: Harald Welte <laforge at netfilter.org>
  244. + * - port to 2.6
  245. + * - update to recent post-2.6.11 api changes
  246. + * 2006-09-14: Steven Van Acker <deepstar at singularity.be>
  247. + * - removed calls to NAT code from conntrack helper: NAT no longer needed to use rtsp-conntrack
  248. + * 2007-04-18: Michael Guntsche <mike at it-loops.com>
  249. + * - Port to new NF API
  250. + * 2013-03-04: Il'inykh Sergey <sergeyi at inango-sw.com>. Inango Systems Ltd
  251. + * - fixed rtcp nat mapping and other port mapping fixes
  252. + * - simple TEARDOWN request handling
  253. + * - codestyle fixes and other less significant bug fixes
  254. + * 2018-04-17: Alin Nastac <alin.nastac at gmail.com>
  255. + * Hans Dedecker <dedeckeh at gmail.com>
  256. + * - use IP address read from SETUP URI in expected connections
  257. + * 2018-04-18: Hans Dedecker <dedeckeh at gmail.com>
  258. + * - update RTP expected connection source IP based on SOURCE
  259. + * in the SETUP reply message
  260. + * 2018-08-03: Alin Nastac <alin.nastac at gmail.com>
  261. + * Hans Dedecker <dedeckeh at gmail.com>
  262. + * - parse non-standard destination=address:port format
  263. + *
  264. + * based on ip_conntrack_irc.c
  265. + *
  266. + * This program is free software; you can redistribute it and/or
  267. + * modify it under the terms of the GNU General Public License
  268. + * as published by the Free Software Foundation; either version
  269. + * 2 of the License, or (at your option) any later version.
  270. + *
  271. + * Module load syntax:
  272. + * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
  273. + * max_outstanding=n setup_timeout=secs
  274. + *
  275. + * If no ports are specified, the default will be port 554.
  276. + *
  277. + * With max_outstanding you can define the maximum number of not yet
  278. + * answered SETUP requests per RTSP session (default 8).
  279. + * With setup_timeout you can specify how long the system waits for
  280. + * an expected data channel (default 300 seconds).
  281. + *
  282. + */
  283. +
  284. +#include <linux/module.h>
  285. +#include <linux/netfilter.h>
  286. +#include <linux/ip.h>
  287. +#include <linux/inet.h>
  288. +#include <net/tcp.h>
  289. +
  290. +#include <net/netfilter/nf_conntrack.h>
  291. +#include <net/netfilter/nf_conntrack_expect.h>
  292. +#include <net/netfilter/nf_conntrack_helper.h>
  293. +#include <net/netfilter/nf_conntrack_zones.h>
  294. +#include "nf_conntrack_rtsp.h"
  295. +
  296. +#define NF_NEED_STRNCASECMP
  297. +#define NF_NEED_STRTOU16
  298. +#define NF_NEED_STRTOU32
  299. +#define NF_NEED_NEXTLINE
  300. +#include "netfilter_helpers.h"
  301. +#define NF_NEED_MIME_NEXTLINE
  302. +#include "netfilter_mime.h"
  303. +
  304. +#include <linux/ctype.h>
  305. +
  306. +#define MAX_PORTS 8
  307. +static int ports[MAX_PORTS];
  308. +static int num_ports = 0;
  309. +static int max_outstanding = 8;
  310. +static unsigned int setup_timeout = 300;
  311. +
  312. +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
  313. +MODULE_DESCRIPTION("RTSP connection tracking module");
  314. +MODULE_LICENSE("GPL");
  315. +module_param_array(ports, int, &num_ports, 0400);
  316. +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
  317. +module_param(max_outstanding, int, 0400);
  318. +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
  319. +module_param(setup_timeout, int, 0400);
  320. +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
  321. +
  322. +static char *rtsp_buffer;
  323. +static DEFINE_SPINLOCK(rtsp_buffer_lock);
  324. +
  325. +static struct nf_conntrack_expect_policy rtsp_exp_policy;
  326. +
  327. +unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
  328. + enum ip_conntrack_info ctinfo,
  329. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  330. + unsigned int protoff,
  331. +#endif
  332. + unsigned int matchoff, unsigned int matchlen,
  333. + struct ip_ct_rtsp_expect* prtspexp,
  334. + struct nf_conntrack_expect *rtp_exp,
  335. + struct nf_conntrack_expect *rtcp_exp);
  336. +
  337. +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
  338. +
  339. +/*
  340. + * Max mappings we will allow for one RTSP connection (for RTP, the number
  341. + * of allocated ports is twice this value). Note that SMIL burns a lot of
  342. + * ports so keep this reasonably high. If this is too low, you will see a
  343. + * lot of "no free client map entries" messages.
  344. + */
  345. +#define MAX_PORT_MAPS 16
  346. +
  347. +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
  348. +
  349. +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
  350. +
  351. +/*
  352. + * Parse an RTSP packet.
  353. + *
  354. + * Returns zero if parsing failed.
  355. + *
  356. + * Parameters:
  357. + * IN ptcp tcp data pointer
  358. + * IN tcplen tcp data len
  359. + * IN/OUT ptcpoff points to current tcp offset
  360. + * OUT phdrsoff set to offset of rtsp headers
  361. + * OUT phdrslen set to length of rtsp headers
  362. + * OUT pcseqoff set to offset of CSeq header
  363. + * OUT pcseqlen set to length of CSeq header
  364. + */
  365. +static int
  366. +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
  367. + uint* phdrsoff, uint* phdrslen,
  368. + uint* pcseqoff, uint* pcseqlen,
  369. + uint* transoff, uint* translen)
  370. +{
  371. + uint entitylen = 0;
  372. + uint lineoff;
  373. + uint linelen;
  374. +
  375. + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
  376. + return 0;
  377. +
  378. + *phdrsoff = *ptcpoff;
  379. + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
  380. + if (linelen == 0) {
  381. + if (entitylen > 0)
  382. + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
  383. + break;
  384. + }
  385. + if (lineoff+linelen > tcplen) {
  386. + pr_info("!! overrun !!\n");
  387. + break;
  388. + }
  389. +
  390. + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
  391. + *pcseqoff = lineoff;
  392. + *pcseqlen = linelen;
  393. + }
  394. +
  395. + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
  396. + *transoff = lineoff;
  397. + *translen = linelen;
  398. + }
  399. +
  400. + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0) {
  401. + uint off = lineoff+15;
  402. + SKIP_WSPACE(ptcp+lineoff, linelen, off);
  403. + nf_strtou32(ptcp+off, &entitylen);
  404. + }
  405. + }
  406. + *phdrslen = (*ptcpoff) - (*phdrsoff);
  407. +
  408. + return 1;
  409. +}
  410. +
  411. +/*
  412. + * Find lo/hi client ports and/or source (if any) in transport header
  413. + * In:
  414. + * ptcp, tcplen = packet
  415. + * tranoff, tranlen = buffer to search
  416. + *
  417. + * Out:
  418. + * pport_lo, pport_hi = lo/hi ports (host endian)
  419. + * srvaddr
  420. + *
  421. + * Returns nonzero if any client ports found
  422. + *
  423. + * Note: it is valid (and expected) for the client to request multiple
  424. + * transports, so we need to parse the entire line.
  425. + */
  426. +static int
  427. +rtsp_parse_transport(char* ptran, uint tranlen,
  428. + struct ip_ct_rtsp_expect* prtspexp)
  429. +{
  430. + int rc = 0;
  431. + uint off = 0;
  432. +
  433. + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
  434. + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
  435. + pr_info("sanity check failed\n");
  436. + return 0;
  437. + }
  438. +
  439. + pr_debug("tran='%.*s'\n", (int)tranlen, ptran);
  440. + off += 10;
  441. + SKIP_WSPACE(ptran, tranlen, off);
  442. +
  443. + /* Transport: tran;field;field=val,tran;field;field=val,... */
  444. + while (off < tranlen) {
  445. + const char* pparamend;
  446. + uint nextparamoff;
  447. +
  448. + pparamend = memchr(ptran+off, ',', tranlen-off);
  449. + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
  450. + nextparamoff = pparamend-ptran;
  451. +
  452. + while (off < nextparamoff) {
  453. + const char* pfieldend;
  454. + uint nextfieldoff;
  455. +
  456. + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
  457. + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
  458. +
  459. + if (strncmp(ptran+off, "client_port=", 12) == 0) {
  460. + u_int16_t port;
  461. + uint numlen;
  462. +
  463. + off += 12;
  464. + numlen = nf_strtou16(ptran+off, &port);
  465. + off += numlen;
  466. + if (prtspexp->loport != 0 && prtspexp->loport != port)
  467. + pr_debug("multiple ports found, port %hu ignored\n", port);
  468. + else {
  469. + pr_debug("lo port found : %hu\n", port);
  470. + prtspexp->loport = prtspexp->hiport = port;
  471. + if (ptran[off] == '-') {
  472. + off++;
  473. + numlen = nf_strtou16(ptran+off, &port);
  474. + off += numlen;
  475. + prtspexp->pbtype = pb_range;
  476. + prtspexp->hiport = port;
  477. +
  478. + // If we have a range, assume rtp:
  479. + // loport must be even, hiport must be loport+1
  480. + if ((prtspexp->loport & 0x0001) != 0 ||
  481. + prtspexp->hiport != prtspexp->loport+1) {
  482. + pr_debug("incorrect range: %hu-%hu, correcting\n",
  483. + prtspexp->loport, prtspexp->hiport);
  484. + prtspexp->loport &= 0xfffe;
  485. + prtspexp->hiport = prtspexp->loport+1;
  486. + }
  487. + } else if (ptran[off] == '/') {
  488. + off++;
  489. + numlen = nf_strtou16(ptran+off, &port);
  490. + off += numlen;
  491. + prtspexp->pbtype = pb_discon;
  492. + prtspexp->hiport = port;
  493. + }
  494. + rc = 1;
  495. + }
  496. + } else if (strncmp(ptran+off, "source=", 7) == 0) {
  497. + uint srcaddrlen;
  498. +
  499. + off += 7;
  500. + srcaddrlen = nextfieldoff - off - 1;
  501. +
  502. + if (in4_pton(ptran + off, srcaddrlen,
  503. + (u8 *)&prtspexp->srvaddr.in,
  504. + -1, NULL))
  505. + pr_debug("source found : %pI4\n",
  506. + &prtspexp->srvaddr.ip);
  507. + } else if (nextfieldoff - off > 12 && strncmp(ptran+off, "destination=", 12) == 0) {
  508. + const char *psep;
  509. + u_int16_t port;
  510. +
  511. + off += 12;
  512. +
  513. + if (in4_pton(ptran+off, nextfieldoff - off - 1, (u8 *)&prtspexp->cltaddr.in, -1, NULL)) {
  514. + pr_debug("destination found : %pI4\n", &prtspexp->cltaddr.ip);
  515. +
  516. + /*
  517. + * Some RTSP clients(mostly STBs) use non-standard destination parameters:
  518. + * destination=address:port
  519. + */
  520. + psep = memchr(ptran+off, ':', nextfieldoff-off);
  521. + if (psep != NULL && nf_strtou16(psep + 1, &port)) {
  522. + if (prtspexp->loport != 0 && prtspexp->loport != port)
  523. + pr_debug("multiple ports found, port %hu ignored\n", port);
  524. + else {
  525. + pr_debug("lo port found : %hu\n", port);
  526. + prtspexp->loport = prtspexp->hiport = port;
  527. + }
  528. + }
  529. + }
  530. + }
  531. +
  532. + /*
  533. + * Note we don't look for the destination parameter here.
  534. + * If we are using NAT, the NAT module will handle it. If not,
  535. + * and the client is sending packets elsewhere, the expectation
  536. + * will quietly time out.
  537. + */
  538. +
  539. + off = nextfieldoff;
  540. + }
  541. +
  542. + off = nextparamoff;
  543. + }
  544. +
  545. + return rc;
  546. +}
  547. +
  548. +
  549. +/*** conntrack functions ***/
  550. +
  551. +/* outbound packet: client->server */
  552. +
  553. +static inline int
  554. +help_out(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
  555. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  556. + struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  557. + unsigned int protoff)
  558. +#else
  559. + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  560. +#endif
  561. +{
  562. + struct ip_ct_rtsp_expect expinfo;
  563. +
  564. + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
  565. + //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
  566. + //uint tcplen = pktlen - iph->ihl * 4;
  567. + char* pdata = rb_ptr;
  568. + //uint datalen = tcplen - tcph->doff * 4;
  569. + uint dataoff = 0;
  570. + int ret = NF_ACCEPT;
  571. +
  572. + struct nf_conntrack_expect *rtp_exp;
  573. + struct nf_conntrack_expect *rtcp_exp = NULL;
  574. +
  575. + __be16 be_loport;
  576. + __be16 be_hiport;
  577. +
  578. + typeof(nf_nat_rtsp_hook) nf_nat_rtsp;
  579. +
  580. + memset(&expinfo, 0, sizeof(expinfo));
  581. +
  582. + while (dataoff < datalen) {
  583. + uint cmdoff = dataoff;
  584. + uint hdrsoff = 0;
  585. + uint hdrslen = 0;
  586. + uint cseqoff = 0;
  587. + uint cseqlen = 0;
  588. + uint transoff = 0;
  589. + uint translen = 0;
  590. + uint off;
  591. + union nf_inet_addr srvaddr;
  592. +
  593. + if (!rtsp_parse_message(pdata, datalen, &dataoff,
  594. + &hdrsoff, &hdrslen,
  595. + &cseqoff, &cseqlen,
  596. + &transoff, &translen))
  597. + break; /* not a valid message */
  598. +
  599. + if (strncmp(pdata+cmdoff, "TEARDOWN ", 9) == 0) {
  600. + pr_debug("teardown handled\n");
  601. + nf_ct_remove_expectations(ct); /* FIXME must be session id aware */
  602. + break;
  603. + }
  604. +
  605. + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
  606. + continue; /* not a SETUP message */
  607. +
  608. + srvaddr = ct->tuplehash[!dir].tuple.src.u3;
  609. +
  610. + /* try to get RTP media source from SETUP URI */
  611. + off = cmdoff + 6;
  612. + while (off < datalen) {
  613. + if (strncmp(pdata+off, "://", 3) == 0) {
  614. + off += 3;
  615. + cmdoff = off;
  616. +
  617. + while (off < datalen) {
  618. + if (pdata[off] == ':' ||
  619. + pdata[off] == '/' ||
  620. + pdata[off] == ' ') {
  621. + in4_pton(pdata + cmdoff,
  622. + off - cmdoff,
  623. + (u8 *)&srvaddr.in,
  624. + -1, NULL);
  625. + break;
  626. + }
  627. + off++;
  628. + }
  629. + break;
  630. + }
  631. + off++;
  632. + }
  633. +
  634. + pr_debug("found a setup message\n");
  635. +
  636. + off = 0;
  637. + if(translen)
  638. + rtsp_parse_transport(pdata+transoff, translen, &expinfo);
  639. +
  640. + if (expinfo.loport == 0) {
  641. + pr_debug("no udp transports found\n");
  642. + continue; /* no udp transports found */
  643. + }
  644. +
  645. + pr_debug("udp transport found, ports=(%d,%hu,%hu)\n",
  646. + (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
  647. +
  648. +
  649. + be_loport = htons(expinfo.loport);
  650. +
  651. + rtp_exp = nf_ct_expect_alloc(ct);
  652. + if (rtp_exp == NULL) {
  653. + ret = NF_DROP;
  654. + goto out;
  655. + }
  656. +
  657. + nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT,
  658. + nf_ct_l3num(ct), &srvaddr,
  659. + &ct->tuplehash[!dir].tuple.dst.u3,
  660. + IPPROTO_UDP, NULL, &be_loport);
  661. +
  662. + rtp_exp->flags = 0;
  663. +
  664. + if (expinfo.pbtype == pb_range) {
  665. + pr_debug("setup expectation for rtcp\n");
  666. +
  667. + be_hiport = htons(expinfo.hiport);
  668. + rtcp_exp = nf_ct_expect_alloc(ct);
  669. + if (rtcp_exp == NULL) {
  670. + ret = NF_DROP;
  671. + goto out1;
  672. + }
  673. +
  674. + nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT,
  675. + nf_ct_l3num(ct), &srvaddr,
  676. + &ct->tuplehash[!dir].tuple.dst.u3,
  677. + IPPROTO_UDP, NULL, &be_hiport);
  678. +
  679. + rtcp_exp->flags = 0;
  680. +
  681. + pr_debug("expect_related %pI4:%u-%u-%pI4:%u-%u\n",
  682. + &rtp_exp->tuple.src.u3.ip,
  683. + ntohs(rtp_exp->tuple.src.u.udp.port),
  684. + ntohs(rtcp_exp->tuple.src.u.udp.port),
  685. + &rtp_exp->tuple.dst.u3.ip,
  686. + ntohs(rtp_exp->tuple.dst.u.udp.port),
  687. + ntohs(rtcp_exp->tuple.dst.u.udp.port));
  688. + } else {
  689. + pr_debug("expect_related %pI4:%u-%pI4:%u\n",
  690. + &rtp_exp->tuple.src.u3.ip,
  691. + ntohs(rtp_exp->tuple.src.u.udp.port),
  692. + &rtp_exp->tuple.dst.u3.ip,
  693. + ntohs(rtp_exp->tuple.dst.u.udp.port));
  694. + }
  695. +
  696. + nf_nat_rtsp = rcu_dereference(nf_nat_rtsp_hook);
  697. + if (nf_nat_rtsp && ct->status & IPS_NAT_MASK)
  698. + /* pass the request off to the nat helper */
  699. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  700. + ret = nf_nat_rtsp(skb, ctinfo, protoff, hdrsoff, hdrslen,
  701. + &expinfo, rtp_exp, rtcp_exp);
  702. +#else
  703. + ret = nf_nat_rtsp(skb, ctinfo, hdrsoff, hdrslen,
  704. + &expinfo, rtp_exp, rtcp_exp);
  705. +#endif
  706. + else {
  707. + if (nf_ct_expect_related(rtp_exp) == 0) {
  708. + if (rtcp_exp && nf_ct_expect_related(rtcp_exp) != 0) {
  709. + nf_ct_unexpect_related(rtp_exp);
  710. + pr_info("nf_conntrack_expect_related failed for rtcp\n");
  711. + ret = NF_DROP;
  712. + }
  713. + } else {
  714. + pr_info("nf_conntrack_expect_related failed for rtp\n");
  715. + ret = NF_DROP;
  716. + }
  717. + }
  718. + if (rtcp_exp) {
  719. + nf_ct_expect_put(rtcp_exp);
  720. + }
  721. +out1:
  722. + nf_ct_expect_put(rtp_exp);
  723. + goto out;
  724. + }
  725. +out:
  726. +
  727. + return ret;
  728. +}
  729. +
  730. +
  731. +static inline int
  732. +help_in(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
  733. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  734. + struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  735. + unsigned int protoff)
  736. +#else
  737. + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  738. +#endif
  739. + {
  740. + struct ip_ct_rtsp_expect expinfo;
  741. + union nf_inet_addr srvaddr;
  742. + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
  743. + int len;
  744. + char* pdata = rb_ptr;
  745. + uint dataoff = 0;
  746. + int ret = NF_ACCEPT;
  747. + u_int8_t family;
  748. + struct nf_conntrack_expect *exp_ct = NULL;
  749. + struct nf_conntrack_tuple t;
  750. + struct net *net = nf_ct_net(ct);
  751. +
  752. + memset(&expinfo, 0, sizeof(expinfo));
  753. +
  754. + while (dataoff < datalen) {
  755. + uint cmdoff = dataoff;
  756. + uint hdrsoff = 0;
  757. + uint hdrslen = 0;
  758. + uint cseqoff = 0;
  759. + uint cseqlen = 0;
  760. + uint transoff = 0;
  761. + uint translen = 0;
  762. +
  763. + if (!rtsp_parse_message(pdata, datalen, &dataoff,
  764. + &hdrsoff, &hdrslen,
  765. + &cseqoff, &cseqlen,
  766. + &transoff, &translen))
  767. + break; /* not a valid message */
  768. +
  769. + if (strncmp(pdata+cmdoff, "RTSP/", 5) == 0 && translen) {
  770. + union nf_inet_addr zeroaddr;
  771. +
  772. + memset(&zeroaddr, 0, sizeof(zeroaddr));
  773. +
  774. + if (!rtsp_parse_transport(pdata+transoff, translen, &expinfo))
  775. + continue;
  776. +
  777. + srvaddr = expinfo.srvaddr;
  778. +
  779. + if (nf_inet_addr_cmp(&srvaddr, &ct->tuplehash[dir].tuple.src.u3) ||
  780. + nf_inet_addr_cmp(&srvaddr, &zeroaddr))
  781. + continue;
  782. + } else
  783. + continue; /* not valid RTSP reply */
  784. +
  785. + if (expinfo.loport == 0) {
  786. + pr_debug("no udp transports found\n");
  787. + continue; /* no udp transports found */
  788. + }
  789. +
  790. + family = nf_ct_l3num(ct);
  791. + if (family == AF_INET)
  792. + len = 4;
  793. + else
  794. + len = 16;
  795. +
  796. + /* replace rtp expect src addr */
  797. + t.src.l3num = family;
  798. + t.dst.protonum = IPPROTO_UDP;
  799. +
  800. + memcpy(&t.src.u3, &ct->tuplehash[dir].tuple.src.u3, len);
  801. + if (sizeof(t.src.u3) > len)
  802. + /* address needs to be cleared for nf_ct_tuple_equal */
  803. + memset((void *)&t.src.u3 + len, 0, sizeof(t.src.u3) - len);
  804. +
  805. + t.src.u.all = 0;
  806. +
  807. + memcpy(&t.dst.u3, &ct->tuplehash[dir].tuple.dst.u3, len);
  808. + if (sizeof(t.dst.u3) > len)
  809. + /* address needs to be cleared for nf_ct_tuple_equal */
  810. + memset((void *)&t.dst.u3 + len, 0, sizeof(t.dst.u3) - len);
  811. +
  812. + t.dst.u.all = htons(expinfo.loport);
  813. +
  814. + /* get the rtp expect and replace the srcaddr with RTP server addr */
  815. + exp_ct = nf_ct_expect_find_get(net, nf_ct_zone(ct), &t);
  816. + if (exp_ct) {
  817. + memcpy(&exp_ct->tuple.src.u3, &srvaddr, len);
  818. + if (sizeof(exp_ct->tuple.src.u3) > len)
  819. + /* address needs to be cleared for nf_ct_tuple_equal */
  820. + memset((void *)&exp_ct->tuple.src.u3 + len, 0,
  821. + sizeof(exp_ct->tuple.src.u3) - len);
  822. + } else
  823. + goto out;
  824. +
  825. + /* replace rtcp expect src addr */
  826. + if (expinfo.pbtype == pb_range) {
  827. + t.dst.u.all = htons(expinfo.hiport);
  828. +
  829. + /* get the rtcp expect and replace the srcaddr with RTP server addr */
  830. + exp_ct = nf_ct_expect_find_get(net, nf_ct_zone(ct), &t);
  831. + if (exp_ct) {
  832. + memcpy(&exp_ct->tuple.src.u3, &srvaddr, len);
  833. + if (sizeof(exp_ct->tuple.src.u3) > len)
  834. + /* address needs to be cleared for nf_ct_tuple_equal */
  835. + memset((void *)&exp_ct->tuple.src.u3 + len, 0,
  836. + sizeof(exp_ct->tuple.src.u3) - len);
  837. + } else
  838. + goto out;
  839. + }
  840. +
  841. + goto out;
  842. + }
  843. +out:
  844. + return ret;
  845. + }
  846. +
  847. +static int help(struct sk_buff *skb, unsigned int protoff,
  848. + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  849. +{
  850. + struct tcphdr _tcph, *th;
  851. + unsigned int dataoff, datalen;
  852. + char *rb_ptr;
  853. + int ret = NF_DROP;
  854. +
  855. + /* Until there's been traffic both ways, don't look in packets. */
  856. + if (ctinfo != IP_CT_ESTABLISHED &&
  857. + ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
  858. + pr_debug("conntrackinfo = %u\n", ctinfo);
  859. + return NF_ACCEPT;
  860. + }
  861. +
  862. + /* Not whole TCP header? */
  863. + th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
  864. +
  865. + if (!th)
  866. + return NF_ACCEPT;
  867. +
  868. + /* No data ? */
  869. + dataoff = protoff + th->doff*4;
  870. + datalen = skb->len - dataoff;
  871. + if (dataoff >= skb->len)
  872. + return NF_ACCEPT;
  873. +
  874. + spin_lock_bh(&rtsp_buffer_lock);
  875. + rb_ptr = skb_header_pointer(skb, dataoff,
  876. + skb->len - dataoff, rtsp_buffer);
  877. + BUG_ON(rb_ptr == NULL);
  878. +
  879. +#if 0
  880. + /* Checksum invalid? Ignore. */
  881. + /* FIXME: Source route IP option packets --RR */
  882. + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
  883. + csum_partial((char*)tcph, tcplen, 0)))
  884. + {
  885. + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
  886. + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  887. + return NF_ACCEPT;
  888. + }
  889. +#endif
  890. +
  891. + switch (CTINFO2DIR(ctinfo)) {
  892. + case IP_CT_DIR_ORIGINAL:
  893. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  894. + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo, protoff);
  895. +#else
  896. + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
  897. +#endif
  898. + break;
  899. + case IP_CT_DIR_REPLY:
  900. + pr_debug("IP_CT_DIR_REPLY\n");
  901. + /* inbound packet: server->client */
  902. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  903. + ret = help_in(skb, rb_ptr, datalen, ct, ctinfo, protoff);
  904. +#else
  905. + ret = help_in(skb, rb_ptr, datalen, ct, ctinfo);
  906. +#endif
  907. + break;
  908. + default:
  909. + break;
  910. + }
  911. +
  912. + spin_unlock_bh(&rtsp_buffer_lock);
  913. +
  914. + return ret;
  915. +}
  916. +
  917. +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
  918. +static char rtsp_names[MAX_PORTS][10];
  919. +
  920. +/* This function is intentionally _NOT_ defined as __exit */
  921. +static void
  922. +fini(void)
  923. +{
  924. + int i;
  925. + for (i = 0; i < num_ports; i++) {
  926. + pr_debug("unregistering port %d\n", ports[i]);
  927. + nf_conntrack_helper_unregister(&rtsp_helpers[i]);
  928. + }
  929. + kfree(rtsp_buffer);
  930. +}
  931. +
  932. +static int __init
  933. +init(void)
  934. +{
  935. + int i, ret;
  936. + struct nf_conntrack_helper *hlpr;
  937. + char *tmpname;
  938. +
  939. + printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
  940. +
  941. + if (max_outstanding < 1) {
  942. + printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
  943. + return -EBUSY;
  944. + }
  945. + if (setup_timeout < 0) {
  946. + printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
  947. + return -EBUSY;
  948. + }
  949. +
  950. + rtsp_exp_policy.max_expected = max_outstanding;
  951. + rtsp_exp_policy.timeout = setup_timeout;
  952. +
  953. + rtsp_buffer = kmalloc(65536, GFP_KERNEL);
  954. + if (!rtsp_buffer)
  955. + return -ENOMEM;
  956. +
  957. + /* If no port given, default to standard rtsp port */
  958. + if (ports[0] == 0) {
  959. + ports[0] = RTSP_PORT;
  960. + num_ports = 1;
  961. + }
  962. +
  963. + for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
  964. + hlpr = &rtsp_helpers[i];
  965. + memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
  966. + hlpr->tuple.src.l3num = AF_INET;
  967. + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
  968. + hlpr->tuple.dst.protonum = IPPROTO_TCP;
  969. + hlpr->expect_policy = &rtsp_exp_policy;
  970. + hlpr->me = THIS_MODULE;
  971. + hlpr->help = help;
  972. +
  973. + tmpname = &rtsp_names[i][0];
  974. + if (ports[i] == RTSP_PORT) {
  975. + sprintf(tmpname, "rtsp");
  976. + } else {
  977. + sprintf(tmpname, "rtsp-%d", i);
  978. + }
  979. +
  980. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
  981. + strlcpy(hlpr->name, tmpname, sizeof(hlpr->name));
  982. +#else
  983. + hlpr->name = tmpname;
  984. +#endif
  985. + pr_debug("port #%d: %d\n", i, ports[i]);
  986. +
  987. + ret = nf_conntrack_helper_register(hlpr);
  988. +
  989. + if (ret) {
  990. + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
  991. + fini();
  992. + return -EBUSY;
  993. + }
  994. + }
  995. + return 0;
  996. +}
  997. +
  998. +module_init(init);
  999. +module_exit(fini);
  1000. --- /dev/null
  1001. +++ b/extensions/rtsp/nf_conntrack_rtsp.h
  1002. @@ -0,0 +1,74 @@
  1003. +/*
  1004. + * RTSP extension for IP connection tracking.
  1005. + * (C) 2003 by Tom Marshall <tmarshall at real.com>
  1006. + * based on ip_conntrack_irc.h
  1007. + *
  1008. + * This program is free software; you can redistribute it and/or
  1009. + * modify it under the terms of the GNU General Public License
  1010. + * as published by the Free Software Foundation; either version
  1011. + * 2 of the License, or (at your option) any later version.
  1012. + *
  1013. + * 2013-03-04: Il'inykh Sergey <sergeyi at inango-sw.com>. Inango Systems Ltd
  1014. + * - conditional compilation for kernel 3.7
  1015. + * - port mapping improvements
  1016. +*/
  1017. +#ifndef _IP_CONNTRACK_RTSP_H
  1018. +#define _IP_CONNTRACK_RTSP_H
  1019. +
  1020. +#include <linux/version.h>
  1021. +
  1022. +//#define IP_NF_RTSP_DEBUG 1
  1023. +#define IP_NF_RTSP_VERSION "0.7"
  1024. +
  1025. +#ifdef __KERNEL__
  1026. +/* port block types */
  1027. +typedef enum {
  1028. + pb_single, /* client_port=x */
  1029. + pb_range, /* client_port=x-y */
  1030. + pb_discon /* client_port=x/y (rtspbis) */
  1031. +} portblock_t;
  1032. +
  1033. +/* We record seq number and length of rtsp headers here, all in host order. */
  1034. +
  1035. +/*
  1036. + * This structure is per expected connection. It is a member of struct
  1037. + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
  1038. + * there and we are expected to only store the length of the data which
  1039. + * needs replaced. If a packet contains multiple RTSP messages, we create
  1040. + * one expected connection per message.
  1041. + *
  1042. + * We use these variables to mark the entire header block. This may seem
  1043. + * like overkill, but the nature of RTSP requires it. A header may appear
  1044. + * multiple times in a message. We must treat two Transport headers the
  1045. + * same as one Transport header with two entries.
  1046. + */
  1047. +struct ip_ct_rtsp_expect
  1048. +{
  1049. + u_int32_t len; /* length of header block */
  1050. + portblock_t pbtype; /* Type of port block that was requested */
  1051. + u_int16_t loport; /* Port that was requested, low or first */
  1052. + u_int16_t hiport; /* Port that was requested, high or second */
  1053. + union nf_inet_addr srvaddr; /* src address in SETUP reply */
  1054. + union nf_inet_addr cltaddr; /* destination address */
  1055. +#if 0
  1056. + uint method; /* RTSP method */
  1057. + uint cseq; /* CSeq from request */
  1058. +#endif
  1059. +};
  1060. +
  1061. +extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
  1062. + enum ip_conntrack_info ctinfo,
  1063. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1064. + unsigned int protoff,
  1065. +#endif
  1066. + unsigned int matchoff,
  1067. + unsigned int matchlen,
  1068. + struct ip_ct_rtsp_expect *prtspexp,
  1069. + struct nf_conntrack_expect *rtp_exp,
  1070. + struct nf_conntrack_expect *rtcp_exp);
  1071. +
  1072. +#define RTSP_PORT 554
  1073. +
  1074. +#endif /* __KERNEL__ */
  1075. +
  1076. +#endif /* _IP_CONNTRACK_RTSP_H */
  1077. --- /dev/null
  1078. +++ b/extensions/rtsp/nf_nat_rtsp.c
  1079. @@ -0,0 +1,634 @@
  1080. +/*
  1081. + * RTSP extension for TCP NAT alteration
  1082. + * (C) 2003 by Tom Marshall <tmarshall at real.com>
  1083. + *
  1084. + * 2013-03-04: Il'inykh Sergey <sergeyi at inango-sw.com>. Inango Systems Ltd
  1085. + * - fixed rtcp nat mapping and other port mapping fixes
  1086. + * - fixed system hard lock because of bug in the parser
  1087. + * - codestyle fixes and less significant fixes
  1088. + *
  1089. + * based on ip_nat_irc.c
  1090. + *
  1091. + * This program is free software; you can redistribute it and/or
  1092. + * modify it under the terms of the GNU General Public License
  1093. + * as published by the Free Software Foundation; either version
  1094. + * 2 of the License, or (at your option) any later version.
  1095. + *
  1096. + * Module load syntax:
  1097. + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
  1098. + * stunaddr=<address>
  1099. + * destaction=[auto|strip|none]
  1100. + *
  1101. + * If no ports are specified, the default will be port 554 only.
  1102. + *
  1103. + * stunaddr specifies the address used to detect that a client is using STUN.
  1104. + * If this address is seen in the destination parameter, it is assumed that
  1105. + * the client has already punched a UDP hole in the firewall, so we don't
  1106. + * mangle the client_port. If none is specified, it is autodetected. It
  1107. + * only needs to be set if you have multiple levels of NAT. It should be
  1108. + * set to the external address that the STUN clients detect. Note that in
  1109. + * this case, it will not be possible for clients to use UDP with servers
  1110. + * between the NATs.
  1111. + *
  1112. + * If no destaction is specified, auto is used.
  1113. + * destaction=auto: strip destination parameter if it is not stunaddr.
  1114. + * destaction=strip: always strip destination parameter (not recommended).
  1115. + * destaction=none: do not touch destination parameter (not recommended).
  1116. + */
  1117. +
  1118. +#include <linux/module.h>
  1119. +#include <linux/version.h>
  1120. +#include <net/tcp.h>
  1121. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1122. +# include <net/netfilter/nf_nat.h>
  1123. +#else
  1124. +# include <net/netfilter/nf_nat_rule.h>
  1125. +#endif
  1126. +#include <net/netfilter/nf_nat_helper.h>
  1127. +#include "nf_conntrack_rtsp.h"
  1128. +#include <net/netfilter/nf_conntrack_expect.h>
  1129. +
  1130. +#include <linux/inet.h>
  1131. +#include <linux/ctype.h>
  1132. +#define NF_NEED_STRNCASECMP
  1133. +#define NF_NEED_STRTOU16
  1134. +#include "netfilter_helpers.h"
  1135. +#define NF_NEED_MIME_NEXTLINE
  1136. +#include "netfilter_mime.h"
  1137. +
  1138. +#define MAX_PORTS 8
  1139. +#define DSTACT_AUTO 0
  1140. +#define DSTACT_STRIP 1
  1141. +#define DSTACT_NONE 2
  1142. +
  1143. +static char* stunaddr = NULL;
  1144. +static char* destaction = NULL;
  1145. +
  1146. +static u_int32_t extip = 0;
  1147. +static int dstact = 0;
  1148. +
  1149. +static void nf_nat_rtsp_expected(struct nf_conn* ct, struct nf_conntrack_expect *exp);
  1150. +
  1151. +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
  1152. +MODULE_DESCRIPTION("RTSP network address translation module");
  1153. +MODULE_LICENSE("GPL");
  1154. +module_param(stunaddr, charp, 0644);
  1155. +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
  1156. +module_param(destaction, charp, 0644);
  1157. +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
  1158. +
  1159. +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
  1160. +
  1161. +/*** helper functions ***/
  1162. +
  1163. +static void
  1164. +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
  1165. +{
  1166. + struct iphdr* iph = ip_hdr(skb);
  1167. + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
  1168. +
  1169. + *pptcpdata = (char*)tcph + tcph->doff*4;
  1170. + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
  1171. +}
  1172. +
  1173. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1174. +/* copy of sip_sprintf_addr */
  1175. +static int rtsp_sprintf_addr(const struct nf_conn *ct, char *buffer,
  1176. + const union nf_inet_addr *addr, bool delim)
  1177. +{
  1178. + if (nf_ct_l3num(ct) == NFPROTO_IPV4) {
  1179. + return sprintf(buffer, "%pI4", &addr->ip);
  1180. + } else {
  1181. + if (delim)
  1182. + return sprintf(buffer, "[%pI6c]", &addr->ip6);
  1183. + else
  1184. + return sprintf(buffer, "%pI6c", &addr->ip6);
  1185. + }
  1186. +}
  1187. +#endif
  1188. +
  1189. +/*** nat functions ***/
  1190. +
  1191. +/*
  1192. + * Mangle the "Transport:" header:
  1193. + * - Replace all occurences of "client_port=<spec>"
  1194. + * - Handle destination parameter
  1195. + *
  1196. + * In:
  1197. + * ct, ctinfo = conntrack context
  1198. + * skb = packet
  1199. + * tranoff = Transport header offset from TCP data
  1200. + * tranlen = Transport header length (incl. CRLF)
  1201. + * rport_lo = replacement low port (host endian)
  1202. + * rport_hi = replacement high port (host endian)
  1203. + *
  1204. + * Returns packet size difference.
  1205. + *
  1206. + * Assumes that a complete transport header is present, ending with CR or LF
  1207. + */
  1208. +static int
  1209. +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
  1210. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1211. + unsigned int protoff,
  1212. +#endif
  1213. + struct nf_conntrack_expect* rtp_exp,
  1214. + struct nf_conntrack_expect* rtcp_exp,
  1215. + struct ip_ct_rtsp_expect* prtspexp,
  1216. + struct sk_buff* skb, uint tranoff, uint tranlen)
  1217. +{
  1218. + char* ptcp;
  1219. + uint tcplen;
  1220. + char* ptran;
  1221. + char rbuf1[16]; /* Replacement buffer (one port) */
  1222. + uint rbuf1len; /* Replacement len (one port) */
  1223. + char rbufa[16]; /* Replacement buffer (all ports) */
  1224. + uint rbufalen; /* Replacement len (all ports) */
  1225. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1226. + union nf_inet_addr newip;
  1227. +#else
  1228. + u_int32_t newip;
  1229. +#endif
  1230. + u_int16_t loport, hiport;
  1231. + uint off = 0;
  1232. + uint diff; /* Number of bytes we removed */
  1233. +
  1234. + struct nf_conn *ct = rtp_exp->master;
  1235. + /* struct nf_conn *ct = nf_ct_get(skb, &ctinfo); */
  1236. + struct nf_conntrack_tuple *rtp_t;
  1237. +
  1238. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1239. + char szextaddr[INET6_ADDRSTRLEN + 16];
  1240. +#else
  1241. + char szextaddr[INET_ADDRSTRLEN + 16];
  1242. +#endif
  1243. + uint extaddrlen;
  1244. + int is_stun;
  1245. +
  1246. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1247. + ptran = ptcp+tranoff;
  1248. +
  1249. + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
  1250. + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
  1251. + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
  1252. + pr_info("sanity check failed\n");
  1253. + return 0;
  1254. + }
  1255. + off += 10;
  1256. + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
  1257. +
  1258. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1259. + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3;
  1260. + rtp_t = &rtp_exp->tuple;
  1261. + rtp_t->dst.u3 = newip;
  1262. + if (rtcp_exp) {
  1263. + rtcp_exp->tuple.dst.u3 = newip;
  1264. + }
  1265. + extaddrlen = rtsp_sprintf_addr(ct, szextaddr, &newip, true); // FIXME handle extip
  1266. + pr_debug("stunaddr=%s (auto)\n", szextaddr);
  1267. +#else
  1268. + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
  1269. + rtp_t = &rtp_exp->tuple;
  1270. + rtp_t->dst.u3.ip = newip;
  1271. + if (rtcp_exp) {
  1272. + rtcp_exp->tuple.dst.u3.ip = newip;
  1273. + }
  1274. + extaddrlen = extip ? sprintf(szextaddr, "%pI4", &extip)
  1275. + : sprintf(szextaddr, "%pI4", &newip);
  1276. + pr_debug("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
  1277. +#endif
  1278. + hiport = 0;
  1279. + rbuf1len = rbufalen = 0;
  1280. + switch (prtspexp->pbtype) {
  1281. + case pb_single:
  1282. + for (loport = prtspexp->loport; loport != 0; loport++) { /* XXX: improper wrap? */
  1283. + rtp_t->dst.u.udp.port = htons(loport);
  1284. + if (nf_ct_expect_related(rtp_exp) == 0) {
  1285. + pr_debug("using port %hu\n", loport);
  1286. + break;
  1287. + }
  1288. + }
  1289. + if (loport != 0) {
  1290. + rbuf1len = sprintf(rbuf1, "%hu", loport);
  1291. + rbufalen = sprintf(rbufa, "%hu", loport);
  1292. + }
  1293. + break;
  1294. + case pb_range:
  1295. + for (loport = prtspexp->loport; loport != 0; loport += 2) { /* XXX: improper wrap? */
  1296. + rtp_t->dst.u.udp.port = htons(loport);
  1297. + if (nf_ct_expect_related(rtp_exp) != 0) {
  1298. + continue;
  1299. + }
  1300. + hiport = loport + 1;
  1301. + rtcp_exp->tuple.dst.u.udp.port = htons(hiport);
  1302. + if (nf_ct_expect_related(rtcp_exp) != 0) {
  1303. + nf_ct_unexpect_related(rtp_exp);
  1304. + continue;
  1305. + }
  1306. +
  1307. + /* FIXME: invalid print in case of ipv6 */
  1308. + pr_debug("nat expect_related %pI4:%u-%u-%pI4:%u-%u\n",
  1309. + &rtp_exp->tuple.src.u3.ip,
  1310. + ntohs(rtp_exp->tuple.src.u.udp.port),
  1311. + ntohs(rtcp_exp->tuple.src.u.udp.port),
  1312. + &rtp_exp->tuple.dst.u3.ip,
  1313. + ntohs(rtp_exp->tuple.dst.u.udp.port),
  1314. + ntohs(rtcp_exp->tuple.dst.u.udp.port));
  1315. + break;
  1316. + }
  1317. + if (loport != 0) {
  1318. + rbuf1len = sprintf(rbuf1, "%hu", loport);
  1319. + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
  1320. + }
  1321. + break;
  1322. + case pb_discon:
  1323. + for (loport = prtspexp->loport; loport != 0; loport++) { /* XXX: improper wrap? */
  1324. + rtp_t->dst.u.udp.port = htons(loport);
  1325. + if (nf_ct_expect_related(rtp_exp) == 0) {
  1326. + pr_debug("using port %hu (1 of 2)\n", loport);
  1327. + break;
  1328. + }
  1329. + }
  1330. + for (hiport = prtspexp->hiport; hiport != 0; hiport++) { /* XXX: improper wrap? */
  1331. + rtp_t->dst.u.udp.port = htons(hiport);
  1332. + if (nf_ct_expect_related(rtp_exp) == 0) {
  1333. + pr_debug("using port %hu (2 of 2)\n", hiport);
  1334. + break;
  1335. + }
  1336. + }
  1337. + if (loport != 0 && hiport != 0) {
  1338. + rbuf1len = sprintf(rbuf1, "%hu", loport);
  1339. + rbufalen = sprintf(rbufa, hiport == loport+1 ?
  1340. + "%hu-%hu":"%hu/%hu", loport, hiport);
  1341. + }
  1342. + break;
  1343. + }
  1344. +
  1345. + if (rbuf1len == 0)
  1346. + return 0; /* cannot get replacement port(s) */
  1347. +
  1348. + /* Transport: tran;field;field=val,tran;field;field=val,...
  1349. + `off` is set to the start of Transport value from start of line
  1350. + */
  1351. + while (off < tranlen) {
  1352. + uint saveoff;
  1353. + const char* pparamend;
  1354. + uint nextparamoff;
  1355. +
  1356. + pparamend = memchr(ptran+off, ',', tranlen-off);
  1357. + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
  1358. + nextparamoff = pparamend-ptran;
  1359. +
  1360. + /*
  1361. + * We pass over each param twice. On the first pass, we look for a
  1362. + * destination= field. It is handled by the security policy. If it
  1363. + * is present, allowed, and equal to our external address, we assume
  1364. + * that STUN is being used and we leave the client_port= field alone.
  1365. + */
  1366. + is_stun = 0;
  1367. + saveoff = off;
  1368. + while (off < nextparamoff) {
  1369. + const char* pfieldend;
  1370. + uint nextfieldoff;
  1371. +
  1372. + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
  1373. + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
  1374. + SKIP_WSPACE(ptran, nextfieldoff, off);
  1375. +
  1376. + if (dstact != DSTACT_NONE && nextfieldoff - off > 12 && strncmp(ptran+off, "destination=", 12) == 0) {
  1377. + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
  1378. + is_stun = 1;
  1379. +
  1380. + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun)) {
  1381. + uint dstoff = (ptran-ptcp)+off;
  1382. + uint dstlen = nextfieldoff-off;
  1383. + char* pdstrep = NULL;
  1384. + uint dstreplen = 0;
  1385. + diff = dstlen;
  1386. + if (dstact == DSTACT_AUTO && !is_stun) {
  1387. + const char* psep = memchr(ptran+off, ':', dstlen);
  1388. + u_int16_t port;
  1389. +
  1390. + dstoff += 12;
  1391. + dstlen -= 13;
  1392. + pdstrep = szextaddr;
  1393. +
  1394. + if (psep != NULL && nf_strtou16(psep + 1, &port)) {
  1395. + pr_debug("RTSP: replace dst addr&port\n");
  1396. +
  1397. + if (port != prtspexp->loport) {
  1398. + pr_debug("multiple ports found, port %hu ignored\n", port);
  1399. + dstreplen = extaddrlen;
  1400. + } else {
  1401. + sprintf(szextaddr+extaddrlen, ":%s", rbuf1);
  1402. + dstreplen = extaddrlen+1+rbuf1len;
  1403. + }
  1404. + } else {
  1405. + pr_debug("RTSP: replace dst addr\n");
  1406. + dstreplen = extaddrlen;
  1407. + }
  1408. + diff = nextfieldoff-off-13-dstreplen;
  1409. + }
  1410. +
  1411. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1412. + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
  1413. + dstoff, dstlen, pdstrep, dstreplen)) {
  1414. +#else
  1415. + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
  1416. + dstoff, dstlen, pdstrep, dstreplen)) {
  1417. +#endif
  1418. + /* mangle failed, all we can do is bail */
  1419. + nf_ct_unexpect_related(rtp_exp);
  1420. + if (rtcp_exp)
  1421. + nf_ct_unexpect_related(rtcp_exp);
  1422. + return 0;
  1423. + }
  1424. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1425. + ptran = ptcp+tranoff;
  1426. + tranlen -= diff;
  1427. + nextparamoff -= diff;
  1428. + nextfieldoff -= diff;
  1429. + }
  1430. + }
  1431. +
  1432. + off = nextfieldoff;
  1433. + }
  1434. +
  1435. + if (is_stun)
  1436. + continue;
  1437. +
  1438. + off = saveoff;
  1439. + while (off < nextparamoff) {
  1440. + const char* pfieldend;
  1441. + uint nextfieldoff;
  1442. +
  1443. + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
  1444. + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
  1445. +
  1446. + if (nextfieldoff - off > 12 && strncmp(ptran+off, "client_port=", 12) == 0) {
  1447. + u_int16_t port;
  1448. + uint numlen;
  1449. + uint origoff;
  1450. + uint origlen;
  1451. + char* rbuf = rbuf1;
  1452. + uint rbuflen = rbuf1len;
  1453. +
  1454. + off += 12;
  1455. + origoff = (ptran-ptcp)+off;
  1456. + origlen = 0;
  1457. + numlen = nf_strtou16(ptran+off, &port);
  1458. + off += numlen;
  1459. + origlen += numlen;
  1460. + if (port != prtspexp->loport) {
  1461. + pr_debug("multiple ports found, port %hu ignored\n", port);
  1462. + } else {
  1463. + if (ptran[off] == '-' || ptran[off] == '/') {
  1464. + off++;
  1465. + origlen++;
  1466. + numlen = nf_strtou16(ptran+off, &port);
  1467. + off += numlen;
  1468. + origlen += numlen;
  1469. + rbuf = rbufa;
  1470. + rbuflen = rbufalen;
  1471. + }
  1472. +
  1473. + /*
  1474. + * note we cannot just memcpy() if the sizes are the same.
  1475. + * the mangle function does skb resizing, checks for a
  1476. + * cloned skb, and updates the checksums.
  1477. + *
  1478. + * parameter 4 below is offset from start of tcp data.
  1479. + */
  1480. + diff = origlen-rbuflen;
  1481. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1482. + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
  1483. + origoff, origlen, rbuf, rbuflen)) {
  1484. +#else
  1485. + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
  1486. + origoff, origlen, rbuf, rbuflen)) {
  1487. +#endif
  1488. + /* mangle failed, all we can do is bail */
  1489. + nf_ct_unexpect_related(rtp_exp);
  1490. + if (rtcp_exp)
  1491. + nf_ct_unexpect_related(rtcp_exp);
  1492. + return 0;
  1493. + }
  1494. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1495. + ptran = ptcp+tranoff;
  1496. + tranlen -= diff;
  1497. + nextparamoff -= diff;
  1498. + nextfieldoff -= diff;
  1499. + }
  1500. + }
  1501. +
  1502. + off = nextfieldoff;
  1503. + }
  1504. +
  1505. + off = nextparamoff;
  1506. + }
  1507. +
  1508. + return 1;
  1509. +}
  1510. +
  1511. +static uint
  1512. +help_out(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
  1513. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1514. + unsigned int protoff,
  1515. +#endif
  1516. + unsigned int matchoff, unsigned int matchlen,
  1517. + struct ip_ct_rtsp_expect* prtspexp,
  1518. + struct nf_conntrack_expect* rtp_exp,
  1519. + struct nf_conntrack_expect* rtcp_exp)
  1520. +{
  1521. + char* ptcp;
  1522. + uint tcplen;
  1523. + uint hdrsoff;
  1524. + uint hdrslen;
  1525. + uint lineoff;
  1526. + uint linelen;
  1527. + uint off;
  1528. + int dir = CTINFO2DIR(ctinfo);
  1529. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1530. + union nf_inet_addr saddr = rtp_exp->master->tuplehash[dir].tuple.src.u3;
  1531. +#else
  1532. + __be32 saddr = rtp_exp->master->tuplehash[dir].tuple.src.u3.ip;
  1533. +#endif
  1534. +
  1535. + //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
  1536. + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
  1537. +
  1538. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1539. + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
  1540. + hdrslen = matchlen;
  1541. + off = hdrsoff;
  1542. + pr_debug("NAT rtsp help_out\n");
  1543. +
  1544. + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen)) {
  1545. + if (linelen == 0)
  1546. + break;
  1547. +
  1548. + if (off > hdrsoff+hdrslen) {
  1549. + pr_info("!! overrun !!");
  1550. + break;
  1551. + }
  1552. + pr_debug("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
  1553. +
  1554. + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
  1555. + uint oldtcplen = tcplen;
  1556. + pr_debug("hdr: Transport\n");
  1557. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1558. + if (!rtsp_mangle_tran(ctinfo, protoff, rtp_exp, rtcp_exp,
  1559. + prtspexp, skb, lineoff, linelen)) {
  1560. +#else
  1561. + if (!rtsp_mangle_tran(ctinfo, rtp_exp, rtcp_exp, prtspexp,
  1562. + skb, lineoff, linelen)) {
  1563. +#endif
  1564. + pr_debug("hdr: Transport mangle failed");
  1565. + break;
  1566. + }
  1567. + rtp_exp->expectfn = nf_nat_rtsp_expected;
  1568. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1569. + rtp_exp->saved_addr = saddr;
  1570. +#else
  1571. + rtp_exp->saved_ip = saddr;
  1572. +#endif
  1573. + rtp_exp->saved_proto.udp.port = htons(prtspexp->loport);
  1574. + rtp_exp->dir = !dir;
  1575. + if (rtcp_exp) {
  1576. + rtcp_exp->expectfn = nf_nat_rtsp_expected;
  1577. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1578. + rtcp_exp->saved_addr = saddr;
  1579. +#else
  1580. + rtcp_exp->saved_ip = saddr;
  1581. +#endif
  1582. + rtcp_exp->saved_proto.udp.port = htons(prtspexp->hiport);
  1583. + rtcp_exp->dir = !dir;
  1584. + }
  1585. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1586. + hdrslen -= (oldtcplen-tcplen);
  1587. + off -= (oldtcplen-tcplen);
  1588. + lineoff -= (oldtcplen-tcplen);
  1589. + linelen -= (oldtcplen-tcplen);
  1590. + pr_debug("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
  1591. + }
  1592. + }
  1593. +
  1594. + return NF_ACCEPT;
  1595. +}
  1596. +
  1597. +static unsigned int
  1598. +nf_nat_rtsp(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
  1599. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1600. + unsigned int protoff,
  1601. +#endif
  1602. + unsigned int matchoff, unsigned int matchlen,
  1603. + struct ip_ct_rtsp_expect* prtspexp,
  1604. + struct nf_conntrack_expect* rtp_exp,
  1605. + struct nf_conntrack_expect* rtcp_exp)
  1606. +{
  1607. + int dir = CTINFO2DIR(ctinfo);
  1608. + int rc = NF_ACCEPT;
  1609. +
  1610. + switch (dir) {
  1611. + case IP_CT_DIR_ORIGINAL:
  1612. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1613. + rc = help_out(skb, ctinfo, protoff, matchoff, matchlen, prtspexp,
  1614. + rtp_exp, rtcp_exp);
  1615. +#else
  1616. + rc = help_out(skb, ctinfo, matchoff, matchlen, prtspexp,
  1617. + rtp_exp, rtcp_exp);
  1618. +#endif
  1619. + break;
  1620. + case IP_CT_DIR_REPLY:
  1621. + pr_debug("unmangle ! %u\n", ctinfo);
  1622. + /* XXX: unmangle */
  1623. + rc = NF_ACCEPT;
  1624. + break;
  1625. + }
  1626. + //UNLOCK_BH(&ip_rtsp_lock);
  1627. +
  1628. + return rc;
  1629. +}
  1630. +
  1631. +static void nf_nat_rtsp_expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
  1632. +{
  1633. +#if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0) || LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1634. + struct nf_nat_range range;
  1635. +#else
  1636. + struct nf_nat_ipv4_range range;
  1637. +#endif
  1638. +
  1639. + /* This must be a fresh one. */
  1640. + BUG_ON(ct->status & IPS_NAT_DONE_MASK);
  1641. +
  1642. + /* For DST manip, map port here to where it's expected. */
  1643. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1644. + range.min_proto = range.max_proto = exp->saved_proto;
  1645. + range.min_addr = range.max_addr = exp->saved_addr;
  1646. +#else
  1647. + range.min = range.max = exp->saved_proto;
  1648. + range.min_ip = range.max_ip = exp->saved_ip;
  1649. +#endif
  1650. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0)
  1651. + range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
  1652. + nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
  1653. +#else
  1654. + range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED);
  1655. + nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
  1656. +#endif
  1657. +
  1658. + /* Change src to where master sends to, but only if the connection
  1659. + * actually came from the same source. */
  1660. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1661. + if (nf_inet_addr_cmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3,
  1662. + &ct->master->tuplehash[exp->dir].tuple.src.u3)) {
  1663. + range.min_addr = range.max_addr
  1664. + = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
  1665. +#else
  1666. + if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip ==
  1667. + ct->master->tuplehash[exp->dir].tuple.src.u3.ip) {
  1668. + range.min_ip = range.max_ip
  1669. + = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
  1670. +#endif
  1671. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0)
  1672. + range.flags = NF_NAT_RANGE_MAP_IPS;
  1673. + nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
  1674. +#else
  1675. + range.flags = IP_NAT_RANGE_MAP_IPS;
  1676. + nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
  1677. +#endif
  1678. + }
  1679. +}
  1680. +
  1681. +
  1682. +static void __exit fini(void)
  1683. +{
  1684. + rcu_assign_pointer(nf_nat_rtsp_hook, NULL);
  1685. + synchronize_net();
  1686. +}
  1687. +
  1688. +static int __init init(void)
  1689. +{
  1690. + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
  1691. +
  1692. + BUG_ON(nf_nat_rtsp_hook);
  1693. + rcu_assign_pointer(nf_nat_rtsp_hook, nf_nat_rtsp);
  1694. +
  1695. + if (stunaddr != NULL)
  1696. + extip = in_aton(stunaddr);
  1697. +
  1698. + if (destaction != NULL) {
  1699. + if (strcmp(destaction, "auto") == 0)
  1700. + dstact = DSTACT_AUTO;
  1701. +
  1702. + if (strcmp(destaction, "strip") == 0)
  1703. + dstact = DSTACT_STRIP;
  1704. +
  1705. + if (strcmp(destaction, "none") == 0)
  1706. + dstact = DSTACT_NONE;
  1707. + }
  1708. +
  1709. + return 0;
  1710. +}
  1711. +
  1712. +module_init(init);
  1713. +module_exit(fini);
  1714. --- a/extensions/Kbuild
  1715. +++ b/extensions/Kbuild
  1716. @@ -26,6 +26,7 @@ obj-${build_lscan} += xt_lscan.o
  1717. obj-${build_pknock} += pknock/
  1718. obj-${build_psd} += xt_psd.o
  1719. obj-${build_quota2} += xt_quota2.o
  1720. +obj-${build_rtsp} += rtsp/
  1721. -include ${M}/*.Kbuild
  1722. -include ${M}/Kbuild.*
  1723. --- a/mconfig
  1724. +++ b/mconfig
  1725. @@ -22,3 +22,4 @@ build_lscan=m
  1726. build_pknock=m
  1727. build_psd=m
  1728. build_quota2=m
  1729. +build_rtsp=m