2 * dhcp_proxy.h: DHCP v4 & v6 proxy common functions/types
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef included_dhcp_proxy_h
19 #define included_dhcp_proxy_h
21 #include <vnet/vnet.h>
22 #include <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/ip/format.h>
28 #include <vnet/udp/udp_local.h>
32 #define dhcp_proxy_error(n,s) DHCP_PROXY_ERROR_##n,
33 #include <dhcp/dhcp4_proxy_error.def>
34 #undef dhcp_proxy_error
40 #define dhcpv6_proxy_error(n,s) DHCPV6_PROXY_ERROR_##n,
41 #include <dhcp/dhcp6_proxy_error.def>
42 #undef dhcpv6_proxy_error
44 } dhcpv6_proxy_error_t;
46 /* flags to indicate which DHCP ports should be or have been registered */
49 DHCP_PORT_REG_CLIENT = 0x1,
50 DHCP_PORT_REG_SERVER = 0x2,
51 } dhcp_port_reg_flags_t;
54 * @brief The Virtual Sub-net Selection information for a given RX FIB
56 typedef struct dhcp_vss_t_
59 * @brief VSS type as defined in RFC 6607:
60 * 0 for NVT ASCII VPN Identifier
61 * 1 for RFC 2685 VPN-ID of 7 octects - 3 bytes OUI & 4 bytes VPN index
62 * 255 for global default VPN
65 #define VSS_TYPE_ASCII 0
66 #define VSS_TYPE_VPN_ID 1
67 #define VSS_TYPE_INVALID 123
68 #define VSS_TYPE_DEFAULT 255
70 * @brief Type 1 VPN-ID
74 * @brief Type 0 ASCII VPN Identifier
80 * @brief A representation of a single DHCP Server within a given VRF config
82 typedef struct dhcp_server_t_
85 * @brief The address of the DHCP server to which to relay the client's
88 ip46_address_t dhcp_server;
91 * @brief The FIB index (not the external Table-ID) in which the server
98 * @brief A DHCP proxy representation fpr per-client VRF config
100 typedef struct dhcp_proxy_t_
103 * @brief The set of DHCP servers to which messages are relayed.
104 * If multiple servers are configured then discover/solict messages
105 * are relayed to each. A cookie is maintained for the relay, and only
106 * one message is replayed to the client, based on the presence of the
108 * The expectation is there are only 1 or 2 servers, hence no fancy DB.
110 dhcp_server_t *dhcp_servers;
113 * @brief Hash table of pending requets key'd on the clients MAC address
118 * @brief A lock for the pending request DB.
123 * @brief The source address to use in relayed messaes
125 ip46_address_t dhcp_src_address;
128 * @brief The FIB index (not the external Table-ID) in which the client
134 #define DHCP_N_PROTOS (FIB_PROTOCOL_IP6 + 1)
137 * @brief Collection of global DHCP proxy data
141 /* Pool of DHCP servers */
142 dhcp_proxy_t *dhcp_servers[DHCP_N_PROTOS];
144 /* Pool of selected DHCP server. Zero is the default server */
145 u32 *dhcp_server_index_by_rx_fib_index[DHCP_N_PROTOS];
147 /* to drop pkts in server-to-client direction */
148 u32 error_drop_node_index;
150 dhcp_vss_t *vss[DHCP_N_PROTOS];
152 /* hash lookup specific vrf_id -> option 82 vss suboption */
153 u32 *vss_index_by_rx_fib_index[DHCP_N_PROTOS];
155 /* flags to indicate which udp ports have been registered */
156 int udp_ports_registered;
159 vlib_main_t *vlib_main;
163 extern dhcp_proxy_main_t dhcp_proxy_main;
166 * @brief Register the dhcp client and/or server ports, if not already done
168 void dhcp_maybe_register_udp_ports (dhcp_port_reg_flags_t ports);
171 * @brief Send the details of a proxy session to the API client during a dump
173 void dhcp_send_details (fib_protocol_t proto,
174 void *opaque, u32 context, dhcp_proxy_t * proxy);
177 * @brief Show (on CLI) a VSS config during a show walk
179 int dhcp_vss_show_walk (dhcp_vss_t * vss, u32 rx_table_id, void *ctx);
182 * @brief Configure/set a new VSS info
184 int dhcp_proxy_set_vss (fib_protocol_t proto,
187 u8 * vpn_ascii_id, u32 oui, u32 vpn_index, u8 is_del);
190 * @brief Dump the proxy configs to the API
192 void dhcp_proxy_dump (fib_protocol_t proto, void *opaque, u32 context);
195 * @brief Add a new DHCP proxy server configuration.
196 * @return 1 is the config is new,
197 * 0 otherwise (implying a modify of an existing)
199 int dhcp_proxy_server_add (fib_protocol_t proto,
200 ip46_address_t * addr,
201 ip46_address_t * src_address,
202 u32 rx_fib_iindex, u32 server_table_id);
205 * @brief Delete a DHCP proxy config
206 * @return 1 if the proxy is deleted, 0 otherwise
208 int dhcp_proxy_server_del (fib_protocol_t proto,
210 ip46_address_t * addr, u32 server_table_id);
212 u32 dhcp_proxy_rx_table_get_table_id (fib_protocol_t proto, u32 fib_index);
215 * @brief Callback function invoked for each DHCP proxy entry
216 * return 0 to break the walk, non-zero otherwise.
218 typedef int (*dhcp_proxy_walk_fn_t) (dhcp_proxy_t * server, void *ctx);
221 * @brief Walk/Visit each DHCP proxy server
223 void dhcp_proxy_walk (fib_protocol_t proto,
224 dhcp_proxy_walk_fn_t fn, void *ctx);
227 * @brief Callback function invoked for each DHCP VSS entry
228 * return 0 to break the walk, non-zero otherwise.
230 typedef int (*dhcp_vss_walk_fn_t) (dhcp_vss_t * server,
231 u32 rx_table_id, void *ctx);
234 * @brief Walk/Visit each DHCP proxy VSS
236 void dhcp_vss_walk (fib_protocol_t proto, dhcp_vss_walk_fn_t fn, void *ctx);
239 * @brief Lock a proxy object to prevent simultaneous access of its
242 void dhcp_proxy_lock (dhcp_proxy_t * server);
245 * @brief Lock a proxy object to prevent simultaneous access of its
248 void dhcp_proxy_unlock (dhcp_proxy_t * server);
251 * @brief Get the VSS data for the FIB index
253 static inline dhcp_vss_t *
254 dhcp_get_vss_info (dhcp_proxy_main_t * dm,
255 u32 rx_fib_index, fib_protocol_t proto)
257 dhcp_vss_t *v = NULL;
259 if (vec_len (dm->vss_index_by_rx_fib_index[proto]) > rx_fib_index &&
260 dm->vss_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
262 v = pool_elt_at_index (dm->vss[proto],
263 dm->vss_index_by_rx_fib_index[proto]
271 * @brief Get the DHCP proxy server data for the FIB index
273 static inline dhcp_proxy_t *
274 dhcp_get_proxy (dhcp_proxy_main_t * dm,
275 u32 rx_fib_index, fib_protocol_t proto)
277 dhcp_proxy_t *s = NULL;
279 if (vec_len (dm->dhcp_server_index_by_rx_fib_index[proto]) > rx_fib_index &&
280 dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
282 s = pool_elt_at_index (dm->dhcp_servers[proto],
283 dm->dhcp_server_index_by_rx_fib_index[proto]
290 int dhcp6_proxy_set_server (ip46_address_t * addr,
291 ip46_address_t * src_addr,
292 u32 rx_table_id, u32 server_table_id, int is_del);
293 int dhcp4_proxy_set_server (ip46_address_t * addr,
294 ip46_address_t * src_addr,
295 u32 rx_table_id, u32 server_table_id, int is_del);
297 #endif /* included_dhcp_proxy_h */
300 * fd.io coding-style-patch-verification: ON
303 * eval: (c-set-style "gnu")