Linux Audio

Check our new training course

Loading...
   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
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		Implementation of the Transmission Control Protocol(TCP).
 *
 * Version:	@(#)tcp_input.c	1.0.16	05/25/93
 *
 * Authors:	Ross Biro, <bir7@leland.Stanford.Edu>
 *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
 *		Mark Evans, <evansmp@uhura.aston.ac.uk>
 *		Corey Minyard <wf-rch!minyard@relay.EU.net>
 *		Florian La Roche, <flla@stud.uni-sb.de>
 *		Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
 *		Linus Torvalds, <torvalds@cs.helsinki.fi>
 *		Alan Cox, <gw4pts@gw4pts.ampr.org>
 *		Matthew Dillon, <dillon@apollo.west.oic.com>
 *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
 *		Jorge Cwik, <jorge@laser.satlink.net>
 */

/*
 *	TODO
 *		- A better sock cache
 *
 */

/*
 * Changes:
 *		Pedro Roque	:	Fast Retransmit/Recovery.
 *					Two receive queues.
 *					Retransmit queue handled by TCP.
 *					Better retransmit timer handling.
 *					New congestion avoidance.
 *					Header prediction.
 *					Variable renaming.
 *
 *		Eric		:	Fast Retransmit.
 *		Randy Scott	:	MSS option defines.
 *		Eric Schenk	:	Fixes to slow start algorithm.
 *		Eric Schenk	:	Yet another double ACK bug.
 *		Eric Schenk	:	Delayed ACK bug fixes.
 *		Eric Schenk	:	Floyd style fast retrans war avoidance.
 */

#include <linux/config.h>
#include <linux/mm.h>
#include <linux/sysctl.h>
#include <net/tcp.h>



typedef void			(*tcp_sys_cong_ctl_t)(struct sock *sk,
						      u32 seq, u32 ack,
						      u32 seq_rtt);

static void tcp_cong_avoid_vanj(struct sock *sk, u32 seq, u32 ack,
				u32 seq_rtt);
static void tcp_cong_avoid_vegas(struct sock *sk, u32 seq, u32 ack,
				 u32 seq_rtt);

int sysctl_tcp_cong_avoidance = 0;

static tcp_sys_cong_ctl_t tcp_sys_cong_ctl_f = &tcp_cong_avoid_vanj;

/*
 *	Called each time to estimate the delayed ack timeout. This is
 *	how it should be done so a fast link isnt impacted by ack delay.
 *
 *	I think we need a medium deviation here also...
 *	The estimated value is changing to fast
 */
 
static void tcp_delack_estimator(struct tcp_opt *tp)
{
	int m;

	/*
	 *	Delayed ACK time estimator.
	 */
	
	m = jiffies - tp->lrcvtime;

	tp->lrcvtime = jiffies;

	if (m < 0)
		return;

	/*
	 * if the mesured value is bigger than
	 * twice the round trip time ignore it.
	 */
	if ((m << 2) <= tp->srtt) 
	{
		m -= (tp->iat >> 3);
		tp->iat += m;

		if (m <0)
			m = -m;

		m -= (tp->iat_mdev >> 2);
		tp->iat_mdev += m;

		tp->ato = (tp->iat >> 3) + (tp->iat_mdev >> 2);

		if (tp->ato < HZ/50)
			tp->ato = HZ/50;
	}
	else
		tp->ato = 0;
}

/*
 *	Called on frames that were known _not_ to have been
 *	retransmitted [see Karn/Partridge Proceedings SIGCOMM 87]. 
 *	The algorithm is from the SIGCOMM 88 piece by Van Jacobson.
 */

extern __inline__ void tcp_rtt_estimator(struct tcp_opt *tp, __u32 mrtt)
{
	long m;
	/*
	 *	The following amusing code comes from Jacobson's
	 *	article in SIGCOMM '88.  Note that rtt and mdev
	 *	are scaled versions of rtt and mean deviation.
	 *	This is designed to be as fast as possible 
	 *	m stands for "measurement".
	 */
	/*
	 *	On a 1990 paper the rto value is changed to:
	 *	RTO = rtt + 4 * mdev
	 */

	m = mrtt;  /* RTT */

	if (tp->srtt != 0) {
		if(m<=0)
			m=1;		/* IS THIS RIGHT FOR <0 ??? */
		m -= (tp->srtt >> 3);	/* m is now error in rtt est */
		tp->srtt += m;		/* rtt = 7/8 rtt + 1/8 new */
		if (m < 0)
			m = -m;		/* m is now abs(error) */
		m -= (tp->mdev >> 2);   /* similar update on mdev */
		tp->mdev += m;	    	/* mdev = 3/4 mdev + 1/4 new */
	} else {
					/* no previous measure. */
		tp->srtt = m<<3;	/* take the measured time to be rtt */
		tp->mdev = m<<2;	/* make sure rto = 3*rtt */
	}


	/*
	 *	Now update timeout.  Note that this removes any backoff.
	 */
			 
	tp->rto = (tp->srtt >> 3) + tp->mdev;

	if (tp->rto > 120*HZ)
		tp->rto = 120*HZ;

	/* Was 1*HZ - keep .2 as minimum cos of the BSD delayed acks */
	if (tp->rto < HZ/5)
		tp->rto = HZ/5;

	tp->backoff = 0;
}
 

/*
 *	This functions checks to see if the tcp header is actually acceptable. 
 */
 
extern __inline__ int tcp_sequence(struct tcp_opt *tp, u32 seq, u32 seg_nxt)
{
	u32 end_window = tp->rcv_wup + tp->rcv_wnd;
	u32 end_seq = seg_nxt;

	/*
	 *	When the window is open (most common case)
	 *	we want to accept segments if they have yet unseen data
	 *	or in the case of a dataless segment if seg.seq == rcv.nxt
	 *	this means:
	 *
	 *	if (seq == end_seq)
	 *		end_seq >= rcv.nxt
	 *	else
	 *		end_seq >  rcv.nxt
	 */

	if (seq == end_seq)
		end_seq++;

	return ((before(seq, end_window) && after(end_seq, tp->rcv_nxt)) ||
		(seq == end_window && seq == end_seq));
}

/*
 *	When we get a reset we do this. This probably is a tcp_output routine
 *	really.
 */

static int tcp_reset(struct sock *sk, struct sk_buff *skb)
{
	sk->zapped = 1;
	/*
	 *	We want the right error as BSD sees it (and indeed as we do).
	 */
	switch (sk->state) {
		case TCP_TIME_WAIT:
			break;
		case TCP_SYN_SENT:
			sk->err = ECONNREFUSED;
			break;
		case TCP_CLOSE_WAIT:
			sk->err = EPIPE;
			break;
		default:
			sk->err = ECONNRESET;
	}
#ifdef CONFIG_TCP_RFC1337
	/*
	 *	Time wait assassination protection [RFC1337]
	 *
	 *	This is a good idea, but causes more sockets to take time to close.
	 *
	 *	Ian Heavens has since shown this is an inadequate fix for the protocol
	 *	bug in question.
	 */
	if(sk->state!=TCP_TIME_WAIT)
	{	
		tcp_set_state(sk,TCP_CLOSE);
		sk->shutdown = SHUTDOWN_MASK;
	}
#else	
	tcp_set_state(sk,TCP_CLOSE);
	sk->shutdown = SHUTDOWN_MASK;
#endif	
	if (!sk->dead) 
		sk->state_change(sk);

	return(0);
}


/*
 *	Look for tcp options. Parses everything but only knows about MSS.
 *	This routine is always called with the packet containing the SYN.
 *	However it may also be called with the ack to the SYN.  So you
 *	can't assume this is always the SYN.  It's always called after
 *	we have set up sk->mtu to our own MTU.
 *
 *	We need at minimum to add PAWS support here. Possibly large windows
 *	as Linux gets deployed on 100Mb/sec networks.
 */
 
int tcp_parse_options(struct tcphdr *th)
{
	unsigned char *ptr;
	int length=(th->doff*4)-sizeof(struct tcphdr);
	int mss = 0;
    
	ptr = (unsigned char *)(th + 1);
  
	while(length>0)
	{
	  	int opcode=*ptr++;
	  	int opsize=*ptr++;
	  	switch(opcode)
	  	{
	  		case TCPOPT_EOL:
	  			return 0;
	  		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
	  			length--;
	  			ptr--;		/* the opsize=*ptr++ above was a mistake */
	  			continue;
	  		
	  		default:
	  			if(opsize<=2)	/* Avoid silly options looping forever */
	  				return 0;
	  			switch(opcode)
	  			{
	  				case TCPOPT_MSS:
	  					if(opsize==TCPOLEN_MSS && th->syn)
	  					{
							mss = ntohs(*(unsigned short *)ptr);
	  					}
	  					break;
		  				/* Add other options here as people feel the urge to implement stuff like large windows */
	  			}
	  			ptr+=opsize-2;
	  			length-=opsize;
	  	}
	}

	return mss;
}


/* 
 *  See draft-stevens-tcpca-spec-01 for documentation.
 */

static void tcp_fast_retrans(struct sock *sk, u32 ack, int not_dup)
{
	struct tcp_opt *tp=&(sk->tp_pinfo.af_tcp);

	/*
	 * An ACK is a duplicate if:
	 * (1) it has the same sequence number as the largest number we've 
	 *     seen,
	 * (2) it has the same window as the last ACK,
	 * (3) we have outstanding data that has not been ACKed
	 * (4) The packet was not carrying any data.
	 * (5) [From Floyds paper on fast retransmit wars]
	 *     The packet acked data after high_seq;
	 */

	if (ack == tp->snd_una && sk->packets_out && (not_dup == 0) &&
	    after(ack, tp->high_seq))
	{
		
		sk->dup_acks++;	
		

		/*
		 * 1. When the third duplicate ack is received, set ssthresh 
		 * to one half the current congestion window, but no less 
		 * than two segments. Retransmit the missing segment.
		 */
	
		if (sk->dup_acks == 3) 
		{
			sk->ssthresh = max(sk->cong_window >> 1, 2);
			sk->cong_window = sk->ssthresh + 3;
			tcp_do_retransmit(sk, 0);
		}

		/*
		 * 2. Each time another duplicate ACK arrives, increment 
		 * cwnd by the segment size. [...] Transmit a packet...
		 *
		 * Packet transmission will be done on normal flow processing
		 * since we're not in "retransmit mode"
		 */
		
		if (sk->dup_acks > 3) 
		{
			sk->cong_window++;
		}
	}
	else
	{
		/*
		 * 3. When the next ACK arrives that acknowledges new data,
		 *    set cwnd to ssthresh
		 */

		if (sk->dup_acks >= 3)
		{
			sk->tp_pinfo.af_tcp.retrans_head = NULL;
			sk->cong_window = sk->ssthresh;
			sk->retransmits = 0;
		}
		sk->dup_acks = 0;
	}
	
}

/*
 *      TCP slow start and congestion avoidance in two flavors:
 *      RFC 1122 and TCP Vegas.
 *
 *      This is a /proc/sys configurable option. 
 */

#define SHIFT_FACTOR 16

static void tcp_cong_avoid_vegas(struct sock *sk, u32 seq, u32 ack,
				 u32 seq_rtt)
{
	/*
	 *	From:
	 *      TCP Vegas: New Techniques for Congestion 
	 *	Detection and Avoidance.
	 *              
	 *
	 *	Warning: This code is a scratch implementation taken
	 *	from the paper only. The code they distribute seams
	 *	to have improved several things over the initial spec.
	 */

	struct tcp_opt * tp;
	unsigned int Actual, Expected;
	unsigned int inv_rtt, inv_basertt; 
	u32 snt_bytes;
	

	tp = &(sk->tp_pinfo.af_tcp);

	if (!seq_rtt)
		seq_rtt = 1;
	
	if (tp->basertt)
		tp->basertt = min(seq_rtt, tp->basertt);
	else
		tp->basertt = seq_rtt;

	/*
	 * 
	 *	Actual	 = throughput for this segment.
	 *	Expected = number_of_bytes in transit / BaseRTT
	 * 
	 */

	snt_bytes = (ack - seq) << SHIFT_FACTOR;
	inv_rtt = (1 << SHIFT_FACTOR) / seq_rtt;
       
	Actual =  snt_bytes * inv_rtt;

	inv_basertt = (1 << SHIFT_FACTOR) / tp->basertt;
	Expected = ((tp->snd_nxt - tp->snd_una) << SHIFT_FACTOR) * inv_basertt;

	/*
	 *      Slow Start
	 */
	
	if (sk->cong_window < sk->ssthresh &&
	    (seq == tp->snd_nxt ||
	      (((Expected - Actual) <=
		((TCP_VEGAS_GAMMA << SHIFT_FACTOR) * sk->mss * inv_basertt))
	       )
	     ))
	{
		/*
		 * "Vegas allows exponential growth only every other
		 *  RTT"
		 */
			
		if (!(sk->cong_count++))
		{
			sk->cong_window++;
			sk->cong_count = 0;
		}
	}
	else 
	{
		/*
		 *      Congestion Avoidance
		 */
			
		if (Expected - Actual <=
		    ((TCP_VEGAS_ALPHA << SHIFT_FACTOR) * sk->mss * inv_basertt))
		{
			/* Increase Linearly */
				
			if (sk->cong_count++ >= sk->cong_window)
			{
				sk->cong_window++;
				sk->cong_count = 0;
			}
		}
			
		if (Expected - Actual >=
		    ((TCP_VEGAS_BETA << SHIFT_FACTOR) * sk->mss * inv_basertt))
		{
			/* Decrease Linearly */
				
			if (sk->cong_count++ >= sk->cong_window)
			{
				sk->cong_window--;
				sk->cong_count = 0;
			}
			
			/* Never less than 2 segments */
			if (sk->cong_window < 2)
				sk->cong_window = 2;
		}
	}
}

static void tcp_cong_avoid_vanj(struct sock *sk, u32 seq, u32 ack, u32 seq_rtt)
{
	
        /* 
         * This is Jacobson's slow start and congestion avoidance. 
         * SIGCOMM '88, p. 328.  Because we keep cong_window in 
         * integral mss's, we can't do cwnd += 1 / cwnd.  
         * Instead, maintain a counter and increment it once every 
         * cwnd times.  
         */

        if (sk->cong_window <= sk->ssthresh)  
	{
                /* 
                 *	In "safe" area, increase
                 */

                sk->cong_window++;
	}
        else 
	{
                /*
                 *	In dangerous area, increase slowly.  
                 *      In theory this is
                 *  	sk->cong_window += 1 / sk->cong_window
                 */

                if (sk->cong_count >= sk->cong_window) {
			
                        sk->cong_window++;
                        sk->cong_count = 0;
                }
                else 
                        sk->cong_count++;
        }       
}


#define FLAG_DATA		0x01
#define FLAG_WIN_UPDATE		0x02
#define FLAG_DATA_ACKED		0x04

static int tcp_clean_rtx_queue(struct sock *sk, __u32 ack, __u32 *seq,
			       __u32 *seq_rtt)
{
	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
	struct sk_buff *skb;
	unsigned long now = jiffies;
	int acked = 0;
	
	while((skb=skb_peek(&sk->write_queue)) && (skb != tp->send_head))
	{

#ifdef TCP_DEBUG
		/* Check for a bug. */

		if (skb->next != (struct sk_buff*) &sk->write_queue &&
		    after(skb->end_seq, skb->next->seq)) 
			printk("INET: tcp_input.c: *** "
			       "bug send_list out of order.\n");
#endif								
		/*
		 *	If our packet is before the ack sequence we can
		 *	discard it as it's confirmed to have arrived the 
		 *	other end.
		 */
		 
		if (after(skb->end_seq, ack))
			break;
		
		if (sk->debug)
		{
			printk(KERN_DEBUG "removing seg %x-%x from "
			       "retransmit queue\n", skb->seq, skb->end_seq);
		}
		
		acked = FLAG_DATA_ACKED;
		
		atomic_dec(&sk->packets_out);

		*seq = skb->seq;
		*seq_rtt = now - skb->when;
				
		skb_unlink(skb);		
		
		kfree_skb(skb, FREE_WRITE);
	}

	if (acked)
	{
		tp->retrans_head = NULL;
		if (!sk->dead)
			sk->write_space(sk);
	}
	
	return acked;
}

static void tcp_ack_probe(struct sock *sk, __u32 ack)
{
	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
	
	/*
	 *	Our probe was answered
	 */
	tp->probes_out = 0;
	
	/*
	 *	Was it a usable window open ?
	 */
	
	/* should always be non-null */
	if (tp->send_head != NULL &&
	    !before (ack + tp->snd_wnd, tp->send_head->end_seq))
	{
		tp->backoff = 0;
		tp->pending = 0;
		
		tcp_clear_xmit_timer(sk, TIME_PROBE0);
		
	}
	else
	{
		tcp_reset_xmit_timer(sk, TIME_PROBE0,
				     min(tp->rto << tp->backoff, 120*HZ));
	}
}
 
/*
 *	This routine deals with incoming acks, but not outgoing ones.
 */

static int tcp_ack(struct sock *sk, struct tcphdr *th, 
		   u32 ack_seq, u32 ack, int len)
{
	int flag = 0;
	u32 seq = 0;
	u32 seq_rtt = 0;
	struct sk_buff *skb;
	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);


	if(sk->zapped)
		return(1);	/* Dead, can't ack any more so why bother */

	 
	if (tp->pending == TIME_KEEPOPEN) 
	{
	  	tp->probes_out = 0;
	}

	tp->rcv_tstamp = jiffies;
		
	/*
	 *	If the ack is newer than sent or older than previous acks
	 *	then we can probably ignore it.
	 */
	 
	if (after(ack, tp->snd_nxt) || before(ack, tp->snd_una))
		goto uninteresting_ack;

	/*
	 *	If there is data set flag 1
	 */
	 
	if (len != th->doff*4) 
	{
		flag |= FLAG_DATA;
		tcp_delack_estimator(tp);
	}

	/*
	 *	Update our send window
	 */

	/*
	 *	This is the window update code as per RFC 793
	 *	snd_wl{1,2} are used to prevent unordered
	 *	segments from shrinking the window 
	 */

	if ((tp->snd_wl1 == 0) || before(tp->snd_wl1, ack_seq) ||
	    (tp->snd_wl1 == ack_seq && !after(tp->snd_wl2, ack)))
	{
		tp->snd_wnd = ntohs(th->window);
		tp->snd_wl1 = ack_seq;
		tp->snd_wl2 = ack;

		flag |= FLAG_WIN_UPDATE;

		if (tp->snd_wnd > sk->max_window)
		{
			sk->max_window = tp->snd_wnd;
		}
	}

	
	/*
	 *	We passed data and got it acked, remove any soft error
	 *	log. Something worked...
	 */
	 
	sk->err_soft = 0;

	/*
	 *	If this ack opens up a zero window, clear backoff.  It was
	 *	being used to time the probes, and is probably far higher than
	 *	it needs to be for normal retransmission.
	 */

	if (tp->pending == TIME_PROBE0)
	{
		tcp_ack_probe(sk, ack);
	}

	/* 
	 *	See if we can take anything off of the retransmit queue.
	 */

	if (tcp_clean_rtx_queue(sk, ack, &seq, &seq_rtt))
		flag |= FLAG_DATA_ACKED;


	/* 
	 * if we where retransmiting don't count rtt estimate
	 */

	if (sk->retransmits)
	{
		if (sk->packets_out == 0)
			sk->retransmits = 0;
	}
	else
	{
		/*
		 * Note that we only reset backoff and rto in the
		 * rtt recomputation code.  And that doesn't happen
		 * if there were retransmissions in effect.  So the
		 * first new packet after the retransmissions is
		 * sent with the backoff still in effect.  Not until
		 * we get an ack from a non-retransmitted packet do
		 * we reset the backoff and rto.  This allows us to deal
		 * with a situation where the network delay has increased
		 * suddenly.  I.e. Karn's algorithm. (SIGCOMM '87, p5.)
		 */

		if (flag & FLAG_DATA_ACKED)
		{
			tcp_rtt_estimator(tp, seq_rtt);
			
			(*tcp_sys_cong_ctl_f)(sk, seq, ack, seq_rtt);
		}
	}

			
#ifdef TCP_DEBUG

	/* Sanity check out packets_out counter */
	if (skb_queue_len(&sk->write_queue) == 0 || 
	    ack == tp->snd_nxt ) 
	{
		if (sk->packets_out) 
		{
			printk(KERN_DEBUG "tcp_ack: packets_out %d\n",
			       sk->packets_out);
                        sk->packets_out = 0;
                }
	}
#endif

	if (sk->packets_out)
	{
		if (flag & FLAG_DATA_ACKED)
		{
			long when;
				
			skb = skb_peek(&sk->write_queue);
		
			when = tp->rto - (jiffies - skb->when);
		
			if (when <= 0) 
			{
				tp->retrans_head = NULL;
				/* 
				 * This is tricky. We are retransmiting a 
				 * segment of a window when congestion occured.
				 */
				tcp_do_retransmit(sk, 0);
				tcp_reset_xmit_timer(sk, TIME_RETRANS,
						     tp->rto);
			}
			else 
				tcp_reset_xmit_timer(sk, TIME_RETRANS, when);
		}
	}
	else
		tcp_clear_xmit_timer(sk, TIME_RETRANS);
	

	/*
	 *	Remember the highest ack received.
	 */
	 
	tp->snd_una = ack;

	tcp_fast_retrans(sk, ack, (flag & (FLAG_DATA|FLAG_WIN_UPDATE)));


	return 1;

uninteresting_ack:

	tcp_fast_retrans(sk, ack, 0);

	if(sk->debug)
		printk("Ack ignored %u %u\n",ack,tp->snd_nxt);
			
	return 0;
}


