This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheth.cpp
152 lines (131 loc) · 4.9 KB
/
eth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <net/eth.hpp>
#include <debug.h>
#include "../kernel.h"
/* conversion from 'uint48_t' {aka 'long unsigned int'} to 'long unsigned int:48' may change value */
#pragma GCC diagnostic ignored "-Wconversion"
namespace NetworkEthernet
{
struct EthernetEventHelperStruct
{
EthernetEvents *Ptr;
uint16_t Type;
};
std::vector<EthernetEventHelperStruct> RegisteredEvents;
Ethernet::Ethernet(NetworkInterfaceManager::DeviceInterface *Interface) : NetworkInterfaceManager::Events(Interface)
{
debug("Ethernet interface %#lx created.", this);
this->Interface = Interface;
}
Ethernet::~Ethernet()
{
debug("Ethernet interface %#lx destroyed.", this);
}
void Ethernet::Send(MediaAccessControl MAC, FrameType Type, uint8_t *Data, size_t Length)
{
netdbg("Sending frame type %#x to %s", Type, MAC.ToString());
size_t PacketLength = sizeof(EthernetHeader) + Length;
EthernetPacket *Packet = (EthernetPacket *)kmalloc(PacketLength);
Packet->Header.DestinationMAC = b48(MAC.ToHex());
Packet->Header.SourceMAC = b48(this->Interface->MAC.ToHex());
Packet->Header.Type = b16(Type);
memcpy(Packet->Data, Data, Length);
/* Network Interface Manager takes care of physical allocation.
So basically, we allocate here and then it allocates again but 1:1 mapped. */
// NIManager->Send(Interface, (uint8_t *)Packet, PacketLength);
assert(!"Function not implemented");
kfree(Packet);
}
void Ethernet::Receive(uint8_t *Data, size_t Length)
{
EthernetPacket *Packet = (EthernetPacket *)Data;
size_t PacketLength = Length - sizeof(EthernetHeader);
/* TODO: I have to do checks here to be sure that PacketLength is right */
UNUSED(PacketLength);
MediaAccessControl SourceMAC;
SourceMAC.FromHex(b48(Packet->Header.SourceMAC));
MediaAccessControl DestinationMAC;
DestinationMAC.FromHex(b48(Packet->Header.DestinationMAC));
netdbg("Received frame type %#x from %s to %s", b16(Packet->Header.Type), SourceMAC.ToString(), DestinationMAC.ToString());
/* Byte-swapped little-endian */
if (b48(Packet->Header.DestinationMAC) == 0xFFFFFFFFFFFF ||
/* Byte-swapped Driver interface has little-endian order */
b48(Packet->Header.DestinationMAC) == this->Interface->MAC.ToHex())
/* This is true only if the packet is for us (Interface MAC or broadcast) */
{
netdbg("Received data from %s [Type %#x]", SourceMAC.ToString(), b16(Packet->Header.Type));
bool Reply = false;
switch (b16(Packet->Header.Type))
{
case TYPE_IPV4:
foreach (auto e in RegisteredEvents)
if (e.Type == TYPE_IPV4)
Reply = e.Ptr->OnEthernetPacketReceived((uint8_t *)Packet->Data, Length);
break;
case TYPE_ARP:
foreach (auto e in RegisteredEvents)
if (e.Type == TYPE_ARP)
Reply = e.Ptr->OnEthernetPacketReceived((uint8_t *)Packet->Data, Length);
break;
case TYPE_RARP:
foreach (auto e in RegisteredEvents)
if (e.Type == TYPE_RARP)
Reply = e.Ptr->OnEthernetPacketReceived((uint8_t *)Packet->Data, Length);
break;
case TYPE_IPV6:
foreach (auto e in RegisteredEvents)
if (e.Type == TYPE_IPV6)
Reply = e.Ptr->OnEthernetPacketReceived((uint8_t *)Packet->Data, Length);
break;
default:
warn("Unknown packet type %#lx", Packet->Header.Type);
break;
}
if (Reply)
{
/* FIXME: I should reply, right? I have to do more research here... */
// Packet->Header.DestinationMAC = Packet->Header.SourceMAC;
// Packet->Header.SourceMAC = b48(this->Interface->MAC.ToHex());
fixme("Replying to %s [%s]=>[%s]", SourceMAC.ToString(),
this->Interface->MAC.ToString(), DestinationMAC.ToString());
}
}
else
{
netdbg("Packet not for us [%s]=>[%s]", this->Interface->MAC.ToString(), DestinationMAC.ToString());
}
}
void Ethernet::OnInterfaceReceived(NetworkInterfaceManager::DeviceInterface *Interface, uint8_t *Data, size_t Length)
{
if (Interface == this->Interface)
this->Receive(Data, Length);
}
EthernetEvents::EthernetEvents(FrameType Type)
{
this->FType = Type;
RegisteredEvents.push_back({.Ptr = this, .Type = (uint16_t)Type});
}
EthernetEvents::~EthernetEvents()
{
forItr(itr, RegisteredEvents)
{
if (itr->Ptr == this)
{
RegisteredEvents.erase(itr);
return;
}
}
}
}