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;
103 void (*fp) (u32, u32, u8 *, u8, u8, u8 *, u8 *, u8 *) = c->event_callback;
105 /* disable the feature */
106 vnet_feature_enable_disable ("ip4-unicast",
107 "ip4-dhcp-client-detect",
108 c->sw_if_index, 0 /* disable */ , 0, 0);
109 c->client_detect_feature_enabled = 0;
111 /* if renewing the lease, the address and route have already been added */
112 if (c->state == DHCP_BOUND)
115 /* add the address to the interface */
116 dhcp_client_acquire_address (dcm, c);
119 * Configure default IP route:
121 if (c->router_address.as_u32)
123 fib_prefix_t all_0s = {
125 .fp_addr.ip4.as_u32 = 0x0,
126 .fp_proto = FIB_PROTOCOL_IP4,
128 ip46_address_t nh = {
129 .ip4 = c->router_address,
133 fib_table_entry_path_add (
134 fib_table_get_index_for_sw_if_index (
142 ~0, 1, NULL, // no label stack
143 FIB_ROUTE_PATH_FLAG_NONE);
148 * Call the user's event callback to report DHCP information
151 (*fp) (c->client_index, /* clinet index */
152 c->pid, c->hostname, c->subnet_mask_width, 0, /* is_ipv6 */
153 (u8 *) & c->leased_address, /* host IP address */
154 (u8 *) & c->router_address, /* router IP address */
155 (u8 *) (c->l2_rewrite + 6)); /* host MAC address */
159 * dhcp_client_for_us - server-to-client callback.
160 * Called from proxy_node.c:dhcp_proxy_to_client_input().
161 * This function first decides that the packet in question is
162 * actually for the dhcp client code in case we're also acting as
163 * a dhcp proxy. Ay caramba, what a folly!
166 dhcp_client_for_us (u32 bi, vlib_buffer_t * b,
168 udp_header_t * udp, dhcp_header_t * dhcp)
170 dhcp_client_main_t *dcm = &dhcp_client_main;
171 vlib_main_t *vm = dcm->vlib_main;
174 f64 now = vlib_time_now (dcm->vlib_main);
175 u8 dhcp_message_type = 0;
179 * Doing dhcp client on this interface?
180 * Presumably we will always receive dhcp clnt for-us pkts on
181 * the interface that's asking for an address.
183 p = hash_get (dcm->client_by_sw_if_index,
184 vnet_buffer (b)->sw_if_index[VLIB_RX]);
188 c = pool_elt_at_index (dcm->clients, p[0]);
190 /* Mixing dhcp relay and dhcp proxy? DGMS... */
191 if (c->state == DHCP_BOUND && c->retry_count == 0)
194 /* Packet not for us? Turf it... */
195 if (memcmp (dhcp->client_hardware_address, c->client_hardware_address,
196 sizeof (c->client_hardware_address)))
198 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
199 DHCP_STAT_NOT_FOR_US, 1);
203 /* parse through the packet, learn what we can */
204 if (dhcp->your_ip_address.as_u32)
205 c->leased_address.as_u32 = dhcp->your_ip_address.as_u32;
207 c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32;
209 o = (dhcp_option_t *) dhcp->options;
211 while (o->option != 0xFF /* end of options */ &&
212 (u8 *) o < (b->data + b->current_data + b->current_length))
216 case 53: /* dhcp message type */
217 dhcp_message_type = o->data[0];
220 case 51: /* lease time */
222 u32 lease_time_in_seconds =
223 clib_host_to_net_u32 (o->data_as_u32[0]);
224 // for debug: lease_time_in_seconds = 20; /*$$$$*/
225 c->lease_expires = now + (f64) lease_time_in_seconds;
226 c->lease_lifetime = lease_time_in_seconds;
227 /* Set a sensible default, in case we don't get opt 58 */
228 c->lease_renewal_interval = lease_time_in_seconds / 2;
232 case 58: /* lease renew time in seconds */
234 u32 lease_renew_time_in_seconds =
235 clib_host_to_net_u32 (o->data_as_u32[0]);
236 c->lease_renewal_interval = lease_renew_time_in_seconds;
240 case 54: /* dhcp server address */
241 c->dhcp_server.as_u32 = o->data_as_u32[0];
244 case 1: /* subnet mask */
246 u32 subnet_mask = clib_host_to_net_u32 (o->data_as_u32[0]);
247 c->subnet_mask_width = count_set_bits (subnet_mask);
250 case 3: /* router address */
252 u32 router_address = o->data_as_u32[0];
253 c->router_address.as_u32 = router_address;
257 case 12: /* hostname */
259 /* Replace the existing hostname if necessary */
260 vec_free (c->hostname);
261 vec_validate (c->hostname, o->length - 1);
262 clib_memcpy (c->hostname, o->data, o->length);
266 /* $$$$ Your message in this space, parse more options */
271 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
277 if (dhcp_message_type != DHCP_PACKET_OFFER)
279 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
280 DHCP_STAT_NON_OFFER_DISCOVER, 1);
281 c->next_transmit = now + 5.0;
285 /* Received an offer, go send a request */
286 c->state = DHCP_REQUEST;
288 c->next_transmit = 0; /* send right now... */
289 /* Poke the client process, which will send the request */
290 uword client_id = c - dcm->clients;
291 vl_api_rpc_call_main_thread (dhcp_client_proc_callback,
292 (u8 *) & client_id, sizeof (uword));
297 if (dhcp_message_type == DHCP_PACKET_NAK)
299 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
301 /* Probably never happens in bound state, but anyhow... */
302 if (c->state == DHCP_BOUND)
304 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
305 (void *) &c->leased_address,
306 c->subnet_mask_width,
308 vnet_feature_enable_disable ("ip4-unicast",
309 "ip4-dhcp-client-detect",
310 c->sw_if_index, 1 /* enable */ ,
312 c->client_detect_feature_enabled = 1;
314 /* Wipe out any memory of the address we had... */
315 c->state = DHCP_DISCOVER;
316 c->next_transmit = now;
318 c->leased_address.as_u32 = 0;
319 c->subnet_mask_width = 0;
320 c->router_address.as_u32 = 0;
321 c->lease_renewal_interval = 0;
322 c->dhcp_server.as_u32 = 0;
326 if (dhcp_message_type != DHCP_PACKET_ACK &&
327 dhcp_message_type != DHCP_PACKET_OFFER)
329 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
330 DHCP_STAT_NON_OFFER_DISCOVER, 1);
331 clib_warning ("sw_if_index %d state %U message type %d",
332 c->sw_if_index, format_dhcp_client_state,
333 c->state, dhcp_message_type);
334 c->next_transmit = now + 5.0;
337 /* OK, we own the address (etc), add to the routing table(s) */
338 vl_api_rpc_call_main_thread (dhcp_client_addr_callback,
339 (u8 *) c, sizeof (*c));
341 c->state = DHCP_BOUND;
343 c->next_transmit = now + (f64) c->lease_renewal_interval;
344 c->lease_expires = now + (f64) c->lease_lifetime;
345 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
350 clib_warning ("client %d bogus state %d", c - dcm->clients, c->state);
354 /* drop the pkt, return 1 */
355 vlib_buffer_free (vm, &bi, 1);
360 send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c,
361 dhcp_packet_type_t type, int is_broadcast)
363 vlib_main_t *vm = dcm->vlib_main;
364 vnet_main_t *vnm = dcm->vnet_main;
365 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index);
366 vnet_sw_interface_t *sup_sw
367 = vnet_get_sup_sw_interface (vnm, c->sw_if_index);
368 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, c->sw_if_index);
377 u16 udp_length, ip_length;
380 /* Interface(s) down? */
381 if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
383 if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
385 if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
388 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
390 clib_warning ("buffer allocation failure");
391 c->next_transmit = 0;
395 /* Build a dhcpv4 pkt from whole cloth */
396 b = vlib_get_buffer (vm, bi);
398 ASSERT (b->current_data == 0);
400 vnet_buffer (b)->sw_if_index[VLIB_RX] = c->sw_if_index;
403 f = vlib_get_frame_to_node (vm, hw->output_node_index);
404 vnet_buffer (b)->sw_if_index[VLIB_TX] = c->sw_if_index;
405 clib_memcpy (b->data, c->l2_rewrite, vec_len (c->l2_rewrite));
407 (((u8 *) vlib_buffer_get_current (b)) + vec_len (c->l2_rewrite));
411 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
412 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; /* use interface VRF */
413 ip = vlib_buffer_get_current (b);
416 /* Enqueue the packet right now */
417 to_next = vlib_frame_vector_args (f);
422 vlib_put_frame_to_node (vm, hw->output_node_index, f);
424 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
426 udp = (udp_header_t *) (ip + 1);
427 dhcp = (dhcp_header_t *) (udp + 1);
429 /* $$$ optimize, maybe */
430 memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp));
432 ip->ip_version_and_header_length = 0x45;
434 ip->protocol = IP_PROTOCOL_UDP;
438 /* src = 0.0.0.0, dst = 255.255.255.255 */
439 ip->dst_address.as_u32 = ~0;
443 /* Renewing an active lease, plain old ip4 src/dst */
444 ip->src_address.as_u32 = c->leased_address.as_u32;
445 ip->dst_address.as_u32 = c->dhcp_server.as_u32;
448 udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client);
449 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server);
451 /* Send the interface MAC address */
452 clib_memcpy (dhcp->client_hardware_address, c->l2_rewrite + 6, 6);
454 /* And remember it for rx-packet-for-us checking */
455 clib_memcpy (c->client_hardware_address, dhcp->client_hardware_address,
456 sizeof (c->client_hardware_address));
458 /* Lease renewal, set up client_ip_address */
459 if (is_broadcast == 0)
460 dhcp->client_ip_address.as_u32 = c->leased_address.as_u32;
462 dhcp->opcode = 1; /* request, all we send */
463 dhcp->hardware_type = 1; /* ethernet */
464 dhcp->hardware_address_length = 6;
465 dhcp->transaction_identifier = c->transaction_id;
467 clib_host_to_net_u16 (is_broadcast && c->set_broadcast_flag ?
468 DHCP_FLAG_BROADCAST : 0);
469 dhcp->magic_cookie.as_u32 = DHCP_MAGIC;
471 o = (dhcp_option_t *) dhcp->options;
473 /* Send option 53, the DHCP message type */
474 o->option = DHCP_PACKET_OPTION_MSG_TYPE;
477 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
479 /* Send option 57, max msg length */
480 if (0 /* not needed, apparently */ )
485 u16 *o2 = (u16 *) o->data;
486 *o2 = clib_host_to_net_u16 (1152);
487 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
492 * If server ip address is available with non-zero value,
493 * option 54 (DHCP Server Identifier) is sent.
495 if (c->dhcp_server.as_u32)
499 clib_memcpy (o->data, &c->dhcp_server.as_u32, 4);
500 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
503 /* send option 50, requested IP address */
504 if (c->leased_address.as_u32)
508 clib_memcpy (o->data, &c->leased_address.as_u32, 4);
509 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
512 /* send option 12, host name */
513 if (vec_len (c->hostname))
516 o->length = vec_len (c->hostname);
517 clib_memcpy (o->data, c->hostname, vec_len (c->hostname));
518 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
521 /* send option 61, client_id */
522 if (vec_len (c->client_identifier))
525 o->length = vec_len (c->client_identifier);
526 clib_memcpy (o->data, c->client_identifier,
527 vec_len (c->client_identifier));
528 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
531 /* $$ maybe send the client s/w version if anyone cares */
534 * send option 55, parameter request list
535 * The current list - see below, matches the Linux dhcp client's list
536 * Any specific dhcp server config and/or dhcp server may or may
537 * not yield specific options.
540 o->length = vec_len (c->option_55_data);
541 clib_memcpy (o->data, c->option_55_data, vec_len (c->option_55_data));
542 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
549 b->current_length = ((u8 *) o) - b->data;
551 /* fix ip length, checksum and udp length */
552 ip_length = vlib_buffer_length_in_chain (vm, b);
554 ip_length -= vec_len (c->l2_rewrite);
556 ip->length = clib_host_to_net_u16 (ip_length);
557 ip->checksum = ip4_header_checksum (ip);
559 udp_length = ip_length - (sizeof (*ip));
560 udp->length = clib_host_to_net_u16 (udp_length);
564 #define _(a,b) case DHCP_PACKET_##a: {counter_index = DHCP_STAT_##a; break;}
565 foreach_dhcp_sent_packet_stat
568 counter_index = DHCP_STAT_UNKNOWN;
572 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
577 dhcp_discover_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
580 * State machine "DISCOVER" state. Send a dhcp discover packet,
581 * eventually back off the retry rate.
584 if (c->client_detect_feature_enabled == 0)
586 vnet_feature_enable_disable ("ip4-unicast",
587 "ip4-dhcp-client-detect",
588 c->sw_if_index, 1 /* enable */ , 0, 0);
589 c->client_detect_feature_enabled = 1;
592 send_dhcp_pkt (dcm, c, DHCP_PACKET_DISCOVER, 1 /* is_broadcast */ );
595 if (c->retry_count > 10)
596 c->next_transmit = now + 5.0;
598 c->next_transmit = now + 1.0;
603 dhcp_request_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
606 * State machine "REQUEST" state. Send a dhcp request packet,
607 * eventually drop back to the discover state.
609 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 1 /* is_broadcast */ );
612 if (c->retry_count > 7 /* lucky you */ )
614 c->state = DHCP_DISCOVER;
615 c->next_transmit = now;
619 c->next_transmit = now + 1.0;
624 dhcp_bound_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
627 * State machine "BOUND" state. Send a dhcp request packet to renew
629 * Eventually, when the lease expires, forget the dhcp data
630 * and go back to the stone age.
634 * We disable the client detect feature when we bind a
635 * DHCP address. Turn it back on again on first renew attempt.
636 * Otherwise, if the DHCP server replies we'll never see it.
638 if (c->client_detect_feature_enabled == 0)
640 vnet_feature_enable_disable ("ip4-unicast",
641 "ip4-dhcp-client-detect",
642 c->sw_if_index, 1 /* enable */ , 0, 0);
643 c->client_detect_feature_enabled = 1;
646 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 0 /* is_broadcast */ );
649 if (c->retry_count > 10)
650 c->next_transmit = now + 5.0;
652 c->next_transmit = now + 1.0;
654 if (now > c->lease_expires)
656 /* Remove the default route */
657 if (c->router_address.as_u32)
659 fib_prefix_t all_0s = {
661 .fp_addr.ip4.as_u32 = 0x0,
662 .fp_proto = FIB_PROTOCOL_IP4,
664 ip46_address_t nh = {
665 .ip4 = c->router_address,
668 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
669 (FIB_PROTOCOL_IP4, c->sw_if_index),
670 &all_0s, FIB_SOURCE_DHCP,
671 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
672 1, FIB_ROUTE_PATH_FLAG_NONE);
674 /* Remove the interface address */
675 dhcp_client_release_address (dcm, c);
676 c->state = DHCP_DISCOVER;
677 c->next_transmit = now;
679 /* Wipe out any memory of the address we had... */
680 c->leased_address.as_u32 = 0;
681 c->subnet_mask_width = 0;
682 c->router_address.as_u32 = 0;
683 c->lease_renewal_interval = 0;
684 c->dhcp_server.as_u32 = 0;
691 dhcp_client_sm (f64 now, f64 timeout, uword pool_index)
693 dhcp_client_main_t *dcm = &dhcp_client_main;
696 /* deleted, pooched, yadda yadda yadda */
697 if (pool_is_free_index (dcm->clients, pool_index))
700 c = pool_elt_at_index (dcm->clients, pool_index);
702 /* Time for us to do something with this client? */
703 if (now < c->next_transmit)
709 case DHCP_DISCOVER: /* send a discover */
710 if (dhcp_discover_state (dcm, c, now))
714 case DHCP_REQUEST: /* send a request */
715 if (dhcp_request_state (dcm, c, now))
719 case DHCP_BOUND: /* bound, renew needed? */
720 if (dhcp_bound_state (dcm, c, now))
725 clib_warning ("dhcp client %d bogus state %d",
726 c - dcm->clients, c->state);
730 if (c->next_transmit < now + timeout)
731 return c->next_transmit - now;
737 dhcp_client_process (vlib_main_t * vm,
738 vlib_node_runtime_t * rt, vlib_frame_t * f)
743 uword *event_data = 0;
744 dhcp_client_main_t *dcm = &dhcp_client_main;
750 vlib_process_wait_for_event_or_clock (vm, timeout);
752 event_type = vlib_process_get_events (vm, &event_data);
754 now = vlib_time_now (vm);
758 case EVENT_DHCP_CLIENT_WAKEUP:
759 for (i = 0; i < vec_len (event_data); i++)
760 timeout = dhcp_client_sm (now, timeout, event_data[i]);
765 pool_foreach (c, dcm->clients,
767 timeout = dhcp_client_sm (now, timeout,
768 (uword) (c - dcm->clients));
771 if (pool_elts (dcm->clients) == 0)
776 vec_reset_length (event_data);
784 VLIB_REGISTER_NODE (dhcp_client_process_node,static) = {
785 .function = dhcp_client_process,
786 .type = VLIB_NODE_TYPE_PROCESS,
787 .name = "dhcp-client-process",
788 .process_log2_n_stack_bytes = 16,
789 .n_errors = ARRAY_LEN(dhcp_client_process_stat_strings),
790 .error_strings = dhcp_client_process_stat_strings,
795 format_dhcp_client_state (u8 * s, va_list * va)
797 dhcp_client_state_t state = va_arg (*va, dhcp_client_state_t);
798 char *str = "BOGUS!";
806 foreach_dhcp_client_state;
812 s = format (s, "%s", str);
817 format_dhcp_client (u8 * s, va_list * va)
819 dhcp_client_main_t *dcm = va_arg (*va, dhcp_client_main_t *);
820 dhcp_client_t *c = va_arg (*va, dhcp_client_t *);
821 int verbose = va_arg (*va, int);
823 s = format (s, "[%d] %U state %U ", c - dcm->clients,
824 format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index,
825 format_dhcp_client_state, c->state);
827 if (c->leased_address.as_u32)
828 s = format (s, "addr %U/%d gw %U\n",
829 format_ip4_address, &c->leased_address,
830 c->subnet_mask_width, format_ip4_address, &c->router_address);
832 s = format (s, "no address\n");
836 s = format (s, "retry count %d, next xmt %.2f",
837 c->retry_count, c->next_transmit);
842 static clib_error_t *
843 show_dhcp_client_command_fn (vlib_main_t * vm,
844 unformat_input_t * input,
845 vlib_cli_command_t * cmd)
847 dhcp_client_main_t *dcm = &dhcp_client_main;
850 u32 sw_if_index = ~0;
853 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
855 if (unformat (input, "intfc %U",
856 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
858 else if (unformat (input, "verbose"))
864 if (sw_if_index != ~0)
866 p = hash_get (dcm->client_by_sw_if_index, sw_if_index);
868 return clib_error_return (0, "dhcp client not configured");
869 c = pool_elt_at_index (dcm->clients, p[0]);
870 vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
875 pool_foreach (c, dcm->clients,
877 vlib_cli_output (vm, "%U",
878 format_dhcp_client, dcm,
887 VLIB_CLI_COMMAND (show_dhcp_client_command, static) = {
888 .path = "show dhcp client",
889 .short_help = "show dhcp client [intfc <intfc>][verbose]",
890 .function = show_dhcp_client_command_fn,
896 dhcp_client_add_del (dhcp_client_add_del_args_t * a)
898 dhcp_client_main_t *dcm = &dhcp_client_main;
899 vlib_main_t *vm = dcm->vlib_main;
902 fib_prefix_t all_0s = {
904 .fp_addr.ip4.as_u32 = 0x0,
905 .fp_proto = FIB_PROTOCOL_IP4,
908 p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index);
910 if ((p && a->is_add) || (!p && a->is_add == 0))
911 return VNET_API_ERROR_INVALID_VALUE;
915 pool_get (dcm->clients, c);
916 memset (c, 0, sizeof (*c));
917 c->state = DHCP_DISCOVER;
918 c->sw_if_index = a->sw_if_index;
919 c->client_index = a->client_index;
921 c->event_callback = a->event_callback;
922 c->option_55_data = a->option_55_data;
923 c->hostname = a->hostname;
924 c->client_identifier = a->client_identifier;
925 c->set_broadcast_flag = a->set_broadcast_flag;
928 c->transaction_id = random_u32 (&dcm->seed);
930 while (c->transaction_id == 0);
931 set_l2_rewrite (dcm, c);
932 hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients);
935 * In order to accept any OFFER, whether broadcasted or unicasted, we
936 * need to configure the dhcp-client-detect feature as an input feature
937 * so the DHCP OFFER is sent to the ip4-local node. Without this a
938 * broadcasted OFFER hits the 255.255.255.255/32 address and a unicast
939 * hits 0.0.0.0/0 both of which default to drop and the latter may forward
940 * of box - not what we want. Nor to we want to change these route for
941 * all interfaces in this table
943 vnet_feature_enable_disable ("ip4-unicast",
944 "ip4-dhcp-client-detect",
945 c->sw_if_index, 1 /* enable */ , 0, 0);
946 c->client_detect_feature_enabled = 1;
948 vlib_process_signal_event (vm, dhcp_client_process_node.index,
949 EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
953 c = pool_elt_at_index (dcm->clients, p[0]);
955 if (c->router_address.as_u32)
957 ip46_address_t nh = {
958 .ip4 = c->router_address,
961 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
962 (FIB_PROTOCOL_IP4, c->sw_if_index),
963 &all_0s, FIB_SOURCE_DHCP,
964 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
965 1, FIB_ROUTE_PATH_FLAG_NONE);
967 dhcp_client_release_address (dcm, c);
969 vec_free (c->option_55_data);
970 vec_free (c->hostname);
971 vec_free (c->client_identifier);
972 vec_free (c->l2_rewrite);
973 hash_unset (dcm->client_by_sw_if_index, c->sw_if_index);
974 pool_put (dcm->clients, c);
980 dhcp_client_config (vlib_main_t * vm,
986 void *event_callback, u8 set_broadcast_flag, u32 pid)
988 dhcp_client_add_del_args_t _a, *a = &_a;
991 memset (a, 0, sizeof (*a));
993 a->sw_if_index = sw_if_index;
994 a->client_index = client_index;
996 a->event_callback = event_callback;
997 a->set_broadcast_flag = set_broadcast_flag;
998 vec_validate (a->hostname, strlen ((char *) hostname) - 1);
999 strncpy ((char *) a->hostname, (char *) hostname, vec_len (a->hostname));
1000 vec_validate (a->client_identifier, strlen ((char *) client_id) - 1);
1001 strncpy ((char *) a->client_identifier, (char *) client_id,
1002 vec_len (a->client_identifier));
1005 * Option 55 request list. These data precisely match
1006 * the Ubuntu dhcp client. YMMV.
1010 vec_add1 (a->option_55_data, 1);
1011 /* Broadcast address */
1012 vec_add1 (a->option_55_data, 28);
1014 vec_add1 (a->option_55_data, 2);
1016 vec_add1 (a->option_55_data, 3);
1018 vec_add1 (a->option_55_data, 15);
1020 vec_add1 (a->option_55_data, 6);
1022 vec_add1 (a->option_55_data, 119);
1024 vec_add1 (a->option_55_data, 12);
1025 /* NetBIOS name server */
1026 vec_add1 (a->option_55_data, 44);
1028 vec_add1 (a->option_55_data, 47);
1030 vec_add1 (a->option_55_data, 26);
1031 /* Classless static route */
1032 vec_add1 (a->option_55_data, 121);
1034 vec_add1 (a->option_55_data, 42);
1036 rv = dhcp_client_add_del (a);
1043 case VNET_API_ERROR_INVALID_VALUE:
1045 vec_free (a->hostname);
1046 vec_free (a->client_identifier);
1047 vec_free (a->option_55_data);
1050 clib_warning ("dhcp client already enabled on intf_idx %d",
1053 clib_warning ("dhcp client not enabled on on intf_idx %d",
1058 clib_warning ("dhcp_client_add_del returned %d", rv);
1064 static clib_error_t *
1065 dhcp_client_set_command_fn (vlib_main_t * vm,
1066 unformat_input_t * input,
1067 vlib_cli_command_t * cmd)
1070 dhcp_client_main_t *dcm = &dhcp_client_main;
1073 u8 sw_if_index_set = 0;
1074 u8 set_broadcast_flag = 1;
1076 dhcp_client_add_del_args_t _a, *a = &_a;
1079 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1081 if (unformat (input, "intfc %U",
1082 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
1083 sw_if_index_set = 1;
1084 else if (unformat (input, "hostname %v", &hostname))
1086 else if (unformat (input, "del"))
1088 else if (unformat (input, "broadcast", &set_broadcast_flag))
1094 if (sw_if_index_set == 0)
1095 return clib_error_return (0, "interface not specified");
1097 memset (a, 0, sizeof (*a));
1099 a->sw_if_index = sw_if_index;
1100 a->hostname = hostname;
1101 a->client_identifier = format (0, "vpe 1.0%c", 0);
1102 a->set_broadcast_flag = set_broadcast_flag;
1105 * Option 55 request list. These data precisely match
1106 * the Ubuntu dhcp client. YMMV.
1110 vec_add1 (a->option_55_data, 1);
1111 /* Broadcast address */
1112 vec_add1 (a->option_55_data, 28);
1114 vec_add1 (a->option_55_data, 2);
1116 vec_add1 (a->option_55_data, 3);
1118 vec_add1 (a->option_55_data, 15);
1120 vec_add1 (a->option_55_data, 6);
1122 vec_add1 (a->option_55_data, 119);
1124 vec_add1 (a->option_55_data, 12);
1125 /* NetBIOS name server */
1126 vec_add1 (a->option_55_data, 44);
1128 vec_add1 (a->option_55_data, 47);
1130 vec_add1 (a->option_55_data, 26);
1131 /* Classless static route */
1132 vec_add1 (a->option_55_data, 121);
1134 vec_add1 (a->option_55_data, 42);
1136 rv = dhcp_client_add_del (a);
1143 case VNET_API_ERROR_INVALID_VALUE:
1145 vec_free (a->hostname);
1146 vec_free (a->client_identifier);
1147 vec_free (a->option_55_data);
1149 return clib_error_return (0, "dhcp client already enabled on %U",
1150 format_vnet_sw_if_index_name,
1151 dcm->vnet_main, sw_if_index);
1153 return clib_error_return (0, "dhcp client not enabled on %U",
1154 format_vnet_sw_if_index_name,
1155 dcm->vnet_main, sw_if_index);
1159 vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv);
1166 VLIB_CLI_COMMAND (dhcp_client_set_command, static) = {
1167 .path = "set dhcp client",
1168 .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]",
1169 .function = dhcp_client_set_command_fn,
1173 static clib_error_t *
1174 dhcp_client_init (vlib_main_t * vm)
1176 dhcp_client_main_t *dcm = &dhcp_client_main;
1178 dcm->vlib_main = vm;
1179 dcm->vnet_main = vnet_get_main ();
1180 dcm->seed = 0xdeaddabe;
1184 VLIB_INIT_FUNCTION (dhcp_client_init);
1187 * fd.io coding-style-patch-verification: ON
1190 * eval: (c-set-style "gnu")