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
|
/*
* 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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2018, Joyent Inc.
* Copyright (c) 2015, 2016 by Delphix. All rights reserved.
* Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* This module contains functions used to bring up and tear down the
* Virtual Platform: [un]mounting file-systems, [un]plumbing network
* interfaces, [un]configuring devices, establishing resource controls,
* and creating/destroying the zone in the kernel. These actions, on
* the way up, ready the zone; on the way down, they halt the zone.
* See the much longer block comment at the beginning of zoneadmd.c
* for a bigger picture of how the whole program functions.
*
* This module also has primary responsibility for the layout of "scratch
* zones." These are mounted, but inactive, zones that are used during
* operating system upgrade and potentially other administrative action. The
* scratch zone environment is similar to the miniroot environment. The zone's
* actual root is mounted read-write on /a, and the standard paths (/usr,
* /sbin, /lib) all lead to read-only copies of the running system's binaries.
* This allows the administrative tools to manipulate the zone using "-R /a"
* without relying on any binaries in the zone itself.
*
* If the scratch zone is on an alternate root (Live Upgrade [LU] boot
* environment), then we must resolve the lofs mounts used there to uncover
* writable (unshared) resources. Shared resources, though, are always
* read-only. In addition, if the "same" zone with a different root path is
* currently running, then "/b" inside the zone points to the running zone's
* root. This allows LU to synchronize configuration files during the upgrade
* process.
*
* To construct this environment, this module creates a tmpfs mount on
* $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as
* described above is constructed on the fly. The zone is then created using
* $ZONEPATH/lu as the root.
*
* Note that scratch zones are inactive. The zone's bits are not running and
* likely cannot be run correctly until upgrade is done. Init is not running
* there, nor is SMF. Because of this, the "mounted" state of a scratch zone
* is not a part of the usual halt/ready/boot state machine.
*/
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sockio.h>
#include <sys/stropts.h>
#include <sys/conf.h>
#include <sys/systeminfo.h>
#include <sys/secflags.h>
#include <sys/vnic.h>
#include <libdlpi.h>
#include <libdllink.h>
#include <libdlvlan.h>
#include <libdlvnic.h>
#include <inet/tcp.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/route.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <rctl.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <wait.h>
#include <limits.h>
#include <libgen.h>
#include <libzfs.h>
#include <libdevinfo.h>
#include <zone.h>
#include <assert.h>
#include <libcontract.h>
#include <libcontract_priv.h>
#include <uuid/uuid.h>
#include <sys/mntio.h>
#include <sys/mnttab.h>
#include <sys/fs/autofs.h> /* for _autofssys() */
#include <sys/fs/lofs_info.h>
#include <sys/fs/zfs.h>
#include <pool.h>
#include <sys/pool.h>
#include <sys/priocntl.h>
#include <libbrand.h>
#include <sys/brand.h>
#include <libzonecfg.h>
#include <synch.h>
#include "zoneadmd.h"
#include <tsol/label.h>
#include <libtsnet.h>
#include <sys/priv.h>
#include <libinetutil.h>
#define V4_ADDR_LEN 32
#define V6_ADDR_LEN 128
#define RESOURCE_DEFAULT_OPTS \
MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
#define DFSTYPES "/etc/dfs/fstypes"
#define MAXTNZLEN 2048
/* Number of times to retry unmounting if it fails */
#define UMOUNT_RETRIES 30
#define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT)
/* a reasonable estimate for the number of lwps per process */
#define LWPS_PER_PROCESS 10
/* for routing socket */
static int rts_seqno = 0;
/* mangled zone name when mounting in an alternate root environment */
static char kernzone[ZONENAME_MAX];
/* array of cached mount entries for resolve_lofs */
static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
/* for Trusted Extensions */
static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
static int tsol_mounts(zlog_t *, char *, char *);
static void tsol_unmounts(zlog_t *, char *);
static m_label_t *zlabel = NULL;
static m_label_t *zid_label = NULL;
static priv_set_t *zprivs = NULL;
static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn";
typedef struct zone_proj_rctl_map {
char *zpr_zone_rctl;
char *zpr_project_rctl;
} zone_proj_rctl_map_t;
static zone_proj_rctl_map_t zone_proj_rctl_map[] = {
{"zone.max-msg-ids", "project.max-msg-ids"},
{"zone.max-sem-ids", "project.max-sem-ids"},
{"zone.max-shm-ids", "project.max-shm-ids"},
{"zone.max-shm-memory", "project.max-shm-memory"},
{NULL, NULL}
};
/* from libsocket, not in any header file */
extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
/* from zoneadmd */
extern char query_hook[];
extern char post_statechg_hook[];
/*
* For each "net" resource configured in zonecfg, we track a zone_addr_list_t
* node in a linked list that is sorted by linkid. The list is constructed as
* the xml configuration file is parsed, and the information
* contained in each node is added to the kernel before the zone is
* booted, to be retrieved and applied from within the exclusive-IP NGZ
* on boot.
*/
typedef struct zone_addr_list {
struct zone_addr_list *za_next;
datalink_id_t za_linkid; /* datalink_id_t of interface */
struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
} zone_addr_list_t;
/*
* An optimization for build_mnttable: reallocate (and potentially copy the
* data) only once every N times through the loop.
*/
#define MNTTAB_HUNK 32
/* some handy macros */
#define SIN(s) ((struct sockaddr_in *)s)
#define SIN6(s) ((struct sockaddr_in6 *)s)
/*
* Private autofs system call
*/
extern int _autofssys(int, void *);
static int
autofs_cleanup(zoneid_t zoneid)
{
/*
* Ask autofs to unmount all trigger nodes in the given zone.
*/
return (_autofssys(AUTOFS_UNMOUNTALL, (void *)((uintptr_t)zoneid)));
}
static void
free_mnttable(struct mnttab *mnt_array, uint_t nelem)
{
uint_t i;
if (mnt_array == NULL)
return;
for (i = 0; i < nelem; i++) {
free(mnt_array[i].mnt_mountp);
free(mnt_array[i].mnt_fstype);
free(mnt_array[i].mnt_special);
free(mnt_array[i].mnt_mntopts);
assert(mnt_array[i].mnt_time == NULL);
}
free(mnt_array);
}
/*
* Build the mount table for the zone rooted at "zroot", storing the resulting
* array of struct mnttabs in "mnt_arrayp" and the number of elements in the
* array in "nelemp".
*/
static int
build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
struct mnttab **mnt_arrayp, uint_t *nelemp)
{
struct mnttab mnt;
struct mnttab *mnts;
struct mnttab *mnp;
uint_t nmnt;
rewind(mnttab);
resetmnttab(mnttab);
nmnt = 0;
mnts = NULL;
while (getmntent(mnttab, &mnt) == 0) {
struct mnttab *tmp_array;
if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
continue;
if (nmnt % MNTTAB_HUNK == 0) {
tmp_array = realloc(mnts,
(nmnt + MNTTAB_HUNK) * sizeof (*mnts));
if (tmp_array == NULL) {
free_mnttable(mnts, nmnt);
return (-1);
}
mnts = tmp_array;
}
mnp = &mnts[nmnt++];
/*
* Zero out any fields we're not using.
*/
(void) memset(mnp, 0, sizeof (*mnp));
if (mnt.mnt_special != NULL)
mnp->mnt_special = strdup(mnt.mnt_special);
if (mnt.mnt_mntopts != NULL)
mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
mnp->mnt_mountp = strdup(mnt.mnt_mountp);
mnp->mnt_fstype = strdup(mnt.mnt_fstype);
if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
(mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
free_mnttable(mnts, nmnt);
return (-1);
}
}
*mnt_arrayp = mnts;
*nelemp = nmnt;
return (0);
}
/*
* This is an optimization. The resolve_lofs function is used quite frequently
* to manipulate file paths, and on a machine with a large number of zones,
* there will be a huge number of mounted file systems. Thus, we trigger a
* reread of the list of mount points
*/
static void
lofs_discard_mnttab(void)
{
free_mnttable(resolve_lofs_mnts,
resolve_lofs_mnt_max - resolve_lofs_mnts);
resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
}
static int
lofs_read_mnttab(zlog_t *zlogp)
{
FILE *mnttab;
uint_t nmnts;
if ((mnttab = fopen(MNTTAB, "r")) == NULL)
return (-1);
if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
&nmnts) == -1) {
(void) fclose(mnttab);
return (-1);
}
(void) fclose(mnttab);
resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
return (0);
}
/*
* This function loops over potential loopback mounts and symlinks in a given
* path and resolves them all down to an absolute path.
*/
void
resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
{
int len, arlen;
const char *altroot;
char tmppath[MAXPATHLEN];
boolean_t outside_altroot;
if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
return;
tmppath[len] = '\0';
(void) strlcpy(path, tmppath, sizeof (tmppath));
/* This happens once per zoneadmd operation. */
if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
return;
altroot = zonecfg_get_root();
arlen = strlen(altroot);
outside_altroot = B_FALSE;
for (;;) {
struct mnttab *mnp;
/* Search in reverse order to find longest match */
for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
mnp--) {
if (mnp->mnt_fstype == NULL ||
mnp->mnt_mountp == NULL ||
mnp->mnt_special == NULL)
continue;
len = strlen(mnp->mnt_mountp);
if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
(path[len] == '/' || path[len] == '\0'))
break;
}
if (mnp < resolve_lofs_mnts)
break;
/* If it's not a lofs then we're done */
if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
break;
if (outside_altroot) {
char *cp;
int olen = sizeof (MNTOPT_RO) - 1;
/*
* If we run into a read-only mount outside of the
* alternate root environment, then the user doesn't
* want this path to be made read-write.
*/
if (mnp->mnt_mntopts != NULL &&
(cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
NULL &&
(cp == mnp->mnt_mntopts || cp[-1] == ',') &&
(cp[olen] == '\0' || cp[olen] == ',')) {
break;
}
} else if (arlen > 0 &&
(strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
(mnp->mnt_special[arlen] != '\0' &&
mnp->mnt_special[arlen] != '/'))) {
outside_altroot = B_TRUE;
}
/* use temporary buffer because new path might be longer */
(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
mnp->mnt_special, path + len);
if ((len = resolvepath(tmppath, path, pathlen)) == -1)
break;
path[len] = '\0';
}
}
/*
* For a regular mount, check if a replacement lofs mount is needed because the
* referenced device is already mounted somewhere.
*/
static int
check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
{
struct mnttab *mnp;
zone_fsopt_t *optptr, *onext;
/* This happens once per zoneadmd operation. */
if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
return (-1);
/*
* If this special node isn't already in use, then it's ours alone;
* no need to worry about conflicting mounts.
*/
for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
mnp++) {
if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
break;
}
if (mnp >= resolve_lofs_mnt_max)
return (0);
/*
* Convert this duplicate mount into a lofs mount.
*/
(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
sizeof (fsptr->zone_fs_special));
(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
sizeof (fsptr->zone_fs_type));
fsptr->zone_fs_raw[0] = '\0';
/*
* Discard all but one of the original options and set that to our
* default set of options used for resources.
*/
optptr = fsptr->zone_fs_options;
if (optptr == NULL) {
optptr = malloc(sizeof (*optptr));
if (optptr == NULL) {
zerror(zlogp, B_TRUE, "cannot mount %s",
fsptr->zone_fs_dir);
return (-1);
}
} else {
while ((onext = optptr->zone_fsopt_next) != NULL) {
optptr->zone_fsopt_next = onext->zone_fsopt_next;
free(onext);
}
}
(void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
optptr->zone_fsopt_next = NULL;
fsptr->zone_fs_options = optptr;
return (0);
}
int
make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
uid_t userid, gid_t groupid)
{
char path[MAXPATHLEN];
struct stat st;
if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
sizeof (path)) {
zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
subdir);
return (-1);
}
if (lstat(path, &st) == 0) {
/*
* We don't check the file mode since presumably the zone
* administrator may have had good reason to change the mode,
* and we don't need to second guess them.
*/
if (!S_ISDIR(st.st_mode)) {
if (S_ISREG(st.st_mode)) {
/*
* Allow readonly mounts of /etc/ files; this
* is needed most by Trusted Extensions.
*/
if (strncmp(subdir, "/etc/",
strlen("/etc/")) != 0) {
zerror(zlogp, B_FALSE,
"%s is not in /etc", path);
return (-1);
}
} else {
zerror(zlogp, B_FALSE,
"%s is not a directory", path);
return (-1);
}
}
return (0);
}
if (mkdirp(path, mode) != 0) {
if (errno == EROFS)
zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
"a read-only file system in this local zone.\nMake "
"sure %s exists in the global zone.", path, subdir);
else
zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
return (-1);
}
(void) chown(path, userid, groupid);
return (0);
}
static void
free_remote_fstypes(char **types)
{
uint_t i;
if (types == NULL)
return;
for (i = 0; types[i] != NULL; i++)
free(types[i]);
free(types);
}
static char **
get_remote_fstypes(zlog_t *zlogp)
{
char **types = NULL;
FILE *fp;
char buf[MAXPATHLEN];
char fstype[MAXPATHLEN];
uint_t lines = 0;
uint_t i;
if ((fp = fopen(DFSTYPES, "r")) == NULL) {
zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
return (NULL);
}
/*
* Count the number of lines
*/
while (fgets(buf, sizeof (buf), fp) != NULL)
lines++;
if (lines == 0) /* didn't read anything; empty file */
goto out;
rewind(fp);
/*
* Allocate enough space for a NULL-terminated array.
*/
types = calloc(lines + 1, sizeof (char *));
if (types == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
goto out;
}
i = 0;
while (fgets(buf, sizeof (buf), fp) != NULL) {
/* LINTED - fstype is big enough to hold buf */
if (sscanf(buf, "%s", fstype) == 0) {
zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
free_remote_fstypes(types);
types = NULL;
goto out;
}
types[i] = strdup(fstype);
if (types[i] == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
free_remote_fstypes(types);
types = NULL;
goto out;
}
i++;
}
out:
(void) fclose(fp);
return (types);
}
static boolean_t
is_remote_fstype(const char *fstype, char *const *remote_fstypes)
{
uint_t i;
if (remote_fstypes == NULL)
return (B_FALSE);
for (i = 0; remote_fstypes[i] != NULL; i++) {
if (strcmp(remote_fstypes[i], fstype) == 0)
return (B_TRUE);
}
return (B_FALSE);
}
/*
* This converts a zone root path (normally of the form .../root) to a Live
* Upgrade scratch zone root (of the form .../lu).
*/
static void
root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
{
if (!isresolved && zonecfg_in_alt_root())
resolve_lofs(zlogp, zroot, zrootlen);
(void) strcpy(strrchr(zroot, '/') + 1, "lu");
}
/*
* Perform brand-specific cleanup if we are unable to unmount a FS.
*/
static void
brand_umount_cleanup(zlog_t *zlogp, char *path)
{
char cmdbuf[2 * MAXPATHLEN];
if (post_statechg_hook[0] == '\0')
return;
if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", post_statechg_hook,
ZONE_STATE_DOWN, Z_UNMOUNT, path) > sizeof (cmdbuf))
return;
(void) do_subproc(zlogp, cmdbuf, NULL, B_FALSE);
}
/*
* The general strategy for unmounting filesystems is as follows:
*
* - Remote filesystems may be dead, and attempting to contact them as
* part of a regular unmount may hang forever; we want to always try to
* forcibly unmount such filesystems and only fall back to regular
* unmounts if the filesystem doesn't support forced unmounts.
*
* - We don't want to unnecessarily corrupt metadata on local
* filesystems (ie UFS), so we want to start off with graceful unmounts,
* and only escalate to doing forced unmounts if we get stuck.
*
* We start off walking backwards through the mount table. This doesn't
* give us strict ordering but ensures that we try to unmount submounts
* first. We thus limit the number of failed umount2(2) calls.
*
* The mechanism for determining if we're stuck is to count the number
* of failed unmounts each iteration through the mount table. This
* gives us an upper bound on the number of filesystems which remain
* mounted (autofs trigger nodes are dealt with separately). If at the
* end of one unmount+autofs_cleanup cycle we still have the same number
* of mounts that we started out with, we're stuck and try a forced
* unmount. If that fails (filesystem doesn't support forced unmounts)
* then we bail and are unable to teardown the zone. If it succeeds,
* we're no longer stuck so we continue with our policy of trying
* graceful mounts first.
*
* Zone must be down (ie, no processes or threads active).
*/
static int
unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
{
int error = 0;
int fail = 0;
FILE *mnttab;
struct mnttab *mnts;
uint_t nmnt;
char zroot[MAXPATHLEN + 1];
size_t zrootlen;
uint_t oldcount = UINT_MAX;
boolean_t stuck = B_FALSE;
char **remote_fstypes = NULL;
if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
zerror(zlogp, B_FALSE, "unable to determine zone root");
return (-1);
}
if (unmount_cmd)
root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
(void) strcat(zroot, "/");
zrootlen = strlen(zroot);
/*
* For Trusted Extensions unmount each higher level zone's mount
* of our zone's /export/home
*/
if (!unmount_cmd)
tsol_unmounts(zlogp, zone_name);
if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
return (-1);
}
/*
* Use our hacky mntfs ioctl so we see everything, even mounts with
* MS_NOMNTTAB.
*/
if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
error++;
goto out;
}
/*
* Build the list of remote fstypes so we know which ones we
* should forcibly unmount.
*/
remote_fstypes = get_remote_fstypes(zlogp);
for (; /* ever */; ) {
uint_t newcount = 0;
boolean_t unmounted;
struct mnttab *mnp;
char *path;
uint_t i;
mnts = NULL;
nmnt = 0;
/*
* MNTTAB gives us a way to walk through mounted
* filesystems; we need to be able to walk them in
* reverse order, so we build a list of all mounted
* filesystems.
*/
if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
&nmnt) != 0) {
error++;
goto out;
}
for (i = 0; i < nmnt; i++) {
mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
path = mnp->mnt_mountp;
unmounted = B_FALSE;
/*
* Try forced unmount first for remote filesystems.
*
* Not all remote filesystems support forced unmounts,
* so if this fails (ENOTSUP) we'll continue on
* and try a regular unmount.
*/
if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
if (umount2(path, MS_FORCE) == 0)
unmounted = B_TRUE;
}
/*
* Try forced unmount if we're stuck.
*/
if (stuck) {
if (umount2(path, MS_FORCE) == 0) {
unmounted = B_TRUE;
stuck = B_FALSE;
fail = 0;
} else {
/*
* We may hit a failure here if there
* is an app in the GZ with an open
* pipe into the zone (commonly into
* the zone's /var/run). This type
* of app will notice the closed
* connection and cleanup, but it may
* take a while and we have no easy
* way to notice that. To deal with
* this case, we will wait and retry
* a few times before we give up.
*/
fail++;
if (fail < (UMOUNT_RETRIES - 1)) {
zerror(zlogp, B_FALSE,
"unable to unmount '%s', "
"retrying in 2 seconds",
path);
(void) sleep(2);
} else if (fail > UMOUNT_RETRIES) {
error++;
zerror(zlogp, B_FALSE,
"unmount of '%s' failed",
path);
free_mnttable(mnts, nmnt);
goto out;
} else {
/* Try the hook 2 times */
brand_umount_cleanup(zlogp,
path);
}
}
}
/*
* Try regular unmounts for everything else.
*/
if (!unmounted && umount2(path, 0) != 0)
newcount++;
}
free_mnttable(mnts, nmnt);
if (newcount == 0)
break;
if (newcount >= oldcount) {
/*
* Last round didn't unmount anything; we're stuck and
* should start trying forced unmounts.
*/
stuck = B_TRUE;
}
oldcount = newcount;
/*
* Autofs doesn't let you unmount its trigger nodes from
* userland so we have to tell the kernel to cleanup for us.
*/
if (autofs_cleanup(zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
error++;
goto out;
}
}
out:
free_remote_fstypes(remote_fstypes);
(void) fclose(mnttab);
return (error ? -1 : 0);
}
static int
fs_compare(const void *m1, const void *m2)
{
struct zone_fstab *i = (struct zone_fstab *)m1;
struct zone_fstab *j = (struct zone_fstab *)m2;
return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
}
/*
* Fork and exec (and wait for) the mentioned binary with the provided
* arguments. Returns (-1) if something went wrong with fork(2) or exec(2),
* returns the exit status otherwise.
*
* If we were unable to exec the provided pathname (for whatever
* reason), we return the special token ZEXIT_EXEC. The current value
* of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
* consumers of this function; any future consumers must make sure this
* remains the case.
*/
static int
forkexec(zlog_t *zlogp, const char *path, char *const argv[])
{
pid_t child_pid;
int child_status = 0;
/*
* Do not let another thread localize a message while we are forking.
*/
(void) mutex_lock(&msglock);
child_pid = fork();
(void) mutex_unlock(&msglock);
if (child_pid == -1) {
zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
return (-1);
} else if (child_pid == 0) {
closefrom(0);
/* redirect stdin, stdout & stderr to /dev/null */
(void) open("/dev/null", O_RDONLY); /* stdin */
(void) open("/dev/null", O_WRONLY); /* stdout */
(void) open("/dev/null", O_WRONLY); /* stderr */
(void) execv(path, argv);
/*
* Since we are in the child, there is no point calling zerror()
* since there is nobody waiting to consume it. So exit with a
* special code that the parent will recognize and call zerror()
* accordingly.
*/
_exit(ZEXIT_EXEC);
} else {
(void) waitpid(child_pid, &child_status, 0);
}
if (WIFSIGNALED(child_status)) {
zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
"signal %d", path, WTERMSIG(child_status));
return (-1);
}
assert(WIFEXITED(child_status));
if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
zerror(zlogp, B_FALSE, "failed to exec %s", path);
return (-1);
}
return (WEXITSTATUS(child_status));
}
static int
isregfile(const char *path)
{
struct stat64 st;
if (stat64(path, &st) == -1)
return (-1);
return (S_ISREG(st.st_mode));
}
static int
dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
{
char cmdbuf[MAXPATHLEN];
char *argv[5];
int status;
/*
* We could alternatively have called /usr/sbin/fsck -F <fstype>, but
* that would cost us an extra fork/exec without buying us anything.
*/
if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
>= sizeof (cmdbuf)) {
zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
return (-1);
}
/*
* If it doesn't exist, that's OK: we verified this previously
* in zoneadm.
*/
if (isregfile(cmdbuf) == -1)
return (0);
argv[0] = "fsck";
argv[1] = "-o";
argv[2] = "p";
argv[3] = (char *)rawdev;
argv[4] = NULL;
status = forkexec(zlogp, cmdbuf, argv);
if (status == 0 || status == -1)
return (status);
zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
"run fsck manually", rawdev, status);
return (-1);
}
static int
domount(zlog_t *zlogp, const char *fstype, const char *opts,
const char *special, const char *directory)
{
char cmdbuf[MAXPATHLEN];
char *argv[6];
int status;
/*
* We could alternatively have called /usr/sbin/mount -F <fstype>, but
* that would cost us an extra fork/exec without buying us anything.
*/
if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
>= sizeof (cmdbuf)) {
zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
return (-1);
}
argv[0] = "mount";
if (opts[0] == '\0') {
argv[1] = (char *)special;
argv[2] = (char *)directory;
argv[3] = NULL;
} else {
argv[1] = "-o";
argv[2] = (char *)opts;
argv[3] = (char *)special;
argv[4] = (char *)directory;
argv[5] = NULL;
}
status = forkexec(zlogp, cmdbuf, argv);
if (status == 0 || status == -1)
return (status);
if (opts[0] == '\0')
zerror(zlogp, B_FALSE, "\"%s %s %s\" "
"failed with exit code %d",
cmdbuf, special, directory, status);
else
zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
"failed with exit code %d",
cmdbuf, opts, special, directory, status);
return (-1);
}
/*
* Check if a given mount point path exists.
* If it does, make sure it doesn't contain any symlinks.
* Note that if "leaf" is false we're checking an intermediate
* component of the mount point path, so it must be a directory.
* If "leaf" is true, then we're checking the entire mount point
* path, so the mount point itself can be anything aside from a
* symbolic link.
*
* If the path is invalid then a negative value is returned. If the
* path exists and is a valid mount point path then 0 is returned.
* If the path doesn't exist return a positive value.
*/
static int
valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
{
struct stat statbuf;
char respath[MAXPATHLEN];
int res;
if (lstat(path, &statbuf) != 0) {
if (errno == ENOENT)
return (1);
zerror(zlogp, B_TRUE, "can't stat %s", path);
return (-1);
}
if (S_ISLNK(statbuf.st_mode)) {
zerror(zlogp, B_FALSE, "%s is a symlink", path);
return (-1);
}
if (!leaf && !S_ISDIR(statbuf.st_mode)) {
zerror(zlogp, B_FALSE, "%s is not a directory", path);
return (-1);
}
if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
return (-1);
}
respath[res] = '\0';
if (strcmp(path, respath) != 0) {
/*
* We don't like ".."s, "."s, or "//"s throwing us off
*/
zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
return (-1);
}
return (0);
}
/*
* Validate a mount point path. A valid mount point path is an
* absolute path that either doesn't exist, or, if it does exists it
* must be an absolute canonical path that doesn't have any symbolic
* links in it. The target of a mount point path can be any filesystem
* object. (Different filesystems can support different mount points,
* for example "lofs" and "mntfs" both support files and directories
* while "ufs" just supports directories.)
*
* If the path is invalid then a negative value is returned. If the
* path exists and is a valid mount point path then 0 is returned.
* If the path doesn't exist return a positive value.
*/
int
valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
const char *dir, const char *fstype)
{
char abspath[MAXPATHLEN], *slashp, *slashp_next;
int rv;
/*
* Sanity check the target mount point path.
* It must be a non-null string that starts with a '/'.
*/
if (dir[0] != '/') {
/* Something went wrong. */
zerror(zlogp, B_FALSE, "invalid mount directory, "
"type: \"%s\", special: \"%s\", dir: \"%s\"",
fstype, spec, dir);
return (-1);
}
/*
* Join rootpath and dir. Make sure abspath ends with '/', this
* is added to all paths (even non-directory paths) to allow us
* to detect the end of paths below. If the path already ends
* in a '/', then that's ok too (although we'll fail the
* cannonical path check in valid_mount_point()).
*/
if (snprintf(abspath, sizeof (abspath),
"%s%s/", rootpath, dir) >= sizeof (abspath)) {
zerror(zlogp, B_FALSE, "pathname %s%s is too long",
rootpath, dir);
return (-1);
}
/*
* Starting with rootpath, verify the mount path one component
* at a time. Continue until we've evaluated all of abspath.
*/
slashp = &abspath[strlen(rootpath)];
assert(*slashp == '/');
do {
slashp_next = strchr(slashp + 1, '/');
*slashp = '\0';
if (slashp_next != NULL) {
/* This is an intermediary mount path component. */
rv = valid_mount_point(zlogp, abspath, B_FALSE);
} else {
/* This is the last component of the mount path. */
rv = valid_mount_point(zlogp, abspath, B_TRUE);
}
if (rv < 0)
return (rv);
*slashp = '/';
} while ((slashp = slashp_next) != NULL);
return (rv);
}
static int
mount_one_dev_device_cb(void *arg, const char *match, const char *name)
{
di_prof_t prof = arg;
if (name == NULL)
return (di_prof_add_dev(prof, match));
return (di_prof_add_map(prof, match, name));
}
static int
mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
{
di_prof_t prof = arg;
return (di_prof_add_symlink(prof, source, target));
}
int
vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
{
if (zonecfg_get_iptype(snap_hndl, iptypep) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid ip-type configuration");
return (-1);
}
return (0);
}
/*
* Apply the standard lists of devices/symlinks/mappings and the user-specified
* list of devices (via zonecfg) to the /dev filesystem. The filesystem will
* use these as a profile/filter to determine what exists in /dev.
*/
static int
mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
{
char brand[MAXNAMELEN];
brand_handle_t bh = NULL;
struct zone_devtab ztab;
di_prof_t prof = NULL;
int err;
int retval = -1;
zone_iptype_t iptype;
const char *curr_iptype = NULL;
if (di_prof_init(devpath, &prof)) {
zerror(zlogp, B_TRUE, "failed to initialize profile");
goto cleanup;
}
/*
* Get a handle to the brand info for this zone.
* If we are mounting the zone, then we must always use the default
* brand device mounts.
*/
if (ALT_MOUNT(mount_cmd)) {
(void) strlcpy(brand, default_brand, sizeof (brand));
} else {
(void) strlcpy(brand, brand_name, sizeof (brand));
}
if ((bh = brand_open(brand)) == NULL) {
zerror(zlogp, B_FALSE, "unable to determine zone brand");
goto cleanup;
}
if (vplat_get_iptype(zlogp, &iptype) < 0) {
zerror(zlogp, B_TRUE, "unable to determine ip-type");
goto cleanup;
}
switch (iptype) {
case ZS_SHARED:
curr_iptype = "shared";
break;
case ZS_EXCLUSIVE:
curr_iptype = "exclusive";
break;
}
if (curr_iptype == NULL)
abort();
if (brand_platform_iter_devices(bh, zone_name,
mount_one_dev_device_cb, prof, curr_iptype) != 0) {
zerror(zlogp, B_TRUE, "failed to add standard device");
goto cleanup;
}
if (brand_platform_iter_link(bh,
mount_one_dev_symlink_cb, prof) != 0) {
zerror(zlogp, B_TRUE, "failed to add standard symlink");
goto cleanup;
}
/* Add user-specified devices and directories */
if ((err = zonecfg_setdevent(snap_hndl)) != 0) {
zerror(zlogp, B_FALSE, "%s: %s", zone_name,
zonecfg_strerror(err));
goto cleanup;
}
while (zonecfg_getdevent(snap_hndl, &ztab) == Z_OK) {
char path[MAXPATHLEN];
if ((err = resolve_device_match(zlogp, &ztab,
path, sizeof (path))) != Z_OK)
goto cleanup;
if (di_prof_add_dev(prof, path)) {
zerror(zlogp, B_TRUE, "failed to add "
"user-specified device '%s'", path);
goto cleanup;
}
}
(void) zonecfg_enddevent(snap_hndl);
/* Send profile to kernel */
if (di_prof_commit(prof)) {
zerror(zlogp, B_TRUE, "failed to commit profile");
goto cleanup;
}
retval = 0;
cleanup:
if (bh != NULL)
brand_close(bh);
if (prof)
di_prof_fini(prof);
return (retval);
}
static int
mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
zone_mnt_t mount_cmd)
{
char path[MAXPATHLEN];
char optstr[MAX_MNTOPT_STR];
zone_fsopt_t *optptr;
int rv;
if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
rootpath, fsptr->zone_fs_dir);
return (-1);
} else if (rv > 0) {
/* The mount point path doesn't exist, create it now. */
if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
DEFAULT_DIR_GROUP) != 0) {
zerror(zlogp, B_FALSE, "failed to create mount point");
return (-1);
}
/*
* Now this might seem weird, but we need to invoke
* valid_mount_path() again. Why? Because it checks
* to make sure that the mount point path is canonical,
* which it can only do if the path exists, so now that
* we've created the path we have to verify it again.
*/
if ((rv = valid_mount_path(zlogp, rootpath,
fsptr->zone_fs_special, fsptr->zone_fs_dir,
fsptr->zone_fs_type)) < 0) {
zerror(zlogp, B_FALSE,
"%s%s is not a valid mount point",
rootpath, fsptr->zone_fs_dir);
return (-1);
}
}
(void) snprintf(path, sizeof (path), "%s%s", rootpath,
fsptr->zone_fs_dir);
/*
* In general the strategy here is to do just as much verification as
* necessary to avoid crashing or otherwise doing something bad; if the
* administrator initiated the operation via zoneadm(1m), they'll get
* auto-verification which will let them know what's wrong. If they
* modify the zone configuration of a running zone, and don't attempt
* to verify that it's OK, then we won't crash but won't bother trying
* to be too helpful either. zoneadm verify is only a couple keystrokes
* away.
*/
if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
"invalid file-system type %s", fsptr->zone_fs_special,
fsptr->zone_fs_dir, fsptr->zone_fs_type);
return (-1);
}
/*
* If we're looking at an alternate root environment, then construct
* read-only loopback mounts as necessary. Note that any special
* paths for lofs zone mounts in an alternate root must have
* already been pre-pended with any alternate root path by the
* time we get here.
*/
if (zonecfg_in_alt_root()) {
struct stat64 st;
if (stat64(fsptr->zone_fs_special, &st) != -1 &&
S_ISBLK(st.st_mode)) {
/*
* If we're going to mount a block device we need
* to check if that device is already mounted
* somewhere else, and if so, do a lofs mount
* of the device instead of a direct mount
*/
if (check_lofs_needed(zlogp, fsptr) == -1)
return (-1);
} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
/*
* For lofs mounts, the special node is inside the
* alternate root. We need lofs resolution for
* this case in order to get at the underlying
* read-write path.
*/
resolve_lofs(zlogp, fsptr->zone_fs_special,
sizeof (fsptr->zone_fs_special));
}
}
/*
* Run 'fsck -m' if there's a device to fsck.
*/
if (fsptr->zone_fs_raw[0] != '\0' &&
dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
return (-1);
} else if (isregfile(fsptr->zone_fs_special) == 1 &&
dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
return (-1);
}
/*
* Build up mount option string.
*/
optstr[0] = '\0';
if (fsptr->zone_fs_options != NULL) {
(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
sizeof (optstr));
for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
optptr != NULL; optptr = optptr->zone_fsopt_next) {
(void) strlcat(optstr, ",", sizeof (optstr));
(void) strlcat(optstr, optptr->zone_fsopt_opt,
sizeof (optstr));
}
}
if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
fsptr->zone_fs_special, path)) != 0)
return (rv);
/*
* The mount succeeded. If this was not a mount of /dev then
* we're done.
*/
if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
return (0);
/*
* We just mounted an instance of a /dev filesystem, so now we
* need to configure it.
*/
return (mount_one_dev(zlogp, path, mount_cmd));
}
static void
free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
{
uint_t i;
if (fsarray == NULL)
return;
for (i = 0; i < nelem; i++)
zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
free(fsarray);
}
/*
* This function initiates the creation of a small Solaris Environment for
* scratch zone. The Environment creation process is split up into two
* functions(build_mounted_pre_var() and build_mounted_post_var()). It
* is done this way because:
* We need to have both /etc and /var in the root of the scratchzone.
* We loopback mount zone's own /etc and /var into the root of the
* scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
* need to delay the mount of /var till the zone's root gets populated.
* So mounting of localdirs[](/etc and /var) have been moved to the
* build_mounted_post_var() which gets called only after the zone
* specific filesystems are mounted.
*
* Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
* does not loopback mount the zone's own /etc and /var into the root of the
* scratch zone.
*/
static boolean_t
build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
{
char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
const char **cpp;
static const char *mkdirs[] = {
"/system", "/system/contract", "/system/object", "/proc",
"/dev", "/tmp", "/a", NULL
};
char *altstr;
FILE *fp;
uuid_t uuid;
resolve_lofs(zlogp, rootpath, rootlen);
(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
resolve_lofs(zlogp, luroot, lurootlen);
(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
(void) symlink("./usr/bin", tmp);
/*
* These are mostly special mount points; not handled here. (See
* zone_mount_early.)
*/
for (cpp = mkdirs; *cpp != NULL; cpp++) {
(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
if (mkdir(tmp, 0755) != 0) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
}
/*
* This is here to support lucopy. If there's an instance of this same
* zone on the current running system, then we mount its root up as
* read-only inside the scratch zone.
*/
(void) zonecfg_get_uuid(zone_name, uuid);
altstr = strdup(zonecfg_get_root());
if (altstr == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
return (B_FALSE);
}
zonecfg_set_root("");
(void) strlcpy(tmp, zone_name, sizeof (tmp));
(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
strcmp(fromdir, rootpath) != 0) {
(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
if (mkdir(tmp, 0755) != 0) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
tmp) != 0) {
zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
fromdir);
return (B_FALSE);
}
}
zonecfg_set_root(altstr);
free(altstr);
if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
zerror(zlogp, B_TRUE, "cannot open zone mapfile");
return (B_FALSE);
}
(void) ftruncate(fileno(fp), 0);
if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
}
zonecfg_close_scratch(fp);
(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
return (B_FALSE);
(void) strlcpy(rootpath, tmp, rootlen);
return (B_TRUE);
}
static boolean_t
build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
const char *luroot)
{
char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
const char **cpp;
const char **loopdirs;
const char **tmpdirs;
static const char *localdirs[] = {
"/etc", "/var", NULL
};
static const char *scr_loopdirs[] = {
"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
"/usr", NULL
};
static const char *upd_loopdirs[] = {
"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
"/usr", "/var", NULL
};
static const char *scr_tmpdirs[] = {
"/tmp", "/var/run", NULL
};
static const char *upd_tmpdirs[] = {
"/tmp", "/var/run", "/var/tmp", NULL
};
struct stat st;
if (mount_cmd == Z_MNT_SCRATCH) {
/*
* These are mounted read-write from the zone undergoing
* upgrade. We must be careful not to 'leak' things from the
* main system into the zone, and this accomplishes that goal.
*/
for (cpp = localdirs; *cpp != NULL; cpp++) {
(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
*cpp);
(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
rootpath, *cpp);
if (mkdir(tmp, 0755) != 0) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
!= 0) {
zerror(zlogp, B_TRUE, "cannot mount %s on %s",
tmp, *cpp);
return (B_FALSE);
}
}
}
if (mount_cmd == Z_MNT_UPDATE)
loopdirs = upd_loopdirs;
else
loopdirs = scr_loopdirs;
/*
* These are things mounted read-only from the running system because
* they contain binaries that must match system.
*/
for (cpp = loopdirs; *cpp != NULL; cpp++) {
(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
if (mkdir(tmp, 0755) != 0) {
if (errno != EEXIST) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
if (lstat(tmp, &st) != 0) {
zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
return (B_FALSE);
}
/*
* Ignore any non-directories encountered. These are
* things that have been converted into symlinks
* (/etc/fs and /etc/lib) and no longer need a lofs
* fixup.
*/
if (!S_ISDIR(st.st_mode))
continue;
}
if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
tmp) != 0) {
zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
*cpp);
return (B_FALSE);
}
}
if (mount_cmd == Z_MNT_UPDATE)
tmpdirs = upd_tmpdirs;
else
tmpdirs = scr_tmpdirs;
/*
* These are things with tmpfs mounted inside.
*/
for (cpp = tmpdirs; *cpp != NULL; cpp++) {
(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
errno != EEXIST) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
/*
* We could set the mode for /tmp when we do the mkdir but
* since that can be modified by the umask we will just set
* the correct mode for /tmp now.
*/
if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
return (B_FALSE);
}
if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
return (B_FALSE);
}
}
return (B_TRUE);
}
typedef struct plat_gmount_cb_data {
zlog_t *pgcd_zlogp;
struct zone_fstab **pgcd_fs_tab;
int *pgcd_num_fs;
} plat_gmount_cb_data_t;
/*
* plat_gmount_cb() is a callback function invoked by libbrand to iterate
* through all global brand platform mounts.
*/
int
plat_gmount_cb(void *data, const char *spec, const char *dir,
const char *fstype, const char *opt)
{
plat_gmount_cb_data_t *cp = data;
zlog_t *zlogp = cp->pgcd_zlogp;
struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab;
int num_fs = *cp->pgcd_num_fs;
struct zone_fstab *fsp, *tmp_ptr;
num_fs++;
if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
return (-1);
}
fs_ptr = tmp_ptr;
fsp = &fs_ptr[num_fs - 1];
/* update the callback struct passed in */
*cp->pgcd_fs_tab = fs_ptr;
*cp->pgcd_num_fs = num_fs;
fsp->zone_fs_raw[0] = '\0';
(void) strlcpy(fsp->zone_fs_special, spec,
sizeof (fsp->zone_fs_special));
(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
fsp->zone_fs_options = NULL;
if ((opt != NULL) &&
(zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
zerror(zlogp, B_FALSE, "error adding property");
return (-1);
}
return (0);
}
static int
mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
{
struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
int num_fs;
num_fs = *num_fsp;
fs_ptr = *fs_tabp;
if (zonecfg_setfsent(handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
return (-1);
}
while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
/*
* ZFS filesystems will not be accessible under an alternate
* root, since the pool will not be known. Ignore them in this
* case.
*/
if (ALT_MOUNT(mount_cmd) &&
strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
continue;
num_fs++;
if ((tmp_ptr = realloc(fs_ptr,
num_fs * sizeof (*tmp_ptr))) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
(void) zonecfg_endfsent(handle);
return (-1);
}
/* update the pointers passed in */
*fs_tabp = tmp_ptr;
*num_fsp = num_fs;
fs_ptr = tmp_ptr;
fsp = &fs_ptr[num_fs - 1];
(void) strlcpy(fsp->zone_fs_dir,
fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
sizeof (fsp->zone_fs_raw));
(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
sizeof (fsp->zone_fs_type));
fsp->zone_fs_options = fstab.zone_fs_options;
/*
* For all lofs mounts, make sure that the 'special'
* entry points inside the alternate root. The
* source path for a lofs mount in a given zone needs
* to be relative to the root of the boot environment
* that contains the zone. Note that we don't do this
* for non-lofs mounts since they will have a device
* as a backing store and device paths must always be
* specified relative to the current boot environment.
*/
fsp->zone_fs_special[0] = '\0';
if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
sizeof (fsp->zone_fs_special));
}
(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
sizeof (fsp->zone_fs_special));
}
(void) zonecfg_endfsent(handle);
return (0);
}
static int
mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
{
char rootpath[MAXPATHLEN];
char brand[MAXNAMELEN];
char luroot[MAXPATHLEN];
int i, num_fs = 0;
struct zone_fstab *fs_ptr = NULL;
zone_state_t zstate;
brand_handle_t bh;
plat_gmount_cb_data_t cb;
if (zone_get_state(zone_name, &zstate) != Z_OK ||
(zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
zerror(zlogp, B_FALSE,
"zone must be in '%s' or '%s' state to mount file-systems",
zone_state_str(ZONE_STATE_READY),
zone_state_str(ZONE_STATE_MOUNTED));
goto bad;
}
if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
zerror(zlogp, B_TRUE, "unable to determine zone root");
goto bad;
}
if (zonecfg_setfsent(snap_hndl) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
goto bad;
}
/*
* If we are mounting the zone, then we must always use the default
* brand global mounts.
*/
if (ALT_MOUNT(mount_cmd)) {
(void) strlcpy(brand, default_brand, sizeof (brand));
} else {
(void) strlcpy(brand, brand_name, sizeof (brand));
}
/* Get a handle to the brand info for this zone */
if ((bh = brand_open(brand)) == NULL) {
zerror(zlogp, B_FALSE, "unable to determine zone brand");
return (-1);
}
/*
* Get the list of global filesystems to mount from the brand
* configuration.
*/
cb.pgcd_zlogp = zlogp;
cb.pgcd_fs_tab = &fs_ptr;
cb.pgcd_num_fs = &num_fs;
if (brand_platform_iter_gmounts(bh, zone_name, zonepath,
plat_gmount_cb, &cb) != 0) {
zerror(zlogp, B_FALSE, "unable to mount filesystems");
brand_close(bh);
return (-1);
}
brand_close(bh);
/*
* Iterate through the rest of the filesystems. Sort them all,
* then mount them in sorted order. This is to make sure the
* higher level directories (e.g., /usr) get mounted before
* any beneath them (e.g., /usr/local).
*/
if (mount_filesystems_fsent(snap_hndl, zlogp, &fs_ptr, &num_fs,
mount_cmd) != 0)
goto bad;
/*
* Normally when we mount a zone all the zone filesystems
* get mounted relative to rootpath, which is usually
* <zonepath>/root. But when mounting a zone for administration
* purposes via the zone "mount" state, build_mounted_pre_var()
* updates rootpath to be <zonepath>/lu/a so we'll mount all
* the zones filesystems there instead.
*
* build_mounted_pre_var() and build_mounted_post_var() will
* also do some extra work to create directories and lofs mount
* a bunch of global zone file system paths into <zonepath>/lu.
*
* This allows us to be able to enter the zone (now rooted at
* <zonepath>/lu) and run the upgrade/patch tools that are in the
* global zone and have them upgrade the to-be-modified zone's
* files mounted on /a. (Which mirrors the existing standard
* upgrade environment.)
*
* There is of course one catch. When doing the upgrade
* we need <zoneroot>/lu/dev to be the /dev filesystem
* for the zone and we don't want to have any /dev filesystem
* mounted at <zoneroot>/lu/a/dev. Since /dev is specified
* as a normal zone filesystem by default we'll try to mount
* it at <zoneroot>/lu/a/dev, so we have to detect this
* case and instead mount it at <zoneroot>/lu/dev.
*
* All this work is done in three phases:
* 1) Create and populate lu directory (build_mounted_pre_var()).
* 2) Mount the required filesystems as per the zone configuration.
* 3) Set up the rest of the scratch zone environment
* (build_mounted_post_var()).
*/
if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
goto bad;
qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
for (i = 0; i < num_fs; i++) {
if (ALT_MOUNT(mount_cmd)) {
if (strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
size_t slen = strlen(rootpath) - 2;
/*
* By default we'll try to mount /dev
* as /a/dev but /dev is special and
* always goes at the top so strip the
* trailing '/a' from the rootpath.
*/
assert(strcmp(&rootpath[slen], "/a") == 0);
rootpath[slen] = '\0';
if (mount_one(zlogp, &fs_ptr[i], rootpath,
mount_cmd) != 0)
goto bad;
rootpath[slen] = '/';
continue;
} else if (strcmp(brand_name, default_brand) != 0) {
/*
* If mounting non-native brand, skip
* mounting global mounts and
* filesystem entries since they are
* only needed for native pkg upgrade
* tools.
*
* The only exception right now is
* /dev (handled above), which is
* needed in the luroot in order to
* zlogin -S into the zone.
*/
continue;
}
}
if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
goto bad;
}
if (ALT_MOUNT(mount_cmd) &&
!build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
goto bad;
/*
* For Trusted Extensions cross-mount each lower level /export/home
*/
if (mount_cmd == Z_MNT_BOOT &&
tsol_mounts(zlogp, zone_name, rootpath) != 0)
goto bad;
free_fs_data(fs_ptr, num_fs);
/*
* Everything looks fine.
*/
return (0);
bad:
free_fs_data(fs_ptr, num_fs);
return (-1);
}
/* caller makes sure neither parameter is NULL */
static int
addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
{
int prefixlen;
prefixlen = atoi(prefixstr);
if (prefixlen < 0 || prefixlen > maxprefixlen)
return (1);
while (prefixlen > 0) {
if (prefixlen >= 8) {
*maskstr++ = 0xFF;
prefixlen -= 8;
continue;
}
*maskstr |= 1 << (8 - prefixlen);
prefixlen--;
}
return (0);
}
/*
* Tear down all interfaces belonging to the given zone. This should
* be called with the zone in a state other than "running", so that
* interfaces can't be assigned to the zone after this returns.
*
* If anything goes wrong, log an error message and return an error.
*/
static int
unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
{
struct lifnum lifn;
struct lifconf lifc;
struct lifreq *lifrp, lifrl;
int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
int num_ifs, s, i, ret_code = 0;
uint_t bufsize;
char *buf = NULL;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
zerror(zlogp, B_TRUE, "could not get socket");
ret_code = -1;
goto bad;
}
lifn.lifn_family = AF_UNSPEC;
lifn.lifn_flags = (int)lifc_flags;
if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
zerror(zlogp, B_TRUE,
"could not determine number of network interfaces");
ret_code = -1;
goto bad;
}
num_ifs = lifn.lifn_count;
bufsize = num_ifs * sizeof (struct lifreq);
if ((buf = malloc(bufsize)) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
ret_code = -1;
goto bad;
}
lifc.lifc_family = AF_UNSPEC;
lifc.lifc_flags = (int)lifc_flags;
lifc.lifc_len = bufsize;
lifc.lifc_buf = buf;
if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
zerror(zlogp, B_TRUE, "could not get configured network "
"interfaces");
ret_code = -1;
goto bad;
}
lifrp = lifc.lifc_req;
for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
(void) close(s);
if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
0) {
zerror(zlogp, B_TRUE, "%s: could not get socket",
lifrl.lifr_name);
ret_code = -1;
continue;
}
(void) memset(&lifrl, 0, sizeof (lifrl));
(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
sizeof (lifrl.lifr_name));
if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
if (errno == ENXIO)
/*
* Interface may have been removed by admin or
* another zone halting.
*/
continue;
zerror(zlogp, B_TRUE,
"%s: could not determine the zone to which this "
"network interface is bound", lifrl.lifr_name);
ret_code = -1;
continue;
}
if (lifrl.lifr_zoneid == zone_id) {
if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
zerror(zlogp, B_TRUE,
"%s: could not remove network interface",
lifrl.lifr_name);
ret_code = -1;
continue;
}
}
}
bad:
if (s > 0)
(void) close(s);
if (buf)
free(buf);
return (ret_code);
}
static union sockunion {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_dl sdl;
struct sockaddr_in6 sin6;
} so_dst, so_ifp;
static struct {
struct rt_msghdr hdr;
char space[512];
} rtmsg;
static int
salen(struct sockaddr *sa)
{
switch (sa->sa_family) {
case AF_INET:
return (sizeof (struct sockaddr_in));
case AF_LINK:
return (sizeof (struct sockaddr_dl));
case AF_INET6:
return (sizeof (struct sockaddr_in6));
default:
return (sizeof (struct sockaddr));
}
}
#define ROUNDUP_LONG(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
/*
* Look up which zone is using a given IP address. The address in question
* is expected to have been stuffed into the structure to which lifr points
* via a previous SIOCGLIFADDR ioctl().
*
* This is done using black router socket magic.
*
* Return the name of the zone on success or NULL on failure.
*
* This is a lot of code for a simple task; a new ioctl request to take care
* of this might be a useful RFE.
*/
static char *
who_is_using(zlog_t *zlogp, struct lifreq *lifr)
{
static char answer[ZONENAME_MAX];
pid_t pid;
int s, rlen, l, i;
char *cp = rtmsg.space;
struct sockaddr_dl *ifp = NULL;
struct sockaddr *sa;
char save_if_name[LIFNAMSIZ];
answer[0] = '\0';
pid = getpid();
if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
zerror(zlogp, B_TRUE, "could not get routing socket");
return (NULL);
}
if (lifr->lifr_addr.ss_family == AF_INET) {
struct sockaddr_in *sin4;
so_dst.sa.sa_family = AF_INET;
sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
so_dst.sin.sin_addr = sin4->sin_addr;
} else {
struct sockaddr_in6 *sin6;
so_dst.sa.sa_family = AF_INET6;
sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
so_dst.sin6.sin6_addr = sin6->sin6_addr;
}
so_ifp.sa.sa_family = AF_LINK;
(void) memset(&rtmsg, 0, sizeof (rtmsg));
rtmsg.hdr.rtm_type = RTM_GET;
rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
rtmsg.hdr.rtm_version = RTM_VERSION;
rtmsg.hdr.rtm_seq = ++rts_seqno;
rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
l = ROUNDUP_LONG(salen(&so_dst.sa));
(void) memmove(cp, &(so_dst), l);
cp += l;
l = ROUNDUP_LONG(salen(&so_ifp.sa));
(void) memmove(cp, &(so_ifp), l);
cp += l;
rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
if ((rlen = write(s, &rtmsg, l)) < 0) {
zerror(zlogp, B_TRUE, "writing to routing socket");
return (NULL);
} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
zerror(zlogp, B_TRUE,
"write to routing socket got only %d for len\n", rlen);
return (NULL);
}
do {
l = read(s, &rtmsg, sizeof (rtmsg));
} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
rtmsg.hdr.rtm_pid != pid));
if (l < 0) {
zerror(zlogp, B_TRUE, "reading from routing socket");
return (NULL);
}
if (rtmsg.hdr.rtm_version != RTM_VERSION) {
zerror(zlogp, B_FALSE,
"routing message version %d not understood",
rtmsg.hdr.rtm_version);
return (NULL);
}
if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
zerror(zlogp, B_FALSE, "message length mismatch, "
"expected %d bytes, returned %d bytes",
rtmsg.hdr.rtm_msglen, l);
return (NULL);
}
if (rtmsg.hdr.rtm_errno != 0) {
errno = rtmsg.hdr.rtm_errno;
zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
return (NULL);
}
if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
zerror(zlogp, B_FALSE, "network interface not found");
return (NULL);
}
cp = ((char *)(&rtmsg.hdr + 1));
for (i = 1; i != 0; i <<= 1) {
/* LINTED E_BAD_PTR_CAST_ALIGN */
sa = (struct sockaddr *)cp;
if (i != RTA_IFP) {
if ((i & rtmsg.hdr.rtm_addrs) != 0)
cp += ROUNDUP_LONG(salen(sa));
continue;
}
if (sa->sa_family == AF_LINK &&
((struct sockaddr_dl *)sa)->sdl_nlen != 0)
ifp = (struct sockaddr_dl *)sa;
break;
}
if (ifp == NULL) {
zerror(zlogp, B_FALSE, "network interface could not be "
"determined");
return (NULL);
}
/*
* We need to set the I/F name to what we got above, then do the
* appropriate ioctl to get its zone name. But lifr->lifr_name is
* used by the calling function to do a REMOVEIF, so if we leave the
* "good" zone's I/F name in place, *that* I/F will be removed instead
* of the bad one. So we save the old (bad) I/F name before over-
* writing it and doing the ioctl, then restore it after the ioctl.
*/
(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
lifr->lifr_name[ifp->sdl_nlen] = '\0';
i = ioctl(s, SIOCGLIFZONE, lifr);
(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
if (i < 0) {
zerror(zlogp, B_TRUE,
"%s: could not determine the zone network interface "
"belongs to", lifr->lifr_name);
return (NULL);
}
if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
(void) snprintf(answer, sizeof (answer), "%d",
lifr->lifr_zoneid);
if (strlen(answer) > 0)
return (answer);
return (NULL);
}
/*
* Configures a single interface: a new virtual interface is added, based on
* the physical interface nwiftabptr->zone_nwif_physical, with the address
* specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that
* the "address" can be an IPv6 address (with a /prefixlength required), an
* IPv4 address (with a /prefixlength optional), or a name; for the latter,
* an IPv4 name-to-address resolution will be attempted.
*
* If anything goes wrong, we log an detailed error message, attempt to tear
* down whatever we set up and return an error.
*/
static int
configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
struct zone_nwiftab *nwiftabptr)
{
struct lifreq lifr;
struct sockaddr_in netmask4;
struct sockaddr_in6 netmask6;
struct sockaddr_storage laddr;
struct in_addr in4;
sa_family_t af;
char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
int s;
boolean_t got_netmask = B_FALSE;
boolean_t is_loopback = B_FALSE;
char addrstr4[INET_ADDRSTRLEN];
int res;
res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
if (res != Z_OK) {
zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
nwiftabptr->zone_nwif_address);
return (-1);
}
af = lifr.lifr_addr.ss_family;
if (af == AF_INET)
in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
zerror(zlogp, B_TRUE, "could not get socket");
return (-1);
}
/*
* This is a similar kind of "hack" like in addif() to get around
* the problem of SIOCLIFADDIF. The problem is that this ioctl
* does not include the netmask when adding a logical interface.
* To get around this problem, we first add the logical interface
* with a 0 address. After that, we set the netmask if provided.
* Finally we set the interface address.
*/
laddr = lifr.lifr_addr;
(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
sizeof (lifr.lifr_name));
(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
/*
* Here, we know that the interface can't be brought up.
*/
(void) close(s);
return (Z_OK);
}
/* Preserve literal IPv4 address for later potential printing. */
if (af == AF_INET)
(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
lifr.lifr_zoneid = zone_id;
if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not place network interface "
"into zone", lifr.lifr_name);
goto bad;
}
/*
* Loopback interface will use the default netmask assigned, if no
* netmask is found.
*/
if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
is_loopback = B_TRUE;
}
if (af == AF_INET) {
/*
* The IPv4 netmask can be determined either
* directly if a prefix length was supplied with
* the address or via the netmasks database. Not
* being able to determine it is a common failure,
* but it often is not fatal to operation of the
* interface. In that case, a warning will be
* printed after the rest of the interface's
* parameters have been configured.
*/
(void) memset(&netmask4, 0, sizeof (netmask4));
if (slashp != NULL) {
if (addr2netmask(slashp + 1, V4_ADDR_LEN,
(uchar_t *)&netmask4.sin_addr) != 0) {
*slashp = '/';
zerror(zlogp, B_FALSE,
"%s: invalid prefix length in %s",
lifr.lifr_name,
nwiftabptr->zone_nwif_address);
goto bad;
}
got_netmask = B_TRUE;
} else if (getnetmaskbyaddr(in4,
&netmask4.sin_addr) == 0) {
got_netmask = B_TRUE;
}
if (got_netmask) {
netmask4.sin_family = af;
(void) memcpy(&lifr.lifr_addr, &netmask4,
sizeof (netmask4));
}
} else {
(void) memset(&netmask6, 0, sizeof (netmask6));
if (addr2netmask(slashp + 1, V6_ADDR_LEN,
(uchar_t *)&netmask6.sin6_addr) != 0) {
*slashp = '/';
zerror(zlogp, B_FALSE,
"%s: invalid prefix length in %s",
lifr.lifr_name,
nwiftabptr->zone_nwif_address);
goto bad;
}
got_netmask = B_TRUE;
netmask6.sin6_family = af;
(void) memcpy(&lifr.lifr_addr, &netmask6,
sizeof (netmask6));
}
if (got_netmask &&
ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not set netmask",
lifr.lifr_name);
goto bad;
}
/* Set the interface address */
lifr.lifr_addr = laddr;
if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE,
"%s: could not set IP address to %s",
lifr.lifr_name, nwiftabptr->zone_nwif_address);
goto bad;
}
if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not get flags",
lifr.lifr_name);
goto bad;
}
lifr.lifr_flags |= IFF_UP;
if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
int save_errno = errno;
char *zone_using;
/*
* If we failed with something other than EADDRNOTAVAIL,
* then skip to the end. Otherwise, look up our address,
* then call a function to determine which zone is already
* using that address.
*/
if (errno != EADDRNOTAVAIL) {
zerror(zlogp, B_TRUE,
"%s: could not bring network interface up",
lifr.lifr_name);
goto bad;
}
if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not get address",
lifr.lifr_name);
goto bad;
}
zone_using = who_is_using(zlogp, &lifr);
errno = save_errno;
if (zone_using == NULL)
zerror(zlogp, B_TRUE,
"%s: could not bring network interface up",
lifr.lifr_name);
else
zerror(zlogp, B_TRUE, "%s: could not bring network "
"interface up: address in use by zone '%s'",
lifr.lifr_name, zone_using);
goto bad;
}
if (!got_netmask && !is_loopback) {
/*
* A common, but often non-fatal problem, is that the system
* cannot find the netmask for an interface address. This is
* often caused by it being only in /etc/inet/netmasks, but
* /etc/nsswitch.conf says to use NIS or NIS+ and it's not
* in that. This doesn't show up at boot because the netmask
* is obtained from /etc/inet/netmasks when no network
* interfaces are up, but isn't consulted when NIS/NIS+ is
* available. We warn the user here that something like this
* has happened and we're just running with a default and
* possible incorrect netmask.
*/
char buffer[INET6_ADDRSTRLEN];
void *addr;
const char *nomatch = "no matching subnet found in netmasks(4)";
if (af == AF_INET)
addr = &((struct sockaddr_in *)
(&lifr.lifr_addr))->sin_addr;
else
addr = &((struct sockaddr_in6 *)
(&lifr.lifr_addr))->sin6_addr;
/*
* Find out what netmask the interface is going to be using.
* If we just brought up an IPMP data address on an underlying
* interface above, the address will have already migrated, so
* the SIOCGLIFNETMASK won't be able to find it (but we need
* to bring the address up to get the actual netmask). Just
* omit printing the actual netmask in this corner-case.
*/
if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
nomatch);
} else {
zerror(zlogp, B_FALSE,
"WARNING: %s: %s: %s; using default of %s.",
lifr.lifr_name, nomatch, addrstr4, buffer);
}
}
/*
* If a default router was specified for this interface
* set the route now. Ignore if already set.
*/
if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
int status;
char *argv[7];
argv[0] = "route";
argv[1] = "add";
argv[2] = "-ifp";
argv[3] = nwiftabptr->zone_nwif_physical;
argv[4] = "default";
argv[5] = nwiftabptr->zone_nwif_defrouter;
argv[6] = NULL;
status = forkexec(zlogp, "/usr/sbin/route", argv);
if (status != 0 && status != EEXIST)
zerror(zlogp, B_FALSE, "Unable to set route for "
"interface %s to %s\n",
nwiftabptr->zone_nwif_physical,
nwiftabptr->zone_nwif_defrouter);
}
(void) close(s);
return (Z_OK);
bad:
(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
(void) close(s);
return (-1);
}
/*
* Sets up network interfaces based on information from the zone configuration.
* IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
* system.
*
* If anything goes wrong, we log a general error message, attempt to tear down
* whatever we set up, and return an error.
*/
static int
configure_shared_network_interfaces(zlog_t *zlogp)
{
struct zone_nwiftab nwiftab, loopback_iftab;
zoneid_t zoneid;
if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
zerror(zlogp, B_TRUE, "unable to get zoneid");
return (-1);
}
if (zonecfg_setnwifent(snap_hndl) == Z_OK) {
for (;;) {
if (zonecfg_getnwifent(snap_hndl, &nwiftab) != Z_OK)
break;
nwifent_free_attrs(&nwiftab);
if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
Z_OK) {
(void) zonecfg_endnwifent(snap_hndl);
return (-1);
}
}
(void) zonecfg_endnwifent(snap_hndl);
}
if (is_system_labeled()) {
/*
* Labeled zones share the loopback interface
* so it is not plumbed for shared stack instances.
*/
return (0);
}
(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
sizeof (loopback_iftab.zone_nwif_physical));
(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
sizeof (loopback_iftab.zone_nwif_address));
loopback_iftab.zone_nwif_defrouter[0] = '\0';
if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
return (-1);
/* Always plumb up the IPv6 loopback interface. */
(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
sizeof (loopback_iftab.zone_nwif_address));
if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
return (-1);
return (0);
}
static void
zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
{
char errmsg[DLADM_STRSIZE];
(void) dladm_status2str(err, errmsg);
zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
}
static int
add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
{
dladm_status_t err;
boolean_t cpuset, poolset;
char *poolp;
/* First check if it's in use by global zone. */
if (zonecfg_ifname_exists(AF_INET, dlname) ||
zonecfg_ifname_exists(AF_INET6, dlname)) {
zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
"'%s' which is used in the global zone", dlname);
return (-1);
}
/* Set zoneid of this link. */
err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
DLADM_OPT_ACTIVE);
if (err != DLADM_STATUS_OK) {
zdlerror(zlogp, err, dlname,
"WARNING: unable to add network interface");
return (-1);
}
/*
* Set the pool of this link if the zone has a pool and
* neither the cpus nor the pool datalink property is
* already set.
*/
err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
"cpus", &cpuset);
if (err != DLADM_STATUS_OK) {
zdlerror(zlogp, err, dlname,
"WARNING: unable to check if cpus link property is set");
}
err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
"pool", &poolset);
if (err != DLADM_STATUS_OK) {
zdlerror(zlogp, err, dlname,
"WARNING: unable to check if pool link property is set");
}
if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
poolp = pool_name;
err = dladm_set_linkprop(dld_handle, linkid, "pool",
&poolp, 1, DLADM_OPT_ACTIVE);
if (err != DLADM_STATUS_OK) {
zerror(zlogp, B_FALSE, "WARNING: unable to set "
"pool %s to datalink %s", pool_name, dlname);
bzero(pool_name, sizeof (pool_name));
}
} else {
bzero(pool_name, sizeof (pool_name));
}
return (0);
}
static boolean_t
sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
char *straddr, size_t len)
{
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
const char *str = NULL;
if (af == AF_INET) {
/* LINTED E_BAD_PTR_CAST_ALIGN */
sin = SIN(sockaddr);
str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
} else if (af == AF_INET6) {
/* LINTED E_BAD_PTR_CAST_ALIGN */
sin6 = SIN6(sockaddr);
str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
len);
}
return (str != NULL);
}
static int
ipv4_prefixlen(struct sockaddr_in *sin)
{
struct sockaddr_in *m;
struct sockaddr_storage mask;
m = SIN(&mask);
m->sin_family = AF_INET;
if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
return (mask2plen((struct sockaddr *)&mask));
} else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
return (8);
} else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
return (16);
} else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
return (24);
}
return (0);
}
static int
zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
void *buf, size_t bufsize)
{
zone_net_data_t *zndata;
size_t znsize;
int err;
znsize = sizeof (*zndata) + bufsize;
zndata = calloc(1, znsize);
if (zndata == NULL)
return (ENOMEM);
zndata->zn_type = type;
zndata->zn_len = bufsize;
zndata->zn_linkid = linkid;
bcopy(buf, zndata->zn_val, zndata->zn_len);
err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
free(zndata);
return (err);
}
static int
add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
{
struct lifreq lifr;
char **astr, *address;
dladm_status_t dlstatus;
char *ip_nospoof = "ip-nospoof";
int nnet, naddr, err = 0, j;
size_t zlen, cpleft;
zone_addr_list_t *ptr, *end;
char tmp[INET6_ADDRSTRLEN], *maskstr;
char *zaddr, *cp;
struct in6_addr *routes = NULL;
boolean_t is_set;
datalink_id_t linkid;
assert(start != NULL);
naddr = 0; /* number of addresses */
nnet = 0; /* number of net resources */
linkid = start->za_linkid;
for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
ptr = ptr->za_next) {
nnet++;
}
end = ptr;
zlen = nnet * (INET6_ADDRSTRLEN + 1);
astr = calloc(1, nnet * sizeof (uintptr_t));
zaddr = calloc(1, zlen);
if (astr == NULL || zaddr == NULL) {
err = ENOMEM;
goto done;
}
cp = zaddr;
cpleft = zlen;
j = 0;
for (ptr = start; ptr != end; ptr = ptr->za_next) {
address = ptr->za_nwiftab.zone_nwif_allowed_address;
if (address[0] == '\0')
continue;
(void) snprintf(tmp, sizeof (tmp), "%s", address);
/*
* Validate the data. zonecfg_valid_net_address() clobbers
* the /<mask> in the address string.
*/
if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid address [%s]\n",
address);
err = EINVAL;
goto done;
}
/*
* convert any hostnames to numeric address strings.
*/
if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
(const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
err = EINVAL;
goto done;
}
/*
* make a copy of the numeric string for the data needed
* by the "allowed-ips" datalink property.
*/
astr[j] = strdup(cp);
if (astr[j] == NULL) {
err = ENOMEM;
goto done;
}
j++;
/*
* compute the default netmask from the address, if necessary
*/
if ((maskstr = strchr(tmp, '/')) == NULL) {
int prefixlen;
if (lifr.lifr_addr.ss_family == AF_INET) {
prefixlen = ipv4_prefixlen(
SIN(&lifr.lifr_addr));
} else {
struct sockaddr_in6 *sin6;
sin6 = SIN6(&lifr.lifr_addr);
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
prefixlen = 10;
else
prefixlen = 64;
}
(void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
maskstr = tmp;
} else {
maskstr++;
}
/* append the "/<netmask>" */
(void) strlcat(cp, "/", cpleft);
(void) strlcat(cp, maskstr, cpleft);
(void) strlcat(cp, ",", cpleft);
cp += strnlen(cp, zlen);
cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
}
naddr = j; /* the actual number of addresses in the net resource */
assert(naddr <= nnet);
/*
* zonecfg has already verified that the defrouter property can only
* be set if there is at least one address defined for the net resource.
* If j is 0, there are no addresses defined, and therefore no routers
* to configure, and we are done at that point.
*/
if (j == 0)
goto done;
/* over-write last ',' with '\0' */
zaddr[strnlen(zaddr, zlen) - 1] = '\0';
/*
* First make sure L3 protection is not already set on the link.
*/
dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
"protection", &is_set);
if (dlstatus != DLADM_STATUS_OK) {
err = EINVAL;
zerror(zlogp, B_FALSE, "unable to check if protection is set");
goto done;
}
if (is_set) {
err = EINVAL;
zerror(zlogp, B_FALSE, "Protection is already set");
goto done;
}
dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
"allowed-ips", &is_set);
if (dlstatus != DLADM_STATUS_OK) {
err = EINVAL;
zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
goto done;
}
if (is_set) {
zerror(zlogp, B_FALSE, "allowed-ips is already set");
err = EINVAL;
goto done;
}
/*
* Enable ip-nospoof for the link, and add address to the allowed-ips
* list.
*/
dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
&ip_nospoof, 1, DLADM_OPT_ACTIVE);
if (dlstatus != DLADM_STATUS_OK) {
zerror(zlogp, B_FALSE, "could not set protection\n");
err = EINVAL;
goto done;
}
dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
astr, naddr, DLADM_OPT_ACTIVE);
if (dlstatus != DLADM_STATUS_OK) {
zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
err = EINVAL;
goto done;
}
/* now set the address in the data-store */
err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
zaddr, strnlen(zaddr, zlen) + 1);
if (err != 0)
goto done;
/*
* add the defaultrouters
*/
routes = calloc(1, nnet * sizeof (*routes));
j = 0;
for (ptr = start; ptr != end; ptr = ptr->za_next) {
address = ptr->za_nwiftab.zone_nwif_defrouter;
if (address[0] == '\0')
continue;
if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
/*
* zonecfg_valid_net_address() expects numeric IPv6
* addresses to have a CIDR format netmask.
*/
(void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
(void) strlcat(address, tmp, INET6_ADDRSTRLEN);
}
if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
zerror(zlogp, B_FALSE,
"invalid router [%s]\n", address);
err = EINVAL;
goto done;
}
if (lifr.lifr_addr.ss_family == AF_INET6) {
routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
} else {
IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
&routes[j]);
}
j++;
}
assert(j <= nnet);
if (j > 0) {
err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
linkid, routes, j * sizeof (*routes));
}
done:
free(routes);
for (j = 0; j < naddr; j++)
free(astr[j]);
free(astr);
free(zaddr);
return (err);
}
static int
add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
{
zone_addr_list_t *ptr;
datalink_id_t linkid;
int err;
if (zalist == NULL)
return (0);
linkid = zalist->za_linkid;
err = add_net_for_linkid(zlogp, zoneid, zalist);
if (err != 0)
return (err);
for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
if (ptr->za_linkid == linkid)
continue;
linkid = ptr->za_linkid;
err = add_net_for_linkid(zlogp, zoneid, ptr);
if (err != 0)
return (err);
}
return (0);
}
/*
* Add "new" to the list of network interfaces to be configured by
* add_net on zone boot in "old". The list of interfaces in "old" is
* sorted by datalink_id_t, with interfaces sorted FIFO for a given
* datalink_id_t.
*
* Returns the merged list of IP interfaces containing "old" and "new"
*/
static zone_addr_list_t *
add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
{
zone_addr_list_t *ptr, *next;
datalink_id_t linkid = new->za_linkid;
assert(old != new);
if (old == NULL)
return (new);
for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
if (ptr->za_linkid == linkid)
break;
}
if (ptr == NULL) {
/* linkid does not already exist, add to the beginning */
new->za_next = old;
return (new);
}
/*
* adding to the middle of the list; ptr points at the first
* occurrence of linkid. Find the last occurrence.
*/
while ((next = ptr->za_next) != NULL) {
if (next->za_linkid != linkid)
break;
ptr = next;
}
/* insert new after ptr */
new->za_next = next;
ptr->za_next = new;
return (old);
}
void
free_ip_interface(zone_addr_list_t *zalist)
{
zone_addr_list_t *ptr, *new;
for (ptr = zalist; ptr != NULL; ) {
new = ptr;
ptr = ptr->za_next;
free(new);
}
}
/*
* Add the kernel access control information for the interface names.
* If anything goes wrong, we log a general error message, attempt to tear down
* whatever we set up, and return an error.
*/
static int
configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
{
struct zone_nwiftab nwiftab;
char rootpath[MAXPATHLEN];
char path[MAXPATHLEN];
datalink_id_t linkid;
di_prof_t prof = NULL;
boolean_t added = B_FALSE;
zone_addr_list_t *zalist = NULL, *new;
if (zonecfg_setnwifent(snap_hndl) != Z_OK)
return (0);
for (;;) {
if (zonecfg_getnwifent(snap_hndl, &nwiftab) != Z_OK)
break;
nwifent_free_attrs(&nwiftab);
if (prof == NULL) {
if (zone_get_devroot(zone_name, rootpath,
sizeof (rootpath)) != Z_OK) {
(void) zonecfg_endnwifent(snap_hndl);
zerror(zlogp, B_TRUE,
"unable to determine dev root");
return (-1);
}
(void) snprintf(path, sizeof (path), "%s%s", rootpath,
"/dev");
if (di_prof_init(path, &prof) != 0) {
(void) zonecfg_endnwifent(snap_hndl);
zerror(zlogp, B_TRUE,
"failed to initialize profile");
return (-1);
}
}
/*
* Create the /dev entry for backward compatibility.
* Only create the /dev entry if it's not in use.
* Note that the zone still boots when the assigned
* interface is inaccessible, used by others, etc.
* Also, when vanity naming is used, some interface do
* do not have corresponding /dev node names (for example,
* vanity named aggregations). The /dev entry is not
* created in that case. The /dev/net entry is always
* accessible.
*/
if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
&linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
add_datalink(zlogp, zone_name, linkid,
nwiftab.zone_nwif_physical) == 0) {
added = B_TRUE;
} else {
/*
* Failed to add network device, but the brand hook
* might be doing this for us, so keep silent.
*/
continue;
}
/* set up the new IP interface, and add them all later */
new = malloc(sizeof (*new));
if (new == NULL) {
zerror(zlogp, B_TRUE, "no memory for %s",
nwiftab.zone_nwif_physical);
free_ip_interface(zalist);
}
bzero(new, sizeof (*new));
new->za_nwiftab = nwiftab;
new->za_linkid = linkid;
zalist = add_ip_interface(zalist, new);
}
if (zalist != NULL) {
if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
(void) zonecfg_endnwifent(snap_hndl);
zerror(zlogp, B_TRUE, "failed to add address");
free_ip_interface(zalist);
return (-1);
}
free_ip_interface(zalist);
}
(void) zonecfg_endnwifent(snap_hndl);
if (prof != NULL && added) {
if (di_prof_commit(prof) != 0) {
zerror(zlogp, B_TRUE, "failed to commit profile");
return (-1);
}
}
if (prof != NULL)
di_prof_fini(prof);
return (0);
}
static int
remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
{
ushort_t flags;
zone_iptype_t iptype;
int i, dlnum = 0;
datalink_id_t *dllink, *dllinks = NULL;
dladm_status_t err;
if (strlen(pool_name) == 0)
return (0);
if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
sizeof (flags)) < 0) {
if (vplat_get_iptype(zlogp, &iptype) < 0) {
zerror(zlogp, B_FALSE, "unable to determine ip-type");
return (-1);
}
} else {
if (flags & ZF_NET_EXCL)
iptype = ZS_EXCLUSIVE;
else
iptype = ZS_SHARED;
}
if (iptype == ZS_EXCLUSIVE) {
/*
* Get the datalink count and for each datalink,
* attempt to clear the pool property and clear
* the pool_name.
*/
if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
zerror(zlogp, B_TRUE, "unable to count network "
"interfaces");
return (-1);
}
if (dlnum == 0)
return (0);
if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
== NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
return (-1);
}
if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
zerror(zlogp, B_TRUE, "unable to list network "
"interfaces");
return (-1);
}
bzero(pool_name, sizeof (pool_name));
for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
err = dladm_set_linkprop(dld_handle, *dllink, "pool",
NULL, 0, DLADM_OPT_ACTIVE);
if (err != DLADM_STATUS_OK) {
zerror(zlogp, B_TRUE,
"WARNING: unable to clear pool");
}
}
free(dllinks);
}
return (0);
}
static int
remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
{
ushort_t flags;
zone_iptype_t iptype;
int i, dlnum = 0;
dladm_status_t dlstatus;
datalink_id_t *dllink, *dllinks = NULL;
if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
sizeof (flags)) < 0) {
if (vplat_get_iptype(zlogp, &iptype) < 0) {
zerror(zlogp, B_FALSE, "unable to determine ip-type");
return (-1);
}
} else {
if (flags & ZF_NET_EXCL)
iptype = ZS_EXCLUSIVE;
else
iptype = ZS_SHARED;
}
if (iptype != ZS_EXCLUSIVE)
return (0);
/*
* Get the datalink count and for each datalink,
* attempt to clear the pool property and clear
* the pool_name.
*/
if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
zerror(zlogp, B_TRUE, "unable to count network interfaces");
return (-1);
}
if (dlnum == 0)
return (0);
if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
return (-1);
}
if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
zerror(zlogp, B_TRUE, "unable to list network interfaces");
free(dllinks);
return (-1);
}
for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
char dlerr[DLADM_STRSIZE];
dlstatus = dladm_set_linkprop(dld_handle, *dllink,
"protection", NULL, 0, DLADM_OPT_ACTIVE);
if (dlstatus == DLADM_STATUS_NOTFOUND) {
/* datalink does not belong to the GZ */
continue;
}
if (dlstatus != DLADM_STATUS_OK)
zerror(zlogp, B_FALSE,
"clear 'protection' link property: %s",
dladm_status2str(dlstatus, dlerr));
dlstatus = dladm_set_linkprop(dld_handle, *dllink,
"allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
if (dlstatus != DLADM_STATUS_OK)
zerror(zlogp, B_FALSE,
"clear 'allowed-ips' link property: %s",
dladm_status2str(dlstatus, dlerr));
}
free(dllinks);
return (0);
}
static int
tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
{
int fd;
struct strioctl ioc;
tcp_ioc_abort_conn_t conn;
int error;
conn.ac_local = *local;
conn.ac_remote = *remote;
conn.ac_start = TCPS_SYN_SENT;
conn.ac_end = TCPS_TIME_WAIT;
conn.ac_zoneid = zoneid;
ioc.ic_cmd = TCP_IOC_ABORT_CONN;
ioc.ic_timout = -1; /* infinite timeout */
ioc.ic_len = sizeof (conn);
ioc.ic_dp = (char *)&conn;
if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
return (-1);
}
error = ioctl(fd, I_STR, &ioc);
(void) close(fd);
if (error == 0 || errno == ENOENT) /* ENOENT is not an error */
return (0);
return (-1);
}
static int
tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
{
struct sockaddr_storage l, r;
struct sockaddr_in *local, *remote;
struct sockaddr_in6 *local6, *remote6;
int error;
/*
* Abort IPv4 connections.
*/
bzero(&l, sizeof (*local));
local = (struct sockaddr_in *)&l;
local->sin_family = AF_INET;
local->sin_addr.s_addr = INADDR_ANY;
local->sin_port = 0;
bzero(&r, sizeof (*remote));
remote = (struct sockaddr_in *)&r;
remote->sin_family = AF_INET;
remote->sin_addr.s_addr = INADDR_ANY;
remote->sin_port = 0;
if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
return (error);
/*
* Abort IPv6 connections.
*/
bzero(&l, sizeof (*local6));
local6 = (struct sockaddr_in6 *)&l;
local6->sin6_family = AF_INET6;
local6->sin6_port = 0;
local6->sin6_addr = in6addr_any;
bzero(&r, sizeof (*remote6));
remote6 = (struct sockaddr_in6 *)&r;
remote6->sin6_family = AF_INET6;
remote6->sin6_port = 0;
remote6->sin6_addr = in6addr_any;
if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
return (error);
return (0);
}
static int
get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
{
int error = -1;
char *privname = NULL;
if (ALT_MOUNT(mount_cmd)) {
zone_iptype_t iptype;
const char *curr_iptype = NULL;
if (zonecfg_get_iptype(snap_hndl, &iptype) != Z_OK) {
zerror(zlogp, B_TRUE, "unable to determine ip-type");
return (-1);
}
switch (iptype) {
case ZS_SHARED:
curr_iptype = "shared";
break;
case ZS_EXCLUSIVE:
curr_iptype = "exclusive";
break;
}
if (zonecfg_default_privset(privs, curr_iptype) == Z_OK)
return (0);
zerror(zlogp, B_FALSE,
"failed to determine the zone's default privilege set");
return (-1);
}
switch (zonecfg_get_privset(snap_hndl, privs, &privname)) {
case Z_OK:
error = 0;
break;
case Z_PRIV_PROHIBITED:
zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
"within the zone's privilege set", privname);
break;
case Z_PRIV_REQUIRED:
zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
"from the zone's privilege set", privname);
break;
case Z_PRIV_UNKNOWN:
zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
"in the zone's privilege set", privname);
break;
default:
zerror(zlogp, B_FALSE, "failed to determine the zone's "
"privilege set");
break;
}
free(privname);
return (error);
}
static char *
zone_proj_rctl(const char *name)
{
int i;
for (i = 0; zone_proj_rctl_map[i].zpr_zone_rctl != NULL; i++) {
if (strcmp(name, zone_proj_rctl_map[i].zpr_zone_rctl) == 0) {
return (zone_proj_rctl_map[i].zpr_project_rctl);
}
}
return (NULL);
}
static int
get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
{
nvlist_t *nvl = NULL;
char *nvl_packed = NULL;
size_t nvl_size = 0;
nvlist_t **nvlv = NULL;
int rctlcount = 0;
int error = -1;
struct zone_rctltab rctltab;
rctlblk_t *rctlblk = NULL;
uint64_t maxlwps;
uint64_t maxprocs;
int rproc, rlwp;
*bufp = NULL;
*bufsizep = 0;
rctltab.zone_rctl_valptr = NULL;
if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
goto out;
}
/*
* Allow the administrator to control both the maximum number of
* process table slots, and the maximum number of lwps, with a single
* max-processes or max-lwps property. If only the max-processes
* property is set, we add a max-lwps property with a limit derived
* from max-processes. If only the max-lwps property is set, we add a
* max-processes property with the same limit as max-lwps.
*/
rproc = zonecfg_get_aliased_rctl(snap_hndl, ALIAS_MAXPROCS, &maxprocs);
rlwp = zonecfg_get_aliased_rctl(snap_hndl, ALIAS_MAXLWPS, &maxlwps);
if (rproc == Z_OK && rlwp == Z_NO_ENTRY) {
if (zonecfg_set_aliased_rctl(snap_hndl, ALIAS_MAXLWPS,
maxprocs * LWPS_PER_PROCESS) != Z_OK) {
zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
goto out;
}
} else if (rlwp == Z_OK && rproc == Z_NO_ENTRY) {
/* no scaling for max-proc value */
if (zonecfg_set_aliased_rctl(snap_hndl, ALIAS_MAXPROCS,
maxlwps) != Z_OK) {
zerror(zlogp, B_FALSE,
"unable to set max-processes alias");
goto out;
}
}
if (zonecfg_setrctlent(snap_hndl) != Z_OK) {
zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
goto out;
}
if ((rctlblk = malloc(rctlblk_size())) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
goto out;
}
while (zonecfg_getrctlent(snap_hndl, &rctltab) == Z_OK) {
struct zone_rctlvaltab *rctlval;
uint_t i, count;
const char *name = rctltab.zone_rctl_name;
char *proj_nm;
/* zoneadm should have already warned about unknown rctls. */
if (!zonecfg_is_rctl(name)) {
zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
rctltab.zone_rctl_valptr = NULL;
continue;
}
count = 0;
for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
rctlval = rctlval->zone_rctlval_next) {
count++;
}
if (count == 0) { /* ignore */
continue; /* Nothing to free */
}
if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
goto out;
i = 0;
for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
rctlval = rctlval->zone_rctlval_next, i++) {
if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
zerror(zlogp, B_TRUE, "%s failed",
"nvlist_alloc");
goto out;
}
if (zonecfg_construct_rctlblk(rctlval, rctlblk)
!= Z_OK) {
zerror(zlogp, B_FALSE, "invalid rctl value: "
"(priv=%s,limit=%s,action=%s)",
rctlval->zone_rctlval_priv,
rctlval->zone_rctlval_limit,
rctlval->zone_rctlval_action);
goto out;
}
if (!zonecfg_valid_rctl(name, rctlblk)) {
zerror(zlogp, B_FALSE,
"(priv=%s,limit=%s,action=%s) is not a "
"valid value for rctl '%s'",
rctlval->zone_rctlval_priv,
rctlval->zone_rctlval_limit,
rctlval->zone_rctlval_action,
name);
goto out;
}
if (nvlist_add_uint64(nvlv[i], "privilege",
rctlblk_get_privilege(rctlblk)) != 0) {
zerror(zlogp, B_FALSE, "%s failed",
"nvlist_add_uint64");
goto out;
}
if (nvlist_add_uint64(nvlv[i], "limit",
rctlblk_get_value(rctlblk)) != 0) {
zerror(zlogp, B_FALSE, "%s failed",
"nvlist_add_uint64");
goto out;
}
if (nvlist_add_uint64(nvlv[i], "action",
(uint_t)rctlblk_get_local_action(rctlblk, NULL))
!= 0) {
zerror(zlogp, B_FALSE, "%s failed",
"nvlist_add_uint64");
goto out;
}
}
zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
rctltab.zone_rctl_valptr = NULL;
/*
* With no action on our part we will start zsched with the
* project rctl values for our (zoneadmd) current project. For
* brands running a variant of Illumos, that's not a problem
* since they will setup their own projects, but for a
* non-native brand like lx, where there are no projects, we
* want to start things up with the same project rctls as the
* corresponding zone rctls, since nothing within the zone will
* ever change the project rctls.
*/
if ((proj_nm = zone_proj_rctl(name)) != NULL) {
if (nvlist_add_nvlist_array(nvl, proj_nm, nvlv, count)
!= 0) {
zerror(zlogp, B_FALSE,
"nvlist_add_nvlist_arrays failed");
goto out;
}
}
if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
!= 0) {
zerror(zlogp, B_FALSE, "%s failed",
"nvlist_add_nvlist_array");
goto out;
}
for (i = 0; i < count; i++)
nvlist_free(nvlv[i]);
free(nvlv);
nvlv = NULL;
rctlcount++;
}
(void) zonecfg_endrctlent(snap_hndl);
if (rctlcount == 0) {
error = 0;
goto out;
}
if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
!= 0) {
zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
goto out;
}
error = 0;
*bufp = nvl_packed;
*bufsizep = nvl_size;
out:
free(rctlblk);
zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
if (error && nvl_packed != NULL)
free(nvl_packed);
nvlist_free(nvl);
if (nvlv != NULL)
free(nvlv);
return (error);
}
static int
get_implicit_datasets(zlog_t *zlogp, char **retstr)
{
char cmdbuf[2 * MAXPATHLEN];
if (query_hook[0] == '\0')
return (0);
if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
> sizeof (cmdbuf))
return (-1);
if (do_subproc(zlogp, cmdbuf, retstr, B_FALSE) != 0)
return (-1);
return (0);
}
static int
get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
{
struct zone_dstab dstab;
size_t total, offset, len;
int error = -1;
char *str = NULL;
char *implicit_datasets = NULL;
int implicit_len = 0;
*bufp = NULL;
*bufsizep = 0;
if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
zerror(zlogp, B_FALSE, "getting implicit datasets failed");
goto out;
}
if (zonecfg_setdsent(snap_hndl) != Z_OK) {
zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
goto out;
}
total = 0;
while (zonecfg_getdsent(snap_hndl, &dstab) == Z_OK)
total += strlen(dstab.zone_dataset_name) + 1;
(void) zonecfg_enddsent(snap_hndl);
if (implicit_datasets != NULL)
implicit_len = strlen(implicit_datasets);
if (implicit_len > 0)
total += implicit_len + 1;
if (total == 0) {
error = 0;
goto out;
}
if ((str = malloc(total)) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
goto out;
}
if (zonecfg_setdsent(snap_hndl) != Z_OK) {
zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
goto out;
}
offset = 0;
while (zonecfg_getdsent(snap_hndl, &dstab) == Z_OK) {
len = strlen(dstab.zone_dataset_name);
(void) strlcpy(str + offset, dstab.zone_dataset_name,
total - offset);
offset += len;
if (offset < total - 1)
str[offset++] = ',';
}
(void) zonecfg_enddsent(snap_hndl);
if (implicit_len > 0)
(void) strlcpy(str + offset, implicit_datasets, total - offset);
error = 0;
*bufp = str;
*bufsizep = total;
out:
if (error != 0 && str != NULL)
free(str);
if (implicit_datasets != NULL)
free(implicit_datasets);
return (error);
}
static int
validate_datasets(zlog_t *zlogp)
{
struct zone_dstab dstab;
zfs_handle_t *zhp;
libzfs_handle_t *hdl;
if (zonecfg_setdsent(snap_hndl) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
return (-1);
}
if ((hdl = libzfs_init()) == NULL) {
zerror(zlogp, B_FALSE, "opening ZFS library");
return (-1);
}
while (zonecfg_getdsent(snap_hndl, &dstab) == Z_OK) {
if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
ZFS_TYPE_FILESYSTEM)) == NULL) {
zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
dstab.zone_dataset_name);
libzfs_fini(hdl);
return (-1);
}
/*
* Automatically set the 'zoned' property. We check the value
* first because we'll get EPERM if it is already set.
*/
if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
"on") != 0) {
zerror(zlogp, B_FALSE, "cannot set 'zoned' "
"property for ZFS dataset '%s'\n",
dstab.zone_dataset_name);
zfs_close(zhp);
libzfs_fini(hdl);
return (-1);
}
zfs_close(zhp);
}
(void) zonecfg_enddsent(snap_hndl);
libzfs_fini(hdl);
return (0);
}
/*
* Return true if the path is its own zfs file system. We determine this
* by stat-ing the path to see if it is zfs and stat-ing the parent to see
* if it is a different fs.
*/
boolean_t
is_zonepath_zfs(char *zonepath)
{
int res;
char *path;
char *parent;
struct statvfs64 buf1, buf2;
if (statvfs64(zonepath, &buf1) != 0)
return (B_FALSE);
if (strcmp(buf1.f_basetype, "zfs") != 0)
return (B_FALSE);
if ((path = strdup(zonepath)) == NULL)
return (B_FALSE);
parent = dirname(path);
res = statvfs64(parent, &buf2);
free(path);
if (res != 0)
return (B_FALSE);
if (buf1.f_fsid == buf2.f_fsid)
return (B_FALSE);
return (B_TRUE);
}
/*
* Verify the MAC label in the root dataset for the zone.
* If the label exists, it must match the label configured for the zone.
* Otherwise if there's no label on the dataset, create one here.
*/
static int
validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
{
int error = -1;
zfs_handle_t *zhp;
libzfs_handle_t *hdl;
m_label_t ds_sl;
char ds_hexsl[MAXNAMELEN];
if (!is_system_labeled())
return (0);
if (!is_zonepath_zfs(zonepath))
return (0);
if ((hdl = libzfs_init()) == NULL) {
zerror(zlogp, B_FALSE, "opening ZFS library");
return (-1);
}
if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
ZFS_TYPE_FILESYSTEM)) == NULL) {
zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
rootpath);
libzfs_fini(hdl);
return (-1);
}
/* Get the mlslabel property if it exists. */
if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
NULL, NULL, 0, B_TRUE) != 0) ||
(strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
char *str2 = NULL;
/*
* No label on the dataset (or default only); create one.
* (Only do this automatic labeling for the labeled brand.)
*/
if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
error = 0;
goto out;
}
error = l_to_str_internal(zone_sl, &str2);
if (error)
goto out;
if (str2 == NULL) {
error = -1;
goto out;
}
if ((error = zfs_prop_set(zhp,
zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
"property for root dataset at '%s'\n", rootpath);
}
free(str2);
goto out;
}
/* Convert the retrieved dataset label to binary form. */
error = hexstr_to_label(ds_hexsl, &ds_sl);
if (error) {
zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
"property on root dataset at '%s'\n", rootpath);
goto out; /* exit with error */
}
/*
* Perform a MAC check by comparing the zone label with the
* dataset label.
*/
error = (!blequal(zone_sl, &ds_sl));
if (error)
zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
out:
zfs_close(zhp);
libzfs_fini(hdl);
return (error);
}
/*
* Mount lower level home directories into/from current zone
* Share exported directories specified in dfstab for zone
*/
static int
tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
{
zoneid_t *zids = NULL;
priv_set_t *zid_privs;
const priv_impl_info_t *ip = NULL;
uint_t nzents_saved;
uint_t nzents;
int i;
char readonly[] = "ro";
struct zone_fstab lower_fstab;
char *argv[4];
if (!is_system_labeled())
return (0);
if (zid_label == NULL) {
zid_label = m_label_alloc(MAC_LABEL);
if (zid_label == NULL)
return (-1);
}
/* Make sure our zone has an /export/home dir */
(void) make_one_dir(zlogp, rootpath, "/export/home",
DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
lower_fstab.zone_fs_raw[0] = '\0';
(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
sizeof (lower_fstab.zone_fs_type));
lower_fstab.zone_fs_options = NULL;
(void) zonecfg_add_fs_option(&lower_fstab, readonly);
/*
* Get the list of zones from the kernel
*/
if (zone_list(NULL, &nzents) != 0) {
zerror(zlogp, B_TRUE, "unable to list zones");
zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
return (-1);
}
again:
if (nzents == 0) {
zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
return (-1);
}
zids = malloc(nzents * sizeof (zoneid_t));
if (zids == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
return (-1);
}
nzents_saved = nzents;
if (zone_list(zids, &nzents) != 0) {
zerror(zlogp, B_TRUE, "unable to list zones");
zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
free(zids);
return (-1);
}
if (nzents != nzents_saved) {
/* list changed, try again */
free(zids);
goto again;
}
ip = getprivimplinfo();
if ((zid_privs = priv_allocset()) == NULL) {
zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
zonecfg_free_fs_option_list(
lower_fstab.zone_fs_options);
free(zids);
return (-1);
}
for (i = 0; i < nzents; i++) {
char zid_name[ZONENAME_MAX];
zone_state_t zid_state;
char zid_rpath[MAXPATHLEN];
struct stat stat_buf;
if (zids[i] == GLOBAL_ZONEID)
continue;
if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
continue;
/*
* Do special setup for the zone we are booting
*/
if (strcmp(zid_name, zone_name) == 0) {
struct zone_fstab autofs_fstab;
char map_path[MAXPATHLEN];
int fd;
/*
* Create auto_home_<zone> map for this zone
* in the global zone. The non-global zone entry
* will be created by automount when the zone
* is booted.
*/
(void) snprintf(autofs_fstab.zone_fs_special,
MAXPATHLEN, "auto_home_%s", zid_name);
(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
"/zone/%s/home", zid_name);
(void) snprintf(map_path, sizeof (map_path),
"/etc/%s", autofs_fstab.zone_fs_special);
/*
* If the map file doesn't exist create a template
*/
if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
int len;
char map_rec[MAXPATHLEN];
len = snprintf(map_rec, sizeof (map_rec),
"+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
autofs_fstab.zone_fs_special, rootpath);
(void) write(fd, map_rec, len);
(void) close(fd);
}
/*
* Mount auto_home_<zone> in the global zone if absent.
* If it's already of type autofs, then
* don't mount it again.
*/
if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
char optstr[] = "indirect,ignore,nobrowse";
(void) make_one_dir(zlogp, "",
autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
/*
* Mount will fail if automounter has already
* processed the auto_home_<zonename> map
*/
(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
autofs_fstab.zone_fs_special,
autofs_fstab.zone_fs_dir);
}
continue;
}
if (zone_get_state(zid_name, &zid_state) != Z_OK ||
(zid_state != ZONE_STATE_READY &&
zid_state != ZONE_STATE_RUNNING))
/* Skip over zones without mounted filesystems */
continue;
if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
sizeof (m_label_t)) < 0)
/* Skip over zones with unspecified label */
continue;
if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
sizeof (zid_rpath)) == -1)
/* Skip over zones with bad path */
continue;
if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
/* Skip over zones with bad privs */
continue;
/*
* Reading down is valid according to our label model
* but some customers want to disable it because it
* allows execute down and other possible attacks.
* Therefore, we restrict this feature to zones that
* have the NET_MAC_AWARE privilege which is required
* for NFS read-down semantics.
*/
if ((bldominates(zlabel, zid_label)) &&
(priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
/*
* Our zone dominates this one.
* Create a lofs mount from lower zone's /export/home
*/
(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
"%s/zone/%s/export/home", rootpath, zid_name);
/*
* If the target is already an LOFS mount
* then don't do it again.
*/
if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
if (snprintf(lower_fstab.zone_fs_special,
MAXPATHLEN, "%s/export",
zid_rpath) > MAXPATHLEN)
continue;
/*
* Make sure the lower-level home exists
*/
if (make_one_dir(zlogp,
lower_fstab.zone_fs_special, "/home",
DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
DEFAULT_DIR_GROUP) != 0)
continue;
(void) strlcat(lower_fstab.zone_fs_special,
"/home", MAXPATHLEN);
/*
* Mount can fail because the lower-level
* zone may have already done a mount up.
*/
(void) mount_one(zlogp, &lower_fstab, "",
Z_MNT_BOOT);
}
} else if ((bldominates(zid_label, zlabel)) &&
(priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
/*
* This zone dominates our zone.
* Create a lofs mount from our zone's /export/home
*/
if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
"%s/zone/%s/export/home", zid_rpath,
zone_name) > MAXPATHLEN)
continue;
/*
* If the target is already an LOFS mount
* then don't do it again.
*/
if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
(void) snprintf(lower_fstab.zone_fs_special,
MAXPATHLEN, "%s/export/home", rootpath);
/*
* Mount can fail because the higher-level
* zone may have already done a mount down.
*/
(void) mount_one(zlogp, &lower_fstab, "",
Z_MNT_BOOT);
}
}
}
zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
priv_freeset(zid_privs);
free(zids);
/*
* Now share any exported directories from this zone.
* Each zone can have its own dfstab.
*/
argv[0] = "zoneshare";
argv[1] = "-z";
argv[2] = zone_name;
argv[3] = NULL;
(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
/* Don't check for errors since they don't affect the zone */
return (0);
}
/*
* Unmount lofs mounts from higher level zones
* Unshare nfs exported directories
*/
static void
tsol_unmounts(zlog_t *zlogp, char *zone_name)
{
zoneid_t *zids = NULL;
uint_t nzents_saved;
uint_t nzents;
int i;
char *argv[4];
char path[MAXPATHLEN];
if (!is_system_labeled())
return;
/*
* Get the list of zones from the kernel
*/
if (zone_list(NULL, &nzents) != 0) {
return;
}
if (zid_label == NULL) {
zid_label = m_label_alloc(MAC_LABEL);
if (zid_label == NULL)
return;
}
again:
if (nzents == 0)
return;
zids = malloc(nzents * sizeof (zoneid_t));
if (zids == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
return;
}
nzents_saved = nzents;
if (zone_list(zids, &nzents) != 0) {
free(zids);
return;
}
if (nzents != nzents_saved) {
/* list changed, try again */
free(zids);
goto again;
}
for (i = 0; i < nzents; i++) {
char zid_name[ZONENAME_MAX];
zone_state_t zid_state;
char zid_rpath[MAXPATHLEN];
if (zids[i] == GLOBAL_ZONEID)
continue;
if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
continue;
/*
* Skip the zone we are halting
*/
if (strcmp(zid_name, zone_name) == 0)
continue;
if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
sizeof (zid_state)) < 0) ||
(zid_state < ZONE_IS_READY))
/* Skip over zones without mounted filesystems */
continue;
if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
sizeof (m_label_t)) < 0)
/* Skip over zones with unspecified label */
continue;
if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
sizeof (zid_rpath)) == -1)
/* Skip over zones with bad path */
continue;
if (zlabel != NULL && bldominates(zid_label, zlabel)) {
/*
* This zone dominates our zone.
* Unmount the lofs mount of our zone's /export/home
*/
if (snprintf(path, MAXPATHLEN,
"%s/zone/%s/export/home", zid_rpath,
zone_name) > MAXPATHLEN)
continue;
/* Skip over mount failures */
(void) umount(path);
}
}
free(zids);
/*
* Unmount global zone autofs trigger for this zone
*/
(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
/* Skip over mount failures */
(void) umount(path);
/*
* Next unshare any exported directories from this zone.
*/
argv[0] = "zoneunshare";
argv[1] = "-z";
argv[2] = zone_name;
argv[3] = NULL;
(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
/* Don't check for errors since they don't affect the zone */
/*
* Finally, deallocate any devices in the zone.
*/
argv[0] = "deallocate";
argv[1] = "-Isz";
argv[2] = zone_name;
argv[3] = NULL;
(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
/* Don't check for errors since they don't affect the zone */
}
/*
* Fetch the Trusted Extensions label and multi-level ports (MLPs) for
* this zone.
*/
static tsol_zcent_t *
get_zone_label(zlog_t *zlogp, priv_set_t *privs)
{
FILE *fp;
tsol_zcent_t *zcent = NULL;
char line[MAXTNZLEN];
if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
return (NULL);
}
while (fgets(line, sizeof (line), fp) != NULL) {
/*
* Check for malformed database
*/
if (strlen(line) == MAXTNZLEN - 1)
break;
if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
continue;
if (strcmp(zcent->zc_name, zone_name) == 0)
break;
tsol_freezcent(zcent);
zcent = NULL;
}
(void) fclose(fp);
if (zcent == NULL) {
zerror(zlogp, B_FALSE, "zone requires a label assignment. "
"See tnzonecfg(4)");
} else {
if (zlabel == NULL)
zlabel = m_label_alloc(MAC_LABEL);
/*
* Save this zone's privileges for later read-down processing
*/
if ((zprivs = priv_allocset()) == NULL) {
zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
return (NULL);
} else {
priv_copyset(privs, zprivs);
}
}
return (zcent);
}
/*
* Add the Trusted Extensions multi-level ports for this zone.
*/
static void
set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
{
tsol_mlp_t *mlp;
tsol_mlpent_t tsme;
if (!is_system_labeled())
return;
tsme.tsme_zoneid = zoneid;
tsme.tsme_flags = 0;
for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
tsme.tsme_mlp = *mlp;
if (tnmlp(TNDB_LOAD, &tsme) != 0) {
zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
"on %d-%d/%d", mlp->mlp_port,
mlp->mlp_port_upper, mlp->mlp_ipp);
}
}
tsme.tsme_flags = TSOL_MEF_SHARED;
for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
tsme.tsme_mlp = *mlp;
if (tnmlp(TNDB_LOAD, &tsme) != 0) {
zerror(zlogp, B_TRUE, "cannot set shared MLP "
"on %d-%d/%d", mlp->mlp_port,
mlp->mlp_port_upper, mlp->mlp_ipp);
}
}
}
static void
remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
{
tsol_mlpent_t tsme;
if (!is_system_labeled())
return;
(void) memset(&tsme, 0, sizeof (tsme));
tsme.tsme_zoneid = zoneid;
if (tnmlp(TNDB_FLUSH, &tsme) != 0)
zerror(zlogp, B_TRUE, "cannot flush MLPs");
}
int
prtmount(const struct mnttab *fs, void *x)
{
zerror((zlog_t *)x, B_FALSE, " %s", fs->mnt_mountp);
return (0);
}
/*
* Look for zones running on the main system that are using this root (or any
* subdirectory of it). Return B_TRUE and print an error if a conflicting zone
* is found or if we can't tell.
*/
static boolean_t
duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
{
zoneid_t *zids = NULL;
uint_t nzids = 0;
boolean_t retv;
int rlen, zlen;
char zroot[MAXPATHLEN];
char zonename[ZONENAME_MAX];
for (;;) {
nzids += 10;
zids = malloc(nzids * sizeof (*zids));
if (zids == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
return (B_TRUE);
}
if (zone_list(zids, &nzids) == 0)
break;
free(zids);
}
retv = B_FALSE;
rlen = strlen(rootpath);
while (nzids > 0) {
/*
* Ignore errors; they just mean that the zone has disappeared
* while we were busy.
*/
if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
sizeof (zroot)) == -1)
continue;
zlen = strlen(zroot);
if (zlen > rlen)
zlen = rlen;
if (strncmp(rootpath, zroot, zlen) == 0 &&
(zroot[zlen] == '\0' || zroot[zlen] == '/') &&
(rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
if (getzonenamebyid(zids[nzids], zonename,
sizeof (zonename)) == -1)
(void) snprintf(zonename, sizeof (zonename),
"id %d", (int)zids[nzids]);
zerror(zlogp, B_FALSE,
"zone root %s already in use by zone %s",
rootpath, zonename);
retv = B_TRUE;
break;
}
}
free(zids);
return (retv);
}
/*
* Search for loopback mounts that use this same source node (same device and
* inode). Return B_TRUE if there is one or if we can't tell.
*/
static boolean_t
duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
{
struct stat64 rst, zst;
struct mnttab *mnp;
if (stat64(rootpath, &rst) == -1) {
zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
return (B_TRUE);
}
if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
return (B_TRUE);
for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
if (mnp->mnt_fstype == NULL ||
strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
continue;
/* We're looking at a loopback mount. Stat it. */
if (mnp->mnt_special != NULL &&
stat64(mnp->mnt_special, &zst) != -1 &&
rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
zerror(zlogp, B_FALSE,
"zone root %s is reachable through %s",
rootpath, mnp->mnt_mountp);
return (B_TRUE);
}
}
return (B_FALSE);
}
/*
* Set pool info for the zone's resource management configuration.
*/
static int
setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
{
int res;
uint64_t tmp;
char sched[MAXNAMELEN];
char pool_err[128];
/* Get the scheduling class set in the zone configuration. */
if (zonecfg_get_sched_class(snap_hndl, sched, sizeof (sched)) == Z_OK &&
strlen(sched) > 0) {
if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
strlen(sched)) == -1)
zerror(zlogp, B_TRUE, "WARNING: unable to set the "
"default scheduling class");
if (strcmp(sched, "FX") == 0) {
/*
* When FX is specified then by default all processes
* will start at the lowest priority level (0) and
* stay there. We support an optional attr which
* indicates that all the processes should be "high
* priority". We set this on the zone so that starting
* init will set the priority high.
*/
struct zone_attrtab a;
bzero(&a, sizeof (a));
(void) strlcpy(a.zone_attr_name, "fixed-hi-prio",
sizeof (a.zone_attr_name));
if (zonecfg_lookup_attr(snap_hndl, &a) == Z_OK &&
strcmp(a.zone_attr_value, "true") == 0) {
boolean_t hi = B_TRUE;
if (zone_setattr(zoneid,
ZONE_ATTR_SCHED_FIXEDHI, (void *)hi,
sizeof (hi)) == -1)
zerror(zlogp, B_TRUE, "WARNING: unable "
"to set high priority");
}
}
} else if (zonecfg_get_aliased_rctl(snap_hndl, ALIAS_SHARES, &tmp)
== Z_OK) {
/*
* If the zone has the zone.cpu-shares rctl set then we want to
* use the Fair Share Scheduler (FSS) for processes in the
* zone. Check what scheduling class the zone would be running
* in by default so we can print a warning and modify the class
* if we wouldn't be using FSS.
*/
char class_name[PC_CLNMSZ];
if (zonecfg_get_dflt_sched_class(snap_hndl, class_name,
sizeof (class_name)) != Z_OK) {
zerror(zlogp, B_FALSE, "WARNING: unable to determine "
"the zone's scheduling class");
} else if (strcmp("FSS", class_name) != 0) {
zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
"rctl is set but\nFSS is not the default "
"scheduling class for\nthis zone. FSS will be "
"used for processes\nin the zone but to get the "
"full benefit of FSS,\nit should be the default "
"scheduling class.\nSee dispadmin(1M) for more "
"details.");
if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
strlen("FSS")) == -1)
zerror(zlogp, B_TRUE, "WARNING: unable to set "
"zone scheduling class to FSS");
}
}
/*
* The next few blocks of code attempt to set up temporary pools as
* well as persistent pools. In all cases we call the functions
* unconditionally. Within each funtion the code will check if the
* zone is actually configured for a temporary pool or persistent pool
* and just return if there is nothing to do.
*
* If we are rebooting we want to attempt to reuse any temporary pool
* that was previously set up. zonecfg_bind_tmp_pool() will do the
* right thing in all cases (reuse or create) based on the current
* zonecfg.
*/
if ((res = zonecfg_bind_tmp_pool(snap_hndl, zoneid, pool_err,
sizeof (pool_err))) != Z_OK) {
if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
"cannot be instantiated", zonecfg_strerror(res),
pool_err);
else
zerror(zlogp, B_FALSE, "could not bind zone to "
"temporary pool: %s", zonecfg_strerror(res));
return (Z_POOL_BIND);
}
/*
* Check if we need to warn about poold not being enabled.
*/
if (zonecfg_warn_poold(snap_hndl)) {
zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
"been specified\nbut the dynamic pool service is not "
"enabled.\nThe system will not dynamically adjust the\n"
"processor allocation within the specified range\n"
"until svc:/system/pools/dynamic is enabled.\n"
"See poold(1M).");
}
/* The following is a warning, not an error. */
if ((res = zonecfg_bind_pool(snap_hndl, zoneid, pool_err,
sizeof (pool_err))) != Z_OK) {
if (res == Z_POOL_BIND)
zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
"pool '%s'; using default pool.", pool_err);
else if (res == Z_POOL)
zerror(zlogp, B_FALSE, "WARNING: %s: %s",
zonecfg_strerror(res), pool_err);
else
zerror(zlogp, B_FALSE, "WARNING: %s",
zonecfg_strerror(res));
}
/* Update saved pool name in case it has changed */
(void) zonecfg_get_poolname(snap_hndl, zone_name, pool_name,
sizeof (pool_name));
return (Z_OK);
}
static void
report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
{
switch (res) {
case Z_TOO_BIG:
zerror(zlogp, B_FALSE, "%s property value is too large.", name);
break;
case Z_INVALID_PROPERTY:
zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
name, value);
break;
default:
zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
break;
}
}
/*
* Sets the hostid of the new zone based on its configured value. The zone's
* zone_t structure must already exist in kernel memory. 'zlogp' refers to the
* log used to report errors and warnings and must be non-NULL. 'zone_namep'
* is the name of the new zone and must be non-NULL. 'zoneid' is the numeric
* ID of the new zone.
*
* This function returns zero on success and a nonzero error code on failure.
*/
static int
setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
{
int res;
char hostidp[HW_HOSTID_LEN];
unsigned int hostid;
res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
if (res == Z_BAD_PROPERTY) {
return (Z_OK);
} else if (res != Z_OK) {
report_prop_err(zlogp, "hostid", hostidp, res);
return (res);
}
hostid = (unsigned int)strtoul(hostidp, NULL, 16);
if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
sizeof (hostid))) != 0) {
zerror(zlogp, B_TRUE,
"zone hostid is not valid: %s: %d", hostidp, res);
return (Z_SYSTEM);
}
return (res);
}
static int
setup_zone_secflags(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
{
psecflags_t secflags;
struct zone_secflagstab tab = {0};
secflagdelta_t delt;
int res;
res = zonecfg_lookup_secflags(handle, &tab);
if ((res != Z_OK) &&
/* The general defaulting code will handle this */
(res != Z_NO_ENTRY) && (res != Z_BAD_PROPERTY)) {
zerror(zlogp, B_FALSE, "security-flags property is "
"invalid: %d", res);
return (res);
}
if (strlen(tab.zone_secflags_lower) == 0)
(void) strlcpy(tab.zone_secflags_lower, "none",
sizeof (tab.zone_secflags_lower));
if (strlen(tab.zone_secflags_default) == 0)
(void) strlcpy(tab.zone_secflags_default,
tab.zone_secflags_lower,
sizeof (tab.zone_secflags_default));
if (strlen(tab.zone_secflags_upper) == 0)
(void) strlcpy(tab.zone_secflags_upper, "all",
sizeof (tab.zone_secflags_upper));
if (secflags_parse(NULL, tab.zone_secflags_default,
&delt) == -1) {
zerror(zlogp, B_FALSE, "default security-flags: '%s'"
"are invalid", tab.zone_secflags_default);
return (Z_BAD_PROPERTY);
} else if (delt.psd_ass_active != B_TRUE) {
zerror(zlogp, B_FALSE, "relative security-flags are not "
"allowed in zone configuration (default "
"security-flags: '%s')",
tab.zone_secflags_default);
return (Z_BAD_PROPERTY);
} else {
secflags_copy(&secflags.psf_inherit, &delt.psd_assign);
secflags_copy(&secflags.psf_effective, &delt.psd_assign);
}
if (secflags_parse(NULL, tab.zone_secflags_lower,
&delt) == -1) {
zerror(zlogp, B_FALSE, "lower security-flags: '%s'"
"are invalid", tab.zone_secflags_lower);
return (Z_BAD_PROPERTY);
} else if (delt.psd_ass_active != B_TRUE) {
zerror(zlogp, B_FALSE, "relative security-flags are not "
"allowed in zone configuration (lower "
"security-flags: '%s')",
tab.zone_secflags_lower);
return (Z_BAD_PROPERTY);
} else {
secflags_copy(&secflags.psf_lower, &delt.psd_assign);
}
if (secflags_parse(NULL, tab.zone_secflags_upper,
&delt) == -1) {
zerror(zlogp, B_FALSE, "upper security-flags: '%s'"
"are invalid", tab.zone_secflags_upper);
return (Z_BAD_PROPERTY);
} else if (delt.psd_ass_active != B_TRUE) {
zerror(zlogp, B_FALSE, "relative security-flags are not "
"allowed in zone configuration (upper "
"security-flags: '%s')",
tab.zone_secflags_upper);
return (Z_BAD_PROPERTY);
} else {
secflags_copy(&secflags.psf_upper, &delt.psd_assign);
}
if (!psecflags_validate(&secflags)) {
zerror(zlogp, B_TRUE, "security-flags violate invariants");
return (Z_BAD_PROPERTY);
}
if ((res = zone_setattr(zoneid, ZONE_ATTR_SECFLAGS, &secflags,
sizeof (secflags))) != 0) {
zerror(zlogp, B_TRUE,
"security-flags couldn't be set: %d", res);
return (Z_SYSTEM);
}
return (Z_OK);
}
static int
setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
{
char fsallowed[ZONE_FS_ALLOWED_MAX];
char *fsallowedp = fsallowed;
int len = sizeof (fsallowed);
int res;
res = zonecfg_get_fs_allowed(handle, fsallowed, len);
if (res == Z_BAD_PROPERTY) {
/* No value, set the defaults */
(void) strlcpy(fsallowed, DFLT_FS_ALLOWED, len);
} else if (res != Z_OK) {
report_prop_err(zlogp, "fs-allowed", fsallowed, res);
return (res);
} else if (fsallowed[0] == '-') {
/* dropping default filesystems - use remaining list */
if (fsallowed[1] != ',')
return (Z_OK);
fsallowedp += 2;
len -= 2;
} else {
/* Has a value, append the defaults */
if (strlcat(fsallowed, ",", len) >= len ||
strlcat(fsallowed, DFLT_FS_ALLOWED, len) >= len) {
report_prop_err(zlogp, "fs-allowed", fsallowed,
Z_TOO_BIG);
return (Z_TOO_BIG);
}
}
if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, fsallowedp, len) != 0) {
zerror(zlogp, B_TRUE,
"fs-allowed couldn't be set: %s: %d", fsallowedp, res);
return (Z_SYSTEM);
}
return (Z_OK);
}
static int
setup_zone_attrs(zlog_t *zlogp, zoneid_t zoneid)
{
int res = Z_OK;
if ((res = setup_zone_hostid(snap_hndl, zlogp, zoneid)) != Z_OK)
goto out;
if ((res = setup_zone_fs_allowed(snap_hndl, zlogp, zoneid)) != Z_OK)
goto out;
if ((res = setup_zone_secflags(snap_hndl, zlogp, zoneid)) != Z_OK)
goto out;
out:
return (res);
}
/*
* The zone_did is a persistent debug ID. Each zone should have a unique ID
* in the kernel. This is used for things like DTrace which want to monitor
* zones across reboots. They can't use the zoneid since that changes on
* each boot.
*/
zoneid_t
vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zone_did)
{
zoneid_t rval = -1;
priv_set_t *privs;
char rootpath[MAXPATHLEN];
char *rctlbuf = NULL;
size_t rctlbufsz = 0;
char *zfsbuf = NULL;
size_t zfsbufsz = 0;
zoneid_t zoneid = -1;
int xerr;
char *kzone;
FILE *fp = NULL;
tsol_zcent_t *zcent = NULL;
int match = 0;
int doi = 0;
int flags = -1;
zone_iptype_t iptype;
if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
zerror(zlogp, B_TRUE, "unable to determine zone root");
return (-1);
}
if (zonecfg_in_alt_root())
resolve_lofs(zlogp, rootpath, sizeof (rootpath));
if (vplat_get_iptype(zlogp, &iptype) < 0) {
zerror(zlogp, B_TRUE, "unable to determine ip-type");
return (-1);
}
switch (iptype) {
case ZS_SHARED:
flags = 0;
break;
case ZS_EXCLUSIVE:
flags = ZCF_NET_EXCL;
break;
}
if (flags == -1)
abort();
if ((privs = priv_allocset()) == NULL) {
zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
return (-1);
}
priv_emptyset(privs);
if (get_privset(zlogp, privs, mount_cmd) != 0)
goto error;
if (mount_cmd == Z_MNT_BOOT &&
get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
zerror(zlogp, B_FALSE, "Unable to get list of rctls");
goto error;
}
if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
goto error;
}
if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
zcent = get_zone_label(zlogp, privs);
if (zcent != NULL) {
match = zcent->zc_match;
doi = zcent->zc_doi;
*zlabel = zcent->zc_label;
} else {
goto error;
}
if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
goto error;
}
kzone = zone_name;
/*
* We must do this scan twice. First, we look for zones running on the
* main system that are using this root (or any subdirectory of it).
* Next, we reduce to the shortest path and search for loopback mounts
* that use this same source node (same device and inode).
*/
if (duplicate_zone_root(zlogp, rootpath))
goto error;
if (duplicate_reachable_path(zlogp, rootpath))
goto error;
if (ALT_MOUNT(mount_cmd)) {
root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
/*
* Forge up a special root for this zone. When a zone is
* mounted, we can't let the zone have its own root because the
* tools that will be used in this "scratch zone" need access
* to both the zone's resources and the running machine's
* executables.
*
* Note that the mkdir here also catches read-only filesystems.
*/
if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
goto error;
}
if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
goto error;
}
if (zonecfg_in_alt_root()) {
/*
* If we are mounting up a zone in an alternate root partition,
* then we have some additional work to do before starting the
* zone. First, resolve the root path down so that we're not
* fooled by duplicates. Then forge up an internal name for
* the zone.
*/
if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
zerror(zlogp, B_TRUE, "cannot open mapfile");
goto error;
}
if (zonecfg_lock_scratch(fp) != 0) {
zerror(zlogp, B_TRUE, "cannot lock mapfile");
goto error;
}
if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
NULL, 0) == 0) {
zerror(zlogp, B_FALSE, "scratch zone already running");
goto error;
}
/* This is the preferred name */
(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
zone_name);
srandom(getpid());
while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
0) == 0) {
/* This is just an arbitrary name; note "." usage */
(void) snprintf(kernzone, sizeof (kernzone),
"SUNWlu.%08lX%08lX", random(), random());
}
kzone = kernzone;
}
xerr = 0;
if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
flags, zone_did)) == -1) {
if (xerr == ZE_AREMOUNTS) {
if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
zerror(zlogp, B_FALSE,
"An unknown file-system is mounted on "
"a subdirectory of %s", rootpath);
} else {
zerror(zlogp, B_FALSE,
"These file-systems are mounted on "
"subdirectories of %s:", rootpath);
(void) zonecfg_find_mounts(rootpath,
prtmount, zlogp);
}
} else if (xerr == ZE_CHROOTED) {
zerror(zlogp, B_FALSE, "%s: "
"cannot create a zone from a chrooted "
"environment", "zone_create");
} else if (xerr == ZE_LABELINUSE) {
char zonename[ZONENAME_MAX];
(void) getzonenamebyid(getzoneidbylabel(zlabel),
zonename, ZONENAME_MAX);
zerror(zlogp, B_FALSE, "The zone label is already "
"used by the zone '%s'.", zonename);
} else {
zerror(zlogp, B_TRUE, "%s failed", "zone_create");
}
goto error;
}
if (zonecfg_in_alt_root() &&
zonecfg_add_scratch(fp, zone_name, kernzone,
zonecfg_get_root()) == -1) {
zerror(zlogp, B_TRUE, "cannot add mapfile entry");
goto error;
}
/*
* The following actions are not performed when merely mounting a zone
* for administrative use.
*/
if (mount_cmd == Z_MNT_BOOT) {
brand_handle_t bh;
struct brand_attr attr;
char modname[MAXPATHLEN];
if (setup_zone_attrs(zlogp, zoneid) != Z_OK)
goto error;
if ((bh = brand_open(brand_name)) == NULL) {
zerror(zlogp, B_FALSE,
"unable to determine brand name");
goto error;
}
if (!is_system_labeled() &&
(strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
brand_close(bh);
zerror(zlogp, B_FALSE,
"cannot boot labeled zone on unlabeled system");
goto error;
}
/*
* If this brand requires any kernel support, now is the time to
* get it loaded and initialized.
*/
if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
brand_close(bh);
zerror(zlogp, B_FALSE,
"unable to determine brand kernel module");
goto error;
}
brand_close(bh);
if (strlen(modname) > 0) {
(void) strlcpy(attr.ba_brandname, brand_name,
sizeof (attr.ba_brandname));
(void) strlcpy(attr.ba_modname, modname,
sizeof (attr.ba_modname));
if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
sizeof (attr) != 0)) {
zerror(zlogp, B_TRUE,
"could not set zone brand attribute.");
goto error;
}
}
if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
goto error;
set_mlps(zlogp, zoneid, zcent);
}
rval = zoneid;
zoneid = -1;
error:
if (zoneid != -1) {
(void) zone_shutdown(zoneid);
(void) zone_destroy(zoneid);
}
if (rctlbuf != NULL)
free(rctlbuf);
if (zfsbuf != NULL)
free(zfsbuf);
priv_freeset(privs);
if (fp != NULL)
zonecfg_close_scratch(fp);
lofs_discard_mnttab();
if (zcent != NULL)
tsol_freezcent(zcent);
return (rval);
}
/*
* Enter the zone and write a /etc/zones/index file there. This allows
* libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
* details from inside the zone.
*/
static void
write_index_file(zoneid_t zoneid)
{
FILE *zef;
FILE *zet;
struct zoneent *zep;
pid_t child;
int tmpl_fd;
ctid_t ct;
int fd;
char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
/* Locate the zone entry in the global zone's index file */
if ((zef = setzoneent()) == NULL)
return;
while ((zep = getzoneent_private(zef)) != NULL) {
if (strcmp(zep->zone_name, zone_name) == 0)
break;
free(zep);
}
endzoneent(zef);
if (zep == NULL)
return;
if ((tmpl_fd = init_template()) == -1) {
free(zep);
return;
}
if ((child = fork()) == -1) {
(void) ct_tmpl_clear(tmpl_fd);
(void) close(tmpl_fd);
free(zep);
return;
}
/* parent waits for child to finish */
if (child != 0) {
free(zep);
if (contract_latest(&ct) == -1)
ct = -1;
(void) ct_tmpl_clear(tmpl_fd);
(void) close(tmpl_fd);
(void) waitpid(child, NULL, 0);
(void) contract_abandon_id(ct);
return;
}
/* child enters zone and sets up index file */
(void) ct_tmpl_clear(tmpl_fd);
if (zone_enter(zoneid) != -1) {
(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
ZONE_CONFIG_GID);
fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
ZONE_INDEX_MODE);
if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
if (uuid_is_null(zep->zone_uuid))
uuidstr[0] = '\0';
else
uuid_unparse(zep->zone_uuid, uuidstr);
(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
zone_state_str(zep->zone_state),
uuidstr);
(void) fclose(zet);
}
}
_exit(0);
}
int
vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
{
char zpath[MAXPATHLEN];
if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
lofs_discard_mnttab();
return (-1);
}
/*
* Before we try to mount filesystems we need to create the
* attribute backing store for /dev
*/
(void) strlcpy(zpath, zonepath, sizeof (zpath));
resolve_lofs(zlogp, zpath, sizeof (zpath));
/* Make /dev directory owned by root, grouped sys */
if (make_one_dir(zlogp, zpath, "/dev", DEFAULT_DIR_MODE, 0, 3) != 0) {
lofs_discard_mnttab();
return (-1);
}
if (mount_filesystems(zlogp, mount_cmd) != 0) {
lofs_discard_mnttab();
return (-1);
}
if (mount_cmd == Z_MNT_BOOT) {
zone_iptype_t iptype;
if (vplat_get_iptype(zlogp, &iptype) < 0) {
zerror(zlogp, B_TRUE, "unable to determine ip-type");
lofs_discard_mnttab();
return (-1);
}
switch (iptype) {
case ZS_SHARED:
/* Always do this to make lo0 get configured */
if (configure_shared_network_interfaces(zlogp) != 0) {
lofs_discard_mnttab();
return (-1);
}
break;
case ZS_EXCLUSIVE:
if (configure_exclusive_network_interfaces(zlogp,
zoneid) !=
0) {
lofs_discard_mnttab();
return (-1);
}
break;
default:
abort();
}
}
write_index_file(zoneid);
lofs_discard_mnttab();
return (0);
}
static int
lu_root_teardown(zlog_t *zlogp)
{
char zroot[MAXPATHLEN];
if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
zerror(zlogp, B_FALSE, "unable to determine zone root");
return (-1);
}
root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
/*
* At this point, the processes are gone, the filesystems (save the
* root) are unmounted, and the zone is on death row. But there may
* still be creds floating about in the system that reference the
* zone_t, and which pin down zone_rootvp causing this call to fail
* with EBUSY. Thus, we try for a little while before just giving up.
* (How I wish this were not true, and umount2 just did the right
* thing, or tmpfs supported MS_FORCE This is a gross hack.)
*/
if (umount2(zroot, MS_FORCE) != 0) {
if (errno == ENOTSUP && umount2(zroot, 0) == 0)
goto unmounted;
if (errno == EBUSY) {
int tries = 10;
while (--tries >= 0) {
(void) sleep(1);
if (umount2(zroot, 0) == 0)
goto unmounted;
if (errno != EBUSY)
break;
}
}
zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
return (-1);
}
unmounted:
/*
* Only zones in an alternate root environment have scratch zone
* entries.
*/
if (zonecfg_in_alt_root()) {
FILE *fp;
int retv;
if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
zerror(zlogp, B_TRUE, "cannot open mapfile");
return (-1);
}
retv = -1;
if (zonecfg_lock_scratch(fp) != 0)
zerror(zlogp, B_TRUE, "cannot lock mapfile");
else if (zonecfg_delete_scratch(fp, kernzone) != 0)
zerror(zlogp, B_TRUE, "cannot delete map entry");
else
retv = 0;
zonecfg_close_scratch(fp);
return (retv);
} else {
return (0);
}
}
/*
* Delete all transient VNICs belonging to this zone. A transient VNIC
* is one that is created and destroyed along with the lifetime of the
* zone. Non-transient VNICs, ones that are assigned from the GZ to a
* NGZ, are reassigned to the GZ in zone_shutdown() via the
* zone-specific data (zsd) callbacks.
*/
static int
delete_transient_vnics(zlog_t *zlogp, zoneid_t zoneid)
{
dladm_status_t status;
int num_links = 0;
datalink_id_t *links, link;
uint32_t link_flags;
datalink_class_t link_class;
char link_name[MAXLINKNAMELEN];
if (zone_list_datalink(zoneid, &num_links, NULL) != 0) {
zerror(zlogp, B_TRUE, "unable to determine "
"number of network interfaces");
return (-1);
}
if (num_links == 0)
return (0);
links = malloc(num_links * sizeof (datalink_id_t));
if (links == NULL) {
zerror(zlogp, B_TRUE, "failed to delete "
"network interfaces because of alloc fail");
return (-1);
}
if (zone_list_datalink(zoneid, &num_links, links) != 0) {
zerror(zlogp, B_TRUE, "failed to delete "
"network interfaces because of failure "
"to list them");
return (-1);
}
for (int i = 0; i < num_links; i++) {
char dlerr[DLADM_STRSIZE];
link = links[i];
status = dladm_datalink_id2info(dld_handle, link, &link_flags,
&link_class, NULL, link_name, sizeof (link_name));
if (status != DLADM_STATUS_OK) {
zerror(zlogp, B_FALSE, "failed to "
"delete network interface (%u)"
"due to failure to get link info: %s",
link,
dladm_status2str(status, dlerr));
return (-1);
}
if (link_flags & DLADM_OPT_TRANSIENT) {
assert(link_class & DATALINK_CLASS_VNIC);
status = dladm_vnic_delete(dld_handle, link,
DLADM_OPT_ACTIVE);
if (status != DLADM_STATUS_OK) {
zerror(zlogp, B_TRUE, "failed to delete link "
"with id %d: %s", link,
dladm_status2str(status, dlerr));
return (-1);
}
}
}
return (0);
}
int
vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting,
boolean_t debug)
{
char *kzone;
zoneid_t zoneid;
int res;
char pool_err[128];
char cmdbuf[MAXPATHLEN];
brand_handle_t bh = NULL;
dladm_status_t status;
char errmsg[DLADM_STRSIZE];
ushort_t flags;
kzone = zone_name;
if (zonecfg_in_alt_root()) {
FILE *fp;
if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
zerror(zlogp, B_TRUE, "unable to open map file");
goto error;
}
if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
kernzone, sizeof (kernzone)) != 0) {
zerror(zlogp, B_FALSE, "unable to find scratch zone");
zonecfg_close_scratch(fp);
goto error;
}
zonecfg_close_scratch(fp);
kzone = kernzone;
}
if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
if (!bringup_failure_recovery)
zerror(zlogp, B_TRUE, "unable to get zoneid");
if (unmount_cmd)
(void) lu_root_teardown(zlogp);
goto error;
}
if (remove_datalink_pool(zlogp, zoneid) != 0)
zerror(zlogp, B_FALSE, "unable clear datalink pool property");
if (remove_datalink_protect(zlogp, zoneid) != 0)
zerror(zlogp, B_FALSE,
"unable clear datalink protect property");
/*
* The datalinks assigned to the zone will be removed from the NGZ as
* part of zone_shutdown() so that we need to remove protect/pool etc.
* before zone_shutdown(). Even if the shutdown itself fails, the zone
* will not be able to violate any constraints applied because the
* datalinks are no longer available to the zone.
*/
if (zone_shutdown(zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to shutdown zone");
goto error;
}
/* Get a handle to the brand info for this zone */
if ((bh = brand_open(brand_name)) == NULL) {
zerror(zlogp, B_FALSE, "unable to determine zone brand");
return (-1);
}
/*
* If there is a brand 'halt' callback, execute it now to give the
* brand a chance to cleanup any custom configuration.
*/
(void) strcpy(cmdbuf, EXEC_PREFIX);
if (brand_get_halt(bh, zone_name, zonepath, cmdbuf + EXEC_LEN,
sizeof (cmdbuf) - EXEC_LEN) < 0) {
brand_close(bh);
zerror(zlogp, B_FALSE, "unable to determine branded zone's "
"halt callback.");
goto error;
}
brand_close(bh);
if ((strlen(cmdbuf) > EXEC_LEN) &&
(do_subproc(zlogp, cmdbuf, NULL, debug) != Z_OK)) {
zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
goto error;
}
if (!unmount_cmd) {
zone_iptype_t iptype;
if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
sizeof (flags)) < 0) {
if (vplat_get_iptype(zlogp, &iptype) < 0) {
zerror(zlogp, B_TRUE, "unable to determine "
"ip-type");
goto error;
}
} else {
if (flags & ZF_NET_EXCL)
iptype = ZS_EXCLUSIVE;
else
iptype = ZS_SHARED;
}
switch (iptype) {
case ZS_SHARED:
if (unconfigure_shared_network_interfaces(zlogp,
zoneid) != 0) {
zerror(zlogp, B_FALSE, "unable to unconfigure "
"network interfaces in zone");
goto error;
}
break;
case ZS_EXCLUSIVE:
if (delete_transient_vnics(zlogp, zoneid) != 0) {
zerror(zlogp, B_FALSE, "unable to delete "
"transient vnics in zone");
goto error;
}
status = dladm_zone_halt(dld_handle, zoneid);
if (status != DLADM_STATUS_OK) {
zerror(zlogp, B_FALSE, "unable to notify "
"dlmgmtd of zone halt: %s",
dladm_status2str(status, errmsg));
goto error;
}
break;
}
}
if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to abort TCP connections");
goto error;
}
if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
zerror(zlogp, B_FALSE,
"unable to unmount file systems in zone");
goto error;
}
/*
* If we are rebooting then we normally don't want to destroy an
* existing temporary pool at this point so that we can just reuse it
* when the zone boots back up. However, it is also possible we were
* running with a temporary pool and the zone configuration has been
* modified to no longer use a temporary pool. In that case we need
* to destroy the temporary pool now. This case looks like the case
* where we never had a temporary pool configured but
* zonecfg_destroy_tmp_pool will do the right thing either way.
*/
if (!unmount_cmd) {
boolean_t destroy_tmp_pool = B_TRUE;
if (rebooting) {
struct zone_psettab pset_tab;
if (zonecfg_lookup_pset(snap_hndl, &pset_tab) == Z_OK)
destroy_tmp_pool = B_FALSE;
}
if (destroy_tmp_pool) {
if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
sizeof (pool_err))) != Z_OK) {
if (res == Z_POOL)
zerror(zlogp, B_FALSE, pool_err);
}
}
}
remove_mlps(zlogp, zoneid);
if (zone_destroy(zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to destroy zone");
goto error;
}
/*
* Special teardown for alternate boot environments: remove the tmpfs
* root for the zone and then remove it from the map file.
*/
if (unmount_cmd && lu_root_teardown(zlogp) != 0)
goto error;
lofs_discard_mnttab();
return (0);
error:
lofs_discard_mnttab();
return (-1);
}
|