SNAT: static mappings for dhcp addressed interfaces (VPP-590)
[vpp.git] / src / plugins / snat / snat.c
1 /*
2  * snat.c - simple nat plugin
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
22 #include <vlibapi/api.h>
23 #include <snat/snat.h>
24 #include <snat/snat_ipfix_logging.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
29
30 snat_main_t snat_main;
31
32 /* define message IDs */
33 #include <snat/snat_msg_enum.h>
34
35 /* define message structures */
36 #define vl_typedefs
37 #include <snat/snat_all_api_h.h> 
38 #undef vl_typedefs
39
40 /* define generated endian-swappers */
41 #define vl_endianfun
42 #include <snat/snat_all_api_h.h> 
43 #undef vl_endianfun
44
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46
47 /* Get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <snat/snat_all_api_h.h>
50 #undef vl_api_version
51
52 /* Macro to finish up custom dump fns */
53 #define FINISH                                  \
54     vec_add1 (s, 0);                            \
55     vl_print (handle, (char *)s);               \
56     vec_free (s);                               \
57     return handle;
58
59 /* 
60  * A handy macro to set up a message reply.
61  * Assumes that the following variables are available:
62  * mp - pointer to request message
63  * rmp - pointer to reply message type
64  * rv - return value
65  */
66
67 #define REPLY_MACRO(t)                                          \
68 do {                                                            \
69     unix_shared_memory_queue_t * q =                            \
70     vl_api_client_index_to_input_queue (mp->client_index);      \
71     if (!q)                                                     \
72         return;                                                 \
73                                                                 \
74     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
75     rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
76     rmp->context = mp->context;                                 \
77     rmp->retval = ntohl(rv);                                    \
78                                                                 \
79     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
80 } while(0);
81
82 #define REPLY_MACRO2(t, body)                                   \
83 do {                                                            \
84     unix_shared_memory_queue_t * q =                            \
85     vl_api_client_index_to_input_queue (mp->client_index);      \
86     if (!q)                                                     \
87         return;                                                 \
88                                                                 \
89     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
90     rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
91     rmp->context = mp->context;                                 \
92     rmp->retval = ntohl(rv);                                    \
93     do {body;} while (0);                                       \
94     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
95 } while(0);
96
97
98 /* Hook up input features */
99 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
100   .arc_name = "ip4-unicast",
101   .node_name = "snat-in2out",
102   .runs_before = VNET_FEATURES ("snat-out2in"),
103 };
104 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
105   .arc_name = "ip4-unicast",
106   .node_name = "snat-out2in",
107   .runs_before = VNET_FEATURES ("ip4-lookup"),
108 };
109 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
110   .arc_name = "ip4-unicast",
111   .node_name = "snat-in2out-worker-handoff",
112   .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
113 };
114 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
115   .arc_name = "ip4-unicast",
116   .node_name = "snat-out2in-worker-handoff",
117   .runs_before = VNET_FEATURES ("ip4-lookup"),
118 };
119 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
120   .arc_name = "ip4-unicast",
121   .node_name = "snat-in2out-fast",
122   .runs_before = VNET_FEATURES ("snat-out2in-fast"),
123 };
124 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
125   .arc_name = "ip4-unicast",
126   .node_name = "snat-out2in-fast",
127   .runs_before = VNET_FEATURES ("ip4-lookup"),
128 };
129
130
131 /* 
132  * This routine exists to convince the vlib plugin framework that
133  * we haven't accidentally copied a random .dll into the plugin directory.
134  *
135  * Also collects global variable pointers passed from the vpp engine
136  */
137
138 clib_error_t * 
139 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
140                       int from_early_init)
141 {
142   snat_main_t * sm = &snat_main;
143   clib_error_t * error = 0;
144
145   sm->vlib_main = vm;
146   sm->vnet_main = h->vnet_main;
147   sm->ethernet_main = h->ethernet_main;
148
149   return error;
150 }
151
152 /*$$$$$ move to an installed header file */
153 #if (1 || CLIB_DEBUG > 0)       /* "trust, but verify" */
154
155 #define VALIDATE_SW_IF_INDEX(mp)                                \
156  do { u32 __sw_if_index = ntohl(mp->sw_if_index);               \
157     vnet_main_t *__vnm = vnet_get_main();                       \
158     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
159                            __sw_if_index)) {                    \
160         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
161         goto bad_sw_if_index;                                   \
162     }                                                           \
163 } while(0);
164
165 #define BAD_SW_IF_INDEX_LABEL                   \
166 do {                                            \
167 bad_sw_if_index:                                \
168     ;                                           \
169 } while (0);
170
171 #define VALIDATE_RX_SW_IF_INDEX(mp)                             \
172  do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index);         \
173     vnet_main_t *__vnm = vnet_get_main();                       \
174     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
175                            __rx_sw_if_index)) {                 \
176         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
177         goto bad_rx_sw_if_index;                                \
178     }                                                           \
179 } while(0);
180
181 #define BAD_RX_SW_IF_INDEX_LABEL                \
182 do {                                            \
183 bad_rx_sw_if_index:                             \
184     ;                                           \
185 } while (0);
186
187 #define VALIDATE_TX_SW_IF_INDEX(mp)                             \
188  do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index);         \
189     vnet_main_t *__vnm = vnet_get_main();                       \
190     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
191                            __tx_sw_if_index)) {                 \
192         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
193         goto bad_tx_sw_if_index;                                \
194     }                                                           \
195 } while(0);
196
197 #define BAD_TX_SW_IF_INDEX_LABEL                \
198 do {                                            \
199 bad_tx_sw_if_index:                             \
200     ;                                           \
201 } while (0);
202
203 #else
204
205 #define VALIDATE_SW_IF_INDEX(mp)
206 #define BAD_SW_IF_INDEX_LABEL
207 #define VALIDATE_RX_SW_IF_INDEX(mp)
208 #define BAD_RX_SW_IF_INDEX_LABEL
209 #define VALIDATE_TX_SW_IF_INDEX(mp)
210 #define BAD_TX_SW_IF_INDEX_LABEL
211
212 #endif  /* CLIB_DEBUG > 0 */
213
214 void snat_add_address (snat_main_t *sm, ip4_address_t *addr)
215 {
216   snat_address_t * ap;
217
218   /* Check if address already exists */
219   vec_foreach (ap, sm->addresses)
220     {
221       if (ap->addr.as_u32 == addr->as_u32)
222         return;
223     }
224
225   vec_add2 (sm->addresses, ap, 1);
226   ap->addr = *addr;
227   clib_bitmap_alloc (ap->busy_port_bitmap, 65535);
228 }
229
230 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
231                                                    ip4_address_t addr)
232 {
233   snat_static_mapping_t *m;
234   pool_foreach (m, sm->static_mappings,
235   ({
236       if (m->external_addr.as_u32 == addr.as_u32)
237         return 1;
238   }));
239
240   return 0;
241 }
242
243 static void increment_v4_address (ip4_address_t * a)
244 {
245   u32 v;
246   
247   v = clib_net_to_host_u32(a->as_u32) + 1;
248   a->as_u32 = clib_host_to_net_u32(v);
249 }
250
251 static void 
252 snat_add_static_mapping_when_resolved (snat_main_t * sm, 
253                                        ip4_address_t l_addr, 
254                                        u16 l_port, 
255                                        u32 sw_if_index, 
256                                        u16 e_port, 
257                                        u32 vrf_id,
258                                        int addr_only,  
259                                        int is_add)
260 {
261   snat_static_map_resolve_t *rp;
262
263   vec_add2 (sm->to_resolve, rp, 1);
264   rp->l_addr.as_u32 = l_addr.as_u32;
265   rp->l_port = l_port;
266   rp->sw_if_index = sw_if_index;
267   rp->e_port = e_port;
268   rp->vrf_id = vrf_id;
269   rp->addr_only = addr_only;
270   rp->is_add = is_add;
271 }
272
273 /**
274  * @brief Add static mapping.
275  *
276  * Create static mapping between local addr+port and external addr+port.
277  *
278  * @param l_addr Local IPv4 address.
279  * @param e_addr External IPv4 address.
280  * @param l_port Local port number.
281  * @param e_port External port number.
282  * @param vrf_id VRF ID.
283  * @param addr_only If 0 address port and pair mapping, otherwise address only.
284  * @param sw_if_index External port instead of specific IP address.
285  * @param is_add If 0 delete static mapping, otherwise add.
286  *
287  * @returns
288  */
289 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
290                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
291                             u32 sw_if_index, int is_add)
292 {
293   snat_main_t * sm = &snat_main;
294   snat_static_mapping_t *m;
295   snat_static_mapping_key_t m_key;
296   clib_bihash_kv_8_8_t kv, value;
297   snat_address_t *a = 0;
298   u32 fib_index = ~0;
299   uword * p;
300   int i;
301
302   /* If outside FIB index is not resolved yet */
303   if (sm->outside_fib_index == ~0)
304     {
305       p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
306       if (!p)
307         return VNET_API_ERROR_NO_SUCH_FIB;
308       sm->outside_fib_index = p[0];
309     }
310
311   /* If the external address is a specific interface address */
312   if (sw_if_index != ~0)
313     {
314       ip4_address_t * first_int_addr;
315
316       /* Might be already set... */
317       first_int_addr = ip4_interface_first_address 
318         (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
319
320       /* DHCP resolution required? */
321       if (first_int_addr == 0)
322         {
323           snat_add_static_mapping_when_resolved 
324             (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, 
325              addr_only,  is_add);
326           return 0;
327         }
328         else
329           e_addr.as_u32 = first_int_addr->as_u32;
330     }
331
332   m_key.addr = e_addr;
333   m_key.port = addr_only ? 0 : e_port;
334   m_key.fib_index = sm->outside_fib_index;
335   kv.key = m_key.as_u64;
336   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
337     m = 0;
338   else
339     m = pool_elt_at_index (sm->static_mappings, value.value);
340
341   if (is_add)
342     {
343       if (m)
344         return VNET_API_ERROR_VALUE_EXIST;
345
346       /* Convert VRF id to FIB index */
347       if (vrf_id != ~0)
348         {
349           p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
350           if (!p)
351             return VNET_API_ERROR_NO_SUCH_FIB;
352           fib_index = p[0];
353         }
354       /* If not specified use inside VRF id from SNAT plugin startup config */
355       else
356         {
357           if (sm->inside_fib_index == ~0)
358             {
359               p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->inside_vrf_id);
360               if (!p)
361                 return VNET_API_ERROR_NO_SUCH_FIB;
362               fib_index = p[0];
363               sm->inside_fib_index = fib_index;
364             }
365           else
366             fib_index = sm->inside_fib_index;
367
368           vrf_id = sm->inside_vrf_id;
369         }
370
371       /* Find external address in allocated addresses and reserve port for
372          address and port pair mapping when dynamic translations enabled */
373       if (!addr_only && !(sm->static_mapping_only))
374         {
375           for (i = 0; i < vec_len (sm->addresses); i++)
376             {
377               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
378                 {
379                   a = sm->addresses + i;
380                   /* External port must be unused */
381                   if (clib_bitmap_get_no_check (a->busy_port_bitmap, e_port))
382                     return VNET_API_ERROR_INVALID_VALUE;
383                   clib_bitmap_set_no_check (a->busy_port_bitmap, e_port, 1);
384                   if (e_port > 1024)
385                     a->busy_ports++;
386
387                   break;
388                 }
389             }
390           /* External address must be allocated */
391           if (!a)
392             return VNET_API_ERROR_NO_SUCH_ENTRY;
393         }
394
395       pool_get (sm->static_mappings, m);
396       memset (m, 0, sizeof (*m));
397       m->local_addr = l_addr;
398       m->external_addr = e_addr;
399       m->addr_only = addr_only;
400       m->vrf_id = vrf_id;
401       m->fib_index = fib_index;
402       if (!addr_only)
403         {
404           m->local_port = l_port;
405           m->external_port = e_port;
406         }
407
408       m_key.addr = m->local_addr;
409       m_key.port = m->local_port;
410       m_key.fib_index = m->fib_index;
411       kv.key = m_key.as_u64;
412       kv.value = m - sm->static_mappings;
413       clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
414
415       m_key.addr = m->external_addr;
416       m_key.port = m->external_port;
417       m_key.fib_index = sm->outside_fib_index;
418       kv.key = m_key.as_u64;
419       kv.value = m - sm->static_mappings;
420       clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
421
422       /* Assign worker */
423       if (sm->workers)
424         {
425           snat_user_key_t w_key0;
426           snat_static_mapping_key_t w_key1;
427
428           w_key0.addr = m->local_addr;
429           w_key0.fib_index = m->fib_index;
430           kv.key = w_key0.as_u64;
431
432           if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
433             {
434               kv.value = sm->first_worker_index +
435                 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
436
437               clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
438             }
439           else
440             {
441               kv.value = value.value;
442             }
443
444           w_key1.addr = m->external_addr;
445           w_key1.port = clib_host_to_net_u16 (m->external_port);
446           w_key1.fib_index = sm->outside_fib_index;
447           kv.key = w_key1.as_u64;
448           clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
449         }
450     }
451   else
452     {
453       if (!m)
454         return VNET_API_ERROR_NO_SUCH_ENTRY;
455
456       /* Free external address port */
457       if (!addr_only && !(sm->static_mapping_only))
458         {
459           for (i = 0; i < vec_len (sm->addresses); i++)
460             {
461               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
462                 {
463                   a = sm->addresses + i;
464                   clib_bitmap_set_no_check (a->busy_port_bitmap, e_port, 0);
465                   a->busy_ports--;
466
467                   break;
468                 }
469             }
470         }
471
472       m_key.addr = m->local_addr;
473       m_key.port = m->local_port;
474       m_key.fib_index = m->fib_index;
475       kv.key = m_key.as_u64;
476       clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
477
478       m_key.addr = m->external_addr;
479       m_key.port = m->external_port;
480       m_key.fib_index = sm->outside_fib_index;
481       kv.key = m_key.as_u64;
482       clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
483
484       /* Delete session(s) for static mapping if exist */
485       if (!(sm->static_mapping_only) ||
486           (sm->static_mapping_only && sm->static_mapping_connection_tracking))
487         {
488           snat_user_key_t u_key;
489           snat_user_t *u;
490           dlist_elt_t * head, * elt;
491           u32 elt_index, head_index, del_elt_index;
492           u32 ses_index;
493           u64 user_index;
494           snat_session_t * s;
495           snat_main_per_thread_data_t *tsm;
496
497           u_key.addr = m->local_addr;
498           u_key.fib_index = m->fib_index;
499           kv.key = u_key.as_u64;
500           if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
501             {
502               user_index = value.value;
503               if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
504                 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
505               else
506                 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
507               u = pool_elt_at_index (tsm->users, user_index);
508               if (u->nstaticsessions)
509                 {
510                   head_index = u->sessions_per_user_list_head_index;
511                   head = pool_elt_at_index (tsm->list_pool, head_index);
512                   elt_index = head->next;
513                   elt = pool_elt_at_index (tsm->list_pool, elt_index);
514                   ses_index = elt->value;
515                   while (ses_index != ~0)
516                     {
517                       s =  pool_elt_at_index (tsm->sessions, ses_index);
518                       del_elt_index = elt_index;
519                       elt_index = elt->next;
520                       elt = pool_elt_at_index (tsm->list_pool, elt_index);
521                       ses_index = elt->value;
522
523                       if (!addr_only)
524                         {
525                           if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
526                               (clib_net_to_host_u16 (s->out2in.port) != e_port))
527                             continue;
528                         }
529
530                       /* log NAT event */
531                       snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
532                                                           s->out2in.addr.as_u32,
533                                                           s->in2out.protocol,
534                                                           s->in2out.port,
535                                                           s->out2in.port,
536                                                           s->in2out.fib_index);
537
538                       value.key = s->in2out.as_u64;
539                       clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
540                       value.key = s->out2in.as_u64;
541                       clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
542                       pool_put (tsm->sessions, s);
543
544                       clib_dlist_remove (tsm->list_pool, del_elt_index);
545                       pool_put_index (tsm->list_pool, del_elt_index);
546                       u->nstaticsessions--;
547
548                       if (!addr_only)
549                         break;
550                     }
551                   if (addr_only)
552                     {
553                       pool_put (tsm->users, u);
554                       clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
555                     }
556                 }
557             }
558         }
559
560       /* Delete static mapping from pool */
561       pool_put (sm->static_mappings, m);
562     }
563
564   return 0;
565 }
566
567 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
568 {
569   snat_address_t *a = 0;
570   snat_session_t *ses;
571   u32 *ses_to_be_removed = 0, *ses_index;
572   clib_bihash_kv_8_8_t kv, value;
573   snat_user_key_t user_key;
574   snat_user_t *u;
575   snat_main_per_thread_data_t *tsm;
576   snat_static_mapping_t *m;
577   int i;
578
579   /* Find SNAT address */
580   for (i=0; i < vec_len (sm->addresses); i++)
581     {
582       if (sm->addresses[i].addr.as_u32 == addr.as_u32)
583         {
584           a = sm->addresses + i;
585           break;
586         }
587     }
588   if (!a)
589     return VNET_API_ERROR_NO_SUCH_ENTRY;
590
591   if (delete_sm)
592     {
593       pool_foreach (m, sm->static_mappings,
594       ({
595           if (m->external_addr.as_u32 == addr.as_u32)
596             (void) snat_add_static_mapping (m->local_addr, m->external_addr,
597                                             m->local_port, m->external_port,
598                                             m->vrf_id, m->addr_only, ~0, 0);
599       }));
600     }
601   else
602     {
603       /* Check if address is used in some static mapping */
604       if (is_snat_address_used_in_static_mapping(sm, addr))
605         {
606           clib_warning ("address used in static mapping");
607           return VNET_API_ERROR_UNSPECIFIED;
608         }
609     }
610
611   /* Delete sessions using address */
612   if (a->busy_ports)
613     {
614       vec_foreach (tsm, sm->per_thread_data)
615         {
616           pool_foreach (ses, tsm->sessions, ({
617             if (ses->out2in.addr.as_u32 == addr.as_u32)
618               {
619                 /* log NAT event */
620                 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
621                                                     ses->out2in.addr.as_u32,
622                                                     ses->in2out.protocol,
623                                                     ses->in2out.port,
624                                                     ses->out2in.port,
625                                                     ses->in2out.fib_index);
626                 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
627                 kv.key = ses->in2out.as_u64;
628                 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
629                 kv.key = ses->out2in.as_u64;
630                 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
631                 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
632                 user_key.addr = ses->in2out.addr;
633                 user_key.fib_index = ses->in2out.fib_index;
634                 kv.key = user_key.as_u64;
635                 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
636                   {
637                     u = pool_elt_at_index (tsm->users, value.value);
638                     u->nsessions--;
639                   }
640               }
641           }));
642
643           vec_foreach (ses_index, ses_to_be_removed)
644             pool_put_index (tsm->sessions, ses_index[0]);
645
646           vec_free (ses_to_be_removed);
647        }
648     }
649
650   vec_del1 (sm->addresses, i);
651
652   return 0;
653 }
654
655 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
656 {
657   snat_main_t *sm = &snat_main;
658   snat_interface_t *i;
659   const char * feature_name;
660
661   if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
662     feature_name = is_inside ?  "snat-in2out-fast" : "snat-out2in-fast";
663   else
664     {
665       if (sm->num_workers > 1)
666         feature_name = is_inside ?  "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
667       else
668         feature_name = is_inside ?  "snat-in2out" : "snat-out2in";
669     }
670
671   vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
672                                !is_del, 0, 0);
673
674   if (sm->fq_in2out_index == ~0)
675     sm->fq_in2out_index = vlib_frame_queue_main_init (snat_in2out_node.index, 0);
676
677   if (sm->fq_out2in_index == ~0)
678     sm->fq_out2in_index = vlib_frame_queue_main_init (snat_out2in_node.index, 0);
679
680   pool_foreach (i, sm->interfaces,
681   ({
682     if (i->sw_if_index == sw_if_index)
683       {
684         if (is_del)
685           pool_put (sm->interfaces, i);
686         else
687           return VNET_API_ERROR_VALUE_EXIST;
688
689         return 0;
690       }
691   }));
692
693   if (is_del)
694     return VNET_API_ERROR_NO_SUCH_ENTRY;
695
696   pool_get (sm->interfaces, i);
697   i->sw_if_index = sw_if_index;
698   i->is_inside = is_inside;
699
700   return 0;
701 }
702
703 static int snat_set_workers (uword * bitmap)
704 {
705   snat_main_t *sm = &snat_main;
706   int i;
707
708   if (sm->num_workers < 2)
709     return VNET_API_ERROR_FEATURE_DISABLED;
710
711   if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
712     return VNET_API_ERROR_INVALID_WORKER;
713
714   vec_free (sm->workers);
715   clib_bitmap_foreach (i, bitmap,
716     ({
717       vec_add1(sm->workers, i);
718     }));
719
720   return 0;
721 }
722
723 static void 
724 vl_api_snat_add_address_range_t_handler
725 (vl_api_snat_add_address_range_t * mp)
726 {
727   snat_main_t * sm = &snat_main;
728   vl_api_snat_add_address_range_reply_t * rmp;
729   ip4_address_t this_addr;
730   u32 start_host_order, end_host_order;
731   int i, count;
732   int rv = 0;
733   u32 * tmp;
734
735   if (mp->is_ip4 != 1)
736     {
737       rv = VNET_API_ERROR_UNIMPLEMENTED;
738       goto send_reply;
739     }
740
741   if (sm->static_mapping_only)
742     {
743       rv = VNET_API_ERROR_FEATURE_DISABLED;
744       goto send_reply;
745     }
746
747   tmp = (u32 *) mp->first_ip_address;
748   start_host_order = clib_host_to_net_u32 (tmp[0]);
749   tmp = (u32 *) mp->last_ip_address;
750   end_host_order = clib_host_to_net_u32 (tmp[0]);
751
752   count = (end_host_order - start_host_order) + 1;
753
754   if (count > 1024)
755     clib_warning ("%U - %U, %d addresses...",
756                   format_ip4_address, mp->first_ip_address,
757                   format_ip4_address, mp->last_ip_address,
758                   count);
759   
760   memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
761
762   for (i = 0; i < count; i++)
763     {
764       if (mp->is_add)
765         snat_add_address (sm, &this_addr);
766       else
767         rv = snat_del_address (sm, this_addr, 0);
768
769       if (rv)
770         goto send_reply;
771
772       increment_v4_address (&this_addr);
773     }
774
775  send_reply:
776   REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
777 }
778
779 static void *vl_api_snat_add_address_range_t_print
780 (vl_api_snat_add_address_range_t *mp, void * handle)
781 {
782   u8 * s;
783
784   s = format (0, "SCRIPT: snat_add_address_range ");
785   s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
786   if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
787     {
788       s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
789     }
790   FINISH;
791 }
792
793 static void
794 send_snat_address_details
795 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
796 {
797   vl_api_snat_address_details_t *rmp;
798   snat_main_t * sm = &snat_main;
799
800   rmp = vl_msg_api_alloc (sizeof (*rmp));
801   memset (rmp, 0, sizeof (*rmp));
802   rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
803   rmp->is_ip4 = 1;
804   clib_memcpy (rmp->ip_address, &(a->addr), 4);
805   rmp->context = context;
806
807   vl_msg_api_send_shmem (q, (u8 *) & rmp);
808 }
809
810 static void
811 vl_api_snat_address_dump_t_handler
812 (vl_api_snat_address_dump_t * mp)
813 {
814   unix_shared_memory_queue_t *q;
815   snat_main_t * sm = &snat_main;
816   snat_address_t * a;
817
818   q = vl_api_client_index_to_input_queue (mp->client_index);
819   if (q == 0)
820     return;
821
822   vec_foreach (a, sm->addresses)
823     send_snat_address_details (a, q, mp->context);
824 }
825
826 static void *vl_api_snat_address_dump_t_print
827 (vl_api_snat_address_dump_t *mp, void * handle)
828 {
829   u8 *s;
830
831   s = format (0, "SCRIPT: snat_address_dump ");
832
833   FINISH;
834 }
835
836 static void
837 vl_api_snat_interface_add_del_feature_t_handler
838 (vl_api_snat_interface_add_del_feature_t * mp)
839 {
840   snat_main_t * sm = &snat_main;
841   vl_api_snat_interface_add_del_feature_reply_t * rmp;
842   u8 is_del = mp->is_add == 0;
843   u32 sw_if_index = ntohl(mp->sw_if_index);
844   int rv = 0;
845
846   VALIDATE_SW_IF_INDEX(mp);
847
848   rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
849   
850   BAD_SW_IF_INDEX_LABEL;
851
852   REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
853 }
854
855 static void *vl_api_snat_interface_add_del_feature_t_print
856 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
857 {
858   u8 * s;
859
860   s = format (0, "SCRIPT: snat_interface_add_del_feature ");
861   s = format (s, "sw_if_index %d %s %s",
862               clib_host_to_net_u32(mp->sw_if_index),
863               mp->is_inside ? "in":"out",
864               mp->is_add ? "" : "del");
865
866   FINISH;
867 }
868
869 static void
870 send_snat_interface_details
871 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
872 {
873   vl_api_snat_interface_details_t *rmp;
874   snat_main_t * sm = &snat_main;
875
876   rmp = vl_msg_api_alloc (sizeof (*rmp));
877   memset (rmp, 0, sizeof (*rmp));
878   rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
879   rmp->sw_if_index = ntohl (i->sw_if_index);
880   rmp->is_inside = i->is_inside;
881   rmp->context = context;
882
883   vl_msg_api_send_shmem (q, (u8 *) & rmp);
884 }
885
886 static void
887 vl_api_snat_interface_dump_t_handler
888 (vl_api_snat_interface_dump_t * mp)
889 {
890   unix_shared_memory_queue_t *q;
891   snat_main_t * sm = &snat_main;
892   snat_interface_t * i;
893
894   q = vl_api_client_index_to_input_queue (mp->client_index);
895   if (q == 0)
896     return;
897
898   pool_foreach (i, sm->interfaces,
899   ({
900     send_snat_interface_details(i, q, mp->context);
901   }));
902 }
903
904 static void *vl_api_snat_interface_dump_t_print
905 (vl_api_snat_interface_dump_t *mp, void * handle)
906 {
907   u8 *s;
908
909   s = format (0, "SCRIPT: snat_interface_dump ");
910
911   FINISH;
912 }static void
913
914 vl_api_snat_add_static_mapping_t_handler
915 (vl_api_snat_add_static_mapping_t * mp)
916 {
917   snat_main_t * sm = &snat_main;
918   vl_api_snat_add_static_mapping_reply_t * rmp;
919   ip4_address_t local_addr, external_addr;
920   u16 local_port = 0, external_port = 0;
921   u32 vrf_id, external_sw_if_index;
922   int rv = 0;
923
924   if (mp->is_ip4 != 1)
925     {
926       rv = VNET_API_ERROR_UNIMPLEMENTED;
927       goto send_reply;
928     }
929
930   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
931   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
932   if (mp->addr_only == 0)
933     {
934       local_port = clib_net_to_host_u16 (mp->local_port);
935       external_port = clib_net_to_host_u16 (mp->external_port);
936     }
937   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
938   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
939
940   rv = snat_add_static_mapping(local_addr, external_addr, local_port,
941                                external_port, vrf_id, mp->addr_only,
942                                external_sw_if_index,
943                                mp->is_add);
944
945  send_reply:
946   REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
947 }
948
949 static void *vl_api_snat_add_static_mapping_t_print
950 (vl_api_snat_add_static_mapping_t *mp, void * handle)
951 {
952   u8 * s;
953
954   s = format (0, "SCRIPT: snat_add_static_mapping ");
955   s = format (s, "local_addr %U external_addr %U ",
956               format_ip4_address, mp->local_ip_address,
957               format_ip4_address, mp->external_ip_address);
958
959   if (mp->addr_only == 0)
960     s = format (s, "local_port %d external_port %d ",
961                 clib_net_to_host_u16 (mp->local_port),
962                 clib_net_to_host_u16 (mp->external_port));
963
964   if (mp->vrf_id != ~0)
965     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
966
967   if (mp->external_sw_if_index != ~0)
968     s = format (s, "external_sw_if_index %d",
969                 clib_net_to_host_u32 (mp->external_sw_if_index));
970   FINISH;
971 }
972
973 static void
974 send_snat_static_mapping_details
975 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
976 {
977   vl_api_snat_static_mapping_details_t *rmp;
978   snat_main_t * sm = &snat_main;
979
980   rmp = vl_msg_api_alloc (sizeof (*rmp));
981   memset (rmp, 0, sizeof (*rmp));
982   rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
983   rmp->is_ip4 = 1;
984   rmp->addr_only = m->addr_only;
985   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
986   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
987   rmp->local_port = htons (m->local_port);
988   rmp->external_port = htons (m->external_port);
989   rmp->vrf_id = htonl (m->vrf_id);
990   rmp->context = context;
991
992   vl_msg_api_send_shmem (q, (u8 *) & rmp);
993 }
994
995 static void
996 vl_api_snat_static_mapping_dump_t_handler
997 (vl_api_snat_static_mapping_dump_t * mp)
998 {
999   unix_shared_memory_queue_t *q;
1000   snat_main_t * sm = &snat_main;
1001   snat_static_mapping_t * m;
1002
1003   q = vl_api_client_index_to_input_queue (mp->client_index);
1004   if (q == 0)
1005     return;
1006
1007   pool_foreach (m, sm->static_mappings,
1008   ({
1009       send_snat_static_mapping_details (m, q, mp->context);
1010   }));
1011 }
1012
1013 static void *vl_api_snat_static_mapping_dump_t_print
1014 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
1015 {
1016   u8 *s;
1017
1018   s = format (0, "SCRIPT: snat_static_mapping_dump ");
1019
1020   FINISH;
1021 }
1022
1023 static void
1024 vl_api_snat_control_ping_t_handler
1025 (vl_api_snat_control_ping_t * mp)
1026 {
1027   vl_api_snat_control_ping_reply_t *rmp;
1028   snat_main_t * sm = &snat_main;
1029   int rv = 0;
1030
1031   REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
1032   ({
1033     rmp->vpe_pid = ntohl (getpid());
1034   }));
1035 }
1036
1037 static void *vl_api_snat_control_ping_t_print
1038 (vl_api_snat_control_ping_t *mp, void * handle)
1039 {
1040   u8 *s;
1041
1042   s = format (0, "SCRIPT: snat_control_ping ");
1043
1044   FINISH;
1045 }
1046
1047 static void
1048 vl_api_snat_show_config_t_handler
1049 (vl_api_snat_show_config_t * mp)
1050 {
1051   vl_api_snat_show_config_reply_t *rmp;
1052   snat_main_t * sm = &snat_main;
1053   int rv = 0;
1054
1055   REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
1056   ({
1057     rmp->translation_buckets = htonl (sm->translation_buckets);
1058     rmp->translation_memory_size = htonl (sm->translation_memory_size);
1059     rmp->user_buckets = htonl (sm->user_buckets);
1060     rmp->user_memory_size = htonl (sm->user_memory_size);
1061     rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
1062     rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
1063     rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
1064     rmp->static_mapping_only = sm->static_mapping_only;
1065     rmp->static_mapping_connection_tracking =
1066       sm->static_mapping_connection_tracking;
1067   }));
1068 }
1069
1070 static void *vl_api_snat_show_config_t_print
1071 (vl_api_snat_show_config_t *mp, void * handle)
1072 {
1073   u8 *s;
1074
1075   s = format (0, "SCRIPT: snat_show_config ");
1076
1077   FINISH;
1078 }
1079
1080 static void 
1081 vl_api_snat_set_workers_t_handler
1082 (vl_api_snat_set_workers_t * mp)
1083 {
1084   snat_main_t * sm = &snat_main;
1085   vl_api_snat_set_workers_reply_t * rmp;
1086   int rv = 0;
1087   uword *bitmap = 0;
1088   u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1089
1090   if (sm->num_workers < 2)
1091     {
1092       rv = VNET_API_ERROR_FEATURE_DISABLED;
1093       goto send_reply;
1094     }
1095
1096   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask)); 
1097   rv = snat_set_workers(bitmap);
1098   clib_bitmap_free (bitmap);
1099
1100  send_reply:
1101   REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
1102 }
1103
1104 static void *vl_api_snat_set_workers_t_print
1105 (vl_api_snat_set_workers_t *mp, void * handle)
1106 {
1107   u8 * s;
1108   uword *bitmap = 0;
1109   u8 first = 1;
1110   int i;
1111   u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1112
1113   s = format (0, "SCRIPT: snat_set_workers ");
1114   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask)); 
1115   clib_bitmap_foreach (i, bitmap,
1116     ({
1117       if (first)
1118         s = format (s, "%d", i);
1119       else
1120         s = format (s, ",%d", i);
1121       first = 0;
1122     }));
1123   clib_bitmap_free (bitmap);
1124   FINISH;
1125 }
1126
1127 static void
1128 send_snat_worker_details
1129 (u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
1130 {
1131   vl_api_snat_worker_details_t *rmp;
1132   snat_main_t * sm = &snat_main;
1133   vlib_worker_thread_t *w =
1134     vlib_worker_threads + worker_index + sm->first_worker_index;
1135
1136   rmp = vl_msg_api_alloc (sizeof (*rmp));
1137   memset (rmp, 0, sizeof (*rmp));
1138   rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS+sm->msg_id_base);
1139   rmp->context = context;
1140   rmp->worker_index = htonl (worker_index);
1141   rmp->lcore_id = htonl (w->lcore_id);
1142   strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
1143
1144   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1145 }
1146
1147 static void
1148 vl_api_snat_worker_dump_t_handler
1149 (vl_api_snat_worker_dump_t * mp)
1150 {
1151   unix_shared_memory_queue_t *q;
1152   snat_main_t * sm = &snat_main;
1153   u32 * worker_index;
1154
1155   q = vl_api_client_index_to_input_queue (mp->client_index);
1156   if (q == 0)
1157     return;
1158
1159   vec_foreach (worker_index, sm->workers)
1160     {
1161       send_snat_worker_details(*worker_index, q, mp->context);
1162     }
1163 }
1164
1165 static void *vl_api_snat_worker_dump_t_print
1166 (vl_api_snat_worker_dump_t *mp, void * handle)
1167 {
1168   u8 *s;
1169
1170   s = format (0, "SCRIPT: snat_worker_dump ");
1171
1172   FINISH;
1173 }
1174
1175 static int snat_add_interface_address(snat_main_t *sm,
1176                                       u32 sw_if_index,
1177                                       int is_del);
1178
1179 static void
1180 vl_api_snat_add_del_interface_addr_t_handler
1181 (vl_api_snat_add_del_interface_addr_t * mp)
1182 {
1183   snat_main_t * sm = &snat_main;
1184   vl_api_snat_add_del_interface_addr_reply_t * rmp;
1185   u8 is_del = mp->is_add == 0;
1186   u32 sw_if_index = ntohl(mp->sw_if_index);
1187   int rv = 0;
1188
1189   VALIDATE_SW_IF_INDEX(mp);
1190
1191   rv = snat_add_interface_address (sm, sw_if_index, is_del);
1192   
1193   BAD_SW_IF_INDEX_LABEL;
1194
1195   REPLY_MACRO(VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
1196 }
1197
1198 static void *vl_api_snat_add_del_interface_addr_t_print
1199 (vl_api_snat_add_del_interface_addr_t * mp, void *handle)
1200 {
1201   u8 * s;
1202
1203   s = format (0, "SCRIPT: snat_add_del_interface_addr ");
1204   s = format (s, "sw_if_index %d %s",
1205               clib_host_to_net_u32(mp->sw_if_index),
1206               mp->is_add ? "" : "del");
1207
1208   FINISH;
1209 }
1210
1211 static void
1212 send_snat_interface_addr_details
1213 (u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
1214 {
1215   vl_api_snat_interface_addr_details_t *rmp;
1216   snat_main_t * sm = &snat_main;
1217
1218   rmp = vl_msg_api_alloc (sizeof (*rmp));
1219   memset (rmp, 0, sizeof (*rmp));
1220   rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS+sm->msg_id_base);
1221   rmp->sw_if_index = ntohl (sw_if_index);
1222   rmp->context = context;
1223
1224   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1225 }
1226
1227 static void
1228 vl_api_snat_interface_addr_dump_t_handler
1229 (vl_api_snat_interface_addr_dump_t * mp)
1230 {
1231   unix_shared_memory_queue_t *q;
1232   snat_main_t * sm = &snat_main;
1233   u32 * i;
1234
1235   q = vl_api_client_index_to_input_queue (mp->client_index);
1236   if (q == 0)
1237     return;
1238
1239   vec_foreach (i, sm->auto_add_sw_if_indices)
1240     send_snat_interface_addr_details(*i, q, mp->context);
1241 }
1242
1243 static void *vl_api_snat_interface_addr_dump_t_print
1244 (vl_api_snat_interface_addr_dump_t *mp, void * handle)
1245 {
1246   u8 *s;
1247
1248   s = format (0, "SCRIPT: snat_interface_addr_dump ");
1249
1250   FINISH;
1251 }
1252
1253 static void
1254 vl_api_snat_ipfix_enable_disable_t_handler
1255 (vl_api_snat_ipfix_enable_disable_t * mp)
1256 {
1257   snat_main_t * sm = &snat_main;
1258   vl_api_snat_ipfix_enable_disable_reply_t * rmp;
1259   int rv = 0;
1260
1261   rv = snat_ipfix_logging_enable_disable(mp->enable,
1262                                          clib_host_to_net_u32 (mp->domain_id),
1263                                          clib_host_to_net_u16 (mp->src_port));
1264
1265   REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
1266 }
1267
1268 static void *vl_api_snat_ipfix_enable_disable_t_print
1269 (vl_api_snat_ipfix_enable_disable_t *mp, void * handle)
1270 {
1271   u8 * s;
1272
1273   s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
1274   if (mp->domain_id)
1275     s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
1276   if (mp->src_port)
1277     s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
1278   if (!mp->enable)
1279     s = format (s, "disable ");
1280
1281   FINISH;
1282 }
1283
1284 /* List of message types that this plugin understands */
1285 #define foreach_snat_plugin_api_msg                                     \
1286 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range)                       \
1287 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature)       \
1288 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping)                     \
1289 _(SNAT_CONTROL_PING, snat_control_ping)                                 \
1290 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump)                   \
1291 _(SNAT_SHOW_CONFIG, snat_show_config)                                   \
1292 _(SNAT_ADDRESS_DUMP, snat_address_dump)                                 \
1293 _(SNAT_INTERFACE_DUMP, snat_interface_dump)                             \
1294 _(SNAT_SET_WORKERS, snat_set_workers)                                   \
1295 _(SNAT_WORKER_DUMP, snat_worker_dump)                                   \
1296 _(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr)             \
1297 _(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump)                   \
1298 _(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable)
1299
1300 /* Set up the API message handling tables */
1301 static clib_error_t *
1302 snat_plugin_api_hookup (vlib_main_t *vm)
1303 {
1304    snat_main_t * sm __attribute__ ((unused)) = &snat_main;
1305 #define _(N,n)                                                  \
1306     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
1307                            #n,                                  \
1308                            vl_api_##n##_t_handler,              \
1309                            vl_noop_handler,                     \
1310                            vl_api_##n##_t_endian,               \
1311                            vl_api_##n##_t_print,                \
1312                            sizeof(vl_api_##n##_t), 1); 
1313     foreach_snat_plugin_api_msg;
1314 #undef _
1315
1316     return 0;
1317 }
1318
1319 #define vl_msg_name_crc_list
1320 #include <snat/snat_all_api_h.h>
1321 #undef vl_msg_name_crc_list
1322
1323 static void
1324 setup_message_id_table (snat_main_t * sm, api_main_t * am)
1325 {
1326 #define _(id,n,crc) \
1327   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1328   foreach_vl_msg_name_crc_snat;
1329 #undef _
1330 }
1331
1332 static void plugin_custom_dump_configure (snat_main_t * sm) 
1333 {
1334 #define _(n,f) sm->api_main->msg_print_handlers \
1335   [VL_API_##n + sm->msg_id_base]                \
1336     = (void *) vl_api_##f##_t_print;
1337   foreach_snat_plugin_api_msg;
1338 #undef _
1339 }
1340
1341
1342 static void
1343 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1344                                        uword opaque,
1345                                        u32 sw_if_index,
1346                                        ip4_address_t * address,
1347                                        u32 address_length,
1348                                        u32 if_address_index,
1349                                        u32 is_delete);
1350
1351 static clib_error_t * snat_init (vlib_main_t * vm)
1352 {
1353   snat_main_t * sm = &snat_main;
1354   clib_error_t * error = 0;
1355   ip4_main_t * im = &ip4_main;
1356   ip_lookup_main_t * lm = &im->lookup_main;
1357   u8 * name;
1358   uword *p;
1359   vlib_thread_registration_t *tr;
1360   vlib_thread_main_t *tm = vlib_get_thread_main ();
1361   uword *bitmap = 0;
1362   u32 i;
1363   ip4_add_del_interface_address_callback_t cb4;
1364
1365   name = format (0, "snat_%08x%c", api_version, 0);
1366
1367   /* Ask for a correctly-sized block of API message decode slots */
1368   sm->msg_id_base = vl_msg_api_get_msg_ids 
1369       ((char *) name, VL_MSG_FIRST_AVAILABLE);
1370
1371   sm->vlib_main = vm;
1372   sm->vnet_main = vnet_get_main();
1373   sm->ip4_main = im;
1374   sm->ip4_lookup_main = lm;
1375   sm->api_main = &api_main;
1376   sm->first_worker_index = 0;
1377   sm->next_worker = 0;
1378   sm->num_workers = 0;
1379   sm->workers = 0;
1380   sm->fq_in2out_index = ~0;
1381   sm->fq_out2in_index = ~0;
1382
1383   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1384   if (p)
1385     {
1386       tr = (vlib_thread_registration_t *) p[0];
1387       if (tr)
1388         {
1389           sm->num_workers = tr->count;
1390           sm->first_worker_index = tr->first_index;
1391         }
1392     }
1393
1394   /* Use all available workers by default */
1395   if (sm->num_workers > 1)
1396     {
1397       for (i=0; i < sm->num_workers; i++)
1398         bitmap = clib_bitmap_set (bitmap, i, 1);
1399       snat_set_workers(bitmap);
1400       clib_bitmap_free (bitmap);
1401     }
1402
1403   error = snat_plugin_api_hookup (vm);
1404
1405   /* Add our API messages to the global name_crc hash table */
1406   setup_message_id_table (sm, &api_main);
1407
1408   plugin_custom_dump_configure (sm);
1409   vec_free(name);
1410
1411   /* Set up the interface address add/del callback */
1412   cb4.function = snat_ip4_add_del_interface_address_cb;
1413   cb4.function_opaque = 0;
1414
1415   vec_add1 (im->add_del_interface_address_callbacks, cb4);
1416
1417   /* Init IPFIX logging */
1418   snat_ipfix_logging_init(vm);
1419
1420   return error;
1421 }
1422
1423 VLIB_INIT_FUNCTION (snat_init);
1424
1425 void snat_free_outside_address_and_port (snat_main_t * sm, 
1426                                          snat_session_key_t * k, 
1427                                          u32 address_index)
1428 {
1429   snat_address_t *a;
1430   u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1431   
1432   ASSERT (address_index < vec_len (sm->addresses));
1433
1434   a = sm->addresses + address_index;
1435
1436   ASSERT (clib_bitmap_get_no_check (a->busy_port_bitmap,
1437     port_host_byte_order) == 1);
1438
1439   clib_bitmap_set_no_check (a->busy_port_bitmap, port_host_byte_order, 0);
1440   a->busy_ports--;
1441 }  
1442
1443 /**
1444  * @brief Match SNAT static mapping.
1445  *
1446  * @param sm          SNAT main.
1447  * @param match       Address and port to match.
1448  * @param mapping     External or local address and port of the matched mapping.
1449  * @param by_external If 0 match by local address otherwise match by external
1450  *                    address.
1451  *
1452  * @returns 0 if match found otherwise 1.
1453  */
1454 int snat_static_mapping_match (snat_main_t * sm,
1455                                snat_session_key_t match,
1456                                snat_session_key_t * mapping,
1457                                u8 by_external)
1458 {
1459   clib_bihash_kv_8_8_t kv, value;
1460   snat_static_mapping_t *m;
1461   snat_static_mapping_key_t m_key;
1462   clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1463
1464   if (by_external)
1465     mapping_hash = &sm->static_mapping_by_external;
1466
1467   m_key.addr = match.addr;
1468   m_key.port = clib_net_to_host_u16 (match.port);
1469   m_key.fib_index = match.fib_index;
1470
1471   kv.key = m_key.as_u64;
1472
1473   if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1474     {
1475       /* Try address only mapping */
1476       m_key.port = 0;
1477       kv.key = m_key.as_u64;
1478       if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1479         return 1;
1480     }
1481
1482   m = pool_elt_at_index (sm->static_mappings, value.value);
1483
1484   if (by_external)
1485     {
1486       mapping->addr = m->local_addr;
1487       /* Address only mapping doesn't change port */
1488       mapping->port = m->addr_only ? match.port
1489         : clib_host_to_net_u16 (m->local_port);
1490       mapping->fib_index = m->fib_index;
1491     }
1492   else
1493     {
1494       mapping->addr = m->external_addr;
1495       /* Address only mapping doesn't change port */
1496       mapping->port = m->addr_only ? match.port
1497         : clib_host_to_net_u16 (m->external_port);
1498       mapping->fib_index = sm->outside_fib_index;
1499     }
1500
1501   return 0;
1502 }
1503
1504 int snat_alloc_outside_address_and_port (snat_main_t * sm, 
1505                                          snat_session_key_t * k,
1506                                          u32 * address_indexp)
1507 {
1508   int i;
1509   snat_address_t *a;
1510   u32 portnum;
1511
1512   for (i = 0; i < vec_len (sm->addresses); i++)
1513     {
1514       if (sm->addresses[i].busy_ports < (65535-1024))
1515         {
1516           a = sm->addresses + i;
1517
1518           while (1)
1519             {
1520               portnum = random_u32 (&sm->random_seed);
1521               portnum &= 0xFFFF;
1522               if (portnum < 1024)
1523                 continue;
1524               if (clib_bitmap_get_no_check (a->busy_port_bitmap, portnum))
1525                 continue;
1526               clib_bitmap_set_no_check (a->busy_port_bitmap, portnum, 1);
1527               a->busy_ports++;
1528               /* Caller sets protocol and fib index */
1529               k->addr = a->addr;
1530               k->port = clib_host_to_net_u16(portnum);
1531               *address_indexp = i;
1532               return 0;
1533             }
1534         }
1535     }
1536   /* Totally out of translations to use... */
1537   snat_ipfix_logging_addresses_exhausted(0);
1538   return 1;
1539 }
1540
1541
1542 static clib_error_t *
1543 add_address_command_fn (vlib_main_t * vm,
1544                         unformat_input_t * input,
1545                         vlib_cli_command_t * cmd)
1546 {
1547   unformat_input_t _line_input, *line_input = &_line_input;
1548   snat_main_t * sm = &snat_main;
1549   ip4_address_t start_addr, end_addr, this_addr;
1550   u32 start_host_order, end_host_order;
1551   int i, count;
1552   int is_add = 1;
1553   int rv = 0;
1554
1555   /* Get a line of input. */
1556   if (!unformat_user (input, unformat_line_input, line_input))
1557     return 0;
1558
1559   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1560     {
1561       if (unformat (line_input, "%U - %U",
1562                     unformat_ip4_address, &start_addr,
1563                     unformat_ip4_address, &end_addr))
1564         ;
1565       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1566         end_addr = start_addr;
1567       else if (unformat (line_input, "del"))
1568         is_add = 0;
1569       else
1570         return clib_error_return (0, "unknown input '%U'",
1571           format_unformat_error, input);
1572      }
1573   unformat_free (line_input);
1574
1575   if (sm->static_mapping_only)
1576     return clib_error_return (0, "static mapping only mode");
1577
1578   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1579   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1580   
1581   if (end_host_order < start_host_order)
1582     return clib_error_return (0, "end address less than start address");
1583
1584   count = (end_host_order - start_host_order) + 1;
1585
1586   if (count > 1024)
1587     clib_warning ("%U - %U, %d addresses...",
1588                   format_ip4_address, &start_addr,
1589                   format_ip4_address, &end_addr,
1590                   count);
1591   
1592   this_addr = start_addr;
1593
1594   for (i = 0; i < count; i++)
1595     {
1596       if (is_add)
1597         snat_add_address (sm, &this_addr);
1598       else
1599         rv = snat_del_address (sm, this_addr, 0);
1600
1601       switch (rv)
1602         {
1603         case VNET_API_ERROR_NO_SUCH_ENTRY:
1604           return clib_error_return (0, "S-NAT address not exist.");
1605           break;
1606         case VNET_API_ERROR_UNSPECIFIED:
1607           return clib_error_return (0, "S-NAT address used in static mapping.");
1608           break;
1609         default:
1610           break;
1611         }
1612
1613       increment_v4_address (&this_addr);
1614     }
1615
1616   return 0;
1617 }
1618
1619 VLIB_CLI_COMMAND (add_address_command, static) = {
1620   .path = "snat add address",
1621   .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] [del]",
1622   .function = add_address_command_fn,
1623 };
1624
1625 static clib_error_t *
1626 snat_feature_command_fn (vlib_main_t * vm,
1627                           unformat_input_t * input,
1628                           vlib_cli_command_t * cmd)
1629 {
1630   unformat_input_t _line_input, *line_input = &_line_input;
1631   vnet_main_t * vnm = vnet_get_main();
1632   clib_error_t * error = 0;
1633   u32 sw_if_index;
1634   u32 * inside_sw_if_indices = 0;
1635   u32 * outside_sw_if_indices = 0;
1636   int is_del = 0;
1637   int i;
1638
1639   sw_if_index = ~0;
1640
1641   /* Get a line of input. */
1642   if (!unformat_user (input, unformat_line_input, line_input))
1643     return 0;
1644
1645   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1646     {
1647       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1648                     vnm, &sw_if_index))
1649         vec_add1 (inside_sw_if_indices, sw_if_index);
1650       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1651                          vnm, &sw_if_index))
1652         vec_add1 (outside_sw_if_indices, sw_if_index);
1653       else if (unformat (line_input, "del"))
1654         is_del = 1;
1655       else
1656         return clib_error_return (0, "unknown input '%U'",
1657           format_unformat_error, input);
1658     }
1659   unformat_free (line_input);
1660
1661   if (vec_len (inside_sw_if_indices))
1662     {
1663       for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1664         {
1665           sw_if_index = inside_sw_if_indices[i];
1666           snat_interface_add_del (sw_if_index, 1, is_del);
1667         }
1668     }
1669
1670   if (vec_len (outside_sw_if_indices))
1671     {
1672       for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1673         {
1674           sw_if_index = outside_sw_if_indices[i];
1675           snat_interface_add_del (sw_if_index, 0, is_del);
1676         }
1677     }
1678
1679   vec_free (inside_sw_if_indices);
1680   vec_free (outside_sw_if_indices);
1681
1682   return error;
1683 }
1684
1685 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1686   .path = "set interface snat",
1687   .function = snat_feature_command_fn,
1688   .short_help = "set interface snat in <intfc> out <intfc> [del]",
1689 };
1690
1691 static clib_error_t *
1692 add_static_mapping_command_fn (vlib_main_t * vm,
1693                                unformat_input_t * input,
1694                                vlib_cli_command_t * cmd)
1695 {
1696   unformat_input_t _line_input, *line_input = &_line_input;
1697   clib_error_t * error = 0;
1698   ip4_address_t l_addr, e_addr;
1699   u32 l_port = 0, e_port = 0, vrf_id = ~0;
1700   int is_add = 1;
1701   int addr_only = 1;
1702   u32 sw_if_index = ~0;
1703   vnet_main_t * vnm = vnet_get_main();
1704   int rv;
1705
1706   /* Get a line of input. */
1707   if (!unformat_user (input, unformat_line_input, line_input))
1708     return 0;
1709
1710   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1711     {
1712       if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1713                     &l_port))
1714         addr_only = 0;
1715       else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1716         ;
1717       else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1718                          &e_addr, &e_port))
1719         addr_only = 0;
1720       else if (unformat (line_input, "external %U", unformat_ip4_address,
1721                          &e_addr))
1722         ;
1723       else if (unformat (line_input, "external %U %u",
1724                          unformat_vnet_sw_interface, vnm, &sw_if_index,
1725                          &e_port))
1726         addr_only = 0;
1727
1728       else if (unformat (line_input, "external %U",
1729                          unformat_vnet_sw_interface, vnm, &sw_if_index))
1730         ;
1731       else if (unformat (line_input, "vrf %u", &vrf_id))
1732         ;
1733       else if (unformat (line_input, "del"))
1734         is_add = 0;
1735       else
1736         return clib_error_return (0, "unknown input: '%U'",
1737           format_unformat_error, line_input);
1738     }
1739   unformat_free (line_input);
1740
1741   rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1742                                vrf_id, addr_only, sw_if_index, is_add);
1743
1744   switch (rv)
1745     {
1746     case VNET_API_ERROR_INVALID_VALUE:
1747       return clib_error_return (0, "External port already in use.");
1748       break;
1749     case VNET_API_ERROR_NO_SUCH_ENTRY:
1750       if (is_add)
1751         return clib_error_return (0, "External addres must be allocated.");
1752       else
1753         return clib_error_return (0, "Mapping not exist.");
1754       break;
1755     case VNET_API_ERROR_NO_SUCH_FIB:
1756       return clib_error_return (0, "No such VRF id.");
1757     case VNET_API_ERROR_VALUE_EXIST:
1758       return clib_error_return (0, "Mapping already exist.");
1759     default:
1760       break;
1761     }
1762
1763   return error;
1764 }
1765
1766 /*?
1767  * @cliexpar
1768  * @cliexstart{snat add static mapping}
1769  * Static mapping allows hosts on the external network to initiate connection
1770  * to to the local network host.
1771  * To create static mapping between local host address 10.0.0.3 port 6303 and
1772  * external address 4.4.4.4 port 3606 use:
1773  *  vpp# snat add static mapping local 10.0.0.3 6303 external 4.4.4.4 3606
1774  * If not runnig "static mapping only" S-NAT plugin mode use before:
1775  *  vpp# snat add address 4.4.4.4
1776  * To create static mapping between local and external address use:
1777  *  vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1778  * @cliexend
1779 ?*/
1780 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1781   .path = "snat add static mapping",
1782   .function = add_static_mapping_command_fn,
1783   .short_help =
1784     "snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1785 };
1786
1787 static clib_error_t *
1788 set_workers_command_fn (vlib_main_t * vm,
1789                         unformat_input_t * input,
1790                         vlib_cli_command_t * cmd)
1791 {
1792   unformat_input_t _line_input, *line_input = &_line_input;
1793   uword *bitmap = 0;
1794   int rv = 0;
1795
1796   /* Get a line of input. */
1797   if (!unformat_user (input, unformat_line_input, line_input))
1798     return 0;
1799
1800   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1801     {
1802       if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1803         ;
1804       else
1805         return clib_error_return (0, "unknown input '%U'",
1806           format_unformat_error, input);
1807      }
1808   unformat_free (line_input);
1809
1810   if (bitmap == 0)
1811     return clib_error_return (0, "List of workers must be specified.");
1812
1813   rv = snat_set_workers(bitmap);
1814
1815   clib_bitmap_free (bitmap);
1816
1817   switch (rv)
1818     {
1819     case VNET_API_ERROR_INVALID_WORKER:
1820       return clib_error_return (0, "Invalid worker(s).");
1821       break;
1822     case VNET_API_ERROR_FEATURE_DISABLED:
1823       return clib_error_return (0,
1824         "Supported only if 2 or more workes available.");
1825       break;
1826     default:
1827       break;
1828     }
1829
1830   return 0;
1831 }
1832
1833 /*?
1834  * @cliexpar
1835  * @cliexstart{set snat workers}
1836  * Set SNAT workers if 2 or more workers available, use:
1837  *  vpp# set snat workers 0-2,5
1838  * @cliexend
1839 ?*/
1840 VLIB_CLI_COMMAND (set_workers_command, static) = {
1841   .path = "set snat workers",
1842   .function = set_workers_command_fn,
1843   .short_help =
1844     "set snat workers <workers-list>",
1845 };
1846
1847 static clib_error_t *
1848 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1849                                               unformat_input_t * input,
1850                                               vlib_cli_command_t * cmd)
1851 {
1852   unformat_input_t _line_input, *line_input = &_line_input;
1853   u32 domain_id = 0;
1854   u32 src_port = 0;
1855   u8 enable = 1;
1856   int rv = 0;
1857
1858   /* Get a line of input. */
1859   if (!unformat_user (input, unformat_line_input, line_input))
1860     return 0;
1861
1862   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1863     {
1864       if (unformat (line_input, "domain %d", &domain_id))
1865         ;
1866       else if (unformat (line_input, "src-port %d", &src_port))
1867         ;
1868       else if (unformat (line_input, "disable"))
1869         enable = 0;
1870       else
1871         return clib_error_return (0, "unknown input '%U'",
1872           format_unformat_error, input);
1873      }
1874   unformat_free (line_input);
1875
1876   rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1877
1878   if (rv)
1879     return clib_error_return (0, "ipfix logging enable failed");
1880
1881   return 0;
1882 }
1883
1884 /*?
1885  * @cliexpar
1886  * @cliexstart{snat ipfix logging}
1887  * To enable SNAT IPFIX logging use:
1888  *  vpp# snat ipfix logging
1889  * To set IPFIX exporter use:
1890  *  vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1891  * @cliexend
1892 ?*/
1893 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1894   .path = "snat ipfix logging",
1895   .function = snat_ipfix_logging_enable_disable_command_fn,
1896   .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1897 };
1898
1899 static clib_error_t *
1900 snat_config (vlib_main_t * vm, unformat_input_t * input)
1901 {
1902   snat_main_t * sm = &snat_main;
1903   u32 translation_buckets = 1024;
1904   u32 translation_memory_size = 128<<20;
1905   u32 user_buckets = 128;
1906   u32 user_memory_size = 64<<20;
1907   u32 max_translations_per_user = 100;
1908   u32 outside_vrf_id = 0;
1909   u32 inside_vrf_id = 0;
1910   u32 static_mapping_buckets = 1024;
1911   u32 static_mapping_memory_size = 64<<20;
1912   u8 static_mapping_only = 0;
1913   u8 static_mapping_connection_tracking = 0;
1914   vlib_thread_main_t *tm = vlib_get_thread_main ();
1915
1916   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1917     {
1918       if (unformat (input, "translation hash buckets %d", &translation_buckets))
1919         ;
1920       else if (unformat (input, "translation hash memory %d",
1921                          &translation_memory_size));
1922       else if (unformat (input, "user hash buckets %d", &user_buckets))
1923         ;
1924       else if (unformat (input, "user hash memory %d",
1925                          &user_memory_size))
1926         ;
1927       else if (unformat (input, "max translations per user %d",
1928                          &max_translations_per_user))
1929         ;
1930       else if (unformat (input, "outside VRF id %d",
1931                          &outside_vrf_id))
1932         ;
1933       else if (unformat (input, "inside VRF id %d",
1934                          &inside_vrf_id))
1935         ;
1936       else if (unformat (input, "static mapping only"))
1937         {
1938           static_mapping_only = 1;
1939           if (unformat (input, "connection tracking"))
1940             static_mapping_connection_tracking = 1;
1941         }
1942       else 
1943         return clib_error_return (0, "unknown input '%U'",
1944                                   format_unformat_error, input);
1945     }
1946
1947   /* for show commands, etc. */
1948   sm->translation_buckets = translation_buckets;
1949   sm->translation_memory_size = translation_memory_size;
1950   sm->user_buckets = user_buckets;
1951   sm->user_memory_size = user_memory_size;
1952   sm->max_translations_per_user = max_translations_per_user;
1953   sm->outside_vrf_id = outside_vrf_id;
1954   sm->outside_fib_index = ~0;
1955   sm->inside_vrf_id = inside_vrf_id;
1956   sm->inside_fib_index = ~0;
1957   sm->static_mapping_only = static_mapping_only;
1958   sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1959
1960   if (!static_mapping_only ||
1961       (static_mapping_only && static_mapping_connection_tracking))
1962     {
1963       clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1964                             user_memory_size);
1965
1966       clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1967                             user_memory_size);
1968
1969       vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1970
1971       clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1972                             translation_memory_size);
1973
1974       clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1975                             translation_memory_size);
1976
1977       clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1978                             user_memory_size);
1979     }
1980   clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1981                         "static_mapping_by_local", static_mapping_buckets,
1982                         static_mapping_memory_size);
1983
1984   clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1985                         "static_mapping_by_external", static_mapping_buckets,
1986                         static_mapping_memory_size);
1987   return 0;
1988 }
1989
1990 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1991
1992 u8 * format_snat_key (u8 * s, va_list * args)
1993 {
1994   snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1995   char * protocol_string = "unknown";
1996   static char *protocol_strings[] = {
1997       "UDP",
1998       "TCP",
1999       "ICMP",
2000   };
2001
2002   if (key->protocol < ARRAY_LEN(protocol_strings))
2003       protocol_string = protocol_strings[key->protocol];
2004
2005   s = format (s, "%U proto %s port %d fib %d",
2006               format_ip4_address, &key->addr, protocol_string,
2007               clib_net_to_host_u16 (key->port), key->fib_index);
2008   return s;
2009 }
2010
2011 u8 * format_snat_session (u8 * s, va_list * args)
2012 {
2013   snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2014   snat_session_t * sess = va_arg (*args, snat_session_t *);
2015
2016   s = format (s, "  i2o %U\n", format_snat_key, &sess->in2out);
2017   s = format (s, "    o2i %U\n", format_snat_key, &sess->out2in);
2018   s = format (s, "       last heard %.2f\n", sess->last_heard);
2019   s = format (s, "       total pkts %d, total bytes %lld\n",
2020               sess->total_pkts, sess->total_bytes);
2021   if (snat_is_session_static (sess))
2022     s = format (s, "       static translation\n");
2023   else
2024     s = format (s, "       dynamic translation\n");
2025
2026   return s;
2027 }
2028
2029 u8 * format_snat_user (u8 * s, va_list * args)
2030 {
2031   snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2032   snat_user_t * u = va_arg (*args, snat_user_t *);
2033   int verbose = va_arg (*args, int);
2034   dlist_elt_t * head, * elt;
2035   u32 elt_index, head_index;
2036   u32 session_index;
2037   snat_session_t * sess;
2038
2039   s = format (s, "%U: %d dynamic translations, %d static translations\n",
2040               format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2041
2042   if (verbose == 0)
2043     return s;
2044
2045   if (u->nsessions || u->nstaticsessions)
2046     {
2047       head_index = u->sessions_per_user_list_head_index;
2048       head = pool_elt_at_index (sm->list_pool, head_index);
2049
2050       elt_index = head->next;
2051       elt = pool_elt_at_index (sm->list_pool, elt_index);
2052       session_index = elt->value;
2053
2054       while (session_index != ~0)
2055         {
2056           sess = pool_elt_at_index (sm->sessions, session_index);
2057
2058           s = format (s, "  %U\n", format_snat_session, sm, sess);
2059
2060           elt_index = elt->next;
2061           elt = pool_elt_at_index (sm->list_pool, elt_index);
2062           session_index = elt->value;
2063         }
2064     }
2065
2066   return s;
2067 }
2068
2069 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2070 {
2071   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2072
2073   if (m->addr_only)
2074       s = format (s, "local %U external %U vrf %d",
2075                   format_ip4_address, &m->local_addr,
2076                   format_ip4_address, &m->external_addr,
2077                   m->vrf_id);
2078   else
2079       s = format (s, "local %U:%d external %U:%d vrf %d",
2080                   format_ip4_address, &m->local_addr, m->local_port,
2081                   format_ip4_address, &m->external_addr, m->external_port,
2082                   m->vrf_id);
2083
2084   return s;
2085 }
2086
2087 static clib_error_t *
2088 show_snat_command_fn (vlib_main_t * vm,
2089                  unformat_input_t * input,
2090                  vlib_cli_command_t * cmd)
2091 {
2092   int verbose = 0;
2093   snat_main_t * sm = &snat_main;
2094   snat_user_t * u;
2095   snat_static_mapping_t *m;
2096   snat_interface_t *i;
2097   snat_address_t * ap;
2098   vnet_main_t *vnm = vnet_get_main();
2099   snat_main_per_thread_data_t *tsm;
2100   u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2101   uword j = 0;
2102
2103   if (unformat (input, "detail"))
2104     verbose = 1;
2105   else if (unformat (input, "verbose"))
2106     verbose = 2;
2107
2108   if (sm->static_mapping_only)
2109     {
2110       if (sm->static_mapping_connection_tracking)
2111         vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2112                          "tracking");
2113       else
2114         vlib_cli_output (vm, "SNAT mode: static mapping only");
2115     }
2116   else
2117     {
2118       vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2119     }
2120
2121   if (verbose > 0)
2122     {
2123       pool_foreach (i, sm->interfaces,
2124       ({
2125         vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2126                          vnet_get_sw_interface (vnm, i->sw_if_index),
2127                          i->is_inside ? "in" : "out");
2128       }));
2129
2130       if (vec_len (sm->auto_add_sw_if_indices))
2131         {
2132           vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2133           vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2134             {
2135               vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2136                                vnet_get_sw_interface (vnm, *sw_if_index));
2137             }
2138         }
2139
2140       vec_foreach (ap, sm->addresses)
2141         {
2142           u8 * s = format (0, "");
2143           vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2144           clib_bitmap_foreach (j, ap->busy_port_bitmap,
2145             ({
2146               s = format (s, " %d", j);
2147             }));
2148           vlib_cli_output (vm, "  %d busy ports:%s", ap->busy_ports, s);
2149         }
2150     }
2151
2152   if (sm->num_workers > 1)
2153     {
2154       vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2155       if (verbose > 0)
2156         {
2157           vec_foreach (worker, sm->workers)
2158             {
2159               vlib_worker_thread_t *w =
2160                 vlib_worker_threads + *worker + sm->first_worker_index;
2161               vlib_cli_output (vm, "  %s", w->name);
2162             }
2163         }
2164     }
2165
2166   if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2167     {
2168       vlib_cli_output (vm, "%d static mappings",
2169                        pool_elts (sm->static_mappings));
2170
2171       if (verbose > 0)
2172         {
2173           pool_foreach (m, sm->static_mappings,
2174           ({
2175             vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2176           }));
2177         }
2178     }
2179   else
2180     {
2181       vec_foreach (tsm, sm->per_thread_data)
2182         {
2183           users_num += pool_elts (tsm->users);
2184           sessions_num += pool_elts (tsm->sessions);
2185         }
2186
2187       vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2188                        " %d static mappings",
2189                        users_num,
2190                        vec_len (sm->addresses),
2191                        sessions_num,
2192                        pool_elts (sm->static_mappings));
2193
2194       if (verbose > 0)
2195         {
2196           vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2197                            verbose - 1);
2198           vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2199                            verbose - 1);
2200           vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2201                            verbose - 1);
2202           vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2203                            verbose - 1);
2204           vec_foreach_index (j, sm->per_thread_data)
2205             {
2206               tsm = vec_elt_at_index (sm->per_thread_data, j);
2207
2208               if (pool_elts (tsm->users) == 0)
2209                 continue;
2210
2211               vlib_worker_thread_t *w = vlib_worker_threads + j;
2212               vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2213                                w->lcore_id);
2214               vlib_cli_output (vm, "  %d list pool elements",
2215                                pool_elts (tsm->list_pool));
2216
2217               pool_foreach (u, tsm->users,
2218               ({
2219                 vlib_cli_output (vm, "  %U", format_snat_user, tsm, u,
2220                                  verbose - 1);
2221               }));
2222             }
2223
2224           if (pool_elts (sm->static_mappings))
2225             {
2226               vlib_cli_output (vm, "static mappings:");
2227               pool_foreach (m, sm->static_mappings,
2228               ({
2229                 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2230               }));
2231             }
2232         }
2233     }
2234
2235   return 0;
2236 }
2237
2238 VLIB_CLI_COMMAND (show_snat_command, static) = {
2239     .path = "show snat",
2240     .short_help = "show snat",
2241     .function = show_snat_command_fn,
2242 };
2243
2244
2245 static void
2246 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2247                                        uword opaque,
2248                                        u32 sw_if_index,
2249                                        ip4_address_t * address,
2250                                        u32 address_length,
2251                                        u32 if_address_index,
2252                                        u32 is_delete)
2253 {
2254   snat_main_t *sm = &snat_main;
2255   snat_static_map_resolve_t *rp;
2256   u32 *indices_to_delete = 0;
2257   int i, j;
2258   int rv;
2259
2260   for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2261     {
2262       if (sw_if_index == sm->auto_add_sw_if_indices[i])
2263         {
2264           if (!is_delete)
2265             {
2266               /* Don't trip over lease renewal, static config */
2267               for (j = 0; j < vec_len(sm->addresses); j++)
2268                 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2269                   return;
2270
2271               snat_add_address (sm, address);
2272               /* Scan static map resolution vector */
2273               for (j = 0; j < vec_len (sm->to_resolve); j++)
2274                 {
2275                   rp = sm->to_resolve + j;
2276                   /* On this interface? */
2277                   if (rp->sw_if_index == sw_if_index)
2278                     {
2279                       /* Add the static mapping */
2280                       rv = snat_add_static_mapping (rp->l_addr,
2281                                                     address[0],
2282                                                     rp->l_port,
2283                                                     rp->e_port,
2284                                                     rp->vrf_id,
2285                                                     rp->addr_only,
2286                                                     ~0 /* sw_if_index */,
2287                                                     rp->is_add);
2288                       if (rv)
2289                         clib_warning ("snat_add_static_mapping returned %d", 
2290                                       rv);
2291                       vec_add1 (indices_to_delete, j);
2292                     }
2293                 }
2294               /* If we resolved any of the outstanding static mappings */
2295               if (vec_len(indices_to_delete))
2296                 {
2297                   /* Delete them */
2298                   for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2299                     vec_delete(sm->to_resolve, 1, j);
2300                   vec_free(indices_to_delete);
2301                 }
2302               return;
2303             }
2304           else
2305             {
2306               (void) snat_del_address(sm, address[0], 1);
2307               return;
2308             }
2309         }
2310     }
2311 }
2312
2313
2314 static int snat_add_interface_address (snat_main_t *sm,
2315                                        u32 sw_if_index,
2316                                        int is_del)
2317 {
2318   ip4_main_t * ip4_main = sm->ip4_main;
2319   ip4_address_t * first_int_addr;
2320   snat_static_map_resolve_t *rp;
2321   u32 *indices_to_delete = 0;
2322   int i, j;
2323
2324   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2325                                                 0 /* just want the address*/);
2326
2327   for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2328     {
2329       if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2330         {
2331           if (is_del)
2332             {
2333               /* if have address remove it */
2334               if (first_int_addr)
2335                   (void) snat_del_address (sm, first_int_addr[0], 1);
2336               else
2337                 {
2338                   for (j = 0; j < vec_len (sm->to_resolve); j++)
2339                     {
2340                       rp = sm->to_resolve + j;
2341                       if (rp->sw_if_index == sw_if_index)
2342                         vec_add1 (indices_to_delete, j);
2343                     }
2344                   if (vec_len(indices_to_delete))
2345                     {
2346                       for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2347                         vec_del1(sm->to_resolve, j);
2348                       vec_free(indices_to_delete);
2349                     }
2350                 }
2351               vec_del1(sm->auto_add_sw_if_indices, i);
2352             }
2353           else
2354             return VNET_API_ERROR_VALUE_EXIST;
2355
2356           return 0;
2357         }
2358     }
2359   
2360   if (is_del)
2361     return VNET_API_ERROR_NO_SUCH_ENTRY;
2362
2363   /* add to the auto-address list */
2364   vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2365
2366   /* If the address is already bound - or static - add it now */
2367   if (first_int_addr)
2368       snat_add_address (sm, first_int_addr);
2369
2370   return 0;
2371 }
2372
2373 static clib_error_t *
2374 snat_add_interface_address_command_fn (vlib_main_t * vm,
2375                                        unformat_input_t * input,
2376                                        vlib_cli_command_t * cmd)
2377 {
2378   snat_main_t *sm = &snat_main;
2379   unformat_input_t _line_input, *line_input = &_line_input;
2380   u32 sw_if_index;
2381   int rv;
2382   int is_del = 0;
2383
2384   /* Get a line of input. */
2385   if (!unformat_user (input, unformat_line_input, line_input))
2386     return 0;
2387
2388   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2389     {
2390       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2391                     sm->vnet_main, &sw_if_index))
2392         ;
2393       else if (unformat (line_input, "del"))
2394         is_del = 1;
2395       else
2396         return clib_error_return (0, "unknown input '%U'",
2397                                   format_unformat_error, line_input);
2398     }
2399
2400   rv = snat_add_interface_address (sm, sw_if_index, is_del);
2401
2402   switch (rv)
2403     {
2404     case 0:
2405       break;
2406
2407     default:
2408       return clib_error_return (0, "snat_add_interface_address returned %d",
2409                                 rv);
2410     }
2411   return 0;
2412 }
2413
2414 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2415     .path = "snat add interface address",
2416     .short_help = "snat add interface address <interface> [del]",
2417     .function = snat_add_interface_address_command_fn,
2418 };