/*
 * 	Process the FIN bit. This now behaves as it is supposed to work
 *	and the FIN takes effect when it is validly part of sequence
 *	space. Not before when we get holes.
 *
 *	If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
 *	(and thence onto LAST-ACK and finally, CLOSE, we never enter
 *	TIME-WAIT)
 *
 *	If we are in FINWAIT-1, a received FIN indicates simultaneous
 *	close and we go into CLOSING (and later onto TIME-WAIT)
 *
 *	If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
 *
 */
 
static int tcp_fin(struct sk_buff *skb, struct sock *sk, struct tcphdr *th)
{
	sk->fin_seq = skb->end_seq;

	tcp_send_ack(sk);

	if (!sk->dead) 
	{
		sk->state_change(sk);
		sock_wake_async(sk->socket, 1);
	}

	switch(sk->state) 
	{
		case TCP_SYN_RECV:
		case TCP_SYN_SENT:
		case TCP_ESTABLISHED:
			/*
			 * move to CLOSE_WAIT
			 */

			tcp_set_state(sk, TCP_CLOSE_WAIT);
			
			if (th->rst)
				sk->shutdown = SHUTDOWN_MASK;
			break;

		case TCP_CLOSE_WAIT:
		case TCP_CLOSING:
			/*
			 * received a retransmission of the FIN, do
			 * nothing.
			 */
			break;
		case TCP_TIME_WAIT:
			/*
			 * received a retransmission of the FIN,
			 * restart the TIME_WAIT timer.
			 */
			tcp_reset_msl_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
			return(0);
		case TCP_FIN_WAIT1:
			/*
			 * This case occurs when a simultaneous close
			 * happens, we must ack the received FIN and
			 * enter the CLOSING state.
			 *
			 * This causes a WRITE timeout, which will either
			 * move on to TIME_WAIT when we timeout, or resend
			 * the FIN properly (maybe we get rid of that annoying
			 * FIN lost hang). The TIME_WRITE code is already 
			 * correct for handling this timeout.
			 */

			tcp_set_state(sk, TCP_CLOSING);
			break;
		case TCP_FIN_WAIT2:
			/*
			 * received a FIN -- send ACK and enter TIME_WAIT
			 */
			tcp_reset_msl_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
			sk->shutdown|=SHUTDOWN_MASK;
			tcp_set_state(sk,TCP_TIME_WAIT);
			break;
		case TCP_CLOSE:
			/*
			 * already in CLOSE
			 */
			break;
		default:
			tcp_set_state(sk,TCP_LAST_ACK);
	
			/* Start the timers. */
			tcp_reset_msl_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
			return(0);
	}

	return(0);
}



	/*
	 * This one checks to see if we can put data from the
	 * out_of_order queue into the receive_queue
	 */

