Implement DHCPv6 IA NA client (VPP-1094)

Change-Id: I682a47d6cf9975aca6136188d28ee93eaadf4fe3
Signed-off-by: Juraj Sloboda <jsloboda@cisco.com>
diff --git a/src/vnet/dhcp/dhcp6_pd_client_dp.c b/src/vnet/dhcp/dhcp6_pd_client_dp.c
index b2b8f88..9331121 100644
--- a/src/vnet/dhcp/dhcp6_pd_client_dp.c
+++ b/src/vnet/dhcp/dhcp6_pd_client_dp.c
@@ -24,6 +24,7 @@
 #include <vlibapi/api_common.h>
 #include <vlibmemory/api.h>
 #include <vnet/dhcp/dhcp6_pd_client_dp.h>
+#include <vnet/dhcp/dhcp6_client_common_dp.h>
 
 #include <vnet/vnet_msg_enum.h>
 
@@ -43,86 +44,11 @@
 
 #include <vlibapi/api_helper_macros.h>
 
-typedef struct
-{
-  u8 entry_valid;
-  u8 keep_sending_client_message;	/* when true then next fields are valid */
-  dhcp6_pd_send_client_message_params_t params;
-  f64 transaction_start;
-  f64 sleep_interval;
-  f64 due_time;
-  u32 n_left;
-  f64 start_time;
-  u32 transaction_id;
-  vlib_buffer_t *buffer;
-  u32 elapsed_pos;
-  u32 adj_index;
-} dhcp6_pd_client_state_t;
-
-typedef struct
-{
-  u8 *data;
-  u16 len;
-} server_id_t;
-
-typedef struct
-{
-  dhcp6_pd_client_state_t *client_state_by_sw_if_index;
-  server_id_t *server_ids;
-
-  uword publisher_node;
-  uword publisher_et;
-
-  u32 seed;
-
-  /* convenience */
-  vlib_main_t *vlib_main;
-  vnet_main_t *vnet_main;
-} dhcp6_pd_client_main_t;
-
-static dhcp6_pd_client_main_t dhcp6_pd_client_main;
+dhcp6_pd_client_main_t dhcp6_pd_client_main;
 dhcp6_pd_client_public_main_t dhcp6_pd_client_public_main;
 
-typedef struct
-{
-  ip6_address_t prefix;
-  u8 prefix_length;
-  u32 valid_time;
-  u32 preferred_time;
-  u16 status_code;
-} prefix_info_t;
-
-typedef struct
-{
-  u32 sw_if_index;
-  u32 server_index;
-  u8 msg_type;
-  u32 T1;
-  u32 T2;
-  u16 inner_status_code;
-  u16 status_code;
-  u8 preference;
-  u32 n_prefixes;
-  prefix_info_t *prefixes;
-} report_t;
-
-typedef union
-{
-  CLIB_PACKED (struct
-	       {
-	       u16 duid_type;
-	       u16 hardware_type;
-	       u8 lla[6];
-	       });
-  char bin_string[10];
-} dhcpv6_duid_ll_string_t;
-
-static dhcpv6_duid_ll_string_t client_duid;
-#define CLIENT_DUID_LENGTH sizeof (client_duid)
-#define DHCPV6_CLIENT_IAID 1
-
 static void
-signal_report (report_t * r)
+signal_report (prefix_report_t * r)
 {
   vlib_main_t *vm = vlib_get_main ();
   dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main;
@@ -131,13 +57,14 @@
 
   if (ni == (uword) ~ 0)
     return;
-  report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
+  prefix_report_t *q =
+    vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
 
   *q = *r;
 }
 
-static int
-publish_report (report_t * r)
+int
+dhcp6_pd_publish_report (prefix_report_t * r)
 {
   void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
   vl_api_rpc_call_main_thread (signal_report, (u8 *) r, sizeof *r);
@@ -152,62 +79,6 @@
   cm->publisher_et = event_type;
 }
 
