Wireshark 4.5.0
The Wireshark network protocol analyzer
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
packet-tcp.h
1/* packet-tcp.h
2 *
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10#ifndef __PACKET_TCP_H__
11#define __PACKET_TCP_H__
12
13#include "ws_symbol_export.h"
14
15#include <epan/conversation.h>
16#include <epan/reassemble.h>
17#include <epan/wmem_scopes.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif /* __cplusplus */
22
23/* TCP flags */
24#define TH_FIN 0x0001
25#define TH_SYN 0x0002
26#define TH_RST 0x0004
27#define TH_PUSH 0x0008
28#define TH_ACK 0x0010
29#define TH_URG 0x0020
30#define TH_ECE 0x0040
31#define TH_CWR 0x0080
32#define TH_AE 0x0100
33#define TH_RES 0x0E00 /* 3 reserved bits */
34#define TH_MASK 0x0FFF
35
36#define IS_TH_FIN(x) (x & TH_FIN)
37#define IS_TH_URG(x) (x & TH_URG)
38
39/* Idea for gt: either x > y, or y is much bigger (assume wrap) */
40#define GT_SEQ(x, y) ((int32_t)((y) - (x)) < 0)
41#define LT_SEQ(x, y) ((int32_t)((x) - (y)) < 0)
42#define GE_SEQ(x, y) ((int32_t)((y) - (x)) <= 0)
43#define LE_SEQ(x, y) ((int32_t)((x) - (y)) <= 0)
44#define EQ_SEQ(x, y) (x) == (y)
45
46/* Stop counting over 100 isolated parts, although it could technically reach 2^31 */
47#define MAX_CONTIGUOUS_SEQUENCES 100
48
49/* mh as in mptcp header */
51
52 bool mh_mpc; /* true if seen an mp_capable option */
53 bool mh_join; /* true if seen an mp_join option */
54 bool mh_dss; /* true if seen a dss */
55 bool mh_add; /* true if seen an MP_ADD */
56 bool mh_remove; /* true if seen an MP_REMOVE */
57 bool mh_prio; /* true if seen an MP_PRIO */
58 bool mh_fail; /* true if seen an MP_FAIL */
59 bool mh_fastclose; /* true if seen a fastclose */
60 bool mh_tcprst; /* true if seen a MP_TCPRST */
61
62 uint8_t mh_capable_flags; /* to get hmac version for instance */
63 uint8_t mh_dss_flags; /* data sequence signal flag */
64 uint32_t mh_dss_ssn; /* DSS Subflow Sequence Number */
65 uint64_t mh_dss_rawdsn; /* DSS Data Sequence Number */
66 uint64_t mh_dss_rawack; /* DSS raw data ack */
67 uint16_t mh_dss_length; /* mapping/DSS length */
68
69 uint64_t mh_key; /* Sender key in MP_CAPABLE */
70 uint32_t mh_token; /* seen in MP_JOIN. Should be a hash of the initial key */
71
72 uint32_t mh_stream; /* this stream index field is included to help differentiate when address/port pairs are reused */
73
74 /* Data Sequence Number of the current segment. It needs to be computed from previous mappings
75 * and as such is not necessarily set
76 */
77 uint64_t mh_rawdsn64;
78 /* DSN formatted according to the wireshark MPTCP options */
79 uint64_t mh_dsn;
80};
81
82/* the tcp header structure, passed to tap listeners */
83typedef struct tcpheader {
84 uint32_t th_rawseq; /* raw value */
85 uint32_t th_seq; /* raw or relative value depending on tcp_relative_seq */
86
87 uint32_t th_rawack; /* raw value */
88 uint32_t th_ack; /* raw or relative value depending on tcp_relative_seq */
89 bool th_have_seglen; /* true if th_seglen is valid */
90 uint32_t th_seglen; /* in bytes */
91 uint32_t th_win; /* make it 32 bits so we can handle some scaling */
92 uint16_t th_sport;
93 uint16_t th_dport;
94 uint8_t th_hlen;
95 bool th_use_ace;
96 uint16_t th_flags;
97 uint32_t th_stream; /* this stream index field is included to help differentiate when address/port pairs are reused */
98 address ip_src;
99 address ip_dst;
100 bool flagkarn; /* XXX - might later become a bit field */
101
102 /* This is the absolute maximum we could find in TCP options (RFC2018, section 3) */
103 #define MAX_TCP_SACK_RANGES 4
104 uint8_t num_sack_ranges;
105 uint32_t sack_left_edge[MAX_TCP_SACK_RANGES];
106 uint32_t sack_right_edge[MAX_TCP_SACK_RANGES];
107
108 /* header for TCP option Multipath Operation */
109 struct mptcpheader *th_mptcp;
110} tcp_info_t;
111
112/*
113 * Private data passed from the TCP dissector to subdissectors.
114 * NOTE: This structure is used by Export PDU functionality so
115 * make sure that handling is also updated if this structure
116 * changes!
117 */
118struct tcpinfo {
119 uint32_t seq; /* Sequence number of first byte in the data */
120 uint32_t nxtseq; /* Sequence number of first byte after data */
121 uint32_t lastackseq; /* Sequence number of last ack */
122 bool is_reassembled; /* This is reassembled data. */
123 uint16_t flags; /* TCP flags */
124 uint16_t urgent_pointer; /* Urgent pointer value for the current packet. */
125 uint32_t stream; /* Stream id passed to export PDU */
126};
127
128/*
129 * Loop for dissecting PDUs within a TCP stream; assumes that a PDU
130 * consists of a fixed-length chunk of data that contains enough information
131 * to determine the length of the PDU, followed by rest of the PDU.
132 *
133 * The first three arguments are the arguments passed to the dissector
134 * that calls this routine.
135 *
136 * "proto_desegment" is the dissector's flag controlling whether it should
137 * desegment PDUs that cross TCP segment boundaries.
138 *
139 * "fixed_len" is the length of the fixed-length part of the PDU.
140 *
141 * "get_pdu_len()" is a routine called to get the length of the PDU from
142 * the fixed-length part of the PDU; it's passed "pinfo", "tvb", "offset" and
143 * "dissector_data".
144 *
145 * "dissect_pdu()" is the routine to dissect a PDU.
146 */
147WS_DLL_PUBLIC void
148tcp_dissect_pdus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
149 bool proto_desegment, unsigned fixed_len,
150 unsigned (*get_pdu_len)(packet_info *, tvbuff_t *, int, void*),
151 dissector_t dissect_pdu, void* dissector_data);
152
154tcp_reassembly_table_functions;
155
156extern struct tcp_multisegment_pdu *
157pdu_store_sequencenumber_of_next_pdu(packet_info *pinfo, uint32_t seq, uint32_t nxtpdu, wmem_tree_t *multisegment_pdus);
158
159typedef struct _tcp_unacked_t {
160 struct _tcp_unacked_t *next;
161 uint32_t frame;
162 uint32_t seq;
163 uint32_t nextseq;
164 nstime_t ts;
165 bool karn_flag; /* indication for the later Karn discovery */
167
168struct tcp_acked {
169 uint32_t frame_acked;
170 nstime_t ts;
171
172 uint32_t rto_frame;
173 nstime_t rto_ts; /* Time since previous packet for
174 retransmissions. */
175 uint16_t flags; /* see TCP_A_* in packet-tcp.c */
176 uint32_t dupack_num; /* dup ack number */
177 uint32_t dupack_frame; /* dup ack to frame # */
178 uint32_t bytes_in_flight; /* number of bytes in flight */
179 uint32_t push_bytes_sent; /* bytes since the last PSH flag */
180
181 uint32_t new_data_seq; /* For segments with old data,
182 where new data starts */
183 bool partial_ack; /* true when acknowledging data
184 and not a full segment */
185 bool iskarn; /* true when this ACK is ambiguous according to Karn */
186};
187
188/* One instance of this structure is created for each pdu that spans across
189 * multiple tcp segments.
190 */
192 uint32_t seq;
193 uint32_t nxtpdu;
194 uint32_t first_frame; /* The frame where this MSP was created (used as key in reassembly tables). */
195 uint32_t last_frame;
196 nstime_t last_frame_time;
197 uint32_t first_frame_with_seq; /* The frame that contains the first frame that matches 'seq'
198 (same as 'first_frame', larger than 'first_frame' for OoO segments) */
199 uint32_t flags;
200#define MSP_FLAGS_REASSEMBLE_ENTIRE_SEGMENT 0x00000001
201/* Whether this MSP is finished and no more segments can be added. */
202#define MSP_FLAGS_GOT_ALL_SEGMENTS 0x00000002
203/* Whether the first segment of this MSP was not yet seen. */
204#define MSP_FLAGS_MISSING_FIRST_SEGMENT 0x00000004
205};
206
207
208/* Represents the MPTCP DSS option mapping part
209 It allows to map relative subflow sequence number (ssn) to global MPTCP sequence numbers
210 under their 64 bits form
211*/
212typedef struct _mptcp_dss_mapping_t {
213
214/* In DSS, SSN are enumerated with relative seq_nb, i.e. starting from 0 */
215
216 uint32_t ssn_low;
217 uint32_t ssn_high;
218
219/* Ideally the dsn should always be registered with the extended version
220 * but it may not be possible if we don't know the 32 MSB of the base_dsn
221 */
222 bool extended_dsn; /* true if MPTCP_DSS_FLAG_DATA_8BYTES */
223
224 uint64_t rawdsn; /* matches the low member of range
225 should be converted to the 64 bits version before being registered
226 */
227/* to check if mapping was sent before or after packet */
228uint32_t frame;
230
231
232/* Structure used in mptcp meta member 'dsn_map'
233 */
235 uint32_t frame; /* packet to look into PINFO_FD_NUM */
236 struct tcp_analysis* subflow; /* in order to get statistics */
238
239
240/* Should basically look like a_tcp_flow_t but for mptcp with 64bit sequence number.
241The meta is specific to a direction of the communication and aggregates information of
242all the subflows
243*/
244typedef struct _mptcp_meta_flow_t {
245
246 uint8_t static_flags; /* remember which fields are set */
247
248 /* flags exchanged between hosts during 3WHS. Gives checksum/extensibility/hmac information */
249 uint8_t flags;
250 uint64_t base_dsn; /* first data seq number (used by relative sequence numbers) seen. */
251 uint64_t nextseq; /* highest seen nextseq */
252 uint64_t dfin; /* data fin */
253
254 uint8_t version; /* negotiated mptcp version */
255
256 uint64_t key; /* if it was set */
257
258 /* expected token sha1 digest of keys, truncated to 32 most significant bits
259 derived from key. Stored to speed up subflow/MPTCP connection mapping */
260 uint32_t token;
261
262 uint32_t nextseqframe; /* frame number for segment with highest sequence number */
263
264 /* highest seen continuous seq number (without hole in the stream) */
265 uint64_t maxseqtobeacked;
266
267 uint64_t fin; /* frame number of the final dataFIN */
268
269 /* first addresses registered */
270 address ip_src;
271 address ip_dst;
272 uint32_t sport;
273 uint32_t dport;
275
276/* MPTCP data specific to this subflow direction */
278 uint8_t static_flags; /* flags stating which of the flow */
279 uint32_t nonce; /* used only for MP_JOIN */
280 uint8_t address_id; /* sent during an MP_JOIN */
281
282
283 /* map DSN to packets
284 * Used when looking for reinjections across subflows
285 */
286 wmem_itree_t *dsn2packet_map;
287
288 /* Map SSN to a DSS mappings
289 * a DSS can map DSN to SSNs possibily over several packets,
290 * hence some packets may have been mapped by previous DSS,
291 * whence the necessity to be able to look for SSN -> DSN */
292 wmem_itree_t *ssn2dsn_mappings;
293 /* meta flow to which it is attached. Helps setting forward and backward meta flow */
294 mptcp_meta_flow_t *meta;
295};
296
297
298typedef enum {
299 MPTCP_HMAC_NOT_SET = 0,
300 /* this is either SHA1 for MPTCP v0 or sha256 for MPTCP v1 */
301 MPTCP_HMAC_SHA = 1,
302 MPTCP_HMAC_LAST
303} mptcp_hmac_algorithm_t;
304
305
306#define MPTCP_CAPABLE_CRYPTO_MASK 0x3F
307
308#define MPTCP_CHECKSUM_MASK 0x80
309
310/* Information in a flow that is only used when tcp_analyze_seq preference
311 * is enabled, so save the memory when it isn't
312 */
314 tcp_unacked_t *segments;/* List of segments for which we haven't seen an ACK */
315 uint16_t segment_count; /* How many unacked segments we're currently storing */
316 uint32_t lastack; /* Last seen ack for the reverse flow */
317 nstime_t lastacktime; /* Time of the last ack packet */
318 uint32_t lastnondupack; /* frame number of last seen non dupack */
319 uint32_t dupacknum; /* dupack number */
320 uint32_t nextseq; /* highest seen nextseq */
321 uint32_t maxseqtobeacked;/* highest seen continuous seq number (without hole in the stream) from the fwd party,
322 * this is the maximum seq number that can be acked by the rev party in normal case.
323 * If the rev party sends an ACK beyond this seq number it indicates TCP_A_ACK_LOST_PACKET condition */
324 uint32_t nextseqframe; /* frame number for segment with highest
325 * sequence number
326 */
327 nstime_t nextseqtime; /* Time of the nextseq packet so we can
328 * distinguish between retransmission,
329 * fast retransmissions and outoforder
330 */
331
332 uint8_t lastacklen; /* length of the last fwd ACK packet - 0 means pure ACK */
333
334 bool valid_bif; /* if lost pkts, disable BiF until ACK is recvd */
335 bool push_set_last; /* tracking last time PSH flag was set */
336 uint32_t push_bytes_sent; /* bytes since the last PSH flag */
337
338 /*
339 * Handling of contiguous SEQ ranges
340 */
341 bool is_client; /* tracking who initiated the conversation */
342 uint8_t num_contiguous_ranges;
343 uint32_t contiguous_ranges[MAX_CONTIGUOUS_SEQUENCES][2];
344
345 /*
346 * Handling of SACK blocks
347 * Copied from tcpheader
348 */
349 uint8_t num_sack_ranges;
350 uint32_t sack_left_edge[MAX_TCP_SACK_RANGES];
351 uint32_t sack_right_edge[MAX_TCP_SACK_RANGES];
352
354
355 /* Process info, currently discovered via IPFIX */
356typedef struct tcp_process_info_t {
357 uint32_t process_uid; /* UID of local process */
358 uint32_t process_pid; /* PID of local process */
359 char *username; /* Username of the local process */
360 char *command; /* Local process name + path + args */
361
363
364typedef struct _tcp_flow_t {
365 uint8_t static_flags; /* true if base seq set */
366 uint32_t base_seq; /* base seq number (used by relative sequence numbers)*/
367#define TCP_MAX_UNACKED_SEGMENTS 10000 /* The most unacked segments we'll store */
368 uint32_t fin; /* frame number of the final FIN */
369 uint32_t window; /* last seen window */
370 int16_t win_scale; /* -1 is we don't know, -2 is window scaling is not used */
371 int16_t mss; /* maximum segment size, -1 unknown */
372 bool scps_capable; /* flow advertised scps capabilities */
373 uint16_t maxsizeacked; /* 0 if not yet known */
374 uint8_t mp_operations; /* tracking of the MPTCP operations */
375 bool is_first_ack; /* indicates if this is the first ACK */
376 bool closing_initiator; /* tracking who is responsible of the connection end */
377
378 tcp_analyze_seq_flow_info_t* tcp_analyze_seq_info;
379
380/* This tcp flow/session contains only one single PDU and should
381 * be reassembled until the final FIN segment.
382 */
383#define TCP_FLOW_REASSEMBLE_UNTIL_FIN 0x0001
384 uint16_t flags;
385
386 /* see TCP_A_* in packet-tcp.c */
387 uint32_t lastsegmentflags;
388
389 /* The next (largest) sequence number after all segments seen so far.
390 * Valid only on the first pass and used to handle out-of-order segments
391 * during reassembly. */
392 uint32_t maxnextseq;
393
394 /* The number of data flows seen in that direction */
395 uint16_t flow_count;
396
397 /* This tree is indexed by sequence number and keeps track of all
398 * all pdus spanning multiple segments for this flow.
399 */
400 wmem_tree_t *multisegment_pdus;
401
402 /* A sorted list of pending out-of-order segments. */
403 wmem_list_t *ooo_segments;
404
405 /* Process info, currently discovered via IPFIX */
406 tcp_process_info_t* process_info;
407
408 /* MPTCP subflow intel */
410} tcp_flow_t;
411
412/* Stores common information between both hosts of the MPTCP connection*/
414
415 uint16_t mp_flags; /* MPTCP meta analysis related, see MPTCP_META_* in packet-tcp.c */
416
417 /*
418 * For other subflows, they link the meta via mptcp_subflow_t::meta_flow
419 * according to the validity of the token.
420 */
421 mptcp_meta_flow_t meta_flow[2];
422
423 uint32_t stream; /* Keep track of unique mptcp stream (per MP_CAPABLE handshake) */
424 uint8_t hmac_algo; /* hmac decided after negotiation */
425 wmem_list_t* subflows; /* List of subflows (tcp_analysis) */
426
427 /* identifier of the tcp stream that saw the initial 3WHS with MP_CAPABLE option */
428 struct tcp_analysis *master;
429
430 /* Keep track of the last TCP operations seen in order to avoid false DUP ACKs */
431 uint8_t mp_operations;
432};
433
435 /* These two structs are managed based on comparing the source
436 * and destination addresses and, if they're equal, comparing
437 * the source and destination ports.
438 *
439 * If the source is greater than the destination, then stuff
440 * sent from src is in flow1.
441 *
442 * If the source is less than the destination, then stuff
443 * sent from src is in flow2.
444 *
445 * XXX - if the addresses and ports are equal, we don't guarantee
446 * the behavior.
447 */
448 tcp_flow_t flow1;
449 tcp_flow_t flow2;
450
451 /* These pointers are set by get_tcp_conversation_data()
452 * fwd point in the same direction as the current packet
453 * and rev in the reverse direction
454 */
455 tcp_flow_t *fwd;
456 tcp_flow_t *rev;
457
458 /* This pointer is NULL or points to a tcp_acked struct if this
459 * packet has "interesting" properties such as being a KeepAlive or
460 * similar
461 */
462 struct tcp_acked *ta;
463
464 /* This structure contains a tree containing all the various ta's
465 * keyed by frame number.
466 */
467 wmem_tree_t *acked_table;
468
469 /* Remember the timestamp of the first frame seen in this tcp
470 * conversation to be able to calculate a relative time compared
471 * to the start of this conversation
472 */
473 nstime_t ts_first;
474
475 /* Remember the timestamp of the most recent SYN in this conversation in
476 * order to calculate the first_rtt below. Not necessarily ts_first, if
477 * the SYN is retransmitted. */
478 nstime_t ts_mru_syn;
479
480 /* If we have the handshake, remember the RTT between the initial SYN
481 * and ACK for use detecting out-of-order segments. */
482 nstime_t ts_first_rtt;
483
484 /* Remember the timestamp of the frame that was last seen in this
485 * tcp conversation to be able to calculate a delta time compared
486 * to previous frame in this conversation
487 */
488 nstime_t ts_prev;
489
490 /* Keep track of tcp stream numbers instead of using the conversation
491 * index (as how it was done before). This prevents gaps in the
492 * stream index numbering
493 */
494 uint32_t stream;
495
496 /* Keep track of packet number within the TCP stream */
497 uint32_t pnum;
498
499 /* Remembers the server port on the SYN (or SYN|ACK) packet to
500 * help determine which dissector to call
501 */
502 uint16_t server_port;
503
504 /* Set when the client sends a SYN with data and the cookie in the Fast Open
505 * option.
506 */
507 bool tfo_syn_data;
508
509 /* Remembers which side is currently sending data. */
510 int8_t flow_direction : 2;
511
512 /* allocated only when mptcp enabled
513 * several tcp_analysis may refer to the same mptcp_analysis
514 * can exist without any meta
515 */
517
518 /* Track the TCP conversation completeness, as the capture might
519 * contain all parts of a TCP flow (establishment, data, clearing) or
520 * just some parts if we jumped on the bandwagon of an already established
521 * connection or left before it was terminated explicitly
522 */
523 uint8_t conversation_completeness;
524
525 /* Stores the value as a String to be displayed in the appropriate field */
526 char *conversation_completeness_str;
527
528 /* Track AccECN support */
529 bool had_acc_ecn_setup_syn;
530 bool had_acc_ecn_setup_syn_ack;
531 bool had_acc_ecn_option;
532};
533
534/* Structure that keeps per packet data. First used to be able
535 * to calculate the time_delta from the last seen frame in this
536 * TCP conversation. Can be extended for future use.
537 */
539 nstime_t ts_del;
540 uint32_t pnum;
541 uint8_t tcp_snd_manual_analysis;
542 bool karn_flag; /* XXX - might later become a bit field */
543};
544
545/* Structure that keeps per packet data. Some operations are cpu-intensive and are
546 * best cached into this structure
547 */
549
550 /* Mapping that covers this packet content */
551 mptcp_dss_mapping_t *mapping;
552
554
555
556WS_DLL_PUBLIC void dissect_tcp_payload(tvbuff_t *tvb, packet_info *pinfo, int offset,
557 uint32_t seq, uint32_t nxtseq, uint32_t sport,
558 uint32_t dport, proto_tree *tree,
559 proto_tree *tcp_tree,
560 struct tcp_analysis *tcpd, struct tcpinfo *tcpinfo);
561
562WS_DLL_PUBLIC struct tcp_analysis *get_tcp_conversation_data(conversation_t *conv,
563 packet_info *pinfo);
564
569WS_DLL_PUBLIC struct tcp_analysis *get_tcp_conversation_data_idempotent(conversation_t *conv);
570
571WS_DLL_PUBLIC bool decode_tcp_ports(tvbuff_t *, int, packet_info *, proto_tree *, int, int, struct tcp_analysis *, struct tcpinfo *);
572
585extern void add_tcp_process_info(uint32_t frame_num, address *local_addr, address *remote_addr, uint16_t local_port, uint16_t remote_port, uint32_t uid, uint32_t pid, char *username, char *command);
586
591WS_DLL_PUBLIC uint32_t get_tcp_stream_count(void);
592
597WS_DLL_PUBLIC uint32_t get_mptcp_stream_count(void);
598
599/* Follow Stream functionality shared with HTTP (and SSL?) */
600extern char *tcp_follow_conv_filter(epan_dissect_t *edt, packet_info *pinfo, unsigned *stream, unsigned *sub_stream);
601extern char *tcp_follow_index_filter(unsigned stream, unsigned sub_stream);
602extern char *tcp_follow_address_filter(address *src_addr, address *dst_addr, int src_port, int dst_port);
603
604#ifdef __cplusplus
605}
606#endif /* __cplusplus */
607
608#endif
Definition address.h:56
Definition packet-tcp.h:234
Definition packet-tcp.h:212
Definition packet-tcp.h:244
Definition packet_info.h:43
Definition proto.h:906
Definition packet-tcp.h:364
Definition packet-tcp.h:159
Definition wmem_list.c:23
Definition wmem_tree-int.h:48
Definition conversation.h:228
Definition epan_dissect.h:28
Definition packet-tcp.h:413
Definition packet-tcp.h:548
Definition packet-tcp.h:277
Definition packet-tcp.h:50
Definition nstime.h:26
Definition reassemble.h:149
Definition stream.c:41
Definition packet-tcp.h:168
Definition packet-tcp.h:434
Definition packet-tcp.h:313
Definition packet-tcp.h:191
Definition packet-tcp.h:538
Definition packet-tcp.h:356
Definition packet-tcp.h:83
Definition packet-tcp.h:118
Definition tvbuff-int.h:35