summaryrefslogtreecommitdiffstats
path: root/src/vnet/syslog/syslog_udp.c
blob: a3b3bc487e2bb2662c1d21a2abf7c9a96d1135ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (c) 2018 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.
 */
/**
 * @file syslog_udp.c
 * syslog protocol UDP transport layer implementation (RFC5426)
 */

#include <vnet/syslog/syslog_udp.h>
#include <vnet/ip/ip4.h>
#include <vnet/udp/udp_packet.h>

void
syslog_add_udp_transport (vlib_main_t * vm, u32 bi)
{
  syslog_main_t *sm = &syslog_main;
  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
  ip4_header_t *ip;
  udp_header_t *udp;

  vlib_buffer_advance (b, -(sizeof (ip4_header_t) + sizeof (udp_header_t)));

  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
  vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
  vnet_buffer (b)->sw_if_index[VLIB_TX] = sm->fib_index;

  ip = vlib_buffer_get_current (b);
  clib_memset (ip, 0, sizeof (*ip));
  udp = (udp_header_t *) (ip + 1);
  clib_memset (udp, 0, sizeof (*udp));

  ip->ip_version_and_header_length = 0x45;
  ip->flags_and_fragment_offset =
    clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
  ip->ttl = 255;
  ip->protocol = IP_PROTOCOL_UDP;
  ip->src_address.as_u32 = sm->src_address.as_u32;
  ip->dst_address.as_u32 = sm->collector.as_u32;

  udp->src_port = udp->dst_port = clib_host_to_net_u16 (sm->collector_port);

  const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
  ip->length = clib_host_to_net_u16 (ip_length);
  ip->checksum = ip4_header_checksum (ip);

  const u16 udp_length = ip_length - (sizeof (ip4_header_t));
  udp->length = clib_host_to_net_u16 (udp_length);
  /* RFC5426 3.6. */
  udp->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip);
  if (udp->checksum == 0)
    udp->checksum = 0xffff;

  b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */