1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * TCP Support with SACK for file transfer.
4  *
5  * Copyright 2017 Duncan Hare, All rights reserved.
6  */
7 
8 #define TCP_ACTIVITY 127		/* Number of packets received   */
9 					/* before console progress mark */
10 /**
11  * struct ip_tcp_hdr - IP and TCP header
12  * @ip_hl_v: header length and version
13  * @ip_tos: type of service
14  * @ip_len: total length
15  * @ip_id: identification
16  * @ip_off: fragment offset field
17  * @ip_ttl: time to live
18  * @ip_p: protocol
19  * @ip_sum: checksum
20  * @ip_src: Source IP address
21  * @ip_dst: Destination IP address
22  * @tcp_src: TCP source port
23  * @tcp_dst: TCP destination port
24  * @tcp_seq: TCP sequence number
25  * @tcp_ack: TCP Acknowledgment number
26  * @tcp_hlen: 4 bits TCP header Length/4, 4 bits reserved, 2 more bits reserved
27  * @tcp_flag: flags of TCP
28  * @tcp_win: TCP windows size
29  * @tcp_xsum: Checksum
30  * @tcp_ugr: Pointer to urgent data
31  */
32 struct ip_tcp_hdr {
33 	u8		ip_hl_v;
34 	u8		ip_tos;
35 	u16		ip_len;
36 	u16		ip_id;
37 	u16		ip_off;
38 	u8		ip_ttl;
39 	u8		ip_p;
40 	u16		ip_sum;
41 	struct in_addr	ip_src;
42 	struct in_addr	ip_dst;
43 	u16		tcp_src;
44 	u16		tcp_dst;
45 	u32		tcp_seq;
46 	u32		tcp_ack;
47 	u8		tcp_hlen;
48 	u8		tcp_flags;
49 	u16		tcp_win;
50 	u16		tcp_xsum;
51 	u16		tcp_ugr;
52 } __packed;
53 
54 #define IP_TCP_HDR_SIZE		(sizeof(struct ip_tcp_hdr))
55 #define TCP_HDR_SIZE		(IP_TCP_HDR_SIZE  - IP_HDR_SIZE)
56 
57 #define TCP_DATA	0x00	/* Data Packet - internal use only	*/
58 #define TCP_FIN		0x01	/* Finish flag				*/
59 #define TCP_SYN		0x02	/* Synch (start) flag			*/
60 #define TCP_RST		0x04	/* reset flag				*/
61 #define TCP_PUSH	0x08	/* Push - Notify app			*/
62 #define TCP_ACK		0x10	/* Acknowledgment of data received	*/
63 #define TCP_URG		0x20	/* Urgent				*/
64 #define TCP_ECE		0x40	/* Congestion control			*/
65 #define TCP_CWR		0x80	/* Congestion Control			*/
66 
67 /*
68  * TCP header options, Seq, MSS, and SACK
69  */
70 
71 #define TCP_SACK 32			/* Number of packets analyzed   */
72 					/* on leading edge of stream    */
73 
74 #define TCP_O_END	0x00		/* End of option list		*/
75 #define TCP_1_NOP	0x01		/* Single padding NOP		*/
76 #define TCP_O_NOP	0x01010101	/* NOPs pad to 32 bit boundary	*/
77 #define TCP_O_MSS	0x02		/* MSS Size option		*/
78 #define TCP_O_SCL	0x03		/* Window Scale option		*/
79 #define TCP_P_SACK	0x04		/* SACK permitted		*/
80 #define TCP_V_SACK	0x05		/* SACK values			*/
81 #define TCP_O_TS	0x08		/* Timestamp option		*/
82 #define TCP_OPT_LEN_2	0x02
83 #define TCP_OPT_LEN_3	0x03
84 #define TCP_OPT_LEN_4	0x04
85 #define TCP_OPT_LEN_6	0x06
86 #define TCP_OPT_LEN_8	0x08
87 #define TCP_OPT_LEN_A	0x0a		/* Timestamp Length		*/
88 #define TCP_MSS		1460		/* Max segment size		*/
89 #define TCP_SCALE	0x01		/* Scale			*/
90 
91 /**
92  * struct tcp_mss - TCP option structure for MSS (Max segment size)
93  * @kind: Field ID
94  * @len: Field length
95  * @mss: Segment size value
96  */
97 struct tcp_mss {
98 	u8	kind;
99 	u8	len;
100 	u16	mss;
101 } __packed;
102 
103 /**
104  * struct tcp_scale - TCP option structure for Windows scale
105  * @kind: Field ID
106  * @len: Field length
107  * @scale: windows shift value used for networks with many hops.
108  *         Typically 4 or more hops
109  */
110 struct tcp_scale {
111 	u8	kind;
112 	u8	len;
113 	u8	scale;
114 } __packed;
115 
116 /**
117  * struct tcp_sack_p - TCP option structure for SACK permitted
118  * @kind: Field ID
119  * @len: Field length
120  */
121 struct tcp_sack_p {
122 	u8	kind;
123 	u8	len;
124 } __packed;
125 
126 /**
127  * struct sack_edges - structure for SACK edges
128  * @l: Left edge of stream
129  * @r: right edge of stream
130  */
131 struct sack_edges {
132 	u32	l;
133 	u32	r;
134 } __packed;
135 
136 #define TCP_SACK_SIZE (sizeof(struct sack_edges))
137 
138 /*
139  * A TCP stream has holes when packets are missing or disordered.
140  * A hill is the inverse of a hole, and is data received.
141  * TCP received hills (a sequence of data), and inferrs Holes
142  * from the "hills" or packets received.
143  */
144 
145 #define TCP_SACK_HILLS	4
146 
147 /**
148  * struct tcp_sack_v - TCP option structure for SACK
149  * @kind: Field ID
150  * @len: Field length
151  * @hill: L & R window edges
152  */
153 struct tcp_sack_v {
154 	u8	kind;
155 	u8	len;
156 	struct	sack_edges hill[TCP_SACK_HILLS];
157 } __packed;
158 
159 /**
160  * struct tcp_t_opt - TCP option structure for time stamps
161  * @kind: Field ID
162  * @len: Field length
163  * @t_snd: Sender timestamp
164  * @t_rcv: Receiver timestamp
165  */
166 struct tcp_t_opt {
167 	u8	kind;
168 	u8	len;
169 	u32	t_snd;
170 	u32	t_rcv;
171 } __packed;
172 
173 #define TCP_TSOPT_SIZE (sizeof(struct tcp_t_opt))
174 
175 /*
176  * ip tcp  structure with options
177  */
178 
179 /**
180  * struct ip_tcp_hdr_o - IP + TCP header + TCP options
181  * @hdr: IP + TCP header
182  * @mss: TCP MSS Option
183  * @scale: TCP Windows Scale Option
184  * @sack_p: TCP Sack-Permitted Option
185  * @t_opt: TCP Timestamp Option
186  * @end: end of options
187  */
188 struct ip_tcp_hdr_o {
189 	struct	ip_tcp_hdr hdr;
190 	struct	tcp_mss	   mss;
191 	struct	tcp_scale  scale;
192 	struct	tcp_sack_p sack_p;
193 	struct	tcp_t_opt  t_opt;
194 	u8	end;
195 } __packed;
196 
197 #define IP_TCP_O_SIZE (sizeof(struct ip_tcp_hdr_o))
198 
199 /**
200  * struct ip_tcp_hdr_s - IP + TCP header + TCP options
201  * @hdr: IP + TCP header
202  * @t_opt: TCP Timestamp Option
203  * @sack_v: TCP SACK Option
204  * @end: end of options
205  */
206 struct ip_tcp_hdr_s {
207 	struct	ip_tcp_hdr	hdr;
208 	struct	tcp_t_opt	t_opt;
209 	struct	tcp_sack_v	sack_v;
210 	u8	end;
211 } __packed;
212 
213 #define IP_TCP_SACK_SIZE (sizeof(struct ip_tcp_hdr_s))
214 
215 /*
216  * TCP pseudo header definitions
217  */
218 #define PSEUDO_PAD_SIZE	8
219 
220 /**
221  * struct pseudo_hdr - Pseudo Header
222  * @padding: pseudo hdr size = ip_tcp hdr size
223  * @p_src: Source IP address
224  * @p_dst: Destination IP address
225  * @rsvd: reserved
226  * @p: protocol
227  * @len: length of header
228  */
229 struct pseudo_hdr {
230 	u8 padding[PSEUDO_PAD_SIZE];
231 	struct in_addr p_src;
232 	struct in_addr p_dst;
233 	u8      rsvd;
234 	u8      p;
235 	u16     len;
236 } __packed;
237 
238 #define PSEUDO_HDR_SIZE	((sizeof(struct pseudo_hdr)) - PSEUDO_PAD_SIZE)
239 
240 /**
241  * union tcp_build_pkt - union for building TCP/IP packet.
242  * @ph: pseudo header
243  * @ip: IP and TCP header plus TCP options
244  * @sack: IP and TCP header plus SACK options
245  * @raw: buffer
246  *
247  * Build Pseudo header in packed buffer
248  * first, calculate TCP checksum, then build IP header in packed buffer.
249  *
250  */
251 union tcp_build_pkt {
252 	struct pseudo_hdr ph;
253 	struct ip_tcp_hdr_o ip;
254 	struct ip_tcp_hdr_s sack;
255 	uchar  raw[1600];
256 } __packed;
257 
258 /**
259  * enum tcp_state - TCP State machine states for connection
260  * @TCP_CLOSED: Need to send SYN to connect
261  * @TCP_SYN_SENT: Trying to connect, waiting for SYN ACK
262  * @TCP_SYN_RECEIVED: Initial SYN received, waiting for ACK
263  * @TCP_ESTABLISHED: both server & client have a connection
264  * @TCP_CLOSE_WAIT: Rec FIN, passed to app for FIN, ACK rsp
265  * @TCP_CLOSING: Rec FIN, sent FIN, ACK waiting for ACK
266  * @TCP_FIN_WAIT_1: Sent FIN waiting for response
267  * @TCP_FIN_WAIT_2: Rec ACK from FIN sent, waiting for FIN
268  * @TCP_LAST_ACK: Waiting for ACK of the connection termination
269  */
270 enum tcp_state {
271 	TCP_CLOSED,
272 	TCP_SYN_SENT,
273 	TCP_SYN_RECEIVED,
274 	TCP_ESTABLISHED,
275 	TCP_CLOSE_WAIT,
276 	TCP_CLOSING,
277 	TCP_FIN_WAIT_1,
278 	TCP_FIN_WAIT_2,
279 	TCP_LAST_ACK,
280 };
281 
282 /**
283  * enum tcp_status - TCP stream status for connection
284  * @TCP_ERR_OK: no rx/tx errors
285  * @TCP_ERR_TOUT: rx/tx timeout happened
286  * @TCP_ERR_RST: connection was reset
287  * @TCP_ERR_IO: input/output error
288  */
289 enum tcp_status {
290 	TCP_ERR_OK = 0,
291 	TCP_ERR_TOUT,
292 	TCP_ERR_RST,
293 	TCP_ERR_IO
294 };
295 
296 /**
297  * struct tcp_stream - TCP data stream structure
298  * @rhost:		Remote host, network byte order
299  * @rport:		Remote port, host byte order
300  * @lport:		Local port, host byte order
301  *
302  * @priv:		User private data (not used by tcp module)
303  *
304  * @max_retry_count:	Maximum retransmit attempts (default 3)
305  * @initial_timeout:	Timeout from initial TX to reTX (default 2 sec)
306  * @rx_inactiv_timeout:	Maximum time from last rx till connection drop
307  *			  (default 30 sec)
308  *
309  * @on_closed:		User callback, called just before destroying TCP stream
310  * @on_established:	User callback, called when TCP stream enters
311  *			  TCP_ESTABLISHED state
312  * @on_rcv_nxt_update:	User callback, called when all data in the segment
313  *			  [0..rx_bytes - 1] was received
314  * @on_snd_una_update:	User callback, called when all data in the segment
315  *			  [0..tx_bytes - 1] were transferred and acknowledged
316  * @rx:			User callback, called on receive of segment
317  *			  [rx_offs..rx_offs+len-1]. If NULL -- all incoming data
318  *			  will be ignored. User SHOULD store the segment and
319  *			  return the number of accepted bytes or negative value
320  *			  on error.
321  *			  WARNING: Previous segmengs may not be received yet
322  * @tx:			User callback, called on transmit/retransmit of segment
323  *			  [tx_offs..tx_offs+maxlen-1]. If NULL -- no data will
324  *			  be transmitted. User SHOULD fill provided buffer and
325  *			  return the number of bytes in the buffer or negative
326  *			  value on error.
327  *			  WARNING: do not use tcp_stream_close() from this
328  *			    callback (it will break stream). Better use
329  *			    on_snd_una_update() callback for such purposes.
330  *
331  * @time_last_rx:	Arrival time of last valid incoming package (ticks)
332  * @time_start:		Timeout start time (ticks)
333  * @time_delta:		Timeout duration (ticks)
334  * @time_handler	Timeout handler for a stream
335  *
336  * @state:		TCP connection state
337  * @status:		TCP stream status (OK or ERR)
338  * @rx_packets:		total number of received packets
339  * @tx_packets:		total number of transmitted packets
340  *
341  * @fin_rx:		Non-zero if TCP_FIN was received
342  * @fin_rx_seq:		TCP sequence of rx FIN bit
343  * @fin_tx:		Non-zero if TCP_FIN was sent (or planned to send)
344  * @fin_tx_seq:		TCP sequence of tx FIN bit
345  *
346  * @iss:		Initial send sequence number
347  * @snd_una:		Send unacknowledged
348  * @snd_nxt:		Send next
349  * @snd_wnd:		Send window (in bytes)
350  * @snd_wl1:		Segment sequence number used for last window update
351  * @snd_wl2:		Segment acknowledgment number used for last window update
352  *
353  * @irs:		Initial receive sequence number
354  * @rcv_nxt:		Receive next
355  * @rcv_wnd:		Receive window (in bytes)
356  *
357  * @loc_timestamp:	Local timestamp
358  * @rmt_timestamp:	Remote timestamp
359  *
360  * @rmt_win_scale:	Remote window scale factor
361  *
362  * @lost:		Used for SACK
363  *
364  * @retry_cnt:		Number of retry attempts remaining. Only SYN, FIN
365  *			  or DATA segments are tried to retransmit.
366  * @retry_timeout:	Current retry timeout (ms)
367  * @retry_action:	TCP flags used for sending
368  * @retry_seq_num:	TCP sequence for retransmit
369  * retry_tx_len:	Number of data to transmit
370  * @retry_tx_offs:	Position in the TX stream
371  */
372 struct tcp_stream {
373 	struct in_addr	rhost;
374 	u16		rport;
375 	u16		lport;
376 
377 	void		*priv;
378 
379 	int		max_retry_count;
380 	int		initial_timeout;
381 	int		rx_inactiv_timeout;
382 
383 	void		(*on_closed)(struct tcp_stream *tcp);
384 	void		(*on_established)(struct tcp_stream *tcp);
385 	void		(*on_rcv_nxt_update)(struct tcp_stream *tcp, u32 rx_bytes);
386 	void		(*on_snd_una_update)(struct tcp_stream *tcp, u32 tx_bytes);
387 	int		(*rx)(struct tcp_stream *tcp, u32 rx_offs, void *buf, int len);
388 	int		(*tx)(struct tcp_stream *tcp, u32 tx_offs, void *buf, int maxlen);
389 
390 	ulong		time_last_rx;
391 	ulong		time_start;
392 	ulong		time_delta;
393 	void		(*time_handler)(struct tcp_stream *tcp);
394 
395 	enum tcp_state	state;
396 	enum tcp_status	status;
397 	u32		rx_packets;
398 	u32		tx_packets;
399 
400 	int		fin_rx;
401 	u32		fin_rx_seq;
402 
403 	int		fin_tx;
404 	u32		fin_tx_seq;
405 
406 	u32		iss;
407 	u32		snd_una;
408 	u32		snd_nxt;
409 	u32		snd_wnd;
410 	u32		snd_wl1;
411 	u32		snd_wl2;
412 
413 	u32		irs;
414 	u32		rcv_nxt;
415 	u32		rcv_wnd;
416 
417 	/* TCP option timestamp */
418 	u32		loc_timestamp;
419 	u32		rmt_timestamp;
420 
421 	/* TCP window scale */
422 	u8		rmt_win_scale;
423 
424 	/* TCP sliding window control used to request re-TX */
425 	struct tcp_sack_v lost;
426 
427 	/* used for data retransmission */
428 	int		retry_cnt;
429 	int		retry_timeout;
430 	u8		retry_action;
431 	u32		retry_seq_num;
432 	u32		retry_tx_len;
433 	u32		retry_tx_offs;
434 };
435 
436 void tcp_init(void);
437 
438 /*
439  * This function sets user callback called on TCP stream creation.
440  * Callback should:
441  *  + Check TCP stream endpoint and make connection verdict
442  *    - return non-zero value to accept connection
443  *    - return zero to drop connection
444  *  + Setup TCP stream callbacks like: on_closed(), on_established(),
445  *    n_rcv_nxt_update(), on_snd_una_update(), rx() and tx().
446  *  + Setup other stream related data
447  *
448  * WARNING: User MUST setup TCP stream on_create handler. Without it
449  *          no connection (including outgoung) will be created.
450  */
451 void tcp_stream_set_on_create_handler(int (*on_create)(struct tcp_stream *));
452 
453 /*
454  * tcp_stream_get -- Get or create TCP stream
455  * @is_new:	if non-zero and no stream found, then create a new one
456  * @rhost:	Remote host, network byte order
457  * @rport:	Remote port, host byte order
458  * @lport:	Local port, host byte order
459  *
460  * Returns: TCP stream structure or NULL (if not found/created)
461  */
462 struct tcp_stream *tcp_stream_get(int is_new, struct in_addr rhost,
463 				  u16 rport, u16 lport);
464 
465 /*
466  * tcp_stream_connect -- Create new TCP stream for remote connection.
467  * @rhost:	Remote host, network byte order
468  * @rport:	Remote port, host byte order
469  *
470  * Returns: TCP new stream structure or NULL (if not created).
471  *          Random local port will be used.
472  */
473 struct tcp_stream *tcp_stream_connect(struct in_addr rhost, u16 rport);
474 
475 /*
476  * tcp_stream_put -- Return stream to a TCP subsystem. Subsystem will
477  *                   check stream and destroy it (if stream was already
478  *                   closed). Otherwize no stream change will happen.
479  * @tcp:	TCP stream to put
480  */
481 void tcp_stream_put(struct tcp_stream *tcp);
482 
483 /*
484  * tcp_stream_restart_rx_timer -- Restart RX inactivity timer. Usually there
485  *                                is no needs to call this function. Timer
486  *                                will be restarted on receiving of any valid
487  *                                tcp packet belonging to a stream.
488  *
489  *                                This function may be used to prevent connection
490  *                                break in the following case:
491  *                                  - u-boot is busy with very long data processing
492  *                                  - remote side waits for u-boot reply
493  *
494  * @tcp:	TCP stream to put
495  */
496 void tcp_stream_restart_rx_timer(struct tcp_stream *tcp);
497 
498 enum tcp_state  tcp_stream_get_state(struct tcp_stream *tcp);
499 enum tcp_status tcp_stream_get_status(struct tcp_stream *tcp);
500 
501 /*
502  * tcp_stream_rx_offs(),
503  * tcp_stream_tx_offs()  -- Returns offset of first unacknowledged byte
504  *                          in receive/transmit stream correspondingly.
505  *                          The result is NOT affected by sin/fin flags.
506  * @tcp:	TCP stream
507  */
508 u32 tcp_stream_rx_offs(struct tcp_stream *tcp);
509 u32 tcp_stream_tx_offs(struct tcp_stream *tcp);
510 
511 /* reset tcp stream */
512 void tcp_stream_reset(struct tcp_stream *tcp);
513 /* force TCP stream closing, do NOT use from tcp->tx callback */
514 void tcp_stream_close(struct tcp_stream *tcp);
515 
516 void tcp_streams_poll(void);
517 
518 int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len,
519 		       u8 action, u32 tcp_seq_num, u32 tcp_ack_num);
520 
521 void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int len);
522 
523 u16 tcp_set_pseudo_header(uchar *pkt, struct in_addr src, struct in_addr dest,
524 			  int tcp_len, int pkt_len);
525