blob: f8fc2902684dfa93102c23a7a0a08e6386b8a345 [file] [log] [blame]
Neale Ranns2dd68522017-02-16 03:38:59 -08001/*
2 * dhcp_proxy.h: DHCP v4 & v6 proxy common functions/types
3 *
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef included_dhcp_proxy_h
19#define included_dhcp_proxy_h
20
21#include <vnet/vnet.h>
22#include <vnet/dhcp/dhcp4_packet.h>
23#include <vnet/ethernet/ethernet.h>
24#include <vnet/ip/ip.h>
25#include <vnet/ip/ip4.h>
26#include <vnet/ip/ip4_packet.h>
27#include <vnet/pg/pg.h>
28#include <vnet/ip/format.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050029#include <vnet/udp/udp.h>
Neale Ranns2dd68522017-02-16 03:38:59 -080030
khemendra kumar34719e32017-12-08 18:06:52 +053031typedef enum
32{
Neale Ranns2dd68522017-02-16 03:38:59 -080033#define dhcp_proxy_error(n,s) DHCP_PROXY_ERROR_##n,
34#include <vnet/dhcp/dhcp4_proxy_error.def>
35#undef dhcp_proxy_error
36 DHCP_PROXY_N_ERROR,
37} dhcp_proxy_error_t;
38
khemendra kumar34719e32017-12-08 18:06:52 +053039typedef enum
40{
Neale Ranns2dd68522017-02-16 03:38:59 -080041#define dhcpv6_proxy_error(n,s) DHCPV6_PROXY_ERROR_##n,
42#include <vnet/dhcp/dhcp6_proxy_error.def>
43#undef dhcpv6_proxy_error
44 DHCPV6_PROXY_N_ERROR,
45} dhcpv6_proxy_error_t;
46
47
48/**
49 * @brief The Virtual Sub-net Selection information for a given RX FIB
50 */
khemendra kumar34719e32017-12-08 18:06:52 +053051typedef struct dhcp_vss_t_
52{
Neale Ranns2dd68522017-02-16 03:38:59 -080053 /**
John Lo70bfcaf2017-11-14 13:19:26 -050054 * @brief VSS type as defined in RFC 6607:
55 * 0 for NVT ASCII VPN Identifier
56 * 1 for RFC 2685 VPN-ID of 7 octects - 3 bytes OUI & 4 bytes VPN index
57 * 255 for global default VPN
Neale Ranns2dd68522017-02-16 03:38:59 -080058 */
khemendra kumar34719e32017-12-08 18:06:52 +053059 u8 vss_type;
John Lo70bfcaf2017-11-14 13:19:26 -050060#define VSS_TYPE_ASCII 0
61#define VSS_TYPE_VPN_ID 1
62#define VSS_TYPE_INVALID 123
63#define VSS_TYPE_DEFAULT 255
Neale Ranns2dd68522017-02-16 03:38:59 -080064 /**
John Lo70bfcaf2017-11-14 13:19:26 -050065 * @brief Type 1 VPN-ID
Neale Ranns2dd68522017-02-16 03:38:59 -080066 */
khemendra kumar34719e32017-12-08 18:06:52 +053067 u8 vpn_id[7];
John Lo70bfcaf2017-11-14 13:19:26 -050068 /**
69 * @brief Type 0 ASCII VPN Identifier
70 */
khemendra kumar34719e32017-12-08 18:06:52 +053071 u8 *vpn_ascii_id;
Neale Ranns2dd68522017-02-16 03:38:59 -080072} dhcp_vss_t;
73
74/**
Neale Ranns3466c302017-02-16 07:45:03 -080075 * @brief A representation of a single DHCP Server within a given VRF config
Neale Ranns2dd68522017-02-16 03:38:59 -080076 */
Neale Ranns3466c302017-02-16 07:45:03 -080077typedef struct dhcp_server_t_
78{
Neale Ranns2dd68522017-02-16 03:38:59 -080079 /**
80 * @brief The address of the DHCP server to which to relay the client's
81 * messages
82 */
khemendra kumar34719e32017-12-08 18:06:52 +053083 ip46_address_t dhcp_server;
Neale Ranns2dd68522017-02-16 03:38:59 -080084
85 /**
Neale Ranns2dd68522017-02-16 03:38:59 -080086 * @brief The FIB index (not the external Table-ID) in which the server
87 * is reachable.
88 */
khemendra kumar34719e32017-12-08 18:06:52 +053089 u32 server_fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -080090} dhcp_server_t;
91
92/**
93 * @brief A DHCP proxy represenation fpr per-client VRF config
94 */
khemendra kumar34719e32017-12-08 18:06:52 +053095typedef struct dhcp_proxy_t_
96{
Neale Ranns3466c302017-02-16 07:45:03 -080097 /**
98 * @brief The set of DHCP servers to which messages are relayed.
99 * If multiple servers are configured then discover/solict messages
100 * are relayed to each. A cookie is maintained for the relay, and only
101 * one message is replayed to the client, based on the presence of the
102 * cookie.
103 * The expectation is there are only 1 or 2 servers, hence no fancy DB.
104 */
khemendra kumar34719e32017-12-08 18:06:52 +0530105 dhcp_server_t *dhcp_servers;
Neale Ranns3466c302017-02-16 07:45:03 -0800106
107 /**
108 * @brief Hash table of pending requets key'd on the clients MAC address
109 */
khemendra kumar34719e32017-12-08 18:06:52 +0530110 uword *dhcp_pending;
Neale Ranns3466c302017-02-16 07:45:03 -0800111
112 /**
113 * @brief A lock for the pending request DB.
114 */
khemendra kumar34719e32017-12-08 18:06:52 +0530115 int lock;
Neale Ranns3466c302017-02-16 07:45:03 -0800116
117 /**
118 * @brief The source address to use in relayed messaes
119 */
khemendra kumar34719e32017-12-08 18:06:52 +0530120 ip46_address_t dhcp_src_address;
Neale Ranns2dd68522017-02-16 03:38:59 -0800121
122 /**
123 * @brief The FIB index (not the external Table-ID) in which the client
124 * is resides.
125 */
khemendra kumar34719e32017-12-08 18:06:52 +0530126 u32 rx_fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -0800127} dhcp_proxy_t;
Neale Ranns2dd68522017-02-16 03:38:59 -0800128
129#define DHCP_N_PROTOS (FIB_PROTOCOL_IP6 + 1)
130
131/**
132 * @brief Collection of global DHCP proxy data
133 */
khemendra kumar34719e32017-12-08 18:06:52 +0530134typedef struct
135{
Neale Ranns2dd68522017-02-16 03:38:59 -0800136 /* Pool of DHCP servers */
Neale Ranns3466c302017-02-16 07:45:03 -0800137 dhcp_proxy_t *dhcp_servers[DHCP_N_PROTOS];
Neale Ranns2dd68522017-02-16 03:38:59 -0800138
139 /* Pool of selected DHCP server. Zero is the default server */
khemendra kumar34719e32017-12-08 18:06:52 +0530140 u32 *dhcp_server_index_by_rx_fib_index[DHCP_N_PROTOS];
Neale Ranns2dd68522017-02-16 03:38:59 -0800141
142 /* to drop pkts in server-to-client direction */
143 u32 error_drop_node_index;
144
145 dhcp_vss_t *vss[DHCP_N_PROTOS];
146
147 /* hash lookup specific vrf_id -> option 82 vss suboption */
148 u32 *vss_index_by_rx_fib_index[DHCP_N_PROTOS];
Dave Barach8a9566e2018-10-23 10:47:36 -0400149
150 /* udp ports have been registered */
151 int udp_ports_registered;
152
153 /* convenience */
154 vlib_main_t *vlib_main;
155
Neale Ranns2dd68522017-02-16 03:38:59 -0800156} dhcp_proxy_main_t;
157
158extern dhcp_proxy_main_t dhcp_proxy_main;
159
160/**
Dave Barach8a9566e2018-10-23 10:47:36 -0400161 * @brief Register the dhcp client and server ports, if not already done
162 */
163void dhcp_maybe_register_udp_ports (void);
164
165/**
Neale Ranns2dd68522017-02-16 03:38:59 -0800166 * @brief Send the details of a proxy session to the API client during a dump
167 */
168void dhcp_send_details (fib_protocol_t proto,
khemendra kumar34719e32017-12-08 18:06:52 +0530169 void *opaque, u32 context, dhcp_proxy_t * proxy);
Neale Ranns2dd68522017-02-16 03:38:59 -0800170
171/**
172 * @brief Show (on CLI) a VSS config during a show walk
173 */
khemendra kumar34719e32017-12-08 18:06:52 +0530174int dhcp_vss_show_walk (dhcp_vss_t * vss, u32 rx_table_id, void *ctx);
Neale Ranns2dd68522017-02-16 03:38:59 -0800175
176/**
177 * @brief Configure/set a new VSS info
178 */
John Lo70bfcaf2017-11-14 13:19:26 -0500179int dhcp_proxy_set_vss (fib_protocol_t proto,
khemendra kumar34719e32017-12-08 18:06:52 +0530180 u32 tbl_id,
John Lo70bfcaf2017-11-14 13:19:26 -0500181 u8 vss_type,
khemendra kumar34719e32017-12-08 18:06:52 +0530182 u8 * vpn_ascii_id, u32 oui, u32 vpn_index, u8 is_del);
Neale Ranns2dd68522017-02-16 03:38:59 -0800183
184/**
185 * @brief Dump the proxy configs to the API
186 */
khemendra kumar34719e32017-12-08 18:06:52 +0530187void dhcp_proxy_dump (fib_protocol_t proto, void *opaque, u32 context);
Neale Ranns2dd68522017-02-16 03:38:59 -0800188
189/**
190 * @brief Add a new DHCP proxy server configuration.
191 * @return 1 is the config is new,
192 * 0 otherwise (implying a modify of an existing)
193 */
khemendra kumar34719e32017-12-08 18:06:52 +0530194int dhcp_proxy_server_add (fib_protocol_t proto,
195 ip46_address_t * addr,
196 ip46_address_t * src_address,
197 u32 rx_fib_iindex, u32 server_table_id);
Neale Ranns2dd68522017-02-16 03:38:59 -0800198
199/**
200 * @brief Delete a DHCP proxy config
Neale Ranns3466c302017-02-16 07:45:03 -0800201 * @return 1 if the proxy is deleted, 0 otherwise
Neale Ranns2dd68522017-02-16 03:38:59 -0800202 */
khemendra kumar34719e32017-12-08 18:06:52 +0530203int dhcp_proxy_server_del (fib_protocol_t proto,
204 u32 rx_fib_index,
205 ip46_address_t * addr, u32 server_table_id);
Neale Ranns3466c302017-02-16 07:45:03 -0800206
khemendra kumar34719e32017-12-08 18:06:52 +0530207u32 dhcp_proxy_rx_table_get_table_id (fib_protocol_t proto, u32 fib_index);
Neale Ranns2dd68522017-02-16 03:38:59 -0800208
209/**
210 * @brief Callback function invoked for each DHCP proxy entry
211 * return 0 to break the walk, non-zero otherwise.
212 */
khemendra kumar34719e32017-12-08 18:06:52 +0530213typedef int (*dhcp_proxy_walk_fn_t) (dhcp_proxy_t * server, void *ctx);
Neale Ranns2dd68522017-02-16 03:38:59 -0800214
215/**
216 * @brief Walk/Visit each DHCP proxy server
217 */
khemendra kumar34719e32017-12-08 18:06:52 +0530218void dhcp_proxy_walk (fib_protocol_t proto,
219 dhcp_proxy_walk_fn_t fn, void *ctx);
Neale Ranns2dd68522017-02-16 03:38:59 -0800220
221/**
222 * @brief Callback function invoked for each DHCP VSS entry
223 * return 0 to break the walk, non-zero otherwise.
224 */
khemendra kumar34719e32017-12-08 18:06:52 +0530225typedef int (*dhcp_vss_walk_fn_t) (dhcp_vss_t * server,
226 u32 rx_table_id, void *ctx);
Neale Ranns2dd68522017-02-16 03:38:59 -0800227
228/**
229 * @brief Walk/Visit each DHCP proxy VSS
230 */
khemendra kumar34719e32017-12-08 18:06:52 +0530231void dhcp_vss_walk (fib_protocol_t proto, dhcp_vss_walk_fn_t fn, void *ctx);
Neale Ranns2dd68522017-02-16 03:38:59 -0800232
233/**
Neale Ranns3466c302017-02-16 07:45:03 -0800234 * @brief Lock a proxy object to prevent simultaneous access of its
235 * pending store
236 */
khemendra kumar34719e32017-12-08 18:06:52 +0530237void dhcp_proxy_lock (dhcp_proxy_t * server);
Neale Ranns3466c302017-02-16 07:45:03 -0800238
239/**
240 * @brief Lock a proxy object to prevent simultaneous access of its
241 * pending store
242 */
khemendra kumar34719e32017-12-08 18:06:52 +0530243void dhcp_proxy_unlock (dhcp_proxy_t * server);
Neale Ranns3466c302017-02-16 07:45:03 -0800244
245/**
Neale Ranns2dd68522017-02-16 03:38:59 -0800246 * @brief Get the VSS data for the FIB index
247 */
248static inline dhcp_vss_t *
khemendra kumar34719e32017-12-08 18:06:52 +0530249dhcp_get_vss_info (dhcp_proxy_main_t * dm,
250 u32 rx_fib_index, fib_protocol_t proto)
Neale Ranns2dd68522017-02-16 03:38:59 -0800251{
252 dhcp_vss_t *v = NULL;
253
khemendra kumar34719e32017-12-08 18:06:52 +0530254 if (vec_len (dm->vss_index_by_rx_fib_index[proto]) > rx_fib_index &&
Neale Ranns2dd68522017-02-16 03:38:59 -0800255 dm->vss_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
khemendra kumar34719e32017-12-08 18:06:52 +0530256 {
257 v = pool_elt_at_index (dm->vss[proto],
258 dm->vss_index_by_rx_fib_index[proto]
259 [rx_fib_index]);
260 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800261
262 return (v);
263}
264
265/**
266 * @brief Get the DHCP proxy server data for the FIB index
267 */
Neale Ranns3466c302017-02-16 07:45:03 -0800268static inline dhcp_proxy_t *
khemendra kumar34719e32017-12-08 18:06:52 +0530269dhcp_get_proxy (dhcp_proxy_main_t * dm,
270 u32 rx_fib_index, fib_protocol_t proto)
Neale Ranns2dd68522017-02-16 03:38:59 -0800271{
Neale Ranns3466c302017-02-16 07:45:03 -0800272 dhcp_proxy_t *s = NULL;
Neale Ranns2dd68522017-02-16 03:38:59 -0800273
khemendra kumar34719e32017-12-08 18:06:52 +0530274 if (vec_len (dm->dhcp_server_index_by_rx_fib_index[proto]) > rx_fib_index &&
Neale Ranns2dd68522017-02-16 03:38:59 -0800275 dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
khemendra kumar34719e32017-12-08 18:06:52 +0530276 {
277 s = pool_elt_at_index (dm->dhcp_servers[proto],
278 dm->dhcp_server_index_by_rx_fib_index[proto]
279 [rx_fib_index]);
280 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800281
282 return (s);
283}
284
khemendra kumar34719e32017-12-08 18:06:52 +0530285int dhcp6_proxy_set_server (ip46_address_t * addr,
286 ip46_address_t * src_addr,
287 u32 rx_table_id, u32 server_table_id, int is_del);
288int dhcp4_proxy_set_server (ip46_address_t * addr,
289 ip46_address_t * src_addr,
290 u32 rx_table_id, u32 server_table_id, int is_del);
Neale Ranns2dd68522017-02-16 03:38:59 -0800291
292#endif /* included_dhcp_proxy_h */
khemendra kumar34719e32017-12-08 18:06:52 +0530293
294/*
295 * fd.io coding-style-patch-verification: ON
296 *
297 * Local Variables:
298 * eval: (c-set-style "gnu")
299 * End:
300 */