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 <vnet/dhcp/dhcp6_client_common_dp.h>
17 #include <vnet/dhcp/dhcp6_ia_na_client_dp.h>
18 #include <vnet/dhcp/dhcp6_pd_client_dp.h>
19 #include <vnet/dhcp/dhcp6_packet.h>
20 #include <vnet/udp/udp.h>
22 dhcp6_client_common_main_t dhcp6_client_common_main;
23 dhcpv6_duid_ll_string_t client_duid;
26 server_index_get_or_create (u8 * data, u16 len)
28 dhcp6_client_common_main_t *ccm = &dhcp6_client_common_main;
33 for (i = 0; i < vec_len (ccm->server_ids); i++)
35 se = &ccm->server_ids[i];
36 if (se->len == len && 0 == memcmp (se->data, data, len))
42 vec_validate (new_se.data, len - 1);
43 memcpy (new_se.data, data, len);
45 vec_add1 (ccm->server_ids, new_se);
47 return vec_len (ccm->server_ids) - 1;
51 vl_api_dhcp6_duid_ll_set_t_handler (vl_api_dhcp6_duid_ll_set_t * mp)
53 vl_api_dhcp6_duid_ll_set_reply_t *rmp;
54 dhcpv6_duid_ll_string_t *duid;
57 duid = (dhcpv6_duid_ll_string_t *) mp->duid_ll;
58 if (duid->duid_type != htonl (DHCPV6_DUID_LL))
60 rv = VNET_API_ERROR_INVALID_VALUE;
63 clib_memcpy (&client_duid, &duid, sizeof (client_duid));
66 REPLY_MACRO (VL_API_DHCP6_DUID_LL_SET_REPLY);
70 generate_client_duid (void)
72 client_duid.duid_type = htons (DHCPV6_DUID_LL);
73 client_duid.hardware_type = htons (1);
75 vnet_main_t *vnm = vnet_get_main ();
76 vnet_interface_main_t *im = &vnm->interface_main;
77 vnet_hw_interface_t *hi;
78 ethernet_interface_t *eth_if = 0;
81 pool_foreach (hi, im->hw_interfaces,
83 eth_if = ethernet_get_interface (ðernet_main, hi->hw_if_index);
90 clib_memcpy (client_duid.lla, eth_if->address, 6);
93 clib_warning ("Failed to find any Ethernet interface, "
94 "setting DHCPv6 DUID link-layer address to random value");
95 u32 seed = random_default_seed ();
97 client_duid.lla[0] = 0xc2; /* locally administered unicast */
98 client_duid.lla[1] = 0x18;
99 client_duid.lla[2] = 0x44;
100 client_duid.lla[3] = random_u32 (&seed);
101 client_duid.lla[4] = random_u32 (&seed);
102 client_duid.lla[5] = random_u32 (&seed);
106 #define foreach_dhcpv6_client \
107 _(DROP, "error-drop") \
108 _(LOOKUP, "ip6-lookup")
112 #define _(sym,str) DHCPV6_CLIENT_NEXT_##sym,
113 foreach_dhcpv6_client
115 DHCPV6_CLIENT_N_NEXT,
116 } dhcpv6_client_next_t;
119 * per-packet trace data
121 typedef struct dhcpv6_client_trace_t_
123 } dhcpv6_client_trace_t;
126 format_dhcpv6_client_trace (u8 * s, va_list * args)
128 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
129 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
130 //dhcpv6_client_trace_t *t = va_arg (*args, dhcpv6_client_trace_t *);
132 s = format (s, "nothing");
138 dhcpv6_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
139 vlib_frame_t * frame)
141 dhcp6_ia_na_client_main_t *icm = &dhcp6_ia_na_client_main;
142 dhcp6_pd_client_main_t *pcm = &dhcp6_pd_client_main;
144 dhcpv6_client_next_t next_index;
145 u32 n_left_from, *from, *to_next;
147 n_left_from = frame->n_vectors;
148 from = vlib_frame_vector_args (frame);
150 while (n_left_from > 0)
154 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
156 while (n_left_from > 0 && n_left_to_next > 0)
160 dhcpv6_header_t *dhcpv60;
161 dhcpv6_option_t *option;
163 dhcp6_report_common_t report;
164 dhcp6_address_info_t *addresses = 0;
165 dhcp6_prefix_info_t *prefixes = 0;
166 u32 next0 = DHCPV6_CLIENT_NEXT_DROP;
171 u8 client_id_present = 0;
175 dhcp6_ia_na_client_state_t *ia_na_client_state = NULL;
176 dhcp6_pd_client_state_t *pd_client_state = NULL;
185 b0 = vlib_get_buffer (vm, bi0);
187 dhcpv60 = vlib_buffer_get_current (b0);
188 ip0 = (void *) (b0->data + vnet_buffer (b0)->l3_hdr_offset);
189 u32 dhcpv6_ip6_palyoad_offset =
190 (u8 *) dhcpv60 - ((u8 *) ip0 + sizeof (*ip0));
192 ntohs (ip0->payload_length) - dhcpv6_ip6_palyoad_offset -
195 clib_memset (&report, 0, sizeof (report));
197 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
198 if (sw_if_index >= vec_len (icm->client_state_by_sw_if_index))
199 ia_na_client_state = 0;
202 &icm->client_state_by_sw_if_index[sw_if_index];
203 if (sw_if_index >= vec_len (pcm->client_state_by_sw_if_index))
206 pd_client_state = &pcm->client_state_by_sw_if_index[sw_if_index];
209 (dhcpv60->xid[0] << 16) + (dhcpv60->xid[1] << 8) +
211 if (ia_na_client_state && ia_na_client_state->transaction_id == xid)
213 else if (pd_client_state && pd_client_state->transaction_id == xid)
218 ("Received DHCPv6 message with wrong Transaction ID");
222 report.sw_if_index = sw_if_index;
223 report.msg_type = dhcpv60->msg_type;
224 report.server_index = ~0;
226 switch (dhcpv60->msg_type)
228 case DHCPV6_MSG_ADVERTISE:
229 case DHCPV6_MSG_REPLY:
230 option = (dhcpv6_option_t *) (dhcpv60 + 1);
231 while (options_length > 0)
234 ntohs (option->length) + sizeof (*option))
237 ("remaining payload length < option length (%d < %d)",
239 ntohs (option->length) + sizeof (*option));
242 u16 oo = ntohs (option->option);
243 if (oo == DHCPV6_OPTION_IA_NA || oo == DHCPV6_OPTION_IA_PD)
245 u8 discard_option = 0;
246 dhcpv6_ia_header_t *ia_header = (void *) option;
247 iaid = ntohl (ia_header->iaid);
248 u32 T1 = ntohl (ia_header->t1);
249 u32 T2 = ntohl (ia_header->t2);
250 if (iaid != DHCPV6_CLIENT_IAID)
252 if (T1 != 0 && T2 != 0 && T1 > T2)
259 dhcpv6_option_t *inner_option =
260 (void *) ia_header->data;
261 u16 inner_options_length =
262 ntohs (option->length) - (sizeof (*ia_header) -
263 sizeof (dhcpv6_option_t));
264 while (inner_options_length > 0)
266 u16 inner_oo = ntohs (inner_option->option);
269 else if (inner_oo == DHCPV6_OPTION_IAADDR)
271 dhcpv6_ia_opt_addr_t *iaaddr =
272 (void *) inner_option;
273 u32 n_addresses = vec_len (addresses);
274 vec_validate (addresses, n_addresses);
275 dhcp6_address_info_t *address_info =
276 &addresses[n_addresses];
277 address_info->preferred_time =
278 ntohl (iaaddr->preferred);
279 address_info->valid_time =
280 ntohl (iaaddr->valid);
281 address_info->address = iaaddr->addr;
283 else if (inner_oo == DHCPV6_OPTION_IAPREFIX)
285 dhcpv6_ia_opt_pd_t *iaprefix =
286 (void *) inner_option;
287 u32 n_prefixes = vec_len (prefixes);
288 vec_validate (prefixes, n_prefixes);
289 dhcp6_prefix_info_t *prefix_info =
290 &prefixes[n_prefixes];
291 prefix_info->preferred_time =
292 ntohl (iaprefix->preferred);
293 prefix_info->valid_time =
294 ntohl (iaprefix->valid);
295 prefix_info->prefix_length = iaprefix->prefix;
296 prefix_info->prefix = iaprefix->addr;
298 else if (inner_oo == DHCPV6_OPTION_STATUS_CODE)
300 dhcpv6_status_code_t *sc =
301 (void *) inner_option;
302 report.inner_status_code =
303 ntohs (sc->status_code);
305 inner_options_length -=
306 sizeof (*inner_option) +
307 ntohs (inner_option->length);
309 (void *) ((u8 *) inner_option +
310 sizeof (*inner_option) +
311 ntohs (inner_option->length));
314 else if (oo == DHCPV6_OPTION_CLIENTID)
316 if (client_id_present)
319 ("Duplicate Client ID in received DHVPv6 message");
324 u16 len = ntohs (option->length);
325 client_id_present = 1;
326 if (len != CLIENT_DUID_LENGTH ||
327 0 != memcmp (option->data,
328 client_duid.bin_string,
332 ("Unrecognized client DUID inside received DHVPv6 message");
337 else if (oo == DHCPV6_OPTION_SERVERID)
339 if (report.server_index != ~0)
342 ("Duplicate Server ID in received DHVPv6 message");
347 u16 ol = ntohs (option->length);
348 if (ol - 2 /* 2 byte DUID type code */ > 128)
351 ("Server DUID (without type code) is longer than 128 octets");
356 report.server_index =
357 server_index_get_or_create (option->data, ol);
361 else if (oo == DHCPV6_OPTION_PREFERENCE)
363 report.preference = option->data[0];
365 else if (oo == DHCPV6_OPTION_STATUS_CODE)
367 dhcpv6_status_code_t *sc = (void *) option;
368 report.status_code = ntohs (sc->status_code);
370 options_length -= sizeof (*option) + ntohs (option->length);
372 (void *) ((u8 *) option + sizeof (*option) +
373 ntohs (option->length));
376 if (!client_id_present)
379 ("Missing Client ID in received DHVPv6 message");
382 if (report.server_index == ~0)
385 ("Missing Server ID in received DHVPv6 message");
395 r.n_addresses = vec_len (addresses);
396 r.addresses = addresses;
397 dhcp6_publish_report (&r);
398 /* We just gave addresses to another process! */
405 r.n_prefixes = vec_len (prefixes);
406 r.prefixes = prefixes;
407 dhcp6_pd_publish_report (&r);
408 /* We just gave prefixes to another process! */
412 vec_free (addresses);
420 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
422 dhcpv6_client_trace_t *t =
423 vlib_add_trace (vm, node, b0, sizeof (*t));
426 /* verify speculative enqueue, maybe switch current next frame */
427 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
428 to_next, n_left_to_next,
432 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
435 return frame->n_vectors;
439 VLIB_REGISTER_NODE (dhcpv6_client_node, static) = {
440 .function = dhcpv6_client_node_fn,
441 .name = "dhcpv6-client",
442 .vector_size = sizeof (u32),
446 .n_next_nodes = DHCPV6_CLIENT_N_NEXT,
448 #define _(s,n) [DHCPV6_CLIENT_NEXT_##s] = n,
449 foreach_dhcpv6_client
453 .format_trace = format_dhcpv6_client_trace,
458 dhcp6_clients_enable_disable (u8 enable)
460 vlib_main_t *vm = vlib_get_main ();
464 if (client_duid.duid_type == 0)
465 generate_client_duid ();
466 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
467 dhcpv6_client_node.index, 0 /* is_ip6 */ );
470 udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
475 vl_api_dhcp6_clients_enable_disable_t_handler
476 (vl_api_dhcp6_clients_enable_disable_t * mp)
478 vl_api_dhcp6_clients_enable_disable_reply_t *rmp;
481 dhcp6_clients_enable_disable (mp->enable);
483 REPLY_MACRO (VL_API_DHCP6_CLIENTS_ENABLE_DISABLE_REPLY);
487 * fd.io coding-style-patch-verification: ON
490 * eval: (c-set-style "gnu")