blob: 2c0dd77df280e05b165c1dd0a40f6a4ee0ed0550 [file] [log] [blame]
Pavel Kotucekc8d87702017-01-25 07:25:32 +01001/*
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>
Neale Ranns2dd68522017-02-16 03:38:59 -080025#include <vnet/dhcp/dhcp_proxy.h>
Pavel Kotucekc8d87702017-01-25 07:25:32 +010026#include <vnet/dhcp/client.h>
Neale Ranns3466c302017-02-16 07:45:03 -080027#include <vnet/fib/fib_table.h>
Pavel Kotucekc8d87702017-01-25 07:25:32 +010028
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) \
Neale Ranns20a175a2017-02-14 07:28:41 -080049_(DHCP_PROXY_DUMP,dhcp_proxy_dump) \
Pavel Kotucekc8d87702017-01-25 07:25:32 +010050_(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \
51_(DHCP_CLIENT_CONFIG, dhcp_client_config)
52
Pavel Kotucekc8d87702017-01-25 07:25:32 +010053
54static void
55vl_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;
Neale Ranns2dd68522017-02-16 03:38:59 -080059
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);
Pavel Kotucekc8d87702017-01-25 07:25:32 +010066
67 REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
68}
69
70
71static void vl_api_dhcp_proxy_config_t_handler
72 (vl_api_dhcp_proxy_config_t * mp)
73{
Neale Ranns2dd68522017-02-16 03:38:59 -080074 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 }
Pavel Kotucekc8d87702017-01-25 07:25:32 +010089 else
Neale Ranns2dd68522017-02-16 03:38:59 -080090 {
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);
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100106}
107
Neale Ranns20a175a2017-02-14 07:28:41 -0800108static void
109vl_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
Neale Ranns3466c302017-02-16 07:45:03 -0800117 dhcp_proxy_dump ((mp->is_ip6 == 1 ?
Neale Ranns2dd68522017-02-16 03:38:59 -0800118 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), q, mp->context);
Neale Ranns20a175a2017-02-14 07:28:41 -0800119}
120
121void
Neale Ranns2dd68522017-02-16 03:38:59 -0800122dhcp_send_details (fib_protocol_t proto,
Neale Ranns3466c302017-02-16 07:45:03 -0800123 void *opaque, u32 context, dhcp_proxy_t * proxy)
Neale Ranns20a175a2017-02-14 07:28:41 -0800124{
125 vl_api_dhcp_proxy_details_t *mp;
126 unix_shared_memory_queue_t *q = opaque;
Neale Ranns3466c302017-02-16 07:45:03 -0800127 vl_api_dhcp_server_t *v_server;
128 dhcp_server_t *server;
129 fib_table_t *s_fib;
130 dhcp_vss_t *vss;
131 u32 count;
132 size_t n;
Neale Ranns20a175a2017-02-14 07:28:41 -0800133
Neale Ranns3466c302017-02-16 07:45:03 -0800134 count = vec_len (proxy->dhcp_servers);
135 n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
136 mp = vl_msg_api_alloc (n);
Neale Ranns20a175a2017-02-14 07:28:41 -0800137 if (!mp)
138 return;
Neale Ranns3466c302017-02-16 07:45:03 -0800139 memset (mp, 0, n);
Neale Ranns20a175a2017-02-14 07:28:41 -0800140 mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
141 mp->context = context;
Neale Ranns3466c302017-02-16 07:45:03 -0800142 mp->count = count;
Neale Ranns20a175a2017-02-14 07:28:41 -0800143
Neale Ranns2dd68522017-02-16 03:38:59 -0800144 mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
Neale Ranns3466c302017-02-16 07:45:03 -0800145 mp->rx_vrf_id =
146 htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
147
148 vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
149
150 if (NULL != vss)
151 {
152 mp->vss_oui = htonl (vss->oui);
153 mp->vss_fib_id = htonl (vss->fib_id);
154 }
155
156 vec_foreach_index (count, proxy->dhcp_servers)
157 {
158 server = &proxy->dhcp_servers[count];
159 v_server = &mp->servers[count];
160
161 s_fib = fib_table_get (server->server_fib_index, proto);
162
163 v_server->server_vrf_id = htonl (s_fib->ft_table_id);
164
165 if (mp->is_ipv6)
166 {
167 memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16);
168 }
169 else
170 {
171 /* put the address in the first bytes */
172 memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4);
173 }
174 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800175
176 if (mp->is_ipv6)
177 {
Neale Ranns3466c302017-02-16 07:45:03 -0800178 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
Neale Ranns20a175a2017-02-14 07:28:41 -0800179 }
180 else
181 {
182 /* put the address in the first bytes */
Neale Ranns3466c302017-02-16 07:45:03 -0800183 memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
Neale Ranns20a175a2017-02-14 07:28:41 -0800184 }
185 vl_msg_api_send_shmem (q, (u8 *) & mp);
186}
187
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100188void
189dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
190 u8 is_ipv6, u8 * host_address, u8 * router_address,
191 u8 * host_mac)
192{
193 unix_shared_memory_queue_t *q;
194 vl_api_dhcp_compl_event_t *mp;
Jon Loeliger69186d92017-04-27 21:20:51 -0500195 u32 len;
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100196
197 q = vl_api_client_index_to_input_queue (client_index);
198 if (!q)
199 return;
200
201 mp = vl_msg_api_alloc (sizeof (*mp));
202 mp->client_index = client_index;
203 mp->pid = pid;
204 mp->is_ipv6 = is_ipv6;
Jon Loeliger69186d92017-04-27 21:20:51 -0500205 len = (vec_len (hostname) < 63) ? vec_len (hostname) : 63;
206 clib_memcpy (&mp->hostname, hostname, len);
207 mp->hostname[len] = 0;
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100208 clib_memcpy (&mp->host_address[0], host_address, 16);
209 clib_memcpy (&mp->router_address[0], router_address, 16);
210
211 if (NULL != host_mac)
212 clib_memcpy (&mp->host_mac[0], host_mac, 6);
213
214 mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
215
216 vl_msg_api_send_shmem (q, (u8 *) & mp);
217}
218
Pavel Kotucekc8d87702017-01-25 07:25:32 +0100219static void vl_api_dhcp_client_config_t_handler
220 (vl_api_dhcp_client_config_t * mp)
221{
222 vlib_main_t *vm = vlib_get_main ();
223 vl_api_dhcp_client_config_reply_t *rmp;
224 int rv = 0;
225
226 VALIDATE_SW_IF_INDEX (mp);
227
228 rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
229 mp->hostname, mp->is_add, mp->client_index,
230 mp->want_dhcp_event ? dhcp_compl_event_callback :
231 NULL, mp->pid);
232
233 BAD_SW_IF_INDEX_LABEL;
234
235 REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
236}
237
238/*
239 * dhcp_api_hookup
240 * Add vpe's API message handlers to the table.
241 * vlib has alread mapped shared memory and
242 * added the client registration handlers.
243 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
244 */
245#define vl_msg_name_crc_list
246#include <vnet/vnet_all_api_h.h>
247#undef vl_msg_name_crc_list
248
249static void
250setup_message_id_table (api_main_t * am)
251{
252#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
253 foreach_vl_msg_name_crc_dhcp;
254#undef _
255}
256
257static clib_error_t *
258dhcp_api_hookup (vlib_main_t * vm)
259{
260 api_main_t *am = &api_main;
261
262#define _(N,n) \
263 vl_msg_api_set_handlers(VL_API_##N, #n, \
264 vl_api_##n##_t_handler, \
265 vl_noop_handler, \
266 vl_api_##n##_t_endian, \
267 vl_api_##n##_t_print, \
268 sizeof(vl_api_##n##_t), 1);
269 foreach_vpe_api_msg;
270#undef _
271
272 /*
273 * Set up the (msg_name, crc, message-id) table
274 */
275 setup_message_id_table (am);
276
277 return 0;
278}
279
280VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
281
282/*
283 * fd.io coding-style-patch-verification: ON
284 *
285 * Local Variables:
286 * eval: (c-set-style "gnu")
287 * End:
288 */