> From: Baruch Siach <[email protected]>
>
> Use a single IP checksum routine for ping, traceroute and udhcp.
>
> Signed-off-by: Baruch Siach <[email protected]>
> ---
>
> Changes from v1:
>    Fix inet_cksum() with odd length on big endian systems
>    Remove declaration of removed udhcp_checksum()

This looks like it is from quagga. I cleaned that up/optimized it
some years ago into this(you might want to fix the types):

int                     /* return checksum in low-order 16 bits */
in_cksum(void *parg, int nbytes)
{
        u_short *ptr = parg;
        register long           sum;            /* assumes long == 32 bits */
        register u_short        answer;         /* assumes u_short == 16 bits */
        register int count;
        /*
         * Our algorithm is simple, using a 32-bit accumulator (sum),
         * we add sequential 16-bit words to it, and at the end, fold back
         * all the carry bits from the top 16 bits into the lower 16 bits.
         */
        sum = 0;
        count = nbytes >> 1; /* div by 2 */
        for(ptr--; count; --count)
          sum += *++ptr;

        if (nbytes & 1) /* Odd */
          sum += *(u_char *)(++ptr);   /* one byte only */

        /*
         * Add back carry outs from top 16 bits to low 16 bits.
         */

        sum  = (sum >> 16) + (sum & 0xffff);    /* add high-16 to low-16 */
        sum += (sum >> 16);                     /* add carry */
        answer = ~sum;          /* ones-complement, then truncate to 16 bits */
        return(answer);
}
>
>  include/libbb.h           |    1 +
>  libbb/Kbuild.src          |    6 ++++++
>  libbb/inet_cksum.c        |   41 +++++++++++++++++++++++++++++++++++++++++
>  networking/ping.c         |   31 +++----------------------------
>  networking/traceroute.c   |   35 +----------------------------------
>  networking/udhcp/common.h |    2 --
>  networking/udhcp/dhcpc.c  |    4 ++--
>  networking/udhcp/packet.c |   34 +++-------------------------------
>  8 files changed, 57 insertions(+), 97 deletions(-)
>  create mode 100644 libbb/inet_cksum.c
>
> diff --git a/include/libbb.h b/include/libbb.h
> index 1ca4489..f60f427 100644
> --- a/include/libbb.h
> +++ b/include/libbb.h
> @@ -640,6 +640,7 @@ ssize_t recv_from_to(int fd, void *buf, size_t len, int 
> flags,
>        struct sockaddr *to,
>        socklen_t sa_size) FAST_FUNC;
>
> +uint16_t inet_cksum(uint16_t *addr, int len) FAST_FUNC;
>
>  char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC;
>  char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC;
> diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
> index 875d024..335b341 100644
> --- a/libbb/Kbuild.src
> +++ b/libbb/Kbuild.src
> @@ -166,6 +166,12 @@ lib-$(CONFIG_IOSTAT) += get_cpu_count.o
>  lib-$(CONFIG_MPSTAT) += get_cpu_count.o
>  lib-$(CONFIG_POWERTOP) += get_cpu_count.o
>
> +lib-$(CONFIG_PING) += inet_cksum.o
> +lib-$(CONFIG_TRACEROUTE) += inet_cksum.o
> +lib-$(CONFIG_TRACEROUTE6) += inet_cksum.o
> +lib-$(CONFIG_UDHCPC) += inet_cksum.o
> +lib-$(CONFIG_UDHCPD) += inet_cksum.o
> +
>  # We shouldn't build xregcomp.c if we don't need it - this ensures we don't
>  # require regex.h to be in the include dir even if we don't need it thereby
>  # allowing us to build busybox even if uclibc regex support is disabled.
> diff --git a/libbb/inet_cksum.c b/libbb/inet_cksum.c
> new file mode 100644
> index 0000000..79b7f2c
> --- /dev/null
> +++ b/libbb/inet_cksum.c
> @@ -0,0 +1,41 @@
> +/*
> + * Checksum routine for Internet Protocol family headers (C Version)
> + *
> + * Licensed under GPLv2, see file LICENSE in this source tree.
> + */
> +
> +#include "libbb.h"
> +
> +uint16_t FAST_FUNC inet_cksum(uint16_t *addr, int len)
> +{
> +   int nleft = len;
> +   uint16_t *w = addr;
> +   uint16_t answer;
> +   int sum = 0;
> +
> +   /*
> +    * Our algorithm is simple, using a 32 bit accumulator (sum),
> +    * we add sequential 16 bit words to it, and at the end, fold
> +    * back all the carry bits from the top 16 bits into the lower
> +    * 16 bits.
> +    */
> +   while (nleft > 1) {
> +      sum += *w++;
> +      nleft -= 2;
> +   }
> +
> +   /* mop up an odd byte, if necessary */
> +   if (nleft == 1) {
> +      /* Make sure that the left-over byte is added correctly both
> +       * with little and big endian hosts */
> +      uint16_t tmp = 0;
> +      *(uint8_t*)&tmp = *(uint8_t*)w;
> +      sum += tmp;
> +   }
> +
> +   /* add back carry outs from top 16 bits to low 16 bits */
> +   sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
> +   sum += (sum >> 16);                     /* add carry */
> +   answer = ~sum;                          /* truncate to 16 bits */
> +   return answer;
> +}
> diff --git a/networking/ping.c b/networking/ping.c
> index efd4f21..a1fd9df 100644
> --- a/networking/ping.c
> +++ b/networking/ping.c
> @@ -149,31 +149,6 @@ enum {
>     PINGINTERVAL = 1, /* 1 second */
>  };
>
> -/* Common routines */
> -
> -static int in_cksum(unsigned short *buf, int sz)
> -{
> -   int nleft = sz;
> -   int sum = 0;
> -   unsigned short *w = buf;
> -   unsigned short ans = 0;
> -
> -   while (nleft > 1) {
> -      sum += *w++;
> -      nleft -= 2;
> -   }
> -
> -   if (nleft == 1) {
> -      *(unsigned char *) (&ans) = *(unsigned char *) w;
> -      sum += ans;
> -   }
> -
> -   sum = (sum >> 16) + (sum & 0xFFFF);
> -   sum += (sum >> 16);
> -   ans = ~sum;
> -   return ans;
> -}
> -
>  #if !ENABLE_FEATURE_FANCY_PING
>
>  /* Simple version */
> @@ -201,7 +176,7 @@ static void ping4(len_and_sockaddr *lsa)
>     pkt = (struct icmp *) G.packet;
>     memset(pkt, 0, sizeof(G.packet));
>     pkt->icmp_type = ICMP_ECHO;
> -   pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(G.packet));
> +   pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet));
>
>     xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, 
> lsa->len);
>
> @@ -493,7 +468,7 @@ static void sendping4(int junk UNUSED_PARAM)
>        /* No hton: we'll read it back on the same machine */
>        *(uint32_t*)&pkt->icmp_dun = monotonic_us();
>
> -   pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
> +   pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, datalen + ICMP_MINLEN);
>
>     sendping_tail(sendping4, ICMP_MINLEN);
>  }
> @@ -512,7 +487,7 @@ static void sendping6(int junk UNUSED_PARAM)
>     /*if (datalen >= 4)*/
>        *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
>
> -   //TODO? pkt->icmp_cksum = in_cksum(...);
> +   //TODO? pkt->icmp_cksum = inet_cksum(...);
>
>     sendping_tail(sendping6, sizeof(struct icmp6_hdr));
>  }
> diff --git a/networking/traceroute.c b/networking/traceroute.c
> index c321035..d197e54 100644
> --- a/networking/traceroute.c
> +++ b/networking/traceroute.c
> @@ -418,39 +418,6 @@ wait_for_reply(len_and_sockaddr *from_lsa, struct 
> sockaddr *to, unsigned *timest
>     return read_len;
>  }
>
> -/*
> - * Checksum routine for Internet Protocol family headers (C Version)
> - */
> -static uint16_t
> -in_cksum(uint16_t *addr, int len)
> -{
> -   int nleft = len;
> -   uint16_t *w = addr;
> -   uint16_t answer;
> -   int sum = 0;
> -
> -   /*
> -    * Our algorithm is simple, using a 32 bit accumulator (sum),
> -    * we add sequential 16 bit words to it, and at the end, fold
> -    * back all the carry bits from the top 16 bits into the lower
> -    * 16 bits.
> -    */
> -   while (nleft > 1) {
> -      sum += *w++;
> -      nleft -= 2;
> -   }
> -
> -   /* mop up an odd byte, if necessary */
> -   if (nleft == 1)
> -      sum += *(unsigned char *)w;
> -
> -   /* add back carry outs from top 16 bits to low 16 bits */
> -   sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
> -   sum += (sum >> 16);                     /* add carry */
> -   answer = ~sum;                          /* truncate to 16 bits */
> -   return answer;
> -}
> -
>  static void
>  send_probe(int seq, int ttl)
>  {
> @@ -477,7 +444,7 @@ send_probe(int seq, int ttl)
>
>           /* Always calculate checksum for icmp packets */
>           outicmp->icmp_cksum = 0;
> -         outicmp->icmp_cksum = in_cksum((uint16_t *)outicmp,
> +         outicmp->icmp_cksum = inet_cksum((uint16_t *)outicmp,
>                    packlen - (sizeof(*outip) + optlen));
>           if (outicmp->icmp_cksum == 0)
>              outicmp->icmp_cksum = 0xffff;
> diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h
> index ad6991c..80d97e8 100644
> --- a/networking/udhcp/common.h
> +++ b/networking/udhcp/common.h
> @@ -277,8 +277,6 @@ int FAST_FUNC udhcp_str2nip(const char *str, void *arg);
>  /* 2nd param is "struct option_set**" */
>  int FAST_FUNC udhcp_str2optset(const char *str, void *arg);
>
> -uint16_t udhcp_checksum(void *addr, int count) FAST_FUNC;
> -
>  void udhcp_init_header(struct dhcp_packet *packet, char type) FAST_FUNC;
>
>  int udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) FAST_FUNC;
> diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
> index 4d755e6..3be09f4 100644
> --- a/networking/udhcp/dhcpc.c
> +++ b/networking/udhcp/dhcpc.c
> @@ -739,7 +739,7 @@ static NOINLINE int udhcp_recv_raw_packet(struct 
> dhcp_packet *dhcp_pkt, int fd)
>     /* verify IP checksum */
>     check = packet.ip.check;
>     packet.ip.check = 0;
> -   if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
> +   if (check != inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip))) {
>        log1("Bad IP header checksum, ignoring");
>        return -2;
>     }
> @@ -750,7 +750,7 @@ static NOINLINE int udhcp_recv_raw_packet(struct 
> dhcp_packet *dhcp_pkt, int fd)
>     packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
>     check = packet.udp.check;
>     packet.udp.check = 0;
> -   if (check && check != udhcp_checksum(&packet, bytes)) {
> +   if (check && check != inet_cksum((uint16_t *)&packet, bytes)) {
>        log1("Packet with bad UDP checksum received, ignoring");
>        return -2;
>     }
> diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c
> index 66b42c5..4d5ff06 100644
> --- a/networking/udhcp/packet.c
> +++ b/networking/udhcp/packet.c
> @@ -129,35 +129,6 @@ int FAST_FUNC udhcp_recv_kernel_packet(struct 
> dhcp_packet *packet, int fd)
>     return bytes;
>  }
>
> -uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
> -{
> -   /* Compute Internet Checksum for "count" bytes
> -    * beginning at location "addr".
> -    */
> -   int32_t sum = 0;
> -   uint16_t *source = (uint16_t *) addr;
> -
> -   while (count > 1)  {
> -      /*  This is the inner loop */
> -      sum += *source++;
> -      count -= 2;
> -   }
> -
> -   /*  Add left-over byte, if any */
> -   if (count > 0) {
> -      /* Make sure that the left-over byte is added correctly both
> -       * with little and big endian hosts */
> -      uint16_t tmp = 0;
> -      *(uint8_t*)&tmp = *(uint8_t*)source;
> -      sum += tmp;
> -   }
> -   /*  Fold 32-bit sum to 16 bits */
> -   while (sum >> 16)
> -      sum = (sum & 0xffff) + (sum >> 16);
> -
> -   return ~sum;
> -}
> -
>  /* Construct a ip/udp header for a packet, send packet */
>  int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
>        uint32_t source_nip, int source_port,
> @@ -212,13 +183,14 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet 
> *dhcp_pkt,
>     packet.udp.len = htons(UDP_DHCP_SIZE - padding);
>     /* for UDP checksumming, ip.len is set to UDP packet len */
>     packet.ip.tot_len = packet.udp.len;
> -   packet.udp.check = udhcp_checksum(&packet, IP_UDP_DHCP_SIZE - padding);
> +   packet.udp.check = inet_cksum((uint16_t *)&packet,
> +         IP_UDP_DHCP_SIZE - padding);
>     /* but for sending, it is set to IP packet len */
>     packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
>     packet.ip.ihl = sizeof(packet.ip) >> 2;
>     packet.ip.version = IPVERSION;
>     packet.ip.ttl = IPDEFTTL;
> -   packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
> +   packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
>
>     udhcp_dump_packet(dhcp_pkt);
>     result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
> --
> 1.7.5.4
>
> _______________________________________________
> busybox mailing list
> [email protected]
> http://lists.busybox.net/mailman/listinfo/busybox

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to