-/*******************************/
-/*** Deterministic NAT (CGN) ***/
-/*******************************/
-
-static void
-vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
-{
- snat_main_t *sm = &snat_main;
- vl_api_nat_det_add_del_map_reply_t *rmp;
- int rv = 0;
- ip4_address_t in_addr, out_addr;
-
- if (!sm->deterministic)
- {
- rv = VNET_API_ERROR_UNSUPPORTED;
- goto send_reply;
- }
-
- clib_memcpy (&in_addr, mp->in_addr, 4);
- clib_memcpy (&out_addr, mp->out_addr, 4);
- rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
- mp->out_plen, mp->is_add);
-
-send_reply:
- REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
-}
-
-static void *
-vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
- void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: nat_det_add_del_map ");
- s = format (s, "inside address %U/%d outside address %U/%d\n",
- format_ip4_address, mp->in_addr, mp->in_plen,
- format_ip4_address, mp->out_addr, mp->out_plen);
-
- FINISH;
-}
-
-static void
-vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
-{
- snat_main_t *sm = &snat_main;
- vl_api_nat_det_forward_reply_t *rmp;
- int rv = 0;
- u16 lo_port = 0, hi_port = 0;
- snat_det_map_t *dm;
- ip4_address_t in_addr, out_addr;
-
- if (!sm->deterministic)
- {
- rv = VNET_API_ERROR_UNSUPPORTED;
- REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
- return;
- }
-
- out_addr.as_u32 = 0;
- clib_memcpy (&in_addr, mp->in_addr, 4);
- dm = snat_det_map_by_user (sm, &in_addr);
- if (!dm)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
- }
-
- snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
- hi_port = lo_port + dm->ports_per_host - 1;
-
-send_reply:
- /* *INDENT-OFF* */
- REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
- ({
- rmp->out_port_lo = ntohs (lo_port);
- rmp->out_port_hi = ntohs (hi_port);
- clib_memcpy (rmp->out_addr, &out_addr, 4);
- }))
- /* *INDENT-ON* */
-}
-
-static void *
-vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: nat_det_forward");
- s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
-
- FINISH;
-}
-
-static void
-vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
-{
- snat_main_t *sm = &snat_main;
- vl_api_nat_det_reverse_reply_t *rmp;
- int rv = 0;
- ip4_address_t out_addr, in_addr;
- snat_det_map_t *dm;
-
- if (!sm->deterministic)
- {
- rv = VNET_API_ERROR_UNSUPPORTED;
- REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
- return;
- }
-
- in_addr.as_u32 = 0;
- clib_memcpy (&out_addr, mp->out_addr, 4);
- dm = snat_det_map_by_out (sm, &out_addr);
- if (!dm)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
- }
-
- snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
-
-send_reply:
- /* *INDENT-OFF* */
- REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
- ({
- clib_memcpy (rmp->in_addr, &in_addr, 4);
- }))
- /* *INDENT-ON* */
-}
-
-static void *
-vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: nat_det_reverse");
- s = format (s, "outside ip address %U outside port %d",
- format_ip4_address, mp->out_addr, ntohs (mp->out_port));
-
- FINISH;
-}
-
-static void
-sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
- u32 context)
-{
- vl_api_nat_det_map_details_t *rmp;
- snat_main_t *sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- clib_memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
- clib_memcpy (rmp->in_addr, &m->in_addr, 4);
- rmp->in_plen = m->in_plen;
- clib_memcpy (rmp->out_addr, &m->out_addr, 4);
- rmp->out_plen = m->out_plen;
- rmp->sharing_ratio = htonl (m->sharing_ratio);
- rmp->ports_per_host = htons (m->ports_per_host);
- rmp->ses_num = htonl (m->ses_num);
- rmp->context = context;
-
- vl_api_send_msg (reg, (u8 *) rmp);
-}
-
-static void
-vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
-{
- vl_api_registration_t *reg;
- snat_main_t *sm = &snat_main;
- snat_det_map_t *m;
-
- if (!sm->deterministic)
- return;
-
- reg = vl_api_client_index_to_registration (mp->client_index);
- if (!reg)
- return;
-
- /* *INDENT-OFF* */
- vec_foreach(m, sm->det_maps)
- sent_nat_det_map_details(m, reg, mp->context);
- /* *INDENT-ON* */
-}
-
-static void *
-vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: nat_det_map_dump ");
-
- FINISH;
-}
-
-static void
-vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
- * mp)
-{
- snat_main_t *sm = &snat_main;
- vl_api_nat_det_close_session_out_reply_t *rmp;
- ip4_address_t out_addr, ext_addr, in_addr;
- snat_det_out_key_t key;
- snat_det_map_t *dm;
- snat_det_session_t *ses;
- int rv = 0;
-
- if (!sm->deterministic)
- {
- rv = VNET_API_ERROR_UNSUPPORTED;
- goto send_reply;
- }
-
- clib_memcpy (&out_addr, mp->out_addr, 4);
- clib_memcpy (&ext_addr, mp->ext_addr, 4);
-
- dm = snat_det_map_by_out (sm, &out_addr);
- if (!dm)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
- }
- snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
- key.ext_host_addr = ext_addr;
- key.ext_host_port = mp->ext_port;
- key.out_port = mp->out_port;
- ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
- if (!ses)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
- }
- snat_det_ses_close (dm, ses);
-
-send_reply:
- REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
-}
-
-static void *
-vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
- mp, void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: nat_det_close_session_out ");
- s = format (s, "out_addr %U out_port %d "
- "ext_addr %U ext_port %d\n",
- format_ip4_address, mp->out_addr, ntohs (mp->out_port),
- format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
-
- FINISH;
-}
-
-static void
-vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
- mp)
-{
- snat_main_t *sm = &snat_main;
- vl_api_nat_det_close_session_in_reply_t *rmp;
- ip4_address_t in_addr, ext_addr;
- snat_det_out_key_t key;
- snat_det_map_t *dm;
- snat_det_session_t *ses;
- int rv = 0;
-
- if (!sm->deterministic)
- {
- rv = VNET_API_ERROR_UNSUPPORTED;
- goto send_reply;
- }
-
- clib_memcpy (&in_addr, mp->in_addr, 4);
- clib_memcpy (&ext_addr, mp->ext_addr, 4);
-
- dm = snat_det_map_by_user (sm, &in_addr);
- if (!dm)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
- }
- key.ext_host_addr = ext_addr;
- key.ext_host_port = mp->ext_port;
- ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
- if (!ses)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
- }
- snat_det_ses_close (dm, ses);
-
-send_reply:
- REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
-}
-
-static void *
-vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
- mp, void *handle)
-{
- u8 *s;
- s = format (0, "SCRIPT: nat_det_close_session_in ");
- s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
- format_ip4_address, mp->in_addr, ntohs (mp->in_port),
- format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
-
- FINISH;
-}
-
-static void
-send_nat_det_session_details (snat_det_session_t * s,
- vl_api_registration_t * reg, u32 context)
-{
- vl_api_nat_det_session_details_t *rmp;
- snat_main_t *sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- clib_memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
- rmp->in_port = s->in_port;
- clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
- rmp->ext_port = s->out.ext_host_port;
- rmp->out_port = s->out.out_port;
- rmp->state = s->state;
- rmp->expire = ntohl (s->expire);
- rmp->context = context;
-
- vl_api_send_msg (reg, (u8 *) rmp);
-}
-
-static void
-vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
-{
- vl_api_registration_t *reg;
- snat_main_t *sm = &snat_main;
- ip4_address_t user_addr;
- snat_det_map_t *dm;
- snat_det_session_t *s, empty_ses;
- u16 i;
-
- if (!sm->deterministic)
- return;
-
- reg = vl_api_client_index_to_registration (mp->client_index);
- if (!reg)
- return;
-
- clib_memset (&empty_ses, 0, sizeof (empty_ses));
- clib_memcpy (&user_addr, mp->user_addr, 4);
- dm = snat_det_map_by_user (sm, &user_addr);
- if (!dm)
- return;
-
- s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
- for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
- {
- if (s->out.as_u64)
- send_nat_det_session_details (s, reg, mp->context);
- s++;
- }
-}
-
-static void *
-vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
- void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: nat_det_session_dump ");
- s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
-
- FINISH;
-}
-