Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
/*	$KAME: dccp_usrreq.c,v 1.67 2005/11/03 16:05:04 nishida Exp $	*/
/*	$NetBSD: dccp_usrreq.c,v 1.26 2022/11/04 09:01:53 ozaki-r Exp $ */

/*
 * Copyright (c) 2003 Joacim Häggmark, Magnus Erixzon, Nils-Erik Mattsson 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Id: dccp_usrreq.c,v 1.47 2003/07/31 11:23:08 joahag-9 Exp
 */

/*
 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dccp_usrreq.c,v 1.26 2022/11/04 09:01:53 ozaki-r Exp $");

#ifdef _KERNEL_OPT
#include "opt_inet.h"
#include "opt_dccp.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/domain.h>
#include <sys/kernel.h>
#include <sys/pool.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/protosw.h>
#include <sys/signalvar.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/mutex.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/queue.h>

#include <net/if.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/in_var.h>
#ifdef INET6
#include <netinet/ip6.h>
#endif
#include <netinet/ip_icmp.h>
#include <netinet/icmp_var.h>
#include <netinet/ip_var.h>
#ifdef INET6
#include <netinet6/in6_pcb.h>
#include <netinet6/ip6_var.h>
#include <netinet6/dccp6_var.h>
#endif
#include <netinet/dccp.h>
#include <netinet/dccp_var.h>
#include <netinet/dccp_cc_sw.h>

#define DEFAULT_CCID 2

#define INP_INFO_LOCK_INIT(x,y)
#define INP_INFO_WLOCK(x)
#define INP_INFO_WUNLOCK(x)
#define INP_INFO_RLOCK(x)
#define INP_INFO_RUNLOCK(x)
#define INP_LOCK(x)
#define IN6P_LOCK(x)
#define INP_UNLOCK(x)
#define IN6P_UNLOCK(x)

/* Congestion control switch table */
extern struct dccp_cc_sw cc_sw[];

int	dccp_log_in_vain = 1;
int	dccp_do_feature_nego = 1;

struct	inpcbhead dccpb;		/* from dccp_var.h */
#ifdef __FreeBSD__
struct	inpcbinfo dccpbinfo;
#else
struct	inpcbtable dccpbtable;
#endif

#ifndef DCCPBHASHSIZE
#define DCCPBHASHSIZE 16
#endif

struct	pool dccpcb_pool;

u_long	dccp_sendspace = 32768;
u_long	dccp_recvspace = 65536;

struct	dccpstat dccpstat;	/* from dccp_var.h */

static struct dccpcb * dccp_close(struct dccpcb *);
static int dccp_disconnect2(struct dccpcb *);
int dccp_get_option(char *, int, int, char *,int);
void dccp_parse_options(struct dccpcb *, char *, int);
int dccp_remove_feature(struct dccpcb *, u_int8_t, u_int8_t);
int dccp_add_feature_option(struct dccpcb *, u_int8_t, u_int8_t, char *, u_int8_t);
void dccp_feature_neg(struct dccpcb *, u_int8_t, u_int8_t, u_int8_t, char *);
void dccp_close_t(void *);
void dccp_timewait_t(void *);

/* Ack Vector functions */
#define DCCP_VECTORSIZE 512 /* initial ack and cwnd-vector size. Multiple of 8 ! */
void dccp_use_ackvector(struct dccpcb *);
void dccp_update_ackvector(struct dccpcb *, u_int64_t);
void dccp_increment_ackvector(struct dccpcb *, u_int64_t);
u_int16_t dccp_generate_ackvector(struct dccpcb *, u_char *);
u_char dccp_ackvector_state(struct dccpcb *, u_int64_t);

/*
 * DCCP initialization
 */
void
dccp_init(void)
{
	pool_init(&dccpcb_pool, sizeof(struct dccpcb), 0, 0, 0, "dccpcbpl",
		  NULL, IPL_SOFTNET);

	inpcb_init(&dccpbtable, DCCPBHASHSIZE, DCCPBHASHSIZE);
}

