summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/FreeRTOS_UDP_IP.c
blob: 804cbb9f041479364725e825421b78ae73204ef5 (plain)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
/*
 * FreeRTOS+UDP V1.0.4
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * 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. If you wish to use our Amazon
 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
 *
 * 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.
 *
 * http://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 *
 * 1 tab == 4 spaces!
 */

/* Standard includes. */
#include <stdint.h>

/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "timers.h"

/* FreeRTOS+UDP includes. */
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_DHCP.h"
#include "NetworkInterface.h"
#include "NetworkBufferManagement.h"

/* Sanity check the configuration. */
#if configUSE_TIMERS != 1
	#error configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h to use this file
#endif

#if configTICK_RATE_HZ > 1000
	#error configTICK_RATE_HZ must be less than 1000 to use FreeRTOS+UDP
#endif

#if ( ipconfigEVENT_QUEUE_LENGTH < ( ipconfigNUM_NETWORK_BUFFERS + 5 ) )
	#error The ipconfigEVENT_QUEUE_LENGTH parameter must be at least ipconfigNUM_NETWORK_BUFFERS + 5
#endif

#if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS == 1 && ipconfigSUPPORT_OUTGOING_PINGS == 1
	#error ipconfigSUPPORT_OUTGOING_PINGS can only be set to 1 if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS is set to 0 as IP fragmentation is not supported for ICMP (ping) packets
#endif

#if ( ipconfigNETWORK_MTU < 46 )
	#error ipconfigNETWORK_MTU must be at least 46.
#endif
/*-----------------------------------------------------------*/

/* The IP header length in bytes. */
#define ipIP_HEADER_LENGTH		( 20 )

/* IP protocol definitions. */
#define ipPROTOCOL_ICMP			( 1 )
#define ipPROTOCOL_UDP			( 17 )

/* ICMP protocol definitions. */
#define ipICMP_ECHO_REQUEST		( ( uint16_t ) 8 )
#define ipICMP_ECHO_REPLY		( ( uint16_t ) 0 )

/* The expected IP version and header length coded into the IP header itself. */
#define ipIP_VERSION_AND_HEADER_LENGTH_BYTE ( ( uint8_t ) 0x45 )

/* Time delay between repeated attempts to initialise the network hardware. */
#define ipINITIALISATION_RETRY_DELAY	( ( ( TickType_t ) 3000 ) / portTICK_RATE_MS )

/* The local MAC address is accessed from within xDefaultPartUDPPacketHeader,
rather than duplicated in its own variable. */
#define ipLOCAL_MAC_ADDRESS ( xDefaultPartUDPPacketHeader )

/* The local IP address is accessed from within xDefaultPartUDPPacketHeader,
rather than duplicated in its own variable. */
#define ipLOCAL_IP_ADDRESS_POINTER ( ( uint32_t * ) &( xDefaultPartUDPPacketHeader[ 20 ] ) )

/* Defines how often the ARP timer callback function is executed.  The time is
shorted in the Windows simulator as simulated time is not real time. */
#ifdef _WINDOWS_
	#define ipARP_TIMER_PERIOD_MS	( 500 ) /* For windows simulator builds. */
#else
	#define ipARP_TIMER_PERIOD_MS	( 10000 )
#endif

/* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
driver will filter incoming packets and only pass the stack those packets it
considers need processing.  In this case ipCONSIDER_FRAME_FOR_PROCESSING() can
be #defined away.  If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 0
then the Ethernet driver will pass all received packets to the stack, and the
stack must do the filtering itself.  In this case ipCONSIDER_FRAME_FOR_PROCESSING
needs to call eConsiderFrameForProcessing. */
#if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0
	#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
#else
	#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
#endif

/* When the age of an entry in the ARP table reaches this value (it counts down
to zero, so this is an old entry) an ARP request will be sent to see if the
entry is still valid and can therefore be refreshed. */
#define ipMAX_ARP_AGE_BEFORE_NEW_ARP_REQUEST		( 3 )

/* Number of bits to shift to divide by 8.  Used to remove the need for a
divide. */
#define ipSHIFT_TO_DIVIDE_BY_8 						( 3U )

/* The bit set in the IP header flags to indicate that the IP packet contains
a fragment of the eventual total payload, and that more fragments will follow. */
#define ipMORE_FRAGMENTS_FLAG_BIT 					( 0x2000U )

/* ICMP packets are sent using the same function as UDP packets.  The port
number is used to distinguish between the two, as 0 is an invalid UDP port. */
#define ipPACKET_CONTAINS_ICMP_DATA					( 0 )

/* The character used to fill ICMP echo requests, and therefore also the
character expected to fill ICMP echo replies. */
#define ipECHO_DATA_FILL_BYTE						'x'

#if( ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN )
	#define ipFRAGMENT_OFFSET_BIT_MASK				( ( uint16_t ) 0xff0f ) /* The bits in the two byte IP header field that make up the fragment offset value. */
#else
	#define ipFRAGMENT_OFFSET_BIT_MASK				( ( uint16_t ) 0x0fff ) /* The bits in the two byte IP header field that make up the fragment offset value. */
	#if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS == 1
		#warning Fragment offsets have not been tested on big endian machines.
	#endif /* ipconfigCAN_FRAGMENT_OUTGOING_PACKETS */
#endif /* ipconfigBYTE_ORDER */

/*-----------------------------------------------------------*/
/* Miscellaneous structure and definitions. */
/*-----------------------------------------------------------*/

typedef struct xARP_CACHE_TABLE_ROW
{
	uint32_t ulIPAddress;		/* The IP address of an ARP cache entry. */
	xMACAddress_t xMACAddress;  /* The MAC address of an ARP cache entry. */
	uint8_t ucAge;				/* A value that is periodically decremented but can also be refreshed by active communication.  The ARP cache entry is removed if the value reaches zero. */
} xARPCacheRow_t;

typedef enum
{
	eARPCacheMiss = 0,			/* An ARP table lookup did not find a valid entry. */
	eARPCacheHit,				/* An ARP table lookup found a valid entry. */
	eCantSendPacket				/* There is no IP address, or an ARP is still in progress, so the packet cannot be sent. */
} eARPLookupResult_t;

typedef enum
{
	eNotFragment = 0,			/* The IP packet being sent is not part of a fragment. */
	eFirstFragment,				/* The IP packet being sent is the first in a set of fragmented packets. */
	eFollowingFragment			/* The IP packet being sent is part of a set of fragmented packets. */
} eIPFragmentStatus_t;


/*-----------------------------------------------------------*/

/*
 * Called when new data is available from the network interface.
 */
static void prvProcessEthernetPacket( xNetworkBufferDescriptor_t * const pxNetworkBuffer );

/*
 * Called when the application has generated a UDP packet to send.
 */
static void prvProcessGeneratedPacket( xNetworkBufferDescriptor_t * const pxNetworkBuffer );

/*
 * Processes incoming ARP packets.
 */
static eFrameProcessingResult_t prvProcessARPPacket( xARPPacket_t * const pxARPFrame );

/*
 * Process incoming IP packets.
 */
static eFrameProcessingResult_t prvProcessIPPacket( const xIPPacket_t * const pxIPPacket, xNetworkBufferDescriptor_t * const pxNetworkBuffer );

/*
 * Process incoming ICMP packets.
 */
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
	static eFrameProcessingResult_t prvProcessICMPPacket( xICMPPacket_t * const pxICMPPacket );
#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */

/*
 * Swap the source and destination addresses in an already constructed Ethernet
 * frame, and send the frame to the network.
 */
static void prvReturnEthernetFrame( xNetworkBufferDescriptor_t * const pxNetworkBuffer );

/*
 * Return the checksum generated over usDataLengthBytes from pucNextData.
 */
static uint16_t prvGenerateChecksum( const uint8_t * const pucNextData, const uint16_t usDataLengthBytes, BaseType_t xChecksumIsOffloaded );

/*
 * The callback function that is assigned to all periodic processing timers -
 * namely the DHCP timer and the ARP timer.
 */
void vIPFunctionsTimerCallback( xTimerHandle xTimer );

/*
 * Reduce the age count in each entry within the ARP cache.  An entry is no
 * longer considered valid and is deleted if its age reaches zero.
 */
static void prvAgeARPCache( void );

/*
 * If ulIPAddress is already in the ARP cache table then reset the age of the
 * entry back to its maximum value.  If ulIPAddress is not already in the ARP
 * cache table then add it - replacing the oldest current entry if there is not
 * a free space available.
 */
static void prvRefreshARPCacheEntry( const xMACAddress_t * const pxMACAddress, const uint32_t ulIPAddress );

/*
 * Creates the pseudo header necessary then generate the checksum over the UDP
 * packet.  Returns the calculated checksum.
 */
static uint16_t prvGenerateUDPChecksum( const xUDPPacket_t * const pxUDPPacket, BaseType_t xChecksumIsOffloaded );

/*
 * Look for ulIPAddress in the ARP cache.  If the IP address exists, copy the
 * associated MAC address into pxMACAddress, refresh the ARP cache entry's
 * age, and return eARPCacheHit.  If the IP address does not exist in the ARP
 * cache return eARPCacheMiss.  If the packet cannot be sent for any reason
 * (maybe DHCP is still in process, or the addressing needs a gateway but there
 * isn't a gateway defined) then return eCantSendPacket.
 */
