Commit 2a6cfb22 authored by Patrick McHardy's avatar Patrick McHardy Committed by David S. Miller

[NETFILTER]: nf_conntrack_sip: adjust dptr and datalen after packet mangling

After mangling the packet, the pointer to the data and the length of the data
portion may change and need to be adjusted.

Use double data pointers and a pointer to the length everywhere and add a
helper function to the NAT helper for performing the adjustments.
Signed-off-by: default avatarPatrick McHardy <kaber@trash.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent b1ec488b
...@@ -24,11 +24,13 @@ enum sip_header_pos { ...@@ -24,11 +24,13 @@ enum sip_header_pos {
extern unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb, extern unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conn *ct, struct nf_conn *ct,
const char **dptr); const char **dptr,
unsigned int *datalen);
extern unsigned int (*nf_nat_sdp_hook)(struct sk_buff *skb, extern unsigned int (*nf_nat_sdp_hook)(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conntrack_expect *exp, struct nf_conntrack_expect *exp,
const char *dptr); const char **dptr,
unsigned int *datalen);
extern int ct_sip_get_info(const struct nf_conn *ct, const char *dptr, extern int ct_sip_get_info(const struct nf_conn *ct, const char *dptr,
size_t dlen, unsigned int *matchoff, size_t dlen, unsigned int *matchoff,
......
...@@ -60,15 +60,35 @@ static void addr_map_init(const struct nf_conn *ct, struct addr_map *map) ...@@ -60,15 +60,35 @@ static void addr_map_init(const struct nf_conn *ct, struct addr_map *map)
} }
} }
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;
}
static int map_sip_addr(struct sk_buff *skb, enum ip_conntrack_info ctinfo, static int map_sip_addr(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
struct nf_conn *ct, const char **dptr, size_t dlen, struct nf_conn *ct,
const char **dptr, unsigned int *datalen,
enum sip_header_pos pos, struct addr_map *map) enum sip_header_pos pos, struct addr_map *map)
{ {
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
unsigned int matchlen, matchoff, addrlen; unsigned int matchlen, matchoff, addrlen;
char *addr; char *addr;
if (ct_sip_get_info(ct, *dptr, dlen, &matchoff, &matchlen, pos) <= 0) if (ct_sip_get_info(ct, *dptr, *datalen, &matchoff, &matchlen,
pos) <= 0)
return 1; return 1;
if ((matchlen == map->addr[dir].srciplen || if ((matchlen == map->addr[dir].srciplen ||
...@@ -84,26 +104,19 @@ static int map_sip_addr(struct sk_buff *skb, enum ip_conntrack_info ctinfo, ...@@ -84,26 +104,19 @@ static int map_sip_addr(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
} else } else
return 1; return 1;
if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, return mangle_packet(skb, dptr, datalen, matchoff, matchlen,
matchoff, matchlen, addr, addrlen)) addr, addrlen);
return 0;
*dptr = skb->data + ip_hdrlen(skb) + sizeof(struct udphdr);
return 1;
} }
static unsigned int ip_nat_sip(struct sk_buff *skb, static unsigned int ip_nat_sip(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conn *ct, struct nf_conn *ct,
const char **dptr) const char **dptr, unsigned int *datalen)
{ {
enum sip_header_pos pos; enum sip_header_pos pos;
struct addr_map map; struct addr_map map;
int dataoff, datalen;
dataoff = ip_hdrlen(skb) + sizeof(struct udphdr); if (*datalen < sizeof("SIP/2.0") - 1)
datalen = skb->len - dataoff;
if (datalen < sizeof("SIP/2.0") - 1)
return NF_ACCEPT; return NF_ACCEPT;
addr_map_init(ct, &map); addr_map_init(ct, &map);
...@@ -115,7 +128,7 @@ static unsigned int ip_nat_sip(struct sk_buff *skb, ...@@ -115,7 +128,7 @@ static unsigned int ip_nat_sip(struct sk_buff *skb,
* The "userinfo" and "@" components of the SIP URI MUST NOT * The "userinfo" and "@" components of the SIP URI MUST NOT
* be present. * be present.
*/ */
if (datalen >= sizeof("REGISTER") - 1 && if (*datalen >= sizeof("REGISTER") - 1 &&
strncmp(*dptr, "REGISTER", sizeof("REGISTER") - 1) == 0) strncmp(*dptr, "REGISTER", sizeof("REGISTER") - 1) == 0)
pos = POS_REG_REQ_URI; pos = POS_REG_REQ_URI;
else else
...@@ -136,51 +149,45 @@ static unsigned int ip_nat_sip(struct sk_buff *skb, ...@@ -136,51 +149,45 @@ static unsigned int ip_nat_sip(struct sk_buff *skb,
static unsigned int mangle_sip_packet(struct sk_buff *skb, static unsigned int mangle_sip_packet(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conn *ct, struct nf_conn *ct,
const char **dptr, size_t dlen, const char **dptr, unsigned int *datalen,
char *buffer, int bufflen, char *buffer, int bufflen,
enum sip_header_pos pos) enum sip_header_pos pos)
{ {
unsigned int matchlen, matchoff; unsigned int matchlen, matchoff;
if (ct_sip_get_info(ct, *dptr, dlen, &matchoff, &matchlen, pos) <= 0) if (ct_sip_get_info(ct, *dptr, *datalen, &matchoff, &matchlen,
return 0; pos) <= 0)
if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo,
matchoff, matchlen, buffer, bufflen))
return 0; return 0;
/* We need to reload this. Thanks Patrick. */ return mangle_packet(skb, dptr, datalen, matchoff, matchlen,
*dptr = skb->data + ip_hdrlen(skb) + sizeof(struct udphdr); buffer, bufflen);
return 1;
} }
static int mangle_content_len(struct sk_buff *skb, static int mangle_content_len(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conn *ct, struct nf_conn *ct,
const char *dptr) const char **dptr, unsigned int *datalen)
{ {
unsigned int dataoff, matchoff, matchlen; unsigned int matchoff, matchlen;
char buffer[sizeof("65536")]; char buffer[sizeof("65536")];
int bufflen; int bufflen;
dataoff = ip_hdrlen(skb) + sizeof(struct udphdr);
/* Get actual SDP length */ /* Get actual SDP length */
if (ct_sip_get_info(ct, dptr, skb->len - dataoff, &matchoff, if (ct_sip_get_info(ct, *dptr, *datalen, &matchoff,
&matchlen, POS_SDP_HEADER) > 0) { &matchlen, POS_SDP_HEADER) > 0) {
/* since ct_sip_get_info() give us a pointer passing 'v=' /* since ct_sip_get_info() give us a pointer passing 'v='
we need to add 2 bytes in this count. */ we need to add 2 bytes in this count. */
int c_len = skb->len - dataoff - matchoff + 2; int c_len = *datalen - matchoff + 2;
/* Now, update SDP length */ /* Now, update SDP length */
if (ct_sip_get_info(ct, dptr, skb->len - dataoff, &matchoff, if (ct_sip_get_info(ct, *dptr, *datalen, &matchoff,
&matchlen, POS_CONTENT) > 0) { &matchlen, POS_CONTENT) > 0) {
bufflen = sprintf(buffer, "%u", c_len); bufflen = sprintf(buffer, "%u", c_len);
return nf_nat_mangle_udp_packet(skb, ct, ctinfo, return mangle_packet(skb, dptr, datalen,
matchoff, matchlen, matchoff, matchlen,
buffer, bufflen); buffer, bufflen);
} }
} }
return 0; return 0;
...@@ -190,30 +197,28 @@ static unsigned int mangle_sdp(struct sk_buff *skb, ...@@ -190,30 +197,28 @@ static unsigned int mangle_sdp(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conn *ct, struct nf_conn *ct,
__be32 newip, u_int16_t port, __be32 newip, u_int16_t port,
const char *dptr) const char **dptr, unsigned int *datalen)
{ {
char buffer[sizeof("nnn.nnn.nnn.nnn")]; char buffer[sizeof("nnn.nnn.nnn.nnn")];
unsigned int dataoff, bufflen; unsigned int bufflen;
dataoff = ip_hdrlen(skb) + sizeof(struct udphdr);
/* Mangle owner and contact info. */ /* Mangle owner and contact info. */
bufflen = sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(newip)); bufflen = sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(newip));
if (!mangle_sip_packet(skb, ctinfo, ct, &dptr, skb->len - dataoff, if (!mangle_sip_packet(skb, ctinfo, ct, dptr, datalen,
buffer, bufflen, POS_OWNER_IP4)) buffer, bufflen, POS_OWNER_IP4))
return 0; return 0;
if (!mangle_sip_packet(skb, ctinfo, ct, &dptr, skb->len - dataoff, if (!mangle_sip_packet(skb, ctinfo, ct, dptr, datalen,
buffer, bufflen, POS_CONNECTION_IP4)) buffer, bufflen, POS_CONNECTION_IP4))
return 0; return 0;
/* Mangle media port. */ /* Mangle media port. */
bufflen = sprintf(buffer, "%u", port); bufflen = sprintf(buffer, "%u", port);
if (!mangle_sip_packet(skb, ctinfo, ct, &dptr, skb->len - dataoff, if (!mangle_sip_packet(skb, ctinfo, ct, dptr, datalen,
buffer, bufflen, POS_MEDIA)) buffer, bufflen, POS_MEDIA))
return 0; return 0;
return mangle_content_len(skb, ctinfo, ct, dptr); return mangle_content_len(skb, ctinfo, ct, dptr, datalen);
} }
static void ip_nat_sdp_expect(struct nf_conn *ct, static void ip_nat_sdp_expect(struct nf_conn *ct,
...@@ -242,7 +247,7 @@ static void ip_nat_sdp_expect(struct nf_conn *ct, ...@@ -242,7 +247,7 @@ static void ip_nat_sdp_expect(struct nf_conn *ct,
static unsigned int ip_nat_sdp(struct sk_buff *skb, static unsigned int ip_nat_sdp(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conntrack_expect *exp, struct nf_conntrack_expect *exp,
const char *dptr) const char **dptr, unsigned int *datalen)
{ {
struct nf_conn *ct = exp->master; struct nf_conn *ct = exp->master;
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
...@@ -275,7 +280,7 @@ static unsigned int ip_nat_sdp(struct sk_buff *skb, ...@@ -275,7 +280,7 @@ static unsigned int ip_nat_sdp(struct sk_buff *skb,
if (port == 0) if (port == 0)
return NF_DROP; return NF_DROP;
if (!mangle_sdp(skb, ctinfo, ct, newip, port, dptr)) { if (!mangle_sdp(skb, ctinfo, ct, newip, port, dptr, datalen)) {
nf_ct_unexpect_related(exp); nf_ct_unexpect_related(exp);
return NF_DROP; return NF_DROP;
} }
......
...@@ -39,13 +39,15 @@ MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session"); ...@@ -39,13 +39,15 @@ MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb, unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conn *ct, struct nf_conn *ct,
const char **dptr) __read_mostly; const char **dptr,
unsigned int *datalen) __read_mostly;
EXPORT_SYMBOL_GPL(nf_nat_sip_hook); EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
unsigned int (*nf_nat_sdp_hook)(struct sk_buff *skb, unsigned int (*nf_nat_sdp_hook)(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
struct nf_conntrack_expect *exp, struct nf_conntrack_expect *exp,
const char *dptr) __read_mostly; const char **dptr,
unsigned int *datalen) __read_mostly;
EXPORT_SYMBOL_GPL(nf_nat_sdp_hook); EXPORT_SYMBOL_GPL(nf_nat_sdp_hook);
static int digits_len(const struct nf_conn *, const char *, const char *, int *); static int digits_len(const struct nf_conn *, const char *, const char *, int *);
...@@ -369,7 +371,7 @@ static int set_expected_rtp(struct sk_buff *skb, ...@@ -369,7 +371,7 @@ static int set_expected_rtp(struct sk_buff *skb,
enum ip_conntrack_info ctinfo, enum ip_conntrack_info ctinfo,
union nf_inet_addr *addr, union nf_inet_addr *addr,
__be16 port, __be16 port,
const char *dptr) const char **dptr, unsigned int *datalen)
{ {
struct nf_conntrack_expect *exp; struct nf_conntrack_expect *exp;
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
...@@ -386,7 +388,7 @@ static int set_expected_rtp(struct sk_buff *skb, ...@@ -386,7 +388,7 @@ static int set_expected_rtp(struct sk_buff *skb,
nf_nat_sdp = rcu_dereference(nf_nat_sdp_hook); nf_nat_sdp = rcu_dereference(nf_nat_sdp_hook);
if (nf_nat_sdp && ct->status & IPS_NAT_MASK) if (nf_nat_sdp && ct->status & IPS_NAT_MASK)
ret = nf_nat_sdp(skb, ctinfo, exp, dptr); ret = nf_nat_sdp(skb, ctinfo, exp, dptr, datalen);
else { else {
if (nf_ct_expect_related(exp) != 0) if (nf_ct_expect_related(exp) != 0)
ret = NF_DROP; ret = NF_DROP;
...@@ -429,7 +431,7 @@ static int sip_help(struct sk_buff *skb, ...@@ -429,7 +431,7 @@ static int sip_help(struct sk_buff *skb,
nf_nat_sip = rcu_dereference(nf_nat_sip_hook); nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
if (nf_nat_sip && ct->status & IPS_NAT_MASK) { if (nf_nat_sip && ct->status & IPS_NAT_MASK) {
if (!nf_nat_sip(skb, ctinfo, ct, &dptr)) { if (!nf_nat_sip(skb, ctinfo, ct, &dptr, &datalen)) {
ret = NF_DROP; ret = NF_DROP;
goto out; goto out;
} }
...@@ -466,7 +468,7 @@ static int sip_help(struct sk_buff *skb, ...@@ -466,7 +468,7 @@ static int sip_help(struct sk_buff *skb,
goto out; goto out;
} }
ret = set_expected_rtp(skb, ct, ctinfo, &addr, ret = set_expected_rtp(skb, ct, ctinfo, &addr,
htons(port), dptr); htons(port), &dptr, &datalen);
} }
} }
out: out:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment