DHCPv6 - Be consistent with the use of MFIB index as the RX FIB index for DHCPv6...
[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 ?? RFC doesn't say
52      */
53     u32 oui;
54     /**
55      * @brief VPN-ID
56      */
57     u32 fib_id;
58 } dhcp_vss_t;
59
60 /**
61  * @brief A DHCP proxy server represenation
62  */
63 typedef struct dhcp_server_t_ {
64     /**
65      * @brief The address of the DHCP server to which to relay the client's
66      *        messages
67      */
68     ip46_address_t dhcp_server;
69
70     /**
71      * @brief The source address to use in relayed messaes
72      */
73     ip46_address_t dhcp_src_address;
74
75     /**
76      * @brief The FIB index (not the external Table-ID) in which the server
77      *        is reachable.
78      */
79     u32 server_fib_index;
80
81     /**
82      * @brief The FIB index (not the external Table-ID) in which the client
83      *        is resides.
84      */
85     u32 rx_fib_index;
86 } dhcp_server_t;
87
88 #define DHCP_N_PROTOS (FIB_PROTOCOL_IP6 + 1)
89
90 /**
91  * @brief Collection of global DHCP proxy data
92  */
93 typedef struct {
94   /* Pool of DHCP servers */
95   dhcp_server_t *dhcp_servers[DHCP_N_PROTOS];
96
97   /* Pool of selected DHCP server. Zero is the default server */
98   u32 * dhcp_server_index_by_rx_fib_index[DHCP_N_PROTOS];
99
100   /* to drop pkts in server-to-client direction */
101   u32 error_drop_node_index;
102
103   dhcp_vss_t *vss[DHCP_N_PROTOS];
104
105   /* hash lookup specific vrf_id -> option 82 vss suboption  */
106   u32 *vss_index_by_rx_fib_index[DHCP_N_PROTOS];
107 } dhcp_proxy_main_t;
108
109 extern dhcp_proxy_main_t dhcp_proxy_main;
110
111 /**
112  * @brief Send the details of a proxy session to the API client during a dump
113  */
114 void dhcp_send_details (fib_protocol_t proto,
115                         void *opaque,
116                         u32 context,
117                         const ip46_address_t *server,
118                         const ip46_address_t *src,
119                         u32 server_fib_id,
120                         u32 rx_fib_id,
121                         u32 vss_fib_id,
122                         u32 vss_oui);
123
124 /**
125  * @brief Show (on CLI) a VSS config during a show walk
126  */
127 int dhcp_vss_show_walk (dhcp_vss_t *vss,
128                         u32 rx_table_id,
129                         void *ctx);
130
131 /**
132  * @brief Configure/set a new VSS info
133  */
134 int dhcp_proxy_set_vss(fib_protocol_t proto,
135                        u32 vrf_id,
136                        u32 oui,
137                        u32 fib_id,
138                        int is_del);
139
140 /**
141  * @brief Dump the proxy configs to the API
142  */
143 void dhcp_proxy_dump(fib_protocol_t proto,
144                      void *opaque,
145                      u32 context);
146
147 /**
148  * @brief Add a new DHCP proxy server configuration.
149  * @return 1 is the config is new,
150  *         0 otherwise (implying a modify of an existing)
151  */
152 int dhcp_proxy_server_add(fib_protocol_t proto,
153                           ip46_address_t *addr,
154                           ip46_address_t *src_address,
155                           u32 rx_fib_iindex,
156                           u32 server_table_id);
157
158 /**
159  * @brief Delete a DHCP proxy config
160  * @return 0 is deleted, otherwise an error code
161  */
162 int dhcp_proxy_server_del(fib_protocol_t proto,
163                           u32 rx_fib_index);
164
165 /**
166  * @brief Callback function invoked for each DHCP proxy entry
167  *  return 0 to break the walk, non-zero otherwise.
168  */
169 typedef int (*dhcp_proxy_walk_fn_t)(dhcp_server_t *server,
170                                     void *ctx);
171
172 /**
173  * @brief Walk/Visit each DHCP proxy server
174  */
175 void dhcp_proxy_walk(fib_protocol_t proto,
176                      dhcp_proxy_walk_fn_t fn,
177                      void *ctx);
178
179 /**
180  * @brief Callback function invoked for each DHCP VSS entry
181  *  return 0 to break the walk, non-zero otherwise.
182  */
183 typedef int (*dhcp_vss_walk_fn_t)(dhcp_vss_t *server,
184                                   u32 rx_table_id,
185                                   void *ctx);
186
187 /**
188  * @brief Walk/Visit each DHCP proxy VSS
189  */
190 void dhcp_vss_walk(fib_protocol_t proto,
191                    dhcp_vss_walk_fn_t fn,
192                    void *ctx);
193
194 /**
195  * @brief Get the VSS data for the FIB index
196  */
197 static inline dhcp_vss_t *
198 dhcp_get_vss_info (dhcp_proxy_main_t *dm,
199                    u32 rx_fib_index,
200                    fib_protocol_t proto)
201 {
202   dhcp_vss_t *v = NULL;
203
204   if (vec_len(dm->vss_index_by_rx_fib_index[proto]) > rx_fib_index &&
205       dm->vss_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
206   {
207       v = pool_elt_at_index (
208               dm->vss[proto],
209               dm->vss_index_by_rx_fib_index[proto][rx_fib_index]);
210   }
211
212   return (v);
213 }
214
215 /**
216  * @brief Get the DHCP proxy server data for the FIB index
217  */
218 static inline dhcp_server_t *
219 dhcp_get_server (dhcp_proxy_main_t *dm,
220                  u32 rx_fib_index,
221                  fib_protocol_t proto)
222 {
223   dhcp_server_t *s = NULL;
224
225   if (vec_len(dm->dhcp_server_index_by_rx_fib_index[proto]) > rx_fib_index &&
226       dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] != ~0)
227   {
228       s = pool_elt_at_index (
229               dm->dhcp_servers[proto],
230               dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index]);
231   }
232
233   return (s);
234 }
235
236 int dhcp6_proxy_set_server (ip46_address_t *addr,
237                             ip46_address_t *src_addr,
238                             u32 rx_table_id,
239                             u32 server_table_id,
240                             int is_del);
241 int dhcp4_proxy_set_server (ip46_address_t *addr,
242                             ip46_address_t *src_addr,
243                             u32 rx_table_id,
244                             u32 server_table_id,
245                             int is_del);
246
247 #endif /* included_dhcp_proxy_h */