static eARPLookupResult_t prvGetARPCacheEntry( uint32_t *pulIPAddress, xMACAddress_t * const pxMACAddress );

/*
 * The main UDP/IP stack processing task.  This task receives commands/events
 * from the network hardware drivers, tasks that are using sockets, and software
 * timers (such as the ARP timer).
 */
static void prvIPTask( void *pvParameters );

/*
 * Send out an ARP request for the IP address contained in pxNetworkBuffer, and
 * add an entry into the ARP table that indicates that an ARP reply is
 * outstanding so re-transmissions can be generated.
 */
static void prvGenerateARPRequestPacket( xNetworkBufferDescriptor_t * const pxNetworkBuffer );

/*
 * Called when outgoing packets are fragmented and require a fragment offset in
 * their IP headers.  Set the fragment offset (which includes the IP flags) and
 * length from the data passed in the pxFragmentParameters structure.
 */
 #if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS == 1
	static void prvCalculateFragmentOffsetAndLength( xIPFragmentParameters_t *pxFragmentParameters, uint16_t *pusFragmentOffset, uint16_t *pusFragmentLength );
#endif /* ipconfigCAN_FRAGMENT_OUTGOING_PACKETS */

/*
 * Complete the pxUDPPacket header with the information passed in
 * pxNetworkBuffer.  ucSocketOptions are passed in case the options include
 * disabling the checksum.
 */
static void prvCompleteUDPHeader( xNetworkBufferDescriptor_t *pxNetworkBuffer, xUDPPacket_t *pxUDPPacket, uint8_t ucSocketOptions );

/*
 * Send the event eEvent to the IP task event queue, using a block time of
 * zero.  Return pdPASS if the message was sent successfully, otherwise return
 * pdFALSE.
*/
static BaseType_t prvSendEventToIPTask( eIPEvent_t eEvent );

/*
 * Generate and send an ARP request for the IP address passed in ulIPAddress.
 */
static void prvOutputARPRequest( uint32_t ulIPAddress );

/*
 * Turns around an incoming ping request to convert it into a ping reply.
 */
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 )
	static eFrameProcessingResult_t prvProcessICMPEchoRequest( xICMPPacket_t * const pxICMPPacket );
#endif /* ipconfigREPLY_TO_INCOMING_PINGS */

/*
 * Processes incoming ping replies.  The application callback function
 * vApplicationPingReplyHook() is called with the results.
 */
#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
	static void prvProcessICMPEchoReply( xICMPPacket_t * const pxICMPPacket );
#endif /* ipconfigSUPPORT_OUTGOING_PINGS */

/*
 * Called to create a network connection when the stack is first started, or
 * when the network connection is lost.
 */
static void prvProcessNetworkDownEvent( void );

/*-----------------------------------------------------------*/

/* The queue used to pass events into the UDP task for processing. */
xQueueHandle xNetworkEventQueue = NULL;

/* The ARP cache. */
static xARPCacheRow_t xARPCache[ ipconfigARP_CACHE_ENTRIES ];

/* The timer that triggers ARP events. */
static xTimerHandle xARPTimer = NULL;

/* Used to ensure network down events cannot be missed when they cannot be
posted to the network event queue because the network event queue is already
full. */
static BaseType_t xNetworkDownEventPending = pdFALSE;

/* For convenience, a MAC address of all zeros and another of all 0xffs are
defined const for quick reference. */
static const xMACAddress_t xNullMACAddress = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };
static const xMACAddress_t xBroadcastMACAddress = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };

/* Part of the Ethernet and IP headers are always constant when sending an IPv4
UDP packet.  This array defines the constant parts, allowing this part of the
packet to be filled in using a simple memcpy() instead of individual writes. */
uint8_t xDefaultPartUDPPacketHeader[] =
{
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 	/* Ethernet source MAC address. */
	0x08, 0x00, 							/* Ethernet frame type. */
	ipIP_VERSION_AND_HEADER_LENGTH_BYTE, 	/* ucVersionHeaderLength. */
	0x00, 									/* ucDifferentiatedServicesCode. */
	0x00, 0x00, 							/* usLength. */
	0x00, 0x00, 							/* usIdentification. */
	0x00, 0x00, 							/* usFragmentOffset. */
	updconfigIP_TIME_TO_LIVE, 				/* ucTimeToLive */
	ipPROTOCOL_UDP, 						/* ucProtocol. */
	0x00, 0x00, 							/* usHeaderChecksum. */
	0x00, 0x00, 0x00, 0x00 					/* Source IP address. */
};

/* Part of the Ethernet and ARP headers are always constant when sending an IPv4
ARP packet.  This array defines the constant parts, allowing this part of the
packet to be filled in using a simple memcpy() instead of individual writes. */
static const uint8_t xDefaultPartARPPacketHeader[] =
{
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 	/* Ethernet destination address. */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 	/* Ethernet source address. */
	0x08, 0x06, 							/* Ethernet frame type (ipARP_TYPE). */
	0x00, 0x01, 							/* usHardwareType (ipARP_HARDWARE_TYPE_ETHERNET). */
	0x08, 0x00,								/* usProtocolType. */
	ipMAC_ADDRESS_LENGTH_BYTES, 			/* ucHardwareAddressLength. */
	ipIP_ADDRESS_LENGTH_BYTES, 				/* ucProtocolAddressLength. */
	0x00, 0x01, 							/* usOperation (ipARP_REQUEST). */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 	/* xSenderHardwareAddress. */
	0x00, 0x00, 0x00, 0x00, 				/* ulSenderProtocolAddress. */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00 	/* xTargetHardwareAddress. */
};

/* Structure that stores the netmask, gateway address and DNS server addresses. */
static xNetworkAddressingParameters_t xNetworkAddressing = { 0, 0, 0, 0 };

/*-----------------------------------------------------------*/

static void prvIPTask( void *pvParameters )
{
xIPStackEvent_t xReceivedEvent;

	/* Just to prevent compiler warnings about unused parameters. */
	( void ) pvParameters;

	/* Create the ARP timer, but don't start it until the network has
	connected. */
	xARPTimer = xTimerCreate( "ARPTimer", ( ipARP_TIMER_PERIOD_MS / portTICK_RATE_MS ), pdTRUE, ( void * ) eARPTimerEvent, vIPFunctionsTimerCallback );
	configASSERT( xARPTimer );

	/* Generate a dummy message to say that the network connection has gone
	down.  This will cause this task to initialise the network interface.  After
	this it is the responsibility of the network interface hardware driver to
	send this message if a previously connected network is disconnected. */
	FreeRTOS_NetworkDown();

	/* Loop, processing IP events. */
	for( ;; )
	{
		/* Wait until there is something to do. */
		if( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, portMAX_DELAY ) == pdPASS )
		{
			iptraceNETWORK_EVENT_RECEIVED( xReceivedEvent.eEventType );

			switch( xReceivedEvent.eEventType )
			{
				case eNetworkDownEvent :
					/* Attempt to establish a connection. */
					prvProcessNetworkDownEvent();
					break;

				case eEthernetRxEvent :
					/* The network hardware driver has received a new packet.
					A pointer to the received buffer is located in the pvData
					member of the received event structure. */
					prvProcessEthernetPacket( ( xNetworkBufferDescriptor_t * ) ( xReceivedEvent.pvData ) );
					break;

				case eARPTimerEvent :
					/* The ARP timer has expired, process the ARP cache. */
					prvAgeARPCache();
					break;

				case eStackTxEvent :
					/* The network stack has generated a packet to send.  A
					pointer to the generated buffer is located in the pvData
					member of the received event structure. */
					prvProcessGeneratedPacket( ( xNetworkBufferDescriptor_t * ) ( xReceivedEvent.pvData ) );
					break;

				case eDHCPEvent:
					/* The DHCP state machine needs processing. */
					#if ipconfigUSE_DHCP == 1
					{
						vDHCPProcess( pdFALSE, ( xMACAddress_t * ) ipLOCAL_MAC_ADDRESS, ipLOCAL_IP_ADDRESS_POINTER, &xNetworkAddressing );
					}
					#endif
					break;

				default :
					/* Should not get here. */
					break;
			}

			if( xNetworkDownEventPending != pdFALSE )
			{
				/* A network down event could not be posted to the network
				event queue because the queue was full.  Try posting again. */
				FreeRTOS_NetworkDown();
			}
		}
	}
}
/*-----------------------------------------------------------*/

void FreeRTOS_NetworkDown( void )
{
static const xIPStackEvent_t xNetworkDownEvent = { eNetworkDownEvent, NULL };
const TickType_t xDontBlock = 0;

	/* Simply send the network task the appropriate event. */
	if( xQueueSendToBack( xNetworkEventQueue, &xNetworkDownEvent, xDontBlock ) != pdPASS )
	{
		xNetworkDownEventPending = pdTRUE;
	}
	else
	{
		xNetworkDownEventPending = pdFALSE;
	}

	iptraceNETWORK_DOWN();
}
/*-----------------------------------------------------------*/

