cnat: allow max_u16 translation backends
[vpp.git] / src / plugins / cnat / cnat_api.c
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  */
40 static u32 cnat_base_msg_id;
41
42 #define REPLY_MSG_ID_BASE cnat_base_msg_id
43
44 #include <vlibapi/api_helper_macros.h>
45
46 static void
47 cnat_endpoint_decode (const vl_api_cnat_endpoint_t * in,
48                       cnat_endpoint_t * out)
49 {
50   out->ce_port = clib_net_to_host_u16 (in->port);
51   out->ce_sw_if_index = clib_net_to_host_u32 (in->sw_if_index);
52   out->ce_flags = 0;
53   if (out->ce_sw_if_index == INDEX_INVALID)
54     ip_address_decode2 (&in->addr, &out->ce_ip);
55   else
56     ip_address_family_decode (in->if_af, &out->ce_ip.version);
57 }
58
59 static void
60 cnat_endpoint_tuple_decode (const vl_api_cnat_endpoint_tuple_t * in,
61                             cnat_endpoint_tuple_t * out)
62 {
63   cnat_endpoint_decode (&in->src_ep, &out->src_ep);
64   cnat_endpoint_decode (&in->dst_ep, &out->dst_ep);
65 }
66
67 static void
68 cnat_endpoint_encode (const cnat_endpoint_t * in,
69                       vl_api_cnat_endpoint_t * out)
70 {
71   out->port = clib_net_to_host_u16 (in->ce_port);
72   out->sw_if_index = clib_net_to_host_u32 (in->ce_sw_if_index);
73   out->if_af = ip_address_family_encode (in->ce_ip.version);
74   if (in->ce_flags & CNAT_EP_FLAG_RESOLVED)
75     ip_address_encode2 (&in->ce_ip, &out->addr);
76   else
77     clib_memset ((void *) &in->ce_ip, 0, sizeof (in->ce_ip));
78 }
79
80 static void
81 vl_api_cnat_translation_update_t_handler (vl_api_cnat_translation_update_t
82                                           * mp)
83 {
84   vl_api_cnat_translation_update_reply_t *rmp;
85   cnat_endpoint_t vip;
86   cnat_endpoint_tuple_t *paths = NULL, *path;
87   ip_protocol_t ip_proto;
88   u32 id = ~0;
89   u8 flags;
90   int rv = 0;
91   u32 pi, n_paths;
92
93   rv = ip_proto_decode (mp->translation.ip_proto, &ip_proto);
94
95   if (rv)
96     goto done;
97
98   n_paths = clib_net_to_host_u32 (mp->translation.n_paths);
99   vec_validate (paths, n_paths - 1);
100
101   for (pi = 0; pi < n_paths; pi++)
102     {
103       path = &paths[pi];
104       cnat_endpoint_tuple_decode (&mp->translation.paths[pi], path);
105     }
106   cnat_endpoint_decode (&mp->translation.vip, &vip);
107
108   flags = mp->translation.flags;
109   if (!mp->translation.is_real_ip)
110     flags |= CNAT_FLAG_EXCLUSIVE;
111   id = cnat_translation_update (&vip, ip_proto, paths, flags);
112
113   vec_free (paths);
114
115 done:
116   /* *INDENT-OFF* */
117   REPLY_MACRO2 (VL_API_CNAT_TRANSLATION_UPDATE_REPLY,
118   ({
119     rmp->id = htonl (id);
120   }));
121   /* *INDENT-ON* */
122 }
123
124 static void
125 vl_api_cnat_translation_del_t_handler (vl_api_cnat_translation_del_t * mp)
126 {
127   vl_api_cnat_translation_del_reply_t *rmp;
128   int rv;
129
130   rv = cnat_translation_delete (ntohl (mp->id));
131
132   REPLY_MACRO (VL_API_CNAT_TRANSLATION_DEL_REPLY);
133 }
134
135 typedef struct cnat_dump_walk_ctx_t_
136 {
137   vl_api_registration_t *rp;
138   u32 context;
139 } cnat_dump_walk_ctx_t;
140
141 static walk_rc_t
142 cnat_translation_send_details (u32 cti, void *args)
143 {
144   vl_api_cnat_translation_details_t *mp;
145   cnat_dump_walk_ctx_t *ctx;
146   cnat_ep_trk_t *trk;
147   vl_api_cnat_endpoint_tuple_t *path;
148   size_t msg_size;
149   cnat_translation_t *ct;
150   u32 n_paths;
151
152   ctx = args;
153   ct = cnat_translation_get (cti);
154   n_paths = vec_len (ct->ct_paths);
155   msg_size = sizeof (*mp) + sizeof (mp->translation.paths[0]) * n_paths;
156
157   mp = vl_msg_api_alloc_zero (msg_size);
158   mp->_vl_msg_id = ntohs (VL_API_CNAT_TRANSLATION_DETAILS + cnat_base_msg_id);
159
160   /* fill in the message */
161   mp->context = ctx->context;
162   mp->translation.n_paths = clib_host_to_net_u32 (n_paths);
163   mp->translation.id = clib_host_to_net_u32 (cti);
164   cnat_endpoint_encode (&ct->ct_vip, &mp->translation.vip);
165   mp->translation.ip_proto = ip_proto_encode (ct->ct_proto);
166
167   path = mp->translation.paths;
168   vec_foreach (trk, ct->ct_paths)
169   {
170     cnat_endpoint_encode (&trk->ct_ep[VLIB_TX], &path->dst_ep);
171     cnat_endpoint_encode (&trk->ct_ep[VLIB_RX], &path->src_ep);
172     path++;
173   }
174
175   vl_api_send_msg (ctx->rp, (u8 *) mp);
176
177   return (WALK_CONTINUE);
178 }
179
180 static void
181 vl_api_cnat_translation_dump_t_handler (vl_api_cnat_translation_dump_t * mp)
182 {
183   vl_api_registration_t *rp;
184
185   rp = vl_api_client_index_to_registration (mp->client_index);
186   if (rp == 0)
187     return;
188
189   cnat_dump_walk_ctx_t ctx = {
190     .rp = rp,
191     .context = mp->context,
192   };
193
194   cnat_translation_walk (cnat_translation_send_details, &ctx);
195 }
196
197 static void
198 ip_address2_from_46 (const ip46_address_t * nh,
199                      ip_address_family_t af, ip_address_t * ip)
200 {
201   ip_addr_46 (ip) = *nh;
202   ip_addr_version (ip) = af;
203 }
204
205 static walk_rc_t
206 cnat_session_send_details (const cnat_session_t * session, void *args)
207 {
208   vl_api_cnat_session_details_t *mp;
209   cnat_dump_walk_ctx_t *ctx;
210   cnat_endpoint_t ep;
211
212   ctx = args;
213
214   mp = vl_msg_api_alloc_zero (sizeof (*mp));
215   mp->_vl_msg_id = ntohs (VL_API_CNAT_SESSION_DETAILS + cnat_base_msg_id);
216
217   /* fill in the message */
218   mp->context = ctx->context;
219
220   ip_address2_from_46 (&session->value.cs_ip[VLIB_TX], session->key.cs_af,
221                        &ep.ce_ip);
222   ep.ce_port = clib_host_to_net_u16 (session->value.cs_port[VLIB_TX]);
223   cnat_endpoint_encode (&ep, &mp->session.new);
224
225   ip_address2_from_46 (&session->key.cs_ip[VLIB_RX], session->key.cs_af,
226                        &ep.ce_ip);
227   ep.ce_port = clib_host_to_net_u16 (session->key.cs_port[VLIB_RX]);
228   cnat_endpoint_encode (&ep, &mp->session.src);
229
230   ip_address2_from_46 (&session->key.cs_ip[VLIB_TX], session->key.cs_af,
231                        &ep.ce_ip);
232   ep.ce_port = clib_host_to_net_u16 (session->key.cs_port[VLIB_TX]);
233   cnat_endpoint_encode (&ep, &mp->session.dst);
234
235   mp->session.ip_proto = ip_proto_encode (session->key.cs_proto);
236
237   vl_api_send_msg (ctx->rp, (u8 *) mp);
238
239   return (WALK_CONTINUE);
240 }
241
242 static void
243 vl_api_cnat_session_dump_t_handler (vl_api_cnat_session_dump_t * mp)
244 {
245   vl_api_registration_t *rp;
246
247   rp = vl_api_client_index_to_registration (mp->client_index);
248   if (rp == 0)
249     return;
250
251   cnat_dump_walk_ctx_t ctx = {
252     .rp = rp,
253     .context = mp->context,
254   };
255
256   cnat_session_walk (cnat_session_send_details, &ctx);
257 }
258
259 static void
260 vl_api_cnat_session_purge_t_handler (vl_api_cnat_session_purge_t * mp)
261 {
262   vl_api_cnat_session_purge_reply_t *rmp;
263   int rv;
264
265   cnat_client_throttle_pool_process ();
266   rv = cnat_session_purge ();
267   rv |= cnat_translation_purge ();
268
269   REPLY_MACRO (VL_API_CNAT_SESSION_PURGE_REPLY);
270 }
271
272 static void
273 vl_api_cnat_get_snat_addresses_t_handler (vl_api_cnat_get_snat_addresses_t
274                                           * mp)
275 {
276   vl_api_cnat_get_snat_addresses_reply_t *rmp;
277   int rv = 0;
278
279   /* *INDENT-OFF* */
280   REPLY_MACRO2 (VL_API_CNAT_GET_SNAT_ADDRESSES_REPLY,
281   ({
282     ip6_address_encode (&ip_addr_v6(&cnat_main.snat_ip6.ce_ip), rmp->snat_ip6);
283     ip4_address_encode (&ip_addr_v4(&cnat_main.snat_ip4.ce_ip), rmp->snat_ip4);
284     rmp->sw_if_index = clib_host_to_net_u32 (cnat_main.snat_ip6.ce_sw_if_index);
285   }));
286   /* *INDENT-ON* */
287 }
288
289 static void
290 vl_api_cnat_set_snat_addresses_t_handler (vl_api_cnat_set_snat_addresses_t
291                                           * mp)
292 {
293   vl_api_cnat_set_snat_addresses_reply_t *rmp;
294   u32 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
295   ip4_address_t ip4;
296   ip6_address_t ip6;
297   int rv = 0;
298
299   ip4_address_decode (mp->snat_ip4, &ip4);
300   ip6_address_decode (mp->snat_ip6, &ip6);
301
302   cnat_set_snat (&ip4, &ip6, sw_if_index);
303
304   REPLY_MACRO (VL_API_CNAT_SET_SNAT_ADDRESSES_REPLY);
305 }
306
307 static void
308   vl_api_cnat_add_del_snat_prefix_t_handler
309   (vl_api_cnat_add_del_snat_prefix_t * mp)
310 {
311   vl_api_cnat_add_del_snat_prefix_reply_t *rmp;
312   ip_prefix_t pfx;
313   int rv;
314
315   ip_prefix_decode2 (&mp->prefix, &pfx);
316   if (mp->is_add)
317     rv = cnat_add_snat_prefix (&pfx);
318   else
319     rv = cnat_del_snat_prefix (&pfx);
320
321   REPLY_MACRO (VL_API_CNAT_ADD_DEL_SNAT_PREFIX_REPLY);
322 }
323
324 #include <cnat/cnat.api.c>
325
326 static clib_error_t *
327 cnat_api_init (vlib_main_t * vm)
328 {
329   /* Ask for a correctly-sized block of API message decode slots */
330   cnat_base_msg_id = setup_message_id_table ();
331
332   return 0;
333 }
334
335 VLIB_INIT_FUNCTION (cnat_api_init);
336
337 /* *INDENT-OFF* */
338 VLIB_PLUGIN_REGISTER () = {
339     .version = VPP_BUILD_VER,
340     .description = "CNat Translate",
341 };
342 /* *INDENT-ON* */
343
344 /*
345  * fd.io coding-style-patch-verification: ON
346  *
347  * Local Variables:
348  * eval: (c-set-style "gnu")
349  * End:
350  */