1 /* SPDX-License-Identifier: GPL-2.0 */
3 * TCP Support with SACK for file transfer.
5 * Copyright 2017 Duncan Hare, All rights reserved.
8 #define TCP_ACTIVITY 127 /* Number of packets received */
9 /* before console progress mark */
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
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
30 * @tcp_ugr: Pointer to urgent data
41 struct in_addr ip_src;
42 struct in_addr ip_dst;
54 #define IP_TCP_HDR_SIZE (sizeof(struct ip_tcp_hdr))
55 #define TCP_HDR_SIZE (IP_TCP_HDR_SIZE - IP_HDR_SIZE)
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 */
68 * TCP header options, Seq, MSS, and SACK
71 #define TCP_SACK 32 /* Number of packets analyzed */
72 /* on leading edge of stream */
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 */
92 * struct tcp_mss - TCP option structure for MSS (Max segment size)
95 * @mss: Segment size value
104 * struct tcp_scale - TCP option structure for Windows scale
107 * @scale: windows shift value used for networks with many hops.
108 * Typically 4 or more hops
117 * struct tcp_sack_p - TCP option structure for SACK permitted
127 * struct sack_edges - structure for SACK edges
128 * @l: Left edge of stream
129 * @r: right edge of stream
136 #define TCP_SACK_SIZE (sizeof(struct sack_edges))
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.
145 #define TCP_SACK_HILLS 4
148 * struct tcp_sack_v - TCP option structure for SACK
151 * @hill: L & R window edges
156 struct sack_edges hill[TCP_SACK_HILLS];
160 * struct tcp_t_opt - TCP option structure for time stamps
163 * @t_snd: Sender timestamp
164 * @t_rcv: Receiver timestamp
173 #define TCP_TSOPT_SIZE (sizeof(struct tcp_t_opt))
176 * ip tcp structure with options
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
188 struct ip_tcp_hdr_o {
189 struct ip_tcp_hdr hdr;
191 struct tcp_scale scale;
192 struct tcp_sack_p sack_p;
193 struct tcp_t_opt t_opt;
197 #define IP_TCP_O_SIZE (sizeof(struct ip_tcp_hdr_o))
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
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;
213 #define IP_TCP_SACK_SIZE (sizeof(struct ip_tcp_hdr_s))
216 * TCP pseudo header definitions
218 #define PSEUDO_PAD_SIZE 8
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
227 * @len: length of header
230 u8 padding[PSEUDO_PAD_SIZE];
231 struct in_addr p_src;
232 struct in_addr p_dst;
238 #define PSEUDO_HDR_SIZE (sizeof(struct pseudo_hdr)) - PSEUDO_PAD_SIZE
241 * union tcp_build_pkt - union for building TCP/IP packet.
243 * @ip: IP and TCP header plus TCP options
244 * @sack: IP and TCP header plus SACK options
247 * Build Pseudo header in packed buffer
248 * first, calculate TCP checksum, then build IP header in packed buffer.
251 union tcp_build_pkt {
252 struct pseudo_hdr ph;
253 struct ip_tcp_hdr_o ip;
254 struct ip_tcp_hdr_s sack;
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_ESTABLISHED: both server & client have a connection
263 * @TCP_CLOSE_WAIT: Rec FIN, passed to app for FIN, ACK rsp
264 * @TCP_CLOSING: Rec FIN, sent FIN, ACK waiting for ACK
265 * @TCP_FIN_WAIT_1: Sent FIN waiting for response
266 * @TCP_FIN_WAIT_2: Rec ACK from FIN sent, waiting for FIN
278 enum tcp_state tcp_get_tcp_state(void);
279 void tcp_set_tcp_state(enum tcp_state new_state);
280 int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len,
281 u8 action, u32 tcp_seq_num, u32 tcp_ack_num);
284 * rxhand_tcp() - An incoming packet handler.
285 * @pkt: pointer to the application packet
286 * @dport: destination UDP port
287 * @sip: source IP address
288 * @sport: source UDP port
289 * @len: packet length
291 typedef void rxhand_tcp(uchar *pkt, unsigned int dport,
292 struct in_addr sip, unsigned int sport,
294 void tcp_set_tcp_handler(rxhand_tcp *f);
296 void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int len);
298 u16 tcp_set_pseudo_header(uchar *pkt, struct in_addr src, struct in_addr dest,
299 int tcp_len, int pkt_len);