blob: b5710684bc18b2333bca1cc290de3cf1aab330d9 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include <vlib/vlib.h>
Neale Ranns20a175a2017-02-14 07:28:41 -080016#include <vnet/dhcp/client.h>
Neale Ranns2dd68522017-02-16 03:38:59 -080017#include <vnet/dhcp/dhcp_proxy.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010018#include <vnet/fib/fib_table.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070019
20dhcp_client_main_t dhcp_client_main;
khemendra kumar34719e32017-12-08 18:06:52 +053021static u8 *format_dhcp_client_state (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -070022static vlib_node_registration_t dhcp_client_process_node;
23
Dave Barach4585eb82018-03-23 09:52:52 -040024#define foreach_dhcp_sent_packet_stat \
Dave Barach4941fcc2018-02-14 17:58:33 -050025_(DISCOVER, "DHCP discover packets sent") \
26_(OFFER, "DHCP offer packets sent") \
27_(REQUEST, "DHCP request packets sent") \
28_(ACK, "DHCP ack packets sent")
29
Dave Barach4585eb82018-03-23 09:52:52 -040030#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")
36
Dave Barach4941fcc2018-02-14 17:58:33 -050037typedef enum
38{
39#define _(sym,str) DHCP_STAT_##sym,
Dave Barach4585eb82018-03-23 09:52:52 -040040 foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter
Dave Barach4941fcc2018-02-14 17:58:33 -050041#undef _
42 DHCP_STAT_UNKNOWN,
43 DHCP_STAT_N_STAT,
44} sample_error_t;
45
46static char *dhcp_client_process_stat_strings[] = {
47#define _(sym,string) string,
Dave Barach4585eb82018-03-23 09:52:52 -040048 foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter
Dave Barach4941fcc2018-02-14 17:58:33 -050049#undef _
50 "DHCP unknown packets sent",
51};
52
53
khemendra kumar34719e32017-12-08 18:06:52 +053054static void
Ed Warnickecb9cada2015-12-08 15:45:58 -070055dhcp_client_acquire_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
56{
khemendra kumar34719e32017-12-08 18:06:52 +053057 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 * Install any/all info gleaned from dhcp, right here
59 */
60 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
khemendra kumar34719e32017-12-08 18:06:52 +053061 (void *) &c->leased_address,
62 c->subnet_mask_width, 0 /*is_del */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -070063}
64
khemendra kumar34719e32017-12-08 18:06:52 +053065static void
Ed Warnickecb9cada2015-12-08 15:45:58 -070066dhcp_client_release_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
67{
khemendra kumar34719e32017-12-08 18:06:52 +053068 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 * Remove any/all info gleaned from dhcp, right here. Caller(s)
70 * have not wiped out the info yet.
71 */
72
73 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
khemendra kumar34719e32017-12-08 18:06:52 +053074 (void *) &c->leased_address,
75 c->subnet_mask_width, 1 /*is_del */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -070076}
77
Neale Rannsb80c5362016-10-08 13:03:40 +010078static void
79set_l2_rewrite (dhcp_client_main_t * dcm, dhcp_client_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 /* Acquire the L2 rewrite string for the indicated sw_if_index */
khemendra kumar34719e32017-12-08 18:06:52 +053082 c->l2_rewrite = vnet_build_rewrite_for_sw_interface (dcm->vnet_main,
83 c->sw_if_index,
84 VNET_LINK_IP4,
85 0 /* broadcast */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -070086}
87
John Lo609707e2017-09-19 21:45:10 -040088void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
89
90static void
91dhcp_client_proc_callback (uword * client_index)
92{
93 vlib_main_t *vm = vlib_get_main ();
94 ASSERT (vlib_get_thread_index () == 0);
khemendra kumar34719e32017-12-08 18:06:52 +053095 vlib_process_signal_event (vm, dhcp_client_process_node.index,
John Lo609707e2017-09-19 21:45:10 -040096 EVENT_DHCP_CLIENT_WAKEUP, *client_index);
97}
98
Matthew Smith7a192832018-02-10 11:11:45 -060099static void
100dhcp_client_addr_callback (dhcp_client_t * c)
101{
102 dhcp_client_main_t *dcm = &dhcp_client_main;
Matthew Smith7a192832018-02-10 11:11:45 -0600103
Matthew Smith7cb7bb32018-05-07 07:52:12 -0500104 /* disable the feature */
Matthew Smith7a192832018-02-10 11:11:45 -0600105 vnet_feature_enable_disable ("ip4-unicast",
106 "ip4-dhcp-client-detect",
Dave Barach4585eb82018-03-23 09:52:52 -0400107 c->sw_if_index, 0 /* disable */ , 0, 0);
Dave Barach525c9d02018-05-26 10:48:55 -0400108 c->client_detect_feature_enabled = 0;
Matthew Smith7a192832018-02-10 11:11:45 -0600109
Matthew Smith7cb7bb32018-05-07 07:52:12 -0500110 /* if renewing the lease, the address and route have already been added */
111 if (c->state == DHCP_BOUND)
112 return;
113
114 /* add the address to the interface */
115 dhcp_client_acquire_address (dcm, c);
116
Matthew Smith7a192832018-02-10 11:11:45 -0600117 /*
118 * Configure default IP route:
119 */
120 if (c->router_address.as_u32)
121 {
122 fib_prefix_t all_0s = {
123 .fp_len = 0,
124 .fp_addr.ip4.as_u32 = 0x0,
125 .fp_proto = FIB_PROTOCOL_IP4,
126 };
127 ip46_address_t nh = {
128 .ip4 = c->router_address,
129 };
130
131 /* *INDENT-OFF* */
132 fib_table_entry_path_add (
133 fib_table_get_index_for_sw_if_index (
134 FIB_PROTOCOL_IP4,
135 c->sw_if_index),
136 &all_0s,
137 FIB_SOURCE_DHCP,
138 FIB_ENTRY_FLAG_NONE,
139 DPO_PROTO_IP4,
140 &nh, c->sw_if_index,
141 ~0, 1, NULL, // no label stack
142 FIB_ROUTE_PATH_FLAG_NONE);
143 /* *INDENT-ON* */
144 }
145
146 /*
147 * Call the user's event callback to report DHCP information
148 */
Neale Rannsdaff1782018-05-16 04:12:18 -0700149 if (c->event_callback)
150 c->event_callback (c->client_index, c);
Matthew Smith7a192832018-02-10 11:11:45 -0600151}
152
khemendra kumar34719e32017-12-08 18:06:52 +0530153/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 * dhcp_client_for_us - server-to-client callback.
155 * Called from proxy_node.c:dhcp_proxy_to_client_input().
156 * This function first decides that the packet in question is
157 * actually for the dhcp client code in case we're also acting as
158 * a dhcp proxy. Ay caramba, what a folly!
159 */
khemendra kumar34719e32017-12-08 18:06:52 +0530160int
161dhcp_client_for_us (u32 bi, vlib_buffer_t * b,
162 ip4_header_t * ip,
163 udp_header_t * udp, dhcp_header_t * dhcp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164{
khemendra kumar34719e32017-12-08 18:06:52 +0530165 dhcp_client_main_t *dcm = &dhcp_client_main;
166 vlib_main_t *vm = dcm->vlib_main;
167 dhcp_client_t *c;
168 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 f64 now = vlib_time_now (dcm->vlib_main);
170 u8 dhcp_message_type = 0;
khemendra kumar34719e32017-12-08 18:06:52 +0530171 dhcp_option_t *o;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
khemendra kumar34719e32017-12-08 18:06:52 +0530173 /*
174 * Doing dhcp client on this interface?
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 * Presumably we will always receive dhcp clnt for-us pkts on
176 * the interface that's asking for an address.
177 */
khemendra kumar34719e32017-12-08 18:06:52 +0530178 p = hash_get (dcm->client_by_sw_if_index,
179 vnet_buffer (b)->sw_if_index[VLIB_RX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 if (p == 0)
khemendra kumar34719e32017-12-08 18:06:52 +0530181 return 0; /* no */
182
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 c = pool_elt_at_index (dcm->clients, p[0]);
184
185 /* Mixing dhcp relay and dhcp proxy? DGMS... */
186 if (c->state == DHCP_BOUND && c->retry_count == 0)
187 return 0;
188
Dave Barach4585eb82018-03-23 09:52:52 -0400189 /* Packet not for us? Turf it... */
190 if (memcmp (dhcp->client_hardware_address, c->client_hardware_address,
191 sizeof (c->client_hardware_address)))
192 {
193 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
194 DHCP_STAT_NOT_FOR_US, 1);
195 return 0;
196 }
197
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 /* parse through the packet, learn what we can */
199 if (dhcp->your_ip_address.as_u32)
200 c->leased_address.as_u32 = dhcp->your_ip_address.as_u32;
Neale Ranns808c5b22017-08-02 05:15:07 -0700201
202 c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32;
203
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 o = (dhcp_option_t *) dhcp->options;
khemendra kumar34719e32017-12-08 18:06:52 +0530205
206 while (o->option != 0xFF /* end of options */ &&
207 (u8 *) o < (b->data + b->current_data + b->current_length))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 {
209 switch (o->option)
khemendra kumar34719e32017-12-08 18:06:52 +0530210 {
211 case 53: /* dhcp message type */
212 dhcp_message_type = o->data[0];
213 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
khemendra kumar34719e32017-12-08 18:06:52 +0530215 case 51: /* lease time */
216 {
217 u32 lease_time_in_seconds =
218 clib_host_to_net_u32 (o->data_as_u32[0]);
Dave Barach525c9d02018-05-26 10:48:55 -0400219 // for debug: lease_time_in_seconds = 20; /*$$$$*/
khemendra kumar34719e32017-12-08 18:06:52 +0530220 c->lease_expires = now + (f64) lease_time_in_seconds;
221 c->lease_lifetime = lease_time_in_seconds;
222 /* Set a sensible default, in case we don't get opt 58 */
223 c->lease_renewal_interval = lease_time_in_seconds / 2;
224 }
225 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
khemendra kumar34719e32017-12-08 18:06:52 +0530227 case 58: /* lease renew time in seconds */
228 {
229 u32 lease_renew_time_in_seconds =
230 clib_host_to_net_u32 (o->data_as_u32[0]);
231 c->lease_renewal_interval = lease_renew_time_in_seconds;
232 }
233 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
khemendra kumar34719e32017-12-08 18:06:52 +0530235 case 54: /* dhcp server address */
236 c->dhcp_server.as_u32 = o->data_as_u32[0];
237 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
khemendra kumar34719e32017-12-08 18:06:52 +0530239 case 1: /* subnet mask */
240 {
241 u32 subnet_mask = clib_host_to_net_u32 (o->data_as_u32[0]);
242 c->subnet_mask_width = count_set_bits (subnet_mask);
243 }
244 break;
245 case 3: /* router address */
246 {
247 u32 router_address = o->data_as_u32[0];
248 c->router_address.as_u32 = router_address;
249 }
250 break;
jackiechen198556bc7382019-04-29 12:00:43 +0800251 case 6: /* domain server address */
252 {
253 vec_free (c->domain_server_address);
254 vec_validate (c->domain_server_address,
255 o->length / sizeof (ip4_address_t) - 1);
256 clib_memcpy (c->domain_server_address, o->data, o->length);
257 }
258 break;
khemendra kumar34719e32017-12-08 18:06:52 +0530259 case 12: /* hostname */
260 {
261 /* Replace the existing hostname if necessary */
262 vec_free (c->hostname);
263 vec_validate (c->hostname, o->length - 1);
264 clib_memcpy (c->hostname, o->data, o->length);
265 }
266 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
khemendra kumar34719e32017-12-08 18:06:52 +0530268 /* $$$$ Your message in this space, parse more options */
269 default:
270 break;
271 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
273 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
274 }
275
276 switch (c->state)
277 {
278 case DHCP_DISCOVER:
279 if (dhcp_message_type != DHCP_PACKET_OFFER)
khemendra kumar34719e32017-12-08 18:06:52 +0530280 {
Dave Barach4585eb82018-03-23 09:52:52 -0400281 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
282 DHCP_STAT_NON_OFFER_DISCOVER, 1);
khemendra kumar34719e32017-12-08 18:06:52 +0530283 c->next_transmit = now + 5.0;
284 break;
285 }
Neale Ranns808c5b22017-08-02 05:15:07 -0700286
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 /* Received an offer, go send a request */
288 c->state = DHCP_REQUEST;
289 c->retry_count = 0;
khemendra kumar34719e32017-12-08 18:06:52 +0530290 c->next_transmit = 0; /* send right now... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 /* Poke the client process, which will send the request */
khemendra kumar34719e32017-12-08 18:06:52 +0530292 uword client_id = c - dcm->clients;
John Lo609707e2017-09-19 21:45:10 -0400293 vl_api_rpc_call_main_thread (dhcp_client_proc_callback,
khemendra kumar34719e32017-12-08 18:06:52 +0530294 (u8 *) & client_id, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 break;
296
297 case DHCP_BOUND:
298 case DHCP_REQUEST:
Dave Barach43ebe292018-03-21 13:45:12 -0400299 if (dhcp_message_type == DHCP_PACKET_NAK)
300 {
Dave Barach4585eb82018-03-23 09:52:52 -0400301 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
302 DHCP_STAT_NAK, 1);
Dave Barach43ebe292018-03-21 13:45:12 -0400303 /* Probably never happens in bound state, but anyhow... */
304 if (c->state == DHCP_BOUND)
305 {
306 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
307 (void *) &c->leased_address,
308 c->subnet_mask_width,
309 1 /*is_del */ );
310 vnet_feature_enable_disable ("ip4-unicast",
311 "ip4-dhcp-client-detect",
Dave Barach4585eb82018-03-23 09:52:52 -0400312 c->sw_if_index, 1 /* enable */ ,
313 0, 0);
Dave Barach525c9d02018-05-26 10:48:55 -0400314 c->client_detect_feature_enabled = 1;
Dave Barach43ebe292018-03-21 13:45:12 -0400315 }
316 /* Wipe out any memory of the address we had... */
317 c->state = DHCP_DISCOVER;
318 c->next_transmit = now;
319 c->retry_count = 0;
320 c->leased_address.as_u32 = 0;
321 c->subnet_mask_width = 0;
322 c->router_address.as_u32 = 0;
323 c->lease_renewal_interval = 0;
324 c->dhcp_server.as_u32 = 0;
jackiechen198556bc7382019-04-29 12:00:43 +0800325 vec_free (c->domain_server_address);
Dave Barach43ebe292018-03-21 13:45:12 -0400326 break;
327 }
328
329 if (dhcp_message_type != DHCP_PACKET_ACK &&
330 dhcp_message_type != DHCP_PACKET_OFFER)
khemendra kumar34719e32017-12-08 18:06:52 +0530331 {
Dave Barach4585eb82018-03-23 09:52:52 -0400332 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
333 DHCP_STAT_NON_OFFER_DISCOVER, 1);
khemendra kumar34719e32017-12-08 18:06:52 +0530334 clib_warning ("sw_if_index %d state %U message type %d",
335 c->sw_if_index, format_dhcp_client_state,
336 c->state, dhcp_message_type);
337 c->next_transmit = now + 5.0;
338 break;
339 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 /* OK, we own the address (etc), add to the routing table(s) */
Matthew Smith7cb7bb32018-05-07 07:52:12 -0500341 vl_api_rpc_call_main_thread (dhcp_client_addr_callback,
342 (u8 *) c, sizeof (*c));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
344 c->state = DHCP_BOUND;
345 c->retry_count = 0;
346 c->next_transmit = now + (f64) c->lease_renewal_interval;
347 c->lease_expires = now + (f64) c->lease_lifetime;
Dave Barach4585eb82018-03-23 09:52:52 -0400348 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
349 DHCP_STAT_BOUND, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 break;
351
352 default:
khemendra kumar34719e32017-12-08 18:06:52 +0530353 clib_warning ("client %d bogus state %d", c - dcm->clients, c->state);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354 break;
355 }
356
357 /* drop the pkt, return 1 */
358 vlib_buffer_free (vm, &bi, 1);
359 return 1;
360}
361
khemendra kumar34719e32017-12-08 18:06:52 +0530362static void
363send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c,
364 dhcp_packet_type_t type, int is_broadcast)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365{
khemendra kumar34719e32017-12-08 18:06:52 +0530366 vlib_main_t *vm = dcm->vlib_main;
367 vnet_main_t *vnm = dcm->vnet_main;
368 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index);
369 vnet_sw_interface_t *sup_sw
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 = vnet_get_sup_sw_interface (vnm, c->sw_if_index);
khemendra kumar34719e32017-12-08 18:06:52 +0530371 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, c->sw_if_index);
372 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 u32 bi;
khemendra kumar34719e32017-12-08 18:06:52 +0530374 ip4_header_t *ip;
375 udp_header_t *udp;
376 dhcp_header_t *dhcp;
377 u32 *to_next;
378 vlib_frame_t *f;
379 dhcp_option_t *o;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 u16 udp_length, ip_length;
Dave Barach4941fcc2018-02-14 17:58:33 -0500381 u32 counter_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382
383 /* Interface(s) down? */
384 if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
385 return;
386 if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
387 return;
388 if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
389 return;
390
khemendra kumar34719e32017-12-08 18:06:52 +0530391 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
392 {
393 clib_warning ("buffer allocation failure");
394 c->next_transmit = 0;
395 return;
396 }
397
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 /* Build a dhcpv4 pkt from whole cloth */
399 b = vlib_get_buffer (vm, bi);
400
401 ASSERT (b->current_data == 0);
402
khemendra kumar34719e32017-12-08 18:06:52 +0530403 vnet_buffer (b)->sw_if_index[VLIB_RX] = c->sw_if_index;
404 if (is_broadcast)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 {
406 f = vlib_get_frame_to_node (vm, hw->output_node_index);
khemendra kumar34719e32017-12-08 18:06:52 +0530407 vnet_buffer (b)->sw_if_index[VLIB_TX] = c->sw_if_index;
408 clib_memcpy (b->data, c->l2_rewrite, vec_len (c->l2_rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 ip = (void *)
khemendra kumar34719e32017-12-08 18:06:52 +0530410 (((u8 *) vlib_buffer_get_current (b)) + vec_len (c->l2_rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 }
412 else
413 {
414 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
khemendra kumar34719e32017-12-08 18:06:52 +0530415 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; /* use interface VRF */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 ip = vlib_buffer_get_current (b);
417 }
418
419 /* Enqueue the packet right now */
420 to_next = vlib_frame_vector_args (f);
421 to_next[0] = bi;
422 f->n_vectors = 1;
423
424 if (is_broadcast)
425 vlib_put_frame_to_node (vm, hw->output_node_index, f);
426 else
427 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
khemendra kumar34719e32017-12-08 18:06:52 +0530428
429 udp = (udp_header_t *) (ip + 1);
430 dhcp = (dhcp_header_t *) (udp + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
432 /* $$$ optimize, maybe */
Dave Barachb7b92992018-10-17 10:38:51 -0400433 clib_memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
435 ip->ip_version_and_header_length = 0x45;
436 ip->ttl = 128;
437 ip->protocol = IP_PROTOCOL_UDP;
khemendra kumar34719e32017-12-08 18:06:52 +0530438
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 if (is_broadcast)
440 {
441 /* src = 0.0.0.0, dst = 255.255.255.255 */
442 ip->dst_address.as_u32 = ~0;
443 }
444 else
445 {
446 /* Renewing an active lease, plain old ip4 src/dst */
447 ip->src_address.as_u32 = c->leased_address.as_u32;
448 ip->dst_address.as_u32 = c->dhcp_server.as_u32;
449 }
450
451 udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client);
452 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server);
453
454 /* Send the interface MAC address */
Damjan Marionf1213b82016-03-13 02:22:06 +0100455 clib_memcpy (dhcp->client_hardware_address, c->l2_rewrite + 6, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
Dave Barach4585eb82018-03-23 09:52:52 -0400457 /* And remember it for rx-packet-for-us checking */
458 clib_memcpy (c->client_hardware_address, dhcp->client_hardware_address,
459 sizeof (c->client_hardware_address));
460
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 /* Lease renewal, set up client_ip_address */
462 if (is_broadcast == 0)
463 dhcp->client_ip_address.as_u32 = c->leased_address.as_u32;
464
khemendra kumar34719e32017-12-08 18:06:52 +0530465 dhcp->opcode = 1; /* request, all we send */
466 dhcp->hardware_type = 1; /* ethernet */
467 dhcp->hardware_address_length = 6;
468 dhcp->transaction_identifier = c->transaction_id;
Neale Ranns54c6dc42018-01-17 10:29:10 -0800469 dhcp->flags =
470 clib_host_to_net_u16 (is_broadcast && c->set_broadcast_flag ?
471 DHCP_FLAG_BROADCAST : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 dhcp->magic_cookie.as_u32 = DHCP_MAGIC;
khemendra kumar34719e32017-12-08 18:06:52 +0530473
474 o = (dhcp_option_t *) dhcp->options;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475
476 /* Send option 53, the DHCP message type */
Neale Ranns3466c302017-02-16 07:45:03 -0800477 o->option = DHCP_PACKET_OPTION_MSG_TYPE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 o->length = 1;
479 o->data[0] = type;
480 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
481
482 /* Send option 57, max msg length */
khemendra kumar34719e32017-12-08 18:06:52 +0530483 if (0 /* not needed, apparently */ )
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484 {
485 o->option = 57;
486 o->length = 2;
487 {
khemendra kumar34719e32017-12-08 18:06:52 +0530488 u16 *o2 = (u16 *) o->data;
489 *o2 = clib_host_to_net_u16 (1152);
490 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491 }
492 }
493
494 /*
495 * If server ip address is available with non-zero value,
496 * option 54 (DHCP Server Identifier) is sent.
497 */
498 if (c->dhcp_server.as_u32)
499 {
500 o->option = 54;
501 o->length = 4;
Damjan Marionf1213b82016-03-13 02:22:06 +0100502 clib_memcpy (o->data, &c->dhcp_server.as_u32, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
504 }
505
506 /* send option 50, requested IP address */
507 if (c->leased_address.as_u32)
508 {
509 o->option = 50;
510 o->length = 4;
Damjan Marionf1213b82016-03-13 02:22:06 +0100511 clib_memcpy (o->data, &c->leased_address.as_u32, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
513 }
514
515 /* send option 12, host name */
516 if (vec_len (c->hostname))
517 {
518 o->option = 12;
519 o->length = vec_len (c->hostname);
Damjan Marionf1213b82016-03-13 02:22:06 +0100520 clib_memcpy (o->data, c->hostname, vec_len (c->hostname));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
522 }
523
Neale Ranns51822bf2017-07-18 09:26:53 -0700524 /* send option 61, client_id */
525 if (vec_len (c->client_identifier))
526 {
527 o->option = 61;
528 o->length = vec_len (c->client_identifier);
529 clib_memcpy (o->data, c->client_identifier,
khemendra kumar34719e32017-12-08 18:06:52 +0530530 vec_len (c->client_identifier));
Neale Ranns51822bf2017-07-18 09:26:53 -0700531 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
532 }
533
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 /* $$ maybe send the client s/w version if anyone cares */
535
khemendra kumar34719e32017-12-08 18:06:52 +0530536 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 * send option 55, parameter request list
538 * The current list - see below, matches the Linux dhcp client's list
539 * Any specific dhcp server config and/or dhcp server may or may
540 * not yield specific options.
541 */
542 o->option = 55;
543 o->length = vec_len (c->option_55_data);
khemendra kumar34719e32017-12-08 18:06:52 +0530544 clib_memcpy (o->data, c->option_55_data, vec_len (c->option_55_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
546
547 /* End of list */
548 o->option = 0xff;
549 o->length = 0;
550 o++;
khemendra kumar34719e32017-12-08 18:06:52 +0530551
552 b->current_length = ((u8 *) o) - b->data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553
554 /* fix ip length, checksum and udp length */
555 ip_length = vlib_buffer_length_in_chain (vm, b);
556 if (is_broadcast)
khemendra kumar34719e32017-12-08 18:06:52 +0530557 ip_length -= vec_len (c->l2_rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558
khemendra kumar34719e32017-12-08 18:06:52 +0530559 ip->length = clib_host_to_net_u16 (ip_length);
560 ip->checksum = ip4_header_checksum (ip);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561
562 udp_length = ip_length - (sizeof (*ip));
563 udp->length = clib_host_to_net_u16 (udp_length);
Dave Barach4941fcc2018-02-14 17:58:33 -0500564
565 switch (type)
566 {
567#define _(a,b) case DHCP_PACKET_##a: {counter_index = DHCP_STAT_##a; break;}
Dave Barach4585eb82018-03-23 09:52:52 -0400568 foreach_dhcp_sent_packet_stat
Dave Barach4941fcc2018-02-14 17:58:33 -0500569#undef _
570 default:
571 counter_index = DHCP_STAT_UNKNOWN;
572 break;
573 }
574
575 vlib_node_increment_counter (vm, dhcp_client_process_node.index,
576 counter_index, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577}
578
khemendra kumar34719e32017-12-08 18:06:52 +0530579static int
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580dhcp_discover_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
581{
582 /*
583 * State machine "DISCOVER" state. Send a dhcp discover packet,
584 * eventually back off the retry rate.
585 */
Dave Barach525c9d02018-05-26 10:48:55 -0400586
587 if (c->client_detect_feature_enabled == 0)
588 {
589 vnet_feature_enable_disable ("ip4-unicast",
590 "ip4-dhcp-client-detect",
591 c->sw_if_index, 1 /* enable */ , 0, 0);
592 c->client_detect_feature_enabled = 1;
593 }
594
khemendra kumar34719e32017-12-08 18:06:52 +0530595 send_dhcp_pkt (dcm, c, DHCP_PACKET_DISCOVER, 1 /* is_broadcast */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
597 c->retry_count++;
598 if (c->retry_count > 10)
599 c->next_transmit = now + 5.0;
600 else
601 c->next_transmit = now + 1.0;
602 return 0;
603}
khemendra kumar34719e32017-12-08 18:06:52 +0530604
605static int
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606dhcp_request_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
khemendra kumar34719e32017-12-08 18:06:52 +0530607{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 /*
609 * State machine "REQUEST" state. Send a dhcp request packet,
610 * eventually drop back to the discover state.
611 */
khemendra kumar34719e32017-12-08 18:06:52 +0530612 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 1 /* is_broadcast */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613
614 c->retry_count++;
khemendra kumar34719e32017-12-08 18:06:52 +0530615 if (c->retry_count > 7 /* lucky you */ )
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616 {
617 c->state = DHCP_DISCOVER;
618 c->next_transmit = now;
619 c->retry_count = 0;
620 return 1;
621 }
622 c->next_transmit = now + 1.0;
623 return 0;
624}
625
khemendra kumar34719e32017-12-08 18:06:52 +0530626static int
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627dhcp_bound_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
628{
629 /*
Matthew Smith7cb7bb32018-05-07 07:52:12 -0500630 * State machine "BOUND" state. Send a dhcp request packet to renew
631 * the lease.
632 * Eventually, when the lease expires, forget the dhcp data
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 * and go back to the stone age.
634 */
Matthew Smith7cb7bb32018-05-07 07:52:12 -0500635
636 /*
637 * We disable the client detect feature when we bind a
638 * DHCP address. Turn it back on again on first renew attempt.
639 * Otherwise, if the DHCP server replies we'll never see it.
640 */
Dave Barach525c9d02018-05-26 10:48:55 -0400641 if (c->client_detect_feature_enabled == 0)
642 {
643 vnet_feature_enable_disable ("ip4-unicast",
644 "ip4-dhcp-client-detect",
645 c->sw_if_index, 1 /* enable */ , 0, 0);
646 c->client_detect_feature_enabled = 1;
647 }
Matthew Smith7cb7bb32018-05-07 07:52:12 -0500648
khemendra kumar34719e32017-12-08 18:06:52 +0530649 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 0 /* is_broadcast */ );
650
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651 c->retry_count++;
652 if (c->retry_count > 10)
653 c->next_transmit = now + 5.0;
654 else
655 c->next_transmit = now + 1.0;
khemendra kumar34719e32017-12-08 18:06:52 +0530656
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 if (now > c->lease_expires)
658 {
Dave Barach4941fcc2018-02-14 17:58:33 -0500659 /* Remove the default route */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660 if (c->router_address.as_u32)
khemendra kumar34719e32017-12-08 18:06:52 +0530661 {
662 fib_prefix_t all_0s = {
663 .fp_len = 0,
664 .fp_addr.ip4.as_u32 = 0x0,
665 .fp_proto = FIB_PROTOCOL_IP4,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100666 };
667 ip46_address_t nh = {
khemendra kumar34719e32017-12-08 18:06:52 +0530668 .ip4 = c->router_address,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100669 };
670
khemendra kumar34719e32017-12-08 18:06:52 +0530671 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
672 (FIB_PROTOCOL_IP4, c->sw_if_index),
673 &all_0s, FIB_SOURCE_DHCP,
674 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
675 1, FIB_ROUTE_PATH_FLAG_NONE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100676 }
Dave Barach4941fcc2018-02-14 17:58:33 -0500677 /* Remove the interface address */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 dhcp_client_release_address (dcm, c);
679 c->state = DHCP_DISCOVER;
680 c->next_transmit = now;
681 c->retry_count = 0;
682 /* Wipe out any memory of the address we had... */
683 c->leased_address.as_u32 = 0;
684 c->subnet_mask_width = 0;
685 c->router_address.as_u32 = 0;
686 c->lease_renewal_interval = 0;
687 c->dhcp_server.as_u32 = 0;
688 return 1;
689 }
690 return 0;
691}
692
khemendra kumar34719e32017-12-08 18:06:52 +0530693static f64
694dhcp_client_sm (f64 now, f64 timeout, uword pool_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695{
khemendra kumar34719e32017-12-08 18:06:52 +0530696 dhcp_client_main_t *dcm = &dhcp_client_main;
697 dhcp_client_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
699 /* deleted, pooched, yadda yadda yadda */
700 if (pool_is_free_index (dcm->clients, pool_index))
701 return timeout;
702
703 c = pool_elt_at_index (dcm->clients, pool_index);
704
705 /* Time for us to do something with this client? */
706 if (now < c->next_transmit)
707 return timeout;
708
khemendra kumar34719e32017-12-08 18:06:52 +0530709again:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 switch (c->state)
711 {
khemendra kumar34719e32017-12-08 18:06:52 +0530712 case DHCP_DISCOVER: /* send a discover */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 if (dhcp_discover_state (dcm, c, now))
khemendra kumar34719e32017-12-08 18:06:52 +0530714 goto again;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715 break;
716
khemendra kumar34719e32017-12-08 18:06:52 +0530717 case DHCP_REQUEST: /* send a request */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718 if (dhcp_request_state (dcm, c, now))
khemendra kumar34719e32017-12-08 18:06:52 +0530719 goto again;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 break;
khemendra kumar34719e32017-12-08 18:06:52 +0530721
722 case DHCP_BOUND: /* bound, renew needed? */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723 if (dhcp_bound_state (dcm, c, now))
khemendra kumar34719e32017-12-08 18:06:52 +0530724 goto again;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725 break;
khemendra kumar34719e32017-12-08 18:06:52 +0530726
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727 default:
khemendra kumar34719e32017-12-08 18:06:52 +0530728 clib_warning ("dhcp client %d bogus state %d",
729 c - dcm->clients, c->state);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730 break;
731 }
khemendra kumar34719e32017-12-08 18:06:52 +0530732
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 if (c->next_transmit < now + timeout)
734 return c->next_transmit - now;
735
736 return timeout;
737}
738
739static uword
740dhcp_client_process (vlib_main_t * vm,
khemendra kumar34719e32017-12-08 18:06:52 +0530741 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742{
743 f64 timeout = 100.0;
744 f64 now;
745 uword event_type;
khemendra kumar34719e32017-12-08 18:06:52 +0530746 uword *event_data = 0;
747 dhcp_client_main_t *dcm = &dhcp_client_main;
748 dhcp_client_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 int i;
750
751 while (1)
752 {
753 vlib_process_wait_for_event_or_clock (vm, timeout);
754
755 event_type = vlib_process_get_events (vm, &event_data);
756
757 now = vlib_time_now (vm);
758
759 switch (event_type)
khemendra kumar34719e32017-12-08 18:06:52 +0530760 {
761 case EVENT_DHCP_CLIENT_WAKEUP:
762 for (i = 0; i < vec_len (event_data); i++)
763 timeout = dhcp_client_sm (now, timeout, event_data[i]);
764 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
khemendra kumar34719e32017-12-08 18:06:52 +0530766 case ~0:
Neale Ranns54c6dc42018-01-17 10:29:10 -0800767 /* *INDENT-OFF* */
768 pool_foreach (c, dcm->clients,
769 ({
770 timeout = dhcp_client_sm (now, timeout,
771 (uword) (c - dcm->clients));
772 }));
773 /* *INDENT-ON* */
khemendra kumar34719e32017-12-08 18:06:52 +0530774 if (pool_elts (dcm->clients) == 0)
775 timeout = 100.0;
776 break;
777 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778
779 vec_reset_length (event_data);
780 }
781
782 /* NOTREACHED */
783 return 0;
784}
785
khemendra kumar34719e32017-12-08 18:06:52 +0530786/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787VLIB_REGISTER_NODE (dhcp_client_process_node,static) = {
788 .function = dhcp_client_process,
789 .type = VLIB_NODE_TYPE_PROCESS,
790 .name = "dhcp-client-process",
791 .process_log2_n_stack_bytes = 16,
Dave Barach4941fcc2018-02-14 17:58:33 -0500792 .n_errors = ARRAY_LEN(dhcp_client_process_stat_strings),
793 .error_strings = dhcp_client_process_stat_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700794};
khemendra kumar34719e32017-12-08 18:06:52 +0530795/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796
khemendra kumar34719e32017-12-08 18:06:52 +0530797static u8 *
798format_dhcp_client_state (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799{
800 dhcp_client_state_t state = va_arg (*va, dhcp_client_state_t);
khemendra kumar34719e32017-12-08 18:06:52 +0530801 char *str = "BOGUS!";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802
803 switch (state)
804 {
805#define _(a) \
806 case a: \
807 str = #a; \
808 break;
809 foreach_dhcp_client_state;
810#undef _
811 default:
812 break;
813 }
814
815 s = format (s, "%s", str);
816 return s;
817}
818
khemendra kumar34719e32017-12-08 18:06:52 +0530819static u8 *
820format_dhcp_client (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821{
khemendra kumar34719e32017-12-08 18:06:52 +0530822 dhcp_client_main_t *dcm = va_arg (*va, dhcp_client_main_t *);
823 dhcp_client_t *c = va_arg (*va, dhcp_client_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824 int verbose = va_arg (*va, int);
jackiechen198556bc7382019-04-29 12:00:43 +0800825 ip4_address_t *addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826
khemendra kumar34719e32017-12-08 18:06:52 +0530827 s = format (s, "[%d] %U state %U ", c - dcm->clients,
828 format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index,
829 format_dhcp_client_state, c->state);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700830
831 if (c->leased_address.as_u32)
jackiechen198556bc7382019-04-29 12:00:43 +0800832 {
jackiechen19857fa41602019-05-07 18:59:13 +0800833 s = format (s, "addr %U/%d gw %U",
jackiechen198556bc7382019-04-29 12:00:43 +0800834 format_ip4_address, &c->leased_address,
835 c->subnet_mask_width, format_ip4_address,
836 &c->router_address);
837
838 vec_foreach (addr, c->domain_server_address)
jackiechen19857fa41602019-05-07 18:59:13 +0800839 s = format (s, " dns %U", format_ip4_address, addr);
840 vec_add1 (s, '\n');
jackiechen198556bc7382019-04-29 12:00:43 +0800841 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842 else
jackiechen198556bc7382019-04-29 12:00:43 +0800843 {
844 s = format (s, "no address\n");
845 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846
847 if (verbose)
848 {
849 s = format (s, "retry count %d, next xmt %.2f",
khemendra kumar34719e32017-12-08 18:06:52 +0530850 c->retry_count, c->next_transmit);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851 }
852 return s;
853}
854
855static clib_error_t *
856show_dhcp_client_command_fn (vlib_main_t * vm,
khemendra kumar34719e32017-12-08 18:06:52 +0530857 unformat_input_t * input,
858 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859{
khemendra kumar34719e32017-12-08 18:06:52 +0530860 dhcp_client_main_t *dcm = &dhcp_client_main;
861 dhcp_client_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862 int verbose = 0;
863 u32 sw_if_index = ~0;
khemendra kumar34719e32017-12-08 18:06:52 +0530864 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865
khemendra kumar34719e32017-12-08 18:06:52 +0530866 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 {
khemendra kumar34719e32017-12-08 18:06:52 +0530868 if (unformat (input, "intfc %U",
869 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
870 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871 else if (unformat (input, "verbose"))
khemendra kumar34719e32017-12-08 18:06:52 +0530872 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873 else
khemendra kumar34719e32017-12-08 18:06:52 +0530874 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875 }
876
877 if (sw_if_index != ~0)
878 {
879 p = hash_get (dcm->client_by_sw_if_index, sw_if_index);
880 if (p == 0)
khemendra kumar34719e32017-12-08 18:06:52 +0530881 return clib_error_return (0, "dhcp client not configured");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100882 c = pool_elt_at_index (dcm->clients, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883 vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
884 return 0;
885 }
886
Neale Ranns54c6dc42018-01-17 10:29:10 -0800887 /* *INDENT-OFF* */
888 pool_foreach (c, dcm->clients,
889 ({
890 vlib_cli_output (vm, "%U",
891 format_dhcp_client, dcm,
892 c, verbose);
893 }));
894 /* *INDENT-ON* */
khemendra kumar34719e32017-12-08 18:06:52 +0530895
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896 return 0;
897}
898
khemendra kumar34719e32017-12-08 18:06:52 +0530899/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900VLIB_CLI_COMMAND (show_dhcp_client_command, static) = {
901 .path = "show dhcp client",
902 .short_help = "show dhcp client [intfc <intfc>][verbose]",
903 .function = show_dhcp_client_command_fn,
904};
khemendra kumar34719e32017-12-08 18:06:52 +0530905/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906
907
khemendra kumar34719e32017-12-08 18:06:52 +0530908int
909dhcp_client_add_del (dhcp_client_add_del_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910{
khemendra kumar34719e32017-12-08 18:06:52 +0530911 dhcp_client_main_t *dcm = &dhcp_client_main;
912 vlib_main_t *vm = dcm->vlib_main;
913 dhcp_client_t *c;
914 uword *p;
khemendra kumar34719e32017-12-08 18:06:52 +0530915 fib_prefix_t all_0s = {
916 .fp_len = 0,
917 .fp_addr.ip4.as_u32 = 0x0,
918 .fp_proto = FIB_PROTOCOL_IP4,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100919 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920
921 p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index);
922
923 if ((p && a->is_add) || (!p && a->is_add == 0))
924 return VNET_API_ERROR_INVALID_VALUE;
925
926 if (a->is_add)
927 {
Matthew Smithd4bdd932019-03-27 12:30:29 -0500928 dhcp_maybe_register_udp_ports (DHCP_PORT_REG_CLIENT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929 pool_get (dcm->clients, c);
Dave Barachb7b92992018-10-17 10:38:51 -0400930 clib_memset (c, 0, sizeof (*c));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 c->state = DHCP_DISCOVER;
932 c->sw_if_index = a->sw_if_index;
933 c->client_index = a->client_index;
934 c->pid = a->pid;
935 c->event_callback = a->event_callback;
936 c->option_55_data = a->option_55_data;
937 c->hostname = a->hostname;
938 c->client_identifier = a->client_identifier;
Neale Ranns54c6dc42018-01-17 10:29:10 -0800939 c->set_broadcast_flag = a->set_broadcast_flag;
khemendra kumar34719e32017-12-08 18:06:52 +0530940 do
941 {
942 c->transaction_id = random_u32 (&dcm->seed);
943 }
944 while (c->transaction_id == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945 set_l2_rewrite (dcm, c);
946 hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100947
khemendra kumar34719e32017-12-08 18:06:52 +0530948 /*
Neale Ranns54c6dc42018-01-17 10:29:10 -0800949 * In order to accept any OFFER, whether broadcasted or unicasted, we
950 * need to configure the dhcp-client-detect feature as an input feature
951 * so the DHCP OFFER is sent to the ip4-local node. Without this a
952 * broadcasted OFFER hits the 255.255.255.255/32 address and a unicast
953 * hits 0.0.0.0/0 both of which default to drop and the latter may forward
954 * of box - not what we want. Nor to we want to change these route for
955 * all interfaces in this table
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100956 */
Neale Ranns54c6dc42018-01-17 10:29:10 -0800957 vnet_feature_enable_disable ("ip4-unicast",
958 "ip4-dhcp-client-detect",
Dave Barach4585eb82018-03-23 09:52:52 -0400959 c->sw_if_index, 1 /* enable */ , 0, 0);
Dave Barach525c9d02018-05-26 10:48:55 -0400960 c->client_detect_feature_enabled = 1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100961
khemendra kumar34719e32017-12-08 18:06:52 +0530962 vlib_process_signal_event (vm, dhcp_client_process_node.index,
963 EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964 }
965 else
966 {
967 c = pool_elt_at_index (dcm->clients, p[0]);
968
969 if (c->router_address.as_u32)
khemendra kumar34719e32017-12-08 18:06:52 +0530970 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100971 ip46_address_t nh = {
khemendra kumar34719e32017-12-08 18:06:52 +0530972 .ip4 = c->router_address,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100973 };
974
khemendra kumar34719e32017-12-08 18:06:52 +0530975 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
976 (FIB_PROTOCOL_IP4, c->sw_if_index),
977 &all_0s, FIB_SOURCE_DHCP,
978 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
979 1, FIB_ROUTE_PATH_FLAG_NONE);
980 }
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700981 dhcp_client_release_address (dcm, c);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100982
jackiechen198556bc7382019-04-29 12:00:43 +0800983 vec_free (c->domain_server_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984 vec_free (c->option_55_data);
985 vec_free (c->hostname);
986 vec_free (c->client_identifier);
987 vec_free (c->l2_rewrite);
988 hash_unset (dcm->client_by_sw_if_index, c->sw_if_index);
989 pool_put (dcm->clients, c);
990 }
991 return 0;
992}
993
994int
Neale Rannsdaff1782018-05-16 04:12:18 -0700995dhcp_client_config (u32 is_add,
996 u32 client_index,
997 vlib_main_t * vm,
khemendra kumar34719e32017-12-08 18:06:52 +0530998 u32 sw_if_index,
999 u8 * hostname,
1000 u8 * client_id,
Neale Rannsdaff1782018-05-16 04:12:18 -07001001 dhcp_event_cb_t event_callback,
1002 u8 set_broadcast_flag, u32 pid)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003{
1004 dhcp_client_add_del_args_t _a, *a = &_a;
1005 int rv;
1006
Dave Barachb7b92992018-10-17 10:38:51 -04001007 clib_memset (a, 0, sizeof (*a));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 a->is_add = is_add;
1009 a->sw_if_index = sw_if_index;
1010 a->client_index = client_index;
1011 a->pid = pid;
1012 a->event_callback = event_callback;
Neale Ranns54c6dc42018-01-17 10:29:10 -08001013 a->set_broadcast_flag = set_broadcast_flag;
khemendra kumar34719e32017-12-08 18:06:52 +05301014 vec_validate (a->hostname, strlen ((char *) hostname) - 1);
1015 strncpy ((char *) a->hostname, (char *) hostname, vec_len (a->hostname));
1016 vec_validate (a->client_identifier, strlen ((char *) client_id) - 1);
1017 strncpy ((char *) a->client_identifier, (char *) client_id,
1018 vec_len (a->client_identifier));
Neale Ranns51822bf2017-07-18 09:26:53 -07001019
khemendra kumar34719e32017-12-08 18:06:52 +05301020 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 * Option 55 request list. These data precisely match
1022 * the Ubuntu dhcp client. YMMV.
1023 */
1024
1025 /* Subnet Mask */
1026 vec_add1 (a->option_55_data, 1);
1027 /* Broadcast address */
1028 vec_add1 (a->option_55_data, 28);
1029 /* time offset */
1030 vec_add1 (a->option_55_data, 2);
1031 /* Router */
1032 vec_add1 (a->option_55_data, 3);
1033 /* Domain Name */
1034 vec_add1 (a->option_55_data, 15);
1035 /* DNS */
1036 vec_add1 (a->option_55_data, 6);
1037 /* Domain search */
1038 vec_add1 (a->option_55_data, 119);
1039 /* Host name */
1040 vec_add1 (a->option_55_data, 12);
1041 /* NetBIOS name server */
1042 vec_add1 (a->option_55_data, 44);
1043 /* NetBIOS Scope */
1044 vec_add1 (a->option_55_data, 47);
1045 /* MTU */
1046 vec_add1 (a->option_55_data, 26);
1047 /* Classless static route */
1048 vec_add1 (a->option_55_data, 121);
1049 /* NTP servers */
1050 vec_add1 (a->option_55_data, 42);
1051
1052 rv = dhcp_client_add_del (a);
1053
1054 switch (rv)
1055 {
1056 case 0:
1057 break;
1058
1059 case VNET_API_ERROR_INVALID_VALUE:
1060
1061 vec_free (a->hostname);
1062 vec_free (a->client_identifier);
1063 vec_free (a->option_55_data);
1064
1065 if (is_add)
khemendra kumar34719e32017-12-08 18:06:52 +05301066 clib_warning ("dhcp client already enabled on intf_idx %d",
1067 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068 else
khemendra kumar34719e32017-12-08 18:06:52 +05301069 clib_warning ("dhcp client not enabled on on intf_idx %d",
1070 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001071 break;
1072
1073 default:
1074 clib_warning ("dhcp_client_add_del returned %d", rv);
1075 }
1076
1077 return rv;
1078}
1079
Neale Rannsdaff1782018-05-16 04:12:18 -07001080void
1081dhcp_client_walk (dhcp_client_walk_cb_t cb, void *ctx)
1082{
1083 dhcp_client_main_t *dcm = &dhcp_client_main;
1084 dhcp_client_t *c;
1085
1086 /* *INDENT-OFF* */
1087 pool_foreach (c, dcm->clients,
1088 ({
1089 if (!cb(c, ctx))
1090 break;
1091 }));
1092 /* *INDENT-ON* */
1093
1094}
1095
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096static clib_error_t *
1097dhcp_client_set_command_fn (vlib_main_t * vm,
khemendra kumar34719e32017-12-08 18:06:52 +05301098 unformat_input_t * input,
1099 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100{
1101
khemendra kumar34719e32017-12-08 18:06:52 +05301102 dhcp_client_main_t *dcm = &dhcp_client_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103 u32 sw_if_index;
khemendra kumar34719e32017-12-08 18:06:52 +05301104 u8 *hostname = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105 u8 sw_if_index_set = 0;
Neale Ranns54c6dc42018-01-17 10:29:10 -08001106 u8 set_broadcast_flag = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107 int is_add = 1;
1108 dhcp_client_add_del_args_t _a, *a = &_a;
1109 int rv;
1110
khemendra kumar34719e32017-12-08 18:06:52 +05301111 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112 {
khemendra kumar34719e32017-12-08 18:06:52 +05301113 if (unformat (input, "intfc %U",
1114 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
1115 sw_if_index_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 else if (unformat (input, "hostname %v", &hostname))
khemendra kumar34719e32017-12-08 18:06:52 +05301117 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 else if (unformat (input, "del"))
khemendra kumar34719e32017-12-08 18:06:52 +05301119 is_add = 0;
Neale Ranns54c6dc42018-01-17 10:29:10 -08001120 else if (unformat (input, "broadcast", &set_broadcast_flag))
1121 is_add = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122 else
khemendra kumar34719e32017-12-08 18:06:52 +05301123 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124 }
1125
1126 if (sw_if_index_set == 0)
1127 return clib_error_return (0, "interface not specified");
1128
Dave Barachb7b92992018-10-17 10:38:51 -04001129 clib_memset (a, 0, sizeof (*a));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130 a->is_add = is_add;
1131 a->sw_if_index = sw_if_index;
1132 a->hostname = hostname;
1133 a->client_identifier = format (0, "vpe 1.0%c", 0);
Neale Ranns54c6dc42018-01-17 10:29:10 -08001134 a->set_broadcast_flag = set_broadcast_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135
khemendra kumar34719e32017-12-08 18:06:52 +05301136 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137 * Option 55 request list. These data precisely match
1138 * the Ubuntu dhcp client. YMMV.
1139 */
1140
1141 /* Subnet Mask */
1142 vec_add1 (a->option_55_data, 1);
1143 /* Broadcast address */
1144 vec_add1 (a->option_55_data, 28);
1145 /* time offset */
1146 vec_add1 (a->option_55_data, 2);
1147 /* Router */
1148 vec_add1 (a->option_55_data, 3);
1149 /* Domain Name */
1150 vec_add1 (a->option_55_data, 15);
1151 /* DNS */
1152 vec_add1 (a->option_55_data, 6);
1153 /* Domain search */
1154 vec_add1 (a->option_55_data, 119);
1155 /* Host name */
1156 vec_add1 (a->option_55_data, 12);
1157 /* NetBIOS name server */
1158 vec_add1 (a->option_55_data, 44);
1159 /* NetBIOS Scope */
1160 vec_add1 (a->option_55_data, 47);
1161 /* MTU */
1162 vec_add1 (a->option_55_data, 26);
1163 /* Classless static route */
1164 vec_add1 (a->option_55_data, 121);
1165 /* NTP servers */
1166 vec_add1 (a->option_55_data, 42);
1167
1168 rv = dhcp_client_add_del (a);
1169
1170 switch (rv)
1171 {
1172 case 0:
1173 break;
1174
1175 case VNET_API_ERROR_INVALID_VALUE:
1176
1177 vec_free (a->hostname);
1178 vec_free (a->client_identifier);
1179 vec_free (a->option_55_data);
1180 if (is_add)
khemendra kumar34719e32017-12-08 18:06:52 +05301181 return clib_error_return (0, "dhcp client already enabled on %U",
1182 format_vnet_sw_if_index_name,
1183 dcm->vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 else
khemendra kumar34719e32017-12-08 18:06:52 +05301185 return clib_error_return (0, "dhcp client not enabled on %U",
1186 format_vnet_sw_if_index_name,
1187 dcm->vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188 break;
1189
1190 default:
1191 vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv);
1192 }
1193
1194 return 0;
1195}
1196
khemendra kumar34719e32017-12-08 18:06:52 +05301197/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198VLIB_CLI_COMMAND (dhcp_client_set_command, static) = {
1199 .path = "set dhcp client",
1200 .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]",
1201 .function = dhcp_client_set_command_fn,
1202};
khemendra kumar34719e32017-12-08 18:06:52 +05301203/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204
1205static clib_error_t *
1206dhcp_client_init (vlib_main_t * vm)
1207{
khemendra kumar34719e32017-12-08 18:06:52 +05301208 dhcp_client_main_t *dcm = &dhcp_client_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
1210 dcm->vlib_main = vm;
khemendra kumar34719e32017-12-08 18:06:52 +05301211 dcm->vnet_main = vnet_get_main ();
jackiechen19856b7e2332019-04-29 11:29:04 +08001212 dcm->seed = (u32) clib_cpu_time_now ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001213 return 0;
1214}
1215
1216VLIB_INIT_FUNCTION (dhcp_client_init);
khemendra kumar34719e32017-12-08 18:06:52 +05301217
1218/*
1219 * fd.io coding-style-patch-verification: ON
1220 *
1221 * Local Variables:
1222 * eval: (c-set-style "gnu")
1223 * End:
1224 */