FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
dhcp_api.c
Go to the documentation of this file.
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>
30 #include <vnet/fib/fib_table.h>
31 #include <vnet/ip/ip_types_api.h>
32 
33 #include <vnet/vnet_msg_enum.h>
34 
35 #define vl_typedefs /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_typedefs
38 
39 #define vl_endianfun /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
41 #undef vl_endianfun
42 
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <vnet/vnet_all_api_h.h>
47 #undef vl_printfun
48 
50 
51 #define foreach_vpe_api_msg \
52 _(DHCP_PROXY_CONFIG,dhcp_proxy_config) \
53 _(DHCP_PROXY_DUMP,dhcp_proxy_dump) \
54 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \
55 _(DHCP_CLIENT_CONFIG, dhcp_client_config) \
56 _(DHCP_CLIENT_DUMP, dhcp_client_dump) \
57 _(WANT_DHCP6_PD_REPLY_EVENTS, want_dhcp6_pd_reply_events) \
58 _(DHCP6_PD_SEND_CLIENT_MESSAGE, dhcp6_pd_send_client_message) \
59 _(WANT_DHCP6_REPLY_EVENTS, want_dhcp6_reply_events) \
60 _(DHCP6_SEND_CLIENT_MESSAGE, dhcp6_send_client_message) \
61 _(DHCP6_CLIENTS_ENABLE_DISABLE, dhcp6_clients_enable_disable) \
62 _(DHCP6_DUID_LL_SET, dhcp6_duid_ll_set)
63 
64 
65 static void
67 {
68  vl_api_dhcp_proxy_set_vss_reply_t *rmp;
69  u8 *vpn_ascii_id;
70  int rv;
71 
72  mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0;
73  vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id);
74  rv =
76  ntohl (mp->tbl_id), mp->vss_type, vpn_ascii_id,
77  ntohl (mp->oui), ntohl (mp->vpn_index),
78  mp->is_add == 0);
79 
80  REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
81 }
82 
83 
86 {
87  vl_api_dhcp_proxy_set_vss_reply_t *rmp;
88  ip46_address_t src, server;
89  int rv = -1;
90 
91  if (mp->is_ipv6)
92  {
93  clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6));
94  clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6));
95 
96  rv = dhcp6_proxy_set_server (&server,
97  &src,
98  (u32) ntohl (mp->rx_vrf_id),
99  (u32) ntohl (mp->server_vrf_id),
100  (int) (mp->is_add == 0));
101  }
102  else
103  {
104  ip46_address_reset (&src);
105  ip46_address_reset (&server);
106 
107  clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4));
108  clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4));
109 
110  rv = dhcp4_proxy_set_server (&server,
111  &src,
112  (u32) ntohl (mp->rx_vrf_id),
113  (u32) ntohl (mp->server_vrf_id),
114  (int) (mp->is_add == 0));
115  }
116 
117 
118  REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
119 }
120 
121 static void
123 {
125 
127  if (!reg)
128  return;;
129 
130  dhcp_proxy_dump ((mp->is_ip6 == 1 ?
132 }
133 
134 void
136  void *opaque, u32 context, dhcp_proxy_t * proxy)
137 {
139  vl_api_registration_t *reg = opaque;
140  vl_api_dhcp_server_t *v_server;
141  dhcp_server_t *server;
142  fib_table_t *s_fib;
143  dhcp_vss_t *vss;
144  u32 count;
145  size_t n;
146 
147  count = vec_len (proxy->dhcp_servers);
148  n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
149  mp = vl_msg_api_alloc (n);
150  if (!mp)
151  return;
152  clib_memset (mp, 0, n);
153  mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
154  mp->context = context;
155  mp->count = count;
156 
157  mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
158  mp->rx_vrf_id =
159  htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
160 
161  vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
162 
163  if (vss)
164  {
165  mp->vss_type = vss->vss_type;
166  if (vss->vss_type == VSS_TYPE_ASCII)
167  {
168  u32 id_len = vec_len (vss->vpn_ascii_id);
169  clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len);
170  }
171  else if (vss->vss_type == VSS_TYPE_VPN_ID)
172  {
173  u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
174  + ((u32) vss->vpn_id[2]);
175  u32 fib_id = ((u32) vss->vpn_id[3] << 24) +
176  ((u32) vss->vpn_id[4] << 16) + ((u32) vss->vpn_id[5] << 8) +
177  ((u32) vss->vpn_id[6]);
178  mp->vss_oui = htonl (oui);
179  mp->vss_fib_id = htonl (fib_id);
180  }
181  }
182  else
184 
185  vec_foreach_index (count, proxy->dhcp_servers)
186  {
187  server = &proxy->dhcp_servers[count];
188  v_server = &mp->servers[count];
189 
190  s_fib = fib_table_get (server->server_fib_index, proto);
191 
192  v_server->server_vrf_id = htonl (s_fib->ft_table_id);
193 
194  if (mp->is_ipv6)
195  {
196  memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16);
197  }
198  else
199  {
200  /* put the address in the first bytes */
201  memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4);
202  }
203  }
204 
205  if (mp->is_ipv6)
206  {
207  memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
208  }
209  else
210  {
211  /* put the address in the first bytes */
212  memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
213  }
214  vl_api_send_msg (reg, (u8 *) mp);
215 }
216 
217 static void
219  const dhcp_client_t * client)
220 {
221  size_t len;
222  u8 i;
223 
224  lease->is_ipv6 = 0; // only support IPv6 clients
225  lease->sw_if_index = ntohl (client->sw_if_index);
226  lease->state = client->state;
227  len = clib_min (sizeof (lease->hostname) - 1, vec_len (client->hostname));
228  clib_memcpy (&lease->hostname, client->hostname, len);
229  lease->hostname[len] = 0;
230 
231  lease->mask_width = client->installed.subnet_mask_width;
232  clib_memcpy (&lease->host_address[0],
233  (u8 *) & client->installed.leased_address,
234  sizeof (ip4_address_t));
235  clib_memcpy (&lease->router_address[0],
236  (u8 *) & client->installed.router_address,
237  sizeof (ip4_address_t));
238 
239  lease->count = vec_len (client->domain_server_address);
240  for (i = 0; i < lease->count; i++)
241  clib_memcpy (&lease->domain_server[i].address,
242  (u8 *) & client->domain_server_address[i],
243  sizeof (ip4_address_t));
244 
245  clib_memcpy (&lease->host_mac[0], client->client_hardware_address, 6);
246 }
247 
248 static void
250  const dhcp_client_t * client)
251 {
252  size_t len;
253 
254  vclient->sw_if_index = ntohl (client->sw_if_index);
255  len = clib_min (sizeof (vclient->hostname) - 1, vec_len (client->hostname));
256  clib_memcpy (&vclient->hostname, client->hostname, len);
257  vclient->hostname[len] = 0;
258 
259  len = clib_min (sizeof (vclient->id) - 1,
260  vec_len (client->client_identifier));
261  clib_memcpy (&vclient->id, client->client_identifier, len);
262  vclient->id[len] = 0;
263 
264  if (NULL != client->event_callback)
265  vclient->want_dhcp_event = 1;
266  else
267  vclient->want_dhcp_event = 0;
268  vclient->set_broadcast_flag = client->set_broadcast_flag;
269  vclient->dscp = ip_dscp_encode (client->dscp);
270  vclient->pid = client->pid;
271 }
272 
273 static void
274 dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
275 {
278 
279  reg = vl_api_client_index_to_registration (client_index);
280  if (!reg)
281  return;
282 
283  mp = vl_msg_api_alloc (sizeof (*mp));
284  mp->client_index = client_index;
285  mp->pid = client->pid;
286  dhcp_client_lease_encode (&mp->lease, client);
287 
288  mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
289 
290  vl_api_send_msg (reg, (u8 *) mp);
291 }
292 
295 {
297  vl_api_dhcp_client_config_reply_t *rmp;
299  ip_dscp_t dscp;
300  int rv = 0;
301 
302  VALIDATE_SW_IF_INDEX (&(mp->client));
303 
304  sw_if_index = ntohl (mp->client.sw_if_index);
305  dscp = ip_dscp_decode (mp->client.dscp);
306 
307  rv = dhcp_client_config (mp->is_add,
308  mp->client_index,
309  vm,
310  sw_if_index,
311  mp->client.hostname,
312  mp->client.id,
313  (mp->client.want_dhcp_event ?
315  NULL),
317  dscp, mp->client.pid);
318 
320  REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
321 }
322 
324 {
328 
329 static int
330 send_dhcp_client_entry (const dhcp_client_t * client, void *arg)
331 {
334 
335  ctx = arg;
336 
337  mp = vl_msg_api_alloc (sizeof (*mp));
338  clib_memset (mp, 0, sizeof (*mp));
339 
340  mp->_vl_msg_id = ntohs (VL_API_DHCP_CLIENT_DETAILS);
341  mp->context = ctx->context;
342 
343  dhcp_client_data_encode (&mp->client, client);
344  dhcp_client_lease_encode (&mp->lease, client);
345 
346  vl_api_send_msg (ctx->reg, (u8 *) mp);
347 
348  return (1);
349 }
350 
351 static void
353 {
355 
357  if (!reg)
358  return;
359 
361  .reg = reg,
362  .context = mp->context,
363  };
365 }
366 
367 /*
368  * dhcp_api_hookup
369  * Add vpe's API message handlers to the table.
370  * vlib has already mapped shared memory and
371  * added the client registration handlers.
372  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
373  */
374 #define vl_msg_name_crc_list
375 #include <vnet/vnet_all_api_h.h>
376 #undef vl_msg_name_crc_list
377 
378 static void
380 {
381 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
382  foreach_vl_msg_name_crc_dhcp;
383 #undef _
384 }
385 
386 static clib_error_t *
388 {
389  api_main_t *am = &api_main;
390 
391 #define _(N,n) \
392  vl_msg_api_set_handlers(VL_API_##N, #n, \
393  vl_api_##n##_t_handler, \
394  vl_noop_handler, \
395  vl_api_##n##_t_endian, \
396  vl_api_##n##_t_print, \
397  sizeof(vl_api_##n##_t), 1);
399 #undef _
400 
401  /*
402  * Set up the (msg_name, crc, message-id) table
403  */
405 
410 
411  return 0;
412 }
413 
415 
416 /*
417  * fd.io coding-style-patch-verification: ON
418  *
419  * Local Variables:
420  * eval: (c-set-style "gnu")
421  * End:
422  */
DHCP Proxy set / unset vss request.
Definition: dhcp.api:54
vl_api_dhcp_lease_t lease
Definition: dhcp.api:166
#define vec_foreach_index(var, v)
Iterate over vector indices.
static dhcp_vss_t * dhcp_get_vss_info(dhcp_proxy_main_t *dm, u32 rx_fib_index, fib_protocol_t proto)
Get the VSS data for the FIB index.
Definition: dhcp_proxy.h:255
vl_api_domain_server_t domain_server[count]
Definition: dhcp.api:131
#define clib_min(x, y)
Definition: clib.h:302
static clib_error_t * dhcp_api_hookup(vlib_main_t *vm)
Definition: dhcp_api.c:387
#define VSS_TYPE_INVALID
Definition: dhcp_proxy.h:68
ip4_address_t router_address
The address of this client&#39;s default gateway - may not be present.
Definition: client.h:60
DHCP Client details returned from dump.
Definition: dhcp.api:162
dhcp_server_t * dhcp_servers
The set of DHCP servers to which messages are relayed.
Definition: dhcp_proxy.h:111
DHCP Client config data.
Definition: dhcp.api:78
ip_dscp_t dscp
Definition: client.h:114
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
dhcp_proxy_main_t dhcp_proxy_main
Shard 4/6 instance of DHCP main.
Definition: dhcp_proxy.c:25
int dhcp6_proxy_set_server(ip46_address_t *addr, ip46_address_t *src_addr, u32 rx_table_id, u32 server_table_id, int is_del)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
ip46_address_t dhcp_src_address
The source address to use in relayed messaes.
Definition: dhcp_proxy.h:126
The Virtual Sub-net Selection information for a given RX FIB.
Definition: dhcp_proxy.h:57
vlib_node_registration_t dhcp6_reply_process_node
(constructor) VLIB_REGISTER_NODE (dhcp6_reply_process_node)
vl_api_address_t src
Definition: gre.api:51
int i
u32 sw_if_index
Definition: client.h:68
void dhcp_send_details(fib_protocol_t proto, void *opaque, u32 context, dhcp_proxy_t *proxy)
Send the details of a proxy session to the API client during a dump.
Definition: dhcp_api.c:135
u32 rx_fib_index
The FIB index (not the external Table-ID) in which the client is resides.
Definition: dhcp_proxy.h:132
void dhcp_client_walk(dhcp_client_walk_cb_t cb, void *ctx)
Walk (visit each) DHCP client configuration.
Definition: client.c:1111
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
dhcp_client_fwd_addresses_t installed
Definition: client.h:86
u8 vss_type
VSS type as defined in RFC 6607: 0 for NVT ASCII VPN Identifier 1 for RFC 2685 VPN-ID of 7 octects - ...
Definition: dhcp_proxy.h:65
static void vl_api_dhcp_client_dump_t_handler(vl_api_dhcp_client_dump_t *mp)
Definition: dhcp_api.c:352
dhcp_client_state_t state
Definition: client.h:65
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
void dhcp6_set_publisher_node(uword node_index, uword event_type)
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
Dump the DHCP client configurations.
Definition: dhcp.api:152
int dhcp_client_config(u32 is_add, u32 client_index, vlib_main_t *vm, u32 sw_if_index, u8 *hostname, u8 *client_id, dhcp_event_cb_t event_callback, u8 set_broadcast_flag, ip_dscp_t dscp, u32 pid)
Add/Delete DHCP clients.
Definition: client.c:1026
#define clib_memcpy(d, s, n)
Definition: string.h:180
void dhcp_proxy_dump(fib_protocol_t proto, void *opaque, u32 context)
Dump the proxy configs to the API.
Definition: dhcp_proxy.c:248
vl_api_dhcp_client_t client
Definition: dhcp.api:165
vl_api_dhcp_server_t servers[count]
Definition: dhcp.api:200
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
static void vl_api_dhcp_proxy_config_t_handler(vl_api_dhcp_proxy_config_t *mp)
Definition: dhcp_api.c:85
static int send_dhcp_client_entry(const dhcp_client_t *client, void *arg)
Definition: dhcp_api.c:330
dhcp_event_cb_t event_callback
Definition: client.h:116
#define foreach_vpe_api_msg
Definition: dhcp_api.c:51
void dhcp6_pd_set_publisher_node(uword node_index, uword event_type)
#define VSS_TYPE_ASCII
Definition: dhcp_proxy.h:66
unsigned int u32
Definition: types.h:88
ip_dscp_t ip_dscp_decode(u8 in)
Definition: ip_types_api.c:99
u8 set_broadcast_flag
Definition: client.h:106
u32 server_fib_index
The FIB index (not the external Table-ID) in which the server is reachable.
Definition: dhcp_proxy.h:95
struct dhcp_client_send_walk_ctx_t_ dhcp_client_send_walk_ctx_t
long ctx[MAX_CONNS]
Definition: main.c:144
ip4_address_t leased_address
the address assigned to this client and it&#39;s mask
Definition: client.h:52
Data learned by the client during the DHCP process.
Definition: dhcp.api:120
#define REPLY_MACRO(t)
static void vl_api_dhcp_proxy_dump_t_handler(vl_api_dhcp_proxy_dump_t *mp)
Definition: dhcp_api.c:122
int dhcp_proxy_set_vss(fib_protocol_t proto, u32 tbl_id, u8 vss_type, u8 *vpn_ascii_id, u32 oui, u32 vpn_index, u8 is_del)
Configure/set a new VSS info.
Definition: dhcp_proxy.c:309
static void dhcp_client_data_encode(vl_api_dhcp_client_t *vclient, const dhcp_client_t *client)
Definition: dhcp_api.c:249
u8 client_hardware_address[6]
Definition: client.h:108
u8 len
Definition: ip_types.api:90
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
#define BAD_SW_IF_INDEX_LABEL
u8 * vpn_ascii_id
Type 0 ASCII VPN Identifier.
Definition: dhcp_proxy.h:77
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:89
vlib_main_t * vm
Definition: buffer.c:323
ip4_address_t * domain_server_address
Definition: client.h:90
static void dhcp_compl_event_callback(u32 client_index, const dhcp_client_t *client)
Definition: dhcp_api.c:274
vl_api_registration_t * reg
Definition: dhcp_api.c:325
A representation of a single DHCP Server within a given VRF config.
Definition: dhcp_proxy.h:83
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
u8 router_address[16]
Definition: dhcp.api:128
vl_api_ip_dscp_t dscp
Definition: dhcp.api:85
int dhcp4_proxy_set_server(ip46_address_t *addr, ip46_address_t *src_addr, u32 rx_table_id, u32 server_table_id, int is_del)
ip46_address_t dhcp_server
The address of the DHCP server to which to relay the client&#39;s messages.
Definition: dhcp_proxy.h:89
enum ip_dscp_t_ ip_dscp_t
VLIB_API_INIT_FUNCTION(dhcp_api_hookup)
size_t count
Definition: vapi.c:47
vl_api_dhcp_lease_t lease
Definition: dhcp.api:143
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u8 dhcp_server[16]
Definition: dhcp.api:183
Tell client about a DHCP completion event.
Definition: dhcp.api:189
#define ip46_address_reset(ip46)
Definition: ip6_packet.h:91
A DHCP proxy representation fpr per-client VRF config.
Definition: dhcp_proxy.h:101
u8 * client_identifier
Definition: client.h:99
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u8 vpn_id[7]
Type 1 VPN-ID.
Definition: dhcp_proxy.h:73
vl_api_dhcp_client_t client
Definition: dhcp.api:100
static void setup_message_id_table(api_main_t *am)
Definition: dhcp_api.c:379
#define VSS_TYPE_VPN_ID
Definition: dhcp_proxy.h:67
static void vl_api_dhcp_client_config_t_handler(vl_api_dhcp_client_config_t *mp)
Definition: dhcp_api.c:294
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:27
static void vl_api_dhcp_proxy_set_vss_t_handler(vl_api_dhcp_proxy_set_vss_t *mp)
Definition: dhcp_api.c:66
Dump DHCP proxy table.
Definition: dhcp.api:173
u8 * hostname
Definition: client.h:98
vlib_node_registration_t dhcp6_pd_reply_process_node
(constructor) VLIB_REGISTER_NODE (dhcp6_pd_reply_process_node)
u8 ip_dscp_encode(ip_dscp_t dscp)
Definition: ip_types_api.c:105
u32 dhcp_proxy_rx_table_get_table_id(fib_protocol_t proto, u32 fib_index)
Definition: dhcp_proxy.c:46
static void dhcp_client_lease_encode(vl_api_dhcp_lease_t *lease, const dhcp_client_t *client)
Definition: dhcp_api.c:218
Tell client about a DHCP completion event.
Definition: dhcp.api:139
u32 context
Definition: gre.api:45
api_main_t api_main
Definition: api_shared.c:35
DHCP Client config add / del request.
Definition: dhcp.api:95
u8 host_address[16]
Definition: dhcp.api:127
vl_api_fib_path_nh_proto_t proto
Definition: fib_types.api:125
DHCP Proxy config add / del request.
Definition: dhcp.api:31
#define VALIDATE_SW_IF_INDEX(mp)
A protocol Independent FIB table.
Definition: fib_table.h:69