2006-12-02 23:09:57 -07:00
|
|
|
/* SIP extension for UDP NAT alteration.
|
|
|
|
*
|
|
|
|
* (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
|
|
|
|
* based on RR's ip_nat_ftp.c and other modules.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ip.h>
|
2007-03-12 17:09:15 -06:00
|
|
|
#include <net/ip.h>
|
2006-12-02 23:09:57 -07:00
|
|
|
#include <linux/udp.h>
|
|
|
|
|
|
|
|
#include <net/netfilter/nf_nat.h>
|
|
|
|
#include <net/netfilter/nf_nat_helper.h>
|
|
|
|
#include <net/netfilter/nf_nat_rule.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_helper.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_expect.h>
|
|
|
|
#include <linux/netfilter/nf_conntrack_sip.h>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
|
|
|
|
MODULE_DESCRIPTION("SIP NAT helper");
|
|
|
|
MODULE_ALIAS("ip_nat_sip");
|
|
|
|
|
|
|
|
|
2008-03-25 21:16:54 -06:00
|
|
|
static unsigned int mangle_packet(struct sk_buff *skb,
|
|
|
|
const char **dptr, unsigned int *datalen,
|
|
|
|
unsigned int matchoff, unsigned int matchlen,
|
|
|
|
const char *buffer, unsigned int buflen)
|
|
|
|
{
|
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
|
|
|
|
if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, matchoff, matchlen,
|
|
|
|
buffer, buflen))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Reload data pointer and adjust datalen value */
|
|
|
|
*dptr = skb->data + ip_hdrlen(skb) + sizeof(struct udphdr);
|
|
|
|
*datalen += buflen - matchlen;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-03-25 21:18:40 -06:00
|
|
|
static int map_addr(struct sk_buff *skb,
|
|
|
|
const char **dptr, unsigned int *datalen,
|
|
|
|
unsigned int matchoff, unsigned int matchlen,
|
2008-03-25 21:19:30 -06:00
|
|
|
union nf_inet_addr *addr, __be16 port)
|
2006-12-02 23:09:57 -07:00
|
|
|
{
|
2008-03-25 21:17:13 -06:00
|
|
|
enum ip_conntrack_info ctinfo;
|
2008-03-25 21:19:30 -06:00
|
|
|
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
|
2006-12-02 23:09:57 -07:00
|
|
|
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
|
2008-03-25 21:19:30 -06:00
|
|
|
char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
|
|
|
|
unsigned int buflen;
|
|
|
|
__be32 newaddr;
|
|
|
|
__be16 newport;
|
|
|
|
|
|
|
|
if (ct->tuplehash[dir].tuple.src.u3.ip == addr->ip &&
|
|
|
|
ct->tuplehash[dir].tuple.src.u.udp.port == port) {
|
|
|
|
newaddr = ct->tuplehash[!dir].tuple.dst.u3.ip;
|
|
|
|
newport = ct->tuplehash[!dir].tuple.dst.u.udp.port;
|
|
|
|
} else if (ct->tuplehash[dir].tuple.dst.u3.ip == addr->ip &&
|
|
|
|
ct->tuplehash[dir].tuple.dst.u.udp.port == port) {
|
|
|
|
newaddr = ct->tuplehash[!dir].tuple.src.u3.ip;
|
|
|
|
newport = ct->tuplehash[!dir].tuple.src.u.udp.port;
|
2006-12-02 23:09:57 -07:00
|
|
|
} else
|
|
|
|
return 1;
|
|
|
|
|
2008-03-25 21:19:30 -06:00
|
|
|
if (newaddr == addr->ip && newport == port)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
buflen = sprintf(buffer, "%u.%u.%u.%u:%u",
|
|
|
|
NIPQUAD(newaddr), ntohs(newport));
|
|
|
|
|
2008-03-25 21:16:54 -06:00
|
|
|
return mangle_packet(skb, dptr, datalen, matchoff, matchlen,
|
2008-03-25 21:19:30 -06:00
|
|
|
buffer, buflen);
|
2006-12-02 23:09:57 -07:00
|
|
|
}
|
|
|
|
|
2008-03-25 21:18:40 -06:00
|
|
|
static int map_sip_addr(struct sk_buff *skb,
|
|
|
|
const char **dptr, unsigned int *datalen,
|
2008-03-25 21:19:30 -06:00
|
|
|
enum sip_header_types type)
|
2008-03-25 21:18:40 -06:00
|
|
|
{
|
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
unsigned int matchlen, matchoff;
|
2008-03-25 21:19:30 -06:00
|
|
|
union nf_inet_addr addr;
|
|
|
|
__be16 port;
|
2008-03-25 21:18:40 -06:00
|
|
|
|
2008-03-25 21:19:30 -06:00
|
|
|
if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, type, NULL,
|
|
|
|
&matchoff, &matchlen, &addr, &port) <= 0)
|
2008-03-25 21:18:40 -06:00
|
|
|
return 1;
|
2008-03-25 21:19:30 -06:00
|
|
|
return map_addr(skb, dptr, datalen, matchoff, matchlen, &addr, port);
|
2008-03-25 21:18:40 -06:00
|
|
|
}
|
|
|
|
|
2007-10-15 01:53:15 -06:00
|
|
|
static unsigned int ip_nat_sip(struct sk_buff *skb,
|
2008-03-25 21:16:54 -06:00
|
|
|
const char **dptr, unsigned int *datalen)
|
2006-12-02 23:09:57 -07:00
|
|
|
{
|
2008-03-25 21:17:13 -06:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
|
2008-03-25 21:18:40 -06:00
|
|
|
unsigned int matchoff, matchlen;
|
2008-03-25 21:19:30 -06:00
|
|
|
union nf_inet_addr addr;
|
|
|
|
__be16 port;
|
2006-12-02 23:09:57 -07:00
|
|
|
|
|
|
|
/* Basic rules: requests and responses. */
|
2008-03-25 21:17:36 -06:00
|
|
|
if (strnicmp(*dptr, "SIP/2.0", strlen("SIP/2.0")) != 0) {
|
2008-03-25 21:18:40 -06:00
|
|
|
if (ct_sip_parse_request(ct, *dptr, *datalen,
|
2008-03-25 21:19:30 -06:00
|
|
|
&matchoff, &matchlen,
|
|
|
|
&addr, &port) > 0 &&
|
|
|
|
!map_addr(skb, dptr, datalen, matchoff, matchlen,
|
|
|
|
&addr, port))
|
2006-12-02 23:09:57 -07:00
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
|
2008-03-25 21:19:30 -06:00
|
|
|
if (!map_sip_addr(skb, dptr, datalen, SIP_HDR_FROM) ||
|
|
|
|
!map_sip_addr(skb, dptr, datalen, SIP_HDR_TO) ||
|
|
|
|
!map_sip_addr(skb, dptr, datalen, SIP_HDR_VIA) ||
|
|
|
|
!map_sip_addr(skb, dptr, datalen, SIP_HDR_CONTACT))
|
2006-12-02 23:09:57 -07:00
|
|
|
return NF_DROP;
|
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
|
2008-03-25 21:17:55 -06:00
|
|
|
static int mangle_content_len(struct sk_buff *skb,
|
|
|
|
const char **dptr, unsigned int *datalen)
|
2006-12-02 23:09:57 -07:00
|
|
|
{
|
2008-03-25 21:17:13 -06:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
|
2008-03-25 21:17:55 -06:00
|
|
|
unsigned int matchoff, matchlen;
|
|
|
|
char buffer[sizeof("65536")];
|
|
|
|
int buflen, c_len;
|
2006-12-02 23:09:57 -07:00
|
|
|
|
2008-03-25 21:17:55 -06:00
|
|
|
/* Get actual SDP length */
|
|
|
|
if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
|
|
|
|
SDP_HDR_VERSION, SDP_HDR_UNSPEC,
|
|
|
|
&matchoff, &matchlen) <= 0)
|
|
|
|
return 0;
|
|
|
|
c_len = *datalen - matchoff + strlen("v=");
|
|
|
|
|
|
|
|
/* Now, update SDP length */
|
2008-03-25 21:18:57 -06:00
|
|
|
if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH,
|
|
|
|
&matchoff, &matchlen) <= 0)
|
2006-12-02 23:09:57 -07:00
|
|
|
return 0;
|
|
|
|
|
2008-03-25 21:17:55 -06:00
|
|
|
buflen = sprintf(buffer, "%u", c_len);
|
2008-03-25 21:16:54 -06:00
|
|
|
return mangle_packet(skb, dptr, datalen, matchoff, matchlen,
|
2008-03-25 21:17:55 -06:00
|
|
|
buffer, buflen);
|
2006-12-02 23:09:57 -07:00
|
|
|
}
|
|
|
|
|
2008-03-25 21:17:55 -06:00
|
|
|
static unsigned mangle_sdp_packet(struct sk_buff *skb,
|
|
|
|
const char **dptr, unsigned int *datalen,
|
|
|
|
enum sdp_header_types type,
|
|
|
|
char *buffer, int buflen)
|
2006-12-02 23:09:57 -07:00
|
|
|
{
|
2008-03-25 21:17:13 -06:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
|
2008-03-25 21:17:55 -06:00
|
|
|
unsigned int matchlen, matchoff;
|
2006-12-02 23:09:57 -07:00
|
|
|
|
2008-03-25 21:17:55 -06:00
|
|
|
if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen, type, SDP_HDR_UNSPEC,
|
|
|
|
&matchoff, &matchlen) <= 0)
|
|
|
|
return 0;
|
|
|
|
return mangle_packet(skb, dptr, datalen, matchoff, matchlen,
|
|
|
|
buffer, buflen);
|
2006-12-02 23:09:57 -07:00
|
|
|
}
|
|
|
|
|
2007-10-15 01:53:15 -06:00
|
|
|
static unsigned int mangle_sdp(struct sk_buff *skb,
|
2006-12-02 23:09:57 -07:00
|
|
|
enum ip_conntrack_info ctinfo,
|
|
|
|
struct nf_conn *ct,
|
|
|
|
__be32 newip, u_int16_t port,
|
2008-03-25 21:16:54 -06:00
|
|
|
const char **dptr, unsigned int *datalen)
|
2006-12-02 23:09:57 -07:00
|
|
|
{
|
|
|
|
char buffer[sizeof("nnn.nnn.nnn.nnn")];
|
2008-03-25 21:16:54 -06:00
|
|
|
unsigned int bufflen;
|
2006-12-02 23:09:57 -07:00
|
|
|
|
|
|
|
/* Mangle owner and contact info. */
|
|
|
|
bufflen = sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(newip));
|
2008-03-25 21:17:55 -06:00
|
|
|
if (!mangle_sdp_packet(skb, dptr, datalen, SDP_HDR_OWNER_IP4,
|
|
|
|
buffer, bufflen))
|
2006-12-02 23:09:57 -07:00
|
|
|
return 0;
|
|
|
|
|
2008-03-25 21:17:55 -06:00
|
|
|
if (!mangle_sdp_packet(skb, dptr, datalen, SDP_HDR_CONNECTION_IP4,
|
|
|
|
buffer, bufflen))
|
2006-12-02 23:09:57 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Mangle media port. */
|
|
|
|
bufflen = sprintf(buffer, "%u", port);
|
2008-03-25 21:17:55 -06:00
|
|
|
if (!mangle_sdp_packet(skb, dptr, datalen, SDP_HDR_MEDIA,
|
|
|
|
buffer, bufflen))
|
2006-12-02 23:09:57 -07:00
|
|
|
return 0;
|
|
|
|
|
2008-03-25 21:17:13 -06:00
|
|
|
return mangle_content_len(skb, dptr, datalen);
|
2006-12-02 23:09:57 -07:00
|
|
|
}
|
|
|
|
|
2007-05-03 04:35:31 -06:00
|
|
|
static void ip_nat_sdp_expect(struct nf_conn *ct,
|
|
|
|
struct nf_conntrack_expect *exp)
|
|
|
|
{
|
|
|
|
struct nf_nat_range range;
|
|
|
|
|
|
|
|
/* This must be a fresh one. */
|
|
|
|
BUG_ON(ct->status & IPS_NAT_DONE_MASK);
|
|
|
|
|
|
|
|
/* For DST manip, map port here to where it's expected. */
|
|
|
|
range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED);
|
|
|
|
range.min = range.max = exp->saved_proto;
|
|
|
|
range.min_ip = range.max_ip = exp->saved_ip;
|
2007-12-17 23:38:20 -07:00
|
|
|
nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
|
2008-03-25 21:09:51 -06:00
|
|
|
|
|
|
|
/* Change src to where master sends to */
|
|
|
|
range.flags = IP_NAT_RANGE_MAP_IPS;
|
|
|
|
range.min_ip = range.max_ip
|
|
|
|
= ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
|
|
|
|
nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
|
2007-05-03 04:35:31 -06:00
|
|
|
}
|
|
|
|
|
2006-12-02 23:09:57 -07:00
|
|
|
/* So, this packet has hit the connection tracking matching code.
|
|
|
|
Mangle it, and change the expectation to match the new version. */
|
2007-10-15 01:53:15 -06:00
|
|
|
static unsigned int ip_nat_sdp(struct sk_buff *skb,
|
2008-03-25 21:17:13 -06:00
|
|
|
const char **dptr, unsigned int *datalen,
|
|
|
|
struct nf_conntrack_expect *exp)
|
2006-12-02 23:09:57 -07:00
|
|
|
{
|
2008-03-25 21:17:13 -06:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
|
2006-12-02 23:09:57 -07:00
|
|
|
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
|
|
|
|
__be32 newip;
|
|
|
|
u_int16_t port;
|
|
|
|
|
|
|
|
/* Connection will come from reply */
|
2007-07-07 23:19:48 -06:00
|
|
|
if (ct->tuplehash[dir].tuple.src.u3.ip ==
|
|
|
|
ct->tuplehash[!dir].tuple.dst.u3.ip)
|
|
|
|
newip = exp->tuple.dst.u3.ip;
|
|
|
|
else
|
|
|
|
newip = ct->tuplehash[!dir].tuple.dst.u3.ip;
|
2006-12-02 23:09:57 -07:00
|
|
|
|
2007-05-03 04:35:31 -06:00
|
|
|
exp->saved_ip = exp->tuple.dst.u3.ip;
|
2006-12-02 23:09:57 -07:00
|
|
|
exp->tuple.dst.u3.ip = newip;
|
|
|
|
exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port;
|
|
|
|
exp->dir = !dir;
|
|
|
|
|
|
|
|
/* When you see the packet, we need to NAT it the same as the
|
|
|
|
this one. */
|
2007-05-03 04:35:31 -06:00
|
|
|
exp->expectfn = ip_nat_sdp_expect;
|
2006-12-02 23:09:57 -07:00
|
|
|
|
|
|
|
/* Try to get same port: if not, try to change it. */
|
|
|
|
for (port = ntohs(exp->saved_proto.udp.port); port != 0; port++) {
|
|
|
|
exp->tuple.dst.u.udp.port = htons(port);
|
2007-07-07 23:30:49 -06:00
|
|
|
if (nf_ct_expect_related(exp) == 0)
|
2006-12-02 23:09:57 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port == 0)
|
|
|
|
return NF_DROP;
|
|
|
|
|
2008-03-25 21:16:54 -06:00
|
|
|
if (!mangle_sdp(skb, ctinfo, ct, newip, port, dptr, datalen)) {
|
2007-07-07 23:30:49 -06:00
|
|
|
nf_ct_unexpect_related(exp);
|
2006-12-02 23:09:57 -07:00
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit nf_nat_sip_fini(void)
|
|
|
|
{
|
|
|
|
rcu_assign_pointer(nf_nat_sip_hook, NULL);
|
|
|
|
rcu_assign_pointer(nf_nat_sdp_hook, NULL);
|
|
|
|
synchronize_rcu();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init nf_nat_sip_init(void)
|
|
|
|
{
|
2007-11-05 21:43:30 -07:00
|
|
|
BUG_ON(nf_nat_sip_hook != NULL);
|
|
|
|
BUG_ON(nf_nat_sdp_hook != NULL);
|
2006-12-02 23:09:57 -07:00
|
|
|
rcu_assign_pointer(nf_nat_sip_hook, ip_nat_sip);
|
|
|
|
rcu_assign_pointer(nf_nat_sdp_hook, ip_nat_sdp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(nf_nat_sip_init);
|
|
|
|
module_exit(nf_nat_sip_fini);
|