static void  tcp_ofo_queue(struct sock *sk)
{
	struct sk_buff * skb;
	struct tcp_opt *tp=&(sk->tp_pinfo.af_tcp);

	while ((skb = skb_peek(&sk->out_of_order_queue))) {
		
		if (after(skb->seq, tp->rcv_nxt))
			break;

		if (!after(skb->end_seq, tp->rcv_nxt)) {

			if (sk->debug)
				printk("ofo packet was allready received \n");

			skb_unlink(skb);
			kfree_skb(skb, FREE_READ);
			
			continue;
		}

		if (sk->debug) 
			printk("ofo requeuing : rcv_next %X seq %X - %X\n", 
			       tp->rcv_nxt, skb->seq, skb->end_seq);
		
		skb_unlink(skb);

		 
		skb_queue_tail(&sk->receive_queue, skb);


		tp->rcv_nxt = skb->end_seq;
	}
}

static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
{
	struct sk_buff * skb1;
	struct tcp_opt *tp=&(sk->tp_pinfo.af_tcp);

	/*
	 *  Queue data for delivery to the user
	 *  Packets in sequence go to the receive queue
	 *  Out of sequence packets to out_of_order_queue
	 */


	if (skb->seq == tp->rcv_nxt) {

		/*
		 * Ok. In sequence.
		 */
		
 
		skb_queue_tail(&sk->receive_queue, skb);


		tp->rcv_nxt = skb->end_seq;

		tcp_ofo_queue(sk);
		
		if (skb_queue_len(&sk->out_of_order_queue) == 0)
			tp->pred_flags = htonl((0x5010 << 16) | tp->snd_wnd);

		return;
	}
	
	/*
	 *  Not in sequence
	 *  either a retransmit or some packet got lost
	 */

	if (!after(skb->end_seq, tp->rcv_nxt)) {
		
		/* 
		 * A retransmit.
		 * 2nd most common case.
		 * force an imediate ack
		 */

		if (sk->debug) 
			printk("retransmit received: seq %X\n", skb->seq);

		sk->delayed_acks = MAX_DELAY_ACK;
		kfree_skb(skb, FREE_READ);

		return;
	}


	if (before(skb->seq, tp->rcv_nxt)) {

		/*
		 * Partial packet
		 * seq < rcv_next < end_seq
		 */

		if (sk->debug) 
			printk("partial packet: rcv_next %X seq %X - %X\n", 
			       tp->rcv_nxt, skb->seq, skb->end_seq);
		
		skb_queue_tail(&sk->receive_queue, skb);


		tp->rcv_nxt = skb->end_seq;

		tcp_ofo_queue(sk);

		if (skb_queue_len(&sk->out_of_order_queue) == 0)
			tp->pred_flags = htonl((0x5010 << 16) | tp->snd_wnd);

		return;		
	}

	/* 
	 * Ok. This is an out_of_order segment 
	 */
	
	/* Force an ack */
	
	sk->delayed_acks = MAX_DELAY_ACK;

	/*
	 *	disable header predition
	 */

	tp->pred_flags = 0;

	if (sk->debug) 
		printk("out of order segment: rcv_next %X seq %X - %X\n", 
		       tp->rcv_nxt, skb->seq, skb->end_seq);

	if (skb_peek(&sk->out_of_order_queue) == NULL) {
		skb_queue_head(&sk->out_of_order_queue,skb);
	}
	else 
		for(skb1=sk->out_of_order_queue.prev; ; skb1 = skb1->prev) {

			/* allready there */
			if (skb->seq==skb1->seq && skb->len>=skb1->len)
			{
 				skb_append(skb1,skb);
 				skb_unlink(skb1);
 				kfree_skb(skb1,FREE_READ);
				break;
			}
			
			if (after(skb->seq, skb1->seq))
			{
				skb_append(skb1,skb);
				break;
			}
			
                        /*
			 *	See if we've hit the start. If so insert.
			 */
			if (skb1 == skb_peek(&sk->out_of_order_queue)) {
				skb_queue_head(&sk->out_of_order_queue,skb);
				break;
			}
		}
			
}


