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