/* * 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 RFC5424 syslog protocol implementation */ #include #include #include #include #include #define SYSLOG_VERSION "1" #define NILVALUE "-" #define DEFAULT_UDP_PORT 514 #define DEFAULT_MAX_MSG_SIZE 480 #define encode_priority(f, p) ((f << 3) | p) syslog_main_t syslog_main; /* format timestamp RFC5424 6.2.3. */ static u8 * format_syslog_timestamp (u8 * s, va_list * args) { f64 timestamp = va_arg (*args, f64); struct tm *tm; word msec; time_t t = timestamp; tm = gmtime (&t); msec = 1e6 * (timestamp - t); return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, msec); } /* format header RFC5424 6.2. */ static u8 * format_syslog_header (u8 * s, va_list * args) { syslog_main_t *sm = &syslog_main; syslog_header_t *h = va_arg (*args, syslog_header_t *); u32 pri = encode_priority (h->facility, h->severity); return format (s, "<%d>%s %U %U %s %d %s", pri, SYSLOG_VERSION, format_syslog_timestamp, h->timestamp + sm->time_offset, format_ip4_address, &sm->src_address, h->app_name ? h->app_name : NILVALUE, sm->procid, h->msgid ? h->msgid : NILVALUE); } /* format strucured data elements RFC5424 6.3. */ static u8 * format_syslog_structured_data (u8 * s, va_list * args) { u8 **sds = va_arg (*args, u8 **); int i; if (vec_len (sds)) { for (i = 0; i < vec_len (sds); i++) s = format (s, "[%s]", sds[i]); } /* if zero structured data elemts field must contain NILVALUE */ else s = format (s, "%s", NILVALUE); return s; } static u8 * format_syslog_msg (u8 * s, va_list * args) { syslog_msg_t *m = va_arg (*args, syslog_msg_t *); s = format (s, "%U %U", format_syslog_header, &m->header, format_syslog_structured_data, m->structured_data); /* free-form message is optional */ if (m->msg) s = format (s, " %s", m->msg); return s; } void syslog_msg_sd_init (syslog_msg_t * syslog_msg, char *sd_id) { u8 *sd; sd = format (0, "%s", sd_id); vec_add1 (syslog_msg->structured_data, sd); syslog_msg->curr_sd_index++; } void syslog_msg_add_sd_param (syslog_msg_t * syslog_msg, char *name, char *fmt, ...) { va_list va; u8 *value; va_start (va, fmt); value = va_format (0, fmt, &va); va_end (va); vec_terminate_c_string (value); syslog_msg->structured_data[syslog_msg->curr_sd_index] = format (syslog_msg->structured_data[syslog_msg->curr_sd_index], " %s=\"%s\"", name, value); vec_free (value); } void syslog_msg_add_msg (syslog_msg_t * syslog_msg, char *fmt, ...) { va_list va; u8 *msg; va_start (va, fmt); msg = va_format (0, fmt, &va); va_end (va); vec_terminate_c_string (msg); syslog_msg->msg = msg; } void syslog_msg_init (syslog_msg_t * syslog_msg, syslog_facility_t facility, syslog_severity_t severity, char *app_name, char *msgid) { syslog_main_t *sm = &syslog_main; vlib_main_t *vm = sm->vlib_main; syslog_msg->header.facility = facility; syslog_msg->header.severity = severity; syslog_msg->header.timestamp = vlib_time_now (vm); syslog_msg->header.app_name = app_name; syslog_msg->header.msgid = msgid; syslog_msg->structured_data = 0; syslog_msg->curr_sd_index = ~0; syslog_msg->msg = 0; } int syslog_msg_send (syslog_msg_t * syslog_msg) { syslog_main_t *sm = &syslog_main; vlib_main_t *vm = sm->vlib_main; u32 bi, msg_len, *to_next; u8 *tmp; vlib_buffer_t *b; vlib_frame_t *f; int i; if (vlib_buffer_alloc (vm, &bi, 1) != 1) return -1; b = vlib_get_buffer (vm, bi); VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b); /* one message per UDP datagram RFC5426 3.1. */ tmp = format (0, "%U", format_syslog_msg, syslog_msg); msg_len = vec_len (tmp) - (vec_c_string_is_terminated (tmp) ? 1 : 0); msg_len = msg_len < sm->max_msg_size ? msg_len : sm->max_msg_size; clib_memcpy_fast (b->data, tmp, msg_len); b->current_length = msg_len; vec_free (tmp); vec_free (syslog_msg->msg); for (i = 0; i < vec_len (syslog_msg->structured_data); i++) vec_free (syslog_msg->structured_data[i]); vec_free (syslog_msg->structured_data); syslog_add_udp_transport (vm, bi); f = vlib_get_frame_to_node (vm, sm->ip4_lookup_node_index); to_next = vlib_frame_vector_args (f); to_next[0] = bi; f->n_vectors = 1; vlib_put_frame_to_node (vm, sm->ip4_lookup_node_index, f); return 0; } static uword unformat_syslog_facility (unformat_input_t * input, va_list * args) { u32 *r = va_arg (*args, u32 *); if (0); #define _(v,f,s) else if (unformat (input, s)) *r = SYSLOG_FACILITY_##f; foreach_syslog_facility #undef _ else return 0; return 1; } static uword unformat_syslog_severity (unformat_input_t * input, va_list * args) { u32 *r = va_arg (*args, u32 *); if (0); #define _(v,f,s) else if (unformat (input, s)) *r = SYSLOG_SEVERITY_##f; fo
# Copyright (c) 2020 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.

add_vpp_plugin(urpf
  SOURCES
  urpf.c
  urpf_api.c
  ip4_urpf.c
  ip6_urpf.c

  MULTIARCH_SOURCES
  ip4_urpf.c
  ip6_urpf.c

  API_FILES
  urpf.api
)