aboutsummaryrefslogtreecommitdiff
path: root/kernel/inc/net.h
blob: 2d0dbe71b0138be53382d980c26c3f3916e4e216 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// MIT License, Copyright (c) 2020 Marvin Borner

#ifndef NET_H
#define NET_H

#include <def.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 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 ARP_REQUEST 1
#define ARP_REPLY 2

#define DHCP_REQUEST 1
#define DHCP_REPLY 2
#define DHCP_TRANSACTION_IDENTIFIER 0x55555555

#define HARDWARE_TYPE_ETHERNET 0x01

// Protocol structs

struct ethernet_packet {
	u8 dst[6];
	u8 src[6];
	u16 type;
	u8 data[];
} __attribute__((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;
} __attribute__((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[];
} __attribute__((packed));

struct dhcp_packet {
	u8 op;
	u8 hardware_type;
	u8 hardware_addr_len;
	u8 hops;
	u32 xid;
	u16 seconds;
	u16 flags;
	u32 client_ip;
	u32 your_ip;
	u32 server_ip;
	u32 gateway_ip;
	u8 client_hardware_addr[16];
	u8 server_name[64];
	u8 file[128];
	u8 options[64];
} __attribute__((packed));

struct udp_packet {
	u16 src_port;
	u16 dst_port;
	u16 length;
	u16 checksum;
	u8 data[];
} __attribute__((packed));

struct icmp_packet {
	u8 type;
	u8 version;
	u16 checksum;
} __attribute__((packed));

// Other structs

struct arp_table_entry {
	u32 ip_addr;
	u64 mac_addr;
};

void ethernet_handle_packet(struct ethernet_packet *packet, int len);
void net_install();

#endif