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 <vlibmemory/api.h>
17 #include <vnet/dhcp/client.h>
18 #include <vnet/dhcp/dhcp_proxy.h>
19 #include <vnet/fib/fib_table.h>
21 dhcp_client_main_t dhcp_client_main;
22 static u8 *format_dhcp_client_state (u8 * s, va_list * va);
23 static vlib_node_registration_t dhcp_client_process_node;
25 #define foreach_dhcp_sent_packet_stat \
26 _(DISCOVER, "DHCP discover packets sent") \
27 _(OFFER, "DHCP offer packets sent") \
28 _(REQUEST, "DHCP request packets sent") \
29 _(ACK, "DHCP ack packets sent")
31 #define foreach_dhcp_error_counter \
32 _(NOT_FOR_US, "DHCP packets for other hosts, dropped") \
33 _(NAK, "DHCP nak packets received") \
34 _(NON_OFFER_DISCOVER, "DHCP non-offer packets in discover state") \
35 _(ODDBALL, "DHCP non-ack, non-offer packets received") \
36 _(BOUND, "DHCP bind success")
40 #define _(sym,str) DHCP_STAT_##sym,
41 foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter
47 static char *dhcp_client_process_stat_strings[] = {
48 #define _(sym,string) string,
49 foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter
51 "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 dhcp_client_proc_callback (uword * client_index)
81 vlib_main_t *vm = vlib_get_main ();
82 ASSERT (vlib_get_thread_index () == 0);
83 vlib_process_signal_event (vm, dhcp_client_process_node.index,
84 EVENT_DHCP_CLIENT_WAKEUP, *client_index);
88 dhcp_client_addr_callback (dhcp_client_t * c)
90 dhcp_client_main_t *dcm = &dhcp_client_main;
92 /* disable the feature */
93 vnet_feature_enable_disable ("ip4-unicast",
94 "ip4-dhcp-client-detect",
95 c->sw_if_index, 0 /* disable */ , 0, 0);
96 c->client_detect_feature_enabled = 0;
98 /* if renewing the lease, the address and route have already been added */
99 if (c->state == DHCP_BOUND)
102 /* add the address to the interface */
103 dhcp_client_acquire_address (dcm, c);
106 * Configure default IP route:
108 if (c->router_address.as_u32)
110 fib_prefix_t all_0s = {
112 .fp_addr.ip4.as_u32 = 0x0,
113 .fp_proto = FIB_PROTOCOL_IP4,
115 ip46_address_t nh = {
116 .ip4 = c->router_address,
120 fib_table_entry_path_add (
121 fib_table_get_index_for_sw_if_index (
129 ~0, 1, NULL, // no label stack
130 FIB_ROUTE_PATH_FLAG_NONE);
133 if (c->dhcp_server.as_u32)
135 ip46_address_t dst = {
136 .ip4 = c->dhcp_server,
138 c->ai_ucast = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
139 VNET_LINK_IP4, &dst, c->sw_if_index);
143 * Call the user's event callback to report DHCP information
145 if (c->event_callback)
146 c->event_callback (c->client_index, c);
150 * dhcp_client_for_us - server-to-client callback.
151 * Called from proxy_node.c:dhcp_proxy_to_client_input().
152 * This function first decides that the packet in question is
153 * actually for the dhcp client code in case we're also acting as
154 * a dhcp proxy. Ay caramba, what a folly!
157 dhcp_client_for_us (u32 bi, vlib_buffer_t * b,
159 udp_header_t * udp, dhcp_header_t * dhcp)
161 dhcp_client_main_t *dcm = &dhcp_client_main;
162 vlib_main_t *vm = dcm->vlib_main;
165 f64 now = vlib_time_now (dcm->vlib_main);
166 u8 dhcp_message_type = 0;
170 * Doing dhcp client on this interface?
171 * Presumably we will always receive dhcp clnt for-us pkts on
172 * the interface that's asking for an address.
174 p = hash_get (dcm->client_by_sw_if_index,
175 vnet_buffer (b)->sw_if_index[VLIB_RX]);
179 c = pool_elt_at_index (dcm->clients, p[0]);
181 /* Mixing dhcp relay and dhcp proxy? DGMS... */
182 if (c->state == DHCP_BOUND && c->retry_count == 0)
185 /* Packet not for us? Turf it... */
186 if (memcmp (dhcp->client_hardware_address, c->client_hardware_address,
187 sizeof (c->client_hardware_address)))
189 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
190 DHCP_STAT_NOT_FOR_US, 1);
194 /* parse through the packet, learn what we can */
195 if (dhcp->your_ip_address.as_u32)
196 c->leased_address.as_u32 = dhcp->your_ip_address.as_u32;
198 c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32;
200 o = (dhcp_option_t *) dhcp->options;
202 while (o->option != 0xFF /* end of options */ &&
203 (u8 *) o < (b->data + b->current_data + b->current_length))
207 case 53: /* dhcp message type */
208 dhcp_message_type = o->data[0];
211 case 51: /* lease time */
213 u32 lease_time_in_seconds =
214 clib_host_to_net_u32 (o->data_as_u32[0]);
215 // for debug: lease_time_in_seconds = 20; /*$$$$*/
216 c->lease_expires = now + (f64) lease_time_in_seconds;
217 c->lease_lifetime = lease_time_in_seconds;
218 /* Set a sensible default, in case we don't get opt 58 */
219 c->lease_renewal_interval = lease_time_in_seconds / 2;
223 case 58: /* lease renew time in seconds */
225 u32 lease_renew_time_in_seconds =
226 clib_host_to_net_u32 (o->data_as_u32[0]);
227 c->lease_renewal_interval = lease_renew_time_in_seconds;
231 case 54: /* dhcp server address */
232 c->dhcp_server.as_u32 = o->data_as_u32[0];
235 case 1: /* subnet mask */
237 u32 subnet_mask = clib_host_to_net_u32 (o->data_as_u32[0]);
238 c->subnet_mask_width = count_set_bits (subnet_mask);
241 case 3: /* router address */
243 u32 router_address = o->data_as_u32[0];
244 c->router_address.as_u32 = router_address;
247 case 6: /* domain server address */
249 vec_free (c->domain_server_address);
250 vec_validate (c->domain_server_address,
251 o->length / sizeof (ip4_address_t) - 1);
252 clib_memcpy (c->domain_server_address, o->data, o->length);
255 case 12: /* hostname */
257 /* Replace the existing hostname if necessary */
258 vec_free (c->hostname);
259 vec_validate (c->hostname, o->length - 1);
260 clib_memcpy (c->hostname, o->data, o->length);
264 /* $$$$ Your message in this space, parse more options */
269 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
275 if (dhcp_message_type != DHCP_PACKET_OFFER)
277 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
278 DHCP_STAT_NON_OFFER_DISCOVER, 1);
279 c->next_transmit = now + 5.0;
283 /* Received an offer, go send a request */
284 c->state = DHCP_REQUEST;
286 c->next_transmit = 0; /* send right now... */
287 /* Poke the client process, which will send the request */
288 uword client_id = c - dcm->clients;
289 vl_api_rpc_call_main_thread (dhcp_client_proc_callback,
290 (u8 *) & client_id, sizeof (uword));
295 if (dhcp_message_type == DHCP_PACKET_NAK)
297 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
299 /* Probably never happens in bound state, but anyhow... */
300 if (c->state == DHCP_BOUND)
302 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
303 (void *) &c->leased_address,
304 c->subnet_mask_width,
306 vnet_feature_enable_disable ("ip4-unicast",
307 "ip4-dhcp-client-detect",
308 c->sw_if_index, 1 /* enable */ ,
310 c->client_detect_feature_enabled = 1;
312 /* Wipe out any memory of the address we had... */
313 c->state = DHCP_DISCOVER;
314 c->next_transmit = now;
316 c->leased_address.as_u32 = 0;
317 c->subnet_mask_width = 0;
318 c->router_address.as_u32 = 0;
319 c->lease_renewal_interval = 0;
320 c->dhcp_server.as_u32 = 0;
321 vec_free (c->domain_server_address);
325 if (dhcp_message_type != DHCP_PACKET_ACK &&
326 dhcp_message_type != DHCP_PACKET_OFFER)
328 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
329 DHCP_STAT_NON_OFFER_DISCOVER, 1);
330 clib_warning ("sw_if_index %d state %U message type %d",
331 c->sw_if_index, format_dhcp_client_state,
332 c->state, dhcp_message_type);
333 c->next_transmit = now + 5.0;
336 /* OK, we own the address (etc), add to the routing table(s) */
337 vl_api_rpc_call_main_thread (dhcp_client_addr_callback,
338 (u8 *) c, sizeof (*c));
340 c->state = DHCP_BOUND;
342 c->next_transmit = now + (f64) c->lease_renewal_interval;
343 c->lease_expires = now + (f64) c->lease_lifetime;
344 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
349 clib_warning ("client %d bogus state %d", c - dcm->clients, c->state);
353 /* drop the pkt, return 1 */
354 vlib_buffer_free (vm, &bi, 1);
359 send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c,
360 dhcp_packet_type_t type, int is_broadcast)
362 vlib_main_t *vm = dcm->vlib_main;
363 vnet_main_t *vnm = dcm->vnet_main;
364 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index);
365 vnet_sw_interface_t *sup_sw
366 = vnet_get_sup_sw_interface (vnm, c->sw_if_index);
367 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, c->sw_if_index);
376 u16 udp_length, ip_length;
377 u32 counter_index, node_index;
379 /* Interface(s) down? */
380 if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
382 if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
384 if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
387 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
389 clib_warning ("buffer allocation failure");
390 c->next_transmit = 0;
394 /* Build a dhcpv4 pkt from whole cloth */
395 b = vlib_get_buffer (vm, bi);
396 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
398 ASSERT (b->current_data == 0);
400 vnet_buffer (b)->sw_if_index[VLIB_RX] = c->sw_if_index;
404 node_index = ip4_rewrite_node.index;
405 vnet_buffer (b)->ip.adj_index[VLIB_TX] = c->ai_bcast;
409 ip_adjacency_t *adj = adj_get (c->ai_ucast);
411 if (IP_LOOKUP_NEXT_ARP == adj->lookup_next_index)
412 node_index = ip4_arp_node.index;
414 node_index = ip4_rewrite_node.index;
415 vnet_buffer (b)->ip.adj_index[VLIB_TX] = c->ai_ucast;
418 /* Enqueue the packet right now */
419 f = vlib_get_frame_to_node (vm, node_index);
420 to_next = vlib_frame_vector_args (f);
423 vlib_put_frame_to_node (vm, node_index, f);
425 /* build the headers */
426 ip = vlib_buffer_get_current (b);
427 udp = (udp_header_t *) (ip + 1);
428 dhcp = (dhcp_header_t *) (udp + 1);
430 /* $$$ optimize, maybe */
431 clib_memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp));
433 ip->ip_version_and_header_length = 0x45;
435 ip->protocol = IP_PROTOCOL_UDP;
439 /* src = 0.0.0.0, dst = 255.255.255.255 */
440 ip->dst_address.as_u32 = ~0;
444 /* Renewing an active lease, plain old ip4 src/dst */
445 ip->src_address.as_u32 = c->leased_address.as_u32;
446 ip->dst_address.as_u32 = c->dhcp_server.as_u32;
449 udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client);
450 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server);
452 /* Send the interface MAC address */
453 clib_memcpy (dhcp->client_hardware_address,
454 vnet_sw_interface_get_hw_address (vnm, c->sw_if_index), 6);
456 /* And remember it for rx-packet-for-us checking */
457 clib_memcpy (c->client_hardware_address, dhcp->client_hardware_address,
458 sizeof (c->client_hardware_address));
460 /* Lease renewal, set up client_ip_address */
461 if (is_broadcast == 0)
462 dhcp->client_ip_address.as_u32 = c->leased_address.as_u32;
464 dhcp->opcode = 1; /* request, all we send */
465 dhcp->hardware_type = 1; /* ethernet */
466 dhcp->hardware_address_length = 6;
467 dhcp->transaction_identifier = c->transaction_id;
469 clib_host_to_net_u16 (is_broadcast && c->set_broadcast_flag ?
470 DHCP_FLAG_BROADCAST : 0);
471 dhcp->magic_cookie.as_u32 = DHCP_MAGIC;
473 o = (dhcp_option_t *) dhcp->options;
475 /* Send option 53, the DHCP message type */
476 o->option = DHCP_PACKET_OPTION_MSG_TYPE;
479 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
481 /* Send option 57, max msg length */
482 if (0 /* not needed, apparently */ )
487 u16 *o2 = (u16 *) o->data;
488 *o2 = clib_host_to_net_u16 (1152);
489 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
494 * If server ip address is available with non-zero value,
495 * option 54 (DHCP Server Identifier) is sent.
497 if (c->dhcp_server.as_u32)
501 clib_memcpy (o->data, &c->dhcp_server.as_u32, 4);
502 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
505 /* send option 50, requested IP address */
506 if (c->leased_address.as_u32)
510 clib_memcpy (o->data, &c->leased_address.as_u32, 4);
511 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
514 /* send option 12, host name */
515 if (vec_len (c->hostname))
518 o->length = vec_len (c->hostname);
519 clib_memcpy (o->data, c->hostname, vec_len (c->hostname));
520 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
523 /* send option 61, client_id */
524 if (vec_len (c->client_identifier))
527 o->length = vec_len (c->client_identifier);
528 clib_memcpy (o->data, c->client_identifier,
529 vec_len (c->client_identifier));
530 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
533 /* $$ maybe send the client s/w version if anyone cares */
536 * send option 55, parameter request list
537 * The current list - see below, matches the Linux dhcp client's list
538 * Any specific dhcp server config and/or dhcp server may or may
539 * not yield specific options.
542 o->length = vec_len (c->option_55_data);
543 clib_memcpy (o->data, c->option_55_data, vec_len (c->option_55_data));
544 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
551 b->current_length = ((u8 *) o) - b->data;
553 /* fix ip length, checksum and udp length */
554 ip_length = vlib_buffer_length_in_chain (vm, b);
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);
824 s = format (s, "[%d] %U state %U ", c - dcm->clients,
825 format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index,
826 format_dhcp_client_state, c->state);
828 if (c->leased_address.as_u32)
830 s = format (s, "addr %U/%d gw %U",
831 format_ip4_address, &c->leased_address,
832 c->subnet_mask_width, format_ip4_address,
835 vec_foreach (addr, c->domain_server_address)
836 s = format (s, " dns %U", format_ip4_address, addr);
840 s = format (s, "no address\n");
847 "\n lease: lifetime:%d renewal-interval:%d expires:%.2f (now:%.2f)",
848 c->lease_lifetime, c->lease_renewal_interval,
849 c->lease_expires, vlib_time_now (dcm->vlib_main));
851 format (s, "\n retry-count:%d, next-xmt:%.2f", c->retry_count,
854 format (s, "\n adjacencies:[unicast:%d broadcast:%d]", c->ai_ucast,
860 static clib_error_t *
861 show_dhcp_client_command_fn (vlib_main_t * vm,
862 unformat_input_t * input,
863 vlib_cli_command_t * cmd)
865 dhcp_client_main_t *dcm = &dhcp_client_main;
868 u32 sw_if_index = ~0;
871 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
873 if (unformat (input, "intfc %U",
874 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
876 else if (unformat (input, "verbose"))
882 if (sw_if_index != ~0)
884 p = hash_get (dcm->client_by_sw_if_index, sw_if_index);
886 return clib_error_return (0, "dhcp client not configured");
887 c = pool_elt_at_index (dcm->clients, p[0]);
888 vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
893 pool_foreach (c, dcm->clients,
895 vlib_cli_output (vm, "%U",
896 format_dhcp_client, dcm,
905 VLIB_CLI_COMMAND (show_dhcp_client_command, static) = {
906 .path = "show dhcp client",
907 .short_help = "show dhcp client [intfc <intfc>][verbose]",
908 .function = show_dhcp_client_command_fn,
914 dhcp_client_add_del (dhcp_client_add_del_args_t * a)
916 dhcp_client_main_t *dcm = &dhcp_client_main;
917 vlib_main_t *vm = dcm->vlib_main;
920 fib_prefix_t all_0s = {
922 .fp_addr.ip4.as_u32 = 0x0,
923 .fp_proto = FIB_PROTOCOL_IP4,
926 p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index);
928 if ((p && a->is_add) || (!p && a->is_add == 0))
929 return VNET_API_ERROR_INVALID_VALUE;
933 dhcp_maybe_register_udp_ports (DHCP_PORT_REG_CLIENT);
934 pool_get (dcm->clients, c);
935 clib_memset (c, 0, sizeof (*c));
936 c->state = DHCP_DISCOVER;
937 c->sw_if_index = a->sw_if_index;
938 c->client_index = a->client_index;
940 c->event_callback = a->event_callback;
941 c->option_55_data = a->option_55_data;
942 c->hostname = a->hostname;
943 c->client_identifier = a->client_identifier;
944 c->set_broadcast_flag = a->set_broadcast_flag;
945 c->ai_ucast = ADJ_INDEX_INVALID;
946 c->ai_bcast = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
948 &ADJ_BCAST_ADDR, c->sw_if_index);
952 c->transaction_id = random_u32 (&dcm->seed);
954 while (c->transaction_id == 0);
956 hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients);
959 * In order to accept any OFFER, whether broadcasted or unicasted, we
960 * need to configure the dhcp-client-detect feature as an input feature
961 * so the DHCP OFFER is sent to the ip4-local node. Without this a
962 * broadcasted OFFER hits the 255.255.255.255/32 address and a unicast
963 * hits 0.0.0.0/0 both of which default to drop and the latter may forward
964 * of box - not what we want. Nor to we want to change these route for
965 * all interfaces in this table
967 vnet_feature_enable_disable ("ip4-unicast",
968 "ip4-dhcp-client-detect",
969 c->sw_if_index, 1 /* enable */ , 0, 0);
970 c->client_detect_feature_enabled = 1;
972 vlib_process_signal_event (vm, dhcp_client_process_node.index,
973 EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
977 c = pool_elt_at_index (dcm->clients, p[0]);
979 if (c->router_address.as_u32)
981 ip46_address_t nh = {
982 .ip4 = c->router_address,
985 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
986 (FIB_PROTOCOL_IP4, c->sw_if_index),
987 &all_0s, FIB_SOURCE_DHCP,
988 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
989 1, FIB_ROUTE_PATH_FLAG_NONE);
991 dhcp_client_release_address (dcm, c);
993 adj_unlock (c->ai_ucast);
994 adj_unlock (c->ai_bcast);
996 vec_free (c->domain_server_address);
997 vec_free (c->option_55_data);
998 vec_free (c->hostname);
999 vec_free (c->client_identifier);
1000 hash_unset (dcm->client_by_sw_if_index, c->sw_if_index);
1001 pool_put (dcm->clients, c);
1007 dhcp_client_config (u32 is_add,
1013 dhcp_event_cb_t event_callback,
1014 u8 set_broadcast_flag, u32 pid)
1016 dhcp_client_add_del_args_t _a, *a = &_a;
1019 clib_memset (a, 0, sizeof (*a));
1021 a->sw_if_index = sw_if_index;
1022 a->client_index = client_index;
1024 a->event_callback = event_callback;
1025 a->set_broadcast_flag = set_broadcast_flag;
1026 vec_validate (a->hostname, strlen ((char *) hostname) - 1);
1027 strncpy ((char *) a->hostname, (char *) hostname, vec_len (a->hostname));
1028 vec_validate (a->client_identifier, strlen ((char *) client_id) - 1);
1029 strncpy ((char *) a->client_identifier, (char *) client_id,
1030 vec_len (a->client_identifier));
1033 * Option 55 request list. These data precisely match
1034 * the Ubuntu dhcp client. YMMV.
1038 vec_add1 (a->option_55_data, 1);
1039 /* Broadcast address */
1040 vec_add1 (a->option_55_data, 28);
1042 vec_add1 (a->option_55_data, 2);
1044 vec_add1 (a->option_55_data, 3);
1046 vec_add1 (a->option_55_data, 15);
1048 vec_add1 (a->option_55_data, 6);
1050 vec_add1 (a->option_55_data, 119);
1052 vec_add1 (a->option_55_data, 12);
1053 /* NetBIOS name server */
1054 vec_add1 (a->option_55_data, 44);
1056 vec_add1 (a->option_55_data, 47);
1058 vec_add1 (a->option_55_data, 26);
1059 /* Classless static route */
1060 vec_add1 (a->option_55_data, 121);
1062 vec_add1 (a->option_55_data, 42);
1064 rv = dhcp_client_add_del (a);
1071 case VNET_API_ERROR_INVALID_VALUE:
1073 vec_free (a->hostname);
1074 vec_free (a->client_identifier);
1075 vec_free (a->option_55_data);
1078 clib_warning ("dhcp client already enabled on intf_idx %d",
1081 clib_warning ("dhcp client not enabled on on intf_idx %d",
1086 clib_warning ("dhcp_client_add_del returned %d", rv);
1093 dhcp_client_walk (dhcp_client_walk_cb_t cb, void *ctx)
1095 dhcp_client_main_t *dcm = &dhcp_client_main;
1099 pool_foreach (c, dcm->clients,
1108 static clib_error_t *
1109 dhcp_client_set_command_fn (vlib_main_t * vm,
1110 unformat_input_t * input,
1111 vlib_cli_command_t * cmd)
1114 dhcp_client_main_t *dcm = &dhcp_client_main;
1117 u8 sw_if_index_set = 0;
1118 u8 set_broadcast_flag = 1;
1120 dhcp_client_add_del_args_t _a, *a = &_a;
1123 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1125 if (unformat (input, "intfc %U",
1126 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
1127 sw_if_index_set = 1;
1128 else if (unformat (input, "hostname %v", &hostname))
1130 else if (unformat (input, "del"))
1132 else if (unformat (input, "broadcast", &set_broadcast_flag))
1138 if (sw_if_index_set == 0)
1139 return clib_error_return (0, "interface not specified");
1141 clib_memset (a, 0, sizeof (*a));
1143 a->sw_if_index = sw_if_index;
1144 a->hostname = hostname;
1145 a->client_identifier = format (0, "vpe 1.0%c", 0);
1146 a->set_broadcast_flag = set_broadcast_flag;
1149 * Option 55 request list. These data precisely match
1150 * the Ubuntu dhcp client. YMMV.
1154 vec_add1 (a->option_55_data, 1);
1155 /* Broadcast address */
1156 vec_add1 (a->option_55_data, 28);
1158 vec_add1 (a->option_55_data, 2);
1160 vec_add1 (a->option_55_data, 3);
1162 vec_add1 (a->option_55_data, 15);
1164 vec_add1 (a->option_55_data, 6);
1166 vec_add1 (a->option_55_data, 119);
1168 vec_add1 (a->option_55_data, 12);
1169 /* NetBIOS name server */
1170 vec_add1 (a->option_55_data, 44);
1172 vec_add1 (a->option_55_data, 47);
1174 vec_add1 (a->option_55_data, 26);
1175 /* Classless static route */
1176 vec_add1 (a->option_55_data, 121);
1178 vec_add1 (a->option_55_data, 42);
1180 rv = dhcp_client_add_del (a);
1187 case VNET_API_ERROR_INVALID_VALUE:
1189 vec_free (a->hostname);
1190 vec_free (a->client_identifier);
1191 vec_free (a->option_55_data);
1193 return clib_error_return (0, "dhcp client already enabled on %U",
1194 format_vnet_sw_if_index_name,
1195 dcm->vnet_main, sw_if_index);
1197 return clib_error_return (0, "dhcp client not enabled on %U",
1198 format_vnet_sw_if_index_name,
1199 dcm->vnet_main, sw_if_index);
1203 vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv);
1210 VLIB_CLI_COMMAND (dhcp_client_set_command, static) = {
1211 .path = "set dhcp client",
1212 .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]",
1213 .function = dhcp_client_set_command_fn,
1217 static clib_error_t *
1218 dhcp_client_init (vlib_main_t * vm)
1220 dhcp_client_main_t *dcm = &dhcp_client_main;
1222 dcm->vlib_main = vm;
1223 dcm->vnet_main = vnet_get_main ();
1224 dcm->seed = (u32) clib_cpu_time_now ();
1228 VLIB_INIT_FUNCTION (dhcp_client_init);
1231 * fd.io coding-style-patch-verification: ON
1234 * eval: (c-set-style "gnu")