void
dccp_input(struct mbuf *m, int off, int proto)
{
	int iphlen;
	struct ip *ip = NULL;
	struct dccphdr *dh;
	struct dccplhdr *dlh;
	struct inpcb *inp = NULL, *oinp = NULL;
	struct dccpcb *dp;
	struct ipovly *ipov = NULL;
	struct dccp_requesthdr *drqh;
	struct dccp_ackhdr *dah = NULL;
	struct dccp_acklhdr *dalh = NULL;
	struct dccp_resethdr *drth;
	struct socket *so;
	u_char *optp = NULL;
	struct mbuf *opts = 0;
	int len, data_off, extrah_len, optlen;
	/*struct ip save_ip;*/
	char options[DCCP_MAX_OPTIONS];
	char test[2];
	u_int32_t cslen;
	dccp_seq seqnr, low_seqnr, high_seqnr;
	int isipv6 = 0;
	int is_shortseq; /* Is this shortseq packet? */ 
#ifdef INET6
	struct ip6_hdr *ip6 = NULL;
#endif

	iphlen = off;

	DCCP_DEBUG((LOG_INFO, "Got DCCP packet!\n"));

	dccpstat.dccps_ipackets++;
	dccpstat.dccps_ibytes += m->m_pkthdr.len;

#ifdef INET6
	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
#endif

#ifdef INET6
	if (isipv6) {
		DCCP_DEBUG((LOG_INFO, "Got DCCP ipv6 packet, iphlen = %u!\n", iphlen));
		ip6 = mtod(m, struct ip6_hdr *);
		M_REGION_GET(dh, struct dccphdr *, m, iphlen, sizeof(*dh));
		if (dh == NULL) {
			dccpstat.dccps_badlen++;
			return;
		}
	} else
#endif
	{
		/*
		 * Strip IP options, if any; should skip this,
		 * make available to user, and use on returned packets,
		 * but we don't yet have a way to check the checksum
		 * with options still present.
		 */
		if (iphlen > sizeof (struct ip)) {
			DCCP_DEBUG((LOG_INFO, "Need to strip options\n"));
#if 0				/* XXX */
			ip_stripoptions(m, (struct mbuf *)0);
#endif
			iphlen = sizeof(struct ip);
		}

		/*
		 * Get IP and DCCP header together in first mbuf.
		 */
		ip = mtod(m, struct ip *);
		M_REGION_GET(dh, struct dccphdr *, m, iphlen, sizeof(*dh));
		if (dh == NULL) {
			dccpstat.dccps_badlen++;
			return;
		}
	}
	dlh = (struct dccplhdr*)dh;
	is_shortseq = !dh->dh_x;
	
	if (!is_shortseq) {
		DCCP_DEBUG((LOG_INFO, 
		"Header info: cslen = %u, off = %u, type = %u, reserved = %u, seq = %u.%lu\n",
			 dlh->dh_cscov, dlh->dh_off, dlh->dh_type, dlh->dh_res, ntohs(dlh->dh_seq),
			 (unsigned long)ntohl(dlh->dh_seq2)));
	} else {
		DCCP_DEBUG((LOG_INFO, 
		"Header info(short): cslen = %u, off = %u, type = %u, reserved = %u, seq = %u\n", 
			dh->dh_cscov, dh->dh_off, dh->dh_type, dh->dh_res, ntohl(dh->dh_seq)));
	}

	/*
	 * Make mbuf data length reflect DCCP length.
	 * If not enough data to reflect DCCP length, drop.
	 */

#ifdef INET6
	if (isipv6)
		len = m->m_pkthdr.len - off;
	else
#endif
	{
		len = ntohs(ip->ip_len);
		len -= ip->ip_hl << 2;
	}

	if (len < sizeof(struct dccphdr)) {
		DCCP_DEBUG((LOG_INFO, "Dropping DCCP packet!\n"));
		dccpstat.dccps_badlen++;
		goto badunlocked;
	}
	/*
	 * Save a copy of the IP header in case we want restore it
	 * for sending a DCCP reset packet in response.
	 */
	if (!isipv6) {
		/*save_ip = *ip;*/
		ipov = (struct ipovly *)ip;
	}

	if (dh->dh_cscov == 0) {
		cslen = len;
	} else {
		cslen = dh->dh_off * 4 + (dh->dh_cscov - 1) * 4;
		if (cslen > len)
			cslen = len;
	}
	
	/*
	 * Checksum extended DCCP header and data.
	 */
	
#ifdef INET6
	if (isipv6) {
		if (in6_cksum(m, IPPROTO_DCCP, off, cslen) != 0) {
			dccpstat.dccps_badsum++;
			goto badunlocked;
		}
	} else
#endif
	{
		memset(ipov->ih_x1, 0, sizeof(ipov->ih_x1));
		ip->ip_len = htons(m->m_pkthdr.len);
		dh->dh_sum = in4_cksum(m, IPPROTO_DCCP, off, cslen);

		if (dh->dh_sum) {
			dccpstat.dccps_badsum++;
			goto badunlocked;
		}
	}

	INP_INFO_WLOCK(&dccpbinfo);

	/*
	 * Locate pcb for datagram.
	 */
#ifdef INET6
	if (isipv6) {
		inp = in6pcb_lookup(&dccpbtable, &ip6->ip6_src,
		    dh->dh_sport, &ip6->ip6_dst, dh->dh_dport, 0, 0);
		if (inp == NULL) {
			/* XXX stats increment? */
			inp = in6pcb_lookup_bound(&dccpbtable, &ip6->ip6_dst,
			    dh->dh_dport, 0);
		}
	} else
#endif
	{
		inp = inpcb_lookup(&dccpbtable, ip->ip_src,
		    dh->dh_sport, ip->ip_dst, dh->dh_dport, 0);
		if (inp == NULL) {
			/* XXX stats increment? */
			inp = inpcb_lookup_bound(&dccpbtable, ip->ip_dst,
			    dh->dh_dport);
		}
	}
	if (isipv6) {
		DCCP_DEBUG((LOG_INFO, "in6p=%p\n", inp));
	} else {
		DCCP_DEBUG((LOG_INFO, "inp=%p\n", inp));
	}

	if (inp == NULL) {
		if (dccp_log_in_vain) {
#ifdef INET6
			char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
#else
			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
#endif

#ifdef INET6
			if (isipv6) {
				char ip6buf[INET6_ADDRSTRLEN];
				strlcpy(dbuf, "[", sizeof dbuf);
				strlcat(dbuf, IN6_PRINT(ip6buf, &ip6->ip6_dst), sizeof dbuf);
				strlcat(dbuf, "]", sizeof dbuf);
				strlcpy(sbuf, "[", sizeof sbuf);
				strlcat(sbuf, IN6_PRINT(ip6buf, &ip6->ip6_src), sizeof sbuf);
				strlcat(sbuf, "]", sizeof sbuf);
			} else
#endif
			{
				strlcpy(dbuf, inet_ntoa(ip->ip_dst), sizeof dbuf);
				strlcpy(sbuf, inet_ntoa(ip->ip_src), sizeof sbuf);
			}
			log(LOG_INFO,
			    "Connection attempt to DCCP %s:%d from %s:%d\n",
			    dbuf, ntohs(dh->dh_dport), sbuf,
			    ntohs(dh->dh_sport));
		}
		dccpstat.dccps_noport++;

		/*
		 * We should send DCCP reset here but we can't call dccp_output
		 * since we have no dccpcb. A icmp unreachable works great but
		 * the specs says DCCP reset :(
		 *
		 * if (!isipv6) {
		 *	*ip = save_ip;
		 *	ip->ip_len += iphlen;
		 *	icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
		 * } 
		 */

		INP_INFO_WUNLOCK(&dccpbinfo);
		goto badunlocked;
	}
	INP_LOCK(inp);

	dp = intodccpcb(inp);
	if (dp == NULL) {
		INP_UNLOCK(inp);
		INP_INFO_WUNLOCK(&dccpbinfo);
		goto badunlocked;
	}

	if (dp->state == DCCPS_CLOSED) {
		DCCP_DEBUG((LOG_INFO, "We are in closed state, dropping packet and sending reset!\n"));
		if (dh->dh_type != DCCP_TYPE_RESET)
			dccp_output(dp, DCCP_TYPE_RESET + 2);
		INP_UNLOCK(inp);
		INP_INFO_WUNLOCK(&dccpbinfo);
		goto badunlocked;
	}

	so = inp->inp_socket;

	if (so->so_options & SO_ACCEPTCONN) {
		DCCP_DEBUG((LOG_INFO, "so->options & SO_ACCEPTCONN! dp->state = %i\n", dp->state));
		so = sonewconn(so, SS_ISCONNECTED);
		if (so == 0) {
			DCCP_DEBUG((LOG_INFO, "Error, sonewconn failed!\n"));
			INP_UNLOCK(inp);
			INP_INFO_WUNLOCK(&dccpbinfo);
			goto badunlocked;
		}

		/* INP_LOCK(inp); XXX */

		oinp = inp;

#ifdef INET6
		if (isipv6) {
			inp = sotoinpcb(so);
			in6p_laddr(inp) = ip6->ip6_dst;
			in6p_faddr(inp) = ip6->ip6_src;
			inp->inp_lport = dh->dh_dport;
			inp->inp_fport = dh->dh_sport;
			inpcb_set_state(inp, INP_CONNECTED);
		} else 
#endif
		{
			inp = sotoinpcb(so);
			in4p_laddr(inp) = ip->ip_dst;
			in4p_faddr(inp) = ip->ip_src;
			inp->inp_lport = dh->dh_dport;
			inp->inp_fport = dh->dh_sport;
		}

		if (!isipv6)
			inpcb_set_state(inp, INP_BOUND);

		dp = inp->inp_ppcb;

		dp->state = DCCPS_LISTEN;
		dp->who = DCCP_SERVER;
		dp->cslen = ((struct dccpcb *)oinp->inp_ppcb)->cslen;
		dp->avgpsize = ((struct dccpcb *)oinp->inp_ppcb)->avgpsize;
		dp->scode = ((struct dccpcb *)oinp->inp_ppcb)->scode;
		dp->seq_snd = (((u_int64_t)random() << 32) | random()) % 281474976710656LL;
		dp->ref_seq.hi = dp->seq_snd >> 24;
		dp->ref_seq.lo = (u_int64_t)(dp->seq_snd & 0xffffff);
		INP_UNLOCK(oinp);
		DCCP_DEBUG((LOG_INFO, "New dp = %u, dp->state = %u!\n", (int)dp, dp->state));
	}

	INP_INFO_WUNLOCK(&dccpbinfo);

	/*
	 * Check if sequence number is inside the loss window 
	 */
	if (!is_shortseq) { 
		DHDR_TO_DSEQ(seqnr, dlh);
	} else {
		/* shortseq */
		seqnr = CONVERT_TO_LONGSEQ((ntohl(dh->dh_seq) >> 8), dp->ref_pseq);
		DCCP_DEBUG((LOG_INFO, "short seq conversion %x,  %u %u\n", 
			ntohl(dh->dh_seq) >> 8, dp->ref_pseq.hi, dp->ref_pseq.lo));
	}

	DCCP_DEBUG((LOG_INFO, "Received DCCP packet with sequence number = %llu , gsn_rcv %llu\n", seqnr, dp->gsn_rcv));

	/* store ccval */
	dp->ccval = dh->dh_ccval;

	if (dp->gsn_rcv == 281474976710656LL) dp->gsn_rcv = seqnr;
	if (dp->gsn_rcv > (dp->loss_window / 4))
		low_seqnr = (dp->gsn_rcv - (dp->loss_window / 4)) % 281474976710656LL;
	else
		low_seqnr = 0ll;
	high_seqnr = (dp->gsn_rcv + (dp->loss_window / 4 * 3)) % 281474976710656LL;

	if (! (DCCP_SEQ_GT(seqnr, low_seqnr) && DCCP_SEQ_LT(seqnr, high_seqnr))) {
		dccpstat.dccps_badseq++;
		DCCP_DEBUG((LOG_INFO, "Received DCCP packet with bad sequence number = %llu (low_seqnr = %llu, high_seqnr = %llu)\n", seqnr, low_seqnr, high_seqnr));
		INP_UNLOCK(inp);
		goto badunlocked;
	}

	/* dp->gsn_rcv should always be the highest received valid sequence number */
	if (DCCP_SEQ_GT(seqnr, dp->gsn_rcv))
		dp->gsn_rcv = seqnr;

	/* Just ignore DCCP-Move for now */
	if (dlh->dh_type == DCCP_TYPE_DATA) {
		extrah_len = 0;
		if (!is_shortseq)
			optp = (u_char *)(dlh + 1);
		else
			optp = (u_char *)(dh + 1);
	} else if (dh->dh_type == DCCP_TYPE_REQUEST) {
		drqh = (struct dccp_requesthdr *)(dlh + 1);
		if (drqh->drqh_scode != dp->scode){
			DCCP_DEBUG((LOG_INFO, "service code in request packet doesn't match! %x %x\n", drqh->drqh_scode, dp->scode));
			INP_UNLOCK(inp);
			dp->state = DCCPS_SERVER_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */
			dccp_disconnect2(dp);
			dccp_output(dp, DCCP_TYPE_RESET + 2);
			dccp_close(dp);
			goto badunlocked;
		}
		optp = (u_char *)(drqh + 1);
		extrah_len = 4;

		/* store reference peer sequence number */
		dp->ref_pseq.hi = seqnr >> 24;
		dp->ref_pseq.lo = (u_int64_t)(seqnr & 0xffffff);

	} else if (dh->dh_type == DCCP_TYPE_RESET) {
		extrah_len = 8 ;
		drth = (struct dccp_resethdr *)(dlh + 1);
		optp = (u_char *)(drth + 1);
	} else {
		if (!is_shortseq){
			extrah_len = 8;
			dalh = (struct dccp_acklhdr *)(dlh + 1);
			if (dh->dh_type == DCCP_TYPE_RESPONSE) {
				extrah_len += 4;
				drqh = (struct dccp_requesthdr *)(dalh + 1);
				if (drqh->drqh_scode != dp->scode){
					DCCP_DEBUG((LOG_INFO, "service code in response packet doesn't match! %x %x\n", drqh->drqh_scode, dp->scode));
					INP_UNLOCK(inp);
					dp->state = DCCPS_CLIENT_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */
					dccp_disconnect2(dp);
					dccp_output(dp, DCCP_TYPE_RESET + 2);
					dccp_close(dp);
					goto badunlocked;
				}
				optp = (u_char *)(drqh + 1);

				/* store reference peer sequence number */
				dp->ref_pseq.hi = seqnr >> 24;
				dp->ref_pseq.lo = (u_int64_t)(seqnr & 0xffffff);
			} else 
				optp = (u_char *)(dalh + 1);
		} else {
			extrah_len = 4;
			dah = (struct dccp_ackhdr *)(dh + 1);
			optp = (u_char *)(dah + 1);
		}

	}

	data_off = (dh->dh_off << 2);

	dp->seq_rcv = seqnr;
	dp->ack_rcv = 0; /* Clear it for now */
	dp->type_rcv = dh->dh_type;
	dp->len_rcv = m->m_len - data_off - iphlen; /* Correct length ? */
	
	if (!is_shortseq)
		optlen = data_off - (sizeof(struct dccplhdr) + extrah_len);
	else
		optlen = data_off - (sizeof(struct dccphdr) + extrah_len);

	if (optlen < 0) {
		DCCP_DEBUG((LOG_INFO, "Data offset is smaller then it could be, optlen = %i data_off = %i, m_len = %i, iphlen = %i extrah_len = %i !\n", optlen, data_off, m->m_len, iphlen, extrah_len));
		INP_UNLOCK(inp);
		goto badunlocked;
	}

	if (optlen > 0) {
		if (optlen > DCCP_MAX_OPTIONS) {
			DCCP_DEBUG((LOG_INFO, "Error, more options (%i) then DCCP_MAX_OPTIONS options!\n", optlen));
			INP_UNLOCK(inp);
			goto badunlocked;
		}

		DCCP_DEBUG((LOG_INFO, "Parsing DCCP options, optlen = %i\n", optlen));
		memcpy(options, optp, optlen);
		dccp_parse_options(dp, options, optlen);
	}

	DCCP_DEBUG((LOG_INFO, "BEFORE state check, Got a %u packet while in %u state, who = %u!\n", dh->dh_type, dp->state, dp->who));

	if (dp->state == DCCPS_LISTEN) {
		switch (dh->dh_type) {

		case DCCP_TYPE_REQUEST:
			DCCP_DEBUG((LOG_INFO, "Got DCCP REQUEST\n"));
			dp->state = DCCPS_REQUEST;
			if (dp->cc_in_use[1] < 0) {
				test[0] = DEFAULT_CCID;
				test[1] = 3;
				dccp_add_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC, test, 2);
			}
			if (len > data_off) {
				DCCP_DEBUG((LOG_INFO, "XXX: len=%d, data_off=%d\n", len, data_off));
				dccp_add_option(dp, DCCP_OPT_DATA_DISCARD, test, 0);
			}
			callout_reset(&dp->connect_timer, DCCP_CONNECT_TIMER,
			    dccp_connect_t, dp);
			dccp_output(dp, 0);
			break;


		/* These are ok if the sender has a valid init Cookie */
		case DCCP_TYPE_ACK:
		case DCCP_TYPE_DATAACK:
		case DCCP_TYPE_DATA:
			DCCP_DEBUG((LOG_INFO, "Got DCCP ACK/DATAACK/DATA, should check init cookie...\n"));
			dccp_output(dp, DCCP_TYPE_RESET + 2);
			break;

		case DCCP_TYPE_RESET:
			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
			dp->state = DCCPS_TIME_WAIT;
			dp = dccp_close(dp);
			return;

		default:
			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in listen stage!\n", dh->dh_type));
			/* Force send reset. */
			dccp_output(dp, DCCP_TYPE_RESET + 2);
		}
	} else if (dp->state == DCCPS_REQUEST) {
		switch (dh->dh_type) {
		case DCCP_TYPE_RESPONSE:
			DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash);
			dp->ack_snd = dp->seq_rcv;
			DCCP_DEBUG((LOG_INFO, "Got DCCP REPSONSE %x %llx\n", dp, dp->ack_snd));

			callout_stop(&dp->retrans_timer);
			callout_stop(&dp->connect_timer);

			/* First check if we have negotiated a cc */
			if (dp->cc_in_use[0] > 0 && dp->cc_in_use[1] > 0) {
				DCCP_DEBUG((LOG_INFO, "Setting DCCPS_ESTAB & soisconnected\n"));
				dp->state = DCCPS_ESTAB;
				dccpstat.dccps_connects++;
				soisconnected(inp->inp_socket);
			} else {
				dp->state = DCCPS_RESPOND;
				DCCP_DEBUG((LOG_INFO, "CC negotiation is not finished, cc_in_use[0] = %u, cc_in_use[1] = %u\n",dp->cc_in_use[0], dp->cc_in_use[1]));

			}
			dccp_output(dp, 0);
			break;

		case DCCP_TYPE_RESET:
			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
			dp->state = DCCPS_TIME_WAIT;
			dp = dccp_close(dp);
			return;

		default:
			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in REQUEST stage!\n", dh->dh_type));
			/* Force send reset. */
			dccp_output(dp, DCCP_TYPE_RESET + 2);
			if (dh->dh_type == DCCP_TYPE_CLOSE) {
				dp = dccp_close(dp);
				return;
			} else {
				callout_stop(&dp->retrans_timer);
				dp->state = DCCPS_TIME_WAIT;
			}
		}
	} else if (dp->state == DCCPS_RESPOND) {
		switch (dh->dh_type) {

		case DCCP_TYPE_REQUEST:
			break;
		case DCCP_TYPE_ACK:
		case DCCP_TYPE_DATAACK:
			DCCP_DEBUG((LOG_INFO, "Got DCCP ACK/DATAACK\n"));

			callout_stop(&dp->connect_timer);

			if (!is_shortseq) {
				DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash);
			} else {
				/* shortseq XXX */
				dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq);
			} 

			if (dp->cc_in_use[0] > 0 && dp->cc_in_use[1] > 0) {
				DCCP_DEBUG((LOG_INFO, "Setting DCCPS_ESTAB & soisconnected\n"));
				dp->state = DCCPS_ESTAB;
				dccpstat.dccps_connects++;
				soisconnected(inp->inp_socket);
			} else {
				DCCP_DEBUG((LOG_INFO, "CC negotiation is not finished, cc_in_use[0] = %u, cc_in_use[1] = %u\n",dp->cc_in_use[0], dp->cc_in_use[1]));
				/* Force an output!!! */
				dp->ack_snd = dp->seq_rcv;
				dccp_output(dp, 0);
			}

			if (dh->dh_type == DCCP_TYPE_DATAACK && dp->cc_in_use[1] > 0) {
				if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv;
				DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_recv_packet_recv!\n", dp->cc_in_use[1]));
				(*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen);
			}
			break;

		case DCCP_TYPE_CLOSE:
			dccp_output(dp, DCCP_TYPE_CLOSE + 1);
			dp = dccp_close(dp);
			goto badunlocked;

		case DCCP_TYPE_RESET:
			dp->state = DCCPS_TIME_WAIT;
			callout_stop(&dp->retrans_timer);
			break;

		default:
			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in response stage!\n", dh->dh_type));
			/* Force send reset. */
			dccp_output(dp, DCCP_TYPE_RESET + 2);
		}
	} else if (dp->state == DCCPS_ESTAB) {
		switch (dh->dh_type) {

		case DCCP_TYPE_DATA:
			DCCP_DEBUG((LOG_INFO, "Got DCCP DATA, state = %i, cc_in_use[1] = %u\n", dp->state, dp->cc_in_use[1]));
			
			if (dp->cc_in_use[1] > 0) {
				if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv;
				DCCP_DEBUG((LOG_INFO, "Calling data *cc_sw[%u].cc_recv_packet_recv! %llx %llx dp=%x\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv, dp));
				(*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen);
			}
			break;
	
		case DCCP_TYPE_ACK:
			DCCP_DEBUG((LOG_INFO, "Got DCCP ACK\n"));
			if (!is_shortseq) {
				DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash);
			} else {
				/* shortseq */
				dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq);
			} 

			if (dp->cc_in_use[1] > 0) {
				/* This is called so Acks on Acks can be handled */
				if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv;
				DCCP_DEBUG((LOG_INFO, "Calling ACK *cc_sw[%u].cc_recv_packet_recv! %llx %llx\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv));
				(*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen);
			}
			break;
	
		case DCCP_TYPE_DATAACK:
			DCCP_DEBUG((LOG_INFO, "Got DCCP DATAACK\n"));

			if (!is_shortseq) {
				DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash);
			} else {
				/* shortseq */
				dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq);
			} 

			if (dp->cc_in_use[1] > 0) {
				if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv;
				DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_recv_packet_recv! %llx %llx\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv));
				(*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen);
			}
			break;
	
		case DCCP_TYPE_CLOSEREQ:
			DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSEREQ, state = estab\n"));
			if (dp->who == DCCP_CLIENT) {
				dccp_disconnect2(dp);
			} else {
				dccp_output(dp, DCCP_TYPE_RESET + 2);
			}
			break;
	
		case DCCP_TYPE_CLOSE:
			DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSE, state = estab\n"));
			dp->state = DCCPS_SERVER_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */
			dccp_disconnect2(dp);
			dccp_output(dp, DCCP_TYPE_RESET + 2);
			dccp_close(dp);
			goto badunlocked;
			break;
	
		case DCCP_TYPE_RESET:
			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
			dp->state = DCCPS_TIME_WAIT;
			callout_stop(&dp->retrans_timer);
			callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER,
			    dccp_timewait_t, dp);
			break;
	
		case DCCP_TYPE_MOVE:
			DCCP_DEBUG((LOG_INFO, "Got DCCP MOVE\n"));
			break;

		default:
			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in established stage!\n", dh->dh_type));
		}

	} else if (dp->state == DCCPS_SERVER_CLOSE) {
		/* Server */
		switch (dh->dh_type) {
		case DCCP_TYPE_CLOSE:
			DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSE (State DCCPS_SERVER_CLOSE)\n"));
			callout_stop(&dp->retrans_timer);
			dccp_output(dp, DCCP_TYPE_RESET + 2);
			dp = dccp_close(dp);
			goto badunlocked;

		case DCCP_TYPE_RESET:
			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
			callout_stop(&dp->retrans_timer);
			dccp_output(dp, DCCP_TYPE_RESET + 2);
			dp->state = DCCPS_TIME_WAIT;
			break;
		default:
			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in server_close stage!\n", dh->dh_type));
		}

	} else if (dp->state == DCCPS_CLIENT_CLOSE) {
		/* Client */
		switch (dh->dh_type) {
		case DCCP_TYPE_CLOSE:
			/* Ignore */
			break;
		case DCCP_TYPE_CLOSEREQ:
			DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSEREQ, state = DCCPS_CLIENT_CLOSE\n"));
			/* Just resend close */
			dccp_output(dp, 0);
			break;
		case DCCP_TYPE_RESET:
			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
			callout_stop(&dp->retrans_timer);
			dp->state = DCCPS_TIME_WAIT;
			callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER,
			    dccp_timewait_t, dp);
			break;
		default:
			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in client_close stage!\n", dh->dh_type));

		}
	} else {
		DCCP_DEBUG((LOG_INFO, "Got a %u packet while in %u state!\n", dh->dh_type, dp->state));
		if (dh->dh_type != DCCP_TYPE_RESET) {
			/* Force send reset. */
			DCCP_DEBUG((LOG_INFO, "Force sending a request!\n"));
			dccp_output(dp, DCCP_TYPE_RESET + 2);
		}
	}

	if (dh->dh_type == DCCP_TYPE_DATA ||
	    dh->dh_type == DCCP_TYPE_ACK  ||
	    dh->dh_type == DCCP_TYPE_DATAACK) {
		if (dp->cc_in_use[0] > 0) {
			(*cc_sw[dp->cc_in_use[0]].cc_send_packet_recv)(dp->cc_state[0],options, optlen);
		}
		
	}

	if (dh->dh_type == DCCP_TYPE_DATA || dh->dh_type == DCCP_TYPE_DATAACK) {
		if (so->so_state & SS_CANTRCVMORE) 
		{
			DCCP_DEBUG((LOG_INFO, "state & SS_CANTRCVMORE...!\n"));
			m_freem(m);
			if (opts)
				m_freem(opts);
		} else {
			m_adj(m, (iphlen + data_off));
			DCCP_DEBUG((LOG_INFO, "Calling sbappend!\n"));
			sbappend(&so->so_rcv, m);
		}
		DCCP_DEBUG((LOG_INFO, "Calling sorwakeup...!\n"));
		sorwakeup(so);
	} else {
		m_freem(m);
		if (opts)
			m_freem(opts);
	}
