blob: ea4a23813d26e66bf3c5e1513dbdb247b53a06dd [file] [log] [blame]
Patrick McHardy48f8ac22010-02-11 12:29:38 +01001/* SIP extension for NAT alteration.
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002 *
3 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
4 * based on RR's ip_nat_ftp.c and other modules.
Patrick McHardyf49e1aa2008-03-25 20:27:05 -07005 * (C) 2007 United Security Providers
6 * (C) 2007, 2008 Patrick McHardy <kaber@trash.net>
Patrick McHardy9fafcd72006-12-02 22:09:57 -08007 *
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
13#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/ip.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030016#include <net/ip.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080017#include <linux/udp.h>
Patrick McHardy48f8ac22010-02-11 12:29:38 +010018#include <linux/tcp.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080019
20#include <net/netfilter/nf_nat.h>
21#include <net/netfilter/nf_nat_helper.h>
22#include <net/netfilter/nf_nat_rule.h>
23#include <net/netfilter/nf_conntrack_helper.h>
24#include <net/netfilter/nf_conntrack_expect.h>
25#include <linux/netfilter/nf_conntrack_sip.h>
26
27MODULE_LICENSE("GPL");
28MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
29MODULE_DESCRIPTION("SIP NAT helper");
30MODULE_ALIAS("ip_nat_sip");
31
Patrick McHardy9fafcd72006-12-02 22:09:57 -080032
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +010033static unsigned int mangle_packet(struct sk_buff *skb, unsigned int dataoff,
Patrick McHardy2a6cfb22008-03-25 20:16:54 -070034 const char **dptr, unsigned int *datalen,
35 unsigned int matchoff, unsigned int matchlen,
36 const char *buffer, unsigned int buflen)
37{
38 enum ip_conntrack_info ctinfo;
39 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy48f8ac22010-02-11 12:29:38 +010040 struct tcphdr *th;
41 unsigned int baseoff;
Patrick McHardy2a6cfb22008-03-25 20:16:54 -070042
Patrick McHardy48f8ac22010-02-11 12:29:38 +010043 if (nf_ct_protonum(ct) == IPPROTO_TCP) {
44 th = (struct tcphdr *)(skb->data + ip_hdrlen(skb));
45 baseoff = ip_hdrlen(skb) + th->doff * 4;
46 matchoff += dataoff - baseoff;
47
48 if (!__nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
49 matchoff, matchlen,
50 buffer, buflen, false))
51 return 0;
52 } else {
53 baseoff = ip_hdrlen(skb) + sizeof(struct udphdr);
54 matchoff += dataoff - baseoff;
55
56 if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo,
57 matchoff, matchlen,
58 buffer, buflen))
59 return 0;
60 }
Patrick McHardy2a6cfb22008-03-25 20:16:54 -070061
62 /* Reload data pointer and adjust datalen value */
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +010063 *dptr = skb->data + dataoff;
Patrick McHardy2a6cfb22008-03-25 20:16:54 -070064 *datalen += buflen - matchlen;
65 return 1;
66}
67
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +010068static int map_addr(struct sk_buff *skb, unsigned int dataoff,
Patrick McHardyac367742008-03-25 20:18:40 -070069 const char **dptr, unsigned int *datalen,
70 unsigned int matchoff, unsigned int matchlen,
Patrick McHardy624f8b72008-03-25 20:19:30 -070071 union nf_inet_addr *addr, __be16 port)
Patrick McHardy9fafcd72006-12-02 22:09:57 -080072{
Patrick McHardy212440a2008-03-25 20:17:13 -070073 enum ip_conntrack_info ctinfo;
Patrick McHardy624f8b72008-03-25 20:19:30 -070074 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy9fafcd72006-12-02 22:09:57 -080075 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy624f8b72008-03-25 20:19:30 -070076 char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
77 unsigned int buflen;
78 __be32 newaddr;
79 __be16 newport;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080080
Patrick McHardy624f8b72008-03-25 20:19:30 -070081 if (ct->tuplehash[dir].tuple.src.u3.ip == addr->ip &&
82 ct->tuplehash[dir].tuple.src.u.udp.port == port) {
83 newaddr = ct->tuplehash[!dir].tuple.dst.u3.ip;
84 newport = ct->tuplehash[!dir].tuple.dst.u.udp.port;
85 } else if (ct->tuplehash[dir].tuple.dst.u3.ip == addr->ip &&
86 ct->tuplehash[dir].tuple.dst.u.udp.port == port) {
87 newaddr = ct->tuplehash[!dir].tuple.src.u3.ip;
88 newport = ct->tuplehash[!dir].tuple.src.u.udp.port;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080089 } else
90 return 1;
91
Patrick McHardy624f8b72008-03-25 20:19:30 -070092 if (newaddr == addr->ip && newport == port)
93 return 1;
94
Harvey Harrisoncffee382008-10-31 00:53:08 -070095 buflen = sprintf(buffer, "%pI4:%u", &newaddr, ntohs(newport));
Patrick McHardy624f8b72008-03-25 20:19:30 -070096
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +010097 return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen,
Patrick McHardy624f8b72008-03-25 20:19:30 -070098 buffer, buflen);
Patrick McHardy9fafcd72006-12-02 22:09:57 -080099}
100
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100101static int map_sip_addr(struct sk_buff *skb, unsigned int dataoff,
Patrick McHardyac367742008-03-25 20:18:40 -0700102 const char **dptr, unsigned int *datalen,
Patrick McHardy624f8b72008-03-25 20:19:30 -0700103 enum sip_header_types type)
Patrick McHardyac367742008-03-25 20:18:40 -0700104{
105 enum ip_conntrack_info ctinfo;
106 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
107 unsigned int matchlen, matchoff;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700108 union nf_inet_addr addr;
109 __be16 port;
Patrick McHardyac367742008-03-25 20:18:40 -0700110
Patrick McHardy624f8b72008-03-25 20:19:30 -0700111 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, type, NULL,
112 &matchoff, &matchlen, &addr, &port) <= 0)
Patrick McHardyac367742008-03-25 20:18:40 -0700113 return 1;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100114 return map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen,
115 &addr, port);
Patrick McHardyac367742008-03-25 20:18:40 -0700116}
117
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100118static unsigned int ip_nat_sip(struct sk_buff *skb, unsigned int dataoff,
Patrick McHardy2a6cfb22008-03-25 20:16:54 -0700119 const char **dptr, unsigned int *datalen)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800120{
Patrick McHardy212440a2008-03-25 20:17:13 -0700121 enum ip_conntrack_info ctinfo;
122 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy720ac702008-03-25 20:24:41 -0700123 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100124 unsigned int coff, matchoff, matchlen;
Patrick McHardy48f8ac22010-02-11 12:29:38 +0100125 enum sip_header_types hdr;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700126 union nf_inet_addr addr;
127 __be16 port;
Patrick McHardyc978cd32008-03-25 20:24:57 -0700128 int request, in_header;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800129
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800130 /* Basic rules: requests and responses. */
Patrick McHardy779382e2008-03-25 20:17:36 -0700131 if (strnicmp(*dptr, "SIP/2.0", strlen("SIP/2.0")) != 0) {
Patrick McHardyac367742008-03-25 20:18:40 -0700132 if (ct_sip_parse_request(ct, *dptr, *datalen,
Patrick McHardy624f8b72008-03-25 20:19:30 -0700133 &matchoff, &matchlen,
134 &addr, &port) > 0 &&
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100135 !map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen,
Patrick McHardy624f8b72008-03-25 20:19:30 -0700136 &addr, port))
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800137 return NF_DROP;
Patrick McHardy720ac702008-03-25 20:24:41 -0700138 request = 1;
139 } else
140 request = 0;
141
Patrick McHardy48f8ac22010-02-11 12:29:38 +0100142 if (nf_ct_protonum(ct) == IPPROTO_TCP)
143 hdr = SIP_HDR_VIA_TCP;
144 else
145 hdr = SIP_HDR_VIA_UDP;
146
Patrick McHardy720ac702008-03-25 20:24:41 -0700147 /* Translate topmost Via header and parameters */
148 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
Patrick McHardy48f8ac22010-02-11 12:29:38 +0100149 hdr, NULL, &matchoff, &matchlen,
Patrick McHardy720ac702008-03-25 20:24:41 -0700150 &addr, &port) > 0) {
151 unsigned int matchend, poff, plen, buflen, n;
152 char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
153
154 /* We're only interested in headers related to this
155 * connection */
156 if (request) {
157 if (addr.ip != ct->tuplehash[dir].tuple.src.u3.ip ||
158 port != ct->tuplehash[dir].tuple.src.u.udp.port)
159 goto next;
160 } else {
161 if (addr.ip != ct->tuplehash[dir].tuple.dst.u3.ip ||
162 port != ct->tuplehash[dir].tuple.dst.u.udp.port)
163 goto next;
164 }
165
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100166 if (!map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen,
Patrick McHardy720ac702008-03-25 20:24:41 -0700167 &addr, port))
168 return NF_DROP;
169
170 matchend = matchoff + matchlen;
171
172 /* The maddr= parameter (RFC 2361) specifies where to send
173 * the reply. */
174 if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen,
175 "maddr=", &poff, &plen,
176 &addr) > 0 &&
177 addr.ip == ct->tuplehash[dir].tuple.src.u3.ip &&
178 addr.ip != ct->tuplehash[!dir].tuple.dst.u3.ip) {
Harvey Harrisoncffee382008-10-31 00:53:08 -0700179 buflen = sprintf(buffer, "%pI4",
180 &ct->tuplehash[!dir].tuple.dst.u3.ip);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100181 if (!mangle_packet(skb, dataoff, dptr, datalen,
182 poff, plen, buffer, buflen))
Patrick McHardy720ac702008-03-25 20:24:41 -0700183 return NF_DROP;
184 }
185
186 /* The received= parameter (RFC 2361) contains the address
187 * from which the server received the request. */
188 if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen,
189 "received=", &poff, &plen,
190 &addr) > 0 &&
191 addr.ip == ct->tuplehash[dir].tuple.dst.u3.ip &&
192 addr.ip != ct->tuplehash[!dir].tuple.src.u3.ip) {
Harvey Harrisoncffee382008-10-31 00:53:08 -0700193 buflen = sprintf(buffer, "%pI4",
194 &ct->tuplehash[!dir].tuple.src.u3.ip);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100195 if (!mangle_packet(skb, dataoff, dptr, datalen,
196 poff, plen, buffer, buflen))
Patrick McHardy720ac702008-03-25 20:24:41 -0700197 return NF_DROP;
198 }
199
200 /* The rport= parameter (RFC 3581) contains the port number
201 * from which the server received the request. */
202 if (ct_sip_parse_numerical_param(ct, *dptr, matchend, *datalen,
203 "rport=", &poff, &plen,
204 &n) > 0 &&
205 htons(n) == ct->tuplehash[dir].tuple.dst.u.udp.port &&
206 htons(n) != ct->tuplehash[!dir].tuple.src.u.udp.port) {
207 __be16 p = ct->tuplehash[!dir].tuple.src.u.udp.port;
208 buflen = sprintf(buffer, "%u", ntohs(p));
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100209 if (!mangle_packet(skb, dataoff, dptr, datalen,
210 poff, plen, buffer, buflen))
Patrick McHardy720ac702008-03-25 20:24:41 -0700211 return NF_DROP;
212 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800213 }
214
Patrick McHardy720ac702008-03-25 20:24:41 -0700215next:
Patrick McHardyc978cd32008-03-25 20:24:57 -0700216 /* Translate Contact headers */
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100217 coff = 0;
Patrick McHardyc978cd32008-03-25 20:24:57 -0700218 in_header = 0;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100219 while (ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
Patrick McHardyc978cd32008-03-25 20:24:57 -0700220 SIP_HDR_CONTACT, &in_header,
221 &matchoff, &matchlen,
222 &addr, &port) > 0) {
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100223 if (!map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen,
Patrick McHardyc978cd32008-03-25 20:24:57 -0700224 &addr, port))
225 return NF_DROP;
226 }
227
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100228 if (!map_sip_addr(skb, dataoff, dptr, datalen, SIP_HDR_FROM) ||
229 !map_sip_addr(skb, dataoff, dptr, datalen, SIP_HDR_TO))
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800230 return NF_DROP;
Patrick McHardy48f8ac22010-02-11 12:29:38 +0100231
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800232 return NF_ACCEPT;
233}
234
Patrick McHardy48f8ac22010-02-11 12:29:38 +0100235static void ip_nat_sip_seq_adjust(struct sk_buff *skb, s16 off)
236{
237 enum ip_conntrack_info ctinfo;
238 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
239 const struct tcphdr *th;
240
241 if (nf_ct_protonum(ct) != IPPROTO_TCP || off == 0)
242 return;
243
244 th = (struct tcphdr *)(skb->data + ip_hdrlen(skb));
245 nf_nat_set_seq_adjust(ct, ctinfo, th->seq, off);
246}
247
Patrick McHardy0f32a402008-03-25 20:25:13 -0700248/* Handles expected signalling connections and media streams */
249static void ip_nat_sip_expected(struct nf_conn *ct,
250 struct nf_conntrack_expect *exp)
251{
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100252 struct nf_nat_ipv4_range range;
Patrick McHardy0f32a402008-03-25 20:25:13 -0700253
254 /* This must be a fresh one. */
255 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
256
257 /* For DST manip, map port here to where it's expected. */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100258 range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
Patrick McHardy0f32a402008-03-25 20:25:13 -0700259 range.min = range.max = exp->saved_proto;
260 range.min_ip = range.max_ip = exp->saved_ip;
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100261 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
Patrick McHardy0f32a402008-03-25 20:25:13 -0700262
263 /* Change src to where master sends to, but only if the connection
264 * actually came from the same source. */
265 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip ==
266 ct->master->tuplehash[exp->dir].tuple.src.u3.ip) {
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100267 range.flags = NF_NAT_RANGE_MAP_IPS;
Patrick McHardy0f32a402008-03-25 20:25:13 -0700268 range.min_ip = range.max_ip
269 = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100270 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
Patrick McHardy0f32a402008-03-25 20:25:13 -0700271 }
272}
273
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100274static unsigned int ip_nat_sip_expect(struct sk_buff *skb, unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -0700275 const char **dptr, unsigned int *datalen,
276 struct nf_conntrack_expect *exp,
277 unsigned int matchoff,
278 unsigned int matchlen)
279{
280 enum ip_conntrack_info ctinfo;
281 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
282 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
283 __be32 newip;
284 u_int16_t port;
285 char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
Eric Dumazet95c96172012-04-15 05:58:06 +0000286 unsigned int buflen;
Patrick McHardy0f32a402008-03-25 20:25:13 -0700287
288 /* Connection will come from reply */
289 if (ct->tuplehash[dir].tuple.src.u3.ip == ct->tuplehash[!dir].tuple.dst.u3.ip)
290 newip = exp->tuple.dst.u3.ip;
291 else
292 newip = ct->tuplehash[!dir].tuple.dst.u3.ip;
293
294 /* If the signalling port matches the connection's source port in the
295 * original direction, try to use the destination port in the opposite
296 * direction. */
297 if (exp->tuple.dst.u.udp.port ==
298 ct->tuplehash[dir].tuple.src.u.udp.port)
299 port = ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port);
300 else
301 port = ntohs(exp->tuple.dst.u.udp.port);
302
303 exp->saved_ip = exp->tuple.dst.u3.ip;
304 exp->tuple.dst.u3.ip = newip;
305 exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port;
306 exp->dir = !dir;
307 exp->expectfn = ip_nat_sip_expected;
308
309 for (; port != 0; port++) {
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +0200310 int ret;
311
Patrick McHardy0f32a402008-03-25 20:25:13 -0700312 exp->tuple.dst.u.udp.port = htons(port);
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +0200313 ret = nf_ct_expect_related(exp);
314 if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -0700315 break;
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +0200316 else if (ret != -EBUSY) {
317 port = 0;
318 break;
319 }
Patrick McHardy0f32a402008-03-25 20:25:13 -0700320 }
321
322 if (port == 0)
323 return NF_DROP;
324
325 if (exp->tuple.dst.u3.ip != exp->saved_ip ||
326 exp->tuple.dst.u.udp.port != exp->saved_proto.udp.port) {
Harvey Harrisoncffee382008-10-31 00:53:08 -0700327 buflen = sprintf(buffer, "%pI4:%u", &newip, port);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100328 if (!mangle_packet(skb, dataoff, dptr, datalen,
329 matchoff, matchlen, buffer, buflen))
Patrick McHardy0f32a402008-03-25 20:25:13 -0700330 goto err;
331 }
332 return NF_ACCEPT;
333
334err:
335 nf_ct_unexpect_related(exp);
336 return NF_DROP;
337}
338
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100339static int mangle_content_len(struct sk_buff *skb, unsigned int dataoff,
Patrick McHardy2a6cfb22008-03-25 20:16:54 -0700340 const char **dptr, unsigned int *datalen)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800341{
Patrick McHardy212440a2008-03-25 20:17:13 -0700342 enum ip_conntrack_info ctinfo;
343 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy2a6cfb22008-03-25 20:16:54 -0700344 unsigned int matchoff, matchlen;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800345 char buffer[sizeof("65536")];
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700346 int buflen, c_len;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800347
Joe Perchese00ccd42007-12-20 14:05:03 -0800348 /* Get actual SDP length */
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700349 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
350 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
351 &matchoff, &matchlen) <= 0)
352 return 0;
353 c_len = *datalen - matchoff + strlen("v=");
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800354
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700355 /* Now, update SDP length */
Patrick McHardyea45f122008-03-25 20:18:57 -0700356 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH,
357 &matchoff, &matchlen) <= 0)
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700358 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800359
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700360 buflen = sprintf(buffer, "%u", c_len);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100361 return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen,
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700362 buffer, buflen);
363}
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800364
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100365static int mangle_sdp_packet(struct sk_buff *skb, unsigned int dataoff,
366 const char **dptr, unsigned int *datalen,
367 unsigned int sdpoff,
Herbert Xuc71529e2008-07-21 10:03:23 -0700368 enum sdp_header_types type,
369 enum sdp_header_types term,
370 char *buffer, int buflen)
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700371{
372 enum ip_conntrack_info ctinfo;
373 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
374 unsigned int matchlen, matchoff;
375
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100376 if (ct_sip_get_sdp_header(ct, *dptr, sdpoff, *datalen, type, term,
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -0700377 &matchoff, &matchlen) <= 0)
Herbert Xuc71529e2008-07-21 10:03:23 -0700378 return -ENOENT;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100379 return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen,
Herbert Xuc71529e2008-07-21 10:03:23 -0700380 buffer, buflen) ? 0 : -EINVAL;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800381}
382
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100383static unsigned int ip_nat_sdp_addr(struct sk_buff *skb, unsigned int dataoff,
384 const char **dptr, unsigned int *datalen,
385 unsigned int sdpoff,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700386 enum sdp_header_types type,
387 enum sdp_header_types term,
388 const union nf_inet_addr *addr)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800389{
390 char buffer[sizeof("nnn.nnn.nnn.nnn")];
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700391 unsigned int buflen;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800392
Harvey Harrisoncffee382008-10-31 00:53:08 -0700393 buflen = sprintf(buffer, "%pI4", &addr->ip);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100394 if (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff, type, term,
Herbert Xuc71529e2008-07-21 10:03:23 -0700395 buffer, buflen))
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800396 return 0;
397
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100398 return mangle_content_len(skb, dataoff, dptr, datalen);
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700399}
400
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100401static unsigned int ip_nat_sdp_port(struct sk_buff *skb, unsigned int dataoff,
402 const char **dptr, unsigned int *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700403 unsigned int matchoff,
404 unsigned int matchlen,
405 u_int16_t port)
406{
407 char buffer[sizeof("nnnnn")];
408 unsigned int buflen;
409
410 buflen = sprintf(buffer, "%u", port);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100411 if (!mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700412 buffer, buflen))
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800413 return 0;
414
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100415 return mangle_content_len(skb, dataoff, dptr, datalen);
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700416}
417
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100418static unsigned int ip_nat_sdp_session(struct sk_buff *skb, unsigned int dataoff,
419 const char **dptr, unsigned int *datalen,
420 unsigned int sdpoff,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700421 const union nf_inet_addr *addr)
422{
423 char buffer[sizeof("nnn.nnn.nnn.nnn")];
424 unsigned int buflen;
425
426 /* Mangle session description owner and contact addresses */
Harvey Harrisoncffee382008-10-31 00:53:08 -0700427 buflen = sprintf(buffer, "%pI4", &addr->ip);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100428 if (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700429 SDP_HDR_OWNER_IP4, SDP_HDR_MEDIA,
430 buffer, buflen))
431 return 0;
432
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100433 switch (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff,
Herbert Xuc71529e2008-07-21 10:03:23 -0700434 SDP_HDR_CONNECTION_IP4, SDP_HDR_MEDIA,
435 buffer, buflen)) {
436 case 0:
437 /*
438 * RFC 2327:
439 *
440 * Session description
441 *
442 * c=* (connection information - not required if included in all media)
443 */
444 case -ENOENT:
445 break;
446 default:
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800447 return 0;
Herbert Xuc71529e2008-07-21 10:03:23 -0700448 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800449
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100450 return mangle_content_len(skb, dataoff, dptr, datalen);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800451}
452
453/* So, this packet has hit the connection tracking matching code.
454 Mangle it, and change the expectation to match the new version. */
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100455static unsigned int ip_nat_sdp_media(struct sk_buff *skb, unsigned int dataoff,
456 const char **dptr, unsigned int *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700457 struct nf_conntrack_expect *rtp_exp,
458 struct nf_conntrack_expect *rtcp_exp,
459 unsigned int mediaoff,
460 unsigned int medialen,
461 union nf_inet_addr *rtp_addr)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800462{
Patrick McHardy212440a2008-03-25 20:17:13 -0700463 enum ip_conntrack_info ctinfo;
464 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800465 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800466 u_int16_t port;
467
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800468 /* Connection will come from reply */
Jerome Borsboomf4a607b2007-07-07 22:19:48 -0700469 if (ct->tuplehash[dir].tuple.src.u3.ip ==
470 ct->tuplehash[!dir].tuple.dst.u3.ip)
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700471 rtp_addr->ip = rtp_exp->tuple.dst.u3.ip;
Jerome Borsboomf4a607b2007-07-07 22:19:48 -0700472 else
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700473 rtp_addr->ip = ct->tuplehash[!dir].tuple.dst.u3.ip;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800474
Patrick McHardya9c1d352008-03-25 20:25:49 -0700475 rtp_exp->saved_ip = rtp_exp->tuple.dst.u3.ip;
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700476 rtp_exp->tuple.dst.u3.ip = rtp_addr->ip;
Patrick McHardya9c1d352008-03-25 20:25:49 -0700477 rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port;
478 rtp_exp->dir = !dir;
479 rtp_exp->expectfn = ip_nat_sip_expected;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800480
Patrick McHardya9c1d352008-03-25 20:25:49 -0700481 rtcp_exp->saved_ip = rtcp_exp->tuple.dst.u3.ip;
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700482 rtcp_exp->tuple.dst.u3.ip = rtp_addr->ip;
Patrick McHardya9c1d352008-03-25 20:25:49 -0700483 rtcp_exp->saved_proto.udp.port = rtcp_exp->tuple.dst.u.udp.port;
484 rtcp_exp->dir = !dir;
485 rtcp_exp->expectfn = ip_nat_sip_expected;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800486
Patrick McHardya9c1d352008-03-25 20:25:49 -0700487 /* Try to get same pair of ports: if not, try to change them. */
488 for (port = ntohs(rtp_exp->tuple.dst.u.udp.port);
489 port != 0; port += 2) {
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +0200490 int ret;
491
Patrick McHardya9c1d352008-03-25 20:25:49 -0700492 rtp_exp->tuple.dst.u.udp.port = htons(port);
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +0200493 ret = nf_ct_expect_related(rtp_exp);
494 if (ret == -EBUSY)
Patrick McHardya9c1d352008-03-25 20:25:49 -0700495 continue;
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +0200496 else if (ret < 0) {
497 port = 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800498 break;
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +0200499 }
500 rtcp_exp->tuple.dst.u.udp.port = htons(port + 1);
501 ret = nf_ct_expect_related(rtcp_exp);
502 if (ret == 0)
503 break;
504 else if (ret != -EBUSY) {
505 nf_ct_unexpect_related(rtp_exp);
506 port = 0;
507 break;
508 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800509 }
510
511 if (port == 0)
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700512 goto err1;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800513
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700514 /* Update media port. */
515 if (rtp_exp->tuple.dst.u.udp.port != rtp_exp->saved_proto.udp.port &&
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +0100516 !ip_nat_sdp_port(skb, dataoff, dptr, datalen,
517 mediaoff, medialen, port))
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700518 goto err2;
519
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800520 return NF_ACCEPT;
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700521
522err2:
523 nf_ct_unexpect_related(rtp_exp);
524 nf_ct_unexpect_related(rtcp_exp);
525err1:
526 return NF_DROP;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800527}
528
Pablo Neira Ayuso544d5c72012-02-05 03:44:51 +0100529static struct nf_ct_helper_expectfn sip_nat = {
530 .name = "sip",
531 .expectfn = ip_nat_sip_expected,
532};
533
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800534static void __exit nf_nat_sip_fini(void)
535{
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000536 RCU_INIT_POINTER(nf_nat_sip_hook, NULL);
537 RCU_INIT_POINTER(nf_nat_sip_seq_adjust_hook, NULL);
538 RCU_INIT_POINTER(nf_nat_sip_expect_hook, NULL);
539 RCU_INIT_POINTER(nf_nat_sdp_addr_hook, NULL);
540 RCU_INIT_POINTER(nf_nat_sdp_port_hook, NULL);
541 RCU_INIT_POINTER(nf_nat_sdp_session_hook, NULL);
542 RCU_INIT_POINTER(nf_nat_sdp_media_hook, NULL);
Pablo Neira Ayuso544d5c72012-02-05 03:44:51 +0100543 nf_ct_helper_expectfn_unregister(&sip_nat);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800544 synchronize_rcu();
545}
546
547static int __init nf_nat_sip_init(void)
548{
Patrick McHardyd1332e02007-11-05 20:43:30 -0800549 BUG_ON(nf_nat_sip_hook != NULL);
Patrick McHardy48f8ac22010-02-11 12:29:38 +0100550 BUG_ON(nf_nat_sip_seq_adjust_hook != NULL);
Patrick McHardy0f32a402008-03-25 20:25:13 -0700551 BUG_ON(nf_nat_sip_expect_hook != NULL);
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700552 BUG_ON(nf_nat_sdp_addr_hook != NULL);
Patrick McHardyc7f485a2008-03-25 20:26:43 -0700553 BUG_ON(nf_nat_sdp_port_hook != NULL);
Patrick McHardy4ab9e642008-03-25 20:26:08 -0700554 BUG_ON(nf_nat_sdp_session_hook != NULL);
555 BUG_ON(nf_nat_sdp_media_hook != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000556 RCU_INIT_POINTER(nf_nat_sip_hook, ip_nat_sip);
557 RCU_INIT_POINTER(nf_nat_sip_seq_adjust_hook, ip_nat_sip_seq_adjust);
558 RCU_INIT_POINTER(nf_nat_sip_expect_hook, ip_nat_sip_expect);
559 RCU_INIT_POINTER(nf_nat_sdp_addr_hook, ip_nat_sdp_addr);
560 RCU_INIT_POINTER(nf_nat_sdp_port_hook, ip_nat_sdp_port);
561 RCU_INIT_POINTER(nf_nat_sdp_session_hook, ip_nat_sdp_session);
562 RCU_INIT_POINTER(nf_nat_sdp_media_hook, ip_nat_sdp_media);
Pablo Neira Ayuso544d5c72012-02-05 03:44:51 +0100563 nf_ct_helper_expectfn_register(&sip_nat);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800564 return 0;
565}
566
567module_init(nf_nat_sip_init);
568module_exit(nf_nat_sip_fini);