/*
* Copyright (c) 2015 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* ip/udp_pg: UDP packet-generator interface
*
* Copyright (c) 2008 Eliot Dresselhaus
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <vnet/pg/pg.h>
#include <vnet/ip/ip.h> /* for unformat_udp_udp_port */
#define UDP_PG_EDIT_LENGTH (1 << 0)
#define UDP_PG_EDIT_CHECKSUM (1 << 1)
always_inline void
udp_pg_edit_function_inline (pg_main_t * pg,
pg_stream_t * s,
pg_edit_group_t * g,
u32 * packets, u32 n_packets, u32 flags)
{
vlib_main_t *vm = vlib_get_main ();
u32 ip_offset, udp_offset;
udp_offset = g->start_byte_offset;
ip_offset = (g - 1)->start_byte_offset;
while (n_packets >= 1)
{
vlib_buffer_t *p0;
ip4_header_t *ip0;
udp_header_t *udp0;
u32 udp_len0;
p0 = vlib_get_buffer (vm, packets[0]);
n_packets -= 1;
packets += 1;
ip0 = (void *) (p0->data + ip_offset);
udp0 = (void *) (p0->data + udp_offset);
udp_len0 = clib_net_to_host_u16 (ip0->length) - sizeof (ip0[0]);
if (flags & UDP_PG_EDIT_LENGTH)
udp0->length =
clib_net_to_host_u16 (vlib_buffer_length_in_chain (vm, p0)
- ip_offset);
/* Initialize checksum with header. */
if (flags & UDP_PG_EDIT_CHECKSUM)
{
ip_csum_t sum0;
sum0 = clib_mem_unaligned (&ip0->src_address, u64);
sum0 = ip_csum_with_carry
(sum0, clib_host_to_net_u32 (udp_len0 + (ip0->protocol << 16)));
/* Invalidate possibly old checksum. */
udp0->checksum = 0;
sum0 =
ip_incremental_checksum_buffer (vm, p0, udp_offset, udp_len0,
sum0);
sum0 = ~ip_csum_fold (sum0);
/* Zero checksum means checksumming disabled. */
sum0 = sum0 != 0 ? sum0 : 0xffff;
udp0->checksum = sum0;
}
}
}
static void
udp_pg_edit_function (pg_main_t * pg,
pg_stream_t * s,
pg_edit_group_t * g, u32 * packets, u32 n_packets)
{
switch (g->edit_function_opaque)
{
case UDP_PG_EDIT_LENGTH:
udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
UDP_PG_EDIT_LENGTH);
break;
case UDP_PG_EDIT_CHECKSUM:
udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
UDP_PG_EDIT_CHECKSUM);
break;
case UDP_PG_EDIT_CHECKSUM | UDP_PG_EDIT_LENGTH:
udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
UDP_PG_EDIT_CHECKSUM 1 | -1/+1 |