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#include <AK/HashTable.h>
28#include <AK/StringBuilder.h>
29#include <Kernel/Heap/kmalloc.h>
30#include <Kernel/Lock.h>
31#include <Kernel/Net/EtherType.h>
32#include <Kernel/Net/EthernetFrameHeader.h>
33#include <Kernel/Net/LoopbackAdapter.h>
34#include <Kernel/Net/NetworkAdapter.h>
35#include <Kernel/Random.h>
36#include <LibBareMetal/StdLib.h>
37
38namespace Kernel {
39
40static Lockable<HashTable<NetworkAdapter*>>& all_adapters()
41{
42 static Lockable<HashTable<NetworkAdapter*>>* table;
43 if (!table)
44 table = new Lockable<HashTable<NetworkAdapter*>>;
45 return *table;
46}
47
48void NetworkAdapter::for_each(Function<void(NetworkAdapter&)> callback)
49{
50 LOCKER(all_adapters().lock());
51 for (auto& it : all_adapters().resource())
52 callback(*it);
53}
54
55RefPtr<NetworkAdapter> NetworkAdapter::from_ipv4_address(const IPv4Address& address)
56{
57 LOCKER(all_adapters().lock());
58 for (auto* adapter : all_adapters().resource()) {
59 if (adapter->ipv4_address() == address)
60 return adapter;
61 }
62 if (address[0] == 127)
63 return LoopbackAdapter::the();
64 return nullptr;
65}
66
67RefPtr<NetworkAdapter> NetworkAdapter::lookup_by_name(const StringView& name)
68{
69 NetworkAdapter* found_adapter = nullptr;
70 for_each([&](auto& adapter) {
71 if (adapter.name() == name)
72 found_adapter = &adapter;
73 });
74 return found_adapter;
75}
76
77NetworkAdapter::NetworkAdapter()
78{
79 // FIXME: I wanna lock :(
80 all_adapters().resource().set(this);
81}
82
83NetworkAdapter::~NetworkAdapter()
84{
85 // FIXME: I wanna lock :(
86 all_adapters().resource().remove(this);
87}
88
89void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
90{
91 int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
92 auto buffer = ByteBuffer::create_zeroed(size_in_bytes);
93 auto* eth = (EthernetFrameHeader*)buffer.data();
94 eth->set_source(mac_address());
95 eth->set_destination(destination);
96 eth->set_ether_type(EtherType::ARP);
97 m_packets_out++;
98 m_bytes_out += size_in_bytes;
99 memcpy(eth->payload(), &packet, sizeof(ARPPacket));
100 send_raw((const u8*)eth, size_in_bytes);
101}
102
103void NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const u8* payload, size_t payload_size, u8 ttl)
104{
105 size_t ipv4_packet_size = sizeof(IPv4Packet) + payload_size;
106 if (ipv4_packet_size > mtu()) {
107 send_ipv4_fragmented(destination_mac, destination_ipv4, protocol, payload, payload_size, ttl);
108 return;
109 }
110
111 size_t ethernet_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + payload_size;
112 auto buffer = ByteBuffer::create_zeroed(ethernet_frame_size);
113 auto& eth = *(EthernetFrameHeader*)buffer.data();
114 eth.set_source(mac_address());
115 eth.set_destination(destination_mac);
116 eth.set_ether_type(EtherType::IPv4);
117 auto& ipv4 = *(IPv4Packet*)eth.payload();
118 ipv4.set_version(4);
119 ipv4.set_internet_header_length(5);
120 ipv4.set_source(ipv4_address());
121 ipv4.set_destination(destination_ipv4);
122 ipv4.set_protocol((u8)protocol);
123 ipv4.set_length(sizeof(IPv4Packet) + payload_size);
124 ipv4.set_ident(1);
125 ipv4.set_ttl(ttl);
126 ipv4.set_checksum(ipv4.compute_checksum());
127 m_packets_out++;
128 m_bytes_out += ethernet_frame_size;
129 memcpy(ipv4.payload(), payload, payload_size);
130 send_raw((const u8*)ð, ethernet_frame_size);
131}
132
133void NetworkAdapter::send_ipv4_fragmented(const MACAddress& destination_mac, const IPv4Address& destination_ipv4, IPv4Protocol protocol, const u8* payload, size_t payload_size, u8 ttl)
134{
135 // packets must be split on the 64-bit boundary
136 auto packet_boundary_size = (mtu() - sizeof(IPv4Packet) - sizeof(EthernetFrameHeader)) & 0xfffffff8;
137 auto fragment_block_count = (payload_size + packet_boundary_size) / packet_boundary_size;
138 auto last_block_size = payload_size - packet_boundary_size * (fragment_block_count - 1);
139 auto number_of_blocks_in_fragment = packet_boundary_size / 8;
140
141 auto identification = get_good_random<u16>();
142
143 size_t ethernet_frame_size = mtu();
144 for (size_t packet_index = 0; packet_index < fragment_block_count; ++packet_index) {
145 auto is_last_block = packet_index + 1 == fragment_block_count;
146 auto packet_payload_size = is_last_block ? last_block_size : packet_boundary_size;
147 auto buffer = ByteBuffer::create_zeroed(ethernet_frame_size);
148 auto& eth = *(EthernetFrameHeader*)buffer.data();
149 eth.set_source(mac_address());
150 eth.set_destination(destination_mac);
151 eth.set_ether_type(EtherType::IPv4);
152 auto& ipv4 = *(IPv4Packet*)eth.payload();
153 ipv4.set_version(4);
154 ipv4.set_internet_header_length(5);
155 ipv4.set_source(ipv4_address());
156 ipv4.set_destination(destination_ipv4);
157 ipv4.set_protocol((u8)protocol);
158 ipv4.set_length(sizeof(IPv4Packet) + packet_payload_size);
159 ipv4.set_has_more_fragments(!is_last_block);
160 ipv4.set_ident(identification);
161 ipv4.set_ttl(ttl);
162 ipv4.set_fragment_offset(packet_index * number_of_blocks_in_fragment);
163 ipv4.set_checksum(ipv4.compute_checksum());
164 m_packets_out++;
165 m_bytes_out += ethernet_frame_size;
166 memcpy(ipv4.payload(), payload + packet_index * packet_boundary_size, packet_payload_size);
167 send_raw((const u8*)ð, ethernet_frame_size);
168 }
169}
170
171void NetworkAdapter::did_receive(const u8* data, size_t length)
172{
173 InterruptDisabler disabler;
174 m_packets_in++;
175 m_bytes_in += length;
176
177 Optional<KBuffer> buffer;
178
179 if (m_unused_packet_buffers.is_empty()) {
180 buffer = KBuffer::copy(data, length);
181 } else {
182 buffer = m_unused_packet_buffers.take_first();
183 --m_unused_packet_buffers_count;
184 if (length <= buffer.value().size()) {
185 memcpy(buffer.value().data(), data, length);
186 buffer.value().set_size(length);
187 } else {
188 buffer = KBuffer::copy(data, length);
189 }
190 }
191
192 m_packet_queue.append(buffer.value());
193
194 if (on_receive)
195 on_receive();
196}
197
198size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size)
199{
200 InterruptDisabler disabler;
201 if (m_packet_queue.is_empty())
202 return 0;
203 auto packet = m_packet_queue.take_first();
204 size_t packet_size = packet.size();
205 ASSERT(packet_size <= buffer_size);
206 memcpy(buffer, packet.data(), packet_size);
207 if (m_unused_packet_buffers_count < 100) {
208 m_unused_packet_buffers.append(packet);
209 ++m_unused_packet_buffers_count;
210 }
211 return packet_size;
212}
213
214void NetworkAdapter::set_ipv4_address(const IPv4Address& address)
215{
216 m_ipv4_address = address;
217}
218
219void NetworkAdapter::set_ipv4_netmask(const IPv4Address& netmask)
220{
221 m_ipv4_netmask = netmask;
222}
223
224void NetworkAdapter::set_ipv4_gateway(const IPv4Address& gateway)
225{
226 m_ipv4_gateway = gateway;
227}
228
229void NetworkAdapter::set_interface_name(const StringView& basename)
230{
231 // FIXME: Find a unique name for this interface, starting with $basename.
232 StringBuilder builder;
233 builder.append(basename);
234 builder.append('0');
235 m_name = builder.to_string();
236}
237
238}