]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/netfilter/nf_nat_sip.c
b232e4040dc658f65c43102b53df841a65f977a1
[net-next-2.6.git] / net / ipv4 / netfilter / nf_nat_sip.c
1 /* SIP extension for UDP 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 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/ip.h>
16 #include <net/ip.h>
17 #include <linux/udp.h>
18
19 #include <net/netfilter/nf_nat.h>
20 #include <net/netfilter/nf_nat_helper.h>
21 #include <net/netfilter/nf_nat_rule.h>
22 #include <net/netfilter/nf_conntrack_helper.h>
23 #include <net/netfilter/nf_conntrack_expect.h>
24 #include <linux/netfilter/nf_conntrack_sip.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
28 MODULE_DESCRIPTION("SIP NAT helper");
29 MODULE_ALIAS("ip_nat_sip");
30
31
32 static unsigned int mangle_packet(struct sk_buff *skb, 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
40         if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, matchoff, matchlen,
41                                       buffer, buflen))
42                 return 0;
43
44         /* Reload data pointer and adjust datalen value */
45         *dptr = skb->data + dataoff;
46         *datalen += buflen - matchlen;
47         return 1;
48 }
49
50 static int map_addr(struct sk_buff *skb, unsigned int dataoff,
51                     const char **dptr, unsigned int *datalen,
52                     unsigned int matchoff, unsigned int matchlen,
53                     union nf_inet_addr *addr, __be16 port)
54 {
55         enum ip_conntrack_info ctinfo;
56         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
57         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
58         char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
59         unsigned int buflen;
60         __be32 newaddr;
61         __be16 newport;
62
63         if (ct->tuplehash[dir].tuple.src.u3.ip == addr->ip &&
64             ct->tuplehash[dir].tuple.src.u.udp.port == port) {
65                 newaddr = ct->tuplehash[!dir].tuple.dst.u3.ip;
66                 newport = ct->tuplehash[!dir].tuple.dst.u.udp.port;
67         } else if (ct->tuplehash[dir].tuple.dst.u3.ip == addr->ip &&
68                    ct->tuplehash[dir].tuple.dst.u.udp.port == port) {
69                 newaddr = ct->tuplehash[!dir].tuple.src.u3.ip;
70                 newport = ct->tuplehash[!dir].tuple.src.u.udp.port;
71         } else
72                 return 1;
73
74         if (newaddr == addr->ip && newport == port)
75                 return 1;
76
77         buflen = sprintf(buffer, "%pI4:%u", &newaddr, ntohs(newport));
78
79         return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen,
80                              buffer, buflen);
81 }
82
83 static int map_sip_addr(struct sk_buff *skb, unsigned int dataoff,
84                         const char **dptr, unsigned int *datalen,
85                         enum sip_header_types type)
86 {
87         enum ip_conntrack_info ctinfo;
88         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
89         unsigned int matchlen, matchoff;
90         union nf_inet_addr addr;
91         __be16 port;
92
93         if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, type, NULL,
94                                     &matchoff, &matchlen, &addr, &port) <= 0)
95                 return 1;
96         return map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen,
97                         &addr, port);
98 }
99
100 static unsigned int ip_nat_sip(struct sk_buff *skb, unsigned int dataoff,
101                                const char **dptr, unsigned int *datalen)
102 {
103         enum ip_conntrack_info ctinfo;
104         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
105         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
106         unsigned int coff, matchoff, matchlen;
107         union nf_inet_addr addr;
108         __be16 port;
109         int request, in_header;
110
111         /* Basic rules: requests and responses. */
112         if (strnicmp(*dptr, "SIP/2.0", strlen("SIP/2.0")) != 0) {
113                 if (ct_sip_parse_request(ct, *dptr, *datalen,
114                                          &matchoff, &matchlen,
115                                          &addr, &port) > 0 &&
116                     !map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen,
117                               &addr, port))
118                         return NF_DROP;
119                 request = 1;
120         } else
121                 request = 0;
122
123         /* Translate topmost Via header and parameters */
124         if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
125                                     SIP_HDR_VIA_UDP, NULL, &matchoff, &matchlen,
126                                     &addr, &port) > 0) {
127                 unsigned int matchend, poff, plen, buflen, n;
128                 char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
129
130                 /* We're only interested in headers related to this
131                  * connection */
132                 if (request) {
133                         if (addr.ip != ct->tuplehash[dir].tuple.src.u3.ip ||
134                             port != ct->tuplehash[dir].tuple.src.u.udp.port)
135                                 goto next;
136                 } else {
137                         if (addr.ip != ct->tuplehash[dir].tuple.dst.u3.ip ||
138                             port != ct->tuplehash[dir].tuple.dst.u.udp.port)
139                                 goto next;
140                 }
141
142                 if (!map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen,
143                               &addr, port))
144                         return NF_DROP;
145
146                 matchend = matchoff + matchlen;
147
148                 /* The maddr= parameter (RFC 2361) specifies where to send
149                  * the reply. */
150                 if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen,
151                                                "maddr=", &poff, &plen,
152                                                &addr) > 0 &&
153                     addr.ip == ct->tuplehash[dir].tuple.src.u3.ip &&
154                     addr.ip != ct->tuplehash[!dir].tuple.dst.u3.ip) {
155                         buflen = sprintf(buffer, "%pI4",
156                                         &ct->tuplehash[!dir].tuple.dst.u3.ip);
157                         if (!mangle_packet(skb, dataoff, dptr, datalen,
158                                            poff, plen, buffer, buflen))
159                                 return NF_DROP;
160                 }
161
162                 /* The received= parameter (RFC 2361) contains the address
163                  * from which the server received the request. */
164                 if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen,
165                                                "received=", &poff, &plen,
166                                                &addr) > 0 &&
167                     addr.ip == ct->tuplehash[dir].tuple.dst.u3.ip &&
168                     addr.ip != ct->tuplehash[!dir].tuple.src.u3.ip) {
169                         buflen = sprintf(buffer, "%pI4",
170                                         &ct->tuplehash[!dir].tuple.src.u3.ip);
171                         if (!mangle_packet(skb, dataoff, dptr, datalen,
172                                            poff, plen, buffer, buflen))
173                                 return NF_DROP;
174                 }
175
176                 /* The rport= parameter (RFC 3581) contains the port number
177                  * from which the server received the request. */
178                 if (ct_sip_parse_numerical_param(ct, *dptr, matchend, *datalen,
179                                                  "rport=", &poff, &plen,
180                                                  &n) > 0 &&
181                     htons(n) == ct->tuplehash[dir].tuple.dst.u.udp.port &&
182                     htons(n) != ct->tuplehash[!dir].tuple.src.u.udp.port) {
183                         __be16 p = ct->tuplehash[!dir].tuple.src.u.udp.port;
184                         buflen = sprintf(buffer, "%u", ntohs(p));
185                         if (!mangle_packet(skb, dataoff, dptr, datalen,
186                                            poff, plen, buffer, buflen))
187                                 return NF_DROP;
188                 }
189         }
190
191 next:
192         /* Translate Contact headers */
193         coff = 0;
194         in_header = 0;
195         while (ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
196                                        SIP_HDR_CONTACT, &in_header,
197                                        &matchoff, &matchlen,
198                                        &addr, &port) > 0) {
199                 if (!map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen,
200                               &addr, port))
201                         return NF_DROP;
202         }
203
204         if (!map_sip_addr(skb, dataoff, dptr, datalen, SIP_HDR_FROM) ||
205             !map_sip_addr(skb, dataoff, dptr, datalen, SIP_HDR_TO))
206                 return NF_DROP;
207         return NF_ACCEPT;
208 }
209
210 /* Handles expected signalling connections and media streams */
211 static void ip_nat_sip_expected(struct nf_conn *ct,
212                                 struct nf_conntrack_expect *exp)
213 {
214         struct nf_nat_range range;
215
216         /* This must be a fresh one. */
217         BUG_ON(ct->status & IPS_NAT_DONE_MASK);
218
219         /* For DST manip, map port here to where it's expected. */
220         range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED);
221         range.min = range.max = exp->saved_proto;
222         range.min_ip = range.max_ip = exp->saved_ip;
223         nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
224
225         /* Change src to where master sends to, but only if the connection
226          * actually came from the same source. */
227         if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip ==
228             ct->master->tuplehash[exp->dir].tuple.src.u3.ip) {
229                 range.flags = IP_NAT_RANGE_MAP_IPS;
230                 range.min_ip = range.max_ip
231                         = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
232                 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
233         }
234 }
235
236 static unsigned int ip_nat_sip_expect(struct sk_buff *skb, unsigned int dataoff,
237                                       const char **dptr, unsigned int *datalen,
238                                       struct nf_conntrack_expect *exp,
239                                       unsigned int matchoff,
240                                       unsigned int matchlen)
241 {
242         enum ip_conntrack_info ctinfo;
243         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
244         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
245         __be32 newip;
246         u_int16_t port;
247         char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
248         unsigned buflen;
249
250         /* Connection will come from reply */
251         if (ct->tuplehash[dir].tuple.src.u3.ip == ct->tuplehash[!dir].tuple.dst.u3.ip)
252                 newip = exp->tuple.dst.u3.ip;
253         else
254                 newip = ct->tuplehash[!dir].tuple.dst.u3.ip;
255
256         /* If the signalling port matches the connection's source port in the
257          * original direction, try to use the destination port in the opposite
258          * direction. */
259         if (exp->tuple.dst.u.udp.port ==
260             ct->tuplehash[dir].tuple.src.u.udp.port)
261                 port = ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port);
262         else
263                 port = ntohs(exp->tuple.dst.u.udp.port);
264
265         exp->saved_ip = exp->tuple.dst.u3.ip;
266         exp->tuple.dst.u3.ip = newip;
267         exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port;
268         exp->dir = !dir;
269         exp->expectfn = ip_nat_sip_expected;
270
271         for (; port != 0; port++) {
272                 exp->tuple.dst.u.udp.port = htons(port);
273                 if (nf_ct_expect_related(exp) == 0)
274                         break;
275         }
276
277         if (port == 0)
278                 return NF_DROP;
279
280         if (exp->tuple.dst.u3.ip != exp->saved_ip ||
281             exp->tuple.dst.u.udp.port != exp->saved_proto.udp.port) {
282                 buflen = sprintf(buffer, "%pI4:%u", &newip, port);
283                 if (!mangle_packet(skb, dataoff, dptr, datalen,
284                                    matchoff, matchlen, buffer, buflen))
285                         goto err;
286         }
287         return NF_ACCEPT;
288
289 err:
290         nf_ct_unexpect_related(exp);
291         return NF_DROP;
292 }
293
294 static int mangle_content_len(struct sk_buff *skb, unsigned int dataoff,
295                               const char **dptr, unsigned int *datalen)
296 {
297         enum ip_conntrack_info ctinfo;
298         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
299         unsigned int matchoff, matchlen;
300         char buffer[sizeof("65536")];
301         int buflen, c_len;
302
303         /* Get actual SDP length */
304         if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
305                                   SDP_HDR_VERSION, SDP_HDR_UNSPEC,
306                                   &matchoff, &matchlen) <= 0)
307                 return 0;
308         c_len = *datalen - matchoff + strlen("v=");
309
310         /* Now, update SDP length */
311         if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH,
312                               &matchoff, &matchlen) <= 0)
313                 return 0;
314
315         buflen = sprintf(buffer, "%u", c_len);
316         return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen,
317                              buffer, buflen);
318 }
319
320 static int mangle_sdp_packet(struct sk_buff *skb, unsigned int dataoff,
321                              const char **dptr, unsigned int *datalen,
322                              unsigned int sdpoff,
323                              enum sdp_header_types type,
324                              enum sdp_header_types term,
325                              char *buffer, int buflen)
326 {
327         enum ip_conntrack_info ctinfo;
328         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
329         unsigned int matchlen, matchoff;
330
331         if (ct_sip_get_sdp_header(ct, *dptr, sdpoff, *datalen, type, term,
332                                   &matchoff, &matchlen) <= 0)
333                 return -ENOENT;
334         return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen,
335                              buffer, buflen) ? 0 : -EINVAL;
336 }
337
338 static unsigned int ip_nat_sdp_addr(struct sk_buff *skb, unsigned int dataoff,
339                                     const char **dptr, unsigned int *datalen,
340                                     unsigned int sdpoff,
341                                     enum sdp_header_types type,
342                                     enum sdp_header_types term,
343                                     const union nf_inet_addr *addr)
344 {
345         char buffer[sizeof("nnn.nnn.nnn.nnn")];
346         unsigned int buflen;
347
348         buflen = sprintf(buffer, "%pI4", &addr->ip);
349         if (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff, type, term,
350                               buffer, buflen))
351                 return 0;
352
353         return mangle_content_len(skb, dataoff, dptr, datalen);
354 }
355
356 static unsigned int ip_nat_sdp_port(struct sk_buff *skb, unsigned int dataoff,
357                                     const char **dptr, unsigned int *datalen,
358                                     unsigned int matchoff,
359                                     unsigned int matchlen,
360                                     u_int16_t port)
361 {
362         char buffer[sizeof("nnnnn")];
363         unsigned int buflen;
364
365         buflen = sprintf(buffer, "%u", port);
366         if (!mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen,
367                            buffer, buflen))
368                 return 0;
369
370         return mangle_content_len(skb, dataoff, dptr, datalen);
371 }
372
373 static unsigned int ip_nat_sdp_session(struct sk_buff *skb, unsigned int dataoff,
374                                        const char **dptr, unsigned int *datalen,
375                                        unsigned int sdpoff,
376                                        const union nf_inet_addr *addr)
377 {
378         char buffer[sizeof("nnn.nnn.nnn.nnn")];
379         unsigned int buflen;
380
381         /* Mangle session description owner and contact addresses */
382         buflen = sprintf(buffer, "%pI4", &addr->ip);
383         if (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff,
384                                SDP_HDR_OWNER_IP4, SDP_HDR_MEDIA,
385                                buffer, buflen))
386                 return 0;
387
388         switch (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff,
389                                   SDP_HDR_CONNECTION_IP4, SDP_HDR_MEDIA,
390                                   buffer, buflen)) {
391         case 0:
392         /*
393          * RFC 2327:
394          *
395          * Session description
396          *
397          * c=* (connection information - not required if included in all media)
398          */
399         case -ENOENT:
400                 break;
401         default:
402                 return 0;
403         }
404
405         return mangle_content_len(skb, dataoff, dptr, datalen);
406 }
407
408 /* So, this packet has hit the connection tracking matching code.
409    Mangle it, and change the expectation to match the new version. */
410 static unsigned int ip_nat_sdp_media(struct sk_buff *skb, unsigned int dataoff,
411                                      const char **dptr, unsigned int *datalen,
412                                      struct nf_conntrack_expect *rtp_exp,
413                                      struct nf_conntrack_expect *rtcp_exp,
414                                      unsigned int mediaoff,
415                                      unsigned int medialen,
416                                      union nf_inet_addr *rtp_addr)
417 {
418         enum ip_conntrack_info ctinfo;
419         struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
420         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
421         u_int16_t port;
422
423         /* Connection will come from reply */
424         if (ct->tuplehash[dir].tuple.src.u3.ip ==
425             ct->tuplehash[!dir].tuple.dst.u3.ip)
426                 rtp_addr->ip = rtp_exp->tuple.dst.u3.ip;
427         else
428                 rtp_addr->ip = ct->tuplehash[!dir].tuple.dst.u3.ip;
429
430         rtp_exp->saved_ip = rtp_exp->tuple.dst.u3.ip;
431         rtp_exp->tuple.dst.u3.ip = rtp_addr->ip;
432         rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port;
433         rtp_exp->dir = !dir;
434         rtp_exp->expectfn = ip_nat_sip_expected;
435
436         rtcp_exp->saved_ip = rtcp_exp->tuple.dst.u3.ip;
437         rtcp_exp->tuple.dst.u3.ip = rtp_addr->ip;
438         rtcp_exp->saved_proto.udp.port = rtcp_exp->tuple.dst.u.udp.port;
439         rtcp_exp->dir = !dir;
440         rtcp_exp->expectfn = ip_nat_sip_expected;
441
442         /* Try to get same pair of ports: if not, try to change them. */
443         for (port = ntohs(rtp_exp->tuple.dst.u.udp.port);
444              port != 0; port += 2) {
445                 rtp_exp->tuple.dst.u.udp.port = htons(port);
446                 if (nf_ct_expect_related(rtp_exp) != 0)
447                         continue;
448                 rtcp_exp->tuple.dst.u.udp.port = htons(port + 1);
449                 if (nf_ct_expect_related(rtcp_exp) == 0)
450                         break;
451                 nf_ct_unexpect_related(rtp_exp);
452         }
453
454         if (port == 0)
455                 goto err1;
456
457         /* Update media port. */
458         if (rtp_exp->tuple.dst.u.udp.port != rtp_exp->saved_proto.udp.port &&
459             !ip_nat_sdp_port(skb, dataoff, dptr, datalen,
460                              mediaoff, medialen, port))
461                 goto err2;
462
463         return NF_ACCEPT;
464
465 err2:
466         nf_ct_unexpect_related(rtp_exp);
467         nf_ct_unexpect_related(rtcp_exp);
468 err1:
469         return NF_DROP;
470 }
471
472 static void __exit nf_nat_sip_fini(void)
473 {
474         rcu_assign_pointer(nf_nat_sip_hook, NULL);
475         rcu_assign_pointer(nf_nat_sip_expect_hook, NULL);
476         rcu_assign_pointer(nf_nat_sdp_addr_hook, NULL);
477         rcu_assign_pointer(nf_nat_sdp_port_hook, NULL);
478         rcu_assign_pointer(nf_nat_sdp_session_hook, NULL);
479         rcu_assign_pointer(nf_nat_sdp_media_hook, NULL);
480         synchronize_rcu();
481 }
482
483 static int __init nf_nat_sip_init(void)
484 {
485         BUG_ON(nf_nat_sip_hook != NULL);
486         BUG_ON(nf_nat_sip_expect_hook != NULL);
487         BUG_ON(nf_nat_sdp_addr_hook != NULL);
488         BUG_ON(nf_nat_sdp_port_hook != NULL);
489         BUG_ON(nf_nat_sdp_session_hook != NULL);
490         BUG_ON(nf_nat_sdp_media_hook != NULL);
491         rcu_assign_pointer(nf_nat_sip_hook, ip_nat_sip);
492         rcu_assign_pointer(nf_nat_sip_expect_hook, ip_nat_sip_expect);
493         rcu_assign_pointer(nf_nat_sdp_addr_hook, ip_nat_sdp_addr);
494         rcu_assign_pointer(nf_nat_sdp_port_hook, ip_nat_sdp_port);
495         rcu_assign_pointer(nf_nat_sdp_session_hook, ip_nat_sdp_session);
496         rcu_assign_pointer(nf_nat_sdp_media_hook, ip_nat_sdp_media);
497         return 0;
498 }
499
500 module_init(nf_nat_sip_init);
501 module_exit(nf_nat_sip_fini);