From 0dccff86e50dfe1555b8bc29862dba2b972a3705 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Sun, 17 Mar 2024 20:55:34 +0100 Subject: stuff --- kernel/network/arp.c | 30 +++++++------- kernel/network/arp.h | 5 ++- kernel/network/ipv4.c | 15 +++---- kernel/network/ipv4.h | 2 +- kernel/network/tcp.c | 107 ++++++-------------------------------------------- kernel/network/tcp.h | 5 ++- kernel/network/udp.c | 4 +- kernel/network/udp.h | 2 +- 8 files changed, 43 insertions(+), 127 deletions(-) (limited to 'kernel/network') diff --git a/kernel/network/arp.c b/kernel/network/arp.c index 576a09a..d4cf0dd 100644 --- a/kernel/network/arp.c +++ b/kernel/network/arp.c @@ -1,11 +1,11 @@ #include #include +#include #include #include #include #include #include -#include ipv4_t gateway; ipv4_t bitmask; @@ -38,7 +38,7 @@ struct ARP_TABLE_ENTRY { struct ARP_TABLE_ENTRY arp_table[10] = {0}; // FIXME: This is hardcoded, don't do this. -u8 ip_address[4] = {10, 0, 2, 15}; +ipv4_t ip_address; struct ARP_TABLE_ENTRY *find_arp_entry_to_use(void) { // This does not need to find a "free" entry as a ARP table is @@ -73,7 +73,7 @@ void print_ip(const char *str, const u8 *ip) { kprintf("\n"); } -void send_arp_request(const u8 ip[4]) { +void send_arp_request(const ipv4_t ip) { struct ARP_DATA data; data.htype = htons(1); data.ptype = htons(0x0800); @@ -83,10 +83,10 @@ void send_arp_request(const u8 ip[4]) { data.opcode = htons(0x0001); get_mac_address(data.srchw); - memcpy(data.srcpr, ip_address, sizeof(u8[4])); + memcpy(data.srcpr, &ip_address, sizeof(ipv4_t)); memset(data.dsthw, 0, sizeof(u8[6])); - memcpy(data.dstpr, ip, sizeof(u8[4])); + memcpy(data.dstpr, &ip, sizeof(ipv4_t)); u8 broadcast[6]; memset(broadcast, 0xFF, sizeof(broadcast)); @@ -100,15 +100,13 @@ int ip_inside_network(const ipv4_t ip) { return 0; } -int get_mac_from_ip(const u8 ip[4], u8 mac[6]) { - ipv4_t tmp_ip; - memcpy(tmp_ip.a, ip, sizeof(u8[4])); - if (!ip_inside_network(tmp_ip)) { - return get_mac_from_ip(gateway.a, mac); +int get_mac_from_ip(const ipv4_t ip, u8 mac[6]) { + if (!ip_inside_network(ip)) { + return get_mac_from_ip(gateway, mac); } for (int i = 0; i < 10; i++) { - if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4]))) { + if (0 != memcmp(arp_table[i].ip, &ip, sizeof(u8[4]))) { continue; } memcpy(mac, arp_table[i].mac, sizeof(u8[6])); @@ -119,7 +117,7 @@ int get_mac_from_ip(const u8 ip[4], u8 mac[6]) { send_arp_request(ip); // TODO: Maybe wait a bit? for (int i = 0; i < 10; i++) { - if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4]))) { + if (0 != memcmp(arp_table[i].ip, &ip, sizeof(u8[4]))) { continue; } memcpy(mac, arp_table[i].mac, sizeof(u8[6])); @@ -142,10 +140,10 @@ void handle_arp(const u8 *payload) { if (0x0001 /*arp_request*/ == ntohs(data->opcode)) { struct ARP_TABLE_ENTRY *entry = find_arp_entry_to_use(); entry->is_used = 1; - memcpy(entry->mac, data->srchw, sizeof(uint8_t[6])); - memcpy(entry->ip, data->srcpr, sizeof(uint8_t[4])); + memcpy(entry->mac, data->srchw, sizeof(u8[6])); + memcpy(entry->ip, data->srcpr, sizeof(u8[4])); - assert(0 == memcmp(data->dstpr, ip_address, sizeof(uint8_t[4]))); + assert(0 == memcmp(data->dstpr, &ip_address, sizeof(u8[4]))); // Now we have to construct a ARP response struct ARP_DATA response; @@ -155,7 +153,7 @@ void handle_arp(const u8 *payload) { response.hlen = 6; response.plen = 4; get_mac_address(response.srchw); - memcpy(response.srcpr, ip_address, sizeof(u8[4])); + memcpy(response.srcpr, &ip_address, sizeof(u8[4])); memcpy(response.dsthw, data->srchw, sizeof(u8[6])); memcpy(response.dstpr, data->srcpr, sizeof(u8[4])); diff --git a/kernel/network/arp.h b/kernel/network/arp.h index 3795a62..e2d2fe1 100644 --- a/kernel/network/arp.h +++ b/kernel/network/arp.h @@ -1,6 +1,7 @@ #include +extern ipv4_t ip_address; void setup_network(ipv4_t _gateway, ipv4_t _bitmask); -int get_mac_from_ip(const u8 ip[4], u8 mac[6]); -void send_arp_request(const u8 ip[4]); +int get_mac_from_ip(const ipv4_t ip, u8 mac[6]); +void send_arp_request(const ipv4_t ip); void handle_arp(const u8 *payload); diff --git a/kernel/network/ipv4.c b/kernel/network/ipv4.c index 6335a09..4fe3799 100644 --- a/kernel/network/ipv4.c +++ b/kernel/network/ipv4.c @@ -40,16 +40,15 @@ u16 ip_checksum(void *vdata, size_t length) { return htons(~acc); } -extern u8 ip_address[4]; -void send_ipv4_packet(u32 ip, u8 protocol, const u8 *payload, u16 length) { +void send_ipv4_packet(ipv4_t ip, u8 protocol, const u8 *payload, u16 length) { u8 header[20] = {0}; header[0] = (4 /*version*/ << 4) | (5 /*IHL*/); *((u16 *)(header + 2)) = htons(length + 20); header[8 /*TTL*/] = 0xF8; header[9] = protocol; - memcpy(header + 12 /*src_ip*/, ip_address, sizeof(u8[4])); - memcpy(header + 16, &ip, sizeof(u8[4])); + memcpy(header + 12 /*src_ip*/, &ip_address, sizeof(ipv4_t)); + memcpy(header + 16, &ip, sizeof(ipv4_t)); *((u16 *)(header + 10 /*checksum*/)) = ip_checksum(header, 20); u16 packet_length = length + 20; @@ -58,9 +57,7 @@ void send_ipv4_packet(u32 ip, u8 protocol, const u8 *payload, u16 length) { memcpy(packet + 20, payload, length); u8 mac[6]; - u8 ip_copy[4]; // TODO: Do I need to do this? - memcpy(ip_copy, &ip, sizeof(u8[4])); - for (; !get_mac_from_ip(ip_copy, mac);) + for (; !get_mac_from_ip(ip, mac);) ; send_ethernet_packet(mac, 0x0800, packet, packet_length); kfree(packet); @@ -86,8 +83,8 @@ void handle_ipv4(const u8 *payload, u32 packet_length) { // Make sure the ipv4 header is not trying to get uninitalized memory assert(ipv4_total_length <= packet_length); - u8 src_ip[4]; - memcpy(src_ip, payload + 12, sizeof(u8[4])); + ipv4_t src_ip; + memcpy(&src_ip, payload + 12, sizeof(u8[4])); u8 protocol = *(payload + 9); switch (protocol) { diff --git a/kernel/network/ipv4.h b/kernel/network/ipv4.h index 002aaa7..7b44eb1 100644 --- a/kernel/network/ipv4.h +++ b/kernel/network/ipv4.h @@ -1,4 +1,4 @@ #include void handle_ipv4(const u8 *payload, u32 packet_length); -void send_ipv4_packet(u32 ip, u8 protocol, const u8 *payload, u16 length); +void send_ipv4_packet(ipv4_t ip, u8 protocol, const u8 *payload, u16 length); diff --git a/kernel/network/tcp.c b/kernel/network/tcp.c index fb3c052..b929005 100644 --- a/kernel/network/tcp.c +++ b/kernel/network/tcp.c @@ -1,9 +1,10 @@ #include +#include #include +#include #include #include #include -extern u8 ip_address[4]; #define CWR (1 << 7) #define ECE (1 << 6) @@ -53,8 +54,7 @@ void tcp_wait_reply(struct TcpConnection *con) { if (con->unhandled_packet) { return; } - // TODO: Make the scheduler halt the process - switch_task(); + wait_for_interrupt(); } } @@ -73,10 +73,10 @@ u16 tcp_checksum(u16 *buffer, int size) { return (u16)(~cksum); } -void tcp_calculate_checksum(u8 src_ip[4], u32 dst_ip, const u8 *payload, +void tcp_calculate_checksum(ipv4_t src_ip, u32 dst_ip, const u8 *payload, u16 payload_length, struct TCP_HEADER *header) { struct PSEUDO_TCP_HEADER ps = {0}; - memcpy(&ps.src_addr, src_ip, sizeof(u32)); + memcpy(&ps.src_addr, &src_ip.d, sizeof(u32)); memcpy(&ps.dst_addr, &dst_ip, sizeof(u32)); ps.protocol = 6; ps.tcp_length = htons(20 + payload_length); @@ -118,7 +118,7 @@ void tcp_send_empty_payload(struct TcpConnection *con, u8 flags) { memcpy(send_buffer, &header, sizeof(header)); memcpy(send_buffer + sizeof(header), payload, payload_length); - send_ipv4_packet(con->outgoing_ip, 6, send_buffer, send_len); + send_ipv4_packet((ipv4_t){.d = con->outgoing_ip}, 6, send_buffer, send_len); } void tcp_send_ack(struct TcpConnection *con) { @@ -130,8 +130,6 @@ void tcp_send_syn(struct TcpConnection *con) { con->seq++; } -// void send_tcp_packet(struct INCOMING_TCP_CONNECTION *inc, const u8 *payload, -// u16 payload_length) { void send_tcp_packet(struct TcpConnection *con, const u8 *payload, u16 payload_length) { if (payload_length > 1500 - 20 - sizeof(struct TCP_HEADER)) { @@ -156,12 +154,12 @@ void send_tcp_packet(struct TcpConnection *con, const u8 *payload, u8 send_buffer[send_len]; memcpy(send_buffer, &header, sizeof(header)); memcpy(send_buffer + sizeof(header), payload, payload_length); - send_ipv4_packet(con->outgoing_ip, 6, send_buffer, send_len); + send_ipv4_packet((ipv4_t){.d = con->outgoing_ip}, 6, send_buffer, send_len); con->seq += payload_length; } -void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) { +void handle_tcp(ipv4_t src_ip, const u8 *payload, u32 payload_length) { const struct TCP_HEADER *header = (const struct TCP_HEADER *)payload; (void)header; u16 n_src_port = *(u16 *)(payload); @@ -180,9 +178,8 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) { (void)ack_num; if (SYN == flags) { - u32 t; - memcpy(&t, src_ip, sizeof(u8[4])); - struct TcpConnection *con = internal_tcp_incoming(t, src_port, 0, dst_port); + struct TcpConnection *con = + internal_tcp_incoming(src_ip.d, src_port, 0, dst_port); assert(con); con->ack = seq_num + 1; tcp_send_empty_payload(con, SYN | ACK); @@ -191,6 +188,9 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) { struct TcpConnection *incoming_connection = tcp_find_connection(dst_port); kprintf("dst_port: %d\n", dst_port); + if (!incoming_connection) { + kprintf("unable to find open port for incoming connection\n"); + } if (incoming_connection) { incoming_connection->unhandled_packet = 1; if (0 != (flags & RST)) { @@ -213,7 +213,6 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) { tcp_send_ack(incoming_connection); } - // if (0 != (flags & PSH)) { u16 tcp_payload_length = payload_length - header->data_offset * sizeof(u32); if (tcp_payload_length > 0) { int len = fifo_object_write( @@ -236,83 +235,3 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) { return; } } -/* -void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) { - const struct TCP_HEADER *inc_header = (const struct TCP_HEADER *)payload; - u16 n_src_port = *(u16 *)(payload); - u16 n_dst_port = *(u16 *)(payload + 2); - u32 n_seq_num = *(u32 *)(payload + 4); - u32 n_ack_num = *(u32 *)(payload + 8); - - u8 flags = *(payload + 13); - - u16 src_port = htons(n_src_port); - (void)src_port; - u16 dst_port = htons(n_dst_port); - u32 seq_num = htonl(n_seq_num); - u32 ack_num = htonl(n_ack_num); - (void)ack_num; - - if (SYN == flags) { - struct INCOMING_TCP_CONNECTION *inc; - if (!(inc = handle_incoming_tcp_connection(src_ip, n_src_port, dst_port))) { - return; - } - memcpy(inc->ip, src_ip, sizeof(u8[4])); - inc->seq_num = 0; - inc->ack_num = seq_num + 1; - inc->connection_closed = 0; - inc->requesting_connection_close = 0; - send_empty_tcp_message(inc, SYN | ACK, seq_num, n_dst_port, n_src_port); - inc->seq_num++; - return; - } - struct INCOMING_TCP_CONNECTION *inc = - get_incoming_tcp_connection(src_ip, n_src_port); - if (!inc) { - return; - } - if (flags == (FIN | ACK)) { - if (inc->requesting_connection_close) { - send_empty_tcp_message(inc, ACK, seq_num, n_dst_port, n_src_port); - inc->connection_closed = 1; - } else { - send_empty_tcp_message(inc, FIN | ACK, seq_num, n_dst_port, n_src_port); - } - inc->seq_num++; - inc->connection_closed = 1; - return; - } - if (flags & ACK) { - // inc->seq_num = ack_num; - } - if (flags & PSH) { - kprintf("TCP: Got PSH\n"); - u16 tcp_payload_length = - payload_length - inc_header->data_offset * sizeof(u32); - int rc = fifo_object_write( - (u8 *)(payload + inc_header->data_offset * sizeof(u32)), 0, - tcp_payload_length, inc->data_file); - kprintf("fifo object write rc: %x\n", rc); - *inc->has_data_ptr = 1; - - // Send back a ACK - struct TCP_HEADER header = {0}; - header.src_port = n_dst_port; - header.dst_port = n_src_port; - header.seq_num = htonl(inc->seq_num); - inc->ack_num = seq_num + tcp_payload_length; - header.ack_num = htonl(seq_num + tcp_payload_length); - header.data_offset = 5; - header.reserved = 0; - header.flags = ACK; - header.window_size = htons(WINDOW_SIZE); - header.urgent_pointer = 0; - u32 dst_ip; - memcpy(&dst_ip, src_ip, sizeof(dst_ip)); - char payload[0]; - tcp_calculate_checksum(ip_address, dst_ip, (const u8 *)payload, 0, &header); - send_ipv4_packet(dst_ip, 6, (const u8 *)&header, sizeof(header)); - return; - } -}*/ diff --git a/kernel/network/tcp.h b/kernel/network/tcp.h index 0f9e818..0c53535 100644 --- a/kernel/network/tcp.h +++ b/kernel/network/tcp.h @@ -1,7 +1,8 @@ #include +#include void tcp_send_syn(struct TcpConnection *con); void tcp_wait_reply(struct TcpConnection *con); -void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length); +void handle_tcp(ipv4_t src_ip, const u8 *payload, u32 payload_length); void send_tcp_packet(struct TcpConnection *con, const u8 *payload, u16 payload_length); - void tcp_close_connection(struct INCOMING_TCP_CONNECTION * s); +void tcp_close_connection(struct INCOMING_TCP_CONNECTION *s); diff --git a/kernel/network/udp.c b/kernel/network/udp.c index 4f3848a..1112c3d 100644 --- a/kernel/network/udp.c +++ b/kernel/network/udp.c @@ -15,11 +15,11 @@ void send_udp_packet(struct sockaddr_in *src, const struct sockaddr_in *dst, u8 *packet = kmalloc(packet_length); memcpy(packet, header, sizeof(header)); memcpy(packet + sizeof(header), payload, payload_length); - send_ipv4_packet(dst->sin_addr.s_addr, 0x11, packet, packet_length); + send_ipv4_packet((ipv4_t){.d = dst->sin_addr.s_addr}, 0x11, packet, packet_length); kfree(packet); } -void handle_udp(u8 src_ip[4], const u8 *payload, u32 packet_length) { +void handle_udp(ipv4_t src_ip, const u8 *payload, u32 packet_length) { // TODO: Reimplement assert(NULL); } diff --git a/kernel/network/udp.h b/kernel/network/udp.h index fe21b0f..bcf31bc 100644 --- a/kernel/network/udp.h +++ b/kernel/network/udp.h @@ -1,6 +1,6 @@ #include #include -void handle_udp(u8 src_ip[4], const u8 *payload, u32 packet_length); +void handle_udp(ipv4_t src_ip, const u8 *payload, u32 packet_length); void send_udp_packet(struct sockaddr_in *src, const struct sockaddr_in *dst, const u8 *payload, u16 payload_length); -- cgit v1.2.3