netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Justin Yaple <yaplej@gmail.com>
To: Morgon.J.Kanter@dartmouth.edu
Cc: netfilter-devel@vger.kernel.org
Subject: Re: Recalculate checksums in netfilter queue
Date: Thu, 22 Apr 2010 13:19:56 -0700	[thread overview]
Message-ID: <l2z8363055a1004221319q17cb25f8k8734736374747f99@mail.gmail.com> (raw)
In-Reply-To: <20100422084313.jk0ntlfyss444g0o@webmail.dartmouth.edu>

> The RFC for IP contains C code that you can literally copy and paste for
> this task.
>
> -- Morgon

Thanks.  I finally have both my tcp checksum, and ip checksum
functions working.  I found where I was doing some calculations in
reverse order than the example given in the RFC.  Would there be any
reason not to add checksum functions to libnetfilter_queue?

unsigned short tcp_sum_calc(unsigned short len_tcp, unsigned short
*src_addr, unsigned short *dest_addr, unsigned short *buff)
{
	unsigned short prot_tcp = 6;
	long sum = 0;
	int i = 0;
	
	/* Check if the tcp length is even or odd.  Add padding if odd. */
	if((len_tcp % 2) == 1){
		buff[len_tcp] = 0;  // Empty space in the ip buffer should be 0 anyway.
      	len_tcp += 1; // increase length to make even.
	}
	
	/* add the pseudo header */	
	sum += ntohs(src_addr[0]);
	sum += ntohs(src_addr[1]);
	sum += ntohs(dest_addr[0]);
	sum += ntohs(dest_addr[1]);
	sum += len_tcp; // already in host format.
	sum += prot_tcp; // already in host format.

	/*
	 * calculate the checksum for the tcp header and payload
	 * len_tcp represents number of 8-bit bytes,
	 * we are working with 16-bit words so divide len_tcp by 2.
	 */
	for(i=0;i<(len_tcp/2);i++){
		sum += ntohs(buff[i]);
	}

	// keep only the last 16 bits of the 32 bit calculated sum and add the carries
	while (sum >> 16){
		sum = (sum & 0xFFFF) + (sum >> 16);
	}
	
	// Take the bitwise complement of sum
	sum = ~sum;

	return htons(((unsigned short) sum));
}

unsigned short ip_sum_calc(unsigned short len_ip_header, unsigned short *buff){
	long sum = 0;
	int i = 0;
	
	for (i=0;i<len_ip_header/2;i++){
		sum += ntohs(buff[i]);
	}
	
	while (sum >> 16){
		sum = (sum & 0xFFFF) + (sum >> 16);
	}
	
	sum = ~sum;
	
	return htons(((unsigned short) sum));
}

  reply	other threads:[~2010-04-22 20:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-21  1:02 Recalculate checksums in netfilter queue Justin Yaple
2010-04-21  2:02 ` Bruno Moreira Guedes
2010-04-22  3:15   ` Justin Yaple
2010-04-22 12:43     ` Morgon.J.Kanter
2010-04-22 20:19       ` Justin Yaple [this message]
     [not found]         ` <z2o3fdd6ce01004221432yec6c907bz4124144d403be1b4@mail.gmail.com>
2010-04-22 22:12           ` Justin Yaple
2010-04-22 19:37     ` James King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=l2z8363055a1004221319q17cb25f8k8734736374747f99@mail.gmail.com \
    --to=yaplej@gmail.com \
    --cc=Morgon.J.Kanter@dartmouth.edu \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).