FD.io VPP  v19.01.3-6-g70449b9b9
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 protocol UDP transport layer implementation (RFC5426)
17  */
18 
19 #include <vnet/syslog/syslog_udp.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/udp/udp_packet.h>
22 
23 void
25 {
27  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
28  ip4_header_t *ip;
29  udp_header_t *udp;
30 
31  vlib_buffer_advance (b, -(sizeof (ip4_header_t) + sizeof (udp_header_t)));
32 
33  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
34  vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
35  vnet_buffer (b)->sw_if_index[VLIB_TX] = sm->fib_index;
36 
37  ip = vlib_buffer_get_current (b);
38  clib_memset (ip, 0, sizeof (*ip));
39  udp = (udp_header_t *) (ip + 1);
40  clib_memset (udp, 0, sizeof (*udp));
41 
44  clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
45  ip->ttl = 255;
46  ip->protocol = IP_PROTOCOL_UDP;
49 
50  udp->src_port = udp->dst_port = clib_host_to_net_u16 (sm->collector_port);
51 
52  const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
53  ip->length = clib_host_to_net_u16 (ip_length);
54  ip->checksum = ip4_header_checksum (ip);
55 
56  const u16 udp_length = ip_length - (sizeof (ip4_header_t));
57  udp->length = clib_host_to_net_u16 (udp_length);
58  /* RFC5426 3.6. */
59  udp->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip);
60  if (udp->checksum == 0)
61  udp->checksum = 0xffff;
62 
63  b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
64 }
65 
66 /*
67  * fd.io coding-style-patch-verification: ON
68  *
69  * Local Variables:
70  * eval: (c-set-style "gnu")
71  * End:
72  */
u16 collector_port
UDP port number of remote host (destination)
Definition: syslog.h:120
ip4_address_t src_address
Definition: ip4_packet.h:170
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 fib_index
FIB table index.
Definition: syslog.h:126
u16 flags_and_fragment_offset
Definition: ip4_packet.h:151
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:267
ip4_address_t dst_address
Definition: ip4_packet.h:170
unsigned int u32
Definition: types.h:88
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:214
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:24
vlib_main_t * vm
Definition: buffer.c:301
u16 ip4_tcp_udp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip4_header_t *ip0)
Definition: ip4_forward.c:1129
syslog_main_t syslog_main
Definition: syslog.c:32
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:233
ip4_address_t src_address
IPv4 address of sender (source)
Definition: syslog.h:123
Definition: defs.h:47
#define vnet_buffer(b)
Definition: buffer.h:368
ip4_address_t collector
IPv4 address of remote host (destination)
Definition: syslog.h:117
#define IP4_HEADER_FLAG_DONT_FRAGMENT
Definition: ip4_packet.h:153
u8 ip_version_and_header_length
Definition: ip4_packet.h:138
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:117
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:62
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:247
Definition: defs.h:46