blob: 3be4592c6799b1878795379b0363994d15c6eaaf [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/*
16 * ip/tcp_pg: TCP packet-generator interface
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#include <vnet/ip/ip.h>
41#include <vnet/pg/pg.h>
42
43/* TCP flags bit 0 first. */
44#define foreach_tcp_flag \
45 _ (FIN) \
46 _ (SYN) \
47 _ (RST) \
48 _ (PSH) \
49 _ (ACK) \
50 _ (URG) \
51 _ (ECE) \
52 _ (CWR)
53
54static void
55tcp_pg_edit_function (pg_main_t * pg,
56 pg_stream_t * s,
Dave Barach63681512017-04-20 17:50:39 -040057 pg_edit_group_t * g, u32 * packets, u32 n_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -050058{
Dave Barach63681512017-04-20 17:50:39 -040059 vlib_main_t *vm = vlib_get_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -050060 u32 ip_offset, tcp_offset;
61
62 tcp_offset = g->start_byte_offset;
Dave Barach63681512017-04-20 17:50:39 -040063 ip_offset = (g - 1)->start_byte_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -050064
65 while (n_packets >= 1)
66 {
Dave Barach63681512017-04-20 17:50:39 -040067 vlib_buffer_t *p0;
68 ip4_header_t *ip0;
69 tcp_header_t *tcp0;
Dave Barach68b0fb02017-02-28 15:15:56 -050070 ip_csum_t sum0;
71 u32 tcp_len0;
72
73 p0 = vlib_get_buffer (vm, packets[0]);
74 n_packets -= 1;
75 packets += 1;
76
77 ASSERT (p0->current_data == 0);
78 ip0 = (void *) (p0->data + ip_offset);
79 tcp0 = (void *) (p0->data + tcp_offset);
80 tcp_len0 = clib_net_to_host_u16 (ip0->length) - sizeof (ip0[0]);
81
82 /* Initialize checksum with header. */
83 if (BITS (sum0) == 32)
84 {
85 sum0 = clib_mem_unaligned (&ip0->src_address, u32);
Dave Barach63681512017-04-20 17:50:39 -040086 sum0 =
87 ip_csum_with_carry (sum0,
88 clib_mem_unaligned (&ip0->dst_address, u32));
Dave Barach68b0fb02017-02-28 15:15:56 -050089 }
90 else
91 sum0 = clib_mem_unaligned (&ip0->src_address, u64);
92
93 sum0 = ip_csum_with_carry
94 (sum0, clib_host_to_net_u32 (tcp_len0 + (ip0->protocol << 16)));
95
96 /* Invalidate possibly old checksum. */
97 tcp0->checksum = 0;
98
Dave Barach63681512017-04-20 17:50:39 -040099 sum0 =
100 ip_incremental_checksum_buffer (vm, p0, tcp_offset, tcp_len0, sum0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500101
Dave Barach63681512017-04-20 17:50:39 -0400102 tcp0->checksum = ~ip_csum_fold (sum0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500103 }
104}
105
Dave Barach63681512017-04-20 17:50:39 -0400106typedef struct
107{
Dave Barach68b0fb02017-02-28 15:15:56 -0500108 pg_edit_t src, dst;
109 pg_edit_t seq_number, ack_number;
110 pg_edit_t data_offset_and_reserved;
111#define _(f) pg_edit_t f##_flag;
Dave Barach63681512017-04-20 17:50:39 -0400112 foreach_tcp_flag
Dave Barach68b0fb02017-02-28 15:15:56 -0500113#undef _
Dave Barach63681512017-04-20 17:50:39 -0400114 pg_edit_t window;
Dave Barach68b0fb02017-02-28 15:15:56 -0500115 pg_edit_t checksum;
116 pg_edit_t urgent_pointer;
117} pg_tcp_header_t;
118
119static inline void
120pg_tcp_header_init (pg_tcp_header_t * p)
121{
122 /* Initialize fields that are not bit fields in the IP header. */
123#define _(f) pg_edit_init (&p->f, tcp_header_t, f);
Dave Barach63681512017-04-20 17:50:39 -0400124 _(src);
125 _(dst);
126 _(seq_number);
127 _(ack_number);
128 _(window);
129 _(checksum);
130 _(urgent_pointer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500131#undef _
132
133 /* Initialize bit fields. */
134#define _(f) \
135 pg_edit_init_bitfield (&p->f##_flag, tcp_header_t, \
136 flags, \
137 TCP_FLAG_BIT_##f, 1);
138
139 foreach_tcp_flag
140#undef _
Dave Barach63681512017-04-20 17:50:39 -0400141 pg_edit_init_bitfield (&p->data_offset_and_reserved, tcp_header_t,
142 data_offset_and_reserved, 4, 4);
Dave Barach68b0fb02017-02-28 15:15:56 -0500143}
144
145uword
146unformat_pg_tcp_header (unformat_input_t * input, va_list * args)
147{
Dave Barach63681512017-04-20 17:50:39 -0400148 pg_stream_t *s = va_arg (*args, pg_stream_t *);
149 pg_tcp_header_t *p;
Dave Barach68b0fb02017-02-28 15:15:56 -0500150 u32 group_index;
Dave Barach63681512017-04-20 17:50:39 -0400151
Dave Barach68b0fb02017-02-28 15:15:56 -0500152 p = pg_create_edit_group (s, sizeof (p[0]), sizeof (tcp_header_t),
153 &group_index);
154 pg_tcp_header_init (p);
155
156 /* Defaults. */
157 pg_edit_set_fixed (&p->seq_number, 0);
158 pg_edit_set_fixed (&p->ack_number, 0);
159
Dave Barach63681512017-04-20 17:50:39 -0400160 pg_edit_set_fixed (&p->data_offset_and_reserved,
161 sizeof (tcp_header_t) / sizeof (u32));
Dave Barach68b0fb02017-02-28 15:15:56 -0500162
163 pg_edit_set_fixed (&p->window, 4096);
164 pg_edit_set_fixed (&p->urgent_pointer, 0);
165
166#define _(f) pg_edit_set_fixed (&p->f##_flag, 0);
167 foreach_tcp_flag
168#undef _
Dave Barach63681512017-04-20 17:50:39 -0400169 p->checksum.type = PG_EDIT_UNSPECIFIED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500170
Dave Barach63681512017-04-20 17:50:39 -0400171 if (!unformat (input, "TCP: %U -> %U",
172 unformat_pg_edit,
173 unformat_tcp_udp_port, &p->src,
174 unformat_pg_edit, unformat_tcp_udp_port, &p->dst))
Dave Barach68b0fb02017-02-28 15:15:56 -0500175 goto error;
176
177 /* Parse options. */
178 while (1)
179 {
180 if (unformat (input, "window %U",
Dave Barach63681512017-04-20 17:50:39 -0400181 unformat_pg_edit, unformat_pg_number, &p->window))
Dave Barach68b0fb02017-02-28 15:15:56 -0500182 ;
183
184 else if (unformat (input, "checksum %U",
Dave Barach63681512017-04-20 17:50:39 -0400185 unformat_pg_edit, unformat_pg_number, &p->checksum))
Dave Barach68b0fb02017-02-28 15:15:56 -0500186 ;
187
Dave Barach63681512017-04-20 17:50:39 -0400188 else if (unformat (input, "seqnum %U", unformat_pg_edit,
189 unformat_pg_number, &p->seq_number))
190 ;
191 else if (unformat (input, "acknum %U", unformat_pg_edit,
192 unformat_pg_number, &p->ack_number))
193 ;
Dave Barach68b0fb02017-02-28 15:15:56 -0500194 /* Flags. */
195#define _(f) else if (unformat (input, #f)) pg_edit_set_fixed (&p->f##_flag, 1);
Dave Barach63681512017-04-20 17:50:39 -0400196 foreach_tcp_flag
Dave Barach68b0fb02017-02-28 15:15:56 -0500197#undef _
Dave Barach63681512017-04-20 17:50:39 -0400198 /* Can't parse input: try next protocol level. */
199 else
Dave Barach68b0fb02017-02-28 15:15:56 -0500200 break;
201 }
202
203 {
Dave Barach63681512017-04-20 17:50:39 -0400204 ip_main_t *im = &ip_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500205 u16 dst_port;
Dave Barach63681512017-04-20 17:50:39 -0400206 tcp_udp_port_info_t *pi;
Dave Barach68b0fb02017-02-28 15:15:56 -0500207
208 pi = 0;
209 if (p->dst.type == PG_EDIT_FIXED)
210 {
211 dst_port = pg_edit_get_value (&p->dst, PG_EDIT_LO);
212 pi = ip_get_tcp_udp_port_info (im, dst_port);
213 }
214
215 if (pi && pi->unformat_pg_edit
216 && unformat_user (input, pi->unformat_pg_edit, s))
217 ;
218
Dave Barach63681512017-04-20 17:50:39 -0400219 else if (!unformat_user (input, unformat_pg_payload, s))
Dave Barach68b0fb02017-02-28 15:15:56 -0500220 goto error;
221
222 if (p->checksum.type == PG_EDIT_UNSPECIFIED)
223 {
Dave Barach63681512017-04-20 17:50:39 -0400224 pg_edit_group_t *g = pg_stream_get_group (s, group_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500225 g->edit_function = tcp_pg_edit_function;
226 g->edit_function_opaque = 0;
227 }
228
229 return 1;
230 }
231
Dave Barach63681512017-04-20 17:50:39 -0400232error:
Dave Barach68b0fb02017-02-28 15:15:56 -0500233 /* Free up any edits we may have added. */
234 pg_free_edit_group (s);
235 return 0;
236}
237
Dave Barach63681512017-04-20 17:50:39 -0400238/*
239 * fd.io coding-style-patch-verification: ON
240 *
241 * Local Variables:
242 * eval: (c-set-style "gnu")
243 * End:
244 */