1 
2 /***************************************************************************
3  * TCPHeader.h -- The TCPHeader Class represents a TCP packet. It contains *
4  * methods to set the different header fields. These methods tipically     *
5  * perform the necessary error checks and byte order conversions.          *
6  *                                                                         *
7  ***********************IMPORTANT NMAP LICENSE TERMS************************
8  *                                                                         *
9  * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap  *
10  * Project"). Nmap is also a registered trademark of the Nmap Project.     *
11  *                                                                         *
12  * This program is distributed under the terms of the Nmap Public Source   *
13  * License (NPSL). The exact license text applying to a particular Nmap    *
14  * release or source code control revision is contained in the LICENSE     *
15  * file distributed with that version of Nmap or source code control       *
16  * revision. More Nmap copyright/legal information is available from       *
17  * https://nmap.org/book/man-legal.html, and further information on the    *
18  * NPSL license itself can be found at https://nmap.org/npsl. This header  *
19  * summarizes some key points from the Nmap license, but is no substitute  *
20  * for the actual license text.                                            *
21  *                                                                         *
22  * Nmap is generally free for end users to download and use themselves,    *
23  * including commercial use. It is available from https://nmap.org.        *
24  *                                                                         *
25  * The Nmap license generally prohibits companies from using and           *
26  * redistributing Nmap in commercial products, but we sell a special Nmap  *
27  * OEM Edition with a more permissive license and special features for     *
28  * this purpose. See https://nmap.org/oem                                  *
29  *                                                                         *
30  * If you have received a written Nmap license agreement or contract       *
31  * stating terms other than these (such as an Nmap OEM license), you may   *
32  * choose to use and redistribute Nmap under those terms instead.          *
33  *                                                                         *
34  * The official Nmap Windows builds include the Npcap software             *
35  * (https://npcap.org) for packet capture and transmission. It is under    *
36  * separate license terms which forbid redistribution without special      *
37  * permission. So the official Nmap Windows builds may not be              *
38  * redistributed without special permission (such as an Nmap OEM           *
39  * license).                                                               *
40  *                                                                         *
41  * Source is provided to this software because we believe users have a     *
42  * right to know exactly what a program is going to do before they run it. *
43  * This also allows you to audit the software for security holes.          *
44  *                                                                         *
45  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
46  * and add new features.  You are highly encouraged to submit your         *
47  * changes as a Github PR or by email to the dev@nmap.org mailing list     *
48  * for possible incorporation into the main distribution. Unless you       *
49  * specify otherwise, it is understood that you are offering us very       *
50  * broad rights to use your submissions as described in the Nmap Public    *
51  * Source License Contributor Agreement. This is important because we      *
52  * fund the project by selling licenses with various terms, and also       *
53  * because the inability to relicense code has caused devastating          *
54  * problems for other Free Software projects (such as KDE and NASM).       *
55  *                                                                         *
56  * The free version of Nmap is distributed in the hope that it will be     *
57  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *
58  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,        *
59  * indemnification and commercial support are all available through the    *
60  * Npcap OEM program--see https://nmap.org/oem.                            *
61  *                                                                         *
62  ***************************************************************************/
63 /* This code was originally part of the Nping tool.                        */
64 
65 #ifndef __TCPHEADER_H__
66 #define __TCPHEADER_H__ 1
67 
68 #include "TransportLayerElement.h"
69 
70 /* TCP FLAGS */
71 #define TH_FIN   0x01
72 #define TH_SYN   0x02
73 #define TH_RST   0x04
74 #define TH_PSH   0x08
75 #define TH_ACK   0x10
76 #define TH_URG   0x20
77 #define TH_ECN   0x40
78 #define TH_CWR   0x80
79 
80 /* TCP OPTIONS */
81 #define TCPOPT_EOL         0   /* End of Option List (RFC793)                 */
82 #define TCPOPT_NOOP        1   /* No-Operation (RFC793)                       */
83 #define TCPOPT_MSS         2   /* Maximum Segment Size (RFC793)               */
84 #define TCPOPT_WSCALE      3   /* WSOPT - Window Scale (RFC1323)              */
85 #define TCPOPT_SACKOK      4   /* SACK Permitted (RFC2018)                    */
86 #define TCPOPT_SACK        5   /* SACK (RFC2018)                              */
87 #define TCPOPT_ECHOREQ     6   /* Echo (obsolete) (RFC1072)(RFC6247)          */
88 #define TCPOPT_ECHOREP     7   /* Echo Reply (obsolete) (RFC1072)(RFC6247)    */
89 #define TCPOPT_TSTAMP      8   /* TSOPT - Time Stamp Option (RFC1323)         */
90 #define TCPOPT_POCP        9   /* Partial Order Connection Permitted (obsol.) */
91 #define TCPOPT_POSP        10  /* Partial Order Service Profile (obsolete)    */
92 #define TCPOPT_CC          11  /* CC (obsolete) (RFC1644)(RFC6247)            */
93 #define TCPOPT_CCNEW       12  /* CC.NEW (obsolete) (RFC1644)(RFC6247)        */
94 #define TCPOPT_CCECHO      13  /* CC.ECHO (obsolete) (RFC1644)(RFC6247)       */
95 #define TCPOPT_ALTCSUMREQ  14  /* TCP Alternate Checksum Request (obsolete)   */
96 #define TCPOPT_ALTCSUMDATA 15  /* TCP Alternate Checksum Data (obsolete)      */
97 #define TCPOPT_MD5         19  /* MD5 Signature Option (obsolete) (RFC2385)   */
98 #define TCPOPT_SCPS        20  /* SCPS Capabilities                           */
99 #define TCPOPT_SNACK       21  /* Selective Negative Acknowledgements         */
100 #define TCPOPT_QSRES       27  /* Quick-Start Response (RFC4782)              */
101 #define TCPOPT_UTO         28  /* User Timeout Option (RFC5482)               */
102 #define TCPOPT_AO          29  /* TCP Authentication Option (RFC5925)         */
103 
104 /* Internal constants */
105 #define TCP_HEADER_LEN 20
106 #define MAX_TCP_OPTIONS_LEN 40
107 #define MAX_TCP_PAYLOAD_LEN 65495 /**< Max len of a TCP packet               */
108 
109 /* Default header values */
110 #define TCP_DEFAULT_SPORT 20
111 #define TCP_DEFAULT_DPORT 80
112 #define TCP_DEFAULT_SEQ   0
113 #define TCP_DEFAULT_ACK   0
114 #define TCP_DEFAULT_FLAGS 0x02
115 #define TCP_DEFAULT_WIN   8192
116 #define TCP_DEFAULT_URP   0
117 
118 
119 
120 /*
121 +--------+--------+---------+--------...
122 |  Type  |  Len   |       Value
123 +--------+--------+---------+--------...
124 */
125 struct nping_tcp_opt {
126     u8 type;                           /* Option type code.           */
127     u8 len;                            /* Option length.              */
128     u8 *value;                         /* Option value                */
129 }__attribute__((__packed__));
130 typedef struct nping_tcp_opt nping_tcp_opt_t;
131 
132 
133 class TCPHeader : public TransportLayerElement {
134 
135     private:
136         /*
137         0                   1                   2                   3
138         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
139         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140         |          Source Port          |       Destination Port        |
141         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142         |                        Sequence Number                        |
143         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144         |                    Acknowledgment Number                      |
145         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146         | Offset| Res.  |C|E|U|A|P|R|S|F|            Window             |
147         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148         |           Checksum            |         Urgent Pointer        |
149         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150         |                    Options                    |    Padding    |
151         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
152         */
153         struct nping_tcp_hdr {
154             u16 th_sport;                      /* Source port                 */
155             u16 th_dport;                      /* Destination port            */
156             u32 th_seq;                        /* Sequence number             */
157             u32 th_ack;                        /* Acknowledgement number      */
158             #if WORDS_BIGENDIAN
159                 u8 th_off:4;                   /* Data offset                 */
160                 u8 th_x2:4;                    /* Reserved                    */
161             #else
162                 u8 th_x2:4;                    /* Reserved                    */
163                 u8 th_off:4;                   /* Data offset                 */
164             #endif
165             u8 th_flags;                       /* Flags                       */
166             u16 th_win;                        /* Window size                 */
167             u16 th_sum;                        /* Checksum                    */
168             u16 th_urp;                        /* Urgent pointer              */
169 
170             u8 options[MAX_TCP_OPTIONS_LEN ];  /* Space for TCP Options       */
171         }__attribute__((__packed__));
172 
173         typedef struct nping_tcp_hdr nping_tcp_hdr_t;
174 
175         nping_tcp_hdr_t h;
176 
177         int tcpoptlen; /**< Length of TCP options */
178 
179         void __tcppacketoptinfo(const u8 *optp, int len, char *result, int bufsize) const;
180 
181     public:
182 
183         TCPHeader();
184         ~TCPHeader();
185         void reset();
186         u8 *getBufferPointer();
187         int storeRecvData(const u8 *buf, size_t len);
188         int protocol_id() const;
189         int validate();
190         int print(FILE *output, int detail) const;
191 
192         int setSourcePort(u16 p);
193         u16 getSourcePort() const;
194 
195         int setDestinationPort(u16 p);
196         u16 getDestinationPort() const;
197 
198         int setSeq(u32 p);
199         u32 getSeq() const;
200 
201         int setAck(u32 p);
202         u32 getAck() const;
203 
204         int setOffset(u8 o);
205         int setOffset();
206         u8 getOffset() const;
207 
208         int setReserved(u8 r);
209         u8 getReserved() const;
210 
211         int setFlags(u8 f);
212         u8 getFlags() const;
213         u16 getFlags16() const;
214         bool setCWR();
215         bool unsetCWR();
216         bool getCWR() const;
217         bool setECE();
218         bool unsetECE();
219         bool getECE() const;
220         bool setECN();
221         bool unsetECN();
222         bool getECN() const;
223         bool setURG();
224         bool unsetURG();
225         bool getURG() const;
226         bool setACK();
227         bool unsetACK();
228         bool getACK() const;
229         bool setPSH();
230         bool unsetPSH();
231         bool getPSH() const;
232         bool setRST();
233         bool unsetRST();
234         bool getRST() const;
235         bool setSYN();
236         bool unsetSYN();
237         bool getSYN() const;
238         bool setFIN();
239         bool unsetFIN();
240         bool getFIN() const;
241 
242         int setWindow(u16 p);
243         u16 getWindow() const;
244 
245         int setUrgPointer(u16 l);
246         u16 getUrgPointer() const;
247 
248         int setSum(u16 s);
249         int setSum(struct in_addr source, struct in_addr destination);
250         int setSum();
251         int setSumRandom();
252         int setSumRandom(struct in_addr source, struct in_addr destination);
253         u16 getSum() const;
254 
255         int setOptions(const u8 *optsbuff, size_t optslen);
256         const u8 *getOptions(size_t *optslen) const;
257         nping_tcp_opt_t getOption(unsigned int index) const;
258         static const char *optcode2str(u8 optcode);
259 
260 }; /* End of class TCPHeader */
261 
262 #endif /* __TCPHEADER_H__ */
263