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