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