irqchip: gic: Perform the gic_secondary_init() call via CPU notifier
[deliverable/linux.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 161 !map_addr(skb, protoff, dataoff, dptr, datalen,
b20ab9cc
PNA
162 matchoff, matchlen, &addr, port)) {
163 nf_ct_helper_log(skb, ct, "cannot mangle SIP message");
9fafcd7b 164 return NF_DROP;
b20ab9cc 165 }
720ac708
PM
166 request = 1;
167 } else
168 request = 0;
169
48f8ac26
PM
170 if (nf_ct_protonum(ct) == IPPROTO_TCP)
171 hdr = SIP_HDR_VIA_TCP;
172 else
173 hdr = SIP_HDR_VIA_UDP;
174
720ac708
PM
175 /* Translate topmost Via header and parameters */
176 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
48f8ac26 177 hdr, NULL, &matchoff, &matchlen,
720ac708 178 &addr, &port) > 0) {
f22eb25c 179 unsigned int olen, matchend, poff, plen, buflen, n;
9a664821 180 char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
720ac708
PM
181
182 /* We're only interested in headers related to this
183 * connection */
184 if (request) {
9a664821
PM
185 if (!nf_inet_addr_cmp(&addr,
186 &ct->tuplehash[dir].tuple.src.u3) ||
720ac708
PM
187 port != ct->tuplehash[dir].tuple.src.u.udp.port)
188 goto next;
189 } else {
9a664821
PM
190 if (!nf_inet_addr_cmp(&addr,
191 &ct->tuplehash[dir].tuple.dst.u3) ||
720ac708
PM
192 port != ct->tuplehash[dir].tuple.dst.u.udp.port)
193 goto next;
194 }
195
f22eb25c 196 olen = *datalen;
051966c0 197 if (!map_addr(skb, protoff, dataoff, dptr, datalen,
b20ab9cc
PNA
198 matchoff, matchlen, &addr, port)) {
199 nf_ct_helper_log(skb, ct, "cannot mangle Via header");
720ac708 200 return NF_DROP;
b20ab9cc 201 }
720ac708 202
f22eb25c 203 matchend = matchoff + matchlen + *datalen - olen;
720ac708
PM
204
205 /* The maddr= parameter (RFC 2361) specifies where to send
206 * the reply. */
207 if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen,
208 "maddr=", &poff, &plen,
02b69cbd 209 &addr, true) > 0 &&
9a664821
PM
210 nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3) &&
211 !nf_inet_addr_cmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3)) {
212 buflen = sip_sprintf_addr(ct, buffer,
213 &ct->tuplehash[!dir].tuple.dst.u3,
214 true);
051966c0 215 if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
b20ab9cc
PNA
216 poff, plen, buffer, buflen)) {
217 nf_ct_helper_log(skb, ct, "cannot mangle maddr");
720ac708 218 return NF_DROP;
b20ab9cc 219 }
720ac708
PM
220 }
221
222 /* The received= parameter (RFC 2361) contains the address
223 * from which the server received the request. */
224 if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen,
225 "received=", &poff, &plen,
02b69cbd 226 &addr, false) > 0 &&
9a664821
PM
227 nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.dst.u3) &&
228 !nf_inet_addr_cmp(&addr, &ct->tuplehash[!dir].tuple.src.u3)) {
229 buflen = sip_sprintf_addr(ct, buffer,
230 &ct->tuplehash[!dir].tuple.src.u3,
231 false);
051966c0 232 if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
3b6b9fab 233 poff, plen, buffer, buflen))
b20ab9cc 234 nf_ct_helper_log(skb, ct, "cannot mangle received");
720ac708
PM
235 return NF_DROP;
236 }
237
238 /* The rport= parameter (RFC 3581) contains the port number
239 * from which the server received the request. */
240 if (ct_sip_parse_numerical_param(ct, *dptr, matchend, *datalen,
241 "rport=", &poff, &plen,
242 &n) > 0 &&
243 htons(n) == ct->tuplehash[dir].tuple.dst.u.udp.port &&
244 htons(n) != ct->tuplehash[!dir].tuple.src.u.udp.port) {
245 __be16 p = ct->tuplehash[!dir].tuple.src.u.udp.port;
246 buflen = sprintf(buffer, "%u", ntohs(p));
051966c0 247 if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
b20ab9cc
PNA
248 poff, plen, buffer, buflen)) {
249 nf_ct_helper_log(skb, ct, "cannot mangle rport");
720ac708 250 return NF_DROP;
b20ab9cc 251 }
720ac708 252 }
9fafcd7b
PM
253 }
254
720ac708 255next:
c978cd3a 256 /* Translate Contact headers */
3b6b9fab 257 coff = 0;
c978cd3a 258 in_header = 0;
3b6b9fab 259 while (ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
c978cd3a
PM
260 SIP_HDR_CONTACT, &in_header,
261 &matchoff, &matchlen,
262 &addr, &port) > 0) {
051966c0
PM
263 if (!map_addr(skb, protoff, dataoff, dptr, datalen,
264 matchoff, matchlen,
b20ab9cc
PNA
265 &addr, port)) {
266 nf_ct_helper_log(skb, ct, "cannot mangle contact");
c978cd3a 267 return NF_DROP;
b20ab9cc 268 }
c978cd3a
PM
269 }
270
051966c0 271 if (!map_sip_addr(skb, protoff, dataoff, dptr, datalen, SIP_HDR_FROM) ||
b20ab9cc
PNA
272 !map_sip_addr(skb, protoff, dataoff, dptr, datalen, SIP_HDR_TO)) {
273 nf_ct_helper_log(skb, ct, "cannot mangle SIP from/to");
9fafcd7b 274 return NF_DROP;
b20ab9cc 275 }
48f8ac26 276
7266507d
KC
277 /* Mangle destination port for Cisco phones, then fix up checksums */
278 if (dir == IP_CT_DIR_REPLY && ct_sip_info->forced_dport) {
279 struct udphdr *uh;
280
b20ab9cc
PNA
281 if (!skb_make_writable(skb, skb->len)) {
282 nf_ct_helper_log(skb, ct, "cannot mangle packet");
7266507d 283 return NF_DROP;
b20ab9cc 284 }
7266507d
KC
285
286 uh = (void *)skb->data + protoff;
287 uh->dest = ct_sip_info->forced_dport;
288
289 if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, protoff,
b20ab9cc
PNA
290 0, 0, NULL, 0)) {
291 nf_ct_helper_log(skb, ct, "cannot mangle packet");
7266507d 292 return NF_DROP;
b20ab9cc 293 }
7266507d
KC
294 }
295
9fafcd7b
PM
296 return NF_ACCEPT;
297}
298
9a664821
PM
299static void nf_nat_sip_seq_adjust(struct sk_buff *skb, unsigned int protoff,
300 s16 off)
48f8ac26
PM
301{
302 enum ip_conntrack_info ctinfo;
303 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
304 const struct tcphdr *th;
305
306 if (nf_ct_protonum(ct) != IPPROTO_TCP || off == 0)
307 return;
308
9a664821 309 th = (struct tcphdr *)(skb->data + protoff);
48f8ac26
PM
310 nf_nat_set_seq_adjust(ct, ctinfo, th->seq, off);
311}
312
0f32a40f 313/* Handles expected signalling connections and media streams */
9a664821 314static void nf_nat_sip_expected(struct nf_conn *ct,
0f32a40f
PM
315 struct nf_conntrack_expect *exp)
316{
c7232c99 317 struct nf_nat_range range;
0f32a40f
PM
318
319 /* This must be a fresh one. */
320 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
321
322 /* For DST manip, map port here to where it's expected. */
cbc9f2f4 323 range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
c7232c99
PM
324 range.min_proto = range.max_proto = exp->saved_proto;
325 range.min_addr = range.max_addr = exp->saved_addr;
cbc9f2f4 326 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
0f32a40f
PM
327
328 /* Change src to where master sends to, but only if the connection
329 * actually came from the same source. */
9a664821
PM
330 if (nf_inet_addr_cmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3,
331 &ct->master->tuplehash[exp->dir].tuple.src.u3)) {
cbc9f2f4 332 range.flags = NF_NAT_RANGE_MAP_IPS;
c7232c99
PM
333 range.min_addr = range.max_addr
334 = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
cbc9f2f4 335 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
0f32a40f
PM
336 }
337}
338
9a664821 339static unsigned int nf_nat_sip_expect(struct sk_buff *skb, unsigned int protoff,
051966c0 340 unsigned int dataoff,
0f32a40f
PM
341 const char **dptr, unsigned int *datalen,
342 struct nf_conntrack_expect *exp,
343 unsigned int matchoff,
344 unsigned int matchlen)
345{
346 enum ip_conntrack_info ctinfo;
347 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
348 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
7266507d 349 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
9a664821 350 union nf_inet_addr newaddr;
0f32a40f 351 u_int16_t port;
7266507d 352 __be16 srcport;
9a664821 353 char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
95c96174 354 unsigned int buflen;
0f32a40f
PM
355
356 /* Connection will come from reply */
9a664821
PM
357 if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
358 &ct->tuplehash[!dir].tuple.dst.u3))
359 newaddr = exp->tuple.dst.u3;
0f32a40f 360 else
9a664821 361 newaddr = ct->tuplehash[!dir].tuple.dst.u3;
0f32a40f
PM
362
363 /* If the signalling port matches the connection's source port in the
364 * original direction, try to use the destination port in the opposite
365 * direction. */
7266507d
KC
366 srcport = ct_sip_info->forced_dport ? :
367 ct->tuplehash[dir].tuple.src.u.udp.port;
368 if (exp->tuple.dst.u.udp.port == srcport)
0f32a40f
PM
369 port = ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port);
370 else
371 port = ntohs(exp->tuple.dst.u.udp.port);
372
c7232c99 373 exp->saved_addr = exp->tuple.dst.u3;
9a664821 374 exp->tuple.dst.u3 = newaddr;
0f32a40f
PM
375 exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port;
376 exp->dir = !dir;
9a664821 377 exp->expectfn = nf_nat_sip_expected;
0f32a40f
PM
378
379 for (; port != 0; port++) {
5b92b61f
PNA
380 int ret;
381
0f32a40f 382 exp->tuple.dst.u.udp.port = htons(port);
5b92b61f
PNA
383 ret = nf_ct_expect_related(exp);
384 if (ret == 0)
385 break;
386 else if (ret != -EBUSY) {
387 port = 0;
0f32a40f 388 break;
5b92b61f 389 }
0f32a40f
PM
390 }
391
b20ab9cc
PNA
392 if (port == 0) {
393 nf_ct_helper_log(skb, ct, "all ports in use for SIP");
0f32a40f 394 return NF_DROP;
b20ab9cc 395 }
0f32a40f 396
9a664821 397 if (!nf_inet_addr_cmp(&exp->tuple.dst.u3, &exp->saved_addr) ||
0f32a40f 398 exp->tuple.dst.u.udp.port != exp->saved_proto.udp.port) {
9a664821 399 buflen = sip_sprintf_addr_port(ct, buffer, &newaddr, port);
051966c0 400 if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
b20ab9cc
PNA
401 matchoff, matchlen, buffer, buflen)) {
402 nf_ct_helper_log(skb, ct, "cannot mangle packet");
0f32a40f 403 goto err;
b20ab9cc 404 }
0f32a40f
PM
405 }
406 return NF_ACCEPT;
407
408err:
409 nf_ct_unexpect_related(exp);
410 return NF_DROP;
411}
412
051966c0
PM
413static int mangle_content_len(struct sk_buff *skb, unsigned int protoff,
414 unsigned int dataoff,
3e9b4600 415 const char **dptr, unsigned int *datalen)
9fafcd7b 416{
212440a7
PM
417 enum ip_conntrack_info ctinfo;
418 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
3e9b4600
PM
419 unsigned int matchoff, matchlen;
420 char buffer[sizeof("65536")];
421 int buflen, c_len;
9fafcd7b 422
3e9b4600
PM
423 /* Get actual SDP length */
424 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
425 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
426 &matchoff, &matchlen) <= 0)
427 return 0;
428 c_len = *datalen - matchoff + strlen("v=");
429
430 /* Now, update SDP length */
ea45f12a
PM
431 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH,
432 &matchoff, &matchlen) <= 0)
9fafcd7b
PM
433 return 0;
434
3e9b4600 435 buflen = sprintf(buffer, "%u", c_len);
051966c0
PM
436 return mangle_packet(skb, protoff, dataoff, dptr, datalen,
437 matchoff, matchlen, buffer, buflen);
9fafcd7b
PM
438}
439
051966c0
PM
440static int mangle_sdp_packet(struct sk_buff *skb, unsigned int protoff,
441 unsigned int dataoff,
3b6b9fab
PM
442 const char **dptr, unsigned int *datalen,
443 unsigned int sdpoff,
c71529e4
HX
444 enum sdp_header_types type,
445 enum sdp_header_types term,
446 char *buffer, int buflen)
9fafcd7b 447{
212440a7
PM
448 enum ip_conntrack_info ctinfo;
449 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
3e9b4600 450 unsigned int matchlen, matchoff;
9fafcd7b 451
3b6b9fab 452 if (ct_sip_get_sdp_header(ct, *dptr, sdpoff, *datalen, type, term,
3e9b4600 453 &matchoff, &matchlen) <= 0)
c71529e4 454 return -ENOENT;
051966c0
PM
455 return mangle_packet(skb, protoff, dataoff, dptr, datalen,
456 matchoff, matchlen, buffer, buflen) ? 0 : -EINVAL;
9fafcd7b
PM
457}
458
9a664821 459static unsigned int nf_nat_sdp_addr(struct sk_buff *skb, unsigned int protoff,
051966c0 460 unsigned int dataoff,
3b6b9fab
PM
461 const char **dptr, unsigned int *datalen,
462 unsigned int sdpoff,
4ab9e64e
PM
463 enum sdp_header_types type,
464 enum sdp_header_types term,
465 const union nf_inet_addr *addr)
9fafcd7b 466{
9a664821
PM
467 enum ip_conntrack_info ctinfo;
468 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
469 char buffer[INET6_ADDRSTRLEN];
4ab9e64e 470 unsigned int buflen;
9fafcd7b 471
9a664821 472 buflen = sip_sprintf_addr(ct, buffer, addr, false);
051966c0
PM
473 if (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen,
474 sdpoff, type, term, buffer, buflen))
9fafcd7b
PM
475 return 0;
476
051966c0 477 return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
4ab9e64e
PM
478}
479
9a664821 480static unsigned int nf_nat_sdp_port(struct sk_buff *skb, unsigned int protoff,
051966c0 481 unsigned int dataoff,
3b6b9fab 482 const char **dptr, unsigned int *datalen,
4ab9e64e
PM
483 unsigned int matchoff,
484 unsigned int matchlen,
485 u_int16_t port)
486{
487 char buffer[sizeof("nnnnn")];
488 unsigned int buflen;
489
490 buflen = sprintf(buffer, "%u", port);
051966c0
PM
491 if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
492 matchoff, matchlen, buffer, buflen))
9fafcd7b
PM
493 return 0;
494
051966c0 495 return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
4ab9e64e
PM
496}
497
9a664821 498static unsigned int nf_nat_sdp_session(struct sk_buff *skb, unsigned int protoff,
051966c0 499 unsigned int dataoff,
3b6b9fab
PM
500 const char **dptr, unsigned int *datalen,
501 unsigned int sdpoff,
4ab9e64e
PM
502 const union nf_inet_addr *addr)
503{
9a664821
PM
504 enum ip_conntrack_info ctinfo;
505 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
506 char buffer[INET6_ADDRSTRLEN];
4ab9e64e
PM
507 unsigned int buflen;
508
509 /* Mangle session description owner and contact addresses */
9a664821 510 buflen = sip_sprintf_addr(ct, buffer, addr, false);
051966c0 511 if (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, sdpoff,
9a664821 512 SDP_HDR_OWNER, SDP_HDR_MEDIA, buffer, buflen))
4ab9e64e
PM
513 return 0;
514
051966c0 515 switch (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, sdpoff,
9a664821 516 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
c71529e4
HX
517 buffer, buflen)) {
518 case 0:
519 /*
520 * RFC 2327:
521 *
522 * Session description
523 *
524 * c=* (connection information - not required if included in all media)
525 */
526 case -ENOENT:
527 break;
528 default:
9fafcd7b 529 return 0;
c71529e4 530 }
9fafcd7b 531
051966c0 532 return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
9fafcd7b
PM
533}
534
535/* So, this packet has hit the connection tracking matching code.
536 Mangle it, and change the expectation to match the new version. */
9a664821 537static unsigned int nf_nat_sdp_media(struct sk_buff *skb, unsigned int protoff,
051966c0 538 unsigned int dataoff,
3b6b9fab 539 const char **dptr, unsigned int *datalen,
4ab9e64e
PM
540 struct nf_conntrack_expect *rtp_exp,
541 struct nf_conntrack_expect *rtcp_exp,
542 unsigned int mediaoff,
543 unsigned int medialen,
544 union nf_inet_addr *rtp_addr)
9fafcd7b 545{
212440a7
PM
546 enum ip_conntrack_info ctinfo;
547 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
9fafcd7b 548 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
9fafcd7b
PM
549 u_int16_t port;
550
9fafcd7b 551 /* Connection will come from reply */
9a664821
PM
552 if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
553 &ct->tuplehash[!dir].tuple.dst.u3))
554 *rtp_addr = rtp_exp->tuple.dst.u3;
f4a607bf 555 else
9a664821 556 *rtp_addr = ct->tuplehash[!dir].tuple.dst.u3;
9fafcd7b 557
c7232c99 558 rtp_exp->saved_addr = rtp_exp->tuple.dst.u3;
9a664821 559 rtp_exp->tuple.dst.u3 = *rtp_addr;
a9c1d359
PM
560 rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port;
561 rtp_exp->dir = !dir;
9a664821 562 rtp_exp->expectfn = nf_nat_sip_expected;
a9c1d359 563
c7232c99 564 rtcp_exp->saved_addr = rtcp_exp->tuple.dst.u3;
9a664821 565 rtcp_exp->tuple.dst.u3 = *rtp_addr;
a9c1d359
PM
566 rtcp_exp->saved_proto.udp.port = rtcp_exp->tuple.dst.u.udp.port;
567 rtcp_exp->dir = !dir;
9a664821 568 rtcp_exp->expectfn = nf_nat_sip_expected;
a9c1d359
PM
569
570 /* Try to get same pair of ports: if not, try to change them. */
571 for (port = ntohs(rtp_exp->tuple.dst.u.udp.port);
572 port != 0; port += 2) {
5b92b61f
PNA
573 int ret;
574
a9c1d359 575 rtp_exp->tuple.dst.u.udp.port = htons(port);
5b92b61f
PNA
576 ret = nf_ct_expect_related(rtp_exp);
577 if (ret == -EBUSY)
a9c1d359 578 continue;
5b92b61f
PNA
579 else if (ret < 0) {
580 port = 0;
581 break;
582 }
a9c1d359 583 rtcp_exp->tuple.dst.u.udp.port = htons(port + 1);
5b92b61f
PNA
584 ret = nf_ct_expect_related(rtcp_exp);
585 if (ret == 0)
9fafcd7b 586 break;
3f509c68
PNA
587 else if (ret == -EBUSY) {
588 nf_ct_unexpect_related(rtp_exp);
589 continue;
590 } else if (ret < 0) {
5b92b61f
PNA
591 nf_ct_unexpect_related(rtp_exp);
592 port = 0;
593 break;
594 }
9fafcd7b
PM
595 }
596
b20ab9cc
PNA
597 if (port == 0) {
598 nf_ct_helper_log(skb, ct, "all ports in use for SDP media");
4ab9e64e 599 goto err1;
b20ab9cc 600 }
4ab9e64e
PM
601
602 /* Update media port. */
603 if (rtp_exp->tuple.dst.u.udp.port != rtp_exp->saved_proto.udp.port &&
9a664821 604 !nf_nat_sdp_port(skb, protoff, dataoff, dptr, datalen,
b20ab9cc
PNA
605 mediaoff, medialen, port)) {
606 nf_ct_helper_log(skb, ct, "cannot mangle SDP message");
4ab9e64e 607 goto err2;
b20ab9cc 608 }
9fafcd7b 609
9fafcd7b 610 return NF_ACCEPT;
4ab9e64e
PM
611
612err2:
613 nf_ct_unexpect_related(rtp_exp);
614 nf_ct_unexpect_related(rtcp_exp);
615err1:
616 return NF_DROP;
9fafcd7b
PM
617}
618
544d5c7d 619static struct nf_ct_helper_expectfn sip_nat = {
9a664821
PM
620 .name = "sip",
621 .expectfn = nf_nat_sip_expected,
544d5c7d
PNA
622};
623
9fafcd7b
PM
624static void __exit nf_nat_sip_fini(void)
625{
a9b3cd7f
SH
626 RCU_INIT_POINTER(nf_nat_sip_hook, NULL);
627 RCU_INIT_POINTER(nf_nat_sip_seq_adjust_hook, NULL);
628 RCU_INIT_POINTER(nf_nat_sip_expect_hook, NULL);
629 RCU_INIT_POINTER(nf_nat_sdp_addr_hook, NULL);
630 RCU_INIT_POINTER(nf_nat_sdp_port_hook, NULL);
631 RCU_INIT_POINTER(nf_nat_sdp_session_hook, NULL);
632 RCU_INIT_POINTER(nf_nat_sdp_media_hook, NULL);
544d5c7d 633 nf_ct_helper_expectfn_unregister(&sip_nat);
9fafcd7b
PM
634 synchronize_rcu();
635}
636
637static int __init nf_nat_sip_init(void)
638{
d1332e0a 639 BUG_ON(nf_nat_sip_hook != NULL);
48f8ac26 640 BUG_ON(nf_nat_sip_seq_adjust_hook != NULL);
0f32a40f 641 BUG_ON(nf_nat_sip_expect_hook != NULL);
4ab9e64e 642 BUG_ON(nf_nat_sdp_addr_hook != NULL);
c7f485ab 643 BUG_ON(nf_nat_sdp_port_hook != NULL);
4ab9e64e
PM
644 BUG_ON(nf_nat_sdp_session_hook != NULL);
645 BUG_ON(nf_nat_sdp_media_hook != NULL);
9a664821
PM
646 RCU_INIT_POINTER(nf_nat_sip_hook, nf_nat_sip);
647 RCU_INIT_POINTER(nf_nat_sip_seq_adjust_hook, nf_nat_sip_seq_adjust);
648 RCU_INIT_POINTER(nf_nat_sip_expect_hook, nf_nat_sip_expect);
649 RCU_INIT_POINTER(nf_nat_sdp_addr_hook, nf_nat_sdp_addr);
650 RCU_INIT_POINTER(nf_nat_sdp_port_hook, nf_nat_sdp_port);
651 RCU_INIT_POINTER(nf_nat_sdp_session_hook, nf_nat_sdp_session);
652 RCU_INIT_POINTER(nf_nat_sdp_media_hook, nf_nat_sdp_media);
544d5c7d 653 nf_ct_helper_expectfn_register(&sip_nat);
9fafcd7b
PM
654 return 0;
655}
656
657module_init(nf_nat_sip_init);
658module_exit(nf_nat_sip_fini);
This page took 0.602926 seconds and 5 git commands to generate.