FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
nat66.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /**
16  * @file
17  * @brief NAT66 implementation
18  */
19 
20 #include <nat/nat66.h>
21 #include <vnet/fib/fib_table.h>
22 
24 
25 /* *INDENT-OFF* */
26 
27 /* Hook up input features */
28 VNET_FEATURE_INIT (nat66_in2out, static) = {
29  .arc_name = "ip6-unicast",
30  .node_name = "nat66-in2out",
31  .runs_before = VNET_FEATURES ("ip6-lookup"),
32 };
33 VNET_FEATURE_INIT (nat66_out2in, static) = {
34  .arc_name = "ip6-unicast",
35  .node_name = "nat66-out2in",
36  .runs_before = VNET_FEATURES ("ip6-lookup"),
37 };
38 
39 /* *INDENT-ON* */
40 
41 
42 void
44 {
45  nat66_main_t *nm = &nat66_main;
46  vlib_node_t *node;
47  u32 static_mapping_buckets = 1024;
48  uword static_mapping_memory_size = 64 << 20;
49 
50  node = vlib_get_node_by_name (vm, (u8 *) "nat66-in2out");
51  nm->in2out_node_index = node->index;
52 
53  node = vlib_get_node_by_name (vm, (u8 *) "nat66-out2in");
54  nm->out2in_node_index = node->index;
55 
56  clib_bihash_init_24_8 (&nm->sm_l, "nat66-static-map-by-local",
57  static_mapping_buckets, static_mapping_memory_size);
58  clib_bihash_init_24_8 (&nm->sm_e, "nat66-static-map-by-external",
59  static_mapping_buckets, static_mapping_memory_size);
60 
61  nm->session_counters.name = "session counters";
62 }
63 
64 int
66 {
67  nat66_main_t *nm = &nat66_main;
68  snat_interface_t *interface = 0, *i;
69  const char *feature_name;
70 
71  /* *INDENT-OFF* */
73  ({
74  if (i->sw_if_index == sw_if_index)
75  {
76  interface = i;
77  break;
78  }
79  }));
80  /* *INDENT-ON* */
81 
82  if (is_add)
83  {
84  if (interface)
85  return VNET_API_ERROR_VALUE_EXIST;
86 
87  pool_get (nm->interfaces, interface);
88  interface->sw_if_index = sw_if_index;
89  interface->flags =
90  is_inside ? NAT_INTERFACE_FLAG_IS_INSIDE :
92  }
93  else
94  {
95  if (!interface)
96  return VNET_API_ERROR_NO_SUCH_ENTRY;
97 
98  pool_put (nm->interfaces, interface);
99  }
100 
101  feature_name = is_inside ? "nat66-in2out" : "nat66-out2in";
102  return vnet_feature_enable_disable ("ip6-unicast", feature_name,
103  sw_if_index, is_add, 0, 0);
104 }
105 
106 void
108 {
109  nat66_main_t *nm = &nat66_main;
110  snat_interface_t *i = 0;
111 
112  /* *INDENT-OFF* */
113  pool_foreach (i, nm->interfaces,
114  ({
115  if (fn (i, ctx))
116  break;
117  }));
118  /* *INDENT-ON* */
119 }
120 
123 {
124  nat66_main_t *nm = &nat66_main;
125  nat66_static_mapping_t *sm = 0;
126  nat66_sm_key_t sm_key;
128 
129  sm_key.addr.as_u64[0] = addr->as_u64[0];
130  sm_key.addr.as_u64[1] = addr->as_u64[1];
131  sm_key.fib_index = fib_index;
132  sm_key.rsvd = 0;
133 
134  kv.key[0] = sm_key.as_u64[0];
135  kv.key[1] = sm_key.as_u64[1];
136  kv.key[2] = sm_key.as_u64[2];
137 
138  if (!clib_bihash_search_24_8
139  (is_local ? &nm->sm_l : &nm->sm_e, &kv, &value))
140  sm = pool_elt_at_index (nm->sm, value.value);
141 
142  return sm;
143 }
144 
145 int
147  u32 vrf_id, u8 is_add)
148 {
149  nat66_main_t *nm = &nat66_main;
150  int rv = 0;
151  nat66_static_mapping_t *sm = 0;
152  nat66_sm_key_t sm_key;
154  u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6, vrf_id);
155 
156  sm_key.addr.as_u64[0] = l_addr->as_u64[0];
157  sm_key.addr.as_u64[1] = l_addr->as_u64[1];
158  sm_key.fib_index = fib_index;
159  sm_key.rsvd = 0;
160  kv.key[0] = sm_key.as_u64[0];
161  kv.key[1] = sm_key.as_u64[1];
162  kv.key[2] = sm_key.as_u64[2];
163 
164  if (!clib_bihash_search_24_8 (&nm->sm_l, &kv, &value))
165  sm = pool_elt_at_index (nm->sm, value.value);
166 
167  if (is_add)
168  {
169  if (sm)
170  return VNET_API_ERROR_VALUE_EXIST;
171 
174  pool_get (nm->sm, sm);
175  clib_memset (sm, 0, sizeof (*sm));
176  sm->l_addr.as_u64[0] = l_addr->as_u64[0];
177  sm->l_addr.as_u64[1] = l_addr->as_u64[1];
178  sm->e_addr.as_u64[0] = e_addr->as_u64[0];
179  sm->e_addr.as_u64[1] = e_addr->as_u64[1];
180  sm->fib_index = fib_index;
181 
182  sm_key.fib_index = fib_index;
183  kv.key[0] = sm_key.as_u64[0];
184  kv.key[1] = sm_key.as_u64[1];
185  kv.key[2] = sm_key.as_u64[2];
186  kv.value = sm - nm->sm;
187  if (clib_bihash_add_del_24_8 (&nm->sm_l, &kv, 1))
188  nat_elog_warn ("nat66-static-map-by-local add key failed");
189  sm_key.addr.as_u64[0] = e_addr->as_u64[0];
190  sm_key.addr.as_u64[1] = e_addr->as_u64[1];
191  sm_key.fib_index = 0;
192  kv.key[0] = sm_key.as_u64[0];
193  kv.key[1] = sm_key.as_u64[1];
194  kv.key[2] = sm_key.as_u64[2];
195  if (clib_bihash_add_del_24_8 (&nm->sm_e, &kv, 1))
196  nat_elog_warn ("nat66-static-map-by-external add key failed");
197 
200  }
201  else
202  {
203  if (!sm)
204  return VNET_API_ERROR_NO_SUCH_ENTRY;
205 
206  kv.value = sm - nm->sm;
207  if (clib_bihash_add_del_24_8 (&nm->sm_l, &kv, 0))
208  nat_elog_warn ("nat66-static-map-by-local delete key failed");
209  sm_key.addr.as_u64[0] = e_addr->as_u64[0];
210  sm_key.addr.as_u64[1] = e_addr->as_u64[1];
211  sm_key.fib_index = 0;
212  kv.key[0] = sm_key.as_u64[0];
213  kv.key[1] = sm_key.as_u64[1];
214  kv.key[2] = sm_key.as_u64[2];
215  if (clib_bihash_add_del_24_8 (&nm->sm_e, &kv, 0))
216  nat_elog_warn ("nat66-static-map-by-external delete key failed");
219  pool_put (nm->sm, sm);
220  }
221 
222  return rv;
223 }
224 
225 void
227 {
228  nat66_main_t *nm = &nat66_main;
229  nat66_static_mapping_t *sm = 0;
230 
231  /* *INDENT-OFF* */
232  pool_foreach (sm, nm->sm,
233  ({
234  if (fn (sm, ctx))
235  break;
236  }));
237  /* *INDENT-ON* */
238 }
239 
240 /*
241  * fd.io coding-style-patch-verification: ON
242  *
243  * Local Variables:
244  * eval: (c-set-style "gnu")
245  * End:
246  */
u32 rsvd
Definition: nat66.h:40
void vlib_validate_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
validate a combined counter
Definition: counter.c:108
u64 as_u64[2]
Definition: ip6_packet.h:51
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:280
u32 fib_index
Definition: nat66.h:39
#define NAT_INTERFACE_FLAG_IS_OUTSIDE
Definition: nat.h:294
int i
#define nat_elog_warn(nat_elog_str)
Definition: nat.h:1019
u32 out2in_node_index
Definition: nat66.h:60
clib_bihash_24_8_t sm_e
Static mapping by external address lookup table.
Definition: nat66.h:55
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
vhost_vring_addr_t addr
Definition: vhost_user.h:147
unsigned char u8
Definition: types.h:56
nat66_main_t nat66_main
Definition: nat66.c:23
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
A high priority source a plugin can use.
Definition: fib_entry.h:67
unsigned int u32
Definition: types.h:88
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1080
void nat66_init(vlib_main_t *vm)
Definition: nat66.c:43
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static void vlib_zero_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
Clear a combined counter Clears the set of per-thread counters.
Definition: counter.h:285
void nat66_static_mappings_walk(nat66_static_mapping_walk_fn_t fn, void *ctx)
Definition: nat66.c:226
u64 as_u64[3]
Definition: nat66.h:42
long ctx[MAX_CONNS]
Definition: main.c:144
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
int(* nat66_static_mapping_walk_fn_t)(nat66_static_mapping_t *sm, void *ctx)
Definition: nat66.h:74
void fib_table_unlock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Take a reference counting lock on the table.
Definition: fib_table.c:1253
int nat66_interface_add_del(u32 sw_if_index, u8 is_inside, u8 is_add)
Definition: nat66.c:65
ip6_address_t e_addr
Definition: nat66.h:28
vlib_main_t * vm
Definition: buffer.c:323
snat_interface_t * interfaces
Interface pool.
Definition: nat66.h:49
clib_bihash_24_8_t sm_l
Static mapping by local address lookup table.
Definition: nat66.h:53
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
VNET_FEATURE_INIT(nat66_in2out, static)
u8 value
Definition: qos.api:53
#define NAT_INTERFACE_FLAG_IS_INSIDE
Definition: nat.h:293
u32 fib_table_find_or_create_and_lock(fib_protocol_t proto, u32 table_id, fib_source_t src)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1139
#define VNET_FEATURES(...)
Definition: feature.h:442
ip6_address_t l_addr
Definition: nat66.h:27
vlib_combined_counter_main_t session_counters
Session counters.
Definition: nat66.h:57
int(* nat66_interface_walk_fn_t)(snat_interface_t *i, void *ctx)
Definition: nat66.h:71
void nat66_interfaces_walk(nat66_interface_walk_fn_t fn, void *ctx)
Definition: nat66.c:107
u64 uword
Definition: types.h:112
char * name
The counter collection&#39;s name.
Definition: counter.h:193
int nat66_static_mapping_add_del(ip6_address_t *l_addr, ip6_address_t *e_addr, u32 vrf_id, u8 is_add)
Definition: nat66.c:146
ip6_address_t addr
Definition: nat66.h:38
nat66_static_mapping_t * sm
Static mapping pool.
Definition: nat66.h:51
u32 in2out_node_index
node index
Definition: nat66.h:59
NAT66 global declarations.
nat66_static_mapping_t * nat66_static_mapping_get(ip6_address_t *addr, u32 fib_index, u8 is_local)
Definition: nat66.c:122
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:275