forked from tvelliott/charon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethernet.h
259 lines (224 loc) · 5.34 KB
/
ethernet.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//MIT License
//
//Copyright (c) 2018 tvelliott
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef __ETH_RAW_FILTER_H__
#define __ETH_RAW_FILTER_H__
#include <stdint.h>
#define ICMP_ECHO_REQ 8
#define ICMP_ECHO_REPLY 0
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
#define TCP_CTL 0x3f
#define TCP_OPT_END 0
#define TCP_OPT_NOOP 1
#define TCP_OPT_MSS 2
#define TCP_OPT_WIN_SCALE 3
#define TCP_OPT_SACK 4
#define TCP_OPT_TIMESTAMP 8
#define IP_PROTO_ICMP 0x01
#define IP_PROTO_TCP 0x06
#define IP_PROTO_UDP 0x11
#define ETHERNET_MTU 1500
#define TCP_MAX_OPTIONS_LEN 20
#define ETH_IP_TYPE 0x0800
#define ETH_ARP_TYPE 0x0806
#define ETH_BATMAN_TYPE 0x4305
#define ETH_CHARON_TYPE 0x0420
#define CHARON_ACK_MAGIC 0x2984302A
#ifndef htons
#define htons(A) ((((uint16_t)(A) & 0xff00) >> 8) | \
(((uint16_t)(A) & 0x00ff) << 8))
#endif
#ifndef htonl
#define htonl(A) ((((uint32_t)(A) & 0xff000000) >> 24) | \
(((uint32_t)(A) & 0x00ff0000) >> 8) | \
(((uint32_t)(A) & 0x0000ff00) << 8) | \
(((uint32_t)(A) & 0x000000ff) << 24))
#endif
typedef struct __attribute__((packed))
{
uint8_t dst_mac[6];
uint8_t src_mac[6];
uint16_t eth_type;
uint8_t payload[14];
}
eth_hdr_payload;
typedef struct __attribute__((packed))
{
uint8_t dst_mac[6];
uint8_t src_mac[6];
uint16_t eth_type;
}
eth_hdr;
typedef struct __attribute__((packed))
{
uint8_t type;
uint8_t version;
uint8_t ttl;
uint32_t seq;
uint8_t orig_mac[6];
}
batman_special;
typedef struct __attribute__((packed))
{
uint8_t type;
uint8_t version;
uint8_t ttl;
uint8_t tt_version;
uint8_t dst_mac[6];
}
batman_unicast;
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
#define TCP_CTL 0x3f
#define TCP_OPT_END 0
#define TCP_OPT_NOOP 1
#define TCP_OPT_MSS 2
#define TCP_OPT_WIN_SCALE 3
#define TCP_OPT_SACK 4
#define TCP_OPT_TIMESTAMP 8
#define TCP_MAX_OPTIONS_LEN 20
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO_REQUEST 8
typedef struct __attribute__((packed))
{
uint8_t version;
uint8_t tos;
uint16_t len;
uint16_t id;
uint16_t offset;
uint8_t ttl;
uint8_t proto;
uint16_t chksum;
uint32_t src_ip;
uint32_t dst_ip;
}
ip_hdr;
typedef struct __attribute__((packed))
{
uint16_t src_port;
uint16_t dst_port;
uint32_t tcp_seq;
uint32_t tcp_ack;
uint8_t hdr_len;
uint8_t tcp_flags;
uint16_t win_size;
uint16_t chksum;
uint16_t urgent;
uint8_t payload[0];
}
tcp_hdr;
typedef struct __attribute__((packed))
{
uint16_t src_port;
uint16_t dst_port;
uint16_t len;
uint16_t chksum;
uint8_t payload[0];
}
udp_hdr;
typedef struct __attribute__((packed))
{
uint8_t type;
uint8_t code;
uint16_t chksum;
uint8_t payload[0];
}
icmp_hdr;
typedef struct __attribute__((packed))
{
uint16_t hardware_type;
uint16_t protocol_type;
uint8_t hardware_size;
uint8_t protocol_size;
uint16_t opcode;
uint8_t sender_mac[6];
uint32_t sender_ip;
uint8_t target_mac[6];
uint32_t target_ip;
}
arp_hdr;
typedef struct __attribute__((packed))
{
eth_hdr ethhdr;
arp_hdr arphdr;
}
eth_arp_frame;
typedef struct __attribute__((packed))
{
eth_hdr ethhdr;
ip_hdr iphdr;
union __attribute__((packed)) {
tcp_hdr tcphdr;
udp_hdr udphdr;
icmp_hdr icmphdr;
}
ip_proto_payload;
}
eth_frame;
typedef struct __attribute__((packed))
{
eth_hdr ethhdr_bat;
batman_special bathdr_special;
eth_hdr ethhdr;
ip_hdr iphdr;
union __attribute__((packed)) {
tcp_hdr tcphdr;
udp_hdr udphdr;
icmp_hdr icmphdr;
}
ip_proto_payload;
}
batman_frame_special;
typedef struct __attribute__((packed))
{
eth_hdr ethhdr_bat;
batman_unicast bathdr_unicast;
eth_hdr ethhdr;
ip_hdr iphdr;
union __attribute__((packed)) {
tcp_hdr tcphdr;
udp_hdr udphdr;
icmp_hdr icmphdr;
}
ip_proto_payload;
}
batman_frame_unicast;
typedef struct __attribute__((packed))
{
uint32_t first_four;
eth_hdr ethhdr_charon;
union __attribute__((packed)) {
batman_frame_special bat_special;
batman_frame_unicast bat_unicast;
}
bat_frame;
}
charon_frame;
#endif