BaseType_t FreeRTOS_NetworkDownFromISR( void )
{
static const xIPStackEvent_t xNetworkDownEvent = { eNetworkDownEvent, NULL };
BaseType_t xHigherPriorityTaskWoken = pdFALSE;

	/* Simply send the network task the appropriate event. */
	if( xQueueSendToBackFromISR( xNetworkEventQueue, &xNetworkDownEvent, &xHigherPriorityTaskWoken ) != pdPASS )
	{
		xNetworkDownEventPending = pdTRUE;
	}
	else
	{
		xNetworkDownEventPending = pdFALSE;
	}
	iptraceNETWORK_DOWN();

	return xHigherPriorityTaskWoken;
}
/*-----------------------------------------------------------*/

void *FreeRTOS_GetUDPPayloadBuffer( size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks )
{
xNetworkBufferDescriptor_t *pxNetworkBuffer;
void *pvReturn;

	/* Cap the block time.  The reason for this is explained where
	ipconfigMAX_SEND_BLOCK_TIME_TICKS is defined (assuming an official
	FreeRTOSIPConfig.h header file is being used). */
	if( xBlockTimeTicks > ipconfigMAX_SEND_BLOCK_TIME_TICKS )
	{
		xBlockTimeTicks = ipconfigMAX_SEND_BLOCK_TIME_TICKS;
	}

	/* Obtain a network buffer with the required amount of storage. */
	pxNetworkBuffer = pxNetworkBufferGet( sizeof( xUDPPacket_t ) + xRequestedSizeBytes, xBlockTimeTicks );

	if( pxNetworkBuffer != NULL )
	{
		/* Leave space for the UPD header. */
		pvReturn = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET ] );
	}
	else
	{
		pvReturn = NULL;
	}

	return ( void * ) pvReturn;
}
/*-----------------------------------------------------------*/

void FreeRTOS_ReleaseUDPPayloadBuffer( void *pvBuffer )
{
uint8_t *pucBuffer;

	/* Obtain the network buffer from the zero copy pointer. */
	pucBuffer = ( uint8_t * ) pvBuffer;
	pucBuffer -= ( ipBUFFER_PADDING + sizeof( xUDPPacket_t ) );

	vNetworkBufferRelease( * ( ( xNetworkBufferDescriptor_t ** ) pucBuffer ) );
}
/*-----------------------------------------------------------*/

uint8_t * FreeRTOS_GetMACAddress( void )
{
	return ipLOCAL_MAC_ADDRESS;
}
/*-----------------------------------------------------------*/

BaseType_t FreeRTOS_IPInit( const uint8_t ucIPAddress[ ipIP_ADDRESS_LENGTH_BYTES ], const uint8_t ucNetMask[ ipIP_ADDRESS_LENGTH_BYTES ], const uint8_t ucGatewayAddress[ ipIP_ADDRESS_LENGTH_BYTES ], const uint8_t ucDNSServerAddress[ ipIP_ADDRESS_LENGTH_BYTES ], const uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] )
{
static BaseType_t xReturn = pdFALSE;

	/* Only create the IP event queue if it has not already been created, in
	case this function is called more than once. */
	if( xNetworkEventQueue == NULL )
	{
		xNetworkEventQueue = xQueueCreate( ipconfigEVENT_QUEUE_LENGTH, sizeof( xIPStackEvent_t ) );
		configASSERT( xNetworkEventQueue );
		vQueueAddToRegistry( xNetworkEventQueue, "NetEvnt" );
	}

	if( xNetworkBuffersInitialise() == pdPASS )
	{
		if( xNetworkEventQueue != NULL )
		{
			/* xReturn is static to ensure the network interface is not
			initialised	twice. */
			if( xReturn == pdFALSE )
			{
				/* Store the local IP and MAC address. */
				xNetworkAddressing.ulDefaultIPAddress = FreeRTOS_inet_addr_quick( ucIPAddress[ 0 ], ucIPAddress[ 1 ], ucIPAddress[ 2 ], ucIPAddress[ 3 ] );
				xNetworkAddressing.ulNetMask = FreeRTOS_inet_addr_quick( ucNetMask[ 0 ], ucNetMask[ 1 ], ucNetMask[ 2 ], ucNetMask[ 3 ] );
				xNetworkAddressing.ulGatewayAddress = FreeRTOS_inet_addr_quick( ucGatewayAddress[ 0 ], ucGatewayAddress[ 1 ], ucGatewayAddress[ 2 ], ucGatewayAddress[ 3 ] );
				xNetworkAddressing.ulDNSServerAddress = FreeRTOS_inet_addr_quick( ucDNSServerAddress[ 0 ], ucDNSServerAddress[ 1 ], ucDNSServerAddress[ 2 ], ucDNSServerAddress[ 3 ] );

				#if ipconfigUSE_DHCP == 1
				{
					/* The IP address is not set until DHCP completes. */
					*ipLOCAL_IP_ADDRESS_POINTER = 0x00UL;
				}
				#else
				{
					*ipLOCAL_IP_ADDRESS_POINTER = xNetworkAddressing.ulDefaultIPAddress;

					/* Ensure the gateway is on the same subnet as the IP
					address. */
					configASSERT( ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) == ( xNetworkAddressing.ulGatewayAddress & xNetworkAddressing.ulNetMask ) );
				}
				#endif /* ipconfigUSE_DHCP == 1 */

				/* The MAC address is stored in the start of the default packet
				header fragment, which is used when sending UDP packets. */
				memcpy( ( void * ) ipLOCAL_MAC_ADDRESS, ( void * ) ucMACAddress, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );

				/* Prepare the sockets interface. */
				FreeRTOS_SocketsInit();

				/* Create the task that processes Ethernet and stack events. */
				xReturn = xTaskCreate( prvIPTask, "UDP/IP", ipconfigUDP_TASK_STACK_SIZE_WORDS, NULL, ipconfigUDP_TASK_PRIORITY, NULL );
			}
		}
	}

	return xReturn;
}
/*-----------------------------------------------------------*/

void FreeRTOS_GetAddressConfiguration( uint32_t *pulIPAddress, uint32_t *pulNetMask, uint32_t *pulGatewayAddress, uint32_t *pulDNSServerAddress )
{
	if( pulIPAddress != NULL )
	{
		*pulIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
	}

	if( pulNetMask != NULL )
	{
		*pulNetMask = xNetworkAddressing.ulNetMask;
	}

	if( pulGatewayAddress != NULL )
	{
		*pulGatewayAddress = xNetworkAddressing.ulGatewayAddress;
	}

	if( pulDNSServerAddress != NULL )
	{
		*pulDNSServerAddress = xNetworkAddressing.ulDNSServerAddress;
	}
}
/*-----------------------------------------------------------*/

#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )

	BaseType_t FreeRTOS_SendPingRequest( uint32_t ulIPAddress, size_t xNumberOfBytesToSend, TickType_t xBlockTimeTicks )
	{
	xNetworkBufferDescriptor_t *pxNetworkBuffer;
	xICMPHeader_t *pxICMPHeader;
	BaseType_t xReturn = pdFAIL;
	static uint16_t usSequenceNumber = 0;
	uint8_t *pucChar;
	xIPStackEvent_t xStackTxEvent = { eStackTxEvent, NULL };

		if( xNumberOfBytesToSend < ( ( ipconfigNETWORK_MTU - sizeof( xIPHeader_t ) ) - sizeof( xICMPHeader_t ) ) )
		{
			pxNetworkBuffer = pxNetworkBufferGet( xNumberOfBytesToSend + sizeof( xICMPPacket_t ), xBlockTimeTicks );

			if( pxNetworkBuffer != NULL )
			{
				pxICMPHeader = ( xICMPHeader_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipIP_PAYLOAD_OFFSET ] );
				usSequenceNumber++;

				/* Fill in the basic header information. */
				pxICMPHeader->ucTypeOfMessage = ipICMP_ECHO_REQUEST;
				pxICMPHeader->ucTypeOfService = 0;
				pxICMPHeader->usIdentifier = usSequenceNumber;
				pxICMPHeader->usSequenceNumber = usSequenceNumber;
				pxICMPHeader->usChecksum = 0;

				/* Find the start of the data. */
				pucChar = ( uint8_t * ) pxICMPHeader;
				pucChar += sizeof( xICMPHeader_t );

				/* Just memset the data to a fixed value. */
				memset( ( void * ) pucChar, ( int ) ipECHO_DATA_FILL_BYTE, xNumberOfBytesToSend );

				/* The message is complete, calculate the checksum. */
				pxICMPHeader->usChecksum = prvGenerateChecksum( ( uint8_t * ) pxICMPHeader, ( uint16_t ) ( xNumberOfBytesToSend + sizeof( xICMPHeader_t ) ), pdFALSE );

				/* Complete the network buffer information. */
				pxNetworkBuffer->ulIPAddress = ulIPAddress;
				pxNetworkBuffer->usPort = ipPACKET_CONTAINS_ICMP_DATA;
				pxNetworkBuffer->xDataLength = xNumberOfBytesToSend + sizeof( xICMPHeader_t );

				/* Send to the stack. */
				xStackTxEvent.pvData = pxNetworkBuffer;
				if( xQueueSendToBack( xNetworkEventQueue, &xStackTxEvent, xBlockTimeTicks ) != pdPASS )
				{
					vNetworkBufferRelease( pxNetworkBuffer );
					iptraceSTACK_TX_EVENT_LOST( ipSTACK_TX_EVENT );
				}
				else
				{
					xReturn = usSequenceNumber;
				}
			}
		}
		else
		{
			/* The requested number of bytes will not fit in the available space
			in the network buffer.  Outgoing fragmentation is only supported for
			UDP packets. */
		}

		return xReturn;
	}