#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
	if (dp)
		INP_UNLOCK(inp);
#endif

	return;

badunlocked:
	m_freem(m);
	if (opts)
		m_freem(opts);
	return;
}

/*
 * Notify a dccp user of an asynchronous error;
 * just wake up so that he can collect error status.
 */
void
dccp_notify(struct inpcb *inp, int errno)
{
	inp->inp_socket->so_error = errno;
	sorwakeup(inp->inp_socket);
	sowwakeup(inp->inp_socket);
	return;
}

/*
 * Called when we get ICMP errors (destination unreachable,
 * parameter problem, source quench, time exceeded and redirects)
 */
void *
dccp_ctlinput(int cmd, const struct sockaddr *sa, void *vip)
{
	struct ip *ip = vip;
	struct dccphdr *dh;
	void (*notify)(struct inpcb *, int) = dccp_notify;
	struct in_addr faddr;
	struct inpcb *inp = NULL;

	faddr = ((const struct sockaddr_in *)sa)->sin_addr;
	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
		return NULL;

	if (PRC_IS_REDIRECT(cmd)) {
		ip = 0;
		notify = inpcb_rtchange;
	} else if (cmd == PRC_HOSTDEAD)
		ip = 0;
	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
		return NULL;
	if (ip) {
		/*s = splsoftnet();*/
		dh = (struct dccphdr *)((vaddr_t)ip + (ip->ip_hl << 2));
		INP_INFO_RLOCK(&dccpbinfo);
		inpcb_notify(&dccpbtable, faddr, dh->dh_dport,
		    ip->ip_src, dh->dh_sport, inetctlerrmap[cmd], notify);
		if (inp != NULL) {
			INP_LOCK(inp);
			if (inp->inp_socket != NULL) {
				(*notify)(inp, inetctlerrmap[cmd]);
			}
			INP_UNLOCK(inp);
		}
		INP_INFO_RUNLOCK(&dccpbinfo);
		/*splx(s);*/
	} else
		inpcb_notifyall(&dccpbtable, faddr, inetctlerrmap[cmd], notify);

	return NULL;
}

static int
dccp_optsset(struct dccpcb *dp, struct sockopt *sopt)
{
	int optval;
	int error = 0;

	switch (sopt->sopt_name) {
	case DCCP_CCID:
		error = sockopt_getint(sopt, &optval);
		/* Add check that optval is a CCID we support!!! */
		if (optval == 2 || optval == 3 || optval == 0) {
			dp->pref_cc = optval;
		} else {
			error = EINVAL;
		}
		break;
	case DCCP_CSLEN:
		error = sockopt_getint(sopt, &optval);
		if (optval > 15 || optval < 0) {
			error = EINVAL;
		} else {
			dp->cslen = optval;
		}
		break;
	case DCCP_MAXSEG:
		error = sockopt_getint(sopt, &optval);
		if (optval > 0 && optval <= dp->d_maxseg) {
			dp->d_maxseg = optval;
		} else {
			error = EINVAL;
		}
		break;
	case DCCP_SERVICE:
		error = sockopt_getint(sopt, &optval);
		dp->scode = optval;
		break;
		
	default:
		error = ENOPROTOOPT;
	}

	return error;
}

static int	
dccp_optsget(struct dccpcb *dp, struct sockopt *sopt)
{
	int optval = 0;
	int error = 0;

	switch (sopt->sopt_name) {
	case DCCP_CCID:
		optval = dp->pref_cc;
		error = sockopt_set(sopt, &optval, sizeof(optval));
		break;
	case DCCP_CSLEN:
		optval = dp->cslen;
		error = sockopt_set(sopt, &optval, sizeof(optval));
		break;
	case DCCP_MAXSEG:
		optval = dp->d_maxseg;
		error = sockopt_set(sopt, &optval, sizeof(optval));
		break;
	case DCCP_SERVICE:
		optval = dp->scode;
		error = sockopt_set(sopt, &optval, sizeof(optval));
		break;
	default:
		error = ENOPROTOOPT;
	}

	return error;
}

/* 
 * Called by getsockopt and setsockopt.
 *
 */
int
dccp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
{
	int s, error = 0;
	struct inpcb	*inp;
	struct dccpcb	*dp;
	int family;	/* family of the socket */

	family = so->so_proto->pr_domain->dom_family;
	error = 0;

	s = splsoftnet();
	INP_INFO_RLOCK(&dccpbinfo);
	inp = sotoinpcb(so);
	if (inp == NULL)
	{
		INP_INFO_RUNLOCK(&dccpbinfo);
		splx(s);
		return (ECONNRESET);
	}
/*
	if (inp)
		INP_LOCK(inp);
	else
		IN6P_LOCK(in6p);
	INP_INFO_RUNLOCK(&dccpbinfo);
*/
	if (sopt->sopt_level != IPPROTO_DCCP) {
		switch (family) {
		case PF_INET:
			error = ip_ctloutput(op, so, sopt);
			break;
#if defined(INET6)
		case PF_INET6:
			error = ip6_ctloutput(op, so, sopt);
			break;
#endif
		}
		splx(s);
		return (error);
	}

	dp = intodccpcb(inp);

	if (op == PRCO_SETOPT) {
		error = dccp_optsset(dp, sopt);
	} else if (op ==  PRCO_GETOPT) {
		error = dccp_optsget(dp, sopt);
	} else {
		error = EINVAL;
	}
/*
	if (inp)
		INP_UNLOCK(inp);
	else
		IN6P_UNLOCK(in6p);
*/
	splx(s);
	return error;
}

