]> Pileus Git - ~andy/linux/blob - net/netfilter/nf_nat_sip.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[~andy/linux] / net / netfilter / nf_nat_sip.c
1 /* SIP extension for NAT alteration.
2  *
3  * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
4  * based on RR's ip_nat_ftp.c and other modules.
5  * (C) 2007 United Security Providers
6  * (C) 2007, 2008, 2011, 2012 Patrick McHardy <kaber@trash.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/inet.h>
16 #include <linux/udp.h>
17 #include <linux/tcp.h>
18
19 #include <net/netfilter/nf_nat.h>
20 #include <net/netfilter/nf_nat_helper.h>
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
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
27 MODULE_DESCRIPTION("SIP NAT helper");
28 MODULE_ALIAS("ip_nat_sip");
29
30
31 static unsigned int mangle_packet(struct sk_buff *skb, unsigned int protoff,
32                                   unsigned int dataoff,
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);
39         struct tcphdr *th;
40         unsigned int baseoff;
41
42         if (nf_ct_protonum(ct) == IPPROTO_TCP) {
43                 th = (struct tcphdr *)(skb->data + protoff);
44                 baseoff = protoff + th->doff * 4;
45                 matchoff += dataoff - baseoff;
46
47                 if (!__nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
48                                                 protoff, matchoff, matchlen,
49                                                 buffer, buflen, false))
50                         return 0;
51         } else {
52                 baseoff = protoff + sizeof(struct udphdr);
53                 matchoff += dataoff - baseoff;
54
55                 if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo,
56                                               protoff, matchoff, matchlen,
57                                               buffer, buflen))
58                         return 0;
59         }
60
61         /* Reload data pointer and adjust datalen value */
62         *dptr = skb->data + dataoff;
63         *datalen += buflen - matchlen;
64         return 1;
65 }
66
67 static 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
80 static 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
89 static int map_addr(struct sk_buff *skb, unsigned int protoff,
90                     unsigned int dataoff,
91                     const char **dptr, unsigned int *datalen,
92                     unsigned int matchoff, unsigned int matchlen,
93                     union nf_inet_addr *addr, __be16 port)
94 {
95         enum ip_conntrack_info ctinfo;
96         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
97         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
98         struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
99         char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
100         unsigned int buflen;
101         union nf_inet_addr newaddr;
102         __be16 newport;
103
104         if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, addr) &&
105             ct->tuplehash[dir].tuple.src.u.udp.port == port) {
106                 newaddr = ct->tuplehash[!dir].tuple.dst.u3;
107                 newport = ct->tuplehash[!dir].tuple.dst.u.udp.port;
108         } else if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, addr) &&
109                    ct->tuplehash[dir].tuple.dst.u.udp.port == port) {
110                 newaddr = ct->tuplehash[!dir].tuple.src.u3;
111                 newport = ct_sip_info->forced_dport ? :
112                           ct->tuplehash[!dir].tuple.src.u.udp.port;
113         } else
114                 return 1;
115
116         if (nf_inet_addr_cmp(&newaddr, addr) && newport == port)
117                 return 1;
118
119         buflen = sip_sprintf_addr_port(ct, buffer, &newaddr, ntohs(newport));
120         return mangle_packet(skb, protoff, dataoff, dptr, datalen,
121                              matchoff, matchlen, buffer, buflen);
122 }
123
124 static int map_sip_addr(struct sk_buff *skb, unsigned int protoff,
125                         unsigned int dataoff,
126                         const char **dptr, unsigned int *datalen,
127                         enum sip_header_types type)
128 {
129         enum ip_conntrack_info ctinfo;
130         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
131         unsigned int matchlen, matchoff;
132         union nf_inet_addr addr;
133         __be16 port;
134
135         if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, type, NULL,
136                                     &matchoff, &matchlen, &addr, &port) <= 0)
137                 return 1;
138         return map_addr(skb, protoff, dataoff, dptr, datalen,
139                         matchoff, matchlen, &addr, port);
140 }
141
142 static unsigned int nf_nat_sip(struct sk_buff *skb, unsigned int protoff,
143                                unsigned int dataoff,
144                                const char **dptr, unsigned int *datalen)
145 {
146         enum ip_conntrack_info ctinfo;
147         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
148         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
149         struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
150         unsigned int coff, matchoff, matchlen;
151         enum sip_header_types hdr;
152         union nf_inet_addr addr;
153         __be16 port;
154         int request, in_header;
155
156         /* Basic rules: requests and responses. */
157         if (strnicmp(*dptr, "SIP/2.0", strlen("SIP/2.0")) != 0) {
158                 if (ct_sip_parse_request(ct, *dptr, *datalen,
159                                          &matchoff, &matchlen,
160                                          &addr, &port) > 0 &&
161                     !map_addr(skb, protoff, dataoff, dptr, datalen,
162                               matchoff, matchlen, &addr, port))
163                         return NF_DROP;
164                 request = 1;
165         } else
166                 request = 0;
167
168         if (nf_ct_protonum(ct) == IPPROTO_TCP)
169                 hdr = SIP_HDR_VIA_TCP;
170         else
171                 hdr = SIP_HDR_VIA_UDP;
172
173         /* Translate topmost Via header and parameters */
174         if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
175                                     hdr, NULL, &matchoff, &matchlen,
176                                     &addr, &port) > 0) {
177                 unsigned int olen, matchend, poff, plen, buflen, n;
178                 char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
179
180                 /* We're only interested in headers related to this
181                  * connection */
182                 if (request) {
183                         if (!nf_inet_addr_cmp(&addr,
184                                         &ct->tuplehash[dir].tuple.src.u3) ||
185                             port != ct->tuplehash[dir].tuple.src.u.udp.port)
186                                 goto next;
187                 } else {
188                         if (!nf_inet_addr_cmp(&addr,
189                                         &ct->tuplehash[dir].tuple.dst.u3) ||
190                             port != ct->tuplehash[dir].tuple.dst.u.udp.port)
191                                 goto next;
192                 }
193
194                 olen = *datalen;
195                 if (!map_addr(skb, protoff, dataoff, dptr, datalen,
196                               matchoff, matchlen, &addr, port))
197                         return NF_DROP;
198
199                 matchend = matchoff + matchlen + *datalen - olen;
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,
205                                                &addr, true) > 0 &&
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);
211                         if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
212                                            poff, plen, buffer, buflen))
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,
220                                                &addr, false) > 0 &&
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);
226                         if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
227                                            poff, plen, buffer, buflen))
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));
240                         if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
241                                            poff, plen, buffer, buflen))
242                                 return NF_DROP;
243                 }
244         }
245
246 next:
247         /* Translate Contact headers */
248         coff = 0;
249         in_header = 0;
250         while (ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
251                                        SIP_HDR_CONTACT, &in_header,
252                                        &matchoff, &matchlen,
253                                        &addr, &port) > 0) {
254                 if (!map_addr(skb, protoff, dataoff, dptr, datalen,
255                               matchoff, matchlen,
256                               &addr, port))
257                         return NF_DROP;
258         }
259
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))
262                 return NF_DROP;
263
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
279         return NF_ACCEPT;
280 }
281
282 static void nf_nat_sip_seq_adjust(struct sk_buff *skb, unsigned int protoff,
283                                   s16 off)
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
292         th = (struct tcphdr *)(skb->data + protoff);
293         nf_nat_set_seq_adjust(ct, ctinfo, th->seq, off);
294 }
295
296 /* Handles expected signalling connections and media streams */
297 static void nf_nat_sip_expected(struct nf_conn *ct,
298                                 struct nf_conntrack_expect *exp)
299 {
300         struct nf_nat_range range;
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. */
306         range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
307         range.min_proto = range.max_proto = exp->saved_proto;
308         range.min_addr = range.max_addr = exp->saved_addr;
309         nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
310
311         /* Change src to where master sends to, but only if the connection
312          * actually came from the same source. */
313         if (nf_inet_addr_cmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3,
314                              &ct->master->tuplehash[exp->dir].tuple.src.u3)) {
315                 range.flags = NF_NAT_RANGE_MAP_IPS;
316                 range.min_addr = range.max_addr
317                         = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
318                 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
319         }
320 }
321
322 static unsigned int nf_nat_sip_expect(struct sk_buff *skb, unsigned int protoff,
323                                       unsigned int dataoff,
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);
332         struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
333         union nf_inet_addr newaddr;
334         u_int16_t port;
335         __be16 srcport;
336         char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
337         unsigned int buflen;
338
339         /* Connection will come from reply */
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;
343         else
344                 newaddr = ct->tuplehash[!dir].tuple.dst.u3;
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. */
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)
352                 port = ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port);
353         else
354                 port = ntohs(exp->tuple.dst.u.udp.port);
355
356         exp->saved_addr = exp->tuple.dst.u3;
357         exp->tuple.dst.u3 = newaddr;
358         exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port;
359         exp->dir = !dir;
360         exp->expectfn = nf_nat_sip_expected;
361
362         for (; port != 0; port++) {
363                 int ret;
364
365                 exp->tuple.dst.u.udp.port = htons(port);
366                 ret = nf_ct_expect_related(exp);
367                 if (ret == 0)
368                         break;
369                 else if (ret != -EBUSY) {
370                         port = 0;
371                         break;
372                 }
373         }
374
375         if (port == 0)
376                 return NF_DROP;
377
378         if (!nf_inet_addr_cmp(&exp->tuple.dst.u3, &exp->saved_addr) ||
379             exp->tuple.dst.u.udp.port != exp->saved_proto.udp.port) {
380                 buflen = sip_sprintf_addr_port(ct, buffer, &newaddr, port);
381                 if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
382                                    matchoff, matchlen, buffer, buflen))
383                         goto err;
384         }
385         return NF_ACCEPT;
386
387 err:
388         nf_ct_unexpect_related(exp);
389         return NF_DROP;
390 }
391
392 static int mangle_content_len(struct sk_buff *skb, unsigned int protoff,
393                               unsigned int dataoff,
394                               const char **dptr, unsigned int *datalen)
395 {
396         enum ip_conntrack_info ctinfo;
397         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
398         unsigned int matchoff, matchlen;
399         char buffer[sizeof("65536")];
400         int buflen, c_len;
401
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 */
410         if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH,
411                               &matchoff, &matchlen) <= 0)
412                 return 0;
413
414         buflen = sprintf(buffer, "%u", c_len);
415         return mangle_packet(skb, protoff, dataoff, dptr, datalen,
416                              matchoff, matchlen, buffer, buflen);
417 }
418
419 static int mangle_sdp_packet(struct sk_buff *skb, unsigned int protoff,
420                              unsigned int dataoff,
421                              const char **dptr, unsigned int *datalen,
422                              unsigned int sdpoff,
423                              enum sdp_header_types type,
424                              enum sdp_header_types term,
425                              char *buffer, int buflen)
426 {
427         enum ip_conntrack_info ctinfo;
428         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
429         unsigned int matchlen, matchoff;
430
431         if (ct_sip_get_sdp_header(ct, *dptr, sdpoff, *datalen, type, term,
432                                   &matchoff, &matchlen) <= 0)
433                 return -ENOENT;
434         return mangle_packet(skb, protoff, dataoff, dptr, datalen,
435                              matchoff, matchlen, buffer, buflen) ? 0 : -EINVAL;
436 }
437
438 static unsigned int nf_nat_sdp_addr(struct sk_buff *skb, unsigned int protoff,
439                                     unsigned int dataoff,
440                                     const char **dptr, unsigned int *datalen,
441                                     unsigned int sdpoff,
442                                     enum sdp_header_types type,
443                                     enum sdp_header_types term,
444                                     const union nf_inet_addr *addr)
445 {
446         enum ip_conntrack_info ctinfo;
447         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
448         char buffer[INET6_ADDRSTRLEN];
449         unsigned int buflen;
450
451         buflen = sip_sprintf_addr(ct, buffer, addr, false);
452         if (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen,
453                               sdpoff, type, term, buffer, buflen))
454                 return 0;
455
456         return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
457 }
458
459 static unsigned int nf_nat_sdp_port(struct sk_buff *skb, unsigned int protoff,
460                                     unsigned int dataoff,
461                                     const char **dptr, unsigned int *datalen,
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);
470         if (!mangle_packet(skb, protoff, dataoff, dptr, datalen,
471                            matchoff, matchlen, buffer, buflen))
472                 return 0;
473
474         return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
475 }
476
477 static unsigned int nf_nat_sdp_session(struct sk_buff *skb, unsigned int protoff,
478                                        unsigned int dataoff,
479                                        const char **dptr, unsigned int *datalen,
480                                        unsigned int sdpoff,
481                                        const union nf_inet_addr *addr)
482 {
483         enum ip_conntrack_info ctinfo;
484         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
485         char buffer[INET6_ADDRSTRLEN];
486         unsigned int buflen;
487
488         /* Mangle session description owner and contact addresses */
489         buflen = sip_sprintf_addr(ct, buffer, addr, false);
490         if (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, sdpoff,
491                               SDP_HDR_OWNER, SDP_HDR_MEDIA, buffer, buflen))
492                 return 0;
493
494         switch (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, sdpoff,
495                                   SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
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:
508                 return 0;
509         }
510
511         return mangle_content_len(skb, protoff, dataoff, dptr, datalen);
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. */
516 static unsigned int nf_nat_sdp_media(struct sk_buff *skb, unsigned int protoff,
517                                      unsigned int dataoff,
518                                      const char **dptr, unsigned int *datalen,
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)
524 {
525         enum ip_conntrack_info ctinfo;
526         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
527         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
528         u_int16_t port;
529
530         /* Connection will come from reply */
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;
534         else
535                 *rtp_addr = ct->tuplehash[!dir].tuple.dst.u3;
536
537         rtp_exp->saved_addr = rtp_exp->tuple.dst.u3;
538         rtp_exp->tuple.dst.u3 = *rtp_addr;
539         rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port;
540         rtp_exp->dir = !dir;
541         rtp_exp->expectfn = nf_nat_sip_expected;
542
543         rtcp_exp->saved_addr = rtcp_exp->tuple.dst.u3;
544         rtcp_exp->tuple.dst.u3 = *rtp_addr;
545         rtcp_exp->saved_proto.udp.port = rtcp_exp->tuple.dst.u.udp.port;
546         rtcp_exp->dir = !dir;
547         rtcp_exp->expectfn = nf_nat_sip_expected;
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) {
552                 int ret;
553
554                 rtp_exp->tuple.dst.u.udp.port = htons(port);
555                 ret = nf_ct_expect_related(rtp_exp);
556                 if (ret == -EBUSY)
557                         continue;
558                 else if (ret < 0) {
559                         port = 0;
560                         break;
561                 }
562                 rtcp_exp->tuple.dst.u.udp.port = htons(port + 1);
563                 ret = nf_ct_expect_related(rtcp_exp);
564                 if (ret == 0)
565                         break;
566                 else if (ret == -EBUSY) {
567                         nf_ct_unexpect_related(rtp_exp);
568                         continue;
569                 } else if (ret < 0) {
570                         nf_ct_unexpect_related(rtp_exp);
571                         port = 0;
572                         break;
573                 }
574         }
575
576         if (port == 0)
577                 goto err1;
578
579         /* Update media port. */
580         if (rtp_exp->tuple.dst.u.udp.port != rtp_exp->saved_proto.udp.port &&
581             !nf_nat_sdp_port(skb, protoff, dataoff, dptr, datalen,
582                              mediaoff, medialen, port))
583                 goto err2;
584
585         return NF_ACCEPT;
586
587 err2:
588         nf_ct_unexpect_related(rtp_exp);
589         nf_ct_unexpect_related(rtcp_exp);
590 err1:
591         return NF_DROP;
592 }
593
594 static struct nf_ct_helper_expectfn sip_nat = {
595         .name           = "sip",
596         .expectfn       = nf_nat_sip_expected,
597 };
598
599 static void __exit nf_nat_sip_fini(void)
600 {
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);
608         nf_ct_helper_expectfn_unregister(&sip_nat);
609         synchronize_rcu();
610 }
611
612 static int __init nf_nat_sip_init(void)
613 {
614         BUG_ON(nf_nat_sip_hook != NULL);
615         BUG_ON(nf_nat_sip_seq_adjust_hook != NULL);
616         BUG_ON(nf_nat_sip_expect_hook != NULL);
617         BUG_ON(nf_nat_sdp_addr_hook != NULL);
618         BUG_ON(nf_nat_sdp_port_hook != NULL);
619         BUG_ON(nf_nat_sdp_session_hook != NULL);
620         BUG_ON(nf_nat_sdp_media_hook != NULL);
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);
628         nf_ct_helper_expectfn_register(&sip_nat);
629         return 0;
630 }
631
632 module_init(nf_nat_sip_init);
633 module_exit(nf_nat_sip_fini);