2 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef included_tcp_packet_h
17 #define included_tcp_packet_h
19 #include <vnet/ip/ip4_packet.h>
20 #include <vnet/ip/ip6_packet.h>
22 /* TCP flags bit 0 first. */
23 #define foreach_tcp_flag \
24 _ (FIN) /**< No more data from sender. */ \
25 _ (SYN) /**< Synchronize sequence numbers. */ \
26 _ (RST) /**< Reset the connection. */ \
27 _ (PSH) /**< Push function. */ \
28 _ (ACK) /**< Ack field significant. */ \
29 _ (URG) /**< Urgent pointer field significant. */ \
30 _ (ECE) /**< ECN-echo. Receiver got CE packet */ \
31 _ (CWR) /**< Sender reduced congestion window */
35 #define _(f) TCP_FLAG_BIT_##f,
43 #define _(f) TCP_FLAG_##f = 1 << TCP_FLAG_BIT_##f,
48 typedef struct _tcp_header
54 u16 src_port; /**< Source port. */
55 u16 dst_port; /**< Destination port. */
63 u32 seq_number; /**< Sequence number of the first data octet in this
64 * segment, except when SYN is present. If SYN
65 * is present the seq number is is the ISN and the
66 * first data octet is ISN+1 */
67 u32 ack_number; /**< Acknowledgement number if ACK is set. It contains
68 * the value of the next sequence number the sender
69 * of the segment is expecting to receive. */
70 u8 data_offset_and_reserved;
71 u8 flags; /**< Flags: see the macro above */
72 u16 window; /**< Number of bytes sender is willing to receive. */
74 u16 checksum; /**< Checksum of TCP pseudo header and data. */
75 u16 urgent_pointer; /**< Seq number of the byte after the urgent data. */
76 } __attribute__ ((packed)) tcp_header_t;
78 /* Flag tests that return 0 or !0 */
79 #define tcp_doff(_th) ((_th)->data_offset_and_reserved >> 4)
80 #define tcp_fin(_th) ((_th)->flags & TCP_FLAG_FIN)
81 #define tcp_syn(_th) ((_th)->flags & TCP_FLAG_SYN)
82 #define tcp_rst(_th) ((_th)->flags & TCP_FLAG_RST)
83 #define tcp_psh(_th) ((_th)->flags & TCP_FLAG_PSH)
84 #define tcp_ack(_th) ((_th)->flags & TCP_FLAG_ACK)
85 #define tcp_urg(_th) ((_th)->flags & TCP_FLAG_URG)
86 #define tcp_ece(_th) ((_th)->flags & TCP_FLAG_ECE)
87 #define tcp_cwr(_th) ((_th)->flags & TCP_FLAG_CWR)
89 /* Flag tests that return 0 or 1 */
90 #define tcp_is_syn(_th) !!((_th)->flags & TCP_FLAG_SYN)
91 #define tcp_is_fin(_th) !!((_th)->flags & TCP_FLAG_FIN)
94 tcp_header_bytes (tcp_header_t * t)
96 return tcp_doff (t) * sizeof (u32);
103 typedef enum tcp_option_type
105 TCP_OPTION_EOL = 0, /**< End of options. */
106 TCP_OPTION_NOOP = 1, /**< No operation. */
107 TCP_OPTION_MSS = 2, /**< Limit MSS. */
108 TCP_OPTION_WINDOW_SCALE = 3, /**< Window scale. */
109 TCP_OPTION_SACK_PERMITTED = 4, /**< Selective Ack permitted. */
110 TCP_OPTION_SACK_BLOCK = 5, /**< Selective Ack block. */
111 TCP_OPTION_TIMESTAMP = 8, /**< Timestamps. */
112 TCP_OPTION_UTO = 28, /**< User timeout. */
113 TCP_OPTION_AO = 29, /**< Authentication Option. */
116 #define foreach_tcp_options_flag \
117 _ (MSS) /**< MSS advertised in SYN */ \
118 _ (TSTAMP) /**< Timestamp capability advertised in SYN */ \
119 _ (WSCALE) /**< Wnd scale capability advertised in SYN */ \
120 _ (SACK_PERMITTED) /**< SACK capability advertised in SYN */ \
121 _ (SACK) /**< SACK present */
125 #define _(f) TCP_OPTS_FLAG_BIT_##f,
126 foreach_tcp_options_flag
128 TCP_OPTIONS_N_FLAG_BITS,
133 #define _(f) TCP_OPTS_FLAG_##f = 1 << TCP_OPTS_FLAG_BIT_##f,
134 foreach_tcp_options_flag
138 typedef struct _sack_block
140 u32 start; /**< Start sequence number */
141 u32 end; /**< End sequence number (first outside) */
146 sack_block_t *sacks; /**< SACK blocks */
147 u32 tsval; /**< Timestamp value */
148 u32 tsecr; /**< Echoed/reflected time stamp */
149 u16 mss; /**< Maximum segment size advertised */
150 u8 flags; /**< Option flags, see above */
151 u8 wscale; /**< Window scale advertised */
152 u8 n_sack_blocks; /**< Number of SACKs blocks */
155 /* Flag tests that return 0 or !0 */
156 #define tcp_opts_mss(_to) ((_to)->flags & TCP_OPTS_FLAG_MSS)
157 #define tcp_opts_tstamp(_to) ((_to)->flags & TCP_OPTS_FLAG_TSTAMP)
158 #define tcp_opts_wscale(_to) ((_to)->flags & TCP_OPTS_FLAG_WSCALE)
159 #define tcp_opts_sack(_to) ((_to)->flags & TCP_OPTS_FLAG_SACK)
160 #define tcp_opts_sack_permitted(_to) ((_to)->flags & TCP_OPTS_FLAG_SACK_PERMITTED)
162 /* TCP option lengths */
163 #define TCP_OPTION_LEN_EOL 1
164 #define TCP_OPTION_LEN_NOOP 1
165 #define TCP_OPTION_LEN_MSS 4
166 #define TCP_OPTION_LEN_WINDOW_SCALE 3
167 #define TCP_OPTION_LEN_SACK_PERMITTED 2
168 #define TCP_OPTION_LEN_TIMESTAMP 10
169 #define TCP_OPTION_LEN_SACK_BLOCK 8
171 #define TCP_HDR_LEN_MAX 60
172 #define TCP_WND_MAX 65535U
173 #define TCP_MAX_WND_SCALE 14 /* See RFC 1323 */
174 #define TCP_OPTS_ALIGN 4
175 #define TCP_OPTS_MAX_SACK_BLOCKS 3
176 #define TCP_MAX_GSO_SZ 65536
178 /* Modulo arithmetic for TCP sequence numbers */
179 #define seq_lt(_s1, _s2) ((i32)((_s1)-(_s2)) < 0)
180 #define seq_leq(_s1, _s2) ((i32)((_s1)-(_s2)) <= 0)
181 #define seq_gt(_s1, _s2) ((i32)((_s1)-(_s2)) > 0)
182 #define seq_geq(_s1, _s2) ((i32)((_s1)-(_s2)) >= 0)
183 #define seq_max(_s1, _s2) (seq_gt((_s1), (_s2)) ? (_s1) : (_s2))
185 /* Modulo arithmetic for timestamps */
186 #define timestamp_lt(_t1, _t2) ((i32)((_t1)-(_t2)) < 0)
187 #define timestamp_leq(_t1, _t2) ((i32)((_t1)-(_t2)) <= 0)
190 ip4_tcp_reply_x1 (ip4_header_t *ip0, tcp_header_t *tcp0)
194 src0 = ip0->src_address.data_u32;
195 dst0 = ip0->dst_address.data_u32;
196 ip0->src_address.data_u32 = dst0;
197 ip0->dst_address.data_u32 = src0;
206 ip4_tcp_reply_x2 (ip4_header_t *ip0, ip4_header_t *ip1, tcp_header_t *tcp0,
209 u32 src0, dst0, src1, dst1;
211 src0 = ip0->src_address.data_u32;
212 src1 = ip1->src_address.data_u32;
213 dst0 = ip0->dst_address.data_u32;
214 dst1 = ip1->dst_address.data_u32;
215 ip0->src_address.data_u32 = dst0;
216 ip1->src_address.data_u32 = dst1;
217 ip0->dst_address.data_u32 = src0;
218 ip1->dst_address.data_u32 = src1;
231 ip6_tcp_reply_x1 (ip6_header_t *ip0, tcp_header_t *tcp0)
234 ip6_address_t src0, dst0;
236 src0 = ip0->src_address;
237 dst0 = ip0->dst_address;
238 ip0->src_address = dst0;
239 ip0->dst_address = src0;
253 ip6_tcp_reply_x2 (ip6_header_t *ip0, ip6_header_t *ip1, tcp_header_t *tcp0,
257 ip6_address_t src0, dst0, src1, dst1;
259 src0 = ip0->src_address;
260 src1 = ip1->src_address;
261 dst0 = ip0->dst_address;
262 dst1 = ip1->dst_address;
263 ip0->src_address = dst0;
264 ip1->src_address = dst1;
265 ip0->dst_address = src0;
266 ip1->dst_address = src1;
270 u16 src0, dst0, src1, dst1;
284 * Parse TCP header options.
286 * @param th TCP header
287 * @param to TCP options data structure to be populated
288 * @param is_syn set if packet is syn
289 * @return -1 if parsing failed
292 tcp_options_parse (tcp_header_t * th, tcp_options_t * to, u8 is_syn)
295 u8 opt_len, opts_len, kind;
299 opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t);
300 data = (const u8 *) (th + 1);
302 /* Zero out all flags but those set in SYN */
303 to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE
304 | TCP_OPTS_FLAG_TSTAMP | TCP_OPTS_FLAG_MSS);
306 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
310 /* Get options length */
311 if (kind == TCP_OPTION_EOL)
313 else if (kind == TCP_OPTION_NOOP)
325 /* weird option length */
326 if (opt_len < 2 || opt_len > opts_len)
336 if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th))
338 to->flags |= TCP_OPTS_FLAG_MSS;
339 to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2));
342 case TCP_OPTION_WINDOW_SCALE:
345 if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th))
347 to->flags |= TCP_OPTS_FLAG_WSCALE;
348 to->wscale = data[2];
349 if (to->wscale > TCP_MAX_WND_SCALE)
350 to->wscale = TCP_MAX_WND_SCALE;
353 case TCP_OPTION_TIMESTAMP:
355 to->flags |= TCP_OPTS_FLAG_TSTAMP;
356 if ((to->flags & TCP_OPTS_FLAG_TSTAMP)
357 && opt_len == TCP_OPTION_LEN_TIMESTAMP)
359 to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2));
360 to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6));
363 case TCP_OPTION_SACK_PERMITTED:
366 if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th))
367 to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
369 case TCP_OPTION_SACK_BLOCK:
370 /* If SACK permitted was not advertised or a SYN, break */
371 if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th))
374 /* If too short or not correctly formatted, break */
375 if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK))
378 to->flags |= TCP_OPTS_FLAG_SACK;
379 to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK;
380 vec_reset_length (to->sacks);
381 for (j = 0; j < to->n_sack_blocks; j++)
383 b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 8 * j));
384 b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 8 * j));
385 vec_add1 (to->sacks, b);
389 /* Nothing to see here */
397 * Write TCP options to segment.
399 * @param data buffer where to write the options
400 * @param opts options to write
401 * @return length of options written
404 tcp_options_write (u8 * data, tcp_options_t * opts)
407 u32 buf, seq_len = 4;
409 if (tcp_opts_mss (opts))
411 *data++ = TCP_OPTION_MSS;
412 *data++ = TCP_OPTION_LEN_MSS;
413 buf = clib_host_to_net_u16 (opts->mss);
414 clib_memcpy_fast (data, &buf, sizeof (opts->mss));
415 data += sizeof (opts->mss);
416 opts_len += TCP_OPTION_LEN_MSS;
419 if (tcp_opts_wscale (opts))
421 *data++ = TCP_OPTION_WINDOW_SCALE;
422 *data++ = TCP_OPTION_LEN_WINDOW_SCALE;
423 *data++ = opts->wscale;
424 opts_len += TCP_OPTION_LEN_WINDOW_SCALE;
427 if (tcp_opts_sack_permitted (opts))
429 *data++ = TCP_OPTION_SACK_PERMITTED;
430 *data++ = TCP_OPTION_LEN_SACK_PERMITTED;
431 opts_len += TCP_OPTION_LEN_SACK_PERMITTED;
434 if (tcp_opts_tstamp (opts))
436 *data++ = TCP_OPTION_TIMESTAMP;
437 *data++ = TCP_OPTION_LEN_TIMESTAMP;
438 buf = clib_host_to_net_u32 (opts->tsval);
439 clib_memcpy_fast (data, &buf, sizeof (opts->tsval));
440 data += sizeof (opts->tsval);
441 buf = clib_host_to_net_u32 (opts->tsecr);
442 clib_memcpy_fast (data, &buf, sizeof (opts->tsecr));
443 data += sizeof (opts->tsecr);
444 opts_len += TCP_OPTION_LEN_TIMESTAMP;
447 if (tcp_opts_sack (opts))
451 if (opts->n_sack_blocks != 0)
453 *data++ = TCP_OPTION_SACK_BLOCK;
454 *data++ = 2 + opts->n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
455 for (i = 0; i < opts->n_sack_blocks; i++)
457 buf = clib_host_to_net_u32 (opts->sacks[i].start);
458 clib_memcpy_fast (data, &buf, seq_len);
460 buf = clib_host_to_net_u32 (opts->sacks[i].end);
461 clib_memcpy_fast (data, &buf, seq_len);
464 opts_len += 2 + opts->n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
468 /* Terminate TCP options by padding with NOPs to a u32 boundary. Avoid using
469 * EOL because, it seems, it can break peers with broken option parsers that
470 * rely on options ending on a u32 boundary.
474 *data++ = TCP_OPTION_NOOP;
475 opts_len += TCP_OPTION_LEN_NOOP;
480 #endif /* included_tcp_packet_h */
483 * fd.io coding-style-patch-verification: ON
486 * eval: (c-set-style "gnu")