-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathFreeRTOS_Routing.c
More file actions
1550 lines (1349 loc) · 60.8 KB
/
Copy pathFreeRTOS_Routing.c
File metadata and controls
1550 lines (1349 loc) · 60.8 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
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
/*
* FreeRTOS+TCP <DEVELOPMENT BRANCH>
* Copyright (C) 2020 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.
*
* 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://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/**
* @file FreeRTOS_Routing.c
* @brief Implements endpoint interfaces functions and utilities.
*/
/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_ARP.h"
#include "FreeRTOS_ND.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_DHCP.h"
#include "NetworkBufferManagement.h"
#if ( ipconfigUSE_LLMNR == 1 )
#include "FreeRTOS_DNS.h"
#endif /* ipconfigUSE_LLMNR */
#include "FreeRTOS_Routing.h"
/** @brief A list of all network end-points. Each element has a next pointer. */
struct xNetworkEndPoint * pxNetworkEndPoints = NULL;
/** @brief A list of all network interfaces: */
struct xNetworkInterface * pxNetworkInterfaces = NULL;
/*
* Add a new IP-address to a Network Interface. The object pointed to by
* 'pxEndPoint' and the interface must continue to exist.
*/
static NetworkEndPoint_t * FreeRTOS_AddEndPoint( NetworkInterface_t * pxInterface,
NetworkEndPoint_t * pxEndPoint );
/** @brief A util struct to list the IPv6 IP types, prefix and type bit mask */
struct xIPv6_Couple
{
IPv6_Type_t eType; /**< IPv6 IP type enum */
uint16_t usMask; /**< IPv6 IP type bit mask */
uint16_t usExpected; /**< IPv6 IP type prefix */
};
/*-----------------------------------------------------------*/
#if ( ipconfigUSE_IPv4 != 0 )
/**
* @brief Configure and install a new IPv4 end-point.
*
* @param[in] pxNetworkInterface The interface to which it belongs.
* @param[in] pxEndPoint Space for the new end-point. This memory is dedicated for the
* end-point and should not be freed or get any other purpose.
* @param[in] ucIPAddress The IP-address.
* @param[in] ucNetMask The prefix which shall be used for this end-point.
* @param[in] ucGatewayAddress The IP-address of a device on the LAN which can serve as
* as a gateway to the Internet.
* @param[in] ucDNSServerAddress The IP-address of a DNS server.
* @param[in] ucMACAddress The MAC address of the end-point.
*/
void FreeRTOS_FillEndPoint( NetworkInterface_t * pxNetworkInterface,
NetworkEndPoint_t * pxEndPoint,
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 ] )
{
uint32_t ulIPAddress;
if( ( pxNetworkInterface == NULL ) || ( pxEndPoint == NULL ) )
{
/* Invalid input. */
FreeRTOS_printf( ( "FreeRTOS_FillEndPoint: Invalid input, netif=%p, endpoint=%p\n",
( void * ) pxNetworkInterface,
( void * ) pxEndPoint ) );
}
else
{
/* Fill in and add an end-point to a network interface.
* The user must make sure that the object pointed to by 'pxEndPoint'
* will remain to exist. */
/* As the endpoint might be part of a linked list,
* protect the field pxNext from being overwritten. */
NetworkEndPoint_t * pxNext = pxEndPoint->pxNext;
( void ) memset( pxEndPoint, 0, sizeof( *pxEndPoint ) );
pxEndPoint->pxNext = pxNext;
ulIPAddress = FreeRTOS_inet_addr_quick( ucIPAddress[ 0 ], ucIPAddress[ 1 ], ucIPAddress[ 2 ], ucIPAddress[ 3 ] );
pxEndPoint->ipv4_settings.ulNetMask = FreeRTOS_inet_addr_quick( ucNetMask[ 0 ], ucNetMask[ 1 ], ucNetMask[ 2 ], ucNetMask[ 3 ] );
pxEndPoint->ipv4_settings.ulGatewayAddress = FreeRTOS_inet_addr_quick( ucGatewayAddress[ 0 ], ucGatewayAddress[ 1 ], ucGatewayAddress[ 2 ], ucGatewayAddress[ 3 ] );
pxEndPoint->ipv4_settings.ulDNSServerAddresses[ 0 ] = FreeRTOS_inet_addr_quick( ucDNSServerAddress[ 0 ], ucDNSServerAddress[ 1 ], ucDNSServerAddress[ 2 ], ucDNSServerAddress[ 3 ] );
pxEndPoint->ipv4_settings.ulBroadcastAddress = ulIPAddress | ~( pxEndPoint->ipv4_settings.ulNetMask );
/* Copy the current values to the default values. */
( void ) memcpy( &( pxEndPoint->ipv4_defaults ), &( pxEndPoint->ipv4_settings ), sizeof( pxEndPoint->ipv4_defaults ) );
/* The default IP-address will be used in case DHCP is not used, or also if DHCP has failed, or
* when the user chooses to use the default IP-address. */
pxEndPoint->ipv4_defaults.ulIPAddress = ulIPAddress;
/* The field 'ipv4_settings.ulIPAddress' will be set later on. */
( void ) memcpy( pxEndPoint->xMACAddress.ucBytes, ucMACAddress, sizeof( pxEndPoint->xMACAddress ) );
( void ) FreeRTOS_AddEndPoint( pxNetworkInterface, pxEndPoint );
}
}
/*-----------------------------------------------------------*/
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
#if ( ipconfigCOMPATIBLE_WITH_SINGLE == 0 )
/**
* @brief Add a network interface to the list of interfaces. Check if the interface was
* already added in an earlier call.
*
* @param[in] pxInterface The address of the new interface.
*
* @return The value of the parameter 'pxInterface'.
*/
NetworkInterface_t * FreeRTOS_AddNetworkInterface( NetworkInterface_t * pxInterface )
{
NetworkInterface_t * pxIterator = NULL;
if( pxInterface != NULL )
{
if( pxNetworkInterfaces == NULL )
{
/* No other interfaces are set yet, so this is the first in the list. */
pxNetworkInterfaces = pxInterface;
pxInterface->pxNext = NULL;
}
else
{
/* Other interfaces are already defined, so iterate to the end of the
* list. */
pxIterator = pxNetworkInterfaces;
for( ; ; )
{
if( pxIterator == pxInterface )
{
/* This interface was already added. */
break;
}
if( pxIterator->pxNext == NULL )
{
pxIterator->pxNext = pxInterface;
pxInterface->pxNext = NULL;
break;
}
pxIterator = pxIterator->pxNext;
}
}
}
return pxInterface;
}
/*-----------------------------------------------------------*/
/**
* @brief Get the first Network Interface, or NULL if none has been added.
*
* @return The first interface, or NULL if none has been added
*/
NetworkInterface_t * FreeRTOS_FirstNetworkInterface( void )
{
return pxNetworkInterfaces;
}
/*-----------------------------------------------------------*/
/**
* @brief Get the next interface.
*
* @return The interface that comes after 'pxInterface'. NULL when either 'pxInterface'
* is NULL, or when 'pxInterface' is the last interface.
*/
NetworkInterface_t * FreeRTOS_NextNetworkInterface( const NetworkInterface_t * pxInterface )
{
NetworkInterface_t * pxReturn;
if( pxInterface != NULL )
{
pxReturn = pxInterface->pxNext;
}
else
{
pxReturn = NULL;
}
return pxReturn;
}
/*-----------------------------------------------------------*/
/**
* @brief Add an end-point to a given interface.
*
* @param[in] pxInterface The interface that gets a new end-point.
* @param[in] pxEndPoint The end-point to be added.
*
* @return The value of the parameter 'pxEndPoint'.
*/
static NetworkEndPoint_t * FreeRTOS_AddEndPoint( NetworkInterface_t * pxInterface,
NetworkEndPoint_t * pxEndPoint )
{
NetworkEndPoint_t * pxIterator = NULL;
/* Double link between the NetworkInterface_t that is using the addressing
* defined by this NetworkEndPoint_t structure. */
pxEndPoint->pxNetworkInterface = pxInterface;
if( pxInterface->pxEndPoint == NULL )
{
/*_RB_ When would pxInterface->pxEndPoint ever not be NULL unless this is called twice? */
/*_HT_ It may be called twice. */
pxInterface->pxEndPoint = pxEndPoint;
}
if( pxNetworkEndPoints == NULL )
{
/* No other end points are defined yet - so this is the first in the
* list. */
pxEndPoint->pxNext = NULL;
pxNetworkEndPoints = pxEndPoint;
}
else
{
/* Other end points are already defined so iterate to the end of the
* list. */
pxIterator = pxNetworkEndPoints;
for( ; ; )
{
if( pxIterator == pxEndPoint )
{
/* This end-point has already been added to the list. */
break;
}
if( pxIterator->pxNext == NULL )
{
pxEndPoint->pxNext = NULL;
pxIterator->pxNext = pxEndPoint;
break;
}
pxIterator = pxIterator->pxNext;
}
}
#if ( ipconfigUSE_IPv6 != 0 )
if( pxEndPoint->bits.bIPv6 == pdTRUE_UNSIGNED )
{
FreeRTOS_printf( ( "FreeRTOS_AddEndPoint: MAC: %02x-%02x IPv6: %pip\n",
pxEndPoint->xMACAddress.ucBytes[ 4 ],
pxEndPoint->xMACAddress.ucBytes[ 5 ],
( void * ) pxEndPoint->ipv6_defaults.xIPAddress.ucBytes ) );
}
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
#if ( ipconfigUSE_IPv4 != 0 )
if( pxEndPoint->bits.bIPv6 == pdFALSE_UNSIGNED )
{
FreeRTOS_printf( ( "FreeRTOS_AddEndPoint: MAC: %02x-%02x IPv4: %xip\n",
pxEndPoint->xMACAddress.ucBytes[ 4 ],
pxEndPoint->xMACAddress.ucBytes[ 5 ],
( unsigned ) FreeRTOS_ntohl( pxEndPoint->ipv4_defaults.ulIPAddress ) ) );
}
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
return pxEndPoint;
}
/*-----------------------------------------------------------*/
/**
* @brief Find the first end-point bound to a given interface.
*
* @param[in] pxInterface The interface whose first end-point will be returned.
*
* @return The first end-point that is found to the interface, or NULL when the
* interface doesn't have any end-point yet.
*/
NetworkEndPoint_t * FreeRTOS_FirstEndPoint( const NetworkInterface_t * pxInterface )
{
NetworkEndPoint_t * pxEndPoint = pxNetworkEndPoints;
/* Find and return the NetworkEndPoint_t structure that is associated with
* the pxInterface NetworkInterface_t. *//*_RB_ Could this be made a two way link, so the NetworkEndPoint_t can just be read from the NetworkInterface_t structure? Looks like there is a pointer in the struct already. */
while( pxEndPoint != NULL )
{
if( ( pxInterface == NULL ) || ( pxEndPoint->pxNetworkInterface == pxInterface ) )
{
break;
}
pxEndPoint = pxEndPoint->pxNext;
}
return pxEndPoint;
}
/*-----------------------------------------------------------*/
/**
* @brief Get the next end-point. The parameter 'pxInterface' may be NULL, which means:
* don't care which interface the end-point is bound to.
*
* @param[in] pxInterface An interface of interest, or NULL when iterating through all
* end-points.
* @param[in] pxEndPoint This is the current end-point.
*
* @return The end-point that is found, or NULL when there are no more end-points in the list.
*/
NetworkEndPoint_t * FreeRTOS_NextEndPoint( const NetworkInterface_t * pxInterface,
NetworkEndPoint_t * pxEndPoint )
{
NetworkEndPoint_t * pxResult = pxEndPoint;
if( pxResult != NULL )
{
pxResult = pxResult->pxNext;
while( pxResult != NULL )
{
if( ( pxInterface == NULL ) || ( pxResult->pxNetworkInterface == pxInterface ) )
{
break;
}
pxResult = pxResult->pxNext;
}
}
else
{
pxResult = FreeRTOS_FirstEndPoint( pxInterface );
}
return pxResult;
}
/*-----------------------------------------------------------*/
/**
* @brief Find the end-point which has a given IPv4 address.
*
* @param[in] ulIPAddress The IP-address of interest, or 0 if any IPv4 end-point may be returned.
*
* @return The end-point found or NULL.
*/
NetworkEndPoint_t * FreeRTOS_FindEndPointOnIP_IPv4( uint32_t ulIPAddress )
{
NetworkEndPoint_t * pxEndPoint = pxNetworkEndPoints;
while( pxEndPoint != NULL )
{
#if ( ipconfigUSE_IPv4 != 0 )
#if ( ipconfigUSE_IPv6 != 0 )
if( pxEndPoint->bits.bIPv6 == 0U )
#endif
{
if( ( ulIPAddress == 0U ) ||
( pxEndPoint->ipv4_settings.ulIPAddress == ulIPAddress ) )
{
break;
}
}
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
pxEndPoint = pxEndPoint->pxNext;
}
( void ) ulIPAddress;
return pxEndPoint;
}
/*-----------------------------------------------------------*/
#if ( ipconfigUSE_IPv6 != 0 )
/**
* @brief Find the end-point which handles a given IPv6 address.
*
* @param[in] pxIPAddress The IP-address of interest.
*
* @return The end-point found or NULL.
*/
NetworkEndPoint_t * FreeRTOS_FindEndPointOnIP_IPv6( const IPv6_Address_t * pxIPAddress )
{
return FreeRTOS_InterfaceEPInSameSubnet_IPv6( NULL, pxIPAddress );
}
#endif /* ipconfigUSE_IPv6 */
/*-----------------------------------------------------------*/
/**
* @brief Find the end-point that has a certain MAC-address.
*
* @param[in] pxMACAddress The Ethernet packet.
* @param[in] pxInterface The interface on which the packet was received, or NULL when unknown.
*
* @return The end-point that has the given MAC-address.
*/
NetworkEndPoint_t * FreeRTOS_FindEndPointOnMAC( const MACAddress_t * pxMACAddress,
const NetworkInterface_t * pxInterface )
{
NetworkEndPoint_t * pxEndPoint = pxNetworkEndPoints;
/* If input MAC address is NULL, return NULL. */
if( pxMACAddress == NULL )
{
pxEndPoint = NULL;
}
/*_RB_ Question - would it be more efficient to store the mac addresses in
* uin64_t variables for direct comparison instead of using memcmp()? [don't
* know if there is a quick way of creating a 64-bit number from the 48-byte
* MAC address without getting junk in the top 2 bytes]. */
/* Find the end-point with given MAC-address. */
while( pxEndPoint != NULL )
{
if( ( pxInterface == NULL ) || ( pxInterface == pxEndPoint->pxNetworkInterface ) )
{
if( memcmp( pxEndPoint->xMACAddress.ucBytes, pxMACAddress->ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) == 0 )
{
break;
}
}
pxEndPoint = pxEndPoint->pxNext;
}
return pxEndPoint;
}
/*-----------------------------------------------------------*/
/**
* @brief Find an end-point that handles a given IPv4-address.
*
* @param[in] ulIPAddress The IP-address for which an end-point is looked-up.
*
* @return An end-point that has the same network mask as the given IP-address.
*/
NetworkEndPoint_t * FreeRTOS_FindEndPointOnNetMask( uint32_t ulIPAddress )
{
return FreeRTOS_InterfaceEndPointOnNetMask( NULL, ulIPAddress );
}
/*-----------------------------------------------------------*/
/**
* @brief Find an end-point that handles a given IPv4-address.
*
* @param[in] pxInterface Only end-points that have this interface are returned, unless
* pxInterface is NULL.
* @param[in] ulIPAddress The IP-address for which an end-point is looked-up.
*
* @return An end-point that has the same network mask as the given IP-address.
*/
NetworkEndPoint_t * FreeRTOS_InterfaceEndPointOnNetMask( const NetworkInterface_t * pxInterface,
uint32_t ulIPAddress )
{
NetworkEndPoint_t * pxEndPoint = pxNetworkEndPoints;
/* Find the best fitting end-point to reach a given IP-address. */
/*_RB_ Presumably then a broadcast reply could go out on a different end point to that on
* which the broadcast was received - although that should not be an issue if the nodes are
* on the same LAN it could be an issue if the nodes are on separate LAN's. */
while( pxEndPoint != NULL )
{
if( ( pxInterface == NULL ) || ( pxEndPoint->pxNetworkInterface == pxInterface ) )
{
#if ( ipconfigUSE_IPv4 != 0 )
#if ( ipconfigUSE_IPv6 != 0 )
if( pxEndPoint->bits.bIPv6 == pdFALSE_UNSIGNED )
#endif
{
if( ( ulIPAddress == ~0U ) ||
( ( ulIPAddress & pxEndPoint->ipv4_settings.ulNetMask ) == ( pxEndPoint->ipv4_settings.ulIPAddress & pxEndPoint->ipv4_settings.ulNetMask ) ) )
{
/* Found a match. */
break;
}
}
#endif /* if ( ipconfigUSE_IPv4 != 0 ) */
}
pxEndPoint = pxEndPoint->pxNext;
}
/* This was only for debugging. */
if( pxEndPoint == NULL )
{
FreeRTOS_debug_printf( ( "FreeRTOS_FindEndPointOnNetMask: No match for %xip\n",
( unsigned ) FreeRTOS_ntohl( ulIPAddress ) ) );
}
return pxEndPoint;
}
/*-----------------------------------------------------------*/
#if ( ipconfigUSE_IPv6 != 0 )
/**
* @brief Finds an endpoint on the given interface which is in the same subnet as the
* given IP address. If NULL is passed for pxInterface, it looks through all the
* interfaces to find an endpoint in the same subnet as the given IP address.
*
* @param[in] pxInterface Only end-points that have this interface are returned, unless
* pxInterface is NULL.
* @param[in] pxIPAddress The IPv6-address for which an end-point is looked-up.
* @return An end-point that is in the same subnet as the given IP-address.
*/
NetworkEndPoint_t * FreeRTOS_InterfaceEPInSameSubnet_IPv6( const NetworkInterface_t * pxInterface,
const IPv6_Address_t * pxIPAddress )
{
NetworkEndPoint_t * pxEndPoint = pxNetworkEndPoints;
/* Find the best fitting end-point to reach a given IP-address. */
while( pxEndPoint != NULL )
{
if( ( pxInterface == NULL ) || ( pxEndPoint->pxNetworkInterface == pxInterface ) )
{
if( pxEndPoint->bits.bIPv6 != pdFALSE_UNSIGNED )
{
if( xCompareIPv6_Address( &( pxEndPoint->ipv6_settings.xIPAddress ), pxIPAddress, pxEndPoint->ipv6_settings.uxPrefixLength ) == 0 )
{
/* Found a match. */
break;
}
}
}
pxEndPoint = pxEndPoint->pxNext;
}
return pxEndPoint;
}
/**
* @brief Configure and install a new IPv6 end-point.
*
* @param[in] pxNetworkInterface The interface to which it belongs.
* @param[in] pxEndPoint Space for the new end-point. This memory is dedicated for the
* end-point and should not be freed or get any other purpose.
* @param[in] pxIPAddress The IP-address.
* @param[in] pxNetPrefix The prefix which shall be used for this end-point.
* @param[in] uxPrefixLength The length of the above end-point.
* @param[in] pxGatewayAddress The IP-address of a device on the LAN which can serve as
* as a gateway to the Internet.
* @param[in] pxDNSServerAddress The IP-address of a DNS server.
* @param[in] ucMACAddress The MAC address of the end-point.
*/
void FreeRTOS_FillEndPoint_IPv6( NetworkInterface_t * pxNetworkInterface,
NetworkEndPoint_t * pxEndPoint,
const IPv6_Address_t * pxIPAddress,
const IPv6_Address_t * pxNetPrefix,
size_t uxPrefixLength,
const IPv6_Address_t * pxGatewayAddress,
const IPv6_Address_t * pxDNSServerAddress,
const uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] )
{
if( ( pxNetworkInterface == NULL ) ||
( pxEndPoint == NULL ) ||
( pxIPAddress == NULL ) ||
( ucMACAddress == NULL ) )
{
/* Invalid input. */
FreeRTOS_printf( ( "FreeRTOS_FillEndPoint_IPv6: Invalid input, netif=%p, endpoint=%p, pxIPAddress=%p, ucMACAddress=%p\n",
( void * ) pxNetworkInterface,
( void * ) pxEndPoint,
( void * ) pxIPAddress,
( void * ) ucMACAddress ) );
}
else
{
( void ) memset( pxEndPoint, 0, sizeof( *pxEndPoint ) );
pxEndPoint->bits.bIPv6 = pdTRUE_UNSIGNED;
pxEndPoint->ipv6_settings.uxPrefixLength = uxPrefixLength;
if( pxGatewayAddress != NULL )
{
( void ) memcpy( pxEndPoint->ipv6_settings.xGatewayAddress.ucBytes, pxGatewayAddress->ucBytes, ipSIZE_OF_IPv6_ADDRESS );
}
if( pxDNSServerAddress != NULL )
{
( void ) memcpy( pxEndPoint->ipv6_settings.xDNSServerAddresses[ 0 ].ucBytes, pxDNSServerAddress->ucBytes, ipSIZE_OF_IPv6_ADDRESS );
}
if( pxNetPrefix != NULL )
{
( void ) memcpy( pxEndPoint->ipv6_settings.xPrefix.ucBytes, pxNetPrefix->ucBytes, ipSIZE_OF_IPv6_ADDRESS );
}
/* Copy the current values to the default values. */
( void ) memcpy( &( pxEndPoint->ipv6_defaults ), &( pxEndPoint->ipv6_settings ), sizeof( pxEndPoint->ipv6_defaults ) );
( void ) memcpy( pxEndPoint->ipv6_defaults.xIPAddress.ucBytes, pxIPAddress->ucBytes, ipSIZE_OF_IPv6_ADDRESS );
( void ) memcpy( pxEndPoint->xMACAddress.ucBytes, ucMACAddress, ipMAC_ADDRESS_LENGTH_BYTES );
( void ) FreeRTOS_AddEndPoint( pxNetworkInterface, pxEndPoint );
}
}
#endif /* if ( ipconfigUSE_IPv6 != 0 ) */
/*-----------------------------------------------------------*/
#if ( ipconfigUSE_IPv6 != 0 )
/**
* @brief Find an end-point that handles a given IPv6-address.
*
* @param[in] pxIPv6Address The IP-address for which an end-point is looked-up.
*
* @return An end-point that has the same network mask as the given IP-address.
*/
NetworkEndPoint_t * FreeRTOS_FindEndPointOnNetMask_IPv6( const IPv6_Address_t * pxIPv6Address )
{
return FreeRTOS_InterfaceEPInSameSubnet_IPv6( NULL, pxIPv6Address );
}
#endif /* ipconfigUSE_IPv6 */
/*-----------------------------------------------------------*/
/**
* @brief Check IP-type, IP- and MAC-address found in the network packet.
*/
#define rMATCH_IP_ADDR 0 /**< Find an endpoint with a matching IP-address. */
#define rMATCH_NETMASK 1 /**< Find an endpoint with a matching NetMask. */
#define rMATCH_IPv6_TYPE 2 /**< Find an endpoint with a matching IPv6 type (both global or non global). */
#define rMATCH_MAC_ADDR 3 /**< Find an endpoint with a matching MAC-address. */
#define rMATCH_IP_TYPE 4 /**< Find an endpoint with a matching IP-type, v4 or v6. */
#define rMATCH_COUNT 5 /**< The number of methods. */
NetworkEndPoint_t * pxEasyFit( const NetworkInterface_t * pxNetworkInterface,
const uint16_t usFrameType,
const IP_Address_t * pxIPAddressFrom,
const IP_Address_t * pxIPAddressTo,
const MACAddress_t * pxMACAddress );
/**
* @brief Find an end-point that handles an incoming packet based on its type, source/destination & MAC address.
*
* @param[in] pxNetworkInterface The interface via which the packet was received.
* @param[in] usFrameType Frame type of the packet.
* @param[in] pxIPAddressFrom Source IP address of the packet.
* @param[in] pxIPAddressTo Destination IP address of the packet.
* @param[in] pxMACAddress Destination MAC address of the packet.
*
* @return An end-point that handles the packet.
*/
NetworkEndPoint_t * pxEasyFit( const NetworkInterface_t * pxNetworkInterface,
const uint16_t usFrameType,
const IP_Address_t * pxIPAddressFrom,
const IP_Address_t * pxIPAddressTo,
const MACAddress_t * pxMACAddress )
{
NetworkEndPoint_t * pxEndPoint;
NetworkEndPoint_t * pxReturn = NULL;
/* endpoints found for IP-type, IP-address, NetMask and MAC-address. */
NetworkEndPoint_t * pxFound[ rMATCH_COUNT ] = { NULL, NULL, NULL, NULL, NULL };
BaseType_t xCount[ rMATCH_COUNT ] = { 0, 0, 0, 0, 0 };
BaseType_t xIndex;
BaseType_t xIsIPv6 = ( usFrameType == ipIPv6_FRAME_TYPE ) ? pdTRUE : pdFALSE;
BaseType_t xGatewayTarget = pdFALSE;
BaseType_t xTargetGlobal = pdFALSE;
( void ) pxIPAddressFrom;
( void ) xGatewayTarget;
( void ) xTargetGlobal;
#if ( ipconfigUSE_IPv6 != 0 )
if( xIsIPv6 == pdTRUE )
{
/* Generic GW address fe80::1. */
static const uint8_t ucBytes[ 16 ] =
{
0xfe, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01
};
xGatewayTarget = ( memcmp( ucBytes, pxIPAddressTo->xIP_IPv6.ucBytes, 16 ) == 0 ) ? pdTRUE : pdFALSE;
if( xGatewayTarget == pdTRUE )
{
FreeRTOS_debug_printf( ( " GW address %pip to %pip\n",
( void * ) pxIPAddressFrom->xIP_IPv6.ucBytes,
( void * ) pxIPAddressTo->xIP_IPv6.ucBytes ) );
}
xTargetGlobal = ( xIPv6_GetIPType( &( pxIPAddressTo->xIP_IPv6 ) ) == eIPv6_Global ) ? pdTRUE : pdFALSE;
}
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
for( pxEndPoint = FreeRTOS_FirstEndPoint( pxNetworkInterface );
pxEndPoint != NULL;
pxEndPoint = FreeRTOS_NextEndPoint( pxNetworkInterface, pxEndPoint ) )
{
BaseType_t xSameMACAddress = ( memcmp( pxEndPoint->xMACAddress.ucBytes, pxMACAddress->ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) == 0 ) ? pdTRUE : pdFALSE;
if( xIsIPv6 == ( BaseType_t ) pxEndPoint->bits.bIPv6 )
{
pxFound[ rMATCH_IP_TYPE ] = pxEndPoint;
xCount[ rMATCH_IP_TYPE ]++;
/* Case default is impossible to reach because no endpoints for disabled IP type. */
switch( xIsIPv6 ) /* LCOV_EXCL_BR_LINE */
{
#if ( ipconfigUSE_IPv6 != 0 )
case ( BaseType_t ) pdTRUE:
{
IPv6_Type_t xEndpointType = xIPv6_GetIPType( &( pxEndPoint->ipv6_settings.xIPAddress ) );
if( xEndpointType != eIPv6_Unknown )
{
BaseType_t xEndpointGlobal = ( xEndpointType == eIPv6_Global ) ? pdTRUE : pdFALSE;
if( ( memcmp( pxEndPoint->ipv6_settings.xIPAddress.ucBytes, pxIPAddressTo->xIP_IPv6.ucBytes, ipSIZE_OF_IPv6_ADDRESS ) == 0 ) )
{
pxFound[ rMATCH_IP_ADDR ] = pxEndPoint;
xCount[ rMATCH_IP_ADDR ]++;
}
else if( xTargetGlobal == xEndpointGlobal )
{
pxFound[ rMATCH_IPv6_TYPE ] = pxEndPoint;
xCount[ rMATCH_IPv6_TYPE ]++;
}
else
{
/* do nothing, coverity happy */
}
}
else
{
/* do nothing, coverity happy */
}
}
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
case ( BaseType_t ) pdFALSE:
default:
#if ( ipconfigUSE_IPv4 != 0 )
if( pxEndPoint->ipv4_settings.ulIPAddress == pxIPAddressTo->ulIP_IPv4 )
{
pxFound[ rMATCH_IP_ADDR ] = pxEndPoint;
xCount[ rMATCH_IP_ADDR ]++;
}
else if( FreeRTOS_InterfaceEndPointOnNetMask( pxNetworkInterface, pxIPAddressFrom->ulIP_IPv4 ) == pxEndPoint )
{
pxFound[ rMATCH_NETMASK ] = pxEndPoint;
xCount[ rMATCH_NETMASK ]++;
}
else
{
/* do nothing, coverity happy */
}
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
break;
}
if( xSameMACAddress == pdTRUE )
{
xCount[ rMATCH_MAC_ADDR ]++;
pxFound[ rMATCH_MAC_ADDR ] = pxEndPoint;
}
}
}
for( xIndex = 0; xIndex < rMATCH_COUNT; xIndex++ )
{
if( xCount[ xIndex ] >= 1 )
{
pxReturn = pxFound[ xIndex ];
break;
}
}
#if ( ipconfigHAS_DEBUG_PRINTF != 0 )
if( pxReturn == NULL )
{
char pcBufferFrom[ 40 ];
char pcBufferTo[ 40 ];
BaseType_t xFamily = ( usFrameType == ipIPv6_FRAME_TYPE ) ? FREERTOS_AF_INET6 : FREERTOS_AF_INET4;
const char * xRetNtopTo;
const char * xRetNtopFrom;
xRetNtopTo = FreeRTOS_inet_ntop( xFamily,
( void * ) pxIPAddressTo->xIP_IPv6.ucBytes,
pcBufferTo,
sizeof( pcBufferTo ) );
xRetNtopFrom = FreeRTOS_inet_ntop( xFamily,
( void * ) pxIPAddressFrom->xIP_IPv6.ucBytes,
pcBufferFrom,
sizeof( pcBufferFrom ) );
FreeRTOS_debug_printf( ( "EasyFit[%x]: %d %d %d ( %s ->%s ) BAD\n",
usFrameType,
( unsigned ) xCount[ 0 ],
( unsigned ) xCount[ 1 ],
( unsigned ) xCount[ 2 ],
( xRetNtopFrom == NULL ) ? "INVALID" : pcBufferFrom,
( xRetNtopTo == NULL ) ? "INVALID" : pcBufferTo ) );
}
#endif /* ( ipconfigHAS_DEBUG_PRINTF != 0 ) */
return pxReturn;
}
/**
* @brief Find out the best matching end-point given an incoming Ethernet packet.
*
* @param[in] pxNetworkInterface The interface on which the packet was received.
* @param[in] pucEthernetBuffer The Ethernet packet that was just received.
*
* @return The end-point that should handle the incoming Ethernet packet.
*/
NetworkEndPoint_t * FreeRTOS_MatchingEndpoint( const NetworkInterface_t * pxNetworkInterface,
const uint8_t * pucEthernetBuffer )
{
NetworkEndPoint_t * pxEndPoint = NULL;
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
const ProtocolPacket_t * pxPacket = ( ( const ProtocolPacket_t * ) pucEthernetBuffer );
#if ( ipconfigUSE_IPv6 != 0 )
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
const IPPacket_IPv6_t * pxIPPacket_IPv6 = ( ( const IPPacket_IPv6_t * ) pucEthernetBuffer );
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
configASSERT( pucEthernetBuffer != NULL );
/* Check if 'pucEthernetBuffer()' has the expected alignment,
* which is 32-bits + 2. */
#ifndef _lint
{
/* MISRA Ref 11.4.3 [Casting pointer to int for verification] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
/* coverity[misra_c_2012_rule_11_4_violation] */
uintptr_t uxAddress = ( uintptr_t ) pucEthernetBuffer;
uxAddress += 2U;
configASSERT( ( uxAddress % 4U ) == 0U );
/* And in case configASSERT is not defined. */
( void ) uxAddress;
}
#endif /* ifndef _lint */
/* An Ethernet packet has been received. Inspect the contents to see which
* defined end-point has the best match.
*/
{
uint16_t usFrameType = pxPacket->xUDPPacket.xEthernetHeader.usFrameType;
IP_Address_t xIPAddressFrom;
IP_Address_t xIPAddressTo;
MACAddress_t xMACAddress;
BaseType_t xDoProcessPacket = pdFALSE;
( void ) memset( xIPAddressFrom.xIP_IPv6.ucBytes, 0, ipSIZE_OF_IPv6_ADDRESS );
( void ) memset( xIPAddressTo.xIP_IPv6.ucBytes, 0, ipSIZE_OF_IPv6_ADDRESS );
switch( usFrameType )
{
case ipIPv6_FRAME_TYPE:
/* Handle IPv6 frame types if ipconfigUSE_IPv6 != 0 */
#if ( ipconfigUSE_IPv6 != 0 )
( void ) memcpy( xIPAddressFrom.xIP_IPv6.ucBytes, pxIPPacket_IPv6->xIPHeader.xSourceAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
( void ) memcpy( xIPAddressTo.xIP_IPv6.ucBytes, pxIPPacket_IPv6->xIPHeader.xDestinationAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
xDoProcessPacket = pdTRUE;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
break;
case ipARP_FRAME_TYPE:
/* Handle ARP frame types if ipconfigUSE_IPv4 != 0 */
#if ( ipconfigUSE_IPv4 != 0 )
{
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
const ARPPacket_t * pxARPFrame = ( const ARPPacket_t * ) pucEthernetBuffer;
if( ( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REQUEST ) || ( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REPLY ) )
{
( void ) memcpy( xIPAddressFrom.xIP_IPv6.ucBytes, pxPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, sizeof( uint32_t ) );
xIPAddressTo.ulIP_IPv4 = pxPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress;
}
else
{
/* do nothing, coverity happy */
}
FreeRTOS_debug_printf( ( "pxEasyFit: ARP %xip -> %xip\n", ( unsigned ) FreeRTOS_ntohl( xIPAddressFrom.ulIP_IPv4 ), ( unsigned ) FreeRTOS_ntohl( xIPAddressTo.ulIP_IPv4 ) ) );
}
xDoProcessPacket = pdTRUE;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
break;
case ipIPv4_FRAME_TYPE:
/* Handle IPv4 frame types if ipconfigUSE_IPv4 != 0 */
#if ( ipconfigUSE_IPv4 != 0 )
xIPAddressFrom.ulIP_IPv4 = pxPacket->xUDPPacket.xIPHeader.ulSourceIPAddress;
xIPAddressTo.ulIP_IPv4 = pxPacket->xUDPPacket.xIPHeader.ulDestinationIPAddress;
xDoProcessPacket = pdTRUE;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
break;
default:
#if ( ipconfigPROCESS_CUSTOM_ETHERNET_FRAMES == 1 )
/* Custom frame types, match by MAC address only. */
xDoProcessPacket = pdTRUE;
#endif
break;
}
if( xDoProcessPacket == pdTRUE )
{
( void ) memcpy( xMACAddress.ucBytes, pxPacket->xUDPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES );
pxEndPoint = pxEasyFit( pxNetworkInterface,
usFrameType,
&xIPAddressFrom,
&xIPAddressTo,
&xMACAddress );
}
}
return pxEndPoint;
}
/*-----------------------------------------------------------*/
/**
* @brief Find an end-point that defines a gateway of a certain type ( IPv4 or IPv6 ).
*
* @param[in] xIPType The type of Gateway to look for ( ipTYPE_IPv4 or ipTYPE_IPv6 ).
*
* @return The end-point that will lead to the gateway, or NULL when no gateway was found.
*/
NetworkEndPoint_t * FreeRTOS_FindGateWay( BaseType_t xIPType )
{
NetworkEndPoint_t * pxEndPoint = pxNetworkEndPoints;
while( pxEndPoint != NULL )
{
#if ( ipconfigUSE_IPv6 == 0 )
( void ) xIPType;
if( pxEndPoint->ipv4_settings.ulGatewayAddress != 0U ) /* access to ipv4_settings is checked. */
{
break;
}
#else
if( ( xIPType == ( BaseType_t ) ipTYPE_IPv6 ) && ( pxEndPoint->bits.bIPv6 != pdFALSE_UNSIGNED ) )
{
/* Check if the IP-address is non-zero. */
if( memcmp( FreeRTOS_in6addr_any.ucBytes, pxEndPoint->ipv6_settings.xGatewayAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS ) != 0 )
{
break;
}
}