2 *------------------------------------------------------------------
3 * dhcp_api.c - dhcp api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/dhcp/dhcp_proxy.h>
26 #include <vnet/dhcp/client.h>
27 #include <vnet/fib/fib_table.h>
29 #include <vnet/vnet_msg_enum.h>
31 #define vl_typedefs /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
35 #define vl_endianfun /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #include <vnet/vnet_all_api_h.h>
45 #include <vlibapi/api_helper_macros.h>
47 #define foreach_vpe_api_msg \
48 _(DHCP_PROXY_CONFIG,dhcp_proxy_config) \
49 _(DHCP_PROXY_DUMP,dhcp_proxy_dump) \
50 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \
51 _(DHCP_CLIENT_CONFIG, dhcp_client_config) \
52 _(DHCP_CLIENT_DUMP, dhcp_client_dump)
56 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
58 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
62 mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0;
63 vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id);
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),
70 REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
74 static void vl_api_dhcp_proxy_config_t_handler
75 (vl_api_dhcp_proxy_config_t * mp)
77 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
78 ip46_address_t src, server;
83 clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6));
84 clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6));
86 rv = dhcp6_proxy_set_server (&server,
88 (u32) ntohl (mp->rx_vrf_id),
89 (u32) ntohl (mp->server_vrf_id),
90 (int) (mp->is_add == 0));
94 ip46_address_reset (&src);
95 ip46_address_reset (&server);
97 clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4));
98 clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4));
100 rv = dhcp4_proxy_set_server (&server,
102 (u32) ntohl (mp->rx_vrf_id),
103 (u32) ntohl (mp->server_vrf_id),
104 (int) (mp->is_add == 0));
108 REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
112 vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
114 vl_api_registration_t *reg;
116 reg = vl_api_client_index_to_registration (mp->client_index);
120 dhcp_proxy_dump ((mp->is_ip6 == 1 ?
121 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context);
125 dhcp_send_details (fib_protocol_t proto,
126 void *opaque, u32 context, dhcp_proxy_t * proxy)
128 vl_api_dhcp_proxy_details_t *mp;
129 vl_api_registration_t *reg = opaque;
130 vl_api_dhcp_server_t *v_server;
131 dhcp_server_t *server;
137 count = vec_len (proxy->dhcp_servers);
138 n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
139 mp = vl_msg_api_alloc (n);
143 mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
144 mp->context = context;
147 mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
149 htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
151 vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
155 mp->vss_type = vss->vss_type;
156 if (vss->vss_type == VSS_TYPE_ASCII)
158 u32 id_len = vec_len (vss->vpn_ascii_id);
159 clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len);
161 else if (vss->vss_type == VSS_TYPE_VPN_ID)
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);
173 mp->vss_type = VSS_TYPE_INVALID;
175 vec_foreach_index (count, proxy->dhcp_servers)
177 server = &proxy->dhcp_servers[count];
178 v_server = &mp->servers[count];
180 s_fib = fib_table_get (server->server_fib_index, proto);
182 v_server->server_vrf_id = htonl (s_fib->ft_table_id);
186 memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16);
190 /* put the address in the first bytes */
191 memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4);
197 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
201 /* put the address in the first bytes */
202 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
204 vl_api_send_msg (reg, (u8 *) mp);
208 dhcp_client_lease_encode (vl_api_dhcp_lease_t * lease,
209 const dhcp_client_t * client)
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;
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);
224 if (NULL != client->l2_rewrite)
225 clib_memcpy (&lease->host_mac[0], client->l2_rewrite + 6, 6);
229 dhcp_client_data_encode (vl_api_dhcp_client_t * vclient,
230 const dhcp_client_t * client)
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;
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;
244 if (NULL != client->event_callback)
245 vclient->want_dhcp_event = 1;
247 vclient->want_dhcp_event = 0;
248 vclient->set_broadcast_flag = client->set_broadcast_flag;
249 vclient->pid = client->pid;
253 dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
255 vl_api_registration_t *reg;
256 vl_api_dhcp_compl_event_t *mp;
258 reg = vl_api_client_index_to_registration (client_index);
262 mp = vl_msg_api_alloc (sizeof (*mp));
263 mp->client_index = client_index;
264 mp->pid = client->pid;
265 dhcp_client_lease_encode (&mp->lease, client);
267 mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
269 vl_api_send_msg (reg, (u8 *) mp);
272 static void vl_api_dhcp_client_config_t_handler
273 (vl_api_dhcp_client_config_t * mp)
275 vlib_main_t *vm = vlib_get_main ();
276 vl_api_dhcp_client_config_reply_t *rmp;
280 sw_if_index = ntohl (mp->client.sw_if_index);
281 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
283 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
284 goto bad_sw_if_index;
287 rv = dhcp_client_config (mp->is_add,
293 (mp->client.want_dhcp_event ?
294 dhcp_compl_event_callback :
296 mp->client.set_broadcast_flag, mp->client.pid);
298 BAD_SW_IF_INDEX_LABEL;
300 REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
303 typedef struct dhcp_client_send_walk_ctx_t_
305 vl_api_registration_t *reg;
307 } dhcp_client_send_walk_ctx_t;
310 send_dhcp_client_entry (const dhcp_client_t * client, void *arg)
312 dhcp_client_send_walk_ctx_t *ctx;
313 vl_api_dhcp_client_details_t *mp;
317 mp = vl_msg_api_alloc (sizeof (*mp));
318 memset (mp, 0, sizeof (*mp));
320 mp->_vl_msg_id = ntohs (VL_API_DHCP_CLIENT_DETAILS);
321 mp->context = ctx->context;
323 dhcp_client_data_encode (&mp->client, client);
324 dhcp_client_lease_encode (&mp->lease, client);
326 vl_api_send_msg (ctx->reg, (u8 *) mp);
332 vl_api_dhcp_client_dump_t_handler (vl_api_dhcp_client_dump_t * mp)
334 vl_api_registration_t *reg;
336 reg = vl_api_client_index_to_registration (mp->client_index);
340 dhcp_client_send_walk_ctx_t ctx = {
342 .context = mp->context,
344 dhcp_client_walk (send_dhcp_client_entry, &ctx);
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()
354 #define vl_msg_name_crc_list
355 #include <vnet/vnet_all_api_h.h>
356 #undef vl_msg_name_crc_list
359 setup_message_id_table (api_main_t * am)
361 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
362 foreach_vl_msg_name_crc_dhcp;
366 static clib_error_t *
367 dhcp_api_hookup (vlib_main_t * vm)
369 api_main_t *am = &api_main;
372 vl_msg_api_set_handlers(VL_API_##N, #n, \
373 vl_api_##n##_t_handler, \
375 vl_api_##n##_t_endian, \
376 vl_api_##n##_t_print, \
377 sizeof(vl_api_##n##_t), 1);
382 * Set up the (msg_name, crc, message-id) table
384 setup_message_id_table (am);
389 VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
392 * fd.io coding-style-patch-verification: ON
395 * eval: (c-set-style "gnu")