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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vnet/dhcp/dhcp6_packet.h>
18 #include <vnet/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_neighbor.h>
24 #include <vlibapi/api_common.h>
25 #include <vlibmemory/api.h>
26 #include <vnet/dhcp/dhcp6_pd_client_dp.h>
27 #include <vnet/dhcp/dhcp6_client_common_dp.h>
28 #include <vnet/ip/ip_types_api.h>
30 #include <vnet/vnet_msg_enum.h>
32 #define vl_typedefs /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
36 #define vl_endianfun /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #include <vnet/vnet_all_api_h.h>
46 #include <vlibapi/api_helper_macros.h>
48 dhcp6_pd_client_main_t dhcp6_pd_client_main;
49 dhcp6_pd_client_public_main_t dhcp6_pd_client_public_main;
52 signal_report (prefix_report_t * r)
54 vlib_main_t *vm = vlib_get_main ();
55 dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main;
56 uword ni = cm->publisher_node;
57 uword et = cm->publisher_et;
59 if (ni == (uword) ~ 0)
62 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
68 dhcp6_pd_publish_report (prefix_report_t * r)
70 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
71 vl_api_rpc_call_main_thread (signal_report, (u8 *) r, sizeof *r);
76 dhcp6_pd_set_publisher_node (uword node_index, uword event_type)
78 dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main;
79 cm->publisher_node = node_index;
80 cm->publisher_et = event_type;
84 stop_sending_client_message (vlib_main_t * vm,
85 dhcp6_pd_client_state_t * client_state)
89 client_state->keep_sending_client_message = 0;
90 vec_free (client_state->params.prefixes);
91 if (client_state->buffer)
93 bi0 = vlib_get_buffer_index (vm, client_state->buffer);
94 vlib_buffer_free (vm, &bi0, 1);
95 client_state->buffer = 0;
96 adj_unlock (client_state->adj_index);
97 client_state->adj_index = ~0;
101 static vlib_buffer_t *
102 create_buffer_for_client_message (vlib_main_t * vm,
104 dhcp6_pd_client_state_t
105 * client_state, u32 type)
107 dhcp6_client_common_main_t *ccm = &dhcp6_client_common_main;
108 vnet_main_t *vnm = vnet_get_main ();
114 dhcpv6_header_t *dhcp;
115 ip6_address_t src_addr;
116 u32 dhcp_opt_len = 0;
117 client_state->transaction_start = vlib_time_now (vm);
121 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
122 vnet_sw_interface_t *sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
123 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
125 /* Interface(s) down? */
126 if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
128 if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
130 if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
133 /* Get a link-local address */
134 src_addr = ip6_neighbor_get_link_local_address (sw_if_index);
136 if (src_addr.as_u8[0] != 0xfe)
138 clib_warning ("Could not find source address to send DHCPv6 packet");
142 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
144 clib_warning ("Buffer allocation failed");
148 b = vlib_get_buffer (vm, bi);
149 vnet_buffer (b)->sw_if_index[VLIB_RX] = sw_if_index;
150 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
151 client_state->adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
154 vnet_buffer (b)->ip.adj_index[VLIB_TX] = client_state->adj_index;
155 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
157 ip = (ip6_header_t *) vlib_buffer_get_current (b);
158 udp = (udp_header_t *) (ip + 1);
159 dhcp = (dhcpv6_header_t *) (udp + 1);
161 ip->src_address = src_addr;
163 ip->ip_version_traffic_class_and_flow_label =
164 clib_host_to_net_u32 (0x6 << 28);
165 ip->payload_length = 0;
166 ip->protocol = IP_PROTOCOL_UDP;
168 udp->src_port = clib_host_to_net_u16 (DHCPV6_CLIENT_PORT);
169 udp->dst_port = clib_host_to_net_u16 (DHCPV6_SERVER_PORT);
173 dhcp->msg_type = type;
174 dhcp->xid[0] = (client_state->transaction_id & 0x00ff0000) >> 16;
175 dhcp->xid[1] = (client_state->transaction_id & 0x0000ff00) >> 8;
176 dhcp->xid[2] = (client_state->transaction_id & 0x000000ff) >> 0;
178 void *d = (void *) dhcp->data;
179 dhcpv6_option_t *duid;
180 dhcpv6_elapsed_t *elapsed;
181 dhcpv6_ia_header_t *ia_hdr;
182 dhcpv6_ia_opt_pd_t *opt_pd;
183 if (type == DHCPV6_MSG_SOLICIT || type == DHCPV6_MSG_REQUEST ||
184 type == DHCPV6_MSG_RENEW || type == DHCPV6_MSG_REBIND ||
185 type == DHCPV6_MSG_RELEASE)
187 duid = (dhcpv6_option_t *) d;
188 duid->option = clib_host_to_net_u16 (DHCPV6_OPTION_CLIENTID);
189 duid->length = clib_host_to_net_u16 (CLIENT_DUID_LENGTH);
190 clib_memcpy (duid + 1, client_duid.bin_string, CLIENT_DUID_LENGTH);
191 d += sizeof (*duid) + CLIENT_DUID_LENGTH;
193 if (client_state->params.server_index != ~0)
196 &ccm->server_ids[client_state->params.server_index];
198 duid = (dhcpv6_option_t *) d;
199 duid->option = clib_host_to_net_u16 (DHCPV6_OPTION_SERVERID);
200 duid->length = clib_host_to_net_u16 (se->len);
201 clib_memcpy (duid + 1, se->data, se->len);
202 d += sizeof (*duid) + se->len;
205 elapsed = (dhcpv6_elapsed_t *) d;
206 elapsed->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_ELAPSED_TIME);
207 elapsed->opt.length =
208 clib_host_to_net_u16 (sizeof (*elapsed) - sizeof (elapsed->opt));
209 elapsed->elapsed_10ms = 0;
210 client_state->elapsed_pos =
211 (char *) &elapsed->elapsed_10ms -
212 (char *) vlib_buffer_get_current (b);
213 d += sizeof (*elapsed);
215 ia_hdr = (dhcpv6_ia_header_t *) d;
216 ia_hdr->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_IA_PD);
217 ia_hdr->iaid = clib_host_to_net_u32 (DHCPV6_CLIENT_IAID);
218 ia_hdr->t1 = clib_host_to_net_u32 (client_state->params.T1);
219 ia_hdr->t2 = clib_host_to_net_u32 (client_state->params.T2);
220 d += sizeof (*ia_hdr);
222 n_prefixes = vec_len (client_state->params.prefixes);
225 clib_host_to_net_u16 (sizeof (*ia_hdr) +
226 n_prefixes * sizeof (*opt_pd) -
227 sizeof (ia_hdr->opt));
229 for (i = 0; i < n_prefixes; i++)
231 dhcp6_pd_send_client_message_params_prefix_t *pref =
232 &client_state->params.prefixes[i];
233 opt_pd = (dhcpv6_ia_opt_pd_t *) d;
234 opt_pd->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_IAPREFIX);
236 clib_host_to_net_u16 (sizeof (*opt_pd) - sizeof (opt_pd->opt));
237 opt_pd->addr = pref->prefix;
238 opt_pd->prefix = pref->prefix_length;
239 opt_pd->valid = clib_host_to_net_u32 (pref->valid_lt);
240 opt_pd->preferred = clib_host_to_net_u32 (pref->preferred_lt);
241 d += sizeof (*opt_pd);
246 clib_warning ("State not implemented");
249 dhcp_opt_len = ((u8 *) d) - dhcp->data;
251 clib_host_to_net_u16 (sizeof (*udp) + sizeof (*dhcp) + dhcp_opt_len);
252 ip->payload_length = udp->length;
254 sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp) + dhcp_opt_len;
256 ip->dst_address = all_dhcp6_relay_agents_and_servers;
262 check_pd_send_client_message (vlib_main_t * vm,
263 dhcp6_pd_client_state_t * client_state,
264 f64 current_time, f64 * due_time)
274 int bogus_length = 0;
276 dhcp6_pd_send_client_message_params_t *params;
278 f64 now = vlib_time_now (vm);
280 if (!client_state->keep_sending_client_message)
283 params = &client_state->params;
285 if (client_state->due_time > current_time)
287 *due_time = client_state->due_time;
291 p0 = client_state->buffer;
293 next_index = ip6_rewrite_mcast_node.index;
295 c0 = vlib_buffer_copy (vm, p0);
296 ci0 = vlib_get_buffer_index (vm, c0);
298 ip = (ip6_header_t *) vlib_buffer_get_current (c0);
299 udp = (udp_header_t *) (ip + 1);
301 u16 *elapsed_field = (u16 *) ((void *) ip + client_state->elapsed_pos);
303 clib_host_to_net_u16 ((u16)
304 ((now - client_state->transaction_start) * 100));
308 ip6_tcp_udp_icmp_compute_checksum (vm, 0, ip, &bogus_length);
310 f = vlib_get_frame_to_node (vm, next_index);
311 to_next = vlib_frame_vector_args (f);
314 vlib_put_frame_to_node (vm, next_index, f);
316 if (params->mrc != 0 && --client_state->n_left == 0)
317 stop_sending_client_message (vm, client_state);
320 client_state->sleep_interval =
321 (2 + random_f64_from_to (-0.1, 0.1)) * client_state->sleep_interval;
322 if (client_state->sleep_interval > params->mrt)
323 client_state->sleep_interval =
324 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
326 client_state->due_time = current_time + client_state->sleep_interval;
329 && current_time > client_state->start_time + params->mrd)
330 stop_sending_client_message (vm, client_state);
332 *due_time = client_state->due_time;
335 return client_state->keep_sending_client_message;
339 send_dhcp6_pd_client_message_process (vlib_main_t * vm,
340 vlib_node_runtime_t * rt,
343 dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main;
344 dhcp6_pd_client_state_t *client_state;
345 uword *event_data = 0;
346 f64 sleep_time = 1e9;
354 vlib_process_wait_for_event_or_clock (vm, sleep_time);
355 vlib_process_get_events (vm, &event_data);
356 vec_reset_length (event_data);
358 current_time = vlib_time_now (vm);
361 due_time = current_time + 1e9;
362 for (i = 0; i < vec_len (cm->client_state_by_sw_if_index); i++)
364 client_state = &cm->client_state_by_sw_if_index[i];
365 if (!client_state->entry_valid)
367 if (check_pd_send_client_message
368 (vm, client_state, current_time, &dt) && (dt < due_time))
371 current_time = vlib_time_now (vm);
373 while (due_time < current_time);
375 sleep_time = due_time - current_time;
382 VLIB_REGISTER_NODE (send_dhcp6_pd_client_message_process_node, static) = {
383 .function = send_dhcp6_pd_client_message_process,
384 .type = VLIB_NODE_TYPE_PROCESS,
385 .name = "send-dhcp6-pd-client-message-process",
390 dhcp6_pd_send_client_message (vlib_main_t * vm, u32 sw_if_index, u8 stop,
391 dhcp6_pd_send_client_message_params_t * params)
393 dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main;
394 dhcp6_pd_client_state_t *client_state = 0;
395 dhcp6_pd_client_state_t empty_state = {
399 ASSERT (~0 != sw_if_index);
401 vec_validate_init_empty (cm->client_state_by_sw_if_index, sw_if_index,
403 client_state = &cm->client_state_by_sw_if_index[sw_if_index];
404 if (!client_state->entry_valid)
406 client_state->entry_valid = 1;
407 client_state->adj_index = ~0;
410 stop_sending_client_message (vm, client_state);
414 client_state->keep_sending_client_message = 1;
415 vec_free (client_state->params.prefixes);
416 client_state->params = *params;
417 client_state->params.prefixes = vec_dup (params->prefixes);
418 client_state->n_left = params->mrc;
419 client_state->start_time = vlib_time_now (vm);
420 client_state->sleep_interval =
421 (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
422 client_state->due_time = 0; /* send first packet ASAP */
423 client_state->transaction_id = random_u32 (&cm->seed) & 0x00ffffff;
424 client_state->buffer =
425 create_buffer_for_client_message (vm, sw_if_index, client_state,
427 if (!client_state->buffer)
428 client_state->keep_sending_client_message = 0;
430 vlib_process_signal_event (vm,
431 send_dhcp6_pd_client_message_process_node.index,
437 vl_api_dhcp6_pd_send_client_message_t_handler
438 (vl_api_dhcp6_pd_send_client_message_t * mp)
440 vl_api_dhcp6_pd_send_client_message_reply_t *rmp;
441 dhcp6_pd_send_client_message_params_t params;
442 vlib_main_t *vm = vlib_get_main ();
447 VALIDATE_SW_IF_INDEX (mp);
449 BAD_SW_IF_INDEX_LABEL;
450 REPLY_MACRO (VL_API_DHCP6_PD_SEND_CLIENT_MESSAGE_REPLY);
455 params.sw_if_index = ntohl (mp->sw_if_index);
456 params.server_index = ntohl (mp->server_index);
457 params.irt = ntohl (mp->irt);
458 params.mrt = ntohl (mp->mrt);
459 params.mrc = ntohl (mp->mrc);
460 params.mrd = ntohl (mp->mrd);
461 params.msg_type = ntohl (mp->msg_type);
462 params.T1 = ntohl (mp->T1);
463 params.T2 = ntohl (mp->T2);
464 n_prefixes = ntohl (mp->n_prefixes);
467 vec_validate (params.prefixes, n_prefixes - 1);
468 for (i = 0; i < n_prefixes; i++)
470 vl_api_dhcp6_pd_prefix_info_t *pi = &mp->prefixes[i];
471 dhcp6_pd_send_client_message_params_prefix_t *pref =
473 pref->preferred_lt = ntohl (pi->preferred_time);
474 pref->valid_lt = ntohl (pi->valid_time);
475 ip6_address_decode (pi->prefix.address, &pref->prefix);
476 pref->prefix_length = pi->prefix.len;
479 dhcp6_pd_send_client_message (vm, ntohl (mp->sw_if_index), mp->stop,
483 static clib_error_t *
484 call_dhcp6_pd_reply_event_callbacks (void *data,
485 _vnet_dhcp6_pd_reply_event_function_list_elt_t
488 clib_error_t *error = 0;
492 error = elt->fp (data);
495 elt = elt->next_dhcp6_pd_reply_event_function;
502 dhcp6_pd_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
505 /* These cross the longjmp boundary (vlib_process_wait_for_event)
506 * and need to be volatile - to prevent them from being optimized into
507 * a register - which could change during suspension */
511 vlib_process_wait_for_event (vm);
512 uword event_type = DHCP6_PD_DP_REPLY_REPORT;
513 void *event_data = vlib_process_get_event_data (vm, &event_type);
516 if (event_type == DHCP6_PD_DP_REPLY_REPORT)
518 prefix_report_t *events = event_data;
519 for (i = 0; i < vec_len (events); i++)
522 sizeof (vl_api_dhcp6_pd_reply_event_t) +
523 vec_len (events[i].prefixes) *
524 sizeof (vl_api_dhcp6_pd_prefix_info_t);
525 vl_api_dhcp6_pd_reply_event_t *event =
526 clib_mem_alloc (event_size);
527 clib_memset (event, 0, event_size);
529 event->sw_if_index = htonl (events[i].body.sw_if_index);
530 event->server_index = htonl (events[i].body.server_index);
531 event->msg_type = events[i].body.msg_type;
532 event->T1 = htonl (events[i].body.T1);
533 event->T2 = htonl (events[i].body.T2);
534 event->inner_status_code =
535 htons (events[i].body.inner_status_code);
536 event->status_code = htons (events[i].body.status_code);
537 event->preference = events[i].body.preference;
539 event->n_prefixes = htonl (vec_len (events[i].prefixes));
540 vl_api_dhcp6_pd_prefix_info_t *prefix =
541 (typeof (prefix)) event->prefixes;
543 for (j = 0; j < vec_len (events[i].prefixes); j++)
545 dhcp6_prefix_info_t *info = &events[i].prefixes[j];
546 ip6_address_encode (&info->prefix, prefix->prefix.address);
547 prefix->prefix.len = info->prefix_length;
548 prefix->valid_time = htonl (info->valid_time);
549 prefix->preferred_time = htonl (info->preferred_time);
552 vec_free (events[i].prefixes);
554 dhcp6_pd_client_public_main_t *dpcpm =
555 &dhcp6_pd_client_public_main;
556 call_dhcp6_pd_reply_event_callbacks (event, dpcpm->functions);
558 vpe_client_registration_t *reg;
560 pool_foreach(reg, vpe_api_main.dhcp6_pd_reply_events_registrations,
562 vl_api_registration_t *vl_reg;
564 vl_api_client_index_to_registration (reg->client_index);
565 if (vl_reg && vl_api_can_send_msg (vl_reg))
567 vl_api_dhcp6_pd_reply_event_t *msg =
568 vl_msg_api_alloc (event_size);
569 clib_memcpy (msg, event, event_size);
570 msg->_vl_msg_id = htons (VL_API_DHCP6_PD_REPLY_EVENT);
571 msg->client_index = reg->client_index;
572 msg->pid = reg->client_pid;
573 vl_api_send_msg (vl_reg, (u8 *) msg);
578 clib_mem_free (event);
581 vlib_process_put_event_data (vm, event_data);
588 VLIB_REGISTER_NODE (dhcp6_pd_reply_process_node) = {
589 .function = dhcp6_pd_reply_process,
590 .type = VLIB_NODE_TYPE_PROCESS,
591 .name = "dhcp6-pd-reply-publisher-process",
596 vl_api_want_dhcp6_pd_reply_events_t_handler
597 (vl_api_want_dhcp6_pd_reply_events_t * mp)
599 vpe_api_main_t *am = &vpe_api_main;
600 vl_api_want_dhcp6_pd_reply_events_reply_t *rmp;
604 hash_get (am->dhcp6_pd_reply_events_registration_hash, mp->client_index);
605 vpe_client_registration_t *rp;
608 if (mp->enable_disable)
610 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
611 rv = VNET_API_ERROR_INVALID_REGISTRATION;
617 pool_elt_at_index (am->dhcp6_pd_reply_events_registrations, p[0]);
618 pool_put (am->dhcp6_pd_reply_events_registrations, rp);
619 hash_unset (am->dhcp6_pd_reply_events_registration_hash,
621 if (pool_elts (am->dhcp6_pd_reply_events_registrations) == 0)
622 dhcp6_pd_set_publisher_node (~0, DHCP6_PD_DP_REPORT_MAX);
626 if (mp->enable_disable == 0)
628 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
629 rv = VNET_API_ERROR_INVALID_REGISTRATION;
632 pool_get (am->dhcp6_pd_reply_events_registrations, rp);
633 rp->client_index = mp->client_index;
634 rp->client_pid = ntohl (mp->pid);
635 hash_set (am->dhcp6_pd_reply_events_registration_hash, rp->client_index,
636 rp - am->dhcp6_pd_reply_events_registrations);
637 dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index,
638 DHCP6_PD_DP_REPLY_REPORT);
641 REPLY_MACRO (VL_API_WANT_DHCP6_PD_REPLY_EVENTS_REPLY);
644 static clib_error_t *
645 dhcp6_pd_client_init (vlib_main_t * vm)
647 dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main;
650 cm->vnet_main = vnet_get_main ();
651 cm->publisher_node = ~0;
652 cm->seed = (u32) clib_cpu_time_now ();
657 VLIB_INIT_FUNCTION (dhcp6_pd_client_init);
660 * fd.io coding-style-patch-verification: ON
663 * eval: (c-set-style "gnu")