blob: 9b15ac82d1e961489e69454fb75fe258f0b93b4c [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 /**
John Lo70bfcaf2017-11-14 13:19:26 -050051 * @brief VSS type as defined in RFC 6607:
52 * 0 for NVT ASCII VPN Identifier
53 * 1 for RFC 2685 VPN-ID of 7 octects - 3 bytes OUI & 4 bytes VPN index
54 * 255 for global default VPN
Neale Ranns2dd68522017-02-16 03:38:59 -080055 */
John Lo70bfcaf2017-11-14 13:19:26 -050056 u8 vss_type;
57#define VSS_TYPE_ASCII 0
58#define VSS_TYPE_VPN_ID 1
59#define VSS_TYPE_INVALID 123
60#define VSS_TYPE_DEFAULT 255
Neale Ranns2dd68522017-02-16 03:38:59 -080061 /**
John Lo70bfcaf2017-11-14 13:19:26 -050062 * @brief Type 1 VPN-ID
Neale Ranns2dd68522017-02-16 03:38:59 -080063 */
John Lo70bfcaf2017-11-14 13:19:26 -050064 u8 vpn_id[7];
65 /**
66 * @brief Type 0 ASCII VPN Identifier
67 */
68 u8 *vpn_ascii_id;
Neale Ranns2dd68522017-02-16 03:38:59 -080069} dhcp_vss_t;
70
71/**
Neale Ranns3466c302017-02-16 07:45:03 -080072 * @brief A representation of a single DHCP Server within a given VRF config
Neale Ranns2dd68522017-02-16 03:38:59 -080073 */
Neale Ranns3466c302017-02-16 07:45:03 -080074typedef struct dhcp_server_t_
75{
Neale Ranns2dd68522017-02-16 03:38:59 -080076 /**
77 * @brief The address of the DHCP server to which to relay the client's
78 * messages
79 */
80 ip46_address_t dhcp_server;
81
82 /**
Neale Ranns2dd68522017-02-16 03:38:59 -080083 * @brief The FIB index (not the external Table-ID) in which the server
84 * is reachable.
85 */
86 u32 server_fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -080087} dhcp_server_t;
88
89/**
90 * @brief A DHCP proxy represenation fpr per-client VRF config
91 */
92typedef struct dhcp_proxy_t_ {
93 /**
94 * @brief The set of DHCP servers to which messages are relayed.
95 * If multiple servers are configured then discover/solict messages
96 * are relayed to each. A cookie is maintained for the relay, and only
97 * one message is replayed to the client, based on the presence of the
98 * cookie.
99 * The expectation is there are only 1 or 2 servers, hence no fancy DB.
100 */
101 dhcp_server_t *dhcp_servers;
102
103 /**
104 * @brief Hash table of pending requets key'd on the clients MAC address
105 */
106 uword *dhcp_pending;
107
108 /**
109 * @brief A lock for the pending request DB.
110 */
111 int lock;
112
113 /**
114 * @brief The source address to use in relayed messaes
115 */
116 ip46_address_t dhcp_src_address;
Neale Ranns2dd68522017-02-16 03:38:59 -0800117
118 /**
119 * @brief The FIB index (not the external Table-ID) in which the client
120 * is resides.
121 */
122 u32 rx_fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -0800123} dhcp_proxy_t;
Neale Ranns2dd68522017-02-16 03:38:59 -0800124
125#define DHCP_N_PROTOS (FIB_PROTOCOL_IP6 + 1)
126
127/**
128 * @brief Collection of global DHCP proxy data
129 */
130typedef struct {
131 /* Pool of DHCP servers */
Neale Ranns3466c302017-02-16 07:45:03 -0800132 dhcp_proxy_t *dhcp_servers[DHCP_N_PROTOS];
Neale Ranns2dd68522017-02-16 03:38:59 -0800133
134 /* Pool of selected DHCP server. Zero is the default server */
135 u32 * dhcp_server_index_by_rx_fib_index[DHCP_N_PROTOS];
136
137 /* to drop pkts in server-to-client direction */
138 u32 error_drop_node_index;
139
140 dhcp_vss_t *vss[DHCP_N_PROTOS];
141
142 /* hash lookup specific vrf_id -> option 82 vss suboption */
143 u32 *vss_index_by_rx_fib_index[DHCP_N_PROTOS];
Neale Ranns2dd68522017-02-16 03:38:59 -0800144} dhcp_proxy_main_t;
145
146extern dhcp_proxy_main_t dhcp_proxy_main;
147
148/**
149 * @brief Send the details of a proxy session to the API client during a dump
150 */
151void dhcp_send_details (fib_protocol_t proto,
152 void *opaque,
153 u32 context,
Neale Ranns3466c302017-02-16 07:45:03 -0800154 dhcp_proxy_t *proxy);
Neale Ranns2dd68522017-02-16 03:38:59 -0800155
156/**
157 * @brief Show (on CLI) a VSS config during a show walk
158 */
159int dhcp_vss_show_walk (dhcp_vss_t *vss,
160 u32 rx_table_id,
161 void *ctx);
162
163/**
164 * @brief Configure/set a new VSS info
165 */
John Lo70bfcaf2017-11-14 13:19:26 -0500166int dhcp_proxy_set_vss (fib_protocol_t proto,
167 u32 tbl_id,
168 u8 vss_type,
169 u8 *vpn_ascii_id,
170 u32 oui,
171 u32 vpn_index,
172 u8 is_del);
Neale Ranns2dd68522017-02-16 03:38:59 -0800173
174/**
175 * @brief Dump the proxy configs to the API
176 */
177void dhcp_proxy_dump(fib_protocol_t proto,
178 void *opaque,
179 u32 context);
180
181/**
182 * @brief Add a new DHCP proxy server configuration.
183 * @return 1 is the config is new,
184 * 0 otherwise (implying a modify of an existing)
185 */
186int dhcp_proxy_server_add(fib_protocol_t proto,
187 ip46_address_t *addr,
188 ip46_address_t *src_address,
189 u32 rx_fib_iindex,
190 u32 server_table_id);
191
192/**
193 * @brief Delete a DHCP proxy config
Neale Ranns3466c302017-02-16 07:45:03 -0800194 * @return 1 if the proxy is deleted, 0 otherwise
Neale Ranns2dd68522017-02-16 03:38:59 -0800195 */
196int dhcp_proxy_server_del(fib_protocol_t proto,
Neale Ranns3466c302017-02-16 07:45:03 -0800197 u32 rx_fib_index,
198 ip46_address_t *addr,
199 u32 server_table_id);
200
201u32
202dhcp_proxy_rx_table_get_table_id (fib_protocol_t proto,
203 u32 fib_index);
Neale Ranns2dd68522017-02-16 03:38:59 -0800204
205/**
206 * @brief Callback function invoked for each DHCP proxy entry
207 * return 0 to break the walk, non-zero otherwise.
208 */
Neale Ranns3466c302017-02-16 07:45:03 -0800209typedef int (*dhcp_proxy_walk_fn_t)(dhcp_proxy_t *server,
Neale Ranns2dd68522017-02-16 03:38:59 -0800210 void *ctx);
211
212/**
213 * @brief Walk/Visit each DHCP proxy server
214 */
215void dhcp_proxy_walk(fib_protocol_t proto,
216 dhcp_proxy_walk_fn_t fn,
217 void *ctx);
218
219/**
220 * @brief Callback function invoked for each DHCP VSS entry
221 * return 0 to break the walk, non-zero otherwise.
222 */
223typedef int (*dhcp_vss_walk_fn_t)(dhcp_vss_t *server,
224 u32 rx_table_id,
225 void *ctx);
226
227/**
228 * @brief Walk/Visit each DHCP proxy VSS
229 */
230void dhcp_vss_walk(fib_protocol_t proto,
231 dhcp_vss_walk_fn_t fn,
232 void *ctx);
233
234/**
Neale Ranns3466c302017-02-16 07:45:03 -0800235 * @brief Lock a proxy object to prevent simultaneous access of its
236 * pending store
237 */
238void dhcp_proxy_lock (dhcp_proxy_t *server);
239
240/**
241 * @brief Lock a proxy object to prevent simultaneous access of its
242 * pending store
243 */
244void dhcp_proxy_unlock (dhcp_proxy_t *server);
245
246/**
Neale Ranns2dd68522017-02-16 03:38:59 -0800247 * @brief Get the VSS data for the FIB index
248 */
249static inline dhcp_vss_t *
250dhcp_get_vss_info (dhcp_proxy_main_t *dm,
251 u32 rx_fib_index,
252 fib_protocol_t proto)
253{
254 dhcp_vss_t *v = NULL;
255
256 if (vec_len(dm->vss_index_by_rx_fib_index[proto]) > rx_fib_index &&
257 dm->vss_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
258 {
259 v = pool_elt_at_index (
260 dm->vss[proto],
261 dm->vss_index_by_rx_fib_index[proto][rx_fib_index]);
262 }
263
264 return (v);
265}
266
267/**
268 * @brief Get the DHCP proxy server data for the FIB index
269 */
Neale Ranns3466c302017-02-16 07:45:03 -0800270static inline dhcp_proxy_t *
271dhcp_get_proxy (dhcp_proxy_main_t *dm,
272 u32 rx_fib_index,
273 fib_protocol_t proto)
Neale Ranns2dd68522017-02-16 03:38:59 -0800274{
Neale Ranns3466c302017-02-16 07:45:03 -0800275 dhcp_proxy_t *s = NULL;
Neale Ranns2dd68522017-02-16 03:38:59 -0800276
277 if (vec_len(dm->dhcp_server_index_by_rx_fib_index[proto]) > rx_fib_index &&
278 dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
279 {
280 s = pool_elt_at_index (
281 dm->dhcp_servers[proto],
282 dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index]);
283 }
284
285 return (s);
286}
287
288int dhcp6_proxy_set_server (ip46_address_t *addr,
289 ip46_address_t *src_addr,
290 u32 rx_table_id,
291 u32 server_table_id,
292 int is_del);
293int dhcp4_proxy_set_server (ip46_address_t *addr,
294 ip46_address_t *src_addr,
295 u32 rx_table_id,
296 u32 server_table_id,
297 int is_del);
298
299#endif /* included_dhcp_proxy_h */