diff mbox

[U-Boot,v2,03/11] x86: Allow adding of IP checksums

Message ID 1420774255-702-4-git-send-email-sjg@chromium.org
State Superseded
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass Jan. 9, 2015, 3:30 a.m. UTC
Add a function to add IP checksums.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2:
- Refactor IP checksum patches

 arch/x86/cpu/ip_checksum.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
diff mbox

Patch

diff --git a/arch/x86/cpu/ip_checksum.c b/arch/x86/cpu/ip_checksum.c
index 1b529da..ddfc0d7 100644
--- a/arch/x86/cpu/ip_checksum.c
+++ b/arch/x86/cpu/ip_checksum.c
@@ -32,3 +32,24 @@  unsigned short compute_ip_checksum(const void *vptr, unsigned long nbytes)
 	sum += (sum >> 16);
 	return ~sum;
 }
+
+unsigned long add_ip_checksums(unsigned long offset, unsigned long sum,
+			       unsigned long new)
+{
+	unsigned long checksum;
+
+	sum = ~sum & 0xffff;
+	new = ~new & 0xffff;
+	if (offset & 1) {
+		/*
+		 * byte-swap the sum if it came from an odd offset; since the
+		 * computation is endian independant this works.
+		 */
+		new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
+	}
+	checksum = sum + new;
+	if (checksum > 0xffff)
+		checksum -= 0xffff;
+
+	return (~checksum) & 0xffff;
+}