Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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/udp_pg: UDP 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/pg/pg.h> |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 41 | #include <vnet/ip/ip.h> /* for unformat_udp_udp_port */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | |
| 43 | #define UDP_PG_EDIT_LENGTH (1 << 0) |
| 44 | #define UDP_PG_EDIT_CHECKSUM (1 << 1) |
| 45 | |
| 46 | always_inline void |
| 47 | udp_pg_edit_function_inline (pg_main_t * pg, |
| 48 | pg_stream_t * s, |
| 49 | pg_edit_group_t * g, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 50 | u32 * packets, u32 n_packets, u32 flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 52 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | u32 ip_offset, udp_offset; |
| 54 | |
| 55 | udp_offset = g->start_byte_offset; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 56 | ip_offset = (g - 1)->start_byte_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
| 58 | while (n_packets >= 1) |
| 59 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 60 | vlib_buffer_t *p0; |
| 61 | ip4_header_t *ip0; |
| 62 | udp_header_t *udp0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | u32 udp_len0; |
| 64 | |
| 65 | p0 = vlib_get_buffer (vm, packets[0]); |
| 66 | n_packets -= 1; |
| 67 | packets += 1; |
| 68 | |
| 69 | ip0 = (void *) (p0->data + ip_offset); |
| 70 | udp0 = (void *) (p0->data + udp_offset); |
Kingwel Xie | fc3b8b8 | 2018-10-10 20:51:59 -0400 | [diff] [blame^] | 71 | udp_len0 = vlib_buffer_length_in_chain (vm, p0) - udp_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | if (flags & UDP_PG_EDIT_LENGTH) |
Kingwel Xie | fc3b8b8 | 2018-10-10 20:51:59 -0400 | [diff] [blame^] | 74 | udp0->length = clib_host_to_net_u16 (udp_len0); |
| 75 | else |
| 76 | udp_len0 = clib_host_to_net_u16 (udp0->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
| 78 | /* Initialize checksum with header. */ |
| 79 | if (flags & UDP_PG_EDIT_CHECKSUM) |
| 80 | { |
| 81 | ip_csum_t sum0; |
| 82 | |
| 83 | sum0 = clib_mem_unaligned (&ip0->src_address, u64); |
| 84 | |
| 85 | sum0 = ip_csum_with_carry |
| 86 | (sum0, clib_host_to_net_u32 (udp_len0 + (ip0->protocol << 16))); |
| 87 | |
| 88 | /* Invalidate possibly old checksum. */ |
| 89 | udp0->checksum = 0; |
| 90 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 91 | sum0 = |
| 92 | ip_incremental_checksum_buffer (vm, p0, udp_offset, udp_len0, |
| 93 | sum0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 95 | sum0 = ~ip_csum_fold (sum0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
| 97 | /* Zero checksum means checksumming disabled. */ |
| 98 | sum0 = sum0 != 0 ? sum0 : 0xffff; |
| 99 | |
| 100 | udp0->checksum = sum0; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static void |
| 106 | udp_pg_edit_function (pg_main_t * pg, |
| 107 | pg_stream_t * s, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 108 | pg_edit_group_t * g, u32 * packets, u32 n_packets) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | { |
| 110 | switch (g->edit_function_opaque) |
| 111 | { |
| 112 | case UDP_PG_EDIT_LENGTH: |
| 113 | udp_pg_edit_function_inline (pg, s, g, packets, n_packets, |
| 114 | UDP_PG_EDIT_LENGTH); |
| 115 | break; |
| 116 | |
| 117 | case UDP_PG_EDIT_CHECKSUM: |
| 118 | udp_pg_edit_function_inline (pg, s, g, packets, n_packets, |
| 119 | UDP_PG_EDIT_CHECKSUM); |
| 120 | break; |
| 121 | |
| 122 | case UDP_PG_EDIT_CHECKSUM | UDP_PG_EDIT_LENGTH: |
| 123 | udp_pg_edit_function_inline (pg, s, g, packets, n_packets, |
| 124 | UDP_PG_EDIT_CHECKSUM | UDP_PG_EDIT_LENGTH); |
| 125 | break; |
| 126 | |
| 127 | default: |
| 128 | ASSERT (0); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 133 | typedef struct |
| 134 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | pg_edit_t src_port, dst_port; |
| 136 | pg_edit_t length; |
| 137 | pg_edit_t checksum; |
| 138 | } pg_udp_header_t; |
| 139 | |
| 140 | static inline void |
| 141 | pg_udp_header_init (pg_udp_header_t * p) |
| 142 | { |
| 143 | /* Initialize fields that are not bit fields in the IP header. */ |
| 144 | #define _(f) pg_edit_init (&p->f, udp_header_t, f); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 145 | _(src_port); |
| 146 | _(dst_port); |
| 147 | _(length); |
| 148 | _(checksum); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | #undef _ |
| 150 | } |
| 151 | |
| 152 | uword |
| 153 | unformat_pg_udp_header (unformat_input_t * input, va_list * args) |
| 154 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 155 | pg_stream_t *s = va_arg (*args, pg_stream_t *); |
| 156 | pg_udp_header_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | u32 group_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 158 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | p = pg_create_edit_group (s, sizeof (p[0]), sizeof (udp_header_t), |
| 160 | &group_index); |
| 161 | pg_udp_header_init (p); |
| 162 | |
| 163 | /* Defaults. */ |
| 164 | p->checksum.type = PG_EDIT_UNSPECIFIED; |
| 165 | p->length.type = PG_EDIT_UNSPECIFIED; |
| 166 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 167 | if (!unformat (input, "UDP: %U -> %U", |
| 168 | unformat_pg_edit, |
| 169 | unformat_tcp_udp_port, &p->src_port, |
| 170 | unformat_pg_edit, unformat_tcp_udp_port, &p->dst_port)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | goto error; |
| 172 | |
| 173 | /* Parse options. */ |
| 174 | while (1) |
| 175 | { |
| 176 | if (unformat (input, "length %U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 177 | unformat_pg_edit, unformat_pg_number, &p->length)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | ; |
| 179 | |
| 180 | else if (unformat (input, "checksum %U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 181 | unformat_pg_edit, unformat_pg_number, &p->checksum)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | ; |
| 183 | |
| 184 | /* Can't parse input: try next protocol level. */ |
| 185 | else |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 190 | ip_main_t *im = &ip_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | u16 dst_port; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 192 | tcp_udp_port_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | |
| 194 | pi = 0; |
| 195 | if (p->dst_port.type == PG_EDIT_FIXED) |
| 196 | { |
| 197 | dst_port = pg_edit_get_value (&p->dst_port, PG_EDIT_LO); |
| 198 | pi = ip_get_tcp_udp_port_info (im, dst_port); |
| 199 | } |
| 200 | |
| 201 | if (pi && pi->unformat_pg_edit |
| 202 | && unformat_user (input, pi->unformat_pg_edit, s)) |
| 203 | ; |
| 204 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 205 | else if (!unformat_user (input, unformat_pg_payload, s)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | goto error; |
| 207 | |
| 208 | p = pg_get_edit_group (s, group_index); |
| 209 | if (p->checksum.type == PG_EDIT_UNSPECIFIED |
| 210 | || p->length.type == PG_EDIT_UNSPECIFIED) |
| 211 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 212 | pg_edit_group_t *g = pg_stream_get_group (s, group_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | g->edit_function = udp_pg_edit_function; |
| 214 | g->edit_function_opaque = 0; |
| 215 | if (p->checksum.type == PG_EDIT_UNSPECIFIED) |
| 216 | g->edit_function_opaque |= UDP_PG_EDIT_CHECKSUM; |
| 217 | if (p->length.type == PG_EDIT_UNSPECIFIED) |
| 218 | g->edit_function_opaque |= UDP_PG_EDIT_LENGTH; |
| 219 | } |
| 220 | |
| 221 | return 1; |
| 222 | } |
| 223 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 224 | error: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | /* Free up any edits we may have added. */ |
| 226 | pg_free_edit_group (s); |
| 227 | return 0; |
| 228 | } |
| 229 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * fd.io coding-style-patch-verification: ON |
| 233 | * |
| 234 | * Local Variables: |
| 235 | * eval: (c-set-style "gnu") |
| 236 | * End: |
| 237 | */ |