Neale Ranns | 2dd6852 | 2017-02-16 03:38:59 -0800 | [diff] [blame] | 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/ip/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 | |
| 108 | } dhcp_proxy_main_t; |
| 109 | |
| 110 | extern dhcp_proxy_main_t dhcp_proxy_main; |
| 111 | |
| 112 | /** |
| 113 | * @brief Send the details of a proxy session to the API client during a dump |
| 114 | */ |
| 115 | void dhcp_send_details (fib_protocol_t proto, |
| 116 | void *opaque, |
| 117 | u32 context, |
| 118 | const ip46_address_t *server, |
| 119 | const ip46_address_t *src, |
| 120 | u32 server_fib_id, |
| 121 | u32 rx_fib_id, |
| 122 | u32 vss_fib_id, |
| 123 | u32 vss_oui); |
| 124 | |
| 125 | /** |
| 126 | * @brief Show (on CLI) a VSS config during a show walk |
| 127 | */ |
| 128 | int dhcp_vss_show_walk (dhcp_vss_t *vss, |
| 129 | u32 rx_table_id, |
| 130 | void *ctx); |
| 131 | |
| 132 | /** |
| 133 | * @brief Configure/set a new VSS info |
| 134 | */ |
| 135 | int dhcp_proxy_set_vss(fib_protocol_t proto, |
| 136 | u32 vrf_id, |
| 137 | u32 oui, |
| 138 | u32 fib_id, |
| 139 | int is_del); |
| 140 | |
| 141 | /** |
| 142 | * @brief Dump the proxy configs to the API |
| 143 | */ |
| 144 | void dhcp_proxy_dump(fib_protocol_t proto, |
| 145 | void *opaque, |
| 146 | u32 context); |
| 147 | |
| 148 | /** |
| 149 | * @brief Add a new DHCP proxy server configuration. |
| 150 | * @return 1 is the config is new, |
| 151 | * 0 otherwise (implying a modify of an existing) |
| 152 | */ |
| 153 | int dhcp_proxy_server_add(fib_protocol_t proto, |
| 154 | ip46_address_t *addr, |
| 155 | ip46_address_t *src_address, |
| 156 | u32 rx_fib_iindex, |
| 157 | u32 server_table_id); |
| 158 | |
| 159 | /** |
| 160 | * @brief Delete a DHCP proxy config |
| 161 | * @return 0 is deleted, otherwise an error code |
| 162 | */ |
| 163 | int dhcp_proxy_server_del(fib_protocol_t proto, |
| 164 | u32 rx_fib_index); |
| 165 | |
| 166 | /** |
| 167 | * @brief Callback function invoked for each DHCP proxy entry |
| 168 | * return 0 to break the walk, non-zero otherwise. |
| 169 | */ |
| 170 | typedef int (*dhcp_proxy_walk_fn_t)(dhcp_server_t *server, |
| 171 | void *ctx); |
| 172 | |
| 173 | /** |
| 174 | * @brief Walk/Visit each DHCP proxy server |
| 175 | */ |
| 176 | void dhcp_proxy_walk(fib_protocol_t proto, |
| 177 | dhcp_proxy_walk_fn_t fn, |
| 178 | void *ctx); |
| 179 | |
| 180 | /** |
| 181 | * @brief Callback function invoked for each DHCP VSS entry |
| 182 | * return 0 to break the walk, non-zero otherwise. |
| 183 | */ |
| 184 | typedef int (*dhcp_vss_walk_fn_t)(dhcp_vss_t *server, |
| 185 | u32 rx_table_id, |
| 186 | void *ctx); |
| 187 | |
| 188 | /** |
| 189 | * @brief Walk/Visit each DHCP proxy VSS |
| 190 | */ |
| 191 | void dhcp_vss_walk(fib_protocol_t proto, |
| 192 | dhcp_vss_walk_fn_t fn, |
| 193 | void *ctx); |
| 194 | |
| 195 | /** |
| 196 | * @brief Get the VSS data for the FIB index |
| 197 | */ |
| 198 | static inline dhcp_vss_t * |
| 199 | dhcp_get_vss_info (dhcp_proxy_main_t *dm, |
| 200 | u32 rx_fib_index, |
| 201 | fib_protocol_t proto) |
| 202 | { |
| 203 | dhcp_vss_t *v = NULL; |
| 204 | |
| 205 | if (vec_len(dm->vss_index_by_rx_fib_index[proto]) > rx_fib_index && |
| 206 | dm->vss_index_by_rx_fib_index[proto][rx_fib_index] != ~0) |
| 207 | { |
| 208 | v = pool_elt_at_index ( |
| 209 | dm->vss[proto], |
| 210 | dm->vss_index_by_rx_fib_index[proto][rx_fib_index]); |
| 211 | } |
| 212 | |
| 213 | return (v); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @brief Get the DHCP proxy server data for the FIB index |
| 218 | */ |
| 219 | static inline dhcp_server_t * |
| 220 | dhcp_get_server (dhcp_proxy_main_t *dm, |
| 221 | u32 rx_fib_index, |
| 222 | fib_protocol_t proto) |
| 223 | { |
| 224 | dhcp_server_t *s = NULL; |
| 225 | |
| 226 | if (vec_len(dm->dhcp_server_index_by_rx_fib_index[proto]) > rx_fib_index && |
| 227 | dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] != ~0) |
| 228 | { |
| 229 | s = pool_elt_at_index ( |
| 230 | dm->dhcp_servers[proto], |
| 231 | dm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index]); |
| 232 | } |
| 233 | |
| 234 | return (s); |
| 235 | } |
| 236 | |
| 237 | int dhcp6_proxy_set_server (ip46_address_t *addr, |
| 238 | ip46_address_t *src_addr, |
| 239 | u32 rx_table_id, |
| 240 | u32 server_table_id, |
| 241 | int is_del); |
| 242 | int dhcp4_proxy_set_server (ip46_address_t *addr, |
| 243 | ip46_address_t *src_addr, |
| 244 | u32 rx_table_id, |
| 245 | u32 server_table_id, |
| 246 | int is_del); |
| 247 | |
| 248 | #endif /* included_dhcp_proxy_h */ |