int
dccp_output(struct dccpcb *dp, u_int8_t extra)
{
	struct inpcb *inp;
	struct socket *so;
	struct mbuf *m;

	struct ip *ip = NULL;
	struct dccphdr *dh;
	struct dccplhdr *dlh;
	struct dccp_requesthdr *drqh;
	struct dccp_ackhdr *dah;
	struct dccp_acklhdr *dalh;
	struct dccp_resethdr *drth;
	u_char *optp = NULL;
	int error = 0;
	int off, sendalot, t, i;
	u_int32_t hdrlen, optlen, extrah_len, cslen;
	u_int8_t type;
	char options[DCCP_MAX_OPTIONS *2];
	long len, pktlen;
	int isipv6 = 0;
	int use_shortseq = 0;
#ifdef INET6
	struct ip6_hdr *ip6 = NULL;
#endif

	DCCP_DEBUG((LOG_INFO, "dccp_output start!\n"));

	isipv6 = (dp->inp_vflag & INP_IPV6) != 0;

	DCCP_DEBUG((LOG_INFO, "Going to send a DCCP packet!\n"));
#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
	KASSERT(mutex_assert(&dp->d_inpcb->inp_mtx, MA_OWNED));
#endif

	inp = dp->d_inpcb;
	so = inp->inp_socket;

	if (dp->state != DCCPS_ESTAB && extra == 1) {
		/* Only let cc decide when to resend if we are in established state */
		return 0;
	}

	if (so->so_snd.sb_cc){
		pktlen = dp->pktlen[dp->pktlenidx];
	} else 
		pktlen = 0;

	/* Check with CC if we can send... */
	if (pktlen && dp->cc_in_use[0] > 0 && dp->state == DCCPS_ESTAB) {
		if (!(*cc_sw[dp->cc_in_use[0]].cc_send_packet)(dp->cc_state[0], pktlen)) {
			DCCP_DEBUG((LOG_INFO, "Not allowed to send right now\n"));
			return 0;
		}
	}

	if (pktlen) {
		dp->pktcnt --;
		dp->pktlenidx = (dp->pktlenidx +1) % DCCP_MAX_PKTS;
	}

again:
	sendalot = 0;

	/*
	 * off not needed for dccp because we do not need to wait for ACK
	 * before removing the packet
	 */
	off = 0;
	optlen = 0;

	if (pktlen > dp->d_maxseg) {
		/* this should not happen */
		DCCP_DEBUG((LOG_INFO, "packet will be fragmented! maxseg %d\n", dp->d_maxseg));
		len = dp->d_maxseg;
		pktlen -= len;
		sendalot = 1;
	} else
		len = pktlen;

	if (extra == DCCP_TYPE_RESET + 2) {
		DCCP_DEBUG((LOG_INFO, "Force sending of DCCP TYPE_RESET! seq=%llu\n", dp->seq_snd));
		type = DCCP_TYPE_RESET;
		extrah_len = 12;
	} else if (dp->state <= DCCPS_REQUEST && dp->who == DCCP_CLIENT) {
		DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_REQUEST!\n"));
		type = DCCP_TYPE_REQUEST;
		dp->state = DCCPS_REQUEST;
		extrah_len = 4;
	} else if (dp->state == DCCPS_REQUEST && dp->who == DCCP_SERVER) {
		DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_RESPONSE!\n"));
		type = DCCP_TYPE_RESPONSE;
		dp->state = DCCPS_RESPOND;
		extrah_len = 12;
	} else if (dp->state == DCCPS_RESPOND) {
		DCCP_DEBUG((LOG_INFO, "Still in feature neg, sending DCCP TYPE_ACK!\n"));
		type = DCCP_TYPE_ACK;
		if (!dp->shortseq)
			extrah_len = 8;
		else {
			extrah_len = 4;
			use_shortseq = 1;
		}
	} else if (dp->state == DCCPS_ESTAB) {
		if (dp->ack_snd && len) {
			DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_DATAACK!\n"));
			type = DCCP_TYPE_DATAACK;
			/*(u_int32_t *)&extrah = dp->seq_rcv; */
			if (!dp->shortseq)
				extrah_len = 8;
			else {
				extrah_len = 4;
				use_shortseq = 1;
			}
		} else if (dp->ack_snd) {
			DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_ACK!\n"));
			type = DCCP_TYPE_ACK;
			if (!dp->shortseq)
				extrah_len = 8;
			else {
				extrah_len = 4;
				use_shortseq = 1;
			}
		} else if (len) {
			DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_DATA!\n"));
			type = DCCP_TYPE_DATA;
			extrah_len = 0;
		} else {
			DCCP_DEBUG((LOG_INFO, "No ack or data to send!\n"));
			return 0;
		}
	} else if (dp->state == DCCPS_CLIENT_CLOSE) {
		DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_CLOSE!\n"));
		type = DCCP_TYPE_CLOSE;
		extrah_len = 8;
	} else if (dp->state == DCCPS_SERVER_CLOSE) {
		DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_CLOSEREQ!\n"));
		type = DCCP_TYPE_CLOSEREQ;
		extrah_len = 8;
	} else {
		DCCP_DEBUG((LOG_INFO, "Hey, we should never get here, state = %u\n", dp->state));
		return 1;
	}

	/* Adding options. */
	if (dp->optlen) {
		DCCP_DEBUG((LOG_INFO, "Copying options from dp->options! %u\n", dp->optlen));
		memcpy(options, dp->options, dp->optlen);
		optlen = dp->optlen;
		dp->optlen = 0;
	}

	if (dp->featlen && (optlen + dp->featlen < DCCP_MAX_OPTIONS)) {
		DCCP_DEBUG((LOG_INFO, "Copying options from dp->features! %u\n", dp->featlen));
		memcpy(options + optlen, dp->features, dp->featlen);
		optlen += dp->featlen;
	}

	t = optlen % 4;

	if (t) {
		t = 4 - t;
		for (i = 0 ; i<t; i++) {
			options[optlen] = 0;
			optlen++;
		}
	}

#ifdef INET6
	if (isipv6) {
		DCCP_DEBUG((LOG_INFO, "Sending ipv6 packet...\n"));
		if (!use_shortseq)
			hdrlen = sizeof(struct ip6_hdr) + sizeof(struct dccplhdr) +
			    extrah_len + optlen;
		else 
			hdrlen = sizeof(struct ip6_hdr) + sizeof(struct dccphdr) +
			    extrah_len + optlen;
	} else
#endif
	{
		if (!use_shortseq)
			hdrlen = sizeof(struct ip) + sizeof(struct dccplhdr) +
		   		extrah_len + optlen;
		else
			hdrlen = sizeof(struct ip) + sizeof(struct dccphdr) +
		   		extrah_len + optlen;
	}
	DCCP_DEBUG((LOG_INFO, "Pkt headerlen %u\n", hdrlen));

	if (len > (dp->d_maxseg - extrah_len - optlen)) {
		len = dp->d_maxseg - extrah_len - optlen;
		sendalot = 1;
	}
	
	MGETHDR(m, M_DONTWAIT, MT_HEADER);
	if (m == NULL) {
		error = ENOBUFS;
		goto release;
	}
	if (MHLEN < hdrlen + max_linkhdr) {
		MCLGET(m, M_DONTWAIT);
		if ((m->m_flags & M_EXT) == 0) {
			error = ENOBUFS;
			goto release;
		}
	}

	m->m_data += max_linkhdr;
	m->m_len = hdrlen;

	if (len) { /* We have data to send */
		if (len <= M_TRAILINGSPACE(m) - hdrlen) {
			m_copydata(so->so_snd.sb_mb, off, (int) len,
			mtod(m, char *) + hdrlen);
			m->m_len += len;
		} else {
			m->m_next = m_copym(so->so_snd.sb_mb, off,
			    (int)len, M_DONTWAIT);
			if (m->m_next == 0) {
				error = ENOBUFS;
				goto release;
			}
		}
	} else {
		dp->ndp++;
	}

	m_reset_rcvif(m);

	if (!isipv6 && (len + hdrlen) > IP_MAXPACKET) {
		error = EMSGSIZE;
		goto release;
	}

	/*
	 * Fill in mbuf with extended DCCP header
	 * and addresses and length put into network format.
	 */
#ifdef INET6
	if (isipv6) {
		ip6 = mtod(m, struct ip6_hdr *);
		dh = (struct dccphdr *)(ip6 + 1);
		ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
			(in6p_flowinfo(inp) & IPV6_FLOWINFO_MASK);
		ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
			 (IPV6_VERSION & IPV6_VERSION_MASK);
		ip6->ip6_nxt = IPPROTO_DCCP;
		ip6->ip6_src = in6p_laddr(inp);
		ip6->ip6_dst = in6p_faddr(inp);
	} else 
#endif
	{
		ip = mtod(m, struct ip *);
		dh = (struct dccphdr *)(ip + 1);
		memset(ip, 0, sizeof(struct ip));
		ip->ip_p = IPPROTO_DCCP;
		ip->ip_src = in4p_laddr(inp);
		ip->ip_dst = in4p_faddr(inp);
	}
	dlh = (struct dccplhdr *)dh;

	dh->dh_sport = inp->inp_lport;
	dh->dh_dport = inp->inp_fport;
	dh->dh_cscov = dp->cslen;
	dh->dh_ccval = dp->ccval;
	dh->dh_type = type;
	dh->dh_res = 0; /* Reserved field should be zero */
	if (!use_shortseq) { 
		dlh->dh_res2 = 0; /* Reserved field should be zero */
		dh->dh_off = 4 + (extrah_len / 4) + (optlen / 4);
	} else		
		dh->dh_off = 3 + (extrah_len / 4) + (optlen / 4);

	dp->seq_snd = (dp->seq_snd +1) % 281474976710656LL;
	if (!use_shortseq) { 
		DSEQ_TO_DHDR(dlh, dp->seq_snd);
		dlh->dh_x = 1;
	} else {
		/* short sequene number */
		dh->dh_seq = htonl(dp->seq_snd) >> 8;
		dh->dh_x = 0;
	}       

	if (!use_shortseq) {
		DCCP_DEBUG((LOG_INFO, "Sending with seq %x.%x, (dp->seq_snd = %llu)\n\n", dlh->dh_seq, dlh->dh_seq2, dp->seq_snd));
	} else {
		DCCP_DEBUG((LOG_INFO, "Sending with seq %x, (dp->seq_snd = %llu)\n\n", dh->dh_seq, dp->seq_snd));
	}

	if (dh->dh_type == DCCP_TYPE_REQUEST) {
		drqh = (struct dccp_requesthdr *)(dlh + 1);
		drqh->drqh_scode = dp->scode;
		optp = (u_char *)(drqh + 1);
	} else if (dh->dh_type == DCCP_TYPE_RESET) {
		drth = (struct dccp_resethdr *)(dlh + 1);
		drth->drth_dash.dah_res = 0;
		DSEQ_TO_DAHDR(drth->drth_dash, dp->seq_rcv);
		if (dp->state == DCCPS_SERVER_CLOSE) 
			drth->drth_reason = 1;
		else
			drth->drth_reason = 2;
		drth->drth_data1 = 0;
		drth->drth_data2 = 0;
		drth->drth_data3 = 0;
		optp = (u_char *)(drth + 1);
	} else if (extrah_len) {
		if (!use_shortseq){
			dalh = (struct dccp_acklhdr *)(dlh + 1);
			dalh->dash.dah_res = 0; /* Reserved field should be zero */

			if (dp->state == DCCPS_ESTAB) {
				DSEQ_TO_DAHDR(dalh->dash, dp->ack_snd);
				dp->ack_snd = 0;
			} else {
				DSEQ_TO_DAHDR(dalh->dash, dp->seq_rcv);
			}

			if (dh->dh_type == DCCP_TYPE_RESPONSE) {
				DCCP_DEBUG((LOG_INFO, "Sending dccp type response\n"));
				drqh = (struct dccp_requesthdr *)(dalh + 1);
				drqh->drqh_scode = dp->scode;
				optp = (u_char *)(drqh + 1);
			} else 
				optp = (u_char *)(dalh + 1);
		} else {
			/* XXX shortseq */
			dah = (struct dccp_ackhdr *)(dh + 1);
			dah->dash.dah_res = 0; /* Reserved field should be zero */
			dah->dash.dah_ack = htonl(dp->seq_rcv) >> 8;
			optp = (u_char *)(dah + 1);
		}
		
	} else {
		optp = (u_char *)(dlh + 1);
	}

	if (optlen)
		memcpy(optp, options, optlen);

	m->m_pkthdr.len = hdrlen + len;

	if (dh->dh_cscov == 0) {
#ifdef INET6
		if (isipv6)
			cslen = (hdrlen - sizeof(struct ip6_hdr)) + len;
		else
			cslen = (hdrlen - sizeof(struct ip)) + len;
#else
		cslen = (hdrlen - sizeof(struct ip)) + len;
#endif
	} else {
		cslen = dh->dh_off * 4 + (dh->dh_cscov - 1) * 4;
#ifdef INET6
		if (isipv6) {
			if (cslen > (hdrlen - sizeof(struct ip6_hdr)) + len)
				cslen = (hdrlen - sizeof(struct ip6_hdr)) + len;
		} else {
			if (cslen > (hdrlen - sizeof(struct ip)) + len)
				cslen = (hdrlen - sizeof(struct ip)) + len;
		}
#else
		if (cslen > (hdrlen - sizeof(struct ip)) + len)
			cslen = (hdrlen - sizeof(struct ip)) + len;
#endif
	}

	/*
	 * Set up checksum 
	 */
	m->m_pkthdr.csum_flags = 0;

	dh->dh_sum = 0;
#ifdef INET6
	if (isipv6) {
		dh->dh_sum = in6_cksum(m, IPPROTO_DCCP, sizeof(struct ip6_hdr),
		    cslen);
	} else
#endif
	{
		ip->ip_len = htons(hdrlen + len);
		ip->ip_ttl = dp->inp_ip_ttl;	/* XXX */
		ip->ip_tos = dp->inp_ip_tos;	/* XXX */

		dh->dh_sum = in4_cksum(m, IPPROTO_DCCP, sizeof(struct ip),
		    cslen);
#ifndef __OpenBSD__
		m->m_pkthdr.csum_data = offsetof(struct dccphdr, dh_sum);
#endif
	}

	dccpstat.dccps_opackets++;
	dccpstat.dccps_obytes += m->m_pkthdr.len;

#ifdef INET6
	if (isipv6) {
		DCCP_DEBUG((LOG_INFO, "Calling ip_output6, mbuf->m_len = %u, mbuf->m_pkthdr.len = %u\n", m->m_len, m->m_pkthdr.len));

		error = ip6_output(m, in6p_outputopts(inp), &inp->inp_route,
		    (inp->inp_socket->so_options & SO_DONTROUTE), NULL, NULL,
		    NULL);
	} else
#endif
	{
		DCCP_DEBUG((LOG_INFO, "Calling ip_output, mbuf->m_len = %u, mbuf->m_pkthdr.len = %u\n", m->m_len, m->m_pkthdr.len));
		error = ip_output(m, inp->inp_options, &inp->inp_route,
		    (inp->inp_socket->so_options & SO_DONTROUTE), 0, 
				  inp);
	}

	if (error) {
		DCCP_DEBUG((LOG_INFO, "IP output failed! %d\n", error));
		return (error);
	}

	sbdrop(&inp->inp_socket->so_snd, len);
	sowwakeup(inp->inp_socket);

	if (dp->cc_in_use[0] > 0  && dp->state == DCCPS_ESTAB) {
		DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_send_packet_sent!\n", dp->cc_in_use[0]));
		if (sendalot) {
			(*cc_sw[dp->cc_in_use[0]].cc_send_packet_sent)(dp->cc_state[0], 1,len);
			goto again;
		} else {
			(*cc_sw[dp->cc_in_use[0]].cc_send_packet_sent)(dp->cc_state[0], 0,len);
		}
	} else {
		if (sendalot)
			goto again;
	}

	DCCP_DEBUG((LOG_INFO, "dccp_output finished\n"));

	return (0);

release:
	m_freem(m);
	return (error);
}

