aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/drivers/acpi.c12
-rw-r--r--kernel/drivers/cpu.c2
-rw-r--r--kernel/drivers/interrupts.c4
-rw-r--r--kernel/drivers/mbr.c2
-rw-r--r--kernel/drivers/pci.c4
-rw-r--r--kernel/drivers/ps2/keyboard.c6
-rw-r--r--kernel/drivers/ps2/mouse.c4
-rw-r--r--kernel/drivers/ps2/ps2.c4
-rw-r--r--kernel/drivers/rtc.c6
-rw-r--r--kernel/drivers/rtl8139.c177
-rw-r--r--kernel/drivers/serial.c2
-rw-r--r--kernel/drivers/timer.c6
-rw-r--r--kernel/drivers/vmware.c4
-rw-r--r--kernel/features/bus.c2
-rw-r--r--kernel/features/fs.c6
-rw-r--r--kernel/features/logger.c4
-rw-r--r--kernel/features/net.c865
-rw-r--r--kernel/features/proc.c4
-rw-r--r--kernel/features/syscall.c5
-rw-r--r--kernel/inc/drivers/cpu.h2
-rw-r--r--kernel/inc/drivers/pci.h4
-rw-r--r--kernel/inc/drivers/rtl8139.h36
-rw-r--r--kernel/inc/mm.h2
-rw-r--r--kernel/inc/net.h168
-rw-r--r--kernel/main.c9
25 files changed, 46 insertions, 1294 deletions
diff --git a/kernel/drivers/acpi.c b/kernel/drivers/acpi.c
index 6860a43..13c132c 100644
--- a/kernel/drivers/acpi.c
+++ b/kernel/drivers/acpi.c
@@ -1,13 +1,13 @@
// MIT License, Copyright (c) 2020 Marvin Borner
-#include <drivers/acpi.h>
#include <assert.h>
-#include <drivers/cpu.h>
#include <def.h>
+#include <drivers/acpi.h>
+#include <drivers/cpu.h>
#include <mem.h>
#include <print.h>
-int check_sdt(struct sdt_header *header)
+static int check_sdt(struct sdt_header *header)
{
u8 sum = 0;
@@ -17,7 +17,7 @@ int check_sdt(struct sdt_header *header)
return sum == 0;
}
-int check_sdp(struct sdp_header *header)
+static int check_sdp(struct sdp_header *header)
{
u8 sum = 0;
@@ -27,7 +27,7 @@ int check_sdp(struct sdp_header *header)
return sum == 0;
}
-struct rsdp *find_rsdp(void)
+static struct rsdp *find_rsdp(void)
{
// Main BIOS area
for (int i = 0xe0000; i < 0xfffff; i++) {
@@ -45,7 +45,7 @@ struct rsdp *find_rsdp(void)
return NULL;
}
-void *find_sdt(struct rsdt *rsdt, const char *signature)
+static void *find_sdt(struct rsdt *rsdt, const char *signature)
{
u32 entries = (rsdt->header.length - sizeof(rsdt->header)) / 4;
diff --git a/kernel/drivers/cpu.c b/kernel/drivers/cpu.c
index 8927d1d..8694fc2 100644
--- a/kernel/drivers/cpu.c
+++ b/kernel/drivers/cpu.c
@@ -2,8 +2,8 @@
// This file is a wrapper around some CPU asm calls
#include <assert.h>
-#include <drivers/cpu.h>
#include <def.h>
+#include <drivers/cpu.h>
#include <mem.h>
#include <print.h>
diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c
index e9ab0ce..39a59a0 100644
--- a/kernel/drivers/interrupts.c
+++ b/kernel/drivers/interrupts.c
@@ -2,14 +2,14 @@
// TODO: Remove some magic numbers
#include <assert.h>
-#include <drivers/cpu.h>
#include <def.h>
+#include <drivers/cpu.h>
#include <drivers/interrupts.h>
+#include <drivers/serial.h>
#include <mem.h>
#include <mm.h>
#include <print.h>
#include <proc.h>
-#include <drivers/serial.h>
/**
* IDT
diff --git a/kernel/drivers/mbr.c b/kernel/drivers/mbr.c
index c9eba15..d5f7a40 100644
--- a/kernel/drivers/mbr.c
+++ b/kernel/drivers/mbr.c
@@ -1,9 +1,9 @@
// MIT License, Copyright (c) 2021 Marvin Borner
#include <def.h>
-#include <fs.h>
#include <drivers/ide.h>
#include <drivers/mbr.h>
+#include <fs.h>
#include <mem.h>
#include <print.h>
#include <str.h>
diff --git a/kernel/drivers/pci.c b/kernel/drivers/pci.c
index 55806c2..0aab8e5 100644
--- a/kernel/drivers/pci.c
+++ b/kernel/drivers/pci.c
@@ -2,10 +2,10 @@
// Uses parts of the ToAruOS Project, released under the terms of the NCSA
// Copyright (C) 2011-2018 K. Lange
-#include <drivers/cpu.h>
#include <def.h>
-#include <mem.h>
+#include <drivers/cpu.h>
#include <drivers/pci.h>
+#include <mem.h>
CLEAR void pci_write_field(u32 device, int field, u32 value)
{
diff --git a/kernel/drivers/ps2/keyboard.c b/kernel/drivers/ps2/keyboard.c
index bddb280..bf1a520 100644
--- a/kernel/drivers/ps2/keyboard.c
+++ b/kernel/drivers/ps2/keyboard.c
@@ -1,14 +1,14 @@
// MIT License, Copyright (c) 2020 Marvin Borner
-#include <drivers/cpu.h>
#include <def.h>
-#include <errno.h>
+#include <drivers/cpu.h>
#include <drivers/interrupts.h>
+#include <drivers/ps2.h>
+#include <errno.h>
#include <io.h>
#include <mem.h>
#include <print.h>
#include <proc.h>
-#include <drivers/ps2.h>
#include <stack.h>
#include <str.h>
#include <sys.h>
diff --git a/kernel/drivers/ps2/mouse.c b/kernel/drivers/ps2/mouse.c
index 2942e34..680183d 100644
--- a/kernel/drivers/ps2/mouse.c
+++ b/kernel/drivers/ps2/mouse.c
@@ -2,13 +2,13 @@
#include <assert.h>
#include <drivers/cpu.h>
-#include <errno.h>
#include <drivers/interrupts.h>
+#include <drivers/ps2.h>
+#include <errno.h>
#include <io.h>
#include <mem.h>
#include <print.h>
#include <proc.h>
-#include <drivers/ps2.h>
#include <stack.h>
#include <str.h>
#include <sys.h>
diff --git a/kernel/drivers/ps2/ps2.c b/kernel/drivers/ps2/ps2.c
index f8d849b..0206892 100644
--- a/kernel/drivers/ps2/ps2.c
+++ b/kernel/drivers/ps2/ps2.c
@@ -1,10 +1,10 @@
// MIT License, Copyright (c) 2021 Marvin Borner
#include <assert.h>
-#include <drivers/cpu.h>
#include <def.h>
-#include <print.h>
+#include <drivers/cpu.h>
#include <drivers/ps2.h>
+#include <print.h>
#define PS2_TIMEOUT 100
diff --git a/kernel/drivers/rtc.c b/kernel/drivers/rtc.c
index 63d9461..b67829e 100644
--- a/kernel/drivers/rtc.c
+++ b/kernel/drivers/rtc.c
@@ -1,12 +1,12 @@
// MIT License, Copyright (c) 2021 Marvin Borner
-#include <drivers/cpu.h>
#include <def.h>
+#include <drivers/cpu.h>
+#include <drivers/rtc.h>
+#include <drivers/timer.h>
#include <fs.h>
#include <mem.h>
-#include <drivers/rtc.h>
#include <str.h>
-#include <drivers/timer.h>
static u8 rtc_busy(void)
{
diff --git a/kernel/drivers/rtl8139.c b/kernel/drivers/rtl8139.c
deleted file mode 100644
index 8e7bc88..0000000
--- a/kernel/drivers/rtl8139.c
+++ /dev/null
@@ -1,177 +0,0 @@
-// MIT License, Copyright (c) 2020 Marvin Borner
-// Uses parts of the ToAruOS Project, released under the terms of the NCSA
-// Copyright (C) 2011-2018 K. Lange
-
-#include <def.h>
-#include <drivers/cpu.h>
-#include <drivers/interrupts.h>
-#include <drivers/pci.h>
-#include <drivers/rtl8139.h>
-#include <mem.h>
-#include <net.h>
-#include <print.h>
-
-static int rtl_irq = 0;
-static u8 mac[6] = { 0 };
-static u8 *last_packet = NULL;
-static u8 *rtl_rx_buffer = NULL;
-static u32 rtl_device_pci = 0;
-static u32 rtl_iobase = 0;
-static u32 cur_rx = 0;
-
-u8 *rtl8139_get_mac(void)
-{
- if (!rtl_device_pci)
- return NULL;
-
- return mac;
-}
-
-static void rtl8139_receive_packet(void)
-{
- while ((inb(rtl_iobase + RTL_PORT_CMD) & 0x01) == 0) {
- int offset = cur_rx % 0x2000;
-
- u32 *buf_start = (u32 *)((u32)rtl_rx_buffer + offset);
- u32 rx_status = buf_start[0];
- u32 rx_size = rx_status >> 16;
-
- if (rx_status & (0x0020 | 0x0010 | 0x0004 | 0x0002)) {
- print("RX Error\n");
- } else {
- u8 *buf_8 = (u8 *)&(buf_start[1]);
-
- last_packet = malloc(rx_size);
-
- u32 packet_end = (u32)buf_8 + rx_size;
- if (packet_end > (u32)rtl_rx_buffer + 0x2000) {
- u32 s = ((u32)rtl_rx_buffer + 0x2000) - (u32)buf_8;
- memcpy(last_packet, buf_8, s);
- memcpy((void *)((u32)last_packet + s), rtl_rx_buffer, rx_size - s);
- } else {
- memcpy(last_packet, buf_8, rx_size);
- }
-
- u16 *t = (u16 *)(rtl_rx_buffer + cur_rx);
- ethernet_handle_packet((struct ethernet_packet *)last_packet, *(t + 1));
- }
-
- cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
- outw(rtl_iobase + RTL_PORT_RXPTR, cur_rx - 16);
- }
-}
-
-static u8 tsad_array[4] = { 0x20, 0x24, 0x28, 0x2C };
-static u8 tsd_array[4] = { 0x10, 0x14, 0x18, 0x1C };
-static u8 tx_current = 0;
-void rtl8139_send_packet(void *data, u32 len)
-{
- if (!rtl_device_pci)
- return;
-
- outl(rtl_iobase + tsad_array[tx_current], (u32)data);
- outl(rtl_iobase + tsd_array[tx_current++], len);
- if (tx_current > 3)
- tx_current = 0;
-}
-
-static void rtl8139_find(u32 device, u16 vendor_id, u16 device_id, void *extra)
-{
- if ((vendor_id == 0x10ec) && (device_id == 0x8139))
- *((u32 *)extra) = device;
-}
-
-static void rtl8139_irq_handler(struct regs *r)
-{
- UNUSED(r);
- u16 status = inw(rtl_iobase + RTL_PORT_ISR);
- if (!status)
- return;
- outw(rtl_iobase + RTL_PORT_ISR, status);
-
- if (status & 0x01 || status & 0x02)
- rtl8139_receive_packet();
-}
-
-static void rtl8139_init(void)
-{
- if (!rtl_device_pci)
- return;
-
- u16 command_reg = (u16)pci_read_field(rtl_device_pci, PCI_COMMAND, 4);
- if ((command_reg & (1 << 2)) == 0) {
- command_reg |= (1 << 2);
- pci_write_field(rtl_device_pci, PCI_COMMAND, command_reg);
- }
-
- rtl_irq = pci_get_interrupt(rtl_device_pci);
- irq_install_handler(rtl_irq, rtl8139_irq_handler);
-
- u32 rtl_bar0 = pci_read_field(rtl_device_pci, PCI_BAR0, 4);
- /* u32 rtl_bar1 = pci_read_field(rtl_device_pci, PCI_BAR1, 4); */
- rtl_iobase = 0;
-
- if (rtl_bar0 & 0x00000001)
- rtl_iobase = rtl_bar0 & 0xFFFFFFFC;
-
- // Get mac address
- for (int i = 0; i < 6; ++i)
- mac[i] = inb(rtl_iobase + RTL_PORT_MAC + i);
- printf("Mac address: %x:%x:%x:%x:%x:%x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-
- // Activate
- outb(rtl_iobase + RTL_PORT_CONFIG, 0x0);
-
- // Reset
- outb((u16)(rtl_iobase + RTL_PORT_CMD), 0x10);
- while ((inb(rtl_iobase + RTL_PORT_CMD) & 0x10) != 0)
- ;
-
- // Set receive buffer
- rtl_rx_buffer = (u8 *)malloc(0x3000);
- memset(rtl_rx_buffer, 0, 0x3000);
- outl(rtl_iobase + RTL_PORT_RBSTART, (u32)rtl_rx_buffer);
-
- // Enable IRQs
- outw(rtl_iobase + RTL_PORT_IMR, 0x8000 | /* PCI error */
- 0x4000 | /* PCS timeout */
- 0x40 | /* Rx FIFO over */
- 0x20 | /* Rx underrun */
- 0x10 | /* Rx overflow */
- 0x08 | /* Tx error */
- 0x04 | /* Tx okay */
- 0x02 | /* Rx error */
- 0x01 /* Rx okay */
- );
-
- // Configure transmit
- outl(rtl_iobase + RTL_PORT_TCR, 0);
-
- // Configure receive
- outl(rtl_iobase + RTL_PORT_RCR, (0) | /* 8K receive */
- 0x08 | /* broadcast */
- 0x01 /* all physical */
- );
-
- // Enable receive and transmit
- outb(rtl_iobase + RTL_PORT_CMD, 0x08 | 0x04);
-
- // Reset rx statistics
- outl(rtl_iobase + RTL_PORT_RXMISS, 0);
-}
-
-int rtl8139_installed(void)
-{
- return rtl_device_pci != 0;
-}
-
-int rtl8139_install(void)
-{
- pci_scan(&rtl8139_find, -1, &rtl_device_pci);
-
- if (rtl_device_pci) {
- print("Found rtl8139 card\n");
- rtl8139_init();
- }
- return rtl_device_pci;
-}
diff --git a/kernel/drivers/serial.c b/kernel/drivers/serial.c
index 95ac02d..281772d 100644
--- a/kernel/drivers/serial.c
+++ b/kernel/drivers/serial.c
@@ -1,8 +1,8 @@
// MIT License, Copyright (c) 2020 Marvin Borner
#include <assert.h>
-#include <drivers/cpu.h>
#include <def.h>
+#include <drivers/cpu.h>
#include <drivers/serial.h>
#include <str.h>
diff --git a/kernel/drivers/timer.c b/kernel/drivers/timer.c
index c586088..3ddc229 100644
--- a/kernel/drivers/timer.c
+++ b/kernel/drivers/timer.c
@@ -1,13 +1,13 @@
// MIT License, Copyright (c) 2020 Marvin Borner
-#include <drivers/cpu.h>
#include <def.h>
+#include <drivers/cpu.h>
#include <drivers/interrupts.h>
+#include <drivers/rtc.h>
+#include <drivers/timer.h>
#include <io.h>
#include <mem.h>
#include <proc.h>
-#include <drivers/rtc.h>
-#include <drivers/timer.h>
static u32 timer_ticks = 0;
PROTECTED static u8 call_scheduler = 0;
diff --git a/kernel/drivers/vmware.c b/kernel/drivers/vmware.c
index 6f95d02..169865f 100644
--- a/kernel/drivers/vmware.c
+++ b/kernel/drivers/vmware.c
@@ -3,12 +3,12 @@
#include <def.h>
#include <drivers/interrupts.h>
+#include <drivers/ps2.h>
+#include <drivers/vmware.h>
#include <io.h>
#include <mem.h>
#include <print.h>
-#include <drivers/ps2.h>
#include <stack.h>
-#include <drivers/vmware.h>
#define VMWARE_CMD_VERSION 0x0a
diff --git a/kernel/features/bus.c b/kernel/features/bus.c
index d58cee3..3757a3a 100644
--- a/kernel/features/bus.c
+++ b/kernel/features/bus.c
@@ -2,9 +2,9 @@
#include <assert.h>
#include <bus.h>
-#include <drivers/cpu.h>
#include <crypto.h>
#include <def.h>
+#include <drivers/cpu.h>
#include <errno.h>
#include <io.h>
#include <list.h>
diff --git a/kernel/features/fs.c b/kernel/features/fs.c
index c5ebfcd..7e79a42 100644
--- a/kernel/features/fs.c
+++ b/kernel/features/fs.c
@@ -1,13 +1,13 @@
// MIT License, Copyright (c) 2020 Marvin Borner
#include <assert.h>
-#include <drivers/cpu.h>
#include <crypto.h>
#include <def.h>
-#include <errno.h>
-#include <fs.h>
+#include <drivers/cpu.h>
#include <drivers/ide.h>
#include <drivers/mbr.h>
+#include <errno.h>
+#include <fs.h>
#include <mem.h>
#include <mm.h>
#include <print.h>
diff --git a/kernel/features/logger.c b/kernel/features/logger.c
index 23efa93..fd5d730 100644
--- a/kernel/features/logger.c
+++ b/kernel/features/logger.c
@@ -1,13 +1,13 @@
// MIT License, Copyright (c) 2021 Marvin Borner
-#include <drivers/cpu.h>
#include <def.h>
+#include <drivers/cpu.h>
+#include <drivers/serial.h>
#include <errno.h>
#include <io.h>
#include <logger.h>
#include <mem.h>
#include <print.h>
-#include <drivers/serial.h>
static res logger_write(const void *buf, u32 offset, u32 count)
{
diff --git a/kernel/features/net.c b/kernel/features/net.c
deleted file mode 100644
index 9e40ea6..0000000
--- a/kernel/features/net.c
+++ /dev/null
@@ -1,865 +0,0 @@
-// MIT License, Copyright (c) 2020 Marvin Borner
-
-#include <assert.h>
-#include <drivers/cpu.h>
-#include <def.h>
-#include <list.h>
-#include <mem.h>
-#include <net.h>
-#include <drivers/pci.h>
-#include <print.h>
-#include <rand.h>
-#include <drivers/rtl8139.h>
-#include <socket.h>
-#include <str.h>
-#include <drivers/timer.h>
-
-static u32 current_ip_addr = 0;
-static u32 gateway_addr = 0;
-static u32 subnet_mask = 0;
-static u8 gateway_mac[6] = { 0 };
-
-static struct list *tcp_sockets = NULL;
-static struct list *udp_sockets = NULL;
-
-/**
- * Socket functions
- */
-
-static struct list *socket_list(enum socket_type type)
-{
- struct list *list = NULL;
- if (type == S_TCP)
- list = tcp_sockets;
- else if (type == S_UDP)
- list = udp_sockets;
- return list;
-}
-
-static struct socket *socket_get(enum socket_type type, u32 port)
-{
- struct list *list = socket_list(type);
-
- if (!list || !list->head || !port)
- return NULL;
-
- struct node *iterator = list->head;
- while (iterator != NULL && iterator->data != NULL) {
- if (((struct socket *)iterator->data)->src_port == port)
- return iterator->data;
- iterator = iterator->next;
- }
-
- return NULL;
-}
-
-static struct socket *socket_new(enum socket_type type)
-{
- struct list *list = socket_list(type);
-
- if (!list)
- return NULL;
-
- struct socket *socket = malloc(sizeof(*socket));
- memset(socket, 0, sizeof(*socket));
- if (!list_add(list, socket))
- return NULL;
-
- return socket;
-}
-
-static int socket_close(struct socket *socket)
-{
- if (!socket)
- return 0;
-
- struct list *list = socket_list(socket->type);
-
- if (!list)
- return 0;
-
- struct node *iterator = list->head;
- while (iterator != NULL && iterator->data != NULL) {
- if (iterator->data == socket)
- list_remove(list, iterator);
- iterator = iterator->next;
- }
-
- socket->state = S_CLOSED;
- free(socket);
-
- return 1;
-}
-
-/**
- * Helper functions
- */
-
-static u16 next_port(void)
-{
- static u16 port = 49152;
- return port++;
-}
-
-static int same_net(u32 ip_addr)
-{
- if (gateway_addr && ip_addr > 1)
- return (ip_addr & subnet_mask) == (gateway_addr & subnet_mask);
- else
- return 0;
-}
-
-static u16 ip_calculate_checksum(struct ip_packet *packet)
-{
- int array_size = sizeof(*packet) / 2;
- u16 *array = (u16 *)packet;
- u32 sum = 0;
- for (int i = 0; i < array_size; i++) {
- sum += (u32)htons(array[i]);
- }
- u32 carry = sum >> 16;
- sum = sum & 0x0000ffff;
- sum = sum + carry;
- u16 ret = (u16)~sum;
- return ret;
-}
-
-static u16 tcp_calculate_checksum(struct tcp_packet *packet, struct tcp_pseudo_header *header,
- void *data, u32 len)
-{
- u32 sum = 0;
- u16 *s = (u16 *)header;
-
- // TODO: Checksums for options?
- for (int i = 0; i < 6; i++) {
- sum += (u32)ntohs(s[i]);
- if (sum > 0xffff) {
- sum = (sum >> 16) + (sum & 0xffff);
- }
- }
-
- s = (u16 *)packet;
- for (int i = 0; i < 10; i++) {
- sum += (u32)ntohs(s[i]);
- if (sum > 0xffff) {
- sum = (sum >> 16) + (sum & 0xffff);
- }
- }
-
- u16 d_words = (u16)(len / 2);
-
- s = (u16 *)data;
- for (u32 i = 0; i < d_words; ++i) {
- sum += (u32)ntohs(s[i]);
- if (sum > 0xffff) {
- sum = (sum >> 16) + (sum & 0xffff);
- }
- }
-
- if (d_words * 2 != len) {
- u8 *t = (u8 *)data;
- u8 tmp[2];
- tmp[0] = t[d_words * sizeof(u16)];
- tmp[1] = 0;
-
- u16 *f = (u16 *)tmp;
-
- sum += (u32)ntohs(f[0]);
- if (sum > 0xffff) {
- sum = (sum >> 16) + (sum & 0xffff);
- }
- }
-
- return ~(sum & 0xffff) & 0xffff;
-}
-
-static u16 icmp_calculate_checksum(struct icmp_packet *packet)
-{
- u32 sum = 0;
- u16 *s = (u16 *)packet;
-
- for (int i = 0; i < 5; i++)
- sum += s[i];
-
- if (sum > 0xffff)
- sum = (sum >> 16) + (sum & 0xffff);
-
- return (u16)~sum;
-}
-
-static void *dhcp_get_options(struct dhcp_packet *packet, u8 type)
-{
- u8 *options = packet->options + 4;
- u8 curr_type = 0;
- while ((curr_type = *options) != 0xff) {
- u8 len = *(options + 1);
- if (curr_type == type) {
- void *ret = malloc(len);
- memcpy(ret, options + 2, len);
- return ret;
- }
- options += (2 + len);
- }
- return NULL;
-}
-
-/**
- * Requests
- */
-
-static void ethernet_send_packet(u8 *dst, u8 *data, int len, int prot)
-{
- print("Ethernet send packet\n");
- struct ethernet_packet *packet = malloc(sizeof(*packet) + (u32)len);
- memcpy(packet->src, rtl8139_get_mac(), 6);
- memcpy(packet->dst, dst, 6);
- memcpy(packet->data, data, (u32)len);
- packet->type = (u16)htons(prot);
- rtl8139_send_packet(packet, sizeof(*packet) + (u32)len);
- free(packet);
-}
-
-static u8 broadcast_mac[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-static void arp_send_packet(u8 *dst_mac, u32 dst_protocol_addr, u8 opcode)
-{
- print("ARP send packet\n");
- struct arp_packet *packet = malloc(sizeof(*packet));
-
- memcpy(packet->src_mac, rtl8139_get_mac(), 6);
- packet->src_protocol_addr = htonl(current_ip_addr);
- memcpy(packet->dst_mac, dst_mac, 6);
- packet->dst_protocol_addr = htonl(dst_protocol_addr);
- packet->opcode = (u16)htons(opcode);
- packet->hardware_addr_len = 6;
- packet->protocol_addr_len = 4;
- packet->hardware_type = htons(HARDWARE_TYPE_ETHERNET);
- packet->protocol = htons(ETHERNET_TYPE_IP4);
-
- ethernet_send_packet(broadcast_mac, (u8 *)packet, sizeof(*packet), ETHERNET_TYPE_ARP);
- free(packet);
-}
-
-static int arp_lookup(u8 *ret_hardware_addr, u32 ip_addr);
-static void ip_send_packet(u32 dst, void *data, int len, u8 prot)
-{
- print("IP send packet\n");
- struct ip_packet *packet = malloc(sizeof(*packet) + (u32)len);
- memset(packet, 0, sizeof(*packet));
- packet->version_ihl = ((0x4 << 4) | (0x5 << 0));
- packet->length = (u16)sizeof(*packet) + (u16)len;
- packet->id = htons(1); // TODO: IP fragmentation
- /* packet->flags_fragment = htons(1); */
- packet->ttl = 64;
- packet->protocol = prot;
- packet->src = htonl(current_ip_addr);
- packet->dst = htonl(dst);
- packet->length = (u16)htons(sizeof(*packet) + (u32)len);
- packet->checksum = (u16)htons(ip_calculate_checksum(packet));
-
- if (data)
- memcpy(packet->data, data, (u32)len);
-
- u8 zero_hardware_addr[] = { 0, 0, 0, 0, 0, 0 };
- u8 dst_mac[6];
- if (same_net(dst)) {
- scheduler_disable();
- sti();
- int arp_sent = 3;
- while (!arp_lookup(dst_mac, dst)) {
- if (arp_sent) {
- arp_sent--;
- arp_send_packet(zero_hardware_addr, dst, ARP_REQUEST);
- } else {
- break;
- }
- }
- cli();
- scheduler_enable();
- } else {
- memcpy(dst_mac, gateway_mac, 6);
- }
-
- printf("Destination: %x:%x:%x:%x:%x:%x\n", dst_mac[0], dst_mac[1], dst_mac[2], dst_mac[3],
- dst_mac[4], dst_mac[5]);
- ethernet_send_packet(dst_mac, (u8 *)packet, htons(packet->length), ETHERNET_TYPE_IP4);
-
- free(packet);
-}
-
-static void udp_send_packet(struct socket *socket, void *data, int len)
-{
- print("UDP send packet\n");
- if (!socket || socket->state == S_FAILED)
- return;
-
- u32 length = sizeof(struct udp_packet) + (u32)len;
- struct udp_packet *packet = malloc(length);
- memset(packet, 0, sizeof(*packet));
- packet->src_port = (u16)htons(socket->src_port);
- packet->dst_port = (u16)htons(socket->dst_port);
- packet->length = (u16)htons(length);
- packet->checksum = 0; // Optional
-
- if (data)
- memcpy(packet->data, data, (u32)len);
-
- ip_send_packet(socket->ip_addr, packet, (int)length, IP_PROT_UDP);
- free(packet);
-}
-
-//void tcp_send_packet(u32 dst, u16 src_port, u16 dst_port, u16 flags, void *data, int len)
-static void tcp_send_packet(struct socket *socket, u16 flags, void *data, int len)
-{
- print("TCP send packet\n");
-
- if (!socket || socket->state == S_FAILED)
- return;
-
- u32 length = sizeof(struct tcp_packet) + (u32)len;
- struct tcp_packet *packet = malloc(length);
- memset(packet, 0, sizeof(*packet));
- packet->src_port = (u16)htons(socket->src_port);
- packet->dst_port = (u16)htons(socket->dst_port);
- packet->seq_number = htonl(socket->prot.tcp.seq_no);
- packet->ack_number = (flags & TCP_FLAG_ACK) ? htonl(socket->prot.tcp.ack_no) : 0;
- packet->flags = (u16)htons(0x5000 ^ (flags & 0xff));
- packet->window_size = htons(U16_MAX); // TODO: Support TCP windows
- packet->urgent = 0;
- packet->checksum = 0; // Later
-
- if (data)
- memcpy(packet->data, data, (u32)len);
-
- struct tcp_pseudo_header checksum_hd = {
- .src = htonl(current_ip_addr),
- .dst = htonl(socket->ip_addr),
- .zeros = 0,
- .protocol = 6,
- .tcp_len = (u16)htons(length),
- };
- u16 checksum = tcp_calculate_checksum(packet, &checksum_hd, data,
- length - ((u32)htons(packet->flags) >> 12) * 4);
- packet->checksum = (u16)htons(checksum);
-
- ip_send_packet(socket->ip_addr, packet, (int)length, IP_PROT_TCP);
- free(packet);
-}
-
-static void dhcp_make_packet(struct dhcp_packet *packet, u8 msg_type);
-static void dhcp_request(void)
-{
- struct socket *socket = net_open(S_UDP);
- if (!socket || !net_connect(socket, 0xffffffff, 67))
- return;
-
- struct dhcp_packet *packet = malloc(sizeof(*packet));
- memset(packet, 0, sizeof(*packet));
- dhcp_make_packet(packet, 3);
- net_send(socket, packet, sizeof(*packet));
- free(packet);
-}
-
-/**
- * Responses
- */
-
-static void icmp_handle_packet(struct icmp_packet *request_packet, u32 dst)
-{
- struct icmp_packet *packet = malloc(sizeof(*packet));
- memset(packet, 0, sizeof(*packet));
- packet->type = 0; // Ping reponse
- packet->version = 0;
- packet->checksum = 0;
- packet->identifier = request_packet->identifier;
- packet->sequence = request_packet->sequence;
- packet->checksum = icmp_calculate_checksum(packet);
- ip_send_packet(dst, packet, sizeof(*packet), IP_PROT_ICMP);
- free(packet);
-}
-
-static void dhcp_handle_packet(struct dhcp_packet *packet)
-{
- print("DHCP!\n");
- if (packet->op == DHCP_REPLY && htonl(packet->xid) == DHCP_TRANSACTION_IDENTIFIER) {
- u8 *type = dhcp_get_options(packet, 53);
- if (*type == 2) { // Offer
- print("DHCP offer\n");
- dhcp_request();
- } else if (*type == 5) { // ACK
- current_ip_addr = htonl(packet->your_ip);
-
- memcpy(&subnet_mask, dhcp_get_options(packet, 1), 4);
- memcpy(&gateway_addr, dhcp_get_options(packet, 3), 4);
- memcpy(gateway_mac, dhcp_get_options(packet, 3), 4);
- subnet_mask = htonl(subnet_mask);
- gateway_addr = htonl(gateway_addr);
-
- u8 zero_hardware_addr[] = { 0, 0, 0, 0, 0, 0 };
- printf("New IP: %x\n", current_ip_addr);
- printf("Gateway: %x\n", gateway_addr);
- if (same_net(current_ip_addr))
- arp_send_packet(zero_hardware_addr, gateway_addr, ARP_REQUEST);
- /* sti(); */
- /* while (!arp_lookup(gateway_mac, gateway_addr)) */
- /* hlt(); */
- }
- free(type);
- }
-}
-
-// enum tcp_state { TCP_LISTEN, TCP_SYN_SENT, TCP_SYN_RECIEVED, TCP_ESTABLISHED, TCP_FIN_WAIT_1, TCP_FIN_WAIT_2, TCP_CLOSE_WAIT, TCP_CLOSING, TCP_LAST_ACK, TCP_TIME_WAIT, TCP_CLOSED };
-static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len)
-{
- printf("TCP Port: %d\n", ntohs(packet->dst_port));
-
- struct socket *socket = NULL;
- if (!(socket = socket_get(S_TCP, ntohs(packet->dst_port))) || socket->state == S_CLOSED ||
- socket->state == S_FAILED) {
- print("Port isn't mapped!\n");
- return;
- }
- struct tcp_socket *tcp = &socket->prot.tcp;
-
- u32 data_length = (u32)len - (ntohs(packet->flags) >> 12) * 4;
- u16 flags = (u16)ntohs(packet->flags);
-
- u32 recv_ack = ntohl(packet->ack_number);
- u32 recv_seq = ntohl(packet->seq_number);
-
- // TODO: Verify checksum first, then send ACK
-
- if (flags & TCP_FLAG_RST) {
- proc_from_pid(socket->pid)->state = PROC_RUNNING;
- socket->state = S_FAILED;
- tcp->state = 0;
- return;
- }
-
- // Serve
- if (tcp->state == 0 && (flags & 0xff) == TCP_FLAG_SYN) {
- socket->ip_addr = ntohl(dst);
- socket->dst_port = ntohs(packet->src_port);
- tcp->ack_no = recv_seq + 1;
- tcp->seq_no = 1000;
- tcp_send_packet(socket, TCP_FLAG_SYN | TCP_FLAG_ACK, NULL, 0);
- tcp->state++;
- return;
- } else if (tcp->state == 1 && (flags & 0xff) == TCP_FLAG_ACK) {
- tcp->state++;
- return;
- } else if (tcp->state == 2 && (flags & 0xff) == (TCP_FLAG_ACK | TCP_FLAG_PSH)) {
- struct socket_data *sdata = malloc(sizeof(*sdata));
- sdata->length = data_length;
- if (sdata->length) {
- sdata->data = malloc(data_length);
- memcpy(sdata->data, packet->data, data_length);
- } else {
- sdata->data = NULL;
- }
- list_add(socket->packets, sdata);
- proc_from_pid(socket->pid)->state = PROC_RUNNING;
-
- tcp->ack_no += data_length;
- tcp->seq_no++;
-
- tcp_send_packet(socket, TCP_FLAG_ACK, NULL, 0);
-
- socket->state = S_CONNECTED;
- tcp->state++;
- return;
- } else if (tcp->state == 3 && (flags & 0xff) == TCP_FLAG_ACK) {
- tcp->ack_no = recv_seq + 1;
- tcp->seq_no = recv_ack;
-
- socket->state = S_CONNECTED;
- tcp->state++; // ?
- return;
- } else if (tcp->state == 4 && (flags & 0xff) == (TCP_FLAG_ACK | TCP_FLAG_FIN)) {
- tcp->ack_no = recv_seq + 1;
- tcp->seq_no = recv_ack;
-
- tcp_send_packet(socket, TCP_FLAG_FIN | TCP_FLAG_ACK, NULL, 0);
-
- socket->state = S_CONNECTED;
- tcp->state++;
- return;
- } else if (tcp->state == 5 && (flags & 0xff) == TCP_FLAG_ACK) {
- proc_from_pid(socket->pid)->state = PROC_RUNNING;
-
- socket->state = S_CLOSED;
- tcp->state = 0;
- }
-
- // Receive
- if (tcp->state == 0 && (flags & 0xff) == (TCP_FLAG_ACK | TCP_FLAG_SYN)) {
- tcp->ack_no = recv_seq + 1;
- tcp->seq_no = recv_ack;
-
- tcp_send_packet(socket, TCP_FLAG_ACK, NULL, 0);
-
- socket->state = S_CONNECTED;
- tcp->state = 6; // TODO: TCP enum state machine
- return;
- } else if (tcp->state == 6 && (flags & 0xff) == TCP_FLAG_ACK) {
- tcp->ack_no = recv_seq;
- tcp->seq_no = recv_ack;
-
- socket->state = S_CONNECTED;
- tcp->state++;
- return;
- } else if (tcp->state == 7 && flags & TCP_FLAG_ACK) {
- struct socket_data *sdata = malloc(sizeof(*sdata));
- sdata->length = data_length;
- if (sdata->length) {
- sdata->data = malloc(data_length);
- memcpy(sdata->data, packet->data, data_length);
- } else {
- sdata->data = NULL;
- }
- list_add(socket->packets, sdata);
-
- tcp->ack_no += data_length;
- tcp->seq_no = recv_ack;
-
- tcp_send_packet(socket, TCP_FLAG_ACK, NULL, 0);
-
- socket->state = S_CONNECTED;
- return;
- } else if (tcp->state == 8 && (flags & 0xff) == (TCP_FLAG_ACK | TCP_FLAG_FIN)) {
- tcp->ack_no = recv_seq + 1;
- tcp->seq_no = recv_ack;
-
- tcp_send_packet(socket, TCP_FLAG_ACK, NULL, 0);
-
- socket->state = S_CLOSED;
- tcp->state = 0;
- return;
- }
-}
-
-static void udp_handle_packet(struct udp_packet *packet)
-{
- printf("UDP Port: %d\n", ntohs(packet->dst_port));
-
- void *data_ptr = packet->data;
-
- if (ntohs(packet->dst_port) == DHCP_PORT) {
- dhcp_handle_packet(data_ptr);
- return;
- }
-
- struct socket *socket = NULL;
- if (!(socket = socket_get(S_UDP, ntohs(packet->dst_port))) || socket->state == S_CLOSED ||
- socket->state == S_FAILED) {
- print("Port isn't mapped!\n");
- return;
- }
-
- struct socket_data *sdata = malloc(sizeof(*sdata));
- sdata->length = ntohs(packet->length);
- if (sdata->length) {
- sdata->data = malloc(sdata->length);
- memcpy(sdata->data, packet->data, sdata->length);
- } else {
- sdata->data = NULL;
- }
- list_add(socket->packets, sdata);
-}
-
-static void ip_handle_packet(struct ip_packet *packet, int len)
-{
- (void)len;
- switch (packet->protocol) {
- case IP_PROT_ICMP:
- print("ICMP Packet!\n");
- icmp_handle_packet((struct icmp_packet *)packet->data, packet->src);
- break;
- case IP_PROT_TCP:
- print("TCP Packet!\n");
- tcp_handle_packet((struct tcp_packet *)packet->data, packet->src,
- ntohs(packet->length) - sizeof(*packet));
- break;
- case IP_PROT_UDP:
- print("UDP Packet!\n");
- udp_handle_packet((struct udp_packet *)packet->data);
- break;
- default:
- break;
- }
-}
-
-static struct arp_table_entry arp_table[512] = { 0 };
-static int arp_table_size = 0;
-static void arp_handle_packet(struct arp_packet *packet, int len)
-{
- (void)len;
- u8 dst_mac[6];
- memcpy(dst_mac, packet->src_mac, 6);
- u32 dst_protocol_addr = htonl(packet->src_protocol_addr);
- if (ntohs(packet->opcode) == ARP_REQUEST) {
- print("Got ARP request\n");
- if (htonl(packet->dst_protocol_addr) == current_ip_addr) {
- print("Returning ARP request\n");
- arp_send_packet(dst_mac, dst_protocol_addr, ARP_REPLY);
- }
- return;
- } else if (ntohs(packet->opcode) == ARP_REPLY) {
- print("Got ARP reply\n");
- } else {
- printf("Got unknown ARP, opcode = %d\n", packet->opcode);
- return;
- }
-
- // Store
- arp_table[arp_table_size].ip_addr = dst_protocol_addr;
- memcpy(&arp_table[arp_table_size].mac_addr, dst_mac, 6);
- if (arp_table_size < 512)
- arp_table_size++;
- else
- arp_table_size = 0;
-
- if (dst_protocol_addr == gateway_addr)
- memcpy(gateway_mac, dst_mac, 6);
-}
-
-void ethernet_handle_packet(struct ethernet_packet *packet, int len)
-{
- void *data = packet->data;
- int data_len = len - (int)sizeof(*packet);
- if (ntohs(packet->type) == ETHERNET_TYPE_ARP) {
- print("ARP PACKET\n");
- arp_handle_packet(data, data_len);
- } else if (ntohs(packet->type) == ETHERNET_TYPE_IP4) {
- print("IP4 PACKET\n");
- ip_handle_packet(data, data_len);
- } else if (ntohs(packet->type) == ETHERNET_TYPE_IP6) {
- print("IP6 PACKET\n");
- /* ip_handle_packet(data, data_len); */
- } else {
- /* printf("Unknown packet %x\n", ntohs(packet->type)); */
- }
-}
-
-/**
- * DHCP
- */
-
-static void dhcp_make_packet(struct dhcp_packet *packet, u8 msg_type)
-{
- packet->op = DHCP_REQUEST;
- packet->hardware_type = HARDWARE_TYPE_ETHERNET;
- packet->mac_len = 6;
- packet->hops = 0;
- packet->xid = htonl(DHCP_TRANSACTION_IDENTIFIER);
- packet->flags = htons(0x0001);
- memcpy(packet->client_mac, rtl8139_get_mac(), 6);
-
- // Magic Cookie
- u8 *options = packet->options;
- *((u32 *)(options)) = htonl(0x63825363);
- options += 4;
-
- // First option, message type = DHCP_DISCOVER/DHCP_REQUEST
- *(options++) = 53;
- *(options++) = 1;
- *(options++) = msg_type;
-
- // End
- *(options++) = 0xff;
-}
-
-static int dhcp_discover(void)
-{
- print("DHCP discover\n");
- struct socket *socket = net_open(S_UDP);
- if (socket)
- socket->src_port = DHCP_PORT;
- if (!socket || !net_connect(socket, 0xffffffff, 67))
- return 0;
-
- struct dhcp_packet *packet = malloc(sizeof(*packet));
- memset(packet, 0, sizeof(*packet));
- dhcp_make_packet(packet, 1);
- net_send(socket, packet, sizeof(*packet));
- free(packet);
- return 1;
-}
-
-/**
- * ARP
- */
-
-static void arp_lookup_add(u8 *ret_hardware_addr, u32 ip_addr)
-{
- arp_table[arp_table_size].ip_addr = ip_addr;
- memcpy(&arp_table[arp_table_size].mac_addr, ret_hardware_addr, 6);
- if (arp_table_size < 512)
- arp_table_size++;
- else
- arp_table_size = 0;
-}
-
-static int arp_lookup(u8 *ret_hardware_addr, u32 ip_addr)
-{
- for (int i = 0; i < arp_table_size; i++) {
- if (arp_table[i].ip_addr == ip_addr) {
- memcpy(ret_hardware_addr, &arp_table[i].mac_addr, 6);
- return 1;
- }
- }
- return 0;
-}
-
-/**
- * Wrappers
- */
-
-struct socket *net_open(enum socket_type type)
-{
- if (!net_installed())
- return NULL;
-
- struct socket *socket = socket_new(type);
- if (!socket)
- return NULL;
-
- socket->type = type;
- socket->state = S_OPEN;
- socket->packets = list_new();
- socket->pid = proc_current()->pid;
- return socket;
-}
-
-int net_close(struct socket *socket)
-{
- if (!net_installed())
- return 1;
-
- if (socket->state == S_CLOSING)
- return 0;
-
- if (socket->type == S_TCP && socket->state != S_CLOSED) {
- tcp_send_packet(socket, TCP_FLAG_FIN | TCP_FLAG_ACK, NULL, 0);
- socket->state = S_CLOSING;
- socket->prot.tcp.state++;
- return 0;
- }
-
- return socket_close(socket);
-}
-
-int net_connect(struct socket *socket, u32 ip_addr, u16 dst_port)
-{
- if (!net_installed() || socket->state != S_OPEN || !ip_addr || !dst_port)
- return 0;
-
- socket->ip_addr = ip_addr;
- socket->dst_port = dst_port;
- if (!socket->src_port)
- socket->src_port = next_port();
-
- if (socket->type == S_TCP) {
- srand(timer_get());
- socket->prot.tcp.seq_no = rand();
- socket->prot.tcp.ack_no = 0;
- socket->prot.tcp.state = 0;
- socket->state = S_CONNECTING;
- tcp_send_packet(socket, TCP_FLAG_SYN, NULL, 0);
- return 0;
- } else if (socket->type == S_UDP) {
- socket->state = S_CONNECTED;
- return 1;
- } else {
- socket->state = S_FAILED;
- return 0;
- }
-}
-
-void net_send(struct socket *socket, void *data, u32 len)
-{
- if (!net_installed() || socket->state != S_CONNECTED)
- return;
-
- if (socket->type == S_TCP) {
- tcp_send_packet(socket, TCP_FLAG_PSH | TCP_FLAG_ACK, data, len);
- socket->prot.tcp.ack_no += len;
- } else if (socket->type == S_UDP) {
- udp_send_packet(socket, data, len);
- } else {
- print("Unknown socket type!\n");
- }
-}
-
-int net_receive(struct socket *socket, void *buf, u32 len)
-{
- if (!net_installed() || !socket->packets)
- return 0;
-
- u32 offset = 0;
- struct node *iterator = socket->packets->head;
- while (iterator != NULL) {
- struct socket_data *sdata = iterator->data;
- u32 length = sdata->length;
- // TODO: Fix underflow breaking
- if (offset + length < len) {
- memcpy((u8 *)buf + offset, sdata->data, length);
- offset += length;
- } else {
- break;
- };
- iterator = iterator->next;
- }
-
- return offset;
-}
-
-/**
- * Install
- */
-
-int net_installed(void)
-{
- return rtl8139_installed() != 0;
-}
-
-void net_install(void)
-{
- if (!rtl8139_install())
- return;
-
- sti();
-
- tcp_sockets = list_new();
- udp_sockets = list_new();
-
- arp_lookup_add(broadcast_mac, 0xffffffff);
- if (!dhcp_discover()) {
- print("DHCP discover failed\n");
- loop();
- return;
- }
-
- // DHCP timeout (no gateway address)
- sti();
- u32 time = timer_get();
- while (!gateway_addr && timer_get() - time < 1000)
- ;
-
- if (timer_get() - time >= 1000) {
- print("DHCP timeout\n");
- loop();
- return;
- }
-
- // ARP lookup timeout
- sti();
- time = timer_get();
- while (!arp_lookup(gateway_mac, gateway_addr) && timer_get() - time < 1000)
- ;
-
- if (timer_get() - time >= 1000) {
- printf("Gateway ARP timeout at address %x\n", gateway_addr);
- loop();
- return;
- }
-}
diff --git a/kernel/features/proc.c b/kernel/features/proc.c
index 019b0ed..8fc922e 100644
--- a/kernel/features/proc.c
+++ b/kernel/features/proc.c
@@ -2,9 +2,10 @@
#include <assert.h>
#include <drivers/cpu.h>
+#include <drivers/gdt.h>
+#include <drivers/timer.h>
#include <errno.h>
#include <fs.h>
-#include <drivers/gdt.h>
#include <load.h>
#include <mem.h>
#include <mm.h>
@@ -12,7 +13,6 @@
#include <proc.h>
#include <stack.h>
#include <str.h>
-#include <drivers/timer.h>
#define PROC(node) ((struct proc *)node->data)
diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c
index b376a13..9efb849 100644
--- a/kernel/features/syscall.c
+++ b/kernel/features/syscall.c
@@ -1,20 +1,19 @@
// MIT License, Copyright (c) 2020 Marvin Borner
#include <drivers/cpu.h>
+#include <drivers/interrupts.h>
+#include <drivers/timer.h>
#include <errno.h>
#include <fs.h>
-#include <drivers/interrupts.h>
#include <io.h>
#include <load.h>
#include <mem.h>
#include <mm.h>
-#include <net.h>
#include <print.h>
#include <proc.h>
#include <str.h>
#include <sys.h>
#include <syscall.h>
-#include <drivers/timer.h>
static void syscall_handler(struct regs *r)
{
diff --git a/kernel/inc/drivers/cpu.h b/kernel/inc/drivers/cpu.h
index 7ac6074..75e0495 100644
--- a/kernel/inc/drivers/cpu.h
+++ b/kernel/inc/drivers/cpu.h
@@ -5,7 +5,7 @@
#include <def.h>
-static inline void spinlock(u32 *ptr)
+UNUSED_FUNC static inline void spinlock(u32 *ptr)
{
u32 prev;
do
diff --git a/kernel/inc/drivers/pci.h b/kernel/inc/drivers/pci.h
index 9429f29..ac288c6 100644
--- a/kernel/inc/drivers/pci.h
+++ b/kernel/inc/drivers/pci.h
@@ -76,14 +76,14 @@ static inline u8 pci_extract_func(u32 device)
return (u8)(device);
}
-static inline u32 pci_get_addr(u32 device, int field)
+UNUSED_FUNC static inline u32 pci_get_addr(u32 device, int field)
{
return 0x80000000 | (u32)(pci_extract_bus(device) << 16) |
(u32)(pci_extract_slot(device) << 11) | (u32)(pci_extract_func(device) << 8) |
((field)&0xFC);
}
-static inline u32 pci_box_device(int bus, int slot, int func)
+UNUSED_FUNC static inline u32 pci_box_device(int bus, int slot, int func)
{
return (u32)((bus << 16) | (slot << 8) | func);
}
diff --git a/kernel/inc/drivers/rtl8139.h b/kernel/inc/drivers/rtl8139.h
deleted file mode 100644
index 0d748af..0000000
--- a/kernel/inc/drivers/rtl8139.h
+++ /dev/null
@@ -1,36 +0,0 @@
-// MIT License, Copyright (c) 2020 Marvin Borner
-
-#ifndef RTL8139_H
-#define RTL8139_H
-
-#include <def.h>
-
-#define RX_BUF_SIZE 0x2000
-
-#define RTL8139_VENDOR_ID 0x10ec
-#define RTL8139_DEVICE_ID 0x8139
-
-#define RTL_ROK (1 << 0)
-#define RTL_TOK (1 << 2)
-
-#define RTL_PORT_MAC 0x00
-#define RTL_PORT_MAR 0x08
-#define RTL_PORT_TXSTAT 0x10
-#define RTL_PORT_TXBUF 0x20
-#define RTL_PORT_RBSTART 0x30
-#define RTL_PORT_CMD 0x37
-#define RTL_PORT_RXPTR 0x38
-#define RTL_PORT_RXADDR 0x3A
-#define RTL_PORT_IMR 0x3C
-#define RTL_PORT_ISR 0x3E
-#define RTL_PORT_TCR 0x40
-#define RTL_PORT_RCR 0x44
-#define RTL_PORT_RXMISS 0x4C
-#define RTL_PORT_CONFIG 0x52
-
-int rtl8139_install(void);
-int rtl8139_installed(void);
-void rtl8139_send_packet(void *data, u32 len) NONNULL;
-u8 *rtl8139_get_mac(void);
-
-#endif
diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h
index 8f6f06a..0a2bb81 100644
--- a/kernel/inc/mm.h
+++ b/kernel/inc/mm.h
@@ -4,8 +4,8 @@
#define PAGING_H
#include <def.h>
-#include <errno.h>
#include <drivers/interrupts.h>
+#include <errno.h>
struct memory_range {
u32 base;
diff --git a/kernel/inc/net.h b/kernel/inc/net.h
deleted file mode 100644
index 62ff5e5..0000000
--- a/kernel/inc/net.h
+++ /dev/null
@@ -1,168 +0,0 @@
-// MIT License, Copyright (c) 2020 Marvin Borner
-
-#ifndef NET_H
-#define NET_H
-
-#include <def.h>
-#include <socket.h>
-
-#define htonl(l) \
- ((((l)&0xff) << 24) | (((l)&0xff00) << 8) | (((l)&0xff0000) >> 8) | \
- (((l)&0xff000000) >> 24))
-#define htons(s) ((((s)&0xff) << 8) | (((s)&0xff00) >> 8))
-#define ntohl(l) htonl((l))
-#define ntohs(s) htons((s))
-#define ip(a, b, c, d) \
- ((((a)&0xff) << 24) | (((b)&0xff) << 16) | (((c)&0xff) << 8) | (((d)&0xff) << 0))
-
-#define ETHERNET_TYPE_IP4 0x0800
-#define ETHERNET_TYPE_IP6 0x86dd
-#define ETHERNET_TYPE_ARP 0x0806
-
-#define IP_PROT_ICMP 0x01
-#define IP_PROT_TCP 0x06
-#define IP_PROT_UDP 0x11
-
-#define TCP_FLAG_FIN (1 << 0)
-#define TCP_FLAG_SYN (1 << 1)
-#define TCP_FLAG_RST (1 << 2)
-#define TCP_FLAG_PSH (1 << 3)
-#define TCP_FLAG_ACK (1 << 4)
-#define TCP_FLAG_URG (1 << 5)
-#define TCP_FLAG_ECE (1 << 6)
-#define TCP_FLAG_CWR (1 << 7)
-#define TCP_FLAG_NS (1 << 8)
-
-#define ARP_REQUEST 1
-#define ARP_REPLY 2
-
-#define DHCP_REQUEST 1
-#define DHCP_REPLY 2
-#define DHCP_TRANSACTION_IDENTIFIER 0x18122002
-
-#define HARDWARE_TYPE_ETHERNET 0x01
-
-// Hardcoded ports - TODO!
-#define DHCP_PORT 68
-#define DNS_PORT 50053
-
-// Protocol structs
-
-struct ethernet_packet {
- u8 dst[6];
- u8 src[6];
- u16 type;
- u8 data[];
-} PACKED;
-
-struct arp_packet {
- u16 hardware_type;
- u16 protocol;
- u8 hardware_addr_len;
- u8 protocol_addr_len;
- u16 opcode;
- u8 src_mac[6];
- u32 src_protocol_addr;
- u8 dst_mac[6];
- u32 dst_protocol_addr;
-} PACKED;
-
-struct ip_packet {
- u8 version_ihl;
- u8 dscp_ecn;
- u16 length;
- u16 id;
- u16 flags_fragment;
- u8 ttl;
- u8 protocol;
- u16 checksum;
- u32 src;
- u32 dst;
- u8 data[];
-} PACKED;
-
-struct dhcp_packet {
- u8 op;
- u8 hardware_type;
- u8 mac_len;
- u8 hops;
- u32 xid;
- u16 seconds;
- u16 flags;
- u32 client_ip;
- u32 your_ip;
- u32 server_ip;
- u32 gateway_ip;
- u8 client_mac[6];
- u8 reserved[10];
- u8 server_name[64];
- u8 file[128];
- u8 options[64];
-} PACKED;
-
-struct dns_packet {
- u16 qid;
- u16 flags;
- u16 questions;
- u16 answers;
- u16 authorities;
- u16 additional;
- u8 data[];
-} PACKED;
-
-struct udp_packet {
- u16 src_port;
- u16 dst_port;
- u16 length;
- u16 checksum;
- u8 data[];
-} PACKED;
-
-struct tcp_packet {
- u16 src_port;
- u16 dst_port;
- u32 seq_number;
- u32 ack_number;
- u16 flags;
- u16 window_size;
- u16 checksum;
- u16 urgent;
- u8 data[];
-} PACKED;
-
-struct tcp_pseudo_header {
- u32 src;
- u32 dst;
- u8 zeros;
- u8 protocol;
- u16 tcp_len;
- u8 tcp_packet[];
-};
-
-struct icmp_packet {
- u8 type;
- u8 version;
- u16 checksum;
- u16 identifier;
- u16 sequence;
-} PACKED;
-
-// Other structs
-
-struct arp_table_entry {
- u32 ip_addr;
- u64 mac_addr;
-};
-
-void ethernet_handle_packet(struct ethernet_packet *packet, int len) NONNULL;
-
-struct socket *net_open(enum socket_type type);
-int net_close(struct socket *socket) NONNULL;
-int net_connect(struct socket *socket, u32 ip_addr, u16 dst_port) NONNULL;
-void net_send(struct socket *socket, void *data, u32 len) NONNULL;
-int net_receive(struct socket *socket, void *buf, u32 len) NONNULL;
-
-int net_installed(void);
-void net_install(void);
-
-#endif
diff --git a/kernel/main.c b/kernel/main.c
index 76d8412..44198e4 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -1,20 +1,19 @@
// MIT License, Copyright (c) 2020 Marvin Borner
#include <drivers/cpu.h>
-#include <fs.h>
#include <drivers/gdt.h>
#include <drivers/ide.h>
#include <drivers/interrupts.h>
+#include <drivers/pci.h>
+#include <drivers/rtc.h>
+#include <drivers/serial.h>
+#include <fs.h>
#include <io.h>
#include <load.h>
#include <mem.h>
#include <mm.h>
#include <multiboot.h>
-#include <net.h>
-#include <drivers/pci.h>
#include <rand.h>
-#include <drivers/rtc.h>
-#include <drivers/serial.h>
#include <syscall.h>
PROTECTED extern u32 __stack_chk_guard;