forked from TaraHoleInIt/SLIP8266
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathether.cpp
More file actions
379 lines (304 loc) · 10.9 KB
/
ether.cpp
File metadata and controls
379 lines (304 loc) · 10.9 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <ESP8266WiFi.h>
#include <lwip/netif.h>
#include <lwip/err.h>
#include "ether.h"
#include "ipv4.h"
#include "util.h"
#include "slip.h"
#include "mydebug.h"
extern "C" {
#include <netif/wlan_lwip_if.h>
#include <user_interface.h>
}
#define ARPResponseTimeoutMS 250
#define ARPTableEntries 10
static struct ARPEntry ARPTable[ ARPTableEntries ];
extern netif_linkoutput_fn OriginalLinkoutputFn;
extern netif_output_fn OriginalOutputFn;
extern struct netif* ESPif;
static void OnARPPacket( struct ARPHeader* ARP );
static void ARP_RespondToRequest( struct ARPHeader* ARP );
uint8_t BroadcastMACAddress[ MACAddressLen ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
/*
* Returns 1 if the given IP is on the same subnet as us.
*/
int AreWeOnTheSameSubnet( uint32_t IPAddress ) {
uint32_t A = OurIPAddress & OurNetmask;
uint32_t B = IPAddress & OurNetmask;
return A == B ? 1 : 0;
}
/*
* Sets up an ethernet frame header, pretty straightforward.
*/
int PrepareEthernetHeader( struct EtherFrame* FrameHeader, const uint8_t* SourceMAC, const uint8_t* DestMAC, uint16_t LengthOrType ) {
memcpy( FrameHeader->SourceMAC, SourceMAC, MACAddressLen );
memcpy( FrameHeader->DestMAC, DestMAC, MACAddressLen );
FrameHeader->LengthOrType = htons( LengthOrType );
return sizeof( struct EtherFrame );
}
/*
* Called when the network interface receives an ethernet frame.
*/
void OnDataReceived( const uint8_t* Data, int Length ) {
struct EtherFrame* EHeader = ( struct EtherFrame* ) Data;
struct ip_packet* IPHeader = ( struct ip_packet* ) &Data[ sizeof( struct EtherFrame ) ];
switch ( htons( EHeader->LengthOrType ) ) {
case EtherType_IPv4: {
if ( IPHeader->DestIP == OurIPAddress )
SLIP_WritePacket( &Data[ sizeof( struct EtherFrame ) ], Length - sizeof( struct EtherFrame ) );
//SLIP_QueuePacketForWrite( &Data[ sizeof( struct EtherFrame ) ], Length - sizeof( struct EtherFrame ) );
//OnIPv4Packet( &Data[ sizeof( struct EtherFrame ) ], Length, ( const struct EtherFrame* ) Data );
break;
}
case EtherType_ARP: {
if ( Length >= ( int ) sizeof( struct ARPHeader ) )
OnARPPacket( ( struct ARPHeader* ) ( ( ( uint8_t* ) Data ) + sizeof( struct EtherFrame ) ) );
break;
}
default: break;
};
}
/*
* Writes the given ethernet frame to the network interface.
*/
err_t EtherWrite( void* Data, int Length ) {
struct pbuf* OutPBuf = NULL;
err_t Result = ERR_OK;
OutPBuf = pbuf_alloc( PBUF_LINK, Length, PBUF_RAM );
if ( OutPBuf ) {
memcpy( OutPBuf->payload, Data, Length );
Result = OriginalLinkoutputFn( ESPif, OutPBuf );
pbuf_free( OutPBuf );
}
return Result;
}
/*
* Simple enough, call this to respond to an ARP request.
*/
static void ARP_RespondToRequest( struct ARPHeader* ARP ) {
uint8_t Buffer[ 512 ];
struct ARPHeader* Response = ( struct ARPHeader* ) &Buffer[ sizeof( struct EtherFrame ) ];
struct EtherFrame* Frame = ( struct EtherFrame* ) Buffer;
memcpy( Frame->SourceMAC, OurMACAddress, MACAddressLen );
memset( Frame->DestMAC, 0xFF, MACAddressLen );
Frame->LengthOrType = htons( EtherType_ARP );
Response->HardwareType = htons( 1 );
Response->ProtocolType = htons( EtherType_IPv4 );
Response->HWAddressLen = MACAddressLen;
Response->ProtoAddressLen = 4;
Response->Operation = htons( 2 );
memcpy( Response->TargetMAC, ARP->SenderMAC, MACAddressLen );
memcpy( Response->SenderMAC, OurMACAddress, MACAddressLen );
Response->SenderIP = OurIPAddress;
Response->TargetIP = ARP->SenderIP;
EtherWrite( Buffer, sizeof( struct EtherFrame ) + sizeof( struct ARPHeader ) );
}
/*
* Called when we receive an ARP packet (response or request)
*/
static void OnARPPacket( struct ARPHeader* ARP ) {
int IsRequest = htons( ARP->Operation ) == 1 ? 1 : 0;
/*
* Is someone requesting our MAC address?
* If so, send a timely response.
*/
if ( IsRequest ) {
if ( ARP->TargetIP == OurIPAddress ) {
ARP_RespondToRequest( ARP );
}
}
/* Add sender to ARP table if the sender MAC and IP addresses are not zero or broadcast addresses. */
if ( ! IsMACBroadcast( ARP->SenderMAC ) && ! IsMACZero( ARP->SenderMAC ) && ARP->SenderIP && ARP->SenderIP != 0xFFFFFFFF )
ARP_AddToTable( ARP->SenderMAC, ARP->SenderIP );
/* Ditto except for target */
if ( ! IsMACBroadcast( ARP->TargetMAC ) && ! IsMACZero( ARP->TargetMAC ) && ARP->TargetIP && ARP->TargetIP != 0xFFFFFFFF )
ARP_AddToTable( ARP->TargetMAC, ARP->TargetIP );
}
static void ARP_AddDefaultRoutes( void ) {
ARP_AddStaticRoute( OurIPAddress, OurMACAddress );
}
/*
* Zeroes the ARP table and adds a static entry for ourself.
*/
void ARP_Init( void ) {
ARP_ClearTable( );
ARP_AddDefaultRoutes( );
}
/*
* Adds a static entry to the ARP table.
*/
struct ARPEntry* ARP_AddStaticRoute( uint32_t IP, uint8_t* MACAddress ) {
struct ARPEntry* Entry = NULL;
if ( ( Entry = ARP_FindFreeEntry( ) ) != NULL ) {
Entry->TimeAdded = 0xFFFFFFFF;
Entry->IPAddress = IP;
Entry->Set = 1;
memcpy( Entry->MACAddress, MACAddress, MACAddressLen );
}
return Entry;
}
/*
* Called once every "frame" or run throught the main loop.
*/
void ARP_Tick( void ) {
static uint32_t NextFlush = 0;
uint32_t Now = millis( );
if ( Now >= NextFlush ) {
NextFlush = Now + TimeToFlushARP;
DebugPrintf( "Flushing ARP cache.\n" );
ARP_Init( );
}
}
/*
* Clears out all entries in the ARP table.
*/
void ARP_ClearTable( void ) {
memset( ARPTable, 0, sizeof( ARPTable ) );
}
/*
* Finds the oldest entry in the ARP table so it can be reused.
*/
struct ARPEntry* ARP_FindOldestEntry( void ) {
struct ARPEntry* OldestEntry = &ARPTable[ 0 ];
uint32_t Oldest = 0;
int i = 0;
for ( i = 0; i < ARPTableEntries; i++ ) {
if ( Oldest == 0 || ( ARPTable[ i ].TimeAdded <= Oldest && ARPTable[ i ].Set ) ) {
Oldest = ARPTable[ i ].TimeAdded;
OldestEntry = &ARPTable[ i ];
}
}
return OldestEntry;
}
/* Looks for an unused "slot" in the ARP table, returns a pointer
* to it if found, otherwise NULL.
*/
struct ARPEntry* ARP_FindFreeEntry( void ) {
int i = 0;
for ( i = 0; i < ARPTableEntries; i++ ) {
if ( ARPTable[ i ].Set == 0 )
return &ARPTable[ i ];
}
return ARP_FindOldestEntry( );
}
/* Look in the ARP table for an entry by IP address and returns
* a pointer to it, otherwise NULL.
*/
struct ARPEntry* ARP_FindEntryByIP( uint32_t IP ) {
int i = 0;
for ( i = 0; i < ARPTableEntries; i++ ) {
if ( ARPTable[ i ].Set && ARPTable[ i ].IPAddress == IP ) {
return &ARPTable[ i ];
}
}
return NULL;
}
/* Look in the ARP table for an entry by MAC address and returns
* a pointer to it, otherwise NULL.
*/
struct ARPEntry* ARP_FindEntryByMAC( const uint8_t* MAC ) {
int i = 0;
for ( i = 0; i < ARPTableEntries; i++ ) {
if ( ARPTable[ i ].Set && memcmp( ARPTable[ i ].MACAddress, MAC, MACAddressLen ) == 0 )
return &ARPTable[ i ];
}
return NULL;
}
/* Adds an entry into the ARP table given the supplies MAC address and IP.
* Returns a pointer to a new table entry or an existing one if one was already previously.
*/
struct ARPEntry* ARP_AddToTable( const uint8_t* MAC, uint32_t IP ) {
struct ARPEntry* Ptr = NULL;
/* Try to find an existing entry by IP address */
if ( ( Ptr = ARP_FindEntryByIP( IP ) ) == NULL ) {
/* Try to find an existing entry by MAC address */
if ( ( Ptr = ARP_FindEntryByMAC( MAC ) ) == NULL ) {
/* If none found, add a new one */
if ( ( Ptr = ARP_FindFreeEntry( ) ) != NULL ) {
memcpy( Ptr->MACAddress, MAC, MACAddressLen );
Ptr->TimeAdded = millis( );
Ptr->IPAddress = IP;
Ptr->Set = 1;
}
}
}
return Ptr;
}
/*
* Writes the ARP table contents to the console.
*/
void ARP_DumpTableToConsole( void ) {
#if 0
static char Text[ 256 ];
char MACString[ 32 ];
char IPString[ 32 ];
int Entries = 0;
int i = 0;
Serial.println( "ARP Table contents:" );
for ( i = 0; i < ARPTableEntries; i++ ) {
if ( ARPTable[ i ].Set ) {
Entries++;
MACsprintf( ARPTable[ i ].MACAddress, MACString, sizeof( MACString ) );
IPsprintf( ARPTable[ i ].IPAddress, IPString, sizeof( IPString ) );
snprintf( Text, sizeof( Text ), "MAC: %s\tIP: %s", MACString, IPString );
Serial.println( Text );
}
}
snprintf( Text, sizeof( Text ), "End of ARP table contents. %d entries.", Entries );
Serial.println( Text );
#endif
}
/*
* Given an IP address, send out an ARP request over the wire.
*/
void ARP_RequestMACFromIP( uint32_t IP ) {
struct ARPEntry* Entry = NULL;
static uint8_t Buffer[ sizeof( struct EtherFrame ) + sizeof( struct ARPHeader ) ];
struct ARPHeader* ARPPacket = ( struct ARPHeader* ) &Buffer[ sizeof( struct EtherFrame ) ];
struct EtherFrame* Frame = ( struct EtherFrame* ) Buffer;
memset( Buffer, 0, sizeof( Buffer ) );
/* If the entry already exists in the ARP table, do nothing */
if ( ( Entry = ARP_FindEntryByIP( IP ) ) != NULL )
return;
/* Setup the ethernet frame which is just the source MAC, destination MAC, and frame type */
WiFi.macAddress( Frame->SourceMAC );
memset( Frame->DestMAC, 0xFF, sizeof( Frame->DestMAC ) );
Frame->LengthOrType = htons( EtherType_ARP ); /* ARP */
ARPPacket->HardwareType = htons( 1 ); /* Ethernet */
ARPPacket->ProtocolType = htons( EtherType_IPv4 ); /* TCP/IP */
ARPPacket->HWAddressLen = MACAddressLen; /* 6 Byte hardware address (MAC) */
ARPPacket->ProtoAddressLen = 4; /* 4 Bytes for IPV4 address */
ARPPacket->Operation = htons( 1 ); /* Request */
WiFi.macAddress( ARPPacket->SenderMAC ); /* Our hardware address */
memset( ARPPacket->TargetMAC, 0xFF, MACAddressLen); /* Broadcast address */
ARPPacket->TargetIP = IP; /* Who we're lookin' for */
ARPPacket->SenderIP = WiFi.localIP( ); /* Who we are */
EtherWrite( Buffer, sizeof( Buffer ) );
}
/*
* Given an IP address, send out an ARP request over the wire.
* Blocks for (ARPResponseTimeoutMS) before giving up.
*/
int ARP_RequestMACFromIP_Blocking( uint32_t IP, uint8_t* MAC ) {
struct ARPEntry* ARP = NULL;
uint32_t Now = millis( );
uint32_t Timeout = Now + ARPResponseTimeoutMS;
/*
* Check to see if it already exists in the ARP table.
* If it does, this will return WAY sooner.
*/
ARP = ARP_FindEntryByIP( IP );
if ( ARP == NULL )
ARP_RequestMACFromIP( IP );
/*
* Don't block forever, in fact this is awful and should go away.
*/
while ( Timeout >= Now && ARP == NULL ) {
ARP = ARP_FindEntryByIP( IP );
Now = millis( );
yield( );
}
if ( ARP && MAC )
memcpy( MAC, ARP->MACAddress, MACAddressLen );
return ARP == NULL ? 0 : 1;
}