#endif /* ipconfigSUPPORT_OUTGOING_PINGS == 1 */

/*-----------------------------------------------------------*/

static BaseType_t prvSendEventToIPTask( eIPEvent_t eEvent )
{
xIPStackEvent_t xEventMessage;
const TickType_t xDontBlock = 0;
BaseType_t xReturn;

	xEventMessage.eEventType = eEvent;
	xReturn = xQueueSendToBack( xNetworkEventQueue, &xEventMessage, xDontBlock );

	if( xReturn != pdPASS )
	{
		iptraceSTACK_TX_EVENT_LOST( ipARP_TIMER_EVENT );
	}

	return xReturn;
}
/*-----------------------------------------------------------*/

void vIPFunctionsTimerCallback( xTimerHandle xTimer )
{
eIPEvent_t eMessage;

	/* This time can be used to send more than one type of message to the IP
	task.  The message ID is stored in the ID of the timer.  The strange
	casting is to avoid compiler warnings. */
	eMessage = ( eIPEvent_t ) ( ( BaseType_t ) pvTimerGetTimerID( xTimer ) );

	prvSendEventToIPTask( eMessage );
}
/*-----------------------------------------------------------*/

static void prvOutputARPRequest( uint32_t ulIPAddress )
{
xNetworkBufferDescriptor_t *pxNetworkBuffer;

	/* This is called from the context of the IP event task, so a block time
	must not be used. */
	pxNetworkBuffer = pxNetworkBufferGet( sizeof( xARPPacket_t ), 0 );
	if( pxNetworkBuffer != NULL )
	{
		pxNetworkBuffer->ulIPAddress = ulIPAddress;
		prvGenerateARPRequestPacket( pxNetworkBuffer );
		xNetworkInterfaceOutput( pxNetworkBuffer );
	}
}
/*-----------------------------------------------------------*/

static void prvAgeARPCache( void )
{
BaseType_t x;

	/* Loop through each entry in the ARP cache. */
	for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
	{
		/* If the entry is valid (its age is greater than zero). */
		if( xARPCache[ x ].ucAge > 0U )
		{
			/* Decrement the age value of the entry in this ARP cache table row.
			When the age reaches zero it is no longer considered valid. */
			( xARPCache[ x ].ucAge )--;

			/* If the entry has a MAC address of 0, then it is waiting an ARP
			reply, and the ARP request should be retransmitted. */
			if( memcmp( ( void * ) &xNullMACAddress, ( void * ) &( xARPCache[ x ].xMACAddress ), sizeof( xMACAddress_t ) ) == 0 )
			{
				prvOutputARPRequest( xARPCache[ x ].ulIPAddress );
			}
			else if( xARPCache[ x ].ucAge <= ipMAX_ARP_AGE_BEFORE_NEW_ARP_REQUEST )
			{
				/* This entry will get removed soon.  See if the MAC address is
				still valid to prevent this happening. */
				iptraceARP_TABLE_ENTRY_WILL_EXPIRE( xARPCache[ x ].ulIPAddress );
				prvOutputARPRequest( xARPCache[ x ].ulIPAddress );
			}
			else
			{
				/* The age has just ticked down, with nothing to do. */
			}

			if( xARPCache[ x ].ucAge == 0 )
			{
				/* The entry is no longer valid.  Wipe it out. */
				iptraceARP_TABLE_ENTRY_EXPIRED( xARPCache[ x ].ulIPAddress );
				xARPCache[ x ].ulIPAddress = 0UL;
			}
		}
	}
}
/*-----------------------------------------------------------*/

static eARPLookupResult_t prvGetARPCacheEntry( uint32_t *pulIPAddress, xMACAddress_t * const pxMACAddress )
{
BaseType_t x;
eARPLookupResult_t eReturn;
uint32_t ulAddressToLookup;

	if( *pulIPAddress == ipBROADCAST_IP_ADDRESS )
	{
		/* This is a broadcast so uses the broadcast MAC address. */
		memcpy( ( void * ) pxMACAddress, &xBroadcastMACAddress, sizeof( xMACAddress_t ) );
		eReturn = eARPCacheHit;
	}
	else if( *ipLOCAL_IP_ADDRESS_POINTER == 0UL )
	{
		/* The IP address has not yet been assigned, so there is nothing that
		can be done. */
		eReturn = eCantSendPacket;
	}
	else
	{
		if( ( *pulIPAddress & xNetworkAddressing.ulNetMask ) != ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) )
		{
			/* The IP address is off the local network, so look up the hardware
			address of the router, if any. */
			ulAddressToLookup = xNetworkAddressing.ulGatewayAddress;
		}
		else
		{
			/* The IP address is on the local network, so lookup the requested
			IP address directly. */
			ulAddressToLookup = *pulIPAddress;
		}

		if( ulAddressToLookup == 0UL )
		{
			/* The address is not on the local network, and there is not a
			router. */
			eReturn = eCantSendPacket;
		}
		else
		{
			eReturn = eARPCacheMiss;

			/* Loop through each entry in the ARP cache. */
			for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
			{
				/* Does this row in the ARP cache table hold an entry for the IP
				address being queried? */
				if( xARPCache[ x ].ulIPAddress == ulAddressToLookup )
				{
					/* The IP address matched.  Is there a valid MAC address? */
					if( memcmp( ( void * ) &xNullMACAddress, ( void * ) &( xARPCache[ x ].xMACAddress ), sizeof( xMACAddress_t ) ) == 0 )
					{
						/* This entry is waiting an ARP reply, so is not valid. */
						eReturn = eCantSendPacket;
					}
					else
					{
						/* A valid entry was found. */
						memcpy( pxMACAddress, &( xARPCache[ x ].xMACAddress ), sizeof( xMACAddress_t ) );
						eReturn = eARPCacheHit;
					}
				}

				if( eReturn != eARPCacheMiss )
				{
					break;
				}
			}

			if( eReturn == eARPCacheMiss )
			{
				/* It might be that the ARP has to go to the gateway. */
				*pulIPAddress = ulAddressToLookup;
			}
		}
	}

	return eReturn;
}
/*-----------------------------------------------------------*/

static void prvRefreshARPCacheEntry( const xMACAddress_t * const pxMACAddress, const uint32_t ulIPAddress )
{
BaseType_t x, xEntryFound = pdFALSE, xOldestEntry = 0;
uint8_t ucMinAgeFound = 0U;

	/* Only process the IP address if it is on the local network. */
	if( ( ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) )
	{
		/* Start with the maximum possible number. */
		ucMinAgeFound--;

		/* For each entry in the ARP cache table. */
		for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
		{
			/* Does this line in the cache table hold an entry for the IP
			address	being queried? */
			if( xARPCache[ x ].ulIPAddress == ulIPAddress )
			{
				/* If the MAC address is all zeros then the refresh is due to
				an ARP reply, so in effect this is a new entry in the ARP
				cache. */
				if( memcmp( &( xARPCache[ x ].xMACAddress ), &xNullMACAddress, sizeof( xMACAddress_t ) ) == 0 )
				{
					iptraceARP_TABLE_ENTRY_CREATED( xARPCache[ x ].ulIPAddress, *pxMACAddress );
				}

				/* Refresh the cache entry so the entry's age is back to its
				maximum	value. */
				xARPCache[ x ].ucAge = ipconfigMAX_ARP_AGE;
				memcpy( &( xARPCache[ x ].xMACAddress ), pxMACAddress, sizeof( xMACAddress_t ) );
				xEntryFound = pdTRUE;
				break;
			}
			else
			{
				/* As the table is traversed, remember the table row that
				contains the oldest entry (the lowest age count, as ages are
				decremented to zero) so the row can be re-used if this function
				needs to add an entry that does not already exist. */
				if( xARPCache[ x ].ucAge < ucMinAgeFound )
				{
					ucMinAgeFound = xARPCache[ x ].ucAge;
					xOldestEntry = x;
				}
			}
		}

		if( xEntryFound == pdFALSE )
		{
			/* The wanted entry does not already exist.  Add the entry into the
			cache, replacing the oldest entry (which might be an empty entry). */
			xARPCache[ xOldestEntry ].ulIPAddress = ulIPAddress;
			memcpy( &( xARPCache[ xOldestEntry ].xMACAddress ), pxMACAddress, sizeof( xMACAddress_t ) );

			/* If the MAC address is all zeros, then this entry is not yet
			complete but still waiting the reply from an ARP request.  When this
			is the case	the age is set to a much lower value as an ARP
			retransmission will be generated each time the ARP timer is called
			while the reply is still outstanding. */
			if( pxMACAddress == &xNullMACAddress )
			{
				xARPCache[ xOldestEntry ].ucAge = ipconfigMAX_ARP_RETRANSMISSIONS;
			}
			else
			{
				iptraceARP_TABLE_ENTRY_CREATED( xARPCache[ xOldestEntry ].ulIPAddress, xARPCache[ xOldestEntry ].xMACAddress );
				xARPCache[ xOldestEntry ].ucAge = ipconfigMAX_ARP_AGE;
			}
		}
	}
}
/*-----------------------------------------------------------*/

