Serenity Operating System
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/Assertions.h>
30#include <AK/IPv4Address.h>
31#include <AK/NetworkOrdered.h>
32#include <AK/String.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
43enum class IPv4PacketFlags : u16 {
44 DontFragment = 0x4000,
45 MoreFragments = 0x2000,
46};
47
48NetworkOrdered<u16> internet_checksum(const void*, size_t);
49
50class [[gnu::packed]] IPv4Packet
51{
52public:
53 u8 version() const { return (m_version_and_ihl >> 4) & 0xf; }
54 void set_version(u8 version) { m_version_and_ihl = (m_version_and_ihl & 0x0f) | (version << 4); }
55
56 u8 internet_header_length() const { return m_version_and_ihl & 0xf; }
57 void set_internet_header_length(u8 ihl) { m_version_and_ihl = (m_version_and_ihl & 0xf0) | (ihl & 0x0f); }
58
59 u16 length() const { return m_length; }
60 void set_length(u16 length) { m_length = length; }
61
62 u16 ident() const { return m_ident; }
63 void set_ident(u16 ident) { m_ident = ident; }
64
65 u8 ttl() const { return m_ttl; }
66 void set_ttl(u8 ttl) { m_ttl = ttl; }
67
68 u8 protocol() const { return m_protocol; }
69 void set_protocol(u8 protocol) { m_protocol = protocol; }
70
71 u16 checksum() const { return m_checksum; }
72 void set_checksum(u16 checksum) { m_checksum = checksum; }
73
74 const IPv4Address& source() const { return m_source; }
75 void set_source(const IPv4Address& address) { m_source = address; }
76
77 const IPv4Address& destination() const { return m_destination; }
78 void set_destination(const IPv4Address& address) { m_destination = address; }
79
80 void* payload() { return this + 1; }
81 const void* payload() const { return this + 1; }
82
83 u16 flags_and_fragment() const { return m_flags_and_fragment; }
84 u16 fragment_offset() const { return ((u16)m_flags_and_fragment & 0x2fff); }
85 u16 flags() const { return (((u16)m_flags_and_fragment) & (((u16)IPv4PacketFlags::MoreFragments) | ((u16)IPv4PacketFlags::DontFragment))); }
86
87 void set_has_more_fragments(bool more_fragments)
88 {
89 if (more_fragments)
90 m_flags_and_fragment = (u16)m_flags_and_fragment | ((u16)IPv4PacketFlags::MoreFragments);
91 else
92 m_flags_and_fragment = (u16)m_flags_and_fragment & ((u16)IPv4PacketFlags::MoreFragments);
93 }
94 void set_fragment_offset(u16 offset)
95 {
96 m_flags_and_fragment = flags() | (offset & 0x2fff);
97 }
98
99 bool is_a_fragment() const
100 {
101 // either has More-Fragments set, or has a fragment offset
102 return (((u16)m_flags_and_fragment) & ((u16)IPv4PacketFlags::MoreFragments)) || ((u16)m_flags_and_fragment & 0x2fff);
103 }
104
105 u16 payload_size() const { return m_length - sizeof(IPv4Packet); }
106
107 NetworkOrdered<u16> compute_checksum() const
108 {
109 ASSERT(!m_checksum);
110 return internet_checksum(this, sizeof(IPv4Packet));
111 }
112
113private:
114 u8 m_version_and_ihl { 0 };
115 u8 m_dscp_and_ecn { 0 };
116 NetworkOrdered<u16> m_length;
117 NetworkOrdered<u16> m_ident;
118 NetworkOrdered<u16> m_flags_and_fragment;
119 u8 m_ttl { 0 };
120 NetworkOrdered<u8> m_protocol;
121 NetworkOrdered<u16> m_checksum;
122 IPv4Address m_source;
123 IPv4Address m_destination;
124};
125
126static_assert(sizeof(IPv4Packet) == 20);
127const LogStream& operator<<(const LogStream& stream, const IPv4Packet& packet);
128
129inline NetworkOrdered<u16> internet_checksum(const void* ptr, size_t count)
130{
131 u32 checksum = 0;
132 auto* w = (const u16*)ptr;
133 while (count > 1) {
134 checksum += convert_between_host_and_network(*w++);
135 if (checksum & 0x80000000)
136 checksum = (checksum & 0xffff) | (checksum >> 16);
137 count -= 2;
138 }
139 while (checksum >> 16)
140 checksum = (checksum & 0xffff) + (checksum >> 16);
141 return ~checksum & 0xffff;
142}
143
144}