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.

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