3 #include "net/endianess.h"
5 #include "net/ethernet.h"
10 void ethernet_dump(
void* data,
size_t size, uint16_t type){
11 qemu_log(
"Types: %x", type);
13 char* pkg = (
char*) data;
17 }
else if(pkg[0] == 0x60) {
18 ETH_IPv6_PKG* ipv6 = (ETH_IPv6_PKG*)pkg;
20 qemu_log(
"[NET] [DUMP] Определен: IPv6");
21 qemu_log(
" |--- Version: %x",ipv6->Version);
22 qemu_log(
" |--- Flow: %x %x %x",ipv6->Flow[0],ipv6->Flow[1],ipv6->Flow[2]);
23 qemu_log(
" |--- PayLoad: %x",ipv6->PayLoad);
24 qemu_log(
" |--- NextHead: %x",ipv6->NextHead);
26 if (ipv6->NextHead == ETH_IPv6_HEAD_ICMPv6){
27 ETH_ICMPv6_PKG* icmpv6 = (ETH_ICMPv6_PKG*)(pkg +
sizeof(ETH_IPv6_PKG));
29 qemu_log(
" | |--- Header: [%x] %s", ipv6->NextHead,
"ICMPv6");
30 qemu_log(
" | |--- Type: %x",icmpv6->Type);
31 qemu_log(
" | |--- Code: %x",icmpv6->Code);
32 qemu_log(
" | |--- CheckSum: %x",icmpv6->CheckSum);
33 qemu_log(
" | |--- RAW: %d bytes", size -
sizeof(ETH_IPv6_PKG) -
sizeof(ETH_ICMPv6_PKG));
36 }
else if (ipv6->NextHead == ETH_IPv6_HEAD_UDP){
37 ETH_UDP_PKG* udp = (ETH_UDP_PKG*)(pkg +
sizeof(ETH_IPv6_PKG));
38 qemu_log(
" | |--- Header: [%x] %s", ipv6->NextHead,
"UDP");
39 qemu_log(
" | |--- SourcePort: %d",udp->SourcePort);
40 qemu_log(
" | |--- DestinationPort: %d",udp->DestinationPort);
41 qemu_log(
" | |--- Length: %d",udp->Length);
42 qemu_log(
" | |--- CheckSum: %x",udp->CheckSum);
43 qemu_log(
" | |--- RAW: %d bytes", size -
sizeof(ETH_IPv6_PKG) -
sizeof(ETH_UDP_PKG));
47 qemu_log(
" | |--- Header: [%x] %s", ipv6->NextHead,
"Unknown");
48 qemu_log(
" | |--- RAW: %d bytes", size -
sizeof(ETH_IPv6_PKG));
52 ipv6->Source[0] = htons(ipv6->Source[0]);
53 ipv6->Source[1] = htons(ipv6->Source[1]);
54 ipv6->Source[2] = htons(ipv6->Source[2]);
55 ipv6->Source[3] = htons(ipv6->Source[3]);
56 ipv6->Source[4] = htons(ipv6->Source[4]);
57 ipv6->Source[5] = htons(ipv6->Source[5]);
58 ipv6->Source[6] = htons(ipv6->Source[6]);
59 ipv6->Source[7] = htons(ipv6->Source[7]);
61 ipv6->Destination[0] = htons(ipv6->Destination[0]);
62 ipv6->Destination[1] = htons(ipv6->Destination[1]);
63 ipv6->Destination[2] = htons(ipv6->Destination[2]);
64 ipv6->Destination[3] = htons(ipv6->Destination[3]);
65 ipv6->Destination[4] = htons(ipv6->Destination[4]);
66 ipv6->Destination[5] = htons(ipv6->Destination[5]);
67 ipv6->Destination[6] = htons(ipv6->Destination[6]);
68 ipv6->Destination[7] = htons(ipv6->Destination[7]);
70 qemu_log(
" |--- HopLimit: %x",ipv6->HopLimit);
71 qemu_log(
" |--- Source: %x:%x:%x:%x:%x:%x:%x:%x", ipv6->Source[0], ipv6->Source[1], ipv6->Source[2], ipv6->Source[3], ipv6->Source[4], ipv6->Source[5], ipv6->Source[6], ipv6->Source[7]);
72 qemu_log(
" |--- Destination: %x:%x:%x:%x:%x:%x:%x:%x", ipv6->Destination[0], ipv6->Destination[1], ipv6->Destination[2], ipv6->Destination[3], ipv6->Destination[4], ipv6->Destination[5], ipv6->Destination[6], ipv6->Destination[7]);
82 qemu_log(
"UNKNOWN ETHERNET PACKET TYPE");
86 void ethernet_send_packet(
netcard_entry_t* card, uint8_t* dest_mac, uint8_t* data,
size_t len, uint16_t type) {
87 assert(card == 0,
"%s",
"Card is nullptr.");
90 card->get_mac_addr(src_mac);
92 ethernet_frame_t* frame = kmalloc(
sizeof(ethernet_frame_t) + len);
93 void* frame_data = (
char*)frame +
sizeof(ethernet_frame_t);
95 memcpy(frame->src_mac, src_mac, 6);
96 memcpy(frame->dest_mac, dest_mac, 6);
97 memcpy(frame_data, data, len);
99 frame->type = htons(type);
101 netstack_push(card, frame,
sizeof(ethernet_frame_t) + len);
107 void ethernet_handle_packet(
netcard_entry_t *card, ethernet_frame_t *packet,
size_t len) {
108 void* data = (
void*)packet +
sizeof(ethernet_frame_t);
109 size_t data_len = len -
sizeof(ethernet_frame_t);
111 qemu_log(
"Received Ethernet Packet!");
112 qemu_log(
"=> SRC[%x:%x:%x:%x:%x:%x]; DEST[%x:%x:%x:%x:%x:%x]; TYPE: %x",
125 bit_flip_short(packet->type)
130 ethernet_dump(data, data_len,bit_flip_short(packet->type));
132 if(ntohs(packet->type) == ETHERNET_TYPE_ARP) {
133 arp_handle_packet(card, data, data_len);
134 }
else if(ntohs(packet->type) == ETHERNET_TYPE_IPV4) {
135 ipv4_handle_packet(card, data, data_len);
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.