int
dccp_abort(struct socket *so)
{
	struct inpcb *inp = NULL;
	struct dccpcb *dp;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_abort!\n"));
	INP_INFO_WLOCK(&dccpbinfo);
	inp = sotoinpcb(so);
	if (inp == NULL) {
		INP_INFO_WUNLOCK(&dccpbinfo);
		return EINVAL;
	}
	dp = inp->inp_ppcb;

	dccp_disconnect2(dp);

	INP_INFO_WUNLOCK(&dccpbinfo);
	return 0;
}

static struct dccpcb *
dccp_close(struct dccpcb *dp)
{
	struct socket *so;
	struct inpcb *inp = dp->d_inpcb;
	so = dptosocket(dp);

	DCCP_DEBUG((LOG_INFO, "Entering dccp_close!\n"));

	/* Stop all timers */
	callout_stop(&dp->connect_timer);
	callout_stop(&dp->retrans_timer);
	callout_stop(&dp->close_timer);
	callout_stop(&dp->timewait_timer);

	if (dp->cc_in_use[0] > 0)
		(*cc_sw[dp->cc_in_use[0]].cc_send_free)(dp->cc_state[0]);
	if (dp->cc_in_use[1] > 0)
		(*cc_sw[dp->cc_in_use[1]].cc_recv_free)(dp->cc_state[1]);

	pool_put(&dccpcb_pool, dp);
	inp->inp_ppcb = NULL;
	soisdisconnected(so);
	inpcb_destroy(inp);
	return ((struct dccpcb *)0);
}

/*
 * Runs when a new socket is created with the
 * socket system call or sonewconn.
 */
int
dccp_attach(struct socket *so, int proto)
{
	struct inpcb *inp = NULL;
	struct dccpcb *dp;
	int s, error = 0;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_attach(proto=%d)!\n", proto));
	INP_INFO_WLOCK(&dccpbinfo);
	s = splsoftnet();
	sosetlock(so);

	inp = sotoinpcb(so);
	if (inp != 0) {
		error = EINVAL;
		goto out;
	}
	error = soreserve(so, dccp_sendspace, dccp_recvspace);
	if (error)
		goto out;
	error = inpcb_create(so, &dccpbtable);
	if (error)
		goto out;
	inp = sotoinpcb(so);

	dp = dccp_newdccpcb(inp->inp_af, inp);
	if (dp == 0) {
		int nofd = so->so_state & SS_NOFDREF;
		so->so_state &= ~SS_NOFDREF;
		inpcb_destroy(inp);
		so->so_state |= nofd;
		error = ENOBUFS;
		goto out;
	}

#ifdef INET6
	if (proto == PF_INET6) {
		DCCP_DEBUG((LOG_INFO, "We are an ipv6 socket!!!\n"));
		dp->inp_vflag |= INP_IPV6;
	} else 
#endif
		dp->inp_vflag |= INP_IPV4;
	dp->inp_ip_ttl = ip_defttl;

	dp->state = DCCPS_CLOSED;
out:
	splx(s);
	INP_INFO_WUNLOCK(&dccpbinfo);
	return error;
}

static int
dccp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
{
	struct inpcb *inp;
	int error;
	int s;
	struct sockaddr_in *sin = (struct sockaddr_in *)nam;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_bind!\n"));
	INP_INFO_WLOCK(&dccpbinfo);
	inp = sotoinpcb(so);
	if (inp == 0) {
		INP_INFO_WUNLOCK(&dccpbinfo);
		return EINVAL;
	}

	/* Do not bind to multicast addresses! */
	if (sin->sin_family == AF_INET &&
	    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
		INP_INFO_WUNLOCK(&dccpbinfo);
		return EAFNOSUPPORT;
	}
	INP_LOCK(inp);
	s = splsoftnet();
	error = inpcb_bind(inp, sin, l);
	splx(s);
	INP_UNLOCK(inp);
	INP_INFO_WUNLOCK(&dccpbinfo);
	return error;
}

/*
 * Initiates a connection to a server
 * Called by the connect system call.
 */
static int
dccp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
{
	struct inpcb *inp;
	struct dccpcb *dp;
	int error;
	struct sockaddr_in *sin;
	char test[2];

	DCCP_DEBUG((LOG_INFO, "Entering dccp_connect!\n"));

	INP_INFO_WLOCK(&dccpbinfo);
	inp = sotoinpcb(so);
	if (inp == 0) {
		INP_INFO_WUNLOCK(&dccpbinfo);
		return EINVAL;
	}
	INP_LOCK(inp);
	if (in4p_faddr(inp).s_addr != INADDR_ANY) {
		INP_UNLOCK(inp);
		INP_INFO_WUNLOCK(&dccpbinfo);
		return EISCONN;
	}

	dp = (struct dccpcb *)inp->inp_ppcb;

	if (dp->state == DCCPS_ESTAB) {
		DCCP_DEBUG((LOG_INFO, "Why are we in connect when we already have an established connection?\n"));
	}

	dp->who = DCCP_CLIENT;
	dp->seq_snd = (((u_int64_t)random() << 32) | random()) % 281474976710656LL;
	dp->ref_seq.hi = dp->seq_snd >> 24;
	dp->ref_seq.lo = (u_int64_t)(dp->seq_snd & 0xffffff);
	DCCP_DEBUG((LOG_INFO, "dccp_connect seq_snd %llu\n", dp->seq_snd));

	dccpstat.dccps_connattempt++;

	sin = (struct sockaddr_in *)nam;
	if (sin->sin_family == AF_INET
	    && IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
		error = EAFNOSUPPORT;
		goto bad;
	}

	error = dccp_doconnect(so, nam, l, 0);

	if (error != 0)
		goto bad;

	callout_reset(&dp->retrans_timer, dp->retrans, dccp_retrans_t, dp);
	callout_reset(&dp->connect_timer, DCCP_CONNECT_TIMER, dccp_connect_t, dp);

	if (dccp_do_feature_nego){
		test[0] = dp->pref_cc;
		dccp_add_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC, test, 1);
	}

	error = dccp_output(dp, 0);

bad:
	INP_UNLOCK(inp);
	INP_INFO_WUNLOCK(&dccpbinfo);
	return error;
}

static int
dccp_connect2(struct socket *so, struct socket *so2)
{
	KASSERT(solocked(so));

	return EOPNOTSUPP;
}

/*
 *
 *
 */
int
dccp_doconnect(struct socket *so, struct sockaddr *nam,
    struct lwp *l, int isipv6)
{ 
	struct inpcb *inp;
	int error = 0;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_doconnect!\n"));

	inp = sotoinpcb(so);

	if (inp->inp_lport == 0) {
#ifdef INET6
		if (isipv6) {
			DCCP_DEBUG((LOG_INFO, "Running in6pcb_bind!\n"));
			error = in6pcb_bind(inp, NULL, l);
		} else
#endif /* INET6 */
		{
			error = inpcb_bind(inp, NULL, l);
		}
		if (error) {
			DCCP_DEBUG((LOG_INFO, "inpcb_bind=%d\n",error));
			return error;
		}
	}

#ifdef INET6
	if (isipv6) {
		error = in6pcb_connect(inp, (struct sockaddr_in6 *)nam, l);
		DCCP_DEBUG((LOG_INFO, "in6pcb_connect=%d\n",error));
	} else
#endif
		error = inpcb_connect(inp, (struct sockaddr_in *)nam, l);
	if (error) {
		DCCP_DEBUG((LOG_INFO, "inpcb_connect=%d\n",error));
		return error;
	}

	soisconnecting(so);
	return error;
}

/*
 * Detaches the DCCP protocol from the socket.
 *
 */
int
dccp_detach(struct socket *so)
{
	struct inpcb *inp;
	struct dccpcb *dp;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_detach!\n"));
	inp = sotoinpcb(so);
	if (inp == NULL) {
		return EINVAL;
	}
	dp = inp->inp_ppcb;
	if (! dccp_disconnect2(dp)) {
		INP_UNLOCK(inp);
	}
	INP_INFO_WUNLOCK(&dccpbinfo);
	return 0;
}

/*
 * 
 *
 */
int
dccp_disconnect(struct socket *so)
{
	struct inpcb *inp;
	struct dccpcb *dp;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_disconnect!\n"));
	INP_INFO_WLOCK(&dccpbinfo);
#ifndef __NetBSD__
	inp = sotoinpcb(so);
	if (inp == 0) {
		INP_INFO_WUNLOCK(&dccpbinfo);
		return EINVAL;
	}
	INP_LOCK(inp);
	if (in4p_faddr(inp).s_addr == INADDR_ANY) {
		INP_INFO_WUNLOCK(&dccpbinfo);
		INP_UNLOCK(inp);
		return ENOTCONN;
	}

	dp = (struct dccpcb *)inp->inp_ppcb;
#else /* NetBSD */
	inp = sotoinpcb(so);
	if (inp == NULL) {
		INP_INFO_WUNLOCK(&dccpbinfo);
		return EINVAL;
	}
	dp = inp->inp_ppcb;
#endif
	if (!dccp_disconnect2(dp)) {
		INP_UNLOCK(inp);
	}
	INP_INFO_WUNLOCK(&dccpbinfo);
	return 0;
}

/*
 * If we have don't have an established connection
 * we can call dccp_close, otherwise we can just
 * set SS_ISDISCONNECTED and flush the receive queue.
 */
static int
dccp_disconnect2(struct dccpcb *dp)
{
	struct socket *so = dptosocket(dp);

	DCCP_DEBUG((LOG_INFO, "Entering dccp_disconnect2!\n"));

	if (dp->state < DCCPS_ESTAB) {
		dccp_close(dp);
		return 1;
	} else {
		soisdisconnecting(so);
		sbflush(&so->so_rcv);
		if (dp->state == DCCPS_ESTAB) {
			dp->retrans = 100;
			callout_reset(&dp->retrans_timer, dp->retrans,
			    dccp_retrans_t, dp);
			callout_reset(&dp->close_timer, DCCP_CLOSE_TIMER,
			    dccp_close_t, dp);
			if (dp->who == DCCP_CLIENT) {
				dp->state = DCCPS_CLIENT_CLOSE;
			} else {
				dp->state = DCCPS_SERVER_CLOSE;
			}
			dccp_output(dp, 0);
		}
	}
	return 0;
}

int
dccp_send(struct socket *so, struct mbuf *m, struct sockaddr *addr,
    struct mbuf *control, struct lwp *l)
{
	struct inpcb	*inp;
	struct dccpcb	*dp;
	int		error = 0;
	int		isipv6 = 0;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_send!\n"));
	KASSERT(solocked(so));
	KASSERT(m != NULL);

	if (control && control->m_len) {
		m_freem(control);
		m_freem(m);
		return EINVAL;
	}

#ifdef INET6
	isipv6 = addr && addr->sa_family == AF_INET6;
#endif

	INP_INFO_WLOCK(&dccpbinfo);
	inp = sotoinpcb(so);
	if (inp == NULL) {
		error = EINVAL;
		goto release;
	}
	INP_LOCK(inp);
	dp = inp->inp_ppcb;

	if (dp->state != DCCPS_ESTAB) {
		DCCP_DEBUG((LOG_INFO, "We have no established connection!\n"));
	}

	if (control != NULL) {
		DCCP_DEBUG((LOG_INFO, "We got a control message!\n"));
		/* Are we going to use control messages??? */
		if (control->m_len) {
			m_freem(control);
		}
	}

	if (sbspace_oob(&so->so_snd) == 0) {
		INP_UNLOCK(inp);
		error = ENOBUFS;
		goto release;
	}

	if (m->m_pkthdr.len > dp->d_maxseg) {
		/* XXX we should calculate packet size more carefully */
		INP_UNLOCK(inp);
		error = EINVAL;
		goto release;
	}

	if (dp->pktcnt >= DCCP_MAX_PKTS) {
		INP_UNLOCK(inp);
		error = ENOBUFS;
		goto release;
	}

	sbappend(&so->so_snd, m);
	dp->pktlen[(dp->pktlenidx + dp->pktcnt) % DCCP_MAX_PKTS] = m->m_pkthdr.len;
	dp->pktcnt ++;

	if (addr && dp->state == DCCPS_CLOSED) {
		error = dccp_doconnect(so, addr, l, isipv6);
		if (error)
			goto out;
	}

	error = dccp_output(dp, 0);

out:
	INP_UNLOCK(inp);
	INP_INFO_WUNLOCK(&dccpbinfo);
	return error;

release:
	INP_INFO_WUNLOCK(&dccpbinfo);
	m_freem(m);
	return (error);
}

/*
 * Sets socket to SS_CANTSENDMORE 
 */
int
dccp_shutdown(struct socket *so)
{
	struct inpcb *inp;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_shutdown!\n"));
	INP_INFO_RLOCK(&dccpbinfo);
	inp = sotoinpcb(so);
	if (inp == 0) {
		INP_INFO_RUNLOCK(&dccpbinfo);
		return EINVAL;
	}
	INP_LOCK(inp);
	INP_INFO_RUNLOCK(&dccpbinfo);
	socantsendmore(so);
	INP_UNLOCK(inp);
	return 0;
}

static int
dccp_listen(struct socket *so, struct lwp *td)
{
	struct inpcb *inp;
	struct dccpcb *dp;
	int error = 0;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_listen!\n"));

	INP_INFO_RLOCK(&dccpbinfo);
	inp = sotoinpcb(so);
	if (inp == 0) {
		INP_INFO_RUNLOCK(&dccpbinfo);
		return EINVAL;
	}
	INP_LOCK(inp);
	INP_INFO_RUNLOCK(&dccpbinfo);
	dp = (struct dccpcb *)inp->inp_ppcb;
	if (inp->inp_lport == 0)
		error = inpcb_bind(inp, NULL, td);
	if (error == 0) {
		dp->state = DCCPS_LISTEN;
		dp->who = DCCP_LISTENER;
	}
	INP_UNLOCK(inp);
	return error;
}

/*
 * Accepts a connection (accept system call)
 */
static int
dccp_accept(struct socket *so, struct sockaddr *nam)
{
	struct inpcb *inp = NULL;
	int error = 0;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_accept!\n"));

	if (nam == NULL) {
		return EINVAL;
	}
	if (so->so_state & SS_ISDISCONNECTED) {
		DCCP_DEBUG((LOG_INFO, "so_state && SS_ISDISCONNECTED!, so->state = %i\n", so->so_state));
		return ECONNABORTED;
	}

	INP_INFO_RLOCK(&dccpbinfo);
	inp = sotoinpcb(so);
	if (inp == 0) {
		INP_INFO_RUNLOCK(&dccpbinfo);
		return EINVAL;
	}
	INP_LOCK(inp);
	INP_INFO_RUNLOCK(&dccpbinfo);
	inpcb_fetch_peeraddr(inp, (struct sockaddr_in *)nam);

	return error;
}

/*
 * Initializes a new DCCP control block
 * (inpcb_create in attach has already allocated memory for it)
 */
struct dccpcb *
dccp_newdccpcb(int family, void *aux)
{
	struct inpcb *inp;
	struct dccpcb	*dp;

	DCCP_DEBUG((LOG_INFO, "Creating a new dccpcb!\n"));

	dp = pool_get(&dccpcb_pool, PR_NOWAIT);
	if (dp == NULL)
		return NULL;
	memset((char *) dp, 0, sizeof(struct dccpcb));

	callout_init(&dp->connect_timer, 0);
	callout_init(&dp->retrans_timer, 0);
	callout_init(&dp->close_timer, 0);
	callout_init(&dp->timewait_timer, 0);

	dp->ndp = 0;
	dp->loss_window = 1000;
	dp->cslen = 0;
	dp->pref_cc = DEFAULT_CCID;
	dp->who = DCCP_UNDEF;
	dp->seq_snd = 0;
	dp->seq_rcv = 0;
	dp->shortseq = 0;
	dp->gsn_rcv = 281474976710656LL;
	dp->optlen = 0;
	if (dccp_do_feature_nego){
		dp->cc_in_use[0] = -1;
		dp->cc_in_use[1] = -1;
	} else {
		/* for compatibility with linux */
		dp->cc_in_use[0] = 4;
		dp->cc_in_use[1] = 4;
	}
	dp->av_size = 0; /* no ack vector initially */
	dp->remote_ackvector = 0; /* no ack vector on remote side initially */
	dp->retrans = 200;
	dp->avgpsize = 0;
	dp->d_maxseg = 1400;
	dp->ref_pseq.hi = 0;
	dp->ref_pseq.lo = 0;
	dp->pktlenidx = 0;
	dp->pktcnt = 0;

	inp = (struct inpcb *)aux;
	dp->d_inpcb = inp;
	inp->inp_ppcb = dp;
	switch (family) {
	case PF_INET:
		in4p_ip(inp).ip_ttl = ip_defttl;
		break;
	case PF_INET6:
		in6p_ip6(inp).ip6_hlim = in6pcb_selecthlim_rt(inp);
		break;
	}
	
	if (!dccp_do_feature_nego){
		dp->cc_state[0] = (*cc_sw[4].cc_send_init)(dp);
		dp->cc_state[1] = (*cc_sw[4].cc_recv_init)(dp);
	}

	return dp;
}

int
dccp_add_option(struct dccpcb *dp, u_int8_t opt, char *val, u_int8_t val_len)
{
	return dccp_add_feature_option(dp, opt, 0, val, val_len);
}

int
dccp_add_feature_option(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, char *val, u_int8_t val_len)
{
	int i;
	DCCP_DEBUG((LOG_INFO, "Entering dccp_add_feature_option, opt = %u, val_len = %u optlen %u\n", opt, val_len, dp->optlen));

	if (DCCP_MAX_OPTIONS > (dp->optlen + val_len + 2)) {
		dp->options[dp->optlen] = opt;
		if (opt < 32) {
			dp->optlen++;
		} else {
			if (opt == DCCP_OPT_CONFIRM_L && val_len) {
				dp->options[dp->optlen + 1] = val_len + 3;
				dp->options[dp->optlen +2] = feature;
				dp->optlen += 3;
			} else {
				dp->options[dp->optlen + 1] = val_len + 2;
				dp->optlen += 2;
			}
	
			for (i = 0; i<val_len; i++) {
				dp->options[dp->optlen] = val[i];
				dp->optlen++;
			}
		}
	} else {
		DCCP_DEBUG((LOG_INFO, "No room for more options, optlen = %u\n", dp->optlen));
		return -1;
	}

	return 0;
}

/*
 * Searches "options" for given option type. if found, the data is copied to buffer
 * and returns the data length.
 * Returns 0 if option type not found
 */
int
dccp_get_option(char *options, int optlen, int type, char *buffer, int buflen)
{
	int i, j, size;
	u_int8_t t;
	
	for (i=0; i < optlen;) {
		t = options[i++];
		if (t >= 32) {		  
			size = options[i++] - 2;
			if (t == type) {
				if (size > buflen)
					return 0;
				for (j = 0; j < size; j++)
					buffer[j] = options[i++];
				return size;
			}
			i += size;
		}
	}
	/* If we get here the options was not found */
	DCCP_DEBUG((LOG_INFO, "dccp_get_option option(%d) not found\n", type));
	return 0;
}

void
dccp_parse_options(struct dccpcb *dp, char *options, int optlen)
{
	u_int8_t opt, size, i, j;
	char val[8];

	for (i = 0; i < optlen; i++) {
		opt = options[i];

		DCCP_DEBUG((LOG_INFO, "Parsing opt: 0x%02x\n", opt));

		if (opt < 32) {
			switch (opt) {
			    case DCCP_OPT_PADDING:
				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_PADDING!\n"));
				break;
			    case DCCP_OPT_DATA_DISCARD:
				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_DATA_DISCARD!\n"));
				break;
			    case DCCP_OPT_SLOW_RECV:
				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_SLOW_RECV!\n"));
				break;
			    case DCCP_OPT_BUF_CLOSED:
				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_BUF_CLOSED!\n"));
				break;
			    default:
				DCCP_DEBUG((LOG_INFO, "Got an unknown option, option = %u!\n", opt));
			}
		} else if (opt > 32 && opt < 36) {
			size = options[i+ 1];
			if (size < 3 || size > 10) {
				DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size));
				return;
			}
			/* Feature negotiations are options 33 to 35 */ 
			DCCP_DEBUG((LOG_INFO, "Got option %u, size = %u, feature = %u\n", opt, size, options[i+2]));
			memcpy(val, options + i + 3, size -3);
			DCCP_DEBUG((LOG_INFO, "Calling dccp_feature neg(%u, %u, options[%u + 1], %u)!\n", (u_int)dp, opt, i+ 1, (size - 3)));
			dccp_feature_neg(dp, opt, options[i+2], (size -3) , val);
			i += size - 1;

		} else if (opt < 128) {
			size = options[i+ 1];
			if (size < 3 || size > 10) {
				DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size));
				return;
			}

			switch (opt) {
			    case DCCP_OPT_RECV_BUF_DROPS:
					DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_RECV_BUF_DROPS, size = %u!\n", size));
					for (j=2; j < size; j++) {
						DCCP_DEBUG((LOG_INFO, "val[%u] = %u ", j-1, options[i+j]));
					}
					DCCP_DEBUG((LOG_INFO, "\n"));
				break;

			    case DCCP_OPT_TIMESTAMP:
					DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_TIMESTAMP, size = %u\n", size));

					/* Adding TimestampEcho to next outgoing */
					memcpy(val, options + i + 2, 4);
					memset(val + 4, 0, 4);
					dccp_add_option(dp, DCCP_OPT_TIMESTAMP_ECHO, val, 8);
				break;
				
			    case DCCP_OPT_TIMESTAMP_ECHO:
					DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_TIMESTAMP_ECHO, size = %u\n",size));
					for (j=2; j < size; j++) {
						DCCP_DEBUG((LOG_INFO, "val[%u] = %u ", j-1, options[i+j]));
					}
					DCCP_DEBUG((LOG_INFO, "\n"));

					/*
					memcpy(&(dp->timestamp_echo), options + i + 2, 4);
					memcpy(&(dp->timestamp_elapsed), options + i + 6, 4);
					ACK_DEBUG((LOG_INFO, "DATA; echo = %u , elapsed = %u\n",
					    dp->timestamp_echo, dp->timestamp_elapsed));
					*/
				
				break;

			case DCCP_OPT_ACK_VECTOR0:
			case DCCP_OPT_ACK_VECTOR1:
			case DCCP_OPT_ELAPSEDTIME:
				/* Dont do nothing here. Let the CC deal with it */
				break;
				
			default:
				DCCP_DEBUG((LOG_INFO, "Got an unknown option, option = %u, size = %u!\n", opt, size));
				break;

			}
			i += size - 1;

		} else {
			DCCP_DEBUG((LOG_INFO, "Got a CCID option (%d), do nothing!\n", opt));
			size = options[i+ 1];
			if (size < 3 || size > 10) {
				DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size));
				return;
			}
			i += size - 1;
		}
	}

}

int
dccp_add_feature(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, char *val, u_int8_t val_len)
{
	int i;
	DCCP_DEBUG((LOG_INFO, "Entering dccp_add_feature, opt = %u, feature = %u, val_len = %u\n", opt, feature, val_len));

	if (DCCP_MAX_OPTIONS > (dp->featlen + val_len + 3)) {
		dp->features[dp->featlen] = opt;
		dp->features[dp->featlen + 1] = val_len + 3;
		dp->features[dp->featlen +2] = feature;
		dp->featlen += 3;
		for (i = 0; i<val_len; i++) {
			dp->features[dp->featlen] = val[i];
			dp->featlen++;
		}
	} else {
		DCCP_DEBUG((LOG_INFO, "No room for more features, featlen = %u\n", dp->featlen));
		return -1;
	}

	return 0;
}

int
dccp_remove_feature(struct dccpcb *dp, u_int8_t opt, u_int8_t feature)
{
	int i = 0, j = 0, k;
	u_int8_t t_opt, t_feature, len;
	DCCP_DEBUG((LOG_INFO, "Entering dccp_remove_feature, featlen = %u, opt = %u, feature = %u\n", dp->featlen, opt, feature));

	while (i < dp->featlen) {
		t_opt = dp->features[i];
		len = dp->features[i+ 1];

		if (i + len > dp->featlen) {
			DCCP_DEBUG((LOG_INFO, "Error, len = %u and i(%u) + len > dp->featlen (%u)\n", len, i, dp->featlen));
			return 1;
		}
		t_feature = dp->features[i+2];

		if (t_opt == opt && t_feature == feature) {
			i += len;
		} else {
			if (i != j) {
				for (k = 0; k < len; k++) {
					dp->features[j+k] = dp->features[i+k];
				}
			}
			i += len;
			j += len;
		}
	}
	dp->featlen = j;
	DCCP_DEBUG((LOG_INFO, "Exiting dccp_remove_feature, featlen = %u\n", dp->featlen));
	return 0;
}

void
dccp_feature_neg(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, u_int8_t val_len, char *val)
{
	DCCP_DEBUG((LOG_INFO, "Running dccp_feature_neg, opt = %u, feature = %u len = %u ", opt, feature, val_len));

	switch (feature) {
		case DCCP_FEATURE_CC:
			DCCP_DEBUG((LOG_INFO, "Got CCID negotiation, opt = %u, val[0] = %u\n", opt, val[0]));
			if (opt == DCCP_OPT_CHANGE_R) {
				if (val[0] == 2 || val[0] == 3 || val[0] == 0) {
					/* try to use preferable CCID */
					int i;
					for (i = 1; i < val_len; i ++) if (val[i] == dp->pref_cc) val[0] = dp->pref_cc;
					DCCP_DEBUG((LOG_INFO, "Sending DCCP_OPT_CONFIRM_L on CCID %u\n", val[0]));
					dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_CC);
					dccp_add_feature_option(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_CC , val, 1);
					if (dp->cc_in_use[0] < 1) {
						dp->cc_state[0] = (*cc_sw[val[0] + 1].cc_send_init)(dp);
						dp->cc_in_use[0] = val[0] + 1;
					} else {
						DCCP_DEBUG((LOG_INFO, "We already have negotiated a CC!!!\n"));
					}
				}
			} else if (opt == DCCP_OPT_CONFIRM_L) {
				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_CONFIRM_L on CCID %u\n", val[0]));
				dccp_remove_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC);
				if (dp->cc_in_use[1] < 1) {
					dp->cc_state[1] = (*cc_sw[val[0] + 1].cc_recv_init)(dp);
					dp->cc_in_use[1] = val[0] + 1;
					DCCP_DEBUG((LOG_INFO, "confirmed cc_in_use[1] = %d\n", dp->cc_in_use[1]));
				} else {
					DCCP_DEBUG((LOG_INFO, "We already have negotiated a CC!!! (confirm) %d\n", dp->cc_in_use[1]));
				}
			}
		
		break;

		case DCCP_FEATURE_ACKVECTOR:
			ACK_DEBUG((LOG_INFO, "Got _Use Ack Vector_\n"));
			if (opt == DCCP_OPT_CHANGE_R) {
				if (val[0] == 1) {
					dccp_use_ackvector(dp);
					dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR);
					dccp_add_feature_option(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR , val, 1);
				} else {
					ACK_DEBUG((LOG_INFO, "ERROR. Strange val %u\n", val[0]));
				}
			} else if (opt == DCCP_OPT_CONFIRM_L) {
					dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR);
			if (val[0] == 1) {
					dp->remote_ackvector = 1;
					ACK_DEBUG((LOG_INFO,"Remote side confirmed AckVector usage\n"));
				} else {
					ACK_DEBUG((LOG_INFO, "ERROR. Strange val %u\n", val[0]));
				}
			}
			break;
			
		case DCCP_FEATURE_ACKRATIO:
			if (opt == DCCP_OPT_CHANGE_R) {
				memcpy(&(dp->ack_ratio), val, 1);
				ACK_DEBUG((LOG_INFO, "Feature: Change Ack Ratio to %u\n", dp->ack_ratio));
			}
			break;
			
		case DCCP_FEATURE_ECN:
		case DCCP_FEATURE_MOBILITY:
		default:
			/* we should send back empty CONFIRM_L for unknown feature unless it's not mandatory */
			dccp_add_option(dp, DCCP_OPT_CONFIRM_L, NULL, 0);
		break;

	}
}

#ifdef __FreeBSD__
static int
dccp_pcblist(SYSCTL_HANDLER_ARGS)
{

	int error, i, n, s;
	struct inpcb *inp, **inp_list;
	inp_gen_t gencnt;
	struct xinpgen xig;

	/*
	 * The process of preparing the TCB list is too time-consuming and
	 * resource-intensive to repeat twice on every request.
	 */
	if (req->oldptr == 0) {
		n = dccpbinfo.ipi_count;
		req->oldidx = 2 * (sizeof xig)
			+ (n + n/8) * sizeof(struct xdccpcb);
		return 0;
	}


	if (req->newptr != 0)
		return EPERM;


	/*
	 * OK, now we're committed to doing something.
	 */
	s = splnet();
	gencnt = dccpbinfo.ipi_gencnt;
	n = dccpbinfo.ipi_count;
	splx(s);

#if __FreeBSD_version >= 500000
	sysctl_wire_old_buffer(req, 2 * (sizeof xig)
		+ n * sizeof(struct xdccpcb));
#endif

	xig.xig_len = sizeof xig;
	xig.xig_count = n;
	xig.xig_gen = gencnt;
	xig.xig_sogen = so_gencnt;
	error = SYSCTL_OUT(req, &xig, sizeof xig);
	if (error)
		return error;

	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
	if (inp_list == 0)
		return ENOMEM;
	
	s = splsoftnet();
	INP_INFO_RLOCK(&dccpbinfo);

	for (inp = LIST_FIRST(dccpbinfo.listhead), i = 0; inp && i < n;
	     inp = LIST_NEXT(inp, inp_list)) {
		INP_LOCK(inp);
		if (inp->inp_gencnt <= gencnt &&
#if __FreeBSD_version >= 500000
		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
#else
		    !prison_xinpcb(req->p, inp))
#endif
			inp_list[i++] = inp;
		INP_UNLOCK(inp);
	}
	INP_INFO_RUNLOCK(&dccpbinfo);
	splx(s);
	n = i;

	error = 0;
	for (i = 0; i < n; i++) {
		inp = inp_list[i];
		INP_LOCK(inp);

		if (inp->inp_gencnt <= gencnt) {
			struct xdccpcb xd;
			vaddr_t inp_ppcb;
			xd.xd_len = sizeof xd;
			/* XXX should avoid extra copy */
			memcpy(&xd.xd_inp, inp, sizeof *inp);
			inp_ppcb = inp->inp_ppcb;
			if (inp_ppcb != NULL)
				memcpy(&xd.xd_dp, inp_ppcb, sizeof xd.xd_dp);
			else
				memset((char *) &xd.xd_dp, 0, sizeof xd.xd_dp);
			if (inp->inp_socket)
				 sotoxsocket(inp->inp_socket, &xd.xd_socket);
			error = SYSCTL_OUT(req, &xd, sizeof xd);
		}
		INP_UNLOCK(inp);
	}
	if (!error) {
		/*
		 * Give the user an updated idea of our state.
		 * If the generation differs from what we told
		 * her before, she knows that something happened
		 * while we were processing this request, and it
		 * might be necessary to retry.
		 */
		s = splnet();
		INP_INFO_RLOCK(&dccpbinfo);
		xig.xig_gen = dccpbinfo.ipi_gencnt;
		xig.xig_sogen = so_gencnt;
		xig.xig_count = dccpbinfo.ipi_count;


		INP_INFO_RUNLOCK(&dccpbinfo);
		splx(s);
		error = SYSCTL_OUT(req, &xig, sizeof xig);
	}
	free(inp_list, M_TEMP);
	return error;
}
#endif

#ifdef __FreeBSD__
SYSCTL_PROC(_net_inet_dccp, DCCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
    dccp_pcblist, "S,xdccpcb", "List of active DCCP sockets");
#endif

void
dccp_timewait_t(void *dcb)
{
	struct dccpcb *dp = dcb;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_timewait_t!\n"));
	mutex_enter(softnet_lock);
	INP_INFO_WLOCK(&dccpbinfo);
	INP_LOCK(dp->d_inpcb);
	dccp_close(dp);
	INP_INFO_WUNLOCK(&dccpbinfo);
	mutex_exit(softnet_lock);
}

void
dccp_connect_t(void *dcb)
{
	struct dccpcb *dp = dcb;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_connect_t!\n"));
	mutex_enter(softnet_lock);
	INP_INFO_WLOCK(&dccpbinfo);
	INP_LOCK(dp->d_inpcb);
	dccp_close(dp);
	INP_INFO_WUNLOCK(&dccpbinfo);
	mutex_exit(softnet_lock);
}

void
dccp_close_t(void *dcb)
{
	struct dccpcb *dp = dcb;

	DCCP_DEBUG((LOG_INFO, "Entering dccp_close_t!\n"));
	mutex_enter(softnet_lock);
	INP_INFO_WLOCK(&dccpbinfo);
	dp->state = DCCPS_TIME_WAIT; /* HMM */
	if (dp->who == DCCP_SERVER) {
		INP_LOCK(dp->d_inpcb);
		KERNEL_LOCK(1, NULL);
		dccp_output(dp, DCCP_TYPE_RESET + 2);
		KERNEL_UNLOCK_ONE(NULL);
		dccp_close(dp);
	} else {
		INP_LOCK(dp->d_inpcb);
		dccp_output(dp, DCCP_TYPE_RESET + 2);
		/*dp->state = DCCPS_TIME_WAIT; */
		callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER,
		    dccp_timewait_t, dp);
		INP_UNLOCK(dp->d_inpcb);
	}
	INP_INFO_WUNLOCK(&dccpbinfo);
	mutex_exit(softnet_lock);
}

void
dccp_retrans_t(void *dcb)
{
	struct dccpcb *dp = dcb;
	/*struct inpcb *inp;*/

	DCCP_DEBUG((LOG_INFO, "Entering dccp_retrans_t!\n"));
	mutex_enter(softnet_lock);
	INP_INFO_RLOCK(&dccpbinfo);
	/*inp = dp->d_inpcb;*/
	INP_LOCK(inp);
	INP_INFO_RUNLOCK(&dccpbinfo);
	callout_stop(&dp->retrans_timer);
	KERNEL_LOCK(1, NULL);
	dccp_output(dp, 0);
	KERNEL_UNLOCK_ONE(NULL);
	dp->retrans = dp->retrans * 2;
	callout_reset(&dp->retrans_timer, dp->retrans, dccp_retrans_t, dp);
	INP_UNLOCK(inp);
	mutex_exit(softnet_lock);
}

static int
dccp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
{
	int error = 0;
	int family;

	family = so->so_proto->pr_domain->dom_family;
	switch (family) {
	case PF_INET:
		error = in_control(so, cmd, nam, ifp);
		break;
#ifdef INET6
	case PF_INET6:
		error = in6_control(so, cmd, nam, ifp);
		break;
#endif
	default:
		error =	 EAFNOSUPPORT;
	}
	return (error);
}

static int
dccp_stat(struct socket *so, struct stat *ub)
{
	return 0;
}

static int
dccp_peeraddr(struct socket *so, struct sockaddr *nam)
{

	KASSERT(solocked(so));
	KASSERT(sotoinpcb(so) != NULL);
	KASSERT(nam != NULL);

	inpcb_fetch_peeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
	return 0;
}

static int
dccp_sockaddr(struct socket *so, struct sockaddr *nam)
{

	KASSERT(solocked(so));
	KASSERT(sotoinpcb(so) != NULL);
	KASSERT(nam != NULL);

	inpcb_fetch_sockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
	return 0;
}

static int
dccp_rcvd(struct socket *so, int flags, struct lwp *l)
{
	KASSERT(solocked(so));

	return EOPNOTSUPP;
}

static int
dccp_recvoob(struct socket *so, struct mbuf *m, int flags)
{
	KASSERT(solocked(so));

	return EOPNOTSUPP;
}

static int
dccp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
{
	KASSERT(solocked(so));

	m_freem(m);
	m_freem(control);

	return EOPNOTSUPP;
}

static int
dccp_purgeif(struct socket *so, struct ifnet *ifp)
{
	int s;

	s = splsoftnet();
	mutex_enter(softnet_lock);
	inpcb_purgeif0(&dccpbtable, ifp);
	in_purgeif(ifp);
	inpcb_purgeif(&dccpbtable, ifp);
	mutex_exit(softnet_lock);
	splx(s);

	return 0;
}

/****** Ack Vector functions *********/

/**
 * Initialize and allocate mem for Ack Vector
 **/
void
dccp_use_ackvector(struct dccpcb *dp)
{
	DCCP_DEBUG((LOG_INFO,"Initializing AckVector\n"));
	if (dp->ackvector != 0) {
		DCCP_DEBUG((LOG_INFO, "It was already initialized!!!\n"));
		return;
	}
	dp->av_size = DCCP_VECTORSIZE;
	/* need 2 bits per entry */
	dp->ackvector = malloc(dp->av_size/4, M_PCB, M_NOWAIT | M_ZERO);
	if (dp->ackvector == 0) {
		DCCP_DEBUG((LOG_INFO, "Unable to allocate memory for ackvector\n"));
		/* What to do now? */
		dp->av_size = 0;
		return;
	}
	memset(dp->ackvector, 0xff, dp->av_size/4);
	dp->av_hs = dp->av_ts = 0;
	dp->av_hp = dp->ackvector;
}

/**
 * Set 'seqnr' as the new head in ackvector
 **/
void
dccp_update_ackvector(struct dccpcb *dp, u_int64_t seqnr)
{
	int64_t gap;
	u_char *t;

	/* Ignore wrapping for now */
	
	ACK_DEBUG((LOG_INFO,"New head in ackvector: %u\n", seqnr));
	
	if (dp->av_size == 0) {
		ACK_DEBUG((LOG_INFO, "Update: AckVector NOT YET INITIALIZED!!!\n"));
		dccp_use_ackvector(dp);
	}
	
	if (seqnr > dp->av_hs) {
		gap = seqnr - dp->av_hs;
	} else {
		/* We received obsolete information */
		return;
	}
	
	t = dp->av_hp + (gap/4);
	if (t >= (dp->ackvector + (dp->av_size/4)))
		t -= (dp->av_size / 4); /* ackvector wrapped */
	dp->av_hp = t;
	dp->av_hs = seqnr;
}

/**
 * We've received a packet. store in local av so it's included in
 * next Ack Vector sent
 **/
void
dccp_increment_ackvector(struct dccpcb *dp, u_int64_t seqnr)
{
	u_int64_t offset, dc;
	int64_t gap;
	u_char *t, *n;
	
	DCCP_DEBUG((LOG_INFO, "Entering dccp_increment_ackvecktor %d\n", dp->av_size));
	if (dp->av_size == 0) {
		DCCP_DEBUG((LOG_INFO, "Increment: AckVector NOT YET INITIALIZED!!!\n"));
		dccp_use_ackvector(dp);
	}
	
	if (dp->av_hs == dp->av_ts) {
		/* Empty ack vector */
		dp->av_hs = dp->av_ts = seqnr;
	}

	/* Check for wrapping */
	if (seqnr >= dp->av_hs) {
		/* Not wrapped */
		gap = seqnr - dp->av_hs;
	} else {
		/* Wrapped */
		gap = seqnr + 0x1000000000000LL - dp->av_hs; /* seqnr = 48 bits */
	}
	DCCP_DEBUG((LOG_INFO, "dccp_increment_ackvecktor gap=%llu av_size %d\n", gap, dp->av_size));

	if (gap >= dp->av_size) {
		/* gap is bigger than ackvector size? baaad */
		/* maybe we should increase the ackvector here */
		DCCP_DEBUG((LOG_INFO, "increment_ackvector error. gap: %llu, av_size: %d, seqnr: %d\n",
			    gap, dp->av_size, seqnr));
		return;
	}
	
	offset = gap % 4; /* hi or low 2 bits to mark */
	t = dp->av_hp + (gap/4);
	if (t >= (dp->ackvector + (dp->av_size/4)))
		t -= (dp->av_size / 4); /* ackvector wrapped */
	
	*t = *t & (~(0x03 << (offset *2))); /* turn off bits, 00 is rcvd, 11 is missing */

	dp->av_ts = seqnr + 1;
	if (dp->av_ts == 0x1000000000000LL)
		dp->av_ts = 0;

	if (gap > (dp->av_size - 128)) {
		n = malloc(dp->av_size/2, M_PCB, M_NOWAIT | M_ZERO); /* old size * 2 */
		memset (n + dp->av_size / 4, 0xff, dp->av_size / 4); /* new half all missing */
		dc = (dp->ackvector + (dp->av_size/4)) - dp->av_hp;
		memcpy (n, dp->av_hp, dc); /* tail to end */
		memcpy (n+dc, dp->ackvector, dp->av_hp - dp->ackvector); /* start to tail */
		dp->av_size = dp->av_size * 2; /* counted in items, so it';s a doubling */
		free (dp->ackvector, M_PCB);
		dp->av_hp = dp->ackvector = n;
	}
}

/**
 * Generates the ack vector to send in outgoing packet.
 * These are backwards (first packet in ack vector is packet indicated by Ack Number,
 * subsequent are older packets).
 **/

u_int16_t
dccp_generate_ackvector(struct dccpcb *dp, u_char *buf)
{
	int64_t j;
	u_int64_t i;
	u_int16_t cnt, oldlen, bufsize;
	u_char oldstate, st;

	bufsize = 16;
	cnt = 0;

	oldstate = 0x04; /* bad value */
	oldlen = 0;
	
	if (dp->av_size == 0) {
		ACK_DEBUG((LOG_INFO, "Generate: AckVector NOT YET INITIALIZED!!!\n"));
		return 0;
	}

	if (dp->seq_rcv > dp->av_ts) {
		/* AckNum is beyond our av-list , so we'll start with some
		 * 0x3 (Packet not yet received) */
		j = dp->seq_rcv - dp->av_ts -1;
		do {
			/* state | length */
			oldstate = 0x03;
			if (j > 63)
				oldlen = 63;
			else
				oldlen = j;
			
			buf[cnt] = (0x03 << 6) | oldlen;
			cnt++;
			if (cnt == bufsize) {
				/* I've skipped the realloc bshit */
				/* PANIC */
			}
			j-=63;
		} while (j > 0);
	}
	
	/* Ok now we're at dp->av_ts (unless AckNum is lower) */
	i = (dp->seq_rcv < dp->av_ts) ? dp->seq_rcv : dp->av_ts;
	st = dccp_ackvector_state(dp, i);

	if (st == oldstate) {
		cnt--;
		oldlen++;
	} else {
		oldlen = 0;
		oldstate = st;
	}

	if (dp->av_ts > dp->av_hs) {
		do {
			i--;
			st = dccp_ackvector_state(dp, i);
			if (st == oldstate && oldlen < 64) {
				oldlen++;
			} else {
				buf[cnt] = (oldstate << 6) | (oldlen & 0x3f);
				cnt++;
				oldlen = 0;
				oldstate = st;
				if (cnt == bufsize) {
					/* PANIC */
				}
			}
			
		} while (i > dp->av_hs);
	} else {
		/* It's wrapped */
		do {
			i--;
			st = dccp_ackvector_state(dp, i);
			if (st == oldstate && oldlen < 64) {
				oldlen++;
			} else {
				buf[cnt] = (oldstate << 6) | (oldlen & 0x3f);
				cnt++;
				oldlen = 0;
				oldstate = st;
				if (cnt == bufsize) {
					/* PANIC */
				}
			}
			
		} while (i > 0);
		i = 0x1000000;
		do {
			i--;
			st = dccp_ackvector_state(dp, i);
			if (st == oldstate && oldlen < 64) {
				oldlen++;
			} else {
				buf[cnt] = (oldstate << 6) | (oldlen & 0x3f);
				cnt++;
				oldlen = 0;
				oldstate = st;
				if (cnt == bufsize) {
					/* PANIC */
				}
			}
		} while (i > dp->av_hs);
	}
	
	/* add the last one */
	buf[cnt] = (oldstate << 6) | (oldlen & 0x3f);
	cnt++;

	return cnt;
}

u_char
dccp_ackvector_state(struct dccpcb *dp, u_int64_t seqnr)
{
	u_int64_t gap, offset;
	u_char *t;

	/* Check for wrapping */
	if (seqnr >= dp->av_hs) {
		/* Not wrapped */
		gap = seqnr - dp->av_hs;
	} else {
		/* Wrapped */
		gap = seqnr + 0x1000000000000LL - dp->av_hs; /* seq nr = 48 bits */
	}

	if (gap >= dp->av_size) {
		/* gap is bigger than ackvector size? baaad */
		return 0x03;
	}

	offset = gap % 4 *2;
	t = dp->av_hp + (gap/4);
	if (t >= (dp->ackvector + (dp->av_size/4)))
		t -= (dp->av_size / 4); /* wrapped */
	
	return ((*t & (0x03 << offset)) >> offset);
}

/****** End of Ack Vector functions *********/

/* No cc functions */
void *
dccp_nocc_init(struct dccpcb *pcb)
{
  return (void*) 1;
}

void
dccp_nocc_free(void *ccb)
{
}

int
dccp_nocc_send_packet(void *ccb, long size)
{
  return 1;
}

void
dccp_nocc_send_packet_sent(void *ccb, int moreToSend, long size)
{
}

void
dccp_nocc_packet_recv(void *ccb, char* options ,int optlen)
{
}

void
dccp_log(int level, const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	vprintf(format,  ap);
	va_end(ap);
	return;
}

/*
 * Sysctl for dccp variables.
 */
SYSCTL_SETUP(sysctl_net_inet_dccp_setup, "sysctl net.inet.dccp subtree setup")
{

	sysctl_createv(clog, 0, NULL, NULL,
		CTLFLAG_PERMANENT,
		CTLTYPE_NODE, "net", NULL,
		NULL, 0, NULL, 0,
		CTL_NET, CTL_EOL);

	sysctl_createv(clog, 0, NULL, NULL,
		CTLFLAG_PERMANENT,
		CTLTYPE_NODE, "inet", NULL,
		NULL, 0, NULL, 0,
		CTL_NET, PF_INET, CTL_EOL);

	sysctl_createv(clog, 0, NULL, NULL,
		CTLFLAG_PERMANENT,
		CTLTYPE_NODE, "dccp",
		SYSCTL_DESCR("DCCPv4 related settings"),
		NULL, 0, NULL, 0,
		CTL_NET, PF_INET, IPPROTO_DCCP, CTL_EOL);

	sysctl_createv(clog, 0, NULL, NULL,
		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
		CTLTYPE_INT, "dccp_log_in_vain",
		SYSCTL_DESCR("log all connection attempt"),
		NULL, 0, &dccp_log_in_vain, 0,
		CTL_NET, PF_INET, IPPROTO_DCCP, DCCPCTL_LOGINVAIN,
		CTL_EOL);

	sysctl_createv(clog, 0, NULL, NULL,
		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
		CTLTYPE_INT, "do_feature_nego",
		SYSCTL_DESCR("enable feature negotiation"),
		NULL, 0, &dccp_do_feature_nego, 0,
		CTL_NET, PF_INET, IPPROTO_DCCP, DCCPCTL_DOFEATURENEGO,
		CTL_EOL);
}

PR_WRAP_USRREQS(dccp)
#define	dccp_attach	dccp_attach_wrapper
#define	dccp_detach	dccp_detach_wrapper
#define dccp_accept	dccp_accept_wrapper
#define	dccp_bind	dccp_bind_wrapper
#define	dccp_listen	dccp_listen_wrapper
#define	dccp_connect	dccp_connect_wrapper
#define	dccp_connect2	dccp_connect2_wrapper
#define	dccp_disconnect	dccp_disconnect_wrapper
#define	dccp_shutdown	dccp_shutdown_wrapper
#define	dccp_abort	dccp_abort_wrapper
#define	dccp_ioctl	dccp_ioctl_wrapper
#define	dccp_stat	dccp_stat_wrapper
#define	dccp_peeraddr	dccp_peeraddr_wrapper
#define	dccp_sockaddr	dccp_sockaddr_wrapper
#define	dccp_rcvd	dccp_rcvd_wrapper
#define	dccp_recvoob	dccp_recvoob_wrapper
#define	dccp_send	dccp_send_wrapper
#define	dccp_sendoob	dccp_sendoob_wrapper
#define	dccp_purgeif	dccp_purgeif_wrapper

const struct pr_usrreqs dccp_usrreqs = {
	.pr_attach	= dccp_attach,
	.pr_detach	= dccp_detach,
	.pr_accept	= dccp_accept,
	.pr_bind	= dccp_bind,
	.pr_listen	= dccp_listen,
	.pr_connect	= dccp_connect,
	.pr_connect2	= dccp_connect2,
	.pr_disconnect	= dccp_disconnect,
	.pr_shutdown	= dccp_shutdown,
	.pr_abort	= dccp_abort,
	.pr_ioctl	= dccp_ioctl,
	.pr_stat	= dccp_stat,
	.pr_peeraddr	= dccp_peeraddr,
	.pr_sockaddr	= dccp_sockaddr,
	.pr_rcvd	= dccp_rcvd,
	.pr_recvoob	= dccp_recvoob,
	.pr_send	= dccp_send,
	.pr_sendoob	= dccp_sendoob,
	.pr_purgeif	= dccp_purgeif,
};