blob: 5aed4c0d21eedf14df7fb3369b46761ef6cdf443 [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>
Neale Ranns3466c302017-02-16 07:45:03 -080028#include <vnet/fib/fib_table.h>
Pavel Kotucekc8d87702017-01-25 07:25:32 +010029
30#include <vnet/vnet_msg_enum.h>
31
32#define vl_typedefs /* define message structures */
33#include <vnet/vnet_all_api_h.h>
34#undef vl_typedefs
35
36#define vl_endianfun /* define message structures */
37#include <vnet/vnet_all_api_h.h>
38#undef vl_endianfun
39
40/* instantiate all the print functions we know about */
41#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42#define vl_printfun
43#include <vnet/vnet_all_api_h.h>
44#undef vl_printfun
45
46#include <vlibapi/api_helper_macros.h>
47
48#define foreach_vpe_api_msg \
49_(DHCP_PROXY_CONFIG,dhcp_proxy_config) \
Neale Ranns20a175a2017-02-14 07:28:41 -080050_(DHCP_PROXY_DUMP,dhcp_proxy_dump) \
Pavel Kotucekc8d87702017-01-25 07:25:32 +010051_(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \
Neale Rannsdaff1782018-05-16 04:12:18 -070052_(DHCP_CLIENT_CONFIG, dhcp_client_config) \
Juraj Sloboda81119e82018-05-25 14:02:20 +020053_(DHCP_CLIENT_DUMP, dhcp_client_dump) \
54_(WANT_DHCP6_PD_REPLY_EVENTS, want_dhcp6_pd_reply_events) \
55_(DHCP6_PD_SEND_CLIENT_MESSAGE, dhcp6_pd_send_client_message) \
56_(DHCP6_CLIENTS_ENABLE_DISABLE, dhcp6_clients_enable_disable)
Pavel Kotucekc8d87702017-01-25 07:25:32 +010057
Pavel Kotucekc8d87702017-01-25 07:25:32 +010058
59static void
60vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
61{
62 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
John Lo70bfcaf2017-11-14 13:19:26 -050063 u8 *vpn_ascii_id;
Pavel Kotucekc8d87702017-01-25 07:25:32 +010064 int rv;
Neale Ranns2dd68522017-02-16 03:38:59 -080065
John Lo70bfcaf2017-11-14 13:19:26 -050066 mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0;
67 vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id);
68 rv =
69 dhcp_proxy_set_vss ((mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4),
70 ntohl (mp->tbl_id), mp->vss_type, vpn_ascii_id,
71 ntohl (mp->oui), ntohl (mp->vpn_index),
72 mp->is_add == 0);
Pavel Kotucekc8d87702017-01-25 07:25:32 +010073
74 REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
75}
76
77
78static void vl_api_dhcp_proxy_config_t_handler
79 (vl_api_dhcp_proxy_config_t * mp)
80{
Neale Ranns2dd68522017-02-16 03:38:59 -080081 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
82 ip46_address_t src, server;
83 int rv = -1;
84
85 if (mp->is_ipv6)
86 {
87 clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6));
88 clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6));
89
90 rv = dhcp6_proxy_set_server (&server,
91 &src,
92 (u32) ntohl (mp->rx_vrf_id),
93 (u32) ntohl (mp->server_vrf_id),
94 (int) (mp->is_add == 0));
95 }
Pavel Kotucekc8d87702017-01-25 07:25:32 +010096 else
Neale Ranns2dd68522017-02-16 03:38:59 -080097 {
98 ip46_address_reset (&src);
99 ip46_address_reset (&server);
100
101 clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4));
102 clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4));
103
104 rv = dhcp4_proxy_set_server (&server,
105 &src,
106 (u32) ntohl (mp->rx_vrf_id),
107 (u32) ntohl (mp->server_vrf_id),
108 (int) (mp->is_add == 0));
109 }
110
111
112 REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100113}
114
Neale Ranns20a175a2017-02-14 07:28:41 -0800115static void
116vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
117{
Florin Coras6c4dae22018-01-09 06:39:23 -0800118 vl_api_registration_t *reg;
Neale Ranns20a175a2017-02-14 07:28:41 -0800119
Florin Coras6c4dae22018-01-09 06:39:23 -0800120 reg = vl_api_client_index_to_registration (mp->client_index);
121 if (!reg)
122 return;;
Neale Ranns20a175a2017-02-14 07:28:41 -0800123
Neale Ranns3466c302017-02-16 07:45:03 -0800124 dhcp_proxy_dump ((mp->is_ip6 == 1 ?
Florin Coras6c4dae22018-01-09 06:39:23 -0800125 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context);
Neale Ranns20a175a2017-02-14 07:28:41 -0800126}
127
128void
Neale Ranns2dd68522017-02-16 03:38:59 -0800129dhcp_send_details (fib_protocol_t proto,
Neale Ranns3466c302017-02-16 07:45:03 -0800130 void *opaque, u32 context, dhcp_proxy_t * proxy)
Neale Ranns20a175a2017-02-14 07:28:41 -0800131{
132 vl_api_dhcp_proxy_details_t *mp;
Florin Coras6c4dae22018-01-09 06:39:23 -0800133 vl_api_registration_t *reg = opaque;
Neale Ranns3466c302017-02-16 07:45:03 -0800134 vl_api_dhcp_server_t *v_server;
135 dhcp_server_t *server;
136 fib_table_t *s_fib;
137 dhcp_vss_t *vss;
138 u32 count;
139 size_t n;
Neale Ranns20a175a2017-02-14 07:28:41 -0800140
Neale Ranns3466c302017-02-16 07:45:03 -0800141 count = vec_len (proxy->dhcp_servers);
142 n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
143 mp = vl_msg_api_alloc (n);
Neale Ranns20a175a2017-02-14 07:28:41 -0800144 if (!mp)
145 return;
Neale Ranns3466c302017-02-16 07:45:03 -0800146 memset (mp, 0, n);
Neale Ranns20a175a2017-02-14 07:28:41 -0800147 mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
148 mp->context = context;
Neale Ranns3466c302017-02-16 07:45:03 -0800149 mp->count = count;
Neale Ranns20a175a2017-02-14 07:28:41 -0800150
Neale Ranns2dd68522017-02-16 03:38:59 -0800151 mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
Neale Ranns3466c302017-02-16 07:45:03 -0800152 mp->rx_vrf_id =
153 htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
154
155 vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
156
John Lo70bfcaf2017-11-14 13:19:26 -0500157 if (vss)
Neale Ranns3466c302017-02-16 07:45:03 -0800158 {
John Lo70bfcaf2017-11-14 13:19:26 -0500159 mp->vss_type = vss->vss_type;
160 if (vss->vss_type == VSS_TYPE_ASCII)
161 {
162 u32 id_len = vec_len (vss->vpn_ascii_id);
163 clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len);
164 }
165 else if (vss->vss_type == VSS_TYPE_VPN_ID)
166 {
167 u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
168 + ((u32) vss->vpn_id[2]);
169 u32 fib_id = ((u32) vss->vpn_id[3] << 24) +
170 ((u32) vss->vpn_id[4] << 16) + ((u32) vss->vpn_id[5] << 8) +
171 ((u32) vss->vpn_id[6]);
172 mp->vss_oui = htonl (oui);
173 mp->vss_fib_id = htonl (fib_id);
174 }
Neale Ranns3466c302017-02-16 07:45:03 -0800175 }
John Lo70bfcaf2017-11-14 13:19:26 -0500176 else
177 mp->vss_type = VSS_TYPE_INVALID;
Neale Ranns3466c302017-02-16 07:45:03 -0800178
179 vec_foreach_index (count, proxy->dhcp_servers)
180 {
181 server = &proxy->dhcp_servers[count];
182 v_server = &mp->servers[count];
183
184 s_fib = fib_table_get (server->server_fib_index, proto);
185
186 v_server->server_vrf_id = htonl (s_fib->ft_table_id);
187
188 if (mp->is_ipv6)
189 {
190 memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16);
191 }
192 else
193 {
194 /* put the address in the first bytes */
195 memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4);
196 }
197 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800198
199 if (mp->is_ipv6)
200 {
Neale Ranns3466c302017-02-16 07:45:03 -0800201 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
Neale Ranns20a175a2017-02-14 07:28:41 -0800202 }
203 else
204 {
205 /* put the address in the first bytes */
Neale Ranns3466c302017-02-16 07:45:03 -0800206 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
Neale Ranns20a175a2017-02-14 07:28:41 -0800207 }
Florin Coras6c4dae22018-01-09 06:39:23 -0800208 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns20a175a2017-02-14 07:28:41 -0800209}
210
Neale Rannsdaff1782018-05-16 04:12:18 -0700211static void
212dhcp_client_lease_encode (vl_api_dhcp_lease_t * lease,
213 const dhcp_client_t * client)
214{
215 size_t len;
216
217 lease->is_ipv6 = 0; // only support IPv6 clients
218 lease->sw_if_index = ntohl (client->sw_if_index);
219 lease->state = client->state;
220 len = clib_min (sizeof (lease->hostname) - 1, vec_len (client->hostname));
221 clib_memcpy (&lease->hostname, client->hostname, len);
222 lease->hostname[len] = 0;
223
224 lease->mask_width = client->subnet_mask_width;
225 clib_memcpy (&lease->host_address[0], (u8 *) & client->leased_address, 4);
226 clib_memcpy (&lease->router_address[0], (u8 *) & client->router_address, 4);
227
228 if (NULL != client->l2_rewrite)
229 clib_memcpy (&lease->host_mac[0], client->l2_rewrite + 6, 6);
230}
231
232static void
233dhcp_client_data_encode (vl_api_dhcp_client_t * vclient,
234 const dhcp_client_t * client)
235{
236 size_t len;
237
238 vclient->sw_if_index = ntohl (client->sw_if_index);
239 len = clib_min (sizeof (vclient->hostname) - 1, vec_len (client->hostname));
240 clib_memcpy (&vclient->hostname, client->hostname, len);
241 vclient->hostname[len] = 0;
242
243 len = clib_min (sizeof (vclient->id) - 1,
244 vec_len (client->client_identifier));
245 clib_memcpy (&vclient->id, client->client_identifier, len);
246 vclient->id[len] = 0;
247
248 if (NULL != client->event_callback)
249 vclient->want_dhcp_event = 1;
250 else
251 vclient->want_dhcp_event = 0;
252 vclient->set_broadcast_flag = client->set_broadcast_flag;
253 vclient->pid = client->pid;
254}
255
256static void
257dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100258{
Florin Coras6c4dae22018-01-09 06:39:23 -0800259 vl_api_registration_t *reg;
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100260 vl_api_dhcp_compl_event_t *mp;
261
Florin Coras6c4dae22018-01-09 06:39:23 -0800262 reg = vl_api_client_index_to_registration (client_index);
263 if (!reg)
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100264 return;
265
266 mp = vl_msg_api_alloc (sizeof (*mp));
267 mp->client_index = client_index;
Neale Rannsdaff1782018-05-16 04:12:18 -0700268 mp->pid = client->pid;
269 dhcp_client_lease_encode (&mp->lease, client);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100270
271 mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
272
Florin Coras6c4dae22018-01-09 06:39:23 -0800273 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100274}
275
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100276static void vl_api_dhcp_client_config_t_handler
277 (vl_api_dhcp_client_config_t * mp)
278{
279 vlib_main_t *vm = vlib_get_main ();
280 vl_api_dhcp_client_config_reply_t *rmp;
Neale Rannsdaff1782018-05-16 04:12:18 -0700281 u32 sw_if_index;
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100282 int rv = 0;
283
Neale Rannsdaff1782018-05-16 04:12:18 -0700284 sw_if_index = ntohl (mp->client.sw_if_index);
285 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
286 {
287 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
288 goto bad_sw_if_index;
289 }
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100290
Neale Rannsdaff1782018-05-16 04:12:18 -0700291 rv = dhcp_client_config (mp->is_add,
292 mp->client_index,
293 vm,
294 sw_if_index,
295 mp->client.hostname,
296 mp->client.id,
297 (mp->client.want_dhcp_event ?
298 dhcp_compl_event_callback :
299 NULL),
300 mp->client.set_broadcast_flag, mp->client.pid);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100301
302 BAD_SW_IF_INDEX_LABEL;
303
304 REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
305}
306
Neale Rannsdaff1782018-05-16 04:12:18 -0700307typedef struct dhcp_client_send_walk_ctx_t_
308{
309 vl_api_registration_t *reg;
310 u32 context;
311} dhcp_client_send_walk_ctx_t;
312
313static int
314send_dhcp_client_entry (const dhcp_client_t * client, void *arg)
315{
316 dhcp_client_send_walk_ctx_t *ctx;
317 vl_api_dhcp_client_details_t *mp;
318
319 ctx = arg;
320
321 mp = vl_msg_api_alloc (sizeof (*mp));
322 memset (mp, 0, sizeof (*mp));
323
324 mp->_vl_msg_id = ntohs (VL_API_DHCP_CLIENT_DETAILS);
325 mp->context = ctx->context;
326
327 dhcp_client_data_encode (&mp->client, client);
328 dhcp_client_lease_encode (&mp->lease, client);
329
330 vl_api_send_msg (ctx->reg, (u8 *) mp);
331
332 return (1);
333}
334
335static void
336vl_api_dhcp_client_dump_t_handler (vl_api_dhcp_client_dump_t * mp)
337{
338 vl_api_registration_t *reg;
339
340 reg = vl_api_client_index_to_registration (mp->client_index);
341 if (!reg)
342 return;
343
344 dhcp_client_send_walk_ctx_t ctx = {
345 .reg = reg,
346 .context = mp->context,
347 };
348 dhcp_client_walk (send_dhcp_client_entry, &ctx);
349}
350
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100351/*
352 * dhcp_api_hookup
353 * Add vpe's API message handlers to the table.
354 * vlib has alread mapped shared memory and
355 * added the client registration handlers.
356 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
357 */
358#define vl_msg_name_crc_list
359#include <vnet/vnet_all_api_h.h>
360#undef vl_msg_name_crc_list
361
362static void
363setup_message_id_table (api_main_t * am)
364{
365#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
366 foreach_vl_msg_name_crc_dhcp;
367#undef _
368}
369
370static clib_error_t *
371dhcp_api_hookup (vlib_main_t * vm)
372{
373 api_main_t *am = &api_main;
374
375#define _(N,n) \
376 vl_msg_api_set_handlers(VL_API_##N, #n, \
377 vl_api_##n##_t_handler, \
378 vl_noop_handler, \
379 vl_api_##n##_t_endian, \
380 vl_api_##n##_t_print, \
381 sizeof(vl_api_##n##_t), 1);
382 foreach_vpe_api_msg;
383#undef _
384
385 /*
386 * Set up the (msg_name, crc, message-id) table
387 */
388 setup_message_id_table (am);
389
Juraj Sloboda81119e82018-05-25 14:02:20 +0200390 dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index,
391 DHCP6_PD_DP_REPLY_REPORT);
392
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100393 return 0;
394}
395
396VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
397
398/*
399 * fd.io coding-style-patch-verification: ON
400 *
401 * Local Variables:
402 * eval: (c-set-style "gnu")
403 * End:
404 */