Update DHCPv6 DUID code and fix coverity warnings
[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/dhcp/dhcp6_pd_client_dp.h>
28 #include <vnet/fib/fib_table.h>
29
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)            \
50 _(DHCP_PROXY_DUMP,dhcp_proxy_dump)                \
51 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)          \
52 _(DHCP_CLIENT_CONFIG, dhcp_client_config)         \
53 _(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)           \
57 _(DHCP6_DUID_LL_SET, dhcp6_duid_ll_set)
58
59
60 static void
61 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
62 {
63   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
64   u8 *vpn_ascii_id;
65   int rv;
66
67   mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0;
68   vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id);
69   rv =
70     dhcp_proxy_set_vss ((mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4),
71                         ntohl (mp->tbl_id), mp->vss_type, vpn_ascii_id,
72                         ntohl (mp->oui), ntohl (mp->vpn_index),
73                         mp->is_add == 0);
74
75   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
76 }
77
78
79 static void vl_api_dhcp_proxy_config_t_handler
80   (vl_api_dhcp_proxy_config_t * mp)
81 {
82   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
83   ip46_address_t src, server;
84   int rv = -1;
85
86   if (mp->is_ipv6)
87     {
88       clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6));
89       clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6));
90
91       rv = dhcp6_proxy_set_server (&server,
92                                    &src,
93                                    (u32) ntohl (mp->rx_vrf_id),
94                                    (u32) ntohl (mp->server_vrf_id),
95                                    (int) (mp->is_add == 0));
96     }
97   else
98     {
99       ip46_address_reset (&src);
100       ip46_address_reset (&server);
101
102       clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4));
103       clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4));
104
105       rv = dhcp4_proxy_set_server (&server,
106                                    &src,
107                                    (u32) ntohl (mp->rx_vrf_id),
108                                    (u32) ntohl (mp->server_vrf_id),
109                                    (int) (mp->is_add == 0));
110     }
111
112
113   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
114 }
115
116 static void
117 vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
118 {
119   vl_api_registration_t *reg;
120
121   reg = vl_api_client_index_to_registration (mp->client_index);
122   if (!reg)
123     return;;
124
125   dhcp_proxy_dump ((mp->is_ip6 == 1 ?
126                     FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context);
127 }
128
129 void
130 dhcp_send_details (fib_protocol_t proto,
131                    void *opaque, u32 context, dhcp_proxy_t * proxy)
132 {
133   vl_api_dhcp_proxy_details_t *mp;
134   vl_api_registration_t *reg = opaque;
135   vl_api_dhcp_server_t *v_server;
136   dhcp_server_t *server;
137   fib_table_t *s_fib;
138   dhcp_vss_t *vss;
139   u32 count;
140   size_t n;
141
142   count = vec_len (proxy->dhcp_servers);
143   n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
144   mp = vl_msg_api_alloc (n);
145   if (!mp)
146     return;
147   memset (mp, 0, n);
148   mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
149   mp->context = context;
150   mp->count = count;
151
152   mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
153   mp->rx_vrf_id =
154     htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
155
156   vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
157
158   if (vss)
159     {
160       mp->vss_type = vss->vss_type;
161       if (vss->vss_type == VSS_TYPE_ASCII)
162         {
163           u32 id_len = vec_len (vss->vpn_ascii_id);
164           clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len);
165         }
166       else if (vss->vss_type == VSS_TYPE_VPN_ID)
167         {
168           u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
169             + ((u32) vss->vpn_id[2]);
170           u32 fib_id = ((u32) vss->vpn_id[3] << 24) +
171             ((u32) vss->vpn_id[4] << 16) + ((u32) vss->vpn_id[5] << 8) +
172             ((u32) vss->vpn_id[6]);
173           mp->vss_oui = htonl (oui);
174           mp->vss_fib_id = htonl (fib_id);
175         }
176     }
177   else
178     mp->vss_type = VSS_TYPE_INVALID;
179
180   vec_foreach_index (count, proxy->dhcp_servers)
181   {
182     server = &proxy->dhcp_servers[count];
183     v_server = &mp->servers[count];
184
185     s_fib = fib_table_get (server->server_fib_index, proto);
186
187     v_server->server_vrf_id = htonl (s_fib->ft_table_id);
188
189     if (mp->is_ipv6)
190       {
191         memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16);
192       }
193     else
194       {
195         /* put the address in the first bytes */
196         memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4);
197       }
198   }
199
200   if (mp->is_ipv6)
201     {
202       memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
203     }
204   else
205     {
206       /* put the address in the first bytes */
207       memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
208     }
209   vl_api_send_msg (reg, (u8 *) mp);
210 }
211
212 static void
213 dhcp_client_lease_encode (vl_api_dhcp_lease_t * lease,
214                           const dhcp_client_t * client)
215 {
216   size_t len;
217
218   lease->is_ipv6 = 0;           // only support IPv6 clients
219   lease->sw_if_index = ntohl (client->sw_if_index);
220   lease->state = client->state;
221   len = clib_min (sizeof (lease->hostname) - 1, vec_len (client->hostname));
222   clib_memcpy (&lease->hostname, client->hostname, len);
223   lease->hostname[len] = 0;
224
225   lease->mask_width = client->subnet_mask_width;
226   clib_memcpy (&lease->host_address[0], (u8 *) & client->leased_address, 4);
227   clib_memcpy (&lease->router_address[0], (u8 *) & client->router_address, 4);
228
229   if (NULL != client->l2_rewrite)
230     clib_memcpy (&lease->host_mac[0], client->l2_rewrite + 6, 6);
231 }
232
233 static void
234 dhcp_client_data_encode (vl_api_dhcp_client_t * vclient,
235                          const dhcp_client_t * client)
236 {
237   size_t len;
238
239   vclient->sw_if_index = ntohl (client->sw_if_index);
240   len = clib_min (sizeof (vclient->hostname) - 1, vec_len (client->hostname));
241   clib_memcpy (&vclient->hostname, client->hostname, len);
242   vclient->hostname[len] = 0;
243
244   len = clib_min (sizeof (vclient->id) - 1,
245                   vec_len (client->client_identifier));
246   clib_memcpy (&vclient->id, client->client_identifier, len);
247   vclient->id[len] = 0;
248
249   if (NULL != client->event_callback)
250     vclient->want_dhcp_event = 1;
251   else
252     vclient->want_dhcp_event = 0;
253   vclient->set_broadcast_flag = client->set_broadcast_flag;
254   vclient->pid = client->pid;
255 }
256
257 static void
258 dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
259 {
260   vl_api_registration_t *reg;
261   vl_api_dhcp_compl_event_t *mp;
262
263   reg = vl_api_client_index_to_registration (client_index);
264   if (!reg)
265     return;
266
267   mp = vl_msg_api_alloc (sizeof (*mp));
268   mp->client_index = client_index;
269   mp->pid = client->pid;
270   dhcp_client_lease_encode (&mp->lease, client);
271
272   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
273
274   vl_api_send_msg (reg, (u8 *) mp);
275 }
276
277 static void vl_api_dhcp_client_config_t_handler
278   (vl_api_dhcp_client_config_t * mp)
279 {
280   vlib_main_t *vm = vlib_get_main ();
281   vl_api_dhcp_client_config_reply_t *rmp;
282   u32 sw_if_index;
283   int rv = 0;
284
285   sw_if_index = ntohl (mp->client.sw_if_index);
286   if (!vnet_sw_if_index_is_api_valid (sw_if_index))
287     {
288       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
289       goto bad_sw_if_index;
290     }
291
292   rv = dhcp_client_config (mp->is_add,
293                            mp->client_index,
294                            vm,
295                            sw_if_index,
296                            mp->client.hostname,
297                            mp->client.id,
298                            (mp->client.want_dhcp_event ?
299                             dhcp_compl_event_callback :
300                             NULL),
301                            mp->client.set_broadcast_flag, mp->client.pid);
302
303   BAD_SW_IF_INDEX_LABEL;
304
305   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
306 }
307
308 typedef struct dhcp_client_send_walk_ctx_t_
309 {
310   vl_api_registration_t *reg;
311   u32 context;
312 } dhcp_client_send_walk_ctx_t;
313
314 static int
315 send_dhcp_client_entry (const dhcp_client_t * client, void *arg)
316 {
317   dhcp_client_send_walk_ctx_t *ctx;
318   vl_api_dhcp_client_details_t *mp;
319
320   ctx = arg;
321
322   mp = vl_msg_api_alloc (sizeof (*mp));
323   memset (mp, 0, sizeof (*mp));
324
325   mp->_vl_msg_id = ntohs (VL_API_DHCP_CLIENT_DETAILS);
326   mp->context = ctx->context;
327
328   dhcp_client_data_encode (&mp->client, client);
329   dhcp_client_lease_encode (&mp->lease, client);
330
331   vl_api_send_msg (ctx->reg, (u8 *) mp);
332
333   return (1);
334 }
335
336 static void
337 vl_api_dhcp_client_dump_t_handler (vl_api_dhcp_client_dump_t * mp)
338 {
339   vl_api_registration_t *reg;
340
341   reg = vl_api_client_index_to_registration (mp->client_index);
342   if (!reg)
343     return;
344
345   dhcp_client_send_walk_ctx_t ctx = {
346     .reg = reg,
347     .context = mp->context,
348   };
349   dhcp_client_walk (send_dhcp_client_entry, &ctx);
350 }
351
352 /*
353  * dhcp_api_hookup
354  * Add vpe's API message handlers to the table.
355  * vlib has alread mapped shared memory and
356  * added the client registration handlers.
357  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
358  */
359 #define vl_msg_name_crc_list
360 #include <vnet/vnet_all_api_h.h>
361 #undef vl_msg_name_crc_list
362
363 static void
364 setup_message_id_table (api_main_t * am)
365 {
366 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
367   foreach_vl_msg_name_crc_dhcp;
368 #undef _
369 }
370
371 static clib_error_t *
372 dhcp_api_hookup (vlib_main_t * vm)
373 {
374   api_main_t *am = &api_main;
375
376 #define _(N,n)                                                  \
377     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
378                            vl_api_##n##_t_handler,              \
379                            vl_noop_handler,                     \
380                            vl_api_##n##_t_endian,               \
381                            vl_api_##n##_t_print,                \
382                            sizeof(vl_api_##n##_t), 1);
383   foreach_vpe_api_msg;
384 #undef _
385
386   /*
387    * Set up the (msg_name, crc, message-id) table
388    */
389   setup_message_id_table (am);
390
391   dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index,
392                                DHCP6_PD_DP_REPLY_REPORT);
393
394   return 0;
395 }
396
397 VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
398
399 /*
400  * fd.io coding-style-patch-verification: ON
401  *
402  * Local Variables:
403  * eval: (c-set-style "gnu")
404  * End:
405  */