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 | /* |
| 16 | * client.h: dhcp client |
| 17 | */ |
| 18 | |
| 19 | #ifndef included_dhcp_client_h |
| 20 | #define included_dhcp_client_h |
| 21 | |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 22 | #include <vnet/ip/ip.h> |
Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 23 | #include <vnet/dhcp/dhcp4_packet.h> |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 24 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | #define foreach_dhcp_client_state \ |
| 26 | _(DHCP_DISCOVER) \ |
| 27 | _(DHCP_REQUEST) \ |
| 28 | _(DHCP_BOUND) |
| 29 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 30 | typedef enum |
| 31 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | #define _(a) a, |
| 33 | foreach_dhcp_client_state |
| 34 | #undef _ |
| 35 | } dhcp_client_state_t; |
| 36 | |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 37 | struct dhcp_client_t_; |
| 38 | |
| 39 | /** |
| 40 | * Callback function for DHCP complete events |
| 41 | */ |
| 42 | typedef void (*dhcp_event_cb_t) (u32 client_index, |
| 43 | const struct dhcp_client_t_ * client); |
| 44 | |
| 45 | typedef struct dhcp_client_t_ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 46 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | dhcp_client_state_t state; |
| 48 | |
| 49 | /* the interface in question */ |
| 50 | u32 sw_if_index; |
| 51 | |
| 52 | /* State machine retry counter */ |
| 53 | u32 retry_count; |
| 54 | |
| 55 | /* Send next pkt at this time */ |
| 56 | f64 next_transmit; |
| 57 | f64 lease_expires; |
| 58 | |
| 59 | /* DHCP transaction ID, a random number */ |
| 60 | u32 transaction_id; |
| 61 | |
| 62 | /* leased address, other learned info DHCP */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 63 | ip4_address_t leased_address; /* from your_ip_address field */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | ip4_address_t dhcp_server; |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 65 | u32 subnet_mask_width; /* option 1 */ |
| 66 | ip4_address_t router_address; /* option 3 */ |
jackiechen1985 | 56bc738 | 2019-04-29 12:00:43 +0800 | [diff] [blame] | 67 | ip4_address_t *domain_server_address; /* option 6 */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 68 | u32 lease_renewal_interval; /* option 51 */ |
| 69 | u32 lease_lifetime; /* option 59 */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
| 71 | /* Requested data (option 55) */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 72 | u8 *option_55_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | /* hostname and software client identifiers */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 75 | u8 *hostname; |
| 76 | u8 *client_identifier; /* software version, e.g. vpe 1.0 */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
| 78 | /* Information used for event callback */ |
| 79 | u32 client_index; |
| 80 | u32 pid; |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 81 | |
| 82 | /* Set the broadcast Flag in the Discover/Request messages */ |
| 83 | u8 set_broadcast_flag; |
Dave Barach | 4585eb8 | 2018-03-23 09:52:52 -0400 | [diff] [blame] | 84 | /* Interface MAC address, so we can do an rx-packet-for-us check */ |
| 85 | u8 client_hardware_address[6]; |
Dave Barach | 525c9d0 | 2018-05-26 10:48:55 -0400 | [diff] [blame] | 86 | u8 client_detect_feature_enabled; |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 87 | |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame] | 88 | /* the unicast adjacency for the DHCP server */ |
| 89 | adj_index_t ai_ucast; |
| 90 | /* the broadcast adjacency on the link */ |
| 91 | adj_index_t ai_bcast; |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 92 | /* IP DSCP to set in sent packets */ |
| 93 | ip_dscp_t dscp; |
Neale Ranns | 99536f4 | 2019-07-25 06:11:58 -0700 | [diff] [blame] | 94 | |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 95 | dhcp_event_cb_t event_callback; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | } dhcp_client_t; |
| 97 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 98 | typedef struct |
| 99 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | /* DHCP client pool */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 101 | dhcp_client_t *clients; |
| 102 | uword *client_by_sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | u32 seed; |
| 104 | |
| 105 | /* convenience */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 106 | vlib_main_t *vlib_main; |
| 107 | vnet_main_t *vnet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | } dhcp_client_main_t; |
| 109 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 110 | typedef struct |
| 111 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | int is_add; |
| 113 | u32 sw_if_index; |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 114 | u8 set_broadcast_flag; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | |
| 116 | /* vectors, consumed by dhcp client code */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 117 | u8 *hostname; |
| 118 | u8 *client_identifier; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
| 120 | /* Bytes containing requested option numbers */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 121 | u8 *option_55_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
| 123 | /* Information used for event callback */ |
| 124 | u32 client_index; |
| 125 | u32 pid; |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 126 | ip_dscp_t dscp; |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 127 | dhcp_event_cb_t event_callback; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | } dhcp_client_add_del_args_t; |
| 129 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 130 | extern dhcp_client_main_t dhcp_client_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | |
| 132 | #define EVENT_DHCP_CLIENT_WAKEUP 1 |
| 133 | |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 134 | int dhcp_client_for_us (u32 bi0, |
| 135 | vlib_buffer_t * b0, |
| 136 | ip4_header_t * ip0, |
| 137 | udp_header_t * u0, dhcp_header_t * dh0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Add/Delete DHCP clients |
| 141 | */ |
| 142 | extern int dhcp_client_config (u32 is_add, |
| 143 | u32 client_index, |
| 144 | vlib_main_t * vm, |
| 145 | u32 sw_if_index, |
| 146 | u8 * hostname, |
| 147 | u8 * client_id, |
| 148 | dhcp_event_cb_t event_callback, |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 149 | u8 set_broadcast_flag, |
| 150 | ip_dscp_t dscp, u32 pid); |
Neale Ranns | daff178 | 2018-05-16 04:12:18 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * callback function for clients walking the DHCP client configurations |
| 154 | * |
| 155 | * @param client The client being visitsed |
| 156 | * @param data The data passed during the call to 'walk' |
| 157 | * @return !0 to continue walking 0 to stop. |
| 158 | */ |
| 159 | typedef int (*dhcp_client_walk_cb_t) (const dhcp_client_t * client, |
| 160 | void *data); |
| 161 | |
| 162 | /** |
| 163 | * Walk (visit each) DHCP client configuration |
| 164 | * |
| 165 | * @param cb The callback function invoked as each client is visited |
| 166 | * @param ctx Context data passed back to the client in the invocation of |
| 167 | * the callback. |
| 168 | */ |
| 169 | extern void dhcp_client_walk (dhcp_client_walk_cb_t cb, void *ctx); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
| 171 | #endif /* included_dhcp_client_h */ |
khemendra kumar | 34719e3 | 2017-12-08 18:06:52 +0530 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * fd.io coding-style-patch-verification: ON |
| 175 | * |
| 176 | * Local Variables: |
| 177 | * eval: (c-set-style "gnu") |
| 178 | * End: |
| 179 | */ |