/* *------------------------------------------------------------------ * api_shared.c - API message handling, common code for both clients * and the vlib process itself. * * * Copyright (c) 2009 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. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include /* *INDENT-OFF* */ api_main_t api_main = { .region_name = "/unset", .api_uid = -1, .api_gid = -1, }; /* *INDENT-ON* */ void vl_msg_api_increment_missing_client_counter (void) { api_main_t *am = &api_main; am->missing_clients++; } int vl_msg_api_rx_trace_enabled (api_main_t * am) { return (am->rx_trace && am->rx_trace->enabled); } int vl_msg_api_tx_trace_enabled (api_main_t * am) { return (am->tx_trace && am->tx_trace->enabled); } /* * vl_msg_api_trace */ void vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg) { u8 **this_trace; u8 **old_trace; u8 *msg_copy; u32 length; trace_cfg_t *cfgp; u16 msg_id = ntohs (*((u16 *) msg)); msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data)); cfgp = am->api_trace_cfg + msg_id; if (!cfgp || !cfgp->trace_enable) return; msg_copy = 0; if (tp->nitems == 0) { clib_warning ("tp->nitems is 0"); return; } if (vec_len (tp->traces) < tp->nitems) { vec_add1 (tp->traces, 0); this_trace = tp->traces + vec_len (tp->traces) - 1; } else { tp->wrapped = 1; old_trace = tp->traces + tp->curindex++; if (tp->curindex == tp->nitems) tp->curindex = 0; vec_free (*old_trace); this_trace = old_trace; } length = clib_net_to_host_u32 (header->data_len); vec_validate (msg_copy, length - 1); clib_memcpy_fast (msg_copy, msg, length); *this_trace = msg_copy; } int vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which, int onoff) { vl_api_trace_t *tp; int rv; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; if (tp == 0) { vl_msg_api_trace_configure (am, which, 1024); tp = am->tx_trace; } break; case VL_API_TRACE_RX: tp = am->rx_trace; if (tp == 0) { vl_msg_api_trace_configure (am, which, 1024); tp = am->rx_trace; } break; default: /* duh? */ return -1; } /* Configured? */ if (tp == 0 || tp->nitems == 0) return -1; rv = tp->enabled; tp->enabled = onoff; return rv; } int vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which) { vl_api_trace_t *tp; int i; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; break; case VL_API_TRACE_RX: tp = am->rx_trace; break; default: /* duh? */ return -1; } /* Configured? */ if (!tp || tp->nitems == 0) return -1; tp->curindex = 0; tp->wrapped = 0; for (i = 0; i < vec_len (tp->traces); i++) { vec_free (tp->traces[i]); } vec_free (tp->traces); return 0; } int vl_msg_api_trace_save (api_main_t * am, vl_api_trace_which_t which, FILE * fp) { vl_api_trace_t *tp; vl_api_trace_file_header_t fh; int i; u8 *msg; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; break; case VL_API_TRACE_RX: tp = am->rx_trace; break; default: /* duh? */ return -1; } /* Configured, data present? */ if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0) return -1; /* "Dare to be stupid" check */ if (fp == 0) { return -2; } /* Write the file header */ fh.nitems = vec_len (tp->traces); fh.endian = tp->endian; fh.wrapped = tp->wrapped; if (fwrite (&fh, sizeof (fh), 1, fp) != 1) { return (-10); } /* No-wrap case */ if (tp->wrapped == 0) { /* * Note: vec_len return 0 when fed a NULL pointer. * Unfortunately, the static analysis tool doesn't * figure it out, hence the suppressed warnings. * What a great use of my time. */ for (i = 0; i < vec_len (tp->traces); i++) { u32 msg_length; /*sa_ignore NO_NULL_CHK */ msg = tp->traces[i]; /* * This retarded check required to pass * [sic] SA-checking. */ if (!msg) continue; msg_length = clib_host_to_net_u32 (vec_len (msg)); if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length)) { return (-14); } if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg)) { return (-11); } } } else { /* Wrap case: write oldest -> end of buffer */ for (i = tp->curindex; i < vec_len (tp->traces); i++) { u32 msg_length; msg = tp->traces[i]; /* * This retarded check required to pass * [sic] SA-checking */ if (!msg) continue; msg_length = clib_host_to_net_u32 (vec_len (msg)); if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length)) { return (-14); } if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg)) { return (-12); } } /* write beginning of buffer -> oldest-1 */ for (i = 0; i < tp->curindex; i++) { u32 msg_length; /*sa_ignore NO_NULL_CHK */ msg = tp->traces[i]; /* * This retarded check required to pass * [sic] SA-checking */ if (!msg) continue; msg_length = clib_host_to_net_u32 (vec_len (msg)); if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length)) { return (-14); } if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg)) { return (-13); } } } return 0; } int vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which, u32 nitems) { vl_api_trace_t *tp; int was_on = 0; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; if (tp == 0) { vec_validate (am->tx_trace, 0); tp = am->tx_trace; } break; case VL_API_TRACE_RX: tp = am->rx_trace; if (tp == 0) { vec_validate (am->rx_trace, 0); tp = am->rx_trace; } break; default: return -1; } if (tp->enabled) { was_on = vl_msg_api_trace_onoff (am, whi
/*
 * 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
 * @brief NAT syslog logging
 */

#ifndef __included_nat_syslog_h__
#define __included_nat_syslog_h__

#include <nat/nat.h>

void nat_syslog_nat44_apmadd (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
			      u16 isport, ip4_address_t * xsaddr, u16 xsport,
			      snat_protocol_t proto);

void nat_syslog_nat44_apmdel (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
			      u16 isport, ip4_address_t * xsaddr, u16 xsport,
			      snat_protocol_t proto);

void
nat_syslog_dslite_apmadd (u32 ssubix, ip6_address_t * sv6enc,
			  ip4_address_t * isaddr, u16 isport,
			  ip4_address_t * xsaddr, u16 xsport,
			  snat_protocol_t proto);

void
nat_syslog_dslite_apmdel (u32 ssubix, ip6_address_t * sv6enc,
			  ip4_address_t * isaddr, u16 isport,
			  ip4_address_t * xsaddr, u16 xsport,
			  snat_protocol_t proto);

void nat_syslog_nat44_sadd (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
			    u16 isport, ip4_address_t * idaddr, u16 idport,
			    ip4_address_t * xsaddr