/*
 *	This routine handles the data.  If there is room in the buffer,
 *	it will be have already been moved into it.  If there is no
 *	room, then we will just have to discard the packet.
 */

static int tcp_data(struct sk_buff *skb, struct sock *sk, unsigned int len)
{
	struct tcphdr *th;
	struct tcp_opt *tp=&(sk->tp_pinfo.af_tcp);

	th = skb->h.th;
	skb_pull(skb,th->doff*4);
	skb_trim(skb,len-(th->doff*4));

        if (skb->len == 0 && !th->fin)
        {
		return(0);
        }

	/*
	 *	FIXME: don't accept data after the receved fin
	 */

	/*
	 *	The bytes in the receive read/assembly queue has increased. 
	 *	Needed for the low memory discard algorithm 
	 */
	   
	sk->bytes_rcv += skb->len;
		
	/*
	 *	We no longer have anyone receiving data on this connection.
	 */

	tcp_data_queue(sk, skb);

	if (before(tp->rcv_nxt, sk->copied_seq)) 
	{
		printk("*** tcp.c:tcp_data bug acked < copied\n");
		tp->rcv_nxt = sk->copied_seq;
	}

	sk->delayed_acks++;
	

	/*
	 *	Now tell the user we may have some data. 
	 */
	 
	if (!sk->dead) 
	{
        	if(sk->debug)
        		printk("Data wakeup.\n");
		sk->data_ready(sk,0);
	} 
	return(1);
}

