diff options
| author | Andrey Konovalov <andreyknvl@google.com> | 2017-04-27 20:31:00 +0200 |
|---|---|---|
| committer | Andrey Konovalov <andreyknvl@google.com> | 2017-05-12 15:47:59 +0200 |
| commit | ac0c70f74a5badbebec721c2be0602ea98c0437b (patch) | |
| tree | 69278d03cdaae547c1c87a84deb44969081837bd /executor/common.h | |
| parent | b2dbb4f4d10f436088ba8a1d2d18437911a83887 (diff) | |
prog, executor: move checksum computation to executor
This commit moves checksum computation to executor. This will allow to embed
dynamically generated values (like TCP sequence numbers) into packets.
Diffstat (limited to 'executor/common.h')
| -rw-r--r-- | executor/common.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/executor/common.h b/executor/common.h index 4983802f2..eb56c8ec1 100644 --- a/executor/common.h +++ b/executor/common.h @@ -298,6 +298,36 @@ static uintptr_t syz_emit_ethernet(uintptr_t a0, uintptr_t a1) } #endif // __NR_syz_emit_ethernet +struct csum_inet { + uint32_t acc; +}; + +void csum_inet_init(struct csum_inet* csum) +{ + csum->acc = 0; +} + +void csum_inet_update(struct csum_inet* csum, const uint8_t* data, size_t length) +{ + if (length == 0) + return; + + size_t i; + for (i = 0; i < length - 1; i += 2) + csum->acc += *(uint16_t*)&data[i]; + + if (length & 1) + csum->acc += (uint16_t)data[length - 1]; + + while (csum->acc > 0xffff) + csum->acc = (csum->acc & 0xffff) + (csum->acc >> 16); +} + +uint16_t csum_inet_digest(struct csum_inet* csum) +{ + return ~csum->acc; +} + #ifdef __NR_syz_open_dev static uintptr_t syz_open_dev(uintptr_t a0, uintptr_t a1, uintptr_t a2) { |
