2 * Copyright (c) 2020 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <cnat/cnat_types.h>
18 cnat_main_t cnat_main;
19 fib_source_t cnat_fib_source;
20 cnat_timestamp_t *cnat_timestamps;
22 char *cnat_error_strings[] = {
23 #define cnat_error(n,s) s,
24 #include <cnat/cnat_error.def>
29 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
32 /* Tries to resolve IP from sw_if_index
33 * returns 1 if we need to schedule DHCP */
34 if (INDEX_INVALID == sw_if_index)
38 ip6_address_t *ip6 = 0;
39 ip6 = ip6_interface_first_address (&ip6_main, sw_if_index);
42 ip_address_set (addr, ip6, AF_IP6);
50 ip4_address_t *ip4 = 0;
51 ip4 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
54 ip_address_set (addr, ip4, AF_IP4);
63 cnat_resolve_ep (cnat_endpoint_t * ep)
66 rv = cnat_resolve_addr (ep->ce_sw_if_index, ep->ce_ip.version, &ep->ce_ip);
68 ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
73 unformat_cnat_ep (unformat_input_t * input, va_list * args)
75 cnat_endpoint_t *a = va_arg (*args, cnat_endpoint_t *);
76 vnet_main_t *vnm = vnet_get_main ();
79 clib_memset (a, 0, sizeof (*a));
80 a->ce_sw_if_index = INDEX_INVALID;
81 if (unformat (input, "%U %d", unformat_ip_address, &a->ce_ip, &port))
83 else if (unformat_user (input, unformat_ip_address, &a->ce_ip))
85 else if (unformat (input, "%U v6 %d", unformat_vnet_sw_interface,
86 vnm, &a->ce_sw_if_index, &port))
87 a->ce_ip.version = AF_IP6;
88 else if (unformat (input, "%U v6", unformat_vnet_sw_interface,
89 vnm, &a->ce_sw_if_index))
90 a->ce_ip.version = AF_IP6;
91 else if (unformat (input, "%U %d", unformat_vnet_sw_interface,
92 vnm, &a->ce_sw_if_index, &port))
93 a->ce_ip.version = AF_IP4;
94 else if (unformat_user (input, unformat_vnet_sw_interface,
95 vnm, &a->ce_sw_if_index))
96 a->ce_ip.version = AF_IP4;
97 else if (unformat (input, "%d", &port))
101 a->ce_port = (u16) port;
106 unformat_cnat_ep_flags (unformat_input_t *input, va_list *args)
108 int *a = va_arg (*args, int *);
109 if (unformat (input, ":nonat"))
110 *a = CNAT_TRK_FLAG_NO_NAT;
115 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
117 cnat_endpoint_tuple_t *a = va_arg (*args, cnat_endpoint_tuple_t *);
119 if (unformat (input, "%U->%U%U", unformat_cnat_ep, &a->src_ep,
120 unformat_cnat_ep, &a->dst_ep, unformat_cnat_ep_flags, &flgs))
122 else if (unformat (input, "->%U%U", unformat_cnat_ep, &a->dst_ep,
123 unformat_cnat_ep_flags, &flgs))
125 else if (unformat (input, "%U->%U", unformat_cnat_ep, &a->src_ep,
126 unformat_cnat_ep_flags, &flgs))
134 format_cnat_endpoint (u8 * s, va_list * args)
136 cnat_endpoint_t *cep = va_arg (*args, cnat_endpoint_t *);
137 vnet_main_t *vnm = vnet_get_main ();
138 if (INDEX_INVALID == cep->ce_sw_if_index)
139 s = format (s, "%U;%d", format_ip_address, &cep->ce_ip, cep->ce_port);
142 if (cep->ce_flags & CNAT_EP_FLAG_RESOLVED)
143 s = format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
144 cep->ce_sw_if_index, format_ip_address, &cep->ce_ip,
148 format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
149 cep->ce_sw_if_index, format_ip_address_family,
150 cep->ce_ip.version, cep->ce_port);
155 static clib_error_t *
156 cnat_types_init (vlib_main_t * vm)
158 cnat_fib_source = fib_source_allocate ("cnat",
159 CNAT_FIB_SOURCE_PRIORITY,
160 FIB_SOURCE_BH_SIMPLE);
163 clib_rwlock_init (&cnat_main.ts_lock);
169 cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type)
171 vlib_main_t *vm = vlib_get_main ();
172 vlib_process_signal_event (vm, cnat_main.scanner_node_index, event_type, 0);
178 cnat_main_t *cm = &cnat_main;
179 if (cm->lazy_init_done)
181 cnat_enable_disable_scanner (cm->default_scanner_state);
182 cm->lazy_init_done = 1;
185 static clib_error_t *
186 cnat_config (vlib_main_t * vm, unformat_input_t * input)
188 cnat_main_t *cm = &cnat_main;
190 cm->session_hash_memory = CNAT_DEFAULT_SESSION_MEMORY;
191 cm->session_hash_buckets = CNAT_DEFAULT_SESSION_BUCKETS;
192 cm->translation_hash_memory = CNAT_DEFAULT_TRANSLATION_MEMORY;
193 cm->translation_hash_buckets = CNAT_DEFAULT_TRANSLATION_BUCKETS;
194 cm->snat_hash_memory = CNAT_DEFAULT_SNAT_MEMORY;
195 cm->snat_hash_buckets = CNAT_DEFAULT_SNAT_BUCKETS;
196 cm->snat_if_map_length = CNAT_DEFAULT_SNAT_IF_MAP_LEN;
197 cm->scanner_timeout = CNAT_DEFAULT_SCANNER_TIMEOUT;
198 cm->session_max_age = CNAT_DEFAULT_SESSION_MAX_AGE;
199 cm->tcp_max_age = CNAT_DEFAULT_TCP_MAX_AGE;
200 cm->default_scanner_state = CNAT_SCANNER_ON;
201 cm->maglev_len = CNAT_DEFAULT_MAGLEV_LEN;
202 cm->lazy_init_done = 0;
204 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
207 (input, "session-db-buckets %u", &cm->session_hash_buckets))
209 else if (unformat (input, "session-db-memory %U",
210 unformat_memory_size, &cm->session_hash_memory))
212 else if (unformat (input, "translation-db-buckets %u",
213 &cm->translation_hash_buckets))
215 else if (unformat (input, "translation-db-memory %U",
216 unformat_memory_size, &cm->translation_hash_memory))
218 else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
220 else if (unformat (input, "snat-if-map-len %u", &cm->snat_if_map_length))
222 else if (unformat (input, "snat-db-memory %U",
223 unformat_memory_size, &cm->snat_hash_memory))
225 else if (unformat (input, "session-cleanup-timeout %f",
226 &cm->scanner_timeout))
228 else if (unformat (input, "scanner off"))
229 cm->default_scanner_state = CNAT_SCANNER_OFF;
230 else if (unformat (input, "scanner on"))
231 cm->default_scanner_state = CNAT_SCANNER_ON;
232 else if (unformat (input, "session-max-age %u", &cm->session_max_age))
234 else if (unformat (input, "tcp-max-age %u", &cm->tcp_max_age))
236 else if (unformat (input, "maglev-len %u", &cm->maglev_len))
239 return clib_error_return (0, "unknown input '%U'",
240 format_unformat_error, input);
252 VLIB_EARLY_CONFIG_FUNCTION (cnat_config, "cnat");
253 VLIB_INIT_FUNCTION (cnat_types_init);
256 * fd.io coding-style-patch-verification: ON
259 * eval: (c-set-style "gnu")