blob: ef2bc0a1926d4c6e2ec7089c2317e361cac78f9e [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
31typedef enum {
32#define dhcp_proxy_error(n,s) DHCP_PROXY_ERROR_##n,
33#include <vnet/dhcp/dhcp4_proxy_error.def>
34#undef dhcp_proxy_error
35 DHCP_PROXY_N_ERROR,
36} dhcp_proxy_error_t;
37
38typedef enum {
39#define dhcpv6_proxy_error(n,s) DHCPV6_PROXY_ERROR_##n,
40#include <vnet/dhcp/dhcp6_proxy_error.def>
41#undef dhcpv6_proxy_error
42 DHCPV6_PROXY_N_ERROR,
43} dhcpv6_proxy_error_t;
44
45
46/**
47 * @brief The Virtual Sub-net Selection information for a given RX FIB
48 */
49typedef struct dhcp_vss_t_ {
50 /**
51 * @brief ?? RFC doesn't say
52 */
53 u32 oui;
54 /**
55 * @brief VPN-ID
56 */
57 u32 fib_id;
58} dhcp_vss_t;
59
60/**
Neale Ranns3466c302017-02-16 07:45:03 -080061 * @brief A representation of a single DHCP Server within a given VRF config
Neale Ranns2dd68522017-02-16 03:38:59 -080062 */
Neale Ranns3466c302017-02-16 07:45:03 -080063typedef struct dhcp_server_t_
64{
Neale Ranns2dd68522017-02-16 03:38:59 -080065 /**
66 * @brief The address of the DHCP server to which to relay the client's
67 * messages
68 */
69 ip46_address_t dhcp_server;
70
71 /**
Neale Ranns2dd68522017-02-16 03:38:59 -080072 * @brief The FIB index (not the external Table-ID) in which the server
73 * is reachable.
74 */
75 u32 server_fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -080076} dhcp_server_t;
77
78/**
79 * @brief A DHCP proxy represenation fpr per-client VRF config
80 */
81typedef struct dhcp_proxy_t_ {
82 /**
83 * @brief The set of DHCP servers to which messages are relayed.
84 * If multiple servers are configured then discover/solict messages
85 * are relayed to each. A cookie is maintained for the relay, and only
86 * one message is replayed to the client, based on the presence of the
87 * cookie.
88 * The expectation is there are only 1 or 2 servers, hence no fancy DB.
89 */
90 dhcp_server_t *dhcp_servers;
91
92 /**
93 * @brief Hash table of pending requets key'd on the clients MAC address
94 */
95 uword *dhcp_pending;
96
97 /**
98 * @brief A lock for the pending request DB.
99 */
100 int lock;
101
102 /**
103 * @brief The source address to use in relayed messaes
104 */
105 ip46_address_t dhcp_src_address;
Neale Ranns2dd68522017-02-16 03:38:59 -0800106
107 /**
108 * @brief The FIB index (not the external Table-ID) in which the client
109 * is resides.
110 */
111 u32 rx_fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -0800112} dhcp_proxy_t;
Neale Ranns2dd68522017-02-16 03:38:59 -0800113
114#define DHCP_N_PROTOS (FIB_PROTOCOL_IP6 + 1)
115
116/**
117 * @brief Collection of global DHCP proxy data
118 */
119typedef struct {
120 /* Pool of DHCP servers */
Neale Ranns3466c302017-02-16 07:45:03 -0800121 dhcp_proxy_t *dhcp_servers[DHCP_N_PROTOS];
Neale Ranns2dd68522017-02-16 03:38:59 -0800122
123 /* Pool of selected DHCP server. Zero is the default server */
124 u32 * dhcp_server_index_by_rx_fib_index[DHCP_N_PROTOS];
125
126 /* to drop pkts in server-to-client direction */
127 u32 error_drop_node_index;
128
129 dhcp_vss_t *vss[DHCP_N_PROTOS];
130
131 /* hash lookup specific vrf_id -> option 82 vss suboption */
132 u32 *vss_index_by_rx_fib_index[DHCP_N_PROTOS];
Neale Ranns2dd68522017-02-16 03:38:59 -0800133} dhcp_proxy_main_t;
134
135extern dhcp_proxy_main_t dhcp_proxy_main;
136
137/**
138 * @brief Send the details of a proxy session to the API client during a dump
139 */
140void dhcp_send_details (fib_protocol_t proto,
141 void *opaque,
142 u32 context,
Neale Ranns3466c302017-02-16 07:45:03 -0800143 dhcp_proxy_t *proxy);
Neale Ranns2dd68522017-02-16 03:38:59 -0800144
145/**
146 * @brief Show (on CLI) a VSS config during a show walk
147 */
148int dhcp_vss_show_walk (dhcp_vss_t *vss,
149 u32 rx_table_id,
150 void *ctx);
151
152/**
153 * @brief Configure/set a new VSS info
154 */
155int dhcp_proxy_set_vss(fib_protocol_t proto,
156 u32 vrf_id,
157 u32 oui,
158 u32 fib_id,
159 int is_del);
160
161/**
162 * @brief Dump the proxy configs to the API
163 */
164void dhcp_proxy_dump(fib_protocol_t proto,
165 void *opaque,
166 u32 context);
167
168/**
169 * @brief Add a new DHCP proxy server configuration.
170 * @return 1 is the config is new,
171 * 0 otherwise (implying a modify of an existing)
172 */
173int dhcp_proxy_server_add(fib_protocol_t proto,
174 ip46_address_t *addr,
175 ip46_address_t *src_address,
176 u32 rx_fib_iindex,
177 u32 server_table_id);
178
179/**
180 * @brief Delete a DHCP proxy config
Neale Ranns3466c302017-02-16 07:45:03 -0800181 * @return 1 if the proxy is deleted, 0 otherwise
Neale Ranns2dd68522017-02-16 03:38:59 -0800182 */
183int dhcp_proxy_server_del(fib_protocol_t proto,
Neale Ranns3466c302017-02-16 07:45:03 -0800184 u32 rx_fib_index,
185 ip46_address_t *addr,
186 u32 server_table_id);
187
188u32
189dhcp_proxy_rx_table_get_table_id (fib_protocol_t proto,
190 u32 fib_index);
Neale Ranns2dd68522017-02-16 03:38:59 -0800191
192/**
193 * @brief Callback function invoked for each DHCP proxy entry
194 * return 0 to break the walk, non-zero otherwise.
195 */
Neale Ranns3466c302017-02-16 07:45:03 -0800196typedef int (*dhcp_proxy_walk_fn_t)(dhcp_proxy_t *server,
Neale Ranns2dd68522017-02-16 03:38:59 -0800197 void *ctx);
198
199/**
200 * @brief Walk/Visit each DHCP proxy server
201 */
202void dhcp_proxy_walk(fib_protocol_t proto,
203 dhcp_proxy_walk_fn_t fn,
204 void *ctx);
205
206/**
207 * @brief Callback function invoked for each DHCP VSS entry
208 * return 0 to break the walk, non-zero otherwise.
209 */
210typedef int (*dhcp_vss_walk_fn_t)(dhcp_vss_t *server,
211 u32 rx_table_id,
212 void *ctx);
213
214/**
215 * @brief Walk/Visit each DHCP proxy VSS
216 */
217void dhcp_vss_walk(fib_protocol_t proto,
218 dhcp_vss_walk_fn_t fn,
219 void *ctx);
220
221/**
Neale Ranns3466c302017-02-16 07:45:03 -0800222 * @brief Lock a proxy object to prevent simultaneous access of its
223 * pending store
224 */
225void dhcp_proxy_lock (dhcp_proxy_t *server);
226
227/**
228 * @brief Lock a proxy object to prevent simultaneous access of its
229 * pending store
230 */
231void dhcp_proxy_unlock (dhcp_proxy_t *server);
232
233/**
Neale Ranns2dd68522017-02-16 03:38:59 -0800234 * @brief Get the VSS data for the FIB index
235 */
236static inline dhcp_vss_t *
237dhcp_get_vss_info (dhcp_proxy_main_t *dm,
238 u32 rx_fib_index,
239 fib_protocol_t proto)
240{
241 dhcp_vss_t *v = NULL;
242
243 if (vec_len(dm->vss_index_by_rx_fib_index[proto]) > rx_fib_index &&
244 dm->vss_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
245 {
246 v = pool_elt_at_index (
247 dm->vss[proto],
248 dm->vss_index_by_rx_fib_index[proto][rx_fib_index]);
249 }
250
251 return (v);
252}
253
254/**
255 * @brief Get the DHCP proxy server data for the FIB index
256 */
Neale Ranns3466c302017-02-16 07:45:03 -0800257static inline dhcp_proxy_t *
258dhcp_get_proxy (dhcp_proxy_main_t *dm,
259 u32 rx_fib_index,
260 fib_protocol_t proto)
Neale Ranns2dd68522017-02-16 03:38:59 -0800261{
Neale Ranns3466c302017-02-16 07:45:03 -0800262 dhcp_proxy_t *s = NULL;
Neale Ranns2dd68522017-02-16 03:38:59 -0800263
264 if (vec_len(dm->dhcp_server_index_by_rx_fib_index[proto]) > rx_fib_index &&
265 dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
266 {
267 s = pool_elt_at_index (
268 dm->dhcp_servers[proto],
269 dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index]);
270 }
271
272 return (s);
273}
274
275int dhcp6_proxy_set_server (ip46_address_t *addr,
276 ip46_address_t *src_addr,
277 u32 rx_table_id,
278 u32 server_table_id,
279 int is_del);
280int dhcp4_proxy_set_server (ip46_address_t *addr,
281 ip46_address_t *src_addr,
282 u32 rx_table_id,
283 u32 server_table_id,
284 int is_del);
285
286#endif /* included_dhcp_proxy_h */