static void tcp_data_snd_check(struct sock *sk)
{
	struct sk_buff *skb;
	struct tcp_opt *tp=&(sk->tp_pinfo.af_tcp);

	if ((skb = tp->send_head)) 
	{
		if (!after(skb->end_seq, tp->snd_una + tp->snd_wnd) &&
		    sk->packets_out < sk->cong_window )
		{
			/*
			 *	Add more data to the send queue.
			 */

			tcp_write_xmit(sk);
			if(!sk->dead)
				sk->write_space(sk);
		}
		else if (sk->packets_out == 0 && !tp->pending)
 		{
 			/*
 			 *	Data to queue but no room.
 			 */
 			tcp_reset_xmit_timer(sk, TIME_PROBE0, tp->rto);
 		}		
	}
}	

static __inline__ void tcp_ack_snd_check(struct sock *sk)
{
	/*
	 *	This also takes care of updating the window.
	 *	This if statement needs to be simplified.
	 *
	 *      rules for delaying an ack:
	 *      - delay time <= 0.5 HZ
	 *      - we don't have a window update to send
	 *      - must send at least every 2 full sized packets
	 */

	if (sk->delayed_acks == 0)
		return;

	if (sk->delayed_acks >= MAX_DELAY_ACK || tcp_raise_window(sk)) 
	{
		tcp_send_ack(sk);
	}
	else 
	{	
		tcp_send_delayed_ack(sk, HZ/2);		
	}
}

