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:
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 * ip/tcp_pg: TCP packet-generator interface
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/ip/ip.h>
41 #include <vnet/pg/pg.h>
43 /* TCP flags bit 0 first. */
44 #define foreach_tcp_flag \
55 tcp_pg_edit_function (pg_main_t * pg,
61 vlib_main_t * vm = vlib_get_main();
62 u32 ip_offset, tcp_offset;
64 tcp_offset = g->start_byte_offset;
65 ip_offset = (g-1)->start_byte_offset;
67 while (n_packets >= 1)
75 p0 = vlib_get_buffer (vm, packets[0]);
79 ASSERT (p0->current_data == 0);
80 ip0 = (void *) (p0->data + ip_offset);
81 tcp0 = (void *) (p0->data + tcp_offset);
82 tcp_len0 = clib_net_to_host_u16 (ip0->length) - sizeof (ip0[0]);
84 /* Initialize checksum with header. */
85 if (BITS (sum0) == 32)
87 sum0 = clib_mem_unaligned (&ip0->src_address, u32);
88 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->dst_address, u32));
91 sum0 = clib_mem_unaligned (&ip0->src_address, u64);
93 sum0 = ip_csum_with_carry
94 (sum0, clib_host_to_net_u32 (tcp_len0 + (ip0->protocol << 16)));
96 /* Invalidate possibly old checksum. */
99 sum0 = ip_incremental_checksum_buffer (vm, p0, tcp_offset, tcp_len0, sum0);
101 tcp0->checksum = ~ ip_csum_fold (sum0);
107 pg_edit_t seq_number, ack_number;
108 pg_edit_t data_offset_and_reserved;
109 #define _(f) pg_edit_t f##_flag;
114 pg_edit_t urgent_pointer;
118 pg_tcp_header_init (pg_tcp_header_t * p)
120 /* Initialize fields that are not bit fields in the IP header. */
121 #define _(f) pg_edit_init (&p->f, tcp_header_t, f);
131 /* Initialize bit fields. */
133 pg_edit_init_bitfield (&p->f##_flag, tcp_header_t, \
135 TCP_FLAG_BIT_##f, 1);
140 pg_edit_init_bitfield (&p->data_offset_and_reserved, tcp_header_t,
141 data_offset_and_reserved,
146 unformat_pg_tcp_header (unformat_input_t * input, va_list * args)
148 pg_stream_t * s = va_arg (*args, pg_stream_t *);
152 p = pg_create_edit_group (s, sizeof (p[0]), sizeof (tcp_header_t),
154 pg_tcp_header_init (p);
157 pg_edit_set_fixed (&p->seq_number, 0);
158 pg_edit_set_fixed (&p->ack_number, 0);
160 pg_edit_set_fixed (&p->data_offset_and_reserved,
161 sizeof (tcp_header_t) / sizeof (u32));
163 pg_edit_set_fixed (&p->window, 4096);
164 pg_edit_set_fixed (&p->urgent_pointer, 0);
166 #define _(f) pg_edit_set_fixed (&p->f##_flag, 0);
170 p->checksum.type = PG_EDIT_UNSPECIFIED;
172 if (! unformat (input, "TCP: %U -> %U",
174 unformat_tcp_udp_port, &p->src,
176 unformat_tcp_udp_port, &p->dst))
182 if (unformat (input, "window %U",
184 unformat_pg_number, &p->window))
187 else if (unformat (input, "checksum %U",
189 unformat_pg_number, &p->checksum))
193 #define _(f) else if (unformat (input, #f)) pg_edit_set_fixed (&p->f##_flag, 1);
197 /* Can't parse input: try next protocol level. */
203 ip_main_t * im = &ip_main;
205 tcp_udp_port_info_t * pi;
208 if (p->dst.type == PG_EDIT_FIXED)
210 dst_port = pg_edit_get_value (&p->dst, PG_EDIT_LO);
211 pi = ip_get_tcp_udp_port_info (im, dst_port);
214 if (pi && pi->unformat_pg_edit
215 && unformat_user (input, pi->unformat_pg_edit, s))
218 else if (! unformat_user (input, unformat_pg_payload, s))
221 if (p->checksum.type == PG_EDIT_UNSPECIFIED)
223 pg_edit_group_t * g = pg_stream_get_group (s, group_index);
224 g->edit_function = tcp_pg_edit_function;
225 g->edit_function_opaque = 0;
232 /* Free up any edits we may have added. */
233 pg_free_edit_group (s);