Serenity Operating System
at hosted 106 lines 4.1 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/ByteBuffer.h> 30#include <AK/Function.h> 31#include <AK/MACAddress.h> 32#include <AK/SinglyLinkedList.h> 33#include <AK/Types.h> 34#include <AK/WeakPtr.h> 35#include <AK/Weakable.h> 36#include <Kernel/KBuffer.h> 37#include <Kernel/Net/ARP.h> 38#include <Kernel/Net/ICMP.h> 39#include <Kernel/Net/IPv4.h> 40 41namespace Kernel { 42 43class NetworkAdapter; 44 45class NetworkAdapter : public RefCounted<NetworkAdapter> { 46public: 47 static void for_each(Function<void(NetworkAdapter&)>); 48 static RefPtr<NetworkAdapter> from_ipv4_address(const IPv4Address&); 49 static RefPtr<NetworkAdapter> lookup_by_name(const StringView&); 50 virtual ~NetworkAdapter(); 51 52 virtual const char* class_name() const = 0; 53 54 const String& name() const { return m_name; } 55 MACAddress mac_address() { return m_mac_address; } 56 IPv4Address ipv4_address() const { return m_ipv4_address; } 57 IPv4Address ipv4_netmask() const { return m_ipv4_netmask; } 58 IPv4Address ipv4_gateway() const { return m_ipv4_gateway; } 59 virtual bool link_up() { return false; } 60 61 void set_ipv4_address(const IPv4Address&); 62 void set_ipv4_netmask(const IPv4Address&); 63 void set_ipv4_gateway(const IPv4Address&); 64 65 void send(const MACAddress&, const ARPPacket&); 66 void send_ipv4(const MACAddress&, const IPv4Address&, IPv4Protocol, const u8* payload, size_t payload_size, u8 ttl); 67 void send_ipv4_fragmented(const MACAddress&, const IPv4Address&, IPv4Protocol, const u8* payload, size_t payload_size, u8 ttl); 68 69 size_t dequeue_packet(u8* buffer, size_t buffer_size); 70 71 bool has_queued_packets() const { return !m_packet_queue.is_empty(); } 72 73 u32 mtu() const { return m_mtu; } 74 void set_mtu(u32 mtu) { m_mtu = mtu; } 75 76 u32 packets_in() const { return m_packets_in; } 77 u32 bytes_in() const { return m_bytes_in; } 78 u32 packets_out() const { return m_packets_out; } 79 u32 bytes_out() const { return m_bytes_out; } 80 81 Function<void()> on_receive; 82 83protected: 84 NetworkAdapter(); 85 void set_interface_name(const StringView& basename); 86 void set_mac_address(const MACAddress& mac_address) { m_mac_address = mac_address; } 87 virtual void send_raw(const u8*, size_t) = 0; 88 void did_receive(const u8*, size_t); 89 90private: 91 MACAddress m_mac_address; 92 IPv4Address m_ipv4_address; 93 IPv4Address m_ipv4_netmask; 94 IPv4Address m_ipv4_gateway; 95 SinglyLinkedList<KBuffer> m_packet_queue; 96 SinglyLinkedList<KBuffer> m_unused_packet_buffers; 97 size_t m_unused_packet_buffers_count { 0 }; 98 String m_name; 99 u32 m_packets_in { 0 }; 100 u32 m_bytes_in { 0 }; 101 u32 m_packets_out { 0 }; 102 u32 m_bytes_out { 0 }; 103 u32 m_mtu { 1500 }; 104}; 105 106}