Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 16 | #include <vlibmemory/api.h> |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 17 | #include <vnet/dhcp/client.h> |
Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 18 | #include <vnet/dhcp/dhcp_proxy.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 19 | #include <vnet/fib/fib_table.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | |
| 21 | dhcp_client_main_t dhcp_client_main; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 22 | static u8 *format_dhcp_client_state (u8 * s, va_list * va); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | static vlib_node_registration_t dhcp_client_process_node; |
| 24 | |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 25 | #define foreach_dhcp_sent_packet_stat \ |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 26 | _(DISCOVER, "DHCP discover packets sent") \ |
| 27 | _(OFFER, "DHCP offer packets sent") \ |
| 28 | _(REQUEST, "DHCP request packets sent") \ |
| 29 | _(ACK, "DHCP ack packets sent") |
| 30 | |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 31 | #define foreach_dhcp_error_counter \ |
| 32 | _(NOT_FOR_US, "DHCP packets for other hosts, dropped") \ |
| 33 | _(NAK, "DHCP nak packets received") \ |
| 34 | _(NON_OFFER_DISCOVER, "DHCP non-offer packets in discover state") \ |
| 35 | _(ODDBALL, "DHCP non-ack, non-offer packets received") \ |
| 36 | _(BOUND, "DHCP bind success") |
| 37 | |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 38 | typedef enum |
| 39 | { |
| 40 | #define _(sym,str) DHCP_STAT_##sym, |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 41 | foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 42 | #undef _ |
| 43 | DHCP_STAT_UNKNOWN, |
| 44 | DHCP_STAT_N_STAT, |
| 45 | } sample_error_t; |
| 46 | |
| 47 | static char *dhcp_client_process_stat_strings[] = { |
| 48 | #define _(sym,string) string, |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 49 | foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 50 | #undef _ |
| 51 | "DHCP unknown packets sent", |
| 52 | }; |
| 53 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 54 | static void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | dhcp_client_acquire_address (dhcp_client_main_t * dcm, dhcp_client_t * c) |
| 56 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 57 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | * Install any/all info gleaned from dhcp, right here |
| 59 | */ |
| 60 | ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index, |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 61 | (void *) &c->leased_address, |
| 62 | c->subnet_mask_width, 0 /*is_del */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | } |
| 64 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 65 | static void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | dhcp_client_release_address (dhcp_client_main_t * dcm, dhcp_client_t * c) |
| 67 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 68 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | * 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 kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 74 | (void *) &c->leased_address, |
| 75 | c->subnet_mask_width, 1 /*is_del */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 78 | static void |
John Lo | 609707e | 2017-09-19 21:45:10 -0400 | [diff] [blame] | 79 | dhcp_client_proc_callback (uword * client_index) |
| 80 | { |
| 81 | vlib_main_t *vm = vlib_get_main (); |
| 82 | ASSERT (vlib_get_thread_index () == 0); |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 83 | vlib_process_signal_event (vm, dhcp_client_process_node.index, |
John Lo | 609707e | 2017-09-19 21:45:10 -0400 | [diff] [blame] | 84 | EVENT_DHCP_CLIENT_WAKEUP, *client_index); |
| 85 | } |
| 86 | |
Matthew Smith | 7a19283 | 2018-02-10 11:11:45 -0600 | [diff] [blame] | 87 | static void |
| 88 | dhcp_client_addr_callback (dhcp_client_t * c) |
| 89 | { |
| 90 | dhcp_client_main_t *dcm = &dhcp_client_main; |
Matthew Smith | 7a19283 | 2018-02-10 11:11:45 -0600 | [diff] [blame] | 91 | |
Matthew Smith | 7cb7bb3 | 2018-05-07 07:52:12 -0500 | [diff] [blame] | 92 | /* disable the feature */ |
Matthew Smith | 7a19283 | 2018-02-10 11:11:45 -0600 | [diff] [blame] | 93 | vnet_feature_enable_disable ("ip4-unicast", |
| 94 | "ip4-dhcp-client-detect", |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 95 | c->sw_if_index, 0 /* disable */ , 0, 0); |
Dave Barach | 525c9d0 | 2018-05-26 10:48:55 -0400 | [diff] [blame] | 96 | c->client_detect_feature_enabled = 0; |
Matthew Smith | 7a19283 | 2018-02-10 11:11:45 -0600 | [diff] [blame] | 97 | |
Matthew Smith | 7cb7bb3 | 2018-05-07 07:52:12 -0500 | [diff] [blame] | 98 | /* if renewing the lease, the address and route have already been added */ |
| 99 | if (c->state == DHCP_BOUND) |
| 100 | return; |
| 101 | |
| 102 | /* add the address to the interface */ |
| 103 | dhcp_client_acquire_address (dcm, c); |
| 104 | |
Matthew Smith | 7a19283 | 2018-02-10 11:11:45 -0600 | [diff] [blame] | 105 | /* |
| 106 | * Configure default IP route: |
| 107 | */ |
| 108 | if (c->router_address.as_u32) |
| 109 | { |
| 110 | fib_prefix_t all_0s = { |
| 111 | .fp_len = 0, |
| 112 | .fp_addr.ip4.as_u32 = 0x0, |
| 113 | .fp_proto = FIB_PROTOCOL_IP4, |
| 114 | }; |
| 115 | ip46_address_t nh = { |
| 116 | .ip4 = c->router_address, |
| 117 | }; |
| 118 | |
| 119 | /* *INDENT-OFF* */ |
| 120 | fib_table_entry_path_add ( |
| 121 | fib_table_get_index_for_sw_if_index ( |
| 122 | FIB_PROTOCOL_IP4, |
| 123 | c->sw_if_index), |
| 124 | &all_0s, |
| 125 | FIB_SOURCE_DHCP, |
| 126 | FIB_ENTRY_FLAG_NONE, |
| 127 | DPO_PROTO_IP4, |
| 128 | &nh, c->sw_if_index, |
| 129 | ~0, 1, NULL, // no label stack |
| 130 | FIB_ROUTE_PATH_FLAG_NONE); |
| 131 | /* *INDENT-ON* */ |
| 132 | } |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 133 | if (c->dhcp_server.as_u32) |
| 134 | { |
| 135 | ip46_address_t dst = { |
| 136 | .ip4 = c->dhcp_server, |
| 137 | }; |
| 138 | c->ai_ucast = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, |
| 139 | VNET_LINK_IP4, &dst, c->sw_if_index); |
| 140 | } |
Matthew Smith | 7a19283 | 2018-02-10 11:11:45 -0600 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Call the user's event callback to report DHCP information |
| 144 | */ |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 145 | if (c->event_callback) |
| 146 | c->event_callback (c->client_index, c); |
Matthew Smith | 7a19283 | 2018-02-10 11:11:45 -0600 | [diff] [blame] | 147 | } |
| 148 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 149 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | * dhcp_client_for_us - server-to-client callback. |
| 151 | * Called from proxy_node.c:dhcp_proxy_to_client_input(). |
| 152 | * This function first decides that the packet in question is |
| 153 | * actually for the dhcp client code in case we're also acting as |
| 154 | * a dhcp proxy. Ay caramba, what a folly! |
| 155 | */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 156 | int |
| 157 | dhcp_client_for_us (u32 bi, vlib_buffer_t * b, |
| 158 | ip4_header_t * ip, |
| 159 | udp_header_t * udp, dhcp_header_t * dhcp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 161 | dhcp_client_main_t *dcm = &dhcp_client_main; |
| 162 | vlib_main_t *vm = dcm->vlib_main; |
| 163 | dhcp_client_t *c; |
| 164 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | f64 now = vlib_time_now (dcm->vlib_main); |
| 166 | u8 dhcp_message_type = 0; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 167 | dhcp_option_t *o; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 169 | /* |
| 170 | * Doing dhcp client on this interface? |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | * Presumably we will always receive dhcp clnt for-us pkts on |
| 172 | * the interface that's asking for an address. |
| 173 | */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 174 | p = hash_get (dcm->client_by_sw_if_index, |
| 175 | vnet_buffer (b)->sw_if_index[VLIB_RX]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | if (p == 0) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 177 | return 0; /* no */ |
| 178 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | c = pool_elt_at_index (dcm->clients, p[0]); |
| 180 | |
| 181 | /* Mixing dhcp relay and dhcp proxy? DGMS... */ |
| 182 | if (c->state == DHCP_BOUND && c->retry_count == 0) |
| 183 | return 0; |
| 184 | |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 185 | /* Packet not for us? Turf it... */ |
| 186 | if (memcmp (dhcp->client_hardware_address, c->client_hardware_address, |
| 187 | sizeof (c->client_hardware_address))) |
| 188 | { |
| 189 | vlib_node_increment_counter (vm, dhcp_client_process_node.index, |
| 190 | DHCP_STAT_NOT_FOR_US, 1); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | /* parse through the packet, learn what we can */ |
| 195 | if (dhcp->your_ip_address.as_u32) |
| 196 | c->leased_address.as_u32 = dhcp->your_ip_address.as_u32; |
Neale Ranns | 808c5b2 | 2017-08-02 05:15:07 -0700 | [diff] [blame] | 197 | |
| 198 | c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32; |
| 199 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | o = (dhcp_option_t *) dhcp->options; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 201 | |
| 202 | while (o->option != 0xFF /* end of options */ && |
| 203 | (u8 *) o < (b->data + b->current_data + b->current_length)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | { |
| 205 | switch (o->option) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 206 | { |
| 207 | case 53: /* dhcp message type */ |
| 208 | dhcp_message_type = o->data[0]; |
| 209 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 211 | case 51: /* lease time */ |
| 212 | { |
| 213 | u32 lease_time_in_seconds = |
| 214 | clib_host_to_net_u32 (o->data_as_u32[0]); |
Dave Barach | 525c9d0 | 2018-05-26 10:48:55 -0400 | [diff] [blame] | 215 | // for debug: lease_time_in_seconds = 20; /*$$$$*/ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 216 | c->lease_expires = now + (f64) lease_time_in_seconds; |
| 217 | c->lease_lifetime = lease_time_in_seconds; |
| 218 | /* Set a sensible default, in case we don't get opt 58 */ |
| 219 | c->lease_renewal_interval = lease_time_in_seconds / 2; |
| 220 | } |
| 221 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 223 | case 58: /* lease renew time in seconds */ |
| 224 | { |
| 225 | u32 lease_renew_time_in_seconds = |
| 226 | clib_host_to_net_u32 (o->data_as_u32[0]); |
| 227 | c->lease_renewal_interval = lease_renew_time_in_seconds; |
| 228 | } |
| 229 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 231 | case 54: /* dhcp server address */ |
| 232 | c->dhcp_server.as_u32 = o->data_as_u32[0]; |
| 233 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 235 | case 1: /* subnet mask */ |
| 236 | { |
| 237 | u32 subnet_mask = clib_host_to_net_u32 (o->data_as_u32[0]); |
| 238 | c->subnet_mask_width = count_set_bits (subnet_mask); |
| 239 | } |
| 240 | break; |
| 241 | case 3: /* router address */ |
| 242 | { |
| 243 | u32 router_address = o->data_as_u32[0]; |
| 244 | c->router_address.as_u32 = router_address; |
| 245 | } |
| 246 | break; |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 247 | case 6: /* domain server address */ |
| 248 | { |
| 249 | vec_free (c->domain_server_address); |
| 250 | vec_validate (c->domain_server_address, |
| 251 | o->length / sizeof (ip4_address_t) - 1); |
| 252 | clib_memcpy (c->domain_server_address, o->data, o->length); |
| 253 | } |
| 254 | break; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 255 | case 12: /* hostname */ |
| 256 | { |
| 257 | /* Replace the existing hostname if necessary */ |
| 258 | vec_free (c->hostname); |
| 259 | vec_validate (c->hostname, o->length - 1); |
| 260 | clib_memcpy (c->hostname, o->data, o->length); |
| 261 | } |
| 262 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 264 | /* $$$$ Your message in this space, parse more options */ |
| 265 | default: |
| 266 | break; |
| 267 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
| 269 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 270 | } |
| 271 | |
| 272 | switch (c->state) |
| 273 | { |
| 274 | case DHCP_DISCOVER: |
| 275 | if (dhcp_message_type != DHCP_PACKET_OFFER) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 276 | { |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 277 | vlib_node_increment_counter (vm, dhcp_client_process_node.index, |
| 278 | DHCP_STAT_NON_OFFER_DISCOVER, 1); |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 279 | c->next_transmit = now + 5.0; |
| 280 | break; |
| 281 | } |
Neale Ranns | 808c5b2 | 2017-08-02 05:15:07 -0700 | [diff] [blame] | 282 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | /* Received an offer, go send a request */ |
| 284 | c->state = DHCP_REQUEST; |
| 285 | c->retry_count = 0; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 286 | c->next_transmit = 0; /* send right now... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | /* Poke the client process, which will send the request */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 288 | uword client_id = c - dcm->clients; |
John Lo | 609707e | 2017-09-19 21:45:10 -0400 | [diff] [blame] | 289 | vl_api_rpc_call_main_thread (dhcp_client_proc_callback, |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 290 | (u8 *) & client_id, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | break; |
| 292 | |
| 293 | case DHCP_BOUND: |
| 294 | case DHCP_REQUEST: |
Dave Barach | 43ebe29 | 2018-03-21 13:45:12 -0400 | [diff] [blame] | 295 | if (dhcp_message_type == DHCP_PACKET_NAK) |
| 296 | { |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 297 | vlib_node_increment_counter (vm, dhcp_client_process_node.index, |
| 298 | DHCP_STAT_NAK, 1); |
Dave Barach | 43ebe29 | 2018-03-21 13:45:12 -0400 | [diff] [blame] | 299 | /* Probably never happens in bound state, but anyhow... */ |
| 300 | if (c->state == DHCP_BOUND) |
| 301 | { |
| 302 | ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index, |
| 303 | (void *) &c->leased_address, |
| 304 | c->subnet_mask_width, |
| 305 | 1 /*is_del */ ); |
| 306 | vnet_feature_enable_disable ("ip4-unicast", |
| 307 | "ip4-dhcp-client-detect", |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 308 | c->sw_if_index, 1 /* enable */ , |
| 309 | 0, 0); |
Dave Barach | 525c9d0 | 2018-05-26 10:48:55 -0400 | [diff] [blame] | 310 | c->client_detect_feature_enabled = 1; |
Dave Barach | 43ebe29 | 2018-03-21 13:45:12 -0400 | [diff] [blame] | 311 | } |
| 312 | /* Wipe out any memory of the address we had... */ |
| 313 | c->state = DHCP_DISCOVER; |
| 314 | c->next_transmit = now; |
| 315 | c->retry_count = 0; |
| 316 | c->leased_address.as_u32 = 0; |
| 317 | c->subnet_mask_width = 0; |
| 318 | c->router_address.as_u32 = 0; |
| 319 | c->lease_renewal_interval = 0; |
| 320 | c->dhcp_server.as_u32 = 0; |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 321 | vec_free (c->domain_server_address); |
Dave Barach | 43ebe29 | 2018-03-21 13:45:12 -0400 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | |
| 325 | if (dhcp_message_type != DHCP_PACKET_ACK && |
| 326 | dhcp_message_type != DHCP_PACKET_OFFER) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 327 | { |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 328 | vlib_node_increment_counter (vm, dhcp_client_process_node.index, |
| 329 | DHCP_STAT_NON_OFFER_DISCOVER, 1); |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 330 | clib_warning ("sw_if_index %d state %U message type %d", |
| 331 | c->sw_if_index, format_dhcp_client_state, |
| 332 | c->state, dhcp_message_type); |
| 333 | c->next_transmit = now + 5.0; |
| 334 | break; |
| 335 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | /* OK, we own the address (etc), add to the routing table(s) */ |
Matthew Smith | 7cb7bb3 | 2018-05-07 07:52:12 -0500 | [diff] [blame] | 337 | vl_api_rpc_call_main_thread (dhcp_client_addr_callback, |
| 338 | (u8 *) c, sizeof (*c)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | |
| 340 | c->state = DHCP_BOUND; |
| 341 | c->retry_count = 0; |
| 342 | c->next_transmit = now + (f64) c->lease_renewal_interval; |
| 343 | c->lease_expires = now + (f64) c->lease_lifetime; |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 344 | vlib_node_increment_counter (vm, dhcp_client_process_node.index, |
| 345 | DHCP_STAT_BOUND, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | break; |
| 347 | |
| 348 | default: |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 349 | clib_warning ("client %d bogus state %d", c - dcm->clients, c->state); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | |
| 353 | /* drop the pkt, return 1 */ |
| 354 | vlib_buffer_free (vm, &bi, 1); |
| 355 | return 1; |
| 356 | } |
| 357 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 358 | static void |
| 359 | send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c, |
| 360 | dhcp_packet_type_t type, int is_broadcast) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 362 | vlib_main_t *vm = dcm->vlib_main; |
| 363 | vnet_main_t *vnm = dcm->vnet_main; |
| 364 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index); |
| 365 | vnet_sw_interface_t *sup_sw |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | = vnet_get_sup_sw_interface (vnm, c->sw_if_index); |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 367 | vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, c->sw_if_index); |
| 368 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | u32 bi; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 370 | ip4_header_t *ip; |
| 371 | udp_header_t *udp; |
| 372 | dhcp_header_t *dhcp; |
| 373 | u32 *to_next; |
| 374 | vlib_frame_t *f; |
| 375 | dhcp_option_t *o; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | u16 udp_length, ip_length; |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 377 | u32 counter_index, node_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
| 379 | /* Interface(s) down? */ |
| 380 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0) |
| 381 | return; |
| 382 | if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0) |
| 383 | return; |
| 384 | if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0) |
| 385 | return; |
| 386 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 387 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 388 | { |
| 389 | clib_warning ("buffer allocation failure"); |
| 390 | c->next_transmit = 0; |
| 391 | return; |
| 392 | } |
| 393 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | /* Build a dhcpv4 pkt from whole cloth */ |
| 395 | b = vlib_get_buffer (vm, bi); |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 396 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | |
| 398 | ASSERT (b->current_data == 0); |
| 399 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 400 | vnet_buffer (b)->sw_if_index[VLIB_RX] = c->sw_if_index; |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 401 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 402 | if (is_broadcast) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | { |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 404 | node_index = ip4_rewrite_node.index; |
| 405 | vnet_buffer (b)->ip.adj_index[VLIB_TX] = c->ai_bcast; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | } |
| 407 | else |
| 408 | { |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 409 | ip_adjacency_t *adj = adj_get (c->ai_ucast); |
| 410 | |
| 411 | if (IP_LOOKUP_NEXT_ARP == adj->lookup_next_index) |
| 412 | node_index = ip4_arp_node.index; |
| 413 | else |
| 414 | node_index = ip4_rewrite_node.index; |
| 415 | vnet_buffer (b)->ip.adj_index[VLIB_TX] = c->ai_ucast; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* Enqueue the packet right now */ |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 419 | f = vlib_get_frame_to_node (vm, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | to_next = vlib_frame_vector_args (f); |
| 421 | to_next[0] = bi; |
| 422 | f->n_vectors = 1; |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 423 | vlib_put_frame_to_node (vm, node_index, f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 425 | /* build the headers */ |
| 426 | ip = vlib_buffer_get_current (b); |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 427 | udp = (udp_header_t *) (ip + 1); |
| 428 | dhcp = (dhcp_header_t *) (udp + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | |
| 430 | /* $$$ optimize, maybe */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 431 | clib_memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | |
| 433 | ip->ip_version_and_header_length = 0x45; |
| 434 | ip->ttl = 128; |
| 435 | ip->protocol = IP_PROTOCOL_UDP; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 436 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 437 | if (is_broadcast) |
| 438 | { |
| 439 | /* src = 0.0.0.0, dst = 255.255.255.255 */ |
| 440 | ip->dst_address.as_u32 = ~0; |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | /* Renewing an active lease, plain old ip4 src/dst */ |
| 445 | ip->src_address.as_u32 = c->leased_address.as_u32; |
| 446 | ip->dst_address.as_u32 = c->dhcp_server.as_u32; |
| 447 | } |
| 448 | |
| 449 | udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client); |
| 450 | udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server); |
| 451 | |
| 452 | /* Send the interface MAC address */ |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 453 | clib_memcpy (dhcp->client_hardware_address, |
| 454 | vnet_sw_interface_get_hw_address (vnm, c->sw_if_index), 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 456 | /* And remember it for rx-packet-for-us checking */ |
| 457 | clib_memcpy (c->client_hardware_address, dhcp->client_hardware_address, |
| 458 | sizeof (c->client_hardware_address)); |
| 459 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | /* Lease renewal, set up client_ip_address */ |
| 461 | if (is_broadcast == 0) |
| 462 | dhcp->client_ip_address.as_u32 = c->leased_address.as_u32; |
| 463 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 464 | dhcp->opcode = 1; /* request, all we send */ |
| 465 | dhcp->hardware_type = 1; /* ethernet */ |
| 466 | dhcp->hardware_address_length = 6; |
| 467 | dhcp->transaction_identifier = c->transaction_id; |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 468 | dhcp->flags = |
| 469 | clib_host_to_net_u16 (is_broadcast && c->set_broadcast_flag ? |
| 470 | DHCP_FLAG_BROADCAST : 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | dhcp->magic_cookie.as_u32 = DHCP_MAGIC; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 472 | |
| 473 | o = (dhcp_option_t *) dhcp->options; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | |
| 475 | /* Send option 53, the DHCP message type */ |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 476 | o->option = DHCP_PACKET_OPTION_MSG_TYPE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | o->length = 1; |
| 478 | o->data[0] = type; |
| 479 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 480 | |
| 481 | /* Send option 57, max msg length */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 482 | if (0 /* not needed, apparently */ ) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | { |
| 484 | o->option = 57; |
| 485 | o->length = 2; |
| 486 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 487 | u16 *o2 = (u16 *) o->data; |
| 488 | *o2 = clib_host_to_net_u16 (1152); |
| 489 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * If server ip address is available with non-zero value, |
| 495 | * option 54 (DHCP Server Identifier) is sent. |
| 496 | */ |
| 497 | if (c->dhcp_server.as_u32) |
| 498 | { |
| 499 | o->option = 54; |
| 500 | o->length = 4; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 501 | clib_memcpy (o->data, &c->dhcp_server.as_u32, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 502 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 503 | } |
| 504 | |
| 505 | /* send option 50, requested IP address */ |
| 506 | if (c->leased_address.as_u32) |
| 507 | { |
| 508 | o->option = 50; |
| 509 | o->length = 4; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 510 | clib_memcpy (o->data, &c->leased_address.as_u32, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 512 | } |
| 513 | |
| 514 | /* send option 12, host name */ |
| 515 | if (vec_len (c->hostname)) |
| 516 | { |
| 517 | o->option = 12; |
| 518 | o->length = vec_len (c->hostname); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 519 | clib_memcpy (o->data, c->hostname, vec_len (c->hostname)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 521 | } |
| 522 | |
Neale Ranns | 51822bf | 2017-07-18 09:26:53 -0700 | [diff] [blame] | 523 | /* send option 61, client_id */ |
| 524 | if (vec_len (c->client_identifier)) |
| 525 | { |
| 526 | o->option = 61; |
| 527 | o->length = vec_len (c->client_identifier); |
| 528 | clib_memcpy (o->data, c->client_identifier, |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 529 | vec_len (c->client_identifier)); |
Neale Ranns | 51822bf | 2017-07-18 09:26:53 -0700 | [diff] [blame] | 530 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 531 | } |
| 532 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | /* $$ maybe send the client s/w version if anyone cares */ |
| 534 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 535 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | * send option 55, parameter request list |
| 537 | * The current list - see below, matches the Linux dhcp client's list |
| 538 | * Any specific dhcp server config and/or dhcp server may or may |
| 539 | * not yield specific options. |
| 540 | */ |
| 541 | o->option = 55; |
| 542 | o->length = vec_len (c->option_55_data); |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 543 | clib_memcpy (o->data, c->option_55_data, vec_len (c->option_55_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 544 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 545 | |
| 546 | /* End of list */ |
| 547 | o->option = 0xff; |
| 548 | o->length = 0; |
| 549 | o++; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 550 | |
| 551 | b->current_length = ((u8 *) o) - b->data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | |
| 553 | /* fix ip length, checksum and udp length */ |
| 554 | ip_length = vlib_buffer_length_in_chain (vm, b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 555 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 556 | ip->length = clib_host_to_net_u16 (ip_length); |
| 557 | ip->checksum = ip4_header_checksum (ip); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | |
| 559 | udp_length = ip_length - (sizeof (*ip)); |
| 560 | udp->length = clib_host_to_net_u16 (udp_length); |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 561 | |
| 562 | switch (type) |
| 563 | { |
| 564 | #define _(a,b) case DHCP_PACKET_##a: {counter_index = DHCP_STAT_##a; break;} |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 565 | foreach_dhcp_sent_packet_stat |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 566 | #undef _ |
| 567 | default: |
| 568 | counter_index = DHCP_STAT_UNKNOWN; |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | vlib_node_increment_counter (vm, dhcp_client_process_node.index, |
| 573 | counter_index, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 574 | } |
| 575 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 576 | static int |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 577 | dhcp_discover_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now) |
| 578 | { |
| 579 | /* |
| 580 | * State machine "DISCOVER" state. Send a dhcp discover packet, |
| 581 | * eventually back off the retry rate. |
| 582 | */ |
Dave Barach | 525c9d0 | 2018-05-26 10:48:55 -0400 | [diff] [blame] | 583 | |
| 584 | if (c->client_detect_feature_enabled == 0) |
| 585 | { |
| 586 | vnet_feature_enable_disable ("ip4-unicast", |
| 587 | "ip4-dhcp-client-detect", |
| 588 | c->sw_if_index, 1 /* enable */ , 0, 0); |
| 589 | c->client_detect_feature_enabled = 1; |
| 590 | } |
| 591 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 592 | send_dhcp_pkt (dcm, c, DHCP_PACKET_DISCOVER, 1 /* is_broadcast */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 593 | |
| 594 | c->retry_count++; |
| 595 | if (c->retry_count > 10) |
| 596 | c->next_transmit = now + 5.0; |
| 597 | else |
| 598 | c->next_transmit = now + 1.0; |
| 599 | return 0; |
| 600 | } |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 601 | |
| 602 | static int |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 603 | dhcp_request_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 604 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 605 | /* |
| 606 | * State machine "REQUEST" state. Send a dhcp request packet, |
| 607 | * eventually drop back to the discover state. |
| 608 | */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 609 | send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 1 /* is_broadcast */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 610 | |
| 611 | c->retry_count++; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 612 | if (c->retry_count > 7 /* lucky you */ ) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | { |
| 614 | c->state = DHCP_DISCOVER; |
| 615 | c->next_transmit = now; |
| 616 | c->retry_count = 0; |
| 617 | return 1; |
| 618 | } |
| 619 | c->next_transmit = now + 1.0; |
| 620 | return 0; |
| 621 | } |
| 622 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 623 | static int |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 624 | dhcp_bound_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now) |
| 625 | { |
| 626 | /* |
Matthew Smith | 7cb7bb3 | 2018-05-07 07:52:12 -0500 | [diff] [blame] | 627 | * State machine "BOUND" state. Send a dhcp request packet to renew |
| 628 | * the lease. |
| 629 | * Eventually, when the lease expires, forget the dhcp data |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | * and go back to the stone age. |
| 631 | */ |
Matthew Smith | 7cb7bb3 | 2018-05-07 07:52:12 -0500 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | * We disable the client detect feature when we bind a |
| 635 | * DHCP address. Turn it back on again on first renew attempt. |
| 636 | * Otherwise, if the DHCP server replies we'll never see it. |
| 637 | */ |
Dave Barach | 525c9d0 | 2018-05-26 10:48:55 -0400 | [diff] [blame] | 638 | if (c->client_detect_feature_enabled == 0) |
| 639 | { |
| 640 | vnet_feature_enable_disable ("ip4-unicast", |
| 641 | "ip4-dhcp-client-detect", |
| 642 | c->sw_if_index, 1 /* enable */ , 0, 0); |
| 643 | c->client_detect_feature_enabled = 1; |
| 644 | } |
Matthew Smith | 7cb7bb3 | 2018-05-07 07:52:12 -0500 | [diff] [blame] | 645 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 646 | send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 0 /* is_broadcast */ ); |
| 647 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | c->retry_count++; |
| 649 | if (c->retry_count > 10) |
| 650 | c->next_transmit = now + 5.0; |
| 651 | else |
| 652 | c->next_transmit = now + 1.0; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 653 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 654 | if (now > c->lease_expires) |
| 655 | { |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 656 | /* Remove the default route */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 657 | if (c->router_address.as_u32) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 658 | { |
| 659 | fib_prefix_t all_0s = { |
| 660 | .fp_len = 0, |
| 661 | .fp_addr.ip4.as_u32 = 0x0, |
| 662 | .fp_proto = FIB_PROTOCOL_IP4, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 663 | }; |
| 664 | ip46_address_t nh = { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 665 | .ip4 = c->router_address, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 666 | }; |
| 667 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 668 | fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index |
| 669 | (FIB_PROTOCOL_IP4, c->sw_if_index), |
| 670 | &all_0s, FIB_SOURCE_DHCP, |
| 671 | DPO_PROTO_IP4, &nh, c->sw_if_index, ~0, |
| 672 | 1, FIB_ROUTE_PATH_FLAG_NONE); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 673 | } |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 674 | /* Remove the interface address */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 675 | dhcp_client_release_address (dcm, c); |
| 676 | c->state = DHCP_DISCOVER; |
| 677 | c->next_transmit = now; |
| 678 | c->retry_count = 0; |
| 679 | /* Wipe out any memory of the address we had... */ |
| 680 | c->leased_address.as_u32 = 0; |
| 681 | c->subnet_mask_width = 0; |
| 682 | c->router_address.as_u32 = 0; |
| 683 | c->lease_renewal_interval = 0; |
| 684 | c->dhcp_server.as_u32 = 0; |
| 685 | return 1; |
| 686 | } |
| 687 | return 0; |
| 688 | } |
| 689 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 690 | static f64 |
| 691 | dhcp_client_sm (f64 now, f64 timeout, uword pool_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 692 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 693 | dhcp_client_main_t *dcm = &dhcp_client_main; |
| 694 | dhcp_client_t *c; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 695 | |
| 696 | /* deleted, pooched, yadda yadda yadda */ |
| 697 | if (pool_is_free_index (dcm->clients, pool_index)) |
| 698 | return timeout; |
| 699 | |
| 700 | c = pool_elt_at_index (dcm->clients, pool_index); |
| 701 | |
| 702 | /* Time for us to do something with this client? */ |
| 703 | if (now < c->next_transmit) |
| 704 | return timeout; |
| 705 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 706 | again: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 707 | switch (c->state) |
| 708 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 709 | case DHCP_DISCOVER: /* send a discover */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | if (dhcp_discover_state (dcm, c, now)) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 711 | goto again; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 712 | break; |
| 713 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 714 | case DHCP_REQUEST: /* send a request */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | if (dhcp_request_state (dcm, c, now)) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 716 | goto again; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 717 | break; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 718 | |
| 719 | case DHCP_BOUND: /* bound, renew needed? */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 720 | if (dhcp_bound_state (dcm, c, now)) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 721 | goto again; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 722 | break; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 723 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 724 | default: |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 725 | clib_warning ("dhcp client %d bogus state %d", |
| 726 | c - dcm->clients, c->state); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 727 | break; |
| 728 | } |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 729 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 730 | if (c->next_transmit < now + timeout) |
| 731 | return c->next_transmit - now; |
| 732 | |
| 733 | return timeout; |
| 734 | } |
| 735 | |
| 736 | static uword |
| 737 | dhcp_client_process (vlib_main_t * vm, |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 738 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 739 | { |
| 740 | f64 timeout = 100.0; |
| 741 | f64 now; |
| 742 | uword event_type; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 743 | uword *event_data = 0; |
| 744 | dhcp_client_main_t *dcm = &dhcp_client_main; |
| 745 | dhcp_client_t *c; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | int i; |
| 747 | |
| 748 | while (1) |
| 749 | { |
| 750 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 751 | |
| 752 | event_type = vlib_process_get_events (vm, &event_data); |
| 753 | |
| 754 | now = vlib_time_now (vm); |
| 755 | |
| 756 | switch (event_type) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 757 | { |
| 758 | case EVENT_DHCP_CLIENT_WAKEUP: |
| 759 | for (i = 0; i < vec_len (event_data); i++) |
| 760 | timeout = dhcp_client_sm (now, timeout, event_data[i]); |
| 761 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 763 | case ~0: |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 764 | /* *INDENT-OFF* */ |
| 765 | pool_foreach (c, dcm->clients, |
| 766 | ({ |
| 767 | timeout = dhcp_client_sm (now, timeout, |
| 768 | (uword) (c - dcm->clients)); |
| 769 | })); |
| 770 | /* *INDENT-ON* */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 771 | if (pool_elts (dcm->clients) == 0) |
| 772 | timeout = 100.0; |
| 773 | break; |
| 774 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 775 | |
| 776 | vec_reset_length (event_data); |
| 777 | } |
| 778 | |
| 779 | /* NOTREACHED */ |
| 780 | return 0; |
| 781 | } |
| 782 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 783 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 784 | VLIB_REGISTER_NODE (dhcp_client_process_node,static) = { |
| 785 | .function = dhcp_client_process, |
| 786 | .type = VLIB_NODE_TYPE_PROCESS, |
| 787 | .name = "dhcp-client-process", |
| 788 | .process_log2_n_stack_bytes = 16, |
Dave Barach | 4941fcc | 2018-02-14 17:58:33 -0500 | [diff] [blame] | 789 | .n_errors = ARRAY_LEN(dhcp_client_process_stat_strings), |
| 790 | .error_strings = dhcp_client_process_stat_strings, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 791 | }; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 792 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 793 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 794 | static u8 * |
| 795 | format_dhcp_client_state (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 796 | { |
| 797 | dhcp_client_state_t state = va_arg (*va, dhcp_client_state_t); |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 798 | char *str = "BOGUS!"; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | |
| 800 | switch (state) |
| 801 | { |
| 802 | #define _(a) \ |
| 803 | case a: \ |
| 804 | str = #a; \ |
| 805 | break; |
| 806 | foreach_dhcp_client_state; |
| 807 | #undef _ |
| 808 | default: |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | s = format (s, "%s", str); |
| 813 | return s; |
| 814 | } |
| 815 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 816 | static u8 * |
| 817 | format_dhcp_client (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 818 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 819 | dhcp_client_main_t *dcm = va_arg (*va, dhcp_client_main_t *); |
| 820 | dhcp_client_t *c = va_arg (*va, dhcp_client_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 821 | int verbose = va_arg (*va, int); |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 822 | ip4_address_t *addr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 823 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 824 | s = format (s, "[%d] %U state %U ", c - dcm->clients, |
| 825 | format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index, |
| 826 | format_dhcp_client_state, c->state); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 827 | |
| 828 | if (c->leased_address.as_u32) |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 829 | { |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 830 | s = format (s, "addr %U/%d gw %U", |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 831 | format_ip4_address, &c->leased_address, |
| 832 | c->subnet_mask_width, format_ip4_address, |
| 833 | &c->router_address); |
| 834 | |
| 835 | vec_foreach (addr, c->domain_server_address) |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 836 | s = format (s, " dns %U", format_ip4_address, addr); |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 837 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 838 | else |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 839 | { |
| 840 | s = format (s, "no address\n"); |
| 841 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | |
| 843 | if (verbose) |
| 844 | { |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 845 | s = |
| 846 | format (s, |
| 847 | "\n lease: lifetime:%d renewal-interval:%d expires:%.2f (now:%.2f)", |
| 848 | c->lease_lifetime, c->lease_renewal_interval, |
| 849 | c->lease_expires, vlib_time_now (dcm->vlib_main)); |
| 850 | s = |
| 851 | format (s, "\n retry-count:%d, next-xmt:%.2f", c->retry_count, |
| 852 | c->next_transmit); |
| 853 | s = |
| 854 | format (s, "\n adjacencies:[unicast:%d broadcast:%d]", c->ai_ucast, |
| 855 | c->ai_bcast); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 856 | } |
| 857 | return s; |
| 858 | } |
| 859 | |
| 860 | static clib_error_t * |
| 861 | show_dhcp_client_command_fn (vlib_main_t * vm, |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 862 | unformat_input_t * input, |
| 863 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 864 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 865 | dhcp_client_main_t *dcm = &dhcp_client_main; |
| 866 | dhcp_client_t *c; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 867 | int verbose = 0; |
| 868 | u32 sw_if_index = ~0; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 869 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 870 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 871 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 872 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 873 | if (unformat (input, "intfc %U", |
| 874 | unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index)) |
| 875 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 876 | else if (unformat (input, "verbose")) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 877 | verbose = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 878 | else |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 879 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | if (sw_if_index != ~0) |
| 883 | { |
| 884 | p = hash_get (dcm->client_by_sw_if_index, sw_if_index); |
| 885 | if (p == 0) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 886 | return clib_error_return (0, "dhcp client not configured"); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 887 | c = pool_elt_at_index (dcm->clients, p[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 888 | vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose); |
| 889 | return 0; |
| 890 | } |
| 891 | |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 892 | /* *INDENT-OFF* */ |
| 893 | pool_foreach (c, dcm->clients, |
| 894 | ({ |
| 895 | vlib_cli_output (vm, "%U", |
| 896 | format_dhcp_client, dcm, |
| 897 | c, verbose); |
| 898 | })); |
| 899 | /* *INDENT-ON* */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 900 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 901 | return 0; |
| 902 | } |
| 903 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 904 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 905 | VLIB_CLI_COMMAND (show_dhcp_client_command, static) = { |
| 906 | .path = "show dhcp client", |
| 907 | .short_help = "show dhcp client [intfc <intfc>][verbose]", |
| 908 | .function = show_dhcp_client_command_fn, |
| 909 | }; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 910 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 911 | |
| 912 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 913 | int |
| 914 | dhcp_client_add_del (dhcp_client_add_del_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 915 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 916 | dhcp_client_main_t *dcm = &dhcp_client_main; |
| 917 | vlib_main_t *vm = dcm->vlib_main; |
| 918 | dhcp_client_t *c; |
| 919 | uword *p; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 920 | fib_prefix_t all_0s = { |
| 921 | .fp_len = 0, |
| 922 | .fp_addr.ip4.as_u32 = 0x0, |
| 923 | .fp_proto = FIB_PROTOCOL_IP4, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 924 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 925 | |
| 926 | p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index); |
| 927 | |
| 928 | if ((p && a->is_add) || (!p && a->is_add == 0)) |
| 929 | return VNET_API_ERROR_INVALID_VALUE; |
| 930 | |
| 931 | if (a->is_add) |
| 932 | { |
Matthew Smith | d4bdd93 | 2019-03-27 12:30:29 -0500 | [diff] [blame] | 933 | dhcp_maybe_register_udp_ports (DHCP_PORT_REG_CLIENT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 934 | pool_get (dcm->clients, c); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 935 | clib_memset (c, 0, sizeof (*c)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 936 | c->state = DHCP_DISCOVER; |
| 937 | c->sw_if_index = a->sw_if_index; |
| 938 | c->client_index = a->client_index; |
| 939 | c->pid = a->pid; |
| 940 | c->event_callback = a->event_callback; |
| 941 | c->option_55_data = a->option_55_data; |
| 942 | c->hostname = a->hostname; |
| 943 | c->client_identifier = a->client_identifier; |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 944 | c->set_broadcast_flag = a->set_broadcast_flag; |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 945 | c->ai_ucast = ADJ_INDEX_INVALID; |
| 946 | c->ai_bcast = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, |
| 947 | VNET_LINK_IP4, |
| 948 | &ADJ_BCAST_ADDR, c->sw_if_index); |
| 949 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 950 | do |
| 951 | { |
| 952 | c->transaction_id = random_u32 (&dcm->seed); |
| 953 | } |
| 954 | while (c->transaction_id == 0); |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 955 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 956 | hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 957 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 958 | /* |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 959 | * In order to accept any OFFER, whether broadcasted or unicasted, we |
| 960 | * need to configure the dhcp-client-detect feature as an input feature |
| 961 | * so the DHCP OFFER is sent to the ip4-local node. Without this a |
| 962 | * broadcasted OFFER hits the 255.255.255.255/32 address and a unicast |
| 963 | * hits 0.0.0.0/0 both of which default to drop and the latter may forward |
| 964 | * of box - not what we want. Nor to we want to change these route for |
| 965 | * all interfaces in this table |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 966 | */ |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 967 | vnet_feature_enable_disable ("ip4-unicast", |
| 968 | "ip4-dhcp-client-detect", |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 969 | c->sw_if_index, 1 /* enable */ , 0, 0); |
Dave Barach | 525c9d0 | 2018-05-26 10:48:55 -0400 | [diff] [blame] | 970 | c->client_detect_feature_enabled = 1; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 971 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 972 | vlib_process_signal_event (vm, dhcp_client_process_node.index, |
| 973 | EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 974 | } |
| 975 | else |
| 976 | { |
| 977 | c = pool_elt_at_index (dcm->clients, p[0]); |
| 978 | |
| 979 | if (c->router_address.as_u32) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 980 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 981 | ip46_address_t nh = { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 982 | .ip4 = c->router_address, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 983 | }; |
| 984 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 985 | fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index |
| 986 | (FIB_PROTOCOL_IP4, c->sw_if_index), |
| 987 | &all_0s, FIB_SOURCE_DHCP, |
| 988 | DPO_PROTO_IP4, &nh, c->sw_if_index, ~0, |
| 989 | 1, FIB_ROUTE_PATH_FLAG_NONE); |
| 990 | } |
Neale Ranns | a2fbf6b | 2017-07-18 08:23:32 -0700 | [diff] [blame] | 991 | dhcp_client_release_address (dcm, c); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 992 | |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame^] | 993 | adj_unlock (c->ai_ucast); |
| 994 | adj_unlock (c->ai_bcast); |
| 995 | |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 996 | vec_free (c->domain_server_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 997 | vec_free (c->option_55_data); |
| 998 | vec_free (c->hostname); |
| 999 | vec_free (c->client_identifier); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1000 | hash_unset (dcm->client_by_sw_if_index, c->sw_if_index); |
| 1001 | pool_put (dcm->clients, c); |
| 1002 | } |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | int |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 1007 | dhcp_client_config (u32 is_add, |
| 1008 | u32 client_index, |
| 1009 | vlib_main_t * vm, |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1010 | u32 sw_if_index, |
| 1011 | u8 * hostname, |
| 1012 | u8 * client_id, |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 1013 | dhcp_event_cb_t event_callback, |
| 1014 | u8 set_broadcast_flag, u32 pid) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1015 | { |
| 1016 | dhcp_client_add_del_args_t _a, *a = &_a; |
| 1017 | int rv; |
| 1018 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1019 | clib_memset (a, 0, sizeof (*a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1020 | a->is_add = is_add; |
| 1021 | a->sw_if_index = sw_if_index; |
| 1022 | a->client_index = client_index; |
| 1023 | a->pid = pid; |
| 1024 | a->event_callback = event_callback; |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 1025 | a->set_broadcast_flag = set_broadcast_flag; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1026 | vec_validate (a->hostname, strlen ((char *) hostname) - 1); |
| 1027 | strncpy ((char *) a->hostname, (char *) hostname, vec_len (a->hostname)); |
| 1028 | vec_validate (a->client_identifier, strlen ((char *) client_id) - 1); |
| 1029 | strncpy ((char *) a->client_identifier, (char *) client_id, |
| 1030 | vec_len (a->client_identifier)); |
Neale Ranns | 51822bf | 2017-07-18 09:26:53 -0700 | [diff] [blame] | 1031 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1032 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1033 | * Option 55 request list. These data precisely match |
| 1034 | * the Ubuntu dhcp client. YMMV. |
| 1035 | */ |
| 1036 | |
| 1037 | /* Subnet Mask */ |
| 1038 | vec_add1 (a->option_55_data, 1); |
| 1039 | /* Broadcast address */ |
| 1040 | vec_add1 (a->option_55_data, 28); |
| 1041 | /* time offset */ |
| 1042 | vec_add1 (a->option_55_data, 2); |
| 1043 | /* Router */ |
| 1044 | vec_add1 (a->option_55_data, 3); |
| 1045 | /* Domain Name */ |
| 1046 | vec_add1 (a->option_55_data, 15); |
| 1047 | /* DNS */ |
| 1048 | vec_add1 (a->option_55_data, 6); |
| 1049 | /* Domain search */ |
| 1050 | vec_add1 (a->option_55_data, 119); |
| 1051 | /* Host name */ |
| 1052 | vec_add1 (a->option_55_data, 12); |
| 1053 | /* NetBIOS name server */ |
| 1054 | vec_add1 (a->option_55_data, 44); |
| 1055 | /* NetBIOS Scope */ |
| 1056 | vec_add1 (a->option_55_data, 47); |
| 1057 | /* MTU */ |
| 1058 | vec_add1 (a->option_55_data, 26); |
| 1059 | /* Classless static route */ |
| 1060 | vec_add1 (a->option_55_data, 121); |
| 1061 | /* NTP servers */ |
| 1062 | vec_add1 (a->option_55_data, 42); |
| 1063 | |
| 1064 | rv = dhcp_client_add_del (a); |
| 1065 | |
| 1066 | switch (rv) |
| 1067 | { |
| 1068 | case 0: |
| 1069 | break; |
| 1070 | |
| 1071 | case VNET_API_ERROR_INVALID_VALUE: |
| 1072 | |
| 1073 | vec_free (a->hostname); |
| 1074 | vec_free (a->client_identifier); |
| 1075 | vec_free (a->option_55_data); |
| 1076 | |
| 1077 | if (is_add) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1078 | clib_warning ("dhcp client already enabled on intf_idx %d", |
| 1079 | sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1080 | else |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1081 | clib_warning ("dhcp client not enabled on on intf_idx %d", |
| 1082 | sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1083 | break; |
| 1084 | |
| 1085 | default: |
| 1086 | clib_warning ("dhcp_client_add_del returned %d", rv); |
| 1087 | } |
| 1088 | |
| 1089 | return rv; |
| 1090 | } |
| 1091 | |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 1092 | void |
| 1093 | dhcp_client_walk (dhcp_client_walk_cb_t cb, void *ctx) |
| 1094 | { |
| 1095 | dhcp_client_main_t *dcm = &dhcp_client_main; |
| 1096 | dhcp_client_t *c; |
| 1097 | |
| 1098 | /* *INDENT-OFF* */ |
| 1099 | pool_foreach (c, dcm->clients, |
| 1100 | ({ |
| 1101 | if (!cb(c, ctx)) |
| 1102 | break; |
| 1103 | })); |
| 1104 | /* *INDENT-ON* */ |
| 1105 | |
| 1106 | } |
| 1107 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1108 | static clib_error_t * |
| 1109 | dhcp_client_set_command_fn (vlib_main_t * vm, |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1110 | unformat_input_t * input, |
| 1111 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1112 | { |
| 1113 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1114 | dhcp_client_main_t *dcm = &dhcp_client_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1115 | u32 sw_if_index; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1116 | u8 *hostname = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1117 | u8 sw_if_index_set = 0; |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 1118 | u8 set_broadcast_flag = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1119 | int is_add = 1; |
| 1120 | dhcp_client_add_del_args_t _a, *a = &_a; |
| 1121 | int rv; |
| 1122 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1123 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1124 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1125 | if (unformat (input, "intfc %U", |
| 1126 | unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index)) |
| 1127 | sw_if_index_set = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1128 | else if (unformat (input, "hostname %v", &hostname)) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1129 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1130 | else if (unformat (input, "del")) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1131 | is_add = 0; |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 1132 | else if (unformat (input, "broadcast", &set_broadcast_flag)) |
| 1133 | is_add = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1134 | else |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1135 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | if (sw_if_index_set == 0) |
| 1139 | return clib_error_return (0, "interface not specified"); |
| 1140 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1141 | clib_memset (a, 0, sizeof (*a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1142 | a->is_add = is_add; |
| 1143 | a->sw_if_index = sw_if_index; |
| 1144 | a->hostname = hostname; |
| 1145 | a->client_identifier = format (0, "vpe 1.0%c", 0); |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 1146 | a->set_broadcast_flag = set_broadcast_flag; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1147 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1148 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1149 | * Option 55 request list. These data precisely match |
| 1150 | * the Ubuntu dhcp client. YMMV. |
| 1151 | */ |
| 1152 | |
| 1153 | /* Subnet Mask */ |
| 1154 | vec_add1 (a->option_55_data, 1); |
| 1155 | /* Broadcast address */ |
| 1156 | vec_add1 (a->option_55_data, 28); |
| 1157 | /* time offset */ |
| 1158 | vec_add1 (a->option_55_data, 2); |
| 1159 | /* Router */ |
| 1160 | vec_add1 (a->option_55_data, 3); |
| 1161 | /* Domain Name */ |
| 1162 | vec_add1 (a->option_55_data, 15); |
| 1163 | /* DNS */ |
| 1164 | vec_add1 (a->option_55_data, 6); |
| 1165 | /* Domain search */ |
| 1166 | vec_add1 (a->option_55_data, 119); |
| 1167 | /* Host name */ |
| 1168 | vec_add1 (a->option_55_data, 12); |
| 1169 | /* NetBIOS name server */ |
| 1170 | vec_add1 (a->option_55_data, 44); |
| 1171 | /* NetBIOS Scope */ |
| 1172 | vec_add1 (a->option_55_data, 47); |
| 1173 | /* MTU */ |
| 1174 | vec_add1 (a->option_55_data, 26); |
| 1175 | /* Classless static route */ |
| 1176 | vec_add1 (a->option_55_data, 121); |
| 1177 | /* NTP servers */ |
| 1178 | vec_add1 (a->option_55_data, 42); |
| 1179 | |
| 1180 | rv = dhcp_client_add_del (a); |
| 1181 | |
| 1182 | switch (rv) |
| 1183 | { |
| 1184 | case 0: |
| 1185 | break; |
| 1186 | |
| 1187 | case VNET_API_ERROR_INVALID_VALUE: |
| 1188 | |
| 1189 | vec_free (a->hostname); |
| 1190 | vec_free (a->client_identifier); |
| 1191 | vec_free (a->option_55_data); |
| 1192 | if (is_add) |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1193 | return clib_error_return (0, "dhcp client already enabled on %U", |
| 1194 | format_vnet_sw_if_index_name, |
| 1195 | dcm->vnet_main, sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1196 | else |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1197 | return clib_error_return (0, "dhcp client not enabled on %U", |
| 1198 | format_vnet_sw_if_index_name, |
| 1199 | dcm->vnet_main, sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1200 | break; |
| 1201 | |
| 1202 | default: |
| 1203 | vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv); |
| 1204 | } |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1209 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1210 | VLIB_CLI_COMMAND (dhcp_client_set_command, static) = { |
| 1211 | .path = "set dhcp client", |
| 1212 | .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]", |
| 1213 | .function = dhcp_client_set_command_fn, |
| 1214 | }; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1215 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1216 | |
| 1217 | static clib_error_t * |
| 1218 | dhcp_client_init (vlib_main_t * vm) |
| 1219 | { |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1220 | dhcp_client_main_t *dcm = &dhcp_client_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1221 | |
| 1222 | dcm->vlib_main = vm; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1223 | dcm->vnet_main = vnet_get_main (); |
jackiechen1985 | 6b7e233 | 2019-04-29 11:29:04 +0800 | [diff] [blame] | 1224 | dcm->seed = (u32) clib_cpu_time_now (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | VLIB_INIT_FUNCTION (dhcp_client_init); |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 1229 | |
| 1230 | /* |
| 1231 | * fd.io coding-style-patch-verification: ON |
| 1232 | * |
| 1233 | * Local Variables: |
| 1234 | * eval: (c-set-style "gnu") |
| 1235 | * End: |
| 1236 | */ |