2 * Copyright (c) 2015 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.
15 #include <vlib/vlib.h>
16 #include <vnet/dhcp/client.h>
17 #include <vnet/dhcp/dhcp_proxy.h>
18 #include <vnet/fib/fib_table.h>
20 dhcp_client_main_t dhcp_client_main;
21 static u8 *format_dhcp_client_state (u8 * s, va_list * va);
22 static vlib_node_registration_t dhcp_client_process_node;
24 #define foreach_dhcp_sent_packet_stat \
25 _(DISCOVER, "DHCP discover packets sent") \
26 _(OFFER, "DHCP offer packets sent") \
27 _(REQUEST, "DHCP request packets sent") \
28 _(ACK, "DHCP ack packets sent")
30 #define foreach_dhcp_error_counter \
31 _(NOT_FOR_US, "DHCP packets for other hosts, dropped") \
32 _(NAK, "DHCP nak packets received") \
33 _(NON_OFFER_DISCOVER, "DHCP non-offer packets in discover state") \
34 _(ODDBALL, "DHCP non-ack, non-offer packets received") \
35 _(BOUND, "DHCP bind success")
39 #define _(sym,str) DHCP_STAT_##sym,
40 foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter
46 static char *dhcp_client_process_stat_strings[] = {
47 #define _(sym,string) string,
48 foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter
50 "DHCP unknown packets sent",
55 dhcp_client_acquire_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
58 * Install any/all info gleaned from dhcp, right here
60 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
61 (void *) &c->leased_address,
62 c->subnet_mask_width, 0 /*is_del */ );
66 dhcp_client_release_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
69 * Remove any/all info gleaned from dhcp, right here. Caller(s)
70 * have not wiped out the info yet.
73 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
74 (void *) &c->leased_address,
75 c->subnet_mask_width, 1 /*is_del */ );
79 set_l2_rewrite (dhcp_client_main_t * dcm, dhcp_client_t * c)
81 /* Acquire the L2 rewrite string for the indicated sw_if_index */
82 c->l2_rewrite = vnet_build_rewrite_for_sw_interface (dcm->vnet_main,
88 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
91 dhcp_client_proc_callback (uword * client_index)
93 vlib_main_t *vm = vlib_get_main ();
94 ASSERT (vlib_get_thread_index () == 0);
95 vlib_process_signal_event (vm, dhcp_client_process_node.index,
96 EVENT_DHCP_CLIENT_WAKEUP, *client_index);
100 dhcp_client_addr_callback (dhcp_client_t * c)
102 dhcp_client_main_t *dcm = &dhcp_client_main;
104 /* disable the feature */
105 vnet_feature_enable_disable ("ip4-unicast",
106 "ip4-dhcp-client-detect",
107 c->sw_if_index, 0 /* disable */ , 0, 0);
108 c->client_detect_feature_enabled = 0;
110 /* if renewing the lease, the address and route have already been added */
111 if (c->state == DHCP_BOUND)
114 /* add the address to the interface */
115 dhcp_client_acquire_address (dcm, c);
118 * Configure default IP route:
120 if (c->router_address.as_u32)
122 fib_prefix_t all_0s = {
124 .fp_addr.ip4.as_u32 = 0x0,
125 .fp_proto = FIB_PROTOCOL_IP4,
127 ip46_address_t nh = {
128 .ip4 = c->router_address,
132 fib_table_entry_path_add (
133 fib_table_get_index_for_sw_if_index (
141 ~0, 1, NULL, // no label stack
142 FIB_ROUTE_PATH_FLAG_NONE);
147 * Call the user's event callback to report DHCP information
149 if (c->event_callback)
150 c->event_callback (c->client_index, c);
154 * dhcp_client_for_us - server-to-client callback.
155 * Called from proxy_node.c:dhcp_proxy_to_client_input().
156 * This function first decides that the packet in question is
157 * actually for the dhcp client code in case we're also acting as
158 * a dhcp proxy. Ay caramba, what a folly!
161 dhcp_client_for_us (u32 bi, vlib_buffer_t * b,
163 udp_header_t * udp, dhcp_header_t * dhcp)
165 dhcp_client_main_t *dcm = &dhcp_client_main;
166 vlib_main_t *vm = dcm->vlib_main;
169 f64 now = vlib_time_now (dcm->vlib_main);
170 u8 dhcp_message_type = 0;
174 * Doing dhcp client on this interface?
175 * Presumably we will always receive dhcp clnt for-us pkts on
176 * the interface that's asking for an address.
178 p = hash_get (dcm->client_by_sw_if_index,
179 vnet_buffer (b)->sw_if_index[VLIB_RX]);
183 c = pool_elt_at_index (dcm->clients, p[0]);
185 /* Mixing dhcp relay and dhcp proxy? DGMS... */
186 if (c->state == DHCP_BOUND && c->retry_count == 0)
189 /* Packet not for us? Turf it... */
190 if (memcmp (dhcp->client_hardware_address, c->client_hardware_address,
191 sizeof (c->client_hardware_address)))
193 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
194 DHCP_STAT_NOT_FOR_US, 1);
198 /* parse through the packet, learn what we can */
199 if (dhcp->your_ip_address.as_u32)
200 c->leased_address.as_u32 = dhcp->your_ip_address.as_u32;
202 c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32;
204 o = (dhcp_option_t *) dhcp->options;
206 while (o->option != 0xFF /* end of options */ &&
207 (u8 *) o < (b->data + b->current_data + b->current_length))
211 case 53: /* dhcp message type */
212 dhcp_message_type = o->data[0];
215 case 51: /* lease time */
217 u32 lease_time_in_seconds =
218 clib_host_to_net_u32 (o->data_as_u32[0]);
219 // for debug: lease_time_in_seconds = 20; /*$$$$*/
220 c->lease_expires = now + (f64) lease_time_in_seconds;
221 c->lease_lifetime = lease_time_in_seconds;
222 /* Set a sensible default, in case we don't get opt 58 */
223 c->lease_renewal_interval = lease_time_in_seconds / 2;
227 case 58: /* lease renew time in seconds */
229 u32 lease_renew_time_in_seconds =
230 clib_host_to_net_u32 (o->data_as_u32[0]);
231 c->lease_renewal_interval = lease_renew_time_in_seconds;
235 case 54: /* dhcp server address */
236 c->dhcp_server.as_u32 = o->data_as_u32[0];
239 case 1: /* subnet mask */
241 u32 subnet_mask = clib_host_to_net_u32 (o->data_as_u32[0]);
242 c->subnet_mask_width = count_set_bits (subnet_mask);
245 case 3: /* router address */
247 u32 router_address = o->data_as_u32[0];
248 c->router_address.as_u32 = router_address;
252 case 12: /* hostname */
254 /* Replace the existing hostname if necessary */
255 vec_free (c->hostname);
256 vec_validate (c->hostname, o->length - 1);
257 clib_memcpy (c->hostname, o->data, o->length);
261 /* $$$$ Your message in this space, parse more options */
266 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
272 if (dhcp_message_type != DHCP_PACKET_OFFER)
274 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
275 DHCP_STAT_NON_OFFER_DISCOVER, 1);
276 c->next_transmit = now + 5.0;
280 /* Received an offer, go send a request */
281 c->state = DHCP_REQUEST;
283 c->next_transmit = 0; /* send right now... */
284 /* Poke the client process, which will send the request */
285 uword client_id = c - dcm->clients;
286 vl_api_rpc_call_main_thread (dhcp_client_proc_callback,
287 (u8 *) & client_id, sizeof (uword));
292 if (dhcp_message_type == DHCP_PACKET_NAK)
294 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
296 /* Probably never happens in bound state, but anyhow... */
297 if (c->state == DHCP_BOUND)
299 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
300 (void *) &c->leased_address,
301 c->subnet_mask_width,
303 vnet_feature_enable_disable ("ip4-unicast",
304 "ip4-dhcp-client-detect",
305 c->sw_if_index, 1 /* enable */ ,
307 c->client_detect_feature_enabled = 1;
309 /* Wipe out any memory of the address we had... */
310 c->state = DHCP_DISCOVER;
311 c->next_transmit = now;
313 c->leased_address.as_u32 = 0;
314 c->subnet_mask_width = 0;
315 c->router_address.as_u32 = 0;
316 c->lease_renewal_interval = 0;
317 c->dhcp_server.as_u32 = 0;
321 if (dhcp_message_type != DHCP_PACKET_ACK &&
322 dhcp_message_type != DHCP_PACKET_OFFER)
324 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
325 DHCP_STAT_NON_OFFER_DISCOVER, 1);
326 clib_warning ("sw_if_index %d state %U message type %d",
327 c->sw_if_index, format_dhcp_client_state,
328 c->state, dhcp_message_type);
329 c->next_transmit = now + 5.0;
332 /* OK, we own the address (etc), add to the routing table(s) */
333 vl_api_rpc_call_main_thread (dhcp_client_addr_callback,
334 (u8 *) c, sizeof (*c));
336 c->state = DHCP_BOUND;
338 c->next_transmit = now + (f64) c->lease_renewal_interval;
339 c->lease_expires = now + (f64) c->lease_lifetime;
340 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
345 clib_warning ("client %d bogus state %d", c - dcm->clients, c->state);
349 /* drop the pkt, return 1 */
350 vlib_buffer_free (vm, &bi, 1);
355 send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c,
356 dhcp_packet_type_t type, int is_broadcast)
358 vlib_main_t *vm = dcm->vlib_main;
359 vnet_main_t *vnm = dcm->vnet_main;
360 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index);
361 vnet_sw_interface_t *sup_sw
362 = vnet_get_sup_sw_interface (vnm, c->sw_if_index);
363 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, c->sw_if_index);
372 u16 udp_length, ip_length;
375 /* Interface(s) down? */
376 if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
378 if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
380 if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
383 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
385 clib_warning ("buffer allocation failure");
386 c->next_transmit = 0;
390 /* Build a dhcpv4 pkt from whole cloth */
391 b = vlib_get_buffer (vm, bi);
393 ASSERT (b->current_data == 0);
395 vnet_buffer (b)->sw_if_index[VLIB_RX] = c->sw_if_index;
398 f = vlib_get_frame_to_node (vm, hw->output_node_index);
399 vnet_buffer (b)->sw_if_index[VLIB_TX] = c->sw_if_index;
400 clib_memcpy (b->data, c->l2_rewrite, vec_len (c->l2_rewrite));
402 (((u8 *) vlib_buffer_get_current (b)) + vec_len (c->l2_rewrite));
406 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
407 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; /* use interface VRF */
408 ip = vlib_buffer_get_current (b);
411 /* Enqueue the packet right now */
412 to_next = vlib_frame_vector_args (f);
417 vlib_put_frame_to_node (vm, hw->output_node_index, f);
419 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
421 udp = (udp_header_t *) (ip + 1);
422 dhcp = (dhcp_header_t *) (udp + 1);
424 /* $$$ optimize, maybe */
425 clib_memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp));
427 ip->ip_version_and_header_length = 0x45;
429 ip->protocol = IP_PROTOCOL_UDP;
433 /* src = 0.0.0.0, dst = 255.255.255.255 */
434 ip->dst_address.as_u32 = ~0;
438 /* Renewing an active lease, plain old ip4 src/dst */
439 ip->src_address.as_u32 = c->leased_address.as_u32;
440 ip->dst_address.as_u32 = c->dhcp_server.as_u32;
443 udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client);
444 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server);
446 /* Send the interface MAC address */
447 clib_memcpy (dhcp->client_hardware_address, c->l2_rewrite + 6, 6);
449 /* And remember it for rx-packet-for-us checking */
450 clib_memcpy (c->client_hardware_address, dhcp->client_hardware_address,
451 sizeof (c->client_hardware_address));
453 /* Lease renewal, set up client_ip_address */
454 if (is_broadcast == 0)
455 dhcp->client_ip_address.as_u32 = c->leased_address.as_u32;
457 dhcp->opcode = 1; /* request, all we send */
458 dhcp->hardware_type = 1; /* ethernet */
459 dhcp->hardware_address_length = 6;
460 dhcp->transaction_identifier = c->transaction_id;
462 clib_host_to_net_u16 (is_broadcast && c->set_broadcast_flag ?
463 DHCP_FLAG_BROADCAST : 0);
464 dhcp->magic_cookie.as_u32 = DHCP_MAGIC;
466 o = (dhcp_option_t *) dhcp->options;
468 /* Send option 53, the DHCP message type */
469 o->option = DHCP_PACKET_OPTION_MSG_TYPE;
472 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
474 /* Send option 57, max msg length */
475 if (0 /* not needed, apparently */ )
480 u16 *o2 = (u16 *) o->data;
481 *o2 = clib_host_to_net_u16 (1152);
482 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
487 * If server ip address is available with non-zero value,
488 * option 54 (DHCP Server Identifier) is sent.
490 if (c->dhcp_server.as_u32)
494 clib_memcpy (o->data, &c->dhcp_server.as_u32, 4);
495 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
498 /* send option 50, requested IP address */
499 if (c->leased_address.as_u32)
503 clib_memcpy (o->data, &c->leased_address.as_u32, 4);
504 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
507 /* send option 12, host name */
508 if (vec_len (c->hostname))
511 o->length = vec_len (c->hostname);
512 clib_memcpy (o->data, c->hostname, vec_len (c->hostname));
513 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
516 /* send option 61, client_id */
517 if (vec_len (c->client_identifier))
520 o->length = vec_len (c->client_identifier);
521 clib_memcpy (o->data, c->client_identifier,
522 vec_len (c->client_identifier));
523 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
526 /* $$ maybe send the client s/w version if anyone cares */
529 * send option 55, parameter request list
530 * The current list - see below, matches the Linux dhcp client's list
531 * Any specific dhcp server config and/or dhcp server may or may
532 * not yield specific options.
535 o->length = vec_len (c->option_55_data);
536 clib_memcpy (o->data, c->option_55_data, vec_len (c->option_55_data));
537 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
544 b->current_length = ((u8 *) o) - b->data;
546 /* fix ip length, checksum and udp length */
547 ip_length = vlib_buffer_length_in_chain (vm, b);
549 ip_length -= vec_len (c->l2_rewrite);
551 ip->length = clib_host_to_net_u16 (ip_length);
552 ip->checksum = ip4_header_checksum (ip);
554 udp_length = ip_length - (sizeof (*ip));
555 udp->length = clib_host_to_net_u16 (udp_length);
559 #define _(a,b) case DHCP_PACKET_##a: {counter_index = DHCP_STAT_##a; break;}
560 foreach_dhcp_sent_packet_stat
563 counter_index = DHCP_STAT_UNKNOWN;
567 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
572 dhcp_discover_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
575 * State machine "DISCOVER" state. Send a dhcp discover packet,
576 * eventually back off the retry rate.
579 if (c->client_detect_feature_enabled == 0)
581 vnet_feature_enable_disable ("ip4-unicast",
582 "ip4-dhcp-client-detect",
583 c->sw_if_index, 1 /* enable */ , 0, 0);
584 c->client_detect_feature_enabled = 1;
587 send_dhcp_pkt (dcm, c, DHCP_PACKET_DISCOVER, 1 /* is_broadcast */ );
590 if (c->retry_count > 10)
591 c->next_transmit = now + 5.0;
593 c->next_transmit = now + 1.0;
598 dhcp_request_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
601 * State machine "REQUEST" state. Send a dhcp request packet,
602 * eventually drop back to the discover state.
604 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 1 /* is_broadcast */ );
607 if (c->retry_count > 7 /* lucky you */ )
609 c->state = DHCP_DISCOVER;
610 c->next_transmit = now;
614 c->next_transmit = now + 1.0;
619 dhcp_bound_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
622 * State machine "BOUND" state. Send a dhcp request packet to renew
624 * Eventually, when the lease expires, forget the dhcp data
625 * and go back to the stone age.
629 * We disable the client detect feature when we bind a
630 * DHCP address. Turn it back on again on first renew attempt.
631 * Otherwise, if the DHCP server replies we'll never see it.
633 if (c->client_detect_feature_enabled == 0)
635 vnet_feature_enable_disable ("ip4-unicast",
636 "ip4-dhcp-client-detect",
637 c->sw_if_index, 1 /* enable */ , 0, 0);
638 c->client_detect_feature_enabled = 1;
641 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 0 /* is_broadcast */ );
644 if (c->retry_count > 10)
645 c->next_transmit = now + 5.0;
647 c->next_transmit = now + 1.0;
649 if (now > c->lease_expires)
651 /* Remove the default route */
652 if (c->router_address.as_u32)
654 fib_prefix_t all_0s = {
656 .fp_addr.ip4.as_u32 = 0x0,
657 .fp_proto = FIB_PROTOCOL_IP4,
659 ip46_address_t nh = {
660 .ip4 = c->router_address,
663 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
664 (FIB_PROTOCOL_IP4, c->sw_if_index),
665 &all_0s, FIB_SOURCE_DHCP,
666 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
667 1, FIB_ROUTE_PATH_FLAG_NONE);
669 /* Remove the interface address */
670 dhcp_client_release_address (dcm, c);
671 c->state = DHCP_DISCOVER;
672 c->next_transmit = now;
674 /* Wipe out any memory of the address we had... */
675 c->leased_address.as_u32 = 0;
676 c->subnet_mask_width = 0;
677 c->router_address.as_u32 = 0;
678 c->lease_renewal_interval = 0;
679 c->dhcp_server.as_u32 = 0;
686 dhcp_client_sm (f64 now, f64 timeout, uword pool_index)
688 dhcp_client_main_t *dcm = &dhcp_client_main;
691 /* deleted, pooched, yadda yadda yadda */
692 if (pool_is_free_index (dcm->clients, pool_index))
695 c = pool_elt_at_index (dcm->clients, pool_index);
697 /* Time for us to do something with this client? */
698 if (now < c->next_transmit)
704 case DHCP_DISCOVER: /* send a discover */
705 if (dhcp_discover_state (dcm, c, now))
709 case DHCP_REQUEST: /* send a request */
710 if (dhcp_request_state (dcm, c, now))
714 case DHCP_BOUND: /* bound, renew needed? */
715 if (dhcp_bound_state (dcm, c, now))
720 clib_warning ("dhcp client %d bogus state %d",
721 c - dcm->clients, c->state);
725 if (c->next_transmit < now + timeout)
726 return c->next_transmit - now;
732 dhcp_client_process (vlib_main_t * vm,
733 vlib_node_runtime_t * rt, vlib_frame_t * f)
738 uword *event_data = 0;
739 dhcp_client_main_t *dcm = &dhcp_client_main;
745 vlib_process_wait_for_event_or_clock (vm, timeout);
747 event_type = vlib_process_get_events (vm, &event_data);
749 now = vlib_time_now (vm);
753 case EVENT_DHCP_CLIENT_WAKEUP:
754 for (i = 0; i < vec_len (event_data); i++)
755 timeout = dhcp_client_sm (now, timeout, event_data[i]);
760 pool_foreach (c, dcm->clients,
762 timeout = dhcp_client_sm (now, timeout,
763 (uword) (c - dcm->clients));
766 if (pool_elts (dcm->clients) == 0)
771 vec_reset_length (event_data);
779 VLIB_REGISTER_NODE (dhcp_client_process_node,static) = {
780 .function = dhcp_client_process,
781 .type = VLIB_NODE_TYPE_PROCESS,
782 .name = "dhcp-client-process",
783 .process_log2_n_stack_bytes = 16,
784 .n_errors = ARRAY_LEN(dhcp_client_process_stat_strings),
785 .error_strings = dhcp_client_process_stat_strings,
790 format_dhcp_client_state (u8 * s, va_list * va)
792 dhcp_client_state_t state = va_arg (*va, dhcp_client_state_t);
793 char *str = "BOGUS!";
801 foreach_dhcp_client_state;
807 s = format (s, "%s", str);
812 format_dhcp_client (u8 * s, va_list * va)
814 dhcp_client_main_t *dcm = va_arg (*va, dhcp_client_main_t *);
815 dhcp_client_t *c = va_arg (*va, dhcp_client_t *);
816 int verbose = va_arg (*va, int);
818 s = format (s, "[%d] %U state %U ", c - dcm->clients,
819 format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index,
820 format_dhcp_client_state, c->state);
822 if (c->leased_address.as_u32)
823 s = format (s, "addr %U/%d gw %U\n",
824 format_ip4_address, &c->leased_address,
825 c->subnet_mask_width, format_ip4_address, &c->router_address);
827 s = format (s, "no address\n");
831 s = format (s, "retry count %d, next xmt %.2f",
832 c->retry_count, c->next_transmit);
837 static clib_error_t *
838 show_dhcp_client_command_fn (vlib_main_t * vm,
839 unformat_input_t * input,
840 vlib_cli_command_t * cmd)
842 dhcp_client_main_t *dcm = &dhcp_client_main;
845 u32 sw_if_index = ~0;
848 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
850 if (unformat (input, "intfc %U",
851 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
853 else if (unformat (input, "verbose"))
859 if (sw_if_index != ~0)
861 p = hash_get (dcm->client_by_sw_if_index, sw_if_index);
863 return clib_error_return (0, "dhcp client not configured");
864 c = pool_elt_at_index (dcm->clients, p[0]);
865 vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
870 pool_foreach (c, dcm->clients,
872 vlib_cli_output (vm, "%U",
873 format_dhcp_client, dcm,
882 VLIB_CLI_COMMAND (show_dhcp_client_command, static) = {
883 .path = "show dhcp client",
884 .short_help = "show dhcp client [intfc <intfc>][verbose]",
885 .function = show_dhcp_client_command_fn,
891 dhcp_client_add_del (dhcp_client_add_del_args_t * a)
893 dhcp_client_main_t *dcm = &dhcp_client_main;
894 vlib_main_t *vm = dcm->vlib_main;
897 fib_prefix_t all_0s = {
899 .fp_addr.ip4.as_u32 = 0x0,
900 .fp_proto = FIB_PROTOCOL_IP4,
903 p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index);
905 if ((p && a->is_add) || (!p && a->is_add == 0))
906 return VNET_API_ERROR_INVALID_VALUE;
910 dhcp_maybe_register_udp_ports ();
911 pool_get (dcm->clients, c);
912 clib_memset (c, 0, sizeof (*c));
913 c->state = DHCP_DISCOVER;
914 c->sw_if_index = a->sw_if_index;
915 c->client_index = a->client_index;
917 c->event_callback = a->event_callback;
918 c->option_55_data = a->option_55_data;
919 c->hostname = a->hostname;
920 c->client_identifier = a->client_identifier;
921 c->set_broadcast_flag = a->set_broadcast_flag;
924 c->transaction_id = random_u32 (&dcm->seed);
926 while (c->transaction_id == 0);
927 set_l2_rewrite (dcm, c);
928 hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients);
931 * In order to accept any OFFER, whether broadcasted or unicasted, we
932 * need to configure the dhcp-client-detect feature as an input feature
933 * so the DHCP OFFER is sent to the ip4-local node. Without this a
934 * broadcasted OFFER hits the 255.255.255.255/32 address and a unicast
935 * hits 0.0.0.0/0 both of which default to drop and the latter may forward
936 * of box - not what we want. Nor to we want to change these route for
937 * all interfaces in this table
939 vnet_feature_enable_disable ("ip4-unicast",
940 "ip4-dhcp-client-detect",
941 c->sw_if_index, 1 /* enable */ , 0, 0);
942 c->client_detect_feature_enabled = 1;
944 vlib_process_signal_event (vm, dhcp_client_process_node.index,
945 EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
949 c = pool_elt_at_index (dcm->clients, p[0]);
951 if (c->router_address.as_u32)
953 ip46_address_t nh = {
954 .ip4 = c->router_address,
957 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
958 (FIB_PROTOCOL_IP4, c->sw_if_index),
959 &all_0s, FIB_SOURCE_DHCP,
960 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
961 1, FIB_ROUTE_PATH_FLAG_NONE);
963 dhcp_client_release_address (dcm, c);
965 vec_free (c->option_55_data);
966 vec_free (c->hostname);
967 vec_free (c->client_identifier);
968 vec_free (c->l2_rewrite);
969 hash_unset (dcm->client_by_sw_if_index, c->sw_if_index);
970 pool_put (dcm->clients, c);
976 dhcp_client_config (u32 is_add,
982 dhcp_event_cb_t event_callback,
983 u8 set_broadcast_flag, u32 pid)
985 dhcp_client_add_del_args_t _a, *a = &_a;
988 clib_memset (a, 0, sizeof (*a));
990 a->sw_if_index = sw_if_index;
991 a->client_index = client_index;
993 a->event_callback = event_callback;
994 a->set_broadcast_flag = set_broadcast_flag;
995 vec_validate (a->hostname, strlen ((char *) hostname) - 1);
996 strncpy ((char *) a->hostname, (char *) hostname, vec_len (a->hostname));
997 vec_validate (a->client_identifier, strlen ((char *) client_id) - 1);
998 strncpy ((char *) a->client_identifier, (char *) client_id,
999 vec_len (a->client_identifier));
1002 * Option 55 request list. These data precisely match
1003 * the Ubuntu dhcp client. YMMV.
1007 vec_add1 (a->option_55_data, 1);
1008 /* Broadcast address */
1009 vec_add1 (a->option_55_data, 28);
1011 vec_add1 (a->option_55_data, 2);
1013 vec_add1 (a->option_55_data, 3);
1015 vec_add1 (a->option_55_data, 15);
1017 vec_add1 (a->option_55_data, 6);
1019 vec_add1 (a->option_55_data, 119);
1021 vec_add1 (a->option_55_data, 12);
1022 /* NetBIOS name server */
1023 vec_add1 (a->option_55_data, 44);
1025 vec_add1 (a->option_55_data, 47);
1027 vec_add1 (a->option_55_data, 26);
1028 /* Classless static route */
1029 vec_add1 (a->option_55_data, 121);
1031 vec_add1 (a->option_55_data, 42);
1033 rv = dhcp_client_add_del (a);
1040 case VNET_API_ERROR_INVALID_VALUE:
1042 vec_free (a->hostname);
1043 vec_free (a->client_identifier);
1044 vec_free (a->option_55_data);
1047 clib_warning ("dhcp client already enabled on intf_idx %d",
1050 clib_warning ("dhcp client not enabled on on intf_idx %d",
1055 clib_warning ("dhcp_client_add_del returned %d", rv);
1062 dhcp_client_walk (dhcp_client_walk_cb_t cb, void *ctx)
1064 dhcp_client_main_t *dcm = &dhcp_client_main;
1068 pool_foreach (c, dcm->clients,
1077 static clib_error_t *
1078 dhcp_client_set_command_fn (vlib_main_t * vm,
1079 unformat_input_t * input,
1080 vlib_cli_command_t * cmd)
1083 dhcp_client_main_t *dcm = &dhcp_client_main;
1086 u8 sw_if_index_set = 0;
1087 u8 set_broadcast_flag = 1;
1089 dhcp_client_add_del_args_t _a, *a = &_a;
1092 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1094 if (unformat (input, "intfc %U",
1095 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
1096 sw_if_index_set = 1;
1097 else if (unformat (input, "hostname %v", &hostname))
1099 else if (unformat (input, "del"))
1101 else if (unformat (input, "broadcast", &set_broadcast_flag))
1107 if (sw_if_index_set == 0)
1108 return clib_error_return (0, "interface not specified");
1110 clib_memset (a, 0, sizeof (*a));
1112 a->sw_if_index = sw_if_index;
1113 a->hostname = hostname;
1114 a->client_identifier = format (0, "vpe 1.0%c", 0);
1115 a->set_broadcast_flag = set_broadcast_flag;
1118 * Option 55 request list. These data precisely match
1119 * the Ubuntu dhcp client. YMMV.
1123 vec_add1 (a->option_55_data, 1);
1124 /* Broadcast address */
1125 vec_add1 (a->option_55_data, 28);
1127 vec_add1 (a->option_55_data, 2);
1129 vec_add1 (a->option_55_data, 3);
1131 vec_add1 (a->option_55_data, 15);
1133 vec_add1 (a->option_55_data, 6);
1135 vec_add1 (a->option_55_data, 119);
1137 vec_add1 (a->option_55_data, 12);
1138 /* NetBIOS name server */
1139 vec_add1 (a->option_55_data, 44);
1141 vec_add1 (a->option_55_data, 47);
1143 vec_add1 (a->option_55_data, 26);
1144 /* Classless static route */
1145 vec_add1 (a->option_55_data, 121);
1147 vec_add1 (a->option_55_data, 42);
1149 rv = dhcp_client_add_del (a);
1156 case VNET_API_ERROR_INVALID_VALUE:
1158 vec_free (a->hostname);
1159 vec_free (a->client_identifier);
1160 vec_free (a->option_55_data);
1162 return clib_error_return (0, "dhcp client already enabled on %U",
1163 format_vnet_sw_if_index_name,
1164 dcm->vnet_main, sw_if_index);
1166 return clib_error_return (0, "dhcp client not enabled on %U",
1167 format_vnet_sw_if_index_name,
1168 dcm->vnet_main, sw_if_index);
1172 vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv);
1179 VLIB_CLI_COMMAND (dhcp_client_set_command, static) = {
1180 .path = "set dhcp client",
1181 .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]",
1182 .function = dhcp_client_set_command_fn,
1186 static clib_error_t *
1187 dhcp_client_init (vlib_main_t * vm)
1189 dhcp_client_main_t *dcm = &dhcp_client_main;
1191 dcm->vlib_main = vm;
1192 dcm->vnet_main = vnet_get_main ();
1193 dcm->seed = 0xdeaddabe;
1197 VLIB_INIT_FUNCTION (dhcp_client_init);
1200 * fd.io coding-style-patch-verification: ON
1203 * eval: (c-set-style "gnu")