blob: 8043bf22d43fa633c17509861fc9d5b1f3c19697 [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
khemendra kumar34719e32017-12-08 18:06:52 +053024static void
Neale Ranns808c5b22017-08-02 05:15:07 -070025dhcp_client_add_rx_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
26{
27 /* Install a local entry for the offered address */
khemendra kumar34719e32017-12-08 18:06:52 +053028 fib_prefix_t rx = {
29 .fp_len = 32,
30 .fp_addr.ip4 = c->leased_address,
31 .fp_proto = FIB_PROTOCOL_IP4,
32 };
Neale Ranns808c5b22017-08-02 05:15:07 -070033
khemendra kumar34719e32017-12-08 18:06:52 +053034 fib_table_entry_special_add (fib_table_get_index_for_sw_if_index
35 (FIB_PROTOCOL_IP4, c->sw_if_index), &rx,
36 FIB_SOURCE_DHCP, (FIB_ENTRY_FLAG_LOCAL));
Neale Ranns808c5b22017-08-02 05:15:07 -070037
38 /* And add the server's address as uRPF exempt so we can accept
39 * local packets from it */
khemendra kumar34719e32017-12-08 18:06:52 +053040 fib_prefix_t server = {
41 .fp_len = 32,
42 .fp_addr.ip4 = c->dhcp_server,
43 .fp_proto = FIB_PROTOCOL_IP4,
44 };
Neale Ranns808c5b22017-08-02 05:15:07 -070045
khemendra kumar34719e32017-12-08 18:06:52 +053046 fib_table_entry_special_add (fib_table_get_index_for_sw_if_index
47 (FIB_PROTOCOL_IP4, c->sw_if_index), &server,
48 FIB_SOURCE_URPF_EXEMPT, (FIB_ENTRY_FLAG_DROP));
Neale Ranns808c5b22017-08-02 05:15:07 -070049}
50
51static void
52dhcp_client_remove_rx_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
53{
khemendra kumar34719e32017-12-08 18:06:52 +053054 fib_prefix_t rx = {
55 .fp_len = 32,
56 .fp_addr.ip4 = c->leased_address,
57 .fp_proto = FIB_PROTOCOL_IP4,
58 };
Neale Ranns808c5b22017-08-02 05:15:07 -070059
khemendra kumar34719e32017-12-08 18:06:52 +053060 fib_table_entry_special_remove (fib_table_get_index_for_sw_if_index
61 (FIB_PROTOCOL_IP4, c->sw_if_index), &rx,
62 FIB_SOURCE_DHCP);
63 fib_prefix_t server = {
64 .fp_len = 32,
65 .fp_addr.ip4 = c->dhcp_server,
66 .fp_proto = FIB_PROTOCOL_IP4,
67 };
Neale Ranns808c5b22017-08-02 05:15:07 -070068
khemendra kumar34719e32017-12-08 18:06:52 +053069 fib_table_entry_special_remove (fib_table_get_index_for_sw_if_index
70 (FIB_PROTOCOL_IP4, c->sw_if_index), &server,
71 FIB_SOURCE_URPF_EXEMPT);
Neale Ranns808c5b22017-08-02 05:15:07 -070072}
73
74static void
Ed Warnickecb9cada2015-12-08 15:45:58 -070075dhcp_client_acquire_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
76{
khemendra kumar34719e32017-12-08 18:06:52 +053077 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 * Install any/all info gleaned from dhcp, right here
79 */
80 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
khemendra kumar34719e32017-12-08 18:06:52 +053081 (void *) &c->leased_address,
82 c->subnet_mask_width, 0 /*is_del */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -070083}
84
khemendra kumar34719e32017-12-08 18:06:52 +053085static void
Ed Warnickecb9cada2015-12-08 15:45:58 -070086dhcp_client_release_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
87{
khemendra kumar34719e32017-12-08 18:06:52 +053088 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 * Remove any/all info gleaned from dhcp, right here. Caller(s)
90 * have not wiped out the info yet.
91 */
92
93 ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
khemendra kumar34719e32017-12-08 18:06:52 +053094 (void *) &c->leased_address,
95 c->subnet_mask_width, 1 /*is_del */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -070096}
97
Neale Rannsb80c5362016-10-08 13:03:40 +010098static void
99set_l2_rewrite (dhcp_client_main_t * dcm, dhcp_client_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 /* Acquire the L2 rewrite string for the indicated sw_if_index */
khemendra kumar34719e32017-12-08 18:06:52 +0530102 c->l2_rewrite = vnet_build_rewrite_for_sw_interface (dcm->vnet_main,
103 c->sw_if_index,
104 VNET_LINK_IP4,
105 0 /* broadcast */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106}
107
John Lo609707e2017-09-19 21:45:10 -0400108void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
109
110static void
111dhcp_client_proc_callback (uword * client_index)
112{
113 vlib_main_t *vm = vlib_get_main ();
114 ASSERT (vlib_get_thread_index () == 0);
khemendra kumar34719e32017-12-08 18:06:52 +0530115 vlib_process_signal_event (vm, dhcp_client_process_node.index,
John Lo609707e2017-09-19 21:45:10 -0400116 EVENT_DHCP_CLIENT_WAKEUP, *client_index);
117}
118
khemendra kumar34719e32017-12-08 18:06:52 +0530119/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 * dhcp_client_for_us - server-to-client callback.
121 * Called from proxy_node.c:dhcp_proxy_to_client_input().
122 * This function first decides that the packet in question is
123 * actually for the dhcp client code in case we're also acting as
124 * a dhcp proxy. Ay caramba, what a folly!
125 */
khemendra kumar34719e32017-12-08 18:06:52 +0530126int
127dhcp_client_for_us (u32 bi, vlib_buffer_t * b,
128 ip4_header_t * ip,
129 udp_header_t * udp, dhcp_header_t * dhcp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130{
khemendra kumar34719e32017-12-08 18:06:52 +0530131 dhcp_client_main_t *dcm = &dhcp_client_main;
132 vlib_main_t *vm = dcm->vlib_main;
133 dhcp_client_t *c;
134 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 f64 now = vlib_time_now (dcm->vlib_main);
136 u8 dhcp_message_type = 0;
khemendra kumar34719e32017-12-08 18:06:52 +0530137 dhcp_option_t *o;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
khemendra kumar34719e32017-12-08 18:06:52 +0530139 /*
140 * Doing dhcp client on this interface?
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 * Presumably we will always receive dhcp clnt for-us pkts on
142 * the interface that's asking for an address.
143 */
khemendra kumar34719e32017-12-08 18:06:52 +0530144 p = hash_get (dcm->client_by_sw_if_index,
145 vnet_buffer (b)->sw_if_index[VLIB_RX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 if (p == 0)
khemendra kumar34719e32017-12-08 18:06:52 +0530147 return 0; /* no */
148
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 c = pool_elt_at_index (dcm->clients, p[0]);
150
151 /* Mixing dhcp relay and dhcp proxy? DGMS... */
152 if (c->state == DHCP_BOUND && c->retry_count == 0)
153 return 0;
154
155 /* parse through the packet, learn what we can */
156 if (dhcp->your_ip_address.as_u32)
157 c->leased_address.as_u32 = dhcp->your_ip_address.as_u32;
Neale Ranns808c5b22017-08-02 05:15:07 -0700158
159 c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32;
160
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 o = (dhcp_option_t *) dhcp->options;
khemendra kumar34719e32017-12-08 18:06:52 +0530162
163 while (o->option != 0xFF /* end of options */ &&
164 (u8 *) o < (b->data + b->current_data + b->current_length))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 {
166 switch (o->option)
khemendra kumar34719e32017-12-08 18:06:52 +0530167 {
168 case 53: /* dhcp message type */
169 dhcp_message_type = o->data[0];
170 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
khemendra kumar34719e32017-12-08 18:06:52 +0530172 case 51: /* lease time */
173 {
174 u32 lease_time_in_seconds =
175 clib_host_to_net_u32 (o->data_as_u32[0]);
176 c->lease_expires = now + (f64) lease_time_in_seconds;
177 c->lease_lifetime = lease_time_in_seconds;
178 /* Set a sensible default, in case we don't get opt 58 */
179 c->lease_renewal_interval = lease_time_in_seconds / 2;
180 }
181 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
khemendra kumar34719e32017-12-08 18:06:52 +0530183 case 58: /* lease renew time in seconds */
184 {
185 u32 lease_renew_time_in_seconds =
186 clib_host_to_net_u32 (o->data_as_u32[0]);
187 c->lease_renewal_interval = lease_renew_time_in_seconds;
188 }
189 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
khemendra kumar34719e32017-12-08 18:06:52 +0530191 case 54: /* dhcp server address */
192 c->dhcp_server.as_u32 = o->data_as_u32[0];
193 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
khemendra kumar34719e32017-12-08 18:06:52 +0530195 case 1: /* subnet mask */
196 {
197 u32 subnet_mask = clib_host_to_net_u32 (o->data_as_u32[0]);
198 c->subnet_mask_width = count_set_bits (subnet_mask);
199 }
200 break;
201 case 3: /* router address */
202 {
203 u32 router_address = o->data_as_u32[0];
204 c->router_address.as_u32 = router_address;
205 }
206 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
khemendra kumar34719e32017-12-08 18:06:52 +0530208 case 12: /* hostname */
209 {
210 /* Replace the existing hostname if necessary */
211 vec_free (c->hostname);
212 vec_validate (c->hostname, o->length - 1);
213 clib_memcpy (c->hostname, o->data, o->length);
214 }
215 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
khemendra kumar34719e32017-12-08 18:06:52 +0530217 /* $$$$ Your message in this space, parse more options */
218 default:
219 break;
220 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
222 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
223 }
224
225 switch (c->state)
226 {
227 case DHCP_DISCOVER:
228 if (dhcp_message_type != DHCP_PACKET_OFFER)
khemendra kumar34719e32017-12-08 18:06:52 +0530229 {
230 clib_warning ("sw_if_index %d state %U message type %d",
231 c->sw_if_index, format_dhcp_client_state,
232 c->state, dhcp_message_type);
233 c->next_transmit = now + 5.0;
234 break;
235 }
Neale Ranns808c5b22017-08-02 05:15:07 -0700236 /*
237 * in order to accept unicasted ACKs we need to configure the offered
238 * address on the interface. However, at this point we may not know the
239 * subnet-mask (an OFFER may not contain it). So add a temporary receice
240 * and uRPF excempt entry
241 */
242 dhcp_client_add_rx_address (dcm, c);
243
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 /* Received an offer, go send a request */
245 c->state = DHCP_REQUEST;
246 c->retry_count = 0;
khemendra kumar34719e32017-12-08 18:06:52 +0530247 c->next_transmit = 0; /* send right now... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 /* Poke the client process, which will send the request */
khemendra kumar34719e32017-12-08 18:06:52 +0530249 uword client_id = c - dcm->clients;
John Lo609707e2017-09-19 21:45:10 -0400250 vl_api_rpc_call_main_thread (dhcp_client_proc_callback,
khemendra kumar34719e32017-12-08 18:06:52 +0530251 (u8 *) & client_id, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 break;
253
254 case DHCP_BOUND:
255 case DHCP_REQUEST:
256 if (dhcp_message_type != DHCP_PACKET_ACK)
khemendra kumar34719e32017-12-08 18:06:52 +0530257 {
258 clib_warning ("sw_if_index %d state %U message type %d",
259 c->sw_if_index, format_dhcp_client_state,
260 c->state, dhcp_message_type);
261 c->next_transmit = now + 5.0;
262 break;
263 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 /* OK, we own the address (etc), add to the routing table(s) */
265 if (c->state == DHCP_REQUEST)
khemendra kumar34719e32017-12-08 18:06:52 +0530266 {
267 void (*fp) (u32, u32, u8 *, u8, u8, u8 *, u8 *, u8 *) =
268 c->event_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
khemendra kumar34719e32017-12-08 18:06:52 +0530270 /* replace the temporary RX address with the correct subnet */
271 dhcp_client_remove_rx_address (dcm, c);
272 dhcp_client_acquire_address (dcm, c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
khemendra kumar34719e32017-12-08 18:06:52 +0530274 /*
275 * Configure default IP route:
276 */
277 if (c->router_address.as_u32)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100278 {
khemendra kumar34719e32017-12-08 18:06:52 +0530279 fib_prefix_t all_0s = {
280 .fp_len = 0,
281 .fp_addr.ip4.as_u32 = 0x0,
282 .fp_proto = FIB_PROTOCOL_IP4,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100283 };
khemendra kumar34719e32017-12-08 18:06:52 +0530284 ip46_address_t nh = {
285 .ip4 = c->router_address,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100286 };
287
khemendra kumar34719e32017-12-08 18:06:52 +0530288 fib_table_entry_path_add (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, c->sw_if_index), &all_0s, FIB_SOURCE_DHCP, FIB_ENTRY_FLAG_NONE, DPO_PROTO_IP4, &nh, c->sw_if_index, ~0, 1, NULL, // no label stack
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100289 FIB_ROUTE_PATH_FLAG_NONE);
290 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
khemendra kumar34719e32017-12-08 18:06:52 +0530292 /*
293 * Call the user's event callback to report DHCP information
294 */
295 if (fp)
296 (*fp) (c->client_index, /* clinet index */
297 c->pid, c->hostname, c->subnet_mask_width, 0, /* is_ipv6 */
298 (u8 *) & c->leased_address, /* host IP address */
299 (u8 *) & c->router_address, /* router IP address */
300 (u8 *) (c->l2_rewrite + 6)); /* host MAC address */
301 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
303 c->state = DHCP_BOUND;
304 c->retry_count = 0;
305 c->next_transmit = now + (f64) c->lease_renewal_interval;
306 c->lease_expires = now + (f64) c->lease_lifetime;
307 break;
308
309 default:
khemendra kumar34719e32017-12-08 18:06:52 +0530310 clib_warning ("client %d bogus state %d", c - dcm->clients, c->state);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311 break;
312 }
313
314 /* drop the pkt, return 1 */
315 vlib_buffer_free (vm, &bi, 1);
316 return 1;
317}
318
khemendra kumar34719e32017-12-08 18:06:52 +0530319static void
320send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c,
321 dhcp_packet_type_t type, int is_broadcast)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322{
khemendra kumar34719e32017-12-08 18:06:52 +0530323 vlib_main_t *vm = dcm->vlib_main;
324 vnet_main_t *vnm = dcm->vnet_main;
325 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index);
326 vnet_sw_interface_t *sup_sw
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 = vnet_get_sup_sw_interface (vnm, c->sw_if_index);
khemendra kumar34719e32017-12-08 18:06:52 +0530328 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, c->sw_if_index);
329 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 u32 bi;
khemendra kumar34719e32017-12-08 18:06:52 +0530331 ip4_header_t *ip;
332 udp_header_t *udp;
333 dhcp_header_t *dhcp;
334 u32 *to_next;
335 vlib_frame_t *f;
336 dhcp_option_t *o;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 u16 udp_length, ip_length;
338
339 /* Interface(s) down? */
340 if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
341 return;
342 if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
343 return;
344 if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
345 return;
346
khemendra kumar34719e32017-12-08 18:06:52 +0530347 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
348 {
349 clib_warning ("buffer allocation failure");
350 c->next_transmit = 0;
351 return;
352 }
353
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354 /* Build a dhcpv4 pkt from whole cloth */
355 b = vlib_get_buffer (vm, bi);
356
357 ASSERT (b->current_data == 0);
358
khemendra kumar34719e32017-12-08 18:06:52 +0530359 vnet_buffer (b)->sw_if_index[VLIB_RX] = c->sw_if_index;
360 if (is_broadcast)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 {
362 f = vlib_get_frame_to_node (vm, hw->output_node_index);
khemendra kumar34719e32017-12-08 18:06:52 +0530363 vnet_buffer (b)->sw_if_index[VLIB_TX] = c->sw_if_index;
364 clib_memcpy (b->data, c->l2_rewrite, vec_len (c->l2_rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365 ip = (void *)
khemendra kumar34719e32017-12-08 18:06:52 +0530366 (((u8 *) vlib_buffer_get_current (b)) + vec_len (c->l2_rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 }
368 else
369 {
370 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
khemendra kumar34719e32017-12-08 18:06:52 +0530371 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; /* use interface VRF */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 ip = vlib_buffer_get_current (b);
373 }
374
375 /* Enqueue the packet right now */
376 to_next = vlib_frame_vector_args (f);
377 to_next[0] = bi;
378 f->n_vectors = 1;
379
380 if (is_broadcast)
381 vlib_put_frame_to_node (vm, hw->output_node_index, f);
382 else
383 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
khemendra kumar34719e32017-12-08 18:06:52 +0530384
385 udp = (udp_header_t *) (ip + 1);
386 dhcp = (dhcp_header_t *) (udp + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387
388 /* $$$ optimize, maybe */
389 memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp));
390
391 ip->ip_version_and_header_length = 0x45;
392 ip->ttl = 128;
393 ip->protocol = IP_PROTOCOL_UDP;
khemendra kumar34719e32017-12-08 18:06:52 +0530394
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 if (is_broadcast)
396 {
397 /* src = 0.0.0.0, dst = 255.255.255.255 */
398 ip->dst_address.as_u32 = ~0;
399 }
400 else
401 {
402 /* Renewing an active lease, plain old ip4 src/dst */
403 ip->src_address.as_u32 = c->leased_address.as_u32;
404 ip->dst_address.as_u32 = c->dhcp_server.as_u32;
405 }
406
407 udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client);
408 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server);
409
410 /* Send the interface MAC address */
Damjan Marionf1213b82016-03-13 02:22:06 +0100411 clib_memcpy (dhcp->client_hardware_address, c->l2_rewrite + 6, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412
413 /* Lease renewal, set up client_ip_address */
414 if (is_broadcast == 0)
415 dhcp->client_ip_address.as_u32 = c->leased_address.as_u32;
416
khemendra kumar34719e32017-12-08 18:06:52 +0530417 dhcp->opcode = 1; /* request, all we send */
418 dhcp->hardware_type = 1; /* ethernet */
419 dhcp->hardware_address_length = 6;
420 dhcp->transaction_identifier = c->transaction_id;
421 dhcp->flags = clib_host_to_net_u16 (is_broadcast ? DHCP_FLAG_BROADCAST : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 dhcp->magic_cookie.as_u32 = DHCP_MAGIC;
khemendra kumar34719e32017-12-08 18:06:52 +0530423
424 o = (dhcp_option_t *) dhcp->options;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
426 /* Send option 53, the DHCP message type */
Neale Ranns3466c302017-02-16 07:45:03 -0800427 o->option = DHCP_PACKET_OPTION_MSG_TYPE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 o->length = 1;
429 o->data[0] = type;
430 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
431
432 /* Send option 57, max msg length */
khemendra kumar34719e32017-12-08 18:06:52 +0530433 if (0 /* not needed, apparently */ )
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 {
435 o->option = 57;
436 o->length = 2;
437 {
khemendra kumar34719e32017-12-08 18:06:52 +0530438 u16 *o2 = (u16 *) o->data;
439 *o2 = clib_host_to_net_u16 (1152);
440 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441 }
442 }
443
444 /*
445 * If server ip address is available with non-zero value,
446 * option 54 (DHCP Server Identifier) is sent.
447 */
448 if (c->dhcp_server.as_u32)
449 {
450 o->option = 54;
451 o->length = 4;
Damjan Marionf1213b82016-03-13 02:22:06 +0100452 clib_memcpy (o->data, &c->dhcp_server.as_u32, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
454 }
455
456 /* send option 50, requested IP address */
457 if (c->leased_address.as_u32)
458 {
459 o->option = 50;
460 o->length = 4;
Damjan Marionf1213b82016-03-13 02:22:06 +0100461 clib_memcpy (o->data, &c->leased_address.as_u32, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
463 }
464
465 /* send option 12, host name */
466 if (vec_len (c->hostname))
467 {
468 o->option = 12;
469 o->length = vec_len (c->hostname);
Damjan Marionf1213b82016-03-13 02:22:06 +0100470 clib_memcpy (o->data, c->hostname, vec_len (c->hostname));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
472 }
473
Neale Ranns51822bf2017-07-18 09:26:53 -0700474 /* send option 61, client_id */
475 if (vec_len (c->client_identifier))
476 {
477 o->option = 61;
478 o->length = vec_len (c->client_identifier);
479 clib_memcpy (o->data, c->client_identifier,
khemendra kumar34719e32017-12-08 18:06:52 +0530480 vec_len (c->client_identifier));
Neale Ranns51822bf2017-07-18 09:26:53 -0700481 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
482 }
483
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484 /* $$ maybe send the client s/w version if anyone cares */
485
khemendra kumar34719e32017-12-08 18:06:52 +0530486 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 * send option 55, parameter request list
488 * The current list - see below, matches the Linux dhcp client's list
489 * Any specific dhcp server config and/or dhcp server may or may
490 * not yield specific options.
491 */
492 o->option = 55;
493 o->length = vec_len (c->option_55_data);
khemendra kumar34719e32017-12-08 18:06:52 +0530494 clib_memcpy (o->data, c->option_55_data, vec_len (c->option_55_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
496
497 /* End of list */
498 o->option = 0xff;
499 o->length = 0;
500 o++;
khemendra kumar34719e32017-12-08 18:06:52 +0530501
502 b->current_length = ((u8 *) o) - b->data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
504 /* fix ip length, checksum and udp length */
505 ip_length = vlib_buffer_length_in_chain (vm, b);
506 if (is_broadcast)
khemendra kumar34719e32017-12-08 18:06:52 +0530507 ip_length -= vec_len (c->l2_rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
khemendra kumar34719e32017-12-08 18:06:52 +0530509 ip->length = clib_host_to_net_u16 (ip_length);
510 ip->checksum = ip4_header_checksum (ip);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
512 udp_length = ip_length - (sizeof (*ip));
513 udp->length = clib_host_to_net_u16 (udp_length);
514}
515
khemendra kumar34719e32017-12-08 18:06:52 +0530516static int
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517dhcp_discover_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
518{
519 /*
520 * State machine "DISCOVER" state. Send a dhcp discover packet,
521 * eventually back off the retry rate.
522 */
khemendra kumar34719e32017-12-08 18:06:52 +0530523 send_dhcp_pkt (dcm, c, DHCP_PACKET_DISCOVER, 1 /* is_broadcast */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
525 c->retry_count++;
526 if (c->retry_count > 10)
527 c->next_transmit = now + 5.0;
528 else
529 c->next_transmit = now + 1.0;
530 return 0;
531}
khemendra kumar34719e32017-12-08 18:06:52 +0530532
533static int
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534dhcp_request_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
khemendra kumar34719e32017-12-08 18:06:52 +0530535{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 /*
537 * State machine "REQUEST" state. Send a dhcp request packet,
538 * eventually drop back to the discover state.
539 */
khemendra kumar34719e32017-12-08 18:06:52 +0530540 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 1 /* is_broadcast */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
542 c->retry_count++;
khemendra kumar34719e32017-12-08 18:06:52 +0530543 if (c->retry_count > 7 /* lucky you */ )
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 {
545 c->state = DHCP_DISCOVER;
546 c->next_transmit = now;
547 c->retry_count = 0;
548 return 1;
549 }
550 c->next_transmit = now + 1.0;
551 return 0;
552}
553
khemendra kumar34719e32017-12-08 18:06:52 +0530554static int
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555dhcp_bound_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
556{
557 /*
558 * State machine "BOUND" state. Send a dhcp request packet,
559 * eventually, when the lease expires, forget the dhcp data
560 * and go back to the stone age.
561 */
khemendra kumar34719e32017-12-08 18:06:52 +0530562 send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 0 /* is_broadcast */ );
563
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564 c->retry_count++;
565 if (c->retry_count > 10)
566 c->next_transmit = now + 5.0;
567 else
568 c->next_transmit = now + 1.0;
khemendra kumar34719e32017-12-08 18:06:52 +0530569
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 if (now > c->lease_expires)
571 {
572 if (c->router_address.as_u32)
khemendra kumar34719e32017-12-08 18:06:52 +0530573 {
574 fib_prefix_t all_0s = {
575 .fp_len = 0,
576 .fp_addr.ip4.as_u32 = 0x0,
577 .fp_proto = FIB_PROTOCOL_IP4,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100578 };
579 ip46_address_t nh = {
khemendra kumar34719e32017-12-08 18:06:52 +0530580 .ip4 = c->router_address,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100581 };
582
khemendra kumar34719e32017-12-08 18:06:52 +0530583 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
584 (FIB_PROTOCOL_IP4, c->sw_if_index),
585 &all_0s, FIB_SOURCE_DHCP,
586 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
587 1, FIB_ROUTE_PATH_FLAG_NONE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100588 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589
590 dhcp_client_release_address (dcm, c);
591 c->state = DHCP_DISCOVER;
592 c->next_transmit = now;
593 c->retry_count = 0;
594 /* Wipe out any memory of the address we had... */
595 c->leased_address.as_u32 = 0;
596 c->subnet_mask_width = 0;
597 c->router_address.as_u32 = 0;
598 c->lease_renewal_interval = 0;
599 c->dhcp_server.as_u32 = 0;
600 return 1;
601 }
602 return 0;
603}
604
khemendra kumar34719e32017-12-08 18:06:52 +0530605static f64
606dhcp_client_sm (f64 now, f64 timeout, uword pool_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607{
khemendra kumar34719e32017-12-08 18:06:52 +0530608 dhcp_client_main_t *dcm = &dhcp_client_main;
609 dhcp_client_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610
611 /* deleted, pooched, yadda yadda yadda */
612 if (pool_is_free_index (dcm->clients, pool_index))
613 return timeout;
614
615 c = pool_elt_at_index (dcm->clients, pool_index);
616
617 /* Time for us to do something with this client? */
618 if (now < c->next_transmit)
619 return timeout;
620
khemendra kumar34719e32017-12-08 18:06:52 +0530621again:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622 switch (c->state)
623 {
khemendra kumar34719e32017-12-08 18:06:52 +0530624 case DHCP_DISCOVER: /* send a discover */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 if (dhcp_discover_state (dcm, c, now))
khemendra kumar34719e32017-12-08 18:06:52 +0530626 goto again;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627 break;
628
khemendra kumar34719e32017-12-08 18:06:52 +0530629 case DHCP_REQUEST: /* send a request */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 if (dhcp_request_state (dcm, c, now))
khemendra kumar34719e32017-12-08 18:06:52 +0530631 goto again;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632 break;
khemendra kumar34719e32017-12-08 18:06:52 +0530633
634 case DHCP_BOUND: /* bound, renew needed? */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635 if (dhcp_bound_state (dcm, c, now))
khemendra kumar34719e32017-12-08 18:06:52 +0530636 goto again;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 break;
khemendra kumar34719e32017-12-08 18:06:52 +0530638
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 default:
khemendra kumar34719e32017-12-08 18:06:52 +0530640 clib_warning ("dhcp client %d bogus state %d",
641 c - dcm->clients, c->state);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642 break;
643 }
khemendra kumar34719e32017-12-08 18:06:52 +0530644
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 if (c->next_transmit < now + timeout)
646 return c->next_transmit - now;
647
648 return timeout;
649}
650
651static uword
652dhcp_client_process (vlib_main_t * vm,
khemendra kumar34719e32017-12-08 18:06:52 +0530653 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654{
655 f64 timeout = 100.0;
656 f64 now;
657 uword event_type;
khemendra kumar34719e32017-12-08 18:06:52 +0530658 uword *event_data = 0;
659 dhcp_client_main_t *dcm = &dhcp_client_main;
660 dhcp_client_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 int i;
662
663 while (1)
664 {
665 vlib_process_wait_for_event_or_clock (vm, timeout);
666
667 event_type = vlib_process_get_events (vm, &event_data);
668
669 now = vlib_time_now (vm);
670
671 switch (event_type)
khemendra kumar34719e32017-12-08 18:06:52 +0530672 {
673 case EVENT_DHCP_CLIENT_WAKEUP:
674 for (i = 0; i < vec_len (event_data); i++)
675 timeout = dhcp_client_sm (now, timeout, event_data[i]);
676 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677
khemendra kumar34719e32017-12-08 18:06:52 +0530678 case ~0:
679 pool_foreach (c, dcm->clients, (
680 {
681 timeout =
682 dhcp_client_sm (now, timeout,
683 (uword) (c -
684 dcm->clients));
685 }
686 ));
687 if (pool_elts (dcm->clients) == 0)
688 timeout = 100.0;
689 break;
690 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
692 vec_reset_length (event_data);
693 }
694
695 /* NOTREACHED */
696 return 0;
697}
698
khemendra kumar34719e32017-12-08 18:06:52 +0530699/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700VLIB_REGISTER_NODE (dhcp_client_process_node,static) = {
701 .function = dhcp_client_process,
702 .type = VLIB_NODE_TYPE_PROCESS,
703 .name = "dhcp-client-process",
704 .process_log2_n_stack_bytes = 16,
705};
khemendra kumar34719e32017-12-08 18:06:52 +0530706/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
khemendra kumar34719e32017-12-08 18:06:52 +0530708static u8 *
709format_dhcp_client_state (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710{
711 dhcp_client_state_t state = va_arg (*va, dhcp_client_state_t);
khemendra kumar34719e32017-12-08 18:06:52 +0530712 char *str = "BOGUS!";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713
714 switch (state)
715 {
716#define _(a) \
717 case a: \
718 str = #a; \
719 break;
720 foreach_dhcp_client_state;
721#undef _
722 default:
723 break;
724 }
725
726 s = format (s, "%s", str);
727 return s;
728}
729
khemendra kumar34719e32017-12-08 18:06:52 +0530730static u8 *
731format_dhcp_client (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732{
khemendra kumar34719e32017-12-08 18:06:52 +0530733 dhcp_client_main_t *dcm = va_arg (*va, dhcp_client_main_t *);
734 dhcp_client_t *c = va_arg (*va, dhcp_client_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735 int verbose = va_arg (*va, int);
736
khemendra kumar34719e32017-12-08 18:06:52 +0530737 s = format (s, "[%d] %U state %U ", c - dcm->clients,
738 format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index,
739 format_dhcp_client_state, c->state);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740
741 if (c->leased_address.as_u32)
742 s = format (s, "addr %U/%d gw %U\n",
khemendra kumar34719e32017-12-08 18:06:52 +0530743 format_ip4_address, &c->leased_address,
744 c->subnet_mask_width, format_ip4_address, &c->router_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 else
746 s = format (s, "no address\n");
747
748 if (verbose)
749 {
750 s = format (s, "retry count %d, next xmt %.2f",
khemendra kumar34719e32017-12-08 18:06:52 +0530751 c->retry_count, c->next_transmit);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 }
753 return s;
754}
755
756static clib_error_t *
757show_dhcp_client_command_fn (vlib_main_t * vm,
khemendra kumar34719e32017-12-08 18:06:52 +0530758 unformat_input_t * input,
759 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760{
khemendra kumar34719e32017-12-08 18:06:52 +0530761 dhcp_client_main_t *dcm = &dhcp_client_main;
762 dhcp_client_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763 int verbose = 0;
764 u32 sw_if_index = ~0;
khemendra kumar34719e32017-12-08 18:06:52 +0530765 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
khemendra kumar34719e32017-12-08 18:06:52 +0530767 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 {
khemendra kumar34719e32017-12-08 18:06:52 +0530769 if (unformat (input, "intfc %U",
770 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
771 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772 else if (unformat (input, "verbose"))
khemendra kumar34719e32017-12-08 18:06:52 +0530773 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774 else
khemendra kumar34719e32017-12-08 18:06:52 +0530775 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776 }
777
778 if (sw_if_index != ~0)
779 {
780 p = hash_get (dcm->client_by_sw_if_index, sw_if_index);
781 if (p == 0)
khemendra kumar34719e32017-12-08 18:06:52 +0530782 return clib_error_return (0, "dhcp client not configured");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100783 c = pool_elt_at_index (dcm->clients, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784 vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
785 return 0;
786 }
787
khemendra kumar34719e32017-12-08 18:06:52 +0530788 pool_foreach (c, dcm->clients, (
789 {
790 vlib_cli_output (vm, "%U",
791 format_dhcp_client, dcm,
792 c, verbose);
793 }
794 ));
795
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 return 0;
797}
798
khemendra kumar34719e32017-12-08 18:06:52 +0530799/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800VLIB_CLI_COMMAND (show_dhcp_client_command, static) = {
801 .path = "show dhcp client",
802 .short_help = "show dhcp client [intfc <intfc>][verbose]",
803 .function = show_dhcp_client_command_fn,
804};
khemendra kumar34719e32017-12-08 18:06:52 +0530805/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806
807
khemendra kumar34719e32017-12-08 18:06:52 +0530808int
809dhcp_client_add_del (dhcp_client_add_del_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810{
khemendra kumar34719e32017-12-08 18:06:52 +0530811 dhcp_client_main_t *dcm = &dhcp_client_main;
812 vlib_main_t *vm = dcm->vlib_main;
813 dhcp_client_t *c;
814 uword *p;
815 fib_prefix_t all_1s = {
816 .fp_len = 32,
817 .fp_addr.ip4.as_u32 = 0xffffffff,
818 .fp_proto = FIB_PROTOCOL_IP4,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100819 };
khemendra kumar34719e32017-12-08 18:06:52 +0530820 fib_prefix_t all_0s = {
821 .fp_len = 0,
822 .fp_addr.ip4.as_u32 = 0x0,
823 .fp_proto = FIB_PROTOCOL_IP4,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100824 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825
826 p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index);
827
828 if ((p && a->is_add) || (!p && a->is_add == 0))
829 return VNET_API_ERROR_INVALID_VALUE;
830
831 if (a->is_add)
832 {
833 pool_get (dcm->clients, c);
834 memset (c, 0, sizeof (*c));
835 c->state = DHCP_DISCOVER;
836 c->sw_if_index = a->sw_if_index;
837 c->client_index = a->client_index;
838 c->pid = a->pid;
839 c->event_callback = a->event_callback;
840 c->option_55_data = a->option_55_data;
841 c->hostname = a->hostname;
842 c->client_identifier = a->client_identifier;
khemendra kumar34719e32017-12-08 18:06:52 +0530843 do
844 {
845 c->transaction_id = random_u32 (&dcm->seed);
846 }
847 while (c->transaction_id == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848 set_l2_rewrite (dcm, c);
849 hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100850
851 /* this add is ref counted by FIB so we can add for each itf */
khemendra kumar34719e32017-12-08 18:06:52 +0530852 fib_table_entry_special_add (fib_table_get_index_for_sw_if_index
853 (FIB_PROTOCOL_IP4, c->sw_if_index),
854 &all_1s, FIB_SOURCE_DHCP,
855 FIB_ENTRY_FLAG_LOCAL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100856
khemendra kumar34719e32017-12-08 18:06:52 +0530857 /*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100858 * enable the interface to RX IPv4 packets
859 * this is also ref counted
860 */
861 ip4_sw_interface_enable_disable (c->sw_if_index, 1);
862
khemendra kumar34719e32017-12-08 18:06:52 +0530863 vlib_process_signal_event (vm, dhcp_client_process_node.index,
864 EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865 }
866 else
867 {
868 c = pool_elt_at_index (dcm->clients, p[0]);
869
khemendra kumar34719e32017-12-08 18:06:52 +0530870 fib_table_entry_special_remove (fib_table_get_index_for_sw_if_index
871 (FIB_PROTOCOL_IP4, c->sw_if_index),
872 &all_1s, FIB_SOURCE_DHCP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100873
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 if (c->router_address.as_u32)
khemendra kumar34719e32017-12-08 18:06:52 +0530875 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100876 ip46_address_t nh = {
khemendra kumar34719e32017-12-08 18:06:52 +0530877 .ip4 = c->router_address,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100878 };
879
khemendra kumar34719e32017-12-08 18:06:52 +0530880 fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
881 (FIB_PROTOCOL_IP4, c->sw_if_index),
882 &all_0s, FIB_SOURCE_DHCP,
883 DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
884 1, FIB_ROUTE_PATH_FLAG_NONE);
885 }
Neale Ranns808c5b22017-08-02 05:15:07 -0700886 dhcp_client_remove_rx_address (dcm, c);
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700887 dhcp_client_release_address (dcm, c);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100888 ip4_sw_interface_enable_disable (c->sw_if_index, 0);
889
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890 vec_free (c->option_55_data);
891 vec_free (c->hostname);
892 vec_free (c->client_identifier);
893 vec_free (c->l2_rewrite);
894 hash_unset (dcm->client_by_sw_if_index, c->sw_if_index);
895 pool_put (dcm->clients, c);
896 }
897 return 0;
898}
899
900int
901dhcp_client_config (vlib_main_t * vm,
khemendra kumar34719e32017-12-08 18:06:52 +0530902 u32 sw_if_index,
903 u8 * hostname,
904 u8 * client_id,
905 u32 is_add,
906 u32 client_index, void *event_callback, u32 pid)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907{
908 dhcp_client_add_del_args_t _a, *a = &_a;
909 int rv;
910
911 memset (a, 0, sizeof (*a));
912 a->is_add = is_add;
913 a->sw_if_index = sw_if_index;
914 a->client_index = client_index;
915 a->pid = pid;
916 a->event_callback = event_callback;
khemendra kumar34719e32017-12-08 18:06:52 +0530917 vec_validate (a->hostname, strlen ((char *) hostname) - 1);
918 strncpy ((char *) a->hostname, (char *) hostname, vec_len (a->hostname));
919 vec_validate (a->client_identifier, strlen ((char *) client_id) - 1);
920 strncpy ((char *) a->client_identifier, (char *) client_id,
921 vec_len (a->client_identifier));
Neale Ranns51822bf2017-07-18 09:26:53 -0700922
khemendra kumar34719e32017-12-08 18:06:52 +0530923 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924 * Option 55 request list. These data precisely match
925 * the Ubuntu dhcp client. YMMV.
926 */
927
928 /* Subnet Mask */
929 vec_add1 (a->option_55_data, 1);
930 /* Broadcast address */
931 vec_add1 (a->option_55_data, 28);
932 /* time offset */
933 vec_add1 (a->option_55_data, 2);
934 /* Router */
935 vec_add1 (a->option_55_data, 3);
936 /* Domain Name */
937 vec_add1 (a->option_55_data, 15);
938 /* DNS */
939 vec_add1 (a->option_55_data, 6);
940 /* Domain search */
941 vec_add1 (a->option_55_data, 119);
942 /* Host name */
943 vec_add1 (a->option_55_data, 12);
944 /* NetBIOS name server */
945 vec_add1 (a->option_55_data, 44);
946 /* NetBIOS Scope */
947 vec_add1 (a->option_55_data, 47);
948 /* MTU */
949 vec_add1 (a->option_55_data, 26);
950 /* Classless static route */
951 vec_add1 (a->option_55_data, 121);
952 /* NTP servers */
953 vec_add1 (a->option_55_data, 42);
954
955 rv = dhcp_client_add_del (a);
956
957 switch (rv)
958 {
959 case 0:
960 break;
961
962 case VNET_API_ERROR_INVALID_VALUE:
963
964 vec_free (a->hostname);
965 vec_free (a->client_identifier);
966 vec_free (a->option_55_data);
967
968 if (is_add)
khemendra kumar34719e32017-12-08 18:06:52 +0530969 clib_warning ("dhcp client already enabled on intf_idx %d",
970 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 else
khemendra kumar34719e32017-12-08 18:06:52 +0530972 clib_warning ("dhcp client not enabled on on intf_idx %d",
973 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974 break;
975
976 default:
977 clib_warning ("dhcp_client_add_del returned %d", rv);
978 }
979
980 return rv;
981}
982
983static clib_error_t *
984dhcp_client_set_command_fn (vlib_main_t * vm,
khemendra kumar34719e32017-12-08 18:06:52 +0530985 unformat_input_t * input,
986 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987{
988
khemendra kumar34719e32017-12-08 18:06:52 +0530989 dhcp_client_main_t *dcm = &dhcp_client_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990 u32 sw_if_index;
khemendra kumar34719e32017-12-08 18:06:52 +0530991 u8 *hostname = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992 u8 sw_if_index_set = 0;
993 int is_add = 1;
994 dhcp_client_add_del_args_t _a, *a = &_a;
995 int rv;
996
khemendra kumar34719e32017-12-08 18:06:52 +0530997 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998 {
khemendra kumar34719e32017-12-08 18:06:52 +0530999 if (unformat (input, "intfc %U",
1000 unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
1001 sw_if_index_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 else if (unformat (input, "hostname %v", &hostname))
khemendra kumar34719e32017-12-08 18:06:52 +05301003 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 else if (unformat (input, "del"))
khemendra kumar34719e32017-12-08 18:06:52 +05301005 is_add = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 else
khemendra kumar34719e32017-12-08 18:06:52 +05301007 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 }
1009
1010 if (sw_if_index_set == 0)
1011 return clib_error_return (0, "interface not specified");
1012
1013 memset (a, 0, sizeof (*a));
1014 a->is_add = is_add;
1015 a->sw_if_index = sw_if_index;
1016 a->hostname = hostname;
1017 a->client_identifier = format (0, "vpe 1.0%c", 0);
1018
khemendra kumar34719e32017-12-08 18:06:52 +05301019 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020 * Option 55 request list. These data precisely match
1021 * the Ubuntu dhcp client. YMMV.
1022 */
1023
1024 /* Subnet Mask */
1025 vec_add1 (a->option_55_data, 1);
1026 /* Broadcast address */
1027 vec_add1 (a->option_55_data, 28);
1028 /* time offset */
1029 vec_add1 (a->option_55_data, 2);
1030 /* Router */
1031 vec_add1 (a->option_55_data, 3);
1032 /* Domain Name */
1033 vec_add1 (a->option_55_data, 15);
1034 /* DNS */
1035 vec_add1 (a->option_55_data, 6);
1036 /* Domain search */
1037 vec_add1 (a->option_55_data, 119);
1038 /* Host name */
1039 vec_add1 (a->option_55_data, 12);
1040 /* NetBIOS name server */
1041 vec_add1 (a->option_55_data, 44);
1042 /* NetBIOS Scope */
1043 vec_add1 (a->option_55_data, 47);
1044 /* MTU */
1045 vec_add1 (a->option_55_data, 26);
1046 /* Classless static route */
1047 vec_add1 (a->option_55_data, 121);
1048 /* NTP servers */
1049 vec_add1 (a->option_55_data, 42);
1050
1051 rv = dhcp_client_add_del (a);
1052
1053 switch (rv)
1054 {
1055 case 0:
1056 break;
1057
1058 case VNET_API_ERROR_INVALID_VALUE:
1059
1060 vec_free (a->hostname);
1061 vec_free (a->client_identifier);
1062 vec_free (a->option_55_data);
1063 if (is_add)
khemendra kumar34719e32017-12-08 18:06:52 +05301064 return clib_error_return (0, "dhcp client already enabled on %U",
1065 format_vnet_sw_if_index_name,
1066 dcm->vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067 else
khemendra kumar34719e32017-12-08 18:06:52 +05301068 return clib_error_return (0, "dhcp client not enabled on %U",
1069 format_vnet_sw_if_index_name,
1070 dcm->vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001071 break;
1072
1073 default:
1074 vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv);
1075 }
1076
1077 return 0;
1078}
1079
khemendra kumar34719e32017-12-08 18:06:52 +05301080/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081VLIB_CLI_COMMAND (dhcp_client_set_command, static) = {
1082 .path = "set dhcp client",
1083 .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]",
1084 .function = dhcp_client_set_command_fn,
1085};
khemendra kumar34719e32017-12-08 18:06:52 +05301086/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087
1088static clib_error_t *
1089dhcp_client_init (vlib_main_t * vm)
1090{
khemendra kumar34719e32017-12-08 18:06:52 +05301091 dhcp_client_main_t *dcm = &dhcp_client_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092
1093 dcm->vlib_main = vm;
khemendra kumar34719e32017-12-08 18:06:52 +05301094 dcm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 dcm->seed = 0xdeaddabe;
1096 return 0;
1097}
1098
1099VLIB_INIT_FUNCTION (dhcp_client_init);
khemendra kumar34719e32017-12-08 18:06:52 +05301100
1101/*
1102 * fd.io coding-style-patch-verification: ON
1103 *
1104 * Local Variables:
1105 * eval: (c-set-style "gnu")
1106 * End:
1107 */