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 cnat_timestamp_mpool_t cnat_timestamps;
21 char *cnat_error_strings[] = {
22 #define cnat_error(n,s) s,
23 #include <cnat/cnat_error.def>
28 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
31 /* Tries to resolve IP from sw_if_index
32 * returns 1 if we need to schedule DHCP */
33 if (INDEX_INVALID == sw_if_index)
37 ip6_address_t *ip6 = 0;
38 ip6 = ip6_interface_first_address (&ip6_main, sw_if_index);
41 ip_address_set (addr, ip6, AF_IP6);
49 ip4_address_t *ip4 = 0;
50 ip4 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
53 ip_address_set (addr, ip4, AF_IP4);
62 cnat_resolve_ep (cnat_endpoint_t * ep)
65 rv = cnat_resolve_addr (ep->ce_sw_if_index, ep->ce_ip.version, &ep->ce_ip);
67 ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
72 unformat_cnat_ep (unformat_input_t * input, va_list * args)
74 cnat_endpoint_t *a = va_arg (*args, cnat_endpoint_t *);
75 vnet_main_t *vnm = vnet_get_main ();
78 clib_memset (a, 0, sizeof (*a));
79 a->ce_sw_if_index = INDEX_INVALID;
80 if (unformat (input, "%U %d", unformat_ip_address, &a->ce_ip, &port))
82 else if (unformat_user (input, unformat_ip_address, &a->ce_ip))
84 else if (unformat (input, "%U v6 %d", unformat_vnet_sw_interface,
85 vnm, &a->ce_sw_if_index, &port))
86 a->ce_ip.version = AF_IP6;
87 else if (unformat (input, "%U v6", unformat_vnet_sw_interface,
88 vnm, &a->ce_sw_if_index))
89 a->ce_ip.version = AF_IP6;
90 else if (unformat (input, "%U %d", unformat_vnet_sw_interface,
91 vnm, &a->ce_sw_if_index, &port))
92 a->ce_ip.version = AF_IP4;
93 else if (unformat_user (input, unformat_vnet_sw_interface,
94 vnm, &a->ce_sw_if_index))
95 a->ce_ip.version = AF_IP4;
96 else if (unformat (input, "%d", &port))
100 a->ce_port = (u16) port;
105 unformat_cnat_ep_flags (unformat_input_t *input, va_list *args)
107 int *a = va_arg (*args, int *);
108 if (unformat (input, ":nonat"))
109 *a = CNAT_TRK_FLAG_NO_NAT;
114 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
116 cnat_endpoint_tuple_t *a = va_arg (*args, cnat_endpoint_tuple_t *);
118 if (unformat (input, "%U->%U%U", unformat_cnat_ep, &a->src_ep,
119 unformat_cnat_ep, &a->dst_ep, unformat_cnat_ep_flags, &flgs))
121 else if (unformat (input, "->%U%U", unformat_cnat_ep, &a->dst_ep,
122 unformat_cnat_ep_flags, &flgs))
124 else if (unformat (input, "%U->%U", unformat_cnat_ep, &a->src_ep,
125 unformat_cnat_ep_flags, &flgs))
133 format_cnat_endpoint (u8 * s, va_list * args)
135 cnat_endpoint_t *cep = va_arg (*args, cnat_endpoint_t *);
136 vnet_main_t *vnm = vnet_get_main ();
137 if (INDEX_INVALID == cep->ce_sw_if_index)
138 s = format (s, "%U;%d", format_ip_address, &cep->ce_ip, cep->ce_port);
141 if (cep->ce_flags & CNAT_EP_FLAG_RESOLVED)
142 s = format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
143 cep->ce_sw_if_index, format_ip_address, &cep->ce_ip,
147 format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
148 cep->ce_sw_if_index, format_ip_address_family,
149 cep->ce_ip.version, cep->ce_port);
155 cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type)
157 vlib_main_t *vm = vlib_get_main ();
158 vlib_process_signal_event (vm, cnat_main.scanner_node_index, event_type, 0);
164 cnat_main_t *cm = &cnat_main;
165 if (cm->lazy_init_done)
167 cnat_enable_disable_scanner (cm->default_scanner_state);
168 cm->lazy_init_done = 1;
171 static clib_error_t *
172 cnat_config (vlib_main_t * vm, unformat_input_t * input)
174 cnat_main_t *cm = &cnat_main;
176 cm->session_hash_memory = CNAT_DEFAULT_SESSION_MEMORY;
177 cm->session_hash_buckets = CNAT_DEFAULT_SESSION_BUCKETS;
178 cm->translation_hash_memory = CNAT_DEFAULT_TRANSLATION_MEMORY;
179 cm->translation_hash_buckets = CNAT_DEFAULT_TRANSLATION_BUCKETS;
180 cm->client_hash_memory = CNAT_DEFAULT_CLIENT_MEMORY;
181 cm->client_hash_buckets = CNAT_DEFAULT_CLIENT_BUCKETS;
182 cm->snat_hash_memory = CNAT_DEFAULT_SNAT_MEMORY;
183 cm->snat_hash_buckets = CNAT_DEFAULT_SNAT_BUCKETS;
184 cm->snat_if_map_length = CNAT_DEFAULT_SNAT_IF_MAP_LEN;
185 cm->scanner_timeout = CNAT_DEFAULT_SCANNER_TIMEOUT;
186 cm->session_max_age = CNAT_DEFAULT_SESSION_MAX_AGE;
187 cm->tcp_max_age = CNAT_DEFAULT_TCP_MAX_AGE;
188 cm->default_scanner_state = CNAT_SCANNER_ON;
189 cm->maglev_len = CNAT_DEFAULT_MAGLEV_LEN;
190 cm->lazy_init_done = 0;
192 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
195 (input, "session-db-buckets %u", &cm->session_hash_buckets))
197 else if (unformat (input, "session-db-memory %U",
198 unformat_memory_size, &cm->session_hash_memory))
200 else if (unformat (input, "translation-db-buckets %u",
201 &cm->translation_hash_buckets))
203 else if (unformat (input, "translation-db-memory %U",
204 unformat_memory_size, &cm->translation_hash_memory))
206 else if (unformat (input, "client-db-buckets %u",
207 &cm->client_hash_buckets))
209 else if (unformat (input, "client-db-memory %U", unformat_memory_size,
210 &cm->client_hash_memory))
212 else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
214 else if (unformat (input, "snat-if-map-len %u", &cm->snat_if_map_length))
216 else if (unformat (input, "snat-db-memory %U",
217 unformat_memory_size, &cm->snat_hash_memory))
219 else if (unformat (input, "session-cleanup-timeout %f",
220 &cm->scanner_timeout))
222 else if (unformat (input, "scanner off"))
223 cm->default_scanner_state = CNAT_SCANNER_OFF;
224 else if (unformat (input, "scanner on"))
225 cm->default_scanner_state = CNAT_SCANNER_ON;
226 else if (unformat (input, "session-max-age %u", &cm->session_max_age))
228 else if (unformat (input, "tcp-max-age %u", &cm->tcp_max_age))
230 else if (unformat (input, "maglev-len %u", &cm->maglev_len))
233 return clib_error_return (0, "unknown input '%U'",
234 format_unformat_error, input);
246 VLIB_EARLY_CONFIG_FUNCTION (cnat_config, "cnat");
249 * fd.io coding-style-patch-verification: ON
252 * eval: (c-set-style "gnu")