summaryrefslogtreecommitdiffstats
path: root/lib/ratp.c
blob: 321721ab714a9f43e9e85425dffe52020756b798 (plain)
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
/*
 * barebox RATP implementation.
 * This is the barebox implementation for the Reliable Asynchronous
 * Transfer Protocol (RATP) as described in RFC916.
 *
 * Copyright (C) 2015 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#define pr_fmt(fmt)  "ratp: " fmt

#include <common.h>
#include <malloc.h>
#include <getopt.h>
#include <ratp.h>
#include <crc.h>
#include <clock.h>
#include <asm/unaligned.h>

/*
 * RATP packet format:
 *
 * Byte No.
 *
 *        +-------------------------------+
 *        |                               |
 *    1   |          Synch Leader         | Hex 01
 *        |                               |
 *        +-------------------------------+
 *        | S | A | F | R | S | A | E | S |
 *    2   | Y | C | I | S | N | N | O | O | Control
 *        | N | K | N | T |   |   | R |   |
 *        +-------------------------------+
 *        |                               |
 *    3   |      Data length (0-255)      |
 *        |                               |
 *        +-------------------------------+
 *        |                               |
 *    4   |        Header Checksum        |
 *        |                               |
 *        +-------------------------------+
 *
 */

struct ratp_header {
	uint8_t	synch;
	uint8_t	control;
	uint8_t	data_length;
	uint8_t	cksum;
};

#define RATP_CONTROL_SO		(1 << 0)
#define RATP_CONTROL_EOR	(1 << 1)
#define RATP_CONTROL_AN		(1 << 2)
#define RATP_CONTROL_SN		(1 << 3)
#define RATP_CONTROL_RST	(1 << 4)
#define RATP_CONTROL_FIN	(1 << 5)
#define RATP_CONTROL_ACK	(1 << 6)
#define RATP_CONTROL_SYN	(1 << 7)

enum ratp_state {
	RATP_STATE_LISTEN,
	RATP_STATE_SYN_SENT,
	RATP_STATE_SYN_RECEIVED,
	RATP_STATE_ESTABLISHED,
	RATP_STATE_FIN_WAIT,
	RATP_STATE_LAST_ACK,
	RATP_STATE_CLOSING,
	RATP_STATE_TIME_WAIT,
	RATP_STATE_CLOSED,
};

struct ratp_message {
	void *buf;
	size_t len;
	struct list_head list;
	void (*complete)(void *ctx, int status);
	void *complete_ctx;
	int eor;
};

static char *ratp_state_str[] = {
	[RATP_STATE_LISTEN]       = "LISTEN",
	[RATP_STATE_SYN_SENT]     = "SYN_SENT",
	[RATP_STATE_SYN_RECEIVED] = "SYN_RECEIVED",
	[RATP_STATE_ESTABLISHED]  = "ESTABLISHED",
	[RATP_STATE_FIN_WAIT]     = "FIN_WAIT",
	[RATP_STATE_LAST_ACK]     = "LAST_ACK",
	[RATP_STATE_CLOSING]      = "CLOSING",
	[RATP_STATE_TIME_WAIT]    = "TIME_WAIT",
	[RATP_STATE_CLOSED]       = "CLOSED",
};

struct ratp_internal {
	struct ratp *ratp;

	enum ratp_state state;
	int sn_sent;
	int sn_received;
	int active;

	void *recvbuf;
	void *sendbuf;
	int sendbuf_len;

	struct list_head recvmsg;
	struct list_head sendmsg;

	struct ratp_message *sendmsg_current;

	uint64_t timewait_timer_start;
	uint64_t retransmission_timer_start;
	int max_retransmission;
	int retransmission_count;
	int srtt;
	int rto;

	int status;

	int in_ratp;
};

static bool ratp_sn(struct ratp_header *hdr)
{
	return hdr->control & RATP_CONTROL_SN ? 1 : 0;
}

static bool ratp_an(struct ratp_header *hdr)
{
	return hdr->control & RATP_CONTROL_AN ? 1 : 0;
}

#define ratp_set_sn(sn) (((sn) % 2) ? RATP_CONTROL_SN : 0)
#define ratp_set_an(an) (((an) % 2) ? RATP_CONTROL_AN : 0)

static inline int ratp_header_ok(struct ratp_internal *ri, struct ratp_header *h)
{
	uint8_t cksum;
	int ret;

	cksum = h->control;
	cksum += h->data_length;
	cksum += h->cksum;

	ret = cksum == 0xff ? 1 : 0;

	if (ret)
		pr_vdebug("Header ok\n");
	else
		pr_vdebug("Header cksum failed: %02x\n", cksum);

	return ret;
}

static bool ratp_has_data(struct ratp_header *hdr)
{
	if (hdr->control & RATP_CONTROL_SO)
		return 1;
	if (!(hdr->control & (RATP_CONTROL_SYN | RATP_CONTROL_RST | RATP_CONTROL_FIN)) && hdr->data_length)
		return 1;
	return 0;
}

static void ratp_print_header(struct ratp_internal *ri, struct ratp_header *hdr,
		const char *prefix)
{
	uint8_t control = hdr->control;

	pr_debug("%s>%s %s %s %s %s %s %s %s< len: %-3d\n",
		prefix,
		control & RATP_CONTROL_SO  ? "so" : "--",
		control & RATP_CONTROL_EOR ? "eor" : "---",
		control & RATP_CONTROL_AN ? "an" : "--",
		control & RATP_CONTROL_SN ? "sn" : "--",
		control & RATP_CONTROL_RST ? "rst" : "---",
		control & RATP_CONTROL_FIN ? "fin" : "---",
		control & RATP_CONTROL_ACK ? "ack" : "---",
		control & RATP_CONTROL_SYN ? "syn" : "---",
		hdr->data_length);

#ifdef VERBOSE_DEBUG
	if (hdr->data_length)
		memory_display(hdr + 1, 0, hdr->data_length, 1, 0);
#endif
}

static void ratp_create_packet(struct ratp_internal *ri, struct ratp_header *hdr,
		uint8_t control, uint8_t length)
{
	hdr->synch = 0x1;
	hdr->control = control;
	hdr->data_length = length;
	hdr->cksum = (control + length) ^ 0xff;
}

static void ratp_state_change(struct ratp_internal *ri, enum ratp_state state)
{
	pr_debug("state %-10s -> %-10s\n", ratp_state_str[ri->state],
			ratp_state_str[state]);

	ri->state = state;
}

#define RATP_CONTROL_SO		(1 << 0)
#define RATP_CONTROL_EOR	(1 << 1)
#define RATP_CONTROL_AN		(1 << 2)
#define RATP_CONTROL_SN		(1 << 3)
#define RATP_CONTROL_RST	(1 << 4)
#define RATP_CONTROL_FIN	(1 << 5)
#define RATP_CONTROL_ACK	(1 << 6)
#define RATP_CONTROL_SYN	(1 << 7)

static int ratp_send_pkt(struct ratp_internal *ri, void *pkt, int length)
{
	struct ratp_header *hdr = (void *)pkt;

	ratp_print_header(ri, hdr, "send");

	if (ratp_has_data(hdr) ||
	    (hdr->control & (RATP_CONTROL_SYN | RATP_CONTROL_RST | RATP_CONTROL_FIN))) {
		memcpy(ri->sendbuf, pkt, length);
		ri->sn_sent = ratp_sn(hdr);
		ri->sendbuf_len = length;
		ri->retransmission_timer_start = get_time_ns();
		ri->retransmission_count = 0;
	}

	return ri->ratp->send(ri->ratp, pkt, length);
}

static int ratp_send_hdr(struct ratp_internal *ri, uint8_t control)
{
	struct ratp_header hdr = {};

	ratp_create_packet(ri, &hdr, control, 0);

	return ratp_send_pkt(ri, &hdr, sizeof(hdr));
}

static int ratp_recv_char(struct ratp_internal *ri, uint8_t *data, int poll_timeout_ms)
{
	uint64_t start;
	int ret;

	start = get_time_ns();

	while (1) {
		ret = ri->ratp->recv(ri->ratp, data);
		if (ret < 0 && ret != -EAGAIN)
			return ret;

		if (ret == 0)
			return 0;

		if (is_timeout(start, poll_timeout_ms * MSECOND))
			return -EAGAIN;
	}
}

static int ratp_recv_pkt_header(struct ratp_internal *ri, struct ratp_header *hdr,
		int poll_timeout_ms)
{
	int ret;
	uint8_t buf;

	do {
		ret = ratp_recv_char(ri, &buf, 0);
		if (ret < 0)
			return ret;
		hdr->synch = buf;
	} while (hdr->synch != 1);
	ret = ratp_recv_char(ri, &buf, poll_timeout_ms);
	if (ret < 0)
		return ret;

	hdr->control = buf;
	ret = ratp_recv_char(ri, &buf, poll_timeout_ms);
	if (ret < 0)
		return ret;

	hdr->data_length = buf;

	ret = ratp_recv_char(ri, &buf, poll_timeout_ms);
	if (ret < 0)
		return ret;

	hdr->cksum = buf;

	if (!ratp_header_ok(ri, hdr))
		return -EAGAIN;

	return 0;
}

static int ratp_recv_pkt_data(struct ratp_internal *ri, void *data, uint8_t len,
		int poll_timeout_ms)
{
	uint16_t crc_expect, crc_read;
	int ret, i;

	for (i = 0; i < len + 2; i++) {
		ret = ratp_recv_char(ri, data + i, poll_timeout_ms);
		if (ret < 0)
			return ret;
	}

	crc_expect = cyg_crc16(data, len);

	crc_read = get_unaligned_be16(data + len);

	if (crc_expect != crc_read) {
		pr_vdebug("Wrong CRC: expected: 0x%04x, got 0x%04x\n",
				crc_expect, crc_read);
		return -EBADMSG;
	} else {
		pr_vdebug("correct CRC: 0x%04x\n", crc_expect);
	}

	return 0;
}

static int ratp_recv_pkt(struct ratp_internal *ri, void *pkt, int poll_timeout_ms)
{
	struct ratp_header *hdr = pkt;
	void *data = pkt + sizeof(struct ratp_header);
	int ret;

	ret = ratp_recv_pkt_header(ri, hdr, poll_timeout_ms);
	if (ret < 0)
		return ret;

	if (hdr->control & (RATP_CONTROL_SO | RATP_CONTROL_RST | RATP_CONTROL_SYN |
		RATP_CONTROL_FIN))
		return 0;

	if (hdr->data_length) {
		ret = ratp_recv_pkt_data(ri, data, hdr->data_length,
				poll_timeout_ms);
		if (ret)
			return ret;
	}

	return 0;
}

static bool ratp_an_expected(struct ratp_internal *ri, struct ratp_header *hdr)
{
	return ratp_an(hdr) == (ri->sn_sent + 1) % 2;
}

static bool ratp_sn_expected(struct ratp_internal *ri, struct ratp_header *hdr)
{
	return ratp_sn(hdr) != ri->sn_received;
}

static int ratp_send_ack(struct ratp_internal *ri, struct ratp_header *hdr)
{
	uint8_t control = RATP_CONTROL_ACK;
	int ret;

	if (hdr->control & RATP_CONTROL_SN)
		control |= RATP_CONTROL_AN;
	else
		control |= 0;

	ret = ratp_send_hdr(ri, control);
	if (ret)
		return ret;

	return 0;
}

static int ratp_send_next_data(struct ratp_internal *ri)
{
	uint16_t crc;
	uint8_t control;
	struct ratp_header *hdr;
	uint8_t *data;
	int pktlen;
	struct ratp_message *msg;
	int len;

	if (ri->sendmsg_current) {
		pr_err("%s: busy\n", __func__);
		return -EBUSY;
	}

	if (list_empty(&ri->sendmsg))
		return 0;

	msg = list_first_entry(&ri->sendmsg, struct ratp_message, list);

	ri->sendmsg_current = msg;

	list_del(&msg->list);

	len = msg->len;

	control = ratp_set_sn(ri->sn_sent + 1) |
		ratp_set_an(ri->sn_received + 1) |
		RATP_CONTROL_ACK;

	hdr = msg->buf;
	data = (uint8_t *)(hdr + 1);

	if (msg->eor)
		control |= RATP_CONTROL_EOR;

	pktlen = sizeof(struct ratp_header);
	if (len > 1) {
		pktlen += len + 2;
		crc = cyg_crc16(data, len);
		put_unaligned_be16(crc, data + len);
	} else if (len == 1) {
		control |= RATP_CONTROL_SO;
		len = *data;
	}

	ratp_create_packet(ri, hdr, control, len);

	ri->retransmission_count = 0;

	ratp_send_pkt(ri, msg->buf, pktlen);

	return 0;
}

static void ratp_start_time_wait_timer(struct ratp_internal *ri)
{
	ri->timewait_timer_start = get_time_ns();
}

static void ratp_msg_done(struct ratp_internal *ri, struct ratp_message *msg, int status)
{
	int alpha, beta, rtt;

	if (!status) {
		rtt = (unsigned long)(get_time_ns() - ri->retransmission_timer_start) / MSECOND;

		alpha = 8;
		beta = 15;

		ri->srtt = (alpha * ri->srtt + (10 - alpha) * rtt) / 10;
		ri->rto = max(200, beta * ri->srtt / 10);

		pr_debug("%s: done. SRTT: %dms RTO: %dms status: %d\n",
			__func__, ri->srtt, ri->rto, ri->status);
	}

	if (msg->complete)
		msg->complete(msg->complete_ctx, status);

	free(msg->buf);
	free(msg);
}

/*
 * This procedure details the behavior of the LISTEN state.  First
 * check the packet for the RST flag.  If it is set then packet is
 * discarded and ignored, return and continue the processing
 * associated with this state.
 *
 * We assume now that the RST flag was not set.  Check the packet
 * for the ACK flag.  If it is set we have an illegal condition
 * since no connection has yet been opened.  Send a RST packet
 * with the correct response SN value:
 *
 * <SN=received AN><CTL=RST>
 *
 * Return to the current state without any further processing.
 *
 * We assume now that neither the RST nor the ACK flags were set.
 * Check the packet for a SYN flag.  If it is set then an attempt
 * is being made to open a connection.  Create a TCB for this
 * connection.  The sender has placed its MDL in the LENGTH field,
 * also specified is the sender's initial SN value.  Retrieve and
 * place them into the TCB.  Note that the presence of the SO flag
 * is ignored since it has no meaning when either of the SYN, RST,
 * or FIN flags are set.
 *
 * Send a SYN packet which acknowledges the SYN received.  Choose
 * the initial SN value and the MDL for this end of the
 * connection:
 *
 * <SN=0><AN=received SN+1 modulo 2><CTL=SYN, ACK><LENGTH=MDL>
 *
 * and go to the RATP_STATE_SYN_RECEIVED state without any further
 * processing.
 *
 * Any packet not satisfying the above tests is discarded and
 * ignored.  Return to the current state without any further
 * processing.
 */
static void ratp_behaviour_a(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if (hdr->control & RATP_CONTROL_RST)
		return;

	if (hdr->control & RATP_CONTROL_ACK) {
		uint8_t control = RATP_CONTROL_RST;

		if (hdr->control & RATP_CONTROL_AN)
			control |= RATP_CONTROL_SN;

		ratp_send_hdr(ri, control);

		return;
	}

	if (hdr->control & RATP_CONTROL_SYN) {
		struct ratp_header synack = {};
		uint8_t control = RATP_CONTROL_SYN | RATP_CONTROL_ACK;

		if (!(hdr->control & RATP_CONTROL_SN))
			control |= RATP_CONTROL_AN;

		ratp_create_packet(ri, &synack, control, 255);
		ratp_send_pkt(ri, &synack, sizeof(synack));

		ratp_state_change(ri, RATP_STATE_SYN_RECEIVED);
	}
}

/*
 * This procedure represents the behavior of the SYN-SENT state
 * and is entered when this end of the connection decides to
 * execute an active OPEN.
 *
 * First, check the packet for the ACK flag.  If the ACK flag is
 * set then check to see if the AN value was as expected.  If it
 * was continue below.  Otherwise the AN value was unexpected.  If
 * the RST flag was set then discard the packet and return to the
 * current state without any further processing, else send a
 * reset:
 *
 * <SN=received AN><CTL=RST>
 *
 * Discard the packet and return to the current state without any
 * further processing.
 *
 * At this point either the ACK flag was set and the AN value was
 * as expected or ACK was not set.  Second, check the RST flag.
 * If the RST flag is set there are two cases:
 *
 * . If the ACK flag is set then discard the packet, flush the
 * retransmission queue, inform the user "Error: Connection
 * refused", delete the TCB, and go to the CLOSED state without
 * any further processing.
 *
 * 2. If the ACK flag was not set then discard the packet and
 * return to this state without any further processing.
 *
 * At this point we assume the packet contained an ACK which was
 * Ok, or there was no ACK, and there was no RST.  Now check the
 * packet for the SYN flag.  If the ACK flag was set then our SYN
 * has been acknowledged.  Store MDL received in the TCB.  At this
 * point we are technically in the ESTABLISHED state.  Send an
 * acknowledgment packet and any initial data which is queued to
 * send:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=ACK><DATA>
 *
 * Go to the ESTABLISHED state without any further processing.
 *
 * If the SYN flag was set but the ACK was not set then the other
 * end of the connection has executed an active open also.
 * Acknowledge the SYN, choose your MDL, and send:
 *
 * <SN=0><AN=received SN+1 modulo 2><CTL=SYN, ACK><LENGTH=MDL>
 *
 * Go to the SYN-RECEIVED state without any further processing.
 *
 * Any packet not satisfying the above tests is discarded and
 * ignored.  Return to the current state without any further
 * processing.
 */
static void ratp_behaviour_b(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if ((hdr->control & RATP_CONTROL_ACK) && !ratp_an_expected(ri, hdr)) {
		if (!(hdr->control & RATP_CONTROL_RST)) {
			uint8_t control;

			control = RATP_CONTROL_RST |
				ratp_set_sn(ratp_an(hdr));

			ratp_send_hdr(ri, control);
		}
		return;
	}

	if (hdr->control & RATP_CONTROL_RST) {
		if (hdr->control & RATP_CONTROL_ACK) {
			ri->status = -ECONNREFUSED;

			pr_debug("Connection refused\n");

			ratp_state_change(ri, RATP_STATE_CLOSED);

		}
		return;
	}

	if (hdr->control & RATP_CONTROL_SYN) {
		uint8_t control;

		ri->sn_received = ratp_sn(hdr);

		if (hdr->control & RATP_CONTROL_ACK) {
			control = ratp_set_sn(ratp_an(hdr)) |
				ratp_set_an(!ratp_sn(hdr)) |
				RATP_CONTROL_ACK;
			ratp_send_hdr(ri, control);
			ratp_state_change(ri, RATP_STATE_ESTABLISHED);
		} else {
			struct ratp_header synack = {};

			control = ratp_set_an(!ratp_sn(hdr)) |
				RATP_CONTROL_SYN |
				RATP_CONTROL_ACK;

			ratp_create_packet(ri, &synack, control, 255);
			ratp_send_pkt(ri, &synack, sizeof(synack));
			ratp_state_change(ri, RATP_STATE_SYN_RECEIVED);
		}
	}
}

/*
 * Examine the received SN field value.  If the SN value was
 * expected then return and continue the processing associated
 * with this state.
 *
 * We now assume the SN value was not what was expected.
 *
 * If either RST or FIN were set discard the packet and return to
 * the current state without any further processing.
 *
 * If neither RST nor FIN flags were set it is assumed that this
 * packet is a duplicate of one already received.  Send an ACK
 * back:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=ACK>
 *
 * Discard the duplicate packet and return to the current state
 * without any further processing.
 */
static int ratp_behaviour_c1(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	int ret;

	pr_debug("%s\n", __func__);

	if (ratp_sn_expected(ri, hdr)) {
		pr_vdebug("%s: sn is expected\n", __func__);
		return 0;
	}

	if (!(hdr->control & RATP_CONTROL_RST) &&
			!(hdr->control & RATP_CONTROL_FIN)) {
		ret = ratp_send_ack(ri, hdr);
		if (ret)
			return ret;
	}

	return 1;

}

/*
 * Examine the received SN field value.  If the SN value was
 * expected then return and continue the processing associated
 * with this state.
 *
 * We now assume the SN value was not what was expected.
 *
 * If either RST or FIN were set discard the packet and return to
 * the current state without any further processing.
 *
 * If SYN was set we assume that the other end crashed and has
 * attempted to open a new connection.  We respond by sending a
 * legal reset:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=RST, ACK>
 *
 * This will cause the other end, currently in the SYN-SENT state,
 * to close.  Flush the retransmission queue, inform the user
 * "Error: Connection reset", discard the packet, delete the TCB,
 * and go to the CLOSED state without any further processing.
 *
 * If neither RST, FIN, nor SYN flags were set it is assumed that
 * this packet is a duplicate of one already received.  Send an
 * ACK back:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=ACK>
 *
 * Discard the duplicate packet and return to the current state
 * without any further processing.
 */
static int ratp_behaviour_c2(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	int ret;

	pr_debug("%s\n", __func__);

	if (ratp_sn_expected(ri, hdr))
		return 0;

	if ((hdr->control & RATP_CONTROL_RST) ||
			(hdr->control & RATP_CONTROL_FIN))
		return 1;

	if (hdr->control & RATP_CONTROL_SYN) {
		uint8_t control;

		ri->status = -ECONNRESET;
		pr_debug("Error: Connection reset\n");

		control = RATP_CONTROL_RST | RATP_CONTROL_ACK |
			ratp_set_sn(ratp_an(hdr)) | ratp_set_an(!ratp_sn(hdr));
		ratp_send_hdr(ri, control);

		ratp_state_change(ri, RATP_STATE_CLOSED);
		return 1;
	}

	pr_debug("Sending ack for duplicate message\n");
	ret = ratp_send_ack(ri, hdr);
	if (ret)
		return ret;

	return 1;
}

/*
 * The packet is examined for a RST flag.  If RST is not set then
 * return and continue the processing associated with this state.
 *
 * RST is now assumed to have been set.  If the connection was
 * originally initiated from the LISTEN state (it was passively
 * opened) then flush the retransmission queue, discard the
 * packet, and go to the LISTEN state without any further
 * processing.
 *
 * If instead the connection was initiated actively (came from the
 * SYN-SENT state) then flush the retransmission queue, inform the
 * user "Error: Connection refused", discard the packet, delete
 * the TCB, and go to the CLOSED state without any further
 * processing.
 */
static int ratp_behaviour_d1(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_RST))
		return 0;

	if (!(ri->active)) {
		ratp_state_change(ri, RATP_STATE_LISTEN);
		return 1;
	}

	ri->status = -ECONNREFUSED;

	pr_debug("Error: connection refused\n");

	ratp_state_change(ri, RATP_STATE_CLOSED);

	return 1;
}

/*
 * The packet is examined for a RST flag.  If RST is not set then
 * return and continue the processing associated with this state.
 *
 * RST is now assumed to have been set.  Any data remaining to be
 * sent is flushed.  The retransmission queue is flushed, the user
 * is informed "Error: Connection reset.", discard the packet,
 * delete the TCB, and go to the CLOSED state without any further
 * processing.
 */
static int ratp_behaviour_d2(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_RST))
		return 0;

	ri->status = -ECONNRESET;

	pr_debug("connection reset\n");

	return 0;
}

/*
 * The packet is examined for a RST flag.  If RST is not set then
 * return and continue the processing associated with this state.
 *
 * RST is now assumed to have been set.  Discard the packet,
 * delete the TCB, and go to the CLOSED state without any further
 * processing.
 */
static int ratp_behaviour_d3(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_RST))
		return 0;

	ratp_state_change(ri, RATP_STATE_CLOSED);

	return 1;
}

/*
 * Check the presence of the SYN flag.  If the SYN flag is not set
 * then return and continue the processing associated with this
 * state.
 *
 * We now assume that the SYN flag was set.  The presence of a SYN
 * here is an error.  Flush the retransmission queue, send a legal
 * RST packet.
 *
 * If the ACK flag was set then send:
 *
 * <SN=received AN><CTL=RST>
 *
 * If the ACK flag was not set then send:
 *
 * <SN=0><CTL=RST>
 *
 * The user should receive the message "Error: Connection reset.",
 * then delete the TCB and go to the CLOSED state without any
 * further processing.
 */
static int ratp_behaviour_e(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	uint8_t control;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_SYN))
		return 0;

	ri->status = -ECONNRESET;

	control = RATP_CONTROL_RST;

	if (hdr->control & RATP_CONTROL_ACK)
		control |= ratp_set_sn(ratp_an(hdr));

	ratp_send_hdr(ri, control);

	pr_debug("connection reset\n");

	ratp_state_change(ri, RATP_STATE_CLOSED);

	return 1;
}

/*
 * Check the presence of the ACK flag.  If ACK is not set then
 * discard the packet and return without any further processing.
 *
 * We now assume that the ACK flag was set.  If the AN field value
 * was as expected then return and continue the processing
 * associated with this state.
 *
 * We now assume that the ACK flag was set and that the AN field
 * value was unexpected.  If the connection was originally
 * initiated from the LISTEN state (it was passively opened) then
 * flush the retransmission queue, discard the packet, and send a
 * legal RST packet:
 *
 * <SN=received AN><CTL=RST>
 *
 * Then delete the TCB and go to the LISTEN state without any
 * further processing.
 *
 * Otherwise the connection was initiated actively (came from the
 * SYN-SENT state) then inform the user "Error: Connection
 * refused", flush the retransmission queue, discard the packet,
 * and send a legal RST packet:
 *
 * <SN=received AN><CTL=RST>
 *
 * Then delete the TCB and go to the CLOSED state without any
 * further processing.
 */
static int ratp_behaviour_f1(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	uint8_t control;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_ACK))
		return 1;

	if (ratp_an_expected(ri, hdr))
		return 0;

	control = RATP_CONTROL_RST | ratp_set_sn(ratp_an(hdr));
	ratp_send_hdr(ri, control);

	if (ri->active) {
		ratp_state_change(ri, RATP_STATE_CLOSED);
		ri->status = -ECONNREFUSED;

		pr_debug("connection refused\n");
	} else {
		ratp_state_change(ri, RATP_STATE_LISTEN);
	}

	return 1;
}

/*
 * Check the presence of the ACK flag.  If ACK is not set then
 * discard the packet and return without any further processing.
 *
 * We now assume that the ACK flag was set.  If the AN field value
 * was as expected then flush the retransmission queue and inform
 * the user with an "Ok" if a buffer has been entirely
 * acknowledged.  Another packet containing data may now be sent.
 * Return and continue the processing associated with this state.
 *
 * We now assume that the ACK flag was set and that the AN field
 * value was unexpected.  This is assumed to indicate a duplicate
 * acknowledgment.  It is ignored, return and continue the
 * processing associated with this state.
 */
static int ratp_behaviour_f2(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_ACK))
		return 1;

	if (ratp_an_expected(ri, hdr)) {
		pr_debug("Data succesfully sent\n");
		if (ri->sendmsg_current)
			ratp_msg_done(ri, ri->sendmsg_current, 0);
		ri->sendmsg_current = NULL;
		return 0;
	} else {
		pr_vdebug("%s: an not expected\n", __func__);
	}

	return 0;
}

/*
 * Check the presence of the ACK flag.  If ACK is not set then
 * discard the packet and return without any further processing.
 *
 * We now assume that the ACK flag was set.  If the AN field value
 * was as expected then continue the processing associated with
 * this state.
 *
 * We now assume that the ACK flag was set and that the AN field
 * value was unexpected.  This is ignored, return and continue
 * with the processing associated with this state.
 */
static int ratp_behaviour_f3(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_ACK))
		return 1;

	return 0;
}

/*
 * This procedure represents the behavior of the CLOSED state of a
 * connection.  All incoming packets are discarded.  If the packet
 * had the RST flag set take no action.  Otherwise it is necessary
 * to build a RST packet.  Since this end is closed the other end
 * of the connection has incorrect data about the state of the
 * connection and should be so informed.
 *
 * If the ACK flag was set then send:
 *
 * <SN=received AN><CTL=RST>
 *
 * If the ACK flag was not set then send:
 *
 * <SN=0><AN=received SN+1 modulo 2><CTL=RST, ACK>
 *
 * After sending the reset packet return to the current state
 * without any further processing.
 */
static int ratp_behaviour_g(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	uint8_t control;

	pr_debug("%s\n", __func__);

	if (hdr->control & RATP_CONTROL_RST)
		return 0;

	control = RATP_CONTROL_RST;

	if (hdr->control & RATP_CONTROL_ACK)
		control |= ratp_set_sn(ratp_an(hdr));
	else
		control |= ratp_set_an(ratp_sn(hdr) + 1) | RATP_CONTROL_ACK;

	ratp_send_hdr(ri, control);

	return 0;
}

/*
 * Our SYN has been acknowledged.  At this point we are
 * technically in the ESTABLISHED state.  Send any initial data
 * which is queued to send:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=ACK><DATA>
 *
 * Go to the ESTABLISHED state and execute procedure I1 to process
 * any data which might be in this packet.
 *
 * Any packet not satisfying the above tests is discarded and
 * ignored.  Return to the current state without any further
 * processing.
 */
static int ratp_behaviour_h1(struct ratp_internal *ri, void *pkt)
{
	pr_debug("%s\n", __func__);

	ratp_state_change(ri, RATP_STATE_ESTABLISHED);

	return 0;
}

/*
 * Check the presence of the FIN flag.  If FIN is not set then
 * continue the processing associated with this state.
 *
 * We now assume that the FIN flag was set.  This means the other
 * end has decided to close the connection.  Flush the
 * retransmission queue.  If any data remains to be sent then
 * inform the user "Warning: Data left unsent."  The user must
 * also be informed "Connection closing."  An acknowledgment for
 * the FIN must be sent which also indicates this end is closing:
 *
 * <SN=received AN><AN=received SN + 1 modulo 2><CTL=FIN, ACK>
 *
 * Go to the LAST-ACK state without any further processing.
 */
static int ratp_behaviour_h2(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	uint8_t control;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_FIN))
		return 0;

	ri->status = -ENETDOWN;

	control = ratp_set_sn(ratp_an(hdr)) |
		ratp_set_an(ratp_sn(hdr) + 1) |
		RATP_CONTROL_FIN |
		RATP_CONTROL_ACK;

	ratp_send_hdr(ri, control);

	ratp_state_change(ri, RATP_STATE_LAST_ACK);

	return 1;
}

/*
 * This state represents the final behavior of the FIN-WAIT state.
 *
 * If the packet did not contain a FIN we assume this packet is a
 * duplicate and that the other end of the connection has not seen
 * the FIN packet we sent earlier.  Rely upon retransmission of
 * our earlier FIN packet to inform the other end of our desire to
 * close.  Discard the packet and return without any further
 * processing.
 *
 * At this point we have a packet which should contain a FIN.  By
 * the rules of this protocol an ACK of a FIN requires a FIN, ACK
 * in response and no data.  If the packet contains data we have
 * detected an illegal condition.  Send a reset:
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=RST, ACK>
 *
 * Discard the packet, flush the retransmission queue, inform the
 * ser "Error: Connection reset.", delete the TCB, and go to the
 * CLOSED state without any further processing.
 *
 * We now assume that the FIN flag was set and no data was
 * contained in the packet.  If the AN field value was expected
 * then this packet acknowledges a previously sent FIN packet.
 * The other end of the connection is then also assumed to be
 * closing and expects an acknowledgment.  Send an acknowledgment
 * of the FIN:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=ACK>
 *
 * Start the 2*SRTT timer associated with the TIME-WAIT state,
 * discard the packet, and go to the TIME-WAIT state without any
 * further processing.
 *
 * Otherwise the AN field value was unexpected.  This indicates a
 * simultaneous closing by both sides of the connection.  Send an
 * acknowledgment of the FIN:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=ACK>
 *
 * Discard the packet, and go to the CLOSING state without any
 * further processing.
 */
static int ratp_behaviour_h3(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	uint8_t control;
	int expected;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_FIN))
		return 1;

	if (ratp_has_data(hdr)) {
		control = ratp_set_sn(ratp_an(hdr)) |
			ratp_set_an(ratp_sn(hdr) + 1) |
			RATP_CONTROL_RST |
			RATP_CONTROL_ACK;
		ratp_send_hdr(ri, control);
		ri->status = -ECONNRESET;
		pr_debug("Error: Connection reset\n");
		ratp_state_change(ri, RATP_STATE_CLOSED);
		return 1;
	}

	control = ratp_set_sn(ratp_an(hdr)) |
		ratp_set_an(ratp_sn(hdr) + 1) |
		RATP_CONTROL_ACK;

	expected = ratp_an_expected(ri, hdr);

	ratp_send_hdr(ri, control);

	if (expected) {
		ratp_state_change(ri, RATP_STATE_TIME_WAIT);
		ratp_start_time_wait_timer(ri);
	} else {
		ratp_state_change(ri, RATP_STATE_CLOSING);
	}

	return 1;
}

/*
 * This state represents the final behavior of the LAST-ACK state.
 *
 * If the AN field value is expected then this ACK is in response
 * to the FIN, ACK packet recently sent.  This is the final
 * acknowledging message indicating both side's agreement to close
 * the connection.  Discard the packet, flush all queues, delete
 * the TCB, and go to the CLOSED state without any further
 * processing.
 *
 * Otherwise the AN field value was unexpected.  Discard the
 * packet and remain in the current state without any further
 * processing.
 */
static int ratp_behaviour_h4(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if (ratp_an_expected(ri, hdr))
		ratp_state_change(ri, RATP_STATE_CLOSED);

	return 1;
}

/*
 * This state represents the final behavior of the CLOSING state.
 *
 * If the AN field value was expected then this packet
 * acknowledges the FIN packet recently sent.  This is the final
 * acknowledging message indicating both side's agreement to close
 * the connection.  Start the 2*SRTT timer associated with the
 * TIME-WAIT state, discard the packet, and go to the TIME-WAIT
 * state without any further processing.
 *
 * Otherwise the AN field value was unexpected.  Discard the
 * packet and remain in the current state without any further
 * processing.
 */
static int ratp_behaviour_h5(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;

	pr_debug("%s\n", __func__);

	if (ratp_an_expected(ri, hdr)) {
		ratp_state_change(ri, RATP_STATE_TIME_WAIT);
		ratp_start_time_wait_timer(ri);
	}

	return 0;
}

/*
 * This state represents the behavior of the TIME-WAIT state.
 * Check the presence of the ACK flag.  If ACK is not set then
 * discard the packet and return without any further processing.
 *
 * Check the presence of the FIN flag.  If FIN is not set then
 * discard the packet and return without any further processing.
 *
 * We now assume that the FIN flag was set.  This situation
 * indicates that the last acknowledgment of the FIN packet sent
 * by the other end of the connection did not arrive.  Resend the
 * acknowledgment:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=ACK>
 *
 * Restart the 2*SRTT timer, discard the packet, and remain in the
 * current state without any further processing.
 */
static int ratp_behaviour_h6(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	uint8_t control;

	pr_debug("%s\n", __func__);

	if (!(hdr->control & RATP_CONTROL_ACK))
		return 1;

	if (!(hdr->control & RATP_CONTROL_FIN))
		return 1;

	control = ratp_set_sn(ratp_an(hdr) + 1) | RATP_CONTROL_ACK;

	ratp_send_hdr(ri, control);

	ratp_start_time_wait_timer(ri);

	return 0;
}

static int msg_recv(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	struct ratp_message *msg;

	pr_debug("%s: Put msg in receive queue\n", __func__);

	msg = xzalloc(sizeof(*msg));
	if (hdr->data_length) {
		msg->len = hdr->data_length;
		msg->buf = xzalloc(msg->len);
		memcpy(msg->buf, pkt + sizeof(struct ratp_header), msg->len);
	} else {
		msg->len = 1;
		msg->buf = xzalloc(1);
		*(uint8_t *)msg->buf = hdr->data_length;
	}

	if (hdr->control & RATP_CONTROL_EOR)
		msg->eor = 1;

	list_add_tail(&msg->list, &ri->recvmsg);

	return 0;
}

/*
 * This represents that stage of processing in the ESTABLISHED
 * state in which all the flag bits have been processed and only
 * data may remain.  The packet is examined to see if it contains
 * data.  If not the packet is now discarded, return to the
 * current state without any further processing.
 *
 * We assume the packet contained data, that either the SO flag
 * was set or LENGTH is positive.  That data is placed into the
 * user's receive buffers.  As these become full the user should
 * be informed "Receive buffer full."  An acknowledgment is sent:
 *
 * <SN=received AN><AN=received SN+1 modulo 2><CTL=ACK>
 *
 * If data is queued to send then it is most efficient to
 * 'piggyback' this acknowledgment on that data packet.
 *
 * The packet is now discarded, return to the ESTABLISHED state
 * without any further processing.
 */
static int ratp_behaviour_i1(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	uint8_t control = 0;

	if (!ratp_has_data (hdr))
		return 1;

	pr_vdebug("%s **received** %d\n", __func__, hdr->data_length);

	ri->sn_received = ratp_sn(hdr);

	msg_recv(ri, pkt);

	if (list_empty(&ri->sendmsg) || ri->sendmsg_current) {
		control = ratp_set_sn(!ri->sn_sent) |
			ratp_set_an(ri->sn_received + 1) |
			RATP_CONTROL_ACK;

		ratp_send_hdr(ri, control);
	} else {
		ratp_send_next_data(ri);
	}

	return 0;
}

/*
 * State machine as desribed in RFC916
 *
 * STATE                BEHAVIOR
 * =============+========================
 * LISTEN       |  A
 * -------------+------------------------
 * SYN-SENT     |  B
 * -------------+------------------------
 * SYN-RECEIVED |  C1  D1  E  F1  H1
 * -------------+------------------------
 * ESTABLISHED  |  C2  D2  E  F2  H2  I1
 * -------------+------------------------
 * FIN-WAIT     |  C2  D2  E  F3  H3
 * -------------+------------------------
 * LAST-ACK     |  C2  D3  E  F3  H4
 * -------------+------------------------
 * CLOSING      |  C2  D3  E  F3  H5
 * -------------+------------------------
 * TIME-WAIT    |  D3  E  F3 H6
 * -------------+------------------------
 * CLOSED       |  G
 * -------------+------------------------
 */

static int ratp_state_machine(struct ratp_internal *ri, void *pkt)
{
	struct ratp_header *hdr = pkt;
	int ret;

	ratp_print_header(ri, hdr, "                      recv");
	pr_debug(" state %s\n", ratp_state_str[ri->state]);

	switch (ri->state) {
	case RATP_STATE_LISTEN:
		ratp_behaviour_a(ri, pkt);
		break;
	case RATP_STATE_SYN_SENT:
		ratp_behaviour_b(ri, pkt);
		break;
	case RATP_STATE_SYN_RECEIVED:
		ret = ratp_behaviour_c1(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_d1(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_e(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_f1(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_h1(ri, pkt);
		if (ret)
			return ret;
		break;
	case RATP_STATE_ESTABLISHED:
		ret = ratp_behaviour_c2(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_d2(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_e(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_f2(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_h2(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_i1(ri, pkt);
		if (ret)
			return ret;
		break;
	case RATP_STATE_FIN_WAIT:
		ret = ratp_behaviour_c2(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_d2(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_e(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_f3(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_h3(ri, pkt);
		if (ret)
			return ret;
		break;
	case RATP_STATE_LAST_ACK:
		ret = ratp_behaviour_c2(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_d3(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_e(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_f3(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_h4(ri, pkt);
		if (ret)
			return ret;
		break;
	case RATP_STATE_CLOSING:
		ret = ratp_behaviour_c2(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_d3(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_e(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_f3(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_h5(ri, pkt);
		if (ret)
			return ret;
		break;
	case RATP_STATE_TIME_WAIT:
		ret = ratp_behaviour_d3(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_e(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_f3(ri, pkt);
		if (ret)
			return ret;
		ret = ratp_behaviour_h6(ri, pkt);
		if (ret)
			return ret;
		break;
	case RATP_STATE_CLOSED:
		ratp_behaviour_g(ri, pkt);
		break;
	};

	return 0;
}

/**
 * ratp_closed() - Check if a connection is closed
 *
 * Return: true if a connection is closed, false otherwise
 */
bool ratp_closed(struct ratp *ratp)
{
	struct ratp_internal *ri = ratp->internal;

	if (!ri)
		return true;

	return ri->state == RATP_STATE_CLOSED;
}

/**
 * ratp_busy() - Check if we are inside the RATP code
 *
 * Needed for RATP debugging. The RATP console uses this to determine
 * if it is called from inside the RATP code.
 *
 * Return: true if we are inside the RATP code, false otherwise
 */
bool ratp_busy(struct ratp *ratp)
{
	struct ratp_internal *ri = ratp->internal;

	if (!ri)
		return false;

	return ri->in_ratp != 0;
}

/**
 * ratp_poll() - Execute RATP state machine
 * @ratp: The RATP link
 *
 * This function should be executed periodically to keep the RATP state
 * machine going.
 *
 * Return: 0 if successful, a negative error code otherwise.
 */
int ratp_poll(struct ratp *ratp)
{
	struct ratp_internal *ri = ratp->internal;
	int ret;

	if (!ri)
		return -ENETDOWN;

	ri->in_ratp++;

	ret = ratp_recv_pkt(ri, ri->recvbuf, 100);
	if (ret == 0) {

		if (ri->state == RATP_STATE_TIME_WAIT &&
		    is_timeout(ri->timewait_timer_start, ri->srtt * 2 * MSECOND)) {
			pr_debug("2*SRTT timer timed out\n");
			ret = -ECONNRESET;
			goto out;
		}

		ret = ratp_state_machine(ri, ri->recvbuf);
		if (ret < 0)
			goto out;

		if (ri->status < 0) {
			ret = ri->status;
			goto out;
		}
	}

	if (ri->sendmsg_current && is_timeout(ri->retransmission_timer_start,
	    ri->rto * MSECOND)) {

		ri->retransmission_count++;
		if (ri->retransmission_count == ri->max_retransmission) {
			ri->status = ret = -ETIMEDOUT;
			ri->state = RATP_STATE_CLOSED;
			goto out;
		}

		pr_debug("%s: retransmit\n", __func__);

		ratp_print_header(ri, ri->sendbuf, "resend");

		ri->retransmission_timer_start = get_time_ns();

		ret = ri->ratp->send(ratp, ri->sendbuf, ri->sendbuf_len);
		if (ret)
			goto out;
	}

	if (!ri->sendmsg_current && !list_empty(&ri->sendmsg))
		ratp_send_next_data(ri);

	ret = 0;
out:
	ri->in_ratp--;

	return ret;
}

/**
 * ratp_establish(): Establish a RATP link
 * @ratp: The RATP link
 * @active: if true actively create a connection
 * @timeout_ms: Timeout in ms to wait until a connection is established. If
 *              0 wait forever.
 *
 * This function establishes a link with the remote end. It expects the
 * send and receive functions to be set, all other struct ratp_internal members can
 * be left uninitialized.
 *
 * Return: 0 if successful, a negative error code otherwise.
 */
int ratp_establish(struct ratp *ratp, bool active, int timeout_ms)
{
	struct ratp_internal *ri;
	int ret;
	uint64_t start;

	ri = xzalloc(sizeof(*ri));
	ri->ratp = ratp;
	ratp->internal = ri;

	ri->recvbuf = xmalloc(512);
	ri->sendbuf = xmalloc(512);
	INIT_LIST_HEAD(&ri->recvmsg);
	INIT_LIST_HEAD(&ri->sendmsg);
	ri->max_retransmission = 100;
	ri->srtt = 100;
	ri->rto = 100;
	ri->active = active;

	ri->in_ratp++;

	if (ri->active) {
		ratp_send_hdr(ri, RATP_CONTROL_SYN);

		ratp_state_change(ri, RATP_STATE_SYN_SENT);
	}

	start = get_time_ns();

	while (1) {
		ret = ratp_poll(ri->ratp);
		if (ret < 0)
			goto out;

		if (ri->state == RATP_STATE_ESTABLISHED) {
			ret = 0;
			goto out;
		}

		if (timeout_ms && is_timeout(start, MSECOND * timeout_ms)) {
			ret = -ETIMEDOUT;
			goto out;
		}
	}

out:
	ri->in_ratp--;

	if (ret) {
		free(ri->recvbuf);
		free(ri->sendbuf);
		free(ri);
		ratp->internal = NULL;
	}

	return ret;
}

void ratp_close(struct ratp *ratp)
{
	struct ratp_internal *ri = ratp->internal;
	struct ratp_message *msg, *tmp;
	struct ratp_header fin = {};

	if (!ri)
		return;

	if (ri->state == RATP_STATE_ESTABLISHED) {
		uint64_t start;
		u8 control;

		pr_debug("Closing...\n");

		ratp_state_change(ri, RATP_STATE_FIN_WAIT);

		control = ratp_set_sn(!ri->sn_sent) |
			ratp_set_an(ri->sn_received + 1) |
			RATP_CONTROL_FIN | RATP_CONTROL_ACK;

		ratp_create_packet(ri, &fin, control, 0);

		ratp_send_pkt(ri, &fin, sizeof(fin));

		start = get_time_ns();

		while (!is_timeout(start, ri->srtt * MSECOND * 2))
			ratp_poll(ratp);
	}

	list_for_each_entry_safe(msg, tmp, &ri->sendmsg, list)
		ratp_msg_done(ri, msg, -ECONNRESET);

	free(ri);
	ratp->internal = NULL;

	pr_info("Closed\n");
}

/**
 * ratp_send_complete(): Send data over a RATP link
 * @ratp: The RATP link
 * @data: The data buffer
 * @len: The length of the message to send
 * @complete: The completion callback for the message
 * @complete_ctx: context pointer for the completion callback
 *
 * Queue a RATP message for transmission. This only queues the message,
 * ratp_poll has to be called to actually transfer the message.
 * @complete will be called upon completion of the message.
 *
 * Return: 0 if successful, a negative error code otherwise.
 */
int ratp_send_complete(struct ratp *ratp, const void *data, size_t len,
		   void (*complete)(void *ctx, int status), void *complete_ctx)
{
	struct ratp_internal *ri = ratp->internal;
	struct ratp_message *msg;

	if (!ri || ri->state != RATP_STATE_ESTABLISHED)
		return -ENETDOWN;

	if (!len)
		return -EINVAL;

	ri->in_ratp++;

	while (len) {
		int now = min((int)len, 255);

		msg = xzalloc(sizeof(*msg));
		msg->buf = xzalloc(sizeof(struct ratp_header) + now + 2);
		msg->len = now;
		memcpy(msg->buf + sizeof(struct ratp_header), data, now);

		list_add_tail(&msg->list, &ri->sendmsg);

		len -= now;
	}

	msg->eor = 1;
	msg->complete = complete;
	msg->complete_ctx = complete_ctx;

	ri->in_ratp--;

	return 0;
}

/**
 * ratp_send(): Send data over a RATP link
 * @ratp: The RATP link
 * @data: The data buffer
 * @len: The length of the message to send
 *
 * Queue a RATP message for transmission. This only queues the message,
 * ratp_poll has to be called to actually transfer the message.
 *
 * Return: 0 if successful, a negative error code otherwise.
 */
int ratp_send(struct ratp *ratp, const void *data, size_t len)
{
	return ratp_send_complete(ratp, data, len, NULL, NULL);
}

/**
 * ratp_recv() - Receive data from a RATP link
 * @ratp: The RATP link
 * @data: Pointer to data
 * @len: The length of the data in bytes
 *
 * If a message is available it fills @data with a pointer to the data.
 * This function does not wait for new messages. If no data is available
 * -EAGAIN is returned. If data is received @data has to be freed by the
 * caller.
 *
 * Return: 0 if successful, a negative error code otherwise.
 */
int ratp_recv(struct ratp *ratp, void **data, size_t *len)
{
	struct ratp_internal *ri = ratp->internal;
	struct ratp_message *msg, *tmp;
	void *pos;
	int num = 0;

	*len = 0;

	if (!ri || ri->state != RATP_STATE_ESTABLISHED)
		return -ENETDOWN;

	if (list_empty(&ri->recvmsg))
		return -EAGAIN;

	list_for_each_entry(msg, &ri->recvmsg, list) {
		*len += msg->len;
		num++;
		if (msg->eor)
			goto eor;
	}

	return -EAGAIN;

eor:
	*data = malloc(*len);
	if (!*data)
		return -ENOMEM;

	pos = *data;

	list_for_each_entry_safe(msg, tmp, &ri->recvmsg, list) {
		memcpy(pos, msg->buf, msg->len);
		pos += msg->len;

		list_del(&msg->list);

		free(msg->buf);
		free(msg);
	}

	return 0;
}