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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Ceph msgr2 protocol implementation
*
* Copyright (C) 2020 Ilya Dryomov <idryomov@gmail.com>
*/
#include <linux/ceph/ceph_debug.h>
#include <crypto/aead.h>
#include <crypto/hash.h>
#include <crypto/sha2.h>
#include <crypto/utils.h>
#include <linux/bvec.h>
#include <linux/crc32c.h>
#include <linux/net.h>
#include <linux/scatterlist.h>
#include <linux/socket.h>
#include <linux/sched/mm.h>
#include <net/sock.h>
#include <net/tcp.h>
#include <linux/ceph/ceph_features.h>
#include <linux/ceph/decode.h>
#include <linux/ceph/libceph.h>
#include <linux/ceph/messenger.h>
#include "crypto.h" /* for CEPH_KEY_LEN and CEPH_MAX_CON_SECRET_LEN */
#define FRAME_TAG_HELLO 1
#define FRAME_TAG_AUTH_REQUEST 2
#define FRAME_TAG_AUTH_BAD_METHOD 3
#define FRAME_TAG_AUTH_REPLY_MORE 4
#define FRAME_TAG_AUTH_REQUEST_MORE 5
#define FRAME_TAG_AUTH_DONE 6
#define FRAME_TAG_AUTH_SIGNATURE 7
#define FRAME_TAG_CLIENT_IDENT 8
#define FRAME_TAG_SERVER_IDENT 9
#define FRAME_TAG_IDENT_MISSING_FEATURES 10
#define FRAME_TAG_SESSION_RECONNECT 11
#define FRAME_TAG_SESSION_RESET 12
#define FRAME_TAG_SESSION_RETRY 13
#define FRAME_TAG_SESSION_RETRY_GLOBAL 14
#define FRAME_TAG_SESSION_RECONNECT_OK 15
#define FRAME_TAG_WAIT 16
#define FRAME_TAG_MESSAGE 17
#define FRAME_TAG_KEEPALIVE2 18
#define FRAME_TAG_KEEPALIVE2_ACK 19
#define FRAME_TAG_ACK 20
#define FRAME_LATE_STATUS_ABORTED 0x1
#define FRAME_LATE_STATUS_COMPLETE 0xe
#define FRAME_LATE_STATUS_ABORTED_MASK 0xf
#define IN_S_HANDLE_PREAMBLE 1
#define IN_S_HANDLE_CONTROL 2
#define IN_S_HANDLE_CONTROL_REMAINDER 3
#define IN_S_PREPARE_READ_DATA 4
#define IN_S_PREPARE_READ_DATA_CONT 5
#define IN_S_PREPARE_READ_ENC_PAGE 6
#define IN_S_PREPARE_SPARSE_DATA 7
#define IN_S_PREPARE_SPARSE_DATA_CONT 8
#define IN_S_HANDLE_EPILOGUE 9
#define IN_S_FINISH_SKIP 10
#define OUT_S_QUEUE_DATA 1
#define OUT_S_QUEUE_DATA_CONT 2
#define OUT_S_QUEUE_ENC_PAGE 3
#define OUT_S_QUEUE_ZEROS 4
#define OUT_S_FINISH_MESSAGE 5
#define OUT_S_GET_NEXT 6
#define CTRL_BODY(p) ((void *)(p) + CEPH_PREAMBLE_LEN)
#define FRONT_PAD(p) ((void *)(p) + CEPH_EPILOGUE_SECURE_LEN)
#define MIDDLE_PAD(p) (FRONT_PAD(p) + CEPH_GCM_BLOCK_LEN)
#define DATA_PAD(p) (MIDDLE_PAD(p) + CEPH_GCM_BLOCK_LEN)
#define CEPH_MSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
static int do_recvmsg(struct socket *sock, struct iov_iter *it)
{
struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
int ret;
msg.msg_iter = *it;
while (iov_iter_count(it)) {
ret = sock_recvmsg(sock, &msg, msg.msg_flags);
if (ret <= 0) {
if (ret == -EAGAIN)
ret = 0;
return ret;
}
iov_iter_advance(it, ret);
}
WARN_ON(msg_data_left(&msg));
return 1;
}
/*
* Read as much as possible.
*
* Return:
* 1 - done, nothing (else) to read
* 0 - socket is empty, need to wait
* <0 - error
*/
static int ceph_tcp_recv(struct ceph_connection *con)
{
int ret;
dout("%s con %p %s %zu\n", __func__, con,
iov_iter_is_discard(&con->v2.in_iter) ? "discard" : "need",
iov_iter_count(&con->v2.in_iter));
ret = do_recvmsg(con->sock, &con->v2.in_iter);
dout("%s con %p ret %d left %zu\n", __func__, con, ret,
iov_iter_count(&con->v2.in_iter));
return ret;
}
static int do_sendmsg(struct socket *sock, struct iov_iter *it)
{
struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
int ret;
msg.msg_iter = *it;
while (iov_iter_count(it)) {
ret = sock_sendmsg(sock, &msg);
if (ret <= 0) {
if (ret == -EAGAIN)
ret = 0;
return ret;
}
iov_iter_advance(it, ret);
}
WARN_ON(msg_data_left(&msg));
return 1;
}
static int do_try_sendpage(struct socket *sock, struct iov_iter *it)
{
struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
struct bio_vec bv;
int ret;
if (WARN_ON(!iov_iter_is_bvec(it)))
return -EINVAL;
while (iov_iter_count(it)) {
/* iov_iter_iovec() for ITER_BVEC */
bvec_set_page(&bv, it->bvec->bv_page,
min(iov_iter_count(it),
it->bvec->bv_len - it->iov_offset),
it->bvec->bv_offset + it->iov_offset);
/*
* MSG_SPLICE_PAGES cannot properly handle pages with
* page_count == 0, we need to fall back to sendmsg if
* that's the case.
*
* Same goes for slab pages: skb_can_coalesce() allows
* coalescing neighboring slab objects into a single frag
* which triggers one of hardened usercopy checks.
*/
if (sendpage_ok(bv.bv_page))
msg.msg_flags |= MSG_SPLICE_PAGES;
else
msg.msg_flags &= ~MSG_SPLICE_PAGES;
iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bv, 1, bv.bv_len);
ret = sock_sendmsg(sock, &msg);
if (ret <= 0) {
if (ret == -EAGAIN)
ret = 0;
return ret;
}
iov_iter_advance(it, ret);
}
return 1;
}
/*
* Write as much as possible. The socket is expected to be corked,
* so we don't bother with MSG_MORE here.
*
* Return:
* 1 - done, nothing (else) to write
* 0 - socket is full, need to wait
* <0 - error
*/
static int ceph_tcp_send(struct ceph_connection *con)
{
int ret;
dout("%s con %p have %zu try_sendpage %d\n", __func__, con,
iov_iter_count(&con->v2.out_iter), con->v2.out_iter_sendpage);
if (con->v2.out_iter_sendpage)
ret = do_try_sendpage(con->sock, &con->v2.out_iter);
else
ret = do_sendmsg(con->sock, &con->v2.out_iter);
dout("%s con %p ret %d left %zu\n", __func__, con, ret,
iov_iter_count(&con->v2.out_iter));
return ret;
}
static void add_in_kvec(struct ceph_connection *con, void *buf, int len)
{
BUG_ON(con->v2.in_kvec_cnt >= ARRAY_SIZE(con->v2.in_kvecs));
WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_base = buf;
con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_len = len;
con->v2.in_kvec_cnt++;
con->v2.in_iter.nr_segs++;
con->v2.in_iter.count += len;
}
static void reset_in_kvecs(struct ceph_connection *con)
{
WARN_ON(iov_iter_count(&con->v2.in_iter));
con->v2.in_kvec_cnt = 0;
iov_iter_kvec(&con->v2.in_iter, ITER_DEST, con->v2.in_kvecs, 0, 0);
}
static void set_in_bvec(struct ceph_connection *con, const struct bio_vec *bv)
{
WARN_ON(iov_iter_count(&con->v2.in_iter));
con->v2.in_bvec = *bv;
iov_iter_bvec(&con->v2.in_iter, ITER_DEST, &con->v2.in_bvec, 1, bv->bv_len);
}
static void set_in_skip(struct ceph_connection *con, int len)
{
WARN_ON(iov_iter_count(&con->v2.in_iter));
dout("%s con %p len %d\n", __func__, con, len);
iov_iter_discard(&con->v2.in_iter, ITER_DEST, len);
}
static void add_out_kvec(struct ceph_connection *con, void *buf, int len)
{
BUG_ON(con->v2.out_kvec_cnt >= ARRAY_SIZE(con->v2.out_kvecs));
WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
WARN_ON(con->v2.out_zero);
con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_base = buf;
con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_len = len;
con->v2.out_kvec_cnt++;
con->v2.out_iter.nr_segs++;
con->v2.out_iter.count += len;
}
static void reset_out_kvecs(struct ceph_connection *con)
{
WARN_ON(iov_iter_count(&con->v2.out_iter));
WARN_ON(con->v2.out_zero);
con->v2.out_kvec_cnt = 0;
iov_iter_kvec(&con->v2.out_iter, ITER_SOURCE, con->v2.out_kvecs, 0, 0);
con->v2.out_iter_sendpage = false;
}
static void set_out_bvec(struct ceph_connection *con, const struct bio_vec *bv,
bool zerocopy)
{
WARN_ON(iov_iter_count(&con->v2.out_iter));
WARN_ON(con->v2.out_zero);
con->v2.out_bvec = *bv;
con->v2.out_iter_sendpage = zerocopy;
iov_iter_bvec(&con->v2.out_iter, ITER_SOURCE, &con->v2.out_bvec, 1,
con->v2.out_bvec.bv_len);
}
static void set_out_bvec_zero(struct ceph_connection *con)
{
WARN_ON(iov_iter_count(&con->v2.out_iter));
WARN_ON(!con->v2.out_zero);
bvec_set_page(&con->v2.out_bvec, ceph_zero_page,
min(con->v2.out_zero, (int)PAGE_SIZE), 0);
con->v2.out_iter_sendpage = true;
iov_iter_bvec(&con->v2.out_iter, ITER_SOURCE, &con->v2.out_bvec, 1,
con->v2.out_bvec.bv_len);
}
static void out_zero_add(struct ceph_connection *con, int len)
{
dout("%s con %p len %d\n", __func__, con, len);
con->v2.out_zero += len;
}
static void *alloc_conn_buf(struct ceph_connection *con, int len)
{
void *buf;
dout("%s con %p len %d\n", __func__, con, len);
if (WARN_ON(con->v2.conn_buf_cnt >= ARRAY_SIZE(con->v2.conn_bufs)))
return NULL;
buf = kvmalloc(len, GFP_NOIO);
if (!buf)
return NULL;
con->v2.conn_bufs[con->v2.conn_buf_cnt++] = buf;
return buf;
}
static void free_conn_bufs(struct ceph_connection *con)
{
while (con->v2.conn_buf_cnt)
kvfree(con->v2.conn_bufs[--con->v2.conn_buf_cnt]);
}
static void add_in_sign_kvec(struct ceph_connection *con, void *buf, int len)
{
BUG_ON(con->v2.in_sign_kvec_cnt >= ARRAY_SIZE(con->v2.in_sign_kvecs));
con->v2.in_sign_kvecs[con->v2.in_sign_kvec_cnt].iov_base = buf;
con->v2.in_sign_kvecs[con->v2.in_sign_kvec_cnt].iov_len = len;
con->v2.in_sign_kvec_cnt++;
}
static void clear_in_sign_kvecs(struct ceph_connection *con)
{
con->v2.in_sign_kvec_cnt = 0;
}
static void add_out_sign_kvec(struct ceph_connection *con, void *buf, int len)
{
BUG_ON(con->v2.out_sign_kvec_cnt >= ARRAY_SIZE(con->v2.out_sign_kvecs));
con->v2.out_sign_kvecs[con->v2.out_sign_kvec_cnt].iov_base = buf;
con->v2.out_sign_kvecs[con->v2.out_sign_kvec_cnt].iov_len = len;
con->v2.out_sign_kvec_cnt++;
}
static void clear_out_sign_kvecs(struct ceph_connection *con)
{
con->v2.out_sign_kvec_cnt = 0;
}
static bool con_secure(struct ceph_connection *con)
{
return con->v2.con_mode == CEPH_CON_MODE_SECURE;
}
static int front_len(const struct ceph_msg *msg)
{
return le32_to_cpu(msg->hdr.front_len);
}
static int middle_len(const struct ceph_msg *msg)
{
return le32_to_cpu(msg->hdr.middle_len);
}
static int data_len(const struct ceph_msg *msg)
{
return le32_to_cpu(msg->hdr.data_len);
}
static bool need_padding(int len)
{
return !IS_ALIGNED(len, CEPH_GCM_BLOCK_LEN);
}
static int padded_len(int len)
{
return ALIGN(len, CEPH_GCM_BLOCK_LEN);
}
static int padding_len(int len)
{
return padded_len(len) - len;
}
/* preamble + control segment */
static int head_onwire_len(int ctrl_len, bool secure)
{
int head_len;
int rem_len;
BUG_ON(ctrl_len < 0 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
if (secure) {
head_len = CEPH_PREAMBLE_SECURE_LEN;
if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN;
}
} else {
head_len = CEPH_PREAMBLE_PLAIN_LEN;
if (ctrl_len)
head_len += ctrl_len + CEPH_CRC_LEN;
}
return head_len;
}
/* front, middle and data segments + epilogue */
static int __tail_onwire_len(int front_len, int middle_len, int data_len,
bool secure)
{
BUG_ON(front_len < 0 || front_len > CEPH_MSG_MAX_FRONT_LEN ||
middle_len < 0 || middle_len > CEPH_MSG_MAX_MIDDLE_LEN ||
data_len < 0 || data_len > CEPH_MSG_MAX_DATA_LEN);
if (!front_len && !middle_len && !data_len)
return 0;
if (!secure)
return front_len + middle_len + data_len +
CEPH_EPILOGUE_PLAIN_LEN;
return padded_len(front_len) + padded_len(middle_len) +
padded_len(data_len) + CEPH_EPILOGUE_SECURE_LEN;
}
static int tail_onwire_len(const struct ceph_msg *msg, bool secure)
{
return __tail_onwire_len(front_len(msg), middle_len(msg),
data_len(msg), secure);
}
/* head_onwire_len(sizeof(struct ceph_msg_header2), false) */
#define MESSAGE_HEAD_PLAIN_LEN (CEPH_PREAMBLE_PLAIN_LEN + \
sizeof(struct ceph_msg_header2) + \
CEPH_CRC_LEN)
static const int frame_aligns[] = {
sizeof(void *),
sizeof(void *),
sizeof(void *),
PAGE_SIZE
};
/*
* Discards trailing empty segments, unless there is just one segment.
* A frame always has at least one (possibly empty) segment.
*/
static int calc_segment_count(const int *lens, int len_cnt)
{
int i;
for (i = len_cnt - 1; i >= 0; i--) {
if (lens[i])
return i + 1;
}
return 1;
}
static void init_frame_desc(struct ceph_frame_desc *desc, int tag,
const int *lens, int len_cnt)
{
int i;
memset(desc, 0, sizeof(*desc));
desc->fd_tag = tag;
desc->fd_seg_cnt = calc_segment_count(lens, len_cnt);
BUG_ON(desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT);
for (i = 0; i < desc->fd_seg_cnt; i++) {
desc->fd_lens[i] = lens[i];
desc->fd_aligns[i] = frame_aligns[i];
}
}
/*
* Preamble crc covers everything up to itself (28 bytes) and
* is calculated and verified irrespective of the connection mode
* (i.e. even if the frame is encrypted).
*/
static void encode_preamble(const struct ceph_frame_desc *desc, void *p)
{
void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN;
void *start = p;
int i;
memset(p, 0, CEPH_PREAMBLE_LEN);
ceph_encode_8(&p, desc->fd_tag);
ceph_encode_8(&p, desc->fd_seg_cnt);
for (i = 0; i < desc->fd_seg_cnt; i++) {
ceph_encode_32(&p, desc->fd_lens[i]);
ceph_encode_16(&p, desc->fd_aligns[i]);
}
put_unaligned_le32(crc32c(0, start, crcp - start), crcp);
}
static int decode_preamble(void *p, struct ceph_frame_desc *desc)
{
void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN;
u32 crc, expected_crc;
int i;
crc = crc32c(0, p, crcp - p);
expected_crc = get_unaligned_le32(crcp);
if (crc != expected_crc) {
pr_err("bad preamble crc, calculated %u, expected %u\n",
crc, expected_crc);
return -EBADMSG;
}
memset(desc, 0, sizeof(*desc));
desc->fd_tag = ceph_decode_8(&p);
desc->fd_seg_cnt = ceph_decode_8(&p);
if (desc->fd_seg_cnt < 1 ||
desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT) {
pr_err("bad segment count %d\n", desc->fd_seg_cnt);
return -EINVAL;
}
for (i = 0; i < desc->fd_seg_cnt; i++) {
desc->fd_lens[i] = ceph_decode_32(&p);
desc->fd_aligns[i] = ceph_decode_16(&p);
}
if (desc->fd_lens[0] < 0 ||
desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) {
pr_err("bad control segment length %d\n", desc->fd_lens[0]);
return -EINVAL;
}
if (desc->fd_lens[1] < 0 ||
desc->fd_lens[1] > CEPH_MSG_MAX_FRONT_LEN) {
pr_err("bad front segment length %d\n", desc->fd_lens[1]);
return -EINVAL;
}
if (desc->fd_lens[2] < 0 ||
desc->fd_lens[2] > CEPH_MSG_MAX_MIDDLE_LEN) {
pr_err("bad middle segment length %d\n", desc->fd_lens[2]);
return -EINVAL;
}
if (desc->fd_lens[3] < 0 ||
desc->fd_lens[3] > CEPH_MSG_MAX_DATA_LEN) {
pr_err("bad data segment length %d\n", desc->fd_lens[3]);
return -EINVAL;
}
/*
* This would fire for FRAME_TAG_WAIT (it has one empty
* segment), but we should never get it as client.
*/
if (!desc->fd_lens[desc->fd_seg_cnt - 1]) {
pr_err("last segment empty, segment count %d\n",
desc->fd_seg_cnt);
return -EINVAL;
}
return 0;
}
static void encode_epilogue_plain(struct ceph_connection *con, bool aborted)
{
con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED :
FRAME_LATE_STATUS_COMPLETE;
cpu_to_le32s(&con->v2.out_epil.front_crc);
cpu_to_le32s(&con->v2.out_epil.middle_crc);
cpu_to_le32s(&con->v2.out_epil.data_crc);
}
static void encode_epilogue_secure(struct ceph_connection *con, bool aborted)
{
memset(&con->v2.out_epil, 0, sizeof(con->v2.out_epil));
con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED :
FRAME_LATE_STATUS_COMPLETE;
}
static int decode_epilogue(void *p, u32 *front_crc, u32 *middle_crc,
u32 *data_crc)
{
u8 late_status;
late_status = ceph_decode_8(&p);
if ((late_status & FRAME_LATE_STATUS_ABORTED_MASK) !=
FRAME_LATE_STATUS_COMPLETE) {
/* we should never get an aborted message as client */
pr_err("bad late_status 0x%x\n", late_status);
return -EINVAL;
}
if (front_crc && middle_crc && data_crc) {
*front_crc = ceph_decode_32(&p);
*middle_crc = ceph_decode_32(&p);
*data_crc = ceph_decode_32(&p);
}
return 0;
}
static void fill_header(struct ceph_msg_header *hdr,
const struct ceph_msg_header2 *hdr2,
int front_len, int middle_len, int data_len,
const struct ceph_entity_name *peer_name)
{
hdr->seq = hdr2->seq;
hdr->tid = hdr2->tid;
hdr->type = hdr2->type;
hdr->priority = hdr2->priority;
hdr->version = hdr2->version;
hdr->front_len = cpu_to_le32(front_len);
hdr->middle_len = cpu_to_le32(middle_len);
hdr->data_len = cpu_to_le32(data_len);
hdr->data_off = hdr2->data_off;
hdr->src = *peer_name;
hdr->compat_version = hdr2->compat_version;
hdr->reserved = 0;
hdr->crc = 0;
}
static void fill_header2(struct ceph_msg_header2 *hdr2,
const struct ceph_msg_header *hdr, u64 ack_seq)
{
hdr2->seq = hdr->seq;
hdr2->tid = hdr->tid;
hdr2->type = hdr->type;
hdr2->priority = hdr->priority;
hdr2->version = hdr->version;
hdr2->data_pre_padding_len = 0;
hdr2->data_off = hdr->data_off;
hdr2->ack_seq = cpu_to_le64(ack_seq);
hdr2->flags = 0;
hdr2->compat_version = hdr->compat_version;
hdr2->reserved = 0;
}
static int verify_control_crc(struct ceph_connection *con)
{
int ctrl_len = con->v2.in_desc.fd_lens[0];
u32 crc, expected_crc;
WARN_ON(con->v2.in_kvecs[0].iov_len != ctrl_len);
WARN_ON(con->v2.in_kvecs[1].iov_len != CEPH_CRC_LEN);
crc = crc32c(-1, con->v2.in_kvecs[0].iov_base, ctrl_len);
expected_crc = get_unaligned_le32(con->v2.in_kvecs[1].iov_base);
if (crc != expected_crc) {
pr_err("bad control crc, calculated %u, expected %u\n",
crc, expected_crc);
return -EBADMSG;
}
return 0;
}
static int verify_epilogue_crcs(struct ceph_connection *con, u32 front_crc,
u32 middle_crc, u32 data_crc)
{
if (front_len(con->in_msg)) {
con->in_front_crc = crc32c(-1, con->in_msg->front.iov_base,
front_len(con->in_msg));
} else {
WARN_ON(!middle_len(con->in_msg) && !data_len(con->in_msg));
con->in_front_crc = -1;
}
if (middle_len(con->in_msg))
con->in_middle_crc = crc32c(-1,
con->in_msg->middle->vec.iov_base,
middle_len(con->in_msg));
else if (data_len(con->in_msg))
con->in_middle_crc = -1;
else
con->in_middle_crc = 0;
if (!data_len(con->in_msg))
con->in_data_crc = 0;
dout("%s con %p msg %p crcs %u %u %u\n", __func__, con, con->in_msg,
con->in_front_crc, con->in_middle_crc, con->in_data_crc);
if (con->in_front_crc != front_crc) {
pr_err("bad front crc, calculated %u, expected %u\n",
con->in_front_crc, front_crc);
return -EBADMSG;
}
if (con->in_middle_crc != middle_crc) {
pr_err("bad middle crc, calculated %u, expected %u\n",
con->in_middle_crc, middle_crc);
return -EBADMSG;
}
if (con->in_data_crc != data_crc) {
pr_err("bad data crc, calculated %u, expected %u\n",
con->in_data_crc, data_crc);
return -EBADMSG;
}
return 0;
}
static int setup_crypto(struct ceph_connection *con,
const u8 *session_key, int session_key_len,
const u8 *con_secret, int con_secret_len)
{
unsigned int noio_flag;
int ret;
dout("%s con %p con_mode %d session_key_len %d con_secret_len %d\n",
__func__, con, con->v2.con_mode, session_key_len, con_secret_len);
WARN_ON(con->v2.hmac_tfm || con->v2.gcm_tfm || con->v2.gcm_req);
if (con->v2.con_mode != CEPH_CON_MODE_CRC &&
con->v2.con_mode != CEPH_CON_MODE_SECURE) {
pr_err("bad con_mode %d\n", con->v2.con_mode);
return -EINVAL;
}
if (!session_key_len) {
WARN_ON(con->v2.con_mode != CEPH_CON_MODE_CRC);
WARN_ON(con_secret_len);
return 0; /* auth_none */
}
noio_flag = memalloc_noio_save();
con->v2.hmac_tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
memalloc_noio_restore(noio_flag);
if (IS_ERR(con->v2.hmac_tfm)) {
ret = PTR_ERR(con->v2.hmac_tfm);
con->v2.hmac_tfm = NULL;
pr_err("failed to allocate hmac tfm context: %d\n", ret);
return ret;
}
ret = crypto_shash_setkey(con->v2.hmac_tfm, session_key,
session_key_len);
if (ret) {
pr_err("failed to set hmac key: %d\n", ret);
return ret;
}
if (con->v2.con_mode == CEPH_CON_MODE_CRC) {
WARN_ON(con_secret_len);
return 0; /* auth_x, plain mode */
}
if (con_secret_len < CEPH_GCM_KEY_LEN + 2 * CEPH_GCM_IV_LEN) {
pr_err("con_secret too small %d\n", con_secret_len);
return -EINVAL;
}
noio_flag = memalloc_noio_save();
con->v2.gcm_tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
memalloc_noio_restore(noio_flag);
if (IS_ERR(con->v2.gcm_tfm)) {
ret = PTR_ERR(con->v2.gcm_tfm);
con->v2.gcm_tfm = NULL;
pr_err("failed to allocate gcm tfm context: %d\n", ret);
return ret;
}
WARN_ON((unsigned long)con_secret &
crypto_aead_alignmask(con->v2.gcm_tfm));
ret = crypto_aead_setkey(con->v2.gcm_tfm, con_secret, CEPH_GCM_KEY_LEN);
if (ret) {
pr_err("failed to set gcm key: %d\n", ret);
return ret;
}
WARN_ON(crypto_aead_ivsize(con->v2.gcm_tfm) != CEPH_GCM_IV_LEN);
ret = crypto_aead_setauthsize(con->v2.gcm_tfm, CEPH_GCM_TAG_LEN);
if (ret) {
pr_err("failed to set gcm tag size: %d\n", ret);
return ret;
}
con->v2.gcm_req = aead_request_alloc(con->v2.gcm_tfm, GFP_NOIO);
if (!con->v2.gcm_req) {
pr_err("failed to allocate gcm request\n");
return -ENOMEM;
}
crypto_init_wait(&con->v2.gcm_wait);
aead_request_set_callback(con->v2.gcm_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
crypto_req_done, &con->v2.gcm_wait);
memcpy(&con->v2.in_gcm_nonce, con_secret + CEPH_GCM_KEY_LEN,
CEPH_GCM_IV_LEN);
memcpy(&con->v2.out_gcm_nonce,
con_secret + CEPH_GCM_KEY_LEN + CEPH_GCM_IV_LEN,
CEPH_GCM_IV_LEN);
return 0; /* auth_x, secure mode */
}
static int hmac_sha256(struct ceph_connection *con, const struct kvec *kvecs,
int kvec_cnt, u8 *hmac)
{
SHASH_DESC_ON_STACK(desc, con->v2.hmac_tfm); /* tfm arg is ignored */
int ret;
int i;
dout("%s con %p hmac_tfm %p kvec_cnt %d\n", __func__, con,
con->v2.hmac_tfm, kvec_cnt);
if (!con->v2.hmac_tfm) {
memset(hmac, 0, SHA256_DIGEST_SIZE);
return 0; /* auth_none */
}
desc->tfm = con->v2.hmac_tfm;
ret = crypto_shash_init(desc);
if (ret)
goto out;
for (i = 0; i < kvec_cnt; i++) {
ret = crypto_shash_update(desc, kvecs[i].iov_base,
kvecs[i].iov_len);
if (ret)
goto out;
}
ret = crypto_shash_final(desc, hmac);
out:
shash_desc_zero(desc);
return ret; /* auth_x, both plain and secure modes */
}
static void gcm_inc_nonce(struct ceph_gcm_nonce *nonce)
{
u64 counter;
counter = le64_to_cpu(nonce->counter);
nonce->counter = cpu_to_le64(counter + 1);
}
static int gcm_crypt(struct ceph_connection *con, bool encrypt,
struct scatterlist *src, struct scatterlist *dst,
int src_len)
{
struct ceph_gcm_nonce *nonce;
int ret;
nonce = encrypt ? &con->v2.out_gcm_nonce : &con->v2.in_gcm_nonce;
aead_request_set_ad(con->v2.gcm_req, 0); /* no AAD */
aead_request_set_crypt(con->v2.gcm_req, src, dst, src_len, (u8 *)nonce);
ret = crypto_wait_req(encrypt ? crypto_aead_encrypt(con->v2.gcm_req) :
crypto_aead_decrypt(con->v2.gcm_req),
&con->v2.gcm_wait);
if (ret)
return ret;
gcm_inc_nonce(nonce);
return 0;
}
static void get_bvec_at(struct ceph_msg_data_cursor *cursor,
struct bio_vec *bv)
{
struct page *page;
size_t off, len;
WARN_ON(!cursor->total_resid);
/* skip zero-length data items */
while (!cursor->resid)
ceph_msg_data_advance(cursor, 0);
/* get a piece of data, cursor isn't advanced */
page = ceph_msg_data_next(cursor, &off, &len);
bvec_set_page(bv, page, len, off);
}
static int calc_sg_cnt(void *buf, int buf_len)
{
int sg_cnt;
if (!buf_len)
return 0;
sg_cnt = need_padding(buf_len) ? 1 : 0;
if (is_vmalloc_addr(buf)) {
WARN_ON(offset_in_page(buf));
sg_cnt += PAGE_ALIGN(buf_len) >> PAGE_SHIFT;
} else {
sg_cnt++;
}
return sg_cnt;
}
static int calc_sg_cnt_cursor(struct ceph_msg_data_cursor *cursor)
{
int data_len = cursor->total_resid;
struct bio_vec bv;
int sg_cnt;
if (!data_len)
return 0;
sg_cnt = need_padding(data_len) ? 1 : 0;
do {
get_bvec_at(cursor, &bv);
sg_cnt++;
ceph_msg_data_advance(cursor, bv.bv_len);
} while (cursor->total_resid);
return sg_cnt;
}
static void init_sgs(struct scatterlist **sg, void *buf, int buf_len, u8 *pad)
{
void *end = buf + buf_len;
struct page *page;
int len;
void *p;
if (!buf_len)
return;
if (is_vmalloc_addr(buf)) {
p = buf;
do {
page = vmalloc_to_page(p);
len = min_t(int, end - p, PAGE_SIZE);
WARN_ON(!page || !len || offset_in_page(p));
sg_set_page(*sg, page, len, 0);
*sg = sg_next(*sg);
p += len;
} while (p != end);
} else {
sg_set_buf(*sg, buf, buf_len);
*sg = sg_next(*sg);
}
if (need_padding(buf_len)) {
sg_set_buf(*sg, pad, padding_len(buf_len));
*sg = sg_next(*sg);
}
}
static void init_sgs_cursor(struct scatterlist **sg,
struct ceph_msg_data_cursor *cursor, u8 *pad)
{
int data_len = cursor->total_resid;
struct bio_vec bv;
if (!data_len)
return;
do {
get_bvec_at(cursor, &bv);
sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
*sg = sg_next(*sg);
ceph_msg_data_advance(cursor, bv.bv_len);
} while (cursor->total_resid);
if (need_padding(data_len)) {
sg_set_buf(*sg, pad, padding_len(data_len));
*sg = sg_next(*sg);
}
}
/**
* init_sgs_pages: set up scatterlist on an array of page pointers
* @sg: scatterlist to populate
* @pages: pointer to page array
* @dpos: position in the array to start (bytes)
* @dlen: len to add to sg (bytes)
* @pad: pointer to pad destination (if any)
*
* Populate the scatterlist from the page array, starting at an arbitrary
* byte in the array and running for a specified length.
*/
static void init_sgs_pages(struct scatterlist **sg, struct page **pages,
int dpos, int dlen, u8 *pad)
{
int idx = dpos >> PAGE_SHIFT;
int off = offset_in_page(dpos);
int resid = dlen;
do {
int len = min(resid, (int)PAGE_SIZE - off);
sg_set_page(*sg, pages[idx], len, off);
*sg = sg_next(*sg);
off = 0;
++idx;
resid -= len;
} while (resid);
if (need_padding(dlen)) {
sg_set_buf(*sg, pad, padding_len(dlen));
*sg = sg_next(*sg);
}
}
static int setup_message_sgs(struct sg_table *sgt, struct ceph_msg *msg,
u8 *front_pad, u8 *middle_pad, u8 *data_pad,
void *epilogue, struct page **pages, int dpos,
bool add_tag)
{
struct ceph_msg_data_cursor cursor;
struct scatterlist *cur_sg;
int dlen = data_len(msg);
int sg_cnt;
int ret;
if (!front_len(msg) && !middle_len(msg) && !data_len(msg))
return 0;
sg_cnt = 1; /* epilogue + [auth tag] */
if (front_len(msg))
sg_cnt += calc_sg_cnt(msg->front.iov_base,
front_len(msg));
if (middle_len(msg))
sg_cnt += calc_sg_cnt(msg->middle->vec.iov_base,
middle_len(msg));
if (dlen) {
if (pages) {
sg_cnt += calc_pages_for(dpos, dlen);
if (need_padding(dlen))
sg_cnt++;
} else {
ceph_msg_data_cursor_init(&cursor, msg, dlen);
sg_cnt += calc_sg_cnt_cursor(&cursor);
}
}
ret = sg_alloc_table(sgt, sg_cnt, GFP_NOIO);
if (ret)
return ret;
cur_sg = sgt->sgl;
if (front_len(msg))
init_sgs(&cur_sg, msg->front.iov_base, front_len(msg),
front_pad);
if (middle_len(msg))
init_sgs(&cur_sg, msg->middle->vec.iov_base, middle_len(msg),
middle_pad);
if (dlen) {
if (pages) {
init_sgs_pages(&cur_sg, pages, dpos, dlen, data_pad);
} else {
ceph_msg_data_cursor_init(&cursor, msg, dlen);
init_sgs_cursor(&cur_sg, &cursor, data_pad);
}
}
WARN_ON(!sg_is_last(cur_sg));
sg_set_buf(cur_sg, epilogue,
CEPH_GCM_BLOCK_LEN + (add_tag ? CEPH_GCM_TAG_LEN : 0));
return 0;
}
static int decrypt_preamble(struct ceph_connection *con)
{
struct scatterlist sg;
sg_init_one(&sg, con->v2.in_buf, CEPH_PREAMBLE_SECURE_LEN);
return gcm_crypt(con, false, &sg, &sg, CEPH_PREAMBLE_SECURE_LEN);
}
static int decrypt_control_remainder(struct ceph_connection *con)
{
int ctrl_len = con->v2.in_desc.fd_lens[0];
int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
int pt_len = padding_len(rem_len) + CEPH_GCM_TAG_LEN;
struct scatterlist sgs[2];
WARN_ON(con->v2.in_kvecs[0].iov_len != rem_len);
WARN_ON(con->v2.in_kvecs[1].iov_len != pt_len);
sg_init_table(sgs, 2);
sg_set_buf(&sgs[0], con->v2.in_kvecs[0].iov_base, rem_len);
sg_set_buf(&sgs[1], con->v2.in_buf, pt_len);
return gcm_crypt(con, false, sgs, sgs,
padded_len(rem_len) + CEPH_GCM_TAG_LEN);
}
/* Process sparse read data that lives in a buffer */
static int process_v2_sparse_read(struct ceph_connection *con,
struct page **pages, int spos)
{
struct ceph_msg_data_cursor *cursor = &con->v2.in_cursor;
int ret;
for (;;) {
char *buf = NULL;
ret = con->ops->sparse_read(con, cursor, &buf);
if (ret <= 0)
return ret;
dout("%s: sparse_read return %x buf %p\n", __func__, ret, buf);
do {
int idx = spos >> PAGE_SHIFT;
int soff = offset_in_page(spos);
struct page *spage = con->v2.in_enc_pages[idx];
int len = min_t(int, ret, PAGE_SIZE - soff);
if (buf) {
memcpy_from_page(buf, spage, soff, len);
buf += len;
} else {
struct bio_vec bv;
get_bvec_at(cursor, &bv);
len = min_t(int, len, bv.bv_len);
memcpy_page(bv.bv_page, bv.bv_offset,
spage, soff, len);
ceph_msg_data_advance(cursor, len);
}
spos += len;
ret -= len;
} while (ret);
}
}
static int decrypt_tail(struct ceph_connection *con)
{
struct sg_table enc_sgt = {};
struct sg_table sgt = {};
struct page **pages = NULL;
bool sparse = con->in_msg->sparse_read;
int dpos = 0;
int tail_len;
int ret;
tail_len = tail_onwire_len(con->in_msg, true);
ret = sg_alloc_table_from_pages(&enc_sgt, con->v2.in_enc_pages,
con->v2.in_enc_page_cnt, 0, tail_len,
GFP_NOIO);
if (ret)
goto out;
if (sparse) {
dpos = padded_len(front_len(con->in_msg) + padded_len(middle_len(con->in_msg)));
pages = con->v2.in_enc_pages;
}
ret = setup_message_sgs(&sgt, con->in_msg, FRONT_PAD(con->v2.in_buf),
MIDDLE_PAD(con->v2.in_buf), DATA_PAD(con->v2.in_buf),
con->v2.in_buf, pages, dpos, true);
if (ret)
goto out;
dout("%s con %p msg %p enc_page_cnt %d sg_cnt %d\n", __func__, con,
con->in_msg, con->v2.in_enc_page_cnt, sgt.orig_nents);
ret = gcm_crypt(con, false, enc_sgt.sgl, sgt.sgl, tail_len);
if (ret)
goto out;
if (sparse && data_len(con->in_msg)) {
ret = process_v2_sparse_read(con, con->v2.in_enc_pages, dpos);
if (ret)
goto out;
}
WARN_ON(!con->v2.in_enc_page_cnt);
ceph_release_page_vector(con->v2.in_enc_pages,
con->v2.in_enc_page_cnt);
con->v2.in_enc_pages = NULL;
con->v2.in_enc_page_cnt = 0;
out:
sg_free_table(&sgt);
sg_free_table(&enc_sgt);
return ret;
}
static int prepare_banner(struct ceph_connection *con)
{
int buf_len = CEPH_BANNER_V2_LEN + 2 + 8 + 8;
void *buf, *p;
buf = alloc_conn_buf(con, buf_len);
if (!buf)
return -ENOMEM;
p = buf;
ceph_encode_copy(&p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN);
ceph_encode_16(&p, sizeof(u64) + sizeof(u64));
ceph_encode_64(&p, CEPH_MSGR2_SUPPORTED_FEATURES);
ceph_encode_64(&p, CEPH_MSGR2_REQUIRED_FEATURES);
WARN_ON(p != buf + buf_len);
add_out_kvec(con, buf, buf_len);
add_out_sign_kvec(con, buf, buf_len);
ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
return 0;
}
/*
* base:
* preamble
* control body (ctrl_len bytes)
* space for control crc
*
* extdata (optional):
* control body (extdata_len bytes)
*
* Compute control crc and gather base and extdata into:
*
* preamble
* control body (ctrl_len + extdata_len bytes)
* control crc
*
* Preamble should already be encoded at the start of base.
*/
static void prepare_head_plain(struct ceph_connection *con, void *base,
int ctrl_len, void *extdata, int extdata_len,
bool to_be_signed)
{
int base_len = CEPH_PREAMBLE_LEN + ctrl_len + CEPH_CRC_LEN;
void *crcp = base + base_len - CEPH_CRC_LEN;
u32 crc;
crc = crc32c(-1, CTRL_BODY(base), ctrl_len);
if (extdata_len)
crc = crc32c(crc, extdata, extdata_len);
put_unaligned_le32(crc, crcp);
if (!extdata_len) {
add_out_kvec(con, base, base_len);
if (to_be_signed)
add_out_sign_kvec(con, base, base_len);
return;
}
add_out_kvec(con, base, crcp - base);
add_out_kvec(con, extdata, extdata_len);
add_out_kvec(con, crcp, CEPH_CRC_LEN);
if (to_be_signed) {
add_out_sign_kvec(con, base, crcp - base);
add_out_sign_kvec(con, extdata, extdata_len);
add_out_sign_kvec(con, crcp, CEPH_CRC_LEN);
}
}
static int prepare_head_secure_small(struct ceph_connection *con,
void *base, int ctrl_len)
{
struct scatterlist sg;
int ret;
/* inline buffer padding? */
if (ctrl_len < CEPH_PREAMBLE_INLINE_LEN)
memset(CTRL_BODY(base) + ctrl_len, 0,
CEPH_PREAMBLE_INLINE_LEN - ctrl_len);
sg_init_one(&sg, base, CEPH_PREAMBLE_SECURE_LEN);
ret = gcm_crypt(con, true, &sg, &sg,
CEPH_PREAMBLE_SECURE_LEN - CEPH_GCM_TAG_LEN);
if (ret)
return ret;
add_out_kvec(con, base, CEPH_PREAMBLE_SECURE_LEN);
return 0;
}
/*
* base:
* preamble
* control body (ctrl_len bytes)
* space for padding, if needed
* space for control remainder auth tag
* space for preamble auth tag
*
* Encrypt preamble and the inline portion, then encrypt the remainder
* and gather into:
*
* preamble
* control body (48 bytes)
* preamble auth tag
* control body (ctrl_len - 48 bytes)
* zero padding, if needed
* control remainder auth tag
*
* Preamble should already be encoded at the start of base.
*/
static int prepare_head_secure_big(struct ceph_connection *con,
void *base, int ctrl_len)
{
int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
void *rem = CTRL_BODY(base) + CEPH_PREAMBLE_INLINE_LEN;
void *rem_tag = rem + padded_len(rem_len);
void *pmbl_tag = rem_tag + CEPH_GCM_TAG_LEN;
struct scatterlist sgs[2];
int ret;
sg_init_table(sgs, 2);
sg_set_buf(&sgs[0], base, rem - base);
sg_set_buf(&sgs[1], pmbl_tag, CEPH_GCM_TAG_LEN);
ret = gcm_crypt(con, true, sgs, sgs, rem - base);
if (ret)
return ret;
/* control remainder padding? */
if (need_padding(rem_len))
memset(rem + rem_len, 0, padding_len(rem_len));
sg_init_one(&sgs[0], rem, pmbl_tag - rem);
ret = gcm_crypt(con, true, sgs, sgs, rem_tag - rem);
if (ret)
return ret;
add_out_kvec(con, base, rem - base);
add_out_kvec(con, pmbl_tag, CEPH_GCM_TAG_LEN);
add_out_kvec(con, rem, pmbl_tag - rem);
return 0;
}
static int __prepare_control(struct ceph_connection *con, int tag,
void *base, int ctrl_len, void *extdata,
int extdata_len, bool to_be_signed)
{
int total_len = ctrl_len + extdata_len;
struct ceph_frame_desc desc;
int ret;
dout("%s con %p tag %d len %d (%d+%d)\n", __func__, con, tag,
total_len, ctrl_len, extdata_len);
/* extdata may be vmalloc'ed but not base */
if (WARN_ON(is_vmalloc_addr(base) || !ctrl_len))
return -EINVAL;
init_frame_desc(&desc, tag, &total_len, 1);
encode_preamble(&desc, base);
if (con_secure(con)) {
if (WARN_ON(extdata_len || to_be_signed))
return -EINVAL;
if (ctrl_len <= CEPH_PREAMBLE_INLINE_LEN)
/* fully inlined, inline buffer may need padding */
ret = prepare_head_secure_small(con, base, ctrl_len);
else
/* partially inlined, inline buffer is full */
ret = prepare_head_secure_big(con, base, ctrl_len);
if (ret)
return ret;
} else {
prepare_head_plain(con, base, ctrl_len, extdata, extdata_len,
to_be_signed);
}
ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
return 0;
}
static int prepare_control(struct ceph_connection *con, int tag,
void *base, int ctrl_len)
{
return __prepare_control(con, tag, base, ctrl_len, NULL, 0, false);
}
static int prepare_hello(struct ceph_connection *con)
{
void *buf, *p;
int ctrl_len;
ctrl_len = 1 + ceph_entity_addr_encoding_len(&con->peer_addr);
buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
if (!buf)
return -ENOMEM;
p = CTRL_BODY(buf);
ceph_encode_8(&p, CEPH_ENTITY_TYPE_CLIENT);
ceph_encode_entity_addr(&p, &con->peer_addr);
WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
return __prepare_control(con, FRAME_TAG_HELLO, buf, ctrl_len,
NULL, 0, true);
}
/* so that head_onwire_len(AUTH_BUF_LEN, false) is 512 */
#define AUTH_BUF_LEN (512 - CEPH_CRC_LEN - CEPH_PREAMBLE_PLAIN_LEN)
static int prepare_auth_request(struct ceph_connection *con)
{
void *authorizer, *authorizer_copy;
int ctrl_len, authorizer_len;
void *buf;
int ret;
ctrl_len = AUTH_BUF_LEN;
buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
if (!buf)
return -ENOMEM;
mutex_unlock(&con->mutex);
ret = con->ops->get_auth_request(con, CTRL_BODY(buf), &ctrl_len,
&authorizer, &authorizer_len);
mutex_lock(&con->mutex);
if (con->state != CEPH_CON_S_V2_HELLO) {
dout("%s con %p state changed to %d\n", __func__, con,
con->state);
return -EAGAIN;
}
dout("%s con %p get_auth_request ret %d\n", __func__, con, ret);
if (ret)
return ret;
authorizer_copy = alloc_conn_buf(con, authorizer_len);
if (!authorizer_copy)
return -ENOMEM;
memcpy(authorizer_copy, authorizer, authorizer_len);
return __prepare_control(con, FRAME_TAG_AUTH_REQUEST, buf, ctrl_len,
authorizer_copy, authorizer_len, true);
}
static int prepare_auth_request_more(struct ceph_connection *con,
void *reply, int reply_len)
{
int ctrl_len, authorizer_len;
void *authorizer;
void *buf;
int ret;
ctrl_len = AUTH_BUF_LEN;
buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
if (!buf)
return -ENOMEM;
mutex_unlock(&con->mutex);
ret = con->ops->handle_auth_reply_more(con, reply, reply_len,
CTRL_BODY(buf), &ctrl_len,
&authorizer, &authorizer_len);
mutex_lock(&con->mutex);
if (con->state != CEPH_CON_S_V2_AUTH) {
dout("%s con %p state changed to %d\n", __func__, con,
con->state);
return -EAGAIN;
}
dout("%s con %p handle_auth_reply_more ret %d\n", __func__, con, ret);
if (ret)
return ret;
return __prepare_control(con, FRAME_TAG_AUTH_REQUEST_MORE, buf,
ctrl_len, authorizer, authorizer_len, true);
}
static int prepare_auth_signature(struct ceph_connection *con)
{
void *buf;
int ret;
buf = alloc_conn_buf(con, head_onwire_len(SHA256_DIGEST_SIZE,
con_secure(con)));
if (!buf)
return -ENOMEM;
ret = hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt,
CTRL_BODY(buf));
if (ret)
return ret;
return prepare_control(con, FRAME_TAG_AUTH_SIGNATURE, buf,
SHA256_DIGEST_SIZE);
}
static int prepare_client_ident(struct ceph_connection *con)
{
struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
struct ceph_client *client = from_msgr(con->msgr);
u64 global_id = ceph_client_gid(client);
void *buf, *p;
int ctrl_len;
WARN_ON(con->v2.server_cookie);
WARN_ON(con->v2.connect_seq);
WARN_ON(con->v2.peer_global_seq);
if (!con->v2.client_cookie) {
do {
get_random_bytes(&con->v2.client_cookie,
sizeof(con->v2.client_cookie));
} while (!con->v2.client_cookie);
dout("%s con %p generated cookie 0x%llx\n", __func__, con,
con->v2.client_cookie);
} else {
dout("%s con %p cookie already set 0x%llx\n", __func__, con,
con->v2.client_cookie);
}
dout("%s con %p my_addr %s/%u peer_addr %s/%u global_id %llu global_seq %llu features 0x%llx required_features 0x%llx cookie 0x%llx\n",
__func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce),
ceph_pr_addr(&con->peer_addr), le32_to_cpu(con->peer_addr.nonce),
global_id, con->v2.global_seq, client->supported_features,
client->required_features, con->v2.client_cookie);
ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) +
ceph_entity_addr_encoding_len(&con->peer_addr) + 6 * 8;
buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con)));
if (!buf)
return -ENOMEM;
p = CTRL_BODY(buf);
ceph_encode_8(&p, 2); /* addrvec marker */
ceph_encode_32(&p, 1); /* addr_cnt */
ceph_encode_entity_addr(&p, my_addr);
ceph_encode_entity_addr(&p, &con->peer_addr);
ceph_encode_64(&p, global_id);
ceph_encode_64(&p, con->v2.global_seq);
ceph_encode_64(&p, client->supported_features);
ceph_encode_64(&p, client->required_features);
ceph_encode_64(&p, 0); /* flags */
ceph_encode_64(&p, con->v2.client_cookie);
WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
return prepare_control(con, FRAME_TAG_CLIENT_IDENT, buf, ctrl_len);
}
static int prepare_session_reconnect(struct ceph_connection *con)
{
struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
void *buf, *p;
int ctrl_len;
WARN_ON(!con->v2.client_cookie);
WARN_ON(!con->v2.server_cookie);
WARN_ON(!con->v2.connect_seq);
WARN_ON(!con->v2.peer_global_seq);
dout("%s con %p my_addr %s/%u client_cookie 0x%llx server_cookie 0x%llx global_seq %llu connect_seq %llu in_seq %llu\n",
__func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce),
con->v2.client_cookie, con->v2.server_cookie, con->v2.global_seq,
con->v2.connect_seq, con->in_seq);
ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) + 5 * 8;
buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con)));
if (!buf)
return -ENOMEM;
p = CTRL_BODY(buf);
ceph_encode_8(&p, 2); /* entity_addrvec_t marker */
ceph_encode_32(&p, 1); /* my_addrs len */
ceph_encode_entity_addr(&p, my_addr);
ceph_encode_64(&p, con->v2.client_cookie);
ceph_encode_64(&p, con->v2.server_cookie);
ceph_encode_64(&p, con->v2.global_seq);
ceph_encode_64(&p, con->v2.connect_seq);
ceph_encode_64(&p, con->in_seq);
WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
return prepare_control(con, FRAME_TAG_SESSION_RECONNECT, buf, ctrl_len);
}
static int prepare_keepalive2(struct ceph_connection *con)
{
struct ceph_timespec *ts = CTRL_BODY(con->v2.out_buf);
struct timespec64 now;
ktime_get_real_ts64(&now);
dout("%s con %p timestamp %lld.%09ld\n", __func__, con, now.tv_sec,
now.tv_nsec);
ceph_encode_timespec64(ts, &now);
reset_out_kvecs(con);
return prepare_control(con, FRAME_TAG_KEEPALIVE2, con->v2.out_buf,
sizeof(struct ceph_timespec));
}
static int prepare_ack(struct ceph_connection *con)
{
void *p;
dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con,
con->in_seq_acked, con->in_seq);
con->in_seq_acked = con->in_seq;
p = CTRL_BODY(con->v2.out_buf);
ceph_encode_64(&p, con->in_seq_acked);
reset_out_kvecs(con);
return prepare_control(con, FRAME_TAG_ACK, con->v2.out_buf, 8);
}
static void prepare_epilogue_plain(struct ceph_connection *con, bool aborted)
{
dout("%s con %p msg %p aborted %d crcs %u %u %u\n", __func__, con,
con->out_msg, aborted, con->v2.out_epil.front_crc,
con->v2.out_epil.middle_crc, con->v2.out_epil.data_crc);
encode_epilogue_plain(con, aborted);
add_out_kvec(con, &con->v2.out_epil, CEPH_EPILOGUE_PLAIN_LEN);
}
/*
* For "used" empty segments, crc is -1. For unused (trailing)
* segments, crc is 0.
*/
static void prepare_message_plain(struct ceph_connection *con)
{
struct ceph_msg *msg = con->out_msg;
prepare_head_plain(con, con->v2.out_buf,
sizeof(struct ceph_msg_header2), NULL, 0, false);
if (!front_len(msg) && !middle_len(msg)) {
if (!data_len(msg)) {
/*
* Empty message: once the head is written,
* we are done -- there is no epilogue.
*/
con->v2.out_state = OUT_S_FINISH_MESSAGE;
return;
}
con->v2.out_epil.front_crc = -1;
con->v2.out_epil.middle_crc = -1;
con->v2.out_state = OUT_S_QUEUE_DATA;
return;
}
if (front_len(msg)) {
con->v2.out_epil.front_crc = crc32c(-1, msg->front.iov_base,
front_len(msg));
add_out_kvec(con, msg->front.iov_base, front_len(msg));
} else {
/* middle (at least) is there, checked above */
con->v2.out_epil.front_crc = -1;
}
if (middle_len(msg)) {
con->v2.out_epil.middle_crc =
crc32c(-1, msg->middle->vec.iov_base, middle_len(msg));
add_out_kvec(con, msg->middle->vec.iov_base, middle_len(msg));
} else {
con->v2.out_epil.middle_crc = data_len(msg) ? -1 : 0;
}
if (data_len(msg)) {
con->v2.out_state = OUT_S_QUEUE_DATA;
} else {
con->v2.out_epil.data_crc = 0;
prepare_epilogue_plain(con, false);
con->v2.out_state = OUT_S_FINISH_MESSAGE;
}
}
/*
* Unfortunately the kernel crypto API doesn't support streaming
* (piecewise) operation for AEAD algorithms, so we can't get away
* with a fixed size buffer and a couple sgs. Instead, we have to
* allocate pages for the entire tail of the message (currently up
* to ~32M) and two sgs arrays (up to ~256K each)...
*/
static int prepare_message_secure(struct ceph_connection *con)
{
void *zerop = page_address(ceph_zero_page);
struct sg_table enc_sgt = {};
struct sg_table sgt = {};
struct page **enc_pages;
int enc_page_cnt;
int tail_len;
int ret;
ret = prepare_head_secure_small(con, con->v2.out_buf,
sizeof(struct ceph_msg_header2));
if (ret)
return ret;
tail_len = tail_onwire_len(con->out_msg, true);
if (!tail_len) {
/*
* Empty message: once the head is written,
* we are done -- there is no epilogue.
*/
con->v2.out_state = OUT_S_FINISH_MESSAGE;
return 0;
}
encode_epilogue_secure(con, false);
ret = setup_message_sgs(&sgt, con->out_msg, zerop, zerop, zerop,
&con->v2.out_epil, NULL, 0, false);
if (ret)
goto out;
enc_page_cnt = calc_pages_for(0, tail_len);
enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO);
if (IS_ERR(enc_pages)) {
ret = PTR_ERR(enc_pages);
goto out;
}
WARN_ON(con->v2.out_enc_pages || con->v2.out_enc_page_cnt);
con->v2.out_enc_pages = enc_pages;
con->v2.out_enc_page_cnt = enc_page_cnt;
con->v2.out_enc_resid = tail_len;
con->v2.out_enc_i = 0;
ret = sg_alloc_table_from_pages(&enc_sgt, enc_pages, enc_page_cnt,
0, tail_len, GFP_NOIO);
if (ret)
goto out;
ret = gcm_crypt(con, true, sgt.sgl, enc_sgt.sgl,
tail_len - CEPH_GCM_TAG_LEN);
if (ret)
goto out;
dout("%s con %p msg %p sg_cnt %d enc_page_cnt %d\n", __func__, con,
con->out_msg, sgt.orig_nents, enc_page_cnt);
con->v2.out_state = OUT_S_QUEUE_ENC_PAGE;
out:
sg_free_table(&sgt);
sg_free_table(&enc_sgt);
return ret;
}
static int prepare_message(struct ceph_connection *con)
{
int lens[] = {
sizeof(struct ceph_msg_header2),
front_len(con->out_msg),
middle_len(con->out_msg),
data_len(con->out_msg)
};
struct ceph_frame_desc desc;
int ret;
dout("%s con %p msg %p logical %d+%d+%d+%d\n", __func__, con,
con->out_msg, lens[0], lens[1], lens[2], lens[3]);
if (con->in_seq > con->in_seq_acked) {
dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con,
con->in_seq_acked, con->in_seq);
con->in_seq_acked = con->in_seq;
}
reset_out_kvecs(con);
init_frame_desc(&desc, FRAME_TAG_MESSAGE, lens, 4);
encode_preamble(&desc, con->v2.out_buf);
fill_header2(CTRL_BODY(con->v2.out_buf), &con->out_msg->hdr,
con->in_seq_acked);
if (con_secure(con)) {
ret = prepare_message_secure(con);
if (ret)
return ret;
} else {
prepare_message_plain(con);
}
ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
return 0;
}
static int prepare_read_banner_prefix(struct ceph_connection *con)
{
void *buf;
buf = alloc_conn_buf(con, CEPH_BANNER_V2_PREFIX_LEN);
if (!buf)
return -ENOMEM;
reset_in_kvecs(con);
add_in_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN);
add_in_sign_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN);
con->state = CEPH_CON_S_V2_BANNER_PREFIX;
return 0;
}
static int prepare_read_banner_payload(struct ceph_connection *con,
int payload_len)
{
void *buf;
buf = alloc_conn_buf(con, payload_len);
if (!buf)
return -ENOMEM;
reset_in_kvecs(con);
add_in_kvec(con, buf, payload_len);
add_in_sign_kvec(con, buf, payload_len);
con->state = CEPH_CON_S_V2_BANNER_PAYLOAD;
return 0;
}
static void prepare_read_preamble(struct ceph_connection *con)
{
reset_in_kvecs(con);
add_in_kvec(con, con->v2.in_buf,
con_secure(con) ? CEPH_PREAMBLE_SECURE_LEN :
CEPH_PREAMBLE_PLAIN_LEN);
con->v2.in_state = IN_S_HANDLE_PREAMBLE;
}
static int prepare_read_control(struct ceph_connection *con)
{
int ctrl_len = con->v2.in_desc.fd_lens[0];
int head_len;
void *buf;
reset_in_kvecs(con);
if (con->state == CEPH_CON_S_V2_HELLO ||
con->state == CEPH_CON_S_V2_AUTH) {
head_len = head_onwire_len(ctrl_len, false);
buf = alloc_conn_buf(con, head_len);
if (!buf)
return -ENOMEM;
/* preserve preamble */
memcpy(buf, con->v2.in_buf, CEPH_PREAMBLE_LEN);
add_in_kvec(con, CTRL_BODY(buf), ctrl_len);
add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);
add_in_sign_kvec(con, buf, head_len);
} else {
if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
buf = alloc_conn_buf(con, ctrl_len);
if (!buf)
return -ENOMEM;
add_in_kvec(con, buf, ctrl_len);
} else {
add_in_kvec(con, CTRL_BODY(con->v2.in_buf), ctrl_len);
}
add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);
}
con->v2.in_state = IN_S_HANDLE_CONTROL;
return 0;
}
static int prepare_read_control_remainder(struct ceph_connection *con)
{
int ctrl_len = con->v2.in_desc.fd_lens[0];
int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
void *buf;
buf = alloc_conn_buf(con, ctrl_len);
if (!buf)
return -ENOMEM;
memcpy(buf, CTRL_BODY(con->v2.in_buf), CEPH_PREAMBLE_INLINE_LEN);
reset_in_kvecs(con);
add_in_kvec(con, buf + CEPH_PREAMBLE_INLINE_LEN, rem_len);
add_in_kvec(con, con->v2.in_buf,
padding_len(rem_len) + CEPH_GCM_TAG_LEN);
con->v2.in_state = IN_S_HANDLE_CONTROL_REMAINDER;
return 0;
}
static int prepare_read_data(struct ceph_connection *con)
{
struct bio_vec bv;
con->in_data_crc = -1;
ceph_msg_data_cursor_init(&con->v2.in_cursor, con->in_msg,
data_len(con->in_msg));
get_bvec_at(&con->v2.in_cursor, &bv);
if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
if (unlikely(!con->bounce_page)) {
con->bounce_page = alloc_page(GFP_NOIO);
if (!con->bounce_page) {
pr_err("failed to allocate bounce page\n");
return -ENOMEM;
}
}
bv.bv_page = con->bounce_page;
bv.bv_offset = 0;
}
set_in_bvec(con, &bv);
con->v2.in_state = IN_S_PREPARE_READ_DATA_CONT;
return 0;
}
static void prepare_read_data_cont(struct ceph_connection *con)
{
struct bio_vec bv;
if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
con->in_data_crc = crc32c(con->in_data_crc,
page_address(con->bounce_page),
con->v2.in_bvec.bv_len);
get_bvec_at(&con->v2.in_cursor, &bv);
memcpy_to_page(bv.bv_page, bv.bv_offset,
page_address(con->bounce_page),
con->v2.in_bvec.bv_len);
} else {
con->in_data_crc = ceph_crc32c_page(con->in_data_crc,
con->v2.in_bvec.bv_page,
con->v2.in_bvec.bv_offset,
con->v2.in_bvec.bv_len);
}
ceph_msg_data_advance(&con->v2.in_cursor, con->v2.in_bvec.bv_len);
if (con->v2.in_cursor.total_resid) {
get_bvec_at(&con->v2.in_cursor, &bv);
if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
bv.bv_page = con->bounce_page;
bv.bv_offset = 0;
}
set_in_bvec(con, &bv);
WARN_ON(con->v2.in_state != IN_S_PREPARE_READ_DATA_CONT);
return;
}
/*
* We've read all data. Prepare to read epilogue.
*/
reset_in_kvecs(con);
add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN);
con->v2.in_state = IN_S_HANDLE_EPILOGUE;
}
static int prepare_sparse_read_cont(struct ceph_connection *con)
{
int ret;
struct bio_vec bv;
char *buf = NULL;
struct ceph_msg_data_cursor *cursor = &con->v2.in_cursor;
WARN_ON(con->v2.in_state != IN_S_PREPARE_SPARSE_DATA_CONT);
if (iov_iter_is_bvec(&con->v2.in_iter)) {
if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
con->in_data_crc = crc32c(con->in_data_crc,
page_address(con->bounce_page),
con->v2.in_bvec.bv_len);
get_bvec_at(cursor, &bv);
memcpy_to_page(bv.bv_page, bv.bv_offset,
page_address(con->bounce_page),
con->v2.in_bvec.bv_len);
} else {
con->in_data_crc = ceph_crc32c_page(con->in_data_crc,
con->v2.in_bvec.bv_page,
con->v2.in_bvec.bv_offset,
con->v2.in_bvec.bv_len);
}
ceph_msg_data_advance(cursor, con->v2.in_bvec.bv_len);
cursor->sr_resid -= con->v2.in_bvec.bv_len;
dout("%s: advance by 0x%x sr_resid 0x%x\n", __func__,
con->v2.in_bvec.bv_len, cursor->sr_resid);
WARN_ON_ONCE(cursor->sr_resid > cursor->total_resid);
if (cursor->sr_resid) {
get_bvec_at(cursor, &bv);
if (bv.bv_len > cursor->sr_resid)
bv.bv_len = cursor->sr_resid;
if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
bv.bv_page = con->bounce_page;
bv.bv_offset = 0;
}
set_in_bvec(con, &bv);
con->v2.data_len_remain -= bv.bv_len;
return 0;
}
} else if (iov_iter_is_kvec(&con->v2.in_iter)) {
/* On first call, we have no kvec so don't compute crc */
if (con->v2.in_kvec_cnt) {
WARN_ON_ONCE(con->v2.in_kvec_cnt > 1);
con->in_data_crc = crc32c(con->in_data_crc,
con->v2.in_kvecs[0].iov_base,
con->v2.in_kvecs[0].iov_len);
}
} else {
return -EIO;
}
/* get next extent */
ret = con->ops->sparse_read(con, cursor, &buf);
if (ret <= 0) {
if (ret < 0)
return ret;
reset_in_kvecs(con);
add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN);
con->v2.in_state = IN_S_HANDLE_EPILOGUE;
return 0;
}
if (buf) {
/* receive into buffer */
reset_in_kvecs(con);
add_in_kvec(con, buf, ret);
con->v2.data_len_remain -= ret;
return 0;
}
if (ret > cursor->total_resid) {
pr_warn("%s: ret 0x%x total_resid 0x%zx resid 0x%zx\n",
__func__, ret, cursor->total_resid, cursor->resid);
return -EIO;
}
get_bvec_at(cursor, &bv);
if (bv.bv_len > cursor->sr_resid)
bv.bv_len = cursor->sr_resid;
if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
if (unlikely(!con->bounce_page)) {
con->bounce_page = alloc_page(GFP_NOIO);
if (!con->bounce_page) {
pr_err("failed to allocate bounce page\n");
return -ENOMEM;
}
}
bv.bv_page = con->bounce_page;
bv.bv_offset = 0;
}
set_in_bvec(con, &bv);
con->v2.data_len_remain -= ret;
return ret;
}
static int prepare_sparse_read_data(struct ceph_connection *con)
{
struct ceph_msg *msg = con->in_msg;
dout("%s: starting sparse read\n", __func__);
if (WARN_ON_ONCE(!con->ops->sparse_read))
return -EOPNOTSUPP;
if (!con_secure(con))
con->in_data_crc = -1;
reset_in_kvecs(con);
con->v2.in_state = IN_S_PREPARE_SPARSE_DATA_CONT;
con->v2.data_len_remain = data_len(msg);
return prepare_sparse_read_cont(con);
}
static int prepare_read_tail_plain(struct ceph_connection *con)
{
struct ceph_msg *msg = con->in_msg;
if (!front_len(msg) && !middle_len(msg)) {
WARN_ON(!data_len(msg));
return prepare_read_data(con);
}
reset_in_kvecs(con);
if (front_len(msg)) {
add_in_kvec(con, msg->front.iov_base, front_len(msg));
WARN_ON(msg->front.iov_len != front_len(msg));
}
if (middle_len(msg)) {
add_in_kvec(con, msg->middle->vec.iov_base, middle_len(msg));
WARN_ON(msg->middle->vec.iov_len != middle_len(msg));
}
if (data_len(msg)) {
if (msg->sparse_read)
con->v2.in_state = IN_S_PREPARE_SPARSE_DATA;
else
con->v2.in_state = IN_S_PREPARE_READ_DATA;
} else {
add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN);
con->v2.in_state = IN_S_HANDLE_EPILOGUE;
}
return 0;
}
static void prepare_read_enc_page(struct ceph_connection *con)
{
struct bio_vec bv;
dout("%s con %p i %d resid %d\n", __func__, con, con->v2.in_enc_i,
con->v2.in_enc_resid);
WARN_ON(!con->v2.in_enc_resid);
bvec_set_page(&bv, con->v2.in_enc_pages[con->v2.in_enc_i],
min(con->v2.in_enc_resid, (int)PAGE_SIZE), 0);
set_in_bvec(con, &bv);
con->v2.in_enc_i++;
con->v2.in_enc_resid -= bv.bv_len;
if (con->v2.in_enc_resid) {
con->v2.in_state = IN_S_PREPARE_READ_ENC_PAGE;
return;
}
/*
* We are set to read the last piece of ciphertext (ending
* with epilogue) + auth tag.
*/
WARN_ON(con->v2.in_enc_i != con->v2.in_enc_page_cnt);
con->v2.in_state = IN_S_HANDLE_EPILOGUE;
}
static int prepare_read_tail_secure(struct ceph_connection *con)
{
struct page **enc_pages;
int enc_page_cnt;
int tail_len;
tail_len = tail_onwire_len(con->in_msg, true);
WARN_ON(!tail_len);
enc_page_cnt = calc_pages_for(0, tail_len);
enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO);
if (IS_ERR(enc_pages))
return PTR_ERR(enc_pages);
WARN_ON(con->v2.in_enc_pages || con->v2.in_enc_page_cnt);
con->v2.in_enc_pages = enc_pages;
con->v2.in_enc_page_cnt = enc_page_cnt;
con->v2.in_enc_resid = tail_len;
con->v2.in_enc_i = 0;
prepare_read_enc_page(con);
return 0;
}
static void __finish_skip(struct ceph_connection *con)
{
con->in_seq++;
prepare_read_preamble(con);
}
static void prepare_skip_message(struct ceph_connection *con)
{
struct ceph_frame_desc *desc = &con->v2.in_desc;
int tail_len;
dout("%s con %p %d+%d+%d\n", __func__, con, desc->fd_lens[1],
desc->fd_lens[2], desc->fd_lens[3]);
tail_len = __tail_onwire_len(desc->fd_lens[1], desc->fd_lens[2],
desc->fd_lens[3], con_secure(con));
if (!tail_len) {
__finish_skip(con);
} else {
set_in_skip(con, tail_len);
con->v2.in_state = IN_S_FINISH_SKIP;
}
}
static int process_banner_prefix(struct ceph_connection *con)
{
int payload_len;
void *p;
WARN_ON(con->v2.in_kvecs[0].iov_len != CEPH_BANNER_V2_PREFIX_LEN);
p = con->v2.in_kvecs[0].iov_base;
if (memcmp(p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN)) {
if (!memcmp(p, CEPH_BANNER, CEPH_BANNER_LEN))
con->error_msg = "server is speaking msgr1 protocol";
else
con->error_msg = "protocol error, bad banner";
return -EINVAL;
}
p += CEPH_BANNER_V2_LEN;
payload_len = ceph_decode_16(&p);
dout("%s con %p payload_len %d\n", __func__, con, payload_len);
return prepare_read_banner_payload(con, payload_len);
}
static int process_banner_payload(struct ceph_connection *con)
{
void *end = con->v2.in_kvecs[0].iov_base + con->v2.in_kvecs[0].iov_len;
u64 feat = CEPH_MSGR2_SUPPORTED_FEATURES;
u64 req_feat = CEPH_MSGR2_REQUIRED_FEATURES;
u64 server_feat, server_req_feat;
void *p;
int ret;
p = con->v2.in_kvecs[0].iov_base;
ceph_decode_64_safe(&p, end, server_feat, bad);
ceph_decode_64_safe(&p, end, server_req_feat, bad);
dout("%s con %p server_feat 0x%llx server_req_feat 0x%llx\n",
__func__, con, server_feat, server_req_feat);
if (req_feat & ~server_feat) {
pr_err("msgr2 feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n",
server_feat, req_feat & ~server_feat);
con->error_msg = "missing required protocol features";
return -EINVAL;
}
if (server_req_feat & ~feat) {
pr_err("msgr2 feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n",
feat, server_req_feat & ~feat);
con->error_msg = "missing required protocol features";
return -EINVAL;
}
/* no reset_out_kvecs() as our banner may still be pending */
ret = prepare_hello(con);
if (ret) {
pr_err("prepare_hello failed: %d\n", ret);
return ret;
}
con->state = CEPH_CON_S_V2_HELLO;
prepare_read_preamble(con);
return 0;
bad:
pr_err("failed to decode banner payload\n");
return -EINVAL;
}
static int process_hello(struct ceph_connection *con, void *p, void *end)
{
struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
struct ceph_entity_addr addr_for_me;
u8 entity_type;
int ret;
if (con->state != CEPH_CON_S_V2_HELLO) {
con->error_msg = "protocol error, unexpected hello";
return -EINVAL;
}
ceph_decode_8_safe(&p, end, entity_type, bad);
ret = ceph_decode_entity_addr(&p, end, &addr_for_me);
if (ret) {
pr_err("failed to decode addr_for_me: %d\n", ret);
return ret;
}
dout("%s con %p entity_type %d addr_for_me %s\n", __func__, con,
entity_type, ceph_pr_addr(&addr_for_me));
if (entity_type != con->peer_name.type) {
pr_err("bad peer type, want %d, got %d\n",
con->peer_name.type, entity_type);
con->error_msg = "wrong peer at address";
return -EINVAL;
}
/*
* Set our address to the address our first peer (i.e. monitor)
* sees that we are connecting from. If we are behind some sort
* of NAT and want to be identified by some private (not NATed)
* address, ip option should be used.
*/
if (ceph_addr_is_blank(my_addr)) {
memcpy(&my_addr->in_addr, &addr_for_me.in_addr,
sizeof(my_addr->in_addr));
ceph_addr_set_port(my_addr, 0);
dout("%s con %p set my addr %s, as seen by peer %s\n",
__func__, con, ceph_pr_addr(my_addr),
ceph_pr_addr(&con->peer_addr));
} else {
dout("%s con %p my addr already set %s\n",
__func__, con, ceph_pr_addr(my_addr));
}
WARN_ON(ceph_addr_is_blank(my_addr) || ceph_addr_port(my_addr));
WARN_ON(my_addr->type != CEPH_ENTITY_ADDR_TYPE_ANY);
WARN_ON(!my_addr->nonce);
/* no reset_out_kvecs() as our hello may still be pending */
ret = prepare_auth_request(con);
if (ret) {
if (ret != -EAGAIN)
pr_err("prepare_auth_request failed: %d\n", ret);
return ret;
}
con->state = CEPH_CON_S_V2_AUTH;
return 0;
bad:
pr_err("failed to decode hello\n");
return -EINVAL;
}
static int process_auth_bad_method(struct ceph_connection *con,
void *p, void *end)
{
int allowed_protos[8], allowed_modes[8];
int allowed_proto_cnt, allowed_mode_cnt;
int used_proto, result;
int ret;
int i;
if (con->state != CEPH_CON_S_V2_AUTH) {
con->error_msg = "protocol error, unexpected auth_bad_method";
return -EINVAL;
}
ceph_decode_32_safe(&p, end, used_proto, bad);
ceph_decode_32_safe(&p, end, result, bad);
dout("%s con %p used_proto %d result %d\n", __func__, con, used_proto,
result);
ceph_decode_32_safe(&p, end, allowed_proto_cnt, bad);
if (allowed_proto_cnt > ARRAY_SIZE(allowed_protos)) {
pr_err("allowed_protos too big %d\n", allowed_proto_cnt);
return -EINVAL;
}
for (i = 0; i < allowed_proto_cnt; i++) {
ceph_decode_32_safe(&p, end, allowed_protos[i], bad);
dout("%s con %p allowed_protos[%d] %d\n", __func__, con,
i, allowed_protos[i]);
}
ceph_decode_32_safe(&p, end, allowed_mode_cnt, bad);
if (allowed_mode_cnt > ARRAY_SIZE(allowed_modes)) {
pr_err("allowed_modes too big %d\n", allowed_mode_cnt);
return -EINVAL;
}
for (i = 0; i < allowed_mode_cnt; i++) {
ceph_decode_32_safe(&p, end, allowed_modes[i], bad);
dout("%s con %p allowed_modes[%d] %d\n", __func__, con,
i, allowed_modes[i]);
}
mutex_unlock(&con->mutex);
ret = con->ops->handle_auth_bad_method(con, used_proto, result,
allowed_protos,
allowed_proto_cnt,
allowed_modes,
allowed_mode_cnt);
mutex_lock(&con->mutex);
if (con->state != CEPH_CON_S_V2_AUTH) {
dout("%s con %p state changed to %d\n", __func__, con,
con->state);
return -EAGAIN;
}
dout("%s con %p handle_auth_bad_method ret %d\n", __func__, con, ret);
return ret;
bad:
pr_err("failed to decode auth_bad_method\n");
return -EINVAL;
}
static int process_auth_reply_more(struct ceph_connection *con,
void *p, void *end)
{
int payload_len;
int ret;
if (con->state != CEPH_CON_S_V2_AUTH) {
con->error_msg = "protocol error, unexpected auth_reply_more";
return -EINVAL;
}
ceph_decode_32_safe(&p, end, payload_len, bad);
ceph_decode_need(&p, end, payload_len, bad);
dout("%s con %p payload_len %d\n", __func__, con, payload_len);
reset_out_kvecs(con);
ret = prepare_auth_request_more(con, p, payload_len);
if (ret) {
if (ret != -EAGAIN)
pr_err("prepare_auth_request_more failed: %d\n", ret);
return ret;
}
return 0;
bad:
pr_err("failed to decode auth_reply_more\n");
return -EINVAL;
}
/*
* Align session_key and con_secret to avoid GFP_ATOMIC allocation
* inside crypto_shash_setkey() and crypto_aead_setkey() called from
* setup_crypto(). __aligned(16) isn't guaranteed to work for stack
* objects, so do it by hand.
*/
static int process_auth_done(struct ceph_connection *con, void *p, void *end)
{
u8 session_key_buf[CEPH_KEY_LEN + 16];
u8 con_secret_buf[CEPH_MAX_CON_SECRET_LEN + 16];
u8 *session_key = PTR_ALIGN(&session_key_buf[0], 16);
u8 *con_secret = PTR_ALIGN(&con_secret_buf[0], 16);
int session_key_len, con_secret_len;
int payload_len;
u64 global_id;
int ret;
if (con->state != CEPH_CON_S_V2_AUTH) {
con->error_msg = "protocol error, unexpected auth_done";
return -EINVAL;
}
ceph_decode_64_safe(&p, end, global_id, bad);
ceph_decode_32_safe(&p, end, con->v2.con_mode, bad);
ceph_decode_32_safe(&p, end, payload_len, bad);
dout("%s con %p global_id %llu con_mode %d payload_len %d\n",
__func__, con, global_id, con->v2.con_mode, payload_len);
mutex_unlock(&con->mutex);
session_key_len = 0;
con_secret_len = 0;
ret = con->ops->handle_auth_done(con, global_id, p, payload_len,
session_key, &session_key_len,
con_secret, &con_secret_len);
mutex_lock(&con->mutex);
if (con->state != CEPH_CON_S_V2_AUTH) {
dout("%s con %p state changed to %d\n", __func__, con,
con->state);
ret = -EAGAIN;
goto out;
}
dout("%s con %p handle_auth_done ret %d\n", __func__, con, ret);
if (ret)
goto out;
ret = setup_crypto(con, session_key, session_key_len, con_secret,
con_secret_len);
if (ret)
goto out;
reset_out_kvecs(con);
ret = prepare_auth_signature(con);
if (ret) {
pr_err("prepare_auth_signature failed: %d\n", ret);
goto out;
}
con->state = CEPH_CON_S_V2_AUTH_SIGNATURE;
out:
memzero_explicit(session_key_buf, sizeof(session_key_buf));
memzero_explicit(con_secret_buf, sizeof(con_secret_buf));
return ret;
bad:
pr_err("failed to decode auth_done\n");
return -EINVAL;
}
static int process_auth_signature(struct ceph_connection *con,
void *p, void *end)
{
u8 hmac[SHA256_DIGEST_SIZE];
int ret;
if (con->state != CEPH_CON_S_V2_AUTH_SIGNATURE) {
con->error_msg = "protocol error, unexpected auth_signature";
return -EINVAL;
}
ret = hmac_sha256(con, con->v2.out_sign_kvecs,
con->v2.out_sign_kvec_cnt, hmac);
if (ret)
return ret;
ceph_decode_need(&p, end, SHA256_DIGEST_SIZE, bad);
if (crypto_memneq(p, hmac, SHA256_DIGEST_SIZE)) {
con->error_msg = "integrity error, bad auth signature";
return -EBADMSG;
}
dout("%s con %p auth signature ok\n", __func__, con);
/* no reset_out_kvecs() as our auth_signature may still be pending */
if (!con->v2.server_cookie) {
ret = prepare_client_ident(con);
if (ret) {
pr_err("prepare_client_ident failed: %d\n", ret);
return ret;
}
con->state = CEPH_CON_S_V2_SESSION_CONNECT;
} else {
ret = prepare_session_reconnect(con);
if (ret) {
pr_err("prepare_session_reconnect failed: %d\n", ret);
return ret;
}
con->state = CEPH_CON_S_V2_SESSION_RECONNECT;
}
return 0;
bad:
pr_err("failed to decode auth_signature\n");
return -EINVAL;
}
static int process_server_ident(struct ceph_connection *con,
void *p, void *end)
{
struct ceph_client *client = from_msgr(con->msgr);
u64 features, required_features;
struct ceph_entity_addr addr;
u64 global_seq;
u64 global_id;
u64 cookie;
u64 flags;
int ret;
if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) {
con->error_msg = "protocol error, unexpected server_ident";
return -EINVAL;
}
ret = ceph_decode_entity_addrvec(&p, end, true, &addr);
if (ret) {
pr_err("failed to decode server addrs: %d\n", ret);
return ret;
}
ceph_decode_64_safe(&p, end, global_id, bad);
ceph_decode_64_safe(&p, end, global_seq, bad);
ceph_decode_64_safe(&p, end, features, bad);
ceph_decode_64_safe(&p, end, required_features, bad);
ceph_decode_64_safe(&p, end, flags, bad);
ceph_decode_64_safe(&p, end, cookie, bad);
dout("%s con %p addr %s/%u global_id %llu global_seq %llu features 0x%llx required_features 0x%llx flags 0x%llx cookie 0x%llx\n",
__func__, con, ceph_pr_addr(&addr), le32_to_cpu(addr.nonce),
global_id, global_seq, features, required_features, flags, cookie);
/* is this who we intended to talk to? */
if (memcmp(&addr, &con->peer_addr, sizeof(con->peer_addr))) {
pr_err("bad peer addr/nonce, want %s/%u, got %s/%u\n",
ceph_pr_addr(&con->peer_addr),
le32_to_cpu(con->peer_addr.nonce),
ceph_pr_addr(&addr), le32_to_cpu(addr.nonce));
con->error_msg = "wrong peer at address";
return -EINVAL;
}
if (client->required_features & ~features) {
pr_err("RADOS feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n",
features, client->required_features & ~features);
con->error_msg = "missing required protocol features";
return -EINVAL;
}
/*
* Both name->type and name->num are set in ceph_con_open() but
* name->num may be bogus in the initial monmap. name->type is
* verified in handle_hello().
*/
WARN_ON(!con->peer_name.type);
con->peer_name.num = cpu_to_le64(global_id);
con->v2.peer_global_seq = global_seq;
con->peer_features = features;
WARN_ON(required_features & ~client->supported_features);
con->v2.server_cookie = cookie;
if (flags & CEPH_MSG_CONNECT_LOSSY) {
ceph_con_flag_set(con, CEPH_CON_F_LOSSYTX);
WARN_ON(con->v2.server_cookie);
} else {
WARN_ON(!con->v2.server_cookie);
}
clear_in_sign_kvecs(con);
clear_out_sign_kvecs(con);
free_conn_bufs(con);
con->delay = 0; /* reset backoff memory */
con->state = CEPH_CON_S_OPEN;
con->v2.out_state = OUT_S_GET_NEXT;
return 0;
bad:
pr_err("failed to decode server_ident\n");
return -EINVAL;
}
static int process_ident_missing_features(struct ceph_connection *con,
void *p, void *end)
{
struct ceph_client *client = from_msgr(con->msgr);
u64 missing_features;
if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) {
con->error_msg = "protocol error, unexpected ident_missing_features";
return -EINVAL;
}
ceph_decode_64_safe(&p, end, missing_features, bad);
pr_err("RADOS feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n",
client->supported_features, missing_features);
con->error_msg = "missing required protocol features";
return -EINVAL;
bad:
pr_err("failed to decode ident_missing_features\n");
return -EINVAL;
}
static int process_session_reconnect_ok(struct ceph_connection *con,
void *p, void *end)
{
u64 seq;
if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
con->error_msg = "protocol error, unexpected session_reconnect_ok";
return -EINVAL;
}
ceph_decode_64_safe(&p, end, seq, bad);
dout("%s con %p seq %llu\n", __func__, con, seq);
ceph_con_discard_requeued(con, seq);
clear_in_sign_kvecs(con);
clear_out_sign_kvecs(con);
free_conn_bufs(con);
con->delay = 0; /* reset backoff memory */
con->state = CEPH_CON_S_OPEN;
con->v2.out_state = OUT_S_GET_NEXT;
return 0;
bad:
pr_err("failed to decode session_reconnect_ok\n");
return -EINVAL;
}
static int process_session_retry(struct ceph_connection *con,
void *p, void *end)
{
u64 connect_seq;
int ret;
if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
con->error_msg = "protocol error, unexpected session_retry";
return -EINVAL;
}
ceph_decode_64_safe(&p, end, connect_seq, bad);
dout("%s con %p connect_seq %llu\n", __func__, con, connect_seq);
WARN_ON(connect_seq <= con->v2.connect_seq);
con->v2.connect_seq = connect_seq + 1;
free_conn_bufs(con);
reset_out_kvecs(con);
ret = prepare_session_reconnect(con);
if (ret) {
pr_err("prepare_session_reconnect (cseq) failed: %d\n", ret);
return ret;
}
return 0;
bad:
pr_err("failed to decode session_retry\n");
return -EINVAL;
}
static int process_session_retry_global(struct ceph_connection *con,
void *p, void *end)
{
u64 global_seq;
int ret;
if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
con->error_msg = "protocol error, unexpected session_retry_global";
return -EINVAL;
}
ceph_decode_64_safe(&p, end, global_seq, bad);
dout("%s con %p global_seq %llu\n", __func__, con, global_seq);
WARN_ON(global_seq <= con->v2.global_seq);
con->v2.global_seq = ceph_get_global_seq(con->msgr, global_seq);
free_conn_bufs(con);
reset_out_kvecs(con);
ret = prepare_session_reconnect(con);
if (ret) {
pr_err("prepare_session_reconnect (gseq) failed: %d\n", ret);
return ret;
}
return 0;
bad:
pr_err("failed to decode session_retry_global\n");
return -EINVAL;
}
static int process_session_reset(struct ceph_connection *con,
void *p, void *end)
{
bool full;
int ret;
if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
con->error_msg = "protocol error, unexpected session_reset";
return -EINVAL;
}
ceph_decode_8_safe(&p, end, full, bad);
if (!full) {
con->error_msg = "protocol error, bad session_reset";
return -EINVAL;
}
pr_info("%s%lld %s session reset\n", ENTITY_NAME(con->peer_name),
ceph_pr_addr(&con->peer_addr));
ceph_con_reset_session(con);
mutex_unlock(&con->mutex);
if (con->ops->peer_reset)
con->ops->peer_reset(con);
mutex_lock(&con->mutex);
if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
dout("%s con %p state changed to %d\n", __func__, con,
con->state);
return -EAGAIN;
}
free_conn_bufs(con);
reset_out_kvecs(con);
ret = prepare_client_ident(con);
if (ret) {
pr_err("prepare_client_ident (rst) failed: %d\n", ret);
return ret;
}
con->state = CEPH_CON_S_V2_SESSION_CONNECT;
return 0;
bad:
pr_err("failed to decode session_reset\n");
return -EINVAL;
}
static int process_keepalive2_ack(struct ceph_connection *con,
void *p, void *end)
{
if (con->state != CEPH_CON_S_OPEN) {
con->error_msg = "protocol error, unexpected keepalive2_ack";
return -EINVAL;
}
ceph_decode_need(&p, end, sizeof(struct ceph_timespec), bad);
ceph_decode_timespec64(&con->last_keepalive_ack, p);
dout("%s con %p timestamp %lld.%09ld\n", __func__, con,
con->last_keepalive_ack.tv_sec, con->last_keepalive_ack.tv_nsec);
return 0;
bad:
pr_err("failed to decode keepalive2_ack\n");
return -EINVAL;
}
static int process_ack(struct ceph_connection *con, void *p, void *end)
{
u64 seq;
if (con->state != CEPH_CON_S_OPEN) {
con->error_msg = "protocol error, unexpected ack";
return -EINVAL;
}
ceph_decode_64_safe(&p, end, seq, bad);
dout("%s con %p seq %llu\n", __func__, con, seq);
ceph_con_discard_sent(con, seq);
return 0;
bad:
pr_err("failed to decode ack\n");
return -EINVAL;
}
static int process_control(struct ceph_connection *con, void *p, void *end)
{
int tag = con->v2.in_desc.fd_tag;
int ret;
dout("%s con %p tag %d len %d\n", __func__, con, tag, (int)(end - p));
switch (tag) {
case FRAME_TAG_HELLO:
ret = process_hello(con, p, end);
break;
case FRAME_TAG_AUTH_BAD_METHOD:
ret = process_auth_bad_method(con, p, end);
break;
case FRAME_TAG_AUTH_REPLY_MORE:
ret = process_auth_reply_more(con, p, end);
break;
case FRAME_TAG_AUTH_DONE:
ret = process_auth_done(con, p, end);
break;
case FRAME_TAG_AUTH_SIGNATURE:
ret = process_auth_signature(con, p, end);
break;
case FRAME_TAG_SERVER_IDENT:
ret = process_server_ident(con, p, end);
break;
case FRAME_TAG_IDENT_MISSING_FEATURES:
ret = process_ident_missing_features(con, p, end);
break;
case FRAME_TAG_SESSION_RECONNECT_OK:
ret = process_session_reconnect_ok(con, p, end);
break;
case FRAME_TAG_SESSION_RETRY:
ret = process_session_retry(con, p, end);
break;
case FRAME_TAG_SESSION_RETRY_GLOBAL:
ret = process_session_retry_global(con, p, end);
break;
case FRAME_TAG_SESSION_RESET:
ret = process_session_reset(con, p, end);
break;
case FRAME_TAG_KEEPALIVE2_ACK:
ret = process_keepalive2_ack(con, p, end);
break;
case FRAME_TAG_ACK:
ret = process_ack(con, p, end);
break;
default:
pr_err("bad tag %d\n", tag);
con->error_msg = "protocol error, bad tag";
return -EINVAL;
}
if (ret) {
dout("%s con %p error %d\n", __func__, con, ret);
return ret;
}
prepare_read_preamble(con);
return 0;
}
/*
* Return:
* 1 - con->in_msg set, read message
* 0 - skip message
* <0 - error
*/
static int process_message_header(struct ceph_connection *con,
void *p, void *end)
{
struct ceph_frame_desc *desc = &con->v2.in_desc;
struct ceph_msg_header2 *hdr2 = p;
struct ceph_msg_header hdr;
int skip;
int ret;
u64 seq;
/* verify seq# */
seq = le64_to_cpu(hdr2->seq);
if ((s64)seq - (s64)con->in_seq < 1) {
pr_info("%s%lld %s skipping old message: seq %llu, expected %llu\n",
ENTITY_NAME(con->peer_name),
ceph_pr_addr(&con->peer_addr),
seq, con->in_seq + 1);
return 0;
}
if ((s64)seq - (s64)con->in_seq > 1) {
pr_err("bad seq %llu, expected %llu\n", seq, con->in_seq + 1);
con->error_msg = "bad message sequence # for incoming message";
return -EBADE;
}
ceph_con_discard_sent(con, le64_to_cpu(hdr2->ack_seq));
fill_header(&hdr, hdr2, desc->fd_lens[1], desc->fd_lens[2],
desc->fd_lens[3], &con->peer_name);
ret = ceph_con_in_msg_alloc(con, &hdr, &skip);
if (ret)
return ret;
WARN_ON(!con->in_msg ^ skip);
if (skip)
return 0;
WARN_ON(!con->in_msg);
WARN_ON(con->in_msg->con != con);
return 1;
}
static int process_message(struct ceph_connection *con)
{
ceph_con_process_message(con);
/*
* We could have been closed by ceph_con_close() because
* ceph_con_process_message() temporarily drops con->mutex.
*/
if (con->state != CEPH_CON_S_OPEN) {
dout("%s con %p state changed to %d\n", __func__, con,
con->state);
return -EAGAIN;
}
prepare_read_preamble(con);
return 0;
}
static int __handle_control(struct ceph_connection *con, void *p)
{
void *end = p + con->v2.in_desc.fd_lens[0];
struct ceph_msg *msg;
int ret;
if (con->v2.in_desc.fd_tag != FRAME_TAG_MESSAGE)
return process_control(con, p, end);
ret = process_message_header(con, p, end);
if (ret < 0)
return ret;
if (ret == 0) {
prepare_skip_message(con);
return 0;
}
msg = con->in_msg; /* set in process_message_header() */
if (front_len(msg)) {
WARN_ON(front_len(msg) > msg->front_alloc_len);
msg->front.iov_len = front_len(msg);
} else {
msg->front.iov_len = 0;
}
if (middle_len(msg)) {
WARN_ON(middle_len(msg) > msg->middle->alloc_len);
msg->middle->vec.iov_len = middle_len(msg);
} else if (msg->middle) {
msg->middle->vec.iov_len = 0;
}
if (!front_len(msg) && !middle_len(msg) && !data_len(msg))
return process_message(con);
if (con_secure(con))
return prepare_read_tail_secure(con);
return prepare_read_tail_plain(con);
}
static int handle_preamble(struct ceph_connection *con)
{
struct ceph_frame_desc *desc = &con->v2.in_desc;
int ret;
if (con_secure(con)) {
ret = decrypt_preamble(con);
if (ret) {
if (ret == -EBADMSG)
con->error_msg = "integrity error, bad preamble auth tag";
return ret;
}
}
ret = decode_preamble(con->v2.in_buf, desc);
if (ret) {
if (ret == -EBADMSG)
con->error_msg = "integrity error, bad crc";
else
con->error_msg = "protocol error, bad preamble";
return ret;
}
dout("%s con %p tag %d seg_cnt %d %d+%d+%d+%d\n", __func__,
con, desc->fd_tag, desc->fd_seg_cnt, desc->fd_lens[0],
desc->fd_lens[1], desc->fd_lens[2], desc->fd_lens[3]);
if (!con_secure(con))
return prepare_read_control(con);
if (desc->fd_lens[0] > CEPH_PREAMBLE_INLINE_LEN)
return prepare_read_control_remainder(con);
return __handle_control(con, CTRL_BODY(con->v2.in_buf));
}
static int handle_control(struct ceph_connection *con)
{
int ctrl_len = con->v2.in_desc.fd_lens[0];
void *buf;
int ret;
WARN_ON(con_secure(con));
ret = verify_control_crc(con);
if (ret) {
con->error_msg = "integrity error, bad crc";
return ret;
}
if (con->state == CEPH_CON_S_V2_AUTH) {
buf = alloc_conn_buf(con, ctrl_len);
if (!buf)
return -ENOMEM;
memcpy(buf, con->v2.in_kvecs[0].iov_base, ctrl_len);
return __handle_control(con, buf);
}
return __handle_control(con, con->v2.in_kvecs[0].iov_base);
}
static int handle_control_remainder(struct ceph_connection *con)
{
int ret;
WARN_ON(!con_secure(con));
ret = decrypt_control_remainder(con);
if (ret) {
if (ret == -EBADMSG)
con->error_msg = "integrity error, bad control remainder auth tag";
return ret;
}
return __handle_control(con, con->v2.in_kvecs[0].iov_base -
CEPH_PREAMBLE_INLINE_LEN);
}
static int handle_epilogue(struct ceph_connection *con)
{
u32 front_crc, middle_crc, data_crc;
int ret;
if (con_secure(con)) {
ret = decrypt_tail(con);
if (ret) {
if (ret == -EBADMSG)
con->error_msg = "integrity error, bad epilogue auth tag";
return ret;
}
/* just late_status */
ret = decode_epilogue(con->v2.in_buf, NULL, NULL, NULL);
if (ret) {
con->error_msg = "protocol error, bad epilogue";
return ret;
}
} else {
ret = decode_epilogue(con->v2.in_buf, &front_crc,
&middle_crc, &data_crc);
if (ret) {
con->error_msg = "protocol error, bad epilogue";
return ret;
}
ret = verify_epilogue_crcs(con, front_crc, middle_crc,
data_crc);
if (ret) {
con->error_msg = "integrity error, bad crc";
return ret;
}
}
return process_message(con);
}
static void finish_skip(struct ceph_connection *con)
{
dout("%s con %p\n", __func__, con);
if (con_secure(con))
gcm_inc_nonce(&con->v2.in_gcm_nonce);
__finish_skip(con);
}
static int populate_in_iter(struct ceph_connection *con)
{
int ret;
dout("%s con %p state %d in_state %d\n", __func__, con, con->state,
con->v2.in_state);
WARN_ON(iov_iter_count(&con->v2.in_iter));
if (con->state == CEPH_CON_S_V2_BANNER_PREFIX) {
ret = process_banner_prefix(con);
} else if (con->state == CEPH_CON_S_V2_BANNER_PAYLOAD) {
ret = process_banner_payload(con);
} else if ((con->state >= CEPH_CON_S_V2_HELLO &&
con->state <= CEPH_CON_S_V2_SESSION_RECONNECT) ||
con->state == CEPH_CON_S_OPEN) {
switch (con->v2.in_state) {
case IN_S_HANDLE_PREAMBLE:
ret = handle_preamble(con);
break;
case IN_S_HANDLE_CONTROL:
ret = handle_control(con);
break;
case IN_S_HANDLE_CONTROL_REMAINDER:
ret = handle_control_remainder(con);
break;
case IN_S_PREPARE_READ_DATA:
ret = prepare_read_data(con);
break;
case IN_S_PREPARE_READ_DATA_CONT:
prepare_read_data_cont(con);
ret = 0;
break;
case IN_S_PREPARE_READ_ENC_PAGE:
prepare_read_enc_page(con);
ret = 0;
break;
case IN_S_PREPARE_SPARSE_DATA:
ret = prepare_sparse_read_data(con);
break;
case IN_S_PREPARE_SPARSE_DATA_CONT:
ret = prepare_sparse_read_cont(con);
break;
case IN_S_HANDLE_EPILOGUE:
ret = handle_epilogue(con);
break;
case IN_S_FINISH_SKIP:
finish_skip(con);
ret = 0;
break;
default:
WARN(1, "bad in_state %d", con->v2.in_state);
return -EINVAL;
}
} else {
WARN(1, "bad state %d", con->state);
return -EINVAL;
}
if (ret) {
dout("%s con %p error %d\n", __func__, con, ret);
return ret;
}
if (WARN_ON(!iov_iter_count(&con->v2.in_iter)))
return -ENODATA;
dout("%s con %p populated %zu\n", __func__, con,
iov_iter_count(&con->v2.in_iter));
return 1;
}
int ceph_con_v2_try_read(struct ceph_connection *con)
{
int ret;
dout("%s con %p state %d need %zu\n", __func__, con, con->state,
iov_iter_count(&con->v2.in_iter));
if (con->state == CEPH_CON_S_PREOPEN)
return 0;
/*
* We should always have something pending here. If not,
* avoid calling populate_in_iter() as if we read something
* (ceph_tcp_recv() would immediately return 1).
*/
if (WARN_ON(!iov_iter_count(&con->v2.in_iter)))
return -ENODATA;
for (;;) {
ret = ceph_tcp_recv(con);
if (ret <= 0)
return ret;
ret = populate_in_iter(con);
if (ret <= 0) {
if (ret && ret != -EAGAIN && !con->error_msg)
con->error_msg = "read processing error";
return ret;
}
}
}
static void queue_data(struct ceph_connection *con)
{
struct bio_vec bv;
con->v2.out_epil.data_crc = -1;
ceph_msg_data_cursor_init(&con->v2.out_cursor, con->out_msg,
data_len(con->out_msg));
get_bvec_at(&con->v2.out_cursor, &bv);
set_out_bvec(con, &bv, true);
con->v2.out_state = OUT_S_QUEUE_DATA_CONT;
}
static void queue_data_cont(struct ceph_connection *con)
{
struct bio_vec bv;
con->v2.out_epil.data_crc = ceph_crc32c_page(
con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page,
con->v2.out_bvec.bv_offset, con->v2.out_bvec.bv_len);
ceph_msg_data_advance(&con->v2.out_cursor, con->v2.out_bvec.bv_len);
if (con->v2.out_cursor.total_resid) {
get_bvec_at(&con->v2.out_cursor, &bv);
set_out_bvec(con, &bv, true);
WARN_ON(con->v2.out_state != OUT_S_QUEUE_DATA_CONT);
return;
}
/*
* We've written all data. Queue epilogue. Once it's written,
* we are done.
*/
reset_out_kvecs(con);
prepare_epilogue_plain(con, false);
con->v2.out_state = OUT_S_FINISH_MESSAGE;
}
static void queue_enc_page(struct ceph_connection *con)
{
struct bio_vec bv;
dout("%s con %p i %d resid %d\n", __func__, con, con->v2.out_enc_i,
con->v2.out_enc_resid);
WARN_ON(!con->v2.out_enc_resid);
bvec_set_page(&bv, con->v2.out_enc_pages[con->v2.out_enc_i],
min(con->v2.out_enc_resid, (int)PAGE_SIZE), 0);
set_out_bvec(con, &bv, false);
con->v2.out_enc_i++;
con->v2.out_enc_resid -= bv.bv_len;
if (con->v2.out_enc_resid) {
WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE);
return;
}
/*
* We've queued the last piece of ciphertext (ending with
* epilogue) + auth tag. Once it's written, we are done.
*/
WARN_ON(con->v2.out_enc_i != con->v2.out_enc_page_cnt);
con->v2.out_state = OUT_S_FINISH_MESSAGE;
}
static void queue_zeros(struct ceph_connection *con)
{
dout("%s con %p out_zero %d\n", __func__, con, con->v2.out_zero);
if (con->v2.out_zero) {
set_out_bvec_zero(con);
con->v2.out_zero -= con->v2.out_bvec.bv_len;
con->v2.out_state = OUT_S_QUEUE_ZEROS;
return;
}
/*
* We've zero-filled everything up to epilogue. Queue epilogue
* with late_status set to ABORTED and crcs adjusted for zeros.
* Once it's written, we are done patching up for the revoke.
*/
reset_out_kvecs(con);
prepare_epilogue_plain(con, true);
con->v2.out_state = OUT_S_FINISH_MESSAGE;
}
static void finish_message(struct ceph_connection *con)
{
dout("%s con %p msg %p\n", __func__, con, con->out_msg);
/* we end up here both plain and secure modes */
if (con->v2.out_enc_pages) {
WARN_ON(!con->v2.out_enc_page_cnt);
ceph_release_page_vector(con->v2.out_enc_pages,
con->v2.out_enc_page_cnt);
con->v2.out_enc_pages = NULL;
con->v2.out_enc_page_cnt = 0;
}
/* message may have been revoked */
if (con->out_msg) {
ceph_msg_put(con->out_msg);
con->out_msg = NULL;
}
con->v2.out_state = OUT_S_GET_NEXT;
}
static int populate_out_iter(struct ceph_connection *con)
{
int ret;
dout("%s con %p state %d out_state %d\n", __func__, con, con->state,
con->v2.out_state);
WARN_ON(iov_iter_count(&con->v2.out_iter));
if (con->state != CEPH_CON_S_OPEN) {
WARN_ON(con->state < CEPH_CON_S_V2_BANNER_PREFIX ||
con->state > CEPH_CON_S_V2_SESSION_RECONNECT);
goto nothing_pending;
}
switch (con->v2.out_state) {
case OUT_S_QUEUE_DATA:
WARN_ON(!con->out_msg);
queue_data(con);
goto populated;
case OUT_S_QUEUE_DATA_CONT:
WARN_ON(!con->out_msg);
queue_data_cont(con);
goto populated;
case OUT_S_QUEUE_ENC_PAGE:
queue_enc_page(con);
goto populated;
case OUT_S_QUEUE_ZEROS:
WARN_ON(con->out_msg); /* revoked */
queue_zeros(con);
goto populated;
case OUT_S_FINISH_MESSAGE:
finish_message(con);
break;
case OUT_S_GET_NEXT:
break;
default:
WARN(1, "bad out_state %d", con->v2.out_state);
return -EINVAL;
}
WARN_ON(con->v2.out_state != OUT_S_GET_NEXT);
if (ceph_con_flag_test_and_clear(con, CEPH_CON_F_KEEPALIVE_PENDING)) {
ret = prepare_keepalive2(con);
if (ret) {
pr_err("prepare_keepalive2 failed: %d\n", ret);
return ret;
}
} else if (!list_empty(&con->out_queue)) {
ceph_con_get_out_msg(con);
ret = prepare_message(con);
if (ret) {
pr_err("prepare_message failed: %d\n", ret);
return ret;
}
} else if (con->in_seq > con->in_seq_acked) {
ret = prepare_ack(con);
if (ret) {
pr_err("prepare_ack failed: %d\n", ret);
return ret;
}
} else {
goto nothing_pending;
}
populated:
if (WARN_ON(!iov_iter_count(&con->v2.out_iter)))
return -ENODATA;
dout("%s con %p populated %zu\n", __func__, con,
iov_iter_count(&con->v2.out_iter));
return 1;
nothing_pending:
WARN_ON(iov_iter_count(&con->v2.out_iter));
dout("%s con %p nothing pending\n", __func__, con);
ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
return 0;
}
int ceph_con_v2_try_write(struct ceph_connection *con)
{
int ret;
dout("%s con %p state %d have %zu\n", __func__, con, con->state,
iov_iter_count(&con->v2.out_iter));
/* open the socket first? */
if (con->state == CEPH_CON_S_PREOPEN) {
WARN_ON(con->peer_addr.type != CEPH_ENTITY_ADDR_TYPE_MSGR2);
/*
* Always bump global_seq. Bump connect_seq only if
* there is a session (i.e. we are reconnecting and will
* send session_reconnect instead of client_ident).
*/
con->v2.global_seq = ceph_get_global_seq(con->msgr, 0);
if (con->v2.server_cookie)
con->v2.connect_seq++;
ret = prepare_read_banner_prefix(con);
if (ret) {
pr_err("prepare_read_banner_prefix failed: %d\n", ret);
con->error_msg = "connect error";
return ret;
}
reset_out_kvecs(con);
ret = prepare_banner(con);
if (ret) {
pr_err("prepare_banner failed: %d\n", ret);
con->error_msg = "connect error";
return ret;
}
ret = ceph_tcp_connect(con);
if (ret) {
pr_err("ceph_tcp_connect failed: %d\n", ret);
con->error_msg = "connect error";
return ret;
}
}
if (!iov_iter_count(&con->v2.out_iter)) {
ret = populate_out_iter(con);
if (ret <= 0) {
if (ret && ret != -EAGAIN && !con->error_msg)
con->error_msg = "write processing error";
return ret;
}
}
tcp_sock_set_cork(con->sock->sk, true);
for (;;) {
ret = ceph_tcp_send(con);
if (ret <= 0)
break;
ret = populate_out_iter(con);
if (ret <= 0) {
if (ret && ret != -EAGAIN && !con->error_msg)
con->error_msg = "write processing error";
break;
}
}
tcp_sock_set_cork(con->sock->sk, false);
return ret;
}
static u32 crc32c_zeros(u32 crc, int zero_len)
{
int len;
while (zero_len) {
len = min(zero_len, (int)PAGE_SIZE);
crc = crc32c(crc, page_address(ceph_zero_page), len);
zero_len -= len;
}
return crc;
}
static void prepare_zero_front(struct ceph_connection *con, int resid)
{
int sent;
WARN_ON(!resid || resid > front_len(con->out_msg));
sent = front_len(con->out_msg) - resid;
dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
if (sent) {
con->v2.out_epil.front_crc =
crc32c(-1, con->out_msg->front.iov_base, sent);
con->v2.out_epil.front_crc =
crc32c_zeros(con->v2.out_epil.front_crc, resid);
} else {
con->v2.out_epil.front_crc = crc32c_zeros(-1, resid);
}
con->v2.out_iter.count -= resid;
out_zero_add(con, resid);
}
static void prepare_zero_middle(struct ceph_connection *con, int resid)
{
int sent;
WARN_ON(!resid || resid > middle_len(con->out_msg));
sent = middle_len(con->out_msg) - resid;
dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
if (sent) {
con->v2.out_epil.middle_crc =
crc32c(-1, con->out_msg->middle->vec.iov_base, sent);
con->v2.out_epil.middle_crc =
crc32c_zeros(con->v2.out_epil.middle_crc, resid);
} else {
con->v2.out_epil.middle_crc = crc32c_zeros(-1, resid);
}
con->v2.out_iter.count -= resid;
out_zero_add(con, resid);
}
static void prepare_zero_data(struct ceph_connection *con)
{
dout("%s con %p\n", __func__, con);
con->v2.out_epil.data_crc = crc32c_zeros(-1, data_len(con->out_msg));
out_zero_add(con, data_len(con->out_msg));
}
static void revoke_at_queue_data(struct ceph_connection *con)
{
int boundary;
int resid;
WARN_ON(!data_len(con->out_msg));
WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
resid = iov_iter_count(&con->v2.out_iter);
boundary = front_len(con->out_msg) + middle_len(con->out_msg);
if (resid > boundary) {
resid -= boundary;
WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN);
dout("%s con %p was sending head\n", __func__, con);
if (front_len(con->out_msg))
prepare_zero_front(con, front_len(con->out_msg));
if (middle_len(con->out_msg))
prepare_zero_middle(con, middle_len(con->out_msg));
prepare_zero_data(con);
WARN_ON(iov_iter_count(&con->v2.out_iter) != resid);
con->v2.out_state = OUT_S_QUEUE_ZEROS;
return;
}
boundary = middle_len(con->out_msg);
if (resid > boundary) {
resid -= boundary;
dout("%s con %p was sending front\n", __func__, con);
prepare_zero_front(con, resid);
if (middle_len(con->out_msg))
prepare_zero_middle(con, middle_len(con->out_msg));
prepare_zero_data(con);
queue_zeros(con);
return;
}
WARN_ON(!resid);
dout("%s con %p was sending middle\n", __func__, con);
prepare_zero_middle(con, resid);
prepare_zero_data(con);
queue_zeros(con);
}
static void revoke_at_queue_data_cont(struct ceph_connection *con)
{
int sent, resid; /* current piece of data */
WARN_ON(!data_len(con->out_msg));
WARN_ON(!iov_iter_is_bvec(&con->v2.out_iter));
resid = iov_iter_count(&con->v2.out_iter);
WARN_ON(!resid || resid > con->v2.out_bvec.bv_len);
sent = con->v2.out_bvec.bv_len - resid;
dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
if (sent) {
con->v2.out_epil.data_crc = ceph_crc32c_page(
con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page,
con->v2.out_bvec.bv_offset, sent);
ceph_msg_data_advance(&con->v2.out_cursor, sent);
}
WARN_ON(resid > con->v2.out_cursor.total_resid);
con->v2.out_epil.data_crc = crc32c_zeros(con->v2.out_epil.data_crc,
con->v2.out_cursor.total_resid);
con->v2.out_iter.count -= resid;
out_zero_add(con, con->v2.out_cursor.total_resid);
queue_zeros(con);
}
static void revoke_at_finish_message(struct ceph_connection *con)
{
int boundary;
int resid;
WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
resid = iov_iter_count(&con->v2.out_iter);
if (!front_len(con->out_msg) && !middle_len(con->out_msg) &&
!data_len(con->out_msg)) {
WARN_ON(!resid || resid > MESSAGE_HEAD_PLAIN_LEN);
dout("%s con %p was sending head (empty message) - noop\n",
__func__, con);
return;
}
boundary = front_len(con->out_msg) + middle_len(con->out_msg) +
CEPH_EPILOGUE_PLAIN_LEN;
if (resid > boundary) {
resid -= boundary;
WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN);
dout("%s con %p was sending head\n", __func__, con);
if (front_len(con->out_msg))
prepare_zero_front(con, front_len(con->out_msg));
if (middle_len(con->out_msg))
prepare_zero_middle(con, middle_len(con->out_msg));
con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
WARN_ON(iov_iter_count(&con->v2.out_iter) != resid);
con->v2.out_state = OUT_S_QUEUE_ZEROS;
return;
}
boundary = middle_len(con->out_msg) + CEPH_EPILOGUE_PLAIN_LEN;
if (resid > boundary) {
resid -= boundary;
dout("%s con %p was sending front\n", __func__, con);
prepare_zero_front(con, resid);
if (middle_len(con->out_msg))
prepare_zero_middle(con, middle_len(con->out_msg));
con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
queue_zeros(con);
return;
}
boundary = CEPH_EPILOGUE_PLAIN_LEN;
if (resid > boundary) {
resid -= boundary;
dout("%s con %p was sending middle\n", __func__, con);
prepare_zero_middle(con, resid);
con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
queue_zeros(con);
return;
}
WARN_ON(!resid);
dout("%s con %p was sending epilogue - noop\n", __func__, con);
}
void ceph_con_v2_revoke(struct ceph_connection *con)
{
WARN_ON(con->v2.out_zero);
if (con_secure(con)) {
WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE &&
con->v2.out_state != OUT_S_FINISH_MESSAGE);
dout("%s con %p secure - noop\n", __func__, con);
return;
}
switch (con->v2.out_state) {
case OUT_S_QUEUE_DATA:
revoke_at_queue_data(con);
break;
case OUT_S_QUEUE_DATA_CONT:
revoke_at_queue_data_cont(con);
break;
case OUT_S_FINISH_MESSAGE:
revoke_at_finish_message(con);
break;
default:
WARN(1, "bad out_state %d", con->v2.out_state);
break;
}
}
static void revoke_at_prepare_read_data(struct ceph_connection *con)
{
int remaining;
int resid;
WARN_ON(con_secure(con));
WARN_ON(!data_len(con->in_msg));
WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
resid = iov_iter_count(&con->v2.in_iter);
WARN_ON(!resid);
remaining = data_len(con->in_msg) + CEPH_EPILOGUE_PLAIN_LEN;
dout("%s con %p resid %d remaining %d\n", __func__, con, resid,
remaining);
con->v2.in_iter.count -= resid;
set_in_skip(con, resid + remaining);
con->v2.in_state = IN_S_FINISH_SKIP;
}
static void revoke_at_prepare_read_data_cont(struct ceph_connection *con)
{
int recved, resid; /* current piece of data */
int remaining;
WARN_ON(con_secure(con));
WARN_ON(!data_len(con->in_msg));
WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter));
resid = iov_iter_count(&con->v2.in_iter);
WARN_ON(!resid || resid > con->v2.in_bvec.bv_len);
recved = con->v2.in_bvec.bv_len - resid;
dout("%s con %p recved %d resid %d\n", __func__, con, recved, resid);
if (recved)
ceph_msg_data_advance(&con->v2.in_cursor, recved);
WARN_ON(resid > con->v2.in_cursor.total_resid);
remaining = CEPH_EPILOGUE_PLAIN_LEN;
dout("%s con %p total_resid %zu remaining %d\n", __func__, con,
con->v2.in_cursor.total_resid, remaining);
con->v2.in_iter.count -= resid;
set_in_skip(con, con->v2.in_cursor.total_resid + remaining);
con->v2.in_state = IN_S_FINISH_SKIP;
}
static void revoke_at_prepare_read_enc_page(struct ceph_connection *con)
{
int resid; /* current enc page (not necessarily data) */
WARN_ON(!con_secure(con));
WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter));
resid = iov_iter_count(&con->v2.in_iter);
WARN_ON(!resid || resid > con->v2.in_bvec.bv_len);
dout("%s con %p resid %d enc_resid %d\n", __func__, con, resid,
con->v2.in_enc_resid);
con->v2.in_iter.count -= resid;
set_in_skip(con, resid + con->v2.in_enc_resid);
con->v2.in_state = IN_S_FINISH_SKIP;
}
static void revoke_at_prepare_sparse_data(struct ceph_connection *con)
{
int resid; /* current piece of data */
int remaining;
WARN_ON(con_secure(con));
WARN_ON(!data_len(con->in_msg));
WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter));
resid = iov_iter_count(&con->v2.in_iter);
dout("%s con %p resid %d\n", __func__, con, resid);
remaining = CEPH_EPILOGUE_PLAIN_LEN + con->v2.data_len_remain;
con->v2.in_iter.count -= resid;
set_in_skip(con, resid + remaining);
con->v2.in_state = IN_S_FINISH_SKIP;
}
static void revoke_at_handle_epilogue(struct ceph_connection *con)
{
int resid;
resid = iov_iter_count(&con->v2.in_iter);
WARN_ON(!resid);
dout("%s con %p resid %d\n", __func__, con, resid);
con->v2.in_iter.count -= resid;
set_in_skip(con, resid);
con->v2.in_state = IN_S_FINISH_SKIP;
}
void ceph_con_v2_revoke_incoming(struct ceph_connection *con)
{
switch (con->v2.in_state) {
case IN_S_PREPARE_SPARSE_DATA:
case IN_S_PREPARE_READ_DATA:
revoke_at_prepare_read_data(con);
break;
case IN_S_PREPARE_READ_DATA_CONT:
revoke_at_prepare_read_data_cont(con);
break;
case IN_S_PREPARE_READ_ENC_PAGE:
revoke_at_prepare_read_enc_page(con);
break;
case IN_S_PREPARE_SPARSE_DATA_CONT:
revoke_at_prepare_sparse_data(con);
break;
case IN_S_HANDLE_EPILOGUE:
revoke_at_handle_epilogue(con);
break;
default:
WARN(1, "bad in_state %d", con->v2.in_state);
break;
}
}
bool ceph_con_v2_opened(struct ceph_connection *con)
{
return con->v2.peer_global_seq;
}
void ceph_con_v2_reset_session(struct ceph_connection *con)
{
con->v2.client_cookie = 0;
con->v2.server_cookie = 0;
con->v2.global_seq = 0;
con->v2.connect_seq = 0;
con->v2.peer_global_seq = 0;
}
void ceph_con_v2_reset_protocol(struct ceph_connection *con)
{
iov_iter_truncate(&con->v2.in_iter, 0);
iov_iter_truncate(&con->v2.out_iter, 0);
con->v2.out_zero = 0;
clear_in_sign_kvecs(con);
clear_out_sign_kvecs(con);
free_conn_bufs(con);
if (con->v2.in_enc_pages) {
WARN_ON(!con->v2.in_enc_page_cnt);
ceph_release_page_vector(con->v2.in_enc_pages,
con->v2.in_enc_page_cnt);
con->v2.in_enc_pages = NULL;
con->v2.in_enc_page_cnt = 0;
}
if (con->v2.out_enc_pages) {
WARN_ON(!con->v2.out_enc_page_cnt);
ceph_release_page_vector(con->v2.out_enc_pages,
con->v2.out_enc_page_cnt);
con->v2.out_enc_pages = NULL;
con->v2.out_enc_page_cnt = 0;
}
con->v2.con_mode = CEPH_CON_MODE_UNKNOWN;
memzero_explicit(&con->v2.in_gcm_nonce, CEPH_GCM_IV_LEN);
memzero_explicit(&con->v2.out_gcm_nonce, CEPH_GCM_IV_LEN);
if (con->v2.hmac_tfm) {
crypto_free_shash(con->v2.hmac_tfm);
con->v2.hmac_tfm = NULL;
}
if (con->v2.gcm_req) {
aead_request_free(con->v2.gcm_req);
con->v2.gcm_req = NULL;
}
if (con->v2.gcm_tfm) {
crypto_free_aead(con->v2.gcm_tfm);
con->v2.gcm_tfm = NULL;
}
}
|