FreeRDP
Loading...
Searching...
No Matches
rdpudp.c
1
17
18#include <freerdp/config.h>
19
20#include <string.h>
21
22#include <winpr/assert.h>
23#include <winpr/cast.h>
24#include <winpr/crt.h>
25#include <winpr/crypto.h>
26#include <winpr/sysinfo.h>
27#include <winpr/collections.h>
28#include <winpr/stream.h>
29#include <winpr/winsock.h>
30
31#if !defined(_WIN32)
32#include <sys/socket.h>
33#include <netinet/in.h>
34#endif
35
36#include "rdpudp.h"
37
38/* [MS-RDPEUDP] 2.2.2.1 RDPUDP_FEC_HEADER uFlags */
39typedef enum WINPR_C23_ENUM_TYPE(uint16_t)
40{
41 RDPUDP_FLAG_SYN = 0x0001,
42 RDPUDP_FLAG_ACK = 0x0004,
43 RDPUDP_FLAG_DATA = 0x0008,
44 RDPUDP_FLAG_FEC = 0x0010,
45 RDPUDP_FLAG_CN = 0x0020,
46 RDPUDP_FLAG_CWR = 0x0040,
47 RDPUDP_FLAG_ACK_OF_ACKS = 0x0100,
48 RDPUDP_FLAG_CORRELATION_ID = 0x0800,
49 RDPUDP_FLAG_SYNEX = 0x1000
50} RDPUDP_FLAGS;
51
52/* [MS-RDPEUDP] 2.2.2.9 RDPUDP_SYNDATAEX_PAYLOAD uSynExFlags */
53typedef enum WINPR_C23_ENUM_TYPE(uint16_t)
54{
55 RDPUDP_VERSION_INFO_VALID = 0x0001
56} RDPUDP_SYNEX_FLAGS;
57
58/* [MS-RDPEUDP] 2.2.1.1 VECTOR_ELEMENT_STATE */
59typedef enum WINPR_C23_ENUM_TYPE(uint8_t)
60{
61 DATAGRAM_RECEIVED = 0,
62 DATAGRAM_NOT_YET_RECEIVED = 3
63} RDPUDP_VECTOR_ELEMENT_STATE;
64
65/* [MS-RDPEUDP2] 2.2.1.1 RDPUDP2_PACKET_HEADER Flags */
66typedef enum WINPR_C23_ENUM_TYPE(uint16_t)
67{
68 RDPUDP2_ACK = 0x001,
69 RDPUDP2_DATA = 0x004,
70 RDPUDP2_ACKVEC = 0x008,
71 RDPUDP2_AOA = 0x010,
72 RDPUDP2_OVERHEADSIZE = 0x040,
73 RDPUDP2_DELAYACKINFO = 0x100,
74 RDPUDP2_NOACK = 0x200
75} RDPUDP2_FLAGS;
76
77/* [MS-RDPEUDP2] 2.2.1 packet type index of the prefix byte */
78typedef enum WINPR_C23_ENUM_TYPE(uint8_t)
79{
80 RDPUDP2_PACKET_STANDARD = 0,
81 RDPUDP2_PACKET_DUMMY = 8
82} RDPUDP2_PACKET_TYPE;
83
84/* [MS-RDPEUDP] 2.2.2.1: the ACK vector holds at most 2048 elements */
85#define RDPUDP_MAX_ACK_VECTOR 2048
86
87#define RDPUDP_MTU 1232
88#define RDPUDP_MIN_MTU 1132
89
90/* What this side advertises in uReceiveWindowSize (versions 1 and 2). */
91#define RDPUDP_RECEIVE_WINDOW 64
92
93/* Out of order data held while waiting for a gap to fill, a slot per sequence number. RDP-UDP2
94 * numbers are 16 bit, serial arithmetic tells at most 32767 ahead from behind, and the peer may
95 * get that far: Windows ignores the uReceiveWindowSize of versions 1 and 2 there, and had 5751
96 * chunks outstanding after a 2 second outage. */
97#define RDPUDP_RECEIVE_SLOTS 32768
98#define RDPUDP_RECEIVE_REACH 32767
99
100/* Number of datagrams rdpudp_check() reads from its socket before running timers. The server
101 * tunnel uses the same budget for its flow (RDPEMT_INPUT_BUDGET). */
102#define RDPUDP_INPUT_BUDGET 256
103
104/* Max chunk size a server accepts, capping client slot usage at 64 MB. MTU is not used
105 * as the limit because ACKs are appended to full chunks, and Windows/mstsc datagrams
106 * can exceed MTU by 28 bytes. */
107#define RDPUDP_MAX_CHUNK 2048
108
109/* Server: number of chunks buffered past a gap until rdpudp_set_peer_verified(). This
110 * bounds the number of chunks a sender who knows the cookie hash but is not yet
111 * proven can make the server buffer. Since the client only sends the TLS handshake
112 * and Tunnel Create Request during this phase, a small limit suffices; out-of-reach
113 * chunks are not acked and will be retransmitted. */
114#define RDPUDP_UNVERIFIED_REACH 128
115
116/* Max datagrams in flight: caps the server's congestion window and defines the fixed
117 * client limit. 4096 datagrams are ~5 MB per round trip, ~50 MB/s at 100 ms. */
118#define RDPUDP_MAX_IN_FLIGHT 4096
119#define RDPUDP_CLIENT_MAX_IN_FLIGHT 64
120
121/* Datagrams in flight are found by sequence number through an index with one entry per
122 * 16-bit sequence number. RDP-UDP2 numbers are 16 bit, so a resend under a new number
123 * cannot land on an entry still in use. */
124#define RDPUDP_PENDING_INDEX 65536
125
126/* Server only: RDP-UDP2 congestion control ([MS-RDPEUDP] 3.1.1.8), RTT-based retransmit
127 * timeout, and fast retransmit on reported loss.
128 *
129 * The window (in datagrams) grows by one per ack during slow start, then by one per
130 * RDPUDP2_GROWTH_ACKS acks (~12% per RTT) if the queue is not building. It shrinks to
131 * 70% once per RTT if congested: either the round's loss is at least
132 * RDPUDP2_CONGESTED_LOSS_PERCENT and at least RDPUDP2_LOSS_RISE_PERCENT points above the
133 * smoothed loss of earlier rounds, or smoothed RTT exceeds the lowest sample by more than
134 * the maximum of RDPUDP2_QUEUE_DELAY_MS, half the minimum RTT, or 4 * rttvar. The lowest
135 * sample only rises when the round trip stays unchanged over RDPUDP2_DELAY_REBASE_CUTS such
136 * cuts; this identifies the path's new base delay rather than a queue.
137 *
138 * Random loss is repaired without slowing down. A Windows 11 server maintained ~4 MB/s
139 * under 2% loss and 100 ms delay, whereas halving the window on every loss dropped
140 * throughput to 0.12 MB/s. Comparing each round with the smoothed loss keeps a steady
141 * background loss (for example 10% on a bad radio link) from shrinking the window by
142 * chance in every other round, while loss that grows with the sending rate, as at a
143 * policer or a full queue, still does.
144 *
145 * Lost datagrams are retransmitted when an ACK vector reports them missing (provided
146 * enough subsequent datagrams were received) or when the retransmit timer expires.
147 * The reorder threshold starts at RDPUDP2_REORDER_THRESHOLD and doubles up to
148 * RDPUDP2_MAX_REORDER_THRESHOLD if the original datagram arrives late ([MS-RDPEUDP2]
149 * 3.1.1.2.3). It decays by one every RDPUDP2_REORDER_DECAY_ROUNDS rounds without such
150 * events. Spurious losses are excluded from congestion calculations. */
151#define RDPUDP2_INITIAL_CWND 32
152#define RDPUDP2_MIN_CWND 8
153#define RDPUDP2_REORDER_THRESHOLD 3
154#define RDPUDP2_MAX_REORDER_THRESHOLD 128
155#define RDPUDP2_REORDER_DECAY_ROUNDS 8
156/* Retransmitted data sequence numbers, used to detect late-arriving originals. */
157#define RDPUDP2_RESENT_HISTORY 256
158/* An original that has not shown up by then is not late but lost. Entries only leave through
159 * ACK vectors, and without the limit one that saw no vector for half the sequence space would
160 * match an unrelated datagram a wrap later. */
161#define RDPUDP2_RESENT_MAX_AGE_MS 2000
162/* [RFC6298] 5.5: after a timeout, new datagrams use the doubled timeout until an RTT
163 * sample arrives, backing off by at most 2^RDPUDP2_MAX_RTO_BACKOFF. This ensures RTT
164 * growth past the timeout is measured, as Karn's rule discards samples from
165 * retransmitted datagrams. */
166#define RDPUDP2_MAX_RTO_BACKOFF 6
167#define RDPUDP2_GROWTH_ACKS 8
168#define RDPUDP2_CONGESTED_LOSS_PERCENT 10
169#define RDPUDP2_LOSS_RISE_PERCENT 5
170/* Weight of a round in the smoothed loss, 1/2^RDPUDP2_LOSS_AVG_SHIFT */
171#define RDPUDP2_LOSS_AVG_SHIFT 3
172#define RDPUDP2_ROUND_MIN_SAMPLES 16
173#define RDPUDP2_QUEUE_DELAY_MS 30
174/* Number of consecutive delay verdicts with an unchanged minimum round trip required to
175 * update the path's base delay. See cc_delay_is_ours. */
176#define RDPUDP2_DELAY_REBASE_CUTS 2u
177/* The percentage threshold (plus or minus) that the lowest round trip of a round must
178 * remain within relative to the last verdict to be counted as unchanged. */
179#define RDPUDP2_DELAY_REBASE_PERCENT 90
180/* If an application-limited sender hasn't reached the minimum RTT for this long, the value
181 * may be stale (e.g., the base delay decreased or cross traffic subsided while the sender
182 * maintained its own queue). A drain probe then re-measures it; see cc_probe. */
183#define RDPUDP2_RTT_PROBE_MS 10000
184
185/* Max distance the newest data sequence number can lead the oldest unacked one.
186 * Data received behind a gap is buffered until the gap closes. With mstsc (100 ms delay,
187 * 1% loss), spans of 1024+ sustained full rate, while a span of 384 limited throughput
188 * to <4.7 MB/s. mstsc accepts new data up to a span of 6142. */
189#define RDPUDP2_MAX_SPAN RDPUDP_MAX_IN_FLIGHT
190
191/* [MS-RDPEUDP2] LogWindowSize. Windows always advertises the largest window (2^15 * MTU), a
192 * smaller value throttles the server's sender for no benefit. */
193#define RDPUDP2_LOG_WINDOW_SIZE 15
194
195/* Both Windows endpoints start the RDP-UDP2 data sequence space at 100. */
196#define RDPUDP2_INITIAL_SEQUENCE 100
197
198/* [MS-RDPEUDP2] 3.1.5.2: a receiver acknowledges at least every 8 data packets until the peer's
199 * DelayAckInfo says otherwise, which allows at most 15. */
200#define RDPUDP2_DEFAULT_DELAYED_ACKS 8
201#define RDPUDP2_MAX_DELAYED_ACKS 15
202
203/* What an RDP-UDP2 DATA datagram carries besides its payload: prefix, header, an ACK without
204 * delayed acknowledgements, DelayAckInfo, AckOfAcks, DataHeader and channel sequence number. */
205#define RDPUDP2_DATA_OVERHEAD 19
206
207/* Room kept in a full DATA datagram for the arrival gaps of the ACK it carries. */
208#define RDPUDP2_DATA_ACK_ROOM 5
209
210/* Asked for as the socket receive buffer. A redraw arrives as a burst of hundreds of datagrams,
211 * more than the default buffer holds, and a datagram that does not fit waits for a resend. */
212#define RDPUDP_RECEIVE_BUFFER (2 * 1024 * 1024)
213
214/* RDPUDP2 ACK vectors hold at most 127 state map bytes of 7 packets each. */
215#define RDPUDP2_MAX_ACKVEC 127
216
217/* Room left in every version 1/2 data datagram for the ACK vector and AckOfAcks. */
218#define RDPUDP_ACK_ROOM 64
219
220/* SYN and SYN+ACK are padded to the MTU ([MS-RDPEUDP] 3.1.5.1.1), so a line that drops large
221 * datagrams drops them first: with 4 attempts the tunnel was not set up on a line losing 43% of
222 * them. Ten, a second apart, stay well inside the setup time of the tunnel
223 * (RDPEMT_SETUP_TIMEOUT_MS). */
224#define RDPUDP_SYN_TIMEOUT_MS 1000
225#define RDPUDP_SYN_ATTEMPTS 3
226
227/* [MS-RDPEUDP] 3.1.6.1: the retransmit timeout is twice the round trip, at least the version's
228 * minimum, and doubles with every resend of the same datagram. Windows lets it grow to 120
229 * seconds (note <8>). Below the round trip, every datagram is resent before its acknowledgement
230 * can arrive. */
231#define RDPUDP_MAX_RTO_MS 120000
232/* RDP-UDP2: cap on the backed-off timeout, as in Windows, which clamps it to 2 seconds. A
233 * receiver ends the stream once a chunk has been missing for RDPUDP_STALL_TIMEOUT_MS; with the
234 * timeout doubling to RDPUDP_MAX_RTO_MS, a datagram lost a few times in a row would be resent
235 * too late. Dead connections are handled by the peer timeout. See rto_v3_cap for RTT-based cap
236 * increases. */
237#define RDPUDP2_MAX_RTO_MS 2000
238/* Server: upper bound on the initial RTT sample from the handshake. */
239#define RDPUDP2_HANDSHAKE_RTT_MAX_MS 500
240
241/* [MS-RDPEUDP] 3.1.5.4: a datagram retransmitted three to five times without a response ends
242 * the connection, and so does 65 seconds without any datagram from the peer (3.1.1.9). */
243#define RDPUDP_MAX_RETRANSMITS 5
244#define RDPUDP_KEEPALIVE_MS 5000
245#define RDPUDP_PEER_TIMEOUT_MS 65000
246
247/* [MS-RDPEUDP2] 3.1.1.3: both ends send at least every 16 seconds, Windows every 4, and 16
248 * seconds without a datagram mean the peer is gone. The retransmit limit is left to the sender,
249 * the peer timeout already bounds how long a dead connection goes unnoticed. */
250#define RDPUDP2_MAX_RETRANSMITS 10
251
252/* To prevent ACK vector-induced flooding, retransmits for reported losses don't count
253 * toward the limit. Chunks are resent at most once per RTT since the last send,
254 * capped at cwnd/4 per RTT (minimum RDPUDP2_REPORT_MIN_BUDGET). Both use smoothed RTT,
255 * which is at least RDPUDP2_REPORT_MIN_ROUND_MS (clock resolution) to prevent
256 * duplicate resends when a round trip is measured as 0 ms. */
257#define RDPUDP2_REPORT_MIN_ROUND_MS 1
258#define RDPUDP2_REPORT_MIN_BUDGET 16
259
260#define RDPUDP2_KEEPALIVE_MS 4000
261/* Half a second more than the 16 seconds of the specification, as Windows has it: keepalives of
262 * Windows come a little over 4 seconds apart, so the fourth after three lost ones arrived
263 * 16.06 seconds after the last one heard, and at 16000 an idle tunnel ended 40 ms before it. */
264#define RDPUDP2_PEER_TIMEOUT_MS 16500
265
266/* Silence after which the peer may have moved more than half the sequence space on, see
267 * v3_resync_base. Two keepalive intervals: an idle tunnel gets there when one keepalive is
268 * lost, see v3_send_base_keepalive. */
269#define RDPUDP2_RESYNC_MS (2ull * RDPUDP2_KEEPALIVE_MS)
270/* Server: interval of the keepalive during that resync, see v3_send_base_keepalive. A client
271 * in resync sends nothing until one of these arrives, and the server has heard nothing for
272 * RDPUDP2_RESYNC_MS already: at the normal interval two lost ones would end the tunnel. */
273#define RDPUDP2_RESYNC_KEEPALIVE_MS 1000
274
275/* Server: The distance behind the window that an AckOfAcks after silence can be while
276 * keeping the window stationary. See v3_resync_base: Windows resends at most 8190 packets
277 * past the oldest unacknowledged datagram, plus another 8190 for those in flight. A real
278 * sequence move to this point would require 49152 numbers within the peer timeout, but
279 * Windows moves at most ~2500 per second. */
280#define RDPUDP2_STALE_AOA_REACH 16384
281
282/* Server: Limits the distance between the newest data sequence number and the oldest one in
283 * flight, including resends. [MS-RDPEUDP2] 3.1.1.1.3 limits the active range to 32767, as
284 * exceeding half the space causes serial arithmetic to treat AckOfAcks as a number ahead. */
285#define RDPUDP2_MAX_RESEND_SPAN (RDPUDP_RECEIVE_REACH - 1)
286
287/* [MS-RDPEUDP2] 3.1.5.1.3: the peer resends a lost chunk under a new data sequence number, and
288 * the receiver has no way to ask for it. A stream stuck behind a missing chunk for as long as the
289 * peer timeout is no better than a peer that is gone: the tunnel fails, ending the connection like
290 * a broken transport, instead of leaving the session frozen while the peer's keepalives keep it
291 * alive. */
292#define RDPUDP_STALL_TIMEOUT_MS RDPUDP2_PEER_TIMEOUT_MS
293
294typedef struct
295{
296 UINT32 seq; /* source sequence (v1/v2) or data sequence (v3) */
297 UINT16 channelSeq;
298 BYTE* payload;
299 size_t length;
300 UINT64 sent;
301 UINT64 lastSent; /* Server: timestamp of the most recent transmission; 'sent' stores the initial
302 transmission time. */
303 UINT64 due;
304 UINT32 rto;
305 UINT32 retransmits;
306 UINT32 timeouts; /* Retransmissions due to timeout; only these count toward the limit. */
307 BOOL lost; /* Reported missing by an ACK vector; do not wait for the due timer. */
308 /* server: reported missing within one RTT of sending. run_timers processes the
309 * report after the RTT expires, as the peer may have further reports. */
310 BOOL reportHeld;
311} rdpudp_pending;
312
313typedef struct
314{
315 BOOL present;
316 UINT32 seq;
317 BYTE* data;
318 size_t length;
319} rdpudp_slot;
320
321typedef struct
322{
323 BYTE* data;
324 size_t length;
325} rdpudp_chunk;
326
327struct rdp_udp
328{
329 wLog* log;
330 SOCKET sockfd;
331 BOOL ownsSocket; /* FALSE for server connections, which do not own the socket */
332 BOOL receiveHold; /* see rdpudp_set_receive_hold() */
333 rdpUdpClockFn clock; /* If nullptr, the system clock is used; see rdpudp_set_clock(). */
334 void* clockCustom;
335 UINT16 chunkReach; /* Maximum channel sequence distance ahead of the expected sequence that is
336 accepted */
337 RDPUDP_STATE state;
338 UINT16 version;
339 BOOL offerV3;
340 BYTE cookieHash[32];
341
342 rdpUdpReceiveFn receive;
343 void* custom;
344
345 UINT16 mtu;
346 UINT32 synAttempts;
347 UINT64 synSent;
348 UINT64 lastSent;
349 UINT64 lastReceived;
350 UINT32 srtt;
351 UINT32 rttvar; /* RTT variation in milliseconds, as defined in RFC 6298 */
352 BOOL rttSampled; /* True if srtt and rttvar have been initialized with at least one sample */
353 UINT64 synAckSent; /* server: timestamp of the SYN+ACK transmission, until the first datagram;
354 otherwise 0 */
355 BOOL synAckResent; /* server: the SYN+ACK was retransmitted */
356
357 /* sending side */
358 rdpudp_pending pending[RDPUDP_MAX_IN_FLIGHT]; /* the first inFlight entries are in use */
359 size_t inFlight;
360 /* position in pending + 1 by the low 16 bits of the sequence number, 0 when free */
361 UINT16 pendingIndex[RDPUDP_PENDING_INDEX];
362 UINT32 sendBase; /* lowest sequence number in flight, while inFlight > 0 */
363 UINT64 pendingDue; /* run_timers looks at the datagrams in flight from this time on */
364 UINT32 cwnd; /* Congestion window for RDP-UDP2, measured in datagrams */
365 UINT32 ssthresh; /* The slow start threshold. */
366 UINT32 ccAcked; /* Number of ACKs counted toward the next congestion window increase */
367 UINT64 ccRoundStart; /* Start time of the round trip being tracked for congestion control; see
368 cc_round */
369 UINT32 ccRoundAcked;
370 UINT32 ccRoundLost;
371 UINT32 ccRoundRttMin; /* Minimum RTT sample of the current round; UINT32_MAX if none */
372 UINT32 ccDelayCuts; /* Consecutive delay verdicts that found the round trip unchanged */
373 UINT32 ccDelayLastRtt; /* Lowest RTT sample of the latest delay verdict's round; 0 if none */
374 BOOL ccDelayHold; /* TRUE: the last round was cut, its effect is not in samples yet */
375 UINT64 rttMinSeen; /* Timestamp of the last sample that reached rttMin */
376 UINT32 ccProbe; /* 0: no probe; 1: draining; 2: measuring (drain probe round) */
377 UINT32 ccProbeCwnd; /* The congestion window size to restore after the probe */
378 UINT32 ccLossAvg; /* Smoothed loss of the rounds with enough samples, in 1/1000 */
379 UINT32 reorderThreshold; /* Sequence distance beyond which a missing datagram is
380 * considered lost */
381 UINT32 reorderCalmRounds;
382 UINT32 rtoBackoff; /* Number of times the timeout has doubled since the last RTT sample */
383 struct
384 {
385 BOOL valid;
386 BOOL reported; /* Retransmitted due to an ACK vector report rather than a timeout */
387 UINT16 seq;
388 UINT64 at; /* when it was resent, milliseconds */
389 } resent[RDPUDP2_RESENT_HISTORY]; /* Data sequence numbers that have been superseded by a
390 retransmission */
391 size_t resentNext;
392 UINT32 rttMin; /* Minimum RTT sample; only increased by cc_delay_is_ours. UINT32_MAX if none. */
393 /* RTT for the reported-loss budget (see report_allowed). Unlike ccRoundStart,
394 * this is purely time-based and ignores peer traffic. */
395 UINT64 reportRoundStart;
396 UINT32 reportRoundUsed;
397 UINT32 peerWindow;
398 wQueue* sendQueue;
399 UINT32 initialSequence;
400 UINT32 nextSource;
401 UINT32 nextCoded;
402 BOOL cwrPending;
403 UINT32 announcedAoA;
404 BOOL aoaAnnounced;
405 BOOL peerAcked; /* Indicates whether the peer has acknowledged at least one data datagram */
406 UINT16 nextChannelSeq;
407
408 /* receiving side */
409 BOOL ackPending;
410 BOOL receivedData;
411 rdpudp_slot slots[RDPUDP_RECEIVE_SLOTS];
412 size_t buffered;
413 UINT64 lastDelivery;
414 UINT64 bufferingSince;
415
416 /* receiving, versions 1 and 2 */
417 UINT32 peerInitialSequence;
418 UINT32 recvNext;
419 UINT32 recvHighest;
420 UINT32 ackVectorStart;
421
422 /* receiving, version 3 */
423 BOOL v3BaseKnown;
424 BOOL v3Resync; /* silent for RDPUDP2_RESYNC_MS: no ACK until an AckOfAcks sets the base */
425 UINT16 v3Expected;
426 UINT64 v3ExpectedFull; /* v3Expected without the 16-bit wrap; it only grows */
427 UINT16 v3Highest;
428 /* Per slot, the full sequence number (like v3ExpectedFull) of the data packet that arrived,
429 * or 0 if empty. Since each slot is stamped with its sequence number, moving the window
430 * doesn't require clearing for wrap-around; see v3_received. */
431 UINT64 v3Received[RDPUDP_RECEIVE_SLOTS];
432 UINT64 v3Arrival[RDPUDP_RECEIVE_SLOTS]; /* when each data sequence arrived, in ns */
433 UINT64 v3InOrderArrival; /* when data sequence v3Expected - 1 arrived */
434 BOOL v3InOrderReceived; /* FALSE while v3Expected - 1 is one AckOfAcks gave up on */
435 UINT64 v3LatestArrival; /* when the latest data packet arrived */
436 size_t v3MaxDelayedAcks;
437 UINT32 v3DelayedAckTimeout; /* in ms, UINT32_MAX until the peer's DelayAckInfo sets it */
438 UINT64 v3AckDue; /* in ms, when what waits for our acknowledgement must get it; 0: nothing */
439 size_t v3UnackedPackets; /* data packets received since our last acknowledgement */
440 UINT16 v3AckedSeq; /* the SeqNum of our last ACK payload */
441 BOOL v3AckedGap; /* whether our last acknowledgement reported a missing packet */
442 BOOL v3WindowPending; /* the window has news for an acknowledgement */
443 /* data packets behind the window, acknowledged on their own, see v3_send_ack: a run of
444 * consecutive sequence numbers from v3BehindBase, and when the newest of them arrived */
445 UINT16 v3BehindBase;
446 size_t v3BehindCount;
447 UINT64 v3BehindArrival;
448 UINT16 v3NextChannel;
449
450 /* Server: send functions and the SYN+ACK used for repeated SYN requests. */
451 BOOL server;
452 rdpUdpSendFn sendFn;
453 void* sendCustom;
454 BYTE synAck[RDPUDP_MTU];
455 size_t synAckLength;
456
457 /* One received datagram. Kept here rather than on the stack, rdpudp_check is not reentrant. */
458 BYTE recvBuffer[0x10000];
459};
460
461/* RDP-UDP sequence numbers are 16 (RDPUDP2) or 32 (RDPUDP) bit counters that wrap around, they
462 * are compared and advanced with RFC 1982 serial number arithmetic. The wrap in the helpers
463 * below is intentional, they mask to the counter width before the checked conversion. */
464WINPR_ATTR_NODISCARD
465static inline UINT16 seq16_add(UINT16 a, size_t n)
466{
467 return WINPR_ASSERTING_INT_CAST(UINT16, (a + n) & 0xFFFFu);
468}
469
470/* a - b modulo 2^16 */
471WINPR_ATTR_NODISCARD
472static inline UINT16 seq16_diff(UINT16 a, UINT16 b)
473{
474 return WINPR_ASSERTING_INT_CAST(UINT16, ((UINT32)a - (UINT32)b) & 0xFFFFu);
475}
476
477WINPR_ATTR_NODISCARD
478static inline UINT32 seq32_add(UINT32 a, UINT32 n)
479{
480 return WINPR_ASSERTING_INT_CAST(UINT32, ((UINT64)a + n) & 0xFFFFFFFFull);
481}
482
483/* a - b modulo 2^32 */
484WINPR_ATTR_NODISCARD
485static inline UINT32 seq32_diff(UINT32 a, UINT32 b)
486{
487 return WINPR_ASSERTING_INT_CAST(UINT32, ((UINT64)a - (UINT64)b) & 0xFFFFFFFFull);
488}
489
490WINPR_ATTR_NODISCARD
491static BOOL seq16_after(UINT16 a, UINT16 b)
492{
493 return (a != b) && (seq16_diff(a, b) < 0x8000);
494}
495
496WINPR_ATTR_NODISCARD
497static BOOL seq32_after(UINT32 a, UINT32 b)
498{
499 return (a != b) && (seq32_diff(a, b) < 0x80000000u);
500}
501
502/* Channel sequence numbers start at 1 and Windows wraps 65535 to 1, never using 0. Ours do the
503 * same, both sending and receiving. */
504WINPR_ATTR_NODISCARD
505static UINT16 channel_seq_next(UINT16 seq)
506{
507 return (seq == UINT16_MAX) ? 1 : seq16_add(seq, 1);
508}
509
510/* RDP-UDP2 receive slots. A slot stores the full sequence number of the packet that arrived
511 * in it, so a packet 65536 positions later in the same slot is not incorrectly identified
512 * as received. Previously, slots were cleared as the window advanced, and AckOfAcks jumps
513 * of up to 32767 cleared them. */
514
515/* Returns the full sequence number for seq, assuming it lies within half the sequence
516 * space of v3Expected ([MS-RDPEUDP2] 3.1.1.1.3). */
517WINPR_ATTR_NODISCARD
518static UINT64 v3_full(const rdpUdp* udp, UINT16 seq)
519{
520 WINPR_ASSERT(udp);
521 const UINT16 ahead = seq16_diff(seq, udp->v3Expected);
522 if (ahead < 0x8000)
523 return udp->v3ExpectedFull + ahead;
524 return udp->v3ExpectedFull - (0x10000u - ahead);
525}
526
527WINPR_ATTR_NODISCARD
528static BOOL v3_received(const rdpUdp* udp, UINT16 seq)
529{
530 WINPR_ASSERT(udp);
531 return udp->v3Received[seq % RDPUDP_RECEIVE_SLOTS] == v3_full(udp, seq);
532}
533
534/* Returns the arrival time of seq, or 0 if it was not received or the slot was reused. */
535WINPR_ATTR_NODISCARD
536static UINT64 v3_arrival(const rdpUdp* udp, UINT16 seq)
537{
538 WINPR_ASSERT(udp);
539 return v3_received(udp, seq) ? udp->v3Arrival[seq % RDPUDP_RECEIVE_SLOTS] : 0;
540}
541
542static void v3_mark_received(rdpUdp* udp, UINT16 seq, UINT64 arrival)
543{
544 WINPR_ASSERT(udp);
545 udp->v3Received[seq % RDPUDP_RECEIVE_SLOTS] = v3_full(udp, seq);
546 udp->v3Arrival[seq % RDPUDP_RECEIVE_SLOTS] = arrival;
547}
548
549/* Advances the lower edge of the receive window to seq; preceding slots do not need clearing. */
550static void v3_set_expected(rdpUdp* udp, UINT16 seq)
551{
552 WINPR_ASSERT(udp);
553 udp->v3ExpectedFull = v3_full(udp, seq);
554 udp->v3Expected = seq;
555}
556
557/* [MS-RDPEUDP2] has 24 bit little endian fields, wStream has no helper for those */
558static void stream_write_uint24(wStream* s, UINT32 value)
559{
560 WINPR_ASSERT(s);
561 const UINT16 low = WINPR_ASSERTING_INT_CAST(UINT16, value & 0xFFFF);
562 const BYTE high = WINPR_ASSERTING_INT_CAST(BYTE, (value >> 16) & 0xFF);
563 Stream_Write_UINT16(s, low);
564 Stream_Write_UINT8(s, high);
565}
566
567WINPR_ATTR_NODISCARD
568static UINT64 now_ns(const rdpUdp* udp)
569{
570 WINPR_ASSERT(udp);
571 if (udp->clock)
572 return udp->clock(udp->clockCustom);
573 return winpr_GetTickCount64NS();
574}
575
576WINPR_ATTR_NODISCARD
577static UINT64 now_ms(const rdpUdp* udp)
578{
579 WINPR_ASSERT(udp);
580 if (udp->clock)
581 return udp->clock(udp->clockCustom) / 1000000ULL;
582 return GetTickCount64();
583}
584
585/* [MS-RDPEUDP] 3.1.6.1 specifies 500ms for v1 and 300ms for v2. [MS-RDPEUDP2] 3.1.1.2.3
586 * requires a loss timeout exceeding the delayed ACK time (20ms, sent in DelayAckInfo).
587 * Servers typically have an RTT sample from the handshake; otherwise (e.g. SYN+ACK
588 * resent), 300ms is used to prevent premature retransmissions of TLS flights on
589 * long-RTT paths. After the first sample, rto_initial tracks the samples. */
590WINPR_ATTR_NODISCARD
591static UINT32 rto_floor(const rdpUdp* udp)
592{
593 WINPR_ASSERT(udp);
594 if (udp->server)
595 return udp->rttSampled ? 100 : 300;
596 return (udp->version == RDPUDP_PROTOCOL_VERSION_1) ? 500 : 300;
597}
598
599/* Server RTO: srtt + 4 * rttvar ([RFC6298] 2.3), with a minimum variation term.
600 * On LANs, mstsc/Windows retransmit after roughly RTT + 80-90ms, with the margin
601 * increasing with RTT. This floor also accounts for receivers that delay ACKs
602 * (mstsc up to ~70ms). */
603#define RDPUDP2_RTO_MIN_VARIATION_MS 80
604
605WINPR_ATTR_NODISCARD
606static UINT64 rto_base(const rdpUdp* udp)
607{
608 WINPR_ASSERT(udp);
609 UINT64 rto = 0;
610 if (udp->server && udp->rttSampled)
611 {
612 const UINT64 variation = 4ull * udp->rttvar;
613 const UINT64 margin =
614 (variation > RDPUDP2_RTO_MIN_VARIATION_MS) ? variation : RDPUDP2_RTO_MIN_VARIATION_MS;
615 rto = udp->srtt + margin;
616 }
617 else
618 {
619 const UINT32 floor = rto_floor(udp);
620 const UINT64 rtt2 = (UINT64)udp->srtt * 2ull;
621 rto = (rtt2 > floor) ? rtt2 : floor;
622 }
623 return rto;
624}
625
626/* RDP-UDP2 backoff cap. If RTT exceeds RDPUDP2_MAX_RTO_MS, the cap is raised
627 * to ensure datagrams aren't retransmitted before their ACKs can arrive. */
628WINPR_ATTR_NODISCARD
629static UINT64 rto_v3_cap(const rdpUdp* udp)
630{
631 WINPR_ASSERT(udp);
632 const UINT64 base = rto_base(udp);
633 return (base > RDPUDP2_MAX_RTO_MS) ? base : RDPUDP2_MAX_RTO_MS;
634}
635
636WINPR_ATTR_NODISCARD
637static UINT32 rto_initial(const rdpUdp* udp)
638{
639 WINPR_ASSERT(udp);
640 UINT64 rto = rto_base(udp);
641 if (udp->server)
642 {
643 /* srtt and margin are < 2^35; shift will not overflow 64 bits */
644 WINPR_ASSERT(udp->rtoBackoff <= RDPUDP2_MAX_RTO_BACKOFF);
645 rto <<= udp->rtoBackoff;
646 const UINT64 cap = rto_v3_cap(udp);
647 if (rto > cap)
648 rto = cap;
649 }
650 return (rto > RDPUDP_MAX_RTO_MS) ? RDPUDP_MAX_RTO_MS : WINPR_ASSERTING_INT_CAST(UINT32, rto);
651}
652
653static void chunk_free(void* obj)
654{
655 rdpudp_chunk* chunk = obj;
656 if (!chunk)
657 return;
658 free(chunk->data);
659 free(chunk);
660}
661
662static void slot_clear(rdpudp_slot* slot)
663{
664 WINPR_ASSERT(slot);
665 free(slot->data);
666 slot->data = nullptr;
667 slot->length = 0;
668 slot->present = FALSE;
669}
670
671static void pending_clear(rdpudp_pending* p)
672{
673 WINPR_ASSERT(p);
674 free(p->payload);
675 memset(p, 0, sizeof(*p));
676}
677
678static void rdpudp_fail(rdpUdp* udp, const char* why)
679{
680 WINPR_ASSERT(udp);
681 WINPR_ASSERT(why);
682 if (udp->state != RDPUDP_STATE_FAILED)
683 WLog_Print(udp->log, WLOG_WARN, "RDP-UDP connection failed: %s", why);
684 udp->state = RDPUDP_STATE_FAILED;
685}
686
687/* Socket errors are handled as on the TCP transport (transport_bio_simple_read and
688 * transport_bio_simple_write): a wait condition is retried and anything else fails. A datagram
689 * socket adds the conditions of the path, an ICMP error for an earlier datagram, a full buffer,
690 * a network that is down or unreachable for a moment: they cost the one datagram, which the
691 * retransmit timer recovers, and the peer timeout ends a path that stays broken. A firewall rule
692 * that drops the datagram is one of them, send() fails with EPERM then, which WinPR does not map
693 * and reports as 0; TCP never sees an error for that, it retransmits. */
694BOOL rdpudp_socket_error_is_transient(int error)
695{
696 switch (error)
697 {
698 case 0:
699 case WSAEWOULDBLOCK:
700 case WSAEINTR:
701 case WSAEINPROGRESS:
702 case WSAEALREADY:
703 case WSAENOBUFS:
704 case WSAECONNRESET:
705 case WSAECONNREFUSED:
706 case WSAENETRESET:
707 case WSAENETDOWN:
708 case WSAENETUNREACH:
709 case WSAEHOSTDOWN:
710 case WSAEHOSTUNREACH:
711 return TRUE;
712 default:
713 return FALSE;
714 }
715}
716
717WINPR_ATTR_NODISCARD
718static BOOL rdpudp_send_raw(rdpUdp* udp, const BYTE* data, size_t length)
719{
720 WINPR_ASSERT(udp);
721 WINPR_ASSERT(data);
722 if (udp->sendFn)
723 {
724 if (!udp->sendFn(udp->sendCustom, data, length))
725 {
726 rdpudp_fail(udp, "sending failed");
727 return FALSE;
728 }
729 udp->lastSent = now_ms(udp);
730 return TRUE;
731 }
732
733 const int rc = _send(udp->sockfd, (const char*)data, WINPR_ASSERTING_INT_CAST(int, length), 0);
734 if (rc < 0)
735 {
736 const int error = WSAGetLastError();
737 if (!rdpudp_socket_error_is_transient(error))
738 {
739 WLog_Print(udp->log, WLOG_ERROR, "send failed with %d", error);
740 rdpudp_fail(udp, "sending on the socket failed");
741 return FALSE;
742 }
743 WLog_Print(udp->log, WLOG_DEBUG, "send failed with %d, the datagram counts as lost", error);
744 return TRUE;
745 }
746 udp->lastSent = now_ms(udp);
747 return TRUE;
748}
749
750/* ------------------------------------------------------------------------------------------ */
751/* [MS-RDPEUDP2] encoding */
752/* ------------------------------------------------------------------------------------------ */
753
754WINPR_ATTR_NODISCARD
755static UINT32 v3_timestamp(UINT64 ns)
756{
757 /* [MS-RDPEUDP2] 3.1.1.1.4: 24 bits in units of 4 microseconds */
758 return WINPR_ASSERTING_INT_CAST(UINT32, (ns / 4000ULL) & 0x00FFFFFFULL);
759}
760
761/* Milliseconds an acknowledgement waited after the packet it reports arrived. */
762WINPR_ATTR_NODISCARD
763static BYTE v3_ack_gap(UINT64 arrival, UINT64 now)
764{
765 if ((arrival == 0) || (now < arrival))
766 return 0;
767 const UINT64 gap = (now - arrival) / 1000000ULL;
768 return (gap > 254) ? 254 : WINPR_ASSERTING_INT_CAST(BYTE, gap); /* 255 means invalid */
769}
770
771WINPR_ATTR_NODISCARD
772static BOOL v3_has_gap(const rdpUdp* udp)
773{
774 WINPR_ASSERT(udp);
775 return udp->v3BaseKnown && seq16_after(udp->v3Highest, seq16_diff(udp->v3Expected, 1));
776}
777
778/* [MS-RDPEUDP2] 2.2.1.2.1 ACK payload. receivedTS is when the acknowledged packet arrived and
779 * sendAckTimeGap how long the acknowledgement waited after that. The peer measures the path from
780 * both, a send time in receivedTS would make the time spent here look like network delay.
781 *
782 * The packets acknowledged along with SeqNum since our previous ACK follow as delayed
783 * acknowledgements, at most room of them (3.1.5.6): the gaps between their arrivals, newest
784 * first. The peer estimates the bandwidth of the path from these, mstsc sends them. */
785static void v3_write_ack(rdpUdp* udp, wStream* s, size_t room)
786{
787 WINPR_ASSERT(udp);
788 WINPR_ASSERT(s);
789 const UINT64 now = now_ns(udp);
790 /* A sequence number the peer gave up on never arrived. An older packet's arrival time would
791 * place its receipt before it was sent, so it is reported as received now. */
792 const UINT64 arrival = udp->v3InOrderReceived ? udp->v3InOrderArrival : now;
793 const UINT16 seq = seq16_diff(udp->v3Expected, 1);
794
795 UINT64 gaps[RDPUDP2_MAX_DELAYED_ACKS] = WINPR_C_ARRAY_INIT; /* microseconds, newest first */
796 size_t count = 0;
797 if (udp->v3InOrderReceived)
798 {
799 size_t limit = seq16_diff(seq, udp->v3AckedSeq); /* packets acknowledged, SeqNum included */
800 limit = (limit > 0) ? limit - 1 : 0;
801 if (limit > udp->v3MaxDelayedAcks)
802 limit = udp->v3MaxDelayedAcks;
803 if (limit > room)
804 limit = room;
805 if (limit > RDPUDP2_MAX_DELAYED_ACKS)
806 limit = RDPUDP2_MAX_DELAYED_ACKS;
807
808 UINT16 current = seq;
809 UINT64 currentArrival = arrival;
810 while (count < limit)
811 {
812 const UINT16 previous = seq16_diff(current, 1);
813 const UINT64 previousArrival = v3_arrival(udp, previous);
814 if (previousArrival == 0)
815 break; /* given up on by AckOfAcks, it never arrived */
816 /* packets that overtook each other count as arriving together */
817 gaps[count++] = (currentArrival > previousArrival)
818 ? (currentArrival - previousArrival) / 1000ULL
819 : 0;
820 current = previous;
821 currentArrival = previousArrival;
822 }
823 }
824
825 /* each gap has to fit a byte in units of (1 << scale) microseconds */
826 BYTE scale = 0;
827 for (size_t x = 0; x < count; x++)
828 {
829 while ((scale < 15) && ((gaps[x] >> scale) > 255))
830 scale++;
831 }
832
833 Stream_Write_UINT16(s, seq); /* SeqNum */
834 stream_write_uint24(s, v3_timestamp(arrival)); /* receivedTS */
835 /* the Stream_Write macros evaluate their value more than once */
836 const BYTE gap = v3_ack_gap(arrival, now);
837 Stream_Write_UINT8(s, gap); /* sendAckTimeGapInMs */
838 const BYTE delayed = WINPR_ASSERTING_INT_CAST(BYTE, count | ((size_t)scale << 4));
839 Stream_Write_UINT8(s, delayed); /* numDelayedAcks, delayAckTimeScale */
840 for (size_t x = 0; x < count; x++)
841 {
842 const UINT64 scaled = gaps[x] >> scale;
843 const BYTE addition = (scaled > 255) ? 255 : WINPR_ASSERTING_INT_CAST(BYTE, scaled);
844 Stream_Write_UINT8(s, addition); /* delayAckTimeAdditions */
845 }
846 udp->v3AckedSeq = seq;
847}
848
849/* Starts a packet: one byte is left free for the prefix, then the header. */
850WINPR_ATTR_NODISCARD
851static wStream* v3_packet_init(wStream* buffer, BYTE* data, size_t size, UINT16 flags)
852{
853 WINPR_ASSERT(buffer);
854 WINPR_ASSERT(data);
855 wStream* s = Stream_StaticInit(buffer, data, size);
856 if (!s)
857 return nullptr;
858 Stream_Seek(s, 1);
859 const UINT16 header = WINPR_ASSERTING_INT_CAST(UINT16, flags | (RDPUDP2_LOG_WINDOW_SIZE << 12));
860 Stream_Write_UINT16(s, header);
861 return s;
862}
863
864/* Finishes a packet into its on-wire form and sends it ([MS-RDPEUDP2] 2.2.1): the prefix byte
865 * goes in front, a short packet is padded, and the first and eighth byte trade places. */
866WINPR_ATTR_NODISCARD
867static BOOL v3_send_packet(rdpUdp* udp, wStream* s, BYTE packetType)
868{
869 WINPR_ASSERT(udp);
870 WINPR_ASSERT(s);
871 if (Stream_GetPosition(s) < 1)
872 return FALSE;
873 const size_t layout = Stream_GetPosition(s) - 1;
874 BYTE shortLength = 7;
875 if (layout < 7)
876 {
877 shortLength = WINPR_ASSERTING_INT_CAST(BYTE, layout);
878 Stream_Zero(s, 7 - layout);
879 }
880 const size_t length = Stream_GetPosition(s);
881
882 if (!Stream_SetPosition(s, 0))
883 return FALSE;
884 const BYTE prefix =
885 WINPR_ASSERTING_INT_CAST(BYTE, (shortLength << 5) | ((packetType & 0x0F) << 1));
886 Stream_Write_UINT8(s, prefix);
887
888 /* the swap scrambles the finished datagram, it is not a field of it */
889 BYTE* wire = Stream_Buffer(s);
890 const BYTE eighth = wire[7];
891 wire[7] = wire[0];
892 wire[0] = eighth;
893 return rdpudp_send_raw(udp, wire, length);
894}
895
896/* [MS-RDPEUDP2] 2.2.1.2.6 ACK vector payload. TimeStamp is when the newest packet it reports
897 * arrived, SendAckTimeGapInMs the time since the latest data packet arrived. */
898WINPR_ATTR_NODISCARD
899static BOOL v3_send_ack_vector(rdpUdp* udp, UINT16 base, const BYTE* vec, size_t count,
900 UINT64 arrival)
901{
902 BYTE buffer[64 + RDPUDP2_MAX_ACKVEC] = WINPR_C_ARRAY_INIT;
903 wStream sbuffer = WINPR_C_ARRAY_INIT;
904
905 WINPR_ASSERT(udp);
906 WINPR_ASSERT(vec);
907 WINPR_ASSERT((count > 0) && (count <= RDPUDP2_MAX_ACKVEC));
908 wStream* s =
909 v3_packet_init(&sbuffer, buffer, sizeof(buffer), RDPUDP2_OVERHEADSIZE | RDPUDP2_ACKVEC);
910 if (!s)
911 return FALSE;
912 const UINT64 now = now_ns(udp);
913 const BYTE control = WINPR_ASSERTING_INT_CAST(BYTE, 0x80 | count);
914 /* the Stream_Write macros evaluate their value more than once */
915 const BYTE ackGap = v3_ack_gap(udp->v3LatestArrival, now);
916 const UINT32 timestamp = v3_timestamp((arrival != 0) ? arrival : now);
917 Stream_Write_UINT8(s, 10); /* OverheadSize */
918 Stream_Write_UINT16(s, base); /* BaseSeqNum */
919 Stream_Write_UINT8(s, control); /* TimeStampPresent, codedAckVecSize */
920 stream_write_uint24(s, timestamp); /* TimeStamp */
921 Stream_Write_UINT8(s, ackGap); /* SendAckTimeGapInMs */
922 Stream_Write(s, vec, count); /* codedAckVector */
923 return v3_send_packet(udp, s, RDPUDP2_PACKET_STANDARD);
924}
925
926WINPR_ATTR_NODISCARD
927static BOOL v3_send_ack(rdpUdp* udp)
928{
929 BYTE buffer[64] = WINPR_C_ARRAY_INIT;
930 wStream sbuffer = WINPR_C_ARRAY_INIT;
931
932 WINPR_ASSERT(udp);
933 if (!udp->v3BaseKnown)
934 return TRUE;
935
936 /* Data packets behind our window, see v3_process_datagram, go in an ACK vector of their own:
937 * received, as runs of up to 63. A cumulative ACK would name our own position, which after a
938 * short outage at a high rate can lie ahead of what the peer has sent, and Windows ends the
939 * connection for that. It would also mark everything below it received ([MS-RDPEUDP2] 4.1.3).
940 * The vector reports what arrived and nothing more (4.2.2). */
941 if (udp->v3BehindCount > 0)
942 {
943 BYTE vec[RDPUDP2_MAX_ACKVEC] = WINPR_C_ARRAY_INIT;
944 size_t count = 0;
945 for (size_t left = udp->v3BehindCount; left > 0;)
946 {
947 const size_t run = (left > 63) ? 63 : left;
948 vec[count++] = WINPR_ASSERTING_INT_CAST(BYTE, 0x80 | 0x40 | run);
949 left -= run;
950 }
951 udp->v3BehindCount = 0;
952 if (!v3_send_ack_vector(udp, udp->v3BehindBase, vec, count, udp->v3BehindArrival))
953 return FALSE;
954 if (!udp->v3WindowPending)
955 {
956 udp->ackPending = FALSE;
957 udp->v3UnackedPackets = 0;
958 udp->v3AckDue = 0;
959 return TRUE;
960 }
961 }
962
963 /* After a silence our position may be ahead of the peer's numbers (v3_resync_base): no ACK
964 * until its next AckOfAcks. Nothing stays due meanwhile, or the tunnel thread would spin. */
965 if (udp->v3Resync)
966 {
967 udp->ackPending = FALSE;
968 udp->v3UnackedPackets = 0;
969 udp->v3AckDue = 0;
970 return TRUE;
971 }
972
973 udp->ackPending = FALSE;
974 udp->v3WindowPending = FALSE;
975 udp->v3UnackedPackets = 0;
976 udp->v3AckDue = 0;
977
978 /* A cumulative ACK cannot describe a hole, the ACK vector reports the exact window so only
979 * what is missing gets resent. ACK and ACKVEC are mutually exclusive. */
980 const BOOL gap = v3_has_gap(udp);
981 udp->v3AckedGap = gap;
982 if (!gap)
983 {
984 wStream* s =
985 v3_packet_init(&sbuffer, buffer, sizeof(buffer), RDPUDP2_OVERHEADSIZE | RDPUDP2_ACK);
986 if (!s)
987 return FALSE;
988 v3_write_ack(udp, s, RDPUDP2_MAX_DELAYED_ACKS);
989 Stream_Write_UINT8(s, 10); /* OverheadSize */
990 return v3_send_packet(udp, s, RDPUDP2_PACKET_STANDARD);
991 }
992
993 const UINT16 base = udp->v3Expected;
994 size_t span = (size_t)seq16_diff(udp->v3Highest, base) + 1;
995 if (span > RDPUDP_RECEIVE_REACH)
996 span = RDPUDP_RECEIVE_REACH;
997
998 /* [MS-RDPEUDP2] 2.2.1.2.6 ACK vector payload. Runs of seven or more packets in the same
999 * state go in run-length mode (up to 63 per byte), the rest in 7 bit state maps: with state
1000 * maps only, 127 bytes cover 889 packets, ~70 ms behind a hole at 130 Mbit/s, and everything
1001 * past that would stay unacknowledged until the hole is filled. */
1002 BYTE vec[RDPUDP2_MAX_ACKVEC] = WINPR_C_ARRAY_INIT;
1003 size_t count = 0;
1004 size_t pos = 0;
1005 while ((pos < span) && (count < RDPUDP2_MAX_ACKVEC))
1006 {
1007 const BOOL state = v3_received(udp, seq16_add(base, pos));
1008 size_t run = 1;
1009 while ((pos + run < span) && (run < 63) &&
1010 (v3_received(udp, seq16_add(base, pos + run)) == state))
1011 run++;
1012 if (run >= 7)
1013 {
1014 vec[count++] = WINPR_ASSERTING_INT_CAST(BYTE, 0x80 | (state ? 0x40 : 0x00) | run);
1015 pos += run;
1016 continue;
1017 }
1018 BYTE bits = 0;
1019 for (size_t bit = 0; (bit < 7) && (pos + bit < span); bit++)
1020 {
1021 if (v3_received(udp, seq16_add(base, pos + bit)))
1022 bits |= WINPR_ASSERTING_INT_CAST(BYTE, 1 << bit);
1023 }
1024 vec[count++] = bits; /* state map, bit 0 first */
1025 pos += 7;
1026 }
1027
1028 /* when the highest sequence number received arrived */
1029 const UINT64 highest = v3_arrival(udp, udp->v3Highest);
1030 return v3_send_ack_vector(udp, base, vec, count, highest);
1031}
1032
1033/* The lowest data sequence number of ours still awaiting acknowledgement, which is what
1034 * AckOfAcks tells the peer ([MS-RDPEUDP2] 2.2.1.2.4). */
1035WINPR_ATTR_NODISCARD
1036static UINT32 sender_base(const rdpUdp* udp)
1037{
1038 WINPR_ASSERT(udp);
1039 if (udp->inFlight > 0)
1040 return udp->sendBase;
1041 return (udp->version == RDPUDP_PROTOCOL_VERSION_3) ? udp->nextCoded : udp->nextSource;
1042}
1043
1044/* Server: the keepalive while v3Resync holds back every ACK. Sending nothing would leave the
1045 * peer without a datagram: on an idle tunnel one lost keepalive starts the resync, the peer then
1046 * hears nothing for RDPUDP2_RESYNC_MS, stops acknowledging as well, and both ends wait for an
1047 * AckOfAcks that only data carries until the peer timeout ends the tunnel. An AckOfAcks alone
1048 * ([MS-RDPEUDP2] 2.2.1.2.4) names no position of the peer's, so it is safe after a silence, and
1049 * it ends the resync of a peer that waits for one. A FreeRDP peer answers each with an ACK
1050 * (v3_process_datagram). */
1051WINPR_ATTR_NODISCARD
1052static BOOL v3_send_base_keepalive(rdpUdp* udp)
1053{
1054 BYTE buffer[16] = WINPR_C_ARRAY_INIT;
1055 wStream sbuffer = WINPR_C_ARRAY_INIT;
1056
1057 WINPR_ASSERT(udp);
1058 wStream* s = v3_packet_init(&sbuffer, buffer, sizeof(buffer), RDPUDP2_AOA);
1059 if (!s)
1060 return FALSE;
1061 const UINT16 base = WINPR_ASSERTING_INT_CAST(UINT16, sender_base(udp));
1062 Stream_Write_UINT16(s, base); /* AckOfAcks */
1063 return v3_send_packet(udp, s, RDPUDP2_PACKET_STANDARD);
1064}
1065
1066/* The sequence number the next new or resent datagram gets */
1067WINPR_ATTR_NODISCARD
1068static UINT32 next_send_seq(const rdpUdp* udp)
1069{
1070 WINPR_ASSERT(udp);
1071 return (udp->version == RDPUDP_PROTOCOL_VERSION_3) ? udp->nextCoded : udp->nextSource;
1072}
1073
1074WINPR_ATTR_NODISCARD
1075static UINT32 send_seq_add(const rdpUdp* udp, UINT32 seq, size_t n)
1076{
1077 WINPR_ASSERT(udp);
1078 if (udp->version == RDPUDP_PROTOCOL_VERSION_3)
1079 return seq16_add(WINPR_ASSERTING_INT_CAST(UINT16, seq), n);
1080 return seq32_add(seq, WINPR_ASSERTING_INT_CAST(UINT32, n));
1081}
1082
1083WINPR_ATTR_NODISCARD
1084static rdpudp_pending* pending_find(rdpUdp* udp, UINT32 seq)
1085{
1086 WINPR_ASSERT(udp);
1087 const UINT16 pos = udp->pendingIndex[seq & 0xFFFF];
1088 if (pos == 0)
1089 return nullptr;
1090 WINPR_ASSERT(pos <= udp->inFlight);
1091 rdpudp_pending* p = &udp->pending[pos - 1];
1092 return (p->seq == seq) ? p : nullptr;
1093}
1094
1095/* Moves sendBase up to the oldest datagram still in flight. Every datagram in flight lies
1096 * between sendBase and the next sequence number, so this ends there at the latest. */
1097static void send_base_advance(rdpUdp* udp)
1098{
1099 WINPR_ASSERT(udp);
1100 if (udp->inFlight == 0)
1101 {
1102 udp->sendBase = next_send_seq(udp);
1103 return;
1104 }
1105 while (!pending_find(udp, udp->sendBase))
1106 {
1107 WINPR_ASSERT(udp->sendBase != next_send_seq(udp));
1108 udp->sendBase = send_seq_add(udp, udp->sendBase, 1);
1109 }
1110}
1111
1112/* Frees a datagram in flight; the last entry moves into its place. */
1113static void pending_remove(rdpUdp* udp, rdpudp_pending* p)
1114{
1115 WINPR_ASSERT(udp);
1116 WINPR_ASSERT(p);
1117 WINPR_ASSERT(udp->inFlight > 0);
1118 const size_t pos = WINPR_ASSERTING_INT_CAST(size_t, p - udp->pending);
1119 const size_t last = udp->inFlight - 1;
1120 WINPR_ASSERT(pos <= last);
1121 udp->pendingIndex[p->seq & 0xFFFF] = 0;
1122 pending_clear(p);
1123 if (pos != last)
1124 {
1125 *p = udp->pending[last];
1126 memset(&udp->pending[last], 0, sizeof(udp->pending[last]));
1127 udp->pendingIndex[p->seq & 0xFFFF] = WINPR_ASSERTING_INT_CAST(UINT16, pos + 1);
1128 }
1129 udp->inFlight = last;
1130 send_base_advance(udp);
1131}
1132
1133/* Earliest time run_timers has to look at the datagrams in flight again */
1134static void pending_due_at(rdpUdp* udp, UINT64 at)
1135{
1136 WINPR_ASSERT(udp);
1137 if (at < udp->pendingDue)
1138 udp->pendingDue = at;
1139}
1140
1141WINPR_ATTR_NODISCARD
1142static UINT64 report_resend_interval(const rdpUdp* udp);
1143
1144/* The earlier of due and the time run_timers has to act on this datagram */
1145WINPR_ATTR_NODISCARD
1146static UINT64 pending_next_due(const rdpUdp* udp, const rdpudp_pending* p, UINT64 due)
1147{
1148 WINPR_ASSERT(udp);
1149 WINPR_ASSERT(p);
1150 if (p->lost)
1151 return 0;
1152 if (p->due < due)
1153 due = p->due;
1154 if (p->reportHeld)
1155 {
1156 const UINT64 at = p->lastSent + report_resend_interval(udp);
1157 if (at < due)
1158 due = at;
1159 }
1160 return due;
1161}
1162
1163WINPR_ATTR_NODISCARD
1164static BOOL v3_send_data(rdpUdp* udp, const rdpudp_pending* p)
1165{
1166 BYTE buffer[RDPUDP_MTU + 32] = WINPR_C_ARRAY_INIT;
1167 wStream sbuffer = WINPR_C_ARRAY_INIT;
1168
1169 WINPR_ASSERT(udp);
1170 WINPR_ASSERT(p);
1171
1172 /* Not while packets behind the window wait for their ACK vector: the ACK here would clear
1173 * ackPending, and nothing would send the vector until the next standalone ACK. Not after a
1174 * silence either, our position may lie ahead of the peer's numbers then (v3_resync_base). */
1175 const BOOL withAck = udp->ackPending && udp->v3BaseKnown && !v3_has_gap(udp) &&
1176 (udp->v3BehindCount == 0) && !udp->v3Resync;
1177 const UINT16 base = WINPR_ASSERTING_INT_CAST(UINT16, sender_base(udp));
1178 /* Include AoA on retransmissions if the original announcement was lost, and in all data
1179 * until the peer acknowledges ([MS-RDPEUDP2] 3.1.5.3, matching Windows behavior). The peer
1180 * determines the sequence space start from the first AckOfAcks; otherwise, it uses the
1181 * first arriving datagram and assumes preceding packets were received. */
1182 const BOOL withAoA = !udp->aoaAnnounced || (udp->announcedAoA != base) ||
1183 (p->retransmits > 0) || !udp->peerAcked;
1184
1185 /* Windows sets NOACK on every DATA packet without an ACK */
1186 UINT16 flags = RDPUDP2_DATA | RDPUDP2_DELAYACKINFO;
1187 flags |= withAck ? RDPUDP2_ACK : RDPUDP2_NOACK;
1188 if (withAoA)
1189 flags |= RDPUDP2_AOA;
1190
1191 wStream* s = v3_packet_init(&sbuffer, buffer, sizeof(buffer), flags);
1192 if (!s)
1193 return FALSE;
1194 if (withAck)
1195 {
1196 /* arrival gaps only where the payload leaves room in the datagram */
1197 const size_t used = RDPUDP2_DATA_OVERHEAD + p->length;
1198 v3_write_ack(udp, s, (udp->mtu > used) ? udp->mtu - used : 0);
1199 /* left pending, a later ACK would repeat this position, which after a jump of the
1200 * peer's numbers names a packet it did not send */
1201 udp->ackPending = FALSE;
1202 udp->v3WindowPending = FALSE;
1203 udp->v3UnackedPackets = 0;
1204 udp->v3AckDue = 0;
1205 udp->v3AckedGap = FALSE;
1206 }
1207
1208 Stream_Write_UINT8(s, 1); /* MaxDelayedAcks */
1209 Stream_Write_UINT16(s, 20); /* DelayedAckTimeoutInMs */
1210
1211 if (withAoA)
1212 {
1213 /* AckOfAcks: the lowest sequence number still awaiting acknowledgement */
1214 Stream_Write_UINT16(s, base);
1215 udp->announcedAoA = base;
1216 udp->aoaAnnounced = TRUE;
1217 }
1218
1219 const UINT16 dataSeq = WINPR_ASSERTING_INT_CAST(UINT16, p->seq);
1220 Stream_Write_UINT16(s, dataSeq); /* DataSeqNum */
1221 Stream_Write_UINT16(s, p->channelSeq); /* ChannelSeqNum */
1222 if (!Stream_CheckAndLogRequiredCapacityWLog(udp->log, s, p->length))
1223 return FALSE;
1224 Stream_Write(s, p->payload, p->length);
1225
1226 return v3_send_packet(udp, s, RDPUDP2_PACKET_STANDARD);
1227}
1228
1229/* ------------------------------------------------------------------------------------------ */
1230/* [MS-RDPEUDP] version 1 and 2 encoding */
1231/* ------------------------------------------------------------------------------------------ */
1232
1233/* Writes an RDPUDP_ACK_VECTOR_HEADER covering the peer's packets up to the highest one seen,
1234 * using at most room bytes. Windows encodes a run of n datagrams as n - 1 in an element, so a run
1235 * holds 1 to 64 datagrams; before the peer's first data packet the vector is empty. */
1236static void v1_write_ack_vector(const rdpUdp* udp, wStream* s, size_t room)
1237{
1238 WINPR_ASSERT(udp);
1239 WINPR_ASSERT(s);
1240 if (!udp->receivedData)
1241 {
1242 Stream_Write_UINT16_BE(s, 0); /* uAckVectorSize */
1243 Stream_Zero(s, 2); /* pad to a DWORD boundary */
1244 return;
1245 }
1246
1247 const UINT32 highest = udp->recvHighest;
1248 UINT32 low = udp->ackVectorStart;
1249 if (seq32_after(low, highest))
1250 low = highest;
1251 if (seq32_diff(highest, low) >= RDPUDP_RECEIVE_REACH)
1252 low = seq32_diff(highest, RDPUDP_RECEIVE_REACH - 1);
1253
1254 BYTE elements[RDPUDP_MAX_ACK_VECTOR] = WINPR_C_ARRAY_INIT;
1255 size_t count = 0;
1256 BYTE state = 0xFF;
1257 BYTE run = 0;
1258
1259 UINT32 seq = low;
1260 while (TRUE)
1261 {
1262 BYTE current = DATAGRAM_RECEIVED;
1263 if (!seq32_after(udp->recvNext, seq))
1264 {
1265 const rdpudp_slot* slot = &udp->slots[seq % RDPUDP_RECEIVE_SLOTS];
1266 if (!slot->present || (slot->seq != seq))
1267 current = DATAGRAM_NOT_YET_RECEIVED;
1268 }
1269
1270 if ((current != state) || (run == 0x40))
1271 {
1272 if (run > 0)
1273 {
1274 if (count >= ARRAYSIZE(elements))
1275 break;
1276 elements[count++] = WINPR_ASSERTING_INT_CAST(BYTE, (state << 6) | (run - 1));
1277 }
1278 state = current;
1279 run = 0;
1280 }
1281 run++;
1282
1283 if (seq == highest)
1284 break;
1285 seq++;
1286 }
1287 if ((run > 0) && (count < ARRAYSIZE(elements)))
1288 elements[count++] = WINPR_ASSERTING_INT_CAST(BYTE, (state << 6) | (run - 1));
1289
1290 /* Too long to fit: keep the newest part, the older part has been reported before. */
1291 size_t skip = 0;
1292 while ((2 + (count - skip) + 3) > room && (skip < count))
1293 skip++;
1294 count -= skip;
1295
1296 const UINT16 vectorSize = WINPR_ASSERTING_INT_CAST(UINT16, count);
1297 Stream_Write_UINT16_BE(s, vectorSize); /* uAckVectorSize */
1298 Stream_Write(s, &elements[skip], count); /* AckVector */
1299 Stream_Zero(s, (4 - ((2 + count) % 4)) % 4); /* pad to a DWORD boundary */
1300}
1301
1302WINPR_ATTR_NODISCARD
1303static UINT32 v1_cumulative_ack(const rdpUdp* udp)
1304{
1305 WINPR_ASSERT(udp);
1306 return seq32_diff(sender_base(udp), 1);
1307}
1308
1309WINPR_ATTR_NODISCARD
1310static BOOL v1_send_packet(rdpUdp* udp, const rdpudp_pending* p)
1311{
1312 BYTE buffer[RDPUDP_MTU + 2048] = WINPR_C_ARRAY_INIT;
1313 wStream sbuffer = WINPR_C_ARRAY_INIT;
1314
1315 WINPR_ASSERT(udp);
1316 WINPR_ASSERT(udp->mtu >= RDPUDP_MIN_MTU);
1317
1318 const UINT32 cumulative = v1_cumulative_ack(udp);
1319 const BOOL withAoA = !udp->aoaAnnounced || (udp->announcedAoA != cumulative);
1320
1321 UINT16 flags = RDPUDP_FLAG_ACK;
1322 if (withAoA)
1323 flags |= RDPUDP_FLAG_ACK_OF_ACKS;
1324 if (p)
1325 {
1326 flags |= RDPUDP_FLAG_DATA;
1327 if (udp->cwrPending)
1328 {
1329 flags |= RDPUDP_FLAG_CWR;
1330 udp->cwrPending = FALSE;
1331 }
1332 }
1333
1334 wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
1335 if (!s)
1336 return FALSE;
1337
1338 /* RDPUDP_FEC_HEADER */
1339 Stream_Write_UINT32_BE(s, udp->receivedData ? udp->recvHighest
1340 : udp->peerInitialSequence); /* snSourceAck */
1341 Stream_Write_UINT16_BE(s, RDPUDP_RECEIVE_WINDOW); /* uReceiveWindowSize */
1342 Stream_Write_UINT16_BE(s, flags); /* uFlags */
1343
1344 /* an ACK-only datagram must stay within the MTU too: header, vector, AckOfAcks */
1345 v1_write_ack_vector(udp, s, p ? RDPUDP_ACK_ROOM - 4 : udp->mtu - 12u);
1346 udp->ackPending = FALSE;
1347
1348 if (withAoA)
1349 {
1350 Stream_Write_UINT32_BE(s, cumulative); /* snResetSeqNum */
1351 udp->announcedAoA = cumulative;
1352 udp->aoaAnnounced = TRUE;
1353 }
1354
1355 if (p)
1356 {
1357 /* RDPUDP_SOURCE_PAYLOAD_HEADER, a retransmission gets a new coded sequence number */
1358 const UINT32 snCoded = udp->nextCoded++;
1359 Stream_Write_UINT32_BE(s, snCoded); /* snCoded */
1360 Stream_Write_UINT32_BE(s, p->seq); /* snSourceStart */
1361 if (!Stream_CheckAndLogRequiredCapacityWLog(udp->log, s, p->length))
1362 return FALSE;
1363 Stream_Write(s, p->payload, p->length);
1364 }
1365
1366 return rdpudp_send_raw(udp, Stream_Buffer(s), Stream_GetPosition(s));
1367}
1368
1369WINPR_ATTR_NODISCARD
1370static BOOL rdpudp_send_ack(rdpUdp* udp)
1371{
1372 WINPR_ASSERT(udp);
1373 if (udp->state != RDPUDP_STATE_ESTABLISHED)
1374 return TRUE;
1375 if (udp->version == RDPUDP_PROTOCOL_VERSION_3)
1376 return v3_send_ack(udp);
1377 return v1_send_packet(udp, nullptr);
1378}
1379
1380/* [MS-RDPEUDP2] 3.1.5.2 and 4.2: a receiver acknowledges once MaxDelayedAcks + 1 data packets
1381 * wait, the most one ACK can describe, and at once when a packet goes missing or a missing one
1382 * turns up. Checked after every datagram, so a burst is acknowledged while it is still arriving
1383 * rather than when it ended. Fewer packets wait for ack_when_due. */
1384WINPR_ATTR_NODISCARD
1385static BOOL v3_ack_if_due(rdpUdp* udp)
1386{
1387 WINPR_ASSERT(udp);
1388 if ((udp->version != RDPUDP_PROTOCOL_VERSION_3) || (udp->state != RDPUDP_STATE_ESTABLISHED) ||
1389 !udp->ackPending)
1390 return TRUE;
1391 if ((v3_has_gap(udp) == udp->v3AckedGap) && (udp->v3UnackedPackets <= udp->v3MaxDelayedAcks))
1392 return TRUE;
1393 return v3_send_ack(udp);
1394}
1395
1396/* [MS-RDPEUDP2] 3.1.5.2: half the round trip time until the peer's DelayAckInfo says otherwise */
1397WINPR_ATTR_NODISCARD
1398static UINT32 v3_ack_delay(const rdpUdp* udp)
1399{
1400 WINPR_ASSERT(udp);
1401 return (udp->v3DelayedAckTimeout != UINT32_MAX) ? udp->v3DelayedAckTimeout : udp->srtt / 2;
1402}
1403
1404/* Runs at the end of every receive round. Versions 1 and 2 acknowledge everything that arrived in
1405 * one go, unless a data packet carried the ACK already. [MS-RDPEUDP2] 2.2.1.2.3: version 3 lets
1406 * an acknowledgement wait DelayedAckTimeoutInMs, so that one ACK covers what arrives meanwhile.
1407 * A receive round can be a single datagram, a burst has not ended with it. */
1408WINPR_ATTR_NODISCARD
1409static BOOL ack_when_due(rdpUdp* udp)
1410{
1411 WINPR_ASSERT(udp);
1412 if (!udp->ackPending)
1413 return TRUE;
1414 if (udp->version == RDPUDP_PROTOCOL_VERSION_3)
1415 {
1416 if ((udp->state != RDPUDP_STATE_ESTABLISHED) || !udp->v3BaseKnown)
1417 return TRUE;
1418 const UINT64 now = now_ms(udp);
1419 if (udp->v3AckDue == 0)
1420 udp->v3AckDue = now + v3_ack_delay(udp);
1421 if (now < udp->v3AckDue)
1422 return TRUE;
1423 }
1424 return rdpudp_send_ack(udp);
1425}
1426
1427WINPR_ATTR_NODISCARD
1428static BOOL rdpudp_transmit(rdpUdp* udp, const rdpudp_pending* p)
1429{
1430 WINPR_ASSERT(udp);
1431 WINPR_ASSERT(p);
1432 if (udp->version == RDPUDP_PROTOCOL_VERSION_3)
1433 return v3_send_data(udp, p);
1434 return v1_send_packet(udp, p);
1435}
1436
1437/* ------------------------------------------------------------------------------------------ */
1438/* Sending */
1439/* ------------------------------------------------------------------------------------------ */
1440
1441WINPR_ATTR_NODISCARD
1442static size_t window_limit(const rdpUdp* udp)
1443{
1444 WINPR_ASSERT(udp);
1445 if (udp->server)
1446 return (udp->cwnd < RDPUDP_MAX_IN_FLIGHT) ? udp->cwnd : RDPUDP_MAX_IN_FLIGHT;
1447 size_t limit = RDPUDP_CLIENT_MAX_IN_FLIGHT;
1448 if ((udp->version != RDPUDP_PROTOCOL_VERSION_3) && (udp->peerWindow > 0) &&
1449 (udp->peerWindow < limit))
1450 limit = udp->peerWindow;
1451 return limit;
1452}
1453
1454/* Takes ownership of data, also when it fails. */
1455WINPR_ATTR_NODISCARD
1456static BOOL send_chunk_now(rdpUdp* udp, BYTE* data, size_t length)
1457{
1458 WINPR_ASSERT(udp);
1459 WINPR_ASSERT(data);
1460 if ((udp->inFlight >= RDPUDP_MAX_IN_FLIGHT) ||
1461 (udp->pendingIndex[next_send_seq(udp) & 0xFFFF] != 0))
1462 {
1463 free(data);
1464 return FALSE;
1465 }
1466 rdpudp_pending* p = &udp->pending[udp->inFlight];
1467
1468 /* the pending slot owns the payload from here, pending_clear frees it */
1469 p->payload = data;
1470 p->length = length;
1471 if (udp->version == RDPUDP_PROTOCOL_VERSION_3)
1472 {
1473 const UINT16 coded = WINPR_ASSERTING_INT_CAST(UINT16, udp->nextCoded);
1474 p->seq = coded;
1475 udp->nextCoded = seq16_add(coded, 1);
1476 p->channelSeq = udp->nextChannelSeq;
1477 udp->nextChannelSeq = channel_seq_next(udp->nextChannelSeq);
1478 }
1479 else
1480 {
1481 p->seq = udp->nextSource++;
1482 }
1483
1484 const UINT64 now = now_ms(udp);
1485 p->sent = now;
1486 if (udp->server)
1487 p->lastSent = now;
1488 p->rto = rto_initial(udp);
1489 p->due = now + p->rto;
1490 p->retransmits = 0;
1491 p->timeouts = 0;
1492 if (udp->inFlight == 0)
1493 udp->sendBase = p->seq;
1494 udp->inFlight++;
1495 udp->pendingIndex[p->seq & 0xFFFF] = WINPR_ASSERTING_INT_CAST(UINT16, udp->inFlight);
1496 pending_due_at(udp, p->due);
1497
1498 return rdpudp_transmit(udp, p);
1499}
1500
1501/* Check if another datagram can be sent without exceeding RDPUDP2_MAX_SPAN from the
1502 * oldest unacked packet, and without taking the index entry of one still in flight. */
1503WINPR_ATTR_NODISCARD
1504static BOOL span_has_room(const rdpUdp* udp)
1505{
1506 WINPR_ASSERT(udp);
1507 if (udp->inFlight == 0)
1508 return TRUE;
1509 if (udp->pendingIndex[next_send_seq(udp) & 0xFFFF] != 0)
1510 return FALSE;
1511 if (!udp->server)
1512 return TRUE;
1513 const UINT16 base = WINPR_ASSERTING_INT_CAST(UINT16, sender_base(udp));
1514 return seq16_diff(WINPR_ASSERTING_INT_CAST(UINT16, udp->nextCoded), base) < RDPUDP2_MAX_SPAN;
1515}
1516
1517WINPR_ATTR_NODISCARD
1518static BOOL flush_send_queue(rdpUdp* udp)
1519{
1520 WINPR_ASSERT(udp);
1521 while ((udp->state == RDPUDP_STATE_ESTABLISHED) && (udp->inFlight < window_limit(udp)) &&
1522 (Queue_Count(udp->sendQueue) > 0) && span_has_room(udp))
1523 {
1524 rdpudp_chunk* chunk = Queue_Dequeue(udp->sendQueue);
1525 if (!chunk)
1526 break;
1527 BYTE* data = chunk->data;
1528 const size_t length = chunk->length;
1529 free(chunk);
1530 if (!send_chunk_now(udp, data, length))
1531 return FALSE;
1532 }
1533 return TRUE;
1534}
1535
1536BOOL rdpudp_send(rdpUdp* udp, const BYTE* data, size_t length)
1537{
1538 WINPR_ASSERT(udp);
1539 WINPR_ASSERT(data);
1540
1541 if ((length == 0) || (length > rdpudp_get_max_payload(udp)))
1542 return FALSE;
1543 if ((udp->state != RDPUDP_STATE_ESTABLISHED) && (udp->state != RDPUDP_STATE_SYN_SENT))
1544 return FALSE;
1545 rdpudp_chunk* chunk = calloc(1, sizeof(rdpudp_chunk));
1546 if (!chunk)
1547 return FALSE;
1548 chunk->data = malloc(length);
1549 if (!chunk->data)
1550 {
1551 free(chunk);
1552 return FALSE;
1553 }
1554 memcpy(chunk->data, data, length);
1555 chunk->length = length;
1556
1557 if (!Queue_Enqueue(udp->sendQueue, chunk))
1558 {
1559 chunk_free(chunk);
1560 return FALSE;
1561 }
1562 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): Queue_Enqueue takes ownership of chunk
1563 return flush_send_queue(udp);
1564}
1565
1566static void sample_rtt(rdpUdp* udp, const rdpudp_pending* p)
1567{
1568 WINPR_ASSERT(udp);
1569 WINPR_ASSERT(p);
1570 /* Karn's algorithm states that ACKs for retransmitted packets are ambiguous if the sequence
1571 * number is reused. Version 3 servers use new sequence numbers for retransmissions, so the
1572 * ACK corresponds to the most recent transmission. This ensures that round trips exceeding
1573 * the timeout are still measured rather than being ignored. */
1574 const BOOL lastIsKnown = udp->server && (udp->version == RDPUDP_PROTOCOL_VERSION_3);
1575 if ((p->retransmits > 0) && !lastIsKnown)
1576 return;
1577 const UINT64 sentAt = (p->retransmits > 0) ? p->lastSent : p->sent;
1578 const UINT64 sample64 = now_ms(udp) - sentAt;
1579 const UINT32 sample =
1580 (sample64 > UINT32_MAX) ? UINT32_MAX : WINPR_ASSERTING_INT_CAST(UINT32, sample64);
1581 if (!udp->server)
1582 {
1583 udp->srtt = (udp->srtt == 0)
1584 ? sample
1585 : WINPR_ASSERTING_INT_CAST(UINT32, ((UINT64)udp->srtt * 7 + sample) / 8);
1586 return;
1587 }
1588 udp->rtoBackoff = 0;
1589 if (sample < udp->ccRoundRttMin)
1590 udp->ccRoundRttMin = sample;
1591 if (sample < udp->rttMin)
1592 {
1593 /* The first sample, taken during the handshake, encountered no queue. */
1594 if (udp->rttMin == UINT32_MAX)
1595 udp->rttMinSeen = now_ms(udp);
1596 udp->rttMin = sample;
1597 }
1598 /* A sample at the minimum confirms it is not too low. If the sender's queue is empty,
1599 * it also confirms that the minimum is not too high. */
1600 if ((sample <= udp->rttMin) && (Queue_Count(udp->sendQueue) == 0))
1601 udp->rttMinSeen = now_ms(udp);
1602 if (!udp->rttSampled)
1603 {
1604 /* Initialize SRTT and RTTVAR according to RFC 6298, Section 2.2 */
1605 udp->srtt = sample;
1606 udp->rttvar = sample / 2;
1607 udp->rttSampled = TRUE;
1608 return;
1609 }
1610 /* [RFC6298] 2.3: Update the RTT variation first, using the previous smoothed RTT. */
1611 const UINT32 delta = (udp->srtt > sample) ? udp->srtt - sample : sample - udp->srtt;
1612 udp->rttvar = WINPR_ASSERTING_INT_CAST(UINT32, ((UINT64)udp->rttvar * 3 + delta) / 4);
1613 udp->srtt = WINPR_ASSERTING_INT_CAST(UINT32, ((UINT64)udp->srtt * 7 + sample) / 8);
1614}
1615
1616/* Check if smoothed RTT indicates queue buildup on the path. */
1617WINPR_ATTR_NODISCARD
1618static BOOL cc_queue_is_building(const rdpUdp* udp)
1619{
1620 WINPR_ASSERT(udp);
1621 if (!udp->rttSampled || (udp->rttMin == UINT32_MAX))
1622 return FALSE;
1623 /* Use variation to widen the allowance, as jitter can cause samples to drop far below
1624 * the smoothed value. */
1625 UINT64 allowance =
1626 (udp->rttMin / 2 > RDPUDP2_QUEUE_DELAY_MS) ? udp->rttMin / 2 : RDPUDP2_QUEUE_DELAY_MS;
1627 if (4ull * udp->rttvar > allowance)
1628 allowance = 4ull * udp->rttvar;
1629 return udp->srtt > udp->rttMin + allowance;
1630}
1631
1632/* Called for a round that is cut for delay alone. A sender-built queue evolves after a cut:
1633 * it grows from datagrams sent before the cut, then drains. If the lowest round trip
1634 * remains stable (within RDPUDP2_DELAY_REBASE_PERCENT) for RDPUDP2_DELAY_REBASE_CUTS
1635 * consecutive verdicts, the path itself has slowed (e.g., new route, VPN, handover).
1636 * The current lowest round trip becomes the new minimum, the window regrows via slow
1637 * start as on a new path, and the round is not cut.
1638 * @return TRUE to cut the window for this round */
1639WINPR_ATTR_NODISCARD
1640static BOOL cc_delay_is_ours(rdpUdp* udp)
1641{
1642 WINPR_ASSERT(udp);
1643 if (udp->ccRoundRttMin == UINT32_MAX)
1644 return TRUE;
1645 const UINT64 now = udp->ccRoundRttMin;
1646 const UINT64 last = udp->ccDelayLastRtt;
1647 udp->ccDelayLastRtt = udp->ccRoundRttMin;
1648 const BOOL flat = (last > 0) && (100ull * now >= RDPUDP2_DELAY_REBASE_PERCENT * last) &&
1649 (100ull * now <= (200ull - RDPUDP2_DELAY_REBASE_PERCENT) * last);
1650 if (!flat)
1651 {
1652 udp->ccDelayCuts = 0;
1653 return TRUE;
1654 }
1655 if (++udp->ccDelayCuts < RDPUDP2_DELAY_REBASE_CUTS)
1656 return TRUE;
1657 WLog_Print(udp->log, WLOG_DEBUG,
1658 "round trip stayed at %" PRIu32 " ms after %" PRIu32 " cuts (lowest was %" PRIu32
1659 " ms): new base delay, slow start from a window of %" PRIu32,
1660 udp->ccRoundRttMin, RDPUDP2_DELAY_REBASE_CUTS, udp->rttMin, udp->cwnd);
1661 udp->rttMin = udp->ccRoundRttMin;
1662 udp->rttMinSeen = now_ms(udp);
1663 udp->ssthresh = RDPUDP_MAX_IN_FLIGHT;
1664 udp->ccDelayCuts = 0;
1665 udp->ccDelayLastRtt = 0;
1666 return FALSE;
1667}
1668
1669/* The drain probe: if no sample reaches the minimum RTT for RDPUDP2_RTT_PROBE_MS, one round
1670 * with the smallest window drains the sender's queue. ACKs from the next round are for
1671 * datagrams sent into the empty queue, and their lowest round trip is used to update the
1672 * minimum RTT. The window is then restored. Neither round triggers a congestion verdict.
1673 * @return TRUE if this round belongs to a probe */
1674WINPR_ATTR_NODISCARD
1675static BOOL cc_probe(rdpUdp* udp, UINT64 now)
1676{
1677 WINPR_ASSERT(udp);
1678 switch (udp->ccProbe)
1679 {
1680 case 1:
1681 udp->ccProbe = 2;
1682 return TRUE;
1683 case 2:
1684 if (udp->ccRoundRttMin != UINT32_MAX)
1685 {
1686 WLog_Print(udp->log, WLOG_DEBUG,
1687 "drain probe: lowest round trip %" PRIu32 " ms (was %" PRIu32
1688 " ms), congestion window back to %" PRIu32,
1689 udp->ccRoundRttMin, udp->rttMin, udp->ccProbeCwnd);
1690 udp->rttMin = udp->ccRoundRttMin;
1691 }
1692 else
1693 WLog_Print(
1694 udp->log, WLOG_DEBUG,
1695 "drain probe: no round trip measured, congestion window back to %" PRIu32,
1696 udp->ccProbeCwnd);
1697 udp->rttMinSeen = now;
1698 udp->cwnd = udp->ccProbeCwnd;
1699 udp->ccProbe = 0;
1700 return TRUE;
1701 default:
1702 if ((udp->rttMin == UINT32_MAX) || (now - udp->rttMinSeen < RDPUDP2_RTT_PROBE_MS))
1703 return FALSE;
1704 udp->ccProbeCwnd = udp->cwnd;
1705 udp->cwnd = RDPUDP2_MIN_CWND;
1706 udp->ccProbe = 1;
1707 udp->ccAcked = 0;
1708 udp->ccDelayCuts = 0;
1709 udp->ccDelayLastRtt = 0;
1710 udp->ccDelayHold = FALSE;
1711 return TRUE;
1712 }
1713}
1714
1715/* Initialize the state for the next congestion control round. */
1716static void cc_round_reset(rdpUdp* udp, UINT64 now)
1717{
1718 WINPR_ASSERT(udp);
1719 udp->ccRoundStart = now;
1720 udp->ccRoundAcked = 0;
1721 udp->ccRoundLost = 0;
1722 udp->ccRoundRttMin = UINT32_MAX;
1723}
1724
1725/* End the current round after one smoothed RTT; shrink window to 70% if congestion was
1726 * detected. */
1727static void cc_round(rdpUdp* udp)
1728{
1729 WINPR_ASSERT(udp);
1730 const UINT64 now = now_ms(udp);
1731 const UINT32 rtt = (udp->srtt > 0) ? udp->srtt : rto_floor(udp);
1732 if (udp->ccRoundStart == 0)
1733 udp->ccRoundStart = now;
1734 if (now - udp->ccRoundStart < rtt)
1735 return;
1736 if (cc_probe(udp, now))
1737 {
1738 cc_round_reset(udp, now);
1739 return;
1740 }
1741
1742 /* Use 64-bit sum to prevent overflow. */
1743 const UINT64 total = (UINT64)udp->ccRoundAcked + udp->ccRoundLost;
1744 BOOL lossy = FALSE;
1745 if (total >= RDPUDP2_ROUND_MIN_SAMPLES)
1746 {
1747 /* Congested only when loss is high and rose above the smoothed loss of earlier
1748 * rounds: a steady background loss is repaired, not a reason to slow down. */
1749 const UINT32 loss = WINPR_ASSERTING_INT_CAST(UINT32, udp->ccRoundLost * 1000ull / total);
1750 lossy = (loss >= RDPUDP2_CONGESTED_LOSS_PERCENT * 10u) &&
1751 (loss >= udp->ccLossAvg + RDPUDP2_LOSS_RISE_PERCENT * 10u);
1752 const INT64 delta = (INT64)loss - (INT64)udp->ccLossAvg;
1753 udp->ccLossAvg = WINPR_ASSERTING_INT_CAST(
1754 UINT32, (INT64)udp->ccLossAvg + delta / (1 << RDPUDP2_LOSS_AVG_SHIFT));
1755 }
1756 BOOL queued = cc_queue_is_building(udp);
1757 if (queued && !lossy)
1758 {
1759 /* Samples in the round after a window cut come from datagrams sent before the cut;
1760 * re-evaluate delay only once they reflect the effect of the reduction. */
1761 if (udp->ccDelayHold)
1762 queued = FALSE;
1763 else
1764 queued = cc_delay_is_ours(udp);
1765 }
1766 else
1767 {
1768 udp->ccDelayCuts = 0;
1769 udp->ccDelayLastRtt = 0;
1770 }
1771 udp->ccDelayHold = FALSE;
1772 if (lossy || queued)
1773 {
1774 const UINT32 reduced = WINPR_ASSERTING_INT_CAST(UINT32, udp->cwnd * 7ull / 10);
1775 udp->ssthresh = (reduced > RDPUDP2_MIN_CWND) ? reduced : RDPUDP2_MIN_CWND;
1776 udp->cwnd = udp->ssthresh;
1777 udp->ccAcked = 0;
1778 udp->ccDelayHold = TRUE;
1779 WLog_Print(udp->log, WLOG_DEBUG,
1780 "congestion (%" PRIu32 " of %" PRIu64 " lost, round trip %" PRIu32
1781 " ms, lowest %" PRIu32 " ms), congestion window %" PRIu32,
1782 udp->ccRoundLost, total, udp->srtt, udp->rttMin, udp->cwnd);
1783 }
1784 if ((udp->reorderThreshold > RDPUDP2_REORDER_THRESHOLD) &&
1785 (++udp->reorderCalmRounds >= RDPUDP2_REORDER_DECAY_ROUNDS))
1786 {
1787 udp->reorderThreshold--;
1788 udp->reorderCalmRounds = 0;
1789 }
1790 cc_round_reset(udp, now);
1791}
1792
1793static void cc_on_ack(rdpUdp* udp)
1794{
1795 WINPR_ASSERT(udp);
1796 udp->ccRoundAcked++;
1797 if (udp->ccProbe != 0)
1798 {
1799 /* Keep the congestion window small until the probe completes. */
1800 }
1801 else if (udp->cwnd < udp->ssthresh)
1802 udp->cwnd++;
1803 else if (!cc_queue_is_building(udp) && (++udp->ccAcked >= RDPUDP2_GROWTH_ACKS))
1804 {
1805 udp->ccAcked = 0;
1806 udp->cwnd++;
1807 }
1808 if (udp->cwnd > RDPUDP_MAX_IN_FLIGHT)
1809 udp->cwnd = RDPUDP_MAX_IN_FLIGHT;
1810 cc_round(udp);
1811}
1812
1813/* Record a missing or timed-out datagram for the current round. */
1814static void cc_on_loss(rdpUdp* udp)
1815{
1816 WINPR_ASSERT(udp);
1817 udp->ccRoundLost++;
1818 cc_round(udp);
1819}
1820
1821/* Server: duration a chunk is exempt from reports after sending, and the duration of the
1822 * report-driven retransmit budget. */
1823WINPR_ATTR_NODISCARD
1824static UINT64 report_resend_interval(const rdpUdp* udp)
1825{
1826 WINPR_ASSERT(udp);
1827 return (udp->srtt > RDPUDP2_REPORT_MIN_ROUND_MS) ? udp->srtt : RDPUDP2_REPORT_MIN_ROUND_MS;
1828}
1829
1830/* Server: mark reported chunk as lost if the round trip budget allows; otherwise, keep
1831 * pending for the next vector or timer. Only marking triggers cc_on_loss, ensuring loss
1832 * is counted once per retransmit. Held-back chunks are counted when acted upon;
1833 * duplicate reports are ignored. */
1834static void handle_reported_loss(rdpUdp* udp, rdpudp_pending* p)
1835{
1836 WINPR_ASSERT(udp);
1837 WINPR_ASSERT(p);
1838 const UINT64 now = now_ms(udp);
1839 if (now - udp->reportRoundStart >= report_resend_interval(udp))
1840 {
1841 udp->reportRoundStart = now;
1842 udp->reportRoundUsed = 0;
1843 }
1844 const UINT32 share = udp->cwnd / 4;
1845 const UINT32 budget = (share > RDPUDP2_REPORT_MIN_BUDGET) ? share : RDPUDP2_REPORT_MIN_BUDGET;
1846 p->reportHeld = FALSE;
1847 if (udp->reportRoundUsed >= budget)
1848 return;
1849 udp->reportRoundUsed++;
1850 p->lost = TRUE;
1851 pending_due_at(udp, 0);
1852 cc_on_loss(udp);
1853}
1854
1855static void acknowledge(rdpUdp* udp, rdpudp_pending* p)
1856{
1857 WINPR_ASSERT(udp);
1858 WINPR_ASSERT(p);
1859 sample_rtt(udp, p);
1860 pending_remove(udp, p);
1861 udp->peerAcked = TRUE;
1862 if (udp->server)
1863 cc_on_ack(udp);
1864}
1865
1866static void acknowledge_seq(rdpUdp* udp, UINT32 seq)
1867{
1868 WINPR_ASSERT(udp);
1869 rdpudp_pending* p = pending_find(udp, seq);
1870 if (p)
1871 acknowledge(udp, p);
1872}
1873
1874/* ------------------------------------------------------------------------------------------ */
1875/* Receiving */
1876/* ------------------------------------------------------------------------------------------ */
1877
1878WINPR_ATTR_NODISCARD
1879static BOOL deliver(rdpUdp* udp, const BYTE* data, size_t length)
1880{
1881 WINPR_ASSERT(udp);
1882 udp->lastDelivery = now_ms(udp);
1883 if (length == 0)
1884 return TRUE;
1885 WINPR_ASSERT(data);
1886 WINPR_ASSERT(udp->receive);
1887 if (!udp->receive(udp->custom, data, length))
1888 {
1889 rdpudp_fail(udp, "receiver rejected data");
1890 return FALSE;
1891 }
1892 return TRUE;
1893}
1894
1895WINPR_ATTR_NODISCARD
1896static BOOL store_slot(rdpUdp* udp, UINT32 seq, const BYTE* data, size_t length)
1897{
1898 WINPR_ASSERT(udp);
1899 WINPR_ASSERT(data || (length == 0));
1900 rdpudp_slot* slot = &udp->slots[seq % RDPUDP_RECEIVE_SLOTS];
1901 if (slot->present)
1902 {
1903 if (slot->seq == seq)
1904 return TRUE; /* duplicate */
1905 slot_clear(slot);
1906 udp->buffered--;
1907 }
1908
1909 slot->data = nullptr;
1910 if (length > 0)
1911 {
1912 slot->data = malloc(length);
1913 if (!slot->data)
1914 return FALSE;
1915 memcpy(slot->data, data, length);
1916 }
1917 slot->length = length;
1918 slot->seq = seq;
1919 slot->present = TRUE;
1920 if (udp->buffered++ == 0)
1921 udp->bufferingSince = now_ms(udp);
1922 return TRUE;
1923}
1924
1925WINPR_ATTR_NODISCARD
1926static BOOL v1_receive_source(rdpUdp* udp, UINT32 seq, const BYTE* data, size_t length)
1927{
1928 WINPR_ASSERT(udp);
1929 WINPR_ASSERT(data || (length == 0));
1930 if (!udp->receivedData)
1931 {
1932 /* [MS-RDPEUDP] puts the first source packet at the initial sequence number plus one,
1933 * an implementation that uses the initial sequence number itself is accepted too. */
1934 if (seq == udp->peerInitialSequence)
1935 {
1936 udp->recvNext = seq;
1937 udp->ackVectorStart = seq;
1938 }
1939 udp->recvHighest = seq32_diff(udp->recvNext, 1);
1940 }
1941
1942 udp->ackPending = TRUE;
1943
1944 if (seq32_after(udp->recvNext, seq))
1945 return TRUE; /* already delivered, the ACK above tells the peer again */
1946
1947 const UINT32 ahead = seq32_diff(seq, udp->recvNext);
1948 if (ahead >= RDPUDP_RECEIVE_REACH)
1949 return TRUE;
1950
1951 udp->receivedData = TRUE;
1952 if (seq32_after(seq, udp->recvHighest))
1953 udp->recvHighest = seq;
1954
1955 if (!store_slot(udp, seq, data, length))
1956 return FALSE;
1957
1958 while (TRUE)
1959 {
1960 rdpudp_slot* slot = &udp->slots[udp->recvNext % RDPUDP_RECEIVE_SLOTS];
1961 if (!slot->present || (slot->seq != udp->recvNext))
1962 break;
1963 BYTE* chunk = slot->data;
1964 const size_t chunkLength = slot->length;
1965 slot->data = nullptr;
1966 slot->present = FALSE;
1967 udp->buffered--;
1968 udp->recvNext++;
1969 const BOOL rc = deliver(udp, chunk, chunkLength);
1970 free(chunk);
1971 if (!rc)
1972 return FALSE;
1973 }
1974 return TRUE;
1975}
1976
1977/* Applies the peer's ACK vector, which ends at its snSourceAck, to our own packets. */
1978static void v1_process_ack_vector(rdpUdp* udp, UINT32 snSourceAck, wStream* s, size_t count)
1979{
1980 WINPR_ASSERT(udp);
1981 WINPR_ASSERT(s);
1982 /* count comes from the wire, v1_process_datagram bounds it */
1983 WINPR_ASSERT(count <= RDPUDP_MAX_ACK_VECTOR);
1984 const size_t start = Stream_GetPosition(s);
1985 UINT32 total = 0;
1986 for (size_t x = 0; x < count; x++)
1987 total += (Stream_Get_UINT8(s) & 0x3Fu) + 1u; /* a run of n datagrams is sent as n - 1 */
1988 if ((total == 0) || !Stream_SetPosition(s, start))
1989 return;
1990
1991 UINT32 seq = seq32_add(seq32_diff(snSourceAck, total), 1);
1992 for (size_t x = 0; x < count; x++)
1993 {
1994 const BYTE element = Stream_Get_UINT8(s);
1995 const BYTE state = WINPR_ASSERTING_INT_CAST(BYTE, element >> 6);
1996 const BYTE run = WINPR_ASSERTING_INT_CAST(BYTE, (element & 0x3F) + 1);
1997 for (BYTE y = 0; y < run; y++)
1998 {
1999 if (state == DATAGRAM_RECEIVED)
2000 acknowledge_seq(udp, seq);
2001 seq++;
2002 }
2003 }
2004}
2005
2006WINPR_ATTR_NODISCARD
2007static BOOL v1_process_datagram(rdpUdp* udp, const BYTE* data, size_t length)
2008{
2009 wStream sbuffer = WINPR_C_ARRAY_INIT;
2010 WINPR_ASSERT(udp);
2011 WINPR_ASSERT(data);
2012 wStream* s = Stream_StaticConstInit(&sbuffer, data, length);
2013
2014 /* RDPUDP_FEC_HEADER */
2015 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 8))
2016 return TRUE;
2017 const UINT32 snSourceAck = Stream_Get_UINT32_BE(s);
2018 const UINT16 window = Stream_Get_UINT16_BE(s);
2019 const UINT16 flags = Stream_Get_UINT16_BE(s);
2020
2021 if (flags & RDPUDP_FLAG_SYN)
2022 {
2023 /* a repeated SYN+ACK: our acknowledgement of it got lost */
2024 udp->ackPending = TRUE;
2025 return TRUE;
2026 }
2027
2028 udp->peerWindow = window;
2029 if (flags & RDPUDP_FLAG_CN)
2030 udp->cwrPending = TRUE;
2031
2032 if (flags & RDPUDP_FLAG_ACK)
2033 {
2034 /* RDPUDP_ACK_VECTOR_HEADER */
2035 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 2))
2036 return TRUE;
2037 const size_t size = Stream_Get_UINT16_BE(s);
2038 if (size > RDPUDP_MAX_ACK_VECTOR)
2039 return TRUE; /* [MS-RDPEUDP] 2.2.2.7: more than 2048 elements is invalid, drop */
2040 const size_t padding = (4 - ((2 + size) % 4)) % 4;
2041 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, size + padding))
2042 return TRUE;
2043 v1_process_ack_vector(udp, snSourceAck, s, size);
2044 Stream_Seek(s, padding);
2045 }
2046
2047 if (flags & RDPUDP_FLAG_ACK_OF_ACKS)
2048 {
2049 /* RDPUDP_ACK_OF_ACKVECTOR_HEADER */
2050 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 4))
2051 return TRUE;
2052 const UINT32 reset = Stream_Get_UINT32_BE(s);
2053 /* the peer has seen our acknowledgements up to here, the ACK vector can start later */
2054 const UINT32 start = seq32_add(reset, 1);
2055 if (seq32_after(start, udp->ackVectorStart) && !seq32_after(start, udp->recvNext))
2056 udp->ackVectorStart = start;
2057 }
2058
2059 if ((flags & RDPUDP_FLAG_CORRELATION_ID) && !Stream_SafeSeek(s, 32))
2060 return TRUE;
2061
2062 if (!(flags & RDPUDP_FLAG_DATA))
2063 return TRUE;
2064
2065 if (flags & RDPUDP_FLAG_FEC)
2066 return TRUE; /* FEC packets are not acknowledged, losses are recovered by resending */
2067
2068 /* RDPUDP_SOURCE_PAYLOAD_HEADER */
2069 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 8))
2070 return TRUE;
2071 Stream_Seek_UINT32(s); /* snCoded */
2072 const UINT32 snSourceStart = Stream_Get_UINT32_BE(s);
2073 return v1_receive_source(udp, snSourceStart, Stream_ConstPointer(s),
2074 Stream_GetRemainingLength(s));
2075}
2076
2077static void v3_advance_window(rdpUdp* udp)
2078{
2079 WINPR_ASSERT(udp);
2080 while (v3_received(udp, udp->v3Expected))
2081 {
2082 udp->v3InOrderArrival = v3_arrival(udp, udp->v3Expected);
2083 udp->v3InOrderReceived = TRUE;
2084 v3_set_expected(udp, seq16_add(udp->v3Expected, 1));
2085 }
2086}
2087
2088/* After a silence the peer may have moved more than half the 16 bit sequence space on: through
2089 * an outage it resends its window again and again under new numbers, Windows used about 45000 in
2090 * 25 s. Serial arithmetic then takes its numbers for old ones, and acknowledging our old position
2091 * names numbers it has not sent, for which Windows resets the connection. So after a silence the
2092 * peer's next AckOfAcks becomes the base, whichever way it moves it. */
2093static void v3_resync_base(rdpUdp* udp, UINT16 base)
2094{
2095 WINPR_ASSERT(udp);
2096 udp->v3Resync = FALSE;
2097 /* Server: a base slightly behind ours indicates the peer's sequence numbers haven't wrapped,
2098 * meaning our current position is still valid. The discrepancy likely stems from lost
2099 * ACKs during the silence or a reordered/duplicate packet. Moving the window back to this
2100 * base would place it below what was already acknowledged, which causes mstsc to drop the
2101 * connection upon the next ACK vector. A legitimate move wouldn't land here because the
2102 * peer keeps numbers within RDPUDP2_STALE_AOA_REACH of the oldest unacknowledged one,
2103 * and timeouts occur before the sequence space can wrap. The window remains, and
2104 * acknowledging resumes. */
2105 if (udp->server && seq16_after(udp->v3Expected, base) &&
2106 (seq16_diff(udp->v3Expected, base) <= RDPUDP2_STALE_AOA_REACH))
2107 {
2108 udp->ackPending = TRUE;
2109 udp->v3WindowPending = TRUE;
2110 return;
2111 }
2112 if (base == udp->v3Expected)
2113 return;
2114
2115 WLog_Print(udp->log, WLOG_DEBUG,
2116 "heard from the peer again, its AckOfAcks 0x%04" PRIx16 " replaces 0x%04" PRIx16,
2117 base, udp->v3Expected);
2118 /* Rare (once per silence). The base may move either way, so we reset the slots. */
2119 memset(udp->v3Received, 0, sizeof(udp->v3Received));
2120 memset(udp->v3Arrival, 0, sizeof(udp->v3Arrival));
2121 udp->v3ExpectedFull += 0x10000;
2122 udp->v3Expected = base;
2123 udp->v3Highest = seq16_diff(base, 1);
2124 udp->v3InOrderReceived = FALSE;
2125 udp->ackPending = TRUE;
2126 udp->v3WindowPending = TRUE;
2127}
2128
2129/* Marks a silence of RDPUDP2_RESYNC_MS, see v3_resync_base. */
2130static void v3_note_silence(rdpUdp* udp, UINT64 now)
2131{
2132 WINPR_ASSERT(udp);
2133 if ((udp->version == RDPUDP_PROTOCOL_VERSION_3) && udp->v3BaseKnown &&
2134 (now - udp->lastReceived >= RDPUDP2_RESYNC_MS))
2135 udp->v3Resync = TRUE;
2136}
2137
2138static void v3_advance_base(rdpUdp* udp, UINT16 base)
2139{
2140 WINPR_ASSERT(udp);
2141 /* However far ahead: through an outage the peer resends its window again and again, each
2142 * time under new sequence numbers, and Windows moved almost 5000 ahead in a 2 second outage.
2143 * Only its AckOfAcks brings this side along, data that far ahead is out of reach. seq16_after
2144 * limits the jump to half the sequence space, v3_resync_base goes further after a silence. */
2145 if (!udp->v3BaseKnown)
2146 return;
2147 if (udp->v3Resync)
2148 {
2149 v3_resync_base(udp, base);
2150 return;
2151 }
2152 if (!seq16_after(base, udp->v3Expected))
2153 return;
2154
2155 /* The peer has stopped resending all packets below the base. This only discards transport
2156 * sequence numbers; any stream data sent in that range that is resent will use a later
2157 * transport sequence number, while its channel sequence number remains unchanged. Slots
2158 * below the base retain their stamps, but since no number in the current window can
2159 * match them, nothing is explicitly cleared regardless of how far the base moves. */
2160 v3_set_expected(udp, base);
2161 udp->v3InOrderReceived = FALSE;
2162 const UINT16 last = seq16_diff(base, 1);
2163 if (seq16_after(last, udp->v3Highest))
2164 udp->v3Highest = last;
2165 v3_advance_window(udp);
2166 udp->ackPending = TRUE;
2167 udp->v3WindowPending = TRUE;
2168}
2169
2170/* The data sequence number of the last datagram this end sent. Only data packets take one, all
2171 * from nextCoded. */
2172WINPR_ATTR_NODISCARD
2173static UINT16 v3_last_sent(const rdpUdp* udp)
2174{
2175 WINPR_ASSERT(udp);
2176 return seq16_diff(WINPR_ASSERTING_INT_CAST(UINT16, udp->nextCoded), 1);
2177}
2178
2179/* TRUE if seq is past the last datagram this end sent. */
2180WINPR_ATTR_NODISCARD
2181static BOOL v3_not_sent(const rdpUdp* udp, UINT16 seq)
2182{
2183 return seq16_after(seq, v3_last_sent(udp));
2184}
2185
2186/* Returns TRUE if seq is between sendBase and the next sequence number, with datagrams in
2187 * flight. Unlike serial arithmetic this is exact, because everything in flight lies within
2188 * RDPUDP2_MAX_RESEND_SPAN. */
2189WINPR_ATTR_NODISCARD
2190static BOOL v3_from_send_base(const rdpUdp* udp, UINT16 seq)
2191{
2192 WINPR_ASSERT(udp);
2193 const UINT16 base = WINPR_ASSERTING_INT_CAST(UINT16, udp->sendBase);
2194 return seq16_diff(seq, base) <=
2195 seq16_diff(WINPR_ASSERTING_INT_CAST(UINT16, udp->nextCoded), base);
2196}
2197
2198static void v3_process_ack_vector(rdpUdp* udp, UINT16 base, wStream* s, size_t count)
2199{
2200 WINPR_ASSERT(udp);
2201 WINPR_ASSERT(s);
2202 /* count is from the wire, bounded by the 7-bit codedAckVecSize; v3_process_datagram
2203 * verified the available bytes. */
2204 WINPR_ASSERT(count <= RDPUDP2_MAX_ACKVEC);
2205 WINPR_ASSERT(Stream_GetRemainingLength(s) >= count);
2206
2207 /* 127 entries; each covers up to 63 (run length) or 7 (state map) datagrams. */
2208 BYTE received[RDPUDP2_MAX_ACKVEC * 63] = WINPR_C_ARRAY_INIT;
2209 size_t covered = 0;
2210 size_t highest = 0; /* The index one past the last datagram reported as received */
2211 for (size_t x = 0; x < count; x++)
2212 {
2213 const BYTE entry = Stream_Get_UINT8(s);
2214 if (entry & 0x80)
2215 {
2216 /* run length: received flag and length */
2217 const BOOL got = (entry & 0x40) != 0;
2218 const BYTE run = entry & 0x3F;
2219 for (BYTE y = 0; (y < run) && (covered < ARRAYSIZE(received)); y++)
2220 {
2221 received[covered++] = got ? 1 : 0;
2222 if (got)
2223 highest = covered;
2224 }
2225 }
2226 else
2227 {
2228 /* state map of 7 packets, bit 0 first */
2229 for (BYTE bit = 0; (bit < 7) && (covered < ARRAYSIZE(received)); bit++)
2230 {
2231 const BOOL got = (entry & (1 << bit)) != 0;
2232 received[covered++] = got ? 1 : 0;
2233 if (got)
2234 highest = covered;
2235 }
2236 }
2237 }
2238
2239 if (udp->server)
2240 {
2241 /* A real peer only reports datagrams this end sent, whatever loss or reordering did to
2242 * them, so a base or a received entry past the last one sent is forged. */
2243 const UINT16 last = v3_last_sent(udp);
2244 const size_t sentFromBase = seq16_after(base, last) ? 0 : seq16_diff(last, base) + 1u;
2245 if (v3_not_sent(udp, seq16_diff(base, 1)) || (highest > sentFromBase))
2246 {
2247 WLog_Print(udp->log, WLOG_DEBUG,
2248 "ignoring an ACK vector at base 0x%04" PRIx16
2249 " that reports datagrams never sent (last sent 0x%04" PRIx16 ")",
2250 base, last);
2251 return;
2252 }
2253 }
2254
2255 /* All packets below the base are received, unless the vector reports the base as received.
2256 * The base is typically the receiver's first missing packet ([MS-RDPEUDP2] 3.1.5.7).
2257 * If the base is reported as received, it may be a report on a packet behind the
2258 * receiver's window (sent by FreeRDP clients, see v3_process_datagram and 4.2.2),
2259 * which says nothing about packets below it. After silence, this may refer to a
2260 * stale window. For servers, this doesn't apply if the base is behind all in-flight
2261 * packets (see v3_process_ack). */
2262 const BOOL baseIsMissing = (covered == 0) || !received[0];
2263 const BOOL baseInRange =
2264 baseIsMissing && (!udp->server || (udp->inFlight == 0) || v3_from_send_base(udp, base));
2265 while (baseInRange && (udp->inFlight > 0) &&
2266 seq16_after(base, WINPR_ASSERTING_INT_CAST(UINT16, udp->sendBase)))
2267 acknowledge(udp, pending_find(udp, udp->sendBase));
2268
2269 const size_t sent = seq16_diff(WINPR_ASSERTING_INT_CAST(UINT16, udp->nextCoded), base);
2270 for (size_t offset = 0; (offset < covered) && (offset < sent) && (udp->inFlight > 0); offset++)
2271 {
2272 rdpudp_pending* p = pending_find(udp, seq16_add(base, offset));
2273 if (!p)
2274 continue;
2275 if (received[offset])
2276 acknowledge(udp, p);
2277 else if (udp->server && (highest > offset + udp->reorderThreshold) && !p->lost)
2278 {
2279 /* Missing despite later datagrams arriving, so it's lost. Retransmit now, or after
2280 * one report round since last send. */
2281 if (now_ms(udp) - p->lastSent >= report_resend_interval(udp))
2282 handle_reported_loss(udp, p);
2283 else
2284 {
2285 p->reportHeld = TRUE;
2286 pending_due_at(udp, p->lastSent + report_resend_interval(udp));
2287 }
2288 }
2289 }
2290
2291 if (!udp->server)
2292 return;
2293
2294 /* The original copy of a retransmitted datagram arrived; it was delayed, not lost. */
2295 const UINT64 vectorTime = now_ms(udp);
2296 for (size_t x = 0; x < RDPUDP2_RESENT_HISTORY; x++)
2297 {
2298 if (!udp->resent[x].valid)
2299 continue;
2300 const UINT16 seq = udp->resent[x].seq;
2301 if (seq16_after(base, seq) || (vectorTime - udp->resent[x].at > RDPUDP2_RESENT_MAX_AGE_MS))
2302 {
2303 udp->resent[x].valid = FALSE;
2304 continue;
2305 }
2306 const size_t offset = seq16_diff(seq, base);
2307 if ((offset >= covered) || !received[offset])
2308 continue;
2309 udp->resent[x].valid = FALSE;
2310 if (udp->ccRoundLost > 0)
2311 udp->ccRoundLost--;
2312 if (!udp->resent[x].reported)
2313 continue;
2314 if (udp->reorderThreshold < RDPUDP2_MAX_REORDER_THRESHOLD)
2315 {
2316 udp->reorderThreshold *= 2;
2317 if (udp->reorderThreshold > RDPUDP2_MAX_REORDER_THRESHOLD)
2318 udp->reorderThreshold = RDPUDP2_MAX_REORDER_THRESHOLD;
2319 WLog_Print(udp->log, WLOG_DEBUG,
2320 "datagram 0x%04" PRIx16
2321 " arrived after its resend, reordering threshold: %" PRIu32,
2322 seq, udp->reorderThreshold);
2323 }
2324 udp->reorderCalmRounds = 0;
2325 }
2326}
2327
2328static void v3_process_ack(rdpUdp* udp, UINT16 ack)
2329{
2330 WINPR_ASSERT(udp);
2331 /* a real peer cannot acknowledge a datagram this end never sent */
2332 if (udp->server && v3_not_sent(udp, ack))
2333 {
2334 WLog_Print(udp->log, WLOG_DEBUG,
2335 "ignoring an ACK of 0x%04" PRIx16 ", past the last datagram sent 0x%04" PRIx16,
2336 ack, v3_last_sent(udp));
2337 return;
2338 }
2339 /* ACKs preceding all in-flight packets are ignored. If an ACK is more than half the sequence
2340 * space behind sendBase, wrap-around arithmetic could misinterpret it as being ahead,
2341 * incorrectly acknowledging packets the peer never received. */
2342 if (udp->server && (udp->inFlight > 0) && !v3_from_send_base(udp, ack))
2343 return;
2344 /* cumulative: everything up to and including ack */
2345 while ((udp->inFlight > 0) &&
2346 !seq16_after(WINPR_ASSERTING_INT_CAST(UINT16, udp->sendBase), ack))
2347 acknowledge(udp, pending_find(udp, udp->sendBase));
2348}
2349
2350/* Hands up the chunks that are next in the stream, in channel sequence order. */
2351WINPR_ATTR_NODISCARD
2352static BOOL v3_deliver_ready(rdpUdp* udp)
2353{
2354 WINPR_ASSERT(udp);
2355 while (TRUE)
2356 {
2357 rdpudp_slot* slot = &udp->slots[udp->v3NextChannel % RDPUDP_RECEIVE_SLOTS];
2358 if (!slot->present || (slot->seq != udp->v3NextChannel))
2359 break;
2360 BYTE* chunk = slot->data;
2361 const size_t chunkLength = slot->length;
2362 slot->data = nullptr;
2363 slot->present = FALSE;
2364 udp->buffered--;
2365 udp->v3NextChannel = channel_seq_next(udp->v3NextChannel);
2366 const BOOL rc = deliver(udp, chunk, chunkLength);
2367 free(chunk);
2368 if (!rc)
2369 return FALSE;
2370 }
2371 return TRUE;
2372}
2373
2374WINPR_ATTR_NODISCARD
2375static BOOL v3_receive_channel_data(rdpUdp* udp, UINT16 channelSeq, const BYTE* data, size_t length)
2376{
2377 WINPR_ASSERT(udp);
2378 WINPR_ASSERT(data || (length == 0));
2379 WINPR_ASSERT(!udp->server || (length <= RDPUDP_MAX_CHUNK));
2380
2381 /* A retransmission travels under a new data sequence number with its original channel
2382 * sequence, so the channel sequence alone says where a chunk belongs and whether it has
2383 * been handed up already. */
2384 const UINT16 ahead = seq16_diff(channelSeq, udp->v3NextChannel);
2385 if (ahead >= RDPUDP_RECEIVE_REACH)
2386 return TRUE;
2387
2388 /* The chunk next in line goes up straight from the datagram, without a copy in a slot. */
2389 if ((ahead == 0) && !udp->slots[channelSeq % RDPUDP_RECEIVE_SLOTS].present)
2390 {
2391 udp->v3NextChannel = channel_seq_next(udp->v3NextChannel);
2392 if (!deliver(udp, data, length))
2393 return FALSE;
2394 return v3_deliver_ready(udp);
2395 }
2396
2397 if (!store_slot(udp, channelSeq, data, length))
2398 return FALSE;
2399 return v3_deliver_ready(udp);
2400}
2401
2402WINPR_ATTR_NODISCARD
2403static BOOL v3_process_datagram(rdpUdp* udp, BYTE* data, size_t length)
2404{
2405 WINPR_ASSERT(udp);
2406 WINPR_ASSERT(data);
2407 if (length < 8)
2408 return TRUE;
2409
2410 /* undo the swap of the first and eighth byte ([MS-RDPEUDP2] 2.2.1). In place: with only
2411 * DATA set the chunk starts at the eighth byte, so a copy of the header would not do. */
2412 const BYTE eighth = data[7];
2413 data[7] = data[0];
2414 data[0] = eighth;
2415
2416 wStream sbuffer = WINPR_C_ARRAY_INIT;
2417 wStream* s = Stream_StaticConstInit(&sbuffer, data, length);
2418
2419 const BYTE prefix = Stream_Get_UINT8(s);
2420 if (prefix & 0x01)
2421 return TRUE;
2422 const BYTE shortLength = WINPR_ASSERTING_INT_CAST(BYTE, prefix >> 5);
2423 const BYTE packetType = WINPR_ASSERTING_INT_CAST(BYTE, (prefix >> 1) & 0x0F);
2424 if ((packetType != RDPUDP2_PACKET_STANDARD) && (packetType != RDPUDP2_PACKET_DUMMY))
2425 return TRUE;
2426 /* a packet shorter than 7 bytes was padded, its real length is in the prefix */
2427 if ((shortLength > 0) && (shortLength < 7) && !Stream_SetLength(s, 1ull + shortLength))
2428 return TRUE;
2429
2430 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 2))
2431 return TRUE;
2432 const UINT16 flags = Stream_Get_UINT16(s) & 0x0FFF;
2433
2434 if ((flags & RDPUDP2_ACK) && (flags & RDPUDP2_ACKVEC))
2435 return TRUE;
2436
2437 if (flags & RDPUDP2_ACK)
2438 {
2439 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 7))
2440 return TRUE;
2441 const UINT16 ack = Stream_Get_UINT16(s); /* SeqNum */
2442 Stream_Seek(s, 4); /* receivedTS, sendAckTimeGapInMs */
2443 const size_t delayed = Stream_Get_UINT8(s) & 0x0F; /* numDelayedAcks */
2444 if (!Stream_SafeSeek(s, delayed)) /* DelayAckTimeAdditions */
2445 return TRUE;
2446 v3_process_ack(udp, ack);
2447 }
2448
2449 if ((flags & RDPUDP2_OVERHEADSIZE) && !Stream_SafeSeek(s, 1))
2450 return TRUE;
2451
2452 if (flags & RDPUDP2_DELAYACKINFO)
2453 {
2454 /* [MS-RDPEUDP2] 2.2.1.2.3: how many data packets the peer lets us acknowledge at once,
2455 * and how long an acknowledgement may wait */
2456 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 3))
2457 return TRUE;
2458 const size_t maxDelayed = Stream_Get_UINT8(s); /* MaxDelayedAcks */
2459 udp->v3DelayedAckTimeout = Stream_Get_UINT16(s); /* DelayedAckTimeoutInMs */
2460 if (maxDelayed == 0)
2461 udp->v3MaxDelayedAcks = 1;
2462 else if (maxDelayed > RDPUDP2_MAX_DELAYED_ACKS)
2463 udp->v3MaxDelayedAcks = RDPUDP2_MAX_DELAYED_ACKS;
2464 else
2465 udp->v3MaxDelayedAcks = maxDelayed;
2466 }
2467
2468 BOOL haveAoA = FALSE;
2469 UINT16 aoa = 0;
2470 if (flags & RDPUDP2_AOA)
2471 {
2472 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 2))
2473 return TRUE;
2474 aoa = Stream_Get_UINT16(s);
2475 haveAoA = TRUE;
2476 }
2477
2478 BOOL haveData = FALSE;
2479 UINT16 dataSeq = 0;
2480 if (flags & RDPUDP2_DATA)
2481 {
2482 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 2))
2483 return TRUE;
2484 dataSeq = Stream_Get_UINT16(s);
2485 haveData = TRUE;
2486 }
2487
2488 if (flags & RDPUDP2_ACKVEC)
2489 {
2490 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 3))
2491 return TRUE;
2492 const UINT16 base = Stream_Get_UINT16(s); /* BaseSeqNum */
2493 const BYTE control = Stream_Get_UINT8(s);
2494 const size_t size = control & 0x7F; /* codedAckVecSize */
2495 /* TimeStamp, SendAckTimeGapInMs */
2496 if ((control & 0x80) && !Stream_SafeSeek(s, 4))
2497 return TRUE;
2498 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, size))
2499 return TRUE;
2500 v3_process_ack_vector(udp, base, s, size);
2501 }
2502
2503 /* AckOfAcks moves the lower edge of our receive window. It is the only way past a lost
2504 * dummy packet, those are never resent. */
2505 if (haveAoA)
2506 v3_advance_base(udp, aoa);
2507
2508 /* An AckOfAcks with nothing else is the keepalive of a peer that has not heard from this end
2509 * for RDPUDP2_RESYNC_MS (v3_send_base_keepalive), and is close to its peer timeout. It gets
2510 * an ACK now instead of with the next keepalive: one answer every keepalive interval could
2511 * come too late, or be the one datagram that is lost. */
2512 if (haveAoA && !haveData && !(flags & (RDPUDP2_ACK | RDPUDP2_ACKVEC)) && udp->v3BaseKnown &&
2513 !udp->v3Resync)
2514 {
2515 udp->ackPending = TRUE;
2516 udp->v3WindowPending = TRUE;
2517 }
2518
2519 if (!haveData)
2520 return TRUE;
2521
2522 /* The receiver cannot take more: the packet counts as lost and the peer resends it. */
2523 if (udp->receiveHold)
2524 return TRUE;
2525
2526 /* Server: The base is derived only from AckOfAcks. Before version 3.34, FreeRDP clients
2527 * sent AckOfAcks only on the first data packet and resends (now they repeat it until
2528 * acknowledged, see v3_send_data). Using a later packet as the base could cause a lost
2529 * first packet to be below the base, leading to a false cumulative ACK. Ignoring the
2530 * packet ensures it is resent with an AckOfAcks. */
2531 if (udp->server && !udp->v3BaseKnown && !haveAoA)
2532 return TRUE;
2533
2534 udp->v3LatestArrival = now_ns(udp);
2535 udp->v3UnackedPackets++;
2536 udp->receivedData = TRUE;
2537 if (!udp->v3BaseKnown)
2538 {
2539 /* Windows announces where its sequence space starts in the AckOfAcks of its first
2540 * DATA packets. Taking the base from that, rather than from whichever packet arrives
2541 * first, keeps a lost or late first packet from being acknowledged unseen. */
2542 UINT16 base = dataSeq;
2543 if (haveAoA && !seq16_after(aoa, dataSeq) &&
2544 (seq16_diff(dataSeq, aoa) < RDPUDP_RECEIVE_REACH))
2545 base = aoa;
2546 udp->v3BaseKnown = TRUE;
2547 udp->v3Expected = base;
2548 udp->v3Highest = seq16_diff(base, 1);
2549 udp->v3AckedSeq = seq16_diff(base, 1);
2550 }
2551
2552 const UINT16 ahead = seq16_diff(dataSeq, udp->v3Expected);
2553 /* Behind the window: resent because our ACK got lost, given up by an AckOfAcks, or ahead of
2554 * a position of ours the peer's numbers wrapped past. The window only tracks acknowledgements
2555 * ([MS-RDPEUDP2] 3.1.1.2.2): the packet is acknowledged on its own, and its chunk still goes
2556 * to the channel sequence, which drops it if it was handed up already (3.1.1.2.4.2). */
2557 const BOOL behind = (ahead >= 0x8000);
2558 /* Windows at times puts the next sequence number in the AckOfAcks of a data packet, past the
2559 * packet itself. The peer has the acknowledgments for everything below its AckOfAcks, the
2560 * receiver does not report those again (2.2.1.2.4), and Windows ends the connection on an ACK
2561 * vector for such a packet. Only its chunk counts. */
2562 BOOL acknowledged = haveAoA && seq16_after(aoa, dataSeq);
2563
2564 /* mstsc sometimes resends old data packets and closes the connection if it receives an ACK
2565 * vector for a packet already covered by a cumulative ACK. To avoid this, the server does
2566 * not report packets behind its window. If the server's state becomes incorrect after a
2567 * silence, it waits for the next AckOfAcks to resync. An ACK vector based on a stale
2568 * window could falsely indicate that unseen packets arrived (as v3_process_ack_vector
2569 * treats everything below the base as received). Unacknowledged, the packet will be
2570 * resent with an AckOfAcks. */
2571 if (udp->server && behind)
2572 {
2573 WLog_Print(udp->log, WLOG_DEBUG,
2574 "data packet 0x%04" PRIx16 " is behind the window at 0x%04" PRIx16
2575 ", skipping ACK vector",
2576 dataSeq, udp->v3Expected);
2577 acknowledged = TRUE;
2578 }
2579
2580 if (udp->server && (packetType != RDPUDP2_PACKET_DUMMY) && (Stream_GetRemainingLength(s) >= 2))
2581 {
2582 /* The server validates the chunk before marking the packet for ACK or ACK vector, as the
2583 * peer never resends acked packets. Chunks out of reach wait for retransmission. Oversized
2584 * chunks are dropped as if lost; this prevents a single spoofed datagram from killing the
2585 * connection, while a client repeatedly sending them will stall its own stream until the
2586 * stall check triggers. */
2587 UINT16 nextChannel = 0;
2588 Stream_Peek_UINT16(s, nextChannel); /* Peek the channel sequence number */
2589 const UINT16 chunkAhead = seq16_diff(nextChannel, udp->v3NextChannel);
2590 if ((chunkAhead >= udp->chunkReach) && (chunkAhead < 0x8000))
2591 return TRUE;
2592 if (Stream_GetRemainingLength(s) > 2ull + RDPUDP_MAX_CHUNK)
2593 {
2594 WLog_Print(udp->log, WLOG_DEBUG, "discarding a chunk of %" PRIuz " bytes",
2595 Stream_GetRemainingLength(s) - 2);
2596 return TRUE;
2597 }
2598 }
2599
2600 if (behind && !acknowledged)
2601 {
2602 /* a run of consecutive ones goes in one ACK vector, see v3_send_ack */
2603 const BOOL extends = (udp->v3BehindCount > 0) &&
2604 (dataSeq == seq16_add(udp->v3BehindBase, udp->v3BehindCount)) &&
2605 (udp->v3BehindCount < 63ull * RDPUDP2_MAX_ACKVEC);
2606 if ((udp->v3BehindCount > 0) && !extends && !v3_send_ack(udp))
2607 return FALSE;
2608 if (!extends)
2609 {
2610 udp->v3BehindBase = dataSeq;
2611 udp->v3BehindCount = 0;
2612 }
2613 udp->v3BehindCount++;
2614 udp->v3BehindArrival = udp->v3LatestArrival;
2615 udp->ackPending = TRUE;
2616 }
2617 else if (!behind)
2618 {
2619 udp->ackPending = TRUE;
2620 udp->v3WindowPending = TRUE;
2621 if (ahead >= RDPUDP_RECEIVE_REACH)
2622 return TRUE;
2623 }
2624
2625 /* A dummy packet occupies a sequence number but its body means nothing. */
2626 const BOOL withChunk =
2627 (packetType != RDPUDP2_PACKET_DUMMY) && (Stream_GetRemainingLength(s) >= 2);
2628 UINT16 channelSeq = 0;
2629 if (withChunk)
2630 {
2631 /* A chunk out of reach cannot be kept. Its packet stays unacknowledged then, so the
2632 * peer sends it again: acknowledged, it would be gone for good. */
2633 channelSeq = Stream_Get_UINT16(s); /* ChannelSeqNum */
2634 const UINT16 chunkAhead = seq16_diff(channelSeq, udp->v3NextChannel);
2635 if ((chunkAhead >= udp->chunkReach) && (chunkAhead < 0x8000))
2636 return TRUE;
2637 }
2638
2639 if (!behind)
2640 {
2641 if (seq16_after(dataSeq, udp->v3Highest))
2642 udp->v3Highest = dataSeq;
2643 v3_mark_received(udp, dataSeq, udp->v3LatestArrival);
2644 v3_advance_window(udp);
2645 }
2646
2647 if (!withChunk)
2648 return TRUE;
2649 return v3_receive_channel_data(udp, channelSeq, Stream_ConstPointer(s),
2650 Stream_GetRemainingLength(s));
2651}
2652
2653/* ------------------------------------------------------------------------------------------ */
2654/* Connection setup */
2655/* ------------------------------------------------------------------------------------------ */
2656
2657WINPR_ATTR_NODISCARD
2658static BOOL send_syn(rdpUdp* udp)
2659{
2660 WINPR_ASSERT(udp);
2661 /* [MS-RDPEUDP] 3.1.5.1.1: zero padded to the MTU, which validates the path MTU */
2662 BYTE syn[RDPUDP_MTU] = WINPR_C_ARRAY_INIT;
2663 wStream sbuffer = WINPR_C_ARRAY_INIT;
2664 wStream* s = Stream_StaticInit(&sbuffer, syn, sizeof(syn));
2665 if (!s)
2666 return FALSE;
2667 const UINT16 version = udp->offerV3 ? RDPUDP_PROTOCOL_VERSION_3 : RDPUDP_PROTOCOL_VERSION_2;
2668
2669 /* RDPUDP_FEC_HEADER */
2670 Stream_Write_UINT32_BE(s, UINT32_MAX); /* snSourceAck: -1 */
2671 Stream_Write_UINT16_BE(s, RDPUDP_RECEIVE_WINDOW);
2672 Stream_Write_UINT16_BE(s, RDPUDP_FLAG_SYN | RDPUDP_FLAG_SYNEX);
2673
2674 /* RDPUDP_SYNDATA_PAYLOAD */
2675 Stream_Write_UINT32_BE(s, udp->initialSequence);
2676 Stream_Write_UINT16_BE(s, RDPUDP_MTU); /* uUpStreamMtu */
2677 Stream_Write_UINT16_BE(s, RDPUDP_MTU); /* uDownStreamMtu */
2678
2679 /* RDPUDP_SYNDATAEX_PAYLOAD */
2680 Stream_Write_UINT16_BE(s, RDPUDP_VERSION_INFO_VALID);
2681 Stream_Write_UINT16_BE(s, version);
2682 if (version == RDPUDP_PROTOCOL_VERSION_3)
2683 Stream_Write(s, udp->cookieHash, sizeof(udp->cookieHash));
2684
2685 udp->synSent = now_ms(udp);
2686 udp->synAttempts++;
2687 WLog_Print(udp->log, WLOG_DEBUG, "sending SYN #%" PRIu32 " offering version 0x%04" PRIx16,
2688 udp->synAttempts, version);
2689 return rdpudp_send_raw(udp, syn, sizeof(syn));
2690}
2691
2692WINPR_ATTR_NODISCARD
2693static BOOL process_syn_ack(rdpUdp* udp, const BYTE* data, size_t length)
2694{
2695 wStream sbuffer = WINPR_C_ARRAY_INIT;
2696 WINPR_ASSERT(udp);
2697 WINPR_ASSERT(data);
2698 wStream* s = Stream_StaticConstInit(&sbuffer, data, length);
2699
2700 /* RDPUDP_FEC_HEADER and RDPUDP_SYNDATA_PAYLOAD */
2701 if (!Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 16))
2702 return TRUE;
2703 const UINT32 snSourceAck = Stream_Get_UINT32_BE(s);
2704 const UINT16 window = Stream_Get_UINT16_BE(s);
2705 const UINT16 flags = Stream_Get_UINT16_BE(s);
2706 if ((flags & (RDPUDP_FLAG_SYN | RDPUDP_FLAG_ACK)) != (RDPUDP_FLAG_SYN | RDPUDP_FLAG_ACK))
2707 return TRUE;
2708 /* The server's answer acknowledges the random snInitialSequenceNumber of our SYN. One that
2709 * does not was not sent in answer to it and is dropped, so a third party that spoofs the
2710 * server's address has to guess that number to set up the connection. */
2711 if (snSourceAck != udp->initialSequence)
2712 {
2713 WLog_Print(udp->log, WLOG_WARN,
2714 "dropping a SYN+ACK that acknowledges 0x%08" PRIx32
2715 ", the SYN carried 0x%08" PRIx32,
2716 snSourceAck, udp->initialSequence);
2717 return TRUE;
2718 }
2719
2720 const UINT32 peerInitial = Stream_Get_UINT32_BE(s); /* snInitialSequenceNumber */
2721 UINT16 upMtu = Stream_Get_UINT16_BE(s); /* uUpStreamMtu */
2722 Stream_Seek_UINT16(s); /* uDownStreamMtu */
2723
2724 if ((flags & RDPUDP_FLAG_CORRELATION_ID) && !Stream_SafeSeek(s, 32))
2725 return TRUE;
2726
2727 /* RDPUDP_SYNDATAEX_PAYLOAD, without it the peer only speaks version 1 */
2728 UINT16 version = RDPUDP_PROTOCOL_VERSION_1;
2729 if ((flags & RDPUDP_FLAG_SYNEX) && (Stream_GetRemainingLength(s) >= 4))
2730 {
2731 const UINT16 synExFlags = Stream_Get_UINT16_BE(s);
2732 const UINT16 udpVersion = Stream_Get_UINT16_BE(s);
2733 if (synExFlags & RDPUDP_VERSION_INFO_VALID)
2734 version = udpVersion;
2735 }
2736
2737 switch (version)
2738 {
2739 case RDPUDP_PROTOCOL_VERSION_1:
2740 case RDPUDP_PROTOCOL_VERSION_2:
2741 break;
2742 case RDPUDP_PROTOCOL_VERSION_3:
2743 /* a server that rejected the cookie hash answers with version 2 */
2744 if (!udp->offerV3)
2745 version = RDPUDP_PROTOCOL_VERSION_2;
2746 break;
2747 default:
2748 version = RDPUDP_PROTOCOL_VERSION_1;
2749 break;
2750 }
2751
2752 if ((upMtu < RDPUDP_MIN_MTU) || (upMtu > RDPUDP_MTU))
2753 upMtu = RDPUDP_MIN_MTU;
2754
2755 udp->mtu = upMtu;
2756 udp->version = version;
2757 udp->peerWindow = window;
2758 udp->peerInitialSequence = peerInitial;
2759 udp->recvNext = seq32_add(peerInitial, 1);
2760 udp->recvHighest = peerInitial;
2761 udp->ackVectorStart = seq32_add(peerInitial, 1);
2762 udp->lastDelivery = now_ms(udp);
2763 udp->state = RDPUDP_STATE_ESTABLISHED;
2764
2765 if (version == RDPUDP_PROTOCOL_VERSION_3)
2766 {
2767 /* The 32 bit sequence numbers of the SYN exchange only exist for versions 1 and 2. The
2768 * peer's RDP-UDP2 sequence space is learned from its first DATA packet. */
2769 udp->nextCoded = RDPUDP2_INITIAL_SEQUENCE;
2770 udp->nextChannelSeq = 1;
2771 /* The peer's stream starts at channel sequence 1 too. Taking the start from whichever
2772 * chunk arrives first would throw away an earlier one that it overtook. */
2773 udp->v3NextChannel = 1;
2774 udp->v3MaxDelayedAcks = RDPUDP2_DEFAULT_DELAYED_ACKS;
2775 udp->v3DelayedAckTimeout = UINT32_MAX;
2776 }
2777 else
2778 {
2779 udp->nextSource = seq32_add(udp->initialSequence, 1);
2780 udp->nextCoded = seq32_add(udp->initialSequence, 1);
2781 /* the client completes the three way handshake by acknowledging the SYN+ACK */
2782 udp->ackPending = TRUE;
2783 }
2784
2785 WLog_Print(udp->log, WLOG_INFO,
2786 "RDP-UDP connected, protocol version 0x%04" PRIx16 ", MTU %" PRIu16, version, upMtu);
2787 return flush_send_queue(udp);
2788}
2789
2790/* A SYN datagram has snSourceAck = -1 and the SYN flag set without ACK. No RDP-UDP2 data
2791 * phase datagram begins with four 0xFF bytes followed by these flags. */
2792WINPR_ATTR_NODISCARD
2793static BOOL is_syn(const BYTE* data, size_t length)
2794{
2795 WINPR_ASSERT(data || (length == 0));
2796 if (length < 8)
2797 return FALSE;
2798 const UINT16 flags = WINPR_ASSERTING_INT_CAST(UINT16, (data[6] << 8) | data[7]);
2799 return (data[0] == 0xFF) && (data[1] == 0xFF) && (data[2] == 0xFF) && (data[3] == 0xFF) &&
2800 ((flags & (RDPUDP_FLAG_SYN | RDPUDP_FLAG_ACK)) == RDPUDP_FLAG_SYN);
2801}
2802
2803WINPR_ATTR_NODISCARD
2804static BOOL process_datagram(rdpUdp* udp, BYTE* data, size_t length)
2805{
2806 WINPR_ASSERT(udp);
2807 WINPR_ASSERT(data);
2808
2809 /* The client missed our SYN+ACK and resent its SYN. Since SYNs are padded to the MTU
2810 * ([MS-RDPEUDP] 3.1.5.1.1), a genuine SYN is never shorter than our reply; shorter packets
2811 * may be from a spoofed source attempting to induce asymmetric traffic. */
2812 if (udp->server && is_syn(data, length))
2813 {
2814 if (length < udp->synAckLength)
2815 {
2816 WLog_Print(udp->log, WLOG_DEBUG, "discarding a duplicate SYN shorter than the MTU");
2817 return TRUE;
2818 }
2819 WLog_Print(udp->log, WLOG_DEBUG, "received duplicate SYN, re-sending SYN+ACK");
2820 udp->synAckResent = TRUE;
2821 return rdpudp_send_raw(udp, udp->synAck, udp->synAckLength);
2822 }
2823
2824 const UINT64 now = now_ms(udp);
2825 if (udp->server && (udp->synAckSent != 0))
2826 {
2827 /* [RFC6298] 2.2: the first datagram after the SYN+ACK provides the initial RTT sample.
2828 * Without it, the first retransmission timer uses the minimum value, which may cause
2829 * premature retransmissions of initial TLS flights on slow paths. Per Karn's algorithm,
2830 * no sample is taken if the SYN+ACK was resent. Any datagram from the client's address is
2831 * used, even if invalid; the sample is clamped between 1 ms and
2832 * RDPUDP2_HANDSHAKE_RTT_MAX_MS, with subsequent data samples providing corrections. */
2833 if (!udp->synAckResent && !udp->rttSampled && (now >= udp->synAckSent))
2834 {
2835 UINT64 sample = now - udp->synAckSent;
2836 if (sample < 1)
2837 sample = 1;
2838 if (sample > RDPUDP2_HANDSHAKE_RTT_MAX_MS)
2839 sample = RDPUDP2_HANDSHAKE_RTT_MAX_MS;
2840 udp->srtt = WINPR_ASSERTING_INT_CAST(UINT32, sample);
2841 udp->rttvar = udp->srtt / 2;
2842 udp->rttSampled = TRUE;
2843 }
2844 udp->synAckSent = 0;
2845 }
2846 v3_note_silence(udp, now);
2847 udp->lastReceived = now;
2848
2849 switch (udp->state)
2850 {
2851 case RDPUDP_STATE_SYN_SENT:
2852 return process_syn_ack(udp, data, length);
2853 case RDPUDP_STATE_ESTABLISHED:
2854 if (udp->version == RDPUDP_PROTOCOL_VERSION_3)
2855 return v3_process_datagram(udp, data, length);
2856 return v1_process_datagram(udp, data, length);
2857 default:
2858 return TRUE;
2859 }
2860}
2861
2862/* ------------------------------------------------------------------------------------------ */
2863/* Timers */
2864/* ------------------------------------------------------------------------------------------ */
2865
2866WINPR_ATTR_NODISCARD
2867static BOOL run_timers(rdpUdp* udp)
2868{
2869 WINPR_ASSERT(udp);
2870 const UINT64 now = now_ms(udp);
2871
2872 if (udp->state == RDPUDP_STATE_SYN_SENT)
2873 {
2874 if (now - udp->synSent < RDPUDP_SYN_TIMEOUT_MS)
2875 return TRUE;
2876 if (udp->synAttempts >= RDPUDP_SYN_ATTEMPTS)
2877 {
2878 rdpudp_fail(udp, "no SYN+ACK from the server");
2879 return FALSE;
2880 }
2881 return send_syn(udp);
2882 }
2883
2884 if (udp->state != RDPUDP_STATE_ESTABLISHED)
2885 return FALSE;
2886
2887 const BOOL v3 = udp->version == RDPUDP_PROTOCOL_VERSION_3;
2888 if (now - udp->lastReceived > (v3 ? RDPUDP2_PEER_TIMEOUT_MS : RDPUDP_PEER_TIMEOUT_MS))
2889 {
2890 rdpudp_fail(udp, v3 ? "nothing heard from the peer for 16 seconds"
2891 : "nothing heard from the peer for 65 seconds");
2892 return FALSE;
2893 }
2894 v3_note_silence(udp, now);
2895
2896 /* stuck: chunks have been waiting behind a hole, and nothing moved, for the whole timeout */
2897 const UINT64 progress =
2898 (udp->bufferingSince > udp->lastDelivery) ? udp->bufferingSince : udp->lastDelivery;
2899 if ((udp->buffered > 0) && (now - progress > RDPUDP_STALL_TIMEOUT_MS))
2900 {
2901 rdpudp_fail(udp, "the stream is stuck behind a chunk the peer no longer resends");
2902 return FALSE;
2903 }
2904
2905 /* [RFC6298] 5.5: double the timeout once per expiry. If every timing-out datagram counted, a
2906 * single loss burst would max out the backoff and cause excessive wait times. */
2907 BOOL backedOff = FALSE;
2908 /* Datagrams in flight are only looked at once one of them is due. */
2909 const BOOL scan = (now >= udp->pendingDue);
2910 UINT64 nextDue = UINT64_MAX;
2911 for (size_t x = 0; scan && (x < udp->inFlight); x++)
2912 {
2913 rdpudp_pending* p = &udp->pending[x];
2914 if (p->reportHeld && (now - p->lastSent >= report_resend_interval(udp)))
2915 handle_reported_loss(udp, p);
2916 if ((now < p->due) && !p->lost)
2917 {
2918 nextDue = pending_next_due(udp, p, nextDue);
2919 continue;
2920 }
2921 /* Resends use new sequence numbers. If the gap between the newest and oldest in-flight
2922 * packets reaches the limit, the oldest is resent first to advance sendBase; others
2923 * are handled in the next scan, which occurs immediately. */
2924 if (udp->server && v3 && (p->seq != udp->sendBase) &&
2925 (seq16_diff(WINPR_ASSERTING_INT_CAST(UINT16, udp->nextCoded),
2926 WINPR_ASSERTING_INT_CAST(UINT16, udp->sendBase)) >=
2927 RDPUDP2_MAX_RESEND_SPAN))
2928 {
2929 rdpudp_pending* oldest = pending_find(udp, udp->sendBase);
2930 WINPR_ASSERT(oldest);
2931 oldest->lost = TRUE;
2932 nextDue = 0;
2933 continue;
2934 }
2935 const BOOL reported = p->lost;
2936 if (p->lost)
2937 {
2938 /* Reported missing: retransmit immediately without increasing RTO. Reordering may cause
2939 * repeated reports, so this doesn't count toward the limit; peer timeout and stall
2940 * checks handle dead connections. */
2941 p->lost = FALSE;
2942 }
2943 else
2944 {
2945 if (p->timeouts >= (v3 ? RDPUDP2_MAX_RETRANSMITS : RDPUDP_MAX_RETRANSMITS))
2946 {
2947 rdpudp_fail(udp, "a datagram went unacknowledged too many times");
2948 return FALSE;
2949 }
2950 p->timeouts++;
2951 UINT64 maxRto = v3 ? rto_v3_cap(udp) : RDPUDP_MAX_RTO_MS;
2952 if (maxRto > RDPUDP_MAX_RTO_MS)
2953 maxRto = RDPUDP_MAX_RTO_MS;
2954 p->rto =
2955 (p->rto * 2ull > maxRto) ? WINPR_ASSERTING_INT_CAST(UINT32, maxRto) : p->rto * 2;
2956 if (udp->server)
2957 {
2958 if (!backedOff && (udp->rtoBackoff < RDPUDP2_MAX_RTO_BACKOFF))
2959 udp->rtoBackoff++;
2960 backedOff = TRUE;
2961 cc_on_loss(udp);
2962 }
2963 }
2964 p->retransmits++;
2965 p->due = now + p->rto;
2966 if (udp->server)
2967 {
2968 p->lastSent = now;
2969 p->reportHeld = FALSE;
2970 }
2971 if (udp->version == RDPUDP_PROTOCOL_VERSION_3)
2972 {
2973 /* [MS-RDPEUDP2] 3.1.5.1.3: a lost packet is resent under a new data sequence
2974 * number, the channel sequence number stays. The AckOfAcks then moves past the
2975 * old one, so the peer stops waiting for it. */
2976 const UINT16 coded = WINPR_ASSERTING_INT_CAST(UINT16, udp->nextCoded);
2977 if (udp->pendingIndex[coded] != 0)
2978 {
2979 rdpudp_fail(udp, "a datagram is still in flight 65536 sequence numbers back");
2980 return FALSE;
2981 }
2982 if (udp->server)
2983 {
2984 udp->resent[udp->resentNext].valid = TRUE;
2985 udp->resent[udp->resentNext].reported = reported;
2986 udp->resent[udp->resentNext].seq = WINPR_ASSERTING_INT_CAST(UINT16, p->seq);
2987 udp->resent[udp->resentNext].at = now;
2988 udp->resentNext = (udp->resentNext + 1) % RDPUDP2_RESENT_HISTORY;
2989 }
2990 udp->pendingIndex[p->seq & 0xFFFF] = 0;
2991 udp->pendingIndex[coded] = WINPR_ASSERTING_INT_CAST(UINT16, x + 1);
2992 p->seq = coded;
2993 udp->nextCoded = seq16_add(coded, 1);
2994 send_base_advance(udp);
2995 }
2996 else
2997 {
2998 udp->cwrPending = TRUE;
2999 }
3000 WLog_Print(udp->log, WLOG_TRACE, "retransmitting 0x%08" PRIx32 " (#%" PRIu32 ")", p->seq,
3001 p->retransmits);
3002 if (!rdpudp_transmit(udp, p))
3003 return FALSE;
3004 nextDue = pending_next_due(udp, p, nextDue);
3005 }
3006 if (scan)
3007 udp->pendingDue = nextDue;
3008
3009 /* A resend above may have set lastSent after now was read: nothing is due then. */
3010 const BOOL baseKeepalive = v3 && udp->server && udp->v3Resync;
3011 const UINT64 keepalive = baseKeepalive ? RDPUDP2_RESYNC_KEEPALIVE_MS
3012 : (v3 ? RDPUDP2_KEEPALIVE_MS : RDPUDP_KEEPALIVE_MS);
3013 if ((now >= udp->lastSent) && (now - udp->lastSent >= keepalive))
3014 {
3015 if (baseKeepalive)
3016 {
3017 if (!v3_send_base_keepalive(udp))
3018 return FALSE;
3019 }
3020 else if (!rdpudp_send_ack(udp))
3021 return FALSE;
3022 udp->lastSent = now;
3023 }
3024
3025 return TRUE;
3026}
3027
3028/* ------------------------------------------------------------------------------------------ */
3029/* Public interface */
3030/* ------------------------------------------------------------------------------------------ */
3031
3032rdpUdp* rdpudp_new(wLog* log, const BYTE* cookieHash, rdpUdpReceiveFn receive, void* custom)
3033{
3034 WINPR_ASSERT(log);
3035 WINPR_ASSERT(receive);
3036
3037 rdpUdp* udp = calloc(1, sizeof(rdpUdp));
3038 if (!udp)
3039 return nullptr;
3040
3041 udp->log = log;
3042 udp->sockfd = INVALID_SOCKET;
3043 udp->ownsSocket = TRUE;
3044 udp->state = RDPUDP_STATE_CLOSED;
3045 udp->receive = receive;
3046 udp->custom = custom;
3047 udp->mtu = RDPUDP_MTU;
3048 udp->cwnd = RDPUDP2_INITIAL_CWND;
3049 udp->ssthresh = RDPUDP_MAX_IN_FLIGHT;
3050 /* Set above half the sequence space to ensure that sequence numbers behind the window never
3051 * reach 0, which is used to mark an empty slot. */
3052 udp->v3ExpectedFull = 0x10000;
3053 udp->rttMin = UINT32_MAX;
3054 udp->ccRoundRttMin = UINT32_MAX;
3055 udp->reorderThreshold = RDPUDP2_REORDER_THRESHOLD;
3056 udp->chunkReach = RDPUDP_RECEIVE_REACH;
3057 udp->offerV3 = (cookieHash != nullptr);
3058 udp->version = udp->offerV3 ? RDPUDP_PROTOCOL_VERSION_3 : RDPUDP_PROTOCOL_VERSION_2;
3059 if (cookieHash)
3060 memcpy(udp->cookieHash, cookieHash, sizeof(udp->cookieHash));
3061
3062 udp->sendQueue = Queue_New(FALSE, -1, -1);
3063 if (!udp->sendQueue)
3064 goto fail;
3065 wObject* obj = Queue_Object(udp->sendQueue);
3066 if (!obj)
3067 goto fail;
3068 obj->fnObjectFree = chunk_free;
3069
3070 if (winpr_RAND(&udp->initialSequence, sizeof(udp->initialSequence)) < 0)
3071 goto fail;
3072
3073 return udp;
3074fail:
3075 WINPR_PRAGMA_DIAG_PUSH
3076 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
3077 rdpudp_free(udp);
3078 WINPR_PRAGMA_DIAG_POP
3079 return nullptr;
3080}
3081
3082void rdpudp_free(rdpUdp* udp)
3083{
3084 if (!udp)
3085 return;
3086
3087 if (udp->ownsSocket && (udp->sockfd != INVALID_SOCKET))
3088 closesocket(udp->sockfd);
3089
3090 for (size_t x = 0; x < udp->inFlight; x++)
3091 pending_clear(&udp->pending[x]);
3092 for (size_t x = 0; x < RDPUDP_RECEIVE_SLOTS; x++)
3093 slot_clear(&udp->slots[x]);
3094 if (udp->sendQueue)
3095 {
3096 /* Queue_Free() misses the chunks of a queue that is exactly full, so they are taken out
3097 * first */
3098 rdpudp_chunk* chunk = nullptr;
3099 while ((chunk = Queue_Dequeue(udp->sendQueue)))
3100 chunk_free(chunk);
3101 Queue_Free(udp->sendQueue);
3102 }
3103 free(udp);
3104}
3105
3106WINPR_ATTR_NODISCARD
3107static BOOL set_non_blocking(SOCKET sockfd)
3108{
3109 u_long arg = 1;
3110 return _ioctlsocket(sockfd, FIONBIO, &arg) == 0;
3111}
3112
3113BOOL rdpudp_connect(rdpUdp* udp, const struct sockaddr* addr, size_t addrlen)
3114{
3115 WINPR_ASSERT(udp);
3116 WINPR_ASSERT(addr);
3117
3118 if (udp->state != RDPUDP_STATE_CLOSED)
3119 return FALSE;
3120
3121 udp->sockfd = _socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
3122 if (udp->sockfd == INVALID_SOCKET)
3123 {
3124 WLog_Print(udp->log, WLOG_ERROR, "failed to create the UDP socket");
3125 return FALSE;
3126 }
3127
3128 /* The system can grant less than asked, Linux caps it at net.core.rmem_max. A smaller buffer
3129 * only costs resends, so a failure is not fatal. */
3130 const UINT32 optval = RDPUDP_RECEIVE_BUFFER;
3131 if (_setsockopt(udp->sockfd, SOL_SOCKET, SO_RCVBUF, (const char*)&optval,
3132 WINPR_ASSERTING_INT_CAST(int, sizeof(optval))) != 0)
3133 WLog_Print(udp->log, WLOG_WARN, "setsockopt() SOL_SOCKET, SO_RCVBUF failed");
3134
3135 if (_connect(udp->sockfd, addr, WINPR_ASSERTING_INT_CAST(int, addrlen)) != 0)
3136 {
3137 WLog_Print(udp->log, WLOG_ERROR, "failed to connect the UDP socket");
3138 return FALSE;
3139 }
3140
3141 if (!set_non_blocking(udp->sockfd))
3142 return FALSE;
3143
3144 udp->state = RDPUDP_STATE_SYN_SENT;
3145 udp->lastReceived = now_ms(udp);
3146 return send_syn(udp);
3147}
3148
3149/* [MS-RDPEUDP] 3.1.5.1.3: The SYN+ACK is a SYN that acknowledges the client's
3150 * snInitialSequenceNumber and carries the negotiated MTU and version. Like the
3151 * SYN, it is zero-padded to the MTU, but contains no cookieHash. */
3152WINPR_ATTR_NODISCARD
3153static BOOL build_syn_ack(rdpUdp* udp, UINT32 clientInitial)
3154{
3155 WINPR_ASSERT(udp);
3156 /* rdpudp_accept clamps the MTU to the range [RDPUDP_MIN_MTU, RDPUDP_MTU], where
3157 * RDPUDP_MTU is the size of synAck. */
3158 if ((udp->mtu < RDPUDP_MIN_MTU) || (udp->mtu > sizeof(udp->synAck)))
3159 return FALSE;
3160 memset(udp->synAck, 0, sizeof(udp->synAck));
3161 wStream sbuffer = WINPR_C_ARRAY_INIT;
3162 wStream* s = Stream_StaticInit(&sbuffer, udp->synAck, sizeof(udp->synAck));
3163 if (!s)
3164 return FALSE;
3165
3166 /* Write the RDPUDP_FEC_HEADER */
3167 Stream_Write_UINT32_BE(
3168 s, clientInitial); /* Write the source acknowledgement sequence number (snSourceAck) */
3169 Stream_Write_UINT16_BE(s, RDPUDP_RECEIVE_WINDOW);
3170 Stream_Write_UINT16_BE(s, RDPUDP_FLAG_SYN | RDPUDP_FLAG_ACK | RDPUDP_FLAG_SYNEX);
3171
3172 /* Write the RDPUDP_SYNDATA_PAYLOAD */
3173 Stream_Write_UINT32_BE(s, udp->initialSequence);
3174 Stream_Write_UINT16_BE(s, udp->mtu); /* Set the upstream MTU (uUpStreamMtu) */
3175 Stream_Write_UINT16_BE(s, udp->mtu); /* Downstream MTU */
3176
3177 /* Write the RDPUDP_SYNDATAEX_PAYLOAD */
3178 Stream_Write_UINT16_BE(s, RDPUDP_VERSION_INFO_VALID);
3179 Stream_Write_UINT16_BE(s, udp->version);
3180
3181 udp->synAckLength = udp->mtu;
3182 return TRUE;
3183}
3184
3185BOOL rdpudp_accept(rdpUdp* udp, rdpUdpSendFn send, void* sendCustom, const BYTE* syn, size_t length)
3186{
3187 WINPR_ASSERT(udp);
3188 WINPR_ASSERT(send);
3189 WINPR_ASSERT(syn);
3190
3191 if ((udp->state != RDPUDP_STATE_CLOSED) || !is_syn(syn, length))
3192 return FALSE;
3193
3194 wStream sbuffer = WINPR_C_ARRAY_INIT;
3195 wStream* s = Stream_StaticConstInit(&sbuffer, syn, length);
3196 if (!s || !Stream_CheckAndLogRequiredLengthWLog(udp->log, s, 16))
3197 return FALSE;
3198
3199 /* Parse the RDPUDP_FEC_HEADER */
3200 Stream_Seek_UINT32(s); /* Skip snSourceAck, which is expected to be -1 */
3201 const UINT16 window = Stream_Get_UINT16_BE(s);
3202 const UINT16 flags = Stream_Get_UINT16_BE(s);
3203
3204 /* Parse RDPUDP_SYNDATA_PAYLOAD */
3205 const UINT32 clientInitial = Stream_Get_UINT32_BE(s);
3206 const UINT16 upMtu = Stream_Get_UINT16_BE(s);
3207 const UINT16 downMtu = Stream_Get_UINT16_BE(s);
3208
3209 if ((flags & RDPUDP_FLAG_CORRELATION_ID) && !Stream_SafeSeek(s, 32))
3210 return FALSE;
3211
3212 /* RDPUDP_SYNDATAEX_PAYLOAD. The listener already matched a version 3 SYN's cookieHash to this
3213 * connection; other versions lack a cookieHash and are indistinguishable. */
3214 UINT16 version = RDPUDP_PROTOCOL_VERSION_1;
3215 if ((flags & RDPUDP_FLAG_SYNEX) && (Stream_GetRemainingLength(s) >= 4))
3216 {
3217 const UINT16 synExFlags = Stream_Get_UINT16_BE(s);
3218 const UINT16 udpVersion = Stream_Get_UINT16_BE(s);
3219 if (synExFlags & RDPUDP_VERSION_INFO_VALID)
3220 version = udpVersion;
3221 }
3222 if (version != RDPUDP_PROTOCOL_VERSION_3)
3223 {
3224 WLog_Print(udp->log, WLOG_WARN,
3225 "client offered RDP-UDP version 0x%04" PRIx16 ", only 0x%04" PRIx16
3226 " is implemented for a server",
3227 version, RDPUDP_PROTOCOL_VERSION_3);
3228 return FALSE;
3229 }
3230
3231 /* [MS-RDPEUDP] 3.1.1.3: neither end sends more than the smaller of the two MTUs. */
3232 UINT16 mtu = (upMtu < downMtu) ? upMtu : downMtu;
3233 if (mtu > RDPUDP_MTU)
3234 mtu = RDPUDP_MTU;
3235 if (mtu < RDPUDP_MIN_MTU)
3236 mtu = RDPUDP_MIN_MTU;
3237
3238 /* [MS-RDPEUDP] 3.1.5.1.1: SYN is padded to the smaller MTU, and SYN+ACK is mtu bytes. A
3239 * shorter SYN may indicate a spoofed source, as our reply would be larger than the received
3240 * packet. */
3241 if (length < mtu)
3242 {
3243 WLog_Print(udp->log, WLOG_WARN,
3244 "rejecting a SYN of %" PRIuz " bytes, which is shorter than the MTU of %" PRIu16,
3245 length, mtu);
3246 return FALSE;
3247 }
3248
3249 udp->server = TRUE;
3250 udp->ownsSocket = FALSE;
3251 udp->sendFn = send;
3252 udp->sendCustom = sendCustom;
3253 udp->chunkReach = RDPUDP_UNVERIFIED_REACH;
3254
3255 udp->mtu = mtu;
3256 udp->version = version;
3257 udp->peerWindow = window;
3258 udp->lastReceived = now_ms(udp);
3259 udp->lastDelivery = udp->lastReceived;
3260 udp->state = RDPUDP_STATE_ESTABLISHED;
3261
3262 /* Same as client; see process_syn_ack */
3263 udp->nextCoded = RDPUDP2_INITIAL_SEQUENCE;
3264 udp->nextChannelSeq = 1;
3265 udp->v3NextChannel = 1;
3266 udp->v3MaxDelayedAcks = RDPUDP2_DEFAULT_DELAYED_ACKS;
3267 udp->v3DelayedAckTimeout = UINT32_MAX;
3268
3269 if (!build_syn_ack(udp, clientInitial))
3270 return FALSE;
3271 WLog_Print(udp->log, WLOG_INFO,
3272 "accepted RDP-UDP client, protocol version 0x%04" PRIx16 ", MTU %" PRIu16, version,
3273 mtu);
3274 udp->synAckSent = now_ms(udp);
3275 if (udp->synAckSent == 0)
3276 udp->synAckSent = 1; /* 0 indicates not sent; a test clock might start at 0 */
3277 return rdpudp_send_raw(udp, udp->synAck, udp->synAckLength);
3278}
3279
3280BOOL rdpudp_get_syn_cookie_hash(const BYTE* data, size_t length, BYTE hash[32])
3281{
3282 WINPR_ASSERT(hash);
3283 /* SYN is padded to MTU, which is at least RDPUDP_MIN_MTU ([MS-RDPEUDP] 3.1.5.1.1) */
3284 if ((length < RDPUDP_MIN_MTU) || !is_syn(data, length))
3285 return FALSE;
3286
3287 wStream sbuffer = WINPR_C_ARRAY_INIT;
3288 wStream* s = Stream_StaticConstInit(&sbuffer, data, length);
3289 if (!s || !Stream_SafeSeek(s, 6)) /* Skip snSourceAck and uReceiveWindowSize */
3290 return FALSE;
3291 const UINT16 flags = Stream_Get_UINT16_BE(s);
3292 if (!(flags & RDPUDP_FLAG_SYNEX) || !Stream_SafeSeek(s, 8)) /* Skip RDPUDP_SYNDATA_PAYLOAD */
3293 return FALSE;
3294 if ((flags & RDPUDP_FLAG_CORRELATION_ID) && !Stream_SafeSeek(s, 32))
3295 return FALSE;
3296 if (Stream_GetRemainingLength(s) < 4 + 32)
3297 return FALSE;
3298 const UINT16 synExFlags = Stream_Get_UINT16_BE(s);
3299 const UINT16 version = Stream_Get_UINT16_BE(s);
3300 if (!(synExFlags & RDPUDP_VERSION_INFO_VALID) || (version != RDPUDP_PROTOCOL_VERSION_3))
3301 return FALSE;
3302 Stream_Read(s, hash, 32);
3303 return TRUE;
3304}
3305
3306BOOL rdpudp_receive(rdpUdp* udp, BYTE* data, size_t length)
3307{
3308 WINPR_ASSERT(udp);
3309 WINPR_ASSERT(data); /* process_datagram requires a buffer even for empty datagrams */
3310
3311 if (udp->state != RDPUDP_STATE_ESTABLISHED)
3312 return FALSE;
3313 if (!process_datagram(udp, data, length))
3314 {
3315 rdpudp_fail(udp, "failed to process a datagram");
3316 return FALSE;
3317 }
3318 if (udp->state == RDPUDP_STATE_FAILED)
3319 return FALSE;
3320 if (!v3_ack_if_due(udp))
3321 return FALSE;
3322 if (!ack_when_due(udp))
3323 return FALSE;
3324 if (!run_timers(udp))
3325 return FALSE;
3326 return flush_send_queue(udp);
3327}
3328
3329SOCKET rdpudp_get_socket(const rdpUdp* udp)
3330{
3331 WINPR_ASSERT(udp);
3332 return udp->sockfd;
3333}
3334
3335RDPUDP_STATE rdpudp_get_state(const rdpUdp* udp)
3336{
3337 WINPR_ASSERT(udp);
3338 return udp->state;
3339}
3340
3341UINT16 rdpudp_get_version(const rdpUdp* udp)
3342{
3343 WINPR_ASSERT(udp);
3344 return udp->version;
3345}
3346
3347size_t rdpudp_get_max_payload(const rdpUdp* udp)
3348{
3349 WINPR_ASSERT(udp);
3350 /* the MTU is clamped to [RDPUDP_MIN_MTU, RDPUDP_MTU] when the connection is set up */
3351 WINPR_ASSERT(udp->mtu >= RDPUDP_MIN_MTU);
3352 /* v3: the DATA datagram fields and room for some arrival gaps of the ACK it carries.
3353 * v1/v2: header, room for the ACK vector and AckOfAcks, source payload header. */
3354 const size_t overhead = (udp->version == RDPUDP_PROTOCOL_VERSION_3)
3355 ? (RDPUDP2_DATA_OVERHEAD + RDPUDP2_DATA_ACK_ROOM)
3356 : (8u + RDPUDP_ACK_ROOM + 8u);
3357 if (udp->mtu <= overhead)
3358 return 0;
3359 return udp->mtu - overhead;
3360}
3361
3362size_t rdpudp_get_send_backlog(const rdpUdp* udp)
3363{
3364 WINPR_ASSERT(udp);
3365 return Queue_Count(udp->sendQueue);
3366}
3367
3368size_t rdpudp_get_send_window(const rdpUdp* udp)
3369{
3370 return window_limit(udp);
3371}
3372
3373UINT32 rdpudp_get_rtt(const rdpUdp* udp)
3374{
3375 WINPR_ASSERT(udp);
3376 if (!udp->rttSampled)
3377 return 0;
3378 /* Samples below clock resolution are still valid samples */
3379 return (udp->srtt > 0) ? udp->srtt : 1;
3380}
3381
3382UINT32 rdpudp_get_timeout(const rdpUdp* udp)
3383{
3384 WINPR_ASSERT(udp);
3385 const UINT64 now = now_ms(udp);
3386 UINT64 timeout = 50;
3387
3388 if (udp->state == RDPUDP_STATE_SYN_SENT)
3389 {
3390 const UINT64 due = udp->synSent + RDPUDP_SYN_TIMEOUT_MS;
3391 if (due <= now)
3392 return 0;
3393 if (due - now < timeout)
3394 timeout = due - now;
3395 return WINPR_ASSERTING_INT_CAST(UINT32, timeout);
3396 }
3397
3398 if (udp->inFlight > 0)
3399 {
3400 if (udp->pendingDue <= now)
3401 return 0;
3402 if (udp->pendingDue - now < timeout)
3403 timeout = udp->pendingDue - now;
3404 }
3405
3406 /* an acknowledgement that waits, see ack_when_due */
3407 if (udp->ackPending && (udp->v3AckDue != 0))
3408 {
3409 if (udp->v3AckDue <= now)
3410 return 0;
3411 if (udp->v3AckDue - now < timeout)
3412 timeout = udp->v3AckDue - now;
3413 }
3414 return WINPR_ASSERTING_INT_CAST(UINT32, timeout);
3415}
3416
3417BOOL rdpudp_check(rdpUdp* udp)
3418{
3419 WINPR_ASSERT(udp);
3420
3421 if ((udp->state == RDPUDP_STATE_FAILED) || (udp->state == RDPUDP_STATE_CLOSED))
3422 return FALSE;
3423
3424 BYTE* buffer = udp->recvBuffer;
3425 /* Process at most RDPUDP_INPUT_BUDGET datagrams per call. A peer sending faster than they are
3426 * processed would otherwise keep the socket from draining, and the retransmission timers
3427 * below would never run. The socket stays readable for the next call. */
3428 for (size_t count = 0; udp->ownsSocket && (count < RDPUDP_INPUT_BUDGET); count++)
3429 {
3430 const int rc = _recv(udp->sockfd, (char*)buffer,
3431 WINPR_ASSERTING_INT_CAST(int, sizeof(udp->recvBuffer)), 0);
3432 if (rc < 0)
3433 {
3434 const int error = WSAGetLastError();
3435 if (!rdpudp_socket_error_is_transient(error))
3436 {
3437 WLog_Print(udp->log, WLOG_ERROR, "recv failed with %d", error);
3438 rdpudp_fail(udp, "receiving from the socket failed");
3439 return FALSE;
3440 }
3441 /* Nothing left to read, or an ICMP error for an earlier datagram. Anything still
3442 * queued keeps the socket readable and is read on the next round. */
3443 if (error != WSAEWOULDBLOCK)
3444 WLog_Print(udp->log, WLOG_DEBUG, "recv failed with %d", error);
3445 break;
3446 }
3447
3448 if (!process_datagram(udp, buffer, WINPR_ASSERTING_INT_CAST(size_t, rc)))
3449 {
3450 rdpudp_fail(udp, "failed to process a datagram");
3451 return FALSE;
3452 }
3453 if (udp->state == RDPUDP_STATE_FAILED)
3454 return FALSE;
3455 if (!v3_ack_if_due(udp))
3456 return FALSE;
3457 }
3458
3459 if (!ack_when_due(udp))
3460 return FALSE;
3461
3462 if (!flush_send_queue(udp))
3463 return FALSE;
3464
3465 if (!run_timers(udp))
3466 return FALSE;
3467 /* Server: flush queued chunks that now fit the window instead of waiting for wakeup */
3468 if (udp->server)
3469 return flush_send_queue(udp);
3470 return TRUE;
3471}
3472
3473void rdpudp_set_peer_verified(rdpUdp* udp)
3474{
3475 WINPR_ASSERT(udp);
3476 udp->chunkReach = RDPUDP_RECEIVE_REACH;
3477}
3478
3479void rdpudp_set_receive_hold(rdpUdp* udp, BOOL hold)
3480{
3481 WINPR_ASSERT(udp);
3482 udp->receiveHold = hold;
3483}
3484
3485void rdpudp_set_clock(rdpUdp* udp, rdpUdpClockFn clock, void* custom)
3486{
3487 WINPR_ASSERT(udp);
3488 udp->clock = clock;
3489 udp->clockCustom = custom;
3490}
3491
3492UINT64 rdpudp_get_time(const rdpUdp* udp)
3493{
3494 return now_ms(udp);
3495}
Layout layout(float leftWidth, float leftHeight, float rightWidth, float rightHeight, float width, float margin)
This struct contains function pointer to initialize/free objects.
Definition collections.h:52