Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2016-2019 Cisco and/or its affiliates. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 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 | |
| 54 | static void |
| 55 | tcp_pg_edit_function (pg_main_t * pg, |
| 56 | pg_stream_t * s, |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 57 | pg_edit_group_t * g, u32 * packets, u32 n_packets) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 58 | { |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 59 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 60 | u32 ip_offset, tcp_offset; |
| 61 | |
| 62 | tcp_offset = g->start_byte_offset; |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 63 | ip_offset = (g - 1)->start_byte_offset; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 64 | |
| 65 | while (n_packets >= 1) |
| 66 | { |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 67 | vlib_buffer_t *p0; |
| 68 | ip4_header_t *ip0; |
| 69 | tcp_header_t *tcp0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 70 | 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); |
Kingwel Xie | a91866f | 2018-10-26 23:26:06 -0400 | [diff] [blame] | 80 | /* if IP length has been specified, then calculate the length based on buffer */ |
| 81 | if (ip0->length == 0) |
| 82 | tcp_len0 = vlib_buffer_length_in_chain (vm, p0) - tcp_offset; |
| 83 | else |
| 84 | tcp_len0 = clib_net_to_host_u16 (ip0->length) - tcp_offset; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 85 | |
| 86 | /* Initialize checksum with header. */ |
| 87 | if (BITS (sum0) == 32) |
| 88 | { |
| 89 | sum0 = clib_mem_unaligned (&ip0->src_address, u32); |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 90 | sum0 = |
| 91 | ip_csum_with_carry (sum0, |
| 92 | clib_mem_unaligned (&ip0->dst_address, u32)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 93 | } |
| 94 | else |
| 95 | sum0 = clib_mem_unaligned (&ip0->src_address, u64); |
| 96 | |
| 97 | sum0 = ip_csum_with_carry |
| 98 | (sum0, clib_host_to_net_u32 (tcp_len0 + (ip0->protocol << 16))); |
| 99 | |
| 100 | /* Invalidate possibly old checksum. */ |
| 101 | tcp0->checksum = 0; |
| 102 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 103 | sum0 = |
| 104 | ip_incremental_checksum_buffer (vm, p0, tcp_offset, tcp_len0, sum0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 105 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 106 | tcp0->checksum = ~ip_csum_fold (sum0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 110 | typedef struct |
| 111 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 112 | pg_edit_t src, dst; |
| 113 | pg_edit_t seq_number, ack_number; |
| 114 | pg_edit_t data_offset_and_reserved; |
| 115 | #define _(f) pg_edit_t f##_flag; |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 116 | foreach_tcp_flag |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 117 | #undef _ |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 118 | pg_edit_t window; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 119 | pg_edit_t checksum; |
| 120 | pg_edit_t urgent_pointer; |
| 121 | } pg_tcp_header_t; |
| 122 | |
| 123 | static inline void |
| 124 | pg_tcp_header_init (pg_tcp_header_t * p) |
| 125 | { |
| 126 | /* Initialize fields that are not bit fields in the IP header. */ |
| 127 | #define _(f) pg_edit_init (&p->f, tcp_header_t, f); |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 128 | _(src); |
| 129 | _(dst); |
| 130 | _(seq_number); |
| 131 | _(ack_number); |
| 132 | _(window); |
| 133 | _(checksum); |
| 134 | _(urgent_pointer); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 135 | #undef _ |
| 136 | |
| 137 | /* Initialize bit fields. */ |
| 138 | #define _(f) \ |
| 139 | pg_edit_init_bitfield (&p->f##_flag, tcp_header_t, \ |
| 140 | flags, \ |
| 141 | TCP_FLAG_BIT_##f, 1); |
| 142 | |
| 143 | foreach_tcp_flag |
| 144 | #undef _ |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 145 | pg_edit_init_bitfield (&p->data_offset_and_reserved, tcp_header_t, |
| 146 | data_offset_and_reserved, 4, 4); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | uword |
| 150 | unformat_pg_tcp_header (unformat_input_t * input, va_list * args) |
| 151 | { |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 152 | pg_stream_t *s = va_arg (*args, pg_stream_t *); |
| 153 | pg_tcp_header_t *p; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 154 | u32 group_index; |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 155 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 156 | p = pg_create_edit_group (s, sizeof (p[0]), sizeof (tcp_header_t), |
| 157 | &group_index); |
| 158 | pg_tcp_header_init (p); |
| 159 | |
| 160 | /* Defaults. */ |
| 161 | pg_edit_set_fixed (&p->seq_number, 0); |
| 162 | pg_edit_set_fixed (&p->ack_number, 0); |
| 163 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 164 | pg_edit_set_fixed (&p->data_offset_and_reserved, |
| 165 | sizeof (tcp_header_t) / sizeof (u32)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 166 | |
| 167 | pg_edit_set_fixed (&p->window, 4096); |
| 168 | pg_edit_set_fixed (&p->urgent_pointer, 0); |
| 169 | |
| 170 | #define _(f) pg_edit_set_fixed (&p->f##_flag, 0); |
| 171 | foreach_tcp_flag |
| 172 | #undef _ |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 173 | p->checksum.type = PG_EDIT_UNSPECIFIED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 174 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 175 | if (!unformat (input, "TCP: %U -> %U", |
| 176 | unformat_pg_edit, |
| 177 | unformat_tcp_udp_port, &p->src, |
| 178 | unformat_pg_edit, unformat_tcp_udp_port, &p->dst)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 179 | goto error; |
| 180 | |
| 181 | /* Parse options. */ |
| 182 | while (1) |
| 183 | { |
| 184 | if (unformat (input, "window %U", |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 185 | unformat_pg_edit, unformat_pg_number, &p->window)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 186 | ; |
| 187 | |
| 188 | else if (unformat (input, "checksum %U", |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 189 | unformat_pg_edit, unformat_pg_number, &p->checksum)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 190 | ; |
| 191 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 192 | else if (unformat (input, "seqnum %U", unformat_pg_edit, |
| 193 | unformat_pg_number, &p->seq_number)) |
| 194 | ; |
| 195 | else if (unformat (input, "acknum %U", unformat_pg_edit, |
| 196 | unformat_pg_number, &p->ack_number)) |
| 197 | ; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 198 | /* Flags. */ |
| 199 | #define _(f) else if (unformat (input, #f)) pg_edit_set_fixed (&p->f##_flag, 1); |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 200 | foreach_tcp_flag |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 201 | #undef _ |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 202 | /* Can't parse input: try next protocol level. */ |
| 203 | else |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | |
| 207 | { |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 208 | ip_main_t *im = &ip_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 209 | u16 dst_port; |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 210 | tcp_udp_port_info_t *pi; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 211 | |
| 212 | pi = 0; |
| 213 | if (p->dst.type == PG_EDIT_FIXED) |
| 214 | { |
| 215 | dst_port = pg_edit_get_value (&p->dst, PG_EDIT_LO); |
| 216 | pi = ip_get_tcp_udp_port_info (im, dst_port); |
| 217 | } |
| 218 | |
| 219 | if (pi && pi->unformat_pg_edit |
| 220 | && unformat_user (input, pi->unformat_pg_edit, s)) |
| 221 | ; |
| 222 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 223 | else if (!unformat_user (input, unformat_pg_payload, s)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 224 | goto error; |
| 225 | |
| 226 | if (p->checksum.type == PG_EDIT_UNSPECIFIED) |
| 227 | { |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 228 | pg_edit_group_t *g = pg_stream_get_group (s, group_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 229 | g->edit_function = tcp_pg_edit_function; |
| 230 | g->edit_function_opaque = 0; |
| 231 | } |
| 232 | |
| 233 | return 1; |
| 234 | } |
| 235 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 236 | error: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 237 | /* Free up any edits we may have added. */ |
| 238 | pg_free_edit_group (s); |
| 239 | return 0; |
| 240 | } |
| 241 | |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 242 | /* |
| 243 | * fd.io coding-style-patch-verification: ON |
| 244 | * |
| 245 | * Local Variables: |
| 246 | * eval: (c-set-style "gnu") |
| 247 | * End: |
| 248 | */ |