summaryrefslogtreecommitdiff
path: root/network/ipv4.c
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-27 22:17:24 +0200
committerAnton Kling <anton@kling.gg>2023-10-30 21:49:48 +0100
commitd50d18c9da3a125f0196bec89802dec1c5b0b800 (patch)
treed9a4e74906c5ab51708d148ad3728cc155a9893d /network/ipv4.c
parent4f9ed7087cb58683d9423ab771ad76b31dac5514 (diff)
Kernel/LibC/Networking: Be able to send UDP messages
Now it can send UDP messages to a specific IP address and libc has enough to create a basic UDP ECHO server, that is kinda cool.
Diffstat (limited to 'network/ipv4.c')
-rw-r--r--network/ipv4.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/network/ipv4.c b/network/ipv4.c
index e7ef2c2..099aa0d 100644
--- a/network/ipv4.c
+++ b/network/ipv4.c
@@ -1,11 +1,78 @@
#include <assert.h>
+#include <kmalloc.h>
+#include <network/arp.h>
#include <network/bytes.h>
+#include <network/ethernet.h>
#include <network/ipv4.h>
#include <network/udp.h>
#include <string.h>
+uint16_t ip_checksum(void *vdata, size_t length) {
+ // Cast the data pointer to one that can be indexed.
+ char *data = (char *)vdata;
+
+ // Initialise the accumulator.
+ uint32_t acc = 0xffff;
+
+ // Handle complete 16-bit blocks.
+ for (size_t i = 0; i + 1 < length; i += 2) {
+ uint16_t word;
+ memcpy(&word, data + i, 2);
+ acc += ntohs(word);
+ if (acc > 0xffff) {
+ acc -= 0xffff;
+ }
+ }
+
+ // Handle any partial block at the end of the data.
+ if (length & 1) {
+ uint16_t word = 0;
+ memcpy(&word, data + length - 1, 1);
+ acc += ntohs(word);
+ if (acc > 0xffff) {
+ acc -= 0xffff;
+ }
+ }
+
+ // Return the checksum in network byte order.
+ return htons(~acc);
+}
+
+extern uint8_t ip_address[4];
+void send_ipv4_packet(uint32_t ip, uint8_t protocol, const uint8_t *payload,
+ uint16_t length) {
+ uint8_t header[20] = {0};
+ header[0] = (4 /*version*/ << 4) | (5 /*IHL*/);
+ *((uint16_t *)(header + 2)) = htons(length + 20);
+ header[8 /*TTL*/] = 0xF8;
+ header[9] = protocol;
+
+ memcpy(header + 12 /*src_ip*/, ip_address, sizeof(uint8_t[4]));
+ memcpy(header + 16, &ip, sizeof(uint8_t[4]));
+
+ *((uint16_t *)(header + 10 /*checksum*/)) = ip_checksum(header, 20);
+ uint16_t packet_length = length + 20;
+ uint8_t *packet = kmalloc(packet_length);
+ memcpy(packet, header, 20);
+ memcpy(packet + 20, payload, length);
+
+ uint8_t mac[6];
+ uint8_t ip_copy[4]; // TODO: Do I need to do this?
+ memcpy(ip_copy, &ip, sizeof(uint8_t[4]));
+ get_mac_from_ip(ip_copy, mac);
+ send_ethernet_packet(mac, 0x0800, packet, packet_length);
+ kfree(packet);
+}
+
void handle_ipv4(const uint8_t *payload, uint32_t packet_length) {
assert(packet_length > 4);
+
+ uint16_t saved_checksum = *(uint16_t *)(payload + 10);
+ *(uint16_t *)(payload + 10) = 0;
+ uint16_t calc_checksum = ip_checksum((uint8_t *)payload, 20);
+ *(uint16_t *)(payload + 10) = saved_checksum;
+ assert(calc_checksum == saved_checksum);
+
uint8_t version = (*payload & 0xF0) >> 4;
uint8_t IHL = (*payload & 0xF);
kprintf("version: %x\n", version);