aboutsummaryrefslogtreecommitdiff
path: root/kernel/inc/net.h
blob: 62ff5e5f71ed808d4a319681e149b8c09fd89aaa (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// 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