Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * dhcp_api.c - dhcp api |
| 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include <vnet/interface.h> |
| 24 | #include <vnet/api_errno.h> |
Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 25 | #include <vnet/dhcp/dhcp_proxy.h> |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 26 | #include <vnet/dhcp/client.h> |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 27 | #include <vnet/fib/fib_table.h> |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 28 | |
| 29 | #include <vnet/vnet_msg_enum.h> |
| 30 | |
| 31 | #define vl_typedefs /* define message structures */ |
| 32 | #include <vnet/vnet_all_api_h.h> |
| 33 | #undef vl_typedefs |
| 34 | |
| 35 | #define vl_endianfun /* define message structures */ |
| 36 | #include <vnet/vnet_all_api_h.h> |
| 37 | #undef vl_endianfun |
| 38 | |
| 39 | /* instantiate all the print functions we know about */ |
| 40 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 41 | #define vl_printfun |
| 42 | #include <vnet/vnet_all_api_h.h> |
| 43 | #undef vl_printfun |
| 44 | |
| 45 | #include <vlibapi/api_helper_macros.h> |
| 46 | |
| 47 | #define foreach_vpe_api_msg \ |
| 48 | _(DHCP_PROXY_CONFIG,dhcp_proxy_config) \ |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 49 | _(DHCP_PROXY_DUMP,dhcp_proxy_dump) \ |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 50 | _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \ |
| 51 | _(DHCP_CLIENT_CONFIG, dhcp_client_config) |
| 52 | |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 53 | |
| 54 | static void |
| 55 | vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp) |
| 56 | { |
| 57 | vl_api_dhcp_proxy_set_vss_reply_t *rmp; |
John Lo | 70bfcaf | 2017-11-14 13:19:26 -0500 | [diff] [blame] | 58 | u8 *vpn_ascii_id; |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 59 | int rv; |
Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 60 | |
John Lo | 70bfcaf | 2017-11-14 13:19:26 -0500 | [diff] [blame] | 61 | mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0; |
| 62 | vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id); |
| 63 | rv = |
| 64 | dhcp_proxy_set_vss ((mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), |
| 65 | ntohl (mp->tbl_id), mp->vss_type, vpn_ascii_id, |
| 66 | ntohl (mp->oui), ntohl (mp->vpn_index), |
| 67 | mp->is_add == 0); |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 68 | |
| 69 | REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static void vl_api_dhcp_proxy_config_t_handler |
| 74 | (vl_api_dhcp_proxy_config_t * mp) |
| 75 | { |
Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 76 | vl_api_dhcp_proxy_set_vss_reply_t *rmp; |
| 77 | ip46_address_t src, server; |
| 78 | int rv = -1; |
| 79 | |
| 80 | if (mp->is_ipv6) |
| 81 | { |
| 82 | clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6)); |
| 83 | clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6)); |
| 84 | |
| 85 | rv = dhcp6_proxy_set_server (&server, |
| 86 | &src, |
| 87 | (u32) ntohl (mp->rx_vrf_id), |
| 88 | (u32) ntohl (mp->server_vrf_id), |
| 89 | (int) (mp->is_add == 0)); |
| 90 | } |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 91 | else |
Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 92 | { |
| 93 | ip46_address_reset (&src); |
| 94 | ip46_address_reset (&server); |
| 95 | |
| 96 | clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4)); |
| 97 | clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4)); |
| 98 | |
| 99 | rv = dhcp4_proxy_set_server (&server, |
| 100 | &src, |
| 101 | (u32) ntohl (mp->rx_vrf_id), |
| 102 | (u32) ntohl (mp->server_vrf_id), |
| 103 | (int) (mp->is_add == 0)); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY); |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 110 | static void |
| 111 | vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp) |
| 112 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 113 | vl_api_registration_t *reg; |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 114 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 115 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 116 | if (!reg) |
| 117 | return;; |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 118 | |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 119 | dhcp_proxy_dump ((mp->is_ip6 == 1 ? |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 120 | FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context); |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 124 | dhcp_send_details (fib_protocol_t proto, |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 125 | void *opaque, u32 context, dhcp_proxy_t * proxy) |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 126 | { |
| 127 | vl_api_dhcp_proxy_details_t *mp; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 128 | vl_api_registration_t *reg = opaque; |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 129 | vl_api_dhcp_server_t *v_server; |
| 130 | dhcp_server_t *server; |
| 131 | fib_table_t *s_fib; |
| 132 | dhcp_vss_t *vss; |
| 133 | u32 count; |
| 134 | size_t n; |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 135 | |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 136 | count = vec_len (proxy->dhcp_servers); |
| 137 | n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t)); |
| 138 | mp = vl_msg_api_alloc (n); |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 139 | if (!mp) |
| 140 | return; |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 141 | memset (mp, 0, n); |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 142 | mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS); |
| 143 | mp->context = context; |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 144 | mp->count = count; |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 145 | |
Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 146 | mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6); |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 147 | mp->rx_vrf_id = |
| 148 | htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index)); |
| 149 | |
| 150 | vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto); |
| 151 | |
John Lo | 70bfcaf | 2017-11-14 13:19:26 -0500 | [diff] [blame] | 152 | if (vss) |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 153 | { |
John Lo | 70bfcaf | 2017-11-14 13:19:26 -0500 | [diff] [blame] | 154 | mp->vss_type = vss->vss_type; |
| 155 | if (vss->vss_type == VSS_TYPE_ASCII) |
| 156 | { |
| 157 | u32 id_len = vec_len (vss->vpn_ascii_id); |
| 158 | clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len); |
| 159 | } |
| 160 | else if (vss->vss_type == VSS_TYPE_VPN_ID) |
| 161 | { |
| 162 | u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8) |
| 163 | + ((u32) vss->vpn_id[2]); |
| 164 | u32 fib_id = ((u32) vss->vpn_id[3] << 24) + |
| 165 | ((u32) vss->vpn_id[4] << 16) + ((u32) vss->vpn_id[5] << 8) + |
| 166 | ((u32) vss->vpn_id[6]); |
| 167 | mp->vss_oui = htonl (oui); |
| 168 | mp->vss_fib_id = htonl (fib_id); |
| 169 | } |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 170 | } |
John Lo | 70bfcaf | 2017-11-14 13:19:26 -0500 | [diff] [blame] | 171 | else |
| 172 | mp->vss_type = VSS_TYPE_INVALID; |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 173 | |
| 174 | vec_foreach_index (count, proxy->dhcp_servers) |
| 175 | { |
| 176 | server = &proxy->dhcp_servers[count]; |
| 177 | v_server = &mp->servers[count]; |
| 178 | |
| 179 | s_fib = fib_table_get (server->server_fib_index, proto); |
| 180 | |
| 181 | v_server->server_vrf_id = htonl (s_fib->ft_table_id); |
| 182 | |
| 183 | if (mp->is_ipv6) |
| 184 | { |
| 185 | memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | /* put the address in the first bytes */ |
| 190 | memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4); |
| 191 | } |
| 192 | } |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 193 | |
| 194 | if (mp->is_ipv6) |
| 195 | { |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 196 | memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16); |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 197 | } |
| 198 | else |
| 199 | { |
| 200 | /* put the address in the first bytes */ |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 201 | memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4); |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 202 | } |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 203 | vl_api_send_msg (reg, (u8 *) mp); |
Neale Ranns | 20a175a | 2017-02-14 07:28:41 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 206 | void |
| 207 | dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname, |
Neale Ranns | 4729b1e | 2017-07-06 01:39:05 -0700 | [diff] [blame] | 208 | u8 mask_width, u8 is_ipv6, u8 * host_address, |
| 209 | u8 * router_address, u8 * host_mac) |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 210 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 211 | vl_api_registration_t *reg; |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 212 | vl_api_dhcp_compl_event_t *mp; |
Jon Loeliger | 69186d9 | 2017-04-27 21:20:51 -0500 | [diff] [blame] | 213 | u32 len; |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 214 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 215 | reg = vl_api_client_index_to_registration (client_index); |
| 216 | if (!reg) |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 217 | return; |
| 218 | |
| 219 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 220 | mp->client_index = client_index; |
| 221 | mp->pid = pid; |
| 222 | mp->is_ipv6 = is_ipv6; |
Jon Loeliger | 69186d9 | 2017-04-27 21:20:51 -0500 | [diff] [blame] | 223 | len = (vec_len (hostname) < 63) ? vec_len (hostname) : 63; |
| 224 | clib_memcpy (&mp->hostname, hostname, len); |
| 225 | mp->hostname[len] = 0; |
Neale Ranns | 4729b1e | 2017-07-06 01:39:05 -0700 | [diff] [blame] | 226 | mp->mask_width = mask_width; |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 227 | clib_memcpy (&mp->host_address[0], host_address, 16); |
| 228 | clib_memcpy (&mp->router_address[0], router_address, 16); |
| 229 | |
| 230 | if (NULL != host_mac) |
| 231 | clib_memcpy (&mp->host_mac[0], host_mac, 6); |
| 232 | |
| 233 | mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT); |
| 234 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 235 | vl_api_send_msg (reg, (u8 *) mp); |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 238 | static void vl_api_dhcp_client_config_t_handler |
| 239 | (vl_api_dhcp_client_config_t * mp) |
| 240 | { |
| 241 | vlib_main_t *vm = vlib_get_main (); |
| 242 | vl_api_dhcp_client_config_reply_t *rmp; |
| 243 | int rv = 0; |
| 244 | |
| 245 | VALIDATE_SW_IF_INDEX (mp); |
| 246 | |
| 247 | rv = dhcp_client_config (vm, ntohl (mp->sw_if_index), |
Neale Ranns | 51822bf | 2017-07-18 09:26:53 -0700 | [diff] [blame] | 248 | mp->hostname, mp->client_id, |
| 249 | mp->is_add, mp->client_index, |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 250 | mp->want_dhcp_event ? dhcp_compl_event_callback : |
Neale Ranns | 54c6dc4 | 2018-01-17 10:29:10 -0800 | [diff] [blame] | 251 | NULL, mp->set_broadcast_flag, mp->pid); |
Pavel Kotucek | c8d8770 | 2017-01-25 07:25:32 +0100 | [diff] [blame] | 252 | |
| 253 | BAD_SW_IF_INDEX_LABEL; |
| 254 | |
| 255 | REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * dhcp_api_hookup |
| 260 | * Add vpe's API message handlers to the table. |
| 261 | * vlib has alread mapped shared memory and |
| 262 | * added the client registration handlers. |
| 263 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 264 | */ |
| 265 | #define vl_msg_name_crc_list |
| 266 | #include <vnet/vnet_all_api_h.h> |
| 267 | #undef vl_msg_name_crc_list |
| 268 | |
| 269 | static void |
| 270 | setup_message_id_table (api_main_t * am) |
| 271 | { |
| 272 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 273 | foreach_vl_msg_name_crc_dhcp; |
| 274 | #undef _ |
| 275 | } |
| 276 | |
| 277 | static clib_error_t * |
| 278 | dhcp_api_hookup (vlib_main_t * vm) |
| 279 | { |
| 280 | api_main_t *am = &api_main; |
| 281 | |
| 282 | #define _(N,n) \ |
| 283 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 284 | vl_api_##n##_t_handler, \ |
| 285 | vl_noop_handler, \ |
| 286 | vl_api_##n##_t_endian, \ |
| 287 | vl_api_##n##_t_print, \ |
| 288 | sizeof(vl_api_##n##_t), 1); |
| 289 | foreach_vpe_api_msg; |
| 290 | #undef _ |
| 291 | |
| 292 | /* |
| 293 | * Set up the (msg_name, crc, message-id) table |
| 294 | */ |
| 295 | setup_message_id_table (am); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | VLIB_API_INIT_FUNCTION (dhcp_api_hookup); |
| 301 | |
| 302 | /* |
| 303 | * fd.io coding-style-patch-verification: ON |
| 304 | * |
| 305 | * Local Variables: |
| 306 | * eval: (c-set-style "gnu") |
| 307 | * End: |
| 308 | */ |