DHCP Client Dump
[vpp.git] / src / vnet / dhcp / dhcp_api.c
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>
25 #include <vnet/dhcp/dhcp_proxy.h>
26 #include <vnet/dhcp/client.h>
27 #include <vnet/fib/fib_table.h>
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)            \
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)
53
54
55 static void
56 vl_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;
59   u8 *vpn_ascii_id;
60   int rv;
61
62   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);
69
70   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
71 }
72
73
74 static void vl_api_dhcp_proxy_config_t_handler
75   (vl_api_dhcp_proxy_config_t * mp)
76 {
77   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     }
92   else
93     {
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);
109 }
110
111 static void
112 vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
113 {
114   vl_api_registration_t *reg;
115
116   reg = vl_api_client_index_to_registration (mp->client_index);
117   if (!reg)
118     return;;
119
120   dhcp_proxy_dump ((mp->is_ip6 == 1 ?
121                     FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context);
122 }
123
124 void
125 dhcp_send_details (fib_protocol_t proto,
126                    void *opaque, u32 context, dhcp_proxy_t * proxy)
127 {
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;
132   fib_table_t *s_fib;
133   dhcp_vss_t *vss;
134   u32 count;
135   size_t n;
136
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);
140   if (!mp)
141     return;
142   memset (mp, 0, n);
143   mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
144   mp->context = context;
145   mp->count = count;
146
147   mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
148   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
153   if (vss)
154     {
155       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         }
171     }
172   else
173     mp->vss_type = VSS_TYPE_INVALID;
174
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   }
194
195   if (mp->is_ipv6)
196     {
197       memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
198     }
199   else
200     {
201       /* put the address in the first bytes */
202       memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
203     }
204   vl_api_send_msg (reg, (u8 *) mp);
205 }
206
207 static void
208 dhcp_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
228 static void
229 dhcp_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
252 static void
253 dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
254 {
255   vl_api_registration_t *reg;
256   vl_api_dhcp_compl_event_t *mp;
257
258   reg = vl_api_client_index_to_registration (client_index);
259   if (!reg)
260     return;
261
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);
266
267   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
268
269   vl_api_send_msg (reg, (u8 *) mp);
270 }
271
272 static 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;
277   u32 sw_if_index;
278   int rv = 0;
279
280   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     }
286
287   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);
297
298   BAD_SW_IF_INDEX_LABEL;
299
300   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
301 }
302
303 typedef 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
309 static int
310 send_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
331 static void
332 vl_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
347 /*
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
358 static void
359 setup_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
366 static clib_error_t *
367 dhcp_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
389 VLIB_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  */