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;
25 dhcp_client_add_rx_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
27 /* Install a local entry for the offered address */
31 .fp_addr.ip4 = c->leased_address,
32 .fp_proto = FIB_PROTOCOL_IP4,
35 fib_table_entry_special_add(fib_table_get_index_for_sw_if_index(
40 (FIB_ENTRY_FLAG_LOCAL));
42 /* And add the server's address as uRPF exempt so we can accept
43 * local packets from it */
47 .fp_addr.ip4 = c->dhcp_server,
48 .fp_proto = FIB_PROTOCOL_IP4,
51 fib_table_entry_special_add(fib_table_get_index_for_sw_if_index(
55 FIB_SOURCE_URPF_EXEMPT,
56 (FIB_ENTRY_FLAG_DROP));
60 dhcp_client_remove_rx_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
65 .fp_addr.ip4 = c->leased_address,
66 .fp_proto = FIB_PROTOCOL_IP4,
69 fib_table_entry_special_remove(fib_table_get_index_for_sw_if_index(
77 .fp_addr.ip4 = c->dhcp_server,
78 .fp_proto = FIB_PROTOCOL_IP4,
81 fib_table_entry_special_remove(fib_table_get_index_for_sw_if_index(
85 FIB_SOURCE_URPF_EXEMPT);
89 dhcp_client_acquire_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
92 * Install any/all info gleaned from dhcp, right here
94 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
95 (void *) &c->leased_address,
96 c->subnet_mask_width, 0 /*is_del*/);
100 dhcp_client_release_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
103 * Remove any/all info gleaned from dhcp, right here. Caller(s)
104 * have not wiped out the info yet.
107 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
108 (void *) &c->leased_address,
109 c->subnet_mask_width, 1 /*is_del*/);
113 set_l2_rewrite (dhcp_client_main_t * dcm, dhcp_client_t * c)
115 /* Acquire the L2 rewrite string for the indicated sw_if_index */
116 c->l2_rewrite = vnet_build_rewrite_for_sw_interface(
124 * dhcp_client_for_us - server-to-client callback.
125 * Called from proxy_node.c:dhcp_proxy_to_client_input().
126 * This function first decides that the packet in question is
127 * actually for the dhcp client code in case we're also acting as
128 * a dhcp proxy. Ay caramba, what a folly!
130 int dhcp_client_for_us (u32 bi, vlib_buffer_t * b,
133 dhcp_header_t * dhcp)
135 dhcp_client_main_t * dcm = &dhcp_client_main;
136 vlib_main_t * vm = dcm->vlib_main;
139 f64 now = vlib_time_now (dcm->vlib_main);
140 u8 dhcp_message_type = 0;
144 * Doing dhcp client on this interface?
145 * Presumably we will always receive dhcp clnt for-us pkts on
146 * the interface that's asking for an address.
148 p = hash_get (dcm->client_by_sw_if_index,
149 vnet_buffer(b)->sw_if_index [VLIB_RX]);
153 c = pool_elt_at_index (dcm->clients, p[0]);
155 /* Mixing dhcp relay and dhcp proxy? DGMS... */
156 if (c->state == DHCP_BOUND && c->retry_count == 0)
159 /* parse through the packet, learn what we can */
160 if (dhcp->your_ip_address.as_u32)
161 c->leased_address.as_u32 = dhcp->your_ip_address.as_u32;
163 c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32;
165 o = (dhcp_option_t *) dhcp->options;
167 while (o->option != 0xFF /* end of options */ &&
168 (u8 *) o < (b->data + b->current_data + b->current_length))
172 case 53: /* dhcp message type */
173 dhcp_message_type = o->data[0];
176 case 51: /* lease time */
178 u32 lease_time_in_seconds =
179 clib_host_to_net_u32 (o->data_as_u32[0]);
180 c->lease_expires = now + (f64) lease_time_in_seconds;
181 c->lease_lifetime = lease_time_in_seconds;
182 /* Set a sensible default, in case we don't get opt 58 */
183 c->lease_renewal_interval = lease_time_in_seconds / 2;
187 case 58: /* lease renew time in seconds */
189 u32 lease_renew_time_in_seconds =
190 clib_host_to_net_u32 (o->data_as_u32[0]);
191 c->lease_renewal_interval = lease_renew_time_in_seconds;
195 case 54: /* dhcp server address */
196 c->dhcp_server.as_u32 = o->data_as_u32[0];
199 case 1: /* subnet mask */
202 clib_host_to_net_u32 (o->data_as_u32[0]);
203 c->subnet_mask_width = count_set_bits (subnet_mask);
206 case 3: /* router address */
208 u32 router_address = o->data_as_u32[0];
209 c->router_address.as_u32 = router_address;
213 case 12: /* hostname */
215 /* Replace the existing hostname if necessary */
216 vec_free (c->hostname);
217 vec_validate (c->hostname, o->length - 1);
218 clib_memcpy (c->hostname, o->data, o->length);
222 /* $$$$ Your message in this space, parse more options */
227 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
233 if (dhcp_message_type != DHCP_PACKET_OFFER)
235 clib_warning ("sw_if_index %d state %U message type %d",
236 c->sw_if_index, format_dhcp_client_state,
237 c->state, dhcp_message_type);
238 c->next_transmit = now + 5.0;
242 * in order to accept unicasted ACKs we need to configure the offered
243 * address on the interface. However, at this point we may not know the
244 * subnet-mask (an OFFER may not contain it). So add a temporary receice
245 * and uRPF excempt entry
247 dhcp_client_add_rx_address (dcm, c);
249 /* Received an offer, go send a request */
250 c->state = DHCP_REQUEST;
252 c->next_transmit = 0; /* send right now... */
253 /* Poke the client process, which will send the request */
254 vlib_process_signal_event (vm, dhcp_client_process_node.index,
255 EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
260 if (dhcp_message_type != DHCP_PACKET_ACK)
262 clib_warning ("sw_if_index %d state %U message type %d",
263 c->sw_if_index, format_dhcp_client_state,
264 c->state, dhcp_message_type);
265 c->next_transmit = now + 5.0;
268 /* OK, we own the address (etc), add to the routing table(s) */
269 if (c->state == DHCP_REQUEST)
271 void (*fp)(u32, u32, u8 *, u8, u8, u8 *, u8 *, u8 *) = c->event_callback;
273 /* replace the temporary RX address with the correct subnet */
274 dhcp_client_remove_rx_address (dcm, c);
275 dhcp_client_acquire_address (dcm, c);
278 * Configure default IP route:
280 if (c->router_address.as_u32)
282 fib_prefix_t all_0s =
285 .fp_addr.ip4.as_u32 = 0x0,
286 .fp_proto = FIB_PROTOCOL_IP4,
290 .ip4 = c->router_address,
293 fib_table_entry_path_add (fib_table_get_index_for_sw_if_index(
304 NULL, // no label stack
305 FIB_ROUTE_PATH_FLAG_NONE);
309 * Call the user's event callback to report DHCP information
312 (*fp) (c->client_index, /* clinet index */
315 c->subnet_mask_width,
317 (u8 *)&c->leased_address, /* host IP address */
318 (u8 *)&c->router_address, /* router IP address */
319 (u8 *)(c->l2_rewrite + 6));/* host MAC address */
322 c->state = DHCP_BOUND;
324 c->next_transmit = now + (f64) c->lease_renewal_interval;
325 c->lease_expires = now + (f64) c->lease_lifetime;
329 clib_warning ("client %d bogus state %d",
330 c - dcm->clients, c->state);
334 /* drop the pkt, return 1 */
335 vlib_buffer_free (vm, &bi, 1);
340 send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c,
341 dhcp_packet_type_t type, int is_broadcast)
343 vlib_main_t * vm = dcm->vlib_main;
344 vnet_main_t * vnm = dcm->vnet_main;
345 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index);
346 vnet_sw_interface_t * sup_sw
347 = vnet_get_sup_sw_interface (vnm, c->sw_if_index);
348 vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, c->sw_if_index);
353 dhcp_header_t * dhcp;
357 u16 udp_length, ip_length;
359 /* Interface(s) down? */
360 if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
362 if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
364 if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
367 if (vlib_buffer_alloc (vm, &bi, 1) != 1) {
368 clib_warning ("buffer allocation failure");
369 c->next_transmit = 0;
373 /* Build a dhcpv4 pkt from whole cloth */
374 b = vlib_get_buffer (vm, bi);
376 ASSERT (b->current_data == 0);
378 vnet_buffer(b)->sw_if_index[VLIB_RX] = c->sw_if_index;
381 f = vlib_get_frame_to_node (vm, hw->output_node_index);
382 vnet_buffer(b)->sw_if_index[VLIB_TX] = c->sw_if_index;
383 clib_memcpy (b->data, c->l2_rewrite, vec_len(c->l2_rewrite));
385 (((u8 *)vlib_buffer_get_current (b)) + vec_len (c->l2_rewrite));
389 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
390 vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0; /* use interface VRF */
391 ip = vlib_buffer_get_current (b);
394 /* Enqueue the packet right now */
395 to_next = vlib_frame_vector_args (f);
400 vlib_put_frame_to_node (vm, hw->output_node_index, f);
402 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
404 udp = (udp_header_t *)(ip+1);
405 dhcp = (dhcp_header_t *)(udp+1);
407 /* $$$ optimize, maybe */
408 memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp));
410 ip->ip_version_and_header_length = 0x45;
412 ip->protocol = IP_PROTOCOL_UDP;
416 /* src = 0.0.0.0, dst = 255.255.255.255 */
417 ip->dst_address.as_u32 = ~0;
421 /* Renewing an active lease, plain old ip4 src/dst */
422 ip->src_address.as_u32 = c->leased_address.as_u32;
423 ip->dst_address.as_u32 = c->dhcp_server.as_u32;
426 udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client);
427 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server);
429 /* Send the interface MAC address */
430 clib_memcpy (dhcp->client_hardware_address, c->l2_rewrite + 6, 6);
432 /* Lease renewal, set up client_ip_address */
433 if (is_broadcast == 0)
434 dhcp->client_ip_address.as_u32 = c->leased_address.as_u32;
436 dhcp->opcode = 1; /* request, all we send */
437 dhcp->hardware_type = 1; /* ethernet */
438 dhcp->hardware_address_length = 6;
439 dhcp->transaction_identifier = c->transaction_id;
440 dhcp->flags = clib_host_to_net_u16(is_broadcast ? DHCP_FLAG_BROADCAST : 0);
441 dhcp->magic_cookie.as_u32 = DHCP_MAGIC;
443 o = (dhcp_option_t * )dhcp->options;
445 /* Send option 53, the DHCP message type */
446 o->option = DHCP_PACKET_OPTION_MSG_TYPE;
449 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
451 /* Send option 57, max msg length */
452 if (0 /* not needed, apparently */)
457 u16 *o2 = (u16 *) o->data;
458 *o2 = clib_host_to_net_u16 (1152);
459 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
464 * If server ip address is available with non-zero value,
465 * option 54 (DHCP Server Identifier) is sent.
467 if (c->dhcp_server.as_u32)
471 clib_memcpy (o->data, &c->dhcp_server.as_u32, 4);
472 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
475 /* send option 50, requested IP address */
476 if (c->leased_address.as_u32)
480 clib_memcpy (o->data, &c->leased_address.as_u32, 4);
481 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
484 /* send option 12, host name */
485 if (vec_len (c->hostname))
488 o->length = vec_len (c->hostname);
489 clib_memcpy (o->data, c->hostname, vec_len (c->hostname));
490 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
493 /* send option 61, client_id */
494 if (vec_len (c->client_identifier))
497 o->length = vec_len (c->client_identifier);
498 clib_memcpy (o->data, c->client_identifier,
499 vec_len (c->client_identifier));
500 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
503 /* $$ maybe send the client s/w version if anyone cares */
506 * send option 55, parameter request list
507 * The current list - see below, matches the Linux dhcp client's list
508 * Any specific dhcp server config and/or dhcp server may or may
509 * not yield specific options.
512 o->length = vec_len (c->option_55_data);
513 clib_memcpy (o->data, c->option_55_data, vec_len(c->option_55_data));
514 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
521 b->current_length = ((u8 *)o) - b->data;
523 /* fix ip length, checksum and udp length */
524 ip_length = vlib_buffer_length_in_chain (vm, b);
526 ip_length -= vec_len (c->l2_rewrite);
528 ip->length = clib_host_to_net_u16(ip_length);
529 ip->checksum = ip4_header_checksum(ip);
531 udp_length = ip_length - (sizeof (*ip));
532 udp->length = clib_host_to_net_u16 (udp_length);
536 dhcp_discover_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
539 * State machine "DISCOVER" state. Send a dhcp discover packet,
540 * eventually back off the retry rate.
542 send_dhcp_pkt (dcm, c, DHCP_PACKET_DISCOVER, 1 /* is_broadcast */);
545 if (c->retry_count > 10)
546 c->next_transmit = now + 5.0;
548 c->next_transmit = now + 1.0;
553 dhcp_request_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
556 * State machine "REQUEST" state. Send a dhcp request packet,
557 * eventually drop back to the discover state.
559 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 1 /* is_broadcast */);
562 if (c->retry_count > 7 /* lucky you */)
564 c->state = DHCP_DISCOVER;
565 c->next_transmit = now;
569 c->next_transmit = now + 1.0;
574 dhcp_bound_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
577 * State machine "BOUND" state. Send a dhcp request packet,
578 * eventually, when the lease expires, forget the dhcp data
579 * and go back to the stone age.
581 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 0 /* is_broadcast */);
584 if (c->retry_count > 10)
585 c->next_transmit = now + 5.0;
587 c->next_transmit = now + 1.0;
589 if (now > c->lease_expires)
591 if (c->router_address.as_u32)
593 fib_prefix_t all_0s =
596 .fp_addr.ip4.as_u32 = 0x0,
597 .fp_proto = FIB_PROTOCOL_IP4,
599 ip46_address_t nh = {
600 .ip4 = c->router_address,
603 fib_table_entry_path_remove(fib_table_get_index_for_sw_if_index(
613 FIB_ROUTE_PATH_FLAG_NONE);
616 dhcp_client_release_address (dcm, c);
617 c->state = DHCP_DISCOVER;
618 c->next_transmit = now;
620 /* Wipe out any memory of the address we had... */
621 c->leased_address.as_u32 = 0;
622 c->subnet_mask_width = 0;
623 c->router_address.as_u32 = 0;
624 c->lease_renewal_interval = 0;
625 c->dhcp_server.as_u32 = 0;
631 static f64 dhcp_client_sm (f64 now, f64 timeout, uword pool_index)
633 dhcp_client_main_t * dcm = &dhcp_client_main;
636 /* deleted, pooched, yadda yadda yadda */
637 if (pool_is_free_index (dcm->clients, pool_index))
640 c = pool_elt_at_index (dcm->clients, pool_index);
642 /* Time for us to do something with this client? */
643 if (now < c->next_transmit)
649 case DHCP_DISCOVER: /* send a discover */
650 if (dhcp_discover_state (dcm, c, now))
654 case DHCP_REQUEST: /* send a request */
655 if (dhcp_request_state (dcm, c, now))
659 case DHCP_BOUND: /* bound, renew needed? */
660 if (dhcp_bound_state (dcm, c, now))
665 clib_warning ("dhcp client %d bogus state %d",
666 c - dcm->clients, c->state);
670 if (c->next_transmit < now + timeout)
671 return c->next_transmit - now;
677 dhcp_client_process (vlib_main_t * vm,
678 vlib_node_runtime_t * rt,
684 uword * event_data = 0;
685 dhcp_client_main_t * dcm = &dhcp_client_main;
691 vlib_process_wait_for_event_or_clock (vm, timeout);
693 event_type = vlib_process_get_events (vm, &event_data);
695 now = vlib_time_now (vm);
699 case EVENT_DHCP_CLIENT_WAKEUP:
700 for (i = 0; i < vec_len (event_data); i++)
701 timeout = dhcp_client_sm (now, timeout, event_data[i]);
705 pool_foreach (c, dcm->clients,
707 timeout = dhcp_client_sm (now, timeout,
708 (uword)(c - dcm->clients));
710 if (pool_elts (dcm->clients) == 0)
715 vec_reset_length (event_data);
722 VLIB_REGISTER_NODE (dhcp_client_process_node,static) = {
723 .function = dhcp_client_process,
724 .type = VLIB_NODE_TYPE_PROCESS,
725 .name = "dhcp-client-process",
726 .process_log2_n_stack_bytes = 16,
729 static u8 * format_dhcp_client_state (u8 * s, va_list * va)
731 dhcp_client_state_t state = va_arg (*va, dhcp_client_state_t);
732 char * str = "BOGUS!";
740 foreach_dhcp_client_state;
746 s = format (s, "%s", str);
750 static u8 * format_dhcp_client (u8 * s, va_list * va)
752 dhcp_client_main_t * dcm = va_arg (*va, dhcp_client_main_t *);
753 dhcp_client_t * c = va_arg (*va, dhcp_client_t *);
754 int verbose = va_arg (*va, int);
756 s = format (s, "[%d] %U state %U ", c - dcm->clients,
757 format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index,
758 format_dhcp_client_state, c->state);
760 if (c->leased_address.as_u32)
761 s = format (s, "addr %U/%d gw %U\n",
762 format_ip4_address, &c->leased_address,
763 c->subnet_mask_width, format_ip4_address, &c->router_address);
765 s = format (s, "no address\n");
769 s = format (s, "retry count %d, next xmt %.2f",
770 c->retry_count, c->next_transmit);
775 static clib_error_t *
776 show_dhcp_client_command_fn (vlib_main_t * vm,
777 unformat_input_t * input,
778 vlib_cli_command_t * cmd)
780 dhcp_client_main_t * dcm = &dhcp_client_main;
783 u32 sw_if_index = ~0;
786 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
788 if (unformat (input, "intfc %U",
789 unformat_vnet_sw_interface, dcm->vnet_main,
792 else if (unformat (input, "verbose"))
798 if (sw_if_index != ~0)
800 p = hash_get (dcm->client_by_sw_if_index, sw_if_index);
802 return clib_error_return (0, "dhcp client not configured");
803 c = pool_elt_at_index (dcm->clients, p[0]);
804 vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
808 pool_foreach (c, dcm->clients,
810 vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
816 VLIB_CLI_COMMAND (show_dhcp_client_command, static) = {
817 .path = "show dhcp client",
818 .short_help = "show dhcp client [intfc <intfc>][verbose]",
819 .function = show_dhcp_client_command_fn,
823 int dhcp_client_add_del (dhcp_client_add_del_args_t * a)
825 dhcp_client_main_t * dcm = &dhcp_client_main;
826 vlib_main_t * vm = dcm->vlib_main;
829 fib_prefix_t all_1s =
832 .fp_addr.ip4.as_u32 = 0xffffffff,
833 .fp_proto = FIB_PROTOCOL_IP4,
835 fib_prefix_t all_0s =
838 .fp_addr.ip4.as_u32 = 0x0,
839 .fp_proto = FIB_PROTOCOL_IP4,
842 p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index);
844 if ((p && a->is_add) || (!p && a->is_add == 0))
845 return VNET_API_ERROR_INVALID_VALUE;
849 pool_get (dcm->clients, c);
850 memset (c, 0, sizeof (*c));
851 c->state = DHCP_DISCOVER;
852 c->sw_if_index = a->sw_if_index;
853 c->client_index = a->client_index;
855 c->event_callback = a->event_callback;
856 c->option_55_data = a->option_55_data;
857 c->hostname = a->hostname;
858 c->client_identifier = a->client_identifier;
860 c->transaction_id = random_u32 (&dcm->seed);
861 } while (c->transaction_id == 0);
862 set_l2_rewrite (dcm, c);
863 hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients);
865 /* this add is ref counted by FIB so we can add for each itf */
866 fib_table_entry_special_add(fib_table_get_index_for_sw_if_index(
871 FIB_ENTRY_FLAG_LOCAL);
874 * enable the interface to RX IPv4 packets
875 * this is also ref counted
877 ip4_sw_interface_enable_disable (c->sw_if_index, 1);
879 vlib_process_signal_event (vm, dhcp_client_process_node.index,
880 EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
884 c = pool_elt_at_index (dcm->clients, p[0]);
886 fib_table_entry_special_remove(fib_table_get_index_for_sw_if_index(
892 if (c->router_address.as_u32)
894 ip46_address_t nh = {
895 .ip4 = c->router_address,
898 fib_table_entry_path_remove(fib_table_get_index_for_sw_if_index(
908 FIB_ROUTE_PATH_FLAG_NONE);
910 dhcp_client_remove_rx_address (dcm, c);
911 dhcp_client_release_address (dcm, c);
912 ip4_sw_interface_enable_disable (c->sw_if_index, 0);
914 vec_free (c->option_55_data);
915 vec_free (c->hostname);
916 vec_free (c->client_identifier);
917 vec_free (c->l2_rewrite);
918 hash_unset (dcm->client_by_sw_if_index, c->sw_if_index);
919 pool_put (dcm->clients, c);
925 dhcp_client_config (vlib_main_t * vm,
931 void * event_callback,
934 dhcp_client_add_del_args_t _a, *a = &_a;
937 memset (a, 0, sizeof (*a));
939 a->sw_if_index = sw_if_index;
940 a->client_index = client_index;
942 a->event_callback = event_callback;
943 vec_validate(a->hostname, strlen((char *)hostname) - 1);
944 strncpy((char *)a->hostname, (char *)hostname, vec_len(a->hostname));
945 vec_validate(a->client_identifier, strlen((char *)client_id) - 1);
946 strncpy((char *)a->client_identifier, (char *)client_id, vec_len(a->client_identifier));
949 * Option 55 request list. These data precisely match
950 * the Ubuntu dhcp client. YMMV.
954 vec_add1 (a->option_55_data, 1);
955 /* Broadcast address */
956 vec_add1 (a->option_55_data, 28);
958 vec_add1 (a->option_55_data, 2);
960 vec_add1 (a->option_55_data, 3);
962 vec_add1 (a->option_55_data, 15);
964 vec_add1 (a->option_55_data, 6);
966 vec_add1 (a->option_55_data, 119);
968 vec_add1 (a->option_55_data, 12);
969 /* NetBIOS name server */
970 vec_add1 (a->option_55_data, 44);
972 vec_add1 (a->option_55_data, 47);
974 vec_add1 (a->option_55_data, 26);
975 /* Classless static route */
976 vec_add1 (a->option_55_data, 121);
978 vec_add1 (a->option_55_data, 42);
980 rv = dhcp_client_add_del (a);
987 case VNET_API_ERROR_INVALID_VALUE:
989 vec_free (a->hostname);
990 vec_free (a->client_identifier);
991 vec_free (a->option_55_data);
994 clib_warning ("dhcp client already enabled on intf_idx %d",
997 clib_warning ("dhcp client not enabled on on intf_idx %d",
1002 clib_warning ("dhcp_client_add_del returned %d", rv);
1008 static clib_error_t *
1009 dhcp_client_set_command_fn (vlib_main_t * vm,
1010 unformat_input_t * input,
1011 vlib_cli_command_t * cmd)
1014 dhcp_client_main_t * dcm = &dhcp_client_main;
1017 u8 sw_if_index_set = 0;
1019 dhcp_client_add_del_args_t _a, *a = &_a;
1022 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1024 if (unformat (input, "intfc %U",
1025 unformat_vnet_sw_interface, dcm->vnet_main,
1027 sw_if_index_set = 1;
1028 else if (unformat (input, "hostname %v", &hostname))
1030 else if (unformat (input, "del"))
1036 if (sw_if_index_set == 0)
1037 return clib_error_return (0, "interface not specified");
1039 memset (a, 0, sizeof (*a));
1041 a->sw_if_index = sw_if_index;
1042 a->hostname = hostname;
1043 a->client_identifier = format (0, "vpe 1.0%c", 0);
1046 * Option 55 request list. These data precisely match
1047 * the Ubuntu dhcp client. YMMV.
1051 vec_add1 (a->option_55_data, 1);
1052 /* Broadcast address */
1053 vec_add1 (a->option_55_data, 28);
1055 vec_add1 (a->option_55_data, 2);
1057 vec_add1 (a->option_55_data, 3);
1059 vec_add1 (a->option_55_data, 15);
1061 vec_add1 (a->option_55_data, 6);
1063 vec_add1 (a->option_55_data, 119);
1065 vec_add1 (a->option_55_data, 12);
1066 /* NetBIOS name server */
1067 vec_add1 (a->option_55_data, 44);
1069 vec_add1 (a->option_55_data, 47);
1071 vec_add1 (a->option_55_data, 26);
1072 /* Classless static route */
1073 vec_add1 (a->option_55_data, 121);
1075 vec_add1 (a->option_55_data, 42);
1077 rv = dhcp_client_add_del (a);
1084 case VNET_API_ERROR_INVALID_VALUE:
1086 vec_free (a->hostname);
1087 vec_free (a->client_identifier);
1088 vec_free (a->option_55_data);
1090 return clib_error_return (0, "dhcp client already enabled on %U",
1091 format_vnet_sw_if_index_name,
1092 dcm->vnet_main, sw_if_index);
1094 return clib_error_return (0, "dhcp client not enabled on %U",
1095 format_vnet_sw_if_index_name,
1096 dcm->vnet_main, sw_if_index);
1100 vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv);
1106 VLIB_CLI_COMMAND (dhcp_client_set_command, static) = {
1107 .path = "set dhcp client",
1108 .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]",
1109 .function = dhcp_client_set_command_fn,
1112 static clib_error_t *
1113 dhcp_client_init (vlib_main_t * vm)
1115 dhcp_client_main_t * dcm = &dhcp_client_main;
1117 dcm->vlib_main = vm;
1118 dcm->vnet_main = vnet_get_main();
1119 dcm->seed = 0xdeaddabe;
1123 VLIB_INIT_FUNCTION (dhcp_client_init);