/*
 *	This routine is only called when we have urgent data
 *	signalled. Its the 'slow' part of tcp_urg. It could be
 *	moved inline now as tcp_urg is only called from one
 *	place. We handle URGent data wrong. We have to - as
 *	BSD still doesn't use the correction from RFC961.
 *	For 1003.1g we should support a new option TCP_STDURG to permit
 *	either form.
 */
 
static void tcp_check_urg(struct sock * sk, struct tcphdr * th)
{
	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
	u32 ptr = ntohs(th->urg_ptr);

	if (ptr)
		ptr--;
	ptr += ntohl(th->seq);

	/* ignore urgent data that we've already seen and read */
	if (after(sk->copied_seq, ptr))
		return;

	/* do we already have a newer (or duplicate) urgent pointer? */
	if (sk->urg_data && !after(ptr, sk->urg_seq))
		return;

	/* tell the world about our new urgent pointer */
	if (sk->proc != 0) {
		if (sk->proc > 0) {
			kill_proc(sk->proc, SIGURG, 1);
		} else {
			kill_pg(-sk->proc, SIGURG, 1);
		}
	}
	/*
	 *	We may be adding urgent data when the last byte read was
	 *	urgent. To do this requires some care. We cannot just ignore
	 *	sk->copied_seq since we would read the last urgent byte again
	 *	as data, nor can we alter copied_seq until this data arrives
	 *	or we break the sematics of SIOCATMARK (and thus sockatmark())
	 */
	if (sk->urg_seq == sk->copied_seq)
		sk->copied_seq++;	/* Move the copied sequence on correctly */
	sk->urg_data = URG_NOTYET;
	sk->urg_seq = ptr;

	/* disable header prediction */
	tp->pred_flags = 0;
}

/*
 *	This is the 'fast' part of urgent handling.
 */
 
static inline void tcp_urg(struct sock *sk, struct tcphdr *th, unsigned long len)
{
	/*
	 *	Check if we get a new urgent pointer - normally not 
	 */
	 
	if (th->urg)
		tcp_check_urg(sk,th);

	/*
	 *	Do we wait for any urgent data? - normally not
	 */
	 
	if (sk->urg_data == URG_NOTYET) {
		u32 ptr;

		/*
		 *	Is the urgent pointer pointing into this packet? 
		 */	 
		ptr = sk->urg_seq - ntohl(th->seq) + th->doff*4;
		if (ptr < len) {
			sk->urg_data = URG_VALID | *(ptr + (unsigned char *) th);
			if (!sk->dead)
				sk->data_ready(sk,0);
		}
	}
}


static void prune_queue(struct sock *sk)
{
	struct sk_buff * skb;

	/*
	 *	clean the out_of_order queue
	 */

	while ((skb = skb_dequeue(&sk->out_of_order_queue))) 
	{
		kfree_skb(skb, FREE_READ);
	}
}


int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
			struct tcphdr *th, __u16 len)
{
	struct tcp_opt *tp;
	int queued = 0;
	u32 flg;
	
	/*
	 *	Header prediction.
	 *	The code follows the one in the famous 
	 *	"30 instruction TCP receive" Van Jacobson mail.
	 *	
	 *	Van's trick is to deposit buffers into socket queue 
	 *	on a device interrupt, to call tcp_recv function
	 *	on the receive process context and checksum and copy
	 *	the buffer to user space. smart...
	 *
	 *	Our current scheme is not silly either but we take the 
	 *	extra cost of the net_bh soft interrupt processing...
	 *	We do checksum and copy also but from device to kernel.
	 */

	tp = &(sk->tp_pinfo.af_tcp); 
	flg = *(((u32 *)th) + 3);
		
	/*
	 *	pred_flags is 0x5?10 << 16 + snd_wnd
	 *	if header_predition is to be made
	 *	? will be 0 else it will be !0
	 *	(when there are holes in the receive 
	 *	 space for instance)
	 */

	if (flg == tp->pred_flags && skb->seq == tp->rcv_nxt)
	{
		if (len <= sizeof(struct tcphdr))
		{
			if (len == sizeof(struct tcphdr))
			{
				tcp_ack(sk, th, skb->seq, skb->ack_seq, len);
			}

			tcp_data_snd_check(sk);

			kfree_skb(skb, FREE_READ);
			return 0;
			
		}
		else if (skb->ack_seq == tp->snd_una)
		{
			/* 
			 * Bulk data transfer: receiver 
			 */
			
			skb_pull(skb,sizeof(struct tcphdr));
			
			skb_queue_tail(&sk->receive_queue, skb);
			tp->rcv_nxt = skb->end_seq;
			sk->bytes_rcv += len - sizeof(struct tcphdr);
			
			sk->data_ready(sk, 0);
			tcp_delack_estimator(tp);

			if (sk->delayed_acks++)
			{
				tcp_send_delayed_ack(sk, HZ/2);
			}
			else
				tcp_send_ack(sk);

			return 0;
		}
	}

	if (!tcp_sequence(tp, skb->seq, skb->end_seq))
	{
		if (!th->rst)
		{
			if (after(skb->seq, tp->rcv_nxt))
			{
				printk(KERN_DEBUG "->seq:%d end:%d "
				       "wup:%d wnd:%d\n",
				       skb->seq, skb->end_seq, 
				       tp->rcv_wup, tp->rcv_wnd);
			}
			tcp_send_ack(sk);
			kfree_skb(skb, FREE_READ);
			return 0;
		}
	}

	if(th->syn && skb->seq != sk->syn_seq)
	{
		printk(KERN_DEBUG "syn in established state\n");
		tcp_reset(sk, skb);
		kfree_skb(skb, FREE_READ);
		return 1;
	}
	
	if(th->rst)
	{
		tcp_reset(sk,skb);
		kfree_skb(skb, FREE_READ);
		return 0;
	}
	
	if(th->ack)
	{
		tcp_ack(sk, th, skb->seq, skb->ack_seq, len);
	}

	
	/*
	 *	Process urgent data
	 */

	tcp_urg(sk, th, len);

	/*
	 *	step 7: process the segment text
	 */


	queued = tcp_data(skb, sk, len);

	/*
	 *	step 8: check the FIN bit
	 */

	if (th->fin)
	{
		tcp_fin(skb, sk, th);
	}

	tcp_data_snd_check(sk);
	tcp_ack_snd_check(sk);

	/*
	 *	If our receive queue has grown past its limits,
	 *	try to prune away duplicates etc..
	 */
	if (sk->rmem_alloc > sk->rcvbuf)
		prune_queue(sk);

	/*
	 *	And done
	 */	
	
	if (!queued)
		kfree_skb(skb, FREE_READ);
	return 0;
}
		

/*
 *	This function implements the receiving procedure of RFC 793.
 *	It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
 *	address independent.
 */
	
int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
			  struct tcphdr *th, void *opt, __u16 len)
{
	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
	int queued = 0;
	int rcv_mss;

	/*
	 *	state == CLOSED
	 *	tested in tcp_v{4,6}_rcv
	 */

	switch (sk->state) {


	case TCP_LISTEN:
		
		if (th->rst)			
			goto discard;

		/* 
		 * These use the socket TOS.. 
		 * might want to be the received TOS 
		 */

		if(th->ack)
		{	
			/*
			 *  send reset
			 */

			return 1;
		}
		
		
		if(th->syn)
		{
			int err;
			__u32 isn;

			isn = tp->af_specific->init_sequence(sk, skb);
			err = tp->af_specific->conn_request(sk, skb, opt, isn);

			if (err < 0)
				return 1;

			/*
			 *  Now we have several options: In theory there is 
			 *  nothing else in the frame. KA9Q has an option to 
			 *  send data with the syn, BSD accepts data with the
			 *  syn up to the [to be] advertised window and 
			 *  Solaris 2.1 gives you a protocol error. For now 
			 *  we just ignore it, that fits the spec precisely 
			 *  and avoids incompatibilities. It would be nice in
			 *  future to drop through and process the data.
			 *
			 *  Now that TTCP is starting to be used we ought to 
			 *  queue this data.
			 */

			return 0;
		}
		
		goto discard;
		break;

	case TCP_SYN_SENT:
		
		/*
		 *	SYN sent means we have to look for a suitable ack and 
		 *	either reset for bad matches or go to connected. 
		 *	The SYN_SENT case is unusual and should
		 *	not be in line code. [AC]
		 */
	   
		if(th->ack)
		{
			/* We got an ack, but it's not a good ack */
			if(!tcp_ack(sk,th, skb->seq, skb->ack_seq, len))
			{
				tcp_statistics.TcpAttemptFails++;
				return 1;
			}

			if(th->rst)
			{
				tcp_reset(sk,skb);
				goto discard;
			}

			if(!th->syn)
			{
				/* 
				 *  A valid ack from a different connection
				 *  start. Shouldn't happen but cover it 
				 */
				tcp_statistics.TcpAttemptFails++;
				return 1;
			}

			/*
			 *	Ok.. it's good. Set up sequence 
			 *	numbers and
			 *	move to established.
			 */

			tp->rcv_nxt = skb->seq+1;
			tp->rcv_wnd = 0;
			tp->rcv_wup = skb->seq+1;

			tp->snd_wnd = htons(th->window);
			tp->snd_wl1 = skb->seq;
			tp->snd_wl2 = skb->ack_seq;

			sk->fin_seq = skb->seq;
			tcp_send_ack(sk);

			tcp_set_state(sk, TCP_ESTABLISHED);
			rcv_mss = tcp_parse_options(th);
			
			if (rcv_mss)
				sk->mss = min(sk->mss, rcv_mss);
			
			sk->dummy_th.dest = th->source;
			sk->copied_seq = tp->rcv_nxt;

			if(!sk->dead)
			{
				sk->state_change(sk);
				sock_wake_async(sk->socket, 0);
			}

			/* Drop through step 6 */
			goto step6;
		}
		else
		{
			if(th->syn && !th->rst)
			{
				/* 
				 * the previous version of the code
				 * checked for "connecting to self"
				 * here. that check is done now in
				 * tcp_connect
				 */

				tcp_set_state(sk, TCP_SYN_RECV);
				
				tp->rcv_nxt = skb->seq + 1;
				tp->rcv_wup = skb->seq + 1;

				tp->snd_wnd = htons(th->window);
				tp->snd_wl1 = skb->seq;
				
				tcp_send_synack(sk);
				goto discard;
			}		

		}
		break;

	case TCP_TIME_WAIT:
	        /*
		 *	RFC 1122:
		 *	"When a connection is [...] on TIME-WAIT state [...]
		 *	[a TCP] MAY accept a new SYN from the remote TCP to
		 *	reopen the connection directly, if it:
		 *	
		 *	(1)  assigns its initial sequence number for the new
                 *	connection to be larger than the largest sequence
                 *	number it used on the previous connection incarnation,
                 *	and
		 *
		 *	(2)  returns to TIME-WAIT state if the SYN turns out 
		 *	to be an old duplicate".
		 */

		if (th->syn && !th->rst && after(skb->seq, tp->rcv_nxt))
		{
			__u32 isn;
			int err;

			skb_orphan(skb);
                        sk->err = ECONNRESET;
                        tcp_set_state(sk, TCP_CLOSE);
                        sk->shutdown = SHUTDOWN_MASK;

			isn = tp->rcv_nxt + 128000;

			sk = tp->af_specific->get_sock(skb, th);

			if (sk == NULL)
				goto discard;

			skb_set_owner_r(skb, sk);
			tp = &sk->tp_pinfo.af_tcp;
			
			err = tp->af_specific->conn_request(sk, skb, opt, isn);

			if (err < 0)
				return 1;

			return 0;
		}

		break;

	}

