FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
syslog_udp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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  * @file syslog_udp.c
17  * syslog protocol UDP transport layer implementation (RFC5426)
18  */
19 
20 #include <vnet/syslog/syslog_udp.h>
21 #include <vnet/ip/ip4.h>
22 #include <vnet/udp/udp_packet.h>
23 
24 void
26 {
30  udp_header_t *udp;
31 
32  vlib_buffer_advance (b, -(sizeof (ip4_header_t) + sizeof (udp_header_t)));
33 
34  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
35  vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
36  vnet_buffer (b)->sw_if_index[VLIB_TX] = sm->fib_index;
37 
39  clib_memset (ip, 0, sizeof (*ip));
40  udp = (udp_header_t *) (ip + 1);
41  clib_memset (udp, 0, sizeof (*udp));
42 
43  ip->ip_version_and_header_length = 0x45;
44  ip->flags_and_fragment_offset =
45  clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
46  ip->ttl = 255;
47  ip->protocol = IP_PROTOCOL_UDP;
48  ip->src_address.as_u32 = sm->src_address.as_u32;
49  ip->dst_address.as_u32 = sm->collector.as_u32;
50 
51  udp->src_port = udp->dst_port = clib_host_to_net_u16 (sm->collector_port);
52 
53  const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
54  ip->length = clib_host_to_net_u16 (ip_length);
55  ip->checksum = ip4_header_checksum (ip);
56 
57  const u16 udp_length = ip_length - (sizeof (ip4_header_t));
58  udp->length = clib_host_to_net_u16 (udp_length);
59  /* RFC5426 3.6. */
61  if (udp->checksum == 0)
62  udp->checksum = 0xffff;
63 
64  b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
65 }
66 
67 /*
68  * fd.io coding-style-patch-verification: ON
69  *
70  * Local Variables:
71  * eval: (c-set-style "gnu")
72  * End:
73  */
udp_header_t::src_port
u16 src_port
Definition: udp_packet.h:48
udp_header_t::length
u16 length
Definition: udp_packet.h:51
IP4_HEADER_FLAG_DONT_FRAGMENT
#define IP4_HEADER_FLAG_DONT_FRAGMENT
Definition: ip4_packet.h:108
syslog_add_udp_transport
void syslog_add_udp_transport(vlib_main_t *vm, u32 bi)
Add UDP/IP transport layer by prepending it to existing data.
Definition: syslog_udp.c:25
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
syslog_main_t::src_address
ip4_address_t src_address
IPv4 address of sender (source)
Definition: syslog.h:124
ip4_address_t::as_u32
u32 as_u32
Definition: ip4_packet.h:57
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
vlib_buffer_length_in_chain
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
udp_header_t
Definition: udp_packet.h:45
ip4_header_t
Definition: ip4_packet.h:87
vlib_buffer_advance
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
syslog_main
syslog_main_t syslog_main
Definition: syslog.c:33
syslog_main_t::collector_port
u16 collector_port
UDP port number of remote host (destination)
Definition: syslog.h:121
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
ip4_tcp_udp_compute_checksum
u16 ip4_tcp_udp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip4_header_t *ip0)
Definition: pnat_test_stubs.h:59
udp_packet.h
syslog_udp.h
udp_header_t::checksum
u16 checksum
Definition: udp_packet.h:55
syslog_main_t::collector
ip4_address_t collector
IPv4 address of remote host (destination)
Definition: syslog.h:118
u32
unsigned int u32
Definition: types.h:88
udp_header_t::dst_port
u16 dst_port
Definition: udp_packet.h:48
ip4.h
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
syslog_main_t
Definition: syslog.h:109
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
ip
vl_api_address_t ip
Definition: l2.api:558
ip4_header_checksum
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
syslog_main_t::fib_index
u32 fib_index
FIB table index.
Definition: syslog.h:127
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111