-#define foreach_dhcpv6_pd_client \
-  _(DROP, "error-drop")          \
-  _(LOOKUP, "ip6-lookup")
-
-typedef enum
-{
-#define _(sym,str) DHCPV6_PD_CLIENT_NEXT_##sym,
-  foreach_dhcpv6_pd_client
-#undef _
-    DHCPV6_PD_CLIENT_N_NEXT,
-} dhcpv6_pd_client_next_t;
-
-/**
- * per-packet trace data
- */
-typedef struct dhcpv6_pd_client_trace_t_
-{
-} dhcpv6_pd_client_trace_t;
-
-static u8 *
-format_dhcpv6_pd_client_trace (u8 * s, va_list * args)
-{
-  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
-  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
-  //dhcpv6_pd_client_trace_t *t = va_arg (*args, dhcpv6_pd_client_trace_t *);
-
-  s = format (s, "nothing");
-
-  return s;
-}
-
-static u32
-server_index_get_or_create (u8 * data, u16 len)
-{
-  dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main;
-  u32 i;
-  server_id_t *se;
-  server_id_t new_se;
-
-  for (i = 0; i < vec_len (cm->server_ids); i++)
-    {
-      se = &cm->server_ids[i];
-      if (se->len == len && 0 == memcmp (se->data, data, len))
-	return i;
-    }
-
-  new_se.len = len;
-  new_se.data = 0;
-  vec_validate (new_se.data, len - 1);
-  memcpy (new_se.data, data, len);
-
-  vec_add1 (cm->server_ids, new_se);
-
-  return vec_len (cm->server_ids) - 1;
-}
-
 static void
 stop_sending_client_message (vlib_main_t * vm,
 			     dhcp6_pd_client_state_t * client_state)
@@ -226,304 +97,13 @@
     }
 }
 