	/*
	 *	step 1: check sequence number
	 */

	if (!tcp_sequence(tp, skb->seq, skb->end_seq))
	{
		if (!th->rst)
		{
			tcp_send_ack(sk);
			goto discard;
		}
	}


	/*
	 *	step 2: check RST bit
	 */

	if(th->rst)
	{
		tcp_reset(sk,skb);
		goto discard;
	}

	/*
	 *	step 3: check security and precedence 
	 *	[ignored]
	 */

	/*
	 *	step 4:
	 *
	 *	Check for a SYN, and ensure it matches the SYN we were
	 *	first sent. We have to handle the rather unusual (but valid)
	 *	sequence that KA9Q derived products may generate of
	 *
	 *	SYN
	 *				SYN|ACK Data
	 *	ACK	(lost)
	 *				SYN|ACK Data + More Data
	 *	.. we must ACK not RST...
	 *
	 *	We keep syn_seq as the sequence space occupied by the 
	 *	original syn. 
	 */

	if (th->syn && skb->seq!=sk->syn_seq)
	{
		tcp_reset(sk, skb);
		return 1;
	}

	/*
	 *	step 5: check the ACK field
	 */

	if (th->ack) 
	{
		int acceptable = tcp_ack(sk,th,skb->seq, skb->ack_seq,len);
		
		switch(sk->state) {
		case TCP_SYN_RECV:
			if (acceptable)
			{
				tcp_set_state(sk, TCP_ESTABLISHED);
				sk->dummy_th.dest=th->source;
				sk->copied_seq = tp->rcv_nxt;

				if(!sk->dead)
					sk->state_change(sk);		

				tp->snd_una = skb->ack_seq;
				tp->snd_wnd = htons(th->window);
				tp->snd_wl1 = skb->seq;
				tp->snd_wl2 = skb->ack_seq;

			}
			else
				return 1;
			break;

		case TCP_FIN_WAIT1:
			
			if (tp->snd_una == sk->write_seq) 
			{
				sk->shutdown |= SEND_SHUTDOWN;
				tcp_set_state(sk, TCP_FIN_WAIT2);
				if (!sk->dead) 
					sk->state_change(sk);
			}
			break;

		case TCP_CLOSING:			

			if (tp->snd_una == sk->write_seq) 
			{
				tcp_time_wait(sk);
			}
			break;

		case TCP_LAST_ACK:

			if (tp->snd_una == sk->write_seq) 
			{
				sk->shutdown = SHUTDOWN_MASK;
				tcp_set_state(sk,TCP_CLOSE);
				if (!sk->dead)
					sk->state_change(sk);
				goto discard;
			}
			break;

		case TCP_TIME_WAIT:
			/*
			 * keep us in TIME_WAIT until we stop getting 
			 * packets, reset the timeout.
			 */
			tcp_reset_msl_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
			break;

		}
	}
	else
		goto discard;

  step6:

	/*
	 *	step 6: check the URG bit
	 */

	tcp_urg(sk, th, len);

	/*
	 *	step 7: process the segment text
	 */

	switch (sk->state) {
	case TCP_CLOSE_WAIT:
	case TCP_CLOSING:
		if (!before(skb->seq, sk->fin_seq))
			break;
	
	case TCP_FIN_WAIT1:
	case TCP_FIN_WAIT2:

		/*
		 *	RFC 793 says to queue data in this states,
		 *	RFC 1122 says we MUST send a reset. 
		 *	BSD 4.4 also does reset.
		 */

		if ((sk->shutdown & RCV_SHUTDOWN) && sk->dead)
		{
			if (after(skb->end_seq - th->fin, tp->rcv_nxt))
			{
				tcp_reset(sk, skb);
				return 1;
			}
		}
		
	case TCP_ESTABLISHED:
		queued = tcp_data(skb, sk, len);
		break;
	}

	/*
	 *	step 8: check the FIN bit
	 */

	if (th->fin)
	{
		tcp_fin(skb, sk, th);
	}

	tcp_data_snd_check(sk);
	tcp_ack_snd_check(sk);

	if (queued)
		return 0;
  discard:

	kfree_skb(skb, FREE_READ);
	return 0;
}

int tcp_sysctl_congavoid(ctl_table *ctl, int write, struct file * filp,
			 void *buffer, size_t *lenp)
{
	int val = sysctl_tcp_cong_avoidance;
	int retv;
	
	retv  = proc_dointvec(ctl, write, filp, buffer, lenp);
	
	if (write)
	{
		switch (sysctl_tcp_cong_avoidance) {
			case 0:
				tcp_sys_cong_ctl_f = &tcp_cong_avoid_vanj;
				break;
			case 1:
				tcp_sys_cong_ctl_f = &tcp_cong_avoid_vegas;
				break;
			default:
				retv = -EINVAL;
				sysctl_tcp_cong_avoidance = val;
		}
	}
	
	return retv;
}

/*
 * Local variables:
 *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strength-reduce -pipe -m486 -DCPU=486 -c -o tcp_input.o tcp_input.c"
 * c-file-style: "Linux"
 * End:
 */
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy