tcp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2017 Duncan Hare, all rights reserved.
  4. */
  5. /*
  6. * General Desription:
  7. *
  8. * TCP support for the wget command, for fast file downloading.
  9. *
  10. * HTTP/TCP Receiver:
  11. *
  12. * Prerequisites: - own ethernet address
  13. * - own IP address
  14. * - Server IP address
  15. * - Server with TCP
  16. * - TCP application (eg wget)
  17. * Next Step HTTPS?
  18. */
  19. #include <common.h>
  20. #include <command.h>
  21. #include <console.h>
  22. #include <env_internal.h>
  23. #include <errno.h>
  24. #include <net.h>
  25. #include <net/tcp.h>
  26. /*
  27. * TCP sliding window control used by us to request re-TX
  28. */
  29. static struct tcp_sack_v tcp_lost;
  30. /* TCP option timestamp */
  31. static u32 loc_timestamp;
  32. static u32 rmt_timestamp;
  33. static u32 tcp_seq_init;
  34. static u32 tcp_ack_edge;
  35. static int tcp_activity_count;
  36. /*
  37. * Search for TCP_SACK and review the comments before the code section
  38. * TCP_SACK is the number of packets at the front of the stream
  39. */
  40. enum pkt_state {PKT, NOPKT};
  41. struct sack_r {
  42. struct sack_edges se;
  43. enum pkt_state st;
  44. };
  45. static struct sack_r edge_a[TCP_SACK];
  46. static unsigned int sack_idx;
  47. static unsigned int prev_len;
  48. /*
  49. * TCP lengths are stored as a rounded up number of 32 bit words.
  50. * Add 3 to length round up, rounded, then divided into the
  51. * length in 32 bit words.
  52. */
  53. #define LEN_B_TO_DW(x) ((x) >> 2)
  54. #define ROUND_TCPHDR_LEN(x) (LEN_B_TO_DW((x) + 3))
  55. #define SHIFT_TO_TCPHDRLEN_FIELD(x) ((x) << 4)
  56. #define GET_TCP_HDR_LEN_IN_BYTES(x) ((x) >> 2)
  57. /* TCP connection state */
  58. static enum tcp_state current_tcp_state;
  59. /* Current TCP RX packet handler */
  60. static rxhand_tcp *tcp_packet_handler;
  61. /**
  62. * tcp_get_tcp_state() - get current TCP state
  63. *
  64. * Return: Current TCP state
  65. */
  66. enum tcp_state tcp_get_tcp_state(void)
  67. {
  68. return current_tcp_state;
  69. }
  70. /**
  71. * tcp_set_tcp_state() - set current TCP state
  72. * @new_state: new TCP state
  73. */
  74. void tcp_set_tcp_state(enum tcp_state new_state)
  75. {
  76. current_tcp_state = new_state;
  77. }
  78. static void dummy_handler(uchar *pkt, u16 dport,
  79. struct in_addr sip, u16 sport,
  80. u32 tcp_seq_num, u32 tcp_ack_num,
  81. u8 action, unsigned int len)
  82. {
  83. }
  84. /**
  85. * tcp_set_tcp_handler() - set a handler to receive data
  86. * @f: handler
  87. */
  88. void tcp_set_tcp_handler(rxhand_tcp *f)
  89. {
  90. debug_cond(DEBUG_INT_STATE, "--- net_loop TCP handler set (%p)\n", f);
  91. if (!f)
  92. tcp_packet_handler = dummy_handler;
  93. else
  94. tcp_packet_handler = f;
  95. }
  96. /**
  97. * tcp_set_pseudo_header() - set TCP pseudo header
  98. * @pkt: the packet
  99. * @src: source IP address
  100. * @dest: destinaion IP address
  101. * @tcp_len: tcp length
  102. * @pkt_len: packet length
  103. *
  104. * Return: the checksum of the packet
  105. */
  106. u16 tcp_set_pseudo_header(uchar *pkt, struct in_addr src, struct in_addr dest,
  107. int tcp_len, int pkt_len)
  108. {
  109. union tcp_build_pkt *b = (union tcp_build_pkt *)pkt;
  110. int checksum_len;
  111. /*
  112. * Pseudo header
  113. *
  114. * Zero the byte after the last byte so that the header checksum
  115. * will always work.
  116. */
  117. pkt[pkt_len] = 0;
  118. net_copy_ip((void *)&b->ph.p_src, &src);
  119. net_copy_ip((void *)&b->ph.p_dst, &dest);
  120. b->ph.rsvd = 0;
  121. b->ph.p = IPPROTO_TCP;
  122. b->ph.len = htons(tcp_len);
  123. checksum_len = tcp_len + PSEUDO_HDR_SIZE;
  124. debug_cond(DEBUG_DEV_PKT,
  125. "TCP Pesudo Header (to=%pI4, from=%pI4, Len=%d)\n",
  126. &b->ph.p_dst, &b->ph.p_src, checksum_len);
  127. return compute_ip_checksum(pkt + PSEUDO_PAD_SIZE, checksum_len);
  128. }
  129. /**
  130. * net_set_ack_options() - set TCP options in acknowledge packets
  131. * @b: the packet
  132. *
  133. * Return: TCP header length
  134. */
  135. int net_set_ack_options(union tcp_build_pkt *b)
  136. {
  137. b->sack.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE));
  138. b->sack.t_opt.kind = TCP_O_TS;
  139. b->sack.t_opt.len = TCP_OPT_LEN_A;
  140. b->sack.t_opt.t_snd = htons(loc_timestamp);
  141. b->sack.t_opt.t_rcv = rmt_timestamp;
  142. b->sack.sack_v.kind = TCP_1_NOP;
  143. b->sack.sack_v.len = 0;
  144. if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) {
  145. if (tcp_lost.len > TCP_OPT_LEN_2) {
  146. debug_cond(DEBUG_DEV_PKT, "TCP ack opt lost.len %x\n",
  147. tcp_lost.len);
  148. b->sack.sack_v.len = tcp_lost.len;
  149. b->sack.sack_v.kind = TCP_V_SACK;
  150. b->sack.sack_v.hill[0].l = htonl(tcp_lost.hill[0].l);
  151. b->sack.sack_v.hill[0].r = htonl(tcp_lost.hill[0].r);
  152. /*
  153. * These SACK structures are initialized with NOPs to
  154. * provide TCP header alignment padding. There are 4
  155. * SACK structures used for both header padding and
  156. * internally.
  157. */
  158. b->sack.sack_v.hill[1].l = htonl(tcp_lost.hill[1].l);
  159. b->sack.sack_v.hill[1].r = htonl(tcp_lost.hill[1].r);
  160. b->sack.sack_v.hill[2].l = htonl(tcp_lost.hill[2].l);
  161. b->sack.sack_v.hill[2].r = htonl(tcp_lost.hill[2].r);
  162. b->sack.sack_v.hill[3].l = TCP_O_NOP;
  163. b->sack.sack_v.hill[3].r = TCP_O_NOP;
  164. }
  165. b->sack.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_SIZE +
  166. TCP_TSOPT_SIZE +
  167. tcp_lost.len));
  168. } else {
  169. b->sack.sack_v.kind = 0;
  170. b->sack.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_SIZE +
  171. TCP_TSOPT_SIZE));
  172. }
  173. /*
  174. * This returns the actual rounded up length of the
  175. * TCP header to add to the total packet length
  176. */
  177. return GET_TCP_HDR_LEN_IN_BYTES(b->sack.hdr.tcp_hlen);
  178. }
  179. /**
  180. * net_set_ack_options() - set TCP options in SYN packets
  181. * @b: the packet
  182. */
  183. void net_set_syn_options(union tcp_build_pkt *b)
  184. {
  185. if (IS_ENABLED(CONFIG_PROT_TCP_SACK))
  186. tcp_lost.len = 0;
  187. b->ip.hdr.tcp_hlen = 0xa0;
  188. b->ip.mss.kind = TCP_O_MSS;
  189. b->ip.mss.len = TCP_OPT_LEN_4;
  190. b->ip.mss.mss = htons(TCP_MSS);
  191. b->ip.scale.kind = TCP_O_SCL;
  192. b->ip.scale.scale = TCP_SCALE;
  193. b->ip.scale.len = TCP_OPT_LEN_3;
  194. if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) {
  195. b->ip.sack_p.kind = TCP_P_SACK;
  196. b->ip.sack_p.len = TCP_OPT_LEN_2;
  197. } else {
  198. b->ip.sack_p.kind = TCP_1_NOP;
  199. b->ip.sack_p.len = TCP_1_NOP;
  200. }
  201. b->ip.t_opt.kind = TCP_O_TS;
  202. b->ip.t_opt.len = TCP_OPT_LEN_A;
  203. loc_timestamp = get_ticks();
  204. rmt_timestamp = 0;
  205. b->ip.t_opt.t_snd = 0;
  206. b->ip.t_opt.t_rcv = 0;
  207. b->ip.end = TCP_O_END;
  208. }
  209. int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len,
  210. u8 action, u32 tcp_seq_num, u32 tcp_ack_num)
  211. {
  212. union tcp_build_pkt *b = (union tcp_build_pkt *)pkt;
  213. int pkt_hdr_len;
  214. int pkt_len;
  215. int tcp_len;
  216. /*
  217. * Header: 5 32 bit words. 4 bits TCP header Length,
  218. * 4 bits reserved options
  219. */
  220. b->ip.hdr.tcp_flags = action;
  221. pkt_hdr_len = IP_TCP_HDR_SIZE;
  222. b->ip.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE));
  223. switch (action) {
  224. case TCP_SYN:
  225. debug_cond(DEBUG_DEV_PKT,
  226. "TCP Hdr:SYN (%pI4, %pI4, sq=%u, ak=%u)\n",
  227. &net_server_ip, &net_ip,
  228. tcp_seq_num, tcp_ack_num);
  229. tcp_activity_count = 0;
  230. net_set_syn_options(b);
  231. tcp_seq_num = 0;
  232. tcp_ack_num = 0;
  233. pkt_hdr_len = IP_TCP_O_SIZE;
  234. if (current_tcp_state == TCP_SYN_SENT) { /* Too many SYNs */
  235. action = TCP_FIN;
  236. current_tcp_state = TCP_FIN_WAIT_1;
  237. } else {
  238. current_tcp_state = TCP_SYN_SENT;
  239. }
  240. break;
  241. case TCP_SYN | TCP_ACK:
  242. case TCP_ACK:
  243. pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(b);
  244. b->ip.hdr.tcp_flags = action;
  245. debug_cond(DEBUG_DEV_PKT,
  246. "TCP Hdr:ACK (%pI4, %pI4, s=%u, a=%u, A=%x)\n",
  247. &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num,
  248. action);
  249. break;
  250. case TCP_FIN:
  251. debug_cond(DEBUG_DEV_PKT,
  252. "TCP Hdr:FIN (%pI4, %pI4, s=%u, a=%u)\n",
  253. &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num);
  254. payload_len = 0;
  255. pkt_hdr_len = IP_TCP_HDR_SIZE;
  256. current_tcp_state = TCP_FIN_WAIT_1;
  257. break;
  258. case TCP_RST | TCP_ACK:
  259. case TCP_RST:
  260. debug_cond(DEBUG_DEV_PKT,
  261. "TCP Hdr:RST (%pI4, %pI4, s=%u, a=%u)\n",
  262. &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num);
  263. current_tcp_state = TCP_CLOSED;
  264. break;
  265. /* Notify connection closing */
  266. case (TCP_FIN | TCP_ACK):
  267. case (TCP_FIN | TCP_ACK | TCP_PUSH):
  268. if (current_tcp_state == TCP_CLOSE_WAIT)
  269. current_tcp_state = TCP_CLOSING;
  270. debug_cond(DEBUG_DEV_PKT,
  271. "TCP Hdr:FIN ACK PSH(%pI4, %pI4, s=%u, a=%u, A=%x)\n",
  272. &net_server_ip, &net_ip,
  273. tcp_seq_num, tcp_ack_num, action);
  274. fallthrough;
  275. default:
  276. pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(b);
  277. b->ip.hdr.tcp_flags = action | TCP_PUSH | TCP_ACK;
  278. debug_cond(DEBUG_DEV_PKT,
  279. "TCP Hdr:dft (%pI4, %pI4, s=%u, a=%u, A=%x)\n",
  280. &net_server_ip, &net_ip,
  281. tcp_seq_num, tcp_ack_num, action);
  282. }
  283. pkt_len = pkt_hdr_len + payload_len;
  284. tcp_len = pkt_len - IP_HDR_SIZE;
  285. tcp_ack_edge = tcp_ack_num;
  286. /* TCP Header */
  287. b->ip.hdr.tcp_ack = htonl(tcp_ack_edge);
  288. b->ip.hdr.tcp_src = htons(sport);
  289. b->ip.hdr.tcp_dst = htons(dport);
  290. b->ip.hdr.tcp_seq = htonl(tcp_seq_num);
  291. /*
  292. * TCP window size - TCP header variable tcp_win.
  293. * Change tcp_win only if you have an understanding of network
  294. * overrun, congestion, TCP segment sizes, TCP windows, TCP scale,
  295. * queuing theory and packet buffering. If there are too few buffers,
  296. * there will be data loss, recovery may work or the sending TCP,
  297. * the server, could abort the stream transmission.
  298. * MSS is governed by maximum Ethernet frame length.
  299. * The number of buffers is governed by the desire to have a queue of
  300. * full buffers to be processed at the destination to maximize
  301. * throughput. Temporary memory use for the boot phase on modern
  302. * SOCs is may not be considered a constraint to buffer space, if
  303. * it is, then the u-boot tftp or nfs kernel netboot should be
  304. * considered.
  305. */
  306. b->ip.hdr.tcp_win = htons(PKTBUFSRX * TCP_MSS >> TCP_SCALE);
  307. b->ip.hdr.tcp_xsum = 0;
  308. b->ip.hdr.tcp_ugr = 0;
  309. b->ip.hdr.tcp_xsum = tcp_set_pseudo_header(pkt, net_ip, net_server_ip,
  310. tcp_len, pkt_len);
  311. net_set_ip_header((uchar *)&b->ip, net_server_ip, net_ip,
  312. pkt_len, IPPROTO_TCP);
  313. return pkt_hdr_len;
  314. }
  315. /**
  316. * tcp_hole() - Selective Acknowledgment (Essential for fast stream transfer)
  317. * @tcp_seq_num: TCP sequence start number
  318. * @len: the length of sequence numbers
  319. */
  320. void tcp_hole(u32 tcp_seq_num, u32 len)
  321. {
  322. u32 idx_sack, sack_in;
  323. u32 sack_end = TCP_SACK - 1;
  324. u32 hill = 0;
  325. enum pkt_state expect = PKT;
  326. u32 seq = tcp_seq_num - tcp_seq_init;
  327. u32 hol_l = tcp_ack_edge - tcp_seq_init;
  328. u32 hol_r = 0;
  329. /* Place new seq number in correct place in receive array */
  330. if (prev_len == 0)
  331. prev_len = len;
  332. idx_sack = sack_idx + ((tcp_seq_num - tcp_ack_edge) / prev_len);
  333. if (idx_sack < TCP_SACK) {
  334. edge_a[idx_sack].se.l = tcp_seq_num;
  335. edge_a[idx_sack].se.r = tcp_seq_num + len;
  336. edge_a[idx_sack].st = PKT;
  337. /*
  338. * The fin (last) packet is not the same length as data
  339. * packets, and if it's length is recorded and used for
  340. * array index calculation, calculation breaks.
  341. */
  342. if (prev_len < len)
  343. prev_len = len;
  344. }
  345. debug_cond(DEBUG_DEV_PKT,
  346. "TCP 1 seq %d, edg %d, len %d, sack_idx %d, sack_end %d\n",
  347. seq, hol_l, len, sack_idx, sack_end);
  348. /* Right edge of contiguous stream, is the left edge of first hill */
  349. hol_l = tcp_seq_num - tcp_seq_init;
  350. hol_r = hol_l + len;
  351. if (IS_ENABLED(CONFIG_PROT_TCP_SACK))
  352. tcp_lost.len = TCP_OPT_LEN_2;
  353. debug_cond(DEBUG_DEV_PKT,
  354. "TCP 1 in %d, seq %d, pkt_l %d, pkt_r %d, sack_idx %d, sack_end %d\n",
  355. idx_sack, seq, hol_l, hol_r, sack_idx, sack_end);
  356. for (sack_in = sack_idx; sack_in < sack_end && hill < TCP_SACK_HILLS;
  357. sack_in++) {
  358. switch (expect) {
  359. case NOPKT:
  360. switch (edge_a[sack_in].st) {
  361. case NOPKT:
  362. debug_cond(DEBUG_INT_STATE, "N");
  363. break;
  364. case PKT:
  365. debug_cond(DEBUG_INT_STATE, "n");
  366. if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) {
  367. tcp_lost.hill[hill].l =
  368. edge_a[sack_in].se.l;
  369. tcp_lost.hill[hill].r =
  370. edge_a[sack_in].se.r;
  371. }
  372. expect = PKT;
  373. break;
  374. }
  375. break;
  376. case PKT:
  377. switch (edge_a[sack_in].st) {
  378. case NOPKT:
  379. debug_cond(DEBUG_INT_STATE, "p");
  380. if (sack_in > sack_idx &&
  381. hill < TCP_SACK_HILLS) {
  382. hill++;
  383. if (IS_ENABLED(CONFIG_PROT_TCP_SACK))
  384. tcp_lost.len += TCP_OPT_LEN_8;
  385. }
  386. expect = NOPKT;
  387. break;
  388. case PKT:
  389. debug_cond(DEBUG_INT_STATE, "P");
  390. if (tcp_ack_edge == edge_a[sack_in].se.l) {
  391. tcp_ack_edge = edge_a[sack_in].se.r;
  392. edge_a[sack_in].st = NOPKT;
  393. sack_idx++;
  394. } else {
  395. if (IS_ENABLED(CONFIG_PROT_TCP_SACK) &&
  396. hill < TCP_SACK_HILLS)
  397. tcp_lost.hill[hill].r =
  398. edge_a[sack_in].se.r;
  399. if (IS_ENABLED(CONFIG_PROT_TCP_SACK) &&
  400. sack_in == sack_end - 1)
  401. tcp_lost.hill[hill].r =
  402. edge_a[sack_in].se.r;
  403. }
  404. break;
  405. }
  406. break;
  407. }
  408. }
  409. debug_cond(DEBUG_INT_STATE, "\n");
  410. if (!IS_ENABLED(CONFIG_PROT_TCP_SACK) || tcp_lost.len <= TCP_OPT_LEN_2)
  411. sack_idx = 0;
  412. }
  413. /**
  414. * tcp_parse_options() - parsing TCP options
  415. * @o: pointer to the option field.
  416. * @o_len: length of the option field.
  417. */
  418. void tcp_parse_options(uchar *o, int o_len)
  419. {
  420. struct tcp_t_opt *tsopt;
  421. uchar *p = o;
  422. /*
  423. * NOPs are options with a zero length, and thus are special.
  424. * All other options have length fields.
  425. */
  426. for (p = o; p < (o + o_len); p = p + p[1]) {
  427. if (!p[1])
  428. return; /* Finished processing options */
  429. switch (p[0]) {
  430. case TCP_O_END:
  431. return;
  432. case TCP_O_MSS:
  433. case TCP_O_SCL:
  434. case TCP_P_SACK:
  435. case TCP_V_SACK:
  436. break;
  437. case TCP_O_TS:
  438. tsopt = (struct tcp_t_opt *)p;
  439. rmt_timestamp = tsopt->t_snd;
  440. return;
  441. }
  442. /* Process optional NOPs */
  443. if (p[0] == TCP_O_NOP)
  444. p++;
  445. }
  446. }
  447. static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len)
  448. {
  449. u8 tcp_fin = tcp_flags & TCP_FIN;
  450. u8 tcp_syn = tcp_flags & TCP_SYN;
  451. u8 tcp_rst = tcp_flags & TCP_RST;
  452. u8 tcp_push = tcp_flags & TCP_PUSH;
  453. u8 tcp_ack = tcp_flags & TCP_ACK;
  454. u8 action = TCP_DATA;
  455. int i;
  456. /*
  457. * tcp_flags are examined to determine TX action in a given state
  458. * tcp_push is interpreted to mean "inform the app"
  459. * urg, ece, cer and nonce flags are not supported.
  460. *
  461. * exe and crw are use to signal and confirm knowledge of congestion.
  462. * This TCP only sends a file request and acks. If it generates
  463. * congestion, the network is broken.
  464. */
  465. debug_cond(DEBUG_INT_STATE, "TCP STATE ENTRY %x\n", action);
  466. if (tcp_rst) {
  467. action = TCP_DATA;
  468. current_tcp_state = TCP_CLOSED;
  469. net_set_state(NETLOOP_FAIL);
  470. debug_cond(DEBUG_INT_STATE, "TCP Reset %x\n", tcp_flags);
  471. return TCP_RST;
  472. }
  473. switch (current_tcp_state) {
  474. case TCP_CLOSED:
  475. debug_cond(DEBUG_INT_STATE, "TCP CLOSED %x\n", tcp_flags);
  476. if (tcp_syn) {
  477. action = TCP_SYN | TCP_ACK;
  478. tcp_seq_init = tcp_seq_num;
  479. tcp_ack_edge = tcp_seq_num + 1;
  480. current_tcp_state = TCP_SYN_RECEIVED;
  481. } else if (tcp_ack || tcp_fin) {
  482. action = TCP_DATA;
  483. }
  484. break;
  485. case TCP_SYN_RECEIVED:
  486. case TCP_SYN_SENT:
  487. debug_cond(DEBUG_INT_STATE, "TCP_SYN_SENT | TCP_SYN_RECEIVED %x, %u\n",
  488. tcp_flags, tcp_seq_num);
  489. if (tcp_fin) {
  490. action = action | TCP_PUSH;
  491. current_tcp_state = TCP_CLOSE_WAIT;
  492. } else if (tcp_ack || (tcp_syn && tcp_ack)) {
  493. action |= TCP_ACK;
  494. tcp_seq_init = tcp_seq_num;
  495. tcp_ack_edge = tcp_seq_num + 1;
  496. sack_idx = 0;
  497. edge_a[sack_idx].se.l = tcp_ack_edge;
  498. edge_a[sack_idx].se.r = tcp_ack_edge;
  499. prev_len = 0;
  500. current_tcp_state = TCP_ESTABLISHED;
  501. for (i = 0; i < TCP_SACK; i++)
  502. edge_a[i].st = NOPKT;
  503. if (tcp_syn && tcp_ack)
  504. action |= TCP_PUSH;
  505. } else {
  506. action = TCP_DATA;
  507. }
  508. break;
  509. case TCP_ESTABLISHED:
  510. debug_cond(DEBUG_INT_STATE, "TCP_ESTABLISHED %x\n", tcp_flags);
  511. if (payload_len > 0) {
  512. tcp_hole(tcp_seq_num, payload_len);
  513. tcp_fin = TCP_DATA; /* cause standalone FIN */
  514. }
  515. if ((tcp_fin) &&
  516. (!IS_ENABLED(CONFIG_PROT_TCP_SACK) ||
  517. tcp_lost.len <= TCP_OPT_LEN_2)) {
  518. action = action | TCP_FIN | TCP_PUSH | TCP_ACK;
  519. current_tcp_state = TCP_CLOSE_WAIT;
  520. } else if (tcp_ack) {
  521. action = TCP_DATA;
  522. }
  523. if (tcp_syn)
  524. action = TCP_ACK + TCP_RST;
  525. else if (tcp_push)
  526. action = action | TCP_PUSH;
  527. break;
  528. case TCP_CLOSE_WAIT:
  529. debug_cond(DEBUG_INT_STATE, "TCP_CLOSE_WAIT (%x)\n", tcp_flags);
  530. action = TCP_DATA;
  531. break;
  532. case TCP_FIN_WAIT_2:
  533. debug_cond(DEBUG_INT_STATE, "TCP_FIN_WAIT_2 (%x)\n", tcp_flags);
  534. if (tcp_ack) {
  535. action = TCP_PUSH | TCP_ACK;
  536. current_tcp_state = TCP_CLOSED;
  537. puts("\n");
  538. } else if (tcp_syn) {
  539. action = TCP_DATA;
  540. } else if (tcp_fin) {
  541. action = TCP_DATA;
  542. }
  543. break;
  544. case TCP_FIN_WAIT_1:
  545. debug_cond(DEBUG_INT_STATE, "TCP_FIN_WAIT_1 (%x)\n", tcp_flags);
  546. if (tcp_fin) {
  547. tcp_ack_edge++;
  548. action = TCP_ACK | TCP_FIN;
  549. current_tcp_state = TCP_FIN_WAIT_2;
  550. }
  551. if (tcp_syn)
  552. action = TCP_RST;
  553. if (tcp_ack)
  554. current_tcp_state = TCP_CLOSED;
  555. break;
  556. case TCP_CLOSING:
  557. debug_cond(DEBUG_INT_STATE, "TCP_CLOSING (%x)\n", tcp_flags);
  558. if (tcp_ack) {
  559. action = TCP_PUSH;
  560. current_tcp_state = TCP_CLOSED;
  561. puts("\n");
  562. } else if (tcp_syn) {
  563. action = TCP_RST;
  564. } else if (tcp_fin) {
  565. action = TCP_DATA;
  566. }
  567. break;
  568. }
  569. return action;
  570. }
  571. /**
  572. * rxhand_tcp_f() - process receiving data and call data handler.
  573. * @b: the packet
  574. * @pkt_len: the length of packet.
  575. */
  576. void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len)
  577. {
  578. int tcp_len = pkt_len - IP_HDR_SIZE;
  579. u16 tcp_rx_xsum = b->ip.hdr.ip_sum;
  580. u8 tcp_action = TCP_DATA;
  581. u32 tcp_seq_num, tcp_ack_num;
  582. int tcp_hdr_len, payload_len;
  583. /* Verify IP header */
  584. debug_cond(DEBUG_DEV_PKT,
  585. "TCP RX in RX Sum (to=%pI4, from=%pI4, len=%d)\n",
  586. &b->ip.hdr.ip_src, &b->ip.hdr.ip_dst, pkt_len);
  587. b->ip.hdr.ip_src = net_server_ip;
  588. b->ip.hdr.ip_dst = net_ip;
  589. b->ip.hdr.ip_sum = 0;
  590. if (tcp_rx_xsum != compute_ip_checksum(b, IP_HDR_SIZE)) {
  591. debug_cond(DEBUG_DEV_PKT,
  592. "TCP RX IP xSum Error (%pI4, =%pI4, len=%d)\n",
  593. &net_ip, &net_server_ip, pkt_len);
  594. return;
  595. }
  596. /* Build pseudo header and verify TCP header */
  597. tcp_rx_xsum = b->ip.hdr.tcp_xsum;
  598. b->ip.hdr.tcp_xsum = 0;
  599. if (tcp_rx_xsum != tcp_set_pseudo_header((uchar *)b, b->ip.hdr.ip_src,
  600. b->ip.hdr.ip_dst, tcp_len,
  601. pkt_len)) {
  602. debug_cond(DEBUG_DEV_PKT,
  603. "TCP RX TCP xSum Error (%pI4, %pI4, len=%d)\n",
  604. &net_ip, &net_server_ip, tcp_len);
  605. return;
  606. }
  607. tcp_hdr_len = GET_TCP_HDR_LEN_IN_BYTES(b->ip.hdr.tcp_hlen);
  608. payload_len = tcp_len - tcp_hdr_len;
  609. if (tcp_hdr_len > TCP_HDR_SIZE)
  610. tcp_parse_options((uchar *)b + IP_TCP_HDR_SIZE,
  611. tcp_hdr_len - TCP_HDR_SIZE);
  612. /*
  613. * Incoming sequence and ack numbers are server's view of the numbers.
  614. * The app must swap the numbers when responding.
  615. */
  616. tcp_seq_num = ntohl(b->ip.hdr.tcp_seq);
  617. tcp_ack_num = ntohl(b->ip.hdr.tcp_ack);
  618. /* Packets are not ordered. Send to app as received. */
  619. tcp_action = tcp_state_machine(b->ip.hdr.tcp_flags,
  620. tcp_seq_num, payload_len);
  621. tcp_activity_count++;
  622. if (tcp_activity_count > TCP_ACTIVITY) {
  623. puts("| ");
  624. tcp_activity_count = 0;
  625. }
  626. if ((tcp_action & TCP_PUSH) || payload_len > 0) {
  627. debug_cond(DEBUG_DEV_PKT,
  628. "TCP Notify (action=%x, Seq=%u,Ack=%u,Pay%d)\n",
  629. tcp_action, tcp_seq_num, tcp_ack_num, payload_len);
  630. (*tcp_packet_handler) ((uchar *)b + pkt_len - payload_len, b->ip.hdr.tcp_dst,
  631. b->ip.hdr.ip_src, b->ip.hdr.tcp_src, tcp_seq_num,
  632. tcp_ack_num, tcp_action, payload_len);
  633. } else if (tcp_action != TCP_DATA) {
  634. debug_cond(DEBUG_DEV_PKT,
  635. "TCP Action (action=%x,Seq=%u,Ack=%u,Pay=%d)\n",
  636. tcp_action, tcp_ack_num, tcp_ack_edge, payload_len);
  637. /*
  638. * Warning: Incoming Ack & Seq sequence numbers are transposed
  639. * here to outgoing Seq & Ack sequence numbers
  640. */
  641. net_send_tcp_packet(0, ntohs(b->ip.hdr.tcp_src),
  642. ntohs(b->ip.hdr.tcp_dst),
  643. (tcp_action & (~TCP_PUSH)),
  644. tcp_ack_num, tcp_ack_edge);
  645. }
  646. }