SayoriOS 0.3.3

ethernet.c
1 #include "net/ipv4.h"
2 #include "io/ports.h"
3 #include "net/endianess.h"
4 #include "net/arp.h"
5 #include "net/ethernet.h"
6 #include "mem/vmm.h"
7 //#include "debug/hexview.h"
8 #include "net/stack.h"
9
10 void ethernet_dump(void* data, size_t size, uint16_t type){
11 qemu_log("Types: %x", type);
12
13 char* pkg = (char*) data;
14
15 if(pkg[0] == 0x45) {
16 qemu_log("IPV4");
17 } else if(pkg[0] == 0x60) { // IPv6
18 ETH_IPv6_PKG* ipv6 = (ETH_IPv6_PKG*)pkg;
19
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);
25
26 if (ipv6->NextHead == ETH_IPv6_HEAD_ICMPv6){
27 ETH_ICMPv6_PKG* icmpv6 = (ETH_ICMPv6_PKG*)(pkg + sizeof(ETH_IPv6_PKG));
28
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));
34 qemu_log(" | |");
35
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));
44 qemu_log(" | |");
45
46 } else {
47 qemu_log(" | |--- Header: [%x] %s", ipv6->NextHead, "Unknown");
48 qemu_log(" | |--- RAW: %d bytes", size - sizeof(ETH_IPv6_PKG));
49 qemu_log(" | |");
50 }
51
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]);
60
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]);
69
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]);
73 /*
74 qemu_log(" |--- Дополнительная информация");
75 qemu_log(" |--- Type: %d",ipv6->Opt.Type);
76 qemu_log(" |--- Size: %d",ipv6->Opt.Size);
77
78 // First two bytes may indicate a checksum according to Wireshark
79 qemu_log(" |--- Data: %x:%x:%x:%x:%x:%x", ipv6->Opt.MAC[0], ipv6->Opt.MAC[1], ipv6->Opt.MAC[2], ipv6->Opt.MAC[3], ipv6->Opt.MAC[4], ipv6->Opt.MAC[5]);
80 */
81 } else {
82 qemu_log("UNKNOWN ETHERNET PACKET TYPE");
83 }
84 }
85
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.");
88
89 uint8_t src_mac[6];
90 card->get_mac_addr(src_mac);
91
92 ethernet_frame_t* frame = kmalloc(sizeof(ethernet_frame_t) + len);
93 void* frame_data = (char*)frame + sizeof(ethernet_frame_t);
94
95 memcpy(frame->src_mac, src_mac, 6);
96 memcpy(frame->dest_mac, dest_mac, 6);
97 memcpy(frame_data, data, len);
98
99 frame->type = htons(type);
100
101 netstack_push(card, frame, sizeof(ethernet_frame_t) + len);
102 //card->send_packet(frame, sizeof(ethernet_frame_t) + len);
103
104 kfree(frame);
105 }
106
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); // Get data
109 size_t data_len = len - sizeof(ethernet_frame_t); // Get length of data
110
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",
113 packet->src_mac[0],
114 packet->src_mac[1],
115 packet->src_mac[2],
116 packet->src_mac[3],
117 packet->src_mac[4],
118 packet->src_mac[5],
119 packet->dest_mac[0],
120 packet->dest_mac[1],
121 packet->dest_mac[2],
122 packet->dest_mac[3],
123 packet->dest_mac[4],
124 packet->dest_mac[5],
125 bit_flip_short(packet->type)
126 );
127
128 // hexview_advanced(data, data_len, 10, true, new_qemu_printf);
129
130 ethernet_dump(data, data_len,bit_flip_short(packet->type));
131
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);
136 }
137 }
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
netcard_entry_t
Definition: cards.h:5