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 /* add the advertised subnet and disable the feature */
106 dhcp_client_acquire_address (dcm, c);
107 vnet_feature_enable_disable ("ip4-unicast",
108 "ip4-dhcp-client-detect",
109 c->sw_if_index, 0 /* disable */ , 0, 0);
112 * Configure default IP route:
114 if (c->router_address.as_u32)
116 fib_prefix_t all_0s = {
118 .fp_addr.ip4.as_u32 = 0x0,
119 .fp_proto = FIB_PROTOCOL_IP4,
121 ip46_address_t nh = {
122 .ip4 = c->router_address,
126 fib_table_entry_path_add (
127 fib_table_get_index_for_sw_if_index (
135 ~0, 1, NULL, // no label stack
136 FIB_ROUTE_PATH_FLAG_NONE);
141 * Call the user's event callback to report DHCP information
144 (*fp) (c->client_index, /* clinet index */
145 c->pid, c->hostname, c->subnet_mask_width, 0, /* is_ipv6 */
146 (u8 *) & c->leased_address, /* host IP address */
147 (u8 *) & c->router_address, /* router IP address */
148 (u8 *) (c->l2_rewrite + 6)); /* host MAC address */
152 * dhcp_client_for_us - server-to-client callback.
153 * Called from proxy_node.c:dhcp_proxy_to_client_input().
154 * This function first decides that the packet in question is
155 * actually for the dhcp client code in case we're also acting as
156 * a dhcp proxy. Ay caramba, what a folly!
159 dhcp_client_for_us (u32 bi, vlib_buffer_t * b,
161 udp_header_t * udp, dhcp_header_t * dhcp)
163 dhcp_client_main_t *dcm = &dhcp_client_main;
164 vlib_main_t *vm = dcm->vlib_main;
167 f64 now = vlib_time_now (dcm->vlib_main);
168 u8 dhcp_message_type = 0;
172 * Doing dhcp client on this interface?
173 * Presumably we will always receive dhcp clnt for-us pkts on
174 * the interface that's asking for an address.
176 p = hash_get (dcm->client_by_sw_if_index,
177 vnet_buffer (b)->sw_if_index[VLIB_RX]);
181 c = pool_elt_at_index (dcm->clients, p[0]);
183 /* Mixing dhcp relay and dhcp proxy? DGMS... */
184 if (c->state == DHCP_BOUND && c->retry_count == 0)
187 /* Packet not for us? Turf it... */
188 if (memcmp (dhcp->client_hardware_address, c->client_hardware_address,
189 sizeof (c->client_hardware_address)))
191 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
192 DHCP_STAT_NOT_FOR_US, 1);
196 /* parse through the packet, learn what we can */
197 if (dhcp->your_ip_address.as_u32)
198 c->leased_address.as_u32 = dhcp->your_ip_address.as_u32;
200 c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32;
202 o = (dhcp_option_t *) dhcp->options;
204 while (o->option != 0xFF /* end of options */ &&
205 (u8 *) o < (b->data + b->current_data + b->current_length))
209 case 53: /* dhcp message type */
210 dhcp_message_type = o->data[0];
213 case 51: /* lease time */
215 u32 lease_time_in_seconds =
216 clib_host_to_net_u32 (o->data_as_u32[0]);
217 c->lease_expires = now + (f64) lease_time_in_seconds;
218 c->lease_lifetime = lease_time_in_seconds;
219 /* Set a sensible default, in case we don't get opt 58 */
220 c->lease_renewal_interval = lease_time_in_seconds / 2;
224 case 58: /* lease renew time in seconds */
226 u32 lease_renew_time_in_seconds =
227 clib_host_to_net_u32 (o->data_as_u32[0]);
228 c->lease_renewal_interval = lease_renew_time_in_seconds;
232 case 54: /* dhcp server address */
233 c->dhcp_server.as_u32 = o->data_as_u32[0];
236 case 1: /* subnet mask */
238 u32 subnet_mask = clib_host_to_net_u32 (o->data_as_u32[0]);
239 c->subnet_mask_width = count_set_bits (subnet_mask);
242 case 3: /* router address */
244 u32 router_address = o->data_as_u32[0];
245 c->router_address.as_u32 = router_address;
249 case 12: /* hostname */
251 /* Replace the existing hostname if necessary */
252 vec_free (c->hostname);
253 vec_validate (c->hostname, o->length - 1);
254 clib_memcpy (c->hostname, o->data, o->length);
258 /* $$$$ Your message in this space, parse more options */
263 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
269 if (dhcp_message_type != DHCP_PACKET_OFFER)
271 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
272 DHCP_STAT_NON_OFFER_DISCOVER, 1);
273 c->next_transmit = now + 5.0;
277 /* Received an offer, go send a request */
278 c->state = DHCP_REQUEST;
280 c->next_transmit = 0; /* send right now... */
281 /* Poke the client process, which will send the request */
282 uword client_id = c - dcm->clients;
283 vl_api_rpc_call_main_thread (dhcp_client_proc_callback,
284 (u8 *) & client_id, sizeof (uword));
289 if (dhcp_message_type == DHCP_PACKET_NAK)
291 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
293 /* Probably never happens in bound state, but anyhow... */
294 if (c->state == DHCP_BOUND)
296 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
297 (void *) &c->leased_address,
298 c->subnet_mask_width,
300 vnet_feature_enable_disable ("ip4-unicast",
301 "ip4-dhcp-client-detect",
302 c->sw_if_index, 1 /* enable */ ,
305 /* Wipe out any memory of the address we had... */
306 c->state = DHCP_DISCOVER;
307 c->next_transmit = now;
309 c->leased_address.as_u32 = 0;
310 c->subnet_mask_width = 0;
311 c->router_address.as_u32 = 0;
312 c->lease_renewal_interval = 0;
313 c->dhcp_server.as_u32 = 0;
317 if (dhcp_message_type != DHCP_PACKET_ACK &&
318 dhcp_message_type != DHCP_PACKET_OFFER)
320 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
321 DHCP_STAT_NON_OFFER_DISCOVER, 1);
322 clib_warning ("sw_if_index %d state %U message type %d",
323 c->sw_if_index, format_dhcp_client_state,
324 c->state, dhcp_message_type);
325 c->next_transmit = now + 5.0;
328 /* OK, we own the address (etc), add to the routing table(s) */
329 if (c->state == DHCP_REQUEST)
330 vl_api_rpc_call_main_thread (dhcp_client_addr_callback,
331 (u8 *) c, sizeof (*c));
333 c->state = DHCP_BOUND;
335 c->next_transmit = now + (f64) c->lease_renewal_interval;
336 c->lease_expires = now + (f64) c->lease_lifetime;
337 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
342 clib_warning ("client %d bogus state %d", c - dcm->clients, c->state);
346 /* drop the pkt, return 1 */
347 vlib_buffer_free (vm, &bi, 1);
352 send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c,
353 dhcp_packet_type_t type, int is_broadcast)
355 vlib_main_t *vm = dcm->vlib_main;
356 vnet_main_t *vnm = dcm->vnet_main;
357 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index);
358 vnet_sw_interface_t *sup_sw
359 = vnet_get_sup_sw_interface (vnm, c->sw_if_index);
360 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, c->sw_if_index);
369 u16 udp_length, ip_length;
372 /* Interface(s) down? */
373 if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
375 if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
377 if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
380 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
382 clib_warning ("buffer allocation failure");
383 c->next_transmit = 0;
387 /* Build a dhcpv4 pkt from whole cloth */
388 b = vlib_get_buffer (vm, bi);
390 ASSERT (b->current_data == 0);
392 vnet_buffer (b)->sw_if_index[VLIB_RX] = c->sw_if_index;
395 f = vlib_get_frame_to_node (vm, hw->output_node_index);
396 vnet_buffer (b)->sw_if_index[VLIB_TX] = c->sw_if_index;
397 clib_memcpy (b->data, c->l2_rewrite, vec_len (c->l2_rewrite));
399 (((u8 *) vlib_buffer_get_current (b)) + vec_len (c->l2_rewrite));
403 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
404 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; /* use interface VRF */
405 ip = vlib_buffer_get_current (b);
408 /* Enqueue the packet right now */
409 to_next = vlib_frame_vector_args (f);
414 vlib_put_frame_to_node (vm, hw->output_node_index, f);
416 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
418 udp = (udp_header_t *) (ip + 1);
419 dhcp = (dhcp_header_t *) (udp + 1);
421 /* $$$ optimize, maybe */
422 memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp));
424 ip->ip_version_and_header_length = 0x45;
426 ip->protocol = IP_PROTOCOL_UDP;
430 /* src = 0.0.0.0, dst = 255.255.255.255 */
431 ip->dst_address.as_u32 = ~0;
435 /* Renewing an active lease, plain old ip4 src/dst */
436 ip->src_address.as_u32 = c->leased_address.as_u32;
437 ip->dst_address.as_u32 = c->dhcp_server.as_u32;
440 udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client);
441 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server);
443 /* Send the interface MAC address */
444 clib_memcpy (dhcp->client_hardware_address, c->l2_rewrite + 6, 6);
446 /* And remember it for rx-packet-for-us checking */
447 clib_memcpy (c->client_hardware_address, dhcp->client_hardware_address,
448 sizeof (c->client_hardware_address));
450 /* Lease renewal, set up client_ip_address */
451 if (is_broadcast == 0)
452 dhcp->client_ip_address.as_u32 = c->leased_address.as_u32;
454 dhcp->opcode = 1; /* request, all we send */
455 dhcp->hardware_type = 1; /* ethernet */
456 dhcp->hardware_address_length = 6;
457 dhcp->transaction_identifier = c->transaction_id;
459 clib_host_to_net_u16 (is_broadcast && c->set_broadcast_flag ?
460 DHCP_FLAG_BROADCAST : 0);
461 dhcp->magic_cookie.as_u32 = DHCP_MAGIC;
463 o = (dhcp_option_t *) dhcp->options;
465 /* Send option 53, the DHCP message type */
466 o->option = DHCP_PACKET_OPTION_MSG_TYPE;
469 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
471 /* Send option 57, max msg length */
472 if (0 /* not needed, apparently */ )
477 u16 *o2 = (u16 *) o->data;
478 *o2 = clib_host_to_net_u16 (1152);
479 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
484 * If server ip address is available with non-zero value,
485 * option 54 (DHCP Server Identifier) is sent.
487 if (c->dhcp_server.as_u32)
491 clib_memcpy (o->data, &c->dhcp_server.as_u32, 4);
492 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
495 /* send option 50, requested IP address */
496 if (c->leased_address.as_u32)
500 clib_memcpy (o->data, &c->leased_address.as_u32, 4);
501 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
504 /* send option 12, host name */
505 if (vec_len (c->hostname))
508 o->length = vec_len (c->hostname);
509 clib_memcpy (o->data, c->hostname, vec_len (c->hostname));
510 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
513 /* send option 61, client_id */
514 if (vec_len (c->client_identifier))
517 o->length = vec_len (c->client_identifier);
518 clib_memcpy (o->data, c->client_identifier,
519 vec_len (c->client_identifier));
520 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
523 /* $$ maybe send the client s/w version if anyone cares */
526 * send option 55, parameter request list
527 * The current list - see below, matches the Linux dhcp client's list
528 * Any specific dhcp server config and/or dhcp server may or may
529 * not yield specific options.
532 o->length = vec_len (c->option_55_data);
533 clib_memcpy (o->data, c->option_55_data, vec_len (c->option_55_data));
534 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
541 b->current_length = ((u8 *) o) - b->data;
543 /* fix ip length, checksum and udp length */
544 ip_length = vlib_buffer_length_in_chain (vm, b);
546 ip_length -= vec_len (c->l2_rewrite);
548 ip->length = clib_host_to_net_u16 (ip_length);
549 ip->checksum = ip4_header_checksum (ip);
551 udp_length = ip_length - (sizeof (*ip));
552 udp->length = clib_host_to_net_u16 (udp_length);
556 #define _(a,b) case DHCP_PACKET_##a: {counter_index = DHCP_STAT_##a; break;}
557 foreach_dhcp_sent_packet_stat
560 counter_index = DHCP_STAT_UNKNOWN;
564 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
569 dhcp_discover_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
572 * State machine "DISCOVER" state. Send a dhcp discover packet,
573 * eventually back off the retry rate.
575 send_dhcp_pkt (dcm, c, DHCP_PACKET_DISCOVER, 1 /* is_broadcast */ );
578 if (c->retry_count > 10)
579 c->next_transmit = now + 5.0;
581 c->next_transmit = now + 1.0;
586 dhcp_request_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
589 * State machine "REQUEST" state. Send a dhcp request packet,
590 * eventually drop back to the discover state.
592 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 1 /* is_broadcast */ );
595 if (c->retry_count > 7 /* lucky you */ )
597 c->state = DHCP_DISCOVER;
598 c->next_transmit = now;
602 c->next_transmit = now + 1.0;
607 dhcp_bound_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
610 * State machine "BOUND" state. Send a dhcp request packet,
611 * eventually, when the lease expires, forget the dhcp data
612 * and go back to the stone age.
614 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 0 /* is_broadcast */ );
617 if (c->retry_count > 10)
618 c->next_transmit = now + 5.0;
620 c->next_transmit = now + 1.0;
622 if (now > c->lease_expires)
624 /* Remove the default route */
625 if (c->router_address.as_u32)
627 fib_prefix_t all_0s = {
629 .fp_addr.ip4.as_u32 = 0x0,
630 .fp_proto = FIB_PROTOCOL_IP4,
632 ip46_address_t nh = {
633 .ip4 = c->router_address,
636 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
637 (FIB_PROTOCOL_IP4, c->sw_if_index),
638 &all_0s, FIB_SOURCE_DHCP,
639 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
640 1, FIB_ROUTE_PATH_FLAG_NONE);
642 /* Remove the interface address */
643 dhcp_client_release_address (dcm, c);
644 c->state = DHCP_DISCOVER;
645 c->next_transmit = now;
647 /* Wipe out any memory of the address we had... */
648 c->leased_address.as_u32 = 0;
649 c->subnet_mask_width = 0;
650 c->router_address.as_u32 = 0;
651 c->lease_renewal_interval = 0;
652 c->dhcp_server.as_u32 = 0;
654 * We disable the client detect feature when we bind a
655 * DHCP address. Turn it back on again here.
656 * Otherwise, if the DHCP server replies after an outage,
657 * we'll never see it.
659 vnet_feature_enable_disable ("ip4-unicast",
660 "ip4-dhcp-client-detect",
661 c->sw_if_index, 1 /* enable */ , 0, 0);
668 dhcp_client_sm (f64 now, f64 timeout, uword pool_index)
670 dhcp_client_main_t *dcm = &dhcp_client_main;
673 /* deleted, pooched, yadda yadda yadda */
674 if (pool_is_free_index (dcm->clients, pool_index))
677 c = pool_elt_at_index (dcm->clients, pool_index);
679 /* Time for us to do something with this client? */
680 if (now < c->next_transmit)
686 case DHCP_DISCOVER: /* send a discover */
687 if (dhcp_discover_state (dcm, c, now))
691 case DHCP_REQUEST: /* send a request */
692 if (dhcp_request_state (dcm, c, now))
696 case DHCP_BOUND: /* bound, renew needed? */
697 if (dhcp_bound_state (dcm, c, now))
702 clib_warning ("dhcp client %d bogus state %d",
703 c - dcm->clients, c->state);
707 if (c->next_transmit < now + timeout)
708 return c->next_transmit - now;
714 dhcp_client_process (vlib_main_t * vm,
715 vlib_node_runtime_t * rt, vlib_frame_t * f)
720 uword *event_data = 0;
721 dhcp_client_main_t *dcm = &dhcp_client_main;
727 vlib_process_wait_for_event_or_clock (vm, timeout);
729 event_type = vlib_process_get_events (vm, &event_data);
731 now = vlib_time_now (vm);
735 case EVENT_DHCP_CLIENT_WAKEUP:
736 for (i = 0; i < vec_len (event_data); i++)
737 timeout = dhcp_client_sm (now, timeout, event_data[i]);
742 pool_foreach (c, dcm->clients,
744 timeout = dhcp_client_sm (now, timeout,
745 (uword) (c - dcm->clients));
748 if (pool_elts (dcm->clients) == 0)
753 vec_reset_length (event_data);
761 VLIB_REGISTER_NODE (dhcp_client_process_node,static) = {
762 .function = dhcp_client_process,
763 .type = VLIB_NODE_TYPE_PROCESS,
764 .name = "dhcp-client-process",
765 .process_log2_n_stack_bytes = 16,
766 .n_errors = ARRAY_LEN(dhcp_client_process_stat_strings),
767 .error_strings = dhcp_client_process_stat_strings,
772 format_dhcp_client_state (u8 * s, va_list * va)
774 dhcp_client_state_t state = va_arg (*va, dhcp_client_state_t);
775 char *str = "BOGUS!";
783 foreach_dhcp_client_state;
789 s = format (s, "%s", str);
794 format_dhcp_client (u8 * s, va_list * va)
796 dhcp_client_main_t *dcm = va_arg (*va, dhcp_client_main_t *);
797 dhcp_client_t *c = va_arg (*va, dhcp_client_t *);
798 int verbose = va_arg (*va, int);
800 s = format (s, "[%d] %U state %U ", c - dcm->clients,
801 format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index,
802 format_dhcp_client_state, c->state);
804 if (c->leased_address.as_u32)
805 s = format (s, "addr %U/%d gw %U\n",
806 format_ip4_address, &c->leased_address,
807 c->subnet_mask_width, format_ip4_address, &c->router_address);
809 s = format (s, "no address\n");
813 s = format (s, "retry count %d, next xmt %.2f",
814 c->retry_count, c->next_transmit);
819 static clib_error_t *
820 show_dhcp_client_command_fn (vlib_main_t * vm,
821 unformat_input_t * input,
822 vlib_cli_command_t * cmd)
824 dhcp_client_main_t *dcm = &dhcp_client_main;
827 u32 sw_if_index = ~0;
830 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
832 if (unformat (input, "intfc %U",
833 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
835 else if (unformat (input, "verbose"))
841 if (sw_if_index != ~0)
843 p = hash_get (dcm->client_by_sw_if_index, sw_if_index);
845 return clib_error_return (0, "dhcp client not configured");
846 c = pool_elt_at_index (dcm->clients, p[0]);
847 vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
852 pool_foreach (c, dcm->clients,
854 vlib_cli_output (vm, "%U",
855 format_dhcp_client, dcm,
864 VLIB_CLI_COMMAND (show_dhcp_client_command, static) = {
865 .path = "show dhcp client",
866 .short_help = "show dhcp client [intfc <intfc>][verbose]",
867 .function = show_dhcp_client_command_fn,
873 dhcp_client_add_del (dhcp_client_add_del_args_t * a)
875 dhcp_client_main_t *dcm = &dhcp_client_main;
876 vlib_main_t *vm = dcm->vlib_main;
879 fib_prefix_t all_0s = {
881 .fp_addr.ip4.as_u32 = 0x0,
882 .fp_proto = FIB_PROTOCOL_IP4,
885 p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index);
887 if ((p && a->is_add) || (!p && a->is_add == 0))
888 return VNET_API_ERROR_INVALID_VALUE;
892 pool_get (dcm->clients, c);
893 memset (c, 0, sizeof (*c));
894 c->state = DHCP_DISCOVER;
895 c->sw_if_index = a->sw_if_index;
896 c->client_index = a->client_index;
898 c->event_callback = a->event_callback;
899 c->option_55_data = a->option_55_data;
900 c->hostname = a->hostname;
901 c->client_identifier = a->client_identifier;
902 c->set_broadcast_flag = a->set_broadcast_flag;
905 c->transaction_id = random_u32 (&dcm->seed);
907 while (c->transaction_id == 0);
908 set_l2_rewrite (dcm, c);
909 hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients);
912 * In order to accept any OFFER, whether broadcasted or unicasted, we
913 * need to configure the dhcp-client-detect feature as an input feature
914 * so the DHCP OFFER is sent to the ip4-local node. Without this a
915 * broadcasted OFFER hits the 255.255.255.255/32 address and a unicast
916 * hits 0.0.0.0/0 both of which default to drop and the latter may forward
917 * of box - not what we want. Nor to we want to change these route for
918 * all interfaces in this table
920 vnet_feature_enable_disable ("ip4-unicast",
921 "ip4-dhcp-client-detect",
922 c->sw_if_index, 1 /* enable */ , 0, 0);
924 vlib_process_signal_event (vm, dhcp_client_process_node.index,
925 EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
929 c = pool_elt_at_index (dcm->clients, p[0]);
931 if (c->router_address.as_u32)
933 ip46_address_t nh = {
934 .ip4 = c->router_address,
937 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
938 (FIB_PROTOCOL_IP4, c->sw_if_index),
939 &all_0s, FIB_SOURCE_DHCP,
940 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
941 1, FIB_ROUTE_PATH_FLAG_NONE);
943 dhcp_client_release_address (dcm, c);
945 vec_free (c->option_55_data);
946 vec_free (c->hostname);
947 vec_free (c->client_identifier);
948 vec_free (c->l2_rewrite);
949 hash_unset (dcm->client_by_sw_if_index, c->sw_if_index);
950 pool_put (dcm->clients, c);
956 dhcp_client_config (vlib_main_t * vm,
962 void *event_callback, u8 set_broadcast_flag, u32 pid)
964 dhcp_client_add_del_args_t _a, *a = &_a;
967 memset (a, 0, sizeof (*a));
969 a->sw_if_index = sw_if_index;
970 a->client_index = client_index;
972 a->event_callback = event_callback;
973 a->set_broadcast_flag = set_broadcast_flag;
974 vec_validate (a->hostname, strlen ((char *) hostname) - 1);
975 strncpy ((char *) a->hostname, (char *) hostname, vec_len (a->hostname));
976 vec_validate (a->client_identifier, strlen ((char *) client_id) - 1);
977 strncpy ((char *) a->client_identifier, (char *) client_id,
978 vec_len (a->client_identifier));
981 * Option 55 request list. These data precisely match
982 * the Ubuntu dhcp client. YMMV.
986 vec_add1 (a->option_55_data, 1);
987 /* Broadcast address */
988 vec_add1 (a->option_55_data, 28);
990 vec_add1 (a->option_55_data, 2);
992 vec_add1 (a->option_55_data, 3);
994 vec_add1 (a->option_55_data, 15);
996 vec_add1 (a->option_55_data, 6);
998 vec_add1 (a->option_55_data, 119);
1000 vec_add1 (a->option_55_data, 12);
1001 /* NetBIOS name server */
1002 vec_add1 (a->option_55_data, 44);
1004 vec_add1 (a->option_55_data, 47);
1006 vec_add1 (a->option_55_data, 26);
1007 /* Classless static route */
1008 vec_add1 (a->option_55_data, 121);
1010 vec_add1 (a->option_55_data, 42);
1012 rv = dhcp_client_add_del (a);
1019 case VNET_API_ERROR_INVALID_VALUE:
1021 vec_free (a->hostname);
1022 vec_free (a->client_identifier);
1023 vec_free (a->option_55_data);
1026 clib_warning ("dhcp client already enabled on intf_idx %d",
1029 clib_warning ("dhcp client not enabled on on intf_idx %d",
1034 clib_warning ("dhcp_client_add_del returned %d", rv);
1040 static clib_error_t *
1041 dhcp_client_set_command_fn (vlib_main_t * vm,
1042 unformat_input_t * input,
1043 vlib_cli_command_t * cmd)
1046 dhcp_client_main_t *dcm = &dhcp_client_main;
1049 u8 sw_if_index_set = 0;
1050 u8 set_broadcast_flag = 1;
1052 dhcp_client_add_del_args_t _a, *a = &_a;
1055 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1057 if (unformat (input, "intfc %U",
1058 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
1059 sw_if_index_set = 1;
1060 else if (unformat (input, "hostname %v", &hostname))
1062 else if (unformat (input, "del"))
1064 else if (unformat (input, "broadcast", &set_broadcast_flag))
1070 if (sw_if_index_set == 0)
1071 return clib_error_return (0, "interface not specified");
1073 memset (a, 0, sizeof (*a));
1075 a->sw_if_index = sw_if_index;
1076 a->hostname = hostname;
1077 a->client_identifier = format (0, "vpe 1.0%c", 0);
1078 a->set_broadcast_flag = set_broadcast_flag;
1081 * Option 55 request list. These data precisely match
1082 * the Ubuntu dhcp client. YMMV.
1086 vec_add1 (a->option_55_data, 1);
1087 /* Broadcast address */
1088 vec_add1 (a->option_55_data, 28);
1090 vec_add1 (a->option_55_data, 2);
1092 vec_add1 (a->option_55_data, 3);
1094 vec_add1 (a->option_55_data, 15);
1096 vec_add1 (a->option_55_data, 6);
1098 vec_add1 (a->option_55_data, 119);
1100 vec_add1 (a->option_55_data, 12);
1101 /* NetBIOS name server */
1102 vec_add1 (a->option_55_data, 44);
1104 vec_add1 (a->option_55_data, 47);
1106 vec_add1 (a->option_55_data, 26);
1107 /* Classless static route */
1108 vec_add1 (a->option_55_data, 121);
1110 vec_add1 (a->option_55_data, 42);
1112 rv = dhcp_client_add_del (a);
1119 case VNET_API_ERROR_INVALID_VALUE:
1121 vec_free (a->hostname);
1122 vec_free (a->client_identifier);
1123 vec_free (a->option_55_data);
1125 return clib_error_return (0, "dhcp client already enabled on %U",
1126 format_vnet_sw_if_index_name,
1127 dcm->vnet_main, sw_if_index);
1129 return clib_error_return (0, "dhcp client not enabled on %U",
1130 format_vnet_sw_if_index_name,
1131 dcm->vnet_main, sw_if_index);
1135 vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv);
1142 VLIB_CLI_COMMAND (dhcp_client_set_command, static) = {
1143 .path = "set dhcp client",
1144 .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]",
1145 .function = dhcp_client_set_command_fn,
1149 static clib_error_t *
1150 dhcp_client_init (vlib_main_t * vm)
1152 dhcp_client_main_t *dcm = &dhcp_client_main;
1154 dcm->vlib_main = vm;
1155 dcm->vnet_main = vnet_get_main ();
1156 dcm->seed = 0xdeaddabe;
1160 VLIB_INIT_FUNCTION (dhcp_client_init);
1163 * fd.io coding-style-patch-verification: ON
1166 * eval: (c-set-style "gnu")