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
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Routines in this program implement OS-specific portions of
* a Mobile-IP agent (RFC 2002).
*
*/
#ifndef _REENTRANT
#error "Error! Reentrant must be defined!"
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <netdb.h>
#include <errno.h>
#include <assert.h>
#include <synch.h>
#include <syslog.h>
#include <stropts.h>
#include <sys/dlpi.h>
#include <sys/types.h>
#include <sys/stream.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/stropts.h>
#include <sys/stropts.h>
#include <sys/types.h>
#include <inet/common.h>
#include <inet/ip.h>
#include <net/if.h>
#include <net/route.h>
#include <net/if_dl.h>
#include <net/pfkeyv2.h> /* ipsec alg values, etc. */
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include "agent.h"
#include "mip.h"
#include "agentKernelIntfce.h"
#include "conflib.h"
/* Check for bad os version, and compile anyway xxx WORK -- remove this */
#ifndef IFF_NORTEXCH
#define IFF_NORTEXCH 0
#define IFF_MIPRUNNING 1
#define RTF_PRIVATE 2
#endif
/*
* Routing socket message len for creating route for
* reverse tunnel
*/
#define MIP_RTUN_RTM_MSGLEN sizeof (struct rt_msghdr) + \
sizeof (struct sockaddr_in) + \
sizeof (struct sockaddr_in) + \
sizeof (struct sockaddr_in) + \
sizeof (struct sockaddr_dl) + \
sizeof (struct sockaddr_in) + \
sizeof (struct sockaddr_dl)
/*
* Routing socket message len for normal route
* created to send reg reply to MN
*/
#define MIP_RTM_MSGLEN sizeof (struct rt_msghdr) + \
sizeof (struct sockaddr_in) + \
sizeof (struct sockaddr_in) + \
sizeof (struct sockaddr_in)
/* Common to all mobility agents */
extern struct hash_table haMobileNodeHash;
extern struct hash_table maAdvConfigHash;
extern struct hash_table mipTunlHash;
extern int logVerbosity;
struct dynamicIfacetype *dynamicIfaceHead;
static int ioctl_sockid;
static int rtsock;
static rwlock_t gbl_tunnelLock;
static int first_tun_to_check = 0;
static struct staticIface *StaticIntfaceHead = NULL;
char *err2str(int);
#define DEVICEDIR "/dev/"
#ifndef ETH_ALEN
#define ETH_ALEN sizeof (struct ether_addr)
#endif
/* Definitions needed for refresh_mn_arp() module */
#define BITSPERBYTE 8
#define IPADDRL sizeof (struct in_addr)
#define DEVICEDIR "/dev/"
#define BCAST_HW_ADDR "ff:ff:ff:ff:ff:ff"
#define MAX_ERR 2048
#define INVALID_PPA (MAX_ERR + 1)
#define INVALID_STRING (MAX_ERR + 2)
#define DLOKACK_SHORT_RESPONSE (MAX_ERR + 3)
#define DLOKACK_NOT_M_PCPROTO (MAX_ERR + 4)
#define ERR_MORECTL (MAX_ERR + 5)
#define ERR_MOREDATA (MAX_ERR + 6)
#define ERR_MORECTLDATA (MAX_ERR + 7)
#define DL_PRIMITIVE_ERROR (MAX_ERR + 8)
#define SHORT_CONTROL_PORTION (MAX_ERR + 9)
#define INVALID_ADDR (MAX_ERR + 10)
#define MN_ENTRY_ABSENT (MAX_ERR + 11)
#define NO_MAPPING (MAX_ERR + 12)
/* Tunnel related definitions */
#define MAX_TUNNEL_SUPPORTED 256
#define NO_FREE_TUNNEL (MAX_ERR + 1)
#define TUNNEL_NOT_FOUND (MAX_ERR + 2)
#define DEV_NAME_NOT_FOUND ENXIO
#define INVALID_IP_ADDR ENXIO
#define MN_ENTRY_ABSENT (MAX_ERR + 11)
#define NO_MAPPING (MAX_ERR + 12)
/* Table for mapping tunnelno with mobile-node address */
/*
* WORK -- this will be removed once the tunneling interface returns us
* a unique tunnel id.
*/
static struct {
rwlock_t tunlLock;
ipaddr_t mnaddr;
ipaddr_t tunnelsrc; /* only used on FA side */
uint32_t refcnt;
} mnaddr_tunl[MAX_TUNNEL_SUPPORTED + 1];
#define MAXWAIT 15
/* Maximum address buffer length */
#define MAXDLADDR 1024
/* Handy macro. */
#define OFFADDR(s, n) (unsigned char *)((char *)(s) + (int)(n))
/* Internal Prototypes */
static int settaddr(int tnum, ipaddr_t ifaddr1, ipaddr_t ifaddr2,
ipaddr_t saddr, ipaddr_t daddr, ipsec_req_t *);
static int garp(char *, uint32_t, unsigned char *);
static int plumb_one_tun(int tnum);
static int unplumb_one_tun(int muxfd);
static int setifflags(int tnum, int value);
static int strgetmsg(int fd, struct strbuf *ctlp, struct strbuf *datap,
int *flagsp);
static int expecting(int prim, union DL_primitives *dlp);
static int arpgetHWaddr(ipaddr_t, unsigned char *);
int gettunnelno(ipaddr_t, ipaddr_t);
int arpIfadd(ipaddr_t, char *, uint32_t);
int arpIfdel(ipaddr_t, char *, uint32_t);
/* External Prototypes */
extern MipTunlEntry *CreateTunlEntry(int tnum, ipaddr_t target, ipaddr_t tsrc,
int muxfd);
extern char *ntoa(uint32_t addr_long, char *addr_string);
extern MobilityAgentEntry *findMaeFromIp(ipaddr_t, int);
/*
* Function: InitTunnelModule
*
* Arguments: none
*
* Description: Initialize our globals. Currently, initalize the global
* tunnel lock.
*
* Returns: int (zero on success)
*/
static int
InitTunnelModule()
{
int result;
result = rwlock_init(&gbl_tunnelLock, USYNC_THREAD, NULL);
return (result);
} /* InitTunnelModule */
/*
* Function: newtunnel
*
* Arguments: ipaddr_t addr
* ipaddr_t tsrc_addr
* int *tunnels_scanned
*
* Description: This function returns the available tunnel number in the
* range 0 through MAX_TUNNEL_SUPPORTED. If none are available
* it returns NO_FREE_TUNNEL. tunnels_scanned is used by the caller
* to figure out if all the tunnel numbers are scanned before
* concluding that there's no free tunnel left.
*
* Returns: int (tunnel number, or -1 on error)
*/
static int
newtunnel(ipaddr_t addr, ipaddr_t tsrc_addr, int *tunnels_scanned)
{
int i;
boolean_t found = _B_FALSE;
int tun_num;
/* WARNING: check this xxx WORK */
(void) rw_wrlock(&gbl_tunnelLock);
*tunnels_scanned = 0;
for (i = 0; i < MAX_TUNNEL_SUPPORTED; i++) {
/* start scanning tunnel numbers where we were left last time */
tun_num = (i + first_tun_to_check) % MAX_TUNNEL_SUPPORTED;
/* is this an available tunnel? */
if (mnaddr_tunl[tun_num].mnaddr == 0) {
mnaddr_tunl[tun_num].mnaddr = addr;
mnaddr_tunl[tun_num].tunnelsrc = tsrc_addr;
found = _B_TRUE;
break;
}
}
/*
* Send back the number of tunnels scanned, so that caller can
* give up after scanning MAX_TUNNEL_SUPPORTED many tunnels.
* Also save where we were left, so that next time we start scanning
* from that point on.
*/
if (found) {
*tunnels_scanned = i + 1;
first_tun_to_check = (tun_num + 1) % MAX_TUNNEL_SUPPORTED;
} else {
*tunnels_scanned = MAX_TUNNEL_SUPPORTED;
/*
* First_tun_to_check stays same. We did a one full round of
* scanning tunnel numbers and couldn't find any available.
* Next time we come here to find another available, we'll
* start from the same tunnel number.
*/
}
(void) rw_unlock(&gbl_tunnelLock);
if (found) {
return (tun_num);
} else {
return (-1);
}
} /* newtunnel */
/*
* Function: freetunnel
*
* Arguments: int tnum
*
* Description: This function will free the current tunnel resource. This
* function will disappear with the new kernel tunnel interface.
*
* Returns: void
*/
static void
freetunnel(int tnum)
{
(void) rw_wrlock(&gbl_tunnelLock);
assert(mnaddr_tunl[tnum].refcnt == 0);
mnaddr_tunl[tnum].mnaddr = 0;
mipverbose(("Freeing tunnel module number %d\n", tnum));
(void) rw_unlock(&gbl_tunnelLock);
} /* freetunnel */
/*
* Gets the tunnel number corresponding to given
* mobile node address .
* If the corresponding tunnel number exist then it return s the tunnel no.
* else return s -(TUNNEL_NOT_FOUND)
*/
/*
* Function: gettunnelno
*
* Arguments: ipaddr_t mnaddr
* ipaddr_t tsrc_addr
*
* Description: Returns the tunnel number corresponding to a given mobile
* node address. If the corresponding tunnel number exists, then
* it returns the tunnel number. Otherwise it returns (-1)
* When called from HA mnaddr=mnaddr.
* when called from FA mnaddr=haaddr
* Tunnel source end-point addr=tsrc_addr
*
* Returns: int (Tunnel Number, -1 on failure)
*/
int
gettunnelno(ipaddr_t mnaddr, ipaddr_t tsrc_addr)
{
int i;
int found = _B_FALSE;
(void) rw_rdlock(&gbl_tunnelLock);
for (i = 0; i < MAX_TUNNEL_SUPPORTED; i++) {
if (mnaddr_tunl[i].mnaddr == mnaddr &&
mnaddr_tunl[i].tunnelsrc == tsrc_addr) {
found = _B_TRUE;
break;
}
}
(void) rw_unlock(&gbl_tunnelLock);
if (found == _B_TRUE) {
return (i);
} else {
return (-1);
}
} /* gettunnelno */
#if 0
/*
* Function: printtunnels
*
* Arguments: none
*
* Description: This function prints out all the tunnels. It is used for
* debugging.
*
* Returns: void
*/
void
printtunnels()
{
int i;
char mnaddr[INET_ADDRSTRLEN];
(void) rw_rdlock(&gbl_tunnelLock);
for (i = 0; i < MAX_TUNNEL_SUPPORTED; i++) {
if (mnaddr_tunl[i].mnaddr != 0) {
mipverbose(("Tunnel %d is for %s with refcnt %d\n",
i, ntoa(mnaddr_tunl[i].mnaddr, mnaddr),
mnaddr_tunl[i].refcnt));
}
}
(void) rw_unlock(&gbl_tunnelLock);
} /* printtunnels */
#endif
/* Convert a negative error value into a human readable error message */
/*
* Function: err2str
*
* Arguments: int errval
*
* Description: This function tries to return an error message based on
* an error code, or errno. It first checks for special errors,
* then checks the strerror string.
* WARNING: If the error is not found, then it returns a
* pointer to a static string. This function is NOT thread safe.
*
* Returns: char * (error string)
*/
char *
err2str(int errval)
{
int err = (-1 * errval);
static char tmp[30];
switch (err) {
case INVALID_PPA:
return ("Invalid PPA");
case INVALID_STRING:
return ("Invalid input string");
case DLOKACK_SHORT_RESPONSE:
return ("Short DLOKACK response");
case DLOKACK_NOT_M_PCPROTO:
return ("DLOKACK is not M_PCPROTO");
case ERR_MORECTL:
return ("MORECTL");
case ERR_MOREDATA:
return ("MOREDATA");
case ERR_MORECTLDATA:
return ("MORECTL or MOREDATA");
case SHORT_CONTROL_PORTION:
return ("Control portion is short");
case DL_PRIMITIVE_ERROR:
return ("DL primitive error");
case INVALID_ADDR:
return ("Invalid Address");
case MN_ENTRY_ABSENT:
return ("Missing Mobile node entry");
case NO_MAPPING:
return ("Bad ARP mapping for mobile node");
default:
{
/* Check for errno error */
char *reason;
reason = strerror(err);
if (reason != NULL) {
return (reason);
} else {
(void) sprintf(tmp, "Reason unclear : %d",
err);
return (tmp);
}
}
} /* switch */
} /* err2str */
/*
* Function: ifname2devppa
*
* Arguments: char *ifname, char *devname, int *ppa
*
* Description: Parse the interface name(e.g. "le0" or "hme1") and place the
* corresponding device name(e.g. "/dev/le", "/dev/hme") in
* devname and the ppa(e.g. 0 or 1 for the examples above) in ppa.
* It expects that the caller will pass adequate buffer in
* 'devname' such that it can hold the full devicename.
*
* Returns: void
*/
void
ifname2devppa(char *ifname, char *devname, int *ppa)
{
char *p;
int i, j, val;
val = 0;
j = strlen(DEVICEDIR);
(void) strncpy(devname, DEVICEDIR, j);
for (i = 0, p = ifname; i < strlen(ifname); i++, p++) {
if (*p >= '0' && *p <= '9')
val = 10*val + (*p - '0');
else
devname[j++] = *p;
}
devname[j] = '\0';
*ppa = val;
} /* ifname2devppa */
/*
* Function: mkpt2pt
*
* Arguments: char *ifname, ipaddr_t srcaddr, ipaddr_t dstaddr
*
* Description: Configure the point-to-point interface named ifname with the
* given address. The source address being 'srcaddr' and
* destination address being 'dstaddr'
*
* Returns: int
*/
static int
mkpt2pt(char *ifname, ipaddr_t srcaddr, ipaddr_t dstaddr)
{
struct lifreq lifr;
struct sockaddr_in *sin;
/* set the interface address */
(void) memset((char *)&lifr, 0, sizeof (lifr));
(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
sin = (struct sockaddr_in *)&lifr.lifr_addr;
(void) memset(sin, 0, sizeof (*sin));
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = srcaddr;
if (ioctl(ioctl_sockid, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
syslog(LOG_ERR, "SIOCSLIFADDR failed");
return (-1);
}
/* set the remote/peer address */
(void) memset((char *)&lifr, 0, sizeof (lifr));
(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
sin = (struct sockaddr_in *)&lifr.lifr_addr;
(void) memset(sin, 0, sizeof (*sin));
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = dstaddr;
if (dstaddr && (ioctl(ioctl_sockid, SIOCSLIFDSTADDR,
(caddr_t)&lifr) < 0)) {
syslog(LOG_ERR, "SIOCSIFDSTADDR failed");
return (-1);
}
return (0);
} /* mkpt2pt */
/*
* Function: InitNet
*
* Arguments: none
*
* Description: Network-related initialization, e.g. loading tunneling module
* etc. Information about each mobility supporting interface is
* available in maAdvConfigHash
*
* Returns: int (zero on success)
*/
int
InitNet()
{
/*
* Enable forwarding, disable ICMP redirects in kernel, e.g. FA
* should not redirect visiting mobile nodes to another router).
*/
(void) system(
"/usr/sbin/ndd -set /dev/ip "
"ip_forwarding 1 > /dev/null 2>&1\n");
(void) system(
"/usr/sbin/ndd -set /dev/ip "
"ip_send_redirects 0 > /dev/null 2>&1\n");
mipverbose(("IP forwarding on, ICMP redirects off.\n"));
/* Initialize the tunnel management module */
if (InitTunnelModule() < 0) {
syslog(LOG_ERR, "InitTunnelModule failed.");
return (-1);
}
/* Make a socket for the various SIOCxxxx ioctl commands */
if ((ioctl_sockid = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
syslog(LOG_ERR,
"Error: could not create socket for SIOCxxxx commands.");
return (-1);
}
/* Open a routing socket for passing route commands */
if ((rtsock = socket(AF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
syslog(LOG_ERR,
"Error: could not create socket for Route commands.");
return (-1);
}
return (0);
} /* InitNet */
/*
* Function: encapadd
*
* Arguments: ipaddr_t target, ipaddr_t tsrc, uint32_t tdst, uint8_t tflags
*
* Description: Enables encapsulation of pkts meant for target addr to the
* tunnel's destination addr (tdst) with the source address
* being that of the specified agent (tsrc). To configure an
* IP-in-IP tunnel, first get an unused tunnel number and
* then plumb it. Next, invoke any ipsec policies to ensure outbound
* tunnel packets to, and incomming tunnel packets from the agent-peer are
* protected (or we, or they will discard)! We do NOT set the peer-flag
* here as it's unnecessary, and should have been done anyway when we got
* the registration request (peer status isn't, after all, just about
* having a security association). Next set up a point-to-point
* interface between the target addr (target) - this is
* usually the mobile node address and address of the specified
* agent (tsrc) - this is usually the home agent address;
* the routine also sets up the tunnel with the source address
* of the tunnel being the specified agent address (tsrc)
* and the destination address of the tunnel being the
* address (tdst) - this usually is the foreign agent address.
* Next a number of interface specific flags are set and
* the tunnel is enabled. The tunnel specific entry data is
* next added to the hash table, keying on target address.
*
* If a tunnel entry already exists for the target addr
* increase the entry reference count.
*
* For now, tflags only identify if we're to protect a reverse tunnel,
* but in the future it can be used to identify when a tunnel other than
* an IP in IP tunnel should be set up.
*
* Returns: int
*/
int
encapadd(ipaddr_t target, ipaddr_t tsrc, uint32_t tdst, uint8_t tflags)
{
int tnum, muxfd;
MipTunlEntry *entry;
int tunnels_scanned; /* no. of tunnels scanned in a */
/* single newtunnel() call. */
int total_tunnels_scanned; /* total no. of tunnels scanned */
/* in this encapadd() call. */
MobilityAgentEntry *mae; /* for IPsec Tunnel SA */
ipsec_req_t *ipsr_p = NULL; /* for ipsec tunnel policy */
/*
* We don't need a MipTunlEntryLookup here to match
* with destination endpoint address as we know that
* the target(mnaddr) is unique per HA
*/
if ((entry = (MipTunlEntry *)findHashTableEntryUint(
&mipTunlHash, target, LOCK_WRITE, NULL, 0, 0,
0)) != NULL) {
entry->refcnt++;
(void) rw_unlock(&entry->TunlNodeLock);
return (0);
}
total_tunnels_scanned = 0;
muxfd = -1;
while ((total_tunnels_scanned < MAX_TUNNEL_SUPPORTED) &&
(muxfd == -1)) {
if ((tnum = newtunnel(target, tsrc, &tunnels_scanned)) < 0) {
syslog(LOG_ERR, "encapadd: couldnot find free tnum");
return (-1);
}
total_tunnels_scanned += tunnels_scanned;
if ((muxfd = plumb_one_tun(tnum)) == -1)
freetunnel(tnum);
}
if (muxfd == -1) {
syslog(LOG_ERR, "encapadd: couldnot find free tnum");
return (-1);
}
/*
* Before we can call settaddr, we need to see if we should pass down
* any IPSEC SAs so treqs can be set.
*/
if ((mae = findMaeFromIp(tdst, LOCK_READ)) != NULL) {
/* for encapadd, we're an HA using "ipSecTunnel apply ..." */
if (IPSEC_TUNNEL_ANY(mae->maIPsecSAFlags[IPSEC_APPLY])) {
/* pass down what we've parsed */
ipsr_p = &(mae->maIPsecTunnelIPSR[IPSEC_APPLY]);
mae->maIPsecFlags |= IPSEC_TUNNEL_APPLY;
/* Symetric tunnels: should we set the reverse bit? */
if (tflags & REG_REVERSE_TUNNEL)
mae->maIPsecFlags |=
IPSEC_REVERSE_TUNNEL_PERMIT;
}
/* unlock */
(void) rw_unlock(&mae->maNodeLock);
}
if (settaddr(tnum, tsrc, target, tsrc, tdst, ipsr_p) == -1) {
syslog(LOG_ERR, "encapadd: settaddr failed");
(void) unplumb_one_tun(muxfd);
mipverbose(("unplumb tunnel with number %d\n", tnum));
freetunnel(tnum);
return (-1);
}
if (setifflags(tnum, IFF_UP | IFF_NORTEXCH | IFF_MIPRUNNING |
IFF_PRIVATE) == -1) {
syslog(LOG_ERR, "encapadd: setifflags failed");
mipverbose(("unplumb tunnel with number %d\n", tnum));
(void) unplumb_one_tun(muxfd);
freetunnel(tnum);
return (-1);
}
if ((entry = CreateTunlEntry(tnum, target, tsrc, muxfd)) == NULL) {
syslog(LOG_ERR, "encapadd: CreateTunlEntry failed");
mipverbose(("unplumb tunnel with number %d\n", tnum));
(void) unplumb_one_tun(muxfd);
freetunnel(tnum);
return (-1);
}
entry->refcnt++;
(void) rw_unlock(&entry->TunlNodeLock);
return (0);
} /* encapadd */
/*
* Function: encaprem
*
* Arguments: ipaddr_t target
*
* Description: Terminates encapulation service for target addr.
* First find the tunnel entry in the hash table.
* If this is the last reference count to the tunnel entry
* then unplumb the tunnel and break the tunnel association
* between the foreign agent and home agent for
* encapsulation of packets destined for the target address.
* Next free the tunnel and the tunnel entry from hash table.
*
* Returns: int -1 on failure, the tunnel reference count on success.
*/
int
encaprem(ipaddr_t target)
{
int tnum, muxfd;
MipTunlEntry *entry;
/*
* NOTE: We do not need to call MipTunlEntryLookup here
* because we assume MN home address(target) is unique per HA.
*/
if ((entry = (MipTunlEntry *)findHashTableEntryUint(
&mipTunlHash, target, LOCK_WRITE, NULL, 0, 0, 0)) == NULL) {
syslog(LOG_ERR, "encaprem: Target entry %x missing", target);
return (-1);
}
tnum = entry->tunnelno;
muxfd = entry->mux_fd;
if (entry->refcnt == 1) {
(void) setifflags(entry->tunnelno,
-IFF_UP | -IFF_NORTEXCH | -IFF_MIPRUNNING);
mipverbose(("unplumb tunnel with number %d\n", tnum));
if (unplumb_one_tun(muxfd) == -1) {
(void) rw_unlock(&entry->TunlNodeLock);
syslog(LOG_ERR,
"encaprem: unplumb of tunnel %d failed", tnum);
return (-1);
}
}
/* if refcnt is 0, this encapsulation point just went away */
if (--entry->refcnt == 0) {
/* WORK:Todo: This should call delHashTableEntry */
int index = HASHIT(target);
HashTable *htbl = &mipTunlHash;
HashEntry *p, *q;
freetunnel(tnum);
(void) rw_wrlock(&htbl->bucketLock[index]);
p = htbl->buckets[index];
while (p) {
if (p->key == target)
break;
q = p;
p = p->next;
}
if (p == htbl->buckets[index])
htbl->buckets[index] = p->next;
else
q->next = p->next;
(void) rw_unlock(&entry->TunlNodeLock);
(void) rwlock_destroy(&entry->TunlNodeLock);
free(entry);
free(p);
(void) rw_wrlock(&htbl->hashLock);
htbl->size--;
(void) rw_unlock(&htbl->hashLock);
(void) rw_unlock(&htbl->bucketLock[index]);
} else {
/* Release the lock held in findHashTableEntryUint */
(void) rw_unlock(&entry->TunlNodeLock);
}
return (entry->refcnt);
} /* encaprem */
/*
* Function: decapadd
*
* Arguments: ipaddr_t ipipsrc, ipaddr_t ipipdst
*
* Description: Enable decapsulation service for target addr. To configure
* an IP-in-IP tunnel, first get an unused tunnel number and
* then plumb it. Next set up a point-to-point interface
* between the ipipdst addr - this is usually the foreign agent
* address and a dummy address ("0.0.0.0"); the routine
* also sets up the tunnel with the source address
* of the tunnel being ipipdst - usually foreign agent address
* and the destination address of the tunnel being the address
* ipipsrc - this usually is the home agent address. Next a
* number of interface specific flags are set and the tunnel is
* enabled. The tunnel specific entry data is next added to
* the hash table, keying on ipipsrc address.
*
* If a tunnel entry already exists for the ipipsrc addr
* increase the entry reference count.
* This function is called at the foreign agent end of tunnel.
*
* Returns: int (zero on success)
*/
int
decapadd(ipaddr_t ipipsrc, ipaddr_t ipipdst)
{
int tnum, muxfd;
MipTunlEntry *entry;
int tunnels_scanned; /* no. of tunnels scanned in a */
/* single newtunnel() call. */
int total_tunnels_scanned; /* total no. of tunnels scanned */
/* in this decapadd() call. */
MobilityAgentEntry *mae; /* to check for IPsec policy */
ipsec_req_t *ipsr_p = NULL; /* for ipsec tunnel policy */
if ((entry = (MipTunlEntry *)findHashTableEntryUint(
&mipTunlHash, ipipsrc, LOCK_WRITE, MipTunlEntryLookup,
(uint32_t)ipipdst, 0, 0)) != NULL) {
entry->refcnt++;
(void) rw_unlock(&entry->TunlNodeLock);
return (0);
}
total_tunnels_scanned = 0;
muxfd = -1;
while ((total_tunnels_scanned < MAX_TUNNEL_SUPPORTED) &&
(muxfd == -1)) {
if ((tnum = newtunnel(ipipsrc, ipipdst,
&tunnels_scanned)) < 0) {
syslog(LOG_ERR, "decapadd: couldnot find free tnum");
return (-1);
}
total_tunnels_scanned += tunnels_scanned;
if ((muxfd = plumb_one_tun(tnum)) == -1)
freetunnel(tnum);
}
if (muxfd == -1) {
syslog(LOG_ERR, "decapadd: couldnot find free tnum");
return (-1);
}
/*
* Before tunnel is plumbed, and the interface is created, see if we
* have an IPsecPolicy. If so, point at it for settaddr().
*/
if ((mae = findMaeFromIp(ipipsrc, LOCK_READ)) != NULL) {
/*
* for decapadd, we're an FA using "IPsecTunnel permit ..."
* Note that we set the IPSEC_REVERSE_TUNNEL_PERMIT flag when
* processing for a reverse-tunnel request.
* Note that we don't check to see if the IPSEC_TUNNEL_PERMIT
* flag is set because we always want to make sure the tunnel's
* protected correctly.
*/
if (IPSEC_TUNNEL_ANY(mae->maIPsecSAFlags[IPSEC_PERMIT])) {
/* pass down what we've parsed */
ipsr_p = &(mae->maIPsecTunnelIPSR[IPSEC_PERMIT]);
/* set the invoked bit in case we have to restore */
mae->maIPsecFlags |= IPSEC_TUNNEL_PERMIT;
}
/* unlock */
(void) rw_unlock(&mae->maNodeLock);
}
/*
* Tunnels in Solaris are bi-directional, with the obvious caveat that
* the dst address must be set. For security reasons, we only do this
* if the MN is requesting a reverse tunnel. If so, ipipsrc should be
* the MN's home agent address. ipsr contains our ipsec values.
* From FA end the parameters are : tsrc=COA, tdst=HAA, dstaddr=0.0.0.0
* srcaddr=COA
*/
if (settaddr(tnum, ipipdst, inet_addr("0.0.0.0"), ipipdst,
ipipsrc, ipsr_p) == -1) {
syslog(LOG_ERR, "decapadd: settaddr failed");
mipverbose(("unplumb tunnel with number %d\n", tnum));
(void) unplumb_one_tun(muxfd);
freetunnel(tnum);
return (-1);
}
if (setifflags(tnum, IFF_UP | IFF_NORTEXCH | IFF_MIPRUNNING) == -1) {
syslog(LOG_ERR, "decapadd: setifflags failed");
mipverbose(("unplumb tunnel with number %d\n", tnum));
(void) unplumb_one_tun(muxfd);
freetunnel(tnum);
return (-1);
}
/* Entry will be locked after CreateTunlEntry */
if ((entry = CreateTunlEntry(tnum, ipipsrc, ipipdst, muxfd)) == NULL) {
syslog(LOG_ERR, "decapadd: CreateTunlEntry failed");
mipverbose(("unplumb tunnel with number %d\n", tnum));
(void) unplumb_one_tun(muxfd);
freetunnel(tnum);
return (-1);
}
entry->refcnt++;
(void) rw_unlock(&entry->TunlNodeLock);
return (0);
} /* decapadd */
/*
* Function: decaprem
*
* Arguments: ipaddr_t target : Tunnel outer destination IP address at FA
* ipaddr_t tsrc : Tunnel outer source IP address at FA
*
* Description: Terminates decapulation service for target address.
* First find the tunnel entry in the hash table.
* If this is the last reference count to the tunnel entry
* then unplumb the tunnel and break the tunnel association
* between the foreign agent and home agent for
* decapsulation of packets. Next free the tunnel and the
* tunnel entry from hash table.
*
* Returns: int (zero on success)
*/
int
decaprem(ipaddr_t target, ipaddr_t tsrc)
{
int tnum, muxfd;
MipTunlEntry *entry;
if ((entry = (MipTunlEntry *)findHashTableEntryUint(
&mipTunlHash, target, LOCK_WRITE, MipTunlEntryLookup,
(uint32_t)tsrc, 0, 0)) == NULL) {
syslog(LOG_ERR, "encaprem: Target entry %x missing", target);
return (-1);
}
tnum = entry->tunnelno;
muxfd = entry->mux_fd;
if (entry->refcnt == 1) {
(void) setifflags(entry->tunnelno,
-IFF_UP | -IFF_NORTEXCH | -IFF_MIPRUNNING);
mipverbose(("unplumb tunnel with number %d\n", tnum));
if (unplumb_one_tun(muxfd) == -1) {
(void) rw_unlock(&entry->TunlNodeLock);
syslog(LOG_ERR,
"decaprem: unplumb of tunnel %d failed", tnum);
return (-1);
}
}
if (--entry->refcnt == 0) {
int index = HASHIT(target);
HashTable *htbl = &mipTunlHash;
HashEntry *p, *q;
MipTunlEntry *Tunentry;
freetunnel(tnum);
(void) rw_wrlock(&htbl->bucketLock[index]);
p = htbl->buckets[index];
while (p) {
Tunentry = (MipTunlEntry *)p->data;
if (p->key == target && Tunentry->tunnelsrc == tsrc)
break;
q = p;
p = p->next;
}
if (p == htbl->buckets[index])
htbl->buckets[index] = p->next;
else
q->next = p->next;
(void) rw_unlock(&entry->TunlNodeLock);
(void) rwlock_destroy(&entry->TunlNodeLock);
free(entry);
free(p);
(void) rw_wrlock(&htbl->hashLock);
htbl->size--;
(void) rw_unlock(&htbl->hashLock);
(void) rw_unlock(&htbl->bucketLock[index]);
} else {
/*
* Release the lock. Other mobile node(s) may be
* using this tunnel.
*/
(void) rw_unlock(&entry->TunlNodeLock);
}
return (entry->refcnt);
}
/*
* Function: MipTunlEntryLookup
* Arguments: entry - Pointer to MipTunl entry
* p1- First parameter to match (Tunnel src-endpoint IPaddr)
* p2- Second Parameter to match (unused)
* p3- Second Parameter to match (unused)
* Description:
* This function is used to lookup a tunnel entry which is hashed
* by the tunnel destination endpoint address and matched with
* it's source end point address. This matching is necessary
* to support multihomed foreign agent with more than one COAs
*/
/* ARGSUSED */
boolean_t
MipTunlEntryLookup(void *entry, uint32_t p1, uint32_t p2, uint32_t p3)
{
MipTunlEntry *Tunentry = entry;
if (Tunentry->tunnelsrc == (ipaddr_t)p1)
return (_B_TRUE);
return (_B_FALSE);
}
/*
* Function: arpadd
*
* Arguments: ipaddr_t host, unsigned char *eaddr, char *flag
*
* Description: Adds an arp entry with ip address set to host and hardware
* address set to eaddr with approriate flags specified by flag
* argument, e.g. arpadd(inet_addr("129.146.122.121"),
* "08:20:AB:FE:33:11", "pub") creates a proxy ARP entry for
* 129.146.122.121.
*
* Returns: int
*/
int
arpadd(ipaddr_t host, unsigned char *eaddr, unsigned int flags)
{
struct arpreq ar;
struct sockaddr_in *sin;
bzero((caddr_t)&ar, sizeof (ar));
/* LINTED E_BAD_PTR_CAST_ALIGN */
sin = (struct sockaddr_in *)&ar.arp_pa;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = (host);
(void) memcpy(ar.arp_ha.sa_data, eaddr, ETH_ALEN);
ar.arp_flags = ATF_PERM | flags;
#if 0
if (strncmp(flag, "temp", 4) == 0)
ar.arp_flags &= ~ATF_PERM;
if (strncmp(flag, "pub", 3) == 0)
ar.arp_flags |= ATF_PUBL;
if (strncmp(flag, "trail", 5) == 0)
ar.arp_flags |= ATF_USETRAILERS;
#endif
if (ioctl(ioctl_sockid, SIOCSARP, (caddr_t)&ar) < 0) {
if (errno != EEXIST)
return ((-1) * errno);
}
return (0);
} /* arpadd */
/*
* Function: arpdel
*
* Arguments: ipaddr_t host
*
* Description: Deletes an arp entry for ip address set to host
*
* Returns: int (zero on success)
*/
int
arpdel(ipaddr_t host)
{
struct arpreq ar;
struct sockaddr_in *sin;
bzero((caddr_t)&ar, sizeof (ar));
ar.arp_pa.sa_family = AF_INET;
/* LINTED E_BAD_PTR_CAST_ALIGN */
sin = (struct sockaddr_in *)&ar.arp_pa;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = (host);
if (ioctl(ioctl_sockid, SIOCDARP, (caddr_t)&ar) < 0) {
if (errno != ENXIO)
return ((-1) * errno);
}
return (0);
} /* arpdel */
/*
* Function: arpgetHWaddr
*
* Arguments: ipaddr_t mnaddr, unsigned char *mnetheraddr
*
* Description: Get the hardware address corresponding to mnaddr from
* the arp table.
*
* Returns: int (zero on success)
*/
static int
arpgetHWaddr(ipaddr_t mnaddr, unsigned char *mnetheraddr)
{
struct arpreq ar;
struct sockaddr_in *sin;
bzero((caddr_t)&ar, sizeof (ar));
ar.arp_pa.sa_family = AF_INET;
/* LINTED E_BAD_PTR_CAST_ALIGN */
sin = (struct sockaddr_in *)&ar.arp_pa;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = mnaddr;
if (ioctl(ioctl_sockid, SIOCGARP, (caddr_t)&ar) < 0)
return (-1 * (errno));
else {
(void) memcpy(mnetheraddr, ar.arp_ha.sa_data, ETH_ALEN);
return (0);
}
} /* arpgetHWaddr */
/*
* Function: arprefresh
*
* Arguments: HaMobileNodeEntry *hentry, ipaddr_t mnaddr
*
* Description: Sends gratuitous arp for the mnaddr using the hardware address
* found for mnaddr in its own ARP cache (making sure that the
* hardware address is not HA's own). The home agent calls
* arprefresh when a mobile node returns home and successfully
* deregisters itself.
*
* Returns: int (zero on success)
*/
int
arprefresh(HaMobileNodeEntry *hentry, ipaddr_t mnaddr)
{
int ret;
ipaddr_t ifaceaddr;
char devname[LIFNAMSIZ + 1];
unsigned char hainterfaceaddr[ETH_ALEN + 1];
unsigned char mnetheraddr[ETH_ALEN + 1];
unsigned char zero_addr[ETH_ALEN + 1] = {0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
MaAdvConfigEntry *mentry;
mipverbose(("Arp refresh called for %d.%d.%d.%d\n",
(unsigned char) ((ntohl(mnaddr) >> 24) & 0xff),
(unsigned char) ((ntohl(mnaddr) >> 16) & 0xff),
(unsigned char) ((ntohl(mnaddr) >> 8) & 0xff),
(unsigned char) (ntohl(mnaddr) & 0xff)));
ifaceaddr = hentry->haBindingIfaceAddr;
/* Get the matching maIfaceAddr from mnAdvConfigTable */
if ((mentry = (MaAdvConfigEntry *)findHashTableEntryUint(
&maAdvConfigHash, ifaceaddr, LOCK_NONE, NULL, 0, 0, 0)) == NULL) {
syslog(LOG_ERR, "Unable to find interface in hash table");
return (-1 * MN_ENTRY_ABSENT); /* Unlikely to occur */
}
(void) strcpy(devname, mentry->maIfaceName);
(void) memcpy(hainterfaceaddr, mentry->maIfaceHWaddr, ETH_ALEN);
if ((ret = arpgetHWaddr(mnaddr, mnetheraddr)) < 0)
return (ret);
if ((memcmp(hainterfaceaddr, mnetheraddr, ETH_ALEN) == 0) ||
(memcmp(zero_addr, mnetheraddr, ETH_ALEN) == 0))
return (-1 * NO_MAPPING);
else
return (garp(devname, mnaddr, mnetheraddr));
} /* arprefresh */
/*
* Function: routemodify
*
* Arguments: ipaddr_t dst, ipaddr_t gw,
* ipaddr_t insrc, int in_if, int out_if,
* unsigned int cmd
*
* Description: Add or Delete route depending on 'cmd' argument.
* 'cmd' argument can be either ADDRT or DELRT.
* For adding/deleting route from registration
* process and reply functions, only dst,gw args
* are required. Thus that is defined as simple route.
* NOTE: simple route is not used by mipagent registration
* reply process as it uses IP_XMIT_IF socket option
* instead of simple route. Simple route can not distinguish
* between two different mobile nodes with same private
* addresses. But the code still contains simple route
* section, in case it's needed in future for any purpose.
* After the visitor is accepted at FA, the
* forward route created from FA to MN to relay the
* packet from tunnel from home agent is defined
* as 'ftun_route'. This route must have in_if and out_if
* index arguments. For reverse tunnel route, this
* function expects a valid in_if and a valid out_if
* value and a non-zero source address(insrc). For ftun_route
* insrc must be zero.
*
* To set up forward route to reach MN:
* dst= MN's home addr, gw= COA, in_if=tun's if_index
* out_if = FA's interface index on which MN is attached.
* insrc = 0.0.0.0
*
* To set up reverse tunnel route:
* dst = 0.0.0.0 gw = 0.0.0.0 in_if = FA's interface index on
* which MN is attached, out_if = tunnel's interface index.
* insrc = MN's homeaddr
*
* Returns: int (zero on success)
*/
int
routemodify(ipaddr_t dst, ipaddr_t gw, ipaddr_t insrc, int in_if,
int out_if, unsigned int cmd)
{
struct rt_msghdr *rt_msg;
struct sockaddr_in *dstaddr;
struct sockaddr_in *gwaddr;
struct sockaddr_in *insrcaddr;
struct sockaddr_dl *rta_ifp;
struct sockaddr_dl *rta_srcifp;
char *cp;
static int rtmseq;
int rlen;
int flags = RTF_STATIC | RTF_UP;
boolean_t rtun_route = _B_FALSE; /* when insrc!=0, in_if, out_if !=0 */
boolean_t ftun_route = _B_FALSE; /* when insrc==0, in_if!=0 */
if (insrc == INADDR_ANY && in_if == 0 && out_if == 0) {
/* Simple route case dst<->gw: not used by mipagent */
flags = RTF_HOST | RTF_PRIVATE;
rlen = MIP_RTM_MSGLEN;
} else if (insrc == INADDR_ANY && in_if != 0 && out_if != 0) {
/* Forward route to MN from tunnel */
ftun_route = _B_TRUE;
flags = RTF_HOST | RTF_PRIVATE;
rlen = MIP_RTM_MSGLEN + 2 * (sizeof (struct sockaddr_dl));
} else if (insrc != INADDR_ANY && in_if != 0 && out_if != 0) {
/* Reverse tunnel route: insrc != 0 */
rtun_route = _B_TRUE;
flags = RTF_GATEWAY | RTF_PRIVATE;
rlen = MIP_RTUN_RTM_MSGLEN;
} else {
/* Invalid Call */
return (-1 * EINVAL);
}
rt_msg = (struct rt_msghdr *)malloc(rlen);
if (rt_msg == NULL) {
syslog(LOG_ERR, "route_modify: Cannot allocate memory");
return (-1 * (errno));
}
bzero(rt_msg, rlen);
rt_msg->rtm_msglen = rlen;
rt_msg->rtm_version = RTM_VERSION;
rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
rt_msg->rtm_pid = getpid();
if (cmd == ADDRT)
rt_msg->rtm_type = RTM_ADD;
else
rt_msg->rtm_type = RTM_DELETE;
rt_msg->rtm_seq = ++rtmseq;
rt_msg->rtm_flags = flags;
cp = (char *)rt_msg + sizeof (struct rt_msghdr);
/* DST */
/* LINTED E_BAD_PTR_CAST_ALIGN */
dstaddr = (struct sockaddr_in *)cp;
dstaddr->sin_family = AF_INET;
if (!rtun_route)
dstaddr->sin_addr.s_addr = dst;
else
dstaddr->sin_addr.s_addr = INADDR_ANY;
/* GATEWAY */
cp += sizeof (struct sockaddr_in);
/* LINTED E_BAD_PTR_CAST_ALIGN */
gwaddr = (struct sockaddr_in *)cp;
gwaddr->sin_family = AF_INET;
gwaddr->sin_addr.s_addr = gw;
/* NETMASK */
cp += sizeof (struct sockaddr_in);
/* LINTED E_BAD_PTR_CAST_ALIGN */
dstaddr = (struct sockaddr_in *)cp;
dstaddr->sin_family = AF_INET;
if (!rtun_route) {
dstaddr->sin_addr.s_addr = IP_HOST_MASK;
} else {
dstaddr->sin_addr.s_addr = INADDR_ANY;
}
/* Check if ftun_route or rtun_route is set, else it's simple_route */
if (ftun_route) {
/*
* We need to set both RTA_IFP and RTA_SRCIFP
* in order to support Lucent PPP interfaces to
* mobile nodes. Since there may not be an interface
* route for the dynamically plumbed PPP interfaces
* which are used in 3Gwireless technology to connect
* to the mobile node from the PDSN (Packet Data Service
* Network, IS-835, TIA document), thus the foreign agent
* (PDSN) end of PPP interface may have non-unique address
* (for example, all these special PPP interfaces may have
* same address, COA in the PDSN end). So it is not
* possible to derive interface index from the supplied
* gateway address. Hence the caller of this function
* must provide both outgoing and incoming interface
* index when creating the forward tunnel.
*/
rt_msg->rtm_addrs |= RTA_IFP | RTA_SRCIFP;
/* IFP */
cp += sizeof (struct sockaddr_in);
/* LINTED E_BAD_PTR_CAST_ALIGN */
rta_ifp = (struct sockaddr_dl *)cp;
rta_ifp->sdl_family = AF_LINK;
rta_ifp->sdl_index = out_if;
/* SRCIFP */
cp += sizeof (struct sockaddr_dl);
/* LINTED E_BAD_PTR_CAST_ALIGN */
rta_srcifp = (struct sockaddr_dl *)cp;
rta_srcifp->sdl_family = AF_LINK;
rta_srcifp->sdl_index = in_if;
} else if (rtun_route) {
/* it's a reverse tunnel route */
rt_msg->rtm_addrs |= (RTA_IFP | RTA_SRC | RTA_SRCIFP);
/* IFP */
cp += sizeof (struct sockaddr_in);
/* LINTED E_BAD_PTR_CAST_ALIGN */
rta_ifp = (struct sockaddr_dl *)cp;
rta_ifp->sdl_family = AF_LINK;
rta_ifp->sdl_index = out_if;
/* SRC */
cp += sizeof (struct sockaddr_dl);
/* LINTED E_BAD_PTR_CAST_ALIGN */
insrcaddr = (struct sockaddr_in *)cp;
insrcaddr->sin_family = AF_INET;
insrcaddr->sin_addr.s_addr = insrc;
/* SRCIFP */
cp += sizeof (struct sockaddr_in);
/* LINTED E_BAD_PTR_CAST_ALIGN */
rta_srcifp = (struct sockaddr_dl *)cp;
rta_srcifp->sdl_family = AF_LINK;
rta_srcifp->sdl_index = in_if;
}
/* Send the routing message */
rlen = write(rtsock, rt_msg, rt_msg->rtm_msglen);
if (rlen > 0) {
if (cmd == ADDRT)
mipverbose(("Added route\n"));
else
mipverbose(("Deleted route\n"));
}
/* Free rt_msg now */
free((void *)rt_msg);
if (rlen < 0)
return ((-1) * (errno));
return (0);
} /* routemodify */
/* Routines for refresh_mn_arp() */
/* -------- Start of dlcommon routines */
/*
* Common (shared) DLPI test routines.
* Mostly pretty boring boilerplate sorta stuff.
* These can be split into individual library routines later
* but it's just convenient to keep them in a single file
* while they're being developed.
*
* Not supported:
* Connection Oriented stuff
* QOS stuff
*/
/*
* Function: dlinforeq
*
* Arguments: fd
*
* Description:
*
* Returns: int
*/
static int
dlinforeq(int fd)
{
dl_info_req_t info_req;
struct strbuf ctl;
info_req.dl_primitive = DL_INFO_REQ;
ctl.maxlen = 0;
ctl.len = sizeof (info_req);
ctl.buf = (char *)&info_req;
if (putmsg(fd, &ctl, (struct strbuf *)NULL, RS_HIPRI) < 0) {
return (-1 * errno);
}
return (0);
} /* dlinforeq */
/*
* Function: dlinfoack
*
* Arguments: fd, bufp
*
* Description:
*
* Returns: int
*/
static int
dlinfoack(int fd, char *bufp)
{
union DL_primitives *dlp;
struct strbuf ctl;
int flags;
int ret;
ctl.maxlen = MAXDLBUF;
ctl.len = 0;
ctl.buf = bufp;
(void) strgetmsg(fd, &ctl, (struct strbuf *)NULL, &flags);
/* LINTED E_BAD_PTR_CAST_ALIGN */
dlp = (union DL_primitives *)ctl.buf;
if ((ret = expecting(DL_INFO_ACK, dlp)) < 0) {
(void) close(fd);
return (ret);
}
if (ctl.len < sizeof (dl_info_ack_t)) {
return (-1 * DLOKACK_SHORT_RESPONSE);
}
if (flags != RS_HIPRI) {
return (-1 * DLOKACK_NOT_M_PCPROTO);
}
if (ctl.len < sizeof (dl_info_ack_t)) {
return (-1 * DLOKACK_SHORT_RESPONSE);
}
return (0);
} /* dlinfoack */
/*
* Function: dlattachreq
*
* Arguments: fd, ppa
*
* Description:
*
* Returns: int
*/
int
dlattachreq(int fd, int ppa)
{
dl_attach_req_t attach_req;
struct strbuf ctl;
attach_req.dl_primitive = DL_ATTACH_REQ;
attach_req.dl_ppa = ppa;
ctl.maxlen = 0;
ctl.len = sizeof (attach_req);
ctl.buf = (char *)&attach_req;
if (putmsg(fd, &ctl, (struct strbuf *)NULL, 0) < 0)
return (-1 * errno);
return (0);
} /* dlattachreq */
/*
* Function: dlbindreq
*
* Arguments: fd, sap, max_conind, service_mode, conn_mgmt, xidtest
*
* Description:
*
* Returns: int
*/
int
dlbindreq(int fd, uint32_t sap, uint32_t max_conind, uint32_t service_mode,
uint32_t conn_mgmt, uint32_t xidtest)
{
dl_bind_req_t bind_req;
struct strbuf ctl;
bind_req.dl_primitive = DL_BIND_REQ;
bind_req.dl_sap = sap;
bind_req.dl_max_conind = max_conind;
bind_req.dl_service_mode = service_mode;
bind_req.dl_conn_mgmt = conn_mgmt;
bind_req.dl_xidtest_flg = xidtest;
ctl.maxlen = 0;
ctl.len = sizeof (bind_req);
ctl.buf = (char *)&bind_req;
if (putmsg(fd, &ctl, (struct strbuf *)NULL, 0) < 0)
return (-1 * errno);
return (0);
} /* dlbindreq */
/*
* Function: dlunitdatareq
*
* Arguments: int fd, unsigned char *addrp, int addrlen, ulong_t minpri
* ulong_t maxpri, unsigned char *datap, int datalen)
*
* Description:
*
* Returns: int
*/
static int
dlunitdatareq(int fd, unsigned char *addrp, int addrlen, ulong_t minpri,
ulong_t maxpri, unsigned char *datap, int datalen)
{
long buf[MAXDLBUF];
union DL_primitives *dlp;
struct strbuf data, ctl;
dlp = (union DL_primitives *)buf;
dlp->unitdata_req.dl_primitive = DL_UNITDATA_REQ;
dlp->unitdata_req.dl_dest_addr_length = addrlen;
dlp->unitdata_req.dl_dest_addr_offset = sizeof (dl_unitdata_req_t);
dlp->unitdata_req.dl_priority.dl_min = minpri;
dlp->unitdata_req.dl_priority.dl_max = maxpri;
(void) memcpy(OFFADDR(dlp, sizeof (dl_unitdata_req_t)), addrp,
addrlen);
ctl.maxlen = 0;
ctl.len = sizeof (dl_unitdata_req_t) + addrlen;
ctl.buf = (char *)buf;
data.maxlen = 0;
data.len = datalen;
data.buf = (char *)datap;
if (putmsg(fd, &ctl, &data, 0) < 0)
return (-1 * errno);
return (0);
} /* dlunitdatareq */
/*
* Function: dlokack
*
* Arguments: int fd, char *bufp
*
* Description:
*
* Returns: int
*/
int
dlokack(int fd, char *bufp)
{
union DL_primitives *dlp;
struct strbuf ctl;
int flags;
int ret;
ctl.maxlen = MAXDLBUF;
ctl.len = 0;
ctl.buf = bufp;
(void) strgetmsg(fd, &ctl, (struct strbuf *)NULL, &flags);
/* LINTED E_BAD_PTR_CAST_ALIGN */
dlp = (union DL_primitives *)ctl.buf;
if ((ret = expecting(DL_OK_ACK, dlp)) < 0)
return (ret);
if (ctl.len < sizeof (dl_ok_ack_t))
return (-1 * DLOKACK_SHORT_RESPONSE);
if (flags != RS_HIPRI)
return (-1 * DLOKACK_NOT_M_PCPROTO);
if (ctl.len < sizeof (dl_ok_ack_t))
return (-1 * DLOKACK_SHORT_RESPONSE);
return (0);
} /* dlokack */
/*
* Function: dlbindack
*
* Arguments: int fd, char *bufp
*
* Description:
*
* Returns: int
*/
int
dlbindack(int fd, char *bufp)
{
union DL_primitives *dlp;
struct strbuf ctl;
int flags;
int ret;
ctl.maxlen = MAXDLBUF;
ctl.len = 0;
ctl.buf = bufp;
(void) strgetmsg(fd, &ctl, (struct strbuf *)NULL, &flags);
/* LINTED E_BAD_PTR_CAST_ALIGN */
dlp = (union DL_primitives *)ctl.buf;
if ((ret = expecting(DL_BIND_ACK, dlp)) < 0)
return (ret);
if (flags != RS_HIPRI)
return (-1 * DLOKACK_NOT_M_PCPROTO);
if (ctl.len < sizeof (dl_bind_ack_t))
return (-1 * DLOKACK_SHORT_RESPONSE);
return (0);
} /* dlbindack */
/*
* Function: strgetmsg
*
* Arguments: int fd, struct strbuf *ctlp, struct strbuf *datap, int *flagsp,
* char *caller
*
* Description:
*
* Returns: int
*/
static int
strgetmsg(int fd, struct strbuf *ctlp, struct strbuf *datap, int *flagsp)
{
int rc;
/*
* Set flags argument and issue getmsg().
*/
*flagsp = 0;
if ((rc = getmsg(fd, ctlp, datap, flagsp)) < 0) {
return (-1 * errno);
}
/*
* Check for MOREDATA and/or MORECTL.
*/
if ((rc & (MORECTL | MOREDATA)) == (MORECTL | MOREDATA))
return (-1 * ERR_MORECTLDATA);
if (rc & MORECTL)
return (-1 * ERR_MORECTL);
if (rc & MOREDATA)
return (-1 * ERR_MOREDATA);
/*
* Check for at least sizeof (long) control data portion.
*/
if (ctlp->len < sizeof (long))
return (-1 * SHORT_CONTROL_PORTION);
return (0);
} /* strgetmsg */
/*
* Function: expecting
*
* Arguments: int prim, union DL_primitives *dlp
*
* Description:
*
* Returns: int (zero on success)
*/
static int
expecting(int prim, union DL_primitives *dlp)
{
if (dlp->dl_primitive != (ulong_t)prim)
return (-1 * DL_PRIMITIVE_ERROR);
return (0);
} /* expecting */
/*
* Function: MAaddrtostring
*
* Arguments: unsigned char *addr, ulong_t length, unsigned char *s
*
* Description: return hardware address as string.
*
* Returns: void
*/
static void
MAaddrtostring(unsigned char *addr, ulong_t length, unsigned char *s)
{
int i;
for (i = 0; i < length; i++) {
(void) sprintf((char *)s, "%x:", addr[i] & 0xff);
s = s + strlen((char *)s);
}
if (length)
*(--s) = '\0';
} /* MAaddrtostring */
#if 0
/*
* Function: stringtoaddr
*
* Arguments: char *sp, char *addr
*
* Description: This function converts the string to an address.
*
* Returns: int (length of address)
*/
int
stringtoaddr(char *sp, char *addr)
{
int n = 0;
char *p;
unsigned int val;
p = sp;
while (p = strtok(p, ":")) {
if (sscanf(p, "%x", &val) != 1)
return (-1 * INVALID_STRING);
if (val > 0xff)
return (-1 * INVALID_STRING);
*addr++ = val;
n++;
p = NULL;
}
return (n);
} /* stringtoaddr */
#endif
/* -------- End of dlcommon routines */
/*
* The parms are as follows:
* device String definition of the device (e.g. "/dev/le").
* ppa Int for the instance of the device (e.g. 3, for "le3").
* phys Byte String for the dst MAC address of this packet.
* This is just the bcast address("ff:ff:ff:ff:ff:ff")
* physlen Length (int) of the mac address (not the string). E.g:
* 6 for enet.
* ipaddr long for the ip address of the host we are advertising
* our mac address for.
* ether_addr Ether address we want to set for the ipaddress we are
* impersonating
* NOTE: The mac addr of this system is not passed in, it is obtained
* directly from the dlpi info ack structure.
* Steps executed :-
* -----------------
* Open datalink provider.
* Attach to PPA.
* Bind to sap
* Send arp request as DL_UNITDATA_REQ msg
*
*/
static int send_gratuitous_arp(char *device, int ppa, unsigned char *phys,
int physlen, ipaddr_t ipaddr, unsigned char *ether_addr)
{
int saplen;
int size = sizeof (struct ether_arp);
int sapval = ETHERTYPE_ARP;
int localsap = ETHERTYPE_ARP;
int fd;
char buf[MAXDLBUF];
unsigned char sap[MAXDLADDR];
unsigned char addr[MAXDLADDR];
int addrlen;
struct ether_arp req;
union DL_primitives *dlp;
int i, ret;
ipaddr_t target_ipaddr = ipaddr;
/* initialize buf[] */
for (i = 0; i < MAXDLBUF; i++)
buf[i] = (unsigned char) i & 0xff;
/* Open the device. */
if ((fd = open(device, 2)) < 0)
return (-1 * errno);
/* Attach. */
if ((ret = dlattachreq(fd, ppa)) < 0) {
(void) close(fd);
return (ret);
}
if ((ret = dlokack(fd, buf)) < 0) {
(void) close(fd);
return (ret);
}
/* Bind. */
if ((ret = dlbindreq(fd, localsap, 0, DL_CLDLS, 0, 0)) < 0) {
(void) close(fd);
return (ret);
}
if ((ret = dlbindack(fd, buf)) < 0) {
(void) close(fd);
return (ret);
}
/* Get info */
if ((ret = dlinforeq(fd)) < 0) {
(void) close(fd);
return (ret);
}
if ((ret = dlinfoack(fd, buf)) < 0) {
(void) close(fd);
return (ret);
}
/*
* Verify sap and phys address lengths.
*/
/* LINTED E_BAD_PTR_CAST_ALIGN */
dlp = (union DL_primitives *)buf;
MAaddrtostring(OFFADDR(dlp, dlp->info_ack.dl_addr_offset),
dlp->info_ack.dl_addr_length, addr);
saplen = ABS(dlp->info_ack.dl_sap_length);
/*
* Convert destination address string to address.
*/
for (i = 0; i < saplen; ++i) {
int rev_index = saplen - 1 -i;
sap[i] = (sapval >> (rev_index * BITSPERBYTE)) & 0xff;
}
/*
* printdlprim(dlp);
*/
if (physlen != (dlp->info_ack.dl_addr_length - saplen)) {
(void) close(fd);
return (-1 * INVALID_ADDR);
}
addrlen = saplen + physlen;
/*
* Construct destination address.
*/
if (dlp->info_ack.dl_sap_length > 0) { /* order is sap+phys */
(void) memcpy(addr, sap, saplen);
(void) memcpy(addr + saplen, phys, physlen);
/* obtain our MAC address */
/*
* (void) memcpy((char *)my_etheraddr,
* (char *)OFFADDR(dlp,
* dlp->info_ack.dl_addr_offset + saplen),
* physlen);
*/
} else { /* order is phys+sap */
(void) memcpy(addr, phys, physlen);
(void) memcpy(addr + physlen, sap, saplen);
/* Obtain our MAC address */
/*
* (void) memcpy((char *)my_etheraddr,
* (char *)OFFADDR(dlp, dlp->info_ack.dl_addr_offset), physlen);
*/
}
/* create arp request */
(void) memset(&req, 0, sizeof (req));
req.arp_hrd = htons(ARPHRD_ETHER);
req.arp_pro = htons(ETHERTYPE_IP);
req.arp_hln = ETHERADDRL;
req.arp_pln = IPADDRL;
req.arp_op = htons(ARPOP_REQUEST);
(void) memcpy(&req.arp_sha, ether_addr, ETHERADDRL);
(void) memcpy(&req.arp_spa, &ipaddr, IPADDRL);
(void) memcpy(&req.arp_tpa, &target_ipaddr, IPADDRL);
/* Transmit it. */
if ((ret =
dlunitdatareq(fd, addr, addrlen, 0, 0, (unsigned char *)&req,
size)) < 0) {
(void) close(fd);
return (ret);
}
(void) close(fd);
return (0);
} /* send_gratuitous_arp() */
/*
* SYNOPSIS:
* garp interface ipaddr ether_addr
*
* RETURN VALUE :
* Returns 0 on success else -(error code)
* "interface" is the interface to send the grarp out on.
* "interface" is expressed in ifname convention(e.g. "le0", "bf2"),
* and it will be converted by ifname2device_ppa() into the dlpi convention
* (e.g. "/dev/le" + "0" and "/dev/bf" + "2").
* "ipaddr" is the ipaddr of the system we're "impersonating"
* ether_addr is the ethernet address to which we want to be impersonated.
* Sends a gratuitous arp to hw addr ff:ff:ff:ff:ff:ff.
*
* The arp packet fields are filled in as follows:
* (To be in conformance gratuitous arp described in RFC2002)
* In the gratuitous arp packet for Mobile - IP :
* Ethernet header :
* Source address : Its own hardware address
* Destination address : ff:ff:ff:ff:ff:ff(Broadcast address)
* Frame Type : 0x0806 Arp Request/Reply
*
* Arp Packet :
* Format of hardware address : ARPHRD_ETHER 1
* Format of protocol address : 0x0800
* Length of hardware address : ETH_ALEN 6
* Length of protocol address : 4
* ARP opcode : ARPOP_REQUEST 1
* Sender hardware address : Ethernet address to which to be updated.
* Destination harware address : XXXX Don't Care
* Sender IP address : IP Address(Cache entry to be updated)
* Target IP address : Sender IP Address.
* For an ARP Request Packet :
*
* Note : For ARP Reply packet target IP address should be set same
* as source IP address
*
* Modified from gabriels's arp module which was
* blatantly stolen from dlunitdatareq.c by Neal Nuckolls. Just added
* stuff to compose an arp packet.
*/
int
garp(char *dev, ipaddr_t mnaddr, unsigned char *mnetheraddr)
{
int ppa;
unsigned char phys[MAXDLADDR];
char device[MAX_INPUT];
ifname2devppa(dev, device, &ppa);
(void) memset(phys, 0xff, ETH_ALEN);
/* Validate arguments. */
if (ppa < 0)
return (-1 * INVALID_PPA);
return (send_gratuitous_arp(device, ppa, phys, ETH_ALEN,
mnaddr, mnetheraddr));
} /* garp */
/*
* Function: OScleanup
*
* Arguments: none
*
* Description: Close filedescriptors for shutdown.
* Also cleans up the dynamic interface and static
* interface list entries.
*
* Returns: void
*/
void
OScleanup()
{
(void) close(ioctl_sockid);
/* Cleanup static existing interface table */
if (dynamicIfaceHead != NULL) {
struct dynamicIfacetype *dp;
struct dynamicIfacetype *savep;
dp = dynamicIfaceHead;
while (dp != NULL) {
savep = dp->next;
dp->next = NULL;
free(dp);
dp = savep;
}
}
if (StaticIntfaceHead != NULL) {
struct staticIface *sp;
struct staticIface *save_sp;
sp = StaticIntfaceHead;
while (sp != NULL) {
save_sp = sp->next;
sp->next = NULL;
free(sp);
sp = save_sp;
}
}
dynamicIfaceHead = NULL;
StaticIntfaceHead = NULL;
ioctl_sockid = -1;
} /* OScleanup */
/*
* Function: plumb_one_tun
*
* Arguments: int tnum
*
* Description: Plumb the tunnel interface by opening the
* associated devices and pushing the required modules eg.
* 'tunl' module and others and then create persistent links.
*
* Returns: -1 on error and mux_fd is returned upon success.
* mux_fd will be used by unplumb_one_tun to destroy
* the tunnel.
*/
static int
plumb_one_tun(int tnum)
{
struct lifreq lifr;
int ip_fd, mux_fd, ip_muxid;
char name[LIFNAMSIZ];
mipverbose(("plumb_one_tun: tunnel number %d\n", tnum));
if ((ip_fd = open("/dev/ip", O_RDWR)) < 0) {
syslog(LOG_ERR, "open of /dev/ip failed");
return (-1);
}
if (ioctl(ip_fd, I_PUSH, "tun") < 0) {
syslog(LOG_ERR, "I_PUSH of tun failed");
(void) close(ip_fd);
return (-1);
}
if ((mux_fd = open("/dev/udp", O_RDWR)) < 0) {
syslog(LOG_ERR, "open of /dev/ip failed");
(void) close(ip_fd);
return (-1);
}
if (ioctl(ip_fd, I_PUSH, "ip") == -1) {
syslog(LOG_ERR, "I_PUSH of ip failed");
(void) close(ip_fd);
(void) close(mux_fd);
return (-1);
}
/* Get the existing flags for this stream */
(void) memset(&lifr, 0, sizeof (lifr));
lifr.lifr_name[0] = '\0';
if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1) {
syslog(LOG_ERR, "plumb_one_tun: SIOCGLIFFLAGS");
(void) close(ip_fd);
(void) close(mux_fd);
return (-1);
}
lifr.lifr_ppa = tnum;
(void) sprintf(name, "ip.tun%d", tnum);
(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
if (ioctl(ip_fd, SIOCSLIFNAME, (char *)&lifr) == -1) {
(void) close(ip_fd);
(void) close(mux_fd);
return (-1);
}
if ((ip_muxid = ioctl(mux_fd, I_LINK, ip_fd)) == -1) {
syslog(LOG_ERR, "I_LINK for ip failed");
(void) close(ip_fd);
(void) close(mux_fd);
return (-1);
}
lifr.lifr_ip_muxid = ip_muxid;
/*
* Tell IP the muxids of the LINKed interface
* streams so that they can be closed down at a later
* time by an unplumb operation.
*/
if (ioctl(mux_fd, SIOCSLIFMUXID, (caddr_t)&lifr) < 0) {
syslog(LOG_ERR, "plumb_one_tun: SIOCSLIFMUXID failed");
(void) close(ip_fd);
(void) close(mux_fd);
return (-1);
}
(void) close(ip_fd);
return (mux_fd);
} /* plumb_one_tun */
/*
* Function: unplumb_one_tun
*
* Arguments: int mux_fd
*
* Description: Unplumb the tunnel interface. Destroy all streams
* associated with this tunnel and close it.
*
* Returns: int
*/
static int
unplumb_one_tun(int mux_fd)
{
int retval;
retval = close(mux_fd);
return (retval);
}
/*
* Function: setifflags
*
* Arguments: int tnum, int value
*
* Description: Set the interface specific flags indicated in
* the argument 'value' for the given tunnel interface whose
* tunnel number is the argument 'tnum'.
*
* Returns: int
*/
static int
setifflags(int tnum, int value)
{
struct lifreq lifr;
char name[LIFNAMSIZ];
(void) sprintf(name, "ip.tun%d", tnum);
mipverbose(("setifflags %s\n", name));
(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
if (ioctl(ioctl_sockid, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
syslog(LOG_ERR, "setifflags: SIOCGLIFFLAGS failed");
return (-1);
}
if (value < 0) {
value = -value;
lifr.lifr_flags &= ~value;
} else
lifr.lifr_flags |= value;
(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
if (ioctl(ioctl_sockid, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
syslog(LOG_ERR, "setifflags: SIOCSLIFFLAGS failed");
return (-1);
}
return (0);
}
/*
* Function: settaddr
*
* Arguments: int tnum, ipaddr_t ifaddr1, ipaddr_t ifaddr2,
* ipaddr_t saddr, ipaddr_t daddr, struct ipsec_req_t *ipsr
*
* Description: First forms a point-to-point interface between
* ifaddr1 and ifaddr2 addresses. Next the source address of
* the tunnel is set to address 'saddr'. This is the source
* address of an outer encapsulating IP header and it must be
* the address of an interface that has already been configured.
* The destination address of the tunnel is set to 'daddr'.
*
* Returns: static int
*/
static int
settaddr(int tnum, ipaddr_t ifaddr1, ipaddr_t ifaddr2,
ipaddr_t saddr, ipaddr_t daddr, ipsec_req_t *ipsr_p)
{
struct sockaddr_storage laddr1, laddr2;
struct iftun_req treq;
char name[LIFNAMSIZ];
struct sockaddr_in *sin;
(void) sprintf(name, "ip.tun%d", tnum);
mipverbose(("settaddr %s\n", name));
if (mkpt2pt(name, ifaddr1, ifaddr2) < 0) {
syslog(LOG_ERR, "settaddr: mkpt2pt failed");
return (-1);
}
bzero(&treq, sizeof (struct iftun_req));
treq.ifta_vers = IFTUN_VERSION;
(void) strncpy(treq.ifta_lifr_name, name,
sizeof (treq.ifta_lifr_name));
if (ioctl(ioctl_sockid, SIOCGTUNPARAM, (caddr_t)&treq) < 0) {
syslog(LOG_ERR, "Not a tunnel");
return (-1);
}
if (treq.ifta_lower != IFTAP_IPV4) {
syslog(LOG_ERR, "Unknown lower tunnel");
return (-1);
}
sin = (struct sockaddr_in *)&laddr1;
(void) memset(&laddr1, 0, sizeof (laddr1));
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = saddr;
sin = (struct sockaddr_in *)&laddr2;
(void) memset(&laddr2, 0, sizeof (laddr2));
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = daddr;
treq.ifta_saddr = laddr1;
treq.ifta_daddr = laddr2;
treq.ifta_flags = IFTUN_SRC|IFTUN_DST;
(void) strncpy(treq.ifta_lifr_name, name,
sizeof (treq.ifta_lifr_name));
treq.ifta_vers = IFTUN_VERSION;
/* non-null means there's tunnel protection to be added! */
if (ipsr_p != NULL) {
/* finally set the ipsec protection bits! */
(void) memcpy(&treq.ifta_secinfo, ipsr_p, sizeof (ipsec_req_t));
/* set the flag so the kernel sets up the security! */
treq.ifta_flags |= IFTUN_SECURITY;
}
if (ioctl(ioctl_sockid, SIOCSTUNPARAM, (caddr_t)&treq) < 0) {
syslog(LOG_ERR, "set tunnel addr failed");
return (-1);
}
return (0);
} /* settaddr */
/*
* Function: getEthernetAddr
*
* Arguments: char *ename, unsigned char *eaddr
*
* Description: Get the hardware address for specified interface name.
*
* Returns: int
*/
int
getEthernetAddr(char *ename, unsigned char *eaddr)
{
int saplen;
int localsap = ETHERTYPE_ARP;
int fd;
char buf[MAXDLBUF];
unsigned char addr[MAXDLADDR];
union DL_primitives *dlp;
int i, ret;
int physlen = ETHERADDRL;
int ppa;
char device[LIFNAMSIZ + 5];
char *lasts;
/*
* If this is a virtual interface, remove the ':' character (and
* everything following that character. We do not really care
* about it since the MAC address is the same as the physical
* interface.
*/
ename = strtok_r(ename, ":", &lasts);
if (ename == NULL) {
return (-1);
}
ifname2devppa(ename, device, &ppa);
/* initialize buf[] */
for (i = 0; i < MAXDLBUF; i++)
buf[i] = (unsigned char) i & 0xff;
/* Open the device. */
if ((fd = open(device, (O_RDWR | O_NDELAY))) < 0)
return (-1 * errno);
/* Attach. */
if ((ret = dlattachreq(fd, ppa)) < 0) {
(void) close(fd);
return (ret);
}
if ((ret = dlokack(fd, buf)) < 0) {
(void) close(fd);
return (ret);
}
/* Bind. */
if ((ret = dlbindreq(fd, localsap, 0, DL_CLDLS, 0, 0)) < 0) {
(void) close(fd);
return (ret);
}
if ((ret = dlbindack(fd, buf)) < 0) {
(void) close(fd);
return (ret);
}
/* Get info */
if ((ret = dlinforeq(fd)) < 0) {
(void) close(fd);
return (ret);
}
/* WORK -- check this error message, and send to mohanp@eng */
(void) sleep(1);
if ((ret = dlinfoack(fd, buf)) < 0) {
(void) close(fd);
return (ret);
}
/* Verify sap and phys address lengths */
/* LINTED E_BAD_PTR_CAST_ALIGN */
dlp = (union DL_primitives *)buf;
MAaddrtostring(OFFADDR(dlp, dlp->info_ack.dl_addr_offset),
dlp->info_ack.dl_addr_length, addr);
saplen = ABS(dlp->info_ack.dl_sap_length);
/* Construct destination address. */
if (dlp->info_ack.dl_sap_length > 0) { /* order is sap+phys */
/* obtain our MAC address */
(void) memcpy(eaddr, OFFADDR(dlp,
dlp->info_ack.dl_addr_offset + saplen),
physlen);
} else { /* order is phys+sap */
/* Obtain our MAC address */
(void) memcpy(eaddr, OFFADDR(dlp,
dlp->info_ack.dl_addr_offset),
physlen);
}
(void) close(fd);
return (0);
} /* getEthernetAddr */
/*
* Function: getIfaceInfo
*
* Arguments: char *ifaceName, ipaddr_t *addr, ipaddr_t *mask, uint64_t *flags
* uint32_t *ifindex
*
* Description: Gets the interface information given the name.
*
* Returns: int
*/
int
getIfaceInfo(char *ifaceName, ipaddr_t *addr, ipaddr_t *mask,
uint64_t *flags, uint32_t *ifindex)
{
struct lifreq lifr;
struct sockaddr_in *sin;
int ioc_sockid;
bzero((char *)&lifr, sizeof (lifr));
(void) strncpy(lifr.lifr_name, ifaceName, sizeof (lifr.lifr_name));
ioc_sockid = socket(AF_INET, SOCK_DGRAM, 0);
if (ioc_sockid < 0) {
syslog(LOG_ERR,
"Could not open socket for ioctls in getIfaceInfo()");
return (-1);
}
if (ioctl(ioc_sockid, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
syslog(LOG_ERR, "Could not read IP address for %s", ifaceName);
(void) close(ioc_sockid);
return (-1);
}
sin = (struct sockaddr_in *)&lifr.lifr_addr;
*addr = sin->sin_addr.s_addr;
if (ioctl(ioc_sockid, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
syslog(LOG_ERR, "Could not read netmask for %s", ifaceName);
(void) close(ioc_sockid);
return (-1);
}
sin = (struct sockaddr_in *)&lifr.lifr_addr;
*mask = sin->sin_addr.s_addr;
(void) strncpy(lifr.lifr_name, ifaceName, sizeof (lifr.lifr_name));
if (ioctl(ioc_sockid, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
syslog(LOG_ERR, "Could not read flags for %s", ifaceName);
(void) close(ioc_sockid);
return (-1);
}
*flags = lifr.lifr_flags;
if (ioctl(ioc_sockid, SIOCGLIFINDEX, (char *)&lifr) < 0) {
syslog(LOG_ERR, "Can't read IFINDEX %s", ifaceName);
(void) close(ioc_sockid);
return (-1);
}
*ifindex = lifr.lifr_index;
(void) close(ioc_sockid);
return (0);
} /* getIfaceInfo */
/*
* Function: arpIfadd
*
* Arguments: vaddr - visitor MN's IP address
* inIfindex - inbound interface index
* slla - MN's source link layer addr
*
* Description: Add an ARP entry given the interface index. Invokes
* SIOCSXARP with sdl_data array filled with interface
* name (without null terminator) followed by address.
* This code assumes it is being invoked on Ethernet.
*
* Returns: 0 - sucesss; errno - failure
*/
int
arpIfadd(ipaddr_t vaddr, char *slla, uint32_t inIfindex)
{
struct xarpreq ar;
char *etheraddr;
int val;
char addrstr1[INET_ADDRSTRLEN];
struct ether_addr ether;
(void) memset(&ar, 0, sizeof (struct xarpreq));
if (if_indextoname(inIfindex, ar.xarp_ha.sdl_data) == NULL) {
syslog(LOG_ERR, "if_indextoname returned NULL\n");
return ((-1) * errno);
}
/*
* Mark this entry permanent to prevent ARP from blowing this
* away.
*/
ar.xarp_flags = ATF_PERM;
((struct sockaddr_in *)&ar.xarp_pa)->sin_addr.s_addr = vaddr;
((struct sockaddr_in *)&ar.xarp_pa)->sin_family = AF_INET;
(void) memcpy(ether.ether_addr_octet, slla, ETHERADDRL);
etheraddr = ether_ntoa(ðer);
mipverbose(("Adding temporary ARP entry for visitor %s,"
" hardware address %s on interface %s [index %d]\n",
ntoa(vaddr, addrstr1), etheraddr, ar.xarp_ha.sdl_data, inIfindex));
ar.xarp_ha.sdl_nlen = strlen(ar.xarp_ha.sdl_data);
ar.xarp_ha.sdl_alen = ETHERADDRL;
ar.xarp_ha.sdl_family = AF_LINK;
(void) memcpy(LLADDR(&ar.xarp_ha), slla, ar.xarp_ha.sdl_alen);
val = ioctl(ioctl_sockid, SIOCSXARP, (caddr_t)&ar);
if (val < 0)
return ((-1) * errno);
else
return (val);
}
/*
* Function: arpIfdel
*
* Arguments: vaddr - visitor MN's IP address
* slla - MN's source link layer addr (used here for mipverbose)
* inIfindex - inbound interface index
*
* Description: Delete an ARP entry based on the interface index. Invokes
* SIOCDXARP with sdl_data array filled with interface
* name. This code assumes it is being invoked on Ethernet.
*
* Returns: 0 - sucesss; errno - failure
*/
int
arpIfdel(ipaddr_t vaddr, char *slla, uint32_t inIfindex)
{
struct xarpreq ar;
int val;
char *etheraddr;
char addrstr1[INET_ADDRSTRLEN];
struct ether_addr ether;
(void) memset(&ar, 0, sizeof (struct xarpreq));
if (if_indextoname(inIfindex, ar.xarp_ha.sdl_data) == NULL) {
syslog(LOG_ERR, "if_indextoname returned NULL\n");
return ((-1) * errno);
}
ar.xarp_flags = ATF_PERM;
((struct sockaddr_in *)&ar.xarp_pa)->sin_addr.s_addr = vaddr;
((struct sockaddr_in *)&ar.xarp_pa)->sin_family = AF_INET;
(void) memcpy(ether.ether_addr_octet, slla, ETHERADDRL);
etheraddr = ether_ntoa(ðer);
mipverbose(("Deleting temporary ARP entry for visitor %s,"
" hardware address %s on interface %s [index %d]\n",
ntoa(vaddr, addrstr1), etheraddr, ar.xarp_ha.sdl_data, inIfindex));
ar.xarp_ha.sdl_nlen = strlen(ar.xarp_ha.sdl_data);
ar.xarp_ha.sdl_family = AF_LINK;
/*
* Delete an ARP entry in the ARP cache
*/
val = ioctl(ioctl_sockid, SIOCDXARP, (caddr_t)&ar);
if (val < 0)
return ((-1) * errno);
else
return (0);
}
/*
* Function: CreateListOfExistingIntfce
* This function stores a list of existing interfaces into a
* static interface entry table when the mipagent is started.
* The existing interface list does not include any IPv6,
* loopback and logical interfaces.
* Return value : 0 on success, -1 on failure
*/
int
CreateListOfExistingIntfce(void) {
struct lifnum lifn;
struct lifconf lifc;
struct lifreq lifr;
struct lifreq *lifrp;
int numifs;
int bufsize;
int iocsock;
int n;
char *buf;
StaticIfaceEntry *ifce_ptr;
StaticIfaceEntry *saveptr = NULL;
iocsock = socket(AF_INET, SOCK_DGRAM, 0);
if (iocsock < 0) {
syslog(LOG_ERR, "Can't open IOCTL socket: %m");
return (-1);
}
lifn.lifn_family = AF_INET;
lifn.lifn_flags = 0;
if (ioctl(iocsock, SIOCGLIFNUM, (char *)&lifn) < 0) {
syslog(LOG_ERR, "SIOCGLIFNUM failed: %m");
return (-1);
}
numifs = lifn.lifn_count;
bufsize = numifs * sizeof (struct lifreq);
buf = malloc(bufsize);
if (buf == NULL) {
syslog(LOG_ERR,
"Can't create existing interface list: Out of memory: %m");
return (-1);
}
lifc.lifc_family = AF_INET;
lifc.lifc_flags = 0;
lifc.lifc_len = bufsize;
lifc.lifc_buf = buf;
if (ioctl(iocsock, SIOCGLIFCONF, (char *)&lifc) < 0) {
syslog(LOG_ERR,
"Can't get existing system interface configuration: %m");
free(buf);
return (-1);
}
StaticIntfaceHead = NULL;
lifrp = lifc.lifc_req;
for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
(void) strncpy(lifr.lifr_name, lifrp->lifr_name,
sizeof (lifr.lifr_name));
if (strchr(lifr.lifr_name, ':') != NULL)
continue;
if (ioctl(iocsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
syslog(LOG_ERR,
"Can't get flag information for %s: %m",
lifr.lifr_name);
continue;
}
if (lifr.lifr_flags & IFF_LOOPBACK)
continue;
/* Create or add interface list */
ifce_ptr = (struct staticIface *)
malloc(sizeof (struct staticIface));
if (ifce_ptr == NULL) {
syslog(LOG_ERR,
"malloc: Can't create existing interface list: %m");
free(buf);
return (-1);
}
(void) strncpy(ifce_ptr->ifacename, lifr.lifr_name,
sizeof (lifr.lifr_name));
if (StaticIntfaceHead == NULL)
StaticIntfaceHead = ifce_ptr;
if (saveptr != NULL) {
saveptr->next = ifce_ptr;
}
ifce_ptr->next = NULL;
saveptr = ifce_ptr;
}
(void) close(iocsock);
free(buf);
return (0);
}
/*
* This function returns true or false based on matching entries
* from StaticIfaceEntry list
*/
boolean_t
existingStaticInterface(const char *ifname) {
struct staticIface *Sptr;
Sptr = StaticIntfaceHead;
while (Sptr != NULL) {
if (strcmp(Sptr->ifacename, ifname) == 0) {
mipverbose(("existingStaticInterface:"
"Found a static existing entry\n"));
return (_B_TRUE);
}
Sptr = Sptr->next;
}
return (_B_FALSE);
}
|