Skip to content

Commit f378446

Browse files
committed
fixed some things, still nothing
1 parent c7ddc30 commit f378446

6 files changed

Lines changed: 46 additions & 53 deletions

File tree

kernel/kernel.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,14 @@ void kernel_main(void) {
3636
arp_init();
3737
serial_debug("arp done...");
3838

39-
uint8_t mac_addr[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
39+
uint8_t mac_addr[] = {0};
4040
get_mac_addr(mac_addr);
4141

4242
/*
4343
ethernet_send_packet(mac_addr, (void *)str, strlen(str), 0x0021);
4444
ip_send_packet(ip_addr, (void *)str, strlen(str));
4545
*/
4646

47-
extern uint8_t my_ip[4];
48-
arp_lookup_add(mac_addr, my_ip);
49-
50-
uint8_t dst_ip[] = {127, 0, 0, 1};
51-
icmp_send_echo_request(dst_ip, 1234, 1, 0,
52-
0); // checksum should be the other way
53-
5447
// this does not work i cant seem to set up the network on wsl2 qemu
5548
// the packet looks okay, but i cant reach the dhcp server
5649
/*

net/arp.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#include "network.h"
66
#include "rtl8139.h"
77

8-
arp_table_entry_t arp_table[512];
9-
int arp_table_size;
10-
int arp_table_curr;
8+
#define ARP_TABLE_MAX 16
9+
arp_table_entry_t arp_table[ARP_TABLE_MAX] = {0};
10+
int arp_table_curr = 0;
1111

1212
uint8_t broadcast_mac_address[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1313

@@ -20,12 +20,10 @@ void arp_handle_packet(arp_packet_t *arp_packet) {
2020
// save some packet field
2121
memcpy(dst_hardware_addr, arp_packet->src_hardware_addr, 6);
2222
memcpy(dst_protocol_addr, arp_packet->src_protocol_addr, 4);
23-
// reply arp request, if the ip address matches(have to hard code the IP
24-
// eveywhere, because I don't have dhcp yet)
23+
2524
if (ntohs(arp_packet->opcode) == ARP_REQUEST) {
2625
if (!memcmp(arp_packet->dst_protocol_addr, my_ip, 4)) {
27-
// set source MAC address, IP address (hardcode the IP address as 10.2.2.3
28-
// until we really get one..)
26+
2927
get_mac_addr(arp_packet->src_hardware_addr);
3028
memcpy(arp_packet->src_protocol_addr, my_ip, 4);
3129

@@ -50,24 +48,22 @@ void arp_handle_packet(arp_packet_t *arp_packet) {
5048
ethernet_send_packet(dst_hardware_addr, (uint8_t *)arp_packet,
5149
sizeof(arp_packet_t), ETHERNET_TYPE_ARP);
5250
serial_debug(
53-
"sent an arp reply to %d.%d.%d.%d", arp_packet->dst_protocol_addr[0],
54-
arp_packet->dst_protocol_addr[1], arp_packet->dst_protocol_addr[2],
55-
arp_packet->dst_protocol_addr[3]);
51+
"sent an arp reply to %d.%d.%d.%d %x:%x:%x:%x",
52+
arp_packet->dst_protocol_addr[0], arp_packet->dst_protocol_addr[1],
53+
arp_packet->dst_protocol_addr[2], arp_packet->dst_protocol_addr[3],
54+
arp_packet->dst_hardware_addr[0], arp_packet->dst_hardware_addr[1],
55+
arp_packet->dst_hardware_addr[2], arp_packet->dst_hardware_addr[3]);
5656
}
5757
} else if (ntohs(arp_packet->opcode) == ARP_REPLY) {
58-
return; // reply
58+
// reply
5959
} else {
6060
return; // not for us
6161
}
62-
63-
// now, store the ip-mac address mapping relation
64-
memcpy(&arp_table[arp_table_curr].ip_addr, dst_protocol_addr, 4);
65-
memcpy(&arp_table[arp_table_curr].mac_addr, dst_hardware_addr, 6);
66-
if (arp_table_size < 512)
67-
arp_table_size++;
68-
// wrap around
69-
if (arp_table_curr >= 512)
70-
arp_table_curr = 0;
62+
uint8_t mac[6] = {0};
63+
if (arp_lookup(mac, arp_packet->src_protocol_addr) == 0) {
64+
arp_lookup_add(arp_packet->src_hardware_addr,
65+
arp_packet->src_protocol_addr);
66+
}
7167
}
7268

7369
void arp_send_packet(uint8_t *dst_hardware_addr, uint8_t *dst_protocol_addr) {
@@ -76,6 +72,7 @@ void arp_send_packet(uint8_t *dst_hardware_addr, uint8_t *dst_protocol_addr) {
7672
// set source MAC address, IP address (hardcode the IP address as 10.2.2.3
7773
// until we really get one..)
7874
get_mac_addr(arp_packet.src_hardware_addr);
75+
// TODO
7976
arp_packet.src_protocol_addr[0] = 10;
8077
arp_packet.src_protocol_addr[1] = 0;
8178
arp_packet.src_protocol_addr[2] = 2;
@@ -103,20 +100,20 @@ void arp_send_packet(uint8_t *dst_hardware_addr, uint8_t *dst_protocol_addr) {
103100
sizeof(arp_packet_t), ETHERNET_TYPE_ARP);
104101
}
105102

106-
void arp_lookup_add(uint8_t *ret_hardware_addr, uint8_t *ip_addr) {
103+
void arp_lookup_add(uint8_t *hardware_addr, uint8_t *ip_addr) {
107104
memcpy(&arp_table[arp_table_curr].ip_addr, ip_addr, 4);
108-
memcpy(&arp_table[arp_table_curr].mac_addr, ret_hardware_addr, 6);
109-
if (arp_table_size < 512)
110-
arp_table_size++;
105+
memcpy(&arp_table[arp_table_curr].mac_addr, hardware_addr, 6);
106+
arp_table_curr++;
111107
// wrap around
112-
if (arp_table_curr >= 512)
108+
if (arp_table_curr >= ARP_TABLE_MAX)
113109
arp_table_curr = 0;
114110
}
115111

112+
// supply a destination mac and an ip to search
113+
// 0 not found
116114
int arp_lookup(uint8_t *ret_hardware_addr, uint8_t *ip_addr) {
117-
uint32_t ip_entry = *((uint32_t *)(ip_addr));
118115
for (int i = 0; i < 512; i++) {
119-
if (arp_table[i].ip_addr == ip_entry) {
116+
if (!memcmp((uint8_t *)&arp_table[i].ip_addr, ip_addr, 4)) {
120117
memcpy(ret_hardware_addr, &arp_table[i].mac_addr, 6);
121118
return 1;
122119
}
@@ -125,10 +122,8 @@ int arp_lookup(uint8_t *ret_hardware_addr, uint8_t *ip_addr) {
125122
}
126123

127124
void arp_init() {
128-
uint8_t broadcast_ip[4];
129-
uint8_t broadcast_mac[6];
125+
uint8_t broadcast_ip[4] = {0xff};
126+
uint8_t broadcast_mac[6] = {0xff};
130127

131-
memset(broadcast_ip, 0xff, 4);
132-
memset(broadcast_mac, 0xff, 6);
133128
arp_lookup_add(broadcast_mac, broadcast_ip);
134-
}
129+
}

net/arp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ void arp_handle_packet(arp_packet_t *arp_packet);
2525
void arp_send_packet(uint8_t *dst_hardware_addr, uint8_t *dst_protocol_addr);
2626
int arp_lookup(uint8_t *ret_hardware_addr, uint8_t *ip_addr);
2727
void arp_lookup_add(uint8_t *ret_hardware_addr, uint8_t *ip_addr);
28-
void arp_init();
28+
void arp_init();
29+
void arp_print_table();

net/icmp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ void icmp_handle_packet(icmp_packet_t *packet, uint16_t length,
8383
src_ip[0], src_ip[1], src_ip[2], src_ip[3], ntohs(packet->id),
8484
ntohs(packet->sequence));
8585

86+
// TODO i could use the same packet and send it back
87+
// what the fuck is wrong with the destination mac tho?
8688
icmp_send_packet(src_ip, ICMP_ECHO_REPLY, 0, packet->id, packet->sequence,
8789
packet->data, length - sizeof(icmp_packet_t));
8890
break;

net/ip.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,20 @@ void _ip_send_packet(uint8_t *dst_ip, void *data, uint32_t len,
6060

6161
ip_packet_t *packet = (ip_packet_t *)kmalloc(sizeof(ip_packet_t) + len);
6262

63-
packet->version_ihl = (4 << 4) | 5; // IPv4, header length 5 words (20 bytes)
63+
packet->version_ihl =
64+
(IP_IPV4 << 4) | 5; // IPv4, header length 5 words (20 bytes)
6465
serial_debug("version_ihl = %x", packet->version_ihl);
6566

6667
packet->tos = 0;
6768

6869
uint16_t total_length = sizeof(ip_packet_t) + len;
6970
packet->length = htons(total_length);
70-
serial_debug("total length = %d (%x in network order)", total_length,
71-
packet->length);
71+
// serial_debug("total length = %d (%x in network order)", total_length,
72+
// packet->length);
7273

73-
static uint16_t ip_id = 0;
74-
packet->id = htons(ip_id++);
74+
static uint16_t ip_packet_id = 0;
75+
76+
packet->id = htons(ip_packet_id++);
7577

7678
// no fragmentation
7779
packet->flags_fragment = htons(0);
@@ -85,22 +87,24 @@ void _ip_send_packet(uint8_t *dst_ip, void *data, uint32_t len,
8587

8688
packet->header_checksum = 0;
8789
packet->header_checksum = ip_calculate_checksum(packet);
88-
serial_debug("ip checksum = %x", ntohs(packet->header_checksum));
90+
// serial_debug("ip checksum = %x", ntohs(packet->header_checksum));
8991

9092
memcpy(packet->data, data, len);
9193

92-
uint8_t dst_mac[6];
93-
if (arp_lookup(dst_ip, dst_mac) != 0) {
94+
uint8_t dst_mac[6] = {0};
95+
if (arp_lookup(dst_mac, dst_ip) == 0) {
9496
serial_debug("no MAC found for IP %d.%d.%d.%d - sending ARP request",
9597
dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3]);
9698

9799
uint8_t broadcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
98100
arp_send_packet(broadcast_mac, dst_ip);
99-
// we could queue the packet here for later transmission
101+
// TODO we could queue the packet here for later transmission
102+
// cuz we miss a packet now
103+
// also i should add the mac to the lut when it arrives
100104
kfree(packet);
101105
return;
102106
}
103-
107+
// arp_print_table();
104108
serial_debug("sending to mac %x:%x:%x:%x:%x:%x", dst_mac[0], dst_mac[1],
105109
dst_mac[2], dst_mac[3], dst_mac[4], dst_mac[5]);
106110

net/network.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ int ethernet_send_packet(uint8_t *dst_mac_addr, uint8_t *data, int len,
2929
rtl8139_send_packet(frame, sizeof(ethernet_frame_t) + len);
3030
kfree(frame);
3131

32-
// qemu_printf("Sent an ethernet packet, it looks like this\n");
33-
3432
return len;
3533
}
3634

0 commit comments

Comments
 (0)