nf_nat_sip.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /* SIP extension for NAT alteration.
  2. *
  3. * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
  4. * based on RR's ip_nat_ftp.c and other modules.
  5. * (C) 2007 United Security Providers
  6. * (C) 2007, 2008, 2011, 2012 Patrick McHardy <kaber@trash.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/inet.h>
  15. #include <linux/udp.h>
  16. #include <linux/tcp.h>
  17. #include <net/netfilter/nf_nat.h>
  18. #include <net/netfilter/nf_nat_helper.h>
  19. #include <net/netfilter/nf_conntrack_helper.h>
  20. #include <net/netfilter/nf_conntrack_expect.h>
  21. #include <net/netfilter/nf_conntrack_seqadj.h>
  22. #include <linux/netfilter/nf_conntrack_sip.h>
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
  25. MODULE_DESCRIPTION("SIP NAT helper");
  26. MODULE_ALIAS("ip_nat_sip");
  27. static unsigned int mangle_packet(struct sk_buff *skb, unsigned int protoff,
  28. unsigned int dataoff,
  29. const char **dptr, unsigned int *datalen,
  30. unsigned int matchoff, unsigned int matchlen,
  31. const char *buffer, unsigned int buflen)
  32. {
  33. enum ip_conntrack_info ctinfo;
  34. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  35. struct tcphdr *th;
  36. unsigned int baseoff;
  37. if (nf_ct_protonum(ct) == IPPROTO_TCP) {
  38. th = (struct tcphdr *)(skb->data + protoff);
  39. baseoff = protoff + th->doff * 4;
  40. matchoff += dataoff - baseoff;
  41. if (!__nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
  42. protoff, matchoff, matchlen,
  43. buffer, buflen, false))
  44. return 0;
  45. } else {
  46. baseoff = protoff + sizeof(struct udphdr);
  47. matchoff += dataoff - baseoff;
  48. if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo,
  49. protoff, matchoff, matchlen,
  50. buffer, buflen))
  51. return 0;
  52. }
  53. /* Reload data pointer and adjust datalen value */
  54. *dptr = skb->data + dataoff;
  55. *datalen += buflen - matchlen;
  56. return 1;
  57. }
  58. static int sip_sprintf_addr(const struct nf_conn *ct, char *buffer,
  59. const union nf_inet_addr *addr, bool delim)
  60. {
  61. if (nf_ct_l3num(ct) == NFPROTO_IPV4)
  62. return sprintf(buffer, "%pI4", &addr->ip);
  63. else {
  64. if (delim)
  65. return sprintf(buffer, "[%pI6c]", &addr->ip6);
  66. else
  67. return sprintf(buffer, "%pI6c", &addr->ip6);
  68. }
  69. }
  70. static int sip_sprintf_addr_port(const struct nf_conn *ct, char *buffer,
  71. const union nf_inet_addr *addr, u16 port)
  72. {
  73. if (nf_ct_l3num(ct) == NFPROTO_IPV4)
  74. return sprintf(buffer, "%pI4:%u", &addr->ip, port);
  75. else
  76. return sprintf(buffer, "[%pI6c]:%u", &addr->ip6, port);
  77. }
  78. static int map_addr(struct sk_buff *skb, unsigned int protoff,
  79. unsigned int dataoff,
  80. const char **dptr, unsigned int *datalen,
  81. unsigned int matchoff, unsigned int matchlen,
  82. union nf_inet_addr *addr, __be16 port)
  83. {
  84. enum ip_conntrack_info ctinfo;
  85. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  86. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  87. struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
  88. char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
  89. unsigned int buflen;
  90. union nf_inet_addr newaddr;
  91. __be16 newport;
  92. if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, addr) &&
  93. ct->tuplehash[dir].tuple.src.u.udp.port == port) {
  94. newaddr = ct->tuplehash[!dir].tuple.dst.u3;
  95. newport = ct->tuplehash[!dir].tuple.dst.u.udp.port;
  96. } else if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, addr) &&
  97. ct->tuplehash[dir].tuple.dst.u.udp.port == port) {
  98. newaddr = ct->tuplehash[!dir].tuple.src.u3;
  99. newport = ct_sip_info->forced_dport ? :
  100. ct->tuplehash[!dir].tuple.src.u.udp.port;
  101. } else
  102. return 1;
  103. if (nf_inet_addr_cmp(&newaddr, addr) && newport == port)
  104. return 1;
  105. buflen = sip_sprintf_addr_port(ct, buffer, &newaddr, ntohs(newport));
  106. return mangle_packet(skb, protoff, dataoff, dptr, datalen,
  107. matchoff, matchlen, buffer, buflen);
  108. }
  109. static int map_sip_addr(struct sk_buff *skb, unsigned int protoff,
  110. unsigned int dataoff,
  111. const char **dptr, unsigned int *datalen,
  112. enum sip_header_types type)
  113. {
  114. enum ip_conntrack_info ctinfo;
  115. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  116. unsigned int matchlen, matchoff;
  117. union nf_inet_addr addr;
  118. __be16 port;
  119. if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, type, NULL,
  120. &matchoff, &matchlen, &addr, &port) <= 0)
  121. return 1;
  122. return map_addr(skb, protoff, dataoff, dptr, datalen,
  123. matchoff, matchlen, &addr, port);
  124. }
  125. static unsigned int nf_nat_sip(struct sk_buff *skb, unsigned int protoff,
  126. unsigned int dataoff,
  127. const char **dptr, unsigned int *datalen)
  128. {
  129. enum ip_conntrack_info ctinfo;
  130. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  131. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  132. struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
  133. unsigned int coff, matchoff, matchlen;
  134. enum sip_header_types hdr;
  135. union nf_inet_addr addr;
  136. __be16 port;
  137. int request, in_header;
  138. /* Basic rules: requests and responses. */
  139. if (strncasecmp(*dptr, "SIP/2.0", strlen("SIP/2.0")) != 0) {
  140. if (ct_sip_parse_request(ct, *dptr, *datalen,
  141. &matchoff, &matchlen,
  142. &addr, &port) > 0 &&
  143. !map_addr(skb, protoff, dataoff, dptr, datalen,
  144. matchoff, matchlen, &addr, port)) {
  145. nf_ct_helper_log(skb, ct, "cannot mangle SIP message");
  146. return NF_DROP;
  147. }
  148. request = 1;
  149. } else
  150. request = 0;
  151. if (nf_ct_protonum(ct) == IPPROTO_TCP)
  152. hdr = SIP_HDR_VIA_TCP;
  153. else
  154. hdr = SIP_HDR_VIA_UDP;
  155. /* Translate topmost Via header and parameters */
  156. if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
  157. hdr, NULL, &matchoff, &matchlen,
  158. &addr, &port) > 0) {
  159. unsigned int olen, matchend, poff, plen, buflen, n;
  160. char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
  161. /* We're only interested in headers related to this
  162. * connection */
  163. if (request) {
  164. if (!nf_inet_addr_cmp(&addr,
  165. &ct->tuplehash[dir].tuple.src.u3) ||
  166. port != ct->tuplehash[dir].tuple.src.u.udp.port)
  167. goto next;
  168. } else {
  169. if (!nf_inet_addr_cmp(&addr,
  170. &ct->tuplehash[dir].tuple.dst.u3) ||
  171. port != ct->tuplehash[dir].tuple.dst.u.udp.port)
  172. goto next;
  173. }
  174. olen = *datalen;
  175. if (!map_addr(skb, protoff, dataoff, dptr, datalen,
  176. matchoff, matchlen, &addr, port)) {
  177. nf_ct_helper_log(skb, ct, "cannot mangle Via header");
  178. return NF_DROP;
  179. }
  180. matchend = matchoff + matchlen + *datalen - olen;
  181. /* The maddr= parameter (RFC 2361) specifies where to send
  182. * the reply. */
  183. if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen,
  184. "maddr=", &poff, &plen,
  185. &addr, true) > 0 &&
  186. nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3) &&
  187. !nf_inet_addr_cmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3)) {
  188. buflen = sip_sprintf_addr(ct, buffer,
  189. &ct->tuplehash[!dir].tuple.dst.u3,
  190. true);
  191. if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
  192. poff, plen, buffer, buflen)) {
  193. nf_ct_helper_log(skb, ct, "cannot mangle maddr");
  194. return NF_DROP;
  195. }
  196. }
  197. /* The received= parameter (RFC 2361) contains the address
  198. * from which the server received the request. */
  199. if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen,
  200. "received=", &poff, &plen,
  201. &addr, false) > 0 &&
  202. nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.dst.u3) &&
  203. !nf_inet_addr_cmp(&addr, &ct->tuplehash[!dir].tuple.src.u3)) {
  204. buflen = sip_sprintf_addr(ct, buffer,
  205. &ct->tuplehash[!dir].tuple.src.u3,
  206. false);
  207. if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
  208. poff, plen, buffer, buflen)) {
  209. nf_ct_helper_log(skb, ct, "cannot mangle received");
  210. return NF_DROP;
  211. }
  212. }
  213. /* The rport= parameter (RFC 3581) contains the port number
  214. * from which the server received the request. */
  215. if (ct_sip_parse_numerical_param(ct, *dptr, matchend, *datalen,
  216. "rport=", &poff, &plen,
  217. &n) > 0 &&
  218. htons(n) == ct->tuplehash[dir].tuple.dst.u.udp.port &&
  219. htons(n) != ct->tuplehash[!dir].tuple.src.u.udp.port) {
  220. __be16 p = ct->tuplehash[!dir].tuple.src.u.udp.port;
  221. buflen = sprintf(buffer, "%u", ntohs(p));
  222. if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
  223. poff, plen, buffer, buflen)) {
  224. nf_ct_helper_log(skb, ct, "cannot mangle rport");
  225. return NF_DROP;
  226. }
  227. }
  228. }
  229. next:
  230. /* Translate Contact headers */
  231. coff = 0;
  232. in_header = 0;
  233. while (ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
  234. SIP_HDR_CONTACT, &in_header,
  235. &matchoff, &matchlen,
  236. &addr, &port) > 0) {
  237. if (!map_addr(skb, protoff, dataoff, dptr, datalen,
  238. matchoff, matchlen,
  239. &addr, port)) {
  240. nf_ct_helper_log(skb, ct, "cannot mangle contact");
  241. return NF_DROP;
  242. }
  243. }
  244. if (!map_sip_addr(skb, protoff, dataoff, dptr, datalen, SIP_HDR_FROM) ||
  245. !map_sip_addr(skb, protoff, dataoff, dptr, datalen, SIP_HDR_TO)) {
  246. nf_ct_helper_log(skb, ct, "cannot mangle SIP from/to");
  247. return NF_DROP;
  248. }
  249. /* Mangle destination port for Cisco phones, then fix up checksums */
  250. if (dir == IP_CT_DIR_REPLY && ct_sip_info->forced_dport) {
  251. struct udphdr *uh;
  252. if (!skb_make_writable(skb, skb->len)) {
  253. nf_ct_helper_log(skb, ct, "cannot mangle packet");
  254. return NF_DROP;
  255. }
  256. uh = (void *)skb->data + protoff;
  257. uh->dest = ct_sip_info->forced_dport;
  258. if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, protoff,
  259. 0, 0, NULL, 0)) {
  260. nf_ct_helper_log(skb, ct, "cannot mangle packet");
  261. return NF_DROP;
  262. }
  263. }
  264. return NF_ACCEPT;
  265. }
  266. static void nf_nat_sip_seq_adjust(struct sk_buff *skb, unsigned int protoff,
  267. s16 off)
  268. {
  269. enum ip_conntrack_info ctinfo;
  270. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  271. const struct tcphdr *th;
  272. if (nf_ct_protonum(ct) != IPPROTO_TCP || off == 0)
  273. return;
  274. th = (struct tcphdr *)(skb->data + protoff);
  275. nf_ct_seqadj_set(ct, ctinfo, th->seq, off);
  276. }
  277. /* Handles expected signalling connections and media streams */
  278. static void nf_nat_sip_expected(struct nf_conn *ct,
  279. struct nf_conntrack_expect *exp)
  280. {
  281. struct nf_nat_range range;
  282. /* This must be a fresh one. */
  283. BUG_ON(ct->status & IPS_NAT_DONE_MASK);
  284. /* For DST manip, map port here to where it's expected. */
  285. range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
  286. range.min_proto = range.max_proto = exp->saved_proto;
  287. range.min_addr = range.max_addr = exp->saved_addr;
  288. nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
  289. /* Change src to where master sends to, but only if the connection
  290. * actually came from the same source. */
  291. if (nf_inet_addr_cmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3,
  292. &ct->master->tuplehash[exp->dir].tuple.src.u3)) {
  293. range.flags = NF_NAT_RANGE_MAP_IPS;
  294. range.min_addr = range.max_addr
  295. = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
  296. nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
  297. }
  298. }
  299. static unsigned int nf_nat_sip_expect(struct sk_buff *skb, unsigned int protoff,
  300. unsigned int dataoff,
  301. const char **dptr, unsigned int *datalen,
  302. struct nf_conntrack_expect *exp,
  303. unsigned int matchoff,
  304. unsigned int matchlen)
  305. {
  306. enum ip_conntrack_info ctinfo;
  307. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  308. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  309. struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
  310. union nf_inet_addr newaddr;
  311. u_int16_t port;
  312. __be16 srcport;
  313. char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
  314. unsigned int buflen;
  315. /* Connection will come from reply */
  316. if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
  317. &ct->tuplehash[!dir].tuple.dst.u3))
  318. newaddr = exp->tuple.dst.u3;
  319. else
  320. newaddr = ct->tuplehash[!dir].tuple.dst.u3;
  321. /* If the signalling port matches the connection's source port in the
  322. * original direction, try to use the destination port in the opposite
  323. * direction. */
  324. srcport = ct_sip_info->forced_dport ? :
  325. ct->tuplehash[dir].tuple.src.u.udp.port;
  326. if (exp->tuple.dst.u.udp.port == srcport)
  327. port = ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port);
  328. else
  329. port = ntohs(exp->tuple.dst.u.udp.port);
  330. exp->saved_addr = exp->tuple.dst.u3;
  331. exp->tuple.dst.u3 = newaddr;
  332. exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port;
  333. exp->dir = !dir;
  334. exp->expectfn = nf_nat_sip_expected;
  335. for (; port != 0; port++) {
  336. int ret;
  337. exp->tuple.dst.u.udp.port = htons(port);
  338. ret = nf_ct_expect_related(exp);
  339. if (ret == 0)
  340. break;
  341. else if (ret != -EBUSY) {
  342. port = 0;
  343. break;
  344. }
  345. }
  346. if (port == 0) {
  347. nf_ct_helper_log(skb, ct, "all ports in use for SIP");
  348. return NF_DROP;
  349. }
  350. if (!nf_inet_addr_cmp(&exp->tuple.dst.u3, &exp->saved_addr) ||
  351. exp->tuple.dst.u.udp.port != exp->saved_proto.udp.port) {
  352. buflen = sip_sprintf_addr_port(ct, buffer, &newaddr, port);
  353. if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
  354. matchoff, matchlen, buffer, buflen)) {
  355. nf_ct_helper_log(skb, ct, "cannot mangle packet");
  356. goto err;
  357. }
  358. }
  359. return NF_ACCEPT;
  360. err:
  361. nf_ct_unexpect_related(exp);
  362. return NF_DROP;
  363. }
  364. static int mangle_content_len(struct sk_buff *skb, unsigned int protoff,
  365. unsigned int dataoff,
  366. const char **dptr, unsigned int *datalen)
  367. {
  368. enum ip_conntrack_info ctinfo;
  369. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  370. unsigned int matchoff, matchlen;
  371. char buffer[sizeof("65536")];
  372. int buflen, c_len;
  373. /* Get actual SDP length */
  374. if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
  375. SDP_HDR_VERSION, SDP_HDR_UNSPEC,
  376. &matchoff, &matchlen) <= 0)
  377. return 0;
  378. c_len = *datalen - matchoff + strlen("v=");
  379. /* Now, update SDP length */
  380. if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH,
  381. &matchoff, &matchlen) <= 0)
  382. return 0;
  383. buflen = sprintf(buffer, "%u", c_len);
  384. return mangle_packet(skb, protoff, dataoff, dptr, datalen,
  385. matchoff, matchlen, buffer, buflen);
  386. }
  387. static int mangle_sdp_packet(struct sk_buff *skb, unsigned int protoff,
  388. unsigned int dataoff,
  389. const char **dptr, unsigned int *datalen,
  390. unsigned int sdpoff,
  391. enum sdp_header_types type,
  392. enum sdp_header_types term,
  393. char *buffer, int buflen)
  394. {
  395. enum ip_conntrack_info ctinfo;
  396. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  397. unsigned int matchlen, matchoff;
  398. if (ct_sip_get_sdp_header(ct, *dptr, sdpoff, *datalen, type, term,
  399. &matchoff, &matchlen) <= 0)
  400. return -ENOENT;
  401. return mangle_packet(skb, protoff, dataoff, dptr, datalen,
  402. matchoff, matchlen, buffer, buflen) ? 0 : -EINVAL;
  403. }
  404. static unsigned int nf_nat_sdp_addr(struct sk_buff *skb, unsigned int protoff,
  405. unsigned int dataoff,
  406. const char **dptr, unsigned int *datalen,
  407. unsigned int sdpoff,
  408. enum sdp_header_types type,
  409. enum sdp_header_types term,
  410. const union nf_inet_addr *addr)
  411. {
  412. enum ip_conntrack_info ctinfo;
  413. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  414. char buffer[INET6_ADDRSTRLEN];
  415. unsigned int buflen;
  416. buflen = sip_sprintf_addr(ct, buffer, addr, false);
  417. if (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen,
  418. sdpoff, type, term, buffer, buflen))
  419. return 0;
  420. return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
  421. }
  422. static unsigned int nf_nat_sdp_port(struct sk_buff *skb, unsigned int protoff,
  423. unsigned int dataoff,
  424. const char **dptr, unsigned int *datalen,
  425. unsigned int matchoff,
  426. unsigned int matchlen,
  427. u_int16_t port)
  428. {
  429. char buffer[sizeof("nnnnn")];
  430. unsigned int buflen;
  431. buflen = sprintf(buffer, "%u", port);
  432. if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
  433. matchoff, matchlen, buffer, buflen))
  434. return 0;
  435. return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
  436. }
  437. static unsigned int nf_nat_sdp_session(struct sk_buff *skb, unsigned int protoff,
  438. unsigned int dataoff,
  439. const char **dptr, unsigned int *datalen,
  440. unsigned int sdpoff,
  441. const union nf_inet_addr *addr)
  442. {
  443. enum ip_conntrack_info ctinfo;
  444. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  445. char buffer[INET6_ADDRSTRLEN];
  446. unsigned int buflen;
  447. /* Mangle session description owner and contact addresses */
  448. buflen = sip_sprintf_addr(ct, buffer, addr, false);
  449. if (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, sdpoff,
  450. SDP_HDR_OWNER, SDP_HDR_MEDIA, buffer, buflen))
  451. return 0;
  452. switch (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, sdpoff,
  453. SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
  454. buffer, buflen)) {
  455. case 0:
  456. /*
  457. * RFC 2327:
  458. *
  459. * Session description
  460. *
  461. * c=* (connection information - not required if included in all media)
  462. */
  463. case -ENOENT:
  464. break;
  465. default:
  466. return 0;
  467. }
  468. return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
  469. }
  470. /* So, this packet has hit the connection tracking matching code.
  471. Mangle it, and change the expectation to match the new version. */
  472. static unsigned int nf_nat_sdp_media(struct sk_buff *skb, unsigned int protoff,
  473. unsigned int dataoff,
  474. const char **dptr, unsigned int *datalen,
  475. struct nf_conntrack_expect *rtp_exp,
  476. struct nf_conntrack_expect *rtcp_exp,
  477. unsigned int mediaoff,
  478. unsigned int medialen,
  479. union nf_inet_addr *rtp_addr)
  480. {
  481. enum ip_conntrack_info ctinfo;
  482. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  483. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  484. u_int16_t port;
  485. /* Connection will come from reply */
  486. if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
  487. &ct->tuplehash[!dir].tuple.dst.u3))
  488. *rtp_addr = rtp_exp->tuple.dst.u3;
  489. else
  490. *rtp_addr = ct->tuplehash[!dir].tuple.dst.u3;
  491. rtp_exp->saved_addr = rtp_exp->tuple.dst.u3;
  492. rtp_exp->tuple.dst.u3 = *rtp_addr;
  493. rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port;
  494. rtp_exp->dir = !dir;
  495. rtp_exp->expectfn = nf_nat_sip_expected;
  496. rtcp_exp->saved_addr = rtcp_exp->tuple.dst.u3;
  497. rtcp_exp->tuple.dst.u3 = *rtp_addr;
  498. rtcp_exp->saved_proto.udp.port = rtcp_exp->tuple.dst.u.udp.port;
  499. rtcp_exp->dir = !dir;
  500. rtcp_exp->expectfn = nf_nat_sip_expected;
  501. /* Try to get same pair of ports: if not, try to change them. */
  502. for (port = ntohs(rtp_exp->tuple.dst.u.udp.port);
  503. port != 0; port += 2) {
  504. int ret;
  505. rtp_exp->tuple.dst.u.udp.port = htons(port);
  506. ret = nf_ct_expect_related(rtp_exp);
  507. if (ret == -EBUSY)
  508. continue;
  509. else if (ret < 0) {
  510. port = 0;
  511. break;
  512. }
  513. rtcp_exp->tuple.dst.u.udp.port = htons(port + 1);
  514. ret = nf_ct_expect_related(rtcp_exp);
  515. if (ret == 0)
  516. break;
  517. else if (ret == -EBUSY) {
  518. nf_ct_unexpect_related(rtp_exp);
  519. continue;
  520. } else if (ret < 0) {
  521. nf_ct_unexpect_related(rtp_exp);
  522. port = 0;
  523. break;
  524. }
  525. }
  526. if (port == 0) {
  527. nf_ct_helper_log(skb, ct, "all ports in use for SDP media");
  528. goto err1;
  529. }
  530. /* Update media port. */
  531. if (rtp_exp->tuple.dst.u.udp.port != rtp_exp->saved_proto.udp.port &&
  532. !nf_nat_sdp_port(skb, protoff, dataoff, dptr, datalen,
  533. mediaoff, medialen, port)) {
  534. nf_ct_helper_log(skb, ct, "cannot mangle SDP message");
  535. goto err2;
  536. }
  537. return NF_ACCEPT;
  538. err2:
  539. nf_ct_unexpect_related(rtp_exp);
  540. nf_ct_unexpect_related(rtcp_exp);
  541. err1:
  542. return NF_DROP;
  543. }
  544. static struct nf_ct_helper_expectfn sip_nat = {
  545. .name = "sip",
  546. .expectfn = nf_nat_sip_expected,
  547. };
  548. static void __exit nf_nat_sip_fini(void)
  549. {
  550. RCU_INIT_POINTER(nf_nat_sip_hooks, NULL);
  551. nf_ct_helper_expectfn_unregister(&sip_nat);
  552. synchronize_rcu();
  553. }
  554. static const struct nf_nat_sip_hooks sip_hooks = {
  555. .msg = nf_nat_sip,
  556. .seq_adjust = nf_nat_sip_seq_adjust,
  557. .expect = nf_nat_sip_expect,
  558. .sdp_addr = nf_nat_sdp_addr,
  559. .sdp_port = nf_nat_sdp_port,
  560. .sdp_session = nf_nat_sdp_session,
  561. .sdp_media = nf_nat_sdp_media,
  562. };
  563. static int __init nf_nat_sip_init(void)
  564. {
  565. BUG_ON(nf_nat_sip_hooks != NULL);
  566. RCU_INIT_POINTER(nf_nat_sip_hooks, &sip_hooks);
  567. nf_ct_helper_expectfn_register(&sip_nat);
  568. return 0;
  569. }
  570. module_init(nf_nat_sip_init);
  571. module_exit(nf_nat_sip_fini);