Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * proxy_node.c: dhcp proxy node processing |
| 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 | #include <vlib/vlib.h> |
| 19 | #include <vnet/pg/pg.h> |
| 20 | #include <vnet/dhcp/proxy.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 21 | #include <vnet/fib/ip4_fib.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
| 23 | static char * dhcp_proxy_error_strings[] = { |
| 24 | #define dhcp_proxy_error(n,s) s, |
| 25 | #include "proxy_error.def" |
| 26 | #undef dhcp_proxy_error |
| 27 | }; |
| 28 | |
| 29 | #define foreach_dhcp_proxy_to_server_input_next \ |
| 30 | _ (DROP, "error-drop") \ |
| 31 | _ (LOOKUP, "ip4-lookup") \ |
| 32 | _ (SEND_TO_CLIENT, "dhcp-proxy-to-client") |
| 33 | |
| 34 | typedef enum { |
| 35 | #define _(s,n) DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s, |
| 36 | foreach_dhcp_proxy_to_server_input_next |
| 37 | #undef _ |
| 38 | DHCP_PROXY_TO_SERVER_INPUT_N_NEXT, |
| 39 | } dhcp_proxy_to_server_input_next_t; |
| 40 | |
| 41 | typedef struct { |
| 42 | /* 0 => to server, 1 => to client */ |
| 43 | int which; |
| 44 | ip4_address_t trace_ip4_address; |
| 45 | u32 error; |
| 46 | u32 sw_if_index; |
| 47 | u32 original_sw_if_index; |
| 48 | } dhcp_proxy_trace_t; |
| 49 | |
| 50 | #define VPP_DHCP_OPTION82_SUB1_SIZE 6 |
| 51 | #define VPP_DHCP_OPTION82_SUB5_SIZE 6 |
| 52 | #define VPP_DHCP_OPTION82_VSS_SIZE 12 |
| 53 | #define VPP_DHCP_OPTION82_SIZE (VPP_DHCP_OPTION82_SUB1_SIZE + \ |
| 54 | VPP_DHCP_OPTION82_SUB5_SIZE + \ |
| 55 | VPP_DHCP_OPTION82_VSS_SIZE +3) |
| 56 | |
| 57 | vlib_node_registration_t dhcp_proxy_to_server_node; |
| 58 | vlib_node_registration_t dhcp_proxy_to_client_node; |
| 59 | |
| 60 | u8 * format_dhcp_proxy_trace (u8 * s, va_list * args) |
| 61 | { |
| 62 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 63 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 64 | dhcp_proxy_trace_t * t = va_arg (*args, dhcp_proxy_trace_t *); |
| 65 | |
| 66 | if (t->which == 0) |
| 67 | s = format (s, "DHCP proxy: sent to server %U\n", |
| 68 | format_ip4_address, &t->trace_ip4_address, t->error); |
| 69 | else |
| 70 | s = format (s, "DHCP proxy: broadcast to client from %U\n", |
| 71 | format_ip4_address, &t->trace_ip4_address); |
| 72 | |
| 73 | if (t->error != (u32)~0) |
| 74 | s = format (s, " error: %s\n", dhcp_proxy_error_strings[t->error]); |
| 75 | |
| 76 | s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n", |
| 77 | t->original_sw_if_index, t->sw_if_index); |
| 78 | |
| 79 | return s; |
| 80 | } |
| 81 | |
| 82 | u8 * format_dhcp_proxy_header_with_length (u8 * s, va_list * args) |
| 83 | { |
| 84 | dhcp_header_t * h = va_arg (*args, dhcp_header_t *); |
| 85 | u32 max_header_bytes = va_arg (*args, u32); |
| 86 | u32 header_bytes; |
| 87 | |
| 88 | header_bytes = sizeof (h[0]); |
| 89 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 90 | return format (s, "dhcp header truncated"); |
| 91 | |
| 92 | s = format (s, "DHCP Proxy"); |
| 93 | |
| 94 | return s; |
| 95 | } |
| 96 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | static uword |
| 98 | dhcp_proxy_to_server_input (vlib_main_t * vm, |
| 99 | vlib_node_runtime_t * node, |
| 100 | vlib_frame_t * from_frame) |
| 101 | { |
| 102 | u32 n_left_from, next_index, * from, * to_next; |
| 103 | dhcp_proxy_main_t * dpm = &dhcp_proxy_main; |
| 104 | from = vlib_frame_vector_args (from_frame); |
| 105 | n_left_from = from_frame->n_vectors; |
| 106 | u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0; |
| 107 | u32 pkts_no_interface_address=0; |
| 108 | u32 pkts_too_big=0; |
| 109 | ip4_main_t * im = &ip4_main; |
| 110 | |
| 111 | next_index = node->cached_next_index; |
| 112 | |
| 113 | while (n_left_from > 0) |
| 114 | { |
| 115 | u32 n_left_to_next; |
| 116 | |
| 117 | vlib_get_next_frame (vm, node, next_index, |
| 118 | to_next, n_left_to_next); |
| 119 | |
| 120 | while (n_left_from > 0 && n_left_to_next > 0) |
| 121 | { |
| 122 | u32 bi0; |
| 123 | vlib_buffer_t * b0; |
| 124 | udp_header_t * u0; |
| 125 | dhcp_header_t * h0; |
| 126 | ip4_header_t * ip0; |
| 127 | u32 next0; |
| 128 | u32 old0, new0; |
| 129 | ip_csum_t sum0; |
| 130 | u32 error0 = (u32) ~0; |
| 131 | u32 sw_if_index = 0; |
| 132 | u32 original_sw_if_index = 0; |
| 133 | u8 *end = NULL; |
| 134 | u32 fib_index, server_index; |
| 135 | dhcp_server_t * server; |
| 136 | u32 rx_sw_if_index; |
| 137 | |
| 138 | bi0 = from[0]; |
| 139 | to_next[0] = bi0; |
| 140 | from += 1; |
| 141 | to_next += 1; |
| 142 | n_left_from -= 1; |
| 143 | n_left_to_next -= 1; |
| 144 | |
| 145 | b0 = vlib_get_buffer (vm, bi0); |
| 146 | |
| 147 | h0 = vlib_buffer_get_current (b0); |
| 148 | |
| 149 | /* |
| 150 | * udp_local hands us the DHCP header, need udp hdr, |
| 151 | * ip hdr to relay to server |
| 152 | */ |
| 153 | vlib_buffer_advance (b0, -(sizeof(*u0))); |
| 154 | u0 = vlib_buffer_get_current (b0); |
| 155 | |
| 156 | /* This blows. Return traffic has src_port = 67, dst_port = 67 */ |
| 157 | if (u0->src_port == clib_net_to_host_u16(UDP_DST_PORT_dhcp_to_server)) |
| 158 | { |
| 159 | vlib_buffer_advance (b0, sizeof(*u0)); |
| 160 | next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT; |
| 161 | error0 = 0; |
| 162 | pkts_to_client++; |
| 163 | goto do_enqueue; |
| 164 | } |
| 165 | |
| 166 | rx_sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX]; |
| 167 | |
| 168 | fib_index = im->fib_index_by_sw_if_index [rx_sw_if_index]; |
| 169 | |
| 170 | if (fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index)) |
| 171 | server_index = dpm->dhcp_server_index_by_rx_fib_index[fib_index]; |
| 172 | else |
| 173 | server_index = 0; |
| 174 | |
| 175 | if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp_servers, |
| 176 | server_index))) |
| 177 | { |
| 178 | no_server: |
| 179 | error0 = DHCP_PROXY_ERROR_NO_SERVER; |
| 180 | next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 181 | pkts_no_server++; |
| 182 | goto do_trace; |
| 183 | } |
| 184 | |
| 185 | server = pool_elt_at_index (dpm->dhcp_servers, server_index); |
| 186 | if (server->valid == 0) |
| 187 | goto no_server; |
| 188 | |
| 189 | vlib_buffer_advance (b0, -(sizeof(*ip0))); |
| 190 | ip0 = vlib_buffer_get_current (b0); |
| 191 | |
| 192 | /* disable UDP checksum */ |
| 193 | u0->checksum = 0; |
| 194 | sum0 = ip0->checksum; |
| 195 | old0 = ip0->dst_address.as_u32; |
| 196 | new0 = server->dhcp_server.as_u32; |
| 197 | ip0->dst_address.as_u32 = server->dhcp_server.as_u32; |
| 198 | sum0 = ip_csum_update (sum0, old0, new0, |
| 199 | ip4_header_t /* structure */, |
| 200 | dst_address /* changed member */); |
| 201 | ip0->checksum = ip_csum_fold (sum0); |
| 202 | |
| 203 | sum0 = ip0->checksum; |
| 204 | old0 = ip0->src_address.as_u32; |
| 205 | new0 = server->dhcp_src_address.as_u32; |
| 206 | ip0->src_address.as_u32 = new0; |
| 207 | sum0 = ip_csum_update (sum0, old0, new0, |
| 208 | ip4_header_t /* structure */, |
| 209 | src_address /* changed member */); |
| 210 | ip0->checksum = ip_csum_fold (sum0); |
| 211 | |
| 212 | /* Send to DHCP server via the configured FIB */ |
| 213 | vnet_buffer(b0)->sw_if_index[VLIB_TX] = |
| 214 | server->server_fib_index; |
| 215 | |
| 216 | h0->gateway_ip_address.as_u32 = server->dhcp_src_address.as_u32; |
| 217 | pkts_to_server++; |
| 218 | |
| 219 | if (server->insert_option_82) |
| 220 | { |
| 221 | u32 fib_index, fib_id, opt82_fib_id=0, opt82_oui=0; |
| 222 | ip4_fib_t * fib; |
| 223 | dhcp_option_t *o = (dhcp_option_t *) h0->options; |
| 224 | u32 len = 0; |
| 225 | vlib_buffer_free_list_t *fl; |
| 226 | |
| 227 | fib_index = im->fib_index_by_sw_if_index |
| 228 | [vnet_buffer(b0)->sw_if_index[VLIB_RX]]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 229 | fib = ip4_fib_get (fib_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | fib_id = fib->table_id; |
| 231 | |
| 232 | end = b0->data + b0->current_data + b0->current_length; |
| 233 | /* TLVs are not performance-friendly... */ |
| 234 | while (o->option != 0xFF /* end of options */ && (u8 *)o < end) |
| 235 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 236 | |
| 237 | fl = vlib_buffer_get_free_list (vm, b0->free_list_index); |
| 238 | // start write at (option*)o, some packets have padding |
| 239 | if (((u8 *)o - (u8 *)b0->data + VPP_DHCP_OPTION82_SIZE) > fl->n_data_bytes) |
| 240 | { |
| 241 | next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 242 | pkts_too_big++; |
| 243 | goto do_trace; |
| 244 | } |
| 245 | |
| 246 | if ((o->option == 0xFF) && ((u8 *)o <= end)) |
| 247 | { |
| 248 | vnet_main_t *vnm = vnet_get_main(); |
| 249 | u16 old_l0, new_l0; |
| 250 | ip4_address_t _ia0, * ia0 = &_ia0; |
| 251 | uword *p_vss; |
| 252 | vss_info *vss; |
| 253 | vnet_sw_interface_t *swif; |
| 254 | sw_if_index = 0; |
| 255 | original_sw_if_index = 0; |
| 256 | |
| 257 | original_sw_if_index = sw_if_index = |
| 258 | vnet_buffer(b0)->sw_if_index[VLIB_RX]; |
| 259 | swif = vnet_get_sw_interface (vnm, sw_if_index); |
| 260 | if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) |
| 261 | sw_if_index = swif->unnumbered_sw_if_index; |
| 262 | |
| 263 | p_vss = hash_get (dpm->opt82vss_index_by_vrf_id, |
| 264 | fib_id); |
| 265 | if (p_vss) |
| 266 | { |
| 267 | vss = pool_elt_at_index (dpm->opt82vss, p_vss[0]); |
| 268 | opt82_oui = vss->vpn_id.oui; |
| 269 | opt82_fib_id = vss->vpn_id.fib_id; |
| 270 | } |
| 271 | /* |
| 272 | * Get the first ip4 address on the [client-side] |
| 273 | * RX interface, if not unnumbered. otherwise use |
| 274 | * the loopback interface's ip address. |
| 275 | */ |
Dave Barach | 203c632 | 2016-06-26 10:29:03 -0400 | [diff] [blame] | 276 | ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | |
| 278 | if (ia0 == 0) |
| 279 | { |
| 280 | error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS; |
| 281 | next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 282 | pkts_no_interface_address++; |
| 283 | goto do_trace; |
| 284 | } |
| 285 | |
| 286 | /* Add option 82 */ |
| 287 | o->option = 82; /* option 82 */ |
| 288 | o->length = 12; /* 12 octets to follow */ |
| 289 | o->data[0] = 1; /* suboption 1, circuit ID (=FIB id) */ |
| 290 | o->data[1] = 4; /* length of suboption */ |
| 291 | o->data[2] = (original_sw_if_index >> 24) & 0xFF; |
| 292 | o->data[3] = (original_sw_if_index >> 16) & 0xFF; |
| 293 | o->data[4] = (original_sw_if_index >> 8) & 0xFF; |
| 294 | o->data[5] = (original_sw_if_index >> 0) & 0xFF; |
| 295 | o->data[6] = 5; /* suboption 5 (client RX intfc address) */ |
| 296 | o->data[7] = 4; /* length 4 */ |
| 297 | o->data[8] = ia0->as_u8[0]; |
| 298 | o->data[9] = ia0->as_u8[1]; |
| 299 | o->data[10] = ia0->as_u8[2]; |
| 300 | o->data[11] = ia0->as_u8[3]; |
| 301 | o->data[12] = 0xFF; |
| 302 | if (opt82_oui !=0 || opt82_fib_id != 0) |
| 303 | { |
| 304 | o->data[12] = 151; /* vss suboption */ |
| 305 | if (255 == opt82_fib_id) { |
| 306 | o->data[13] = 1; /* length */ |
| 307 | o->data[14] = 255; /* vss option type */ |
| 308 | o->data[15] = 152; /* vss control suboption */ |
| 309 | o->data[16] = 0; /* length */ |
| 310 | /* and a new "end-of-options" option (0xff) */ |
| 311 | o->data[17] = 0xFF; |
| 312 | o->length += 5; |
| 313 | } else { |
| 314 | o->data[13] = 8; /* length */ |
| 315 | o->data[14] = 1; /* vss option type */ |
| 316 | o->data[15] = (opt82_oui >> 16) & 0xff; |
| 317 | o->data[16] = (opt82_oui >> 8) & 0xff; |
| 318 | o->data[17] = (opt82_oui ) & 0xff; |
| 319 | o->data[18] = (opt82_fib_id >> 24) & 0xff; |
| 320 | o->data[19] = (opt82_fib_id >> 16) & 0xff; |
| 321 | o->data[20] = (opt82_fib_id >> 8) & 0xff; |
| 322 | o->data[21] = (opt82_fib_id) & 0xff; |
| 323 | o->data[22] = 152; /* vss control suboption */ |
| 324 | o->data[23] = 0; /* length */ |
| 325 | |
| 326 | /* and a new "end-of-options" option (0xff) */ |
| 327 | o->data[24] = 0xFF; |
| 328 | o->length += 12; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | len = o->length + 3; |
| 333 | b0->current_length += len; |
| 334 | /* Fix IP header length and checksum */ |
| 335 | old_l0 = ip0->length; |
| 336 | new_l0 = clib_net_to_host_u16 (old_l0); |
| 337 | new_l0 += len; |
| 338 | new_l0 = clib_host_to_net_u16 (new_l0); |
| 339 | ip0->length = new_l0; |
| 340 | sum0 = ip0->checksum; |
| 341 | sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, |
| 342 | length /* changed member */); |
| 343 | ip0->checksum = ip_csum_fold (sum0); |
| 344 | |
| 345 | /* Fix UDP length */ |
| 346 | new_l0 = clib_net_to_host_u16 (u0->length); |
| 347 | new_l0 += len; |
| 348 | u0->length = clib_host_to_net_u16 (new_l0); |
| 349 | } else { |
| 350 | vlib_node_increment_counter |
| 351 | (vm, dhcp_proxy_to_server_node.index, |
| 352 | DHCP_PROXY_ERROR_OPTION_82_ERROR, 1); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP; |
| 357 | |
| 358 | do_trace: |
| 359 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 360 | { |
| 361 | dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node, |
| 362 | b0, sizeof (*tr)); |
| 363 | tr->which = 0; /* to server */ |
| 364 | tr->error = error0; |
| 365 | tr->original_sw_if_index = original_sw_if_index; |
| 366 | tr->sw_if_index = sw_if_index; |
| 367 | if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP) |
| 368 | tr->trace_ip4_address.as_u32 = server->dhcp_server.as_u32; |
| 369 | } |
| 370 | |
| 371 | do_enqueue: |
| 372 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 373 | to_next, n_left_to_next, |
| 374 | bi0, next0); |
| 375 | } |
| 376 | |
| 377 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 378 | } |
| 379 | |
| 380 | vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index, |
| 381 | DHCP_PROXY_ERROR_RELAY_TO_CLIENT, |
| 382 | pkts_to_client); |
| 383 | vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index, |
| 384 | DHCP_PROXY_ERROR_RELAY_TO_SERVER, |
| 385 | pkts_to_server); |
| 386 | vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index, |
| 387 | DHCP_PROXY_ERROR_NO_SERVER, |
| 388 | pkts_no_server); |
| 389 | vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index, |
| 390 | DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS, |
| 391 | pkts_no_interface_address); |
| 392 | vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index, |
| 393 | DHCP_PROXY_ERROR_PKT_TOO_BIG, |
| 394 | pkts_too_big); |
| 395 | return from_frame->n_vectors; |
| 396 | } |
| 397 | |
| 398 | VLIB_REGISTER_NODE (dhcp_proxy_to_server_node) = { |
| 399 | .function = dhcp_proxy_to_server_input, |
| 400 | .name = "dhcp-proxy-to-server", |
| 401 | /* Takes a vector of packets. */ |
| 402 | .vector_size = sizeof (u32), |
| 403 | |
| 404 | .n_errors = DHCP_PROXY_N_ERROR, |
| 405 | .error_strings = dhcp_proxy_error_strings, |
| 406 | |
| 407 | .n_next_nodes = DHCP_PROXY_TO_SERVER_INPUT_N_NEXT, |
| 408 | .next_nodes = { |
| 409 | #define _(s,n) [DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s] = n, |
| 410 | foreach_dhcp_proxy_to_server_input_next |
| 411 | #undef _ |
| 412 | }, |
| 413 | |
| 414 | .format_buffer = format_dhcp_proxy_header_with_length, |
| 415 | .format_trace = format_dhcp_proxy_trace, |
| 416 | #if 0 |
| 417 | .unformat_buffer = unformat_dhcp_proxy_header, |
| 418 | #endif |
| 419 | }; |
| 420 | |
| 421 | static uword |
| 422 | dhcp_proxy_to_client_input (vlib_main_t * vm, |
| 423 | vlib_node_runtime_t * node, |
| 424 | vlib_frame_t * from_frame) |
| 425 | { |
| 426 | u32 n_left_from, * from; |
| 427 | ethernet_main_t *em = ethernet_get_main (vm); |
| 428 | dhcp_proxy_main_t * dpm = &dhcp_proxy_main; |
| 429 | vnet_main_t * vnm = vnet_get_main(); |
| 430 | ip4_main_t * im = &ip4_main; |
| 431 | |
| 432 | from = vlib_frame_vector_args (from_frame); |
| 433 | n_left_from = from_frame->n_vectors; |
| 434 | |
| 435 | while (n_left_from > 0) |
| 436 | { |
| 437 | u32 bi0; |
| 438 | vlib_buffer_t * b0; |
| 439 | udp_header_t * u0; |
| 440 | dhcp_header_t * h0; |
| 441 | ip4_header_t * ip0 = 0; |
| 442 | ip4_address_t * ia0 = 0; |
| 443 | u32 old0, new0; |
| 444 | ip_csum_t sum0; |
| 445 | ethernet_interface_t *ei0; |
| 446 | ethernet_header_t *mac0; |
| 447 | vnet_hw_interface_t *hi0; |
| 448 | vlib_frame_t *f0; |
| 449 | u32 * to_next0; |
| 450 | u32 sw_if_index = ~0; |
| 451 | vnet_sw_interface_t *si0; |
| 452 | u32 error0 = (u32)~0; |
| 453 | vnet_sw_interface_t *swif; |
| 454 | u32 server_index; |
| 455 | u32 fib_index; |
| 456 | dhcp_server_t * server; |
| 457 | u32 original_sw_if_index = (u32) ~0; |
| 458 | |
| 459 | bi0 = from[0]; |
| 460 | from += 1; |
| 461 | n_left_from -= 1; |
| 462 | |
| 463 | b0 = vlib_get_buffer (vm, bi0); |
| 464 | h0 = vlib_buffer_get_current (b0); |
| 465 | |
| 466 | /* |
| 467 | * udp_local hands us the DHCP header, need udp hdr, |
| 468 | * ip hdr to relay to client |
| 469 | */ |
| 470 | vlib_buffer_advance (b0, -(sizeof(*u0))); |
| 471 | u0 = vlib_buffer_get_current (b0); |
| 472 | |
| 473 | vlib_buffer_advance (b0, -(sizeof(*ip0))); |
| 474 | ip0 = vlib_buffer_get_current (b0); |
| 475 | |
| 476 | /* Consumed by dhcp client code? */ |
| 477 | if (dhcp_client_for_us (bi0, b0, ip0, u0, h0)) |
| 478 | continue; |
| 479 | |
| 480 | if (1 /* dpm->insert_option_82 */) |
| 481 | { |
| 482 | dhcp_option_t *o = (dhcp_option_t *) h0->options; |
| 483 | dhcp_option_t *sub; |
| 484 | |
| 485 | /* Parse through TLVs looking for option 82. |
| 486 | The circuit-ID is the FIB number we need |
| 487 | to track down the client-facing interface */ |
| 488 | |
| 489 | while (o->option != 0xFF /* end of options */ && |
| 490 | (u8 *) o < (b0->data + b0->current_data + b0->current_length)) |
| 491 | { |
| 492 | if (o->option == 82) |
| 493 | { |
| 494 | u32 vss_exist = 0; |
| 495 | u32 vss_ctrl = 0; |
| 496 | sub = (dhcp_option_t *) &o->data[0]; |
| 497 | while (sub->option != 0xFF /* end of options */ && |
| 498 | (u8 *) sub < (u8 *)(o + o->length)) { |
| 499 | /* If this is one of ours, it will have |
| 500 | total length 12, circuit-id suboption type, |
| 501 | and the sw_if_index */ |
| 502 | if (sub->option == 1 && sub->length == 4) |
| 503 | { |
| 504 | sw_if_index = (o->data[2] << 24) |
| 505 | | (o->data[3] << 16) |
| 506 | | (o->data[4] << 8) |
| 507 | | (o->data[5]); |
| 508 | } else if (sub->option == 151 && |
| 509 | sub->length == 7 && |
| 510 | sub->data[0] == 1) |
| 511 | vss_exist = 1; |
| 512 | else if (sub->option == 152 && sub->length == 0) |
| 513 | vss_ctrl = 1; |
| 514 | sub = (dhcp_option_t *) |
| 515 | (((uword) sub) + (sub->length + 2)); |
| 516 | } |
| 517 | if (vss_ctrl && vss_exist) |
| 518 | vlib_node_increment_counter |
| 519 | (vm, dhcp_proxy_to_client_node.index, |
| 520 | DHCP_PROXY_ERROR_OPTION_82_VSS_NOT_PROCESSED, 1); |
| 521 | |
| 522 | } |
| 523 | o = (dhcp_option_t *) (((uword) o) + (o->length + 2)); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (sw_if_index == (u32)~0) |
| 528 | { |
| 529 | error0 = DHCP_PROXY_ERROR_NO_OPTION_82; |
| 530 | |
| 531 | drop_packet: |
| 532 | vlib_node_increment_counter (vm, dhcp_proxy_to_client_node.index, |
| 533 | error0, 1); |
| 534 | f0 = vlib_get_frame_to_node (vm, dpm->error_drop_node_index); |
| 535 | to_next0 = vlib_frame_vector_args (f0); |
| 536 | to_next0[0] = bi0; |
| 537 | f0->n_vectors = 1; |
| 538 | vlib_put_frame_to_node (vm, dpm->error_drop_node_index, f0); |
| 539 | goto do_trace; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index)) |
| 544 | { |
| 545 | error0 = DHCP_PROXY_ERROR_BAD_OPTION_82; |
| 546 | goto drop_packet; |
| 547 | } |
| 548 | |
| 549 | fib_index = im->fib_index_by_sw_if_index [sw_if_index]; |
| 550 | |
| 551 | if (fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index)) |
| 552 | server_index = dpm->dhcp_server_index_by_rx_fib_index[fib_index]; |
| 553 | else |
| 554 | server_index = 0; |
| 555 | |
| 556 | if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp_servers, |
| 557 | server_index))) |
| 558 | { |
| 559 | error0 = DHCP_PROXY_ERROR_BAD_OPTION_82; |
| 560 | goto drop_packet; |
| 561 | } |
| 562 | |
| 563 | server = pool_elt_at_index (dpm->dhcp_servers, server_index); |
| 564 | if (server->valid == 0) |
| 565 | { |
| 566 | error0 = DHCP_PROXY_ERROR_NO_SERVER; |
| 567 | goto drop_packet; |
| 568 | } |
| 569 | |
| 570 | if (ip0->src_address.as_u32 != server->dhcp_server.as_u32) |
| 571 | { |
| 572 | error0 = DHCP_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS; |
| 573 | goto drop_packet; |
| 574 | } |
| 575 | |
| 576 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index; |
| 577 | |
| 578 | swif = vnet_get_sw_interface (vnm, sw_if_index); |
| 579 | original_sw_if_index = sw_if_index; |
| 580 | if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) |
| 581 | sw_if_index = swif->unnumbered_sw_if_index; |
| 582 | |
Dave Barach | 203c632 | 2016-06-26 10:29:03 -0400 | [diff] [blame] | 583 | ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 584 | if (ia0 == 0) |
| 585 | { |
| 586 | error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS; |
| 587 | goto drop_packet; |
| 588 | } |
| 589 | |
| 590 | u0->checksum = 0; |
| 591 | u0->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client); |
| 592 | sum0 = ip0->checksum; |
| 593 | old0 = ip0->dst_address.as_u32; |
| 594 | new0 = 0xFFFFFFFF; |
| 595 | ip0->dst_address.as_u32 = new0; |
| 596 | sum0 = ip_csum_update (sum0, old0, new0, |
| 597 | ip4_header_t /* structure */, |
| 598 | dst_address /* offset of changed member */); |
| 599 | ip0->checksum = ip_csum_fold (sum0); |
| 600 | |
| 601 | sum0 = ip0->checksum; |
| 602 | old0 = ip0->src_address.as_u32; |
| 603 | new0 = ia0->as_u32; |
| 604 | ip0->src_address.as_u32 = new0; |
| 605 | sum0 = ip_csum_update (sum0, old0, new0, |
| 606 | ip4_header_t /* structure */, |
| 607 | src_address /* offset of changed member */); |
| 608 | ip0->checksum = ip_csum_fold (sum0); |
| 609 | |
| 610 | vlib_buffer_advance (b0, -(sizeof(ethernet_header_t))); |
| 611 | si0 = vnet_get_sw_interface (vnm, original_sw_if_index); |
| 612 | if (si0->type == VNET_SW_INTERFACE_TYPE_SUB) |
| 613 | vlib_buffer_advance (b0, -4 /* space for VLAN tag */); |
| 614 | |
| 615 | mac0 = vlib_buffer_get_current (b0); |
| 616 | |
| 617 | hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index); |
| 618 | ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 619 | clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 620 | memset (mac0->dst_address, 0xff, sizeof (mac0->dst_address)); |
| 621 | mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ? |
| 622 | clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x0800); |
| 623 | |
| 624 | if (si0->type == VNET_SW_INTERFACE_TYPE_SUB) |
| 625 | { |
| 626 | u32 * vlan_tag = (u32 *)(mac0+1); |
| 627 | u32 tmp; |
| 628 | tmp = (si0->sub.id << 16) | 0x0800; |
| 629 | *vlan_tag = clib_host_to_net_u32 (tmp); |
| 630 | } |
| 631 | |
| 632 | /* $$$ This needs to be rewritten, for sure */ |
| 633 | f0 = vlib_get_frame_to_node (vm, hi0->output_node_index); |
| 634 | to_next0 = vlib_frame_vector_args (f0); |
| 635 | to_next0[0] = bi0; |
| 636 | f0->n_vectors = 1; |
| 637 | vlib_put_frame_to_node (vm, hi0->output_node_index, f0); |
| 638 | |
| 639 | do_trace: |
| 640 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 641 | { |
| 642 | dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node, |
| 643 | b0, sizeof (*tr)); |
| 644 | tr->which = 1; /* to client */ |
| 645 | tr->trace_ip4_address.as_u32 = ia0 ? ia0->as_u32 : 0; |
| 646 | tr->error = error0; |
| 647 | tr->original_sw_if_index = original_sw_if_index; |
| 648 | tr->sw_if_index = sw_if_index; |
| 649 | } |
| 650 | } |
| 651 | return from_frame->n_vectors; |
| 652 | } |
| 653 | |
| 654 | VLIB_REGISTER_NODE (dhcp_proxy_to_client_node) = { |
| 655 | .function = dhcp_proxy_to_client_input, |
| 656 | .name = "dhcp-proxy-to-client", |
| 657 | /* Takes a vector of packets. */ |
| 658 | .vector_size = sizeof (u32), |
| 659 | |
| 660 | .n_errors = DHCP_PROXY_N_ERROR, |
| 661 | .error_strings = dhcp_proxy_error_strings, |
| 662 | .format_buffer = format_dhcp_proxy_header_with_length, |
| 663 | .format_trace = format_dhcp_proxy_trace, |
| 664 | #if 0 |
| 665 | .unformat_buffer = unformat_dhcp_proxy_header, |
| 666 | #endif |
| 667 | }; |
| 668 | |
| 669 | clib_error_t * dhcp_proxy_init (vlib_main_t * vm) |
| 670 | { |
| 671 | dhcp_proxy_main_t * dm = &dhcp_proxy_main; |
| 672 | vlib_node_t * error_drop_node; |
| 673 | dhcp_server_t * server; |
| 674 | |
| 675 | dm->vlib_main = vm; |
| 676 | dm->vnet_main = vnet_get_main(); |
| 677 | error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); |
| 678 | dm->error_drop_node_index = error_drop_node->index; |
| 679 | |
| 680 | dm->opt82vss_index_by_vrf_id = hash_create (0, sizeof (uword)); |
| 681 | |
| 682 | udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client, |
| 683 | dhcp_proxy_to_client_node.index, 1 /* is_ip4 */); |
| 684 | |
| 685 | udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server, |
| 686 | dhcp_proxy_to_server_node.index, 1 /* is_ip4 */); |
| 687 | |
| 688 | /* Create the default server, don't mark it valid */ |
| 689 | pool_get (dm->dhcp_servers, server); |
| 690 | memset (server, 0, sizeof (*server)); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | VLIB_INIT_FUNCTION (dhcp_proxy_init); |
| 696 | |
| 697 | int dhcp_proxy_set_server_2 (ip4_address_t *addr, ip4_address_t *src_address, |
| 698 | u32 rx_fib_id, |
| 699 | u32 server_fib_id, |
| 700 | int insert_option_82, int is_del) |
| 701 | { |
| 702 | dhcp_proxy_main_t * dpm = &dhcp_proxy_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 703 | dhcp_server_t * server = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | u32 server_index = 0; |
| 705 | u32 rx_fib_index = 0; |
| 706 | |
| 707 | if (addr->as_u32 == 0) |
| 708 | return VNET_API_ERROR_INVALID_DST_ADDRESS; |
| 709 | |
| 710 | if (src_address->as_u32 == 0) |
| 711 | return VNET_API_ERROR_INVALID_SRC_ADDRESS; |
| 712 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 713 | rx_fib_index = fib_table_find_or_create_and_lock(FIB_PROTOCOL_IP4, |
| 714 | rx_fib_id); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 715 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 716 | if (rx_fib_id == 0) |
| 717 | { |
| 718 | server = pool_elt_at_index (dpm->dhcp_servers, 0); |
| 719 | |
| 720 | if (is_del) |
| 721 | { |
| 722 | memset (server, 0, sizeof (*server)); |
| 723 | return 0; |
| 724 | } |
| 725 | goto initialize_it; |
| 726 | } |
| 727 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 728 | if (is_del) |
| 729 | { |
| 730 | if (rx_fib_index >= vec_len(dpm->dhcp_server_index_by_rx_fib_index)) |
| 731 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 732 | |
| 733 | server_index = dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index]; |
| 734 | ASSERT(server_index > 0); |
| 735 | |
| 736 | /* Use the default server again. */ |
| 737 | dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index] = 0; |
| 738 | server = pool_elt_at_index (dpm->dhcp_servers, server_index); |
| 739 | memset (server, 0, sizeof (*server)); |
| 740 | pool_put (dpm->dhcp_servers, server); |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | if (rx_fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index)) |
| 745 | { |
| 746 | server_index = dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index]; |
| 747 | if (server_index != 0) |
| 748 | { |
| 749 | server = pool_elt_at_index (dpm->dhcp_servers, server_index); |
| 750 | goto initialize_it; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | pool_get (dpm->dhcp_servers, server); |
| 755 | |
| 756 | initialize_it: |
| 757 | |
| 758 | server->dhcp_server.as_u32 = addr->as_u32; |
Igor Mikhailov (imichail) | 2138b35 | 2016-11-09 12:38:33 -0800 | [diff] [blame] | 759 | server->server_fib_index = |
| 760 | fib_table_find_or_create_and_lock(FIB_PROTOCOL_IP4, |
| 761 | server_fib_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | server->dhcp_src_address.as_u32 = src_address->as_u32; |
| 763 | server->insert_option_82 = insert_option_82; |
| 764 | server->valid = 1; |
| 765 | if (rx_fib_index) |
| 766 | { |
| 767 | vec_validate (dpm->dhcp_server_index_by_rx_fib_index, rx_fib_index); |
| 768 | dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index] = |
| 769 | server - dpm->dhcp_servers; |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | /* Old API, manipulates the default server (only) */ |
| 776 | int dhcp_proxy_set_server (ip4_address_t *addr, ip4_address_t *src_address, |
| 777 | u32 fib_id, int insert_option_82, int is_del) |
| 778 | { |
| 779 | return dhcp_proxy_set_server_2 (addr, src_address, 0 /* rx_fib_id */, |
| 780 | fib_id /* server_fib_id */, |
| 781 | insert_option_82, is_del); |
| 782 | } |
| 783 | |
| 784 | |
| 785 | static clib_error_t * |
| 786 | dhcp_proxy_set_command_fn (vlib_main_t * vm, |
| 787 | unformat_input_t * input, |
| 788 | vlib_cli_command_t * cmd) |
| 789 | { |
| 790 | ip4_address_t server_addr, src_addr; |
| 791 | u32 server_fib_id = 0, rx_fib_id = 0; |
| 792 | int is_del = 0; |
| 793 | int add_option_82 = 0; |
| 794 | int set_src = 0, set_server = 0; |
| 795 | |
| 796 | while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) |
| 797 | { |
| 798 | if (unformat (input, "server %U", |
| 799 | unformat_ip4_address, &server_addr)) |
| 800 | set_server = 1; |
| 801 | else if (unformat (input, "server-fib-id %d", &server_fib_id)) |
| 802 | ; |
| 803 | else if (unformat (input, "rx-fib-id %d", &rx_fib_id)) |
| 804 | ; |
| 805 | else if (unformat(input, "src-address %U", |
| 806 | unformat_ip4_address, &src_addr)) |
| 807 | set_src = 1; |
| 808 | else if (unformat (input, "add-option-82") |
| 809 | || unformat (input, "insert-option-82")) |
| 810 | add_option_82 = 1; |
| 811 | else if (unformat (input, "delete") || |
| 812 | unformat (input, "del")) |
| 813 | is_del = 1; |
| 814 | else |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | if (is_del || (set_server && set_src)) |
| 819 | { |
| 820 | int rv; |
| 821 | |
| 822 | rv = dhcp_proxy_set_server_2 (&server_addr, &src_addr, rx_fib_id, |
| 823 | server_fib_id, add_option_82, is_del); |
| 824 | switch (rv) |
| 825 | { |
| 826 | case 0: |
| 827 | return 0; |
| 828 | |
| 829 | case VNET_API_ERROR_INVALID_DST_ADDRESS: |
| 830 | return clib_error_return (0, "Invalid server address"); |
| 831 | |
| 832 | case VNET_API_ERROR_INVALID_SRC_ADDRESS: |
| 833 | return clib_error_return (0, "Invalid src address"); |
| 834 | |
| 835 | case VNET_API_ERROR_NO_SUCH_INNER_FIB: |
| 836 | return clib_error_return (0, "No such rx fib id %d", rx_fib_id); |
| 837 | |
| 838 | case VNET_API_ERROR_NO_SUCH_FIB: |
| 839 | return clib_error_return (0, "No such server fib id %d", |
| 840 | server_fib_id); |
| 841 | |
| 842 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 843 | return clib_error_return |
| 844 | (0, "Fib id %d: no per-fib DHCP server configured", rx_fib_id); |
| 845 | |
| 846 | default: |
| 847 | return clib_error_return (0, "BUG: rv %d", rv); |
| 848 | } |
| 849 | } |
| 850 | else |
| 851 | return clib_error_return (0, "parse error`%U'", |
| 852 | format_unformat_error, input); |
| 853 | } |
| 854 | |
| 855 | VLIB_CLI_COMMAND (dhcp_proxy_set_command, static) = { |
| 856 | .path = "set dhcp proxy", |
| 857 | .short_help = "set dhcp proxy [del] server <ip-addr> src-address <ip-addr> [add-option-82] [server-fib-id <n>] [rx-fib-id <n>]", |
| 858 | .function = dhcp_proxy_set_command_fn, |
| 859 | }; |
| 860 | |
| 861 | u8 * format_dhcp_proxy_server (u8 * s, va_list * args) |
| 862 | { |
| 863 | dhcp_proxy_main_t * dm = va_arg (*args, dhcp_proxy_main_t *); |
| 864 | dhcp_server_t * server = va_arg (*args, dhcp_server_t *); |
| 865 | u32 rx_fib_index = va_arg (*args, u32); |
| 866 | ip4_fib_t * rx_fib, * server_fib; |
| 867 | u32 server_fib_id = ~0, rx_fib_id = ~0; |
| 868 | |
| 869 | if (dm == 0) |
| 870 | { |
| 871 | s = format (s, "%=16s%=16s%=14s%=14s%=20s", "Server", "Src Address", |
| 872 | "Server FIB", "RX FIB", "Insert Option 82"); |
| 873 | return s; |
| 874 | } |
| 875 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 876 | server_fib = ip4_fib_get(server->server_fib_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 877 | |
| 878 | if (server_fib) |
| 879 | server_fib_id = server_fib->table_id; |
| 880 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 881 | rx_fib = ip4_fib_get(rx_fib_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 882 | |
| 883 | if (rx_fib) |
| 884 | rx_fib_id = rx_fib->table_id; |
| 885 | |
| 886 | s = format (s, "%=16U%=16U%=14u%=14u%=20s", |
| 887 | format_ip4_address, &server->dhcp_server, |
| 888 | format_ip4_address, &server->dhcp_src_address, |
| 889 | server_fib_id, rx_fib_id, |
| 890 | server->insert_option_82 ? "yes" : "no"); |
| 891 | return s; |
| 892 | } |
| 893 | |
| 894 | static clib_error_t * |
| 895 | dhcp_proxy_show_command_fn (vlib_main_t * vm, |
| 896 | unformat_input_t * input, |
| 897 | vlib_cli_command_t * cmd) |
| 898 | { |
| 899 | dhcp_proxy_main_t * dpm = &dhcp_proxy_main; |
| 900 | ip4_main_t * im = &ip4_main; |
| 901 | dhcp_server_t * server; |
| 902 | u32 server_index; |
| 903 | int i; |
| 904 | |
| 905 | vlib_cli_output (vm, "%U", format_dhcp_proxy_server, 0 /* header line */, |
| 906 | 0, 0); |
| 907 | |
| 908 | for (i = 0; i < vec_len (im->fibs); i++) |
| 909 | { |
| 910 | if (i < vec_len(dpm->dhcp_server_index_by_rx_fib_index)) |
| 911 | server_index = dpm->dhcp_server_index_by_rx_fib_index[i]; |
| 912 | else |
| 913 | server_index = 0; |
| 914 | server = pool_elt_at_index (dpm->dhcp_servers, server_index); |
| 915 | if (server->valid) |
| 916 | vlib_cli_output (vm, "%U", format_dhcp_proxy_server, dpm, |
| 917 | server, i); |
| 918 | } |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | VLIB_CLI_COMMAND (dhcp_proxy_show_command, static) = { |
| 924 | .path = "show dhcp proxy", |
| 925 | .short_help = "Display dhcp proxy server info", |
| 926 | .function = dhcp_proxy_show_command_fn, |
| 927 | }; |
| 928 | |
| 929 | |
| 930 | int dhcp_proxy_set_option82_vss( u32 vrf_id, |
| 931 | u32 oui, |
| 932 | u32 fib_id, |
| 933 | int is_del) |
| 934 | { |
| 935 | dhcp_proxy_main_t *dm = &dhcp_proxy_main; |
| 936 | uword *p; |
| 937 | vss_info *a; |
| 938 | u32 old_oui=0, old_fib_id=0; |
| 939 | |
| 940 | p = hash_get (dm->opt82vss_index_by_vrf_id, vrf_id); |
| 941 | |
| 942 | if (p) |
| 943 | { |
| 944 | a = pool_elt_at_index (dm->opt82vss, p[0]); |
| 945 | if (!a) |
| 946 | return VNET_API_ERROR_NO_SUCH_FIB; |
| 947 | old_oui = a->vpn_id.oui; |
| 948 | old_fib_id = a->vpn_id.fib_id; |
| 949 | |
| 950 | if (is_del) |
| 951 | { |
| 952 | if (old_oui == oui && |
| 953 | old_fib_id == fib_id) |
| 954 | { |
| 955 | pool_put(dm->opt82vss, a); |
| 956 | hash_unset (dm->opt82vss_index_by_vrf_id, vrf_id); |
| 957 | return 0; |
| 958 | } |
| 959 | else |
| 960 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 961 | } |
| 962 | pool_put(dm->opt82vss, a); |
| 963 | hash_unset (dm->opt82vss_index_by_vrf_id, vrf_id); |
| 964 | } else if (is_del) |
| 965 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 966 | pool_get (dm->opt82vss, a); |
| 967 | memset (a, ~0, sizeof (a[0])); |
| 968 | a->vpn_id.oui = oui; |
| 969 | a->vpn_id.fib_id = fib_id; |
| 970 | hash_set (dm->opt82vss_index_by_vrf_id, vrf_id, a - dm->opt82vss); |
| 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | static clib_error_t * |
| 976 | dhcp_option_82_vss_fn (vlib_main_t * vm, |
| 977 | unformat_input_t * input, |
| 978 | vlib_cli_command_t * cmd) |
| 979 | { |
| 980 | int is_del = 0, got_new_vpn_id=0; |
| 981 | u32 oui=0, fib_id=0, tbl_id=~0; |
| 982 | |
| 983 | |
| 984 | while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) |
| 985 | { |
| 986 | |
| 987 | if (unformat(input, "delete") || unformat(input, "del")) |
| 988 | is_del = 1; |
| 989 | else if (unformat (input, "oui %d", &oui)) |
| 990 | got_new_vpn_id = 1; |
| 991 | else if (unformat (input, "vpn-id %d", &fib_id)) |
| 992 | got_new_vpn_id = 1; |
| 993 | else if (unformat (input, "table %d", &tbl_id)) |
| 994 | got_new_vpn_id = 1; |
| 995 | else |
| 996 | break; |
| 997 | } |
| 998 | if (tbl_id == ~0) |
| 999 | return clib_error_return (0, "no table ID specified."); |
| 1000 | |
| 1001 | if (is_del || got_new_vpn_id) |
| 1002 | { |
| 1003 | int rv; |
| 1004 | rv = dhcp_proxy_set_option82_vss(tbl_id, oui, fib_id, is_del); |
| 1005 | switch (rv) |
| 1006 | { |
| 1007 | case 0: |
| 1008 | return 0; |
| 1009 | |
| 1010 | case VNET_API_ERROR_NO_SUCH_FIB: |
| 1011 | return clib_error_return (0, "option 82 vss(oui:%d, vpn-id:%d) not found in table %d", |
| 1012 | oui, fib_id, tbl_id); |
| 1013 | |
| 1014 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 1015 | return clib_error_return (0, "option 82 vss for table %d not found in in pool.", |
| 1016 | tbl_id); |
| 1017 | default: |
| 1018 | return clib_error_return (0, "BUG: rv %d", rv); |
| 1019 | } |
| 1020 | } |
| 1021 | else |
| 1022 | return clib_error_return (0, "parse error`%U'", |
| 1023 | format_unformat_error, input); |
| 1024 | } |
| 1025 | |
| 1026 | VLIB_CLI_COMMAND (dhcp_proxy_vss_command,static) = { |
| 1027 | .path = "set dhcp option-82 vss", |
| 1028 | .short_help = "set dhcp option-82 vss [del] table <table id> oui <oui> vpn-id <vpn-id>", |
| 1029 | .function = dhcp_option_82_vss_fn, |
| 1030 | }; |
| 1031 | |
| 1032 | |
| 1033 | static clib_error_t * |
| 1034 | dhcp_vss_show_command_fn (vlib_main_t * vm, |
| 1035 | unformat_input_t * input, |
| 1036 | vlib_cli_command_t * cmd) |
| 1037 | |
| 1038 | { |
| 1039 | dhcp_proxy_main_t * dm = &dhcp_proxy_main; |
| 1040 | vss_info *v; |
| 1041 | u32 oui; |
| 1042 | u32 fib_id; |
| 1043 | u32 tbl_id; |
| 1044 | uword index; |
| 1045 | |
| 1046 | vlib_cli_output (vm, "%=9s%=11s%=12s","Table", "OUI", "VPN-ID"); |
| 1047 | hash_foreach (tbl_id, index, dm->opt82vss_index_by_vrf_id, |
| 1048 | ({ |
| 1049 | v = pool_elt_at_index (dm->opt82vss, index); |
| 1050 | oui = v->vpn_id.oui; |
| 1051 | fib_id = v->vpn_id.fib_id; |
| 1052 | vlib_cli_output (vm, "%=9d 0x%08x%=12d", |
| 1053 | tbl_id, oui, fib_id); |
| 1054 | })); |
| 1055 | |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
| 1059 | VLIB_CLI_COMMAND (dhcp_proxy_vss_show_command, static) = { |
| 1060 | .path = "show dhcp vss", |
| 1061 | .short_help = "show dhcp VSS", |
| 1062 | .function = dhcp_vss_show_command_fn, |
| 1063 | }; |
| 1064 | |
| 1065 | static clib_error_t * |
| 1066 | dhcp_option_82_address_show_command_fn (vlib_main_t * vm, |
| 1067 | unformat_input_t * input, |
| 1068 | vlib_cli_command_t * cmd) |
| 1069 | |
| 1070 | { |
| 1071 | dhcp_proxy_main_t *dm = &dhcp_proxy_main; |
| 1072 | vnet_main_t *vnm = vnet_get_main(); |
| 1073 | u32 sw_if_index0=0, sw_if_index; |
| 1074 | ip4_address_t *ia0; |
| 1075 | vnet_sw_interface_t *swif; |
| 1076 | |
| 1077 | while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) |
| 1078 | { |
| 1079 | |
| 1080 | if (unformat(input, "%U", |
| 1081 | unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0)) |
| 1082 | { |
| 1083 | swif = vnet_get_sw_interface (vnm, sw_if_index0); |
| 1084 | sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ? |
| 1085 | swif->unnumbered_sw_if_index : sw_if_index0; |
Dave Barach | 203c632 | 2016-06-26 10:29:03 -0400 | [diff] [blame] | 1086 | ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1087 | if (ia0) |
| 1088 | { |
| 1089 | vlib_cli_output (vm, "%=20s%=20s", "interface", |
| 1090 | "source IP address"); |
| 1091 | |
| 1092 | vlib_cli_output (vm, "%=20U%=20U", |
| 1093 | format_vnet_sw_if_index_name, |
| 1094 | dm->vnet_main, sw_if_index0, |
| 1095 | format_ip4_address, ia0); |
| 1096 | } |
| 1097 | else |
| 1098 | vlib_cli_output (vm, "%=34s %=20U", |
| 1099 | "No IPv4 address configured on", |
| 1100 | format_vnet_sw_if_index_name, |
| 1101 | dm->vnet_main, sw_if_index); |
| 1102 | } |
| 1103 | else |
| 1104 | break; |
| 1105 | } |
| 1106 | |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | VLIB_CLI_COMMAND (dhcp_proxy_address_show_command,static) = { |
| 1111 | .path = "show dhcp option-82-address interface", |
| 1112 | .short_help = "show dhcp option-82-address interface <interface>", |
| 1113 | .function = dhcp_option_82_address_show_command_fn, |
| 1114 | }; |