Consolidate DHCP v4 and V6 implementation. No functional change intended
[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
28 #include <vnet/vnet_msg_enum.h>
29
30 #define vl_typedefs             /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
32 #undef vl_typedefs
33
34 #define vl_endianfun            /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #define vl_printfun
41 #include <vnet/vnet_all_api_h.h>
42 #undef vl_printfun
43
44 #include <vlibapi/api_helper_macros.h>
45
46 #define foreach_vpe_api_msg                       \
47 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)            \
48 _(DHCP_PROXY_DUMP,dhcp_proxy_dump)                \
49 _(DHCP_PROXY_DETAILS,dhcp_proxy_details)          \
50 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)          \
51 _(DHCP_CLIENT_CONFIG, dhcp_client_config)
52
53
54 static void
55 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
56 {
57   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
58   int rv;
59
60   rv = dhcp_proxy_set_vss ((mp->is_ipv6 ?
61                             FIB_PROTOCOL_IP6 :
62                             FIB_PROTOCOL_IP4),
63                            ntohl (mp->tbl_id),
64                            ntohl (mp->oui),
65                            ntohl (mp->fib_id), (int) mp->is_add == 0);
66
67   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
68 }
69
70
71 static void vl_api_dhcp_proxy_config_t_handler
72   (vl_api_dhcp_proxy_config_t * mp)
73 {
74   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
75   ip46_address_t src, server;
76   int rv = -1;
77
78   if (mp->is_ipv6)
79     {
80       clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6));
81       clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6));
82
83       rv = dhcp6_proxy_set_server (&server,
84                                    &src,
85                                    (u32) ntohl (mp->rx_vrf_id),
86                                    (u32) ntohl (mp->server_vrf_id),
87                                    (int) (mp->is_add == 0));
88     }
89   else
90     {
91       ip46_address_reset (&src);
92       ip46_address_reset (&server);
93
94       clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4));
95       clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4));
96
97       rv = dhcp4_proxy_set_server (&server,
98                                    &src,
99                                    (u32) ntohl (mp->rx_vrf_id),
100                                    (u32) ntohl (mp->server_vrf_id),
101                                    (int) (mp->is_add == 0));
102     }
103
104
105   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
106 }
107
108 static void
109 vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
110 {
111   unix_shared_memory_queue_t *q;
112
113   q = vl_api_client_index_to_input_queue (mp->client_index);
114   if (q == 0)
115     return;
116
117   dhcp_proxy_dump ((mp->is_ip6 == 0 ?
118                     FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), q, mp->context);
119 }
120
121 void
122 dhcp_send_details (fib_protocol_t proto,
123                    void *opaque,
124                    u32 context,
125                    const ip46_address_t * server,
126                    const ip46_address_t * src,
127                    u32 server_fib_id,
128                    u32 rx_fib_id, u32 vss_fib_id, u32 vss_oui)
129 {
130   vl_api_dhcp_proxy_details_t *mp;
131   unix_shared_memory_queue_t *q = opaque;
132
133   mp = vl_msg_api_alloc (sizeof (*mp));
134   if (!mp)
135     return;
136   memset (mp, 0, sizeof (*mp));
137   mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
138   mp->context = context;
139
140   mp->rx_vrf_id = htonl (rx_fib_id);
141   mp->server_vrf_id = htonl (server_fib_id);
142   mp->vss_oui = htonl (vss_oui);
143   mp->vss_fib_id = htonl (vss_fib_id);
144
145   mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
146
147   if (mp->is_ipv6)
148     {
149       memcpy (mp->dhcp_server, server, 16);
150       memcpy (mp->dhcp_src_address, src, 16);
151     }
152   else
153     {
154       /* put the address in the first bytes */
155       memcpy (mp->dhcp_server, &server->ip4, 4);
156       memcpy (mp->dhcp_src_address, &src->ip4, 4);
157     }
158   vl_msg_api_send_shmem (q, (u8 *) & mp);
159 }
160
161
162 static void
163 vl_api_dhcp_proxy_details_t_handler (vl_api_dhcp_proxy_details_t * mp)
164 {
165   clib_warning ("BUG");
166 }
167
168 void
169 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
170                            u8 is_ipv6, u8 * host_address, u8 * router_address,
171                            u8 * host_mac)
172 {
173   unix_shared_memory_queue_t *q;
174   vl_api_dhcp_compl_event_t *mp;
175
176   q = vl_api_client_index_to_input_queue (client_index);
177   if (!q)
178     return;
179
180   mp = vl_msg_api_alloc (sizeof (*mp));
181   mp->client_index = client_index;
182   mp->pid = pid;
183   mp->is_ipv6 = is_ipv6;
184   clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
185   mp->hostname[vec_len (hostname) + 1] = '\n';
186   clib_memcpy (&mp->host_address[0], host_address, 16);
187   clib_memcpy (&mp->router_address[0], router_address, 16);
188
189   if (NULL != host_mac)
190     clib_memcpy (&mp->host_mac[0], host_mac, 6);
191
192   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
193
194   vl_msg_api_send_shmem (q, (u8 *) & mp);
195 }
196
197 static void vl_api_dhcp_client_config_t_handler
198   (vl_api_dhcp_client_config_t * mp)
199 {
200   vlib_main_t *vm = vlib_get_main ();
201   vl_api_dhcp_client_config_reply_t *rmp;
202   int rv = 0;
203
204   VALIDATE_SW_IF_INDEX (mp);
205
206   rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
207                            mp->hostname, mp->is_add, mp->client_index,
208                            mp->want_dhcp_event ? dhcp_compl_event_callback :
209                            NULL, mp->pid);
210
211   BAD_SW_IF_INDEX_LABEL;
212
213   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
214 }
215
216 /*
217  * dhcp_api_hookup
218  * Add vpe's API message handlers to the table.
219  * vlib has alread mapped shared memory and
220  * added the client registration handlers.
221  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
222  */
223 #define vl_msg_name_crc_list
224 #include <vnet/vnet_all_api_h.h>
225 #undef vl_msg_name_crc_list
226
227 static void
228 setup_message_id_table (api_main_t * am)
229 {
230 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
231   foreach_vl_msg_name_crc_dhcp;
232 #undef _
233 }
234
235 static clib_error_t *
236 dhcp_api_hookup (vlib_main_t * vm)
237 {
238   api_main_t *am = &api_main;
239
240 #define _(N,n)                                                  \
241     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
242                            vl_api_##n##_t_handler,              \
243                            vl_noop_handler,                     \
244                            vl_api_##n##_t_endian,               \
245                            vl_api_##n##_t_print,                \
246                            sizeof(vl_api_##n##_t), 1);
247   foreach_vpe_api_msg;
248 #undef _
249
250   /*
251    * Set up the (msg_name, crc, message-id) table
252    */
253   setup_message_id_table (am);
254
255   return 0;
256 }
257
258 VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
259
260 /*
261  * fd.io coding-style-patch-verification: ON
262  *
263  * Local Variables:
264  * eval: (c-set-style "gnu")
265  * End:
266  */