FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
cnat_api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 #include <stddef.h>
17 
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <cnat/cnat_translation.h>
21 #include <cnat/cnat_session.h>
22 #include <cnat/cnat_client.h>
23 #include <cnat/cnat_snat.h>
24 
25 #include <vnet/ip/ip_types_api.h>
26 
27 #include <vpp/app/version.h>
28 
29 #include <vlibapi/api.h>
30 #include <vlibmemory/api.h>
31 
32 /* define message IDs */
33 #include <vnet/format_fns.h>
34 #include <cnat/cnat.api_enum.h>
35 #include <cnat/cnat.api_types.h>
36 
37 /**
38  * Base message ID fot the plugin
39  */
41 
42 #define REPLY_MSG_ID_BASE cnat_base_msg_id
43 
45 
46 static void
47 cnat_endpoint_decode (const vl_api_cnat_endpoint_t * in,
48  cnat_endpoint_t * out)
49 {
50  ip_address_decode2 (&in->addr, &out->ce_ip);
51  out->ce_port = clib_net_to_host_u16 (in->port);
52 }
53 
54 static void
55 cnat_endpoint_tuple_decode (const vl_api_cnat_endpoint_tuple_t * in,
57 {
58  cnat_endpoint_decode (&in->src_ep, &out->src_ep);
59  cnat_endpoint_decode (&in->dst_ep, &out->dst_ep);
60 }
61 
62 static void
64  vl_api_cnat_endpoint_t * out)
65 {
66  ip_address_encode2 (&in->ce_ip, &out->addr);
67  out->port = clib_net_to_host_u16 (in->ce_port);
68 }
69 
70 static void
72  * mp)
73 {
75  cnat_endpoint_t vip;
78  u32 id = ~0;
79  u8 flags;
80  int rv = 0;
81  u8 pi;
82 
83  rv = ip_proto_decode (mp->translation.ip_proto, &ip_proto);
84 
85  if (rv)
86  goto done;
87 
88  vec_validate (paths, mp->translation.n_paths - 1);
89 
90  for (pi = 0; pi < mp->translation.n_paths; pi++)
91  {
92  path = &paths[pi];
93  cnat_endpoint_tuple_decode (&mp->translation.paths[pi], path);
94  }
95  cnat_endpoint_decode (&mp->translation.vip, &vip);
96 
97  flags = mp->translation.flags;
98  if (!mp->translation.is_real_ip)
99  flags |= CNAT_FLAG_EXCLUSIVE;
100  id = cnat_translation_update (&vip, ip_proto, paths, flags);
101 
102  vec_free (paths);
103 
104 done:
105  /* *INDENT-OFF* */
106  REPLY_MACRO2 (VL_API_CNAT_TRANSLATION_UPDATE_REPLY,
107  ({
108  rmp->id = htonl (id);
109  }));
110  /* *INDENT-ON* */
111 }
112 
113 static void
115 {
116  vl_api_cnat_translation_del_reply_t *rmp;
117  int rv;
118 
119  rv = cnat_translation_delete (ntohl (mp->id));
120 
121  REPLY_MACRO (VL_API_CNAT_TRANSLATION_DEL_REPLY);
122 }
123 
124 typedef struct cnat_dump_walk_ctx_t_
125 {
129 
130 static walk_rc_t
132 {
135  cnat_ep_trk_t *trk;
136  vl_api_cnat_endpoint_tuple_t *path;
137  size_t msg_size;
138  cnat_translation_t *ct;
139  u8 n_paths;
140 
141  ctx = args;
142  ct = cnat_translation_get (cti);
143  n_paths = vec_len (ct->ct_paths);
144  msg_size = sizeof (*mp) + sizeof (mp->translation.paths[0]) * n_paths;
145 
146  mp = vl_msg_api_alloc_zero (msg_size);
147  mp->_vl_msg_id = ntohs (VL_API_CNAT_TRANSLATION_DETAILS + cnat_base_msg_id);
148 
149  /* fill in the message */
150  mp->context = ctx->context;
151  mp->translation.n_paths = n_paths;
152  mp->translation.id = htonl (cti);
153  cnat_endpoint_encode (&ct->ct_vip, &mp->translation.vip);
154  mp->translation.ip_proto = ip_proto_encode (ct->ct_proto);
155 
156  path = mp->translation.paths;
157  vec_foreach (trk, ct->ct_paths)
158  {
159  cnat_endpoint_encode (&trk->ct_ep[VLIB_TX], &path->dst_ep);
160  cnat_endpoint_encode (&trk->ct_ep[VLIB_RX], &path->src_ep);
161  path++;
162  }
163 
164  vl_api_send_msg (ctx->rp, (u8 *) mp);
165 
166  return (WALK_CONTINUE);
167 }
168 
169 static void
171 {
173 
175  if (rp == 0)
176  return;
177 
179  .rp = rp,
180  .context = mp->context,
181  };
182 
184 }
185 
186 static void
187 ip_address2_from_46 (const ip46_address_t * nh,
189 {
190  ip_addr_46 (ip) = *nh;
191  ip_addr_version (ip) = af;
192 }
193 
194 static walk_rc_t
195 cnat_session_send_details (const cnat_session_t * session, void *args)
196 {
199  cnat_endpoint_t ep;
200 
201  ctx = args;
202 
203  mp = vl_msg_api_alloc_zero (sizeof (*mp));
204  mp->_vl_msg_id = ntohs (VL_API_CNAT_SESSION_DETAILS + cnat_base_msg_id);
205 
206  /* fill in the message */
207  mp->context = ctx->context;
208 
209  ip_address2_from_46 (&session->value.cs_ip[VLIB_TX], session->key.cs_af,
210  &ep.ce_ip);
211  ep.ce_port = clib_host_to_net_u16 (session->value.cs_port[VLIB_TX]);
212  cnat_endpoint_encode (&ep, &mp->session.new);
213 
214  ip_address2_from_46 (&session->key.cs_ip[VLIB_RX], session->key.cs_af,
215  &ep.ce_ip);
216  ep.ce_port = clib_host_to_net_u16 (session->key.cs_port[VLIB_RX]);
217  cnat_endpoint_encode (&ep, &mp->session.src);
218 
219  ip_address2_from_46 (&session->key.cs_ip[VLIB_TX], session->key.cs_af,
220  &ep.ce_ip);
221  ep.ce_port = clib_host_to_net_u16 (session->key.cs_port[VLIB_TX]);
222  cnat_endpoint_encode (&ep, &mp->session.dst);
223 
224  mp->session.ip_proto = ip_proto_encode (session->key.cs_proto);
225 
226  vl_api_send_msg (ctx->rp, (u8 *) mp);
227 
228  return (WALK_CONTINUE);
229 }
230 
231 static void
233 {
235 
237  if (rp == 0)
238  return;
239 
241  .rp = rp,
242  .context = mp->context,
243  };
244 
246 }
247 
248 static void
250 {
251  vl_api_cnat_session_purge_reply_t *rmp;
252  int rv;
253 
255  rv = cnat_session_purge ();
256  rv |= cnat_translation_purge ();
257 
258  REPLY_MACRO (VL_API_CNAT_SESSION_PURGE_REPLY);
259 }
260 
261 static void
263  * mp)
264 {
265  vl_api_cnat_set_snat_addresses_reply_t *rmp;
266  int rv = 0;
267 
270 
271  REPLY_MACRO (VL_API_CNAT_SET_SNAT_ADDRESSES_REPLY);
272 }
273 
274 static void
277 {
278  vl_api_cnat_add_del_snat_prefix_reply_t *rmp;
279  ip_prefix_t pfx;
280  int rv;
281 
282  ip_prefix_decode2 (&mp->prefix, &pfx);
283  if (mp->is_add)
284  rv = cnat_add_snat_prefix (&pfx);
285  else
286  rv = cnat_del_snat_prefix (&pfx);
287 
288  REPLY_MACRO (VL_API_CNAT_ADD_DEL_SNAT_PREFIX_REPLY);
289 }
290 
291 #include <cnat/cnat.api.c>
292 
293 static clib_error_t *
295 {
296  /* Ask for a correctly-sized block of API message decode slots */
297  cnat_base_msg_id = setup_message_id_table ();
298 
299  return 0;
300 }
301 
303 
304 /* *INDENT-OFF* */
306  .version = VPP_BUILD_VER,
307  .description = "CNat Translate",
308 };
309 /* *INDENT-ON* */
310 
311 /*
312  * fd.io coding-style-patch-verification: ON
313  *
314  * Local Variables:
315  * eval: (c-set-style "gnu")
316  * End:
317  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
static u32 cnat_base_msg_id
Base message ID fot the plugin.
Definition: cnat_api.c:40
#define ip_addr_46(_a)
Definition: ip_types.h:57
static void ip_address2_from_46(const ip46_address_t *nh, ip_address_family_t af, ip_address_t *ip)
Definition: cnat_api.c:187
void ip_address_encode2(const ip_address_t *in, vl_api_address_t *out)
Definition: ip_types_api.c:216
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:103
#define ntohs(x)
Definition: af_xdp.bpf.c:29
static_always_inline cnat_translation_t * cnat_translation_get(index_t cti)
vl_api_fib_path_nh_t nh
Definition: fib_types.api:126
vl_api_ip6_address_t snat_ip6
Definition: cnat.api:119
static void cnat_endpoint_decode(const vl_api_cnat_endpoint_t *in, cnat_endpoint_t *out)
Definition: cnat_api.c:47
#define REPLY_MACRO2(t, body)
ip_proto
Definition: ip_types.api:64
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
u16 cs_port[VLIB_N_DIR]
ports in rx/tx
Definition: cnat_session.h:53
void ip6_address_decode(const vl_api_ip6_address_t in, ip6_address_t *out)
Definition: ip_types_api.c:117
ip_protocol_t ct_proto
The ip protocol for the translation.
vl_api_registration_t * rp
Definition: cnat_api.c:126
A Translation represents the translation of a VEP to one of a set of real server addresses.
vlib_main_t * vm
Definition: in2out_ed.c:1582
cnat_endpoint_t src_ep
Definition: cnat_types.h:61
vl_api_fib_path_t path
Definition: mfib_types.api:34
int cnat_add_snat_prefix(ip_prefix_t *pfx)
Definition: cnat_snat.c:36
struct cnat_dump_walk_ctx_t_ cnat_dump_walk_ctx_t
#define ip_addr_version(_a)
Definition: ip_types.h:60
void * vl_msg_api_alloc_zero(int nbytes)
unsigned char u8
Definition: types.h:56
vl_api_cnat_translation_t translation
Definition: cnat.api:58
static void cnat_endpoint_encode(const cnat_endpoint_t *in, vl_api_cnat_endpoint_t *out)
Definition: cnat_api.c:63
A session represents the memory of a translation.
Definition: cnat_session.h:38
enum walk_rc_t_ walk_rc_t
Walk return code.
int cnat_del_snat_prefix(ip_prefix_t *pfx)
Definition: cnat_snat.c:67
u8 cs_af
The address family describing the IP addresses.
Definition: cnat_session.h:63
cnat_endpoint_t dst_ep
Definition: cnat_types.h:60
int cnat_session_purge(void)
Purge all the sessions.
Definition: cnat_session.c:140
VLIB_PLUGIN_REGISTER()
cnat_endpoint_t ct_vip
The Virtual end point.
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
u32 cnat_translation_update(const cnat_endpoint_t *vip, ip_protocol_t proto, const cnat_endpoint_tuple_t *paths, u8 flags)
create or update a translation
unsigned int u32
Definition: types.h:88
static void vl_api_cnat_add_del_snat_prefix_t_handler(vl_api_cnat_add_del_snat_prefix_t *mp)
Definition: cnat_api.c:276
ip46_address_t cs_ip[VLIB_N_DIR]
IP 4/6 address in the rx/tx direction.
Definition: cnat_session.h:48
static walk_rc_t cnat_session_send_details(const cnat_session_t *session, void *args)
Definition: cnat_api.c:195
int cnat_translation_purge(void)
Purge all the trahslations.
vl_api_cnat_session_t session
Definition: cnat.api:105
static void vl_api_cnat_session_purge_t_handler(vl_api_cnat_session_purge_t *mp)
Definition: cnat_api.c:249
enum ip_protocol ip_protocol_t
static void cnat_endpoint_tuple_decode(const vl_api_cnat_endpoint_tuple_t *in, cnat_endpoint_tuple_t *out)
Definition: cnat_api.c:55
int cnat_translation_delete(u32 id)
Delete a translation.
long ctx[MAX_CONNS]
Definition: main.c:144
void cnat_client_throttle_pool_process()
Definition: cnat_client.c:74
static walk_rc_t cnat_translation_send_details(u32 cti, void *args)
Definition: cnat_api.c:131
#define REPLY_MACRO(t)
cnat_ep_trk_t * ct_paths
The vector of tracked back-ends.
void ip_address_decode2(const vl_api_address_t *in, ip_address_t *out)
Definition: ip_types_api.c:166
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
int ip_prefix_decode2(const vl_api_prefix_t *in, ip_prefix_t *out)
Definition: ip_types_api.c:250
vl_api_ip_proto_t ip_proto_encode(ip_protocol_t ipp)
Definition: ip_types_api.c:83
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
u8 n_paths
Definition: ip.api:145
cnat_endpoint_t ct_ep[VLIB_N_DIR]
The EP being tracked.
static void vl_api_cnat_session_dump_t_handler(vl_api_cnat_session_dump_t *mp)
Definition: cnat_api.c:232
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
vl_api_fib_path_t paths[n_paths]
Definition: ip.api:146
struct cnat_session_t_::@637 value
this value sits in the same memory location a &#39;value&#39; in the bihash kvp
static void vl_api_cnat_translation_del_t_handler(vl_api_cnat_translation_del_t *mp)
Definition: cnat_api.c:114
vl_api_ip4_address_t snat_ip4
Definition: cnat.api:118
static void vl_api_cnat_translation_dump_t_handler(vl_api_cnat_translation_dump_t *mp)
Definition: cnat_api.c:170
ip4_address_t snat_ip4
Definition: cnat_types.h:123
enum ip_address_family_t_ ip_address_family_t
void cnat_translation_walk(cnat_translation_walk_cb_t cb, void *ctx)
Walk/visit each of the translations.
Definition: defs.h:47
ip_protocol_t cs_proto
The IP protocol TCP or UDP only supported.
Definition: cnat_session.h:58
vl_api_address_t ip
Definition: l2.api:501
static void vl_api_cnat_set_snat_addresses_t_handler(vl_api_cnat_set_snat_addresses_t *mp)
Definition: cnat_api.c:262
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void ip4_address_decode(const vl_api_ip4_address_t in, ip4_address_t *out)
Definition: ip_types_api.c:129
struct cnat_session_t_::@636 key
this key sits in the same memory location a &#39;key&#39; in the bihash kvp
cnat_main_t cnat_main
Definition: cnat_types.c:18
vl_api_cnat_translation_t translation
Definition: cnat.api:78
#define vec_foreach(var, vec)
Vector iterator.
ip6_address_t snat_ip6
Definition: cnat_types.h:126
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:2804
ip_address_t ce_ip
Definition: cnat_types.h:54
static void vl_api_cnat_translation_update_t_handler(vl_api_cnat_translation_update_t *mp)
Definition: cnat_api.c:71
Data used to track an EP in the FIB.
int ip_proto_decode(vl_api_ip_proto_t ipp, ip_protocol_t *out)
Definition: ip_types_api.c:65
Definition: defs.h:46
static clib_error_t * cnat_api_init(vlib_main_t *vm)
Definition: cnat_api.c:294
void cnat_session_walk(cnat_session_walk_cb_t cb, void *ctx)
Walk/visit each of the cnat session.
Definition: cnat_session.c:44