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
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
|
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (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 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
include ../Makefile.lib
HDRS = ipsec_util.h ikedoor.h errfp.h
HDRDIR = common
# Hammerhead: amd64-only
SUBDIRS = $(MACH64)
all : TARGET = all
clean : TARGET = clean
clobber : TARGET = clobber
install : TARGET = install
#
# Definitions for message catalog
#
MSGFILES = `$(GREP) -l gettext common/*.[ch]`
POFILE = libipsecutil.po
.KEEP_STATE:
all clean clobber install: $(SUBDIRS)
$(POFILE): pofile_MSGFILES
install_h: $(ROOTHDRS)
check: $(CHECKHDRS)
_msg: $(MSGDOMAINPOFILE)
include ../../Makefile.msg.targ
$(SUBDIRS): FRC
@cd $@; pwd; $(MAKE) $(TARGET)
FRC:
include ../Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (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 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
# Copyright 2018, Joyent, Inc.
#
LIBRARY = libipsecutil.a
VERS = .1
OBJECTS = ipsec_util.o algs.o
include ../../Makefile.lib
LIBS += $(DYNLIB)
SRCDIR = ../common
BERDIR = $(SRC)/lib/libkmf/ber_der/inc
LDLIBS += -ltecla -lsocket -lnsl -lc -lkmf -lkmfberder
LAZYLIBS = $(ZLAZYLOAD) -ltsol $(ZNOLAZYLOAD)
LDLIBS += $(LAZYLIBS)
CFLAGS += $(CCVERBOSE)
CPPFLAGS += -I$(SRCDIR) -I$(BERDIR)
CERRWARN += -Wno-unused-function
CERRWARN += $(CNOWARN_UNINIT)
SMOFF += shift_to_zero
.KEEP_STATE:
all: $(LIBS)
include ../../Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (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 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
include ../Makefile.com
include ../../Makefile.lib.64
install: all $(ROOTLIBS64) $(ROOTLINKS64)
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <ipsec_util.h>
#include <stdlib.h>
#include <strings.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <libintl.h>
#include <errno.h>
static char *preamble =
"# /etc/inet/ipsecalgs output from ipsecalgs(8)\n"
"#\n"
"# DO NOT EDIT OR PARSE THIS FILE!\n"
"#\n"
"# Use the ipsecalgs(8) command to change the contents of this file.\n"
"# The algorithm descriptions contained in this file are synchronised to the\n"
"# kernel with ipsecalgs -s, the kernel validates the entries at this point."
"\n\n"
"# PROTO|protocol-id|protocol-name|exec-mode\n"
"## NOTE: Some protocol numbers are well-known and defined in <netdb.h>\n\n"
"# ALG|protocol-id|alg-id|name,name,...|ef-id| \n"
"# {default/}{key,key..}or{key-key,inc}|block_size or MAC-size|\n"
"# [parameter,parameter..]|[flags]\n\n"
"#\n"
"## Note: Parameters and flags only apply to certain algorithms.\n\n";
#define CFG_PERMS S_IRUSR | S_IRGRP | S_IROTH /* Perms 0444. */
#define CFG_OWNER 0 /* root */
#define CFG_GROUP 1 /* "other" */
/*
* write_new_algfile() helper macros to check for write errors.
*/
#define FPRINTF_ERR(fcall) if ((fcall) < 0) { \
rc = LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE; \
goto bail; \
}
#define FPUT_ERR(fcall) if ((fcall) == EOF) { \
rc = LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE; \
goto bail; \
}
/*
* Helper macros to start and finish a list of entries that were added
* as part of a package installation.
*/
#define PKG_SEC_START(pkgname, doing_pkg, cur_pkg) { \
(void) strcpy((cur_pkg), (pkgname)); \
FPRINTF_ERR(fprintf(f, "%s%s\n", \
LIBIPSEC_ALGS_LINE_PKGSTART, (cur_pkg))); \
(doing_pkg) = B_TRUE; \
}
#define PKG_SEC_END(doing_pkg, cur_pkg) { \
if (doing_pkg) { \
FPRINTF_ERR(fprintf(f, "%s%s\n", \
LIBIPSEC_ALGS_LINE_PKGEND, (cur_pkg))); \
(doing_pkg) = B_FALSE; \
} \
}
/*
* Take a zero-terminated int array and print int1,int2...,intN.
* If zero-only, then print a single '0'.
* Returns 0 on success, -1 if an error occurred while writing to
* the specified file.
*/
int
list_ints(FILE *f, int *floater)
{
boolean_t executed = B_FALSE;
while (*floater != 0) {
executed = B_TRUE;
if (fprintf(f, "%d", *floater) < 0)
return (-1);
if (*(++floater) != 0)
if (fputc(',', f) == EOF)
return (-1);
}
if (!executed)
if (fputc('0', f) == EOF)
return (-1);
return (0);
}
/*
* If the specified algorithm was defined within a package section, i.e.
* between the lines "# Start <pkgname>" and "# End <pkgname>", returns
* the value of <pkgname>.
*/
static char *
alg_has_pkg(ipsec_proto_t *proto, struct ipsecalgent *alg)
{
int i;
if (proto->proto_algs_pkgs == NULL)
return (NULL);
for (i = 0; i < proto->proto_algs_npkgs; i++)
if (proto->proto_algs_pkgs[i].alg_num == alg->a_alg_num)
return (proto->proto_algs_pkgs[i].pkg_name);
return (NULL);
}
/*
* Writes the package start/end delimiters according to the package
* name associated with the current protocol or algorithm, and
* the state of the packaging information already written to the file.
* Called by write_new_algfile(). Returns 0 on success, one of the
* LIBIPSEC_DIAG codes on failure.
*/
static int
pkg_section(FILE *f, char *pkg_name, boolean_t *doing_pkg, char *cur_pkg)
{
int rc = 0;
if (pkg_name != NULL) {
/* protocol or algorithm is associated with a package */
if (!*doing_pkg) {
/* start of a new package section */
PKG_SEC_START(pkg_name, *doing_pkg, cur_pkg);
} else {
/* already in a package section */
if (strcmp(pkg_name, cur_pkg) != 0) {
/* different package name */
PKG_SEC_END(*doing_pkg, cur_pkg);
PKG_SEC_START(pkg_name, *doing_pkg, cur_pkg);
}
}
} else if (*doing_pkg) {
/* in a package section when the entry isn't */
PKG_SEC_END(*doing_pkg, cur_pkg);
}
bail:
return (rc);
}
/*
* Given a list of protocols and number, write them to a new algorithm file.
* This function takes num_protos + num_protos * dois-per-alg operations.
* Also free the protocol structure.
*
* Note that no locking spans the read/update/write phases that can be
* used by callers of this routine. This could cause this function to suffer
* from the "lost update" problem. Since updates to the IPsec protocols
* and algorithm tables are very infrequent, this should not be a issue in
* practice.
*/
static int
write_new_algfile(ipsec_proto_t *protos, int num_protos)
{
FILE *f;
int fd, i, j, k;
int rc = 0;
struct ipsecalgent *alg;
char cur_pkg[1024];
boolean_t doing_pkg = B_FALSE;
char *alg_pkg;
char tmp_name_template[] = INET_IPSECALGSPATH "ipsecalgsXXXXXX";
char *tmp_name;
/*
* In order to avoid potentially corrupting the configuration
* file on file system failure, write the new configuration info
* to a temporary file which is then renamed to the configuration
* file (INET_IPSECALGSFILE.)
*/
tmp_name = mktemp(tmp_name_template);
fd = open(tmp_name, O_WRONLY|O_CREAT|O_EXCL, CFG_PERMS);
if (fd == -1) {
rc = LIBIPSEC_ALGS_DIAG_ALGSFILEOPEN;
goto bail;
}
f = fdopen(fd, "w");
if (f == NULL) {
(void) close(fd);
rc = LIBIPSEC_ALGS_DIAG_ALGSFILEFDOPEN;
goto bail;
}
FPUT_ERR(fputs(preamble, f));
/* Write protocol entries. */
for (i = 0; i < num_protos; i++) {
/* add package section delimiters if needed */
rc = pkg_section(f, protos[i].proto_pkg, &doing_pkg, cur_pkg);
if (rc != 0)
goto bail;
FPRINTF_ERR(fprintf(f, "%s%d|%s|",
LIBIPSEC_ALGS_LINE_PROTO,
protos[i].proto_num, protos[i].proto_name));
switch (protos[i].proto_exec_mode) {
case LIBIPSEC_ALGS_EXEC_SYNC:
FPRINTF_ERR(fprintf(f, "sync\n"));
break;
case LIBIPSEC_ALGS_EXEC_ASYNC:
FPRINTF_ERR(fprintf(f, "async\n"));
break;
}
}
/* terminate the package section for the protocols if needed */
PKG_SEC_END(doing_pkg, cur_pkg);
FPUT_ERR(fputs("\n", f));
/* Write algorithm entries. */
for (i = 0; i < num_protos; i++) {
for (j = 0; j < protos[i].proto_numalgs; j++) {
alg = protos[i].proto_algs[j];
/* add package section delimiters if needed */
alg_pkg = alg_has_pkg(&protos[i], alg);
rc = pkg_section(f, alg_pkg, &doing_pkg, cur_pkg);
if (rc != 0)
goto bail;
/* protocol and algorithm numbers */
FPRINTF_ERR(fprintf(f, "%s%d|%d|",
LIBIPSEC_ALGS_LINE_ALG,
alg->a_proto_num, alg->a_alg_num));
/* algorithm names */
for (k = 0; alg->a_names[k] != NULL; k++) {
FPRINTF_ERR(fprintf(f, "%s", alg->a_names[k]));
if (alg->a_names[k+1] != NULL)
FPRINTF_ERR(fprintf(f, ","));
}
/* mechanism name */
FPRINTF_ERR(fprintf(f, "|%s|", alg->a_mech_name));
/* key sizes */
if (alg->a_key_increment == 0) {
/* key sizes defined by enumeration */
if (list_ints(f, alg->a_key_sizes) == -1) {
rc = LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE;
goto bail;
}
} else {
/* key sizes defined by range */
FPRINTF_ERR(fprintf(f, "%d/%d-%d,%d",
alg->a_key_sizes[0], alg->a_key_sizes[1],
alg->a_key_sizes[2], alg->a_key_increment));
}
FPUT_ERR(fputc('|', f));
/* block sizes */
if (list_ints(f, alg->a_block_sizes) == -1) {
rc = LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE;
goto bail;
}
FPUT_ERR(fputc('|', f));
/*
* Some algorithms require extra parameters, these
* are stored in an array. For algorithms that don't
* need these parameters, or flags (below), these
* extra fields in the ipsecalgs file must contain a
* zero. This fuction will get called if a algorithm
* entry is added, at this point the extra fields will
* be added to the file.
*/
if (list_ints(f, alg->a_mech_params) == -1) {
rc = LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE;
goto bail;
}
/* flags */
FPRINTF_ERR(fprintf(f, "|%d\n", alg->a_alg_flags));
}
}
/* terminate the package section for the algorithms if needed */
PKG_SEC_END(doing_pkg, cur_pkg);
if (fchmod(fd, CFG_PERMS) == -1) {
rc = LIBIPSEC_ALGS_DIAG_ALGSFILECHMOD;
goto bail;
}
if (fchown(fd, CFG_OWNER, CFG_GROUP) == -1) {
rc = LIBIPSEC_ALGS_DIAG_ALGSFILECHOWN;
goto bail;
}
if (fclose(f) == EOF) {
rc = LIBIPSEC_ALGS_DIAG_ALGSFILECLOSE;
goto bail;
}
if (rename(tmp_name, INET_IPSECALGSFILE) == -1)
rc = LIBIPSEC_ALGS_DIAG_ALGSFILERENAME;
bail:
_clean_trash(protos, num_protos);
return (rc);
}
/*
* Return a pointer to the protocol entry corresponding to the specified
* protocol num proto_num. Also builds the list of currently defined
* protocols.
*/
static ipsec_proto_t *
proto_setup(ipsec_proto_t **protos, int *num_protos, int proto_num,
boolean_t cleanup)
{
int i;
ipsec_proto_t *current_proto, *ret_proto = NULL;
_build_internal_algs(protos, num_protos);
if (*protos == NULL)
return (NULL);
for (i = 0; i < *num_protos; i++) {
current_proto = (*protos) + i;
if (current_proto->proto_num == proto_num) {
ret_proto = current_proto;
break;
}
}
if (ret_proto == NULL) {
if (cleanup)
_clean_trash(*protos, *num_protos);
/* else caller wants parsed /etc/inet/ipsecalgs anyway */
}
return (ret_proto);
}
/*
* Delete the first found algorithm of the specified protocol which
* has the same name as the one specified by alg_name. Deletion of
* the entry takes place only if the delete_it flag is set. If an
* entry was found, return B_TRUE, otherwise return B_FALSE.
*/
static boolean_t
delipsecalgbyname_common(const char *name, ipsec_proto_t *proto,
boolean_t delete_it)
{
int i;
char **name_check;
boolean_t found_match = B_FALSE;
for (i = 0; i < proto->proto_numalgs; i++) {
if (!found_match) {
for (name_check =
proto->proto_algs[i]->a_names;
*name_check != NULL; name_check++) {
/*
* Can use strcmp because the algorithm names
* are bound.
*/
if (strcmp(*name_check, name) == 0) {
found_match = B_TRUE;
if (!delete_it)
return (found_match);
freeipsecalgent(proto->proto_algs[i]);
break;
}
}
} else {
proto->proto_algs[i - 1] = proto->proto_algs[i];
}
}
if (found_match)
proto->proto_numalgs--;
return (found_match);
}
/*
* Returns B_TRUE if the specified 0-terminated lists of key or
* block sizes match, B_FALSE otherwise.
*/
static boolean_t
sizes_match(int *a1, int *a2)
{
int i;
for (i = 0; (a1[i] != 0) && (a2[i] != 0); i++) {
if (a1[i] != a2[i])
return (B_FALSE);
}
if ((a1[i] != 0) || (a2[i] != 0))
return (B_FALSE);
return (B_TRUE);
}
/*
* Returns B_TRUE if an _exact_ equivalent of the specified algorithm
* already exists, B_FALSE otherwise.
*/
static boolean_t
ipsecalg_exists(struct ipsecalgent *newbie, ipsec_proto_t *proto)
{
struct ipsecalgent *curalg;
char **curname, **newbiename;
int i;
boolean_t match;
for (i = 0; i < proto->proto_numalgs; i++) {
curalg = proto->proto_algs[i];
if (curalg->a_alg_num != newbie->a_alg_num)
continue;
if (curalg->a_key_increment != newbie->a_key_increment)
continue;
if (strcmp(curalg->a_mech_name, newbie->a_mech_name) != 0)
continue;
curname = curalg->a_names;
newbiename = newbie->a_names;
match = B_TRUE;
while ((*curname != NULL) && (*newbiename != NULL) && match) {
match = (strcmp(*curname, *newbiename) == 0);
curname++;
newbiename++;
}
if (!match || (*curname != NULL) || (*newbiename != NULL))
continue;
if (!sizes_match(curalg->a_block_sizes, newbie->a_block_sizes))
continue;
if (!sizes_match(curalg->a_key_sizes, newbie->a_key_sizes))
continue;
/* we found an exact match */
return (B_TRUE);
}
return (B_FALSE);
}
/*
* Add a new algorithm to the /etc/inet/ipsecalgs file. Caller must free
* or otherwise address "newbie".
*/
int
addipsecalg(struct ipsecalgent *newbie, uint_t flags)
{
ipsec_proto_t *protos, *current_proto;
struct ipsecalgent *clone, **holder;
int num_protos, i;
char **name_check;
boolean_t forced_add = (flags & LIBIPSEC_ALGS_ADD_FORCE) != 0;
boolean_t found_match;
if ((current_proto = proto_setup(&protos, &num_protos,
newbie->a_proto_num, B_TRUE)) == NULL)
return (LIBIPSEC_ALGS_DIAG_UNKN_PROTO);
/*
* If an algorithm that matches _exactly_ the new algorithm
* already exists, we're done.
*/
if (ipsecalg_exists(newbie, current_proto))
return (0);
/*
* We don't allow a new algorithm to be created if one of
* its names is already defined for an existing algorithm,
* unless the operation is forced, in which case existing
* algorithm entries that conflict with the new one are
* deleted.
*/
for (name_check = newbie->a_names; *name_check != NULL; name_check++) {
found_match = delipsecalgbyname_common(*name_check,
current_proto, forced_add);
if (found_match && !forced_add) {
/*
* Duplicate entry found, but the addition was
* not forced.
*/
_clean_trash(protos, num_protos);
return (LIBIPSEC_ALGS_DIAG_ALG_EXISTS);
}
}
for (i = 0; i < current_proto->proto_numalgs; i++) {
if (current_proto->proto_algs[i]->a_alg_num ==
newbie->a_alg_num) {
/*
* An algorithm with the same protocol number
* and algorithm number already exists. Fail
* addition unless the operation is forced.
*/
if (flags & LIBIPSEC_ALGS_ADD_FORCE) {
clone = _duplicate_alg(newbie);
if (clone != NULL) {
freeipsecalgent(
current_proto->proto_algs[i]);
current_proto->proto_algs[i] = clone;
return (write_new_algfile(protos,
num_protos));
} else {
_clean_trash(protos, num_protos);
return (LIBIPSEC_ALGS_DIAG_NOMEM);
}
} else {
_clean_trash(protos, num_protos);
return (LIBIPSEC_ALGS_DIAG_ALG_EXISTS);
}
}
}
/* append the new algorithm */
holder = realloc(current_proto->proto_algs,
sizeof (struct ipsecalgent *) * (i + 1));
if (holder == NULL) {
_clean_trash(protos, num_protos);
return (LIBIPSEC_ALGS_DIAG_NOMEM);
}
clone = _duplicate_alg(newbie);
if (clone == NULL) {
free(holder);
_clean_trash(protos, num_protos);
return (LIBIPSEC_ALGS_DIAG_NOMEM);
}
current_proto->proto_numalgs++;
current_proto->proto_algs = holder;
current_proto->proto_algs[i] = clone;
return (write_new_algfile(protos, num_protos));
}
/*
* Delete an algorithm by name & protocol number from /etc/inet/ipsecalgs.
* Only deletes the first encountered instance.
*/
int
delipsecalgbyname(const char *name, int proto_num)
{
ipsec_proto_t *protos, *current_proto;
int num_protos;
if ((current_proto = proto_setup(&protos, &num_protos, proto_num,
B_TRUE)) == NULL)
return (LIBIPSEC_ALGS_DIAG_UNKN_PROTO);
if (delipsecalgbyname_common(name, current_proto, B_TRUE))
return (write_new_algfile(protos, num_protos));
_clean_trash(protos, num_protos);
return (LIBIPSEC_ALGS_DIAG_UNKN_ALG);
}
/*
* Delete an algorithm by num + protocol num from /etc/inet/ipsecalgs.
*/
int
delipsecalgbynum(int alg_num, int proto_num)
{
ipsec_proto_t *protos, *current_proto;
int i, num_protos;
boolean_t found_match = B_FALSE;
if ((current_proto = proto_setup(&protos, &num_protos, proto_num,
B_TRUE)) == NULL)
return (LIBIPSEC_ALGS_DIAG_UNKN_PROTO);
for (i = 0; i < current_proto->proto_numalgs; i++) {
if (!found_match) {
if (current_proto->proto_algs[i]->a_alg_num ==
alg_num) {
found_match = B_TRUE;
freeipsecalgent(current_proto->proto_algs[i]);
}
} else {
current_proto->proto_algs[i - 1] =
current_proto->proto_algs[i];
}
}
if (found_match) {
current_proto->proto_numalgs--;
return (write_new_algfile(protos, num_protos));
}
_clean_trash(protos, num_protos);
return (LIBIPSEC_ALGS_DIAG_UNKN_ALG);
}
/*
* Remove the specified protocol entry from the list of protocols.
*/
static void
delipsecproto_common(ipsec_proto_t *protos, int num_protos,
ipsec_proto_t *proto)
{
int i;
/* free protocol storage */
free(proto->proto_name);
for (i = 0; i < proto->proto_numalgs; i++)
freeipsecalgent(proto->proto_algs[i]);
/* remove from list of prototocols */
for (i = (proto - protos + 1); i < num_protos; i++)
protos[i - 1] = protos[i];
}
/*
* Add an IPsec protocol to /etc/inet/ipsecalgs.
*/
int
addipsecproto(const char *proto_name, int proto_num,
ipsecalgs_exec_mode_t proto_exec_mode, uint_t flags)
{
ipsec_proto_t *protos, *current_proto, *new_proto;
int i, num_protos;
/*
* NOTE:If build_internal_algs returns NULL for any
* reason, we will end up clobbering /etc/inet/ipsecalgs!
*/
current_proto = proto_setup(&protos, &num_protos, proto_num, B_FALSE);
/* check for protocol with duplicate id */
if (current_proto != NULL) {
if ((strcmp(proto_name, current_proto->proto_name) == 0) &&
(proto_exec_mode == current_proto->proto_exec_mode)) {
/*
* The current protocol being added matches
* exactly an existing protocol, we're done.
*/
return (0);
}
if (!(flags & LIBIPSEC_ALGS_ADD_FORCE))
return (LIBIPSEC_ALGS_DIAG_PROTO_EXISTS);
delipsecproto_common(protos, num_protos--, current_proto);
}
/* check for protocol with duplicate name */
for (i = 0; i < num_protos; i++) {
if (strcmp(protos[i].proto_name, proto_name) == 0) {
if (!(flags & LIBIPSEC_ALGS_ADD_FORCE))
return (LIBIPSEC_ALGS_DIAG_PROTO_EXISTS);
delipsecproto_common(protos, num_protos--, &protos[i]);
break;
}
}
/* add new protocol */
num_protos++;
new_proto = realloc(protos, num_protos *
sizeof (ipsec_proto_t));
if (new_proto == NULL) {
_clean_trash(protos, num_protos - 1);
return (LIBIPSEC_ALGS_DIAG_NOMEM);
}
protos = new_proto;
new_proto += (num_protos - 1);
/* initialize protocol entry */
new_proto->proto_num = proto_num;
new_proto->proto_numalgs = 0;
new_proto->proto_algs = NULL;
new_proto->proto_name = strdup(proto_name);
if (new_proto->proto_name == NULL) {
_clean_trash(protos, num_protos);
return (LIBIPSEC_ALGS_DIAG_NOMEM);
}
new_proto->proto_pkg = NULL;
new_proto->proto_algs_pkgs = NULL;
new_proto->proto_algs_npkgs = 0;
new_proto->proto_exec_mode = proto_exec_mode;
return (write_new_algfile(protos, num_protos));
}
/*
* Delete an IPsec protocol entry from /etc/inet/ipsecalgs. This also
* nukes the associated algorithms.
*/
int
delipsecprotobynum(int proto_num)
{
ipsec_proto_t *protos, *current_proto;
int num_protos;
if ((current_proto = proto_setup(&protos, &num_protos, proto_num,
B_TRUE)) == NULL)
return (LIBIPSEC_ALGS_DIAG_UNKN_PROTO);
delipsecproto_common(protos, num_protos--, current_proto);
return (write_new_algfile(protos, num_protos));
}
int
delipsecprotobyname(const char *proto_name)
{
int proto_num;
proto_num = getipsecprotobyname(proto_name);
if (proto_num == -1)
return (LIBIPSEC_ALGS_DIAG_UNKN_PROTO);
return (delipsecprotobynum(proto_num));
}
/*
* Implement these in libnsl since these are read-only operations.
*/
int *
getipsecprotos(int *nentries)
{
return (_real_getipsecprotos(nentries));
}
int *
getipsecalgs(int *nentries, int proto_num)
{
return (_real_getipsecalgs(nentries, proto_num));
}
const char *
ipsecalgs_diag(int diag)
{
switch (diag) {
case LIBIPSEC_ALGS_DIAG_ALG_EXISTS:
return (dgettext(TEXT_DOMAIN, "Algorithm already exists"));
case LIBIPSEC_ALGS_DIAG_PROTO_EXISTS:
return (dgettext(TEXT_DOMAIN, "Protocol already exists"));
case LIBIPSEC_ALGS_DIAG_UNKN_PROTO:
return (dgettext(TEXT_DOMAIN, "Unknown protocol"));
case LIBIPSEC_ALGS_DIAG_UNKN_ALG:
return (dgettext(TEXT_DOMAIN, "Unknown algorithm"));
case LIBIPSEC_ALGS_DIAG_NOMEM:
return (dgettext(TEXT_DOMAIN, "Out of memory"));
case LIBIPSEC_ALGS_DIAG_ALGSFILEOPEN:
return (dgettext(TEXT_DOMAIN, "open() failed"));
case LIBIPSEC_ALGS_DIAG_ALGSFILEFDOPEN:
return (dgettext(TEXT_DOMAIN, "fdopen() failed"));
case LIBIPSEC_ALGS_DIAG_ALGSFILELOCK:
return (dgettext(TEXT_DOMAIN, "lockf() failed"));
case LIBIPSEC_ALGS_DIAG_ALGSFILERENAME:
return (dgettext(TEXT_DOMAIN, "rename() failed"));
case LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE:
return (dgettext(TEXT_DOMAIN, "write to file failed"));
case LIBIPSEC_ALGS_DIAG_ALGSFILECHMOD:
return (dgettext(TEXT_DOMAIN, "chmod() failed"));
case LIBIPSEC_ALGS_DIAG_ALGSFILECHOWN:
return (dgettext(TEXT_DOMAIN, "chown() failed"));
case LIBIPSEC_ALGS_DIAG_ALGSFILECLOSE:
return (dgettext(TEXT_DOMAIN, "close() failed"));
default:
return (dgettext(TEXT_DOMAIN, "failed"));
}
}
/*
* Get the execution mode corresponding to the specified protocol.
* Returns 0 on success, one of the LIBIPSEC_ALGS_DIAG_* values on
* failure.
*/
int
ipsecproto_get_exec_mode(int proto_num, ipsecalgs_exec_mode_t *exec_mode)
{
ipsec_proto_t *protos, *current_proto;
int num_protos;
if ((current_proto = proto_setup(&protos, &num_protos, proto_num,
B_TRUE)) == NULL)
return (LIBIPSEC_ALGS_DIAG_UNKN_PROTO);
*exec_mode = current_proto->proto_exec_mode;
_clean_trash(protos, num_protos);
return (0);
}
/*
* Set the execution mode of the specified protocol. Returns 0 on success,
* or one of the LIBIPSEC_ALGS_DIAG_* values on failure.
*/
int
ipsecproto_set_exec_mode(int proto_num, ipsecalgs_exec_mode_t exec_mode)
{
ipsec_proto_t *protos, *current_proto;
int num_protos;
if ((current_proto = proto_setup(&protos, &num_protos, proto_num,
B_TRUE)) == NULL)
return (LIBIPSEC_ALGS_DIAG_UNKN_PROTO);
current_proto->proto_exec_mode = exec_mode;
return (write_new_algfile(protos, num_protos));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _ERRFP_H
#define _ERRFP_H
/*
* Headers and definitions for support functions that are shared by
* the ipsec utilities ipseckey and ikeadm.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdarg.h>
/*
* Function Prototypes
*/
#ifdef __PRAGMA_REDEFINE_EXTNAME
#pragma redefine_extname errfp _errfp
#pragma redefine_extname verrfp _verrfp
#pragma redefine_extname errxfp _errxfp
#pragma redefine_extname verrxfp _verrxfp
#pragma redefine_extname warnfp _warnfp
#pragma redefine_extname vwarnfp _vwarnfp
#pragma redefine_extname warnxfp _warnxfp
#pragma redefine_extname vwarnxfp _vwarnxfp
#else
#define errfp _errfp
#define verrfp _verrfp
#define errxfp _errxfp
#define verrxfp _verrxfp
#define warnfp _warnfp
#define vwarnfp _vwarnfp
#define warnxfp _warnxfp
#define vwarnxfp _vwarnxfp
#endif
/* Program exit and warning calls */
extern void errfp(FILE *, int, const char *, ...);
extern void verrfp(FILE *, int, const char *, va_list);
extern void errxfp(FILE *, int, const char *, ...);
extern void verrxfp(FILE *, int, const char *, va_list);
extern void warnfp(FILE *, const char *, ...);
extern void vwarnfp(FILE *, const char *, va_list);
extern void warnxfp(FILE *, const char *, ...);
extern void vwarnxfp(FILE *, const char *, va_list);
#ifdef __cplusplus
}
#endif
#endif /* _ERRFP_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#ifndef _IKEDOOR_H
#define _IKEDOOR_H
#ifdef __cplusplus
extern "C" {
#endif
#include <limits.h>
#include <sys/sysmacros.h>
#include <net/pfkeyv2.h>
#include <door.h>
/*
* This version number is intended to stop the calling process from
* getting confused if a structure is changed and a mismatch occurs.
* This should be incremented each time a structure is changed.
*/
/*
* The IKE process may be a 64-bit process, but ikeadm or any other IKE
* door consumer does not have to be. We need to be strict ala. PF_KEY or
* any on-the-wire-protocol with respect to structure fields offsets and
* alignment. Please make sure all structures are the same size on both
* 64-bit and 32-bit execution environments (or even other ones), and that
* apart from trivial 4-byte enums or base headers, that all structures are
* multiples of 8-bytes (64-bits).
*/
#define DOORVER 4
#define DOORNM "/var/run/ike_door"
typedef enum {
IKE_SVC_GET_DBG,
IKE_SVC_SET_DBG,
IKE_SVC_GET_PRIV,
IKE_SVC_SET_PRIV,
IKE_SVC_GET_STATS,
IKE_SVC_GET_P1,
IKE_SVC_DEL_P1,
IKE_SVC_DUMP_P1S,
IKE_SVC_FLUSH_P1S,
IKE_SVC_GET_RULE,
IKE_SVC_NEW_RULE,
IKE_SVC_DEL_RULE,
IKE_SVC_DUMP_RULES,
IKE_SVC_READ_RULES,
IKE_SVC_WRITE_RULES,
IKE_SVC_GET_PS,
IKE_SVC_NEW_PS,
IKE_SVC_DEL_PS,
IKE_SVC_DUMP_PS,
IKE_SVC_READ_PS,
IKE_SVC_WRITE_PS,
IKE_SVC_DBG_RBDUMP,
IKE_SVC_GET_DEFS,
IKE_SVC_SET_PIN,
IKE_SVC_DEL_PIN,
IKE_SVC_DUMP_CERTCACHE,
IKE_SVC_FLUSH_CERTCACHE,
IKE_SVC_DUMP_GROUPS,
IKE_SVC_DUMP_ENCRALGS,
IKE_SVC_DUMP_AUTHALGS,
IKE_SVC_ERROR
} ike_svccmd_t;
/* DPD status */
typedef enum dpd_status {
DPD_NOT_INITIATED = 0,
DPD_IN_PROGRESS,
DPD_SUCCESSFUL,
DPD_FAILURE
} dpd_status_t;
#define IKE_SVC_MAX IKE_SVC_ERROR
/*
* Support structures/defines
*/
#define IKEDOORROUNDUP(i) P2ROUNDUP((i), sizeof (uint64_t))
/*
* Debug categories. The debug level is a bitmask made up of
* flags indicating the desired categories; only 31 bits are
* available, as the highest-order bit designates an invalid
* setting.
*/
#define D_INVALID 0x80000000
#define D_CERT 0x00000001 /* certificate management */
#define D_KEY 0x00000002 /* key management */
#define D_OP 0x00000004 /* operational: config, init, mem */
#define D_P1 0x00000008 /* phase 1 negotiation */
#define D_P2 0x00000010 /* phase 2 negotiation */
#define D_PFKEY 0x00000020 /* pf key interface */
#define D_POL 0x00000040 /* policy management */
#define D_PROP 0x00000080 /* proposal construction */
#define D_DOOR 0x00000100 /* door server */
#define D_CONFIG 0x00000200 /* config file processing */
#define D_LABEL 0x00000400 /* MAC labels */
#define D_HIGHBIT 0x00000400
#define D_ALL 0x000007ff
/*
* Access privilege levels: define level of access to keying information.
* The privileges granted at each level is a superset of the privileges
* granted at all lower levels.
*
* The door operations which require special privileges are:
*
* - receiving keying material for SAs and preshared key entries
* IKE_PRIV_KEYMAT must be set for this.
*
* - get/dump/new/delete/read/write preshared keys
* IKE_PRIV_KEYMAT or IKE_PRIV_MODKEYS must be set to do this.
* If IKE_PRIV_MODKEYS is set, the information returned for a
* get/dump request will not include the actual key; in order
* to get the key itself, IKE_PRIV_KEYMAT must be set.
*
* - modifying the privilege level: the daemon's privilege level
* is set when the daemon is started; the level may only be
* lowered via the door interface.
*
* All other operations are allowed at any privilege level.
*/
#define IKE_PRIV_MINIMUM 0
#define IKE_PRIV_MODKEYS 1
#define IKE_PRIV_KEYMAT 2
#define IKE_PRIV_MAXIMUM 2
/* global ike stats formatting structure */
typedef struct {
uint32_t st_init_p1_current;
uint32_t st_resp_p1_current;
uint32_t st_init_p1_total;
uint32_t st_resp_p1_total;
uint32_t st_init_p1_attempts;
uint32_t st_resp_p1_attempts;
uint32_t st_init_p1_noresp; /* failed; no response from peer */
uint32_t st_init_p1_respfail; /* failed, but peer responded */
uint32_t st_resp_p1_fail;
uint32_t st_reserved;
char st_pkcs11_libname[PATH_MAX];
} ike_stats_t;
/* structure used to pass default values used by in.iked back to ikeadm */
typedef struct {
uint32_t rule_p1_lifetime_secs;
uint32_t rule_p1_minlife;
uint32_t rule_p1_nonce_len;
uint32_t rule_p2_lifetime_secs;
uint32_t rule_p2_softlife_secs;
uint32_t rule_p2_idletime_secs;
uint32_t sys_p2_lifetime_secs;
uint32_t sys_p2_softlife_secs;
uint32_t sys_p2_idletime_secs;
uint32_t rule_p2_lifetime_kb;
uint32_t rule_p2_softlife_kb;
uint32_t sys_p2_lifetime_bytes;
uint32_t sys_p2_softlife_bytes;
uint32_t rule_p2_minlife_hard_secs;
uint32_t rule_p2_minlife_soft_secs;
uint32_t rule_p2_minlife_idle_secs;
uint32_t rule_p2_minlife_hard_kb;
uint32_t rule_p2_minlife_soft_kb;
uint32_t rule_p2_maxlife_secs;
uint32_t rule_p2_maxlife_kb;
uint32_t rule_p2_nonce_len;
uint32_t rule_p2_pfs;
uint32_t rule_p2_mindiff_secs;
uint32_t rule_p2_mindiff_kb;
uint32_t conversion_factor; /* for secs to kbytes */
uint32_t rule_max_certs;
uint32_t rule_ike_port;
uint32_t rule_natt_port;
uint32_t defaults_reserved; /* For 64-bit alignment. */
} ike_defaults_t;
/* data formatting structures for P1 SA dumps */
typedef struct {
struct sockaddr_storage loc_addr;
struct sockaddr_storage rem_addr;
#define beg_iprange loc_addr
#define end_iprange rem_addr
} ike_addr_pr_t;
typedef struct {
uint64_t cky_i;
uint64_t cky_r;
} ike_cky_pr_t;
typedef struct {
ike_cky_pr_t p1hdr_cookies;
uint8_t p1hdr_major;
uint8_t p1hdr_minor;
uint8_t p1hdr_xchg;
uint8_t p1hdr_isinit;
uint32_t p1hdr_state;
boolean_t p1hdr_support_dpd;
dpd_status_t p1hdr_dpd_state;
uint64_t p1hdr_dpd_time;
} ike_p1_hdr_t;
/* values for p1hdr_xchg (aligned with RFC2408, section 3.1) */
#define IKE_XCHG_NONE 0
#define IKE_XCHG_BASE 1
#define IKE_XCHG_IDENTITY_PROTECT 2
#define IKE_XCHG_AUTH_ONLY 3
#define IKE_XCHG_AGGRESSIVE 4
/* following not from RFC; used only for preshared key definitions */
#define IKE_XCHG_IP_AND_AGGR 240
/* also not from RFC; used as wildcard */
#define IKE_XCHG_ANY 256
/* values for p1hdr_state */
#define IKE_SA_STATE_INVALID 0
#define IKE_SA_STATE_INIT 1
#define IKE_SA_STATE_SENT_SA 2
#define IKE_SA_STATE_SENT_KE 3
#define IKE_SA_STATE_SENT_LAST 4
#define IKE_SA_STATE_DONE 5
#define IKE_SA_STATE_DELETED 6
typedef struct {
uint16_t p1xf_dh_group;
uint16_t p1xf_encr_alg;
uint16_t p1xf_encr_low_bits;
uint16_t p1xf_encr_high_bits;
uint16_t p1xf_auth_alg;
uint16_t p1xf_auth_meth;
uint16_t p1xf_prf;
uint16_t p1xf_pfs;
uint32_t p1xf_max_secs;
uint32_t p1xf_max_kbytes;
uint32_t p1xf_max_keyuses;
uint32_t p1xf_reserved; /* Alignment to 64-bit. */
} ike_p1_xform_t;
/* values for p1xf_dh_group (aligned with RFC2409, Appendix A) */
#define IKE_GRP_DESC_MODP_768 1
#define IKE_GRP_DESC_MODP_1024 2
#define IKE_GRP_DESC_EC2N_155 3
#define IKE_GRP_DESC_EC2N_185 4
/* values for p1xf_dh_group (aligned with RFC3526) */
#define IKE_GRP_DESC_MODP_1536 5
#define IKE_GRP_DESC_MODP_2048 14
#define IKE_GRP_DESC_MODP_3072 15
#define IKE_GRP_DESC_MODP_4096 16
#define IKE_GRP_DESC_MODP_6144 17
#define IKE_GRP_DESC_MODP_8192 18
#define IKE_GRP_DESC_ECP_256 19
#define IKE_GRP_DESC_ECP_384 20
#define IKE_GRP_DESC_ECP_521 21
/* values for p1xf_dh_group (aligned with RFC5114) */
#define IKE_GRP_DESC_MODP_1024_160 22
#define IKE_GRP_DESC_MODP_2048_224 23
#define IKE_GRP_DESC_MODP_2048_256 24
#define IKE_GRP_DESC_ECP_192 25
#define IKE_GRP_DESC_ECP_224 26
/* values for p1xf_auth_meth (aligned with RFC2409, Appendix A) */
#define IKE_AUTH_METH_PRE_SHARED_KEY 1
#define IKE_AUTH_METH_DSS_SIG 2
#define IKE_AUTH_METH_RSA_SIG 3
#define IKE_AUTH_METH_RSA_ENCR 4
#define IKE_AUTH_METH_RSA_ENCR_REVISED 5
/* values for p1xf_prf */
#define IKE_PRF_NONE 0
#define IKE_PRF_HMAC_MD5 1
#define IKE_PRF_HMAC_SHA1 2
#define IKE_PRF_HMAC_SHA256 5
#define IKE_PRF_HMAC_SHA384 6
#define IKE_PRF_HMAC_SHA512 7
typedef struct {
/*
* NOTE: the new and del counters count the actual number of SAs,
* not the number of "suites", as defined in the ike monitoring
* mib draft; we do this because we don't have a good way of
* tracking the deletion of entire suites (we're notified of
* deleted qm sas individually).
*/
uint32_t p1stat_new_qm_sas;
uint32_t p1stat_del_qm_sas;
uint64_t p1stat_start;
uint32_t p1stat_kbytes;
uint32_t p1stat_keyuses;
} ike_p1_stats_t;
typedef struct {
uint32_t p1err_decrypt;
uint32_t p1err_hash;
uint32_t p1err_otherrx;
uint32_t p1err_tx;
} ike_p1_errors_t;
typedef struct {
uint32_t p1key_type;
uint32_t p1key_len;
/*
* followed by (len - sizeof (ike_p1_key_t)) bytes of hex data,
* 64-bit aligned (pad bytes are added at the end, if necessary,
* and NOT INCLUDED in the len value, which reflects the actual
* key size).
*/
} ike_p1_key_t;
/* key info types for ike_p1_key_t struct */
#define IKE_KEY_PRESHARED 1
#define IKE_KEY_SKEYID 2
#define IKE_KEY_SKEYID_D 3
#define IKE_KEY_SKEYID_A 4
#define IKE_KEY_SKEYID_E 5
#define IKE_KEY_ENCR 6
#define IKE_KEY_IV 7
typedef struct {
ike_p1_hdr_t p1sa_hdr;
ike_p1_xform_t p1sa_xform;
ike_addr_pr_t p1sa_ipaddrs;
uint16_t p1sa_stat_off;
uint16_t p1sa_stat_len;
uint16_t p1sa_error_off;
uint16_t p1sa_error_len;
uint16_t p1sa_localid_off;
uint16_t p1sa_localid_len;
uint16_t p1sa_remoteid_off;
uint16_t p1sa_remoteid_len;
uint16_t p1sa_key_off;
uint16_t p1sa_key_len;
uint32_t p1sa_reserved;
/*
* variable-length structures will be included here, as
* indicated by offset/length fields.
* stats and errors will be formatted as ike_p1_stats_t and
* ike_p1_errors_t, respectively.
* key info will be formatted as a series of p1_key_t structs.
* local/remote ids will be formatted as sadb_ident_t structs.
*/
} ike_p1_sa_t;
#define MAX_LABEL_LEN 256
/* data formatting structure for policy (rule) dumps */
typedef struct {
char rule_label[MAX_LABEL_LEN];
uint32_t rule_kmcookie;
uint16_t rule_ike_mode;
uint16_t rule_local_idtype; /* SADB_IDENTTYPE_* value */
uint32_t rule_p1_nonce_len;
uint32_t rule_p2_nonce_len;
uint32_t rule_p2_pfs;
uint32_t rule_p2_lifetime_secs;
uint32_t rule_p2_softlife_secs;
uint32_t rule_p2_idletime_secs;
uint32_t rule_p2_lifetime_kb;
uint32_t rule_p2_softlife_kb;
uint16_t rule_xform_cnt;
uint16_t rule_xform_off;
uint16_t rule_locip_cnt;
uint16_t rule_locip_off;
uint16_t rule_remip_cnt;
uint16_t rule_remip_off;
uint16_t rule_locid_inclcnt;
uint16_t rule_locid_exclcnt;
uint16_t rule_locid_off;
uint16_t rule_remid_inclcnt;
uint16_t rule_remid_exclcnt;
uint16_t rule_remid_off;
/*
* Followed by several lists of variable-length structures, described
* by counts and offsets:
* transforms ike_p1_xform_t structs
* ranges of local ip addrs ike_addr_pr_t structs
* ranges of remote ip addrs ike_addr_pr_t structs
* local identification strings null-terminated ascii strings
* remote identification strings null-terminated ascii strings
*/
} ike_rule_t;
/* data formatting structure for DH group dumps */
typedef struct {
uint16_t group_number;
uint16_t group_bits;
char group_label[MAX_LABEL_LEN];
} ike_group_t;
/* data formatting structure for encryption algorithm dumps */
typedef struct {
uint_t encr_value;
char encr_name[MAX_LABEL_LEN];
int encr_keylen_min;
int encr_keylen_max;
} ike_encralg_t;
/* data formatting structure for authentication algorithm dumps */
typedef struct {
uint_t auth_value;
char auth_name[MAX_LABEL_LEN];
} ike_authalg_t;
/*
* data formatting structure for preshared keys
* ps_ike_mode field uses the IKE_XCHG_* defs
*/
typedef struct {
ike_addr_pr_t ps_ipaddrs;
uint16_t ps_ike_mode;
uint16_t ps_localid_off;
uint16_t ps_localid_len;
uint16_t ps_remoteid_off;
uint16_t ps_remoteid_len;
uint16_t ps_key_off;
uint16_t ps_key_len;
uint16_t ps_key_bits;
int ps_localid_plen;
int ps_remoteid_plen;
/*
* followed by variable-length structures, as indicated by
* offset/length fields.
* key info will be formatted as an array of bytes.
* local/remote ids will be formatted as sadb_ident_t structs.
*/
} ike_ps_t;
#define DN_MAX 1024
#define CERT_OFF_WIRE -1
#define CERT_NO_PRIVKEY 0
#define CERT_PRIVKEY_LOCKED 1
#define CERT_PRIVKEY_AVAIL 2
/*
* data formatting structure for cached certs
*/
typedef struct {
uint32_t cache_id;
uint32_t certclass;
int linkage;
uint32_t certcache_padding; /* For 64-bit alignment. */
char subject[DN_MAX];
char issuer[DN_MAX];
} ike_certcache_t;
/* identification types */
#define IKE_ID_IDENT_PAIR 1
#define IKE_ID_ADDR_PAIR 2
#define IKE_ID_CKY_PAIR 3
#define IKE_ID_LABEL 4
/* locations for read/write requests */
#define IKE_RW_LOC_DEFAULT 1
#define IKE_RW_LOC_USER_SPEC 2
/* door interface error codes */
#define IKE_ERR_NO_OBJ 1 /* nothing found to match the request */
#define IKE_ERR_NO_DESC 2 /* fd was required with this request */
#define IKE_ERR_ID_INVALID 3 /* invalid id info was provided */
#define IKE_ERR_LOC_INVALID 4 /* invalid location info was provided */
#define IKE_ERR_CMD_INVALID 5 /* invalid command was provided */
#define IKE_ERR_DATA_INVALID 6 /* invalid data was provided */
#define IKE_ERR_CMD_NOTSUP 7 /* unsupported command */
#define IKE_ERR_REQ_INVALID 8 /* badly formatted request */
#define IKE_ERR_NO_PRIV 9 /* privilege level not high enough */
#define IKE_ERR_SYS_ERR 10 /* syserr occurred while processing */
#define IKE_ERR_DUP_IGNORED 11 /* attempt to add a duplicate entry */
#define IKE_ERR_NO_TOKEN 12 /* cannot login into pkcs#11 token */
#define IKE_ERR_NO_AUTH 13 /* not authorized */
#define IKE_ERR_IN_PROGRESS 14 /* operation already in progress */
#define IKE_ERR_NO_MEM 15 /* insufficient memory */
/*
* IKE_SVC_GET_DBG
* Used to request the current debug level.
*
* Upon request, dbg_level is 0 (don't care).
*
* Upon return, dbg_level contains the current value.
*
*
* IKE_SVC_SET_DBG
* Used to request modification of the debug level.
*
* Upon request, dbg_level contains desired level. If debug output is
* to be directed to a different file, the fd should be passed in the
* door_desc_t field of the door_arg_t param. NOTE: if the daemon is
* currently running in the background with no debug set, an output
* file MUST be given.
*
* Upon return, dbg_level contains the old debug level, and acknowledges
* successful completion of the request. If an error is encountered,
* ike_err_t is returned instead, with appropriate error value and cmd
* IKE_SVC_ERROR.
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t dbg_level;
} ike_dbg_t;
/*
* IKE_SVC_GET_PRIV
* Used to request the current privilege level.
*
* Upon request, priv_level is 0 (don't care).
*
* Upon return, priv_level contains the current value.
*
*
* IKE_SVC_SET_PRIV
* Used to request modification of the privilege level.
*
* Upon request, priv_level contains the desired level. The level may
* only be lowered via the door interface; it cannot be raised. Thus,
* if in.iked is started at the lowest level, it cannot be changed.
*
* Upon return, priv_level contains the old privilege level, and
* acknowledges successful completion of the request. If an error is
* encountered, ike_err_t is returned instead, with appropriate error
* value and cmd IKE_SVC_ERROR.
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t priv_level;
} ike_priv_t;
/*
* IKE_SVC_GET_STATS
* Used to request current statistics on Phase 1 SA creation and
* failures. The statistics represent all activity in in.iked.
*
* Upon request, cmd is set, and stat_len does not matter.
*
* Upon successful return, stat_len contains the total size of the
* returned buffer, which contains first the ike_statreq_t struct,
* followed by the stat data in the ike_stats_t structure. In case
* of an error in processing the request, ike_err_t is returned with
* IKE_SVC_ERROR command and appropriate error code.
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t stat_len;
} ike_statreq_t;
/*
* IKE_SVC_GET_DEFS
* Used to request default values from in.iked.
*
* Upon request, cmd is set, and stat_len does not matter.
*
* Upon successful return, stat_len contains the total size of the
* returned buffer, this contains a pair of ike_defaults_t's.
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t stat_len;
uint32_t version;
uint32_t defreq_reserved; /* For 64-bit alignment. */
} ike_defreq_t;
/*
* IKE_SVC_DUMP_{P1S|RULES|PS|CERTCACHE}
* Used to request a table dump, and to return info for a single table
* item. The expectation is that all of the table data will be passed
* through the door, one entry at a time; an individual request must be
* sent for each entry, however (the door server can't send unrequested
* data).
*
* Upon request: cmd is set, and dump_next contains the item number
* requested (0 for first request). dump_len is 0; no data follows.
*
* Upon return: cmd is set, and dump_next contains the item number of
* the *next* item in the table (to be used in the subsequent request).
* dump_next = 0 indicates that this is the last item in the table.
* dump_len is the total length (data + struct) returned. Data is
* formatted as indicated by the cmd type:
* IKE_SVC_DUMP_P1S: ike_p1_sa_t
* IKE_SVC_DUMP_RULES: ike_rule_t
* IKE_SVC_DUMP_PS: ike_ps_t
* IKE_SVC_DUMP_CERTCACHE: ike_certcache_t
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t dump_len;
union {
struct {
uint32_t dump_unext;
uint32_t dump_ureserved;
} dump_actual;
uint64_t dump_alignment;
} dump_u;
#define dump_next dump_u.dump_actual.dump_unext
#define dump_reserved dump_u.dump_actual.dump_ureserved
/* dump_len - sizeof (ike_dump_t) bytes of data included here */
} ike_dump_t;
/*
* IKE_SVC_GET_{P1|RULE|PS}
* Used to request and return individual table items.
*
* Upon request: get_len is the total msg length (struct + id data);
* get_idtype indicates the type of identification being used.
* IKE_SVC_GET_P1: ike_addr_pr_t or ike_cky_pr_t
* IKE_SVC_GET_RULE: char string (label)
* IKE_SVC_GET_PS: ike_addr_pr_t or pair of sadb_ident_t
*
* Upon return: get_len is the total size (struct + data), get_idtype
* is unused, and the data that follows is formatted according to cmd:
* IKE_SVC_GET_P1: ike_p1_sa_t
* IKE_SVC_GET_RULE: ike_rule_t
* IKE_SVC_GET_PS: ike_ps_t
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t get_len;
union {
struct {
uint32_t getu_idtype;
uint32_t getu_reserved;
} get_actual;
uint64_t get_alignment;
} get_u;
#define get_idtype get_u.get_actual.getu_idtype
#define get_reserved get_u.get_actual.getu_reserved
/* get_len - sizeof (ike_get_t) bytes of data included here */
} ike_get_t;
/*
* IKE_SVC_NEW_{RULE|PS}
* Used to request and acknowledge insertion of a table item.
*
* Upon request: new_len is the total (data + struct) size passed, or 0.
* new_len = 0 => a door_desc_t is also included with a file descriptor
* for a file containing the data to be added. The file should include
* a single item: a rule, or a pre-shared key. For new_len != 0, the
* data is formatted according to the cmd type:
* IKE_SVC_NEW_RULE: ike_rule_t
* IKE_SVC_NEW_PS: ike_ps_t
*
* Upon return: new_len is 0; simply acknowledges successful insertion
* of the requested item. If insertion is not successful, ike_err_t is
* returned instead with appropriate error value.
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t new_len;
/* new_len - sizeof (ike_new_t) bytes included here */
uint64_t new_align; /* Padding for 64-bit alignment. */
} ike_new_t;
/*
* IKE_SVC_DEL_{P1|RULE|PS}
* Used to request and acknowledge the deletion of an individual table
* item.
*
* Upon request: del_len is the total msg length (struct + id data);
* del_idtype indicates the type of identification being used.
* IKE_SVC_DEL_P1: ike_addr_pr_t or ike_cky_pr_t
* IKE_SVC_DEL_RULE: char string (label)
* IKE_SVC_DEL_PS: ike_addr_pr_t or pair of sadb_ident_t
*
* Upon return: acknowledges deletion of the requested item; del_len and
* del_idtype are unspecified. If deletion is not successful, ike_err_t
* is returned instead with appropriate error value.
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t del_len;
uint32_t del_idtype;
uint32_t del_reserved;
/* del_len - sizeof (ike_del_t) bytes of data included here. */
} ike_del_t;
/*
* IKE_SVC_READ_{RULES|PS}
* Used to ask daemon to re-read particular configuration info.
*
* Upon request: rw_loc indicates where the info should be read from:
* either from a user-supplied file descriptor(s), or from the default
* location(s). If rw_loc indicates user-supplied location, the file
* descriptor(s) should be passed in the door_desc_t struct. For the
* IKE_SVC_READ_RULES cmd, two file descriptors should be specified:
* first, one for the config file which contains the data to be read,
* and second, one for the cookie file which will be written to as
* in.iked process the config file.
*
* Upon return: rw_loc is unspecified; the message simply acknowledges
* successful completion of the request. If an error occurred,
* ike_err_t is returned instead with appropriate error value.
*
*
* IKE_SVC_WRITE_{RULES|PS}
* Used to ask daemon to write its current config info to files.
*
* Request and return are handled the same as for the IKE_SVC_READ_*
* cmds; however, the rw_loc MUST be a user-supplied location. Also,
* for the IKE_SVC_WRITE_RULES cmd, the cookie file fd is not required;
* only a single fd, for the file to which the config info should be
* written, should be passed in.
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t rw_loc;
} ike_rw_t;
/*
* IKE_SVC_FLUSH_P1S
* IKE_SVC_FLUSH_CERTCACHE
*
* Used to request and acknowledge tear-down of all P1 SAs
* or to flush the certificate cache.
*/
typedef struct {
ike_svccmd_t cmd;
} ike_flush_t;
#ifndef PKCS11_TOKSIZE
#define PKCS11_TOKSIZE 32
#endif
#define MAX_PIN_LEN 256
/*
* IKE_SVC_SET_PIN
* IKE_SVC_DEL_PIN
*
* Used to supply a pin for a PKCS#11 tokenj object.
*
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t pin_reserved; /* For 64-bit alignment. */
char pkcs11_token[PKCS11_TOKSIZE];
uchar_t token_pin[MAX_PIN_LEN];
} ike_pin_t;
/*
* IKE_SVC_ERROR
* Used on return if server encountered an error while processing
* the request. An appropriate error code is included (as defined
* in this header file); in the case of IKE_ERR_SYS_ERR, a value
* from the UNIX errno space is included in the ike_err_unix field.
*/
typedef struct {
ike_svccmd_t cmd;
uint32_t ike_err;
uint32_t ike_err_unix;
uint32_t ike_err_reserved;
} ike_err_t;
/*
* Generic type for use when the request/reply type is unknown
*/
typedef struct {
ike_svccmd_t cmd;
} ike_cmd_t;
/*
* Union containing all possible request/return structures.
*/
typedef union {
ike_cmd_t svc_cmd;
ike_dbg_t svc_dbg;
ike_priv_t svc_priv;
ike_statreq_t svc_stats;
ike_dump_t svc_dump;
ike_get_t svc_get;
ike_new_t svc_new;
ike_del_t svc_del;
ike_rw_t svc_rw;
ike_flush_t svc_flush;
ike_pin_t svc_pin;
ike_err_t svc_err;
ike_defreq_t svc_defaults;
} ike_service_t;
#ifdef __cplusplus
}
#endif
#endif /* _IKEDOOR_H */
/*
*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Juri. All rights reserved.
* Copyright 2018 Joyent, Inc.
* Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
* Copyright 2024 Oxide Computer Company
* Copyright 2026 Edgecast Cloud LLC.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/sysconf.h>
#include <strings.h>
#include <ctype.h>
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/pfkeyv2.h>
#include <net/pfpolicy.h>
#include <libintl.h>
#include <setjmp.h>
#include <libgen.h>
#include <libscf.h>
#include <kmfapi.h>
#include <ber_der.h>
#include "ipsec_util.h"
#include "ikedoor.h"
/*
* This file contains support functions that are shared by the ipsec
* utilities and daemons including ipseckey(8), ikeadm(8) and in.iked(8).
*/
#define EFD(file) (((file) == stdout) ? stderr : (file))
/* Limits for interactive mode. */
#define MAX_LINE_LEN IBUF_SIZE
#define MAX_CMD_HIST 64000 /* in bytes */
/* Set standard default/initial values for globals... */
boolean_t pflag = B_FALSE; /* paranoid w.r.t. printing keying material */
boolean_t nflag = B_FALSE; /* avoid nameservice? */
boolean_t interactive = B_FALSE; /* util not running on cmdline */
boolean_t readfile = B_FALSE; /* cmds are being read from a file */
uint_t lineno = 0; /* track location if reading cmds from file */
uint_t lines_added = 0;
uint_t lines_parsed = 0;
jmp_buf env; /* for error recovery in interactive/readfile modes */
char *my_fmri = NULL;
FILE *debugfile = stderr;
static GetLine *gl = NULL; /* for interactive mode */
/*
* Print errno and exit if cmdline or readfile, reset state if interactive
* The error string *what should be dgettext()'d before calling bail().
*/
void
bail(char *what)
{
if (errno != 0)
warn(what);
else
warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what);
if (readfile) {
return;
}
if (interactive && !readfile)
longjmp(env, 2);
EXIT_FATAL(NULL);
}
/*
* Print caller-supplied variable-arg error msg, then exit if cmdline or
* readfile, or reset state if interactive.
*/
/*PRINTFLIKE1*/
void
bail_msg(char *fmt, ...)
{
va_list ap;
char msgbuf[BUFSIZ];
va_start(ap, fmt);
(void) vsnprintf(msgbuf, BUFSIZ, fmt, ap);
va_end(ap);
if (readfile)
warnx(dgettext(TEXT_DOMAIN,
"ERROR on line %u:\n%s\n"), lineno, msgbuf);
else
warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf);
if (interactive && !readfile)
longjmp(env, 1);
EXIT_FATAL(NULL);
}
/*
* bytecnt2str() wrapper. Zeroes out the input buffer and if the number
* of bytes to be converted is more than 1K, it will produce readable string
* in parentheses, store it in the original buffer and return the pointer to it.
* Maximum length of the returned string is 14 characters (not including
* the terminating zero).
*/
char *
bytecnt2out(uint64_t num, char *buf, size_t bufsiz, int flags)
{
char *str;
(void) memset(buf, '\0', bufsiz);
if (num > 1024) {
/* Return empty string in case of out-of-memory. */
if ((str = malloc(bufsiz)) == NULL)
return (buf);
(void) bytecnt2str(num, str, bufsiz);
/* Detect overflow. */
if (strlen(str) == 0) {
free(str);
return (buf);
}
/* Emit nothing in case of overflow. */
if (snprintf(buf, bufsiz, "%s(%sB)%s",
flags & SPC_BEGIN ? " " : "", str,
flags & SPC_END ? " " : "") >= bufsiz)
(void) memset(buf, '\0', bufsiz);
free(str);
}
return (buf);
}
/*
* Convert 64-bit number to human readable string. Useful mainly for the
* byte lifetime counters. Returns pointer to the user supplied buffer.
* Able to convert up to Exabytes. Maximum length of the string produced
* is 9 characters (not counting the terminating zero).
*/
char *
bytecnt2str(uint64_t num, char *buf, size_t buflen)
{
uint64_t n = num;
char u;
int index = 0;
while (n >= 1024) {
n /= 1024;
index++;
}
/* The field has all units this function can represent. */
u = " KMGTPE"[index];
if (index == 0) {
/* Less than 1K */
if (snprintf(buf, buflen, "%llu ", num) >= buflen)
(void) memset(buf, '\0', buflen);
} else {
/* Otherwise display 2 precision digits. */
if (snprintf(buf, buflen, "%.2f %c",
(double)num / (1ULL << index * 10), u) >= buflen)
(void) memset(buf, '\0', buflen);
}
return (buf);
}
/*
* secs2str() wrapper. Zeroes out the input buffer and if the number of
* seconds to be converted is more than minute, it will produce readable
* string in parentheses, store it in the original buffer and return the
* pointer to it.
*/
char *
secs2out(unsigned int secs, char *buf, int bufsiz, int flags)
{
char *str;
(void) memset(buf, '\0', bufsiz);
if (secs > 60) {
/* Return empty string in case of out-of-memory. */
if ((str = malloc(bufsiz)) == NULL)
return (buf);
(void) secs2str(secs, str, bufsiz);
/* Detect overflow. */
if (strlen(str) == 0) {
free(str);
return (buf);
}
/* Emit nothing in case of overflow. */
if (snprintf(buf, bufsiz, "%s(%s)%s",
flags & SPC_BEGIN ? " " : "", str,
flags & SPC_END ? " " : "") >= bufsiz)
(void) memset(buf, '\0', bufsiz);
free(str);
}
return (buf);
}
/*
* Convert number of seconds to human readable string. Useful mainly for
* the lifetime counters. Returns pointer to the user supplied buffer.
* Able to convert up to days.
*/
char *
secs2str(unsigned int secs, char *buf, int bufsiz)
{
double val = secs;
char *unit = "second";
if (val >= 24*60*60) {
val /= 86400;
unit = "day";
} else if (val >= 60*60) {
val /= 60*60;
unit = "hour";
} else if (val >= 60) {
val /= 60;
unit = "minute";
}
/* Emit nothing in case of overflow. */
if (snprintf(buf, bufsiz, "%.2f %s%s", val, unit,
val >= 2 ? "s" : "") >= bufsiz)
(void) memset(buf, '\0', bufsiz);
return (buf);
}
/*
* dump_XXX functions produce ASCII output from various structures.
*
* Because certain errors need to do this to stderr, dump_XXX functions
* take a FILE pointer.
*
* If an error occured while writing to the specified file, these
* functions return -1, zero otherwise.
*/
int
dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only,
FILE *where, boolean_t ignore_nss)
{
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
char *printable_addr, *protocol;
uint8_t *addrptr;
/* Add 4 chars to hold '/nnn' for prefixes. */
char storage[INET6_ADDRSTRLEN + 4];
uint16_t port;
boolean_t unspec;
struct hostent *hp;
int getipnode_errno, addrlen;
switch (sa->sa_family) {
case AF_INET:
/* LINTED E_BAD_PTR_CAST_ALIGN */
sin = (struct sockaddr_in *)sa;
addrptr = (uint8_t *)&sin->sin_addr;
port = sin->sin_port;
protocol = "AF_INET";
unspec = (sin->sin_addr.s_addr == 0);
addrlen = sizeof (sin->sin_addr);
break;
case AF_INET6:
/* LINTED E_BAD_PTR_CAST_ALIGN */
sin6 = (struct sockaddr_in6 *)sa;
addrptr = (uint8_t *)&sin6->sin6_addr;
port = sin6->sin6_port;
protocol = "AF_INET6";
unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr);
addrlen = sizeof (sin6->sin6_addr);
break;
default:
return (0);
}
if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) ==
NULL) {
printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address.");
} else {
char prefix[5]; /* "/nnn" with terminator. */
(void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen);
printable_addr = storage;
if (prefixlen != 0) {
(void) strlcat(printable_addr, prefix,
sizeof (storage));
}
}
if (addr_only) {
if (fprintf(where, "%s", printable_addr) < 0)
return (-1);
} else {
if (fprintf(where, dgettext(TEXT_DOMAIN,
"%s: port %d, %s"), protocol,
ntohs(port), printable_addr) < 0)
return (-1);
if (ignore_nss == B_FALSE) {
/*
* Do AF_independent reverse hostname lookup here.
*/
if (unspec) {
if (fprintf(where,
dgettext(TEXT_DOMAIN,
" <unspecified>")) < 0)
return (-1);
} else {
hp = getipnodebyaddr((char *)addrptr, addrlen,
sa->sa_family, &getipnode_errno);
if (hp != NULL) {
if (fprintf(where,
" (%s)", hp->h_name) < 0)
return (-1);
freehostent(hp);
} else {
if (fprintf(where,
dgettext(TEXT_DOMAIN,
" <unknown>")) < 0)
return (-1);
}
}
}
if (fputs(".\n", where) == EOF)
return (-1);
}
return (0);
}
/*
* Dump a key, any salt and bitlen.
* The key is made up of a stream of bits. If the algorithm requires a salt
* value, this will also be part of the dumped key. The last "saltbits" of the
* key string, reading left to right will be the salt value. To make it easier
* to see which bits make up the key, the salt value is enclosed in []'s.
* This function can also be called when ipseckey(8) -s is run, this "saves"
* the SAs, including the key to a file. When this is the case, the []'s are
* not printed.
*
* The implementation allows the kernel to be told about the length of the salt
* in whole bytes only. If this changes, this function will need to be updated.
*/
int
dump_key(uint8_t *keyp, uint_t bitlen, uint_t saltbits, FILE *where,
boolean_t separate_salt)
{
int numbytes, saltbytes;
numbytes = SADB_1TO8(bitlen);
saltbytes = SADB_1TO8(saltbits);
numbytes += saltbytes;
/* The & 0x7 is to check for leftover bits. */
if ((bitlen & 0x7) != 0)
numbytes++;
while (numbytes-- != 0) {
if (pflag) {
/* Print no keys if paranoid */
if (fprintf(where, "XX") < 0)
return (-1);
} else {
if (fprintf(where, "%02x", *keyp++) < 0)
return (-1);
}
if (separate_salt && saltbytes != 0 &&
numbytes == saltbytes) {
if (fprintf(where, "[") < 0)
return (-1);
}
}
if (separate_salt && saltbits != 0) {
if (fprintf(where, "]/%u+%u", bitlen, saltbits) < 0)
return (-1);
} else {
if (fprintf(where, "/%u", bitlen + saltbits) < 0)
return (-1);
}
return (0);
}
int
dump_keystr(uint8_t *keystr, uint_t bitlen, FILE *where)
{
size_t keylen;
uint_t i;
/* There should be no leftover bits for a key string */
if ((bitlen & 0x7) != 0)
return (-1);
keylen = SADB_1TO8(bitlen);
for (i = 0; i < keylen; i++) {
if (pflag)
(void) fprintf(where, "XX");
else if (isprint(keystr[i]))
(void) fprintf(where, "%c", keystr[i]);
else
(void) fprintf(where, "\\x%x", keystr[i]);
}
return (0);
}
static struct tcpsigalg {
uint8_t ta_value;
const char *ta_name;
} tcpalgs[] = {
{ .ta_name = "md5", .ta_value = SADB_AALG_MD5 }
};
uint8_t
gettcpsigalgbyname(const char *name)
{
uint_t i;
for (i = 0; i < ARRAY_SIZE(tcpalgs); i++) {
if (strcmp(name, tcpalgs[i].ta_name) == 0)
return (tcpalgs[i].ta_value);
}
return (0);
}
const char *
gettcpsigalgbynum(uint8_t num)
{
uint_t i;
for (i = 0; i < ARRAY_SIZE(tcpalgs); i++) {
if (num == tcpalgs[i].ta_value)
return (tcpalgs[i].ta_name);
}
return (NULL);
}
/*
* Print an authentication or encryption algorithm
*/
static int
dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where)
{
struct ipsecalgent *alg;
alg = getipsecalgbynum(alg_num, proto_num, NULL);
if (alg == NULL) {
if (fprintf(where, dgettext(TEXT_DOMAIN,
"<unknown %u>"), alg_num) < 0)
return (-1);
return (0);
}
/*
* Special-case <none> for backward output compat.
* Assume that SADB_AALG_NONE == SADB_EALG_NONE.
*/
if (alg_num == SADB_AALG_NONE) {
if (fputs(dgettext(TEXT_DOMAIN,
"<none>"), where) == EOF)
return (-1);
} else {
if (fputs(alg->a_names[0], where) == EOF)
return (-1);
}
freeipsecalgent(alg);
return (0);
}
int
dump_aalg(uint8_t aalg, FILE *where)
{
return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where));
}
int
dump_ealg(uint8_t ealg, FILE *where)
{
return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where));
}
int
dump_tcpsigalg(uint8_t aalg, FILE *where)
{
const char *name = gettcpsigalgbynum(aalg);
if (name == NULL) {
if (fprintf(where, dgettext(TEXT_DOMAIN,
"<unknown %u>"), aalg) < 0) {
return (-1);
}
return (0);
}
if (fputs(name, where) == EOF)
return (-1);
return (0);
}
/*
* Print an SADB_IDENTTYPE string
*
* Also return TRUE if the actual ident may be printed, FALSE if not.
*
* If rc is not NULL, set its value to -1 if an error occured while writing
* to the specified file, zero otherwise.
*/
boolean_t
dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc)
{
boolean_t canprint = B_TRUE;
int rc_val = 0;
switch (idtype) {
case SADB_IDENTTYPE_PREFIX:
if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF)
rc_val = -1;
break;
case SADB_IDENTTYPE_FQDN:
if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF)
rc_val = -1;
break;
case SADB_IDENTTYPE_USER_FQDN:
if (fputs(dgettext(TEXT_DOMAIN,
"user-FQDN (mbox)"), where) == EOF)
rc_val = -1;
break;
case SADB_X_IDENTTYPE_DN:
if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"),
where) == EOF)
rc_val = -1;
canprint = B_FALSE;
break;
case SADB_X_IDENTTYPE_GN:
if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"),
where) == EOF)
rc_val = -1;
canprint = B_FALSE;
break;
case SADB_X_IDENTTYPE_KEY_ID:
if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"),
where) == EOF)
rc_val = -1;
break;
case SADB_X_IDENTTYPE_ADDR_RANGE:
if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF)
rc_val = -1;
break;
default:
if (fprintf(where, dgettext(TEXT_DOMAIN,
"<unknown %u>"), idtype) < 0)
rc_val = -1;
break;
}
if (rc != NULL)
*rc = rc_val;
return (canprint);
}
/*
* Slice an argv/argc vector from an interactive line or a read-file line.
*/
static int
create_argv(char *ibuf, int *newargc, char ***thisargv)
{
unsigned int argvlen = START_ARG;
char **current;
boolean_t firstchar = B_TRUE;
boolean_t inquotes = B_FALSE;
*thisargv = malloc(sizeof (char *) * argvlen);
if ((*thisargv) == NULL)
return (MEMORY_ALLOCATION);
current = *thisargv;
*current = NULL;
for (; *ibuf != '\0'; ibuf++) {
if (isspace(*ibuf)) {
if (inquotes) {
continue;
}
if (*current != NULL) {
*ibuf = '\0';
current++;
if (*thisargv + argvlen == current) {
/* Regrow ***thisargv. */
if (argvlen == TOO_MANY_ARGS) {
free(*thisargv);
return (TOO_MANY_TOKENS);
}
/* Double the allocation. */
current = realloc(*thisargv,
sizeof (char *) * (argvlen << 1));
if (current == NULL) {
free(*thisargv);
return (MEMORY_ALLOCATION);
}
*thisargv = current;
current += argvlen;
argvlen <<= 1; /* Double the size. */
}
*current = NULL;
}
} else {
if (firstchar) {
firstchar = B_FALSE;
if (*ibuf == COMMENT_CHAR || *ibuf == '\n') {
free(*thisargv);
return (COMMENT_LINE);
}
}
if (*ibuf == QUOTE_CHAR) {
if (inquotes) {
inquotes = B_FALSE;
*ibuf = '\0';
} else {
inquotes = B_TRUE;
}
continue;
}
if (*current == NULL) {
*current = ibuf;
(*newargc)++;
}
}
}
/*
* Tricky corner case...
* I've parsed _exactly_ the amount of args as I have space. It
* won't return NULL-terminated, and bad things will happen to
* the caller.
*/
if (argvlen == *newargc) {
current = realloc(*thisargv, sizeof (char *) * (argvlen + 1));
if (current == NULL) {
free(*thisargv);
return (MEMORY_ALLOCATION);
}
*thisargv = current;
current[argvlen] = NULL;
}
return (SUCCESS);
}
/*
* init interactive mode if needed and not yet initialized
*/
static void
init_interactive(FILE *infile, CplMatchFn *match_fn)
{
if (infile == stdin) {
if (gl == NULL) {
if ((gl = new_GetLine(MAX_LINE_LEN,
MAX_CMD_HIST)) == NULL)
errx(1, dgettext(TEXT_DOMAIN,
"tecla initialization failed"));
if (gl_customize_completion(gl, NULL,
match_fn) != 0) {
(void) del_GetLine(gl);
errx(1, dgettext(TEXT_DOMAIN,
"tab completion failed to initialize"));
}
/*
* In interactive mode we only want to terminate
* when explicitly requested (e.g. by a command).
*/
(void) sigset(SIGINT, SIG_IGN);
}
} else {
readfile = B_TRUE;
}
}
/*
* free tecla data structure
*/
static void
fini_interactive(void)
{
if (gl != NULL)
(void) del_GetLine(gl);
}
/*
* Get single input line, wrapping around interactive and non-interactive
* mode.
*/
static char *
do_getstr(FILE *infile, char *prompt, char *ibuf, size_t ibuf_size)
{
char *line;
if (infile != stdin)
return (fgets(ibuf, ibuf_size, infile));
/*
* If the user hits ^C then we want to catch it and
* start over. If the user hits EOF then we want to
* bail out.
*/
once_again:
line = gl_get_line(gl, prompt, NULL, -1);
if (gl_return_status(gl) == GLR_SIGNAL) {
gl_abandon_line(gl);
goto once_again;
} else if (gl_return_status(gl) == GLR_ERROR) {
gl_abandon_line(gl);
errx(1, dgettext(TEXT_DOMAIN, "Error reading terminal: %s\n"),
gl_error_message(gl, NULL, 0));
} else {
if (line != NULL) {
if (strlcpy(ibuf, line, ibuf_size) >= ibuf_size)
warnx(dgettext(TEXT_DOMAIN,
"Line too long (max=%zu chars)"),
ibuf_size);
line = ibuf;
}
}
return (line);
}
/*
* Enter a mode where commands are read from a file. Treat stdin special.
*/
void
do_interactive(FILE *infile, char *configfile, char *promptstring,
char *my_fmri, parse_cmdln_fn parseit, CplMatchFn *match_fn)
{
char ibuf[IBUF_SIZE], holder[IBUF_SIZE];
char *volatile hptr, **thisargv, *ebuf;
int thisargc;
volatile boolean_t continue_in_progress = B_FALSE;
char *s;
(void) setjmp(env);
ebuf = NULL;
interactive = B_TRUE;
bzero(ibuf, IBUF_SIZE);
/* panics for us */
init_interactive(infile, match_fn);
while ((s = do_getstr(infile, promptstring, ibuf, IBUF_SIZE)) != NULL) {
if (readfile)
lineno++;
thisargc = 0;
thisargv = NULL;
/*
* Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will
* be null-terminated because of fgets().
*/
if (ibuf[IBUF_SIZE - 2] != '\0') {
if (infile == stdin) {
/* do_getstr() issued a warning already */
bzero(ibuf, IBUF_SIZE);
continue;
} else {
ipsecutil_exit(SERVICE_FATAL, my_fmri,
debugfile, dgettext(TEXT_DOMAIN,
"Line %d too big."), lineno);
}
}
if (!continue_in_progress) {
/* Use -2 because of \n from fgets. */
if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) {
/*
* Can use strcpy here, I've checked the
* length already.
*/
(void) strcpy(holder, ibuf);
hptr = &(holder[strlen(holder)]);
/* Remove the CONT_CHAR from the string. */
hptr[-2] = ' ';
continue_in_progress = B_TRUE;
bzero(ibuf, IBUF_SIZE);
continue;
}
} else {
/* Handle continuations... */
(void) strncpy(hptr, ibuf,
(size_t)(&(holder[IBUF_SIZE]) - hptr));
if (holder[IBUF_SIZE - 1] != '\0') {
ipsecutil_exit(SERVICE_FATAL, my_fmri,
debugfile, dgettext(TEXT_DOMAIN,
"Command buffer overrun."));
}
/* Use - 2 because of \n from fgets. */
if (hptr[strlen(hptr) - 2] == CONT_CHAR) {
bzero(ibuf, IBUF_SIZE);
hptr += strlen(hptr);
/* Remove the CONT_CHAR from the string. */
hptr[-2] = ' ';
continue;
} else {
continue_in_progress = B_FALSE;
/*
* I've already checked the length...
*/
(void) strcpy(ibuf, holder);
}
}
/*
* Just in case the command fails keep a copy of the
* command buffer for diagnostic output.
*/
if (readfile) {
/*
* The error buffer needs to be big enough to
* hold the longest command string, plus
* some extra text, see below.
*/
ebuf = calloc((IBUF_SIZE * 2), sizeof (char));
if (ebuf == NULL) {
ipsecutil_exit(SERVICE_FATAL, my_fmri,
debugfile, dgettext(TEXT_DOMAIN,
"Memory allocation error."));
} else {
(void) snprintf(ebuf, (IBUF_SIZE * 2),
dgettext(TEXT_DOMAIN,
"Config file entry near line %u "
"caused error(s) or warnings:\n\n%s\n\n"),
lineno, ibuf);
}
}
switch (create_argv(ibuf, &thisargc, &thisargv)) {
case TOO_MANY_TOKENS:
ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
dgettext(TEXT_DOMAIN, "Too many input tokens."));
break;
case MEMORY_ALLOCATION:
ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
dgettext(TEXT_DOMAIN, "Memory allocation error."));
break;
case COMMENT_LINE:
/* Comment line. */
free(ebuf);
break;
default:
if (thisargc != 0) {
lines_parsed++;
/* ebuf consumed */
parseit(thisargc, thisargv, ebuf, readfile);
} else {
free(ebuf);
}
free(thisargv);
if (infile == stdin) {
(void) printf("%s", promptstring);
(void) fflush(stdout);
}
break;
}
bzero(ibuf, IBUF_SIZE);
}
/*
* The following code is ipseckey specific. This should never be
* used by ikeadm which also calls this function because ikeadm
* only runs interactively. If this ever changes this code block
* sould be revisited.
*/
if (readfile) {
if (lines_parsed != 0 && lines_added == 0) {
ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
dgettext(TEXT_DOMAIN, "Configuration file did not "
"contain any valid SAs"));
}
/*
* There were errors. Putting the service in maintenance mode.
* When svc.startd(8) allows services to degrade themselves,
* this should be revisited.
*
* If this function was called from a program running as a
* smf_method(7), print a warning message. Don't spew out the
* errors as these will end up in the smf(7) log file which is
* publically readable, the errors may contain sensitive
* information.
*/
if ((lines_added < lines_parsed) && (configfile != NULL)) {
if (my_fmri != NULL) {
ipsecutil_exit(SERVICE_BADCONF, my_fmri,
debugfile, dgettext(TEXT_DOMAIN,
"The configuration file contained %d "
"errors.\n"
"Manually check the configuration with:\n"
"ipseckey -c %s\n"
"Use svcadm(8) to clear maintenance "
"condition when errors are resolved.\n"),
lines_parsed - lines_added, configfile);
} else {
EXIT_BADCONFIG(NULL);
}
} else {
if (my_fmri != NULL)
ipsecutil_exit(SERVICE_EXIT_OK, my_fmri,
debugfile, dgettext(TEXT_DOMAIN,
"%d actions successfully processed."),
lines_added);
}
} else {
/* no newline upon Ctrl-D */
if (s != NULL)
(void) putchar('\n');
(void) fflush(stdout);
}
fini_interactive();
EXIT_OK(NULL);
}
/*
* Functions to parse strings that represent a debug or privilege level.
* These functions are copied from main.c and door.c in usr.lib/in.iked/common.
* If this file evolves into a common library that may be used by in.iked
* as well as the usr.sbin utilities, those duplicate functions should be
* deleted.
*
* A privilege level may be represented by a simple keyword, corresponding
* to one of the possible levels. A debug level may be represented by a
* series of keywords, separated by '+' or '-', indicating categories to
* be added or removed from the set of categories in the debug level.
* For example, +all-op corresponds to level 0xfffffffb (all flags except
* for D_OP set); while p1+p2+pfkey corresponds to level 0x38. Note that
* the leading '+' is implicit; the first keyword in the list must be for
* a category that is to be added.
*
* These parsing functions make use of a local version of strtok, strtok_d,
* which includes an additional parameter, char *delim. This param is filled
* in with the character which ends the returned token. In other words,
* this version of strtok, in addition to returning the token, also returns
* the single character delimiter from the original string which marked the
* end of the token.
*/
static char *
strtok_d(char *string, const char *sepset, char *delim)
{
static char *lasts;
char *q, *r;
/* first or subsequent call */
if (string == NULL)
string = lasts;
if (string == 0) /* return if no tokens remaining */
return (NULL);
q = string + strspn(string, sepset); /* skip leading separators */
if (*q == '\0') /* return if no tokens remaining */
return (NULL);
if ((r = strpbrk(q, sepset)) == NULL) { /* move past token */
lasts = 0; /* indicate that this is last token */
} else {
*delim = *r; /* save delimitor */
*r = '\0';
lasts = r + 1;
}
return (q);
}
static keywdtab_t privtab[] = {
{ IKE_PRIV_MINIMUM, "base" },
{ IKE_PRIV_MODKEYS, "modkeys" },
{ IKE_PRIV_KEYMAT, "keymat" },
{ IKE_PRIV_MINIMUM, "0" },
};
int
privstr2num(char *str)
{
keywdtab_t *pp;
char *endp;
int priv;
for (pp = privtab; pp < A_END(privtab); pp++) {
if (strcasecmp(str, pp->kw_str) == 0)
return (pp->kw_tag);
}
priv = strtol(str, &endp, 0);
if (*endp == '\0')
return (priv);
return (-1);
}
static keywdtab_t dbgtab[] = {
{ D_CERT, "cert" },
{ D_KEY, "key" },
{ D_OP, "op" },
{ D_P1, "p1" },
{ D_P1, "phase1" },
{ D_P2, "p2" },
{ D_P2, "phase2" },
{ D_PFKEY, "pfkey" },
{ D_POL, "pol" },
{ D_POL, "policy" },
{ D_PROP, "prop" },
{ D_DOOR, "door" },
{ D_CONFIG, "config" },
{ D_LABEL, "label" },
{ D_ALL, "all" },
{ 0, "0" },
};
int
dbgstr2num(char *str)
{
keywdtab_t *dp;
for (dp = dbgtab; dp < A_END(dbgtab); dp++) {
if (strcasecmp(str, dp->kw_str) == 0)
return (dp->kw_tag);
}
return (D_INVALID);
}
int
parsedbgopts(char *optarg)
{
char *argp, *endp, op, nextop;
int mask = 0, new;
mask = strtol(optarg, &endp, 0);
if (*endp == '\0')
return (mask);
op = optarg[0];
if (op != '-')
op = '+';
argp = strtok_d(optarg, "+-", &nextop);
do {
new = dbgstr2num(argp);
if (new == D_INVALID) {
/* we encountered an invalid keywd */
return (new);
}
if (op == '+') {
mask |= new;
} else {
mask &= ~new;
}
op = nextop;
} while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL);
return (mask);
}
/*
* functions to manipulate the kmcookie-label mapping file
*/
/*
* Open, lockf, fdopen the given file, returning a FILE * on success,
* or NULL on failure.
*/
FILE *
kmc_open_and_lock(char *name)
{
int fd, rtnerr;
FILE *fp;
if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
return (NULL);
}
if (lockf(fd, F_LOCK, 0) < 0) {
return (NULL);
}
if ((fp = fdopen(fd, "a+")) == NULL) {
return (NULL);
}
if (fseek(fp, 0, SEEK_SET) < 0) {
/* save errno in case fclose changes it */
rtnerr = errno;
(void) fclose(fp);
errno = rtnerr;
return (NULL);
}
return (fp);
}
/*
* Extract an integer cookie and string label from a line from the
* kmcookie-label file. Return -1 on failure, 0 on success.
*/
int
kmc_parse_line(char *line, int *cookie, char **label)
{
char *cookiestr;
*cookie = 0;
*label = NULL;
cookiestr = strtok(line, " \t\n");
if (cookiestr == NULL) {
return (-1);
}
/* Everything that follows, up to the newline, is the label. */
*label = strtok(NULL, "\n");
if (*label == NULL) {
return (-1);
}
*cookie = atoi(cookiestr);
return (0);
}
/*
* Insert a mapping into the file (if it's not already there), given the
* new label. Return the assigned cookie, or -1 on error.
*/
int
kmc_insert_mapping(char *label)
{
FILE *map;
char linebuf[IBUF_SIZE];
char *cur_label;
int max_cookie = 0, cur_cookie, rtn_cookie;
int rtnerr = 0;
boolean_t found = B_FALSE;
/* open and lock the file; will sleep until lock is available */
if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
/* kmc_open_and_lock() sets errno appropriately */
return (-1);
}
while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
/* Skip blank lines, which often come near EOF. */
if (strlen(linebuf) == 0)
continue;
if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
rtnerr = EINVAL;
goto error;
}
if (cur_cookie > max_cookie)
max_cookie = cur_cookie;
if ((!found) && (strcmp(cur_label, label) == 0)) {
found = B_TRUE;
rtn_cookie = cur_cookie;
}
}
if (!found) {
rtn_cookie = ++max_cookie;
if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) ||
(fflush(map) < 0)) {
rtnerr = errno;
goto error;
}
}
(void) fclose(map);
return (rtn_cookie);
error:
(void) fclose(map);
errno = rtnerr;
return (-1);
}
/*
* Lookup the given cookie and return its corresponding label. Return
* a pointer to the label on success, NULL on error (or if the label is
* not found). Note that the returned label pointer points to a static
* string, so the label will be overwritten by a subsequent call to the
* function; the function is also not thread-safe as a result.
*
* Because this is possibly publically exported, do not change its name,
* but this is for all intents and purposes an IKEv1/in.iked function.
*/
char *
kmc_lookup_by_cookie(int cookie)
{
FILE *map;
static char linebuf[IBUF_SIZE];
char *cur_label;
int cur_cookie;
if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
return (NULL);
}
while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
(void) fclose(map);
return (NULL);
}
if (cookie == cur_cookie) {
(void) fclose(map);
return (cur_label);
}
}
(void) fclose(map);
return (NULL);
}
/*
* Parse basic extension headers and return in the passed-in pointer vector.
* Return values include:
*
* KGE_OK Everything's nice and parsed out.
* If there are no extensions, place NULL in extv[0].
* KGE_DUP There is a duplicate extension.
* First instance in appropriate bin. First duplicate in
* extv[0].
* KGE_UNK Unknown extension type encountered. extv[0] contains
* unknown header.
* KGE_LEN Extension length error.
* KGE_CHK High-level reality check failed on specific extension.
*
* My apologies for some of the pointer arithmetic in here. I'm thinking
* like an assembly programmer, yet trying to make the compiler happy.
*/
int
spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize,
char *diag_buf, uint_t diag_buf_len)
{
int i;
if (diag_buf != NULL)
diag_buf[0] = '\0';
for (i = 1; i <= SPD_EXT_MAX; i++)
extv[i] = NULL;
i = 0;
/* Use extv[0] as the "current working pointer". */
extv[0] = (spd_ext_t *)(basehdr + 1);
msgsize = SPD_64TO8(msgsize);
while ((char *)extv[0] < ((char *)basehdr + msgsize)) {
/* Check for unknown headers. */
i++;
if (extv[0]->spd_ext_type == 0 ||
extv[0]->spd_ext_type > SPD_EXT_MAX) {
if (diag_buf != NULL) {
(void) snprintf(diag_buf, diag_buf_len,
"spdsock ext 0x%X unknown: 0x%X",
i, extv[0]->spd_ext_type);
}
return (KGE_UNK);
}
/*
* Check length. Use uint64_t because extlen is in units
* of 64-bit words. If length goes beyond the msgsize,
* return an error. (Zero length also qualifies here.)
*/
if (extv[0]->spd_ext_len == 0 ||
(uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
(uint8_t *)((uint8_t *)basehdr + msgsize))
return (KGE_LEN);
/* Check for redundant headers. */
if (extv[extv[0]->spd_ext_type] != NULL)
return (KGE_DUP);
/* If I make it here, assign the appropriate bin. */
extv[extv[0]->spd_ext_type] = extv[0];
/* Advance pointer (See above for uint64_t ptr reasoning.) */
extv[0] = (spd_ext_t *)
((uint64_t *)extv[0] + extv[0]->spd_ext_len);
}
/* Everything's cool. */
/*
* If extv[0] == NULL, then there are no extension headers in this
* message. Ensure that this is the case.
*/
if (extv[0] == (spd_ext_t *)(basehdr + 1))
extv[0] = NULL;
return (KGE_OK);
}
const char *
spdsock_diag(int diagnostic)
{
switch (diagnostic) {
case SPD_DIAGNOSTIC_NONE:
return (dgettext(TEXT_DOMAIN, "no error"));
case SPD_DIAGNOSTIC_UNKNOWN_EXT:
return (dgettext(TEXT_DOMAIN, "unknown extension"));
case SPD_DIAGNOSTIC_BAD_EXTLEN:
return (dgettext(TEXT_DOMAIN, "bad extension length"));
case SPD_DIAGNOSTIC_NO_RULE_EXT:
return (dgettext(TEXT_DOMAIN, "no rule extension"));
case SPD_DIAGNOSTIC_BAD_ADDR_LEN:
return (dgettext(TEXT_DOMAIN, "bad address len"));
case SPD_DIAGNOSTIC_MIXED_AF:
return (dgettext(TEXT_DOMAIN, "mixed address family"));
case SPD_DIAGNOSTIC_ADD_NO_MEM:
return (dgettext(TEXT_DOMAIN, "add: no memory"));
case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT:
return (dgettext(TEXT_DOMAIN, "add: wrong action count"));
case SPD_DIAGNOSTIC_ADD_BAD_TYPE:
return (dgettext(TEXT_DOMAIN, "add: bad type"));
case SPD_DIAGNOSTIC_ADD_BAD_FLAGS:
return (dgettext(TEXT_DOMAIN, "add: bad flags"));
case SPD_DIAGNOSTIC_ADD_INCON_FLAGS:
return (dgettext(TEXT_DOMAIN, "add: inconsistent flags"));
case SPD_DIAGNOSTIC_MALFORMED_LCLPORT:
return (dgettext(TEXT_DOMAIN, "malformed local port"));
case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT:
return (dgettext(TEXT_DOMAIN, "duplicate local port"));
case SPD_DIAGNOSTIC_MALFORMED_REMPORT:
return (dgettext(TEXT_DOMAIN, "malformed remote port"));
case SPD_DIAGNOSTIC_DUPLICATE_REMPORT:
return (dgettext(TEXT_DOMAIN, "duplicate remote port"));
case SPD_DIAGNOSTIC_MALFORMED_PROTO:
return (dgettext(TEXT_DOMAIN, "malformed proto"));
case SPD_DIAGNOSTIC_DUPLICATE_PROTO:
return (dgettext(TEXT_DOMAIN, "duplicate proto"));
case SPD_DIAGNOSTIC_MALFORMED_LCLADDR:
return (dgettext(TEXT_DOMAIN, "malformed local address"));
case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR:
return (dgettext(TEXT_DOMAIN, "duplicate local address"));
case SPD_DIAGNOSTIC_MALFORMED_REMADDR:
return (dgettext(TEXT_DOMAIN, "malformed remote address"));
case SPD_DIAGNOSTIC_DUPLICATE_REMADDR:
return (dgettext(TEXT_DOMAIN, "duplicate remote address"));
case SPD_DIAGNOSTIC_MALFORMED_ACTION:
return (dgettext(TEXT_DOMAIN, "malformed action"));
case SPD_DIAGNOSTIC_DUPLICATE_ACTION:
return (dgettext(TEXT_DOMAIN, "duplicate action"));
case SPD_DIAGNOSTIC_MALFORMED_RULE:
return (dgettext(TEXT_DOMAIN, "malformed rule"));
case SPD_DIAGNOSTIC_DUPLICATE_RULE:
return (dgettext(TEXT_DOMAIN, "duplicate rule"));
case SPD_DIAGNOSTIC_MALFORMED_RULESET:
return (dgettext(TEXT_DOMAIN, "malformed ruleset"));
case SPD_DIAGNOSTIC_DUPLICATE_RULESET:
return (dgettext(TEXT_DOMAIN, "duplicate ruleset"));
case SPD_DIAGNOSTIC_INVALID_RULE_INDEX:
return (dgettext(TEXT_DOMAIN, "invalid rule index"));
case SPD_DIAGNOSTIC_BAD_SPDID:
return (dgettext(TEXT_DOMAIN, "bad spdid"));
case SPD_DIAGNOSTIC_BAD_MSG_TYPE:
return (dgettext(TEXT_DOMAIN, "bad message type"));
case SPD_DIAGNOSTIC_UNSUPP_AH_ALG:
return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm"));
case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG:
return (dgettext(TEXT_DOMAIN,
"unsupported ESP encryption algorithm"));
case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG:
return (dgettext(TEXT_DOMAIN,
"unsupported ESP authentication algorithm"));
case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE:
return (dgettext(TEXT_DOMAIN, "unsupported AH key size"));
case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE:
return (dgettext(TEXT_DOMAIN,
"unsupported ESP encryption key size"));
case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE:
return (dgettext(TEXT_DOMAIN,
"unsupported ESP authentication key size"));
case SPD_DIAGNOSTIC_NO_ACTION_EXT:
return (dgettext(TEXT_DOMAIN, "No ACTION extension"));
case SPD_DIAGNOSTIC_ALG_ID_RANGE:
return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer"));
case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES:
return (dgettext(TEXT_DOMAIN,
"number of key sizes inconsistent"));
case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES:
return (dgettext(TEXT_DOMAIN,
"number of block sizes inconsistent"));
case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN:
return (dgettext(TEXT_DOMAIN, "invalid mechanism name length"));
case SPD_DIAGNOSTIC_NOT_GLOBAL_OP:
return (dgettext(TEXT_DOMAIN,
"operation not applicable to all policies"));
case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS:
return (dgettext(TEXT_DOMAIN,
"using selectors on a transport-mode tunnel"));
default:
return (dgettext(TEXT_DOMAIN, "unknown diagnostic"));
}
}
/*
* PF_KEY Diagnostic table.
*
* PF_KEY NOTE: If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is
* where you need to add new messages.
*/
const char *
keysock_diag(int diagnostic)
{
switch (diagnostic) {
case SADB_X_DIAGNOSTIC_NONE:
return (dgettext(TEXT_DOMAIN, "No diagnostic"));
case SADB_X_DIAGNOSTIC_UNKNOWN_MSG:
return (dgettext(TEXT_DOMAIN, "Unknown message type"));
case SADB_X_DIAGNOSTIC_UNKNOWN_EXT:
return (dgettext(TEXT_DOMAIN, "Unknown extension type"));
case SADB_X_DIAGNOSTIC_BAD_EXTLEN:
return (dgettext(TEXT_DOMAIN, "Bad extension length"));
case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE:
return (dgettext(TEXT_DOMAIN,
"Unknown Security Association type"));
case SADB_X_DIAGNOSTIC_SATYPE_NEEDED:
return (dgettext(TEXT_DOMAIN,
"Specific Security Association type needed"));
case SADB_X_DIAGNOSTIC_NO_SADBS:
return (dgettext(TEXT_DOMAIN,
"No Security Association Databases present"));
case SADB_X_DIAGNOSTIC_NO_EXT:
return (dgettext(TEXT_DOMAIN,
"No extensions needed for message"));
case SADB_X_DIAGNOSTIC_BAD_SRC_AF:
return (dgettext(TEXT_DOMAIN, "Bad source address family"));
case SADB_X_DIAGNOSTIC_BAD_DST_AF:
return (dgettext(TEXT_DOMAIN,
"Bad destination address family"));
case SADB_X_DIAGNOSTIC_BAD_PROXY_AF:
return (dgettext(TEXT_DOMAIN,
"Bad inner-source address family"));
case SADB_X_DIAGNOSTIC_AF_MISMATCH:
return (dgettext(TEXT_DOMAIN,
"Source/destination address family mismatch"));
case SADB_X_DIAGNOSTIC_BAD_SRC:
return (dgettext(TEXT_DOMAIN, "Bad source address value"));
case SADB_X_DIAGNOSTIC_BAD_DST:
return (dgettext(TEXT_DOMAIN, "Bad destination address value"));
case SADB_X_DIAGNOSTIC_ALLOC_HSERR:
return (dgettext(TEXT_DOMAIN,
"Soft allocations limit more than hard limit"));
case SADB_X_DIAGNOSTIC_BYTES_HSERR:
return (dgettext(TEXT_DOMAIN,
"Soft bytes limit more than hard limit"));
case SADB_X_DIAGNOSTIC_ADDTIME_HSERR:
return (dgettext(TEXT_DOMAIN, "Soft add expiration time later "
"than hard expiration time"));
case SADB_X_DIAGNOSTIC_USETIME_HSERR:
return (dgettext(TEXT_DOMAIN, "Soft use expiration time later "
"than hard expiration time"));
case SADB_X_DIAGNOSTIC_MISSING_SRC:
return (dgettext(TEXT_DOMAIN, "Missing source address"));
case SADB_X_DIAGNOSTIC_MISSING_DST:
return (dgettext(TEXT_DOMAIN, "Missing destination address"));
case SADB_X_DIAGNOSTIC_MISSING_SA:
return (dgettext(TEXT_DOMAIN, "Missing SA extension"));
case SADB_X_DIAGNOSTIC_MISSING_EKEY:
return (dgettext(TEXT_DOMAIN, "Missing encryption key"));
case SADB_X_DIAGNOSTIC_MISSING_AKEY:
return (dgettext(TEXT_DOMAIN, "Missing authentication key"));
case SADB_X_DIAGNOSTIC_MISSING_RANGE:
return (dgettext(TEXT_DOMAIN, "Missing SPI range"));
case SADB_X_DIAGNOSTIC_DUPLICATE_SRC:
return (dgettext(TEXT_DOMAIN, "Duplicate source address"));
case SADB_X_DIAGNOSTIC_DUPLICATE_DST:
return (dgettext(TEXT_DOMAIN, "Duplicate destination address"));
case SADB_X_DIAGNOSTIC_DUPLICATE_SA:
return (dgettext(TEXT_DOMAIN, "Duplicate SA extension"));
case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY:
return (dgettext(TEXT_DOMAIN, "Duplicate encryption key"));
case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY:
return (dgettext(TEXT_DOMAIN, "Duplicate authentication key"));
case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE:
return (dgettext(TEXT_DOMAIN, "Duplicate SPI range"));
case SADB_X_DIAGNOSTIC_MALFORMED_SRC:
return (dgettext(TEXT_DOMAIN, "Malformed source address"));
case SADB_X_DIAGNOSTIC_MALFORMED_DST:
return (dgettext(TEXT_DOMAIN, "Malformed destination address"));
case SADB_X_DIAGNOSTIC_MALFORMED_SA:
return (dgettext(TEXT_DOMAIN, "Malformed SA extension"));
case SADB_X_DIAGNOSTIC_MALFORMED_EKEY:
return (dgettext(TEXT_DOMAIN, "Malformed encryption key"));
case SADB_X_DIAGNOSTIC_MALFORMED_AKEY:
return (dgettext(TEXT_DOMAIN, "Malformed authentication key"));
case SADB_X_DIAGNOSTIC_MALFORMED_RANGE:
return (dgettext(TEXT_DOMAIN, "Malformed SPI range"));
case SADB_X_DIAGNOSTIC_AKEY_PRESENT:
return (dgettext(TEXT_DOMAIN, "Authentication key not needed"));
case SADB_X_DIAGNOSTIC_EKEY_PRESENT:
return (dgettext(TEXT_DOMAIN, "Encryption key not needed"));
case SADB_X_DIAGNOSTIC_PROP_PRESENT:
return (dgettext(TEXT_DOMAIN, "Proposal extension not needed"));
case SADB_X_DIAGNOSTIC_SUPP_PRESENT:
return (dgettext(TEXT_DOMAIN,
"Supported algorithms extension not needed"));
case SADB_X_DIAGNOSTIC_BAD_AALG:
return (dgettext(TEXT_DOMAIN,
"Unsupported authentication algorithm"));
case SADB_X_DIAGNOSTIC_BAD_EALG:
return (dgettext(TEXT_DOMAIN,
"Unsupported encryption algorithm"));
case SADB_X_DIAGNOSTIC_BAD_SAFLAGS:
return (dgettext(TEXT_DOMAIN, "Invalid SA flags"));
case SADB_X_DIAGNOSTIC_BAD_SASTATE:
return (dgettext(TEXT_DOMAIN, "Invalid SA state"));
case SADB_X_DIAGNOSTIC_BAD_AKEYBITS:
return (dgettext(TEXT_DOMAIN,
"Bad number of authentication bits"));
case SADB_X_DIAGNOSTIC_BAD_EKEYBITS:
return (dgettext(TEXT_DOMAIN,
"Bad number of encryption bits"));
case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP:
return (dgettext(TEXT_DOMAIN,
"Encryption not supported for this SA type"));
case SADB_X_DIAGNOSTIC_WEAK_EKEY:
return (dgettext(TEXT_DOMAIN, "Weak encryption key"));
case SADB_X_DIAGNOSTIC_WEAK_AKEY:
return (dgettext(TEXT_DOMAIN, "Weak authentication key"));
case SADB_X_DIAGNOSTIC_DUPLICATE_KMP:
return (dgettext(TEXT_DOMAIN,
"Duplicate key management protocol"));
case SADB_X_DIAGNOSTIC_DUPLICATE_KMC:
return (dgettext(TEXT_DOMAIN,
"Duplicate key management cookie"));
case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC:
return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address"));
case SADB_X_DIAGNOSTIC_MISSING_NATT_REM:
return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address"));
case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC:
return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address"));
case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM:
return (dgettext(TEXT_DOMAIN,
"Duplicate NAT-T remote address"));
case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC:
return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address"));
case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM:
return (dgettext(TEXT_DOMAIN,
"Malformed NAT-T remote address"));
case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS:
return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports"));
case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC:
return (dgettext(TEXT_DOMAIN, "Missing inner source address"));
case SADB_X_DIAGNOSTIC_MISSING_INNER_DST:
return (dgettext(TEXT_DOMAIN,
"Missing inner destination address"));
case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC:
return (dgettext(TEXT_DOMAIN,
"Duplicate inner source address"));
case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST:
return (dgettext(TEXT_DOMAIN,
"Duplicate inner destination address"));
case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC:
return (dgettext(TEXT_DOMAIN,
"Malformed inner source address"));
case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST:
return (dgettext(TEXT_DOMAIN,
"Malformed inner destination address"));
case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC:
return (dgettext(TEXT_DOMAIN,
"Invalid inner-source prefix length "));
case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST:
return (dgettext(TEXT_DOMAIN,
"Invalid inner-destination prefix length"));
case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF:
return (dgettext(TEXT_DOMAIN,
"Bad inner-destination address family"));
case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH:
return (dgettext(TEXT_DOMAIN,
"Inner source/destination address family mismatch"));
case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF:
return (dgettext(TEXT_DOMAIN,
"Bad NAT-T remote address family"));
case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF:
return (dgettext(TEXT_DOMAIN,
"Bad NAT-T local address family"));
case SADB_X_DIAGNOSTIC_PROTO_MISMATCH:
return (dgettext(TEXT_DOMAIN,
"Source/desination protocol mismatch"));
case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH:
return (dgettext(TEXT_DOMAIN,
"Inner source/desination protocol mismatch"));
case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS:
return (dgettext(TEXT_DOMAIN,
"Both inner ports and outer ports are set"));
case SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE:
return (dgettext(TEXT_DOMAIN,
"Pairing failed, target SA unsuitable for pairing"));
case SADB_X_DIAGNOSTIC_PAIR_ADD_MISMATCH:
return (dgettext(TEXT_DOMAIN,
"Source/destination address differs from pair SA"));
case SADB_X_DIAGNOSTIC_PAIR_ALREADY:
return (dgettext(TEXT_DOMAIN,
"Already paired with another security association"));
case SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND:
return (dgettext(TEXT_DOMAIN,
"Command failed, pair security association not found"));
case SADB_X_DIAGNOSTIC_BAD_SA_DIRECTION:
return (dgettext(TEXT_DOMAIN,
"Inappropriate SA direction"));
case SADB_X_DIAGNOSTIC_SA_NOTFOUND:
return (dgettext(TEXT_DOMAIN,
"Security association not found"));
case SADB_X_DIAGNOSTIC_SA_EXPIRED:
return (dgettext(TEXT_DOMAIN,
"Security association is not valid"));
case SADB_X_DIAGNOSTIC_BAD_CTX:
return (dgettext(TEXT_DOMAIN,
"Algorithm invalid or not supported by Crypto Framework"));
case SADB_X_DIAGNOSTIC_INVALID_REPLAY:
return (dgettext(TEXT_DOMAIN,
"Invalid Replay counter"));
case SADB_X_DIAGNOSTIC_MISSING_LIFETIME:
return (dgettext(TEXT_DOMAIN,
"Inappropriate lifetimes"));
case SADB_X_DIAGNOSTIC_MISSING_ASTR:
return (dgettext(TEXT_DOMAIN, "Missing authentication string"));
case SADB_X_DIAGNOSTIC_DUPLICATE_ASTR:
return (dgettext(TEXT_DOMAIN,
"Duplicate authentication string"));
case SADB_X_DIAGNOSTIC_MALFORMED_ASTR:
return (dgettext(TEXT_DOMAIN,
"Malformed authentication string"));
default:
return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code"));
}
}
/*
* Convert an IPv6 mask to a prefix len. I assume all IPv6 masks are
* contiguous, so I stop at the first zero bit!
*/
int
in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped)
{
int rc = 0;
uint8_t last;
int limit = IPV6_ABITS;
if (is_v4mapped) {
mask += ((IPV6_ABITS - IP_ABITS)/8);
limit = IP_ABITS;
}
while (*mask == 0xff) {
rc += 8;
if (rc == limit)
return (limit);
mask++;
}
last = *mask;
while (last != 0) {
rc++;
last = (last << 1) & 0xff;
}
return (rc);
}
/*
* Expand the diagnostic code into a message.
*/
void
print_diagnostic(FILE *file, uint16_t diagnostic)
{
/* Use two spaces so above strings can fit on the line. */
(void) fprintf(file, dgettext(TEXT_DOMAIN,
" Diagnostic code %u: %s.\n"),
diagnostic, keysock_diag(diagnostic));
}
/*
* Prints the base PF_KEY message.
*/
void
print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock,
boolean_t vflag)
{
if (wallclock != 0)
printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
"%sTimestamp: %s\n"), "", NULL,
vflag);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Base message (version %u) type "),
samsg->sadb_msg_version);
switch (samsg->sadb_msg_type) {
case SADB_RESERVED:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"RESERVED (warning: set to 0)"));
break;
case SADB_GETSPI:
(void) fprintf(file, "GETSPI");
break;
case SADB_UPDATE:
(void) fprintf(file, "UPDATE");
break;
case SADB_X_UPDATEPAIR:
(void) fprintf(file, "UPDATE PAIR");
break;
case SADB_ADD:
(void) fprintf(file, "ADD");
break;
case SADB_DELETE:
(void) fprintf(file, "DELETE");
break;
case SADB_X_DELPAIR:
(void) fprintf(file, "DELETE PAIR");
break;
case SADB_GET:
(void) fprintf(file, "GET");
break;
case SADB_ACQUIRE:
(void) fprintf(file, "ACQUIRE");
break;
case SADB_REGISTER:
(void) fprintf(file, "REGISTER");
break;
case SADB_EXPIRE:
(void) fprintf(file, "EXPIRE");
break;
case SADB_FLUSH:
(void) fprintf(file, "FLUSH");
break;
case SADB_DUMP:
(void) fprintf(file, "DUMP");
break;
case SADB_X_PROMISC:
(void) fprintf(file, "X_PROMISC");
break;
case SADB_X_INVERSE_ACQUIRE:
(void) fprintf(file, "X_INVERSE_ACQUIRE");
break;
default:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Unknown (%u)"), samsg->sadb_msg_type);
break;
}
(void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type "));
switch (samsg->sadb_msg_satype) {
case SADB_SATYPE_UNSPEC:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"<unspecified/all>"));
break;
case SADB_SATYPE_AH:
(void) fprintf(file, "AH");
break;
case SADB_SATYPE_ESP:
(void) fprintf(file, "ESP");
break;
case SADB_X_SATYPE_TCPSIG:
(void) fprintf(file, "TCPSIG");
break;
case SADB_SATYPE_RSVP:
(void) fprintf(file, "RSVP");
break;
case SADB_SATYPE_OSPFV2:
(void) fprintf(file, "OSPFv2");
break;
case SADB_SATYPE_RIPV2:
(void) fprintf(file, "RIPv2");
break;
case SADB_SATYPE_MIP:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP"));
break;
default:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"<unknown %u>"), samsg->sadb_msg_satype);
break;
}
(void) fprintf(file, ".\n");
if (samsg->sadb_msg_errno != 0) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Error %s from PF_KEY.\n"),
strerror(samsg->sadb_msg_errno));
print_diagnostic(file, samsg->sadb_x_msg_diagnostic);
}
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Message length %u bytes, seq=%u, pid=%u.\n"),
SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq,
samsg->sadb_msg_pid);
}
/*
* Print the SA extension for PF_KEY.
*/
void
print_sa(FILE *file, char *prefix, struct sadb_sa *assoc)
{
if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) {
warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
"WARNING: SA info extension length (%u) is bad."),
SADB_64TO8(assoc->sadb_sa_len));
}
if ((assoc->sadb_sa_flags & SADB_X_SAFLAGS_TCPSIG) == 0) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sSADB_ASSOC spi=0x%x, replay window size=%u, state="),
prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay);
} else {
(void) fprintf(file,
dgettext(TEXT_DOMAIN, "%sSADB_ASSOC state="), prefix);
}
switch (assoc->sadb_sa_state) {
case SADB_SASTATE_LARVAL:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL"));
break;
case SADB_SASTATE_MATURE:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE"));
break;
case SADB_SASTATE_DYING:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING"));
break;
case SADB_SASTATE_DEAD:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD"));
break;
case SADB_X_SASTATE_ACTIVE_ELSEWHERE:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"ACTIVE_ELSEWHERE"));
break;
case SADB_X_SASTATE_IDLE:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "IDLE"));
break;
default:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"<unknown %u>"), assoc->sadb_sa_state);
}
if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"\n%sAuthentication algorithm = "),
prefix);
if ((assoc->sadb_sa_flags & SADB_X_SAFLAGS_TCPSIG) != 0)
(void) dump_tcpsigalg(assoc->sadb_sa_auth, file);
else
(void) dump_aalg(assoc->sadb_sa_auth, file);
}
if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"\n%sEncryption algorithm = "), prefix);
(void) dump_ealg(assoc->sadb_sa_encrypt, file);
}
(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix,
assoc->sadb_sa_flags);
if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS)
(void) fprintf(file, "PFS ");
if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY)
(void) fprintf(file, "NOREPLAY ");
/* BEGIN Solaris-specific flags. */
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED)
(void) fprintf(file, "X_USED ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_PAIRED)
(void) fprintf(file, "X_PAIRED ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_OUTBOUND)
(void) fprintf(file, "X_OUTBOUND ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_INBOUND)
(void) fprintf(file, "X_INBOUND ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE)
(void) fprintf(file, "X_UNIQUE ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1)
(void) fprintf(file, "X_AALG1 ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2)
(void) fprintf(file, "X_AALG2 ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1)
(void) fprintf(file, "X_EALG1 ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2)
(void) fprintf(file, "X_EALG2 ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC)
(void) fprintf(file, "X_NATT_LOC ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM)
(void) fprintf(file, "X_NATT_REM ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
(void) fprintf(file, "X_TUNNEL ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATTED)
(void) fprintf(file, "X_NATTED ");
if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TCPSIG)
(void) fprintf(file, "X_TCPSIG ");
/* END Solaris-specific flags. */
(void) fprintf(file, ">\n");
}
void
printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx,
const char *pfx2, boolean_t vflag)
{
char tbuf[TBUF_SIZE]; /* For strftime() call. */
const char *tp = tbuf;
time_t t = lt;
struct tm res;
if (t != lt) {
if (lt > 0)
t = LONG_MAX;
else
t = LONG_MIN;
}
if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0)
tp = dgettext(TEXT_DOMAIN, "<time conversion failed>");
(void) fprintf(file, msg, pfx, tp);
if (vflag && (pfx2 != NULL))
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s\t(raw time value %" PRIu64 ")\n"), pfx2, lt);
}
/*
* Print the SA lifetime information. (An SADB_EXT_LIFETIME_* extension.)
*/
void
print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current,
struct sadb_lifetime *hard, struct sadb_lifetime *soft,
struct sadb_lifetime *idle, boolean_t vflag)
{
int64_t scratch;
char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: ");
char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: ");
char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: ");
char *idle_prefix = dgettext(TEXT_DOMAIN, "ILT: ");
char byte_str[BYTE_STR_SIZE]; /* byte lifetime string representation */
char secs_str[SECS_STR_SIZE]; /* buffer for seconds representation */
if (current != NULL &&
current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) {
warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
"WARNING: CURRENT lifetime extension length (%u) is bad."),
SADB_64TO8(current->sadb_lifetime_len));
}
if (hard != NULL &&
hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) {
warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
"WARNING: HARD lifetime extension length (%u) is bad."),
SADB_64TO8(hard->sadb_lifetime_len));
}
if (soft != NULL &&
soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) {
warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
"WARNING: SOFT lifetime extension length (%u) is bad."),
SADB_64TO8(soft->sadb_lifetime_len));
}
if (idle != NULL &&
idle->sadb_lifetime_len != SADB_8TO64(sizeof (*idle))) {
warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
"WARNING: IDLE lifetime extension length (%u) is bad."),
SADB_64TO8(idle->sadb_lifetime_len));
}
(void) fprintf(file, " LT: Lifetime information\n");
if (current != NULL) {
/* Express values as current values. */
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sCurrent lifetime information:\n"),
current_prefix);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " bytes %sprotected, %u allocations "
"used.\n"), current_prefix,
current->sadb_lifetime_bytes,
bytecnt2out(current->sadb_lifetime_bytes, byte_str,
sizeof (byte_str), SPC_END),
current->sadb_lifetime_allocations);
printsatime(file, current->sadb_lifetime_addtime,
dgettext(TEXT_DOMAIN, "%sSA added at time: %s\n"),
current_prefix, current_prefix, vflag);
if (current->sadb_lifetime_usetime != 0) {
printsatime(file, current->sadb_lifetime_usetime,
dgettext(TEXT_DOMAIN,
"%sSA first used at time %s\n"),
current_prefix, current_prefix, vflag);
}
printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
"%sTime now is %s\n"), current_prefix, current_prefix,
vflag);
}
if (soft != NULL) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sSoft lifetime information:\n"),
soft_prefix);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " bytes %sof lifetime, %u allocations.\n"),
soft_prefix,
soft->sadb_lifetime_bytes,
bytecnt2out(soft->sadb_lifetime_bytes, byte_str,
sizeof (byte_str), SPC_END),
soft->sadb_lifetime_allocations);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
soft_prefix, soft->sadb_lifetime_addtime,
secs2out(soft->sadb_lifetime_addtime, secs_str,
sizeof (secs_str), SPC_END));
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
soft_prefix, soft->sadb_lifetime_usetime,
secs2out(soft->sadb_lifetime_usetime, secs_str,
sizeof (secs_str), SPC_END));
/* If possible, express values as time remaining. */
if (current != NULL) {
if (soft->sadb_lifetime_bytes != 0)
(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s"
"%" PRIu64 " bytes %smore can be "
"protected.\n"), soft_prefix,
(soft->sadb_lifetime_bytes >
current->sadb_lifetime_bytes) ?
soft->sadb_lifetime_bytes -
current->sadb_lifetime_bytes : 0,
(soft->sadb_lifetime_bytes >
current->sadb_lifetime_bytes) ?
bytecnt2out(soft->sadb_lifetime_bytes -
current->sadb_lifetime_bytes, byte_str,
sizeof (byte_str), SPC_END) : "");
if (soft->sadb_lifetime_addtime != 0 ||
(soft->sadb_lifetime_usetime != 0 &&
current->sadb_lifetime_usetime != 0)) {
int64_t adddelta, usedelta;
if (soft->sadb_lifetime_addtime != 0) {
adddelta =
current->sadb_lifetime_addtime +
soft->sadb_lifetime_addtime -
wallclock;
} else {
adddelta = TIME_MAX;
}
if (soft->sadb_lifetime_usetime != 0 &&
current->sadb_lifetime_usetime != 0) {
usedelta =
current->sadb_lifetime_usetime +
soft->sadb_lifetime_usetime -
wallclock;
} else {
usedelta = TIME_MAX;
}
(void) fprintf(file, "%s", soft_prefix);
scratch = MIN(adddelta, usedelta);
if (scratch >= 0) {
(void) fprintf(file,
dgettext(TEXT_DOMAIN,
"Soft expiration occurs in %"
PRId64 " seconds%s\n"), scratch,
secs2out(scratch, secs_str,
sizeof (secs_str), SPC_BEGIN));
} else {
(void) fprintf(file,
dgettext(TEXT_DOMAIN,
"Soft expiration occurred\n"));
}
scratch += wallclock;
printsatime(file, scratch, dgettext(TEXT_DOMAIN,
"%sTime of expiration: %s.\n"),
soft_prefix, soft_prefix, vflag);
}
}
}
if (hard != NULL) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sHard lifetime information:\n"), hard_prefix);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " bytes %sof lifetime, %u allocations.\n"),
hard_prefix,
hard->sadb_lifetime_bytes,
bytecnt2out(hard->sadb_lifetime_bytes, byte_str,
sizeof (byte_str), SPC_END),
hard->sadb_lifetime_allocations);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
hard_prefix, hard->sadb_lifetime_addtime,
secs2out(hard->sadb_lifetime_addtime, secs_str,
sizeof (secs_str), SPC_END));
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
hard_prefix, hard->sadb_lifetime_usetime,
secs2out(hard->sadb_lifetime_usetime, secs_str,
sizeof (secs_str), SPC_END));
/* If possible, express values as time remaining. */
if (current != NULL) {
if (hard->sadb_lifetime_bytes != 0)
(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s"
"%" PRIu64 " bytes %smore can be "
"protected.\n"), hard_prefix,
(hard->sadb_lifetime_bytes >
current->sadb_lifetime_bytes) ?
hard->sadb_lifetime_bytes -
current->sadb_lifetime_bytes : 0,
(hard->sadb_lifetime_bytes >
current->sadb_lifetime_bytes) ?
bytecnt2out(hard->sadb_lifetime_bytes -
current->sadb_lifetime_bytes, byte_str,
sizeof (byte_str), SPC_END) : "");
if (hard->sadb_lifetime_addtime != 0 ||
(hard->sadb_lifetime_usetime != 0 &&
current->sadb_lifetime_usetime != 0)) {
int64_t adddelta, usedelta;
if (hard->sadb_lifetime_addtime != 0) {
adddelta =
current->sadb_lifetime_addtime +
hard->sadb_lifetime_addtime -
wallclock;
} else {
adddelta = TIME_MAX;
}
if (hard->sadb_lifetime_usetime != 0 &&
current->sadb_lifetime_usetime != 0) {
usedelta =
current->sadb_lifetime_usetime +
hard->sadb_lifetime_usetime -
wallclock;
} else {
usedelta = TIME_MAX;
}
(void) fprintf(file, "%s", hard_prefix);
scratch = MIN(adddelta, usedelta);
if (scratch >= 0) {
(void) fprintf(file,
dgettext(TEXT_DOMAIN,
"Hard expiration occurs in %"
PRId64 " seconds%s\n"), scratch,
secs2out(scratch, secs_str,
sizeof (secs_str), SPC_BEGIN));
} else {
(void) fprintf(file,
dgettext(TEXT_DOMAIN,
"Hard expiration occurred\n"));
}
scratch += wallclock;
printsatime(file, scratch, dgettext(TEXT_DOMAIN,
"%sTime of expiration: %s.\n"),
hard_prefix, hard_prefix, vflag);
}
}
}
if (idle != NULL) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sIdle lifetime information:\n"), idle_prefix);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
idle_prefix, idle->sadb_lifetime_addtime,
secs2out(idle->sadb_lifetime_addtime, secs_str,
sizeof (secs_str), SPC_END));
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
idle_prefix, idle->sadb_lifetime_usetime,
secs2out(idle->sadb_lifetime_usetime, secs_str,
sizeof (secs_str), SPC_END));
}
}
/*
* Print an SADB_EXT_ADDRESS_* extension.
*/
void
print_address(FILE *file, char *prefix, struct sadb_address *addr,
boolean_t ignore_nss)
{
struct protoent *pe;
(void) fprintf(file, "%s", prefix);
switch (addr->sadb_address_exttype) {
case SADB_EXT_ADDRESS_SRC:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address "));
break;
case SADB_X_EXT_ADDRESS_INNER_SRC:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Inner source address "));
break;
case SADB_EXT_ADDRESS_DST:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Destination address "));
break;
case SADB_X_EXT_ADDRESS_INNER_DST:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Inner destination address "));
break;
case SADB_X_EXT_ADDRESS_NATT_LOC:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"NAT-T local address "));
break;
case SADB_X_EXT_ADDRESS_NATT_REM:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"NAT-T remote address "));
break;
}
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"(proto=%d"), addr->sadb_address_proto);
if (ignore_nss == B_FALSE) {
if (addr->sadb_address_proto == 0) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"/<unspecified>"));
} else if ((pe = getprotobynumber(addr->sadb_address_proto))
!= NULL) {
(void) fprintf(file, "/%s", pe->p_name);
} else {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"/<unknown>"));
}
}
(void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix);
(void) dump_sockaddr((struct sockaddr *)(addr + 1),
addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss);
}
/*
* Print an SADB_EXT_KEY extension.
*/
void
print_key(FILE *file, char *prefix, struct sadb_key *key)
{
(void) fprintf(file, "%s", prefix);
switch (key->sadb_key_exttype) {
case SADB_EXT_KEY_AUTH:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication"));
break;
case SADB_EXT_KEY_ENCRYPT:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption"));
break;
}
(void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix);
(void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits,
key->sadb_key_reserved, file, B_TRUE);
(void) fprintf(file, "\n");
}
/*
* Print an SADB_X_EXT_STR_AUTH extension.
*/
void
print_keystr(FILE *file, char *prefix, struct sadb_key *key)
{
(void) fprintf(file, "%s", prefix);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication"));
(void) fprintf(file, dgettext(TEXT_DOMAIN, " string.\n%s"), prefix);
(void) fprintf(file, "\"");
(void) dump_keystr((uint8_t *)(key + 1), key->sadb_key_bits, file);
(void) fprintf(file, "\"\n");
}
/*
* Print an SADB_EXT_IDENTITY_* extension.
*/
void
print_ident(FILE *file, char *prefix, struct sadb_ident *id)
{
boolean_t canprint = B_TRUE;
(void) fprintf(file, "%s", prefix);
switch (id->sadb_ident_exttype) {
case SADB_EXT_IDENTITY_SRC:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source"));
break;
case SADB_EXT_IDENTITY_DST:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination"));
break;
}
(void) fprintf(file, dgettext(TEXT_DOMAIN,
" identity, uid=%d, type "), id->sadb_ident_id);
canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL);
(void) fprintf(file, "\n%s", prefix);
if (canprint) {
(void) fprintf(file, "%s\n", (char *)(id + 1));
} else {
print_asn1_name(file, (const unsigned char *)(id + 1),
SADB_64TO8(id->sadb_ident_len) - sizeof (sadb_ident_t));
}
}
/*
* Convert sadb_sens extension into binary security label.
*/
#include <tsol/label.h>
#include <sys/tsol/tndb.h>
#include <sys/tsol/label_macro.h>
void
ipsec_convert_sens_to_bslabel(const struct sadb_sens *sens, bslabel_t *sl)
{
uint64_t *bitmap = (uint64_t *)(sens + 1);
int bitmap_len = SADB_64TO8(sens->sadb_sens_sens_len);
bsllow(sl);
LCLASS_SET((_bslabel_impl_t *)sl, sens->sadb_sens_sens_level);
bcopy(bitmap, &((_bslabel_impl_t *)sl)->compartments,
bitmap_len);
}
void
ipsec_convert_bslabel_to_string(bslabel_t *sl, char **plabel)
{
if (label_to_str(sl, plabel, M_LABEL, DEF_NAMES) != 0) {
*plabel = strdup(dgettext(TEXT_DOMAIN,
"** Label conversion failed **"));
}
}
void
ipsec_convert_bslabel_to_hex(bslabel_t *sl, char **plabel)
{
if (label_to_str(sl, plabel, M_INTERNAL, DEF_NAMES) != 0) {
*plabel = strdup(dgettext(TEXT_DOMAIN,
"** Label conversion failed **"));
}
}
int
ipsec_convert_sl_to_sens(int doi, bslabel_t *sl, sadb_sens_t *sens)
{
uint8_t *bitmap;
int sens_len = sizeof (sadb_sens_t) + _C_LEN * 4;
if (sens == NULL)
return (sens_len);
(void) memset(sens, 0, sens_len);
sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;
sens->sadb_sens_len = SADB_8TO64(sens_len);
sens->sadb_sens_dpd = doi;
sens->sadb_sens_sens_level = LCLASS(sl);
sens->sadb_sens_integ_level = 0;
sens->sadb_sens_sens_len = _C_LEN >> 1;
sens->sadb_sens_integ_len = 0;
sens->sadb_x_sens_flags = 0;
bitmap = (uint8_t *)(sens + 1);
bcopy(&(((_bslabel_impl_t *)sl)->compartments), bitmap, _C_LEN * 4);
return (sens_len);
}
/*
* Print an SADB_SENSITIVITY extension.
*/
void
print_sens(FILE *file, char *prefix, const struct sadb_sens *sens,
boolean_t ignore_nss)
{
char *plabel;
char *hlabel;
uint64_t *bitmap = (uint64_t *)(sens + 1);
bslabel_t sl;
int i;
int sens_len = sens->sadb_sens_sens_len;
int integ_len = sens->sadb_sens_integ_len;
boolean_t inner = (sens->sadb_sens_exttype == SADB_EXT_SENSITIVITY);
const char *sensname = inner ?
dgettext(TEXT_DOMAIN, "Plaintext Sensitivity") :
dgettext(TEXT_DOMAIN, "Ciphertext Sensitivity");
ipsec_convert_sens_to_bslabel(sens, &sl);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s%s DPD %d, sens level=%d, integ level=%d, flags=%x\n"),
prefix, sensname, sens->sadb_sens_dpd, sens->sadb_sens_sens_level,
sens->sadb_sens_integ_level, sens->sadb_x_sens_flags);
ipsec_convert_bslabel_to_hex(&sl, &hlabel);
if (ignore_nss) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s %s Label: %s\n"), prefix, sensname, hlabel);
for (i = 0; i < sens_len; i++, bitmap++)
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s %s BM extended word %d 0x%" PRIx64 "\n"),
prefix, sensname, i, *bitmap);
} else {
ipsec_convert_bslabel_to_string(&sl, &plabel);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s %s Label: %s (%s)\n"),
prefix, sensname, plabel, hlabel);
free(plabel);
}
free(hlabel);
bitmap = (uint64_t *)(sens + 1 + sens_len);
for (i = 0; i < integ_len; i++, bitmap++)
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s Integrity BM extended word %d 0x%" PRIx64 "\n"),
prefix, i, *bitmap);
}
/*
* Print an SADB_EXT_PROPOSAL extension.
*/
void
print_prop(FILE *file, char *prefix, struct sadb_prop *prop)
{
struct sadb_comb *combs;
int i, numcombs;
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sProposal, replay counter = %u.\n"), prefix,
prop->sadb_prop_replay);
numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop));
numcombs /= SADB_8TO64(sizeof (*combs));
combs = (struct sadb_comb *)(prop + 1);
for (i = 0; i < numcombs; i++) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s Combination #%u "), prefix, i + 1);
if (combs[i].sadb_comb_auth != SADB_AALG_NONE) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Authentication = "));
(void) dump_aalg(combs[i].sadb_comb_auth, file);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
" minbits=%u, maxbits=%u.\n%s "),
combs[i].sadb_comb_auth_minbits,
combs[i].sadb_comb_auth_maxbits, prefix);
}
if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Encryption = "));
(void) dump_ealg(combs[i].sadb_comb_encrypt, file);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
" minbits=%u, maxbits=%u, saltbits=%u.\n%s "),
combs[i].sadb_comb_encrypt_minbits,
combs[i].sadb_comb_encrypt_maxbits,
combs[i].sadb_x_comb_encrypt_saltbits, prefix);
}
(void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: "));
if (combs[i].sadb_comb_hard_allocations)
(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
combs[i].sadb_comb_hard_allocations);
if (combs[i].sadb_comb_hard_bytes)
(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
PRIu64 " "), combs[i].sadb_comb_hard_bytes);
if (combs[i].sadb_comb_hard_addtime)
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"post-add secs=%" PRIu64 " "),
combs[i].sadb_comb_hard_addtime);
if (combs[i].sadb_comb_hard_usetime)
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"post-use secs=%" PRIu64 ""),
combs[i].sadb_comb_hard_usetime);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "),
prefix);
if (combs[i].sadb_comb_soft_allocations)
(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
combs[i].sadb_comb_soft_allocations);
if (combs[i].sadb_comb_soft_bytes)
(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
PRIu64 " "), combs[i].sadb_comb_soft_bytes);
if (combs[i].sadb_comb_soft_addtime)
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"post-add secs=%" PRIu64 " "),
combs[i].sadb_comb_soft_addtime);
if (combs[i].sadb_comb_soft_usetime)
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"post-use secs=%" PRIu64 ""),
combs[i].sadb_comb_soft_usetime);
(void) fprintf(file, "\n");
}
}
/*
* Print an extended proposal (SADB_X_EXT_EPROP).
*/
void
print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop)
{
uint64_t *sofar;
struct sadb_x_ecomb *ecomb;
struct sadb_x_algdesc *algdesc;
int i, j;
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sExtended Proposal, replay counter = %u, "), prefix,
eprop->sadb_prop_replay);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs);
sofar = (uint64_t *)(eprop + 1);
ecomb = (struct sadb_x_ecomb *)sofar;
for (i = 0; i < eprop->sadb_x_prop_numecombs; ) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s Extended combination #%u:\n"), prefix, ++i);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "),
prefix);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
ecomb->sadb_x_ecomb_hard_allocations);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%" PRIu64
", "), ecomb->sadb_x_ecomb_hard_bytes);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-add secs=%"
PRIu64 ", "), ecomb->sadb_x_ecomb_hard_addtime);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
PRIu64 "\n"), ecomb->sadb_x_ecomb_hard_usetime);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "),
prefix);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
ecomb->sadb_x_ecomb_soft_allocations);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"bytes=%" PRIu64 ", "), ecomb->sadb_x_ecomb_soft_bytes);
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"post-add secs=%" PRIu64 ", "),
ecomb->sadb_x_ecomb_soft_addtime);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
PRIu64 "\n"), ecomb->sadb_x_ecomb_soft_usetime);
sofar = (uint64_t *)(ecomb + 1);
algdesc = (struct sadb_x_algdesc *)sofar;
for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s Alg #%u "), prefix, ++j);
switch (algdesc->sadb_x_algdesc_satype) {
case SADB_SATYPE_ESP:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"for ESP "));
break;
case SADB_SATYPE_AH:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"for AH "));
break;
case SADB_X_SATYPE_TCPSIG:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"for TCPSIG "));
break;
default:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"for satype=%d "),
algdesc->sadb_x_algdesc_satype);
}
switch (algdesc->sadb_x_algdesc_algtype) {
case SADB_X_ALGTYPE_CRYPT:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Encryption = "));
(void) dump_ealg(algdesc->sadb_x_algdesc_alg,
file);
break;
case SADB_X_ALGTYPE_AUTH:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"Authentication = "));
(void) dump_aalg(algdesc->sadb_x_algdesc_alg,
file);
break;
default:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"algtype(%d) = alg(%d)"),
algdesc->sadb_x_algdesc_algtype,
algdesc->sadb_x_algdesc_alg);
break;
}
(void) fprintf(file, dgettext(TEXT_DOMAIN,
" minbits=%u, maxbits=%u, saltbits=%u\n"),
algdesc->sadb_x_algdesc_minbits,
algdesc->sadb_x_algdesc_maxbits,
algdesc->sadb_x_algdesc_saltbits);
sofar = (uint64_t *)(++algdesc);
}
ecomb = (struct sadb_x_ecomb *)sofar;
}
}
/*
* Print an SADB_EXT_SUPPORTED extension.
*/
void
print_supp(FILE *file, char *prefix, struct sadb_supported *supp)
{
struct sadb_alg *algs;
int i, numalgs;
(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix);
switch (supp->sadb_supported_exttype) {
case SADB_EXT_SUPPORTED_AUTH:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication"));
break;
case SADB_EXT_SUPPORTED_ENCRYPT:
(void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption"));
break;
}
(void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n"));
algs = (struct sadb_alg *)(supp + 1);
numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp));
numalgs /= SADB_8TO64(sizeof (*algs));
for (i = 0; i < numalgs; i++) {
uint16_t exttype = supp->sadb_supported_exttype;
(void) fprintf(file, "%s", prefix);
switch (exttype) {
case SADB_EXT_SUPPORTED_AUTH:
(void) dump_aalg(algs[i].sadb_alg_id, file);
break;
case SADB_EXT_SUPPORTED_ENCRYPT:
(void) dump_ealg(algs[i].sadb_alg_id, file);
break;
}
(void) fprintf(file, dgettext(TEXT_DOMAIN,
" minbits=%u, maxbits=%u, ivlen=%u, saltbits=%u"),
algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits,
algs[i].sadb_alg_ivlen, algs[i].sadb_x_alg_saltbits);
if (exttype == SADB_EXT_SUPPORTED_ENCRYPT)
(void) fprintf(file, dgettext(TEXT_DOMAIN,
", increment=%u"), algs[i].sadb_x_alg_increment);
(void) fprintf(file, dgettext(TEXT_DOMAIN, ".\n"));
}
}
/*
* Print an SADB_EXT_SPIRANGE extension.
*/
void
print_spirange(FILE *file, char *prefix, struct sadb_spirange *range)
{
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sSPI Range, min=0x%x, max=0x%x\n"), prefix,
htonl(range->sadb_spirange_min),
htonl(range->sadb_spirange_max));
}
/*
* Print an SADB_X_EXT_KM_COOKIE extension.
*/
void
print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc)
{
char *cookie_label;
switch (kmc->sadb_x_kmc_proto) {
case SADB_X_KMP_IKE:
cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie);
if (cookie_label == NULL)
cookie_label =
dgettext(TEXT_DOMAIN, "<Label not found.>");
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s Protocol %u, cookie=\"%s\" (%u)\n"), prefix,
kmc->sadb_x_kmc_proto, cookie_label,
kmc->sadb_x_kmc_cookie);
return;
case SADB_X_KMP_KINK:
cookie_label = dgettext(TEXT_DOMAIN, "KINK:");
break;
case SADB_X_KMP_MANUAL:
cookie_label = dgettext(TEXT_DOMAIN, "Manual SA with cookie:");
break;
case SADB_X_KMP_IKEV2:
cookie_label = dgettext(TEXT_DOMAIN, "IKEV2:");
break;
default:
cookie_label =
dgettext(TEXT_DOMAIN, "<unknown KM protocol>");
break;
}
/*
* Assume native-byte-order printing for now. Exceptions (like
* byte-swapping) should be handled in per-KM-protocol cases above.
*/
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%s Protocol %u, cookie=\"%s\" (0x%"PRIx64"/%"PRIu64")\n"),
prefix, kmc->sadb_x_kmc_proto, cookie_label,
kmc->sadb_x_kmc_cookie64, kmc->sadb_x_kmc_cookie64);
}
/*
* Print an SADB_X_EXT_REPLAY_CTR extension.
*/
void
print_replay(FILE *file, char *prefix, sadb_x_replay_ctr_t *repl)
{
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"%sReplay Value "), prefix);
if ((repl->sadb_x_rc_replay32 == 0) &&
(repl->sadb_x_rc_replay64 == 0)) {
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"<Value not found.>"));
}
/*
* We currently do not support a 64-bit replay value.
* RFC 4301 will require one, however, and we have a field
* in place when 4301 is built.
*/
(void) fprintf(file, "% " PRIu64 "\n",
((repl->sadb_x_rc_replay32 == 0) ?
repl->sadb_x_rc_replay64 : repl->sadb_x_rc_replay32));
}
/*
* Print an SADB_X_EXT_PAIR extension.
*/
static void
print_pair(FILE *file, char *prefix, struct sadb_x_pair *pair)
{
(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sPaired with spi=0x%x\n"),
prefix, ntohl(pair->sadb_x_pair_spi));
}
/*
* Take a PF_KEY message pointed to buffer and print it. Useful for DUMP
* and GET.
*/
void
print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp,
boolean_t vflag, boolean_t ignore_nss)
{
uint64_t *current;
struct sadb_msg *samsg = (struct sadb_msg *)buffer;
struct sadb_ext *ext;
struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL;
struct sadb_lifetime *idlelt = NULL;
int i;
time_t wallclock;
(void) time(&wallclock);
print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag);
current = (uint64_t *)(samsg + 1);
while (current - buffer < samsg->sadb_msg_len) {
int lenbytes;
ext = (struct sadb_ext *)current;
lenbytes = SADB_64TO8(ext->sadb_ext_len);
switch (ext->sadb_ext_type) {
case SADB_EXT_SA:
print_sa(file, dgettext(TEXT_DOMAIN,
"SA: "), (struct sadb_sa *)current);
break;
/*
* Pluck out lifetimes and print them at the end. This is
* to show relative lifetimes.
*/
case SADB_EXT_LIFETIME_CURRENT:
currentlt = (struct sadb_lifetime *)current;
break;
case SADB_EXT_LIFETIME_HARD:
hardlt = (struct sadb_lifetime *)current;
break;
case SADB_EXT_LIFETIME_SOFT:
softlt = (struct sadb_lifetime *)current;
break;
case SADB_X_EXT_LIFETIME_IDLE:
idlelt = (struct sadb_lifetime *)current;
break;
case SADB_EXT_ADDRESS_SRC:
print_address(file, dgettext(TEXT_DOMAIN, "SRC: "),
(struct sadb_address *)current, ignore_nss);
break;
case SADB_X_EXT_ADDRESS_INNER_SRC:
print_address(file, dgettext(TEXT_DOMAIN, "INS: "),
(struct sadb_address *)current, ignore_nss);
break;
case SADB_EXT_ADDRESS_DST:
print_address(file, dgettext(TEXT_DOMAIN, "DST: "),
(struct sadb_address *)current, ignore_nss);
break;
case SADB_X_EXT_ADDRESS_INNER_DST:
print_address(file, dgettext(TEXT_DOMAIN, "IND: "),
(struct sadb_address *)current, ignore_nss);
break;
case SADB_EXT_KEY_AUTH:
print_key(file, dgettext(TEXT_DOMAIN,
"AKY: "), (struct sadb_key *)current);
break;
case SADB_X_EXT_STR_AUTH:
print_keystr(file, dgettext(TEXT_DOMAIN,
"AST: "), (struct sadb_key *)current);
break;
case SADB_EXT_KEY_ENCRYPT:
print_key(file, dgettext(TEXT_DOMAIN,
"EKY: "), (struct sadb_key *)current);
break;
case SADB_EXT_IDENTITY_SRC:
print_ident(file, dgettext(TEXT_DOMAIN, "SID: "),
(struct sadb_ident *)current);
break;
case SADB_EXT_IDENTITY_DST:
print_ident(file, dgettext(TEXT_DOMAIN, "DID: "),
(struct sadb_ident *)current);
break;
case SADB_EXT_SENSITIVITY:
print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "),
(struct sadb_sens *)current, ignore_nss);
break;
case SADB_EXT_PROPOSAL:
print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "),
(struct sadb_prop *)current);
break;
case SADB_EXT_SUPPORTED_AUTH:
print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "),
(struct sadb_supported *)current);
break;
case SADB_EXT_SUPPORTED_ENCRYPT:
print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "),
(struct sadb_supported *)current);
break;
case SADB_EXT_SPIRANGE:
print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "),
(struct sadb_spirange *)current);
break;
case SADB_X_EXT_EPROP:
print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "),
(struct sadb_prop *)current);
break;
case SADB_X_EXT_KM_COOKIE:
print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "),
(struct sadb_x_kmc *)current);
break;
case SADB_X_EXT_ADDRESS_NATT_REM:
print_address(file, dgettext(TEXT_DOMAIN, "NRM: "),
(struct sadb_address *)current, ignore_nss);
break;
case SADB_X_EXT_ADDRESS_NATT_LOC:
print_address(file, dgettext(TEXT_DOMAIN, "NLC: "),
(struct sadb_address *)current, ignore_nss);
break;
case SADB_X_EXT_PAIR:
print_pair(file, dgettext(TEXT_DOMAIN, "OTH: "),
(struct sadb_x_pair *)current);
break;
case SADB_X_EXT_OUTER_SENS:
print_sens(file, dgettext(TEXT_DOMAIN, "OSN: "),
(struct sadb_sens *)current, ignore_nss);
break;
case SADB_X_EXT_REPLAY_VALUE:
(void) print_replay(file, dgettext(TEXT_DOMAIN,
"RPL: "), (sadb_x_replay_ctr_t *)current);
break;
default:
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"UNK: Unknown ext. %d, len %d.\n"),
ext->sadb_ext_type, lenbytes);
for (i = 0; i < ext->sadb_ext_len; i++)
(void) fprintf(file, dgettext(TEXT_DOMAIN,
"UNK: 0x%" PRIx64 "\n"),
((uint64_t *)ext)[i]);
break;
}
current += (lenbytes == 0) ?
SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len;
}
/*
* Print lifetimes NOW.
*/
if (currentlt != NULL || hardlt != NULL || softlt != NULL ||
idlelt != NULL)
print_lifetimes(file, wallclock, currentlt, hardlt,
softlt, idlelt, vflag);
if (current - buffer != samsg->sadb_msg_len) {
warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
"WARNING: insufficient buffer space or corrupt message."));
}
(void) fflush(file); /* Make sure our message is out there. */
}
/*
* save_XXX functions are used when "saving" the SA tables to either a
* file or standard output. They use the dump_XXX functions where needed,
* but mostly they use the rparseXXX functions.
*/
/*
* Print save information for a lifetime extension.
*
* NOTE : It saves the lifetime in absolute terms. For example, if you
* had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though
* there may have been 59 seconds burned off the clock.
*/
boolean_t
save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile)
{
char *prefix;
switch (lifetime->sadb_lifetime_exttype) {
case SADB_EXT_LIFETIME_HARD:
prefix = "hard";
break;
case SADB_EXT_LIFETIME_SOFT:
prefix = "soft";
break;
case SADB_X_EXT_LIFETIME_IDLE:
prefix = "idle";
break;
}
if (putc('\t', ofile) == EOF)
return (B_FALSE);
if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile,
"%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0)
return (B_FALSE);
if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile,
"%s_bytes %" PRIu64 " ", prefix, lifetime->sadb_lifetime_bytes) < 0)
return (B_FALSE);
if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile,
"%s_addtime %" PRIu64 " ", prefix,
lifetime->sadb_lifetime_addtime) < 0)
return (B_FALSE);
if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile,
"%s_usetime %" PRIu64 " ", prefix,
lifetime->sadb_lifetime_usetime) < 0)
return (B_FALSE);
return (B_TRUE);
}
/*
* Print save information for an address extension.
*/
boolean_t
save_address(struct sadb_address *addr, FILE *ofile)
{
char *printable_addr, buf[INET6_ADDRSTRLEN];
const char *prefix, *pprefix;
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1);
struct sockaddr_in *sin = (struct sockaddr_in *)sin6;
int af = sin->sin_family;
/*
* Address-family reality check.
*/
if (af != AF_INET6 && af != AF_INET)
return (B_FALSE);
switch (addr->sadb_address_exttype) {
case SADB_EXT_ADDRESS_SRC:
prefix = "src";
pprefix = "sport";
break;
case SADB_X_EXT_ADDRESS_INNER_SRC:
prefix = "isrc";
pprefix = "isport";
break;
case SADB_EXT_ADDRESS_DST:
prefix = "dst";
pprefix = "dport";
break;
case SADB_X_EXT_ADDRESS_INNER_DST:
prefix = "idst";
pprefix = "idport";
break;
case SADB_X_EXT_ADDRESS_NATT_LOC:
prefix = "nat_loc ";
pprefix = "nat_lport";
break;
case SADB_X_EXT_ADDRESS_NATT_REM:
prefix = "nat_rem ";
pprefix = "nat_rport";
break;
}
if (fprintf(ofile, " %s ", prefix) < 0)
return (B_FALSE);
/*
* Do not do address-to-name translation, given that we live in
* an age of names that explode into many addresses.
*/
printable_addr = (char *)inet_ntop(af,
(af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr,
buf, sizeof (buf));
if (printable_addr == NULL)
printable_addr = "Invalid IP address.";
if (fprintf(ofile, "%s", printable_addr) < 0)
return (B_FALSE);
if (addr->sadb_address_prefixlen != 0 &&
!((addr->sadb_address_prefixlen == 32 && af == AF_INET) ||
(addr->sadb_address_prefixlen == 128 && af == AF_INET6))) {
if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0)
return (B_FALSE);
}
/*
* The port is in the same position for struct sockaddr_in and
* struct sockaddr_in6. We exploit that property here.
*/
if ((pprefix != NULL) && (sin->sin_port != 0))
(void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port));
return (B_TRUE);
}
/*
* Print save information for a key extension. Returns whether writing
* to the specified output file was successful or not.
*/
boolean_t
save_key(struct sadb_key *key, FILE *ofile)
{
char *prefix;
if (putc('\t', ofile) == EOF)
return (B_FALSE);
prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr";
if (fprintf(ofile, "%skey ", prefix) < 0)
return (B_FALSE);
if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits,
key->sadb_key_reserved, ofile, B_FALSE) == -1)
return (B_FALSE);
return (B_TRUE);
}
/*
* Print save information for a key extension. Returns whether writing
* to the specified output file was successful or not.
*/
boolean_t
save_keystr(struct sadb_key *key, FILE *ofile)
{
if (putc('\t', ofile) == EOF)
return (B_FALSE);
if (fprintf(ofile, "authstring \"") < 0)
return (B_FALSE);
if (dump_keystr((uint8_t *)(key + 1), key->sadb_key_bits, ofile) == -1)
return (B_FALSE);
if (fprintf(ofile, "\"") < 0)
return (B_FALSE);
return (B_TRUE);
}
/*
* Print save information for an identity extension.
*/
boolean_t
save_ident(struct sadb_ident *ident, FILE *ofile)
{
char *prefix;
if (putc('\t', ofile) == EOF)
return (B_FALSE);
prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" :
"dst";
if (fprintf(ofile, "%sidtype %s ", prefix,
rparseidtype(ident->sadb_ident_type)) < 0)
return (B_FALSE);
if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN ||
ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) {
if (fprintf(ofile, dgettext(TEXT_DOMAIN,
"<can-not-print>")) < 0)
return (B_FALSE);
} else {
if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0)
return (B_FALSE);
}
return (B_TRUE);
}
boolean_t
save_sens(struct sadb_sens *sens, FILE *ofile)
{
char *prefix;
char *hlabel;
bslabel_t sl;
if (putc('\t', ofile) == EOF)
return (B_FALSE);
if (sens->sadb_sens_exttype == SADB_EXT_SENSITIVITY)
prefix = "label";
else if ((sens->sadb_x_sens_flags & SADB_X_SENS_IMPLICIT) == 0)
prefix = "outer-label";
else
prefix = "implicit-label";
ipsec_convert_sens_to_bslabel(sens, &sl);
ipsec_convert_bslabel_to_hex(&sl, &hlabel);
if (fprintf(ofile, "%s %s ", prefix, hlabel) < 0) {
free(hlabel);
return (B_FALSE);
}
free(hlabel);
return (B_TRUE);
}
/*
* "Save" a security association to an output file.
*
* NOTE the lack of calls to dgettext() because I'm outputting parseable stuff.
* ALSO NOTE that if you change keywords (see parsecmd()), you'll have to
* change them here as well.
*/
void
save_assoc(uint64_t *buffer, FILE *ofile)
{
int terrno;
boolean_t seen_proto = B_FALSE, seen_iproto = B_FALSE;
uint64_t *current;
struct sadb_address *addr;
struct sadb_x_replay_ctr *repl;
struct sadb_msg *samsg = (struct sadb_msg *)buffer;
struct sadb_ext *ext;
boolean_t tcpsig = samsg->sadb_msg_satype == SADB_X_SATYPE_TCPSIG;
#define tidyup() \
terrno = errno; (void) fclose(ofile); errno = terrno; \
interactive = B_FALSE
#define savenl() if (fputs(" \\\n", ofile) == EOF) \
{ bail(dgettext(TEXT_DOMAIN, "savenl")); }
if (fputs("# begin assoc\n", ofile) == EOF)
bail(dgettext(TEXT_DOMAIN,
"save_assoc: Opening comment of SA"));
if (tcpsig) {
if (fprintf(ofile, "add ") < 0) {
bail(dgettext(TEXT_DOMAIN,
"save_assoc: First line of SA"));
}
seen_proto = B_TRUE;
} else {
if (fprintf(ofile, "add %s ",
rparsesatype(samsg->sadb_msg_satype)) < 0) {
bail(dgettext(TEXT_DOMAIN,
"save_assoc: First line of SA"));
}
}
savenl();
current = (uint64_t *)(samsg + 1);
while (current - buffer < samsg->sadb_msg_len) {
struct sadb_sa *assoc;
ext = (struct sadb_ext *)current;
addr = (struct sadb_address *)ext; /* Just in case... */
switch (ext->sadb_ext_type) {
case SADB_EXT_SA:
assoc = (struct sadb_sa *)ext;
if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
if (fprintf(ofile, "# WARNING: SA was dying "
"or dead.\n") < 0) {
tidyup();
bail(dgettext(TEXT_DOMAIN,
"save_assoc: fprintf not mature"));
}
}
if (!tcpsig) {
if (fprintf(ofile, " spi 0x%x ",
ntohl(assoc->sadb_sa_spi)) < 0) {
tidyup();
bail(dgettext(TEXT_DOMAIN,
"save_assoc: fprintf spi"));
}
}
if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
if (fprintf(ofile, "encr_alg %s ",
rparsealg(assoc->sadb_sa_encrypt,
IPSEC_PROTO_ESP)) < 0) {
tidyup();
bail(dgettext(TEXT_DOMAIN,
"save_assoc: fprintf encrypt"));
}
}
if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
int ret;
if ((assoc->sadb_sa_flags &
SADB_X_SAFLAGS_TCPSIG) != 0) {
ret = fprintf(ofile, " authalg %s ",
rparsetcpsigalg(
assoc->sadb_sa_auth));
} else {
ret = fprintf(ofile, "auth_alg %s ",
rparsealg(assoc->sadb_sa_auth,
IPSEC_PROTO_AH));
}
if (ret < 0) {
tidyup();
bail(dgettext(TEXT_DOMAIN,
"save_assoc: fprintf auth"));
}
}
if ((assoc->sadb_sa_flags &
SADB_X_SAFLAGS_TCPSIG) == 0) {
if (fprintf(ofile, "replay %d ",
assoc->sadb_sa_replay) < 0) {
tidyup();
bail(dgettext(TEXT_DOMAIN,
"save_assoc: fprintf replay"));
}
}
if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC |
SADB_X_SAFLAGS_NATT_REM)) {
if (fprintf(ofile, "encap udp") < 0) {
tidyup();
bail(dgettext(TEXT_DOMAIN,
"save_assoc: fprintf encap"));
}
}
savenl();
break;
case SADB_EXT_LIFETIME_HARD:
case SADB_EXT_LIFETIME_SOFT:
case SADB_X_EXT_LIFETIME_IDLE:
if (!save_lifetime((struct sadb_lifetime *)ext,
ofile)) {
tidyup();
bail(dgettext(TEXT_DOMAIN, "save_lifetime"));
}
savenl();
break;
case SADB_X_EXT_ADDRESS_INNER_SRC:
case SADB_X_EXT_ADDRESS_INNER_DST:
if (!seen_iproto && addr->sadb_address_proto) {
(void) fprintf(ofile, " iproto %d",
addr->sadb_address_proto);
savenl();
seen_iproto = B_TRUE;
}
goto skip_srcdst; /* Hack to avoid cases below... */
/* FALLTHRU */
case SADB_EXT_ADDRESS_SRC:
case SADB_EXT_ADDRESS_DST:
if (!seen_proto && addr->sadb_address_proto) {
(void) fprintf(ofile, " proto %d",
addr->sadb_address_proto);
savenl();
seen_proto = B_TRUE;
}
/* FALLTHRU */
case SADB_X_EXT_ADDRESS_NATT_REM:
case SADB_X_EXT_ADDRESS_NATT_LOC:
skip_srcdst:
if (!save_address(addr, ofile)) {
tidyup();
bail(dgettext(TEXT_DOMAIN, "save_address"));
}
savenl();
break;
case SADB_EXT_KEY_AUTH:
case SADB_EXT_KEY_ENCRYPT:
if (!save_key((struct sadb_key *)ext, ofile)) {
tidyup();
bail(dgettext(TEXT_DOMAIN, "save_key"));
}
savenl();
break;
case SADB_X_EXT_STR_AUTH:
if (!save_keystr((struct sadb_key *)ext, ofile)) {
tidyup();
bail(dgettext(TEXT_DOMAIN, "save_keystr"));
}
savenl();
break;
case SADB_EXT_IDENTITY_SRC:
case SADB_EXT_IDENTITY_DST:
if (!save_ident((struct sadb_ident *)ext, ofile)) {
tidyup();
bail(dgettext(TEXT_DOMAIN, "save_ident"));
}
savenl();
break;
case SADB_X_EXT_REPLAY_VALUE:
repl = (sadb_x_replay_ctr_t *)ext;
if ((repl->sadb_x_rc_replay32 == 0) &&
(repl->sadb_x_rc_replay64 == 0)) {
tidyup();
bail(dgettext(TEXT_DOMAIN, "Replay Value"));
}
if (fprintf(ofile, "replay_value %" PRIu64 "",
(repl->sadb_x_rc_replay32 == 0 ?
repl->sadb_x_rc_replay64 :
repl->sadb_x_rc_replay32)) < 0) {
tidyup();
bail(dgettext(TEXT_DOMAIN,
"save_assoc: fprintf replay value"));
}
savenl();
break;
case SADB_EXT_SENSITIVITY:
case SADB_X_EXT_OUTER_SENS:
if (!save_sens((struct sadb_sens *)ext, ofile)) {
tidyup();
bail(dgettext(TEXT_DOMAIN, "save_sens"));
}
savenl();
break;
default:
/* Skip over irrelevant extensions. */
break;
}
current += ext->sadb_ext_len;
}
if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) {
tidyup();
bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs"));
}
}
/*
* Open the output file for the "save" command.
*/
FILE *
opensavefile(char *filename)
{
int fd;
FILE *retval;
struct stat buf;
/*
* If the user specifies "-" or doesn't give a filename, then
* dump to stdout. Make sure to document the dangers of files
* that are NFS, directing your output to strange places, etc.
*/
if (filename == NULL || strcmp("-", filename) == 0)
return (stdout);
/*
* open the file with the create bits set. Since I check for
* real UID == root in main(), I won't worry about the ownership
* problem.
*/
fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR);
if (fd == -1) {
if (errno != EEXIST)
bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
"open error"),
strerror(errno));
fd = open(filename, O_WRONLY | O_TRUNC, 0);
if (fd == -1)
bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
"open error"), strerror(errno));
if (fstat(fd, &buf) == -1) {
(void) close(fd);
bail_msg("%s fstat: %s", filename, strerror(errno));
}
if (S_ISREG(buf.st_mode) &&
((buf.st_mode & S_IAMB) != S_IRUSR)) {
warnx(dgettext(TEXT_DOMAIN,
"WARNING: Save file already exists with "
"permission %o."), (uint_t)(buf.st_mode & S_IAMB));
warnx(dgettext(TEXT_DOMAIN,
"Normal users may be able to read IPsec "
"keying material."));
}
}
/* Okay, we have an FD. Assign it to a stdio FILE pointer. */
retval = fdopen(fd, "w");
if (retval == NULL) {
(void) close(fd);
bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
"fdopen error"), strerror(errno));
}
return (retval);
}
const char *
do_inet_ntop(const void *addr, char *cp, size_t size)
{
boolean_t isv4;
struct in6_addr *inaddr6 = (struct in6_addr *)addr;
struct in_addr inaddr;
if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) {
IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr);
}
return (inet_ntop(isv4 ? AF_INET : AF_INET6,
isv4 ? (void *)&inaddr : inaddr6, cp, size));
}
char numprint[NBUF_SIZE];
/*
* Parse and reverse parse a specific SA type (AH, ESP, etc.).
*/
static struct typetable {
char *type;
int token;
} type_table[] = {
{"all", SADB_SATYPE_UNSPEC},
{"ah", SADB_SATYPE_AH},
{"esp", SADB_SATYPE_ESP},
{"tcpsig", SADB_X_SATYPE_TCPSIG},
/* PF_KEY NOTE: More to come if net/pfkeyv2.h gets updated. */
{NULL, 0} /* Token value is irrelevant for this entry. */
};
char *
rparsesatype(int type)
{
struct typetable *tt = type_table;
while (tt->type != NULL && type != tt->token)
tt++;
if (tt->type == NULL) {
(void) snprintf(numprint, NBUF_SIZE, "%d", type);
} else {
return (tt->type);
}
return (numprint);
}
/*
* Return a string containing the name of the specified numerical algorithm
* identifier.
*/
char *
rparsealg(uint8_t alg, int proto_num)
{
static struct ipsecalgent *holder = NULL; /* we're single-threaded */
if (holder != NULL)
freeipsecalgent(holder);
holder = getipsecalgbynum(alg, proto_num, NULL);
if (holder == NULL) {
(void) snprintf(numprint, NBUF_SIZE, "%d", alg);
return (numprint);
}
return (*(holder->a_names));
}
const char *
rparsetcpsigalg(uint8_t alg)
{
const char *name = gettcpsigalgbynum(alg);
if (name == NULL) {
(void) snprintf(numprint, NBUF_SIZE, "%d", alg);
return (numprint);
}
return (name);
}
/*
* Parse and reverse parse out a source/destination ID type.
*/
static struct idtypes {
char *idtype;
uint8_t retval;
} idtypes[] = {
{"prefix", SADB_IDENTTYPE_PREFIX},
{"fqdn", SADB_IDENTTYPE_FQDN},
{"domain", SADB_IDENTTYPE_FQDN},
{"domainname", SADB_IDENTTYPE_FQDN},
{"user_fqdn", SADB_IDENTTYPE_USER_FQDN},
{"mailbox", SADB_IDENTTYPE_USER_FQDN},
{"der_dn", SADB_X_IDENTTYPE_DN},
{"der_gn", SADB_X_IDENTTYPE_GN},
{NULL, 0}
};
char *
rparseidtype(uint16_t type)
{
struct idtypes *idp;
for (idp = idtypes; idp->idtype != NULL; idp++) {
if (type == idp->retval)
return (idp->idtype);
}
(void) snprintf(numprint, NBUF_SIZE, "%d", type);
return (numprint);
}
/*
* This is a general purpose exit function, calling functions can specify an
* error type. If the command calling this function was started by smf(7) the
* error type could be used as a hint to the restarter. In the future this
* function could be used to do something more intelligent with a process that
* encounters an error. If exit() is called with an error code other than those
* defined by smf(7), the program will just get restarted. Unless restarting
* is likely to resolve the error condition, its probably sensible to just
* log the error and keep running.
*
* The SERVICE_* exit_types mean nothing if the command was run from the
* command line, just exit(). There are two special cases:
*
* SERVICE_DEGRADE - Not implemented in smf(7), one day it could hint that
* the service is not running as well is it could. For
* now, don't do anything, just record the error.
* DEBUG_FATAL - Something happened, if the command was being run in debug
* mode, exit() as you really want to know something happened,
* otherwise just keep running. This is ignored when running
* under smf(7).
*
* The function will handle an optional variable args error message, this
* will be written to the error stream, typically a log file or stderr.
*/
void
ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...)
{
int exit_status;
va_list args;
if (fp == NULL)
fp = stderr;
if (fmt != NULL) {
va_start(args, fmt);
vwarnxfp(fp, fmt, args);
va_end(args);
}
if (fmri == NULL) {
/* Command being run directly from a shell. */
switch (type) {
case SERVICE_EXIT_OK:
exit_status = 0;
break;
case SERVICE_DEGRADE:
return;
case SERVICE_BADPERM:
case SERVICE_BADCONF:
case SERVICE_MAINTAIN:
case SERVICE_DISABLE:
case SERVICE_FATAL:
case SERVICE_RESTART:
case DEBUG_FATAL:
warnxfp(fp, "Fatal error - exiting.");
exit_status = 1;
break;
}
} else {
/* Command being run as a smf(7) method. */
switch (type) {
case SERVICE_EXIT_OK:
exit_status = SMF_EXIT_OK;
break;
case SERVICE_DEGRADE: /* Not implemented yet. */
case DEBUG_FATAL:
/* Keep running, don't exit(). */
return;
case SERVICE_BADPERM:
warnxfp(fp, dgettext(TEXT_DOMAIN,
"Permission error with %s."), fmri);
exit_status = SMF_EXIT_ERR_PERM;
break;
case SERVICE_BADCONF:
warnxfp(fp, dgettext(TEXT_DOMAIN,
"Bad configuration of service %s."), fmri);
exit_status = SMF_EXIT_ERR_FATAL;
break;
case SERVICE_MAINTAIN:
warnxfp(fp, dgettext(TEXT_DOMAIN,
"Service %s needs maintenance."), fmri);
exit_status = SMF_EXIT_ERR_FATAL;
break;
case SERVICE_DISABLE:
exit_status = SMF_EXIT_ERR_FATAL;
break;
case SERVICE_FATAL:
warnxfp(fp, dgettext(TEXT_DOMAIN,
"Service %s fatal error."), fmri);
exit_status = SMF_EXIT_ERR_FATAL;
break;
case SERVICE_RESTART:
exit_status = 1;
break;
}
}
(void) fflush(fp);
(void) fclose(fp);
exit(exit_status);
}
void
print_asn1_name(FILE *file, const unsigned char *buf, long buflen)
{
KMF_X509_NAME name = { 0 };
KMF_DATA data = { 0 };
char *str = NULL;
data.Data = (unsigned char *)buf;
data.Length = buflen;
if (DerDecodeName(&data, &name) != KMF_OK)
goto fail;
if (kmf_dn_to_string(&name, &str) != KMF_OK)
goto fail;
(void) fprintf(file, "%s\n", str);
kmf_free_dn(&name);
free(str);
return;
fail:
kmf_free_dn(&name);
(void) fprintf(file, dgettext(TEXT_DOMAIN, "<cannot interpret>\n"));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
#ifndef _IPSEC_UTIL_H
#define _IPSEC_UTIL_H
/*
* Headers and definitions for support functions that are shared by
* the ipsec utilities ipseckey and ikeadm.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <net/pfkeyv2.h>
#include <netinet/in.h>
#include <inet/ip.h>
#include <setjmp.h>
#include <stdio.h>
#include <err.h>
#include <errfp.h>
#include <net/pfpolicy.h>
#include <libtecla.h>
#ifndef A_CNT
/* macros for array manipulation */
#define A_CNT(arr) (sizeof (arr)/sizeof (arr[0]))
#define A_END(arr) (&arr[A_CNT(arr)])
#endif
/* used for file parsing */
#define NBUF_SIZE 16
#define COMMENT_CHAR '#'
#define CONT_CHAR '\\'
#define QUOTE_CHAR '"'
/*
* Input buffer size limits maximum line length for both file parsing and
* interactive mode. 4K chars should be enough even for broad commands and
* all possible key lenghts of today's symmetric ciphers entered via
* ipseckey(8) which has the most bifurcated grammar from all IPsec commands.
*/
#define IBUF_SIZE 4096
/* used for command-line parsing */
#define START_ARG 8
#define TOO_MANY_ARGS (START_ARG << 9)
/* Return codes for argv/argc vector creation */
#define TOO_MANY_TOKENS -3
#define MEMORY_ALLOCATION -2
#define COMMENT_LINE 1
#define SUCCESS 0
/* Flags for {bytecnt,secs}2out() */
#define SPC_NOSPACES 0x00000000 /* no space prefix/suffix */
#define SPC_BEGIN 0x00000001 /* put space at the beginning */
#define SPC_END 0x00000002 /* put space at the end */
#define SPC_BOTH SPC_BEGIN|SPC_END /* print both spaces */
/*
* The following lengths should be sufficient for character buffers passed to
* bytecnt2str(),secs2str(). This is because the string output of these
* functions consists of limited number and units. The lengths should be also
* sufficient for bytecnt2out(),secs2out() functions.
*/
#define BYTE_STR_SIZE 16
#define SECS_STR_SIZE 20
/*
* Time printing defines...
*
* TBUF_SIZE is pretty arbitrary. Perhaps it shouldn't be.
*/
#define TBUF_SIZE 50
#define TIME_MAX LONG_MAX
#ifndef INSECURE_PERMS
#define INSECURE_PERMS(sbuf) (((sbuf).st_uid != 0) || \
((sbuf).st_mode & S_IRWXG) || ((sbuf).st_mode & S_IRWXO))
#endif
#ifndef PKCS11_TOKSIZE
#define PKCS11_TOKSIZE 32 /* Fixed length of PKCS#11 token string len. */
#endif
/*
* Solaris UDP port used to communicate with the Solaris Cluster
* daemon. It is used only when the node is booted in cluster mode.
*/
#define CLUSTER_UDP_PORT 2005
/* For keyword-lookup tables */
typedef struct keywdtab {
uint_t kw_tag;
char *kw_str;
} keywdtab_t;
/*
* These different exit states are designed to give consistant behaviour
* when a program needs to exit because of an error. These exit_types
* are used in macros, defined later in this file, which call ipsecutil_exit().
* What happens when ipsecutil_exit() may differ if the command was started
* on the command line or via smf(7), See ipsecutil_exit() source for details.
*
* Note: The calling function should decide what "debug mode" is before calling
* ipsecutil_exit() with DEBUG_FATAL.
*/
typedef enum exit_type {
SERVICE_EXIT_OK, /* Exit without error. */
SERVICE_DEGRADE, /* A hint that service should be degraded. */
SERVICE_BADPERM, /* A Permission error occured. */
SERVICE_BADCONF, /* Misconfiguration. */
SERVICE_MAINTAIN, /* smf(7) to put service in maintenance mode. */
SERVICE_DISABLE, /* Tell smf(7) to disable me. */
SERVICE_FATAL, /* Whatever happened is not fixable. */
SERVICE_RESTART, /* Tell smf(7) to restart the service. */
DEBUG_FATAL /* Exit in debug mode. */
} exit_type_t;
/*
* Function Prototypes
*/
/*
* Print errno and if cmdline or readfile, exit; if interactive reset state
*/
extern void ipsecutil_exit(exit_type_t, char *, FILE *, const char *fmt, ...);
extern void bail(char *);
/*
* Localization macro - Only to be used from usr/src/cmd because Macros
* are not expanded in usr/src/lib when message catalogs are built.
*/
#define Bail(s) bail(dgettext(TEXT_DOMAIN, s))
/*
* Print caller-supplied, variable-arg error message, then exit if cmdline
* or readfile, or reset state if interactive.
*/
extern void bail_msg(char *, ...);
/*
* dump_XXX functions produce ASCII output from the passed in data.
*
* Because certain errors need to do this stderr, dump_XXX functions
* take a FILE pointer.
*/
extern int dump_sockaddr(struct sockaddr *, uint8_t, boolean_t, FILE *,
boolean_t);
extern int dump_key(uint8_t *, uint_t, uint_t, FILE *, boolean_t);
extern int dump_aalg(uint8_t, FILE *);
extern int dump_ealg(uint8_t, FILE *);
/* return true if sadb string is printable (based on type), false otherwise */
extern boolean_t dump_sadb_idtype(uint8_t, FILE *, int *);
/*
* do_interactive: Enter a mode where commands are read from a file;
* treat stdin special. infile is the file cmds are read from;
* promptstring is the string printed to stdout (if the cmds are
* being read from stdin) to prompt for a new command; parseit is
* the function to be called to process the command line once it's
* been read in and broken up into an argv/argc vector.
*/
/* callback function passed in to do_interactive() */
typedef void (*parse_cmdln_fn)(int, char **, char *, boolean_t);
extern void do_interactive(FILE *, char *, char *, char *, parse_cmdln_fn,
CplMatchFn *);
extern uint_t lines_parsed;
extern uint_t lines_added;
/* convert a string to an IKE_PRIV_* constant */
extern int privstr2num(char *);
/* convert a string to a D_* debug flag */
extern int dbgstr2num(char *);
/* convert a string of debug strings with +|- delimiters to a debug level */
extern int parsedbgopts(char *);
/*
* functions to manipulate the IKEv1 kmcookie-label mapping file
*/
#define KMCFILE "/var/run/ipsec_kmc_map"
/*
* Insert a mapping into the file (if it's not already there), given the
* new label. Return the assigned cookie, or -1 on error.
*/
extern int kmc_insert_mapping(char *);
/*
* Lookup the given cookie and return its corresponding label. Return
* a pointer to the label on success, NULL on error (or if the label is
* not found).
*/
extern char *kmc_lookup_by_cookie(int);
/*
* These globals are declared for us in ipsec_util.c, since it needs to
* refer to them also...
*/
extern boolean_t nflag; /* Avoid nameservice? */
extern boolean_t pflag; /* Paranoid w.r.t. printing keying material? */
extern boolean_t interactive;
extern boolean_t readfile;
extern uint_t lineno;
extern char numprint[NBUF_SIZE];
/* For error recovery in interactive or read-file mode. */
extern jmp_buf env;
/*
* Back-end stuff for getalgby*().
*/
#define INET_IPSECALGSPATH "/etc/inet/"
#define INET_IPSECALGSFILE (INET_IPSECALGSPATH "ipsecalgs")
/* To preserve packages delimiters in /etc/inet/ipsecalgs */
typedef struct ipsecalgs_pkg {
int alg_num;
char *pkg_name;
} ipsecalgs_pkg_t;
/*
* The cached representation of /etc/inet/ipsecalgs is represented by:
* - A dynamically-grown (optionally sorted) array of IPsec protocols
* - Each protocol has an array (again, dynamically grown and sorted)
* of algorithms, each a full-fledged struct ipsecalgent.
* - The getipsecalg*() routines will search the list, then duplicate the
* struct ipsecalgent and return it.
*/
typedef enum {
LIBIPSEC_ALGS_EXEC_SYNC,
LIBIPSEC_ALGS_EXEC_ASYNC
} ipsecalgs_exec_mode_t;
typedef struct ipsec_proto {
int proto_num;
char *proto_name;
char *proto_pkg;
int proto_numalgs;
struct ipsecalgent **proto_algs;
ipsecalgs_pkg_t *proto_algs_pkgs;
int proto_algs_npkgs;
ipsecalgs_exec_mode_t proto_exec_mode;
} ipsec_proto_t;
extern void _build_internal_algs(ipsec_proto_t **, int *);
extern int _str_to_ipsec_exec_mode(char *, ipsecalgs_exec_mode_t *);
extern int addipsecalg(struct ipsecalgent *, uint_t);
extern int delipsecalgbyname(const char *, int);
extern int delipsecalgbynum(int, int);
extern int addipsecproto(const char *, int, ipsecalgs_exec_mode_t, uint_t);
extern int delipsecprotobyname(const char *);
extern int delipsecprotobynum(int);
extern int *getipsecprotos(int *);
extern int *getipsecalgs(int *, int);
extern int list_ints(FILE *, int *);
extern const char *ipsecalgs_diag(int);
extern int ipsecproto_get_exec_mode(int, ipsecalgs_exec_mode_t *);
extern int ipsecproto_set_exec_mode(int, ipsecalgs_exec_mode_t);
/* Flags for add/delete routines. */
#define LIBIPSEC_ALGS_ADD_FORCE 0x00000001
/*
* Helper definitions for indices into array of key sizes when key sizes
* are defined by range.
*/
#define LIBIPSEC_ALGS_KEY_DEF_IDX 0 /* default key size */
#define LIBIPSEC_ALGS_KEY_MIN_IDX 1 /* min key size */
#define LIBIPSEC_ALGS_KEY_MAX_IDX 2 /* max key size */
#define LIBIPSEC_ALGS_KEY_NUM_VAL 4 /* def, min, max, 0 */
/* Error codes for IPsec algorithms management */
#define LIBIPSEC_ALGS_DIAG_ALG_EXISTS -1
#define LIBIPSEC_ALGS_DIAG_PROTO_EXISTS -2
#define LIBIPSEC_ALGS_DIAG_UNKN_PROTO -3
#define LIBIPSEC_ALGS_DIAG_UNKN_ALG -4
#define LIBIPSEC_ALGS_DIAG_NOMEM -5
#define LIBIPSEC_ALGS_DIAG_ALGSFILEOPEN -6
#define LIBIPSEC_ALGS_DIAG_ALGSFILEFDOPEN -7
#define LIBIPSEC_ALGS_DIAG_ALGSFILELOCK -8
#define LIBIPSEC_ALGS_DIAG_ALGSFILERENAME -9
#define LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE -10
#define LIBIPSEC_ALGS_DIAG_ALGSFILECHMOD -11
#define LIBIPSEC_ALGS_DIAG_ALGSFILECHOWN -12
#define LIBIPSEC_ALGS_DIAG_ALGSFILECLOSE -13
/* /etc/inet/ipsecalgs keywords and package sections delimiters */
#define LIBIPSEC_ALGS_LINE_PROTO "PROTO|"
#define LIBIPSEC_ALGS_LINE_ALG "ALG|"
#define LIBIPSEC_ALGS_LINE_PKGSTART "# Start "
#define LIBIPSEC_ALGS_LINE_PKGEND "# End "
/* Put these in libnsl for and process caching testing. */
extern int *_real_getipsecprotos(int *);
extern int *_real_getipsecalgs(int *, int);
extern struct ipsecalgent *_duplicate_alg(struct ipsecalgent *);
extern void _clean_trash(ipsec_proto_t *, int);
/* spdsock support functions */
/* Return values for spdsock_get_ext(). */
#define KGE_OK 0
#define KGE_DUP 1
#define KGE_UNK 2
#define KGE_LEN 3
#define KGE_CHK 4
extern int spdsock_get_ext(spd_ext_t *[], spd_msg_t *, uint_t, char *, uint_t);
extern const char *spdsock_diag(int);
/* PF_KEY (keysock) support functions */
extern const char *keysock_diag(int);
extern int in_masktoprefix(uint8_t *, boolean_t);
/* SA support functions */
extern char *secs2out(unsigned int, char *, int, int);
extern char *secs2str(unsigned int, char *, int);
extern char *bytecnt2out(uint64_t, char *, size_t, int);
extern char *bytecnt2str(uint64_t, char *, size_t);
extern void print_diagnostic(FILE *, uint16_t);
extern void print_sadb_msg(FILE *, struct sadb_msg *, time_t, boolean_t);
extern void print_sa(FILE *, char *, struct sadb_sa *);
extern void printsatime(FILE *, int64_t, const char *, const char *,
const char *, boolean_t);
extern void print_lifetimes(FILE *, time_t, struct sadb_lifetime *,
struct sadb_lifetime *, struct sadb_lifetime *, struct sadb_lifetime *,
boolean_t vflag);
extern void print_address(FILE *, char *, struct sadb_address *, boolean_t);
extern void print_asn1_name(FILE *, const unsigned char *, long);
extern void print_key(FILE *, char *, struct sadb_key *);
extern void print_keystr(FILE *, char *, struct sadb_key *);
extern void print_ident(FILE *, char *, struct sadb_ident *);
extern void print_sens(FILE *, char *, const struct sadb_sens *, boolean_t);
extern void print_prop(FILE *, char *, struct sadb_prop *);
extern void print_eprop(FILE *, char *, struct sadb_prop *);
extern void print_supp(FILE *, char *, struct sadb_supported *);
extern void print_spirange(FILE *, char *, struct sadb_spirange *);
extern void print_kmc(FILE *, char *, struct sadb_x_kmc *);
extern void print_samsg(FILE *, uint64_t *, boolean_t, boolean_t, boolean_t);
extern char *rparsesatype(int);
extern char *rparsealg(uint8_t, int);
extern const char *rparsetcpsigalg(uint8_t);
extern char *rparseidtype(uint16_t);
extern boolean_t save_lifetime(struct sadb_lifetime *, FILE *);
extern boolean_t save_address(struct sadb_address *, FILE *);
extern boolean_t save_key(struct sadb_key *, FILE *);
extern boolean_t save_keystr(struct sadb_key *, FILE *);
extern boolean_t save_ident(struct sadb_ident *, FILE *);
extern void save_assoc(uint64_t *, FILE *);
extern FILE *opensavefile(char *);
extern const char *do_inet_ntop(const void *, char *, size_t);
extern uint8_t gettcpsigalgbyname(const char *);
extern const char *gettcpsigalgbynum(uint8_t);
/*
* Label conversion convenience functions.
*/
#include <tsol/label.h>
extern void ipsec_convert_sens_to_bslabel(const struct sadb_sens *,
bslabel_t *);
extern int ipsec_convert_sl_to_sens(int doi, bslabel_t *, struct sadb_sens *);
extern void ipsec_convert_bslabel_to_string(bslabel_t *, char **);
extern void ipsec_convert_bslabel_to_hex(bslabel_t *, char **);
/*
* These exit macros give a consistent exit behaviour for all
* programs that use libipsecutil. These wll work in usr/src/cmd
* and usr/src/lib, but because macros in usr/src/lib don't get
* expanded when I18N message catalogs are built, avoid using
* these with text inside libipsecutil. See source of ipsecutil_exit()
* for more details.
*/
#define EXIT_OK(x) \
ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x))
#define EXIT_OK2(x, y) \
ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x), y)
#define EXIT_OK3(x, y, z) \
ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x), y, z)
#define EXIT_BADCONFIG(x) \
ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x))
#define EXIT_BADCONFIG2(x, y) \
ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x), y)
#define EXIT_BADCONFIG3(x, y, z) \
ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x), y, z)
#define EXIT_MAINTAIN(x) \
ipsecutil_exit(SERVICE_MAINTAIN, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x))
#define EXIT_MAINTAIN2(x, y) \
ipsecutil_exit(SERVICE_MAINTAIN, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x), y)
#define EXIT_DEGRADE(x) \
ipsecutil_exit(SERVICE_DEGRADE, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x))
#define EXIT_BADPERM(x) \
ipsecutil_exit(SERVICE_BADPERM, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x))
#define EXIT_BADPERM2(x, y) \
ipsecutil_exit(SERVICE_BADPERM, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x), y)
#define EXIT_FATAL(x) \
ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x))
#define EXIT_FATAL2(x, y) \
ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x), y)
#define EXIT_FATAL3(x, y, z) \
ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x), y, z)
#define EXIT_RESTART(x) \
ipsecutil_exit(SERVICE_RESTART, my_fmri, debugfile, \
dgettext(TEXT_DOMAIN, x))
#ifdef __cplusplus
}
#endif
#endif /* _IPSEC_UTIL_H */
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2024 Oxide Computer Company
#
#
# MAPFILE HEADER START
#
# WARNING: STOP NOW. DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
# usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#
$mapfile_version 2
SYMBOL_VERSION SUNWprivate_1.1 {
global:
addipsecalg;
addipsecproto;
bail;
bail_msg;
dbgstr2num;
delipsecalgbyname;
delipsecalgbynum;
delipsecprotobyname;
delipsecprotobynum;
do_inet_ntop;
do_interactive;
dump_aalg;
dump_ealg;
dump_tcpsigalg;
dump_key;
dump_keystr;
dump_sadb_idtype;
dump_sockaddr;
$if _x86 && _ELF64
env { ASSERT = { TYPE = OBJECT; SIZE = 64; }; };
$elif _x86 && _ELF32
env { ASSERT = { TYPE = OBJECT; SIZE = 40; }; };
$elif _sparc && _ELF64
env { ASSERT = { TYPE = OBJECT; SIZE = 96; }; };
$elif _sparc && _ELF32
env { ASSERT = { TYPE = OBJECT; SIZE = 48; }; };
$else
$error Unknown architecture
$endif
_errfp { TYPE = FUNCTION; FILTER = libc.so.1; };
_errxfp { TYPE = FUNCTION; FILTER = libc.so.1; };
getipsecalgs;
getipsecprotos;
gettcpsigalgbyname;
gettcpsigalgbynum;
in_masktoprefix;
interactive { ASSERT = { TYPE = OBJECT; SIZE = 4; }; };
ipsecalgs_diag;
ipsecproto_get_exec_mode;
ipsecproto_set_exec_mode;
ipsecutil_exit;
ipsec_convert_sens_to_bslabel;
ipsec_convert_bslabel_to_string;
ipsec_convert_bslabel_to_hex;
ipsec_convert_sl_to_sens;
keysock_diag;
kmc_insert_mapping;
kmc_lookup_by_cookie;
lineno { ASSERT = { TYPE = OBJECT; SIZE = 4; }; };
lines_added { ASSERT = { TYPE = OBJECT; SIZE = 4; }; };
lines_parsed { ASSERT = { TYPE = OBJECT; SIZE = 4; }; };
list_ints;
nflag { ASSERT = { TYPE = OBJECT; SIZE = 4; }; };
bytecnt2out;
bytecnt2str;
debugfile {
ASSERT = {
TYPE = OBJECT;
SIZE = addrsize;
};
};
my_fmri {
ASSERT = {
TYPE = OBJECT;
SIZE = addrsize;
};
};
opensavefile;
parsedbgopts;
pflag { ASSERT = { TYPE = OBJECT; SIZE = 4; }; };
print_address;
print_asn1_name;
print_diagnostic;
print_eprop;
print_ident;
print_key;
print_keystr;
print_kmc;
print_lifetimes;
print_prop;
print_sa;
print_sadb_msg;
print_samsg;
print_sens;
print_spirange;
print_supp;
printsatime;
privstr2num;
readfile { ASSERT = { TYPE = OBJECT; SIZE = 4; }; };
rparsealg;
rparsetcpsigalg;
rparseidtype;
rparsesatype;
save_address;
save_assoc;
save_ident;
save_key;
save_keystr;
save_lifetime;
secs2out;
secs2str;
spdsock_diag;
spdsock_get_ext;
_verrfp { TYPE = FUNCTION; FILTER = libc.so.1; };
_verrxfp { TYPE = FUNCTION; FILTER = libc.so.1; };
_vwarnfp { TYPE = FUNCTION; FILTER = libc.so.1; };
_vwarnxfp { TYPE = FUNCTION; FILTER = libc.so.1; };
_warnfp { TYPE = FUNCTION; FILTER = libc.so.1; };
_warnxfp { TYPE = FUNCTION; FILTER = libc.so.1; };
local:
*;
};
|