docs: Use newer Ubuntu LTS in tutorial
[vpp.git] / src / plugins / 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 <dhcp/dhcp_proxy.h>
26 #include <dhcp/client.h>
27 #include <dhcp/dhcp6_pd_client_dp.h>
28 #include <dhcp/dhcp6_ia_na_client_dp.h>
29 #include <dhcp/dhcp6_client_common_dp.h>
30 #include <vnet/fib/fib_table.h>
31 #include <vnet/ip/ip_types_api.h>
32
33 /* define message IDs */
34 #include <vnet/format_fns.h>
35 #include <dhcp/dhcp.api_enum.h>
36 #include <dhcp/dhcp.api_types.h>
37
38 /**
39  * Base message ID fot the plugin
40  */
41 static u32 dhcp_base_msg_id;
42 #define REPLY_MSG_ID_BASE dhcp_base_msg_id
43
44 #include <vlibapi/api_helper_macros.h>
45
46 #define  DHCP_PLUGIN_VERSION_MAJOR 1
47 #define  DHCP_PLUGIN_VERSION_MINOR 0
48
49 static void
50 vl_api_dhcp_plugin_get_version_t_handler (vl_api_dhcp_plugin_get_version_t *
51                                           mp)
52 {
53   vl_api_dhcp_plugin_get_version_reply_t *rmp;
54   int msg_size = sizeof (*rmp);
55   vl_api_registration_t *reg;
56
57   reg = vl_api_client_index_to_registration (mp->client_index);
58   if (!reg)
59     return;
60
61   rmp = vl_msg_api_alloc (msg_size);
62   clib_memset (rmp, 0, msg_size);
63   rmp->_vl_msg_id =
64     ntohs (VL_API_DHCP_PLUGIN_GET_VERSION_REPLY + REPLY_MSG_ID_BASE);
65   rmp->context = mp->context;
66   rmp->major = htonl (DHCP_PLUGIN_VERSION_MAJOR);
67   rmp->minor = htonl (DHCP_PLUGIN_VERSION_MINOR);
68
69   vl_api_send_msg (reg, (u8 *) rmp);
70 }
71
72 static void
73 vl_api_dhcp_plugin_control_ping_t_handler (vl_api_dhcp_plugin_control_ping_t *
74                                            mp)
75 {
76   vl_api_dhcp_plugin_control_ping_reply_t *rmp;
77   int rv = 0;
78
79   REPLY_MACRO2 (VL_API_DHCP_PLUGIN_CONTROL_PING_REPLY,
80   ({
81     rmp->vpe_pid = ntohl (getpid ());
82   }));
83 }
84
85 static void
86 vl_api_dhcp6_duid_ll_set_t_handler (vl_api_dhcp6_duid_ll_set_t * mp)
87 {
88   vl_api_dhcp6_duid_ll_set_reply_t *rmp;
89   dhcpv6_duid_ll_string_t *duid;
90   int rv = 0;
91
92   duid = (dhcpv6_duid_ll_string_t *) mp->duid_ll;
93   if (duid->duid_type != htons (DHCPV6_DUID_LL))
94     {
95       rv = VNET_API_ERROR_INVALID_VALUE;
96       goto reply;
97     }
98   clib_memcpy (&client_duid, &duid, sizeof (client_duid));
99
100 reply:
101   REPLY_MACRO (VL_API_DHCP6_DUID_LL_SET_REPLY);
102 }
103
104 static void
105 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
106 {
107   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
108   u8 *vpn_ascii_id;
109   int rv;
110
111   mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0;
112   vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id);
113   rv =
114     dhcp_proxy_set_vss ((mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4),
115                         ntohl (mp->tbl_id), ntohl (mp->vss_type),
116                         vpn_ascii_id, ntohl (mp->oui), ntohl (mp->vpn_index),
117                         mp->is_add == 0);
118
119   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
120 }
121
122
123 static void vl_api_dhcp_proxy_config_t_handler
124   (vl_api_dhcp_proxy_config_t * mp)
125 {
126   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
127   ip46_address_t src, server;
128   int rv = -1;
129
130   if (mp->dhcp_src_address.af != mp->dhcp_server.af)
131     {
132       rv = VNET_API_ERROR_INVALID_ARGUMENT;
133       goto reply;
134     }
135
136   ip_address_decode (&mp->dhcp_src_address, &src);
137   ip_address_decode (&mp->dhcp_server, &server);
138
139   if (mp->dhcp_src_address.af == ADDRESS_IP4)
140     {
141       rv = dhcp4_proxy_set_server (&server,
142                                    &src,
143                                    (u32) ntohl (mp->rx_vrf_id),
144                                    (u32) ntohl (mp->server_vrf_id),
145                                    (int) (mp->is_add == 0));
146     }
147   else
148     {
149       rv = dhcp6_proxy_set_server (&server,
150                                    &src,
151                                    (u32) ntohl (mp->rx_vrf_id),
152                                    (u32) ntohl (mp->server_vrf_id),
153                                    (int) (mp->is_add == 0));
154     }
155
156 reply:
157   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
158 }
159
160 static void
161 vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
162 {
163   vl_api_registration_t *reg;
164
165   reg = vl_api_client_index_to_registration (mp->client_index);
166   if (!reg)
167     return;;
168
169   dhcp_proxy_dump ((mp->is_ip6 == 1 ?
170                     FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context);
171 }
172
173 void
174 dhcp_send_details (fib_protocol_t proto,
175                    void *opaque, u32 context, dhcp_proxy_t * proxy)
176 {
177   vl_api_dhcp_proxy_details_t *mp;
178   vl_api_registration_t *reg = opaque;
179   vl_api_dhcp_server_t *v_server;
180   dhcp_server_t *server;
181   fib_table_t *s_fib;
182   dhcp_vss_t *vss;
183   u32 count;
184   size_t n;
185
186   count = vec_len (proxy->dhcp_servers);
187   n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
188   mp = vl_msg_api_alloc (n);
189   if (!mp)
190     return;
191   clib_memset (mp, 0, n);
192   mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS + REPLY_MSG_ID_BASE);
193   mp->context = context;
194   mp->count = count;
195
196   mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
197   mp->rx_vrf_id =
198     htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
199
200   vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
201
202   if (vss)
203     {
204       mp->vss_type = ntohl (vss->vss_type);
205       if (vss->vss_type == VSS_TYPE_ASCII)
206         {
207           u32 id_len = vec_len (vss->vpn_ascii_id);
208           clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len);
209         }
210       else if (vss->vss_type == VSS_TYPE_VPN_ID)
211         {
212           u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
213             + ((u32) vss->vpn_id[2]);
214           u32 fib_id = ((u32) vss->vpn_id[3] << 24) +
215             ((u32) vss->vpn_id[4] << 16) + ((u32) vss->vpn_id[5] << 8) +
216             ((u32) vss->vpn_id[6]);
217           mp->vss_oui = htonl (oui);
218           mp->vss_fib_id = htonl (fib_id);
219         }
220     }
221   else
222     mp->vss_type = VSS_TYPE_INVALID;
223
224   vec_foreach_index (count, proxy->dhcp_servers)
225   {
226     server = &proxy->dhcp_servers[count];
227     v_server = &mp->servers[count];
228
229     s_fib = fib_table_get (server->server_fib_index, proto);
230
231     v_server->server_vrf_id = htonl (s_fib->ft_table_id);
232
233     if (mp->is_ipv6)
234       {
235         memcpy (&v_server->dhcp_server.un, &server->dhcp_server.ip6, 16);
236       }
237     else
238       {
239         /* put the address in the first bytes */
240         memcpy (&v_server->dhcp_server.un, &server->dhcp_server.ip4, 4);
241       }
242   }
243
244   if (mp->is_ipv6)
245     {
246       memcpy (&mp->dhcp_src_address.un, &proxy->dhcp_src_address.ip6, 16);
247     }
248   else
249     {
250       /* put the address in the first bytes */
251       memcpy (&mp->dhcp_src_address.un, &proxy->dhcp_src_address.ip4, 4);
252     }
253   vl_api_send_msg (reg, (u8 *) mp);
254 }
255
256 static void
257 dhcp_client_lease_encode (vl_api_dhcp_lease_t * lease,
258                           const dhcp_client_t * client)
259 {
260   size_t len;
261   u8 i;
262
263   lease->is_ipv6 = 0;           // only support IPv4 clients
264   lease->sw_if_index = ntohl (client->sw_if_index);
265   lease->state = ntohl (client->state);
266   len = clib_min (sizeof (lease->hostname) - 1, vec_len (client->hostname));
267   clib_memcpy (&lease->hostname, client->hostname, len);
268   lease->hostname[len] = 0;
269
270   lease->mask_width = client->installed.subnet_mask_width;
271   clib_memcpy (&lease->host_address.un,
272                (u8 *) & client->installed.leased_address,
273                sizeof (ip4_address_t));
274   clib_memcpy (&lease->router_address.un,
275                (u8 *) & client->installed.router_address,
276                sizeof (ip4_address_t));
277
278   lease->count = vec_len (client->domain_server_address);
279   for (i = 0; i < lease->count; i++)
280     clib_memcpy (&lease->domain_server[i].address.un.ip4,
281                  (u8 *) &client->domain_server_address[i],
282                  sizeof (ip4_address_t));
283
284   clib_memcpy (&lease->host_mac[0], client->client_hardware_address, 6);
285 }
286
287 static void
288 dhcp_client_data_encode (vl_api_dhcp_client_t * vclient,
289                          const dhcp_client_t * client)
290 {
291   size_t len;
292
293   vclient->sw_if_index = ntohl (client->sw_if_index);
294   len = clib_min (sizeof (vclient->hostname) - 1, vec_len (client->hostname));
295   clib_memcpy (&vclient->hostname, client->hostname, len);
296   vclient->hostname[len] = 0;
297
298   len = clib_min (sizeof (vclient->id) - 1,
299                   vec_len (client->client_identifier));
300   clib_memcpy (&vclient->id, client->client_identifier, len);
301   vclient->id[len] = 0;
302
303   if (NULL != client->event_callback)
304     vclient->want_dhcp_event = 1;
305   else
306     vclient->want_dhcp_event = 0;
307   vclient->set_broadcast_flag = client->set_broadcast_flag;
308   vclient->dscp = ip_dscp_encode (client->dscp);
309   vclient->pid = client->pid;
310 }
311
312 static void
313 dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
314 {
315   vl_api_registration_t *reg;
316   vl_api_dhcp_compl_event_t *mp;
317
318   reg = vl_api_client_index_to_registration (client_index);
319   if (!reg)
320     return;
321
322   mp = vl_msg_api_alloc (sizeof (*mp) +
323                          sizeof (vl_api_domain_server_t) *
324                            vec_len (client->domain_server_address));
325   mp->client_index = client_index;
326   mp->pid = client->pid;
327   dhcp_client_lease_encode (&mp->lease, client);
328
329   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT + REPLY_MSG_ID_BASE);
330
331   vl_api_send_msg (reg, (u8 *) mp);
332 }
333
334 static void vl_api_dhcp_client_config_t_handler
335   (vl_api_dhcp_client_config_t * mp)
336 {
337   vlib_main_t *vm = vlib_get_main ();
338   vl_api_dhcp_client_config_reply_t *rmp;
339   u32 sw_if_index;
340   ip_dscp_t dscp;
341   int rv = 0;
342
343   VALIDATE_SW_IF_INDEX (&(mp->client));
344
345   sw_if_index = ntohl (mp->client.sw_if_index);
346   dscp = ip_dscp_decode (mp->client.dscp);
347
348   rv = dhcp_client_config (mp->is_add,
349                            mp->client_index,
350                            vm,
351                            sw_if_index,
352                            mp->client.hostname,
353                            mp->client.id,
354                            (mp->client.want_dhcp_event ?
355                             dhcp_compl_event_callback :
356                             NULL),
357                            mp->client.set_broadcast_flag,
358                            dscp, mp->client.pid);
359
360   BAD_SW_IF_INDEX_LABEL;
361   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
362 }
363
364 typedef struct dhcp_client_send_walk_ctx_t_
365 {
366   vl_api_registration_t *reg;
367   u32 context;
368 } dhcp_client_send_walk_ctx_t;
369
370 static int
371 send_dhcp_client_entry (const dhcp_client_t * client, void *arg)
372 {
373   dhcp_client_send_walk_ctx_t *ctx;
374   vl_api_dhcp_client_details_t *mp;
375   u32 count;
376   size_t n;
377
378   ctx = arg;
379
380   count = vec_len (client->domain_server_address);
381   n = sizeof (*mp) + (count * sizeof (vl_api_domain_server_t));
382   mp = vl_msg_api_alloc (n);
383   if (!mp)
384     return 0;
385   clib_memset (mp, 0, n);
386
387   mp->_vl_msg_id = ntohs (VL_API_DHCP_CLIENT_DETAILS + REPLY_MSG_ID_BASE);
388   mp->context = ctx->context;
389
390   dhcp_client_data_encode (&mp->client, client);
391   dhcp_client_lease_encode (&mp->lease, client);
392
393   vl_api_send_msg (ctx->reg, (u8 *) mp);
394
395   return (1);
396 }
397
398 static void
399 vl_api_dhcp_client_dump_t_handler (vl_api_dhcp_client_dump_t * mp)
400 {
401   vl_api_registration_t *reg;
402
403   reg = vl_api_client_index_to_registration (mp->client_index);
404   if (!reg)
405     return;
406
407   dhcp_client_send_walk_ctx_t ctx = {
408     .reg = reg,
409     .context = mp->context,
410   };
411   dhcp_client_walk (send_dhcp_client_entry, &ctx);
412 }
413
414 static void
415   vl_api_dhcp6_clients_enable_disable_t_handler
416   (vl_api_dhcp6_clients_enable_disable_t * mp)
417 {
418   vl_api_dhcp6_clients_enable_disable_reply_t *rmp;
419   int rv = 0;
420
421   dhcp6_clients_enable_disable (mp->enable);
422
423   REPLY_MACRO (VL_API_DHCP6_CLIENTS_ENABLE_DISABLE_REPLY);
424 }
425
426 void
427   vl_api_want_dhcp6_reply_events_t_handler
428   (vl_api_want_dhcp6_reply_events_t * mp)
429 {
430   vpe_api_main_t *am = &vpe_api_main;
431   vl_api_want_dhcp6_reply_events_reply_t *rmp;
432   int rv = 0;
433
434   uword *p =
435     hash_get (am->dhcp6_reply_events_registration_hash, mp->client_index);
436   vpe_client_registration_t *rp;
437   if (p)
438     {
439       if (mp->enable_disable)
440         {
441           clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
442           rv = VNET_API_ERROR_INVALID_REGISTRATION;
443           goto reply;
444         }
445       else
446         {
447           rp = pool_elt_at_index (am->dhcp6_reply_events_registrations, p[0]);
448           pool_put (am->dhcp6_reply_events_registrations, rp);
449           hash_unset (am->dhcp6_reply_events_registration_hash,
450                       mp->client_index);
451           if (pool_elts (am->dhcp6_reply_events_registrations) == 0)
452             dhcp6_set_publisher_node (~0, DHCP6_DP_REPORT_MAX);
453           goto reply;
454         }
455     }
456   if (mp->enable_disable == 0)
457     {
458       clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
459       rv = VNET_API_ERROR_INVALID_REGISTRATION;
460       goto reply;
461     }
462   pool_get (am->dhcp6_reply_events_registrations, rp);
463   rp->client_index = mp->client_index;
464   rp->client_pid = ntohl (mp->pid);
465   hash_set (am->dhcp6_reply_events_registration_hash, rp->client_index,
466             rp - am->dhcp6_reply_events_registrations);
467   dhcp6_set_publisher_node (dhcp6_reply_process_node.index,
468                             DHCP6_DP_REPLY_REPORT);
469
470 reply:
471   REPLY_MACRO (VL_API_WANT_DHCP6_REPLY_EVENTS_REPLY);
472 }
473
474 void
475   vl_api_want_dhcp6_pd_reply_events_t_handler
476   (vl_api_want_dhcp6_pd_reply_events_t * mp)
477 {
478   vpe_api_main_t *am = &vpe_api_main;
479   vl_api_want_dhcp6_pd_reply_events_reply_t *rmp;
480   int rv = 0;
481
482   uword *p =
483     hash_get (am->dhcp6_pd_reply_events_registration_hash, mp->client_index);
484   vpe_client_registration_t *rp;
485   if (p)
486     {
487       if (mp->enable_disable)
488         {
489           clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
490           rv = VNET_API_ERROR_INVALID_REGISTRATION;
491           goto reply;
492         }
493       else
494         {
495           rp =
496             pool_elt_at_index (am->dhcp6_pd_reply_events_registrations, p[0]);
497           pool_put (am->dhcp6_pd_reply_events_registrations, rp);
498           hash_unset (am->dhcp6_pd_reply_events_registration_hash,
499                       mp->client_index);
500           if (pool_elts (am->dhcp6_pd_reply_events_registrations) == 0)
501             dhcp6_pd_set_publisher_node (~0, DHCP6_PD_DP_REPORT_MAX);
502           goto reply;
503         }
504     }
505   if (mp->enable_disable == 0)
506     {
507       clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
508       rv = VNET_API_ERROR_INVALID_REGISTRATION;
509       goto reply;
510     }
511   pool_get (am->dhcp6_pd_reply_events_registrations, rp);
512   rp->client_index = mp->client_index;
513   rp->client_pid = ntohl (mp->pid);
514   hash_set (am->dhcp6_pd_reply_events_registration_hash, rp->client_index,
515             rp - am->dhcp6_pd_reply_events_registrations);
516   dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index,
517                                DHCP6_PD_DP_REPLY_REPORT);
518
519 reply:
520   REPLY_MACRO (VL_API_WANT_DHCP6_PD_REPLY_EVENTS_REPLY);
521 }
522
523 void
524   vl_api_dhcp6_send_client_message_t_handler
525   (vl_api_dhcp6_send_client_message_t * mp)
526 {
527   vl_api_dhcp6_send_client_message_reply_t *rmp;
528   dhcp6_send_client_message_params_t params;
529   vlib_main_t *vm = vlib_get_main ();
530   u32 n_addresses;
531   u32 i;
532   int rv = 0;
533
534   VALIDATE_SW_IF_INDEX (mp);
535
536   BAD_SW_IF_INDEX_LABEL;
537   REPLY_MACRO (VL_API_DHCP6_SEND_CLIENT_MESSAGE_REPLY);
538
539   if (rv != 0)
540     return;
541
542   params.sw_if_index = ntohl (mp->sw_if_index);
543   params.server_index = ntohl (mp->server_index);
544   params.irt = ntohl (mp->irt);
545   params.mrt = ntohl (mp->mrt);
546   params.mrc = ntohl (mp->mrc);
547   params.mrd = ntohl (mp->mrd);
548   params.msg_type = ntohl (mp->msg_type);
549   params.T1 = ntohl (mp->T1);
550   params.T2 = ntohl (mp->T2);
551   n_addresses = ntohl (mp->n_addresses);
552   /* Make sure that the number of addresses is sane */
553   if (n_addresses * sizeof (params.addresses) > vl_msg_api_max_length (mp))
554     {
555       rv = VNET_API_ERROR_INVALID_VALUE;
556       goto bad_sw_if_index;
557     }
558   params.addresses = 0;
559   if (n_addresses > 0)
560     vec_validate (params.addresses, n_addresses - 1);
561   for (i = 0; i < n_addresses; i++)
562     {
563       vl_api_dhcp6_address_info_t *ai = &mp->addresses[i];
564       dhcp6_send_client_message_params_address_t *addr = &params.addresses[i];
565       addr->preferred_lt = ntohl (ai->preferred_time);
566       addr->valid_lt = ntohl (ai->valid_time);
567       ip6_address_decode (ai->address, &addr->address);
568     }
569
570   dhcp6_send_client_message (vm, ntohl (mp->sw_if_index), mp->stop, &params);
571 }
572
573 void
574   vl_api_dhcp6_pd_send_client_message_t_handler
575   (vl_api_dhcp6_pd_send_client_message_t * mp)
576 {
577   vl_api_dhcp6_pd_send_client_message_reply_t *rmp;
578   dhcp6_pd_send_client_message_params_t params;
579   vlib_main_t *vm = vlib_get_main ();
580   u32 n_prefixes;
581   u32 i;
582   int rv = 0;
583
584   VALIDATE_SW_IF_INDEX (mp);
585
586   BAD_SW_IF_INDEX_LABEL;
587   REPLY_MACRO (VL_API_DHCP6_PD_SEND_CLIENT_MESSAGE_REPLY);
588
589   if (rv != 0)
590     return;
591
592   params.sw_if_index = ntohl (mp->sw_if_index);
593   params.server_index = ntohl (mp->server_index);
594   params.irt = ntohl (mp->irt);
595   params.mrt = ntohl (mp->mrt);
596   params.mrc = ntohl (mp->mrc);
597   params.mrd = ntohl (mp->mrd);
598   params.msg_type = ntohl (mp->msg_type);
599   params.T1 = ntohl (mp->T1);
600   params.T2 = ntohl (mp->T2);
601   n_prefixes = ntohl (mp->n_prefixes);
602
603   /* Minimal check to see that the number of prefixes is sane */
604   if (n_prefixes * sizeof (params.prefixes) > vl_msg_api_max_length (mp))
605     {
606       rv = VNET_API_ERROR_INVALID_VALUE;
607       goto bad_sw_if_index;
608     }
609
610   params.prefixes = 0;
611   if (n_prefixes > 0)
612     vec_validate (params.prefixes, n_prefixes - 1);
613   for (i = 0; i < n_prefixes; i++)
614     {
615       vl_api_dhcp6_pd_prefix_info_t *pi = &mp->prefixes[i];
616       dhcp6_pd_send_client_message_params_prefix_t *pref =
617         &params.prefixes[i];
618       pref->preferred_lt = ntohl (pi->preferred_time);
619       pref->valid_lt = ntohl (pi->valid_time);
620       ip6_address_decode (pi->prefix.address, &pref->prefix);
621       pref->prefix_length = pi->prefix.len;
622     }
623
624   dhcp6_pd_send_client_message (vm, ntohl (mp->sw_if_index), mp->stop,
625                                 &params);
626 }
627
628 static clib_error_t *
629 call_dhcp6_reply_event_callbacks (void *data,
630                                   _vnet_dhcp6_reply_event_function_list_elt_t
631                                   * elt)
632 {
633   clib_error_t *error = 0;
634
635   while (elt)
636     {
637       error = elt->fp (data);
638       if (error)
639         return error;
640       elt = elt->next_dhcp6_reply_event_function;
641     }
642
643   return error;
644 }
645
646 static void
647 vl_api_dhcp_client_detect_enable_disable_t_handler (
648   vl_api_dhcp_client_detect_enable_disable_t *mp)
649 {
650   vl_api_dhcp_client_detect_enable_disable_reply_t *rmp;
651   int rv = 0;
652   VALIDATE_SW_IF_INDEX (mp);
653
654   if (mp->enable)
655     {
656       vnet_feature_enable_disable ("ip4-unicast", "ip4-dhcp-client-detect",
657                                    clib_net_to_host_u32 (mp->sw_if_index),
658                                    1 /* enable */, 0, 0);
659     }
660   else
661     {
662       vnet_feature_enable_disable ("ip4-unicast", "ip4-dhcp-client-detect",
663                                    clib_net_to_host_u32 (mp->sw_if_index),
664                                    0 /* disable */, 0, 0);
665     }
666
667   BAD_SW_IF_INDEX_LABEL;
668
669   REPLY_MACRO (VL_API_DHCP_CLIENT_DETECT_ENABLE_DISABLE_REPLY);
670 }
671 static uword
672 dhcp6_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
673                      vlib_frame_t * f)
674 {
675   /* These cross the longjmp  boundary (vlib_process_wait_for_event)
676    * and need to be volatile - to prevent them from being optimized into
677    * a register - which could change during suspension */
678
679   while (1)
680     {
681       vlib_process_wait_for_event (vm);
682       uword event_type = DHCP6_DP_REPLY_REPORT;
683       void *event_data = vlib_process_get_event_data (vm, &event_type);
684
685       int i;
686       if (event_type == DHCP6_DP_REPLY_REPORT)
687         {
688           address_report_t *events = event_data;
689           for (i = 0; i < vec_len (events); i++)
690             {
691               u32 event_size =
692                 sizeof (vl_api_dhcp6_reply_event_t) +
693                 vec_len (events[i].addresses) *
694                 sizeof (vl_api_dhcp6_address_info_t);
695               vl_api_dhcp6_reply_event_t *event = clib_mem_alloc (event_size);
696               clib_memset (event, 0, event_size);
697
698               event->sw_if_index = htonl (events[i].body.sw_if_index);
699               event->server_index = htonl (events[i].body.server_index);
700               event->msg_type = events[i].body.msg_type;
701               event->T1 = htonl (events[i].body.T1);
702               event->T2 = htonl (events[i].body.T2);
703               event->inner_status_code =
704                 htons (events[i].body.inner_status_code);
705               event->status_code = htons (events[i].body.status_code);
706               event->preference = events[i].body.preference;
707
708               event->n_addresses = htonl (vec_len (events[i].addresses));
709               vl_api_dhcp6_address_info_t *address =
710                 (typeof (address)) event->addresses;
711               u32 j;
712               for (j = 0; j < vec_len (events[i].addresses); j++)
713                 {
714                   dhcp6_address_info_t *info = &events[i].addresses[j];
715                   ip6_address_encode (&info->address, address->address);
716                   address->valid_time = htonl (info->valid_time);
717                   address->preferred_time = htonl (info->preferred_time);
718                   address++;
719                 }
720               vec_free (events[i].addresses);
721
722               dhcp6_ia_na_client_public_main_t *dcpm =
723                 &dhcp6_ia_na_client_public_main;
724               call_dhcp6_reply_event_callbacks (event, dcpm->functions);
725
726               vpe_client_registration_t *reg;
727               pool_foreach (reg, vpe_api_main.dhcp6_reply_events_registrations)
728                {
729                 vl_api_registration_t *vl_reg;
730                 vl_reg =
731                   vl_api_client_index_to_registration (reg->client_index);
732                 if (vl_reg && vl_api_can_send_msg (vl_reg))
733                   {
734                     vl_api_dhcp6_reply_event_t *msg =
735                       vl_msg_api_alloc (event_size);
736                     clib_memcpy (msg, event, event_size);
737                     msg->_vl_msg_id = htons (VL_API_DHCP6_REPLY_EVENT + REPLY_MSG_ID_BASE);
738                     msg->client_index = reg->client_index;
739                     msg->pid = reg->client_pid;
740                     vl_api_send_msg (vl_reg, (u8 *) msg);
741                   }
742               }
743
744               clib_mem_free (event);
745             }
746         }
747       vlib_process_put_event_data (vm, event_data);
748     }
749
750   return 0;
751 }
752
753 VLIB_REGISTER_NODE (dhcp6_reply_process_node) = {
754   .function = dhcp6_reply_process,
755   .type = VLIB_NODE_TYPE_PROCESS,
756   .name = "dhcp6-reply-publisher-process",
757 };
758
759 static clib_error_t *
760 call_dhcp6_pd_reply_event_callbacks (void *data,
761                                      _vnet_dhcp6_pd_reply_event_function_list_elt_t
762                                      * elt)
763 {
764   clib_error_t *error = 0;
765
766   while (elt)
767     {
768       error = elt->fp (data);
769       if (error)
770         return error;
771       elt = elt->next_dhcp6_pd_reply_event_function;
772     }
773
774   return error;
775 }
776
777 static uword
778 dhcp6_pd_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
779                         vlib_frame_t * f)
780 {
781   /* These cross the longjmp  boundary (vlib_process_wait_for_event)
782    * and need to be volatile - to prevent them from being optimized into
783    * a register - which could change during suspension */
784
785   while (1)
786     {
787       vlib_process_wait_for_event (vm);
788       uword event_type = DHCP6_PD_DP_REPLY_REPORT;
789       void *event_data = vlib_process_get_event_data (vm, &event_type);
790
791       int i;
792       if (event_type == DHCP6_PD_DP_REPLY_REPORT)
793         {
794           prefix_report_t *events = event_data;
795           for (i = 0; i < vec_len (events); i++)
796             {
797               u32 event_size =
798                 sizeof (vl_api_dhcp6_pd_reply_event_t) +
799                 vec_len (events[i].prefixes) *
800                 sizeof (vl_api_dhcp6_pd_prefix_info_t);
801               vl_api_dhcp6_pd_reply_event_t *event =
802                 clib_mem_alloc (event_size);
803               clib_memset (event, 0, event_size);
804
805               event->sw_if_index = htonl (events[i].body.sw_if_index);
806               event->server_index = htonl (events[i].body.server_index);
807               event->msg_type = events[i].body.msg_type;
808               event->T1 = htonl (events[i].body.T1);
809               event->T2 = htonl (events[i].body.T2);
810               event->inner_status_code =
811                 htons (events[i].body.inner_status_code);
812               event->status_code = htons (events[i].body.status_code);
813               event->preference = events[i].body.preference;
814
815               event->n_prefixes = htonl (vec_len (events[i].prefixes));
816               vl_api_dhcp6_pd_prefix_info_t *prefix =
817                 (typeof (prefix)) event->prefixes;
818               u32 j;
819               for (j = 0; j < vec_len (events[i].prefixes); j++)
820                 {
821                   dhcp6_prefix_info_t *info = &events[i].prefixes[j];
822                   ip6_address_encode (&info->prefix, prefix->prefix.address);
823                   prefix->prefix.len = info->prefix_length;
824                   prefix->valid_time = htonl (info->valid_time);
825                   prefix->preferred_time = htonl (info->preferred_time);
826                   prefix++;
827                 }
828               vec_free (events[i].prefixes);
829
830               dhcp6_pd_client_public_main_t *dpcpm =
831                 &dhcp6_pd_client_public_main;
832               call_dhcp6_pd_reply_event_callbacks (event, dpcpm->functions);
833
834               vpe_client_registration_t *reg;
835               pool_foreach (reg, vpe_api_main.dhcp6_pd_reply_events_registrations)
836                {
837                 vl_api_registration_t *vl_reg;
838                 vl_reg =
839                   vl_api_client_index_to_registration (reg->client_index);
840                 if (vl_reg && vl_api_can_send_msg (vl_reg))
841                   {
842                     vl_api_dhcp6_pd_reply_event_t *msg =
843                       vl_msg_api_alloc (event_size);
844                     clib_memcpy (msg, event, event_size);
845                     msg->_vl_msg_id = htons (VL_API_DHCP6_PD_REPLY_EVENT + REPLY_MSG_ID_BASE);
846                     msg->client_index = reg->client_index;
847                     msg->pid = reg->client_pid;
848                     vl_api_send_msg (vl_reg, (u8 *) msg);
849                   }
850               }
851
852               clib_mem_free (event);
853             }
854         }
855       vlib_process_put_event_data (vm, event_data);
856     }
857
858   return 0;
859 }
860
861 VLIB_REGISTER_NODE (dhcp6_pd_reply_process_node) = {
862   .function = dhcp6_pd_reply_process,
863   .type = VLIB_NODE_TYPE_PROCESS,
864   .name = "dhcp6-pd-reply-publisher-process",
865 };
866
867 /*
868  * dhcp_api_hookup
869  * Add vpe's API message handlers to the table.
870  * vlib has already mapped shared memory and
871  * added the client registration handlers.
872  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
873  */
874 #include <dhcp/dhcp.api.c>
875
876 static clib_error_t *
877 dhcp_api_hookup (vlib_main_t * vm)
878 {
879   /*
880    * Set up the (msg_name, crc, message-id) table
881    */
882   dhcp_base_msg_id = setup_message_id_table ();
883
884   dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index,
885                                DHCP6_PD_DP_REPLY_REPORT);
886   dhcp6_set_publisher_node (dhcp6_reply_process_node.index,
887                             DHCP6_DP_REPLY_REPORT);
888
889   return 0;
890 }
891
892 VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
893
894 #include <vlib/unix/plugin.h>
895 #include <vpp/app/version.h>
896
897 VLIB_PLUGIN_REGISTER () = {
898     .version = VPP_BUILD_VER,
899     .description = "Dynamic Host Configuration Protocol (DHCP)",
900 };
901
902 /*
903  * fd.io coding-style-patch-verification: ON
904  *
905  * Local Variables:
906  * eval: (c-set-style "gnu")
907  * End:
908  */