blob: 1dacf1178c27092c17250f55bdbb0e7a190bd174 [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>
Neale Ranns3466c302017-02-16 07:45:03 -080027#include <vnet/fib/fib_table.h>
Pavel Kotucekc8d87702017-01-25 07:25:32 +010028
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 Ranns20a175a2017-02-14 07:28:41 -080049_(DHCP_PROXY_DUMP,dhcp_proxy_dump) \
Pavel Kotucekc8d87702017-01-25 07:25:32 +010050_(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \
Neale Rannsdaff1782018-05-16 04:12:18 -070051_(DHCP_CLIENT_CONFIG, dhcp_client_config) \
52_(DHCP_CLIENT_DUMP, dhcp_client_dump)
Pavel Kotucekc8d87702017-01-25 07:25:32 +010053
Pavel Kotucekc8d87702017-01-25 07:25:32 +010054
55static void
56vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
57{
58 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
John Lo70bfcaf2017-11-14 13:19:26 -050059 u8 *vpn_ascii_id;
Pavel Kotucekc8d87702017-01-25 07:25:32 +010060 int rv;
Neale Ranns2dd68522017-02-16 03:38:59 -080061
John Lo70bfcaf2017-11-14 13:19:26 -050062 mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0;
63 vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id);
64 rv =
65 dhcp_proxy_set_vss ((mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4),
66 ntohl (mp->tbl_id), mp->vss_type, vpn_ascii_id,
67 ntohl (mp->oui), ntohl (mp->vpn_index),
68 mp->is_add == 0);
Pavel Kotucekc8d87702017-01-25 07:25:32 +010069
70 REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
71}
72
73
74static void vl_api_dhcp_proxy_config_t_handler
75 (vl_api_dhcp_proxy_config_t * mp)
76{
Neale Ranns2dd68522017-02-16 03:38:59 -080077 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
78 ip46_address_t src, server;
79 int rv = -1;
80
81 if (mp->is_ipv6)
82 {
83 clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6));
84 clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6));
85
86 rv = dhcp6_proxy_set_server (&server,
87 &src,
88 (u32) ntohl (mp->rx_vrf_id),
89 (u32) ntohl (mp->server_vrf_id),
90 (int) (mp->is_add == 0));
91 }
Pavel Kotucekc8d87702017-01-25 07:25:32 +010092 else
Neale Ranns2dd68522017-02-16 03:38:59 -080093 {
94 ip46_address_reset (&src);
95 ip46_address_reset (&server);
96
97 clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4));
98 clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4));
99
100 rv = dhcp4_proxy_set_server (&server,
101 &src,
102 (u32) ntohl (mp->rx_vrf_id),
103 (u32) ntohl (mp->server_vrf_id),
104 (int) (mp->is_add == 0));
105 }
106
107
108 REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100109}
110
Neale Ranns20a175a2017-02-14 07:28:41 -0800111static void
112vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
113{
Florin Coras6c4dae22018-01-09 06:39:23 -0800114 vl_api_registration_t *reg;
Neale Ranns20a175a2017-02-14 07:28:41 -0800115
Florin Coras6c4dae22018-01-09 06:39:23 -0800116 reg = vl_api_client_index_to_registration (mp->client_index);
117 if (!reg)
118 return;;
Neale Ranns20a175a2017-02-14 07:28:41 -0800119
Neale Ranns3466c302017-02-16 07:45:03 -0800120 dhcp_proxy_dump ((mp->is_ip6 == 1 ?
Florin Coras6c4dae22018-01-09 06:39:23 -0800121 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context);
Neale Ranns20a175a2017-02-14 07:28:41 -0800122}
123
124void
Neale Ranns2dd68522017-02-16 03:38:59 -0800125dhcp_send_details (fib_protocol_t proto,
Neale Ranns3466c302017-02-16 07:45:03 -0800126 void *opaque, u32 context, dhcp_proxy_t * proxy)
Neale Ranns20a175a2017-02-14 07:28:41 -0800127{
128 vl_api_dhcp_proxy_details_t *mp;
Florin Coras6c4dae22018-01-09 06:39:23 -0800129 vl_api_registration_t *reg = opaque;
Neale Ranns3466c302017-02-16 07:45:03 -0800130 vl_api_dhcp_server_t *v_server;
131 dhcp_server_t *server;
132 fib_table_t *s_fib;
133 dhcp_vss_t *vss;
134 u32 count;
135 size_t n;
Neale Ranns20a175a2017-02-14 07:28:41 -0800136
Neale Ranns3466c302017-02-16 07:45:03 -0800137 count = vec_len (proxy->dhcp_servers);
138 n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
139 mp = vl_msg_api_alloc (n);
Neale Ranns20a175a2017-02-14 07:28:41 -0800140 if (!mp)
141 return;
Neale Ranns3466c302017-02-16 07:45:03 -0800142 memset (mp, 0, n);
Neale Ranns20a175a2017-02-14 07:28:41 -0800143 mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
144 mp->context = context;
Neale Ranns3466c302017-02-16 07:45:03 -0800145 mp->count = count;
Neale Ranns20a175a2017-02-14 07:28:41 -0800146
Neale Ranns2dd68522017-02-16 03:38:59 -0800147 mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
Neale Ranns3466c302017-02-16 07:45:03 -0800148 mp->rx_vrf_id =
149 htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
150
151 vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
152
John Lo70bfcaf2017-11-14 13:19:26 -0500153 if (vss)
Neale Ranns3466c302017-02-16 07:45:03 -0800154 {
John Lo70bfcaf2017-11-14 13:19:26 -0500155 mp->vss_type = vss->vss_type;
156 if (vss->vss_type == VSS_TYPE_ASCII)
157 {
158 u32 id_len = vec_len (vss->vpn_ascii_id);
159 clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len);
160 }
161 else if (vss->vss_type == VSS_TYPE_VPN_ID)
162 {
163 u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
164 + ((u32) vss->vpn_id[2]);
165 u32 fib_id = ((u32) vss->vpn_id[3] << 24) +
166 ((u32) vss->vpn_id[4] << 16) + ((u32) vss->vpn_id[5] << 8) +
167 ((u32) vss->vpn_id[6]);
168 mp->vss_oui = htonl (oui);
169 mp->vss_fib_id = htonl (fib_id);
170 }
Neale Ranns3466c302017-02-16 07:45:03 -0800171 }
John Lo70bfcaf2017-11-14 13:19:26 -0500172 else
173 mp->vss_type = VSS_TYPE_INVALID;
Neale Ranns3466c302017-02-16 07:45:03 -0800174
175 vec_foreach_index (count, proxy->dhcp_servers)
176 {
177 server = &proxy->dhcp_servers[count];
178 v_server = &mp->servers[count];
179
180 s_fib = fib_table_get (server->server_fib_index, proto);
181
182 v_server->server_vrf_id = htonl (s_fib->ft_table_id);
183
184 if (mp->is_ipv6)
185 {
186 memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16);
187 }
188 else
189 {
190 /* put the address in the first bytes */
191 memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4);
192 }
193 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800194
195 if (mp->is_ipv6)
196 {
Neale Ranns3466c302017-02-16 07:45:03 -0800197 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
Neale Ranns20a175a2017-02-14 07:28:41 -0800198 }
199 else
200 {
201 /* put the address in the first bytes */
Neale Ranns3466c302017-02-16 07:45:03 -0800202 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
Neale Ranns20a175a2017-02-14 07:28:41 -0800203 }
Florin Coras6c4dae22018-01-09 06:39:23 -0800204 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns20a175a2017-02-14 07:28:41 -0800205}
206
Neale Rannsdaff1782018-05-16 04:12:18 -0700207static void
208dhcp_client_lease_encode (vl_api_dhcp_lease_t * lease,
209 const dhcp_client_t * client)
210{
211 size_t len;
212
213 lease->is_ipv6 = 0; // only support IPv6 clients
214 lease->sw_if_index = ntohl (client->sw_if_index);
215 lease->state = client->state;
216 len = clib_min (sizeof (lease->hostname) - 1, vec_len (client->hostname));
217 clib_memcpy (&lease->hostname, client->hostname, len);
218 lease->hostname[len] = 0;
219
220 lease->mask_width = client->subnet_mask_width;
221 clib_memcpy (&lease->host_address[0], (u8 *) & client->leased_address, 4);
222 clib_memcpy (&lease->router_address[0], (u8 *) & client->router_address, 4);
223
224 if (NULL != client->l2_rewrite)
225 clib_memcpy (&lease->host_mac[0], client->l2_rewrite + 6, 6);
226}
227
228static void
229dhcp_client_data_encode (vl_api_dhcp_client_t * vclient,
230 const dhcp_client_t * client)
231{
232 size_t len;
233
234 vclient->sw_if_index = ntohl (client->sw_if_index);
235 len = clib_min (sizeof (vclient->hostname) - 1, vec_len (client->hostname));
236 clib_memcpy (&vclient->hostname, client->hostname, len);
237 vclient->hostname[len] = 0;
238
239 len = clib_min (sizeof (vclient->id) - 1,
240 vec_len (client->client_identifier));
241 clib_memcpy (&vclient->id, client->client_identifier, len);
242 vclient->id[len] = 0;
243
244 if (NULL != client->event_callback)
245 vclient->want_dhcp_event = 1;
246 else
247 vclient->want_dhcp_event = 0;
248 vclient->set_broadcast_flag = client->set_broadcast_flag;
249 vclient->pid = client->pid;
250}
251
252static void
253dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100254{
Florin Coras6c4dae22018-01-09 06:39:23 -0800255 vl_api_registration_t *reg;
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100256 vl_api_dhcp_compl_event_t *mp;
257
Florin Coras6c4dae22018-01-09 06:39:23 -0800258 reg = vl_api_client_index_to_registration (client_index);
259 if (!reg)
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100260 return;
261
262 mp = vl_msg_api_alloc (sizeof (*mp));
263 mp->client_index = client_index;
Neale Rannsdaff1782018-05-16 04:12:18 -0700264 mp->pid = client->pid;
265 dhcp_client_lease_encode (&mp->lease, client);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100266
267 mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
268
Florin Coras6c4dae22018-01-09 06:39:23 -0800269 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100270}
271
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100272static void vl_api_dhcp_client_config_t_handler
273 (vl_api_dhcp_client_config_t * mp)
274{
275 vlib_main_t *vm = vlib_get_main ();
276 vl_api_dhcp_client_config_reply_t *rmp;
Neale Rannsdaff1782018-05-16 04:12:18 -0700277 u32 sw_if_index;
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100278 int rv = 0;
279
Neale Rannsdaff1782018-05-16 04:12:18 -0700280 sw_if_index = ntohl (mp->client.sw_if_index);
281 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
282 {
283 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
284 goto bad_sw_if_index;
285 }
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100286
Neale Rannsdaff1782018-05-16 04:12:18 -0700287 rv = dhcp_client_config (mp->is_add,
288 mp->client_index,
289 vm,
290 sw_if_index,
291 mp->client.hostname,
292 mp->client.id,
293 (mp->client.want_dhcp_event ?
294 dhcp_compl_event_callback :
295 NULL),
296 mp->client.set_broadcast_flag, mp->client.pid);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100297
298 BAD_SW_IF_INDEX_LABEL;
299
300 REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
301}
302
Neale Rannsdaff1782018-05-16 04:12:18 -0700303typedef struct dhcp_client_send_walk_ctx_t_
304{
305 vl_api_registration_t *reg;
306 u32 context;
307} dhcp_client_send_walk_ctx_t;
308
309static int
310send_dhcp_client_entry (const dhcp_client_t * client, void *arg)
311{
312 dhcp_client_send_walk_ctx_t *ctx;
313 vl_api_dhcp_client_details_t *mp;
314
315 ctx = arg;
316
317 mp = vl_msg_api_alloc (sizeof (*mp));
318 memset (mp, 0, sizeof (*mp));
319
320 mp->_vl_msg_id = ntohs (VL_API_DHCP_CLIENT_DETAILS);
321 mp->context = ctx->context;
322
323 dhcp_client_data_encode (&mp->client, client);
324 dhcp_client_lease_encode (&mp->lease, client);
325
326 vl_api_send_msg (ctx->reg, (u8 *) mp);
327
328 return (1);
329}
330
331static void
332vl_api_dhcp_client_dump_t_handler (vl_api_dhcp_client_dump_t * mp)
333{
334 vl_api_registration_t *reg;
335
336 reg = vl_api_client_index_to_registration (mp->client_index);
337 if (!reg)
338 return;
339
340 dhcp_client_send_walk_ctx_t ctx = {
341 .reg = reg,
342 .context = mp->context,
343 };
344 dhcp_client_walk (send_dhcp_client_entry, &ctx);
345}
346
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100347/*
348 * dhcp_api_hookup
349 * Add vpe's API message handlers to the table.
350 * vlib has alread mapped shared memory and
351 * added the client registration handlers.
352 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
353 */
354#define vl_msg_name_crc_list
355#include <vnet/vnet_all_api_h.h>
356#undef vl_msg_name_crc_list
357
358static void
359setup_message_id_table (api_main_t * am)
360{
361#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
362 foreach_vl_msg_name_crc_dhcp;
363#undef _
364}
365
366static clib_error_t *
367dhcp_api_hookup (vlib_main_t * vm)
368{
369 api_main_t *am = &api_main;
370
371#define _(N,n) \
372 vl_msg_api_set_handlers(VL_API_##N, #n, \
373 vl_api_##n##_t_handler, \
374 vl_noop_handler, \
375 vl_api_##n##_t_endian, \
376 vl_api_##n##_t_print, \
377 sizeof(vl_api_##n##_t), 1);
378 foreach_vpe_api_msg;
379#undef _
380
381 /*
382 * Set up the (msg_name, crc, message-id) table
383 */
384 setup_message_id_table (am);
385
386 return 0;
387}
388
389VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
390
391/*
392 * fd.io coding-style-patch-verification: ON
393 *
394 * Local Variables:
395 * eval: (c-set-style "gnu")
396 * End:
397 */