FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
dhcp6_ia_na_client_dp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vlib/vlib.h>
17 #include <dhcp/dhcp6_packet.h>
18 #include <dhcp/dhcp_proxy.h>
19 #include <vnet/mfib/mfib_table.h>
20 #include <vnet/mfib/ip6_mfib.h>
21 #include <vnet/fib/fib.h>
22 #include <vnet/adj/adj_mcast.h>
23 #include <vnet/ip/ip6_link.h>
26 #include <vnet/ip/ip_types_api.h>
27 
30 
31 static void
33 {
36  uword ni = cm->publisher_node;
37  uword et = cm->publisher_et;
38 
39  if (ni == (uword) ~ 0)
40  return;
41  address_report_t *q =
42  vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
43 
44  *q = *r;
45 }
46 
47 int
49 {
50  void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
51  vl_api_rpc_call_main_thread (signal_report, (u8 *) r, sizeof *r);
52  return 0;
53 }
54 
55 void
56 dhcp6_set_publisher_node (uword node_index, uword event_type)
57 {
59  cm->publisher_node = node_index;
60  cm->publisher_et = event_type;
61 }
62 
63 static void
65  dhcp6_ia_na_client_state_t * client_state)
66 {
67  u32 bi0;
68 
69  client_state->keep_sending_client_message = 0;
70  vec_free (client_state->params.addresses);
71  if (client_state->buffer)
72  {
73  bi0 = vlib_get_buffer_index (vm, client_state->buffer);
74  vlib_buffer_free (vm, &bi0, 1);
75  client_state->buffer = 0;
76  adj_unlock (client_state->adj_index);
77  client_state->adj_index = ~0;
78  }
79 }
80 
81 static vlib_buffer_t *
83  dhcp6_ia_na_client_state_t * client_state,
84  u32 type)
85 {
87  vlib_buffer_t *b;
88  u32 bi;
90  udp_header_t *udp;
91  dhcpv6_header_t *dhcp;
92  const ip6_address_t *src_addr;
93  u32 dhcp_opt_len = 0;
94  client_state->transaction_start = vlib_time_now (vm);
95  u32 n_addresses;
96  u32 i;
97 
98  /* Get a link-local address */
99  src_addr = ip6_get_link_local_address (sw_if_index);
100 
101  if (src_addr->as_u8[0] != 0xfe)
102  {
103  clib_warning ("Could not find source address to send DHCPv6 packet");
104  return NULL;
105  }
106 
107  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
108  {
109  clib_warning ("Buffer allocation failed");
110  return NULL;
111  }
112 
113  b = vlib_get_buffer (vm, bi);
114  vnet_buffer (b)->sw_if_index[VLIB_RX] = sw_if_index;
115  vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
118  sw_if_index);
119  vnet_buffer (b)->ip.adj_index[VLIB_TX] = client_state->adj_index;
120  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
121 
123  udp = (udp_header_t *) (ip + 1);
124  dhcp = (dhcpv6_header_t *) (udp + 1);
125 
126  ip->src_address = *src_addr;
127  ip->hop_limit = 255;
129  clib_host_to_net_u32 (0x6 << 28);
130  ip->payload_length = 0;
131  ip->protocol = IP_PROTOCOL_UDP;
132 
133  udp->src_port = clib_host_to_net_u16 (DHCPV6_CLIENT_PORT);
134  udp->dst_port = clib_host_to_net_u16 (DHCPV6_SERVER_PORT);
135  udp->checksum = 0;
136  udp->length = 0;
137 
138  dhcp->msg_type = type;
139  dhcp->xid[0] = (client_state->transaction_id & 0x00ff0000) >> 16;
140  dhcp->xid[1] = (client_state->transaction_id & 0x0000ff00) >> 8;
141  dhcp->xid[2] = (client_state->transaction_id & 0x000000ff) >> 0;
142 
143  void *d = (void *) dhcp->data;
144  dhcpv6_option_t *duid;
145  dhcpv6_elapsed_t *elapsed;
146  dhcpv6_ia_header_t *ia_hdr;
147  dhcpv6_ia_opt_addr_t *opt_addr;
148  if (type == DHCPV6_MSG_SOLICIT || type == DHCPV6_MSG_REQUEST ||
149  type == DHCPV6_MSG_RENEW || type == DHCPV6_MSG_REBIND ||
150  type == DHCPV6_MSG_RELEASE)
151  {
152  duid = (dhcpv6_option_t *) d;
153  duid->option = clib_host_to_net_u16 (DHCPV6_OPTION_CLIENTID);
154  duid->length = clib_host_to_net_u16 (CLIENT_DUID_LENGTH);
156  d += sizeof (*duid) + CLIENT_DUID_LENGTH;
157 
158  if (client_state->params.server_index != ~0)
159  {
160  server_id_t *se =
161  &ccm->server_ids[client_state->params.server_index];
162 
163  duid = (dhcpv6_option_t *) d;
164  duid->option = clib_host_to_net_u16 (DHCPV6_OPTION_SERVERID);
165  duid->length = clib_host_to_net_u16 (se->len);
166  clib_memcpy (duid + 1, se->data, se->len);
167  d += sizeof (*duid) + se->len;
168  }
169 
170  elapsed = (dhcpv6_elapsed_t *) d;
171  elapsed->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_ELAPSED_TIME);
172  elapsed->opt.length =
173  clib_host_to_net_u16 (sizeof (*elapsed) - sizeof (elapsed->opt));
174  elapsed->elapsed_10ms = 0;
175  client_state->elapsed_pos =
176  (char *) &elapsed->elapsed_10ms -
177  (char *) vlib_buffer_get_current (b);
178  d += sizeof (*elapsed);
179 
180  ia_hdr = (dhcpv6_ia_header_t *) d;
181  ia_hdr->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_IA_NA);
182  ia_hdr->iaid = clib_host_to_net_u32 (DHCPV6_CLIENT_IAID);
183  ia_hdr->t1 = clib_host_to_net_u32 (client_state->params.T1);
184  ia_hdr->t2 = clib_host_to_net_u32 (client_state->params.T2);
185  d += sizeof (*ia_hdr);
186 
187  n_addresses = vec_len (client_state->params.addresses);
188 
189  ia_hdr->opt.length =
190  clib_host_to_net_u16 (sizeof (*ia_hdr) +
191  n_addresses * sizeof (*opt_addr) -
192  sizeof (ia_hdr->opt));
193 
194  for (i = 0; i < n_addresses; i++)
195  {
197  &client_state->params.addresses[i];
198  opt_addr = (dhcpv6_ia_opt_addr_t *) d;
199  opt_addr->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_IAADDR);
200  opt_addr->opt.length =
201  clib_host_to_net_u16 (sizeof (*opt_addr) -
202  sizeof (opt_addr->opt));
203  opt_addr->addr = addr->address;
204  opt_addr->valid = clib_host_to_net_u32 (addr->valid_lt);
205  opt_addr->preferred = clib_host_to_net_u32 (addr->preferred_lt);
206  d += sizeof (*opt_addr);
207  }
208  }
209  else
210  {
211  clib_warning ("State not implemented");
212  }
213 
214  dhcp_opt_len = ((u8 *) d) - dhcp->data;
215  udp->length =
216  clib_host_to_net_u16 (sizeof (*udp) + sizeof (*dhcp) + dhcp_opt_len);
217  ip->payload_length = udp->length;
218  b->current_length =
219  sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp) + dhcp_opt_len;
220 
222 
223  return b;
224 }
225 
226 static inline u8
228  dhcp6_ia_na_client_state_t * client_state,
229  f64 current_time, f64 * due_time)
230 {
231  vlib_buffer_t *p0;
232  vlib_frame_t *f;
233  u32 *to_next;
234  u32 next_index;
235  vlib_buffer_t *c0;
236  ip6_header_t *ip;
237  udp_header_t *udp;
238  u32 ci0;
239  int bogus_length = 0;
240 
242 
243  f64 now = vlib_time_now (vm);
244 
245  if (!client_state->keep_sending_client_message)
246  return false;
247 
248  params = &client_state->params;
249 
250  if (client_state->due_time > current_time)
251  {
252  *due_time = client_state->due_time;
253  return true;
254  }
255 
256  p0 = client_state->buffer;
257 
258  next_index = ip6_rewrite_mcast_node.index;
259 
260  c0 = vlib_buffer_copy (vm, p0);
261  if (c0 == NULL)
262  return client_state->keep_sending_client_message;
263 
264  ci0 = vlib_get_buffer_index (vm, c0);
265 
267  udp = (udp_header_t *) (ip + 1);
268 
269  u16 *elapsed_field = (u16 *) ((void *) ip + client_state->elapsed_pos);
270  *elapsed_field =
271  clib_host_to_net_u16 ((u16)
272  ((now - client_state->transaction_start) * 100));
273 
274  udp->checksum = 0;
275  udp->checksum =
276  ip6_tcp_udp_icmp_compute_checksum (vm, 0, ip, &bogus_length);
277 
278  f = vlib_get_frame_to_node (vm, next_index);
279  to_next = vlib_frame_vector_args (f);
280  to_next[0] = ci0;
281  f->n_vectors = 1;
282  vlib_put_frame_to_node (vm, next_index, f);
283 
284  if (params->mrc != 0 && --client_state->n_left == 0)
285  stop_sending_client_message (vm, client_state);
286  else
287  {
288  client_state->sleep_interval =
289  (2 + random_f64_from_to (-0.1, 0.1)) * client_state->sleep_interval;
290  if (client_state->sleep_interval > params->mrt)
291  client_state->sleep_interval =
292  (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
293 
294  client_state->due_time = current_time + client_state->sleep_interval;
295 
296  if (params->mrd != 0
297  && current_time > client_state->start_time + params->mrd)
298  stop_sending_client_message (vm, client_state);
299  else
300  *due_time = client_state->due_time;
301  }
302 
303  return client_state->keep_sending_client_message;
304 }
305 
306 static uword
308  vlib_node_runtime_t * rt,
309  vlib_frame_t * f0)
310 {
312  dhcp6_ia_na_client_state_t *client_state;
313  uword *event_data = 0;
314  f64 sleep_time = 1e9;
315  f64 current_time;
316  f64 due_time;
317  f64 dt = 0;
318  int i;
319 
320  while (true)
321  {
322  vlib_process_wait_for_event_or_clock (vm, sleep_time);
323  vlib_process_get_events (vm, &event_data);
324  vec_reset_length (event_data);
325 
326  current_time = vlib_time_now (vm);
327  do
328  {
329  due_time = current_time + 1e9;
330  for (i = 0; i < vec_len (cm->client_state_by_sw_if_index); i++)
331  {
332  client_state = &cm->client_state_by_sw_if_index[i];
333  if (!client_state->entry_valid)
334  continue;
336  (vm, client_state, current_time, &dt) && (dt < due_time))
337  due_time = dt;
338  }
339  current_time = vlib_time_now (vm);
340  }
341  while (due_time < current_time);
342 
343  sleep_time = due_time - current_time;
344  }
345 
346  return 0;
347 }
348 
349 /* *INDENT-OFF* */
352  .type = VLIB_NODE_TYPE_PROCESS,
353  .name = "send-dhcp6-client-message-process",
354 };
355 /* *INDENT-ON* */
356 
357 void
360 {
362  dhcp6_ia_na_client_state_t *client_state = 0;
363  dhcp6_ia_na_client_state_t empty_state = { 0, };
364 
365  ASSERT (~0 != sw_if_index);
366 
368  empty_state);
369  client_state = &cm->client_state_by_sw_if_index[sw_if_index];
370  if (!client_state->entry_valid)
371  {
372  client_state->entry_valid = 1;
373  client_state->adj_index = ~0;
374  }
375 
376  stop_sending_client_message (vm, client_state);
377 
378  if (!stop)
379  {
380  client_state->keep_sending_client_message = 1;
381  vec_free (client_state->params.addresses);
382  client_state->params = *params;
383  client_state->params.addresses = vec_dup (params->addresses);
384  client_state->n_left = params->mrc;
385  client_state->start_time = vlib_time_now (vm);
386  client_state->sleep_interval =
387  (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
388  client_state->due_time = 0; /* send first packet ASAP */
389  client_state->transaction_id = random_u32 (&cm->seed) & 0x00ffffff;
390  client_state->buffer =
391  create_buffer_for_client_message (vm, sw_if_index, client_state,
392  params->msg_type);
393  if (!client_state->buffer)
394  client_state->keep_sending_client_message = 0;
395  else
398  1, 0);
399  }
400 }
401 
402 static clib_error_t *
404 {
406 
407  cm->vlib_main = vm;
408  cm->vnet_main = vnet_get_main ();
409 
410  cm->publisher_node = ~0;
411 
412  cm->seed = 0xdeaccabe;
413 
414  return 0;
415 }
416 
418 
419 /*
420  * fd.io coding-style-patch-verification: ON
421  *
422  * Local Variables:
423  * eval: (c-set-style "gnu")
424  * End:
425  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
static void signal_report(address_report_t *r)
static vlib_buffer_t * create_buffer_for_client_message(vlib_main_t *vm, u32 sw_if_index, dhcp6_ia_na_client_state_t *client_state, u32 type)
static clib_error_t * dhcp6_client_init(vlib_main_t *vm)
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:751
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:937
static void stop_sending_client_message(vlib_main_t *vm, dhcp6_ia_na_client_state_t *client_state)
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:333
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
vlib_main_t * vm
Definition: in2out_ed.c:1582
#define DHCPV6_CLIENT_PORT
Definition: dhcp6_packet.h:26
vhost_vring_addr_t addr
Definition: vhost_user.h:111
ip6_address_t src_address
Definition: ip6_packet.h:310
unsigned char u8
Definition: types.h:56
static vlib_buffer_t * vlib_buffer_copy(vlib_main_t *vm, vlib_buffer_t *b)
void dhcp6_set_publisher_node(uword node_index, uword event_type)
u8 data[128]
Definition: ipsec_types.api:89
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
#define clib_memcpy(d, s, n)
Definition: string.h:180
vlib_node_registration_t ip6_rewrite_mcast_node
(constructor) VLIB_REGISTER_NODE (ip6_rewrite_mcast_node)
Definition: ip6_forward.c:2220
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:579
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:619
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:348
unsigned int u32
Definition: types.h:88
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:293
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
dhcp6_ia_na_client_main_t dhcp6_ia_na_client_main
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1015
static __clib_warn_unused_result u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:677
unsigned short u16
Definition: types.h:57
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:216
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:429
vl_api_mac_address_t src_addr
Definition: flow_types.api:64
static void * vlib_process_signal_event_data(vlib_main_t *vm, uword node_index, uword type_opaque, uword n_data_elts, uword n_data_elt_bytes)
Definition: node_funcs.h:909
dhcp6_ia_na_client_state_t * client_state_by_sw_if_index
dhcpv6_duid_ll_string_t client_duid
dhcp6_ia_na_client_public_main_t dhcp6_ia_na_client_public_main
#define CLIENT_DUID_LENGTH
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:396
#define DHCPV6_CLIENT_IAID
dhcp6_send_client_message_params_address_t * addresses
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
#define clib_warning(format, args...)
Definition: error.h:59
u16 ip6_tcp_udp_icmp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip6_header_t *ip0, int *bogus_lengthp)
Definition: ip6_forward.c:1095
#define ASSERT(truth)
dhcp6_send_client_message_params_t params
void dhcp6_send_client_message(vlib_main_t *vm, u32 sw_if_index, u8 stop, dhcp6_send_client_message_params_t *params)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
dhcp6_client_common_main_t dhcp6_client_common_main
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:297
Definition: defs.h:47
u16 payload_length
Definition: ip6_packet.h:301
vl_api_address_t ip
Definition: l2.api:501
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
int dhcp6_publish_report(address_report_t *r)
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
static vlib_node_registration_t send_dhcp6_client_message_process_node
(constructor) VLIB_REGISTER_NODE (send_dhcp6_client_message_process_node)
static u8 check_send_client_message(vlib_main_t *vm, dhcp6_ia_na_client_state_t *client_state, f64 current_time, f64 *due_time)
#define DHCPV6_SERVER_PORT
Definition: dhcp6_packet.h:27
static uword send_dhcp6_client_message_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f0)
#define vnet_buffer(b)
Definition: buffer.h:417
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
adj_index_t adj_mcast_add_or_lock(fib_protocol_t proto, vnet_link_t link_type, u32 sw_if_index)
Mcast Adjacency.
Definition: adj_mcast.c:51
static const ip6_address_t all_dhcp6_relay_agents_and_servers
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:556
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
Definition: defs.h:46
ip6_address_t dst_address
Definition: ip6_packet.h:310
static_always_inline f64 random_f64_from_to(f64 from, f64 to)