-static uword
-dhcpv6_pd_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
-			  vlib_frame_t * frame)
-{
-  dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main;
-
-  dhcpv6_pd_client_next_t next_index;
-  u32 n_left_from, *from, *to_next;
-  next_index = 0;
-  n_left_from = frame->n_vectors;
-  from = vlib_frame_vector_args (frame);
-
-  while (n_left_from > 0)
-    {
-      u32 n_left_to_next;
-
-      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
-
-      while (n_left_from > 0 && n_left_to_next > 0)
-	{
-	  ip6_header_t *ip0;
-	  u32 options_length;
-	  dhcpv6_header_t *dhcpv60;
-	  dhcpv6_option_t *option;
-	  vlib_buffer_t *b0;
-	  report_t report;
-	  u32 next0 = DHCPV6_PD_CLIENT_NEXT_DROP;
-	  u32 bi0;
-	  u32 xid;
-	  u32 sw_if_index;
-	  u32 iaid;
-	  u8 client_id_present = 0;
-	  u8 discard = 0;
-
-	  dhcp6_pd_client_state_t *client_state = NULL;
-
-	  bi0 = from[0];
-	  to_next[0] = bi0;
-	  from += 1;
-	  to_next += 1;
-	  n_left_from -= 1;
-	  n_left_to_next -= 1;
-
-	  b0 = vlib_get_buffer (vm, bi0);
-
-	  dhcpv60 = vlib_buffer_get_current (b0);
-	  ip0 = (void *) (b0->data + vnet_buffer (b0)->l3_hdr_offset);
-	  u32 dhcpv6_ip6_palyoad_offset =
-	    (u8 *) dhcpv60 - ((u8 *) ip0 + sizeof (*ip0));
-	  options_length =
-	    ntohs (ip0->payload_length) - dhcpv6_ip6_palyoad_offset -
-	    sizeof (*dhcpv60);
-
-	  memset (&report, 0, sizeof (report));
-
-	  sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
-	  if (sw_if_index >= vec_len (cm->client_state_by_sw_if_index))
-	    client_state = 0;
-	  else
-	    client_state = &cm->client_state_by_sw_if_index[sw_if_index];
-
-	  xid =
-	    (dhcpv60->xid[0] << 16) + (dhcpv60->xid[1] << 8) +
-	    dhcpv60->xid[2];
-	  if (!client_state || client_state->transaction_id != xid)
-	    {
-	      clib_warning
-		("Received DHCPv6 message with wrong Transaction ID");
-	      discard = 1;
-	    }
-
-	  report.sw_if_index = sw_if_index;
-	  report.msg_type = dhcpv60->msg_type;
-	  report.server_index = ~0;
-
-	  switch (dhcpv60->msg_type)
-	    {
-	    case DHCPV6_MSG_ADVERTISE:
-	    case DHCPV6_MSG_REPLY:
-	      option = (dhcpv6_option_t *) (dhcpv60 + 1);
-	      while (options_length > 0)
-		{
-		  if (options_length <
-		      ntohs (option->length) + sizeof (*option))
-		    {
-		      clib_warning
-			("remaining payload length < option length (%d < %d)",
-			 options_length,
-			 ntohs (option->length) + sizeof (*option));
-		      break;
-		    }
-		  u16 oo = ntohs (option->option);
-		  if (oo == DHCPV6_OPTION_IA_PD)
-		    {
-		      u8 discard_ia_pd = 0;
-		      dhcpv6_ia_header_t *ia_header = (void *) option;
-		      iaid = ntohl (ia_header->iaid);
-		      u32 T1 = ntohl (ia_header->t1);
-		      u32 T2 = ntohl (ia_header->t2);
-		      if (iaid != DHCPV6_CLIENT_IAID)
-			discard_ia_pd = 1;
-		      if (T1 != 0 && T2 != 0 && T1 > T2)
-			discard_ia_pd = 1;
-		      if (!discard_ia_pd)
-			{
-			  report.T1 = T1;
-			  report.T2 = T2;
-			}
-		      dhcpv6_option_t *inner_option =
-			(void *) ia_header->data;
-		      u16 inner_options_length =
-			ntohs (option->length) - (sizeof (*ia_header) -
-						  sizeof (dhcpv6_option_t));
-		      while (inner_options_length > 0)
-			{
-			  u16 inner_oo = ntohs (inner_option->option);
-			  if (discard_ia_pd)
-			    ;
-			  else if (inner_oo == DHCPV6_OPTION_IAPREFIX)
-			    {
-			      dhcpv6_ia_opt_pd_t *iaprefix =
-				(void *) inner_option;
-			      vec_validate (report.prefixes,
-					    report.n_prefixes);
-			      prefix_info_t *prefix_info =
-				&report.prefixes[report.n_prefixes];
-			      report.n_prefixes++;
-			      prefix_info->preferred_time =
-				ntohl (iaprefix->preferred);
-			      prefix_info->valid_time =
-				ntohl (iaprefix->valid);
-			      prefix_info->prefix_length = iaprefix->prefix;
-			      prefix_info->prefix = iaprefix->addr;
-			    }
-			  else if (inner_oo == DHCPV6_OPTION_STATUS_CODE)
-			    {
-			      dhcpv6_status_code_t *sc =
-				(void *) inner_option;
-			      report.inner_status_code =
-				ntohs (sc->status_code);
-			    }
-			  inner_options_length -=
-			    sizeof (*inner_option) +
-			    ntohs (inner_option->length);
-			  inner_option =
-			    (void *) ((u8 *) inner_option +
-				      sizeof (*inner_option) +
-				      ntohs (inner_option->length));
-			}
-		    }
-		  else if (oo == DHCPV6_OPTION_CLIENTID)
-		    {
-		      if (client_id_present)
-			{
-			  clib_warning
-			    ("Duplicate Client ID in received DHVPv6 message");
-			  discard = 1;
-			}
-		      else
-			{
-			  u16 len = ntohs (option->length);
-			  client_id_present = 1;
-			  if (len != CLIENT_DUID_LENGTH ||
-			      0 != memcmp (option->data,
-					   client_duid.bin_string,
-					   CLIENT_DUID_LENGTH))
-			    {
-			      clib_warning
-				("Unrecognized client DUID inside received DHVPv6 message");
-			      discard = 1;
-			    }
-			}
-		    }
-		  else if (oo == DHCPV6_OPTION_SERVERID)
-		    {
-		      if (report.server_index != ~0)
-			{
-			  clib_warning
-			    ("Duplicate Server ID in received DHVPv6 message");
-			  discard = 1;
-			}
-		      else
-			{
-			  u16 ol = ntohs (option->length);
-			  if (ol - 2 /* 2 byte DUID type code */  > 128)
-			    {
-			      clib_warning
-				("Server DUID (without type code) is longer than 128 octets");
-			      discard = 1;
-			    }
-			  else
-			    {
-			      report.server_index =
-				server_index_get_or_create (option->data, ol);
-			    }
-			}
-		    }
-		  else if (oo == DHCPV6_OPTION_PREFERENCE)
-		    {
-		      report.preference = option->data[0];
-		    }
-		  else if (oo == DHCPV6_OPTION_STATUS_CODE)
-		    {
-		      dhcpv6_status_code_t *sc = (void *) option;
-		      report.status_code = ntohs (sc->status_code);
-		    }
-		  options_length -= sizeof (*option) + ntohs (option->length);
-		  option =
-		    (void *) ((u8 *) option + sizeof (*option) +
-			      ntohs (option->length));
-		}
-
-	      if (!client_id_present)
-		{
-		  clib_warning
-		    ("Missing Client ID in received DHVPv6 message");
-		  discard = 1;
-		}
-	      if (report.server_index == ~0)
-		{
-		  clib_warning
-		    ("Missing Server ID in received DHVPv6 message");
-		  discard = 1;
-		}
-
-	      if (!discard)
-		publish_report (&report);
-	      else
-		vec_free (report.prefixes);
-
-	      break;
-	    default:
-	      break;
-	    }
-
-	  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
-	    {
-	      dhcpv6_pd_client_trace_t *t =
-		vlib_add_trace (vm, node, b0, sizeof (*t));
-	    }
-
-	  /* verify speculative enqueue, maybe switch current next frame */
-	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
-					   to_next, n_left_to_next,
-					   bi0, next0);
-	}
-
-      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
-    }
-
-  return frame->n_vectors;
-}
-
-/* *INDENT-OFF* */
-VLIB_REGISTER_NODE (dhcpv6_pd_client_node, static) = {
-    .function = dhcpv6_pd_client_node_fn,
-    .name = "dhcpv6-pd-client",
-    .vector_size = sizeof (u32),
-
-    .n_errors = 0,
-
-    .n_next_nodes = DHCPV6_PD_CLIENT_N_NEXT,
-    .next_nodes = {
-  #define _(s,n) [DHCPV6_PD_CLIENT_NEXT_##s] = n,
-      foreach_dhcpv6_pd_client
-  #undef _
-    },
-
-    .format_trace = format_dhcpv6_pd_client_trace,
-};
-/* *INDENT-ON* */
-
-static_always_inline f64
-random_f64_from_to (f64 from, f64 to)
-{
-  static u32 seed = 0;
-  static u8 seed_set = 0;
-  if (!seed_set)
-    {
-      seed = random_default_seed ();
-      seed_set = 1;
-    }
-  return random_f64 (&seed) * (to - from) + from;
-}
-
-static const ip6_address_t all_dhcp6_relay_agents_and_servers = {
-  .as_u8 = {
-	    0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}
-};
-
 static vlib_buffer_t *
 create_buffer_for_client_message (vlib_main_t * vm,
 				  u32 sw_if_index,
 				  dhcp6_pd_client_state_t
 				  * client_state, u32 type)
 {
-  dhcp6_pd_client_main_t *dm = &dhcp6_pd_client_main;
+  dhcp6_client_common_main_t *ccm = &dhcp6_client_common_main;
   vnet_main_t *vnm = vnet_get_main ();
 
   vlib_buffer_t *b;
@@ -612,7 +192,7 @@
       if (client_state->params.server_index != ~0)
 	{
 	  server_id_t *se =
-	    &dm->server_ids[client_state->params.server_index];
+	    &ccm->server_ids[client_state->params.server_index];
 
 	  duid = (dhcpv6_option_t *) d;
 	  duid->option = clib_host_to_net_u16 (DHCPV6_OPTION_SERVERID);
@@ -798,7 +378,7 @@
 }
 
 /* *INDENT-OFF* */
-VLIB_REGISTER_NODE (send_dhcp6_pd_client_message_process_node) = {
+VLIB_REGISTER_NODE (send_dhcp6_pd_client_message_process_node, static) = {
     .function = send_dhcp6_pd_client_message_process,
     .type = VLIB_NODE_TYPE_PROCESS,
     .name = "send-dhcp6-pd-client-message-process",
@@ -934,7 +514,7 @@
       int i;
       if (event_type == DHCP6_PD_DP_REPLY_REPORT)
 	{
-	  report_t *events = event_data;
+	  prefix_report_t *events = event_data;
 	  for (i = 0; i < vec_len (events); i++)
 	    {
 	      u32 event_size =
@@ -945,14 +525,15 @@
 		clib_mem_alloc (event_size);
 	      memset (event, 0, event_size);
 
-	      event->sw_if_index = htonl (events[i].sw_if_index);
-	      event->server_index = htonl (events[i].server_index);
-	      event->msg_type = events[i].msg_type;
-	      event->T1 = htonl (events[i].T1);
-	      event->T2 = htonl (events[i].T2);
-	      event->inner_status_code = htons (events[i].inner_status_code);
-	      event->status_code = htons (events[i].status_code);
-	      event->preference = events[i].preference;
+	      event->sw_if_index = htonl (events[i].body.sw_if_index);
+	      event->server_index = htonl (events[i].body.server_index);
+	      event->msg_type = events[i].body.msg_type;
+	      event->T1 = htonl (events[i].body.T1);
+	      event->T2 = htonl (events[i].body.T2);
+	      event->inner_status_code =
+		htons (events[i].body.inner_status_code);
+	      event->status_code = htons (events[i].body.status_code);
+	      event->preference = events[i].body.preference;
 
 	      event->n_prefixes = htonl (vec_len (events[i].prefixes));
 	      vl_api_dhcp6_pd_prefix_info_t *prefix =
@@ -960,7 +541,7 @@
 	      u32 j;
 	      for (j = 0; j < vec_len (events[i].prefixes); j++)
 		{
-		  prefix_info_t *info = &events[i].prefixes[j];
+		  dhcp6_prefix_info_t *info = &events[i].prefixes[j];
 		  memcpy (prefix->prefix, &info->prefix, 16);
 		  prefix->prefix_length = info->prefix_length;
 		  prefix->valid_time = htonl (info->valid_time);
@@ -1002,7 +583,7 @@
 }
 
 /* *INDENT-OFF* */
-VLIB_REGISTER_NODE (dhcp6_pd_reply_process_node, ) = {
+VLIB_REGISTER_NODE (dhcp6_pd_reply_process_node) = {
   .function = dhcp6_pd_reply_process,
   .type = VLIB_NODE_TYPE_PROCESS,
   .name = "dhcp6-pd-reply-publisher-process",
@@ -1036,7 +617,7 @@
 	  hash_unset (am->dhcp6_pd_reply_events_registration_hash,
 		      mp->client_index);
 	  if (pool_elts (am->dhcp6_pd_reply_events_registrations) == 0)
-	    dhcp6_pd_set_publisher_node (~0, REPORT_MAX);
+	    dhcp6_pd_set_publisher_node (~0, DHCP6_PD_DP_REPORT_MAX);
 	  goto reply;
 	}
     }
@@ -1058,91 +639,6 @@
   REPLY_MACRO (VL_API_WANT_DHCP6_PD_REPLY_EVENTS_REPLY);
 }
 
-void
-vl_api_dhcp6_duid_ll_set_t_handler (vl_api_dhcp6_duid_ll_set_t * mp)
-{
-  vl_api_dhcp6_duid_ll_set_reply_t *rmp;
-  dhcpv6_duid_ll_string_t *duid;
-  int rv = 0;
-
-  duid = (dhcpv6_duid_ll_string_t *) mp->duid_ll;
-  if (duid->duid_type != htonl (DHCPV6_DUID_LL))
-    {
-      rv = VNET_API_ERROR_INVALID_VALUE;
-      goto reply;
-    }
-  clib_memcpy (&client_duid, &duid, sizeof (client_duid));
-
-reply:
-  REPLY_MACRO (VL_API_DHCP6_DUID_LL_SET_REPLY);
-}
-
-static void
-generate_client_duid (void)
-{
-  client_duid.duid_type = htons (DHCPV6_DUID_LL);
-  client_duid.hardware_type = htons (1);
-
-  vnet_main_t *vnm = vnet_get_main ();
-  vnet_interface_main_t *im = &vnm->interface_main;
-  vnet_hw_interface_t *hi;
-  ethernet_interface_t *eth_if = 0;
-
-  /* *INDENT-OFF* */
-  pool_foreach (hi, im->hw_interfaces,
-  ({
-    eth_if = ethernet_get_interface (&ethernet_main, hi->hw_if_index);
-    if (eth_if)
-      break;
-  }));
-  /* *INDENT-ON* */
-
-  if (eth_if)
-    clib_memcpy (client_duid.lla, eth_if->address, 6);
-  else
-    {
-      clib_warning ("Failed to find any Ethernet interface, "
-		    "setting DHCPv6 DUID link-layer address to random value");
-      u32 seed = random_default_seed ();
-      random_u32 (&seed);
-      client_duid.lla[0] = 0xc2;	/* locally administered unicast */
-      client_duid.lla[1] = 0x18;
-      client_duid.lla[2] = 0x44;
-      client_duid.lla[3] = random_u32 (&seed);
-      client_duid.lla[4] = random_u32 (&seed);
-      client_duid.lla[5] = random_u32 (&seed);
-    }
-}
-
-void
-dhcp6_clients_enable_disable (u8 enable)
-{
-  vlib_main_t *vm = vlib_get_main ();
-
-  if (enable)
-    {
-      if (client_duid.duid_type == 0)
-	generate_client_duid ();
-      udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
-			     dhcpv6_pd_client_node.index, 0 /* is_ip6 */ );
-    }
-  else
-    udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
-			     0 /* is_ip6 */ );
-}
-
-void
-  vl_api_dhcp6_clients_enable_disable_t_handler
-  (vl_api_dhcp6_clients_enable_disable_t * mp)
-{
-  vl_api_dhcp6_clients_enable_disable_reply_t *rmp;
-  int rv = 0;
-
-  dhcp6_clients_enable_disable (mp->enable);
-
-  REPLY_MACRO (VL_API_WANT_DHCP6_PD_REPLY_EVENTS_REPLY);
-}
-
 static clib_error_t *
 dhcp6_pd_client_init (vlib_main_t * vm)
 {