#if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS == 1

	static void prvCalculateFragmentOffsetAndLength( xIPFragmentParameters_t *pxFragmentParameters, uint16_t *pusFragmentOffset, uint16_t *pusFragmentLength )
	{
		*pusFragmentOffset = pxFragmentParameters->usFragmentedPacketOffset;

		if( *pusFragmentOffset != 0 )
		{
			/* Take into account that the payload has had a UDP header added in the
			first fragment of the set. */
			*pusFragmentOffset += sizeof( xUDPHeader_t );
		}

		/* The offset is defined in multiples of 8 bytes. */
		*pusFragmentOffset >>= ipSHIFT_TO_DIVIDE_BY_8;
		*pusFragmentLength = pxFragmentParameters->usFragmentLength;

		if( ( pxFragmentParameters->ucSocketOptions & FREERTOS_NOT_LAST_IN_FRAGMENTED_PACKET ) != 0 )
		{
			/* Set the more fragments flag. */
			*pusFragmentOffset |= ipMORE_FRAGMENTS_FLAG_BIT;
		}
	}

#endif
/*-----------------------------------------------------------*/

static void prvCompleteUDPHeader( xNetworkBufferDescriptor_t *pxNetworkBuffer, xUDPPacket_t *pxUDPPacket, uint8_t ucSocketOptions )
{
xUDPHeader_t *pxUDPHeader;

	pxUDPHeader = &( pxUDPPacket->xUDPHeader );

	pxUDPHeader->usDestinationPort = pxNetworkBuffer->usPort;
	pxUDPHeader->usSourcePort = pxNetworkBuffer->usBoundPort;
	pxUDPHeader->usLength = ( uint16_t ) ( pxNetworkBuffer->xDataLength + sizeof( xUDPHeader_t ) );
	pxUDPHeader->usLength = FreeRTOS_htons( pxUDPPacket->xUDPHeader.usLength );
	pxUDPHeader->usChecksum = 0;

	if( ( ucSocketOptions & FREERTOS_SO_UDPCKSUM_OUT ) != 0U )
	{
		pxUDPHeader->usChecksum = prvGenerateUDPChecksum( pxUDPPacket, ipconfigETHERNET_DRIVER_ADDS_UDP_CHECKSUM );
		if( pxUDPHeader->usChecksum == 0x00 )
		{
			/* A calculated checksum of 0 must be inverted as 0 means the
			checksum is disabled. */
			pxUDPHeader->usChecksum = 0xffffU;
		}
	}
}
/*-----------------------------------------------------------*/

