File: packet.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (3052 lines) | stat: -rw-r--r-- 109,762 bytes parent folder | download | duplicates (2)
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
/* parsing packets: formats and tools
 *
 * Copyright (C) 1997 Angelos D. Keromytis.
 * Copyright (C) 1998-2001,2013 D. Hugh Redelmeier <hugh@mimosa.com>
 * Copyright (C) 2012 Avesh Agarwal <avagarwa@redhat.com>
 * Copyright (C) 2012-2013 Paul Wouters <pwouters@redhat.com>
 * Copyright (C) 2015,2018 Andrew Cagney
 * Copyright (C) 2020 Nupur Agrawal <nupur202000@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>		/* for AF_INET/AF_INET6 */

#include "constants.h"
#include "lswalloc.h"
#include "impair.h"
#include "ip_info.h"		/* used by pbs_in_address() */
#include "packet.h"
#include "shunk.h"

#include "defs.h"
#include "log.h"

/*
 * IKEv1/IKEv2 Header: for all messages
 *
 * Layout from IKEv1's RFC 2408 "ISAKMP" section 3.1 "ISAKMP Header
 * Format", and IKEv2's RFC 7296 section 3.1. "The IKE Header".
 *
 * In IKEv1 the IKE SA Initiator/Responder SPIs were called cookies.
 *
 * The definition of the Message ID changed from (IKEv1):
 *
 * o  Message ID (4 octets) - Unique Message Identifier used to
 *    identify protocol state during Phase 2 negotiations.  This value
 *    is randomly generated by the initiator of the Phase 2
 *    negotiation.
 *
 * to (IKEv2):
 *
 * o  Message ID (4 octets, unsigned integer) - Message identifier used
 *    to control retransmission of lost packets and matching of requests
 *    and responses.  It is essential to the security of the protocol
 *    because it is used to prevent message replay attacks.  See
 *    Sections 2.1 and 2.2.
 *
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                    IKE SA Initiator's SPI                     !
 * !                           (icookie)                           !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                    IKE SA Responder's SPI                     !
 * !                           (rcookie)                           !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !  Next Payload ! MjVer ! MnVer ! Exchange Type !     Flags     !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                          Message ID                           !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                            Length                             !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

static field_desc isa_fields[] = {
	{ ft_raw, IKE_SA_SPI_SIZE, "initiator SPI", NULL },
	{ ft_raw, IKE_SA_SPI_SIZE, "responder SPI", NULL },
	{ ft_mnpc, 8 / BITS_IN_BYTE, "next payload type", &payload_names_ikev1orv2 },
	{ ft_loose_enum, 8 / BITS_IN_BYTE, "ISAKMP version", &version_names },
	{ ft_enum, 8 / BITS_IN_BYTE, "exchange type", &isakmp_xchg_type_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &isakmp_flag_names },
	{ ft_nat, 32 / BITS_IN_BYTE, "Message ID", NULL },
	{ ft_len, 32 / BITS_IN_BYTE, "length", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_hdr_desc = {
	.name = "ISAKMP Message",
	.fields = isa_fields,
	.size = sizeof(struct isakmp_hdr),
	.pt = ISAKMP_NEXT_NONE,
};

static field_desc raw_isa_fields[] = {
	{ ft_raw, IKE_SA_SPI_SIZE, "initiator SPI", NULL },
	{ ft_raw, IKE_SA_SPI_SIZE, "responder SPI", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "next payload type", NULL, },
	{ ft_nat, 8 / BITS_IN_BYTE, "ISAKMP version", NULL, },
	{ ft_nat, 8 / BITS_IN_BYTE, "exchange type", NULL, },
	{ ft_nat, 8 / BITS_IN_BYTE, "flags", NULL, },
	{ ft_nat, 32 / BITS_IN_BYTE, "Message ID", NULL },
	{ ft_nat, 32 / BITS_IN_BYTE, "length", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc raw_isakmp_hdr_desc = {
	.name = "ISAKMP Message (raw)",
	.fields = raw_isa_fields,
	.size = sizeof(struct isakmp_hdr),
	.pt = ISAKMP_NEXT_NONE,
};

/* Generic portion of all ISAKMP payloads.
 * layout from RFC 2408 "ISAKMP" section 3.2
 * This describes the first 32-bit chunk of all payloads.
 * The previous next payload depends on the actual payload type.
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

static field_desc isag_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &payload_names_ikev1orv2 },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_end, 0, NULL, NULL }
};

/* ISAKMP Data Attribute (generic representation within payloads)
 * layout from RFC 2408 "ISAKMP" section 3.3
 * This is not a payload type.
 * In TLV format, this is followed by a value field.
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !A!       Attribute Type        !    AF=0  Attribute Length     !
 * !F!                             !    AF=1  Attribute Value      !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * .                   AF=0  Attribute Value                       .
 * .                   AF=1  Not Transmitted                       .
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

/* Oakley Attributes */
static field_desc isaat_fields_oakley[] = {
	{ ft_af_enum, 16 / BITS_IN_BYTE, "af+type", &oakley_attr_names },
	{ ft_lv, 16 / BITS_IN_BYTE, "length/value", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_oakley_attribute_desc = {
	.name = "ISAKMP Oakley attribute",
	.fields = isaat_fields_oakley,
	.size = sizeof(struct isakmp_attribute),
};

/* IPsec DOI Attributes */
static field_desc isaat_fields_ipsec[] = {
	{ ft_af_enum, 16 / BITS_IN_BYTE, "af+type", &ipsec_attr_names },
	{ ft_lv, 16 / BITS_IN_BYTE, "length/value", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_ipsec_attribute_desc = {
	.name = "ISAKMP IPsec DOI attribute",
	.fields = isaat_fields_ipsec,
	.size = sizeof(struct isakmp_attribute),
};

/* XAUTH Attributes */
static field_desc isaat_fields_xauth[] = {
	{ ft_af_loose_enum, 16 / BITS_IN_BYTE, "ModeCfg attr type", &modecfg_attr_names },
	{ ft_lv, 16 / BITS_IN_BYTE, "length/value", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_xauth_attribute_desc = {
	.name = "ISAKMP ModeCfg attribute",
	.fields = isaat_fields_xauth,
	.size = sizeof(struct isakmp_attribute),
};

/* ISAKMP Security Association Payload
 * layout from RFC 2408 "ISAKMP" section 3.4
 *
 * A variable length Situation followed by 0 or more Proposal payloads
 * follow.
 *
 * The "Next Payload [...] field MUST NOT contain the values for the
 * Proposal or Transform payloads as they are considered part of the
 * security association negotiation".  Hence .csst is set.
 *
 * Previous next payload: ISAKMP_NEXT_SA
 *
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !              Domain of Interpretation  (DOI)                  !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                           Situation                           ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isasa_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &payload_names_ikev1orv2 },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 32 / BITS_IN_BYTE, "DOI", &doi_names },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_sa_desc = {
	.name = "ISAKMP Security Association Payload",
	.fields = isasa_fields,
	.size = sizeof(struct isakmp_sa),
	.pt = ISAKMP_NEXT_SA,
	.nsst = ISAKMP_NEXT_P,
};

static field_desc ipsec_sit_field[] = {
	{ ft_lset, 32 / BITS_IN_BYTE, "IPsec DOI SIT", &sit_bit_names },
	{ ft_end, 0, NULL, NULL }
};

struct_desc ipsec_sit_desc = {
	.name = "IPsec DOI SIT",
	.fields = ipsec_sit_field,
	.size = sizeof(uint32_t),
};

/* ISAKMP Proposal Payload
 * layout from RFC 2408 "ISAKMP" section 3.5
 *
 * A variable length SPI and then Transform Payloads follow.
 *
 * XXX:
 *
 * The Next Payload field below is something of a misnomer.  It
 * doesn't play any part in the Next Payload Chain.  Instead it just
 * acts as a flag where 0 indicate if it is the last payload within
 * the SA.
 *
 * In IKEv2, this payload/field has been replaced by a sub-structure and
 * "Last Substruct[ure]" field (and then makes the point that it is
 * entirely redundant).
 *
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !  Proposal #   !  Protocol-Id  !    SPI Size   !# of Transforms!
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                        SPI (variable)                         !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isap_fields[] = {
	{ ft_lss, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "proposal number", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "protocol ID", &ikev1_protocol_names },
	{ ft_nat, 8 / BITS_IN_BYTE, "SPI size", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "number of transforms", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_proposal_desc = {
	.name = "ISAKMP Proposal Payload",
	.fields = isap_fields,
	.size = sizeof(struct isakmp_proposal),
	.nsst = ISAKMP_NEXT_T,
};

/* ISAKMP Transform Payload
 * layout from RFC 2408 "ISAKMP" section 3.6
 *
 * Variable length SA Attributes follow.
 *
 * XXX:
 *
 * The Next Payload field below is something of a misnomer.  It
 * doesn't play any part in the Next Payload Chain.  Instead it just
 * acts as a flag where zero indicates that it is last payload within
 * the proposal.
 *
 * In IKEv2, this payload/field has been replaced by a sub-structure and
 * "Last Substruct[ure]" field (and then makes the point that it is
 * entirely redundant).
 *
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !  Transform #  !  Transform-Id !           RESERVED2           !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                        SA Attributes                          ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

/* PROTO_ISAKMP */
static field_desc isat_fields_isakmp[] = {
	{ ft_lss, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "ISAKMP transform number", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ISAKMP transform ID", &isakmp_transformid_names },
	{ ft_zig, 16 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_isakmp_transform_desc = {
	.name = "ISAKMP Transform Payload (ISAKMP)",
	.fields = isat_fields_isakmp,
	.size = sizeof(struct isakmp_transform),
};

/* PROTO_IPSEC_AH */
static field_desc isat_fields_ah[] = {
	{ ft_lss, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "AH transform number", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "AH transform ID", &ah_transformid_names },
	{ ft_zig, 16 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_ah_transform_desc = {
	.name = "ISAKMP Transform Payload (AH)",
	.fields = isat_fields_ah,
	.size = sizeof(struct isakmp_transform),
};

/* PROTO_IPSEC_ESP */
static field_desc isat_fields_esp[] = {
	{ ft_lss, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "ESP transform number", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ESP transform ID", &esp_transformid_names },
	{ ft_zig, 16 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_esp_transform_desc = {
	.name = "ISAKMP Transform Payload (ESP)",
	.fields = isat_fields_esp,
	.size = sizeof(struct isakmp_transform),
};

/* PROTO_IPCOMP */
static field_desc isat_fields_ipcomp[] = {
	{ ft_lss, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "IPCOMP transform number", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "IPCOMP transform ID", &ipsec_ipcomp_algo_names },
	{ ft_zig, 16 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_ipcomp_transform_desc = {
	.name = "ISAKMP Transform Payload (COMP)",
	.fields = isat_fields_ipcomp,
	.size = sizeof(struct isakmp_transform),
};

/* ISAKMP Key Exchange Payload: no fixed fields beyond the generic ones.
 * layout from RFC 2408 "ISAKMP" section 3.7
 * Variable Key Exchange Data follow the generic fields.
 * Previous next payload: ISAKMP_NEXT_KE
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                       Key Exchange Data                       ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
struct_desc isakmp_keyex_desc =	{
	.name = "ISAKMP Key Exchange Payload",
	.fields = isag_fields,
	.size = sizeof(struct isakmp_generic),
	.pt = ISAKMP_NEXT_KE,
};

/* ISAKMP Identification Payload
 * layout from RFC 2408 "ISAKMP" section 3.8
 * See "struct identity" declared later.
 * Variable length Identification Data follow.
 * Previous next payload: ISAKMP_NEXT_ID
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !   ID Type     !             DOI Specific ID Data              !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                   Identification Data                         ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isaid_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &payload_names_ikev1orv2 },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ID type", &ikev1_ike_id_type_names }, /* ??? depends on DOI? */
	{ ft_nat, 8 / BITS_IN_BYTE, "DOI specific A", NULL },          /* ??? depends on DOI? */
	{ ft_nat, 16 / BITS_IN_BYTE, "DOI specific B", NULL },         /* ??? depends on DOI? */
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_identification_desc = {
	.name = "ISAKMP Identification Payload",
	.fields = isaid_fields,
	.size = sizeof(struct isakmp_id),
	.pt = ISAKMP_NEXT_ID,
};

/* IPSEC Identification Payload Content
 * layout from RFC 2407 "IPsec DOI" section 4.6.2
 * See struct isakmp_id declared earlier.
 * Note: Hashing skips the ISAKMP generic payload header
 * Variable length Identification Data follow.
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !  Next Payload !   RESERVED    !        Payload Length         !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !   ID Type     !  Protocol ID  !             Port              !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ~                     Identification Data                       ~
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isaiid_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &payload_names_ikev1orv2 },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ID type", &ikev1_ike_id_type_names },
	{ ft_loose_enum, 8 / BITS_IN_BYTE, "Protocol ID", &ip_protocol_id_names, },
	{ ft_nat, 16 / BITS_IN_BYTE, "port", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_ipsec_identification_desc = {
	.name = "ISAKMP Identification Payload (IPsec DOI)",
	.fields = isaiid_fields,
	.size = sizeof(struct isakmp_ipsec_id),
	.pt = ISAKMP_NEXT_ID,
};

/* ISAKMP Certificate Payload: oddball fixed field beyond the generic ones.
 * layout from RFC 2408 "ISAKMP" section 3.9
 * Variable length Certificate Data follow the generic fields.
 * Previous next payload: ISAKMP_NEXT_CERT.
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Cert Encoding !                                               !
 * +-+-+-+-+-+-+-+-+                                               !
 * ~                       Certificate Data                        ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isacert_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &payload_names_ikev1orv2 },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "cert encoding", &ike_cert_type_names },
	{ ft_end, 0, NULL, NULL }
};

/* Note: the size field of isakmp_ipsec_certificate_desc cannot be
 * sizeof(struct isakmp_cert) because that will rounded up for padding.
 */
struct_desc isakmp_ipsec_certificate_desc = {
	.name = "ISAKMP Certificate Payload",
	.fields = isacert_fields,
	.size = ISAKMP_CERT_SIZE,
	.pt = ISAKMP_NEXT_CERT,
};

/* ISAKMP Certificate Request Payload: oddball field beyond the generic ones.
 * layout from RFC 2408 "ISAKMP" section 3.10
 * Variable length Certificate Types and Certificate Authorities follow.
 * Previous next payload: ISAKMP_NEXT_CR.
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !  Cert. Type   !                                               !
 * +-+-+-+-+-+-+-+-+                                               !
 * ~                    Certificate Authority                      ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isacr_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &payload_names_ikev1orv2 },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "cert type", &ike_cert_type_names },
	{ ft_end, 0, NULL, NULL }
};

/* Note: the size field of isakmp_ipsec_cert_req_desc cannot be
 * sizeof(struct isakmp_cr) because that will rounded up for padding.
 */
struct_desc isakmp_ipsec_cert_req_desc = {
	.name = "ISAKMP Certificate RequestPayload",
	.fields = isacr_fields,
	.size = ISAKMP_CR_SIZE,
	.pt = ISAKMP_NEXT_CR,
};

/* ISAKMP Hash Payload: no fixed fields beyond the generic ones.
 * layout from RFC 2408 "ISAKMP" section 3.11
 * Variable length Hash Data follow.
 * Previous next payload: ISAKMP_NEXT_HASH.
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                           Hash Data                           ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
struct_desc isakmp_hash_desc = {
	.name = "ISAKMP Hash Payload",
	.fields = isag_fields,
	.size = sizeof(struct isakmp_generic),
	.pt = ISAKMP_NEXT_HASH,
};

/* ISAKMP Signature Payload: no fixed fields beyond the generic ones.
 * layout from RFC 2408 "ISAKMP" section 3.12
 * Variable length Signature Data follow.
 * Previous next payload: ISAKMP_NEXT_SIG.
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                         Signature Data                        ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
struct_desc isakmp_signature_desc = {
	.name = "ISAKMP Signature Payload",
	.fields = isag_fields,
	.size = sizeof(struct isakmp_generic),
	.pt = ISAKMP_NEXT_SIG,
};

/* ISAKMP Nonce Payload: no fixed fields beyond the generic ones.
 * layout from RFC 2408 "ISAKMP" section 3.13
 * Variable length Nonce Data follow.
 * Previous next payload: ISAKMP_NEXT_NONCE.
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                            Nonce Data                         ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
struct_desc isakmp_nonce_desc =	{
	.name = "ISAKMP Nonce Payload",
	.fields = isag_fields,
	.size = sizeof(struct isakmp_generic),
	.pt = ISAKMP_NEXT_NONCE,
};

/* ISAKMP Notification Payload
 * layout from RFC 2408 "ISAKMP" section 3.14
 * This is followed by a variable length SPI
 * and then possibly by variable length Notification Data.
 * Previous next payload: ISAKMP_NEXT_N
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !              Domain of Interpretation  (DOI)                  !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !  Protocol-ID  !   SPI Size    !      Notify Message Type      !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                Security Parameter Index (SPI)                 ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                       Notification Data                       ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isan_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 32 / BITS_IN_BYTE, "DOI", &doi_names },
	{ ft_nat, 8 / BITS_IN_BYTE, "protocol ID", NULL }, /* ??? really enum: ISAKMP, IPSEC, ESP, ... */
	{ ft_nat, 8 / BITS_IN_BYTE, "SPI size", NULL },
	{ ft_enum, 16 / BITS_IN_BYTE, "Notify Message Type", &v1_notification_names },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_notification_desc = {
	.name = "ISAKMP Notification Payload",
	.fields = isan_fields,
	.size = sizeof(struct isakmp_notification),
	.pt = ISAKMP_NEXT_N,
};

/* ISAKMP Delete Payload
 * layout from RFC 2408 "ISAKMP" section 3.15
 * This is followed by a variable length SPI.
 * Previous next payload: ISAKMP_NEXT_D
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !              Domain of Interpretation  (DOI)                  !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !  Protocol-Id  !   SPI Size    !           # of SPIs           !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~               Security Parameter Index(es) (SPI)              ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isad_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 32 / BITS_IN_BYTE, "DOI", &doi_names },
	{ ft_nat, 8 / BITS_IN_BYTE, "protocol ID", NULL }, /* ??? really enum: ISAKMP, IPSEC */
	{ ft_nat, 8 / BITS_IN_BYTE, "SPI size", NULL },
	{ ft_nat, 16 / BITS_IN_BYTE, "number of SPIs", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_delete_desc = {
	.name = "ISAKMP Delete Payload",
	.fields = isad_fields,
	.size = sizeof(struct isakmp_delete),
	.pt = ISAKMP_NEXT_D,
};

/* ISAKMP Vendor ID Payload
 * layout from RFC 2408 "ISAKMP" section 3.15
 * This is followed by a variable length VID.
 * Previous next payload: ISAKMP_NEXT_VID
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                        Vendor ID (VID)                        ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
struct_desc isakmp_vendor_id_desc = {
	.name = "ISAKMP Vendor ID Payload",
	.fields = isag_fields,
	.size = sizeof(struct isakmp_generic),
	.pt = ISAKMP_NEXT_VID,
};

/* MODECFG */
/*
 * From draft-dukes-ike-mode-cfg
 * 3.2. Attribute Payload
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   ! Next Payload  !   RESERVED    !         Payload Length        !
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   !     Type      !   RESERVED    !           Identifier          !
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   !                                                               !
 *   ~                           Attributes                          ~
 *   !                                                               !
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isaattr_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "Attr Msg Type", &attr_msg_type_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_nat, 16 / BITS_IN_BYTE, "Identifier", NULL },
	{ ft_end, 0, NULL, NULL }
};

/*
 * MODECFG
 * From draft-dukes-ike-mode-cfg
 * 3.2. Attribute Payload
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   ! Next Payload  !   RESERVED    !         Payload Length        !
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   !     Type      !   RESERVED    !           Identifier          !
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   !                                                               !
 *   !                                                               !
 *   ~                           Attributes                          ~
 *   !                                                               !
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

struct_desc isakmp_attr_desc = {
	.name = "ISAKMP Mode Attribute",
	.fields = isaattr_fields,
	.size = sizeof(struct isakmp_mode_attr),
	.pt = ISAKMP_NEXT_MODECFG,
};

/*
 * ISAKMP NAT-Traversal NAT-D
 * RFC 3947 https://tools.ietf.org/html/rfc3947
 *
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                 HASH of the address and port                  !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
struct_desc isakmp_nat_d = {
	.name = "ISAKMP NAT-D Payload",
	.fields = isag_fields,
	.size = sizeof(struct isakmp_generic),
	.pt = ISAKMP_NEXT_NATD_RFC,
};

struct_desc isakmp_nat_d_drafts = {
	.name = "ISAKMP NAT-D Payload (draft)",
	.fields = isag_fields,
	.size = sizeof(struct isakmp_generic),
	.pt = ISAKMP_NEXT_NATD_DRAFTS,
};

/*
 * ISAKMP NAT-Traversal NAT-OA
 * RFC 3947 https://tools.ietf.org/html/rfc3947
 *
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !   ID Type     !                   RESERVED                    !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !         IPv4 (4 octets) or IPv6 address (16 octets)           !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isanat_oa_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev1_payload_names },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ID type", &ikev1_ike_id_type_names },
	{ ft_zig, 24 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_nat_oa = {
	.name = "ISAKMP NAT-OA Payload",
	.fields = isanat_oa_fields,
	.size = sizeof(struct isakmp_nat_oa),
	.pt = ISAKMP_NEXT_NATOA_RFC,
};

struct_desc isakmp_nat_oa_drafts = {
	.name = "ISAKMP NAT-OA Payload (draft)",
	.fields = isanat_oa_fields,
	.size = sizeof(struct isakmp_nat_oa),
	.pt = ISAKMP_NEXT_NATOA_DRAFTS,
};

/* Generic payload: an unknown input payload */

struct_desc isakmp_ignore_desc = {
	.name = "ignored ISAKMP Generic Payload",
	.fields = isag_fields,
	.size = sizeof(struct isakmp_generic),
	.pt = ISAKMP_NEXT_NONE,
};

/* ISAKMP IKE Fragmentation Payload
 * Cisco proprietary, undocumented
 *
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !   RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !          Fragment ID          !  Frag Number  !     Flags     !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~                         Fragment Data                         ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc isafrag_fields[] = {
	{ ft_zig, 8 / BITS_IN_BYTE, "next payload type", NULL },
	{ ft_zig, 8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat, 16 / BITS_IN_BYTE, "fragment id", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "fragment number", NULL },
	{ ft_nat, 8 / BITS_IN_BYTE, "flags", NULL }, /* 0x1 means last fragment */
	{ ft_end, 0, NULL, NULL }
};

struct_desc isakmp_ikefrag_desc = {
	.name = "ISAKMP IKE Fragment Payload",
	.fields = isafrag_fields,
	.size = sizeof(struct isakmp_ikefrag),
	.pt = ISAKMP_NEXT_IKE_FRAGMENTATION,
};

/*
 * GENERIC IKEv2 header.
 * Note differs from IKEv1, in that it has flags with one bit a critical bit
 */
static field_desc ikev2generic_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_end,  0, NULL, NULL }
};
/* only for reading an unknown-to-us payload */
struct_desc ikev2_generic_desc = {
	.name = "IKEv2 Generic Payload",
	.fields = ikev2generic_fields,
	.size = sizeof(struct ikev2_generic),
	.pt = ISAKMP_NEXT_v2NONE,	/* could be any unknown */
};

/* for IMPAIR_ADD_UNKNOWN_PAYLOAD_TO_* */
struct_desc ikev2_unknown_payload_desc = {
	.name = "IKEv2 Unknown Payload",
	.fields = ikev2generic_fields,
	.size = sizeof(struct ikev2_generic),
	.pt = ISAKMP_NEXT_v2UNKNOWN,
};

/*
 * IKEv2 - Security Association Payload
 *
 * layout from RFC 4306 - section 3.3.
 * A variable number of Proposal Substructures follow.
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                          <Proposals>                          ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */
struct_desc ikev2_sa_desc = {
	.name = "IKEv2 Security Association Payload",
	.fields = ikev2generic_fields,
	.size = sizeof(struct ikev2_sa),
	.pt = ISAKMP_NEXT_v2SA,
	.nsst = v2_PROPOSAL_NON_LAST,
};

/* IKEv2 - Proposal sub-structure
 *
 * 3.3.1.  Proposal Substructure
 *
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! 0 (last) or 2 !   RESERVED    !         Proposal Length       !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Proposal #    !  Protocol ID  !    SPI Size   !# of Transforms!
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ~                        SPI (variable)                         ~
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                        <Transforms>                           ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *             Figure 7:  Proposal Substructure
 */
static field_desc ikev2prop_fields[] = {
	{ ft_lss, 8 / BITS_IN_BYTE, "last proposal", &ikev2_last_proposal_desc },
	{ ft_zig,  8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat,  8 / BITS_IN_BYTE, "prop #", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "proto ID", &ikev2_proposal_protocol_id_names },
	{ ft_nat,  8 / BITS_IN_BYTE, "spi size", NULL },
	{ ft_nat,  8 / BITS_IN_BYTE, "# transforms", NULL },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_prop_desc = {
	.name = "IKEv2 Proposal Substructure Payload",
	.fields = ikev2prop_fields,
	.size = sizeof(struct ikev2_prop),
	.nsst = v2_TRANSFORM_NON_LAST,
};

/*
 * 3.3.2.  Transform Substructure
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! 0 (last) or 3 !   RESERVED    !        Transform Length       !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !Transform Type !   RESERVED    !          Transform ID         !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                      Transform Attributes                     ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */

static field_desc ikev2trans_fields[] = {
	{ ft_lss, 8 / BITS_IN_BYTE, "last transform", &ikev2_last_transform_desc },
	{ ft_zig,  8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_loose_enum, 8 / BITS_IN_BYTE, "IKEv2 transform type", &ikev2_trans_type_names },
	{ ft_zig,  8 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_loose_enum_enum, 16 / BITS_IN_BYTE, "IKEv2 transform ID", &v2_transform_ID_enums }, /* select enum based on transform type */
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_trans_desc = {
	.name = "IKEv2 Transform Substructure Payload",
	.fields = ikev2trans_fields,
	.size = sizeof(struct ikev2_trans),
};

/*
 * 3.3.5.   [Transform] Attribute substructure
 *
 *                          1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     !A!       Attribute Type        !    AF=0  Attribute Length     !
 *     !F!                             !    AF=1  Attribute Value      !
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     !                   AF=0  Attribute Value                       !
 *     !                   AF=1  Not Transmitted                       !
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */
static field_desc ikev2_trans_attr_fields[] = {
	{ ft_af_enum, 16 / BITS_IN_BYTE, "af+type", &ikev2_trans_attr_descs },
	{ ft_lv,      16 / BITS_IN_BYTE, "length/value", NULL },
	{ ft_end,     0, NULL, NULL }
};

struct_desc ikev2_trans_attr_desc = {
	.name = "IKEv2 Attribute Substructure Payload",
	.fields = ikev2_trans_attr_fields,
	.size = sizeof(struct ikev2_trans_attr),
};

/* 3.4.  Key Exchange Payload
 *
 * The Key Exchange Payload, denoted KE in this memo, is used to
 * exchange Diffie-Hellman public numbers as part of a Diffie-Hellman
 * key exchange.  The Key Exchange Payload consists of the IKE generic
 * payload header followed by the Diffie-Hellman public value itself.
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !          DH Group #           !           RESERVED            !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                       Key Exchange Data                       ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *              Figure 10:  Key Exchange Payload Format
 *
 */
static field_desc ikev2ke_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 16 / BITS_IN_BYTE, "DH group", &oakley_group_names },
	{ ft_zig, 16 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end,  0, NULL, NULL },
};

struct_desc ikev2_ke_desc = {
	.name = "IKEv2 Key Exchange Payload",
	.fields = ikev2ke_fields,
	.size = sizeof(struct ikev2_ke),
	.pt = ISAKMP_NEXT_v2KE,
};

/*
 * 3.5.  Identification Payloads
 *
 * The Identification Payloads, denoted IDi and IDr in this memo, allow
 * peers to assert an identity to one another.  This identity may be
 * used for policy lookup, but does not necessarily have to match
 * anything in the CERT payload; both fields may be used by an
 * implementation to perform access control decisions.
 *
 * NOTE: In IKEv1, two ID payloads were used in each direction to hold
 * Traffic Selector (TS) information for data passing over the SA.  In
 * IKEv2, this information is carried in TS payloads (see section 3.13).
 *
 * The Identification Payload consists of the IKE generic payload header
 * followed by identification fields as follows:
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !   ID Type     !                "RESERVED"                     |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                   Identification Data                         ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *             Figure 11:  Identification Payload Format
 *
 * Because the ID payload's RESERVED field is included in the AUTH
 * hash calculation it isn't really reserved (aka send zero, ignore on
 * input).  Hence it is flagged as ft_raw and not ft_zig.
 */

static field_desc ikev2id_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ID type", &ikev2_ike_id_type_names },
	{ ft_raw, 24 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end,  0, NULL, NULL },
};

struct_desc ikev2_id_i_desc = {
	.name ="IKEv2 Identification - Initiator - Payload",
	.fields = ikev2id_fields,
	.size = sizeof(struct ikev2_id),
	.pt = ISAKMP_NEXT_v2IDi,
};

struct_desc ikev2_id_r_desc = {
	.name ="IKEv2 Identification - Responder - Payload",
	.fields = ikev2id_fields,
	.size = sizeof(struct ikev2_id),
	.pt = ISAKMP_NEXT_v2IDr,
};

/*
 * IKEv2 - RFC 8784 (no ascii art provided in RFC)
 * PPK_ID types
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +---------------+-----------------------------------------------+
 *    | PPK Type      |                                               |
 *    +---------------+         PPK Data                              +
 *    ~                                                               ~
 *    +---------------+-----------------------------------------------+
 *
 * PPK_ID Type               Value
 * -----------               -----
 * Reserved                  0
 * PPK_ID_OPAQUE             1
 * PPK_ID_FIXED              2
 * Unassigned                3-127
 * Reserved for private use  128-255
 */
static field_desc ikev2_ppk_id_fields[] = {
	{ ft_enum, 8 / BITS_IN_BYTE, "PPK ID type", &ikev2_ppk_id_type_names },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_ppk_id_desc = {
	.name = "IKEv2 PPK ID Payload",
	.fields = ikev2_ppk_id_fields,
	.size = sizeof(struct ikev2_ppk_id),
};


static field_desc ikev2cp_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ikev2_cfg_type", &ikev2_cp_type_names },
	{ ft_zig, 24 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_cp_desc = {
	.name = "IKEv2 Configuration Payload",
	.fields = ikev2cp_fields,
	.size = sizeof(struct ikev2_cp),
	.pt = ISAKMP_NEXT_v2CP,
};

static field_desc ikev2_cp_attrbute_fields[] = {
	{ ft_loose_enum, 16 / BITS_IN_BYTE, "Attribute Type", &ikev2_cp_attribute_type_names },
	{ ft_lv, 16 / BITS_IN_BYTE, "length/value", NULL },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_cp_attribute_desc = {
	.name = "IKEv2 Configuration Payload Attribute",
	.fields = ikev2_cp_attrbute_fields,
	.size = sizeof(struct ikev2_cp_attribute),
};

/* section 3.6
 * The Certificate Payload is defined as follows:
 *
 *                          1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     ! Cert Encoding !                                               !
 *     +-+-+-+-+-+-+-+-+                                               !
 *     ~                       Certificate Data                        ~
 *     !                                                               !
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */
static field_desc ikev2_cert_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ikev2 cert encoding",
	  &ikev2_cert_type_names },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_certificate_desc = {
	.name = "IKEv2 Certificate Payload",
	.fields = ikev2_cert_fields,
	.size = IKEV2_CERT_SIZE,
	.pt = ISAKMP_NEXT_v2CERT,
};

/* section 3.7
 *
 * The Certificate Request Payload is defined as follows:
 *
 *                          1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     ! Cert Encoding !                                               !
 *     +-+-+-+-+-+-+-+-+                                               !
 *     ~                    Certification Authority                    ~
 *     !                                                               !
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */

static field_desc ikev2_cert_req_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "ikev2 cert encoding",
	  &ikev2_cert_type_names },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_certificate_req_desc = {
	.name = "IKEv2 Certificate Request Payload",
	.fields = ikev2_cert_req_fields,
	.size = IKEV2_CERT_SIZE,
	.pt = ISAKMP_NEXT_v2CERTREQ,
};

/*
 * 3.8.  Authentication Payload
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Auth Method   !                RESERVED                       !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                      Authentication Data                      ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *               Figure 14:  Authentication Payload Format
 *
 */
static field_desc ikev2_auth_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "auth method", &ikev2_auth_method_names },
	{ ft_zig, 24 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_auth_desc = {
	.name = "IKEv2 Authentication Payload",
	.fields = ikev2_auth_fields,
	.size = sizeof(struct ikev2_auth),
	.pt = ISAKMP_NEXT_v2AUTH,
};

/*
 * 3.9.  Nonce Payload
 *
 * The Nonce Payload, denoted Ni and Nr in this memo for the initiator's
 * and responder's nonce respectively, contains random data used to
 * guarantee liveness during an exchange and protect against replay
 * attacks.
 *
 * The Nonce Payload is defined as follows:
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                            Nonce Data                         ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *                 Figure 15:  Nonce Payload Format
 */
struct_desc ikev2_nonce_desc = {
	.name = "IKEv2 Nonce Payload",
	.fields = ikev2generic_fields,
	.size = sizeof(struct ikev2_generic),
	.pt = ISAKMP_NEXT_v2Ni, /*==ISAKMP_NEXT_v2Nr*/
};

/*    3.10 Notify Payload
 *
 *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !  Protocol ID  !   SPI Size    !      Notify Message Type      !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                Security Parameter Index (SPI)                 ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                       Notification Data                       ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */
static field_desc ikev2_notify_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "Protocol ID", &ikev2_notify_protocol_id_names },
	/* names used are v1 names may be we should use 4306 3.3.1 names */
	{ ft_nat,  8 / BITS_IN_BYTE, "SPI size", NULL },
	{ ft_loose_enum, 16 / BITS_IN_BYTE, "Notify Message Type",
	  &v2_notification_names },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_notify_desc = {
	.name = "IKEv2 Notify Payload",
	.fields = ikev2_notify_fields,
	.size = sizeof(struct ikev2_notify),
	.pt = ISAKMP_NEXT_v2N,
};

/* IKEv2 Delete Payload
 * layout from RFC 5996 Section 3.11
 * This is followed by a variable length SPI.
 *
 *                      1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * ! Next Payload  !C| RESERVED    !         Payload Length        !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !  Protocol ID  !   SPI Size    !           Num of SPIs         !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * !                                                               !
 * ~               Security Parameter Index(es) (SPI)              ~
 * !                                                               !
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

static field_desc ikev2_delete_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "protocol ID", &ikev2_delete_protocol_id_names },
	{ ft_nat, 8 / BITS_IN_BYTE, "SPI size", NULL },
	{ ft_nat, 16 / BITS_IN_BYTE, "number of SPIs", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc ikev2_delete_desc = {
	.name = "IKEv2 Delete Payload",
	.fields = ikev2_delete_fields,
	.size = sizeof(struct ikev2_delete),
	.pt = ISAKMP_NEXT_v2D,
};

/*
 * 3.12.  Vendor ID Payload
 *
 *  The Vendor ID Payload fields are defined as follows:
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                        Vendor ID (VID)                        ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */
struct_desc ikev2_vendor_id_desc = {
	.name = "IKEv2 Vendor ID Payload",
	.fields = ikev2generic_fields,
	.size = sizeof(struct ikev2_generic),
	.pt = ISAKMP_NEXT_v2V,
};

/*
 * 3.13.  Traffic Selector Payload
 *
 *
 * The Traffic Selector Payload, denoted TS in this memo, allows peers
 * to identify packet flows for processing by IPsec security services.
 * The Traffic Selector Payload consists of the IKE generic payload
 * header followed by individual traffic selectors as follows:
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Number of TSs !                 RESERVED                      !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                       <Traffic Selectors>                     ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc ikev2_ts_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat,  8 / BITS_IN_BYTE, "number of TS", NULL },
	{ ft_zig, 24 / BITS_IN_BYTE, "reserved", NULL },
	{ ft_end,  0, NULL, NULL }
};
struct_desc ikev2_ts_i_desc = {
	.name = "IKEv2 Traffic Selector - Initiator - Payload",
	.fields = ikev2_ts_fields,
	.size = sizeof(struct ikev2_ts),
	.pt = ISAKMP_NEXT_v2TSi,
};
struct_desc ikev2_ts_r_desc = {
	.name = "IKEv2 Traffic Selector - Responder - Payload",
	.fields = ikev2_ts_fields,
	.size = sizeof(struct ikev2_ts),
	.pt = ISAKMP_NEXT_v2TSr,
};

/*
 * Different types of traffic selector are handled together
 * See RFC 7296 and draft-ietf-ipsecme-labeled-ipsec-04
 *
 * 3.13.1.  Traffic Selector
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !   TS Type     !IP Protocol ID*|       Selector Length         |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |           Start Port          |           End Port            |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                         Starting Address                      ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                                                               !
 *    ~                         Ending Address                        ~
 *    !                                                               !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *                Figure 20: Traffic Selector
 *
 * 2.1.  TS_SECLABEL payload format
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +---------------+---------------+-------------------------------+
 *    |   TS Type     |    Reserved   |       Selector Length         |
 *    +---------------+---------------+-------------------------------+
 *    |                                                               |
 *    ~                         Security Label*                       ~
 *    |                                                               |
 *    +---------------------------------------------------------------+
 *
 *                Figure 1: Labeled IPsec Traffic Selector
 */

/*
 * These are just read raw, so no struct or fields/desc are needed
 */
/*
 * Traffic Selector Header - read header, then sub out work to type handler
 */
static field_desc ikev2_ts_header_fields[] = {
	{ ft_enum, 8 / BITS_IN_BYTE, "TS type", &ikev2_ts_type_names },
	{ ft_loose_enum,  8 / BITS_IN_BYTE, "IP Protocol ID", &ip_protocol_id_names, },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_end,  0, NULL, NULL }
};
struct_desc ikev2_ts_header_desc = {
	.name = "IKEv2 Traffic Selector Header",
	.fields = ikev2_ts_header_fields,
	.size = 4 /*sizeof(struct ikev2_ts_header) */ ,
};

static field_desc ikev2_ts_portrange_fields[] = {
	{ ft_nat, 16 / BITS_IN_BYTE, "start port", NULL },
	{ ft_nat, 16 / BITS_IN_BYTE, "end port", NULL },
	{ ft_end, 0, NULL, NULL }
};
struct_desc ikev2_ts_portrange_desc = {
	.name = "IKEv2 IP Traffic Selector port range",
	.fields = ikev2_ts_portrange_fields,
	.size = sizeof(struct ikev2_ts_portrange),
};

/*
 * 3.14.  Encrypted Payload
 *                         1                   2                   3
 *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ! Next Payload  !C!  RESERVED   !         Payload Length        !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !                     Initialization Vector                     !
 *    !         (length is block size for encryption algorithm)       !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ~                    Encrypted IKE Payloads                     ~
 *    +               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    !               !             Padding (0-255 octets)            !
 *    +-+-+-+-+-+-+-+-+                               +-+-+-+-+-+-+-+-+
 *    !                                               !  Pad Length   !
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ~                    Integrity Checksum Data                    ~
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *             Figure 21:  Encrypted Payload Format
 */
struct_desc ikev2_sk_desc = {
	.name = "IKEv2 Encryption Payload",
	.fields = ikev2generic_fields,
	.size = sizeof(struct ikev2_generic),
	.pt = ISAKMP_NEXT_v2SK,
};

/* 3.16.  EAP
 *
 * Use the generic IKEv2 headers only as the struct here. Several different EAP
 * message types can be as the payload and the encoding depends on EAP header.
 * The EAP messages are encoded/decoded as nested structs, and the message types
 * supported are defined below.
 */
struct_desc ikev2_eap_desc = {
	.name = "EAP Payload",
	.fields = ikev2generic_fields,
	.size = sizeof(struct ikev2_generic),
	.pt = ISAKMP_NEXT_v2EAP,
};

/*
 * RFC 3748 4.2. Success and Failure
 *
 *    0                   1                   2                   3
 *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |     Code      |  Identifier   |            Length             |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc eap_termination_fields[] = {
	{ ft_enum, 8 / BITS_IN_BYTE, "code", &eap_code_names },
	{ ft_nat, 8 / BITS_IN_BYTE, "identifier", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_end, 0, NULL, NULL }
};

struct_desc eap_termination_desc = {
	.name = "EAP Success/Failure",
	.fields = eap_termination_fields,
	.size = sizeof(struct eap_termination),
};

/*
 * RFC 5216 3.1.  EAP-TLS
 *
 *   0                   1                   2                   3
 *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |     Code      |   Identifier  |            Length             |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |     Type      |     Flags     |      TLS Message Length
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |     TLS Message Length        |       TLS Data...
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *  TLS Message Length is included only if 'L' flag is set.
 *
 * Since presence of 'TLS Message Length' depends on flags, it's not part of
 * the field/struct descriptor, but explicitly encoded/decoded in code together
 * with the 'TLS data' as the payload.
 */
static field_desc eap_tls_fields[] = {
	{ ft_enum, 8 / BITS_IN_BYTE, "code", &eap_code_names },
	{ ft_nat, 8 / BITS_IN_BYTE, "identifier", NULL },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "type", &eap_type_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &eaptls_flag_names },
	{ ft_end, 0, NULL, NULL }
};

struct_desc eap_tls_desc = {
	.name = "EAP TLS header",
	.fields = eap_tls_fields,
	.size = sizeof(struct eap_tls),
};

/*
 * RFC 7383 2.5.  Fragmenting Message
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    | Next Payload  |C|  RESERVED   |         Payload Length        |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |        Fragment Number        |        Total Fragments        |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                     Initialization Vector                     |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ~                      Encrypted content                        ~
 *    +               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |               |             Padding (0-255 octets)            |
 *    +-+-+-+-+-+-+-+-+                               +-+-+-+-+-+-+-+-+
 *    |                                               |  Pad Length   |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    ~                    Integrity Checksum Data                    ~
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *	Next Payload (1 octet) - in the very first fragment (with Fragment
 *	Number equal to 1), this field MUST be set to the payload type of
 *	the first inner payload (the same as for the Encrypted payload).
 *	In the rest of the Fragment messages (with Fragment Number greater
 *	than 1), this field MUST be set to zero.
 *
 * XXX: Even though the SKF's Next Payload field isn't really part of
 * the Next Payload chain (it is under the fragmentation code's
 * control so should be an ft_enum) it needs to be ft_pnpc so that the
 * code triggering an update of the message's next payload chain
 * executed.
 *
 *                         Encrypted Fragment Payload
 */
static field_desc ikev2skf_fields[] = {
	{ ft_pnpc, 8 / BITS_IN_BYTE, "next payload type", &ikev2_payload_names },
	{ ft_lset, 8 / BITS_IN_BYTE, "flags", &payload_flag_names },
	{ ft_len, 16 / BITS_IN_BYTE, "length", NULL },
	{ ft_nat, 16 / BITS_IN_BYTE, "fragment number", NULL },
	{ ft_nat, 16 / BITS_IN_BYTE, "total fragments", NULL },
	{ ft_end,  0, NULL, NULL }
};
struct_desc ikev2_skf_desc = {
	.name = "IKEv2 Encrypted Fragment",
	.fields = ikev2skf_fields,
	.size = sizeof(struct ikev2_skf),
	.pt = ISAKMP_NEXT_v2SKF,
};

/*
 * IKEv2 REDIRECT Payload - variable part
 *
 *                         1                   2                   3
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    | GW Ident Type |  GW Ident Len |                               |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               ~
 *    ~                   New Responder GW Identity                   ~
 *    |                                                               |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                                                               |
 *    ~                        Nonce Data                             ~
 *    |                                                               |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * This is actually Notify Data in IKEv2 Notify payload (see RFC 5685)
 *
 * This struct_desc will be used for checking GW Ident Type and GW
 * Ident Len fields, the rest when using in_struct will be stored in
 * separate struct pbs_in.
 */
static field_desc ikev2redirect_fields[] = {
	{ ft_enum, 8 / BITS_IN_BYTE, "GW Identity type", &ikev2_redirect_gw_names },
	{ ft_nat,  8 / BITS_IN_BYTE, "GW Identity length", NULL },	/* this cannot be ft_len,
									   because of the Nonce Data */
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2_redirect_desc = {
	.name = "IKEv2 Redirect Notify Data",
	.fields = ikev2redirect_fields,
	.size = sizeof(struct ikev2_redirect_part),
};

static field_desc suggested_group_fields[] = {
	{ ft_enum, 16 / BITS_IN_BYTE, "suggested DH Group", &oakley_group_names },
	{ ft_end,  0, NULL, NULL }
};

struct_desc suggested_group_desc = {
	.name = "Suggested Group",
	.fields = suggested_group_fields,
	.size = sizeof(struct suggested_group),
	.pt = ISAKMP_NEXT_v2NONE,
};

/*
 * TICKET_LT_OPAQUE Notify Payload - variable part
 *
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                       Lifetime                                |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                                                               |
 *    ~                        Ticket                                 ~
 *    |                                                               |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
static field_desc ikev2_ticket_lifetime_fields[] = {
	{ ft_nat, 32 / BITS_IN_BYTE, "Lifetime", NULL },
	{ ft_end,  0, NULL, NULL }
};
struct_desc ikev2_ticket_lifetime_desc = {
	.name = "TICKET_LT_OPAQUE Lifetime",
	.fields = ikev2_ticket_lifetime_fields,
	.size = sizeof(struct ikev2_ticket_lifetime),
};

/*
 * IPcomp Notify as per RFC 7296:
 *
 * The data associated with this Notify message includes a two-octet
 * IPComp CPI followed by a one-octet Transform ID optionally followed
 * by attributes whose length and format are defined by that Transform
 * ID.
 *
 * Currently, we only support one Transform ID (2, IPCOMP_DEFLATE) which has
 * no further attributes.
 */
static field_desc ikev2notify_ipcomp_fields[] = {
	{ ft_nat, 16 / BITS_IN_BYTE, "IPcomp SPI (CPI)", NULL },
	{ ft_enum, 8 / BITS_IN_BYTE, "IKEv2 Notification IPCOMP Transform IDs", &ipsec_ipcomp_algo_names },
	{ ft_end,  0, NULL, NULL }
};

struct_desc ikev2notify_ipcomp_data_desc = {
	.name = "IKEv2 IPcomp Notify Data",
	.fields = ikev2notify_ipcomp_fields,
	//.size = sizeof(struct ikev2_notify_ipcomp_data),
	.size = 3,
};

/*
 * descriptor for each V1 payload type
 *
 * There is a slight problem in that some payloads differ, depending
 * on the mode.  Since this is table only used for top-level payloads,
 * Proposal and Transform payloads need not be handled.  That leaves
 * only Identification payloads as a problem.  We make all these
 * entries NULL
 */
struct_desc *v1_payload_desc(unsigned p)
{
	static struct_desc *const v1_payload_descs[] = {
		NULL,                           /* 0 ISAKMP_NEXT_NONE (No other payload following) */
		&isakmp_sa_desc,                /* 1 ISAKMP_NEXT_SA (Security Association) */
		NULL,                           /* 2 ISAKMP_NEXT_P (Proposal) */
		NULL,                           /* 3 ISAKMP_NEXT_T (Transform) */
		&isakmp_keyex_desc,             /* 4 ISAKMP_NEXT_KE (Key Exchange) */
		NULL,                           /* 5 ISAKMP_NEXT_ID (Identification) */
		&isakmp_ipsec_certificate_desc, /* 6 ISAKMP_NEXT_CERT (Certificate) */
		&isakmp_ipsec_cert_req_desc,    /* 7 ISAKMP_NEXT_CR (Certificate Request) */
		&isakmp_hash_desc,              /* 8 ISAKMP_NEXT_HASH (Hash) */
		&isakmp_signature_desc,         /* 9 ISAKMP_NEXT_SIG (Signature) */
		&isakmp_nonce_desc,             /* 10 ISAKMP_NEXT_NONCE (Nonce) */
		&isakmp_notification_desc,      /* 11 ISAKMP_NEXT_N (Notification) */
		&isakmp_delete_desc,            /* 12 ISAKMP_NEXT_D (Delete) */
		&isakmp_vendor_id_desc,         /* 13 ISAKMP_NEXT_VID (Vendor ID) */
		&isakmp_attr_desc,              /* 14 ISAKMP_NEXT_MCFG_ATTR (ModeCfg) */
		NULL,                           /* 15 */
		NULL,                           /* 16 */
		NULL,                           /* 17 */
		NULL,                           /* 18 */
		NULL,                           /* 19 */
		&isakmp_nat_d,                  /* 20=130 ISAKMP_NEXT_NATD_RFC=ISAKMP_NEXT_NATD_DRAFTS (NAT-D) */
		&isakmp_nat_oa,                 /* 21=131 ISAKMP_NEXT_NATOA_RFC=ISAKMP_NEXT_NATOA_DRAFTS (NAT-OA) */
	};
	return p < elemsof(v1_payload_descs) ? v1_payload_descs[p] : NULL;
}

struct_desc *v2_payload_desc(unsigned p)
{
	static struct_desc *const v2_payload_descs[] = {
		&ikev2_sa_desc,                 /* 33 ISAKMP_NEXT_v2SA */
		&ikev2_ke_desc,                 /* 34 ISAKMP_NEXT_v2KE */
		&ikev2_id_i_desc,		/* 35 ISAKMP_NEXT_v2IDi */
		&ikev2_id_r_desc,		/* 36 ISAKMP_NEXT_v2IDr */
		&ikev2_certificate_desc,        /* 37 ISAKMP_NEXT_v2CERT */
		&ikev2_certificate_req_desc,    /* 38 ISAKMP_NEXT_v2CERTREQ */
		&ikev2_auth_desc,               /* 39 ISAKMP_NEXT_v2AUTH */
		&ikev2_nonce_desc,              /* 40 ISAKMP_NEXT_v2Ni / ISAKMP_NEXT_v2Nr */
		&ikev2_notify_desc,             /* 41 ISAKMP_NEXT_v2N */
		&ikev2_delete_desc,             /* 42 ISAKMP_NEXT_v2D */
		&ikev2_vendor_id_desc,          /* 43 ISAKMP_NEXT_v2V */
		&ikev2_ts_i_desc,		/* 44 ISAKMP_NEXT_v2TSi */
		&ikev2_ts_r_desc,		/* 45 ISAKMP_NEXT_v2TSr */
		&ikev2_sk_desc,                 /* 46 ISAKMP_NEXT_v2SK */
		&ikev2_cp_desc,			/* 47 ISAKMP_NEXT_v2CP */
		&ikev2_eap_desc,		/* 48 ISAKMP_NEXT_v2EAP */
		NULL,				/* 49 */
		NULL,				/* 50 */
		NULL,				/* 51 */
		NULL,				/* 52 */
		&ikev2_skf_desc,                /* 53 ISAKMP_NEXT_v2SKF */
	};
	if (p < ISAKMP_v2PAYLOAD_TYPE_BASE) {
		return NULL;
	}
	unsigned q = p - ISAKMP_v2PAYLOAD_TYPE_BASE;
	if (q >= elemsof(v2_payload_descs)) {
		return NULL;
	}
	return v2_payload_descs[q];
}

#define PBS_INIT(START, LEN, NAME)			\
	{						\
	/* .container = NULL, */			\
	/* .desc = NULL, */				\
	.name = name,					\
		.start = START,				\
		.cur = START,				\
		.roof = START + LEN,			\
		/* .lenfld = NULL, */			\
		/* .lenfld_desc = NULL, */		\
		/* .previous_np = NULL, */		\
		/* .previous_npc.fp = NULL, */		\
		/* .previous_np_struct = NULL, */	\
		}

struct pbs_out open_pbs_out(const char *name, uint8_t *buffer, size_t sizeof_buffer,
			    struct logger *logger)
{
	struct pbs_out outs = PBS_INIT(buffer, sizeof_buffer, name);
	outs.logger = logger;
	memset(buffer, 0xFA, sizeof_buffer);	/* value likely to be unpleasant */
	ldbg(logger, "opening output PBS %s", name);
	return outs;
}

bool open_fragment_pbs_out(const char *name,
			   struct fragment_pbs_out *pbs,
			   struct logger *logger)
{
	pbs->pbs = open_pbs_out(name, pbs->buffer,
				sizeof(pbs->buffer),
				logger);
	return true;
}

bool open_large_pbs_out(const char *name, struct large_pbs_out *pbs, struct logger *logger)
{
	pbs->pbs = open_pbs_out(name, pbs->buffer, sizeof(pbs->buffer), logger);
	return true;
}

struct pbs_in pbs_in_from_shunk(shunk_t shunk, const char *name)
{
	/*
	 * XXX: note uncast-const hack because pbs_in and pbs_out
	 * share same structure.
	 */
	struct pbs_in pbs = PBS_INIT((void*)shunk.ptr, shunk.len, name);
	return pbs;
}

/* start - roof */

shunk_t pbs_in_all(const struct pbs_in *pbs)
{
	return shunk2(pbs->start, pbs->roof - pbs->start);
}

shunk_t pbs_out_all(const struct pbs_out *pbs)
{
	return shunk2(pbs->start, pbs->cur - pbs->start);
}

chunk_t clone_pbs_in_all(const struct pbs_in *pbs, const char *name)
{
	shunk_t all = pbs_in_all(pbs);
	return clone_hunk(all, name);
}

chunk_t clone_pbs_out_all(const struct pbs_out *pbs, const char *name)
{
	shunk_t all = pbs_out_all(pbs);
	return clone_hunk(all, name);
}

/* start - cursor */

shunk_t pbs_in_to_cursor(const struct pbs_in *pbs)
{
	return shunk2(pbs->start, pbs->cur - pbs->start);
}

shunk_t pbs_out_to_cursor(const struct pbs_out *pbs)
{
	return shunk2(pbs->start, pbs->cur - pbs->start);
}

/* cursor - roof */

shunk_t pbs_in_left(const struct pbs_in *pbs)
{
	return shunk2(pbs->cur, pbs->roof - pbs->cur);
}

/*
 * print a natural number using the specified FMT, with the value in
 * network-byte-order appended.
 */

static void DBG_print_nat(const field_desc *fp, uintmax_t nat)
{
	LLOG_JAMBUF(DEBUG_STREAM, &global_logger, buf) {
		jam(buf, "   %s: %ju", fp->name, nat);
		jam(buf, " (");
		/*
		 * Note that a single byte value such as "(23)" is
		 * ambiguous.  Since it is prefixed by the equivalent
		 * decimal it should be clear.
		 *
		 * XXX: the same conversion code appears in
		 * out_struct() and probably elsewhere?
		 */
		passert(fp->size > 0);
		passert(fp->size <= sizeof(nat));
		uint8_t bytes[sizeof(nat)];
		uint8_t *cur = bytes + fp->size;
		uintmax_t n = nat;
		do {
			cur--;
			*cur = (uint8_t)n;
			n >>= BITS_IN_BYTE;
		} while (cur > bytes);
		jam_dump_bytes(buf, bytes, fp->size);
		jam(buf, ")");
	}
}

/*
 * print a host-byte-ordered struct
 *
 * This code assumes that the network and host structure members have
 * the same alignment and size!  This requires that all padding be
 * explicit.
 */

static void DBG_print_struct(const char *label, unsigned level,
			     const void *struct_ptr,
			     struct_desc *sd, bool len_meaningful)
{
	bool immediate = false;
	const uint8_t *inp = struct_ptr;
	field_desc *fp;
	uintmax_t last_enum = 0;

	char stars[40]; /* arbitrary limit on label+flock-of-* */
	for (unsigned s = 0; s < level && s < elemsof(stars)-2; s++) {
		stars[s+0] = '*';
		stars[s+1] = '\0';
	}

	DBG_log("%s%s%s:", stars, label, sd->name);

	for (fp = sd->fields; fp->field_type != ft_end; fp++) {
		int i = fp->size;
		uintmax_t n = 0;

		switch (fp->field_type) {
		case ft_zig:		/* zero (ignore violations) */
			inp += i;
			break;
		case ft_nat:            /* natural number (may be 0) */
		case ft_len:            /* length of this struct and any following crud */
		case ft_lv:             /* length/value field of attribute */
		case ft_enum:           /* value from an enumeration */
		case ft_loose_enum:     /* value from an enumeration with only some names known */
		case ft_mnpc:
		case ft_pnpc:
		case ft_lss:		/* last substructure field */
		case ft_loose_enum_enum:	/* value from an enumeration with partial name table based on previous enum */
		case ft_af_enum:        /* Attribute Format + value from an enumeration */
		case ft_af_loose_enum:  /* Attribute Format + value from an enumeration */
		case ft_lset:           /* bits representing set */
			/* grab bytes in host-byte-order */
			switch (i) {
			case 8 / BITS_IN_BYTE:
				n = *(const uint8_t *)inp;
				break;
			case 16 / BITS_IN_BYTE:
				n = *(const uint16_t *)inp;
				break;
			case 32 / BITS_IN_BYTE:
				n = *(const uint32_t *)inp;
				break;
			default:
				bad_case(i);
			}

			/* display the result */
			switch (fp->field_type) {

			case ft_len:    /* length of this struct and any following crud */
			case ft_lv:     /* length/value field of attribute */
				if (!immediate && !len_meaningful)
					break;
				DBG_print_nat(fp, n);
				break;

			case ft_nat: /* natural number (may be 0) */
				DBG_print_nat(fp, n);
				break;

			case ft_af_loose_enum:  /* Attribute Format + value from an enumeration */
			case ft_af_enum:        /* Attribute Format + value from an enumeration */
			{
				/*
				 * try to deal with fp->desc
				 * containing a selection of
				 * AF+<value> and <value> entries.
				 */
				immediate = ((n & ISAKMP_ATTR_AF_MASK) ==
					     ISAKMP_ATTR_AF_TV);
				last_enum = n & ~ISAKMP_ATTR_AF_MASK;
				esb_buf nb;
				if (!enum_name(fp->desc, last_enum, &nb)) {
					enum_name(fp->desc, n, &nb);
				}
				DBG_log("   %s: %s%s (0x%jx)",
					fp->name,
					immediate ? "AF+" : "",
					nb.buf, n);
				break;
			}

			case ft_enum:           /* value from an enumeration */
			case ft_loose_enum:     /* value from an enumeration with only some names known */
			case ft_mnpc:
			case ft_pnpc:
			case ft_lss:
			{
				last_enum = n;
				esb_buf nb;
				DBG_log("   %s: %s (0x%jx)",
					fp->name,
					str_enum(fp->desc, n, &nb),
					n);
				break;
			}

			case ft_loose_enum_enum:
			{
				enum_buf buf;
				const char *name = str_enum_enum(fp->desc, last_enum, n, &buf);
				DBG_log("   %s: %s (0x%jx)",
					fp->name, name, n);
				break;
			}

			case ft_lset: /* bits representing set */
				LLOG_JAMBUF(DEBUG_STREAM, &global_logger, buf) {
					jam(buf, "   %s: ", fp->name);
					jam_lset(buf, fp->desc, n);
					jam(buf, " (0x%jx)", n);
				}
				break;

			default:
				bad_case(fp->field_type);
			}
			inp += i;
			break;

		case ft_raw:            /* bytes to be left in network-order */
			LLOG_JAMBUF(DEBUG_STREAM, &global_logger, buf) {
				jam(buf, "   %s: ", fp->name);
				jam_dump_bytes(buf, inp, i);
			}
			inp += i;
			break;

		default:
			bad_case(fp->field_type);
		}
	}
}

static void DBG_prefix_print_pbs_out_struct(const struct pbs_out *pbs,
					    const char *label, const void *struct_ptr,
					    struct_desc *sd, bool len_meaningful)
{
	/*
	 * Print out a title, with a prefix of asterisks to show the
	 * nesting level; minimum of one star.
	 */
	unsigned level = 1;
	for (const struct pbs_out *p = pbs; p != NULL; p = p->container) {
		level++;
	}
	DBG_print_struct(label, level, struct_ptr, sd, len_meaningful);
}

static void DBG_prefix_print_pbs_in_struct(const struct pbs_in *pbs,
					   const char *label, const void *struct_ptr,
					   struct_desc *sd, bool len_meaningful)
{
	/*
	 * Print out a title, with a prefix of asterisks to show the
	 * nesting level; minimum of one star.
	 */
	unsigned level = 1;
	for (const struct pbs_in *p = pbs; p != NULL; p = p->container) {
		level++;
	}
	DBG_print_struct(label, level, struct_ptr, sd, len_meaningful);
}

/* "parse" a network struct into a host struct.
 *
 * This code assumes that the network and host structure
 * members have the same alignment and size!  This requires
 * that all padding be explicit.
 *
 * If obj_pbs is supplied, a new struct pbs_in is created for the
 * variable part of the structure (this depends on their
 * being one length field in the structure).  The cursor of this
 * new PBS is set to after the parsed part of the struct.
 *
 * This routine returns TRUE iff it succeeds.
 */

diag_t pbs_in_struct(struct pbs_in *ins, struct_desc *sd,
		     void *dest_start, size_t dest_size,
		     struct pbs_in *obj_pbs)
{
	const uint8_t *cur = ins->cur;
	if (cur + sd->size > ins->roof) {
		return diag("not enough room in input packet for %s (remain=%li, sd->size=%zu)",
			    sd->name, (long int)(ins->roof - cur),
			    sd->size);
	}

	passert(dest_size >= sd->size);
	const uint8_t *roof = cur + sd->size; /* may be changed by a length field */
	bool length_field_found = false;
	uint8_t *dest = dest_start;
	uint8_t *dest_end = dest + dest_size;
	bool immediate = false;

	for (field_desc *fp = sd->fields; fp->field_type != ft_end; fp++) {

		/* field ends within PBS? */
		passert(cur + fp->size <= ins->roof);
		/* field ends within struct? */
		passert(cur + fp->size <= ins->cur + sd->size);
		/* field ends within dest */
		passert(dest + fp->size <= dest_end);

#if 0
		dbg("%td (%td) '%s'.'%s' %d bytes ",
		    (cur - ins->cur), (cur - ins->start),
		    sd->name, fp->name,
		    fp->size);
#endif

		switch (fp->field_type) {
		case ft_zig: /* should be zero, ignore if not - liberal in what to receive, strict to send */
			for (size_t i = fp->size; i != 0; i--) {
				uint8_t byte = *cur;
				if (byte != 0) {
					/*
					 * We cannot zeroize it, it
					 * would break our hash
					 * calculation.
					 *
					 * XXX: bytes used for hashing
					 * can't be zero/ignore.
					 */
					dbg("byte at offset %td (%td) of '%s'.'%s' is 0x%02"PRIx8" but should have been zero (ignored)",
					    (cur - ins->cur),
					    (cur - ins->start),
					    sd->name, fp->name,
					    byte);
				}
				cur++;
				*dest++ = '\0';
			}
			break;

		case ft_nat:            /* natural number (may be 0) */
		case ft_len:            /* length of this struct and any following crud */
		case ft_lv:             /* length/value field of attribute */
		case ft_enum:           /* value from an enumeration */
		case ft_loose_enum:     /* value from an enumeration with only some names known */
		case ft_mnpc:
		case ft_pnpc:
		case ft_lss:
		case ft_loose_enum_enum:	/* value from an enumeration with partial name table based on previous enum */
		case ft_af_enum:        /* Attribute Format + value from an enumeration */
		case ft_af_loose_enum:  /* Attribute Format + value from an enumeration */
		case ft_lset:           /* bits representing set */
		{
			uintmax_t n = 0;

			/* Reportedly fails on arm, see bug #775 */
			for (size_t i = fp->size; i != 0; i--)
				n = (n << BITS_IN_BYTE) | *cur++;

			switch (fp->field_type) {
			case ft_len:    /* length of this struct and any following crud */
			case ft_lv:     /* length/value field of attribute */
			{
				passert(!length_field_found && obj_pbs != NULL);
				length_field_found = true;
				size_t len = fp->field_type ==
					ft_len ? n :
					immediate ? sd->size :
					n + sd->size;

				if (len < sd->size) {
					return diag("%zd-byte %s of %s is smaller than minimum",
						    len, fp->name, sd->name);
				}

				if (pbs_left(ins) < len) {
					return diag("%zd-byte %s of %s is larger than can fit",
						    len, fp->name, sd->name);
				}

				roof = ins->cur + len;
				break;
			}

			case ft_af_loose_enum: /* Attribute Format + value from an enumeration */
			case ft_af_enum: /* Attribute Format + value from an enumeration */
			{
				immediate = ((n & ISAKMP_ATTR_AF_MASK) ==
					     ISAKMP_ATTR_AF_TV);
				/*
				 * Lookup fp->desc using N and
				 * not LAST_ENUM.  Only when N
				 * (value or AF+value) is
				 * found is it acceptable.
				 */
				enum_buf b;
				if (fp->field_type == ft_af_enum &&
				    !enum_name(fp->desc, n, &b)) {
					return diag("%s of %s has an unknown value: %s%ju (0x%jx)",
						    fp->name, sd->name,
						    immediate ? "AF+" : "",
						    n & ~ISAKMP_ATTR_AF_MASK, n);
				}
				break;
			}

			case ft_enum:   /* value from an enumeration */
			{
				enum_buf b;
				if (!enum_name(fp->desc, n, &b)) {
					return diag("%s of %s has an unknown value: %ju (0x%jx)",
						    fp->name, sd->name,
						    n, n);
				}
				break;
			}

			case ft_loose_enum:     /* value from an enumeration with only some names known */
			case ft_mnpc:
			case ft_pnpc:
			case ft_lss:
				break;

			case ft_loose_enum_enum:	/* value from an enumeration with partial name table based on previous enum */
				break;

			case ft_lset:            /* bits representing set */
				if (!test_lset(fp->desc, n)) {
					lset_buf lb;
					return diag("bitset %s of %s has unknown member(s): %s (0x%ju)",
						    fp->name, sd->name,
						    str_lset(fp->desc, n, &lb),
						    n);
				}
				break;

			default:
				break;
			}

			/* deposit the value in the struct */
			switch (fp->size) {
			case 8 / BITS_IN_BYTE:
				*(uint8_t *)dest = n;
				break;
			case 16 / BITS_IN_BYTE:
				*(uint16_t *)dest = n;
				break;
			case 32 / BITS_IN_BYTE:
				*(uint32_t *)dest = n;
				break;
			default:
				bad_case(fp->size);
			}
			dest += fp->size;
			break;
		}

		case ft_raw: /* bytes to be left in network-order */
			for (size_t i = fp->size; i != 0; i--)
				*dest++ = *cur++;
			break;

		case ft_end: /* end of field list */
			llog_passert(&global_logger, HERE, "should not be here");
		default:
			bad_case(fp->field_type);
		}
	}

	passert(cur == ins->cur + sd->size);
	if (obj_pbs != NULL) {
		passert(length_field_found);
		/*
		 * It starts at the same origin; but is truncated and
		 * cursor skips header
		 */
		*obj_pbs = pbs_in_from_shunk(shunk2(ins->cur/*not CUR*/, roof - ins->cur), sd->name);
		obj_pbs->cur = cur; /* skip header */
		/* back link */
		obj_pbs->container = ins;
		obj_pbs->desc = sd;
	}
	ins->cur = roof;
	if (DBGP(DBG_BASE)) {
		DBG_prefix_print_pbs_in_struct(ins, "parse ",
					       dest_start, sd,
					       true);
	}
	return NULL;
}

/* return first byte. If no byte return -1 */
int pbs_peek_byte(const struct pbs_in *ins)
{
	return pbs_left(ins) == 0 ? -1 : ins->cur[0];
}

diag_t pbs_in_shunk(struct pbs_in *ins, size_t len, shunk_t *shunk, const char *name)
{
	if (pbs_left(ins) < len) {
		return diag("less than %zu bytes left to get %s from %s",
			    len, name, ins->name);
	}

	*shunk = (shunk_t) {
		.ptr = ins->cur,
		.len = len,
	};
	if (DBGP(DBG_BASE)) {
		DBG_log("parsing %zu raw bytes of %s into %s",
			len, ins->name, name);
		DBG_dump_hunk(NULL, *shunk);
	}
	ins->cur += len;
	return NULL;
}

diag_t pbs_in_bytes(struct pbs_in *ins, void *bytes, size_t len, const char *name)
{
	shunk_t shunk = NULL_HUNK;
	diag_t d = pbs_in_shunk(ins, len, &shunk, name);
	if (d != NULL) {
		return d;
	}

	if (bytes != NULL && shunk.len != 0) {
		memcpy(bytes, shunk.ptr, shunk.len);
	}
	return NULL;
}

/*
 * Check IKEv2's Last Substructure field.
 */

static void update_last_substructure(struct pbs_out *outs,
				     struct_desc *sd, field_desc *fp,
				     const uint8_t *inp, uint8_t *cur)
{
	/*
	 * The containing structure should be expecting substructures.
	 */
	passert(fp->size == 1);
	pexpect(outs->desc->nsst != 0);
	uint8_t n = *inp;
	pexpect(n == 0 || n == outs->desc->nsst);
	*cur = n;
	/*
	 * Since there's a previous substructure, it can no longer be
	 * last.  Check/set its last substructure field to its type.
	 */
	if (outs->last_substructure.loc != NULL) {
		esb_buf locb;
		esb_buf nsstb;
		dbg("last substructure: checking '%s'.'%s'.'%s' containing %s (0x%x) is %s (0x%x)",
		    /* '%s'.'%s'.'%s' */
		    outs->desc->name,
		    outs->last_substructure.sd->name,
		    outs->last_substructure.fp->name,
		    /* containing ... */
		    str_enum(outs->last_substructure.fp->desc,
			      outs->last_substructure.loc[0], &locb),
		    outs->last_substructure.loc[0],
		    /* is ... */
		    str_enum(outs->last_substructure.fp->desc,
			      outs->desc->nsst, &nsstb),
		    outs->desc->nsst);
		pexpect(outs->last_substructure.loc[0] == outs->desc->nsst);
	}
	/*
	 * Now save the location of this Last Substructure.
	 */
	dbg("last substructure: saving location '%s'.'%s'.'%s'",
	     outs->desc->name, sd->name, fp->name);
	outs->last_substructure.loc = cur;
	outs->last_substructure.sd = sd;
	outs->last_substructure.fp = fp;
}

static void close_last_substructure(struct pbs_out *pbs)
{
	if (pbs->last_substructure.loc != NULL) {
		dbg("last substructure: checking '%s'.'%s'.'%s' is 0",
		     pbs->desc->name,
		     pbs->last_substructure.sd->name,
		     pbs->last_substructure.fp->name);
		pexpect(pbs->desc->nsst != 0);
		pexpect(pbs->last_substructure.loc[0] == 0);
#if 0
	} else {
		/* XXX: too strong, rejects empty? */
		pexpect(pbs->desc->nsst == 0);
#endif
	}
}

/*
 * Next Payload Chain
 */

static void start_next_payload_chain(struct pbs_out *outs,
				     struct_desc *sd, field_desc *fp,
				     const uint8_t *inp, uint8_t *cur)
{
	passert(fp->size == 1);
	dbg("next payload chain: saving message location '%s'.'%s'",
	     sd->name, fp->name);
	outs->next_payload_chain.loc = cur;
	outs->next_payload_chain.sd = sd;
	outs->next_payload_chain.fp = fp;
	uint8_t n = *inp;
	if (n != ISAKMP_NEXT_NONE) {
		esb_buf npb;
		llog_pexpect(outs->logger, HERE,
			     "next payload chain: ignoring supplied '%s'.'%s' value %d:%s",
			     sd->name, fp->name, n,
			     str_enum(fp->desc, n, &npb));
		n = ISAKMP_NEXT_NONE;
	}
	*cur = n;
}

static void update_next_payload_chain(struct pbs_out *outs,
				      struct_desc *sd, field_desc *fp,
				      const uint8_t *inp, uint8_t *cur)
{
	passert(fp->size == 1);
	passert(sd->pt != ISAKMP_NEXT_NONE);

	/*
	 * Normally only comes after the header and ft_mnpc.  However,
	 * as part of authenticating, IKEv1 fakes up a PBS containing
	 * just the "Identification Payload".
	 */
	if (outs->container == NULL) {
		esb_buf npb;
		dbg("next payload chain: no previous for current %s (%d:%s); assumed to be fake",
		    sd->name, sd->pt, str_enum(fp->desc, sd->pt, &npb));
		return;
	}

	/*
	 * Find the message (packet) PBS containing the next payload
	 * chain pointers initialized by start_next_payload_chain().
	 * Since there is a single chain, running through the message,
	 * this is stored in the outermost PBS.
	 *
	 * XXX: don't try to be all fancy and copy back values; could
	 * use an outs->message pointer; but since nesting is minimal
	 * this isn't really urgent
	 */
	struct pbs_out *message = outs->container;
	passert(message != NULL);
	while (message->container != NULL) {
		message = message->container;
	}
	passert(message->next_payload_chain.loc != NULL);
	passert(message->next_payload_chain.sd != NULL);
	passert(message->next_payload_chain.fp != NULL);
	pexpect(*message->next_payload_chain.loc == ISAKMP_NEXT_NONE);

	/*
	 * Initialize this payload's next payload chain
	 */
	uint8_t n = *inp;
	if (sd->pt == ISAKMP_NEXT_v2SKF) {
		/*
		 * For v2SKF (IKEv2 secured fragments) assume the
		 * header next payload type is correct.  The
		 * fragmenting code sets it to the payload type of the
		 * unfragmented SK message's first payload.  Something
		 * this code isn't going to know.
		 *
		 * This works because v2SKF messages never have more
		 * than one payload.
		 */
		esb_buf npb;
		dbg("next payload chain: using supplied v2SKF '%s'.'%s' value %d:%s",
		     sd->name, fp->name, n,
		     str_enum(fp->desc, n, &npb));
	} else if (n != ISAKMP_NEXT_NONE) {
		esb_buf npb;
		llog_pexpect(outs->logger, HERE,
			     "next payload chain: ignoring supplied '%s'.'%s' value %d:%s",
			     sd->name, fp->name, n,
			     str_enum(fp->desc, n, &npb));
		n = ISAKMP_NEXT_NONE;
	}
	*cur = n;

	/* update previous struct's next payload type field */
	esb_buf npb;
	dbg("next payload chain: setting previous '%s'.'%s' to current %s (%d:%s)",
	     message->next_payload_chain.sd->name,
	     message->next_payload_chain.fp->name,
	     sd->name, sd->pt, str_enum(fp->desc, sd->pt, &npb));
	*message->next_payload_chain.loc = sd->pt;

	/* save new */
	dbg("next payload chain: saving location '%s'.'%s' in '%s'",
	    sd->name, fp->name, message->name);
	message->next_payload_chain.loc = cur;
	message->next_payload_chain.sd = sd;
	message->next_payload_chain.fp = fp;
}

/* "emit" a host struct into a network packet.
 *
 * This code assumes that the network and host structure
 * members have the same alignment and size!  This requires
 * that all padding be explicit.
 *
 * If obj_pbs is non-NULL, its pbs describes a new output stream set up
 * to contain the object.  The cursor will be left at the variable part.
 * This new stream must subsequently be finalized by close_pbs_out().
 *
 * The value of any field of type ft_len is computed, not taken
 * from the input struct.  The length is actually filled in when
 * the object's output stream is finalized.  If obj_pbs is NULL,
 * finalization is done by out_struct before it returns.
 *
 * This routine returns TRUE iff it succeeds.
 */

static bool pbs_out_number(struct pbs_out *outs, struct_desc *sd,
			   const uint8_t **inp,
			   uint8_t **cur,
			   field_desc *fp,
			   bool *immediate)
{

	uint32_t n;

	switch (fp->size) {
	case 8 / BITS_IN_BYTE:
		n = *(const uint8_t *)(*inp);
		break;
	case 16 / BITS_IN_BYTE:
		n = *(const uint16_t *)(*inp);
		break;
	case 32 / BITS_IN_BYTE:
		n = *(const uint32_t *)(*inp);
		break;
	default:
		bad_case(fp->size);
	}

	switch (fp->field_type) {

	case ft_af_loose_enum: /* Attribute Format + value from an enumeration */
	case ft_af_enum: /* Attribute Format + value from an enumeration */
	{
		*immediate = ((n & ISAKMP_ATTR_AF_MASK) == ISAKMP_ATTR_AF_TV);
		enum_buf b;
		if (fp->field_type == ft_af_enum &&
		    !enum_name(fp->desc, n, &b)) {
#define MSG "%s of %s has an unknown value: 0x%x+%" PRIu32 " (0x%" PRIx32 ")", \
				fp->name,				\
				sd->name,				\
				n & ISAKMP_ATTR_AF_MASK,		\
				n & ~ISAKMP_ATTR_AF_MASK,		\
				n
			if (!impair.emitting) {
				llog_pexpect(outs->logger, HERE, MSG);
				return false;
			}
			llog(RC_LOG, outs->logger, "IMPAIR: emitting "MSG);
		}
		break;
	}

	case ft_enum:   /* value from an enumeration */
	{
		enum_buf b;
		if (!enum_name(fp->desc, n, &b)) {
			if (!impair.emitting) {
				llog_pexpect(outs->logger, HERE,
					     "%s of %s has an unknown value: %" PRIu32 " (0x%" PRIx32 ")",
					     fp->name, sd->name,
					     n, n);
				return false;
			}
			llog(RC_LOG, outs->logger,
			     "IMPAIR: %s of %s has an unknown value: %" PRIu32 " (0x%" PRIx32 ")",
			     fp->name, sd->name, n, n);
		}
		break;
	}

	case ft_loose_enum:     /* value from an enumeration with only some names known */
		break;

	case ft_loose_enum_enum:	/* value from an enumeration with partial name table based on previous enum */
		break;

	case ft_lset:           /* bits representing set */
		if (!test_lset(fp->desc, n)) {
			if (!impair.emitting) {
				lset_buf lb;
				llog_pexpect(outs->logger, HERE,
					     "bitset %s of %s has unknown member(s): %s (0x%" PRIx32 ")",
					     fp->name, sd->name,
					     str_lset(fp->desc, n, &lb),
					     n);
				return false;
			}
			lset_buf lb;
			llog(RC_LOG, outs->logger,
			     "IMPAIR: bitset %s of %s has unknown member(s): %s (0x%" PRIx32 ")",
			     fp->name, sd->name,
			     str_lset(fp->desc, n, &lb),
			     n);
		}
		break;

	default:
		break;
	}

	/* emit i low-order bytes of n in network order */
	for (int i = fp->size - 1; i >= 0; i--) {
		(*cur)[i] = (uint8_t)n;
		n >>= BITS_IN_BYTE;
	}
	(*inp) += fp->size;
	(*cur) += fp->size;
	return true;
}

bool pbs_out_struct(struct pbs_out *outs, struct_desc *sd,
		    const void *struct_ptr, size_t struct_size,
		    struct pbs_out *obj_pbs)
{
	const u_int8_t *inp = struct_ptr;
	u_int8_t *cur = outs->cur;

	passert(struct_size == 0 || struct_size >= sd->size);

	if (DBGP(DBG_BASE)) {
		DBG_prefix_print_pbs_out_struct(outs, "emit ", struct_ptr, sd, obj_pbs == NULL);
	}

	if (outs->roof - cur < (ptrdiff_t)sd->size) {
		llog_pexpect(outs->logger, HERE,
			     "not enough room left in output packet to place %s", sd->name);
		return false;
	}


	bool immediate = false;

	/* new child stream for portion of payload after this struct */
	struct pbs_out obj = {
		.container = outs,
		.desc = sd,
		.name = sd->name,
		.logger = outs->logger,

		/* until a length field is discovered */
		/* .lenfld = NULL, */
		/* .lenfld_desc = NULL, */

		/* until an ft_mnpc field is discovered */
		/* message.previous_np = {0}, */

		/* until an ft_lss is discovered */
		/* .last_substructure = {0}, */
	};

	for (field_desc *fp = sd->fields; ; fp++) {
		size_t i = fp->size;

		/* make sure that there is space for the next structure element */
		passert(outs->roof - cur >= (ptrdiff_t)i);

		/* verify that the spot is correct in the offset */
		passert(cur - outs->cur <= (ptrdiff_t)(sd->size - i));

		/* verify that we are at the right place in the input structure */
		passert(inp - (cur - outs->cur) == struct_ptr);

		ldbgf(DBG_TMI, outs->logger, "out_struct: %d %s",
		      (int) (cur - outs->cur),
		      fp->name == NULL ? "<end>" : fp->name);

		switch (fp->field_type) {
		case ft_zig: /* zero */
		{
			uint8_t byte;
			if (impair.send_nonzero_reserved) {
				byte = ISAKMP_PAYLOAD_FLAG_LIBRESWAN_BOGUS;
				llog(RC_LOG, outs->logger,
				     "IMPAIR: setting zero/ignore field to 0x%02x", byte);
			} else {
				byte = 0;
			}
			memset(cur, byte, i);
			inp += i;
			cur += i;
			break;
		}

		case ft_mnpc:
			start_next_payload_chain(outs, sd, fp,
						 inp, cur);
			inp += fp->size;
			cur += fp->size;
			break;

		case ft_pnpc:
			update_next_payload_chain(outs, sd, fp,
						  inp, cur);
			inp += fp->size;
			cur += fp->size;
			break;

		case ft_lss:
			update_last_substructure(outs, sd, fp,
						 inp, cur);
			inp += fp->size;
			cur += fp->size;
			break;

		case ft_len:            /* length of this struct and any following crud */
		case ft_lv:             /* length/value field of attribute */
			if (!immediate) {
				/* We can't check the length because it must
				 * be filled in after variable part is supplied.
				 * We do record where this is so that it can be
				 * filled in by a subsequent close_pbs_out().
				 */
				passert(obj.lenfld == NULL);    /* only one ft_len allowed */
				obj.lenfld = cur;
				obj.lenfld_desc = fp;

				/* fill with crap so failure to overwrite will be noticed */
				memset(cur, 0xFA, i);

				inp += i;
				cur += i;
				break;
			}

			/* immediate form is just like a number */
			if (!pbs_out_number(outs, sd, &inp, &cur, fp, &immediate)) {
				/* already logged */
				return false;
			}
			break;

		case ft_nat:            /* natural number (may be 0) */
		case ft_enum:           /* value from an enumeration */
		case ft_loose_enum:     /* value from an enumeration with only some names known */
		case ft_loose_enum_enum:	/* value from an enumeration with partial name table based on previous enum */
		case ft_af_enum:        /* Attribute Format + value from an enumeration */
		case ft_af_loose_enum:  /* Attribute Format + value from an enumeration */
		case ft_lset:           /* bits representing set */
			if (!pbs_out_number(outs, sd, &inp, &cur, fp, &immediate)) {
				/* already logged */
				return false;
			}
			break;

		case ft_raw: /* bytes to be left in network-order */
			for (; i != 0; i--)
				*cur++ = *inp++;
			break;

		case ft_end: /* end of field list */
			passert(cur == outs->cur + sd->size);

			obj.start = outs->cur;
			obj.cur = cur;
			obj.roof = outs->roof; /* limit of possible */
			/* obj.lenfld* and obj.previous_np* already set */

			if (obj_pbs == NULL) {
				close_pbs_out(&obj); /* fill in length field, if any */
			} else {
				/* We set outs->cur to outs->roof so that
				 * any attempt to output something into outs
				 * before obj is closed will trigger an error.
				 */
				outs->cur = outs->roof;

				*obj_pbs = obj;
			}
			return true;

		default:
			bad_case(fp->field_type);
		}
	}

	/* never reached!?! */
}

bool out_struct(const void *struct_ptr, struct_desc *sd,
		struct pbs_out *outs, struct pbs_out *obj_pbs)
{
	return pbs_out_struct(outs, sd, struct_ptr, 0, obj_pbs);
}

bool ikev1_out_generic(struct_desc *sd,
		       struct pbs_out *outs, struct pbs_out *obj_pbs)
{
	passert(sd->fields == isag_fields);
	passert(sd->pt != ISAKMP_NEXT_NONE);
	struct isakmp_generic gen = {
		.isag_np = 0,
	};
	return out_struct(&gen, sd, outs, obj_pbs);
}

bool ikev1_out_generic_raw(struct_desc *sd,
		     struct pbs_out *outs, const void *bytes, size_t len,
		     const char *name)
{
	struct pbs_out pbs;

	if (!ikev1_out_generic(sd, outs, &pbs)) {
		return false;
	}
	if (!pbs_out_raw(&pbs, bytes, len, name)) {
		/* already logged */
		return false;
	}

	close_pbs_out(&pbs);
	return true;
}

static bool space_for(struct pbs_out *outs, size_t len, const char *what, const char *name, where_t where)
{
	/*
	 * Is there more than enough space for LEN?  The +1 is to
	 * ensure that there is always one byte of space spare.
	 */
	if (pbs_left(outs) >= len + 1) {
		/* logged by caller */
		return true;
	}

	if (pbs_left(outs) > 0) {
		/*
		 * Overflowing.
		 */
		pexpect(pbs_left(outs) <= len);
		llog_pexpect(outs->logger, where,
			     "buffer is full; unable to emit %zu %s bytes of %s into %s",
			     len, what, name, outs->name);
		/* overflow the buffer */
		outs->cur += pbs_left(outs);
		return false;
	}

	/*
	 * Already overflowing.
	 */
	pexpect(pbs_left(outs) == 0);
	llog_pexpect(outs->logger, where,
		     "buffer is overflowing; unable to emit %zu %s bytes of %s into %s",
		     len, what, name, outs->name);
	return false;

}

bool pbs_out_raw(struct pbs_out *outs, const void *bytes, size_t len, const char *name)
{
	if (!space_for(outs, len, "raw", name, HERE)) {
		/* already logged */
		return false;
	}

	if (DBGP(DBG_BASE)) {
		DBG_log("emitting %zu raw bytes of %s into %s", len, name, outs->name);
		if (len > 16) { /* arbitrary */
			DBG_dump(NULL, bytes, len);
		} else {
			LLOG_JAMBUF(DEBUG_STREAM, &global_logger, buf) {
				jam(buf, "%s: ", name);
				jam_dump_bytes(buf, bytes, len);
			}
		}
	}

	memcpy(outs->cur, bytes, len);
	outs->cur += len;
	return true;
}

bool pbs_out_repeated_byte(struct pbs_out *outs, uint8_t byte, size_t len, const char *name)
{
	if (!space_for(outs, len, "repeated", name, HERE)) {
		/* already logged */
		return false;
	}

	dbg("emitting %"PRIu8" as %zu bytes of %s into %s", byte, len, name, outs->name);
	memset(outs->cur, byte, len);
	outs->cur += len;
	return true;
}

bool pbs_out_zero(struct pbs_out *outs, size_t len, const char *name)
{
	if (!space_for(outs, len, "zero", name, HERE)) {
		/* already logged */
		return false;
	}

	dbg("emitting %zu zero bytes of %s into %s", len, name, outs->name);
	memset(outs->cur, 0, len);
	outs->cur += len;
	return true;
}

/*
 * Reply messages are built in this nasty evil global buffer.
 *
 * Only one packet can be built at a time.  That should be ok as
 * packets are only built on the main thread and code and a packet is
 * created using a single operation.
 *
 * In the good old days code would partially construct a packet,
 * wonder off to do crypto and process other packets, and then assume
 * things could be picked up where they were left off.  Code to make
 * that work (saving restoring the buffer, re-initializing the buffer
 * in strange places, ....) has all been removed.
 *
 * Something else that should go is global access to REPLY_STREAM.
 * Instead all code should use open_reply_stream() and a reference
 * with only local scope.  This should reduce the odds of code
 * meddling in reply_stream on the sly.
 *
 * Another possibility is to move the buffer onto the stack.  However,
 * the PBS is 64K and that isn't so good for small machines.  Then
 * again the send.[hc] and demux[hc] code both allocate 64K stack
 * buffers already.  Oops.
 */

uint8_t reply_buffer[MAX_OUTPUT_UDP_SIZE];

/*
 * close_pbs_out: record current length and check previous_NP
 *
 * Note: currently, this may be repeated any number of times;
 * the last call's setting of the length wins.
 */

bool close_pbs_out(struct pbs_out *pbs)
{
	PASSERT(pbs->logger, pbs->logger != NULL);

	if (pbs->lenfld != NULL) {
		size_t len = (pbs->cur - pbs->start);

		if (pbs->lenfld_desc->field_type == ft_lv)
			len -= sizeof(struct isakmp_attribute);

		ldbg(pbs->logger, "emitting length of %s: %zu", pbs->name, len);

		/*
		 * Emit SIZE octets of (host) length in network order.
		 */
		unsigned size = pbs->lenfld_desc->size;
		PASSERT(pbs->logger, size > 0);
		raw_hton(len, pbs->lenfld, size);
	}

	/* if there is one */
	close_last_substructure(pbs);

	if (pbs->container != NULL)
		pbs->container->cur = pbs->cur; /* pass space utilization up */
	/* don't log against a closed pbs */
	pbs->logger = NULL;
	return true;
}

diag_t pbs_in_address(struct pbs_in *input_pbs,
		      ip_address *address, const struct ip_info *ipv,
		      const char *what)
{
	switch (ipv->af) {
	case AF_INET:
	{
		struct in_addr ip;
		diag_t d = pbs_in_thing(input_pbs, ip, what);
		if (d != NULL) {
			*address = unset_address;
			return d;
		}
		*address = address_from_in_addr(&ip);
		return NULL;
	}
	case AF_INET6:
	{
		struct in6_addr ip;
		diag_t d = pbs_in_thing(input_pbs, ip, what);
		if (d != NULL) {
			*address = unset_address;
			return d;
		}
		*address = address_from_in6_addr(&ip);
		return NULL;
	}
	default:
		bad_case(ipv->af);
	}
}

bool pbs_out_address(struct pbs_out *out_pbs, const ip_address address, const char *what)
{
	shunk_t as = address_as_shunk(&address);
	if (!pbs_out_hunk(out_pbs, as, what)) {
		/* already logged */
		return false;
	}
	return true;
}