summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2017-08-02 18:26:23 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2017-08-02 18:26:23 +0200
commitdb1f6609d13a888dd7212c9ed2840846e70a284e (patch)
treee8a4c777bb5feafda1a50feb9c6280a36d0a0dfb /src
parent7d40548496f4d3cdfce828fc8e4f0fd4286e873a (diff)
downloadabaos-db1f6609d13a888dd7212c9ed2840846e70a284e.tar.gz
abaos-db1f6609d13a888dd7212c9ed2840846e70a284e.tar.bz2
added an IPv4 module, currently containg an address to string function
started to add ARP packet definition, reading and printing ARP request now some renames in networking layer
Diffstat (limited to 'src')
-rw-r--r--src/Makefile10
-rw-r--r--src/README2
-rw-r--r--src/drivers/net/rtl8139.c2
-rw-r--r--src/kernel/kernel.c27
-rw-r--r--src/net/arp.c1
-rw-r--r--src/net/arp.h36
-rw-r--r--src/net/ethernet.c2
-rw-r--r--src/net/ethernet.h14
-rw-r--r--src/net/ipv4.c13
-rw-r--r--src/net/ipv4.h18
10 files changed, 109 insertions, 16 deletions
diff --git a/src/Makefile b/src/Makefile
index 32e98e6..feb7685 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -36,7 +36,7 @@ kernel.bin: kernel.elf
kernel.sym: kernel.elf
$(OBJCOPY) --only-keep-debug kernel.elf kernel.sym
-kernel.elf: kernel/entry.o kernel/kernel.o kernel/kernel_asm.o kernel/console.o kernel/vgatext.o kernel/serial.o kernel/memorymanagement.o kernel/tasks.o hardware/port.o hardware/port_asm.o hardware/interrupts.o hardware/interrupts_asm.o hardware/pci.o drivers/driver.o drivers/hdi/mouse.o drivers/hdi/keyboard.o drivers/hdi/ps2/ps2keyboard.o drivers/hdi/ps2/ps2mouse.o drivers/video/video.o drivers/video/vga.o drivers/video/vga_font.o net/ethernet.o drivers/net/network.o drivers/net/rtl8139.o gui/widget.o gui/composite_widget.o gui/window.o gui/desktop.o gui/text_widget.o libc/string.o libc/stdlib.o libc/stdio.o libc/setjmp.o
+kernel.elf: kernel/entry.o kernel/kernel.o kernel/kernel_asm.o kernel/console.o kernel/vgatext.o kernel/serial.o kernel/memorymanagement.o kernel/tasks.o hardware/port.o hardware/port_asm.o hardware/interrupts.o hardware/interrupts_asm.o hardware/pci.o drivers/driver.o drivers/hdi/mouse.o drivers/hdi/keyboard.o drivers/hdi/ps2/ps2keyboard.o drivers/hdi/ps2/ps2mouse.o drivers/video/video.o drivers/video/vga.o drivers/video/vga_font.o net/ethernet.o net/arp.o net/ipv4.o drivers/net/network.o drivers/net/rtl8139.o gui/widget.o gui/composite_widget.o gui/window.o gui/desktop.o gui/text_widget.o libc/string.o libc/stdlib.o libc/stdio.o libc/setjmp.o
$(LD) -o kernel.elf -N -n -Ttext 0x8800 -e kernel_entry --oformat elf32-i386 \
kernel/entry.o \
kernel/kernel.o kernel/kernel_asm.o \
@@ -45,7 +45,7 @@ kernel.elf: kernel/entry.o kernel/kernel.o kernel/kernel_asm.o kernel/console.o
hardware/port.o hardware/port_asm.o \
hardware/interrupts.o hardware/interrupts_asm.o \
hardware/pci.o \
- net/ethernet.o \
+ net/ethernet.o net/arp.o net/ipv4.o \
drivers/driver.o \
drivers/hdi/mouse.o drivers/hdi/keyboard.o \
drivers/hdi/ps2/ps2keyboard.o drivers/hdi/ps2/ps2mouse.o \
@@ -102,6 +102,12 @@ hardware/pci.o: hardware/pci.c hardware/pci.h
network/ethernet.o: network/ethernet.c network/ethernet.h
$(CC) $(CFLAGS) -c -o network/ethernet.o network/ethernet.c
+network/arp.o: network/arp.c network/arp.h
+ $(CC) $(CFLAGS) -c -o network/arp.o network/arp.c
+
+network/ipv4.o: network/ipv4.c network/ipv4.h
+ $(CC) $(CFLAGS) -c -o network/ipv4.o network/ipv4.c
+
drivers/driver.o: drivers/driver.c drivers/driver.h
$(CC) $(CFLAGS) -c -o drivers/driver.o drivers/driver.c
diff --git a/src/README b/src/README
index 43abced..6af8821 100644
--- a/src/README
+++ b/src/README
@@ -81,4 +81,6 @@ net
Network library, currently IPv4 only.
+* ethernet.c - ethernet, MAC address functions
* arp.c - implementation of the ARP protocol
+* ipv4.c - implementation of the IPv4 protocol
diff --git a/src/drivers/net/rtl8139.c b/src/drivers/net/rtl8139.c
index c119112..5b95042 100644
--- a/src/drivers/net/rtl8139.c
+++ b/src/drivers/net/rtl8139.c
@@ -348,7 +348,7 @@ void rtl8139_print_info( void *obj )
if( rtl8139->model == NULL ) {
snprintf( buf2, 30, "unknown submodel 0x%X", rtl8139->model_id );
}
- network_mac_to_string( rtl8139->base.mac_address, buf, 20 );
+ network_mac_address_to_string( rtl8139->base.mac_address, buf, 20 );
printf( "rtl8139 NIC type %s\n at I/O base 0x%X, interrupt 0x%X, MAC: %s\n",
( rtl8139->model != NULL ) ? rtl8139->model : buf2,
rtl8139->pci_descriptor.port_base, rtl8139->pci_descriptor.interrupt, buf );
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index 7cef578..d870375 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -20,6 +20,7 @@
// TODO: move away from main!
#include "network.h"
#include "ethernet.h"
+#include "arp.h"
#include "setjmp.h"
@@ -457,15 +458,27 @@ void handle_network_event( network_event_t *event, void *context )
{
network_ethernet_packet_t *packet = (network_ethernet_packet_t *)event->data;
- printf( "NETWORK DATA: len: %d, type: %X\n", event->length,
+ char src[NETWORK_ETHERNET_MAC_ADDR_STRING];
+ char dst[NETWORK_ETHERNET_MAC_ADDR_STRING];
+ network_mac_address_to_string( packet->header.src_addr, src, NETWORK_ETHERNET_MAC_ADDR_STRING );
+ network_mac_address_to_string( packet->header.dst_addr, dst, NETWORK_ETHERNET_MAC_ADDR_STRING );
+ printf( "NETWORK DATA: src: %s, dst: %s, len: %d, type: %X\n",
+ src, dst,
+ event->length,
packet->header.type );
- if( packet->header.type == NETWORK_ETHER_TYPE_ARP ) {
- char src[NETWORK_ETHERNET_MAC_ADDR_STRING];
- char dst[NETWORK_ETHERNET_MAC_ADDR_STRING];
- network_mac_to_string( packet->header.src_addr, src, NETWORK_ETHERNET_MAC_ADDR_STRING );
- network_mac_to_string( packet->header.dst_addr, dst, NETWORK_ETHERNET_MAC_ADDR_STRING );
- printf( "ARP: src: %s dst: %s\n", src, dst );
+ if( packet->header.type == NETWORK_ETHER_PROTOCOL_TYPE_ARP ) {
+ network_arp_packet_t *arp = (network_arp_packet_t *)&packet->data;
+ char src2[NETWORK_PROTOCOL_IPV4_ADDR_STRING];
+ char dst2[NETWORK_PROTOCOL_IPV4_ADDR_STRING];
+
+ network_mac_address_to_string( arp->source_hardware_address, src, NETWORK_ETHERNET_MAC_ADDR_STRING );
+ network_mac_address_to_string( arp->destination_hardware_address, dst, NETWORK_ETHERNET_MAC_ADDR_STRING );
+ network_ipv4_address_to_string( arp->source_protocol_address, src2, NETWORK_PROTOCOL_IPV4_ADDR_STRING );
+ network_ipv4_address_to_string( arp->destination_protocol_address, dst2, NETWORK_PROTOCOL_IPV4_ADDR_STRING );
+
+ printf( "ARP: hw src: %s, dst: %s, ip src: %s, dst: %s, oper: %d\n",
+ src, dst, src2, dst2, arp->operation );
}
// we must free if data if we no longer need it
diff --git a/src/net/arp.c b/src/net/arp.c
new file mode 100644
index 0000000..1f30b09
--- /dev/null
+++ b/src/net/arp.c
@@ -0,0 +1 @@
+#include "arp.h"
diff --git a/src/net/arp.h b/src/net/arp.h
new file mode 100644
index 0000000..ba8de42
--- /dev/null
+++ b/src/net/arp.h
@@ -0,0 +1,36 @@
+#ifndef ARP_H
+#define ARP_H
+
+#include "ethernet.h"
+#include "ipv4.h"
+
+#if defined( __TINYC__ )
+#pragma pack(1)
+#endif
+
+typedef uint16_t network_hardware_type_t;
+
+// TODO: network order not host oder, so for now swap it
+#define ARP_HARDWARE_TYPE_ETHERNET (network_hardware_type_t)0x0100
+
+#define ARP_OPERATION_REQUEST 0x0100
+#define ARP_OPERATION_ANSWER 0x0200
+
+// TODO: fixed size structure, works for Ethernet MACs and IPv4 addresses
+typedef struct {
+ network_hardware_type_t hardware_type;
+ network_ether_type_t protocol_type;
+ uint8_t hardware_address_len;
+ uint8_t protocol_address_len;
+ uint16_t operation;
+ network_mac_address_t source_hardware_address;
+ network_ipv4_address_t source_protocol_address;
+ network_mac_address_t destination_hardware_address;
+ network_ipv4_address_t destination_protocol_address;
+} __attribute__( ( packed ) ) network_arp_packet_t;
+
+#if defined( __TINYC__ )
+#pragma pack()
+#endif
+
+#endif // ARP_H
diff --git a/src/net/ethernet.c b/src/net/ethernet.c
index 48db20c..bf8bb93 100644
--- a/src/net/ethernet.c
+++ b/src/net/ethernet.c
@@ -2,7 +2,7 @@
#include "stdio.h"
-char *network_mac_to_string( network_mac_address_t mac, char *buf, size_t buflen )
+char *network_mac_address_to_string( network_mac_address_t mac, char *buf, size_t buflen )
{
snprintf( buf, buflen, "%X:%X:%X:%X:%X:%X",
mac.byte[0], mac.byte[1], mac.byte[2],
diff --git a/src/net/ethernet.h b/src/net/ethernet.h
index a2726e7..faeddf5 100644
--- a/src/net/ethernet.h
+++ b/src/net/ethernet.h
@@ -5,14 +5,18 @@
#define NETWORK_ETHERNET_MAC_ADDR_STRING 6 * 3 + 1
+// hardware_address_len
+#define NETWORK_HARDWARE_ETHERNET_ADDRESS_LENGTH 6
+
typedef struct {
- uint8_t byte[6];
+ uint8_t byte[NETWORK_HARDWARE_ETHERNET_ADDRESS_LENGTH];
} network_mac_address_t;
-typedef uint16_t ether_type_t;
+typedef uint16_t network_ether_type_t;
// TODO: network order not host oder, so for now swap it
-#define NETWORK_ETHER_TYPE_ARP 0x0608
+#define NETWORK_ETHER_PROTOCOL_TYPE_ARP (network_ether_type_t)0x0608
+#define NETWORK_ETHER_PROTOCOL_TYPE_IPV4 (network_ether_type_t)0x0008
#if defined( __TINYC__ )
#pragma pack(1)
@@ -21,7 +25,7 @@ typedef uint16_t ether_type_t;
typedef struct {
network_mac_address_t dst_addr;
network_mac_address_t src_addr;
- ether_type_t type;
+ network_ether_type_t type;
} __attribute__( ( packed ) ) network_ethernet_packet_header_t;
typedef struct {
@@ -33,6 +37,6 @@ typedef struct {
#pragma pack()
#endif
-char *network_mac_to_string( network_mac_address_t mac, char *buf, size_t buflen );
+char *network_mac_address_to_string( network_mac_address_t mac, char *buf, size_t buflen );
#endif // ETHERNET_H
diff --git a/src/net/ipv4.c b/src/net/ipv4.c
new file mode 100644
index 0000000..38ae7da
--- /dev/null
+++ b/src/net/ipv4.c
@@ -0,0 +1,13 @@
+#include "ipv4.h"
+
+#include "stdio.h"
+
+char *network_ipv4_address_to_string( network_ipv4_address_t addr, char *buf, size_t buflen )
+{
+ snprintf( buf, buflen, "%d.%d.%d.%d",
+ addr.byte[0], addr.byte[1],
+ addr.byte[2], addr.byte[3] );
+
+ return buf;
+}
+
diff --git a/src/net/ipv4.h b/src/net/ipv4.h
new file mode 100644
index 0000000..acf7f7f
--- /dev/null
+++ b/src/net/ipv4.h
@@ -0,0 +1,18 @@
+#ifndef IPV4_H
+#define IPV4_H
+
+#include "stdint.h"
+#include "stddef.h"
+
+#define NETWORK_PROTOCOL_IPV4_ADDR_STRING 4 * 4 + 1
+
+// protocol_address_len
+#define NETWORK_PROTOCOL_IPV4_ADDRESS_LENGTH 4
+
+typedef struct {
+ uint8_t byte[NETWORK_PROTOCOL_IPV4_ADDRESS_LENGTH];
+} network_ipv4_address_t;
+
+char *network_ipv4_address_to_string( network_ipv4_address_t addr, char *buf, size_t buflen );
+
+#endif // IPV4_H