blob: 7eb2bf46a06558886bc937fe431e530dd1e8621d [file] [log] [blame]
Pavel Kotucekc8d87702017-01-25 07:25:32 +01001/*
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 Ranns2dd68522017-02-16 03:38:59 -080025#include <vnet/dhcp/dhcp_proxy.h>
Pavel Kotucekc8d87702017-01-25 07:25:32 +010026#include <vnet/dhcp/client.h>
Juraj Sloboda81119e82018-05-25 14:02:20 +020027#include <vnet/dhcp/dhcp6_pd_client_dp.h>
Juraj Slobodadd3b8f72018-05-04 14:20:06 +020028#include <vnet/dhcp/dhcp6_ia_na_client_dp.h>
29#include <vnet/dhcp/dhcp6_client_common_dp.h>
Neale Ranns3466c302017-02-16 07:45:03 -080030#include <vnet/fib/fib_table.h>
Pavel Kotucekc8d87702017-01-25 07:25:32 +010031
32#include <vnet/vnet_msg_enum.h>
33
34#define vl_typedefs /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_typedefs
37
38#define vl_endianfun /* define message structures */
39#include <vnet/vnet_all_api_h.h>
40#undef vl_endianfun
41
42/* instantiate all the print functions we know about */
43#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44#define vl_printfun
45#include <vnet/vnet_all_api_h.h>
46#undef vl_printfun
47
48#include <vlibapi/api_helper_macros.h>
49
50#define foreach_vpe_api_msg \
51_(DHCP_PROXY_CONFIG,dhcp_proxy_config) \
Neale Ranns20a175a2017-02-14 07:28:41 -080052_(DHCP_PROXY_DUMP,dhcp_proxy_dump) \
Pavel Kotucekc8d87702017-01-25 07:25:32 +010053_(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \
Neale Rannsdaff1782018-05-16 04:12:18 -070054_(DHCP_CLIENT_CONFIG, dhcp_client_config) \
Juraj Sloboda81119e82018-05-25 14:02:20 +020055_(DHCP_CLIENT_DUMP, dhcp_client_dump) \
56_(WANT_DHCP6_PD_REPLY_EVENTS, want_dhcp6_pd_reply_events) \
57_(DHCP6_PD_SEND_CLIENT_MESSAGE, dhcp6_pd_send_client_message) \
Juraj Slobodadd3b8f72018-05-04 14:20:06 +020058_(WANT_DHCP6_REPLY_EVENTS, want_dhcp6_reply_events) \
59_(DHCP6_SEND_CLIENT_MESSAGE, dhcp6_send_client_message) \
60_(DHCP6_CLIENTS_ENABLE_DISABLE, dhcp6_clients_enable_disable) \
Juraj Slobodad9778c22018-06-12 10:21:05 +020061_(DHCP6_DUID_LL_SET, dhcp6_duid_ll_set)
Pavel Kotucekc8d87702017-01-25 07:25:32 +010062
Pavel Kotucekc8d87702017-01-25 07:25:32 +010063
64static void
65vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
66{
67 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
John Lo70bfcaf2017-11-14 13:19:26 -050068 u8 *vpn_ascii_id;
Pavel Kotucekc8d87702017-01-25 07:25:32 +010069 int rv;
Neale Ranns2dd68522017-02-16 03:38:59 -080070
John Lo70bfcaf2017-11-14 13:19:26 -050071 mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0;
72 vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id);
73 rv =
74 dhcp_proxy_set_vss ((mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4),
75 ntohl (mp->tbl_id), mp->vss_type, vpn_ascii_id,
76 ntohl (mp->oui), ntohl (mp->vpn_index),
77 mp->is_add == 0);
Pavel Kotucekc8d87702017-01-25 07:25:32 +010078
79 REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
80}
81
82
83static void vl_api_dhcp_proxy_config_t_handler
84 (vl_api_dhcp_proxy_config_t * mp)
85{
Neale Ranns2dd68522017-02-16 03:38:59 -080086 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
87 ip46_address_t src, server;
88 int rv = -1;
89
90 if (mp->is_ipv6)
91 {
92 clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6));
93 clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6));
94
95 rv = dhcp6_proxy_set_server (&server,
96 &src,
97 (u32) ntohl (mp->rx_vrf_id),
98 (u32) ntohl (mp->server_vrf_id),
99 (int) (mp->is_add == 0));
100 }
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100101 else
Neale Ranns2dd68522017-02-16 03:38:59 -0800102 {
103 ip46_address_reset (&src);
104 ip46_address_reset (&server);
105
106 clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4));
107 clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4));
108
109 rv = dhcp4_proxy_set_server (&server,
110 &src,
111 (u32) ntohl (mp->rx_vrf_id),
112 (u32) ntohl (mp->server_vrf_id),
113 (int) (mp->is_add == 0));
114 }
115
116
117 REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100118}
119
Neale Ranns20a175a2017-02-14 07:28:41 -0800120static void
121vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
122{
Florin Coras6c4dae22018-01-09 06:39:23 -0800123 vl_api_registration_t *reg;
Neale Ranns20a175a2017-02-14 07:28:41 -0800124
Florin Coras6c4dae22018-01-09 06:39:23 -0800125 reg = vl_api_client_index_to_registration (mp->client_index);
126 if (!reg)
127 return;;
Neale Ranns20a175a2017-02-14 07:28:41 -0800128
Neale Ranns3466c302017-02-16 07:45:03 -0800129 dhcp_proxy_dump ((mp->is_ip6 == 1 ?
Florin Coras6c4dae22018-01-09 06:39:23 -0800130 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context);
Neale Ranns20a175a2017-02-14 07:28:41 -0800131}
132
133void
Neale Ranns2dd68522017-02-16 03:38:59 -0800134dhcp_send_details (fib_protocol_t proto,
Neale Ranns3466c302017-02-16 07:45:03 -0800135 void *opaque, u32 context, dhcp_proxy_t * proxy)
Neale Ranns20a175a2017-02-14 07:28:41 -0800136{
137 vl_api_dhcp_proxy_details_t *mp;
Florin Coras6c4dae22018-01-09 06:39:23 -0800138 vl_api_registration_t *reg = opaque;
Neale Ranns3466c302017-02-16 07:45:03 -0800139 vl_api_dhcp_server_t *v_server;
140 dhcp_server_t *server;
141 fib_table_t *s_fib;
142 dhcp_vss_t *vss;
143 u32 count;
144 size_t n;
Neale Ranns20a175a2017-02-14 07:28:41 -0800145
Neale Ranns3466c302017-02-16 07:45:03 -0800146 count = vec_len (proxy->dhcp_servers);
147 n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
148 mp = vl_msg_api_alloc (n);
Neale Ranns20a175a2017-02-14 07:28:41 -0800149 if (!mp)
150 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400151 clib_memset (mp, 0, n);
Neale Ranns20a175a2017-02-14 07:28:41 -0800152 mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
153 mp->context = context;
Neale Ranns3466c302017-02-16 07:45:03 -0800154 mp->count = count;
Neale Ranns20a175a2017-02-14 07:28:41 -0800155
Neale Ranns2dd68522017-02-16 03:38:59 -0800156 mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
Neale Ranns3466c302017-02-16 07:45:03 -0800157 mp->rx_vrf_id =
158 htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
159
160 vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
161
John Lo70bfcaf2017-11-14 13:19:26 -0500162 if (vss)
Neale Ranns3466c302017-02-16 07:45:03 -0800163 {
John Lo70bfcaf2017-11-14 13:19:26 -0500164 mp->vss_type = vss->vss_type;
165 if (vss->vss_type == VSS_TYPE_ASCII)
166 {
167 u32 id_len = vec_len (vss->vpn_ascii_id);
168 clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len);
169 }
170 else if (vss->vss_type == VSS_TYPE_VPN_ID)
171 {
172 u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
173 + ((u32) vss->vpn_id[2]);
174 u32 fib_id = ((u32) vss->vpn_id[3] << 24) +
175 ((u32) vss->vpn_id[4] << 16) + ((u32) vss->vpn_id[5] << 8) +
176 ((u32) vss->vpn_id[6]);
177 mp->vss_oui = htonl (oui);
178 mp->vss_fib_id = htonl (fib_id);
179 }
Neale Ranns3466c302017-02-16 07:45:03 -0800180 }
John Lo70bfcaf2017-11-14 13:19:26 -0500181 else
182 mp->vss_type = VSS_TYPE_INVALID;
Neale Ranns3466c302017-02-16 07:45:03 -0800183
184 vec_foreach_index (count, proxy->dhcp_servers)
185 {
186 server = &proxy->dhcp_servers[count];
187 v_server = &mp->servers[count];
188
189 s_fib = fib_table_get (server->server_fib_index, proto);
190
191 v_server->server_vrf_id = htonl (s_fib->ft_table_id);
192
193 if (mp->is_ipv6)
194 {
195 memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16);
196 }
197 else
198 {
199 /* put the address in the first bytes */
200 memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4);
201 }
202 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800203
204 if (mp->is_ipv6)
205 {
Neale Ranns3466c302017-02-16 07:45:03 -0800206 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
Neale Ranns20a175a2017-02-14 07:28:41 -0800207 }
208 else
209 {
210 /* put the address in the first bytes */
Neale Ranns3466c302017-02-16 07:45:03 -0800211 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
Neale Ranns20a175a2017-02-14 07:28:41 -0800212 }
Florin Coras6c4dae22018-01-09 06:39:23 -0800213 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns20a175a2017-02-14 07:28:41 -0800214}
215
Neale Rannsdaff1782018-05-16 04:12:18 -0700216static void
217dhcp_client_lease_encode (vl_api_dhcp_lease_t * lease,
218 const dhcp_client_t * client)
219{
220 size_t len;
jackiechen198556bc7382019-04-29 12:00:43 +0800221 u8 i;
Neale Rannsdaff1782018-05-16 04:12:18 -0700222
223 lease->is_ipv6 = 0; // only support IPv6 clients
224 lease->sw_if_index = ntohl (client->sw_if_index);
225 lease->state = client->state;
226 len = clib_min (sizeof (lease->hostname) - 1, vec_len (client->hostname));
227 clib_memcpy (&lease->hostname, client->hostname, len);
228 lease->hostname[len] = 0;
229
230 lease->mask_width = client->subnet_mask_width;
jackiechen198556bc7382019-04-29 12:00:43 +0800231 clib_memcpy (&lease->host_address[0], (u8 *) & client->leased_address,
232 sizeof (ip4_address_t));
233 clib_memcpy (&lease->router_address[0], (u8 *) & client->router_address,
234 sizeof (ip4_address_t));
235
236 lease->count = vec_len (client->domain_server_address);
237 for (i = 0; i < lease->count; i++)
238 clib_memcpy (&lease->domain_server[i].address,
239 (u8 *) & client->domain_server_address[i],
240 sizeof (ip4_address_t));
Neale Rannsdaff1782018-05-16 04:12:18 -0700241
Neale Ranns99536f42019-07-25 06:11:58 -0700242 clib_memcpy (&lease->host_mac[0], client->client_hardware_address, 6);
Neale Rannsdaff1782018-05-16 04:12:18 -0700243}
244
245static void
246dhcp_client_data_encode (vl_api_dhcp_client_t * vclient,
247 const dhcp_client_t * client)
248{
249 size_t len;
250
251 vclient->sw_if_index = ntohl (client->sw_if_index);
252 len = clib_min (sizeof (vclient->hostname) - 1, vec_len (client->hostname));
253 clib_memcpy (&vclient->hostname, client->hostname, len);
254 vclient->hostname[len] = 0;
255
256 len = clib_min (sizeof (vclient->id) - 1,
257 vec_len (client->client_identifier));
258 clib_memcpy (&vclient->id, client->client_identifier, len);
259 vclient->id[len] = 0;
260
261 if (NULL != client->event_callback)
262 vclient->want_dhcp_event = 1;
263 else
264 vclient->want_dhcp_event = 0;
265 vclient->set_broadcast_flag = client->set_broadcast_flag;
266 vclient->pid = client->pid;
267}
268
269static void
270dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100271{
Florin Coras6c4dae22018-01-09 06:39:23 -0800272 vl_api_registration_t *reg;
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100273 vl_api_dhcp_compl_event_t *mp;
274
Florin Coras6c4dae22018-01-09 06:39:23 -0800275 reg = vl_api_client_index_to_registration (client_index);
276 if (!reg)
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100277 return;
278
279 mp = vl_msg_api_alloc (sizeof (*mp));
280 mp->client_index = client_index;
Neale Rannsdaff1782018-05-16 04:12:18 -0700281 mp->pid = client->pid;
282 dhcp_client_lease_encode (&mp->lease, client);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100283
284 mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
285
Florin Coras6c4dae22018-01-09 06:39:23 -0800286 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100287}
288
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100289static void vl_api_dhcp_client_config_t_handler
290 (vl_api_dhcp_client_config_t * mp)
291{
292 vlib_main_t *vm = vlib_get_main ();
293 vl_api_dhcp_client_config_reply_t *rmp;
Neale Rannsdaff1782018-05-16 04:12:18 -0700294 u32 sw_if_index;
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100295 int rv = 0;
296
Neale Rannsdaff1782018-05-16 04:12:18 -0700297 sw_if_index = ntohl (mp->client.sw_if_index);
298 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
299 {
300 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
301 goto bad_sw_if_index;
302 }
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100303
Neale Rannsdaff1782018-05-16 04:12:18 -0700304 rv = dhcp_client_config (mp->is_add,
305 mp->client_index,
306 vm,
307 sw_if_index,
308 mp->client.hostname,
309 mp->client.id,
310 (mp->client.want_dhcp_event ?
311 dhcp_compl_event_callback :
312 NULL),
313 mp->client.set_broadcast_flag, mp->client.pid);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100314
315 BAD_SW_IF_INDEX_LABEL;
316
317 REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
318}
319
Neale Rannsdaff1782018-05-16 04:12:18 -0700320typedef struct dhcp_client_send_walk_ctx_t_
321{
322 vl_api_registration_t *reg;
323 u32 context;
324} dhcp_client_send_walk_ctx_t;
325
326static int
327send_dhcp_client_entry (const dhcp_client_t * client, void *arg)
328{
329 dhcp_client_send_walk_ctx_t *ctx;
330 vl_api_dhcp_client_details_t *mp;
331
332 ctx = arg;
333
334 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400335 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsdaff1782018-05-16 04:12:18 -0700336
337 mp->_vl_msg_id = ntohs (VL_API_DHCP_CLIENT_DETAILS);
338 mp->context = ctx->context;
339
340 dhcp_client_data_encode (&mp->client, client);
341 dhcp_client_lease_encode (&mp->lease, client);
342
343 vl_api_send_msg (ctx->reg, (u8 *) mp);
344
345 return (1);
346}
347
348static void
349vl_api_dhcp_client_dump_t_handler (vl_api_dhcp_client_dump_t * mp)
350{
351 vl_api_registration_t *reg;
352
353 reg = vl_api_client_index_to_registration (mp->client_index);
354 if (!reg)
355 return;
356
357 dhcp_client_send_walk_ctx_t ctx = {
358 .reg = reg,
359 .context = mp->context,
360 };
361 dhcp_client_walk (send_dhcp_client_entry, &ctx);
362}
363
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100364/*
365 * dhcp_api_hookup
366 * Add vpe's API message handlers to the table.
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700367 * vlib has already mapped shared memory and
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100368 * added the client registration handlers.
369 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
370 */
371#define vl_msg_name_crc_list
372#include <vnet/vnet_all_api_h.h>
373#undef vl_msg_name_crc_list
374
375static void
376setup_message_id_table (api_main_t * am)
377{
378#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
379 foreach_vl_msg_name_crc_dhcp;
380#undef _
381}
382
383static clib_error_t *
384dhcp_api_hookup (vlib_main_t * vm)
385{
386 api_main_t *am = &api_main;
387
388#define _(N,n) \
389 vl_msg_api_set_handlers(VL_API_##N, #n, \
390 vl_api_##n##_t_handler, \
391 vl_noop_handler, \
392 vl_api_##n##_t_endian, \
393 vl_api_##n##_t_print, \
394 sizeof(vl_api_##n##_t), 1);
395 foreach_vpe_api_msg;
396#undef _
397
398 /*
399 * Set up the (msg_name, crc, message-id) table
400 */
401 setup_message_id_table (am);
402
Juraj Sloboda81119e82018-05-25 14:02:20 +0200403 dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index,
404 DHCP6_PD_DP_REPLY_REPORT);
Juraj Slobodadd3b8f72018-05-04 14:20:06 +0200405 dhcp6_set_publisher_node (dhcp6_reply_process_node.index,
406 DHCP6_DP_REPLY_REPORT);
Juraj Sloboda81119e82018-05-25 14:02:20 +0200407
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100408 return 0;
409}
410
411VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
412
413/*
414 * fd.io coding-style-patch-verification: ON
415 *
416 * Local Variables:
417 * eval: (c-set-style "gnu")
418 * End:
419 */