All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sebastien Tricaud <sebastien.tricaud@wengo.com>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@lists.netfilter.org,
	Luis Campo Giralte <lcg223@tid.es>, Eric Leblond <eric@inl.fr>
Subject: Re: Change Packet Payload
Date: Wed, 14 Feb 2007 10:39:53 +0100	[thread overview]
Message-ID: <45D2D8E9.3040009@wengo.com> (raw)
In-Reply-To: <45D23748.2090609@netfilter.org>

[-- Attachment #1: Type: text/plain, Size: 519 bytes --]

Hi Pablo,

I wrote this simple patch.
Any comment on it, naming conventions, better approach, etc.. appreciated

Pablo Neira Ayuso wrote:
>
> I would accept a patch to introduce a function that computes the
> checksum. I think that the library should provide as much helper
> functions as possible. We can group such helper functions by protocols
> in different files inside the libnetfilter_queue tree. I have already
> seen several post in the mailing list on how to retrieve data from the
> header and such.
>
>   


[-- Attachment #2: nfq-compute_cksum.1.patch --]
[-- Type: text/x-patch, Size: 4605 bytes --]

Index: include/libnetfilter_queue/Makefile.am
===================================================================
--- include/libnetfilter_queue/Makefile.am	(revision 6757)
+++ include/libnetfilter_queue/Makefile.am	(working copy)
@@ -1,3 +1,3 @@
 
-pkginclude_HEADERS = libnetfilter_queue.h libipq.h linux_nfnetlink_queue.h
+pkginclude_HEADERS = libnetfilter_queue.h libipq.h linux_nfnetlink_queue.h protocol_any_helper.h
 
Index: include/libnetfilter_queue/libnetfilter_queue.h
===================================================================
--- include/libnetfilter_queue/libnetfilter_queue.h	(revision 6757)
+++ include/libnetfilter_queue/libnetfilter_queue.h	(working copy)
@@ -14,9 +14,8 @@
 #define __LIBCTNETLINK_H
 
 #include <libnfnetlink/libnfnetlink.h>
-// #include <libnfnetlink/liunx_nfnetlink.h>
-
 #include <libnetfilter_queue/linux_nfnetlink_queue.h>
+#include <libnetfilter_queue/protocol_any_helper.h>
 
 struct nfq_handle;
 struct nfq_q_handle;
Index: include/libnetfilter_queue/protocol_any_helper.h
===================================================================
--- include/libnetfilter_queue/protocol_any_helper.h	(revision 0)
+++ include/libnetfilter_queue/protocol_any_helper.h	(revision 0)
@@ -0,0 +1,17 @@
+/* 
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 
+ *  as published by the Free Software Foundation
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+extern unsigned short nfq_compute_cksum(struct nfq_data *nfad);
+
Index: src/protocol_any_helper.c
===================================================================
--- src/protocol_any_helper.c	(revision 0)
+++ src/protocol_any_helper.c	(revision 0)
@@ -0,0 +1,53 @@
+/* 
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 
+ *  as published by the Free Software Foundation
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/** 
+ * nfq_compute_cksum - returns the checksum computed 
+ * @data: packet payload
+ * @len: packet lenght (header+payload)
+ *
+ * Helper function to compute packet checkum.
+ */
+/* (c) Richard W. Stevens */
+unsigned short nfq_compute_cksum(unsigned short *data, int len)
+{
+        int nleft             = len;
+        int sum               = 0;
+        unsigned short *w     = data;
+        unsigned short answer = 0;
+
+        /*
+	 * Our algorithm is simple, using a 32 bits 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 ) {
+                *(unsigned char *) (&answer) = *(unsigned char *) w;
+                sum += answer;
+        }
+
+        /* 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);  /* and carry */
+        answer =  ~sum;         /* truncate to 16 bits */
+
+        return answer;
+}
Index: src/Makefile.am
===================================================================
--- src/Makefile.am	(revision 6757)
+++ src/Makefile.am	(working copy)
@@ -10,7 +10,7 @@
 
 libnetfilter_queue_la_LDFLAGS = -Wc,-nostartfiles -lnfnetlink \
 				-version-info $(LIBVERSION)
-libnetfilter_queue_la_SOURCES = libnetfilter_queue.c 
+libnetfilter_queue_la_SOURCES = libnetfilter_queue.c protocol_any_helper.c
 
 libnetfilter_queue_libipq_la_LDFLAGS = -Wc,-nostartfiles \
 				-version-info 1:0:0

  reply	other threads:[~2007-02-14  9:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-09  9:10 Change Packet Payload Luis Campo Giralte
2007-02-09  9:31 ` Eric Leblond
2007-02-09  9:48   ` Henrik Nordstrom
2007-02-13 22:10   ` Pablo Neira Ayuso
2007-02-14  9:39     ` Sebastien Tricaud [this message]
2007-02-14  9:47     ` Sebastien Tricaud
2007-02-14  9:52       ` Patrick McHardy
2007-02-14  9:55         ` Sebastien Tricaud
2007-02-14 10:05           ` Sebastien Tricaud
2007-02-14 10:09             ` Patrick McHardy
2007-02-15  0:36               ` Pablo Neira Ayuso
2007-02-15 14:45                 ` Sebastien Tricaud
2007-02-16 13:11                   ` Sebastien Tricaud

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=45D2D8E9.3040009@wengo.com \
    --to=sebastien.tricaud@wengo.com \
    --cc=eric@inl.fr \
    --cc=lcg223@tid.es \
    --cc=netfilter-devel@lists.netfilter.org \
    --cc=pablo@netfilter.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.