Serenity Operating System
at portability 116 lines 4.0 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/String.h> 30#include <AK/Assertions.h> 31#include <AK/IPv4Address.h> 32#include <AK/NetworkOrdered.h> 33#include <AK/Types.h> 34 35namespace Kernel { 36 37enum class IPv4Protocol : u16 { 38 ICMP = 1, 39 TCP = 6, 40 UDP = 17, 41}; 42 43NetworkOrdered<u16> internet_checksum(const void*, size_t); 44 45class [[gnu::packed]] IPv4Packet 46{ 47public: 48 u8 version() const { return (m_version_and_ihl >> 4) & 0xf; } 49 void set_version(u8 version) { m_version_and_ihl = (m_version_and_ihl & 0x0f) | (version << 4); } 50 51 u8 internet_header_length() const { return m_version_and_ihl & 0xf; } 52 void set_internet_header_length(u8 ihl) { m_version_and_ihl = (m_version_and_ihl & 0xf0) | (ihl & 0x0f); } 53 54 u16 length() const { return m_length; } 55 void set_length(u16 length) { m_length = length; } 56 57 u16 ident() const { return m_ident; } 58 void set_ident(u16 ident) { m_ident = ident; } 59 60 u8 ttl() const { return m_ttl; } 61 void set_ttl(u8 ttl) { m_ttl = ttl; } 62 63 u8 protocol() const { return m_protocol; } 64 void set_protocol(u8 protocol) { m_protocol = protocol; } 65 66 u16 checksum() const { return m_checksum; } 67 void set_checksum(u16 checksum) { m_checksum = checksum; } 68 69 const IPv4Address& source() const { return m_source; } 70 void set_source(const IPv4Address& address) { m_source = address; } 71 72 const IPv4Address& destination() const { return m_destination; } 73 void set_destination(const IPv4Address& address) { m_destination = address; } 74 75 void* payload() { return this + 1; } 76 const void* payload() const { return this + 1; } 77 78 u16 payload_size() const { return m_length - sizeof(IPv4Packet); } 79 80 NetworkOrdered<u16> compute_checksum() const 81 { 82 ASSERT(!m_checksum); 83 return internet_checksum(this, sizeof(IPv4Packet)); 84 } 85 86private: 87 u8 m_version_and_ihl { 0 }; 88 u8 m_dscp_and_ecn { 0 }; 89 NetworkOrdered<u16> m_length; 90 NetworkOrdered<u16> m_ident; 91 NetworkOrdered<u16> m_flags_and_fragment; 92 u8 m_ttl { 0 }; 93 NetworkOrdered<u8> m_protocol; 94 NetworkOrdered<u16> m_checksum; 95 IPv4Address m_source; 96 IPv4Address m_destination; 97}; 98 99static_assert(sizeof(IPv4Packet) == 20); 100 101inline NetworkOrdered<u16> internet_checksum(const void* ptr, size_t count) 102{ 103 u32 checksum = 0; 104 auto* w = (const u16*)ptr; 105 while (count > 1) { 106 checksum += convert_between_host_and_network(*w++); 107 if (checksum & 0x80000000) 108 checksum = (checksum & 0xffff) | (checksum >> 16); 109 count -= 2; 110 } 111 while (checksum >> 16) 112 checksum = (checksum & 0xffff) + (checksum >> 16); 113 return ~checksum & 0xffff; 114} 115 116}