Add Support of DHCP VSS Type 0 where VPN-ID is ASCII
[vpp.git] / src / vnet / dhcp / dhcp_proxy.h
1 /*
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>
29 #include <vnet/udp/udp.h>
30
31 typedef 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
38 typedef 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  */
49 typedef struct dhcp_vss_t_ {
50     /**
51      * @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
55      */
56     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
61     /**
62      * @brief Type 1 VPN-ID
63      */
64     u8 vpn_id[7];
65     /**
66      * @brief Type 0 ASCII VPN Identifier
67      */
68     u8 *vpn_ascii_id;
69 } dhcp_vss_t;
70
71 /**
72  * @brief A representation of a single DHCP Server within a given VRF config
73  */
74 typedef struct dhcp_server_t_
75 {
76     /**
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     /**
83      * @brief The FIB index (not the external Table-ID) in which the server
84      *        is reachable.
85      */
86     u32 server_fib_index;
87 } dhcp_server_t;
88
89 /**
90  * @brief A DHCP proxy represenation fpr per-client VRF config
91  */
92 typedef 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;
117
118     /**
119      * @brief The FIB index (not the external Table-ID) in which the client
120      *        is resides.
121      */
122     u32 rx_fib_index;
123 } dhcp_proxy_t;
124
125 #define DHCP_N_PROTOS (FIB_PROTOCOL_IP6 + 1)
126
127 /**
128  * @brief Collection of global DHCP proxy data
129  */
130 typedef struct {
131   /* Pool of DHCP servers */
132   dhcp_proxy_t *dhcp_servers[DHCP_N_PROTOS];
133
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];
144 } dhcp_proxy_main_t;
145
146 extern 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  */
151 void dhcp_send_details (fib_protocol_t proto,
152                         void *opaque,
153                         u32 context,
154                         dhcp_proxy_t *proxy);
155
156 /**
157  * @brief Show (on CLI) a VSS config during a show walk
158  */
159 int 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  */
166 int 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);
173
174 /**
175  * @brief Dump the proxy configs to the API
176  */
177 void 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  */
186 int 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
194  * @return 1 if the proxy is deleted, 0 otherwise
195  */
196 int dhcp_proxy_server_del(fib_protocol_t proto,
197                           u32 rx_fib_index,
198                           ip46_address_t *addr,
199                           u32 server_table_id);
200
201 u32
202 dhcp_proxy_rx_table_get_table_id (fib_protocol_t proto,
203                                   u32 fib_index);
204
205 /**
206  * @brief Callback function invoked for each DHCP proxy entry
207  *  return 0 to break the walk, non-zero otherwise.
208  */
209 typedef int (*dhcp_proxy_walk_fn_t)(dhcp_proxy_t *server,
210                                     void *ctx);
211
212 /**
213  * @brief Walk/Visit each DHCP proxy server
214  */
215 void 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  */
223 typedef 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  */
230 void dhcp_vss_walk(fib_protocol_t proto,
231                    dhcp_vss_walk_fn_t fn,
232                    void *ctx);
233
234 /**
235  * @brief Lock a proxy object to prevent simultaneous access of its
236  *  pending store
237  */
238 void 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  */
244 void dhcp_proxy_unlock (dhcp_proxy_t *server);
245
246 /**
247  * @brief Get the VSS data for the FIB index
248  */
249 static inline dhcp_vss_t *
250 dhcp_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  */
270 static inline dhcp_proxy_t *
271 dhcp_get_proxy (dhcp_proxy_main_t *dm,
272                 u32 rx_fib_index,
273                 fib_protocol_t proto)
274 {
275   dhcp_proxy_t *s = NULL;
276
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
288 int 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);
293 int 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 */