[NETFILTER]: replace open coded checksum updates
authorPatrick McHardy <kaber@trash.net>
Tue, 22 Aug 2006 07:33:09 +0000 (00:33 -0700)
committerDavid S. Miller <davem@sunset.davemloft.net>
Fri, 22 Sep 2006 21:55:29 +0000 (14:55 -0700)
Replace open coded checksum update by nf_csum_update calls and clean up
the surrounding code a bit.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/ipv4/netfilter/ipt_ECN.c
net/ipv4/netfilter/ipt_TOS.c
net/ipv4/netfilter/ipt_TTL.c

index 35916c74fe4eb93b6ce54629ef7e269c409cbbb1..7e30e6d2b5da3339b17617c812ea781fa0c2ff38 100644 (file)
@@ -27,22 +27,18 @@ MODULE_DESCRIPTION("iptables ECN modification module");
 static inline int
 set_ect_ip(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
 {
-       if (((*pskb)->nh.iph->tos & IPT_ECN_IP_MASK)
-           != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
-               u_int16_t diffs[2];
+       struct iphdr *iph = (*pskb)->nh.iph;
+       u_int16_t oldtos;
 
+       if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
                if (!skb_make_writable(pskb, sizeof(struct iphdr)))
                        return 0;
-
-               diffs[0] = htons((*pskb)->nh.iph->tos) ^ 0xFFFF;
-               (*pskb)->nh.iph->tos &= ~IPT_ECN_IP_MASK;
-               (*pskb)->nh.iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
-               diffs[1] = htons((*pskb)->nh.iph->tos);
-               (*pskb)->nh.iph->check
-                       = csum_fold(csum_partial((char *)diffs,
-                                                sizeof(diffs),
-                                                (*pskb)->nh.iph->check
-                                                ^0xFFFF));
+               iph = (*pskb)->nh.iph;
+               oldtos = iph->tos;
+               iph->tos &= ~IPT_ECN_IP_MASK;
+               iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
+               iph->check = nf_csum_update(oldtos ^ 0xFFFF, iph->tos,
+                                           iph->check);
        } 
        return 1;
 }
index 1c7a5ca399b3225d479aab819341e8847b2d37e2..52e9d705d48ec891dd9feb846fa8e1e55de129ac 100644 (file)
@@ -30,23 +30,17 @@ target(struct sk_buff **pskb,
        void *userinfo)
 {
        const struct ipt_tos_target_info *tosinfo = targinfo;
+       struct iphdr *iph = (*pskb)->nh.iph;
+       u_int16_t oldtos;
 
-       if (((*pskb)->nh.iph->tos & IPTOS_TOS_MASK) != tosinfo->tos) {
-               u_int16_t diffs[2];
-
+       if ((iph->tos & IPTOS_TOS_MASK) != tosinfo->tos) {
                if (!skb_make_writable(pskb, sizeof(struct iphdr)))
                        return NF_DROP;
-
-               diffs[0] = htons((*pskb)->nh.iph->tos) ^ 0xFFFF;
-               (*pskb)->nh.iph->tos
-                       = ((*pskb)->nh.iph->tos & IPTOS_PREC_MASK)
-                       | tosinfo->tos;
-               diffs[1] = htons((*pskb)->nh.iph->tos);
-               (*pskb)->nh.iph->check
-                       = csum_fold(csum_partial((char *)diffs,
-                                                sizeof(diffs),
-                                                (*pskb)->nh.iph->check
-                                                ^0xFFFF));
+               iph = (*pskb)->nh.iph;
+               oldtos = iph->tos;
+               iph->tos = (iph->tos & IPTOS_PREC_MASK) | tosinfo->tos;
+               iph->check = nf_csum_update(oldtos ^ 0xFFFF, iph->tos,
+                                           iph->check);
        }
        return IPT_CONTINUE;
 }
index f48892ae0be5c8bbd094b7296929ed45f94df4bd..2afb2a8aa8c5c31ee2ae4541bf2b825de5760e1c 100644 (file)
@@ -27,7 +27,6 @@ ipt_ttl_target(struct sk_buff **pskb,
 {
        struct iphdr *iph;
        const struct ipt_TTL_info *info = targinfo;
-       u_int16_t diffs[2];
        int new_ttl;
 
        if (!skb_make_writable(pskb, (*pskb)->len))
@@ -55,12 +54,10 @@ ipt_ttl_target(struct sk_buff **pskb,
        }
 
        if (new_ttl != iph->ttl) {
-               diffs[0] = htons(((unsigned)iph->ttl) << 8) ^ 0xFFFF;
+               iph->check = nf_csum_update((iph->ttl << 8) ^ 0xFFFF,
+                                           new_ttl << 8,
+                                           iph->check);
                iph->ttl = new_ttl;
-               diffs[1] = htons(((unsigned)iph->ttl) << 8);
-               iph->check = csum_fold(csum_partial((char *)diffs,
-                                                   sizeof(diffs),
-                                                   iph->check^0xFFFF));
        }
 
        return IPT_CONTINUE;