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