#if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS == 1

	static void prvProcessGeneratedPacket( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
	{
	xUDPPacket_t *pxUDPPacket;
	xUDPHeader_t *pxUDPHeader;
	xIPHeader_t *pxIPHeader;
	eARPLookupResult_t eReturned;
	eIPFragmentStatus_t eFragmentStatus;
	uint16_t usFragmentOffset = 0, usFragmentLength;
	xIPFragmentParameters_t *pxFragmentParameters;
	static uint16_t usPacketIdentifier = 0U;

		/* Map the UDP packet onto the start of the frame. */
		pxUDPPacket = ( xUDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;

		/* Determine the ARP cache status for the requested IP address. */
		eReturned = prvGetARPCacheEntry( &( pxNetworkBuffer->ulIPAddress ), &( pxUDPPacket->xEthernetHeader.xDestinationAddress ) );

		if( eReturned != eCantSendPacket )
		{
			if( eReturned == eARPCacheHit )
			{
				iptraceSENDING_UDP_PACKET( pxNetworkBuffer->ulIPAddress );

				/* Create short cuts to the data within the packet. */
				pxUDPHeader = &( pxUDPPacket->xUDPHeader );
				pxIPHeader = &( pxUDPPacket->xIPHeader );
				pxFragmentParameters = ( xIPFragmentParameters_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipFRAGMENTATION_PARAMETERS_OFFSET ] );

				/* IP header source and destination addresses must be set
				before the UDP checksum is calculated. */
				pxIPHeader->ulDestinationIPAddress = pxNetworkBuffer->ulIPAddress;
				pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;

				/* If the packet is not fragmented, or if the packet is the
				first in a fragmented packet, then a UDP header is required. */
				if( ( pxFragmentParameters->ucSocketOptions & FREERTOS_FRAGMENTED_PACKET ) == 0 )
				{
					eFragmentStatus = eNotFragment;

					#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
					{
						/* Is it possible that the packet is not actually a UDP
						packet after all, but an ICMP packet. */
						if( pxNetworkBuffer->usPort != ipPACKET_CONTAINS_ICMP_DATA )
						{
							prvCompleteUDPHeader( pxNetworkBuffer, pxUDPPacket, pxFragmentParameters->ucSocketOptions );
						}
					}
					#else /* ipconfigSUPPORT_OUTGOING_PINGS */
					{
						prvCompleteUDPHeader( pxNetworkBuffer, pxUDPPacket, pxFragmentParameters->ucSocketOptions );
					}
					#endif /* ipconfigSUPPORT_OUTGOING_PINGS */


					usFragmentLength = 0U;

					/* The identifier is incremented as this is a new and
					unfragmented IP packet. */
					usPacketIdentifier++;
				}
				else if( pxFragmentParameters->usFragmentedPacketOffset == 0 )
				{
					eFragmentStatus = eFirstFragment;
					prvCalculateFragmentOffsetAndLength( pxFragmentParameters, &usFragmentOffset, &usFragmentLength );
					/* Note FREERTOS_SO_UDPCKSUM_OUT is used because checksums
					cannot currently be used on fragmented packets. */
					pxFragmentParameters->ucSocketOptions &= ~FREERTOS_SO_UDPCKSUM_OUT;
					prvCompleteUDPHeader( pxNetworkBuffer, pxUDPPacket, pxFragmentParameters->ucSocketOptions );

					/* The identifier is incremented because, although this is a
					fragmented packet, it is the first in the fragmentation
					set. */
					usPacketIdentifier++;
				}
				else
				{
					eFragmentStatus = eFollowingFragment;
					prvCalculateFragmentOffsetAndLength( pxFragmentParameters, &usFragmentOffset, &usFragmentLength );
				}

				/* memcpy() the constant parts of the header information into the
				correct location within the packet.  This fills in:
					xEthernetHeader.xSourceAddress
					xEthernetHeader.usFrameType
					xIPHeader.ucVersionHeaderLength
					xIPHeader.ucDifferentiatedServicesCode
					xIPHeader.usLength
					xIPHeader.usIdentification
					xIPHeader.usFragmentOffset
					xIPHeader.ucTimeToLive
					xIPHeader.ucProtocol
				and
					xIPHeader.usHeaderChecksum
				*/
				memcpy( ( void *) &( pxUDPPacket->xEthernetHeader.xSourceAddress ), ( void * ) xDefaultPartUDPPacketHeader, sizeof( xDefaultPartUDPPacketHeader ) );

				/* The fragment status is used to complete the length and
				fragment offset fields. */
				if( eFragmentStatus == eNotFragment )
				{
					pxIPHeader->usLength = ( uint16_t ) ( pxNetworkBuffer->xDataLength + sizeof( xIPHeader_t ) + sizeof( xUDPHeader_t ) );
				}
				else if( eFragmentStatus == eFirstFragment )
				{
					pxIPHeader->usFragmentOffset = FreeRTOS_htons( usFragmentOffset );
					pxIPHeader->usLength = ( uint16_t ) ( usFragmentLength + sizeof( xIPHeader_t ) + sizeof( xUDPHeader_t ) );
				}
				else
				{
					pxIPHeader->usFragmentOffset = FreeRTOS_htons( usFragmentOffset );
					pxIPHeader->usLength = ( uint16_t ) ( usFragmentLength + sizeof( xIPHeader_t ) );
				}

				/* The total transmit size adds on the Ethernet header. */
				pxNetworkBuffer->xDataLength = pxIPHeader->usLength + sizeof( xEthernetHeader_t );
				pxIPHeader->usLength = FreeRTOS_htons( pxIPHeader->usLength );
				pxIPHeader->ulDestinationIPAddress = pxNetworkBuffer->ulIPAddress;
				pxIPHeader->usIdentification = usPacketIdentifier;
				pxIPHeader->usHeaderChecksum = prvGenerateChecksum( ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipIP_HEADER_LENGTH, ipconfigETHERNET_DRIVER_ADDS_IP_CHECKSUM );
			}
			else if ( eReturned == eARPCacheMiss )
			{
				/* Send an ARP for the required IP address. */
				iptracePACKET_DROPPED_TO_GENERATE_ARP( pxNetworkBuffer->ulIPAddress );
				prvGenerateARPRequestPacket( pxNetworkBuffer );

				/* Add an entry to the ARP table with a null hardware address.
				This allows the ARP timer to know that an ARP reply is
				outstanding, and perform retransmissions if necessary. */
				prvRefreshARPCacheEntry( &xNullMACAddress, pxNetworkBuffer->ulIPAddress );
			}
			else
			{
				/* The lookup indicated that an ARP request has already been
				sent out for the queried IP address. */
				eReturned = eCantSendPacket;
			}
		}

		if( eReturned != eCantSendPacket )
		{
			/* The network driver is responsible for freeing the network buffer
			after the packet has been sent. */
			xNetworkInterfaceOutput( pxNetworkBuffer );
		}
		else
		{
			/* The packet can't be sent (DHCP not completed?).  Just drop the
			packet. */
			vNetworkBufferRelease( pxNetworkBuffer );
		}
	}

#else /* ipconfigCAN_FRAGMENT_OUTGOING_PACKETS == 1 */

	static void prvProcessGeneratedPacket( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
	{
	xUDPPacket_t *pxUDPPacket;
	xIPHeader_t *pxIPHeader;
	eARPLookupResult_t eReturned;

		/* Map the UDP packet onto the start of the frame. */
		pxUDPPacket = ( xUDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;

		/* Determine the ARP cache status for the requested IP address. */
		eReturned = prvGetARPCacheEntry( &( pxNetworkBuffer->ulIPAddress ), &( pxUDPPacket->xEthernetHeader.xDestinationAddress ) );
		if( eReturned != eCantSendPacket )
		{
			if( eReturned == eARPCacheHit )
			{
				iptraceSENDING_UDP_PACKET( pxNetworkBuffer->ulIPAddress );

				/* Create short cuts to the data within the packet. */
				pxIPHeader = &( pxUDPPacket->xIPHeader );

				/* IP header source and destination addresses must be set before
				the	UDP checksum is calculated.  The socket options, which
				specify whether a checksum should be calculated or not, are
				passed in the as yet unused part of the packet data. */
				pxIPHeader->ulDestinationIPAddress = pxNetworkBuffer->ulIPAddress;
				pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;

				#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
				{
					/* Is it possible that the packet is not actually a UDP packet
					after all, but an ICMP packet. */
					if( pxNetworkBuffer->usPort != ipPACKET_CONTAINS_ICMP_DATA )
					{
						prvCompleteUDPHeader( pxNetworkBuffer, pxUDPPacket, pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ] );
					}
				}
				#else /* ipconfigSUPPORT_OUTGOING_PINGS */
				{
					prvCompleteUDPHeader( pxNetworkBuffer, pxUDPPacket, pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ] );
				}
				#endif /* ipconfigSUPPORT_OUTGOING_PINGS */

				/* memcpy() the constant parts of the header information into
				the	correct location within the packet.  This fills in:
					xEthernetHeader.xSourceAddress
					xEthernetHeader.usFrameType
					xIPHeader.ucVersionHeaderLength
					xIPHeader.ucDifferentiatedServicesCode
					xIPHeader.usLength
					xIPHeader.usIdentification
					xIPHeader.usFragmentOffset
					xIPHeader.ucTimeToLive
					xIPHeader.ucProtocol
				and
					xIPHeader.usHeaderChecksum
				*/
				memcpy( ( void *) &( pxUDPPacket->xEthernetHeader.xSourceAddress ), ( void * ) xDefaultPartUDPPacketHeader, sizeof( xDefaultPartUDPPacketHeader ) );

				#if ipconfigSUPPORT_OUTGOING_PINGS == 1
				{
					if( pxNetworkBuffer->usPort == ipPACKET_CONTAINS_ICMP_DATA )
					{
						pxIPHeader->ucProtocol = ipPROTOCOL_ICMP;
						pxIPHeader->usLength = ( uint16_t ) ( pxNetworkBuffer->xDataLength + sizeof( xIPHeader_t ) );
					}
					else
					{
						pxIPHeader->usLength = ( uint16_t ) ( pxNetworkBuffer->xDataLength + sizeof( xIPHeader_t ) + sizeof( xUDPHeader_t ) );
					}
				}
				#else /* ipconfigSUPPORT_OUTGOING_PINGS */
				{
					pxIPHeader->usLength = ( uint16_t ) ( pxNetworkBuffer->xDataLength + sizeof( xIPHeader_t ) + sizeof( xUDPHeader_t ) );
				}
				#endif /* ipconfigSUPPORT_OUTGOING_PINGS */

				/* The total transmit size adds on the Ethernet header. */
				pxNetworkBuffer->xDataLength = pxIPHeader->usLength + sizeof( xEthernetHeader_t );
				pxIPHeader->usLength = FreeRTOS_htons( pxIPHeader->usLength );
				pxIPHeader->ulDestinationIPAddress = pxNetworkBuffer->ulIPAddress;
				pxIPHeader->usHeaderChecksum = prvGenerateChecksum( ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipIP_HEADER_LENGTH, ipconfigETHERNET_DRIVER_ADDS_IP_CHECKSUM );
			}
			else if ( eReturned == eARPCacheMiss )
			{
				/* Generate an ARP for the required IP address. */
				iptracePACKET_DROPPED_TO_GENERATE_ARP( pxNetworkBuffer->ulIPAddress );
				prvGenerateARPRequestPacket( pxNetworkBuffer );

				/* Add an entry to the ARP table with a null hardware address.
				This allows the ARP timer to know that an ARP reply is
				outstanding, and perform retransmissions if necessary. */
				prvRefreshARPCacheEntry( &xNullMACAddress, pxNetworkBuffer->ulIPAddress );
			}
			else
			{
				/* The lookup indicated that an ARP request has already been
				sent out for the queried IP address. */
				eReturned = eCantSendPacket;
			}
		}

		if( eReturned != eCantSendPacket )
		{
			/* The network driver is responsible for freeing the network buffer
			after the packet has been sent. */
			xNetworkInterfaceOutput( pxNetworkBuffer );
		}
		else
		{
			/* The packet can't be sent (DHCP not completed?).  Just drop the
			packet. */
			vNetworkBufferRelease( pxNetworkBuffer );
		}
	}


#endif /* ipconfigCAN_FRAGMENT_OUTGOING_PACKETS == 1 */
/*-----------------------------------------------------------*/

static void prvGenerateARPRequestPacket( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
{
xARPPacket_t *pxARPPacket;

	pxARPPacket = ( xARPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;

	/* memcpy the const part of the header information into the correct
	location in the packet.  This copies:
		xEthernetHeader.ulDestinationAddress
		xEthernetHeader.usFrameType;
		xARPHeader.usHardwareType;
		xARPHeader.usProtocolType;
		xARPHeader.ucHardwareAddressLength;
		xARPHeader.ucProtocolAddressLength;
		xARPHeader.usOperation;
		xARPHeader.xTargetHardwareAddress;
	*/
	memcpy( ( void * ) &( pxARPPacket->xEthernetHeader ), ( void * ) xDefaultPartARPPacketHeader, sizeof( xDefaultPartARPPacketHeader ) );
	memcpy( ( void * ) &( pxARPPacket->xEthernetHeader.xSourceAddress ) , ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
	memcpy( ( void * ) &( pxARPPacket->xARPHeader.xSenderHardwareAddress ), ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
	pxARPPacket->xARPHeader.ulSenderProtocolAddress = *ipLOCAL_IP_ADDRESS_POINTER;
	pxARPPacket->xARPHeader.ulTargetProtocolAddress = pxNetworkBuffer->ulIPAddress;

	pxNetworkBuffer->xDataLength = sizeof( xARPPacket_t );

	iptraceCREATING_ARP_REQUEST( pxNetworkBuffer->ulIPAddress );
}
/*-----------------------------------------------------------*/

eFrameProcessingResult_t eConsiderFrameForProcessing( const uint8_t * const pucEthernetBuffer )
{
eFrameProcessingResult_t eReturn;
const xEthernetHeader_t *pxEthernetHeader;

	pxEthernetHeader = ( const xEthernetHeader_t * ) pucEthernetBuffer;

	if( memcmp( ( void * ) &xBroadcastMACAddress, ( void * ) &( pxEthernetHeader->xDestinationAddress ), sizeof( xMACAddress_t ) ) == 0 )
	{
		/* The packet was a broadcast - process it. */
		eReturn = eProcessBuffer;
	}
	else if( memcmp( ( void * ) ipLOCAL_MAC_ADDRESS, ( void * ) &( pxEthernetHeader->xDestinationAddress ), sizeof( xMACAddress_t ) ) == 0 )
	{
		/* The packet was to this node directly - process it. */
		eReturn = eProcessBuffer;
	}
	else
	{
		/* The packet was not a broadcast, or for this node, just release
		the buffer without taking any other action. */
		eReturn = eReleaseBuffer;
	}

	#if ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES == 1
	{
		uint16_t usFrameType;

			if( eReturn == eProcessBuffer )
			{
				usFrameType = pxEthernetHeader->usFrameType;
				usFrameType = FreeRTOS_ntohs( usFrameType );

				if( usFrameType <= 0x600U )
				{
					/* Not an Ethernet II frame. */
					eReturn = eReleaseBuffer;
				}
			}
	}
	#endif /* ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES == 1  */

	return eReturn;
}
/*-----------------------------------------------------------*/

static void prvProcessNetworkDownEvent( void )
{
	/* Stop the ARP timer while there is no network. */
	xTimerStop( xARPTimer, portMAX_DELAY );

	#if ipconfigUSE_NETWORK_EVENT_HOOK == 1
	{
		static BaseType_t xCallEventHook = pdFALSE;

		/* The first network down event is generated by the IP stack
		itself to initialise the network hardware, so do not call the
		network down event the first time through. */
		if( xCallEventHook == pdTRUE )
		{
			vApplicationIPNetworkEventHook( eNetworkDown );
		}
		xCallEventHook = pdTRUE;
	}
	#endif

	/* The network has been disconnected (or is being
	initialised for the first time).  Perform whatever hardware
	processing is necessary to bring it up again, or wait for it
	to be available again.  This is hardware dependent. */
	if( xNetworkInterfaceInitialise() != pdPASS )
	{
		/* Ideally the network interface initialisation function
		will only return when the network is available.  In case
		this is not the case, wait a while before retrying the
		initialisation. */
		vTaskDelay( ipINITIALISATION_RETRY_DELAY );
		FreeRTOS_NetworkDown();
	}
	else
	{
		/* Start the ARP timer. */
		xTimerStart( xARPTimer, portMAX_DELAY );

		#if ipconfigUSE_DHCP == 1
		{
			/* The network is not up until DHCP has completed. */
			vDHCPProcess( pdTRUE, ( xMACAddress_t * ) ipLOCAL_MAC_ADDRESS, ipLOCAL_IP_ADDRESS_POINTER, &xNetworkAddressing );
			prvSendEventToIPTask( eDHCPEvent );
		}
		#else
		{
			#if ipconfigUSE_NETWORK_EVENT_HOOK == 1
			{
				vApplicationIPNetworkEventHook( eNetworkUp );
			}
			#endif /* ipconfigUSE_NETWORK_EVENT_HOOK */

			/* Static configuration is being used, so the network is now up. */
			#if ipconfigFREERTOS_PLUS_NABTO == 1
			{
				/* Return value is used in configASSERT() inside the
				function. */
				( void ) xStartNabtoTask();
			}
			#endif /* ipconfigFREERTOS_PLUS_NABTO */
		}
		#endif
	}
}
/*-----------------------------------------------------------*/

static void prvProcessEthernetPacket( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
{
xEthernetHeader_t *pxEthernetHeader;
volatile eFrameProcessingResult_t eReturned; /* Volatile to prevent complier warnings when ipCONSIDER_FRAME_FOR_PROCESSING just sets it to eProcessBuffer. */

	configASSERT( pxNetworkBuffer );

	/* Interpret the Ethernet frame. */
	eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer );
	pxEthernetHeader = ( xEthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;

	if( eReturned == eProcessBuffer )
	{
		/* Interpret the received Ethernet packet. */
		switch ( pxEthernetHeader->usFrameType )
		{
			case ipARP_TYPE	:
				/* The Ethernet frame contains an ARP packet. */
				eReturned = prvProcessARPPacket( ( xARPPacket_t * ) pxEthernetHeader );
				break;

			case ipIP_TYPE	:
				/* The Ethernet frame contains an IP packet. */
				eReturned = prvProcessIPPacket( ( xIPPacket_t * ) pxEthernetHeader, pxNetworkBuffer );
				break;

			default :
				/* No other packet types are handled.  Nothing to do. */
				eReturned = eReleaseBuffer;
				break;
		}
	}

	/* Perform any actions that resulted from processing the Ethernet
	frame. */
	switch( eReturned )
	{
		case eReturnEthernetFrame :
			/* The Ethernet frame will have been updated (maybe it was
			an ARP request or a PING request?) and should be sent back to
			its source. */
			prvReturnEthernetFrame( pxNetworkBuffer );
			/* The buffer must be released once
			the frame has been transmitted. */
			break;

		case eFrameConsumed :
			/* The frame is in use somewhere, don't release the buffer
			yet. */
			break;

		default :
			/* The frame is not being used anywhere, and the
			xNetworkBufferDescriptor_t structure containing the frame should just be
			released back to the list of free buffers. */
			vNetworkBufferRelease( pxNetworkBuffer );
			break;
	}
}
/*-----------------------------------------------------------*/

static eFrameProcessingResult_t prvProcessIPPacket( const xIPPacket_t * const pxIPPacket, xNetworkBufferDescriptor_t * const pxNetworkBuffer )
{
eFrameProcessingResult_t eReturn = eReleaseBuffer;
const xIPHeader_t * pxIPHeader;
xUDPPacket_t *pxUDPPacket;
BaseType_t xChecksumIsCorrect;

	pxIPHeader = &( pxIPPacket->xIPHeader );

	/* Is the packet for this node? */
	if( ( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER ) || ( pxIPHeader->ulDestinationIPAddress == ipBROADCAST_IP_ADDRESS ) || ( *ipLOCAL_IP_ADDRESS_POINTER == 0 ) )
	{
		/* Ensure the frame is IPv4 with no options bytes, and that the incoming
		packet is not fragmented (only outgoing packets can be fragmented) as
		these are the only handled IP frames currently. */
		if( ( pxIPHeader->ucVersionHeaderLength == ipIP_VERSION_AND_HEADER_LENGTH_BYTE ) && ( ( pxIPHeader->usFragmentOffset & ipFRAGMENT_OFFSET_BIT_MASK ) == 0U ) )
		{
			/* Is the IP header checksum correct? */
			if( prvGenerateChecksum( ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipIP_HEADER_LENGTH, ipconfigETHERNET_DRIVER_CHECKS_IP_CHECKSUM ) == 0 )
			{
				/* Add the IP and MAC addresses to the ARP table if they are not
				already there - otherwise refresh the age of the existing
				entry. */
				prvRefreshARPCacheEntry( &( pxIPPacket->xEthernetHeader.xSourceAddress ), pxIPHeader->ulSourceIPAddress );
				switch( pxIPHeader->ucProtocol )
				{
					case ipPROTOCOL_ICMP :

						/* The IP packet contained an ICMP frame.  Don't bother
						checking the ICMP checksum, as if it is wrong then the
						wrong data will also be returned, and the source of the
						ping will know something went wrong because it will not
						be able to validate what it receives. */
						#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
						{
							if( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER )
							{
								eReturn = prvProcessICMPPacket( ( xICMPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer ) );
							}
						}
						#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */
						break;

					case ipPROTOCOL_UDP :

						/* The IP packet contained a UDP frame. */
						pxUDPPacket = ( xUDPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );

						/* Note the header values required prior to the
						checksum generation as the checksum pseudo header
						may clobber some of these values. */
						pxNetworkBuffer->xDataLength = FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( xUDPHeader_t );
						pxNetworkBuffer->usPort = pxUDPPacket->xUDPHeader.usSourcePort;
						pxNetworkBuffer->ulIPAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress;

						/* Is the checksum required? */
						if( pxUDPPacket->xUDPHeader.usChecksum == 0 )
						{
							xChecksumIsCorrect = pdTRUE;
						}
						else if( prvGenerateUDPChecksum( pxUDPPacket, ipconfigETHERNET_DRIVER_CHECKS_UDP_CHECKSUM ) == 0 )
						{
							xChecksumIsCorrect = pdTRUE;
						}
						else
						{
							xChecksumIsCorrect = pdFALSE;
						}

						/* Is the checksum correct? */
						if( xChecksumIsCorrect == pdTRUE )
						{
							/* Pass the packet payload to the UDP sockets
							implementation. */
							if( xProcessReceivedUDPPacket( pxNetworkBuffer, pxUDPPacket->xUDPHeader.usDestinationPort ) == pdPASS )
							{
								eReturn = eFrameConsumed;
							}
						}
						break;

					default	:

						/* Not a supported frame type. */
						break;
				}
			}
		}
	}

	return eReturn;
}
/*-----------------------------------------------------------*/

static uint16_t prvGenerateUDPChecksum( const xUDPPacket_t * const pxUDPPacket, BaseType_t xChecksumIsOffloaded )
{
xPseudoHeader_t *pxPseudoHeader;
uint16_t usLength, usReturn;

	if( xChecksumIsOffloaded == pdFALSE )
	{
		/* Map the pseudo header into the correct place within the real IP
		header. */
		pxPseudoHeader = ( xPseudoHeader_t * ) &( pxUDPPacket->xIPHeader.ucTimeToLive );

		/* Ordering here is important so as not to overwrite data that is required
		but has not yet been used as the pseudo header overlaps the information
		that is being copied into it. */
		pxPseudoHeader->ulSourceAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress;
		pxPseudoHeader->ulDestinationAddress = pxUDPPacket->xIPHeader.ulDestinationIPAddress;
		pxPseudoHeader->ucZeros = 0x00;
		pxPseudoHeader->ucProtocol = ipPROTOCOL_UDP;
		pxPseudoHeader->usUDPLength = pxUDPPacket->xUDPHeader.usLength;

		usLength = FreeRTOS_ntohs( pxPseudoHeader->usUDPLength );
		usReturn = prvGenerateChecksum( ( uint8_t * ) pxPseudoHeader, usLength + sizeof( xPseudoHeader_t ), pdFALSE );
	}
	else
	{
		/* The hardware will check the checksum.  Returning 0 allows this
		function to be used to both check an incoming checksum and set an
		outgoing checksum in this case. */
		usReturn = 0;
	}

	return usReturn;
}
/*-----------------------------------------------------------*/

#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )

	static void prvProcessICMPEchoReply( xICMPPacket_t * const pxICMPPacket )
	{
	ePingReplyStatus_t eStatus = eSuccess;
	uint16_t usDataLength, usCount;
	uint8_t *pucByte;

		/* Find the total length of the IP packet. */
		usDataLength = pxICMPPacket->xIPHeader.usLength;
		usDataLength = FreeRTOS_ntohs( usDataLength );

		/* Remove the length of the IP headers to obtain the length of the ICMP
		message itself. */
		usDataLength -= sizeof( xIPHeader_t );

		if( prvGenerateChecksum( ( uint8_t * ) &( pxICMPPacket->xICMPHeader ), usDataLength, pdFALSE ) != 0 )
		{
			eStatus = eInvalidChecksum;
		}
		else
		{
			/* Remove the length of the ICMP header, to obtain the length of
			data contained in the ping. */
			usDataLength -= sizeof( xICMPHeader_t );

			/* Find the first byte of the data within the ICMP packet. */
			pucByte = ( uint8_t * ) pxICMPPacket;
			pucByte += sizeof( xICMPPacket_t );

			/* Check each byte. */
			for( usCount = 0; usCount < usDataLength; usCount++ )
			{
				if( *pucByte != ipECHO_DATA_FILL_BYTE )
				{
					eStatus = eInvalidData;
					break;
				}

				pucByte++;
			}
		}

		vApplicationPingReplyHook( eStatus, pxICMPPacket->xICMPHeader.usIdentifier );
	}

#endif
/*-----------------------------------------------------------*/

#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 )

	static eFrameProcessingResult_t prvProcessICMPEchoRequest( xICMPPacket_t * const pxICMPPacket )
	{
	xICMPHeader_t *pxICMPHeader;
	xIPHeader_t *pxIPHeader;

		pxICMPHeader = &( pxICMPPacket->xICMPHeader );
		pxIPHeader = &( pxICMPPacket->xIPHeader );

		iptraceSENDING_PING_REPLY( pxIPHeader->ulSourceIPAddress );

		/* The checksum can be checked here - but a ping reply should be
		returned even if the checksum is incorrect so the other end can
		tell that the ping was received - even if the ping reply contains
		invalid data. */
		pxICMPHeader->ucTypeOfMessage = ipICMP_ECHO_REPLY;
		pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;
		pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;

		/* Update the checksum because the ucTypeOfMessage member in the
		header has been changed to ipICMP_ECHO_REPLY. */
		if( pxICMPHeader->usChecksum >= FreeRTOS_htons( ( ( uint16_t ) 0xffffU ) - ( ipICMP_ECHO_REQUEST << ( ( uint16_t ) 8U ) ) ) )
		{
			pxICMPHeader->usChecksum += FreeRTOS_htons( ipICMP_ECHO_REQUEST << ( ( uint16_t ) 8U ) ) + ( uint16_t ) 1U;
		}
		else
		{
			pxICMPHeader->usChecksum += FreeRTOS_htons( ipICMP_ECHO_REQUEST << ( ( uint16_t ) 8U ) );
		}

		return eReturnEthernetFrame;
	}

#endif /* ipconfigREPLY_TO_INCOMING_PINGS == 1 */

/*-----------------------------------------------------------*/

#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )

	static eFrameProcessingResult_t prvProcessICMPPacket( xICMPPacket_t * const pxICMPPacket )
	{
	eFrameProcessingResult_t eReturn = eReleaseBuffer;

		iptraceICMP_PACKET_RECEIVED();

		switch( pxICMPPacket->xICMPHeader.ucTypeOfMessage )
		{
			case ipICMP_ECHO_REQUEST	:
				#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 )
				{
					eReturn = prvProcessICMPEchoRequest( pxICMPPacket );
				}
				#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) */
				break;

			case ipICMP_ECHO_REPLY		:
				#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
				{
					prvProcessICMPEchoReply( pxICMPPacket );
				}
				#endif /* ipconfigSUPPORT_OUTGOING_PINGS */
				break;

			default	:
				break;
		}

		return eReturn;
	}

#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */
/*-----------------------------------------------------------*/

static uint16_t prvGenerateChecksum( const uint8_t * const pucNextData, const uint16_t usDataLengthBytes, BaseType_t xChecksumIsOffloaded )
{
uint32_t ulChecksum = 0;
uint16_t us, usDataLength16BitWords, *pusNextData, usReturn;

	if( xChecksumIsOffloaded == pdFALSE )
	{
		/* There are half as many 16 bit words than bytes. */
		usDataLength16BitWords = ( usDataLengthBytes >> 1U );

		pusNextData = ( uint16_t * ) pucNextData;

		for( us = 0U; us < usDataLength16BitWords; us++ )
		{
			ulChecksum += ( uint32_t ) pusNextData[ us ];
		}

		if( ( usDataLengthBytes & 0x01U ) != 0x00 )
		{
			/* There is one byte left over. */
			#if ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN
			{
				ulChecksum += ( uint32_t ) pucNextData[ usDataLengthBytes - 1 ];
			}
			#else
			{
				us = ( uint16_t ) pucNextData[ usDataLengthBytes - 1 ];
				ulChecksum += ( uint32_t ) ( us << 8 );
			}
			#endif
		}

		while( ( ulChecksum >> 16UL ) != 0x00UL )
		{
			ulChecksum = ( ulChecksum & 0xffffUL ) + ( ulChecksum >> 16UL );
		}

		usReturn = ~( ( uint16_t ) ulChecksum );
	}
	else
	{
		/* The checksum is calculated by the hardware.  Return 0 here to ensure
		this works for both incoming and outgoing checksums. */
		usReturn = 0;
	}

	return usReturn;
}
/*-----------------------------------------------------------*/

static void prvReturnEthernetFrame( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
{
xEthernetHeader_t *pxEthernetHeader;

	pxEthernetHeader = ( xEthernetHeader_t * ) ( pxNetworkBuffer->pucEthernetBuffer );

	/* Swap source and destination MAC addresses. */
	memcpy( ( void * ) &( pxEthernetHeader->xDestinationAddress ), ( void * ) &( pxEthernetHeader->xSourceAddress ), sizeof( pxEthernetHeader->xDestinationAddress ) );
	memcpy( ( void * ) &( pxEthernetHeader->xSourceAddress) , ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );

	/* Send! */
	xNetworkInterfaceOutput( pxNetworkBuffer );
}
/*-----------------------------------------------------------*/

static eFrameProcessingResult_t prvProcessARPPacket( xARPPacket_t * const pxARPFrame )
{
eFrameProcessingResult_t eReturn = eReleaseBuffer;
xARPHeader_t *pxARPHeader;

	pxARPHeader = &( pxARPFrame->xARPHeader );

	traceARP_PACKET_RECEIVED();

	/* Sanity check the protocol type.  Don't do anything if the local IP
	address is zero because that means a DHCP request has not completed. */
	if( ( pxARPHeader->usProtocolType == ipARP_PROTOCOL_TYPE ) && ( *ipLOCAL_IP_ADDRESS_POINTER != 0UL ) )
	{
		switch( pxARPHeader->usOperation )
		{
			case ipARP_REQUEST	:
				/* The packet contained an ARP request.  Was it for the IP
				address of the node running this code? */
				if( pxARPHeader->ulTargetProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER )
				{
					iptraceSENDING_ARP_REPLY( pxARPHeader->ulSenderProtocolAddress );

					/* The request is for the address of this node.  Add the
					entry into the ARP cache, or refresh the entry if it
					already exists. */
					prvRefreshARPCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), pxARPHeader->ulSenderProtocolAddress );

					/* Generate a reply payload in the same buffer. */
					pxARPHeader->usOperation = ipARP_REPLY;
					memcpy( ( void * )  &( pxARPHeader->xTargetHardwareAddress ), ( void * ) &( pxARPHeader->xSenderHardwareAddress ), sizeof( xMACAddress_t ) );
					pxARPHeader->ulTargetProtocolAddress = pxARPHeader->ulSenderProtocolAddress;
					memcpy( ( void * ) &( pxARPHeader->xSenderHardwareAddress ), ( void * ) ipLOCAL_MAC_ADDRESS, sizeof( xMACAddress_t ) );
					pxARPHeader->ulSenderProtocolAddress = *ipLOCAL_IP_ADDRESS_POINTER;

					eReturn = eReturnEthernetFrame;
				}
				break;

			case ipARP_REPLY :
				iptracePROCESSING_RECEIVED_ARP_REPLY( pxARPHeader->ulTargetProtocolAddress );
				prvRefreshARPCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), pxARPHeader->ulSenderProtocolAddress );
				break;

			default :
				/* Invalid. */
				break;
		}
	}

	return eReturn;
}
/*-----------------------------------------------------------*/

#if( ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN )
	uint16_t FreeRTOS_htons( uint16_t usIn )
	{
		return	( ( usIn & ( uint16_t ) 0x00ff ) << ( uint16_t ) 8U ) |
				( ( usIn & ( uint16_t ) 0xff00 ) >> ( uint16_t ) 8U );
	}
#endif /* ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN */
/*-----------------------------------------------------------*/

#if( ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN )
	uint32_t FreeRTOS_htonl( uint32_t ulIn )
	{
		return	( ( ulIn & 0x000000ffUL ) << 24UL ) |
				( ( ulIn & 0x0000ff00UL ) << 8UL  ) |
				( ( ulIn & 0x00ff0000UL ) >> 8UL  ) |
				( ( ulIn & 0xff000000UL ) >> 24UL );
	}
#endif /* ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN */

/*-----------------------------------------------------------*/