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
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
|
// SoftEther VPN Source Code
// Cedar Communication Module
//
// SoftEther VPN Server, Client and Bridge are free software under GPLv2.
//
// Copyright (c) 2012-2014 Daiyuu Nobori.
// Copyright (c) 2012-2014 SoftEther VPN Project, University of Tsukuba, Japan.
// Copyright (c) 2012-2014 SoftEther Corporation.
//
// All Rights Reserved.
//
// http://www.softether.org/
//
// Author: Daiyuu Nobori
// Comments: Tetsuo Sugiyama, Ph.D.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License version 2
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// 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.
//
// THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
// AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
//
//
// THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
// UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
// MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
// SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
// SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
// CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
// DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
// MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
// SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
// CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
// EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
// JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
// AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
// THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
//
// USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS
// YOU HAVE A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY
// CRIMINAL LAWS OR CIVIL RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS
// SOFTWARE IN OTHER COUNTRIES IS COMPLETELY AT YOUR OWN RISK. THE
// SOFTETHER VPN PROJECT HAS DEVELOPED AND DISTRIBUTED THIS SOFTWARE TO
// COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING CIVIL RIGHTS INCLUDING
// PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER COUNTRIES' LAWS OR
// CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES. WE HAVE
// NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
// INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+
// COUNTRIES AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE
// WORLD, WITH DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY
// COUNTRIES' LAWS, REGULATIONS AND CIVIL RIGHTS TO MAKE THE SOFTWARE
// COMPLY WITH ALL COUNTRIES' LAWS BY THE PROJECT. EVEN IF YOU WILL BE
// SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A PUBLIC SERVANT IN YOUR
// COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE LIABLE TO
// RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
// RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT
// JUST A STATEMENT FOR WARNING AND DISCLAIMER.
//
//
// SOURCE CODE CONTRIBUTION
// ------------------------
//
// Your contribution to SoftEther VPN Project is much appreciated.
// Please send patches to us through GitHub.
// Read the SoftEther VPN Patch Acceptance Policy in advance:
// http://www.softether.org/5-download/src/9.patch
//
//
// DEAR SECURITY EXPERTS
// ---------------------
//
// If you find a bug or a security vulnerability please kindly inform us
// about the problem immediately so that we can fix the security problem
// to protect a lot of users around the world as soon as possible.
//
// Our e-mail address for security reports is:
// softether-vpn-security [at] softether.org
//
// Please note that the above e-mail address is not a technical support
// inquiry address. If you need technical assistance, please visit
// http://www.softether.org/ and ask your question on the users forum.
//
// Thank you for your cooperation.
//
//
// NO MEMORY OR RESOURCE LEAKS
// ---------------------------
//
// The memory-leaks and resource-leaks verification under the stress
// test has been passed before release this source code.
// Layer3.c
// Layer-3 switch module
#include "CedarPch.h"
static UCHAR broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
// Process the IP queue
void L3PollingIpQueue(L3IF *f)
{
L3PACKET *p;
// Validate arguments
if (f == NULL)
{
return;
}
// Process the packet came from another session
while (p = GetNext(f->IpPacketQueue))
{
PKT *pkt = p->Packet;
// Send as an IP packet
L3SendIp(f, p);
}
}
// Process IP packets
void L3RecvIp(L3IF *f, PKT *p, bool self)
{
IPV4_HEADER *ip;
UINT header_size;
UINT next_hop = 0;
L3IF *dst;
L3PACKET *packet;
UINT new_ttl = 0;
// Validate arguments
if (f == NULL || p == NULL)
{
return;
}
ip = p->L3.IPv4Header;
header_size = IPV4_GET_HEADER_LEN(p->L3.IPv4Header) * 4;
// Calculate the checksum
if (IpCheckChecksum(ip) == false)
{
// The checksum does not match
goto FREE_PACKET;
}
// Register in the ARP table
L3KnownArp(f, ip->SrcIP, p->MacAddressSrc);
if (p->BroadcastPacket)
{
// Not to route in the case of broadcast packet
goto FREE_PACKET;
}
// Calculate the TTL
if (ip->TimeToLive >= 1)
{
new_ttl = ip->TimeToLive - 1;
}
else
{
new_ttl = 0;
}
if (new_ttl == 0)
{
if (ip->DstIP != f->IpAddress)
{
UINT src_packet_size = p->PacketSize - sizeof(MAC_HEADER);
UINT icmp_packet_total_size = sizeof(MAC_HEADER) + sizeof(IPV4_HEADER) + sizeof(ICMP_HEADER) + 4 + header_size + 8;
UCHAR *buf;
IPV4_HEADER *ipv4;
ICMP_HEADER *icmpv4;
UCHAR *data;
PKT *pkt;
UINT data_size = MIN(p->PacketSize - header_size, header_size + 8);
// Generate an ICMP message that means that the TTL has expired
buf = ZeroMalloc(icmp_packet_total_size);
ipv4 = (IPV4_HEADER *)(buf + sizeof(MAC_HEADER));
icmpv4 = (ICMP_HEADER *)(buf + sizeof(MAC_HEADER) + sizeof(IPV4_HEADER));
data = buf + sizeof(MAC_HEADER) + sizeof(IPV4_HEADER) + sizeof(ICMP_HEADER) + 4;
IPV4_SET_VERSION(ipv4, 4);
IPV4_SET_HEADER_LEN(ipv4, sizeof(IPV4_HEADER) / 4);
ipv4->TotalLength = Endian16((USHORT)(icmp_packet_total_size - sizeof(MAC_HEADER)));
ipv4->TimeToLive = 0xff;
ipv4->Protocol = IP_PROTO_ICMPV4;
ipv4->SrcIP = f->IpAddress;
ipv4->DstIP = ip->SrcIP;
ipv4->Checksum = IpChecksum(ipv4, sizeof(IPV4_HEADER));
icmpv4->Type = 11;
Copy(data, ip, data_size);
icmpv4->Checksum = IpChecksum(icmpv4, sizeof(ICMP_HEADER) + data_size + 4);
buf[12] = 0x08;
buf[13] = 0x00;
pkt = ParsePacket(buf, icmp_packet_total_size);
if (pkt == NULL)
{
Free(buf);
}
else
{
L3RecvIp(f, pkt, true);
}
// Discard the packet body whose the TTL has expired
goto FREE_PACKET;
}
}
// Rewrite the TTL
p->L3.IPv4Header->TimeToLive = (UCHAR)new_ttl;
// Get the interface corresponding to the destination IP address
dst = L3GetNextIf(f->Switch, ip->DstIP, &next_hop);
if (dst == NULL && self == false)
{
UINT src_packet_size = p->PacketSize - sizeof(MAC_HEADER);
UINT icmp_packet_total_size = sizeof(MAC_HEADER) + sizeof(IPV4_HEADER) + sizeof(ICMP_HEADER) + 4 + header_size + 8;
UCHAR *buf;
IPV4_HEADER *ipv4;
ICMP_HEADER *icmpv4;
UCHAR *data;
PKT *pkt;
UINT data_size = MIN(p->PacketSize - header_size, header_size + 8);
// Respond with ICMP that indicates that no route can be found
buf = ZeroMalloc(icmp_packet_total_size);
ipv4 = (IPV4_HEADER *)(buf + sizeof(MAC_HEADER));
icmpv4 = (ICMP_HEADER *)(buf + sizeof(MAC_HEADER) + sizeof(IPV4_HEADER));
data = buf + sizeof(MAC_HEADER) + sizeof(IPV4_HEADER) + sizeof(ICMP_HEADER) + 4;
IPV4_SET_VERSION(ipv4, 4);
IPV4_SET_HEADER_LEN(ipv4, sizeof(IPV4_HEADER) / 4);
ipv4->TotalLength = Endian16((USHORT)(icmp_packet_total_size - sizeof(MAC_HEADER)));
ipv4->TimeToLive = 0xff;
ipv4->Protocol = IP_PROTO_ICMPV4;
ipv4->SrcIP = f->IpAddress;
ipv4->DstIP = ip->SrcIP;
ipv4->Checksum = IpChecksum(ipv4, sizeof(IPV4_HEADER));
icmpv4->Type = 3;
Copy(data, ip, data_size);
icmpv4->Checksum = IpChecksum(icmpv4, sizeof(ICMP_HEADER) + data_size + 4);
buf[12] = 0x08;
buf[13] = 0x00;
pkt = ParsePacket(buf, icmp_packet_total_size);
if (pkt == NULL)
{
Free(buf);
}
else
{
L3RecvIp(f, pkt, true);
}
// Discard the packet body whose route can not be found
goto FREE_PACKET;
}
if (dst != NULL && ip->DstIP == dst->IpAddress)
{
bool free_packet = true;
// IP packet addressed to myself has arrived
if (p->TypeL4 == L4_ICMPV4)
{
ICMP_HEADER *icmp = p->L4.ICMPHeader;
if (icmp->Type == ICMP_TYPE_ECHO_REQUEST)
{
// Reply by rewriting the source and destination of the IP packet
UINT src_ip, dst_ip;
src_ip = p->L3.IPv4Header->DstIP;
dst_ip = p->L3.IPv4Header->SrcIP;
p->L3.IPv4Header->DstIP = dst_ip;
p->L3.IPv4Header->SrcIP = src_ip;
ip->TimeToLive = 0xff;
// Recalculates the checksum
ip->FlagsAndFlagmentOffset[0] = ip->FlagsAndFlagmentOffset[1] = 0;
icmp->Checksum = 0;
icmp->Type = ICMP_TYPE_ECHO_RESPONSE;
icmp->Checksum = IpChecksum(icmp, p->PacketSize - sizeof(MAC_HEADER) - header_size);
dst = L3GetNextIf(f->Switch, ip->DstIP, &next_hop);
free_packet = false;
}
}
if (free_packet)
{
goto FREE_PACKET;
}
}
if (dst == NULL)
{
// The destination does not exist
goto FREE_PACKET;
}
// Recalculate the IP checksum
ip->Checksum = 0;
ip->Checksum = IpChecksum(ip, header_size);
// Treat as a Layer-3 packet
packet = ZeroMalloc(sizeof(L3PACKET));
packet->Expire = Tick64() + IP_WAIT_FOR_ARP_TIMEOUT;
packet->NextHopIp = next_hop;
packet->Packet = p;
// Store to the destination session
L3StoreIpPacketToIf(f, dst, packet);
return;
FREE_PACKET:
// Release the packet
Free(p->PacketData);
FreePacket(p);
return;
}
// Process the Layer 2 packet
void L3RecvL2(L3IF *f, PKT *p)
{
// Validate arguments
if (f == NULL || p == NULL)
{
return;
}
// Ignore any packets except a unicast packet which is destinated other
// or a packet which I sent
if (Cmp(p->MacAddressSrc, f->MacAddress, 6) == 0 ||
(p->BroadcastPacket == false && Cmp(p->MacAddressDest, f->MacAddress, 6) != 0))
{
// Release the packet
Free(p->PacketData);
FreePacket(p);
return;
}
if (p->TypeL3 == L3_ARPV4)
{
// Received an ARP packet
L3RecvArp(f, p);
// Release the packet
Free(p->PacketData);
FreePacket(p);
}
else if (p->TypeL3 == L3_IPV4)
{
// Received an IP packet
L3RecvIp(f, p, false);
}
else
{
// Release the packet
Free(p->PacketData);
FreePacket(p);
}
}
// Store the IP packet to a different interface
void L3StoreIpPacketToIf(L3IF *src_if, L3IF *dst_if, L3PACKET *p)
{
// Validate arguments
if (src_if == NULL || p == NULL || dst_if == NULL)
{
return;
}
// Add to the queue of store-destination session
InsertQueue(dst_if->IpPacketQueue, p);
// Hit the Cancel object of the store-destination session
AddCancelList(src_if->CancelList, dst_if->Session->Cancel1);
}
// Write the packet (Process because the packet was received)
void L3PutPacket(L3IF *f, void *data, UINT size)
{
PKT *p;
L3SW *s;
if (f == NULL)
{
return;
}
s = f->Switch;
if (data != NULL)
{
// Handle the next packet
if (f->CancelList == NULL)
{
f->CancelList = NewCancelList();
}
// Packet analysis
p = ParsePacket(data, size);
if (p == NULL)
{
// Packet analysis failure
Free(data);
}
else
{
// Packet analysis success
Lock(s->lock);
{
L3RecvL2(f, p);
}
Unlock(s->lock);
}
}
else
{
// Cancel for the cancellation list after all packet processing has been finished
if (f->CancelList != NULL)
{
CancelList(f->CancelList);
ReleaseCancelList(f->CancelList);
f->CancelList = NULL;
}
}
}
// Send the waiting IP packets whose destination MAC address has been resolved
void L3SendWaitingIp(L3IF *f, UCHAR *mac, UINT ip, L3ARPENTRY *a)
{
UINT i;
LIST *o = NULL;
// Validate arguments
if (f == NULL || mac == NULL || a == NULL)
{
return;
}
for (i = 0;i < LIST_NUM(f->IpWaitList);i++)
{
L3PACKET *p = LIST_DATA(f->IpWaitList, i);
if (p->NextHopIp == ip)
{
if (o == NULL)
{
o = NewListFast(NULL);
}
Add(o, p);
}
}
if (o != NULL)
{
for (i = 0;i < LIST_NUM(o);i++)
{
L3PACKET *p = LIST_DATA(o, i);
// Transmission
L3SendIpNow(f, a, p);
Delete(f->IpWaitList, p);
Free(p->Packet->PacketData);
FreePacket(p->Packet);
Free(p);
}
ReleaseList(o);
}
}
// Register in the ARP table
void L3InsertArpTable(L3IF *f, UINT ip, UCHAR *mac)
{
L3ARPENTRY *a, t;
// Validate arguments
if (f == NULL || ip == 0 || ip == 0xffffffff || mac == NULL)
{
return;
}
Zero(&t, sizeof(t));
t.IpAddress = ip;
a = Search(f->ArpTable, &t);
if (a == NULL)
{
// Since this is not registered, register this
a = ZeroMalloc(sizeof(L3ARPENTRY));
a->IpAddress = ip;
Copy(a->MacAddress, mac, 6);
Insert(f->ArpTable, a);
}
// Extend the expiration date
a->Expire = Tick64() + ARP_ENTRY_EXPIRES;
// Send waiting IP packets
L3SendWaitingIp(f, mac, ip, a);
}
// Function to be called when the ARP resolved
void L3KnownArp(L3IF *f, UINT ip, UCHAR *mac)
{
L3ARPWAIT t, *w;
// Validate arguments
if (f == NULL || ip == 0 || ip == 0xffffffff || mac == NULL)
{
return;
}
// Delete an ARP query entry to this IP address
Zero(&t, sizeof(t));
t.IpAddress = ip;
w = Search(f->IpWaitList, &t);
if (w != NULL)
{
Delete(f->IpWaitList, w);
Free(w);
}
// Register in the ARP table
L3InsertArpTable(f, ip, mac);
}
// Issue an ARP query
void L3SendArp(L3IF *f, UINT ip)
{
L3ARPWAIT t, *w;
// Validate arguments
if (f == NULL || ip == 0 || ip == 0xffffffff)
{
return;
}
// Examine whether it has not already registered
Zero(&t, sizeof(t));
t.IpAddress = ip;
w = Search(f->ArpWaitTable, &t);
if (w != NULL)
{
// Do not do anything because it is already registered in the waiting list
return;
}
else
{
// Register in the waiting list newly
w = ZeroMalloc(sizeof(L3ARPWAIT));
w->Expire = Tick64() + ARP_REQUEST_GIVEUP;
w->IpAddress = ip;
Insert(f->ArpWaitTable, w);
}
}
// Received an ARP request
void L3RecvArpRequest(L3IF *f, PKT *p)
{
ARPV4_HEADER *a;
// Validate arguments
if (f == NULL || p == NULL)
{
return;
}
a = p->L3.ARPv4Header;
L3KnownArp(f, a->SrcIP, a->SrcAddress);
if (a->TargetIP == f->IpAddress)
{
// Respond only if the ARP packet addressed to myself
L3SendArpResponseNow(f, a->SrcAddress, a->SrcIP, f->IpAddress);
}
}
// Received an ARP response
void L3RecvArpResponse(L3IF *f, PKT *p)
{
ARPV4_HEADER *a;
// Validate arguments
if (f == NULL || p == NULL)
{
return;
}
a = p->L3.ARPv4Header;
L3KnownArp(f, a->SrcIP, a->SrcAddress);
}
// Received an ARP packet
void L3RecvArp(L3IF *f, PKT *p)
{
ARPV4_HEADER *a;
// Validate arguments
if (f == NULL || p == NULL)
{
return;
}
a = p->L3.ARPv4Header;
if (Endian16(a->HardwareType) != ARP_HARDWARE_TYPE_ETHERNET ||
Endian16(a->ProtocolType) != MAC_PROTO_IPV4 ||
a->HardwareSize != 6 || a->ProtocolSize != 4)
{
return;
}
if (Cmp(a->SrcAddress, p->MacAddressSrc, 6) != 0)
{
return;
}
switch (Endian16(a->Operation))
{
case ARP_OPERATION_REQUEST:
// ARP request arrives
L3RecvArpRequest(f, p);
break;
case ARP_OPERATION_RESPONSE:
// ARP response arrives
L3RecvArpResponse(f, p);
break;
}
}
// Send an IP packet
void L3SendIp(L3IF *f, L3PACKET *p)
{
L3ARPENTRY *a = NULL;
bool broadcast = false;
IPV4_HEADER *ip;
bool for_me = false;
// Validate arguments
if (f == NULL || p == NULL)
{
return;
}
if (p->Packet->TypeL3 != L3_IPV4)
{
return;
}
ip = p->Packet->L3.IPv4Header;
// Determining whether it's a broadcast
if (p->NextHopIp == 0xffffffff ||
((p->NextHopIp & f->SubnetMask) == (f->IpAddress & f->SubnetMask)) &&
((p->NextHopIp & (~f->SubnetMask)) == (~f->SubnetMask)))
{
broadcast = true;
}
if (broadcast == false && ip->DstIP == f->IpAddress)
{
// me?
}
else if (broadcast == false)
{
// Examine whether the ARP entry contains this in the case of unicast
a = L3SearchArpTable(f, p->NextHopIp);
if (a == NULL)
{
// Since It is not in the ARP table,
// insert it into the IP waiting list without sending immediately
p->Expire = Tick64() + IP_WAIT_FOR_ARP_TIMEOUT;
Insert(f->IpWaitList, p);
// Issue an ARP query
L3SendArp(f, p->NextHopIp);
return;
}
}
if (for_me == false)
{
// Send the IP packet
L3SendIpNow(f, a, p);
}
// Release the packet
Free(p->Packet->PacketData);
FreePacket(p->Packet);
Free(p);
}
// Send the IP packet immediately
void L3SendIpNow(L3IF *f, L3ARPENTRY *a, L3PACKET *p)
{
// Validate arguments
if (f == NULL || p == NULL)
{
return;
}
L3SendL2Now(f, a != NULL ? a->MacAddress : broadcast, f->MacAddress, Endian16(p->Packet->MacHeader->Protocol),
p->Packet->L3.PointerL3, p->Packet->PacketSize - sizeof(MAC_HEADER));
}
// Search in the ARP table
L3ARPENTRY *L3SearchArpTable(L3IF *f, UINT ip)
{
L3ARPENTRY *e, t;
// Validate arguments
if (f == NULL || ip == 0 || ip == 0xffffffff)
{
return NULL;
}
Zero(&t, sizeof(t));
t.IpAddress = ip;
e = Search(f->ArpTable, &t);
return e;
}
// Send an ARP request packet
void L3SendArpRequestNow(L3IF *f, UINT dest_ip)
{
ARPV4_HEADER arp;
// Validate arguments
if (f == NULL)
{
return;
}
// Build an ARP header
arp.HardwareType = Endian16(ARP_HARDWARE_TYPE_ETHERNET);
arp.ProtocolType = Endian16(MAC_PROTO_IPV4);
arp.HardwareSize = 6;
arp.ProtocolSize = 4;
arp.Operation = Endian16(ARP_OPERATION_REQUEST);
Copy(arp.SrcAddress, f->MacAddress, 6);
arp.SrcIP = f->IpAddress;
Zero(&arp.TargetAddress, 6);
arp.TargetIP = dest_ip;
// Transmission
L3SendL2Now(f, broadcast, f->MacAddress, MAC_PROTO_ARPV4, &arp, sizeof(arp));
}
// Send an ARP response packet
void L3SendArpResponseNow(L3IF *f, UCHAR *dest_mac, UINT dest_ip, UINT src_ip)
{
ARPV4_HEADER arp;
// Validate arguments
if (f == NULL || dest_mac == NULL)
{
return;
}
// Build a header
arp.HardwareType = Endian16(ARP_HARDWARE_TYPE_ETHERNET);
arp.ProtocolType = Endian16(MAC_PROTO_IPV4);
arp.HardwareSize = 6;
arp.ProtocolSize = 4;
arp.Operation = Endian16(ARP_OPERATION_RESPONSE);
Copy(arp.SrcAddress, f->MacAddress, 6);
Copy(arp.TargetAddress, dest_mac, 6);
arp.SrcIP = src_ip;
arp.TargetIP = dest_ip;
// Transmission
L3SendL2Now(f, dest_mac, f->MacAddress, MAC_PROTO_ARPV4, &arp, sizeof(arp));
}
// Generate a MAC address of the interface
void L3GenerateMacAddress(L3IF *f)
{
BUF *b;
UCHAR hash[SHA1_SIZE];
// Validate arguments
if (f == NULL)
{
return;
}
b = NewBuf();
WriteBuf(b, f->Switch->Name, StrLen(f->Switch->Name));
WriteBuf(b, f->HubName, StrLen(f->HubName));
WriteBuf(b, &f->IpAddress, sizeof(f->IpAddress));
GenMacAddress(f->MacAddress);
Hash(hash, b->Buf, b->Size, true);
Copy(f->MacAddress + 2, hash, 4);
f->MacAddress[1] = 0xA3;
FreeBuf(b);
}
// Send an L2 packet immediately
void L3SendL2Now(L3IF *f, UCHAR *dest_mac, UCHAR *src_mac, USHORT protocol, void *data, UINT size)
{
UCHAR *buf;
MAC_HEADER *mac_header;
PKT *p;
// Validate arguments
if (f == NULL || dest_mac == NULL || src_mac == NULL || data == NULL)
{
return;
}
// Buffer creation
buf = Malloc(MAC_HEADER_SIZE + size);
// MAC header
mac_header = (MAC_HEADER *)&buf[0];
Copy(mac_header->DestAddress, dest_mac, 6);
Copy(mac_header->SrcAddress, src_mac, 6);
mac_header->Protocol = Endian16(protocol);
// Copy data
Copy(&buf[sizeof(MAC_HEADER)], data, size);
// Size
size += sizeof(MAC_HEADER);
// Packet generation
p = ZeroMalloc(sizeof(PKT));
p->PacketData = buf;
p->PacketSize = size;
// Add to the queue
InsertQueue(f->SendQueue, p);
}
// Polling for the ARP resolution waiting list
void L3PollingArpWaitTable(L3IF *f)
{
UINT i;
LIST *o = NULL;
// Validate arguments
if (f == NULL)
{
return;
}
for (i = 0;i < LIST_NUM(f->ArpWaitTable);i++)
{
L3ARPWAIT *w = LIST_DATA(f->ArpWaitTable, i);
if (w->Expire <= Tick64())
{
// The ARP request entry is expired
if (o == NULL)
{
o = NewListFast(NULL);
}
Insert(o, w);
}
else if ((w->LastSentTime + ARP_REQUEST_TIMEOUT) <= Tick64())
{
// Send a next ARP request packet
w->LastSentTime = Tick64();
L3SendArpRequestNow(f, w->IpAddress);
}
}
if (o != NULL)
{
for (i = 0;i < LIST_NUM(o);i++)
{
L3ARPWAIT *w = LIST_DATA(o, i);
Delete(f->ArpWaitTable, w);
Free(w);
}
ReleaseList(o);
}
}
// Clear old ARP table entries
void L3DeleteOldArpTable(L3IF *f)
{
UINT i;
LIST *o = NULL;
// Validate arguments
if (f == NULL)
{
return;
}
if ((f->LastDeleteOldArpTable + ARP_ENTRY_POLLING_TIME) > Tick64())
{
return;
}
f->LastDeleteOldArpTable = Tick64();
for (i = 0;i < LIST_NUM(f->ArpTable);i++)
{
L3ARPENTRY *a = LIST_DATA(f->ArpTable, i);
if (a->Expire <= Tick64())
{
// Expired
if (o == NULL)
{
o = NewListFast(NULL);
}
Insert(o, a);
}
}
if (o != NULL)
{
for (i = 0;i < LIST_NUM(o);i++)
{
L3ARPENTRY *a = LIST_DATA(o, i);
Delete(f->ArpTable, a);
Free(a);
}
ReleaseList(o);
}
}
// Clear the IP waiting list
void L3DeleteOldIpWaitList(L3IF *f)
{
UINT i;
LIST *o = NULL;
// Validate arguments
if (f == NULL)
{
return;
}
for (i = 0;i < LIST_NUM(f->IpWaitList);i++)
{
L3PACKET *p = LIST_DATA(f->IpWaitList, i);
if (p->Expire <= Tick64())
{
if (o == NULL)
{
o = NewListFast(NULL);
}
Insert(o, p);
}
}
if (o != NULL)
{
for (i = 0;i < LIST_NUM(o);i++)
{
L3PACKET *p = LIST_DATA(o, i);
Delete(f->IpWaitList, p);
Free(p->Packet->PacketData);
FreePacket(p->Packet);
Free(p);
}
ReleaseList(o);
}
}
// Beacon transmission
void L3PollingBeacon(L3IF *f)
{
// Validate arguments
if (f == NULL)
{
return;
}
if (f->LastBeaconSent == 0 ||
(f->LastBeaconSent + BEACON_SEND_INTERVAL) <= Tick64())
{
UINT dest_ip;
UCHAR *udp_buf;
UINT udp_buf_size;
ARPV4_HEADER arp;
IPV4_HEADER *ip;
UDP_HEADER *udp;
static char beacon_str[] =
"PacketiX VPN Virtual Layer-3 Switch Beacon";
// Send an UDP
dest_ip = (f->IpAddress & f->SubnetMask) | (~f->SubnetMask);
udp_buf_size = sizeof(IPV4_HEADER) + sizeof(UDP_HEADER) + sizeof(beacon_str);
udp_buf = ZeroMalloc(udp_buf_size);
ip = (IPV4_HEADER *)udp_buf;
udp = (UDP_HEADER *)(udp_buf + sizeof(IPV4_HEADER));
udp->DstPort = Endian16(7);
udp->SrcPort = Endian16(7);
udp->PacketLength = Endian16(sizeof(UDP_HEADER) + sizeof(beacon_str));
Copy(udp_buf + sizeof(IPV4_HEADER) + sizeof(UDP_HEADER), beacon_str, sizeof(beacon_str));
udp->Checksum = CalcChecksumForIPv4(f->IpAddress, dest_ip, 0x11, udp, sizeof(UDP_HEADER) + sizeof(beacon_str), 0);
ip->DstIP = dest_ip;
IPV4_SET_VERSION(ip, 4);
IPV4_SET_HEADER_LEN(ip, (IP_HEADER_SIZE / 4));
ip->TypeOfService = DEFAULT_IP_TOS;
ip->TotalLength = Endian16((USHORT)(udp_buf_size));
ip->TimeToLive = DEFAULT_IP_TTL;
ip->Protocol = IP_PROTO_UDP;
ip->SrcIP = f->IpAddress;
ip->Checksum = IpChecksum(ip, IP_HEADER_SIZE);
L3SendL2Now(f, broadcast, f->MacAddress, MAC_PROTO_IPV4, udp_buf, udp_buf_size);
Free(udp_buf);
// Build the ARP header
arp.HardwareType = Endian16(ARP_HARDWARE_TYPE_ETHERNET);
arp.ProtocolType = Endian16(MAC_PROTO_IPV4);
arp.HardwareSize = 6;
arp.ProtocolSize = 4;
arp.Operation = Endian16(ARP_OPERATION_RESPONSE);
Copy(arp.SrcAddress, f->MacAddress, 6);
arp.SrcIP = f->IpAddress;
arp.TargetAddress[0] =
arp.TargetAddress[1] =
arp.TargetAddress[2] =
arp.TargetAddress[3] =
arp.TargetAddress[4] =
arp.TargetAddress[5] = 0xff;
arp.TargetIP = dest_ip;
// Transmission
L3SendL2Now(f, broadcast, f->MacAddress, MAC_PROTO_ARPV4, &arp, sizeof(arp));
f->LastBeaconSent = Tick64();
}
}
// Polling process
void L3Polling(L3IF *f)
{
L3SW *s;
// Validate arguments
if (f == NULL)
{
return;
}
s = f->Switch;
// Lock the entire switch in the middle of the polling process
Lock(s->lock);
{
// Beacon transmission
L3PollingBeacon(f);
// Process the IP queue
L3PollingIpQueue(f);
// Clear old ARP table entries
L3DeleteOldArpTable(f);
// Polling ARP resolution waiting list
L3PollingArpWaitTable(f);
// Clear the IP waiting list
L3DeleteOldIpWaitList(f);
}
Unlock(s->lock);
}
// Get the next packet
UINT L3GetNextPacket(L3IF *f, void **data)
{
UINT ret = 0;
// Validate arguments
if (f == NULL || data == NULL)
{
return 0;
}
START:
// Examine the send queue
LockQueue(f->SendQueue);
{
PKT *p = GetNext(f->SendQueue);
if (p != NULL)
{
// There is a packet
ret = p->PacketSize;
*data = p->PacketData;
// Packet structure may be discarded
Free(p);
}
}
UnlockQueue(f->SendQueue);
if (ret == 0)
{
// Polling process
L3Polling(f);
// Examine whether a new packet is queued for results of the polling process
if (f->SendQueue->num_item != 0)
{
// Get the packet immediately if it's in the queue
goto START;
}
}
return ret;
}
// Determine the packet destined for the specified IP address should be sent to which interface
L3IF *L3GetNextIf(L3SW *s, UINT ip, UINT *next_hop)
{
UINT i;
L3IF *f;
UINT next_hop_ip = 0;
// Validate arguments
if (s == NULL || ip == 0 || ip == 0xffffffff)
{
return NULL;
}
f = NULL;
// Examine whether the specified IP address is contained
// in the networks which each interfaces belong to
for (i = 0;i < LIST_NUM(s->IfList);i++)
{
L3IF *ff = LIST_DATA(s->IfList, i);
if ((ff->IpAddress & ff->SubnetMask) == (ip & ff->SubnetMask))
{
f = ff;
next_hop_ip = ip;
break;
}
}
if (f == NULL)
{
// Find the routing table if it's not found
L3TABLE *t = L3GetBestRoute(s, ip);
if (t == NULL)
{
// Still not found
return NULL;
}
else
{
// Find the interface with the IP address of the router of
// NextHop of the found route
for (i = 0;i < LIST_NUM(s->IfList);i++)
{
L3IF *ff = LIST_DATA(s->IfList, i);
if ((ff->IpAddress & ff->SubnetMask) == (t->GatewayAddress & ff->SubnetMask))
{
f = ff;
next_hop_ip = t->GatewayAddress;
break;
}
}
}
}
if (f == NULL)
{
// Destination interface was unknown after all
return NULL;
}
if (next_hop != NULL)
{
*next_hop = next_hop_ip;
}
return f;
}
// Get the best routing table entry for the specified IP address
L3TABLE *L3GetBestRoute(L3SW *s, UINT ip)
{
UINT i;
UINT max_mask = 0;
UINT min_metric = INFINITE;
L3TABLE *ret = NULL;
// Validate arguments
if (s == NULL || ip == 0)
{
return NULL;
}
// 1st condition: Choose the one which have the largest subnet mask
// 2nd condition: Choose the one which have the smallest metric
for (i = 0;i < LIST_NUM(s->TableList);i++)
{
L3TABLE *t = LIST_DATA(s->TableList, i);
if ((t->NetworkAddress & t->SubnetMask) == (ip & t->SubnetMask))
{
if (t->SubnetMask >= max_mask)
{
max_mask = t->SubnetMask;
if (min_metric >= t->Metric)
{
min_metric = t->Metric;
ret = t;
}
}
}
}
return ret;
}
// Initialize the Layer-3 interface
void L3InitInterface(L3IF *f)
{
// Validate arguments
if (f == NULL)
{
return;
}
// MAC address generation
L3GenerateMacAddress(f);
// List generation
f->ArpTable = NewList(CmpL3ArpEntry);
f->ArpWaitTable = NewList(CmpL3ArpWaitTable);
f->IpPacketQueue = NewQueue();
f->IpWaitList = NewList(NULL);
f->SendQueue = NewQueue();
}
// Release the Layer-3 interface
void L3FreeInterface(L3IF *f)
{
UINT i;
L3PACKET *p;
PKT *pkt;
// Validate arguments
if (f == NULL)
{
return;
}
for (i = 0;i < LIST_NUM(f->ArpTable);i++)
{
L3ARPENTRY *a = LIST_DATA(f->ArpTable, i);
Free(a);
}
ReleaseList(f->ArpTable);
f->ArpTable = NULL;
for (i = 0;i < LIST_NUM(f->ArpWaitTable);i++)
{
L3ARPWAIT *w = LIST_DATA(f->ArpWaitTable, i);
Free(w);
}
ReleaseList(f->ArpWaitTable);
f->ArpWaitTable = NULL;
while (p = GetNext(f->IpPacketQueue))
{
Free(p->Packet->PacketData);
FreePacket(p->Packet);
Free(p);
}
ReleaseQueue(f->IpPacketQueue);
f->IpPacketQueue = NULL;
for (i = 0;i < LIST_NUM(f->IpWaitList);i++)
{
L3PACKET *p = LIST_DATA(f->IpWaitList, i);
Free(p->Packet->PacketData);
FreePacket(p->Packet);
Free(p);
}
ReleaseList(f->IpWaitList);
f->IpWaitList = NULL;
while (pkt = GetNext(f->SendQueue))
{
Free(pkt->PacketData);
FreePacket(pkt);
}
ReleaseQueue(f->SendQueue);
f->SendQueue = NULL;
}
// Layer-3 interface thread
void L3IfThread(THREAD *t, void *param)
{
L3IF *f;
CONNECTION *c;
SESSION *s;
POLICY *policy;
char tmp[MAX_SIZE];
char name[MAX_SIZE];
char username[MAX_SIZE];
// Validate arguments
if (t == NULL || param == NULL)
{
return;
}
f = (L3IF *)param;
StrCpy(username, sizeof(username), L3_USERNAME);
if (f->Switch != NULL)
{
StrCat(username, sizeof(username), f->Switch->Name);
}
// Create a connection
c = NewServerConnection(f->Switch->Cedar, NULL, t);
c->Protocol = CONNECTION_HUB_LAYER3;
// Create a Session
policy = ClonePolicy(GetDefaultPolicy());
// Not to limit the number of broadcast by policy
policy->NoBroadcastLimiter = true;
s = NewServerSession(f->Switch->Cedar, c, f->Hub, username, policy);
c->Session = s;
ReleaseConnection(c);
// Determine the name of the session
GetMachineHostName(tmp, sizeof(tmp));
if (f->Switch->Cedar->Server->ServerType == SERVER_TYPE_STANDALONE)
{
Format(name, sizeof(name), "SID-L3-%s-%u", f->Switch->Name, Inc(f->Hub->SessionCounter));
}
else
{
Format(name, sizeof(name), "SID-L3-%s-%s-%u", tmp, f->Switch->Name, Inc(f->Hub->SessionCounter));
}
ConvertSafeFileName(name, sizeof(name), name);
StrUpper(name);
Free(s->Name);
s->Name = CopyStr(name);
s->L3SwitchMode = true;
s->L3If = f;
if (s->Username != NULL)
{
Free(s->Username);
}
s->Username = CopyStr(username);
StrCpy(s->UserNameReal, sizeof(s->UserNameReal), username);
f->Session = s;
AddRef(s->ref);
// Notify the initialization completion
NoticeThreadInit(t);
// Session main process
SessionMain(s);
// Release the session
ReleaseSession(s);
}
// Initialize all Layer-3 interfaces
void L3InitAllInterfaces(L3SW *s)
{
UINT i;
// Validate arguments
if (s == NULL)
{
return;
}
for (i = 0;i < LIST_NUM(s->IfList);i++)
{
L3IF *f = LIST_DATA(s->IfList, i);
THREAD *t;
L3InitInterface(f);
f->Hub = GetHub(s->Cedar, f->HubName);
t = NewThread(L3IfThread, f);
WaitThreadInit(t);
ReleaseThread(t);
}
}
// Release all Layer-3 interfaces
void L3FreeAllInterfaces(L3SW *s)
{
UINT i;
// Validate arguments
if (s == NULL)
{
return;
}
for (i = 0;i < LIST_NUM(s->IfList);i++)
{
L3IF *f = LIST_DATA(s->IfList, i);
ReleaseHub(f->Hub);
f->Hub = NULL;
ReleaseSession(f->Session);
f->Session = NULL;
L3FreeInterface(f);
}
}
// Layer-3 test
void L3Test(SERVER *s)
{
L3SW *ss = L3AddSw(s->Cedar, "TEST");
L3AddIf(ss, "DEFAULT", 0x0101a8c0, 0x00ffffff);
L3AddIf(ss, "DEFAULT2", 0x0102a8c0, 0x00ffffff);
L3SwStart(ss);
ReleaseL3Sw(ss);
}
// Layer-3 switch thread
void L3SwThread(THREAD *t, void *param)
{
L3SW *s;
bool shutdown_now = false;
// Validate arguments
if (t == NULL || param == NULL)
{
return;
}
s = (L3SW *)param;
s->Active = true;
NoticeThreadInit(t);
// Operation start
SLog(s->Cedar, "L3_SWITCH_START", s->Name);
while (s->Halt == false)
{
if (s->Online == false)
{
// Because the L3 switch is off-line now,
// attempt to make it on-line periodically
LockList(s->Cedar->HubList);
{
Lock(s->lock);
{
UINT i;
UINT n = 0;
bool all_exists = true;
if (LIST_NUM(s->IfList) == 0)
{
// Don't operate if there is no interface
all_exists = false;
}
for (i = 0;i < LIST_NUM(s->IfList);i++)
{
L3IF *f = LIST_DATA(s->IfList, i);
HUB *h = GetHub(s->Cedar, f->HubName);
if (h != NULL)
{
if (h->Offline || h->Type == HUB_TYPE_FARM_DYNAMIC)
{
all_exists = false;
}
else
{
n++;
}
ReleaseHub(h);
}
else
{
all_exists = false;
}
}
if (all_exists && n >= 1)
{
// Start the operation because all Virtual HUBs for
// interfaces are enabled
SLog(s->Cedar, "L3_SWITCH_ONLINE", s->Name);
L3InitAllInterfaces(s);
s->Online = true;
}
}
Unlock(s->lock);
}
UnlockList(s->Cedar->HubList);
}
else
{
// Examine periodically whether all sessions terminated
UINT i;
bool any_halted = false;
LIST *o = NULL;
SHUTDOWN:
Lock(s->lock);
{
for (i = 0;i < LIST_NUM(s->IfList);i++)
{
L3IF *f = LIST_DATA(s->IfList, i);
if (f->Session->Halt || f->Hub->Offline != false)
{
any_halted = true;
break;
}
}
if (shutdown_now)
{
any_halted = true;
}
if (any_halted)
{
SLog(s->Cedar, "L3_SWITCH_OFFLINE", s->Name);
o = NewListFast(NULL);
// If there is any terminated session, terminate all sessions
for (i = 0;i < LIST_NUM(s->IfList);i++)
{
L3IF *f = LIST_DATA(s->IfList, i);
Insert(o, f->Session);
}
// Restore to the offline
s->Online = false;
}
}
Unlock(s->lock);
if (o != NULL)
{
UINT i;
for (i = 0;i < LIST_NUM(o);i++)
{
SESSION *s = LIST_DATA(o, i);
StopSession(s);
}
L3FreeAllInterfaces(s);
ReleaseList(o);
o = NULL;
}
}
SleepThread(50);
}
if (s->Online != false)
{
shutdown_now = true;
goto SHUTDOWN;
}
// Stop the operation
SLog(s->Cedar, "L3_SWITCH_STOP", s->Name);
}
// Start a Layer-3 switch
void L3SwStart(L3SW *s)
{
// Validate arguments
if (s == NULL)
{
return;
}
Lock(s->lock);
{
if (s->Active == false)
{
// Start if there is registered interface
if (LIST_NUM(s->IfList) >= 1)
{
s->Halt = false;
// Create a thread
s->Thread = NewThread(L3SwThread, s);
WaitThreadInit(s->Thread);
}
}
}
Unlock(s->lock);
}
// Stop the Layer-3 switch
void L3SwStop(L3SW *s)
{
THREAD *t = NULL;
// Validate arguments
if (s == NULL)
{
return;
}
Lock(s->lock);
{
if (s->Active == false)
{
Unlock(s->lock);
return;
}
s->Halt = true;
t = s->Thread;
s->Active = false;
}
Unlock(s->lock);
WaitThread(t, INFINITE);
ReleaseThread(t);
}
// Add a Layer-3 switch
L3SW *L3AddSw(CEDAR *c, char *name)
{
L3SW *s = NULL;
// Validate arguments
if (c == NULL || name == NULL)
{
return NULL;
}
LockList(c->L3SwList);
{
s = L3GetSw(c, name);
if (s == NULL)
{
s = NewL3Sw(c, name);
Insert(c->L3SwList, s);
AddRef(s->ref);
}
else
{
ReleaseL3Sw(s);
s = NULL;
}
}
UnlockList(c->L3SwList);
return s;
}
// Delete the Layer-3 switch
bool L3DelSw(CEDAR *c, char *name)
{
L3SW *s;
bool ret = false;
// Validate arguments
if (c == NULL || name == NULL)
{
return false;
}
LockList(c->L3SwList);
{
s = L3GetSw(c, name);
if (s != NULL)
{
// Stop and delete
L3SwStop(s);
Delete(c->L3SwList, s);
ReleaseL3Sw(s);
ReleaseL3Sw(s);
ret = true;
}
}
UnlockList(c->L3SwList);
return ret;
}
// Delete the routing table
bool L3DelTable(L3SW *s, L3TABLE *tbl)
{
bool ret = false;
// Validate arguments
if (s == NULL || tbl == NULL)
{
return false;
}
Lock(s->lock);
{
if (s->Active == false)
{
L3TABLE *t = Search(s->TableList, tbl);
if (t != NULL)
{
Delete(s->TableList, t);
Free(t);
ret = true;
}
}
}
Unlock(s->lock);
return ret;
}
// Add to the routing table
bool L3AddTable(L3SW *s, L3TABLE *tbl)
{
bool ret = false;
// Validate arguments
if (s == NULL || tbl == NULL)
{
return false;
}
if (tbl->Metric == 0 || tbl->GatewayAddress == 0 || tbl->GatewayAddress == 0xffffffff)
{
return false;
}
Lock(s->lock);
{
if (LIST_NUM(s->TableList) >= GetServerCapsInt(s->Cedar->Server, "i_max_l3_table"))
{
// Too many
}
else
{
// Create
if (s->Active == false)
{
if (Search(s->TableList, tbl) == NULL)
{
L3TABLE *t = ZeroMalloc(sizeof(L3TABLE));
Copy(t, tbl, sizeof(L3TABLE));
Insert(s->TableList, t);
ret = true;
}
}
}
}
Unlock(s->lock);
return ret;
}
// Get the L3 switch
L3SW *L3GetSw(CEDAR *c, char *name)
{
L3SW t, *s;
// Validate arguments
if (c == NULL || name == NULL)
{
return NULL;
}
Zero(&t, sizeof(t));
StrCpy(t.Name, sizeof(t.Name), name);
LockList(c->L3SwList);
{
s = Search(c->L3SwList, &t);
}
UnlockList(c->L3SwList);
if (s != NULL)
{
AddRef(s->ref);
}
return s;
}
// Get the interface that is connected to the specified Virtual HUB from the L3 switch
L3IF *L3SearchIf(L3SW *s, char *hubname)
{
L3IF t, *f;
// Validate arguments
if (s == NULL || hubname == NULL)
{
return NULL;
}
Zero(&t, sizeof(t));
StrCpy(t.HubName, sizeof(t.HubName), hubname);
f = Search(s->IfList, &t);
return f;
}
// Delete the interface
bool L3DelIf(L3SW *s, char *hubname)
{
L3IF *f;
bool ret = false;
// Validate arguments
if (s == NULL || hubname == NULL)
{
return false;
}
Lock(s->lock);
{
if (s->Active == false)
{
f = L3SearchIf(s, hubname);
if (f != NULL)
{
// Remove
Delete(s->IfList, f);
Free(f);
ret = true;
}
}
}
Unlock(s->lock);
return ret;
}
// Add an interface
bool L3AddIf(L3SW *s, char *hubname, UINT ip, UINT subnet)
{
L3IF *f;
bool ret = false;
// Validate arguments
if (s == NULL || hubname == NULL || IsSafeStr(hubname) == false ||
ip == 0 || ip == 0xffffffff)
{
return false;
}
Lock(s->lock);
{
if (LIST_NUM(s->TableList) >= GetServerCapsInt(s->Cedar->Server, "i_max_l3_if"))
{
// Too many
}
else
{
if (s->Active == false)
{
// Examine whether the interface is already in the same Virtual HUB
if (L3SearchIf(s, hubname) == NULL)
{
// Add
f = ZeroMalloc(sizeof(L3IF));
f->Switch = s;
StrCpy(f->HubName, sizeof(f->HubName), hubname);
f->IpAddress = ip;
f->SubnetMask = subnet;
Insert(s->IfList, f);
ret = true;
}
}
}
}
Unlock(s->lock);
return ret;
}
// Clean-up the L3 switch
void CleanupL3Sw(L3SW *s)
{
UINT i;
// Validate arguments
if (s == NULL)
{
return;
}
for (i = 0;i < LIST_NUM(s->IfList);i++)
{
L3IF *f = LIST_DATA(s->IfList, i);
Free(f);
}
ReleaseList(s->IfList);
for (i = 0;i < LIST_NUM(s->TableList);i++)
{
L3TABLE *t = LIST_DATA(s->TableList, i);
Free(t);
}
ReleaseList(s->TableList);
DeleteLock(s->lock);
Free(s);
}
// Release the L3 switch
void ReleaseL3Sw(L3SW *s)
{
// Validate arguments
if (s == NULL)
{
return;
}
if (Release(s->ref) == 0)
{
CleanupL3Sw(s);
}
}
// Create a new L3 switch
L3SW *NewL3Sw(CEDAR *c, char *name)
{
L3SW *o;
// Validate arguments
if (c == NULL || name == NULL)
{
return NULL;
}
o = ZeroMalloc(sizeof(L3SW));
StrCpy(o->Name, sizeof(o->Name), name);
o->lock = NewLock();
o->ref = NewRef();
o->Cedar = c;
o->Active = false;
o->IfList = NewList(CmpL3If);
o->TableList = NewList(CmpL3Table);
return o;
}
// Stop all L3 switches in the Cedar
void L3FreeAllSw(CEDAR *c)
{
LIST *o;
UINT i;
// Validate arguments
if (c == NULL)
{
return;
}
o = NewListFast(NULL);
LockList(c->L3SwList);
{
for (i = 0;i < LIST_NUM(c->L3SwList);i++)
{
L3SW *s = LIST_DATA(c->L3SwList, i);
Insert(o, CopyStr(s->Name));
}
for (i = 0;i < LIST_NUM(o);i++)
{
char *name = LIST_DATA(o, i);
L3DelSw(c, name);
Free(name);
}
ReleaseList(o);
}
UnlockList(c->L3SwList);
}
// Stop the L3 switch function of the Cedar
void FreeCedarLayer3(CEDAR *c)
{
// Validate arguments
if (c == NULL)
{
return;
}
ReleaseList(c->L3SwList);
c->L3SwList = NULL;
}
// Start the L3 switch function of the Cedar
void InitCedarLayer3(CEDAR *c)
{
// Validate arguments
if (c == NULL)
{
return;
}
c->L3SwList = NewList(CmpL3Sw);
}
// Interface comparison function
int CmpL3If(void *p1, void *p2)
{
L3IF *f1, *f2;
if (p1 == NULL || p2 == NULL)
{
return 0;
}
f1 = *(L3IF **)p1;
f2 = *(L3IF **)p2;
if (f1 == NULL || f2 == NULL)
{
return 0;
}
return StrCmpi(f1->HubName, f2->HubName);
}
// Routing table entry comparison function
int CmpL3Table(void *p1, void *p2)
{
L3TABLE *t1, *t2;
if (p1 == NULL || p2 == NULL)
{
return 0;
}
t1 = *(L3TABLE **)p1;
t2 = *(L3TABLE **)p2;
if (t1 == NULL || t2 == NULL)
{
return 0;
}
if (t1->NetworkAddress > t2->NetworkAddress)
{
return 1;
}
else if (t1->NetworkAddress < t2->NetworkAddress)
{
return -1;
}
else if (t1->SubnetMask > t2->SubnetMask)
{
return 1;
}
else if (t1->SubnetMask < t2->SubnetMask)
{
return -1;
}
else if (t1->GatewayAddress > t2->GatewayAddress)
{
return 1;
}
else if (t1->GatewayAddress < t2->GatewayAddress)
{
return -1;
}
else if (t1->Metric > t2->Metric)
{
return 1;
}
else if (t1->Metric < t2->Metric)
{
return -1;
}
else
{
return 0;
}
}
// L3SW comparison function
int CmpL3Sw(void *p1, void *p2)
{
L3SW *s1, *s2;
if (p1 == NULL || p2 == NULL)
{
return 0;
}
s1 = *(L3SW **)p1;
s2 = *(L3SW **)p2;
if (s1 == NULL || s2 == NULL)
{
return 0;
}
return StrCmpi(s1->Name, s2->Name);
}
// ARP waiting entry comparison function
int CmpL3ArpWaitTable(void *p1, void *p2)
{
L3ARPWAIT *w1, *w2;
if (p1 == NULL || p2 == NULL)
{
return 0;
}
w1 = *(L3ARPWAIT **)p1;
w2 = *(L3ARPWAIT **)p2;
if (w1 == NULL || w2 == NULL)
{
return 0;
}
if (w1->IpAddress > w2->IpAddress)
{
return 1;
}
else if (w1->IpAddress < w2->IpAddress)
{
return -1;
}
else
{
return 0;
}
}
// ARP entries comparison function
int CmpL3ArpEntry(void *p1, void *p2)
{
L3ARPENTRY *e1, *e2;
if (p1 == NULL || p2 == NULL)
{
return 0;
}
e1 = *(L3ARPENTRY **)p1;
e2 = *(L3ARPENTRY **)p2;
if (e1 == NULL || e2 == NULL)
{
return 0;
}
if (e1->IpAddress > e2->IpAddress)
{
return 1;
}
else if (e1->IpAddress < e2->IpAddress)
{
return -1;
}
else
{
return 0;
}
}
// Developed by SoftEther VPN Project at University of Tsukuba in Japan.
// Department of Computer Science has dozens of overly-enthusiastic geeks.
// Join us: http://www.tsukuba.ac.jp/english/admission/
|