SayoriOS 0.3.3

tcp.c
1 //
2 // Created by ndraey on 21.4.2024.
3 //
4
5 #include "common.h"
6 #include "net/cards.h"
7 #include "net/ethernet.h"
8 #include "net/tcp.h"
9 #include "net/endianess.h"
10 #include "io/ports.h"
11 #include "net/ipv4.h"
12 #include "mem/vmm.h"
13
14 #define MAX_CONNECTIONS 64
15
16 tcp_connection_t tcp_connections[MAX_CONNECTIONS] = {};
17
18 int tcp_find_connection(uint8_t address[4], size_t port) {
19 for(int i = 0; i < MAX_CONNECTIONS; i++) {
20 if(memcmp((uint8_t*)&tcp_connections[i].dest_ip_addr, address, 4) == 0
21 && tcp_connections[i].source_port == port
22 && tcp_connections[i].status != TCP_NONE) {
23 return i;
24 }
25 }
26
27 return -1;
28 }
29
30 bool tcp_new_connection(netcard_entry_t* card, uint8_t address[4], size_t port, size_t seq_nr) {
31 uint8_t empty_addr[4] = {0, 0, 0, 0};
32 int index = -1;
33
34 for(int i = 0; i < MAX_CONNECTIONS; i++) {
35 if(memcmp((uint8_t*)&tcp_connections[i].dest_ip_addr, empty_addr, 4) == 0
36 && tcp_connections[i].source_port == 0
37 && tcp_connections[i].status == TCP_NONE) {
38 index = i;
39 break;
40 }
41 }
42
43 if(index == -1) {
44 return false;
45 }
46
47 memcpy(&tcp_connections[index].dest_ip_addr, address, 4);
48 tcp_connections[index].source_port = port;
49 tcp_connections[index].status = TCP_CREATED;
50 tcp_connections[index].seq = seq_nr;
51 tcp_connections[index].card = card;
52
53 return true;
54 }
55
56 void tcp_handle_packet(netcard_entry_t *card, tcp_packet_t *packet) {
57 qemu_note("!!!!!!!!!!!!!!!!!!!!!!!! TCP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
58
59 ETH_IPv4_PKG *ipv4 = (ETH_IPv4_PKG *)((size_t)packet - sizeof(ETH_IPv4_PKG));
60 size_t data_payload_size = ipv4->TotalLength - sizeof(ETH_IPv4_PKG);
61
62 qemu_log("Data payload size: %d", data_payload_size);
63
64 packet->source = ntohs(packet->source);
65 packet->destination = ntohs(packet->destination);
66
67 qemu_log("FROM: %u.%u.%u.%u", ipv4->Source[0], ipv4->Source[1], ipv4->Source[2], ipv4->Source[3]);
68
69 qemu_note("SRC: %d; DEST: %d", packet->source, packet->destination);
70
71 qemu_note("FLAGS: SYN: %d; ACK: %d; PSH: %d; FIN: %d", packet->syn, packet->ack, packet->psh, packet->fin);
72
73 packet->ack_seq = ntohl(packet->ack_seq);
74 packet->seq = ntohl(packet->seq);
75
76
77 int idx = -1;
78 if(tcp_find_connection(ipv4->Source, packet->source) == -1) {
79 tcp_new_connection(card, ipv4->Source, packet->source, packet->seq);
80 qemu_ok("Created new connection!");
81 }
82 idx = tcp_find_connection(ipv4->Source, packet->source);
83
84 qemu_note("Connection idx: %d", idx);
85
86 bool is_stage_1 = packet->syn && !packet->ack && !packet->psh && !packet->fin;
87 bool is_stage_2 = !packet->syn && packet->ack && !packet->psh && !packet->fin;
88 bool is_push = !packet->syn && !packet->ack && packet->psh && !packet->fin;
89
90
91 tcp_packet_t* sendable_packet = kcalloc(sizeof(tcp_packet_t) + 8, 1);
92 memcpy(sendable_packet, packet, sizeof(tcp_packet_t));
93
94 char* options = (char*)(sendable_packet) + sizeof(tcp_packet_t);
95
96 options[0] = 0x02;
97 options[1] = 0x04;
98 options[2] = 0xff;
99 options[3] = 0xd7;
100 options[4] = 0x04;
101 options[5] = 0x02;
102 options[6] = 0x01;
103 options[7] = 0x01;
104
105 if(is_stage_1) {
106 tcp_connections[idx].seq = rand();
107 tcp_connections[idx].ack = sendable_packet->seq + 1;
108
109 sendable_packet->ack = 1;
110 sendable_packet->seq = ntohl(tcp_connections[idx].seq); // it's rand();
111 sendable_packet->ack_seq = ntohl(tcp_connections[idx].ack);
112
113 uint16_t dest = ntohs(sendable_packet->destination);
114 uint16_t src = ntohs(sendable_packet->source);
115 sendable_packet->source = dest;
116 sendable_packet->destination = src;
117
118 sendable_packet->doff = 7;
119
120 ipv4_send_packet(tcp_connections[idx].card, ipv4->Source, sendable_packet, sizeof(tcp_packet_t) + 8, ETH_IPv4_HEAD_TCP);
121 }
122
123 kfree(sendable_packet);
124 }
common.h
Основные определения ядра
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
memcmp
int32_t memcmp(const char *s1, const char *s2, size_t n)
Сравнение массивов
Definition: string.c:305
netcard_entry_t
Definition: cards.h:5
tcp_connection_t
Definition: tcp.h:36
tcp_packet_t
Definition: tcp.h:10