100-add-rtsp-conntrack.patch 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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,576 @@
  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. + *
  255. + * based on ip_conntrack_irc.c
  256. + *
  257. + * This program is free software; you can redistribute it and/or
  258. + * modify it under the terms of the GNU General Public License
  259. + * as published by the Free Software Foundation; either version
  260. + * 2 of the License, or (at your option) any later version.
  261. + *
  262. + * Module load syntax:
  263. + * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
  264. + * max_outstanding=n setup_timeout=secs
  265. + *
  266. + * If no ports are specified, the default will be port 554.
  267. + *
  268. + * With max_outstanding you can define the maximum number of not yet
  269. + * answered SETUP requests per RTSP session (default 8).
  270. + * With setup_timeout you can specify how long the system waits for
  271. + * an expected data channel (default 300 seconds).
  272. + *
  273. + */
  274. +
  275. +#include <linux/module.h>
  276. +#include <linux/netfilter.h>
  277. +#include <linux/ip.h>
  278. +#include <linux/inet.h>
  279. +#include <net/tcp.h>
  280. +
  281. +#include <net/netfilter/nf_conntrack.h>
  282. +#include <net/netfilter/nf_conntrack_expect.h>
  283. +#include <net/netfilter/nf_conntrack_helper.h>
  284. +#include "nf_conntrack_rtsp.h"
  285. +
  286. +#define NF_NEED_STRNCASECMP
  287. +#define NF_NEED_STRTOU16
  288. +#define NF_NEED_STRTOU32
  289. +#define NF_NEED_NEXTLINE
  290. +#include "netfilter_helpers.h"
  291. +#define NF_NEED_MIME_NEXTLINE
  292. +#include "netfilter_mime.h"
  293. +
  294. +#include <linux/ctype.h>
  295. +
  296. +#define MAX_PORTS 8
  297. +static int ports[MAX_PORTS];
  298. +static int num_ports = 0;
  299. +static int max_outstanding = 8;
  300. +static unsigned int setup_timeout = 300;
  301. +
  302. +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
  303. +MODULE_DESCRIPTION("RTSP connection tracking module");
  304. +MODULE_LICENSE("GPL");
  305. +module_param_array(ports, int, &num_ports, 0400);
  306. +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
  307. +module_param(max_outstanding, int, 0400);
  308. +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
  309. +module_param(setup_timeout, int, 0400);
  310. +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
  311. +
  312. +static char *rtsp_buffer;
  313. +static DEFINE_SPINLOCK(rtsp_buffer_lock);
  314. +
  315. +static struct nf_conntrack_expect_policy rtsp_exp_policy;
  316. +
  317. +unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
  318. + enum ip_conntrack_info ctinfo,
  319. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  320. + unsigned int protoff,
  321. +#endif
  322. + unsigned int matchoff, unsigned int matchlen,
  323. + struct ip_ct_rtsp_expect* prtspexp,
  324. + struct nf_conntrack_expect *rtp_exp,
  325. + struct nf_conntrack_expect *rtcp_exp);
  326. +
  327. +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
  328. +
  329. +/*
  330. + * Max mappings we will allow for one RTSP connection (for RTP, the number
  331. + * of allocated ports is twice this value). Note that SMIL burns a lot of
  332. + * ports so keep this reasonably high. If this is too low, you will see a
  333. + * lot of "no free client map entries" messages.
  334. + */
  335. +#define MAX_PORT_MAPS 16
  336. +
  337. +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
  338. +
  339. +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
  340. +
  341. +/*
  342. + * Parse an RTSP packet.
  343. + *
  344. + * Returns zero if parsing failed.
  345. + *
  346. + * Parameters:
  347. + * IN ptcp tcp data pointer
  348. + * IN tcplen tcp data len
  349. + * IN/OUT ptcpoff points to current tcp offset
  350. + * OUT phdrsoff set to offset of rtsp headers
  351. + * OUT phdrslen set to length of rtsp headers
  352. + * OUT pcseqoff set to offset of CSeq header
  353. + * OUT pcseqlen set to length of CSeq header
  354. + */
  355. +static int
  356. +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
  357. + uint* phdrsoff, uint* phdrslen,
  358. + uint* pcseqoff, uint* pcseqlen,
  359. + uint* transoff, uint* translen)
  360. +{
  361. + uint entitylen = 0;
  362. + uint lineoff;
  363. + uint linelen;
  364. +
  365. + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
  366. + return 0;
  367. +
  368. + *phdrsoff = *ptcpoff;
  369. + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
  370. + if (linelen == 0) {
  371. + if (entitylen > 0)
  372. + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
  373. + break;
  374. + }
  375. + if (lineoff+linelen > tcplen) {
  376. + pr_info("!! overrun !!\n");
  377. + break;
  378. + }
  379. +
  380. + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
  381. + *pcseqoff = lineoff;
  382. + *pcseqlen = linelen;
  383. + }
  384. +
  385. + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
  386. + *transoff = lineoff;
  387. + *translen = linelen;
  388. + }
  389. +
  390. + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0) {
  391. + uint off = lineoff+15;
  392. + SKIP_WSPACE(ptcp+lineoff, linelen, off);
  393. + nf_strtou32(ptcp+off, &entitylen);
  394. + }
  395. + }
  396. + *phdrslen = (*ptcpoff) - (*phdrsoff);
  397. +
  398. + return 1;
  399. +}
  400. +
  401. +/*
  402. + * Find lo/hi client ports (if any) in transport header
  403. + * In:
  404. + * ptcp, tcplen = packet
  405. + * tranoff, tranlen = buffer to search
  406. + *
  407. + * Out:
  408. + * pport_lo, pport_hi = lo/hi ports (host endian)
  409. + *
  410. + * Returns nonzero if any client ports found
  411. + *
  412. + * Note: it is valid (and expected) for the client to request multiple
  413. + * transports, so we need to parse the entire line.
  414. + */
  415. +static int
  416. +rtsp_parse_transport(char* ptran, uint tranlen,
  417. + struct ip_ct_rtsp_expect* prtspexp)
  418. +{
  419. + int rc = 0;
  420. + uint off = 0;
  421. +
  422. + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
  423. + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
  424. + pr_info("sanity check failed\n");
  425. + return 0;
  426. + }
  427. +
  428. + pr_debug("tran='%.*s'\n", (int)tranlen, ptran);
  429. + off += 10;
  430. + SKIP_WSPACE(ptran, tranlen, off);
  431. +
  432. + /* Transport: tran;field;field=val,tran;field;field=val,... */
  433. + while (off < tranlen) {
  434. + const char* pparamend;
  435. + uint nextparamoff;
  436. +
  437. + pparamend = memchr(ptran+off, ',', tranlen-off);
  438. + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
  439. + nextparamoff = pparamend-ptran;
  440. +
  441. + while (off < nextparamoff) {
  442. + const char* pfieldend;
  443. + uint nextfieldoff;
  444. +
  445. + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
  446. + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
  447. +
  448. + if (strncmp(ptran+off, "client_port=", 12) == 0) {
  449. + u_int16_t port;
  450. + uint numlen;
  451. +
  452. + off += 12;
  453. + numlen = nf_strtou16(ptran+off, &port);
  454. + off += numlen;
  455. + if (prtspexp->loport != 0 && prtspexp->loport != port)
  456. + pr_debug("multiple ports found, port %hu ignored\n", port);
  457. + else {
  458. + pr_debug("lo port found : %hu\n", port);
  459. + prtspexp->loport = prtspexp->hiport = port;
  460. + if (ptran[off] == '-') {
  461. + off++;
  462. + numlen = nf_strtou16(ptran+off, &port);
  463. + off += numlen;
  464. + prtspexp->pbtype = pb_range;
  465. + prtspexp->hiport = port;
  466. +
  467. + // If we have a range, assume rtp:
  468. + // loport must be even, hiport must be loport+1
  469. + if ((prtspexp->loport & 0x0001) != 0 ||
  470. + prtspexp->hiport != prtspexp->loport+1) {
  471. + pr_debug("incorrect range: %hu-%hu, correcting\n",
  472. + prtspexp->loport, prtspexp->hiport);
  473. + prtspexp->loport &= 0xfffe;
  474. + prtspexp->hiport = prtspexp->loport+1;
  475. + }
  476. + } else if (ptran[off] == '/') {
  477. + off++;
  478. + numlen = nf_strtou16(ptran+off, &port);
  479. + off += numlen;
  480. + prtspexp->pbtype = pb_discon;
  481. + prtspexp->hiport = port;
  482. + }
  483. + rc = 1;
  484. + }
  485. + }
  486. +
  487. + /*
  488. + * Note we don't look for the destination parameter here.
  489. + * If we are using NAT, the NAT module will handle it. If not,
  490. + * and the client is sending packets elsewhere, the expectation
  491. + * will quietly time out.
  492. + */
  493. +
  494. + off = nextfieldoff;
  495. + }
  496. +
  497. + off = nextparamoff;
  498. + }
  499. +
  500. + return rc;
  501. +}
  502. +
  503. +
  504. +/*** conntrack functions ***/
  505. +
  506. +/* outbound packet: client->server */
  507. +
  508. +static inline int
  509. +help_out(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
  510. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  511. + struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  512. + unsigned int protoff)
  513. +#else
  514. + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  515. +#endif
  516. +{
  517. + struct ip_ct_rtsp_expect expinfo;
  518. +
  519. + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
  520. + //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
  521. + //uint tcplen = pktlen - iph->ihl * 4;
  522. + char* pdata = rb_ptr;
  523. + //uint datalen = tcplen - tcph->doff * 4;
  524. + uint dataoff = 0;
  525. + int ret = NF_ACCEPT;
  526. +
  527. + struct nf_conntrack_expect *rtp_exp;
  528. + struct nf_conntrack_expect *rtcp_exp = NULL;
  529. +
  530. + __be16 be_loport;
  531. + __be16 be_hiport;
  532. +
  533. + typeof(nf_nat_rtsp_hook) nf_nat_rtsp;
  534. +
  535. + memset(&expinfo, 0, sizeof(expinfo));
  536. +
  537. + while (dataoff < datalen) {
  538. + uint cmdoff = dataoff;
  539. + uint hdrsoff = 0;
  540. + uint hdrslen = 0;
  541. + uint cseqoff = 0;
  542. + uint cseqlen = 0;
  543. + uint transoff = 0;
  544. + uint translen = 0;
  545. + uint off;
  546. +
  547. + if (!rtsp_parse_message(pdata, datalen, &dataoff,
  548. + &hdrsoff, &hdrslen,
  549. + &cseqoff, &cseqlen,
  550. + &transoff, &translen))
  551. + break; /* not a valid message */
  552. +
  553. + if (strncmp(pdata+cmdoff, "TEARDOWN ", 9) == 0) {
  554. + pr_debug("teardown handled\n");
  555. + nf_ct_remove_expectations(ct); /* FIXME must be session id aware */
  556. + break;
  557. + }
  558. +
  559. + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
  560. + continue; /* not a SETUP message */
  561. +
  562. + pr_debug("found a setup message\n");
  563. +
  564. + off = 0;
  565. + if(translen)
  566. + rtsp_parse_transport(pdata+transoff, translen, &expinfo);
  567. +
  568. + if (expinfo.loport == 0) {
  569. + pr_debug("no udp transports found\n");
  570. + continue; /* no udp transports found */
  571. + }
  572. +
  573. + pr_debug("udp transport found, ports=(%d,%hu,%hu)\n",
  574. + (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
  575. +
  576. +
  577. + be_loport = htons(expinfo.loport);
  578. +
  579. + rtp_exp = nf_ct_expect_alloc(ct);
  580. + if (rtp_exp == NULL) {
  581. + ret = NF_DROP;
  582. + goto out;
  583. + }
  584. +
  585. + nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT,
  586. + nf_ct_l3num(ct),
  587. + NULL, /* &ct->tuplehash[!dir].tuple.src.u3, */
  588. + &ct->tuplehash[!dir].tuple.dst.u3,
  589. + IPPROTO_UDP, NULL, &be_loport);
  590. +
  591. + rtp_exp->flags = 0;
  592. +
  593. + if (expinfo.pbtype == pb_range) {
  594. + pr_debug("setup expectation for rtcp\n");
  595. +
  596. + be_hiport = htons(expinfo.hiport);
  597. + rtcp_exp = nf_ct_expect_alloc(ct);
  598. + if (rtcp_exp == NULL) {
  599. + ret = NF_DROP;
  600. + goto out1;
  601. + }
  602. +
  603. + nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT,
  604. + nf_ct_l3num(ct),
  605. + NULL, /* &ct->tuplehash[!dir].tuple.src.u3, */
  606. + &ct->tuplehash[!dir].tuple.dst.u3,
  607. + IPPROTO_UDP, NULL, &be_hiport);
  608. +
  609. + rtcp_exp->flags = 0;
  610. +
  611. + pr_debug("expect_related %pI4:%u-%u-%pI4:%u-%u\n",
  612. + &rtp_exp->tuple.src.u3.ip,
  613. + ntohs(rtp_exp->tuple.src.u.udp.port),
  614. + ntohs(rtcp_exp->tuple.src.u.udp.port),
  615. + &rtp_exp->tuple.dst.u3.ip,
  616. + ntohs(rtp_exp->tuple.dst.u.udp.port),
  617. + ntohs(rtcp_exp->tuple.dst.u.udp.port));
  618. + } else {
  619. + pr_debug("expect_related %pI4:%u-%pI4:%u\n",
  620. + &rtp_exp->tuple.src.u3.ip,
  621. + ntohs(rtp_exp->tuple.src.u.udp.port),
  622. + &rtp_exp->tuple.dst.u3.ip,
  623. + ntohs(rtp_exp->tuple.dst.u.udp.port));
  624. + }
  625. +
  626. + nf_nat_rtsp = rcu_dereference(nf_nat_rtsp_hook);
  627. + if (nf_nat_rtsp && ct->status & IPS_NAT_MASK)
  628. + /* pass the request off to the nat helper */
  629. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  630. + ret = nf_nat_rtsp(skb, ctinfo, protoff, hdrsoff, hdrslen,
  631. + &expinfo, rtp_exp, rtcp_exp);
  632. +#else
  633. + ret = nf_nat_rtsp(skb, ctinfo, hdrsoff, hdrslen,
  634. + &expinfo, rtp_exp, rtcp_exp);
  635. +#endif
  636. + else {
  637. + if (nf_ct_expect_related(rtp_exp) == 0) {
  638. + if (rtcp_exp && nf_ct_expect_related(rtcp_exp) != 0) {
  639. + nf_ct_unexpect_related(rtp_exp);
  640. + pr_info("nf_conntrack_expect_related failed for rtcp\n");
  641. + ret = NF_DROP;
  642. + }
  643. + } else {
  644. + pr_info("nf_conntrack_expect_related failed for rtp\n");
  645. + ret = NF_DROP;
  646. + }
  647. + }
  648. + if (rtcp_exp) {
  649. + nf_ct_expect_put(rtcp_exp);
  650. + }
  651. +out1:
  652. + nf_ct_expect_put(rtp_exp);
  653. + goto out;
  654. + }
  655. +out:
  656. +
  657. + return ret;
  658. +}
  659. +
  660. +
  661. +static inline int
  662. +help_in(struct sk_buff *skb, size_t pktlen,
  663. + struct nf_conn* ct, enum ip_conntrack_info ctinfo)
  664. +{
  665. + return NF_ACCEPT;
  666. +}
  667. +
  668. +static int help(struct sk_buff *skb, unsigned int protoff,
  669. + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  670. +{
  671. + struct tcphdr _tcph, *th;
  672. + unsigned int dataoff, datalen;
  673. + char *rb_ptr;
  674. + int ret = NF_DROP;
  675. +
  676. + /* Until there's been traffic both ways, don't look in packets. */
  677. + if (ctinfo != IP_CT_ESTABLISHED &&
  678. + ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
  679. + pr_debug("conntrackinfo = %u\n", ctinfo);
  680. + return NF_ACCEPT;
  681. + }
  682. +
  683. + /* Not whole TCP header? */
  684. + th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
  685. +
  686. + if (!th)
  687. + return NF_ACCEPT;
  688. +
  689. + /* No data ? */
  690. + dataoff = protoff + th->doff*4;
  691. + datalen = skb->len - dataoff;
  692. + if (dataoff >= skb->len)
  693. + return NF_ACCEPT;
  694. +
  695. + spin_lock_bh(&rtsp_buffer_lock);
  696. + rb_ptr = skb_header_pointer(skb, dataoff,
  697. + skb->len - dataoff, rtsp_buffer);
  698. + BUG_ON(rb_ptr == NULL);
  699. +
  700. +#if 0
  701. + /* Checksum invalid? Ignore. */
  702. + /* FIXME: Source route IP option packets --RR */
  703. + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
  704. + csum_partial((char*)tcph, tcplen, 0)))
  705. + {
  706. + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
  707. + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  708. + return NF_ACCEPT;
  709. + }
  710. +#endif
  711. +
  712. + switch (CTINFO2DIR(ctinfo)) {
  713. + case IP_CT_DIR_ORIGINAL:
  714. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  715. + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo, protoff);
  716. +#else
  717. + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
  718. +#endif
  719. + break;
  720. + case IP_CT_DIR_REPLY:
  721. + pr_debug("IP_CT_DIR_REPLY\n");
  722. + /* inbound packet: server->client */
  723. + ret = NF_ACCEPT;
  724. + break;
  725. + }
  726. +
  727. + spin_unlock_bh(&rtsp_buffer_lock);
  728. +
  729. + return ret;
  730. +}
  731. +
  732. +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
  733. +static char rtsp_names[MAX_PORTS][10];
  734. +
  735. +/* This function is intentionally _NOT_ defined as __exit */
  736. +static void
  737. +fini(void)
  738. +{
  739. + int i;
  740. + for (i = 0; i < num_ports; i++) {
  741. + pr_debug("unregistering port %d\n", ports[i]);
  742. + nf_conntrack_helper_unregister(&rtsp_helpers[i]);
  743. + }
  744. + kfree(rtsp_buffer);
  745. +}
  746. +
  747. +static int __init
  748. +init(void)
  749. +{
  750. + int i, ret;
  751. + struct nf_conntrack_helper *hlpr;
  752. + char *tmpname;
  753. +
  754. + printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
  755. +
  756. + if (max_outstanding < 1) {
  757. + printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
  758. + return -EBUSY;
  759. + }
  760. + if (setup_timeout < 0) {
  761. + printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
  762. + return -EBUSY;
  763. + }
  764. +
  765. + rtsp_exp_policy.max_expected = max_outstanding;
  766. + rtsp_exp_policy.timeout = setup_timeout;
  767. +
  768. + rtsp_buffer = kmalloc(65536, GFP_KERNEL);
  769. + if (!rtsp_buffer)
  770. + return -ENOMEM;
  771. +
  772. + /* If no port given, default to standard rtsp port */
  773. + if (ports[0] == 0) {
  774. + ports[0] = RTSP_PORT;
  775. + num_ports = 1;
  776. + }
  777. +
  778. + for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
  779. + hlpr = &rtsp_helpers[i];
  780. + memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
  781. + hlpr->tuple.src.l3num = AF_INET;
  782. + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
  783. + hlpr->tuple.dst.protonum = IPPROTO_TCP;
  784. + hlpr->expect_policy = &rtsp_exp_policy;
  785. + hlpr->me = THIS_MODULE;
  786. + hlpr->help = help;
  787. +
  788. + tmpname = &rtsp_names[i][0];
  789. + if (ports[i] == RTSP_PORT) {
  790. + sprintf(tmpname, "rtsp");
  791. + } else {
  792. + sprintf(tmpname, "rtsp-%d", i);
  793. + }
  794. +
  795. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
  796. + strlcpy(hlpr->name, tmpname, sizeof(hlpr->name));
  797. +#else
  798. + hlpr->name = tmpname;
  799. +#endif
  800. + pr_debug("port #%d: %d\n", i, ports[i]);
  801. +
  802. + ret = nf_conntrack_helper_register(hlpr);
  803. +
  804. + if (ret) {
  805. + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
  806. + fini();
  807. + return -EBUSY;
  808. + }
  809. + }
  810. + return 0;
  811. +}
  812. +
  813. +module_init(init);
  814. +module_exit(fini);
  815. --- /dev/null
  816. +++ b/extensions/rtsp/nf_conntrack_rtsp.h
  817. @@ -0,0 +1,72 @@
  818. +/*
  819. + * RTSP extension for IP connection tracking.
  820. + * (C) 2003 by Tom Marshall <tmarshall at real.com>
  821. + * based on ip_conntrack_irc.h
  822. + *
  823. + * This program is free software; you can redistribute it and/or
  824. + * modify it under the terms of the GNU General Public License
  825. + * as published by the Free Software Foundation; either version
  826. + * 2 of the License, or (at your option) any later version.
  827. + *
  828. + * 2013-03-04: Il'inykh Sergey <sergeyi at inango-sw.com>. Inango Systems Ltd
  829. + * - conditional compilation for kernel 3.7
  830. + * - port mapping improvements
  831. +*/
  832. +#ifndef _IP_CONNTRACK_RTSP_H
  833. +#define _IP_CONNTRACK_RTSP_H
  834. +
  835. +#include <linux/version.h>
  836. +
  837. +//#define IP_NF_RTSP_DEBUG 1
  838. +#define IP_NF_RTSP_VERSION "0.7"
  839. +
  840. +#ifdef __KERNEL__
  841. +/* port block types */
  842. +typedef enum {
  843. + pb_single, /* client_port=x */
  844. + pb_range, /* client_port=x-y */
  845. + pb_discon /* client_port=x/y (rtspbis) */
  846. +} portblock_t;
  847. +
  848. +/* We record seq number and length of rtsp headers here, all in host order. */
  849. +
  850. +/*
  851. + * This structure is per expected connection. It is a member of struct
  852. + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
  853. + * there and we are expected to only store the length of the data which
  854. + * needs replaced. If a packet contains multiple RTSP messages, we create
  855. + * one expected connection per message.
  856. + *
  857. + * We use these variables to mark the entire header block. This may seem
  858. + * like overkill, but the nature of RTSP requires it. A header may appear
  859. + * multiple times in a message. We must treat two Transport headers the
  860. + * same as one Transport header with two entries.
  861. + */
  862. +struct ip_ct_rtsp_expect
  863. +{
  864. + u_int32_t len; /* length of header block */
  865. + portblock_t pbtype; /* Type of port block that was requested */
  866. + u_int16_t loport; /* Port that was requested, low or first */
  867. + u_int16_t hiport; /* Port that was requested, high or second */
  868. +#if 0
  869. + uint method; /* RTSP method */
  870. + uint cseq; /* CSeq from request */
  871. +#endif
  872. +};
  873. +
  874. +extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
  875. + enum ip_conntrack_info ctinfo,
  876. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  877. + unsigned int protoff,
  878. +#endif
  879. + unsigned int matchoff,
  880. + unsigned int matchlen,
  881. + struct ip_ct_rtsp_expect *prtspexp,
  882. + struct nf_conntrack_expect *rtp_exp,
  883. + struct nf_conntrack_expect *rtcp_exp);
  884. +
  885. +#define RTSP_PORT 554
  886. +
  887. +#endif /* __KERNEL__ */
  888. +
  889. +#endif /* _IP_CONNTRACK_RTSP_H */
  890. --- /dev/null
  891. +++ b/extensions/rtsp/nf_nat_rtsp.c
  892. @@ -0,0 +1,617 @@
  893. +/*
  894. + * RTSP extension for TCP NAT alteration
  895. + * (C) 2003 by Tom Marshall <tmarshall at real.com>
  896. + *
  897. + * 2013-03-04: Il'inykh Sergey <sergeyi at inango-sw.com>. Inango Systems Ltd
  898. + * - fixed rtcp nat mapping and other port mapping fixes
  899. + * - fixed system hard lock because of bug in the parser
  900. + * - codestyle fixes and less significant fixes
  901. + *
  902. + * based on ip_nat_irc.c
  903. + *
  904. + * This program is free software; you can redistribute it and/or
  905. + * modify it under the terms of the GNU General Public License
  906. + * as published by the Free Software Foundation; either version
  907. + * 2 of the License, or (at your option) any later version.
  908. + *
  909. + * Module load syntax:
  910. + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
  911. + * stunaddr=<address>
  912. + * destaction=[auto|strip|none]
  913. + *
  914. + * If no ports are specified, the default will be port 554 only.
  915. + *
  916. + * stunaddr specifies the address used to detect that a client is using STUN.
  917. + * If this address is seen in the destination parameter, it is assumed that
  918. + * the client has already punched a UDP hole in the firewall, so we don't
  919. + * mangle the client_port. If none is specified, it is autodetected. It
  920. + * only needs to be set if you have multiple levels of NAT. It should be
  921. + * set to the external address that the STUN clients detect. Note that in
  922. + * this case, it will not be possible for clients to use UDP with servers
  923. + * between the NATs.
  924. + *
  925. + * If no destaction is specified, auto is used.
  926. + * destaction=auto: strip destination parameter if it is not stunaddr.
  927. + * destaction=strip: always strip destination parameter (not recommended).
  928. + * destaction=none: do not touch destination parameter (not recommended).
  929. + */
  930. +
  931. +#include <linux/module.h>
  932. +#include <linux/version.h>
  933. +#include <net/tcp.h>
  934. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  935. +# include <net/netfilter/nf_nat.h>
  936. +#else
  937. +# include <net/netfilter/nf_nat_rule.h>
  938. +#endif
  939. +#include <net/netfilter/nf_nat_helper.h>
  940. +#include "nf_conntrack_rtsp.h"
  941. +#include <net/netfilter/nf_conntrack_expect.h>
  942. +
  943. +#include <linux/inet.h>
  944. +#include <linux/ctype.h>
  945. +#define NF_NEED_STRNCASECMP
  946. +#define NF_NEED_STRTOU16
  947. +#include "netfilter_helpers.h"
  948. +#define NF_NEED_MIME_NEXTLINE
  949. +#include "netfilter_mime.h"
  950. +
  951. +#define MAX_PORTS 8
  952. +#define DSTACT_AUTO 0
  953. +#define DSTACT_STRIP 1
  954. +#define DSTACT_NONE 2
  955. +
  956. +static char* stunaddr = NULL;
  957. +static char* destaction = NULL;
  958. +
  959. +static u_int32_t extip = 0;
  960. +static int dstact = 0;
  961. +
  962. +static void nf_nat_rtsp_expected(struct nf_conn* ct, struct nf_conntrack_expect *exp);
  963. +
  964. +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
  965. +MODULE_DESCRIPTION("RTSP network address translation module");
  966. +MODULE_LICENSE("GPL");
  967. +module_param(stunaddr, charp, 0644);
  968. +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
  969. +module_param(destaction, charp, 0644);
  970. +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
  971. +
  972. +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
  973. +
  974. +/*** helper functions ***/
  975. +
  976. +static void
  977. +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
  978. +{
  979. + struct iphdr* iph = ip_hdr(skb);
  980. + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
  981. +
  982. + *pptcpdata = (char*)tcph + tcph->doff*4;
  983. + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
  984. +}
  985. +
  986. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  987. +/* copy of sip_sprintf_addr */
  988. +static int rtsp_sprintf_addr(const struct nf_conn *ct, char *buffer,
  989. + const union nf_inet_addr *addr, bool delim)
  990. +{
  991. + if (nf_ct_l3num(ct) == NFPROTO_IPV4) {
  992. + return sprintf(buffer, "%pI4", &addr->ip);
  993. + } else {
  994. + if (delim)
  995. + return sprintf(buffer, "[%pI6c]", &addr->ip6);
  996. + else
  997. + return sprintf(buffer, "%pI6c", &addr->ip6);
  998. + }
  999. +}
  1000. +#endif
  1001. +
  1002. +/*** nat functions ***/
  1003. +
  1004. +/*
  1005. + * Mangle the "Transport:" header:
  1006. + * - Replace all occurences of "client_port=<spec>"
  1007. + * - Handle destination parameter
  1008. + *
  1009. + * In:
  1010. + * ct, ctinfo = conntrack context
  1011. + * skb = packet
  1012. + * tranoff = Transport header offset from TCP data
  1013. + * tranlen = Transport header length (incl. CRLF)
  1014. + * rport_lo = replacement low port (host endian)
  1015. + * rport_hi = replacement high port (host endian)
  1016. + *
  1017. + * Returns packet size difference.
  1018. + *
  1019. + * Assumes that a complete transport header is present, ending with CR or LF
  1020. + */
  1021. +static int
  1022. +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
  1023. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1024. + unsigned int protoff,
  1025. +#endif
  1026. + struct nf_conntrack_expect* rtp_exp,
  1027. + struct nf_conntrack_expect* rtcp_exp,
  1028. + struct ip_ct_rtsp_expect* prtspexp,
  1029. + struct sk_buff* skb, uint tranoff, uint tranlen)
  1030. +{
  1031. + char* ptcp;
  1032. + uint tcplen;
  1033. + char* ptran;
  1034. + char rbuf1[16]; /* Replacement buffer (one port) */
  1035. + uint rbuf1len; /* Replacement len (one port) */
  1036. + char rbufa[16]; /* Replacement buffer (all ports) */
  1037. + uint rbufalen; /* Replacement len (all ports) */
  1038. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1039. + union nf_inet_addr newip;
  1040. +#else
  1041. + u_int32_t newip;
  1042. +#endif
  1043. + u_int16_t loport, hiport;
  1044. + uint off = 0;
  1045. + uint diff; /* Number of bytes we removed */
  1046. +
  1047. + struct nf_conn *ct = rtp_exp->master;
  1048. + /* struct nf_conn *ct = nf_ct_get(skb, &ctinfo); */
  1049. + struct nf_conntrack_tuple *rtp_t;
  1050. +
  1051. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1052. + char szextaddr[INET6_ADDRSTRLEN];
  1053. +#else
  1054. + char szextaddr[INET_ADDRSTRLEN];
  1055. +#endif
  1056. + uint extaddrlen;
  1057. + int is_stun;
  1058. +
  1059. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1060. + ptran = ptcp+tranoff;
  1061. +
  1062. + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
  1063. + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
  1064. + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
  1065. + pr_info("sanity check failed\n");
  1066. + return 0;
  1067. + }
  1068. + off += 10;
  1069. + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
  1070. +
  1071. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1072. + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3;
  1073. + rtp_t = &rtp_exp->tuple;
  1074. + rtp_t->dst.u3 = newip;
  1075. + if (rtcp_exp) {
  1076. + rtcp_exp->tuple.dst.u3 = newip;
  1077. + }
  1078. + extaddrlen = rtsp_sprintf_addr(ct, szextaddr, &newip, true); // FIXME handle extip
  1079. + pr_debug("stunaddr=%s (auto)\n", szextaddr);
  1080. +#else
  1081. + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
  1082. + rtp_t = &rtp_exp->tuple;
  1083. + rtp_t->dst.u3.ip = newip;
  1084. + if (rtcp_exp) {
  1085. + rtcp_exp->tuple.dst.u3.ip = newip;
  1086. + }
  1087. + extaddrlen = extip ? sprintf(szextaddr, "%pI4", &extip)
  1088. + : sprintf(szextaddr, "%pI4", &newip);
  1089. + pr_debug("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
  1090. +#endif
  1091. + hiport = 0;
  1092. + rbuf1len = rbufalen = 0;
  1093. + switch (prtspexp->pbtype) {
  1094. + case pb_single:
  1095. + for (loport = prtspexp->loport; loport != 0; loport++) { /* XXX: improper wrap? */
  1096. + rtp_t->dst.u.udp.port = htons(loport);
  1097. + if (nf_ct_expect_related(rtp_exp) == 0) {
  1098. + pr_debug("using port %hu\n", loport);
  1099. + break;
  1100. + }
  1101. + }
  1102. + if (loport != 0) {
  1103. + rbuf1len = sprintf(rbuf1, "%hu", loport);
  1104. + rbufalen = sprintf(rbufa, "%hu", loport);
  1105. + }
  1106. + break;
  1107. + case pb_range:
  1108. + for (loport = prtspexp->loport; loport != 0; loport += 2) { /* XXX: improper wrap? */
  1109. + rtp_t->dst.u.udp.port = htons(loport);
  1110. + if (nf_ct_expect_related(rtp_exp) != 0) {
  1111. + continue;
  1112. + }
  1113. + hiport = loport + 1;
  1114. + rtcp_exp->tuple.dst.u.udp.port = htons(hiport);
  1115. + if (nf_ct_expect_related(rtcp_exp) != 0) {
  1116. + nf_ct_unexpect_related(rtp_exp);
  1117. + continue;
  1118. + }
  1119. +
  1120. + /* FIXME: invalid print in case of ipv6 */
  1121. + pr_debug("nat expect_related %pI4:%u-%u-%pI4:%u-%u\n",
  1122. + &rtp_exp->tuple.src.u3.ip,
  1123. + ntohs(rtp_exp->tuple.src.u.udp.port),
  1124. + ntohs(rtcp_exp->tuple.src.u.udp.port),
  1125. + &rtp_exp->tuple.dst.u3.ip,
  1126. + ntohs(rtp_exp->tuple.dst.u.udp.port),
  1127. + ntohs(rtcp_exp->tuple.dst.u.udp.port));
  1128. + break;
  1129. + }
  1130. + if (loport != 0) {
  1131. + rbuf1len = sprintf(rbuf1, "%hu", loport);
  1132. + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
  1133. + }
  1134. + break;
  1135. + case pb_discon:
  1136. + for (loport = prtspexp->loport; loport != 0; loport++) { /* XXX: improper wrap? */
  1137. + rtp_t->dst.u.udp.port = htons(loport);
  1138. + if (nf_ct_expect_related(rtp_exp) == 0) {
  1139. + pr_debug("using port %hu (1 of 2)\n", loport);
  1140. + break;
  1141. + }
  1142. + }
  1143. + for (hiport = prtspexp->hiport; hiport != 0; hiport++) { /* XXX: improper wrap? */
  1144. + rtp_t->dst.u.udp.port = htons(hiport);
  1145. + if (nf_ct_expect_related(rtp_exp) == 0) {
  1146. + pr_debug("using port %hu (2 of 2)\n", hiport);
  1147. + break;
  1148. + }
  1149. + }
  1150. + if (loport != 0 && hiport != 0) {
  1151. + rbuf1len = sprintf(rbuf1, "%hu", loport);
  1152. + rbufalen = sprintf(rbufa, hiport == loport+1 ?
  1153. + "%hu-%hu":"%hu/%hu", loport, hiport);
  1154. + }
  1155. + break;
  1156. + }
  1157. +
  1158. + if (rbuf1len == 0)
  1159. + return 0; /* cannot get replacement port(s) */
  1160. +
  1161. + /* Transport: tran;field;field=val,tran;field;field=val,...
  1162. + `off` is set to the start of Transport value from start of line
  1163. + */
  1164. + while (off < tranlen) {
  1165. + uint saveoff;
  1166. + const char* pparamend;
  1167. + uint nextparamoff;
  1168. +
  1169. + pparamend = memchr(ptran+off, ',', tranlen-off);
  1170. + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
  1171. + nextparamoff = pparamend-ptran;
  1172. +
  1173. + /*
  1174. + * We pass over each param twice. On the first pass, we look for a
  1175. + * destination= field. It is handled by the security policy. If it
  1176. + * is present, allowed, and equal to our external address, we assume
  1177. + * that STUN is being used and we leave the client_port= field alone.
  1178. + */
  1179. + is_stun = 0;
  1180. + saveoff = off;
  1181. + while (off < nextparamoff) {
  1182. + const char* pfieldend;
  1183. + uint nextfieldoff;
  1184. +
  1185. + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
  1186. + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
  1187. +
  1188. + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0) {
  1189. + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
  1190. + is_stun = 1;
  1191. +
  1192. + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun)) {
  1193. + uint dstoff = (ptran-ptcp)+off;
  1194. + uint dstlen = nextfieldoff-off;
  1195. + char* pdstrep = NULL;
  1196. + uint dstreplen = 0;
  1197. + diff = dstlen;
  1198. + if (dstact == DSTACT_AUTO && !is_stun) {
  1199. + pr_debug("RTSP: replace dst addr\n");
  1200. + dstoff += 12;
  1201. + dstlen -= 13;
  1202. + pdstrep = szextaddr;
  1203. + dstreplen = extaddrlen;
  1204. + diff = nextfieldoff-off-13-extaddrlen;
  1205. + }
  1206. +
  1207. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1208. + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
  1209. + dstoff, dstlen, pdstrep, dstreplen)) {
  1210. +#else
  1211. + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
  1212. + dstoff, dstlen, pdstrep, dstreplen)) {
  1213. +#endif
  1214. + /* mangle failed, all we can do is bail */
  1215. + nf_ct_unexpect_related(rtp_exp);
  1216. + if (rtcp_exp)
  1217. + nf_ct_unexpect_related(rtcp_exp);
  1218. + return 0;
  1219. + }
  1220. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1221. + ptran = ptcp+tranoff;
  1222. + tranlen -= diff;
  1223. + nextparamoff -= diff;
  1224. + nextfieldoff -= diff;
  1225. + }
  1226. + }
  1227. +
  1228. + off = nextfieldoff;
  1229. + }
  1230. +
  1231. + if (is_stun)
  1232. + continue;
  1233. +
  1234. + off = saveoff;
  1235. + while (off < nextparamoff) {
  1236. + const char* pfieldend;
  1237. + uint nextfieldoff;
  1238. +
  1239. + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
  1240. + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
  1241. +
  1242. + if (strncmp(ptran+off, "client_port=", 12) == 0) {
  1243. + u_int16_t port;
  1244. + uint numlen;
  1245. + uint origoff;
  1246. + uint origlen;
  1247. + char* rbuf = rbuf1;
  1248. + uint rbuflen = rbuf1len;
  1249. +
  1250. + off += 12;
  1251. + origoff = (ptran-ptcp)+off;
  1252. + origlen = 0;
  1253. + numlen = nf_strtou16(ptran+off, &port);
  1254. + off += numlen;
  1255. + origlen += numlen;
  1256. + if (port != prtspexp->loport) {
  1257. + pr_debug("multiple ports found, port %hu ignored\n", port);
  1258. + } else {
  1259. + if (ptran[off] == '-' || ptran[off] == '/') {
  1260. + off++;
  1261. + origlen++;
  1262. + numlen = nf_strtou16(ptran+off, &port);
  1263. + off += numlen;
  1264. + origlen += numlen;
  1265. + rbuf = rbufa;
  1266. + rbuflen = rbufalen;
  1267. + }
  1268. +
  1269. + /*
  1270. + * note we cannot just memcpy() if the sizes are the same.
  1271. + * the mangle function does skb resizing, checks for a
  1272. + * cloned skb, and updates the checksums.
  1273. + *
  1274. + * parameter 4 below is offset from start of tcp data.
  1275. + */
  1276. + diff = origlen-rbuflen;
  1277. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1278. + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
  1279. + origoff, origlen, rbuf, rbuflen)) {
  1280. +#else
  1281. + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
  1282. + origoff, origlen, rbuf, rbuflen)) {
  1283. +#endif
  1284. + /* mangle failed, all we can do is bail */
  1285. + nf_ct_unexpect_related(rtp_exp);
  1286. + if (rtcp_exp)
  1287. + nf_ct_unexpect_related(rtcp_exp);
  1288. + return 0;
  1289. + }
  1290. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1291. + ptran = ptcp+tranoff;
  1292. + tranlen -= diff;
  1293. + nextparamoff -= diff;
  1294. + nextfieldoff -= diff;
  1295. + }
  1296. + }
  1297. +
  1298. + off = nextfieldoff;
  1299. + }
  1300. +
  1301. + off = nextparamoff;
  1302. + }
  1303. +
  1304. + return 1;
  1305. +}
  1306. +
  1307. +static uint
  1308. +help_out(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
  1309. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1310. + unsigned int protoff,
  1311. +#endif
  1312. + unsigned int matchoff, unsigned int matchlen,
  1313. + struct ip_ct_rtsp_expect* prtspexp,
  1314. + struct nf_conntrack_expect* rtp_exp,
  1315. + struct nf_conntrack_expect* rtcp_exp)
  1316. +{
  1317. + char* ptcp;
  1318. + uint tcplen;
  1319. + uint hdrsoff;
  1320. + uint hdrslen;
  1321. + uint lineoff;
  1322. + uint linelen;
  1323. + uint off;
  1324. + int dir = CTINFO2DIR(ctinfo);
  1325. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1326. + union nf_inet_addr saddr = rtp_exp->master->tuplehash[dir].tuple.src.u3;
  1327. +#else
  1328. + __be32 saddr = rtp_exp->master->tuplehash[dir].tuple.src.u3.ip;
  1329. +#endif
  1330. +
  1331. + //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
  1332. + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
  1333. +
  1334. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1335. + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
  1336. + hdrslen = matchlen;
  1337. + off = hdrsoff;
  1338. + pr_debug("NAT rtsp help_out\n");
  1339. +
  1340. + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen)) {
  1341. + if (linelen == 0)
  1342. + break;
  1343. +
  1344. + if (off > hdrsoff+hdrslen) {
  1345. + pr_info("!! overrun !!");
  1346. + break;
  1347. + }
  1348. + pr_debug("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
  1349. +
  1350. + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
  1351. + uint oldtcplen = tcplen;
  1352. + pr_debug("hdr: Transport\n");
  1353. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1354. + if (!rtsp_mangle_tran(ctinfo, protoff, rtp_exp, rtcp_exp,
  1355. + prtspexp, skb, lineoff, linelen)) {
  1356. +#else
  1357. + if (!rtsp_mangle_tran(ctinfo, rtp_exp, rtcp_exp, prtspexp,
  1358. + skb, lineoff, linelen)) {
  1359. +#endif
  1360. + pr_debug("hdr: Transport mangle failed");
  1361. + break;
  1362. + }
  1363. + rtp_exp->expectfn = nf_nat_rtsp_expected;
  1364. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1365. + rtp_exp->saved_addr = saddr;
  1366. +#else
  1367. + rtp_exp->saved_ip = saddr;
  1368. +#endif
  1369. + rtp_exp->saved_proto.udp.port = htons(prtspexp->loport);
  1370. + rtp_exp->dir = !dir;
  1371. + if (rtcp_exp) {
  1372. + rtcp_exp->expectfn = nf_nat_rtsp_expected;
  1373. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1374. + rtcp_exp->saved_addr = saddr;
  1375. +#else
  1376. + rtcp_exp->saved_ip = saddr;
  1377. +#endif
  1378. + rtcp_exp->saved_proto.udp.port = htons(prtspexp->hiport);
  1379. + rtcp_exp->dir = !dir;
  1380. + }
  1381. + get_skb_tcpdata(skb, &ptcp, &tcplen);
  1382. + hdrslen -= (oldtcplen-tcplen);
  1383. + off -= (oldtcplen-tcplen);
  1384. + lineoff -= (oldtcplen-tcplen);
  1385. + linelen -= (oldtcplen-tcplen);
  1386. + pr_debug("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
  1387. + }
  1388. + }
  1389. +
  1390. + return NF_ACCEPT;
  1391. +}
  1392. +
  1393. +static unsigned int
  1394. +nf_nat_rtsp(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
  1395. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1396. + unsigned int protoff,
  1397. +#endif
  1398. + unsigned int matchoff, unsigned int matchlen,
  1399. + struct ip_ct_rtsp_expect* prtspexp,
  1400. + struct nf_conntrack_expect* rtp_exp,
  1401. + struct nf_conntrack_expect* rtcp_exp)
  1402. +{
  1403. + int dir = CTINFO2DIR(ctinfo);
  1404. + int rc = NF_ACCEPT;
  1405. +
  1406. + switch (dir) {
  1407. + case IP_CT_DIR_ORIGINAL:
  1408. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1409. + rc = help_out(skb, ctinfo, protoff, matchoff, matchlen, prtspexp,
  1410. + rtp_exp, rtcp_exp);
  1411. +#else
  1412. + rc = help_out(skb, ctinfo, matchoff, matchlen, prtspexp,
  1413. + rtp_exp, rtcp_exp);
  1414. +#endif
  1415. + break;
  1416. + case IP_CT_DIR_REPLY:
  1417. + pr_debug("unmangle ! %u\n", ctinfo);
  1418. + /* XXX: unmangle */
  1419. + rc = NF_ACCEPT;
  1420. + break;
  1421. + }
  1422. + //UNLOCK_BH(&ip_rtsp_lock);
  1423. +
  1424. + return rc;
  1425. +}
  1426. +
  1427. +static void nf_nat_rtsp_expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
  1428. +{
  1429. +#if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0) || LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1430. + struct nf_nat_range range;
  1431. +#else
  1432. + struct nf_nat_ipv4_range range;
  1433. +#endif
  1434. +
  1435. + /* This must be a fresh one. */
  1436. + BUG_ON(ct->status & IPS_NAT_DONE_MASK);
  1437. +
  1438. + /* For DST manip, map port here to where it's expected. */
  1439. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1440. + range.min_proto = range.max_proto = exp->saved_proto;
  1441. + range.min_addr = range.max_addr = exp->saved_addr;
  1442. +#else
  1443. + range.min = range.max = exp->saved_proto;
  1444. + range.min_ip = range.max_ip = exp->saved_ip;
  1445. +#endif
  1446. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0)
  1447. + range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
  1448. + nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
  1449. +#else
  1450. + range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED);
  1451. + nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
  1452. +#endif
  1453. +
  1454. + /* Change src to where master sends to, but only if the connection
  1455. + * actually came from the same source. */
  1456. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  1457. + if (nf_inet_addr_cmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3,
  1458. + &ct->master->tuplehash[exp->dir].tuple.src.u3)) {
  1459. + range.min_addr = range.max_addr
  1460. + = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
  1461. +#else
  1462. + if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip ==
  1463. + ct->master->tuplehash[exp->dir].tuple.src.u3.ip) {
  1464. + range.min_ip = range.max_ip
  1465. + = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
  1466. +#endif
  1467. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0)
  1468. + range.flags = NF_NAT_RANGE_MAP_IPS;
  1469. + nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
  1470. +#else
  1471. + range.flags = IP_NAT_RANGE_MAP_IPS;
  1472. + nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
  1473. +#endif
  1474. + }
  1475. +}
  1476. +
  1477. +
  1478. +static void __exit fini(void)
  1479. +{
  1480. + rcu_assign_pointer(nf_nat_rtsp_hook, NULL);
  1481. + synchronize_net();
  1482. +}
  1483. +
  1484. +static int __init init(void)
  1485. +{
  1486. + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
  1487. +
  1488. + BUG_ON(nf_nat_rtsp_hook);
  1489. + rcu_assign_pointer(nf_nat_rtsp_hook, nf_nat_rtsp);
  1490. +
  1491. + if (stunaddr != NULL)
  1492. + extip = in_aton(stunaddr);
  1493. +
  1494. + if (destaction != NULL) {
  1495. + if (strcmp(destaction, "auto") == 0)
  1496. + dstact = DSTACT_AUTO;
  1497. +
  1498. + if (strcmp(destaction, "strip") == 0)
  1499. + dstact = DSTACT_STRIP;
  1500. +
  1501. + if (strcmp(destaction, "none") == 0)
  1502. + dstact = DSTACT_NONE;
  1503. + }
  1504. +
  1505. + return 0;
  1506. +}
  1507. +
  1508. +module_init(init);
  1509. +module_exit(fini);
  1510. --- a/extensions/Kbuild
  1511. +++ b/extensions/Kbuild
  1512. @@ -28,6 +28,7 @@ obj-${build_lscan} += xt_lscan.o
  1513. obj-${build_pknock} += pknock/
  1514. obj-${build_psd} += xt_psd.o
  1515. obj-${build_quota2} += xt_quota2.o
  1516. +obj-${build_rtsp} += rtsp/
  1517. -include ${M}/*.Kbuild
  1518. -include ${M}/Kbuild.*
  1519. --- a/mconfig
  1520. +++ b/mconfig
  1521. @@ -22,3 +22,4 @@ build_lscan=m
  1522. build_pknock=m
  1523. build_psd=m
  1524. build_quota2=m
  1525. +build_rtsp=m