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