Commit 5b92b61f authored by Pablo Neira Ayuso's avatar Pablo Neira Ayuso Committed by Patrick McHardy

netfilter: nf_nat: better error handling of nf_ct_expect_related() in helpers

This patch improves the situation in which the expectation table is
full for conntrack NAT helpers. Basically, we give up if we don't
find a place in the table instead of looping over nf_ct_expect_related()
with a different port (we should only do this if it returns -EBUSY, for
-EMFILE or -ESHUTDOWN I think that it's better to skip this).
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: default avatarPatrick McHardy <kaber@trash.net>
parent 26c15cfd
...@@ -44,9 +44,16 @@ static unsigned int help(struct sk_buff *skb, ...@@ -44,9 +44,16 @@ static unsigned int help(struct sk_buff *skb,
/* Try to get same port: if not, try to change it. */ /* Try to get same port: if not, try to change it. */
for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) { for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
int ret;
exp->tuple.dst.u.tcp.port = htons(port); exp->tuple.dst.u.tcp.port = htons(port);
if (nf_ct_expect_related(exp) == 0) ret = nf_ct_expect_related(exp);
if (ret == 0)
break;
else if (ret != -EBUSY) {
port = 0;
break; break;
}
} }
if (port == 0) if (port == 0)
......
...@@ -79,9 +79,16 @@ static unsigned int nf_nat_ftp(struct sk_buff *skb, ...@@ -79,9 +79,16 @@ static unsigned int nf_nat_ftp(struct sk_buff *skb,
/* Try to get same port: if not, try to change it. */ /* Try to get same port: if not, try to change it. */
for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) { for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
int ret;
exp->tuple.dst.u.tcp.port = htons(port); exp->tuple.dst.u.tcp.port = htons(port);
if (nf_ct_expect_related(exp) == 0) ret = nf_ct_expect_related(exp);
if (ret == 0)
break;
else if (ret != -EBUSY) {
port = 0;
break; break;
}
} }
if (port == 0) if (port == 0)
......
...@@ -222,13 +222,24 @@ static int nat_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct, ...@@ -222,13 +222,24 @@ static int nat_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
/* Try to get a pair of ports. */ /* Try to get a pair of ports. */
for (nated_port = ntohs(rtp_exp->tuple.dst.u.udp.port); for (nated_port = ntohs(rtp_exp->tuple.dst.u.udp.port);
nated_port != 0; nated_port += 2) { nated_port != 0; nated_port += 2) {
int ret;
rtp_exp->tuple.dst.u.udp.port = htons(nated_port); rtp_exp->tuple.dst.u.udp.port = htons(nated_port);
if (nf_ct_expect_related(rtp_exp) == 0) { ret = nf_ct_expect_related(rtp_exp);
if (ret == 0) {
rtcp_exp->tuple.dst.u.udp.port = rtcp_exp->tuple.dst.u.udp.port =
htons(nated_port + 1); htons(nated_port + 1);
if (nf_ct_expect_related(rtcp_exp) == 0) ret = nf_ct_expect_related(rtcp_exp);
if (ret == 0)
break;
else if (ret != -EBUSY) {
nf_ct_unexpect_related(rtp_exp);
nated_port = 0;
break; break;
nf_ct_unexpect_related(rtp_exp); }
} else if (ret != -EBUSY) {
nated_port = 0;
break;
} }
} }
...@@ -284,9 +295,16 @@ static int nat_t120(struct sk_buff *skb, struct nf_conn *ct, ...@@ -284,9 +295,16 @@ static int nat_t120(struct sk_buff *skb, struct nf_conn *ct,
/* Try to get same port: if not, try to change it. */ /* Try to get same port: if not, try to change it. */
for (; nated_port != 0; nated_port++) { for (; nated_port != 0; nated_port++) {
int ret;
exp->tuple.dst.u.tcp.port = htons(nated_port); exp->tuple.dst.u.tcp.port = htons(nated_port);
if (nf_ct_expect_related(exp) == 0) ret = nf_ct_expect_related(exp);
if (ret == 0)
break;
else if (ret != -EBUSY) {
nated_port = 0;
break; break;
}
} }
if (nated_port == 0) { /* No port available */ if (nated_port == 0) { /* No port available */
...@@ -334,9 +352,16 @@ static int nat_h245(struct sk_buff *skb, struct nf_conn *ct, ...@@ -334,9 +352,16 @@ static int nat_h245(struct sk_buff *skb, struct nf_conn *ct,
/* Try to get same port: if not, try to change it. */ /* Try to get same port: if not, try to change it. */
for (; nated_port != 0; nated_port++) { for (; nated_port != 0; nated_port++) {
int ret;
exp->tuple.dst.u.tcp.port = htons(nated_port); exp->tuple.dst.u.tcp.port = htons(nated_port);
if (nf_ct_expect_related(exp) == 0) ret = nf_ct_expect_related(exp);
if (ret == 0)
break; break;
else if (ret != -EBUSY) {
nated_port = 0;
break;
}
} }
if (nated_port == 0) { /* No port available */ if (nated_port == 0) { /* No port available */
...@@ -418,9 +443,16 @@ static int nat_q931(struct sk_buff *skb, struct nf_conn *ct, ...@@ -418,9 +443,16 @@ static int nat_q931(struct sk_buff *skb, struct nf_conn *ct,
/* Try to get same port: if not, try to change it. */ /* Try to get same port: if not, try to change it. */
for (; nated_port != 0; nated_port++) { for (; nated_port != 0; nated_port++) {
int ret;
exp->tuple.dst.u.tcp.port = htons(nated_port); exp->tuple.dst.u.tcp.port = htons(nated_port);
if (nf_ct_expect_related(exp) == 0) ret = nf_ct_expect_related(exp);
if (ret == 0)
break;
else if (ret != -EBUSY) {
nated_port = 0;
break; break;
}
} }
if (nated_port == 0) { /* No port available */ if (nated_port == 0) { /* No port available */
...@@ -500,9 +532,16 @@ static int nat_callforwarding(struct sk_buff *skb, struct nf_conn *ct, ...@@ -500,9 +532,16 @@ static int nat_callforwarding(struct sk_buff *skb, struct nf_conn *ct,
/* Try to get same port: if not, try to change it. */ /* Try to get same port: if not, try to change it. */
for (nated_port = ntohs(port); nated_port != 0; nated_port++) { for (nated_port = ntohs(port); nated_port != 0; nated_port++) {
int ret;
exp->tuple.dst.u.tcp.port = htons(nated_port); exp->tuple.dst.u.tcp.port = htons(nated_port);
if (nf_ct_expect_related(exp) == 0) ret = nf_ct_expect_related(exp);
if (ret == 0)
break; break;
else if (ret != -EBUSY) {
nated_port = 0;
break;
}
} }
if (nated_port == 0) { /* No port available */ if (nated_port == 0) { /* No port available */
......
...@@ -45,9 +45,16 @@ static unsigned int help(struct sk_buff *skb, ...@@ -45,9 +45,16 @@ static unsigned int help(struct sk_buff *skb,
/* Try to get same port: if not, try to change it. */ /* Try to get same port: if not, try to change it. */
for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) { for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
int ret;
exp->tuple.dst.u.tcp.port = htons(port); exp->tuple.dst.u.tcp.port = htons(port);
if (nf_ct_expect_related(exp) == 0) ret = nf_ct_expect_related(exp);
if (ret == 0)
break;
else if (ret != -EBUSY) {
port = 0;
break; break;
}
} }
if (port == 0) if (port == 0)
......
...@@ -307,9 +307,16 @@ static unsigned int ip_nat_sip_expect(struct sk_buff *skb, unsigned int dataoff, ...@@ -307,9 +307,16 @@ static unsigned int ip_nat_sip_expect(struct sk_buff *skb, unsigned int dataoff,
exp->expectfn = ip_nat_sip_expected; exp->expectfn = ip_nat_sip_expected;
for (; port != 0; port++) { for (; port != 0; port++) {
int ret;
exp->tuple.dst.u.udp.port = htons(port); exp->tuple.dst.u.udp.port = htons(port);
if (nf_ct_expect_related(exp) == 0) ret = nf_ct_expect_related(exp);
if (ret == 0)
break;
else if (ret != -EBUSY) {
port = 0;
break; break;
}
} }
if (port == 0) if (port == 0)
...@@ -480,13 +487,25 @@ static unsigned int ip_nat_sdp_media(struct sk_buff *skb, unsigned int dataoff, ...@@ -480,13 +487,25 @@ static unsigned int ip_nat_sdp_media(struct sk_buff *skb, unsigned int dataoff,
/* Try to get same pair of ports: if not, try to change them. */ /* Try to get same pair of ports: if not, try to change them. */
for (port = ntohs(rtp_exp->tuple.dst.u.udp.port); for (port = ntohs(rtp_exp->tuple.dst.u.udp.port);
port != 0; port += 2) { port != 0; port += 2) {
int ret;
rtp_exp->tuple.dst.u.udp.port = htons(port); rtp_exp->tuple.dst.u.udp.port = htons(port);
if (nf_ct_expect_related(rtp_exp) != 0) ret = nf_ct_expect_related(rtp_exp);
if (ret == -EBUSY)
continue; continue;
else if (ret < 0) {
port = 0;
break;
}
rtcp_exp->tuple.dst.u.udp.port = htons(port + 1); rtcp_exp->tuple.dst.u.udp.port = htons(port + 1);
if (nf_ct_expect_related(rtcp_exp) == 0) ret = nf_ct_expect_related(rtcp_exp);
if (ret == 0)
break; break;
nf_ct_unexpect_related(rtp_exp); else if (ret != -EBUSY) {
nf_ct_unexpect_related(rtp_exp);
port = 0;
break;
}
} }
if (port == 0) if (port == 0)
......
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