Commit 503e81f6 authored by Simon Horman's avatar Simon Horman

ipvs: handle PARTIAL_CHECKSUM

Now that LVS can load balance locally generated traffic, packets may come
from the loopback device and thus may have a partial checksum.

The existing code allows for the case where there is no checksum at all for
TCP, however Herbert Xu has confirmed that this is not legal.
Signed-off-by: default avatarSimon Horman <horms@verge.net.au>
Acked-by: default avatarJulius Volz <juliusv@google.com>
parent 178f5e49
...@@ -134,12 +134,34 @@ tcp_fast_csum_update(int af, struct tcphdr *tcph, ...@@ -134,12 +134,34 @@ tcp_fast_csum_update(int af, struct tcphdr *tcph,
} }
static inline void
tcp_partial_csum_update(int af, struct tcphdr *tcph,
const union nf_inet_addr *oldip,
const union nf_inet_addr *newip,
__be16 oldlen, __be16 newlen)
{
#ifdef CONFIG_IP_VS_IPV6
if (af == AF_INET6)
tcph->check =
csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
ip_vs_check_diff2(oldlen, newlen,
~csum_unfold(tcph->check))));
else
#endif
tcph->check =
csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
ip_vs_check_diff2(oldlen, newlen,
~csum_unfold(tcph->check))));
}
static int static int
tcp_snat_handler(struct sk_buff *skb, tcp_snat_handler(struct sk_buff *skb,
struct ip_vs_protocol *pp, struct ip_vs_conn *cp) struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
{ {
struct tcphdr *tcph; struct tcphdr *tcph;
unsigned int tcphoff; unsigned int tcphoff;
int oldlen;
#ifdef CONFIG_IP_VS_IPV6 #ifdef CONFIG_IP_VS_IPV6
if (cp->af == AF_INET6) if (cp->af == AF_INET6)
...@@ -147,6 +169,7 @@ tcp_snat_handler(struct sk_buff *skb, ...@@ -147,6 +169,7 @@ tcp_snat_handler(struct sk_buff *skb,
else else
#endif #endif
tcphoff = ip_hdrlen(skb); tcphoff = ip_hdrlen(skb);
oldlen = skb->len - tcphoff;
/* csum_check requires unshared skb */ /* csum_check requires unshared skb */
if (!skb_make_writable(skb, tcphoff+sizeof(*tcph))) if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
...@@ -166,7 +189,11 @@ tcp_snat_handler(struct sk_buff *skb, ...@@ -166,7 +189,11 @@ tcp_snat_handler(struct sk_buff *skb,
tcph->source = cp->vport; tcph->source = cp->vport;
/* Adjust TCP checksums */ /* Adjust TCP checksums */
if (!cp->app && (tcph->check != 0)) { if (skb->ip_summed == CHECKSUM_PARTIAL) {
tcp_partial_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr,
htonl(oldlen),
htonl(skb->len - tcphoff));
} else if (!cp->app) {
/* Only port and addr are changed, do fast csum update */ /* Only port and addr are changed, do fast csum update */
tcp_fast_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr, tcp_fast_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr,
cp->dport, cp->vport); cp->dport, cp->vport);
...@@ -204,6 +231,7 @@ tcp_dnat_handler(struct sk_buff *skb, ...@@ -204,6 +231,7 @@ tcp_dnat_handler(struct sk_buff *skb,
{ {
struct tcphdr *tcph; struct tcphdr *tcph;
unsigned int tcphoff; unsigned int tcphoff;
int oldlen;
#ifdef CONFIG_IP_VS_IPV6 #ifdef CONFIG_IP_VS_IPV6
if (cp->af == AF_INET6) if (cp->af == AF_INET6)
...@@ -211,6 +239,7 @@ tcp_dnat_handler(struct sk_buff *skb, ...@@ -211,6 +239,7 @@ tcp_dnat_handler(struct sk_buff *skb,
else else
#endif #endif
tcphoff = ip_hdrlen(skb); tcphoff = ip_hdrlen(skb);
oldlen = skb->len - tcphoff;
/* csum_check requires unshared skb */ /* csum_check requires unshared skb */
if (!skb_make_writable(skb, tcphoff+sizeof(*tcph))) if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
...@@ -235,7 +264,11 @@ tcp_dnat_handler(struct sk_buff *skb, ...@@ -235,7 +264,11 @@ tcp_dnat_handler(struct sk_buff *skb,
/* /*
* Adjust TCP checksums * Adjust TCP checksums
*/ */
if (!cp->app && (tcph->check != 0)) { if (skb->ip_summed == CHECKSUM_PARTIAL) {
tcp_partial_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr,
htonl(oldlen),
htonl(skb->len - tcphoff));
} else if (!cp->app) {
/* Only port and addr are changed, do fast csum update */ /* Only port and addr are changed, do fast csum update */
tcp_fast_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr, tcp_fast_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr,
cp->vport, cp->dport); cp->vport, cp->dport);
......
...@@ -141,12 +141,34 @@ udp_fast_csum_update(int af, struct udphdr *uhdr, ...@@ -141,12 +141,34 @@ udp_fast_csum_update(int af, struct udphdr *uhdr,
uhdr->check = CSUM_MANGLED_0; uhdr->check = CSUM_MANGLED_0;
} }
static inline void
udp_partial_csum_update(int af, struct udphdr *uhdr,
const union nf_inet_addr *oldip,
const union nf_inet_addr *newip,
__be16 oldlen, __be16 newlen)
{
#ifdef CONFIG_IP_VS_IPV6
if (af == AF_INET6)
uhdr->check =
csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
ip_vs_check_diff2(oldlen, newlen,
~csum_unfold(uhdr->check))));
else
#endif
uhdr->check =
csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
ip_vs_check_diff2(oldlen, newlen,
~csum_unfold(uhdr->check))));
}
static int static int
udp_snat_handler(struct sk_buff *skb, udp_snat_handler(struct sk_buff *skb,
struct ip_vs_protocol *pp, struct ip_vs_conn *cp) struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
{ {
struct udphdr *udph; struct udphdr *udph;
unsigned int udphoff; unsigned int udphoff;
int oldlen;
#ifdef CONFIG_IP_VS_IPV6 #ifdef CONFIG_IP_VS_IPV6
if (cp->af == AF_INET6) if (cp->af == AF_INET6)
...@@ -154,6 +176,7 @@ udp_snat_handler(struct sk_buff *skb, ...@@ -154,6 +176,7 @@ udp_snat_handler(struct sk_buff *skb,
else else
#endif #endif
udphoff = ip_hdrlen(skb); udphoff = ip_hdrlen(skb);
oldlen = skb->len - udphoff;
/* csum_check requires unshared skb */ /* csum_check requires unshared skb */
if (!skb_make_writable(skb, udphoff+sizeof(*udph))) if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
...@@ -177,7 +200,11 @@ udp_snat_handler(struct sk_buff *skb, ...@@ -177,7 +200,11 @@ udp_snat_handler(struct sk_buff *skb,
/* /*
* Adjust UDP checksums * Adjust UDP checksums
*/ */
if (!cp->app && (udph->check != 0)) { if (skb->ip_summed == CHECKSUM_PARTIAL) {
udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
htonl(oldlen),
htonl(skb->len - udphoff));
} else if (!cp->app && (udph->check != 0)) {
/* Only port and addr are changed, do fast csum update */ /* Only port and addr are changed, do fast csum update */
udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr, udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
cp->dport, cp->vport); cp->dport, cp->vport);
...@@ -216,6 +243,7 @@ udp_dnat_handler(struct sk_buff *skb, ...@@ -216,6 +243,7 @@ udp_dnat_handler(struct sk_buff *skb,
{ {
struct udphdr *udph; struct udphdr *udph;
unsigned int udphoff; unsigned int udphoff;
int oldlen;
#ifdef CONFIG_IP_VS_IPV6 #ifdef CONFIG_IP_VS_IPV6
if (cp->af == AF_INET6) if (cp->af == AF_INET6)
...@@ -223,6 +251,7 @@ udp_dnat_handler(struct sk_buff *skb, ...@@ -223,6 +251,7 @@ udp_dnat_handler(struct sk_buff *skb,
else else
#endif #endif
udphoff = ip_hdrlen(skb); udphoff = ip_hdrlen(skb);
oldlen = skb->len - udphoff;
/* csum_check requires unshared skb */ /* csum_check requires unshared skb */
if (!skb_make_writable(skb, udphoff+sizeof(*udph))) if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
...@@ -247,7 +276,11 @@ udp_dnat_handler(struct sk_buff *skb, ...@@ -247,7 +276,11 @@ udp_dnat_handler(struct sk_buff *skb,
/* /*
* Adjust UDP checksums * Adjust UDP checksums
*/ */
if (!cp->app && (udph->check != 0)) { if (skb->ip_summed == CHECKSUM_PARTIAL) {
udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
htonl(oldlen),
htonl(skb->len - udphoff));
} else if (!cp->app && (udph->check != 0)) {
/* Only port and addr are changed, do fast csum update */ /* Only port and addr are changed, do fast csum update */
udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr, udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
cp->vport, cp->dport); cp->vport, cp->dport);
......
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