Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * proxy_node.c: dhcpv6 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/dhcpv6/proxy.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 21 | #include <vnet/fib/ip6_fib.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
| 23 | static char * dhcpv6_proxy_error_strings[] = { |
| 24 | #define dhcpv6_proxy_error(n,s) s, |
| 25 | #include "proxy_error.def" |
| 26 | #undef dhcpv6_proxy_error |
| 27 | }; |
| 28 | |
| 29 | #define foreach_dhcpv6_proxy_to_server_input_next \ |
| 30 | _ (DROP, "error-drop") \ |
| 31 | _ (LOOKUP, "ip6-lookup") \ |
| 32 | _ (SEND_TO_CLIENT, "dhcpv6-proxy-to-client") |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 33 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | |
| 35 | typedef enum { |
| 36 | #define _(s,n) DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s, |
| 37 | foreach_dhcpv6_proxy_to_server_input_next |
| 38 | #undef _ |
| 39 | DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT, |
| 40 | } dhcpv6_proxy_to_server_input_next_t; |
| 41 | |
| 42 | typedef struct { |
| 43 | /* 0 => to server, 1 => to client */ |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 44 | int which; |
| 45 | u8 packet_data[64]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | u32 error; |
| 47 | u32 sw_if_index; |
| 48 | u32 original_sw_if_index; |
| 49 | } dhcpv6_proxy_trace_t; |
| 50 | |
| 51 | vlib_node_registration_t dhcpv6_proxy_to_server_node; |
| 52 | vlib_node_registration_t dhcpv6_proxy_to_client_node; |
| 53 | |
| 54 | |
| 55 | u8 * format_dhcpv6_proxy_trace (u8 * s, va_list * args) |
| 56 | { |
| 57 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 58 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 59 | dhcpv6_proxy_trace_t * t = va_arg (*args, dhcpv6_proxy_trace_t *); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 60 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | if (t->which == 0) |
| 62 | s = format (s, "DHCPV6 proxy: sent to server %U", |
| 63 | format_ip6_address, &t->packet_data, sizeof (ip6_address_t)); |
| 64 | else |
| 65 | s = format (s, "DHCPV6 proxy: sent to client from %U", |
| 66 | format_ip6_address, &t->packet_data, sizeof (ip6_address_t)); |
| 67 | if (t->error != (u32)~0) |
| 68 | s = format (s, " error: %s\n", dhcpv6_proxy_error_strings[t->error]); |
| 69 | |
| 70 | s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n", |
| 71 | t->original_sw_if_index, t->sw_if_index); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 72 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | return s; |
| 74 | } |
| 75 | |
| 76 | u8 * format_dhcpv6_proxy_header_with_length (u8 * s, va_list * args) |
| 77 | { |
| 78 | dhcpv6_header_t * h = va_arg (*args, dhcpv6_header_t *); |
| 79 | u32 max_header_bytes = va_arg (*args, u32); |
| 80 | u32 header_bytes; |
| 81 | |
| 82 | header_bytes = sizeof (h[0]); |
| 83 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 84 | return format (s, "dhcpv6 header truncated"); |
| 85 | |
| 86 | s = format (s, "DHCPV6 Proxy"); |
| 87 | |
| 88 | return s; |
| 89 | } |
| 90 | /* get first interface address */ |
| 91 | static ip6_address_t * |
| 92 | ip6_interface_first_global_or_site_address (ip6_main_t * im, u32 sw_if_index) |
| 93 | { |
| 94 | ip_lookup_main_t * lm = &im->lookup_main; |
| 95 | ip_interface_address_t * ia = 0; |
| 96 | ip6_address_t * result = 0; |
| 97 | |
| 98 | foreach_ip_interface_address (lm, ia, sw_if_index, |
| 99 | 1 /* honor unnumbered */, |
| 100 | ({ |
| 101 | ip6_address_t * a = ip_interface_address_get_address (lm, ia); |
| 102 | if ((a->as_u8[0] & 0xe0) == 0x20 || |
| 103 | (a->as_u8[0] & 0xfe) == 0xfc) { |
| 104 | result = a; |
| 105 | break; |
| 106 | } |
| 107 | })); |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | /* get first interface address */ |
| 112 | static ip6_address_t * |
| 113 | ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index) |
| 114 | { |
| 115 | ip_lookup_main_t * lm = &im->lookup_main; |
| 116 | ip_interface_address_t * ia = 0; |
| 117 | ip6_address_t * result = 0; |
| 118 | |
| 119 | foreach_ip_interface_address (lm, ia, sw_if_index, |
| 120 | 1 /* honor unnumbered */, |
| 121 | ({ |
| 122 | ip6_address_t * a = ip_interface_address_get_address (lm, ia); |
| 123 | result = a; |
| 124 | break; |
| 125 | })); |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | static inline void copy_ip6_address (ip6_address_t *dst, ip6_address_t *src) |
| 130 | { |
| 131 | |
| 132 | dst->as_u64[0] = src->as_u64[0]; |
| 133 | dst->as_u64[1] = src->as_u64[1]; |
| 134 | } |
| 135 | |
| 136 | static uword |
| 137 | dhcpv6_proxy_to_server_input (vlib_main_t * vm, |
| 138 | vlib_node_runtime_t * node, |
| 139 | vlib_frame_t * from_frame) |
| 140 | { |
| 141 | u32 n_left_from, next_index, * from, * to_next; |
| 142 | dhcpv6_proxy_main_t * dpm = &dhcpv6_proxy_main; |
| 143 | from = vlib_frame_vector_args (from_frame); |
| 144 | n_left_from = from_frame->n_vectors; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 145 | u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | u32 pkts_no_interface_address=0, pkts_no_exceeding_max_hop=0; |
| 147 | u32 pkts_no_src_address=0; |
| 148 | u32 pkts_wrong_msg_type=0; |
| 149 | u32 pkts_too_big=0; |
| 150 | ip6_main_t * im = &ip6_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | ip6_fib_t * fib; |
| 152 | ip6_address_t * src; |
| 153 | int bogus_length; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 154 | dhcpv6_server_t * server; |
| 155 | u32 rx_fib_idx = 0, server_fib_idx = 0; |
| 156 | u32 server_idx; |
| 157 | u32 fib_id1 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | |
| 159 | next_index = node->cached_next_index; |
| 160 | |
| 161 | while (n_left_from > 0) |
| 162 | { |
| 163 | u32 n_left_to_next; |
| 164 | |
| 165 | vlib_get_next_frame (vm, node, next_index, |
| 166 | to_next, n_left_to_next); |
| 167 | |
| 168 | while (n_left_from > 0 && n_left_to_next > 0) |
| 169 | { |
| 170 | vnet_main_t *vnm = vnet_get_main(); |
| 171 | u32 sw_if_index = 0; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 172 | u32 rx_sw_if_index = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | vnet_sw_interface_t *swif; |
| 174 | u32 bi0; |
| 175 | vlib_buffer_t * b0; |
| 176 | udp_header_t * u0, *u1; |
| 177 | dhcpv6_header_t * h0; // client msg hdr |
| 178 | ip6_header_t * ip0, *ip1; |
| 179 | ip6_address_t _ia0, *ia0=&_ia0; |
| 180 | u32 next0; |
| 181 | u32 error0 = (u32) ~0; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 182 | dhcpv6_option_t *fwd_opt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | dhcpv6_relay_hdr_t *r1; |
| 184 | u16 len; |
| 185 | dhcpv6_int_id_t *id1; |
| 186 | dhcpv6_vss_t *vss1; |
| 187 | dhcpv6_client_mac_t *cmac; // client mac |
| 188 | ethernet_header_t * e_h0; |
| 189 | u8 client_src_mac[6]; |
| 190 | vlib_buffer_free_list_t *fl; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 191 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | uword *p_vss; |
| 193 | u32 oui1=0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | dhcpv6_vss_info *vss; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 195 | |
| 196 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | bi0 = from[0]; |
| 198 | to_next[0] = bi0; |
| 199 | from += 1; |
| 200 | to_next += 1; |
| 201 | n_left_from -= 1; |
| 202 | n_left_to_next -= 1; |
| 203 | |
| 204 | b0 = vlib_get_buffer (vm, bi0); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 205 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | h0 = vlib_buffer_get_current (b0); |
Chris Luke | 194ebc5 | 2016-04-25 14:26:55 -0400 | [diff] [blame] | 207 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 208 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | * udp_local hands us the DHCPV6 header. |
| 210 | */ |
| 211 | u0 = (void *)h0 -(sizeof(*u0)); |
| 212 | ip0 = (void *)u0 -(sizeof(*ip0)); |
Chris Luke | 194ebc5 | 2016-04-25 14:26:55 -0400 | [diff] [blame] | 213 | e_h0 = (void *)ip0 - ethernet_buffer_header_size(b0); |
| 214 | |
| 215 | clib_memcpy(client_src_mac, e_h0->src_address, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
| 217 | switch (h0->u.msg_type) { |
| 218 | case DHCPV6_MSG_SOLICIT: |
| 219 | case DHCPV6_MSG_REQUEST: |
| 220 | case DHCPV6_MSG_CONFIRM: |
| 221 | case DHCPV6_MSG_RENEW: |
| 222 | case DHCPV6_MSG_REBIND: |
| 223 | case DHCPV6_MSG_RELEASE: |
| 224 | case DHCPV6_MSG_DECLINE: |
| 225 | case DHCPV6_MSG_INFORMATION_REQUEST: |
| 226 | case DHCPV6_MSG_RELAY_FORW: |
| 227 | /* send to server */ |
| 228 | break; |
| 229 | case DHCPV6_MSG_RELAY_REPL: |
| 230 | /* send to client */ |
| 231 | next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT; |
| 232 | error0 = 0; |
| 233 | pkts_to_client++; |
| 234 | goto do_enqueue; |
| 235 | default: |
| 236 | /* drop the packet */ |
| 237 | pkts_wrong_msg_type++; |
| 238 | error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE; |
| 239 | next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 240 | goto do_trace; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 241 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | } |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 243 | |
| 244 | /* Send to DHCPV6 server via the configured FIB */ |
| 245 | rx_sw_if_index = sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX]; |
| 246 | rx_fib_idx = im->fib_index_by_sw_if_index [rx_sw_if_index]; |
| 247 | server_idx = dpm->dhcp6_server_index_by_rx_fib_index[rx_fib_idx]; |
| 248 | |
| 249 | if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp6_servers, |
| 250 | server_idx))) |
| 251 | { |
| 252 | no_server: |
| 253 | error0 = DHCPV6_PROXY_ERROR_NO_SERVER; |
| 254 | next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 255 | pkts_no_server++; |
| 256 | goto do_trace; |
| 257 | } |
| 258 | |
| 259 | server = pool_elt_at_index(dpm->dhcp6_servers, server_idx); |
| 260 | if (server->valid == 0) |
| 261 | goto no_server; |
| 262 | |
| 263 | server_fib_idx = server->server_fib6_index; |
| 264 | vnet_buffer(b0)->sw_if_index[VLIB_TX] = server_fib_idx; |
| 265 | |
| 266 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 267 | /* relay-option header pointer */ |
| 268 | vlib_buffer_advance(b0, -(sizeof(*fwd_opt))); |
| 269 | fwd_opt = vlib_buffer_get_current(b0); |
| 270 | /* relay message header pointer */ |
| 271 | vlib_buffer_advance(b0, -(sizeof(*r1))); |
| 272 | r1 = vlib_buffer_get_current(b0); |
| 273 | |
| 274 | vlib_buffer_advance(b0, -(sizeof(*u1))); |
| 275 | u1 = vlib_buffer_get_current(b0); |
| 276 | |
| 277 | vlib_buffer_advance(b0, -(sizeof(*ip1))); |
| 278 | ip1 = vlib_buffer_get_current(b0); |
| 279 | |
| 280 | /* fill in all that rubbish... */ |
| 281 | len = clib_net_to_host_u16(u0->length) - sizeof(udp_header_t); |
| 282 | copy_ip6_address(&r1->peer_addr, &ip0->src_address); |
| 283 | |
| 284 | r1->msg_type = DHCPV6_MSG_RELAY_FORW; |
| 285 | fwd_opt->length = clib_host_to_net_u16(len); |
| 286 | fwd_opt->option = clib_host_to_net_u16(DHCPV6_OPTION_RELAY_MSG); |
| 287 | |
| 288 | r1->hop_count++; |
| 289 | r1->hop_count = (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count; |
| 290 | |
| 291 | if (PREDICT_FALSE(r1->hop_count >= HOP_COUNT_LIMIT)) |
| 292 | { |
| 293 | error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS; |
| 294 | next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 295 | pkts_no_exceeding_max_hop++; |
| 296 | goto do_trace; |
| 297 | } |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 298 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | |
| 300 | /* If relay-fwd and src address is site or global unicast address */ |
| 301 | if (h0->u.msg_type == DHCPV6_MSG_RELAY_FORW && |
| 302 | ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 || |
| 303 | (ip0->src_address.as_u8[0] & 0xfe) == 0xfc)) |
| 304 | { |
| 305 | /* Set link address to zero */ |
| 306 | r1->link_addr.as_u64[0] = 0; |
| 307 | r1->link_addr.as_u64[1] = 0; |
| 308 | goto link_address_set; |
| 309 | } |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 310 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | /* if receiving interface is unnumbered, use receiving interface |
| 312 | * IP address as link address, otherwise use the loopback interface |
| 313 | * IP address as link address. |
| 314 | */ |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 315 | |
| 316 | swif = vnet_get_sw_interface (vnm, rx_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) |
| 318 | sw_if_index = swif->unnumbered_sw_if_index; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 319 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | ia0 = ip6_interface_first_global_or_site_address(&ip6_main, sw_if_index); |
| 321 | if (ia0 == 0) |
| 322 | { |
| 323 | error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS; |
| 324 | next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 325 | pkts_no_interface_address++; |
| 326 | goto do_trace; |
| 327 | } |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 328 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | copy_ip6_address(&r1->link_addr, ia0); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 330 | |
| 331 | link_address_set: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | fl = vlib_buffer_get_free_list (vm, b0->free_list_index); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 333 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | if ((b0->current_length+sizeof(*id1)+sizeof(*vss1)+sizeof(*cmac)) |
| 335 | > fl->n_data_bytes) |
| 336 | { |
| 337 | error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG; |
| 338 | next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 339 | pkts_too_big++; |
| 340 | goto do_trace; |
| 341 | } |
| 342 | |
| 343 | id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length); |
| 344 | b0->current_length += (sizeof (*id1)); |
| 345 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 347 | fib = ip6_fib_get (rx_fib_idx); |
| 348 | |
| 349 | //TODO: Revisit if hash makes sense here |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | p_vss = hash_get (dpm->vss_index_by_vrf_id, |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 351 | fib->table_id); |
| 352 | if (p_vss) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | { |
| 354 | vss = pool_elt_at_index (dpm->vss, p_vss[0]); |
| 355 | oui1 = vss->vpn_id.oui; |
| 356 | fib_id1 = vss->vpn_id.fib_id; |
| 357 | } |
| 358 | |
| 359 | id1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_INTERFACE_ID); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 360 | id1->opt.length = clib_host_to_net_u16(sizeof(rx_sw_if_index)); |
| 361 | id1->int_idx = clib_host_to_net_u32(rx_sw_if_index); |
| 362 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | u1->length =0; |
| 364 | if (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) |
| 365 | { |
| 366 | cmac = (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length); |
| 367 | b0->current_length += (sizeof (*cmac)); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 368 | cmac->opt.length =clib_host_to_net_u16(sizeof(*cmac) - |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | sizeof(cmac->opt)); |
| 370 | cmac->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS); |
| 371 | cmac->link_type = clib_host_to_net_u16(1); // ethernet |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 372 | clib_memcpy(cmac->data, client_src_mac, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | u1->length += sizeof(*cmac); |
| 374 | } |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 375 | if (server->insert_vss !=0 ) { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length); |
| 377 | b0->current_length += (sizeof (*vss1)); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 378 | vss1->opt.length =clib_host_to_net_u16(sizeof(*vss1) - |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | sizeof(vss1->opt)); |
| 380 | vss1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_VSS); |
| 381 | vss1->data[0] = 1; // type |
| 382 | vss1->data[1] = oui1>>16 & 0xff; |
| 383 | vss1->data[2] = oui1>>8 & 0xff; |
| 384 | vss1->data[3] = oui1 & 0xff; |
| 385 | vss1->data[4] = fib_id1>>24 & 0xff; |
| 386 | vss1->data[5] = fib_id1>>16 & 0xff; |
| 387 | vss1->data[6] = fib_id1>>8 & 0xff; |
| 388 | vss1->data[7] = fib_id1 & 0xff; |
| 389 | u1->length += sizeof(*vss1); |
| 390 | } |
| 391 | |
| 392 | pkts_to_server++; |
| 393 | u1->checksum = 0; |
| 394 | u1->src_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_client); |
| 395 | u1->dst_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_server); |
| 396 | |
| 397 | u1->length = |
| 398 | clib_host_to_net_u16( clib_net_to_host_u16(fwd_opt->length) + |
| 399 | sizeof(*r1) + sizeof(*fwd_opt) + |
| 400 | sizeof(*u1) + sizeof(*id1) + u1->length); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 401 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 402 | memset(ip1, 0, sizeof(*ip1)); |
| 403 | ip1->ip_version_traffic_class_and_flow_label = 0x60; |
| 404 | ip1->payload_length = u1->length; |
| 405 | ip1->protocol = PROTO_UDP; |
| 406 | ip1->hop_limit = HOP_COUNT_LIMIT; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 407 | src = (server->dhcp6_server.as_u64[0] || server->dhcp6_server.as_u64[1]) ? |
| 408 | &server->dhcp6_server : &dpm->all_dhcpv6_server_address; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | copy_ip6_address(&ip1->dst_address, src); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 410 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | |
| 412 | ia0 = ip6_interface_first_global_or_site_address |
| 413 | (&ip6_main, vnet_buffer(b0)->sw_if_index[VLIB_RX]); |
| 414 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 415 | src = (server->dhcp6_src_address.as_u64[0] || server->dhcp6_src_address.as_u64[1]) ? |
| 416 | &server->dhcp6_src_address : ia0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | if (ia0 == 0) |
| 418 | { |
| 419 | error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS; |
| 420 | next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP; |
| 421 | pkts_no_src_address++; |
| 422 | goto do_trace; |
| 423 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 425 | copy_ip6_address (&ip1->src_address, src); |
| 426 | |
| 427 | |
| 428 | u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | &bogus_length); |
| 430 | ASSERT(bogus_length == 0); |
| 431 | |
| 432 | next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP; |
| 433 | |
| 434 | do_trace: |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 435 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | { |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 437 | dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | b0, sizeof (*tr)); |
| 439 | tr->which = 0; /* to server */ |
| 440 | tr->error = error0; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 441 | tr->original_sw_if_index = rx_sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 442 | tr->sw_if_index = sw_if_index; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 443 | if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0) |
| 444 | copy_ip6_address((ip6_address_t *)&tr->packet_data[0], &server->dhcp6_server); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | do_enqueue: |
| 448 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 449 | to_next, n_left_to_next, |
| 450 | bi0, next0); |
| 451 | } |
| 452 | |
| 453 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 454 | } |
| 455 | |
| 456 | vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index, |
| 457 | DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT, |
| 458 | pkts_to_client); |
| 459 | vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index, |
| 460 | DHCPV6_PROXY_ERROR_RELAY_TO_SERVER, |
| 461 | pkts_to_server); |
| 462 | vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index, |
| 463 | DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS, |
| 464 | pkts_no_interface_address); |
| 465 | vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index, |
| 466 | DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE, |
| 467 | pkts_wrong_msg_type); |
| 468 | vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index, |
| 469 | DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS, |
| 470 | pkts_no_src_address); |
| 471 | vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index, |
| 472 | DHCPV6_PROXY_ERROR_PKT_TOO_BIG, |
| 473 | pkts_too_big); |
| 474 | return from_frame->n_vectors; |
| 475 | } |
| 476 | |
| 477 | VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node) = { |
| 478 | .function = dhcpv6_proxy_to_server_input, |
| 479 | .name = "dhcpv6-proxy-to-server", |
| 480 | /* Takes a vector of packets. */ |
| 481 | .vector_size = sizeof (u32), |
| 482 | |
| 483 | .n_errors = DHCPV6_PROXY_N_ERROR, |
| 484 | .error_strings = dhcpv6_proxy_error_strings, |
| 485 | |
| 486 | .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT, |
| 487 | .next_nodes = { |
| 488 | #define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n, |
| 489 | foreach_dhcpv6_proxy_to_server_input_next |
| 490 | #undef _ |
| 491 | }, |
| 492 | |
| 493 | .format_buffer = format_dhcpv6_proxy_header_with_length, |
| 494 | .format_trace = format_dhcpv6_proxy_trace, |
| 495 | #if 0 |
| 496 | .unformat_buffer = unformat_dhcpv6_proxy_header, |
| 497 | #endif |
| 498 | }; |
| 499 | |
| 500 | static uword |
| 501 | dhcpv6_proxy_to_client_input (vlib_main_t * vm, |
| 502 | vlib_node_runtime_t * node, |
| 503 | vlib_frame_t * from_frame) |
| 504 | { |
| 505 | |
| 506 | u32 n_left_from, * from; |
| 507 | ethernet_main_t *em = ethernet_get_main (vm); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 508 | dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main; |
| 509 | dhcpv6_server_t * server; |
| 510 | vnet_main_t * vnm = vnet_get_main(); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | int bogus_length; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 512 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | from = vlib_frame_vector_args (from_frame); |
| 514 | n_left_from = from_frame->n_vectors; |
| 515 | |
| 516 | while (n_left_from > 0) |
| 517 | { |
| 518 | u32 bi0; |
| 519 | vlib_buffer_t * b0; |
| 520 | udp_header_t * u0, *u1=0; |
| 521 | dhcpv6_relay_hdr_t * h0; |
| 522 | ip6_header_t * ip1 = 0, *ip0; |
| 523 | ip6_address_t _ia0, * ia0 = &_ia0; |
| 524 | ip6_address_t client_address; |
| 525 | ethernet_interface_t *ei0; |
| 526 | ethernet_header_t *mac0; |
| 527 | vnet_hw_interface_t *hi0; |
| 528 | vlib_frame_t *f0; |
| 529 | u32 * to_next0; |
| 530 | u32 sw_if_index = ~0; |
| 531 | u32 original_sw_if_index = ~0; |
| 532 | vnet_sw_interface_t *si0; |
| 533 | u32 error0 = (u32)~0; |
| 534 | vnet_sw_interface_t *swif; |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 535 | dhcpv6_option_t *r0 = 0, *o; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | u16 len = 0; |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 537 | u8 interface_opt_flag = 0; |
| 538 | u8 relay_msg_opt_flag = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 539 | ip6_fib_t * svr_fib; |
| 540 | ip6_main_t * im = &ip6_main; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 541 | u32 server_fib_idx, svr_fib_id, client_fib_idx, server_idx; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | |
| 543 | bi0 = from[0]; |
| 544 | from += 1; |
| 545 | n_left_from -= 1; |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 546 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | b0 = vlib_get_buffer (vm, bi0); |
| 548 | h0 = vlib_buffer_get_current (b0); |
| 549 | |
| 550 | if (DHCPV6_MSG_RELAY_REPL != h0->msg_type) |
| 551 | { |
| 552 | error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE; |
| 553 | |
| 554 | drop_packet: |
| 555 | vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index, |
| 556 | error0, 1); |
| 557 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 558 | f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | to_next0 = vlib_frame_vector_args (f0); |
| 560 | to_next0[0] = bi0; |
| 561 | f0->n_vectors = 1; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 562 | vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | goto do_trace; |
| 564 | } |
| 565 | /* hop count seems not need to be checked */ |
| 566 | if (HOP_COUNT_LIMIT < h0->hop_count) |
| 567 | { |
| 568 | error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS; |
| 569 | goto drop_packet; |
| 570 | } |
| 571 | u0 = (void *)h0 -(sizeof(*u0)); |
| 572 | ip0 = (void *)u0 -(sizeof(*ip0)); |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 573 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 574 | vlib_buffer_advance (b0, sizeof(*h0)); |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 575 | o = vlib_buffer_get_current (b0); |
| 576 | |
| 577 | /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID) |
| 578 | _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there. |
| 579 | Currently assuming no other options need to be processed |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 580 | The interface-ID is the FIB number we need |
| 581 | to track down the client-facing interface */ |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 582 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 583 | while ((u8 *) o < (b0->data + b0->current_data + b0->current_length)) |
| 584 | { |
| 585 | if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16(o->option)) |
| 586 | { |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 587 | interface_opt_flag = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 588 | if (clib_net_to_host_u16(o->length) == sizeof(sw_if_index)) |
| 589 | sw_if_index = clib_net_to_host_u32(((dhcpv6_int_id_t*)o)->int_idx); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 590 | if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index)) |
| 591 | { |
| 592 | error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION; |
| 593 | goto drop_packet; |
| 594 | } |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 595 | } |
| 596 | if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16(o->option)) |
| 597 | { |
| 598 | relay_msg_opt_flag = 1; |
| 599 | r0 = vlib_buffer_get_current (b0); |
| 600 | } |
| 601 | if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1)) |
| 602 | break; |
| 603 | vlib_buffer_advance (b0, sizeof(*o) + clib_net_to_host_u16(o->length)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 604 | o = (dhcpv6_option_t *) (((uword) o) + clib_net_to_host_u16(o->length) + sizeof(*o)); |
| 605 | } |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 606 | |
| 607 | if ((relay_msg_opt_flag == 0) || (r0 == 0)) |
| 608 | { |
| 609 | error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION; |
| 610 | goto drop_packet; |
| 611 | } |
| 612 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | if ((u32)~0 == sw_if_index) |
| 614 | { |
| 615 | error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION; |
| 616 | goto drop_packet; |
| 617 | } |
| 618 | |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 619 | //Advance buffer to start of encapsulated DHCPv6 message |
| 620 | vlib_buffer_advance (b0, sizeof(*r0)); |
| 621 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 622 | client_fib_idx = im->fib_index_by_sw_if_index[sw_if_index]; |
| 623 | if (client_fib_idx < vec_len(dm->dhcp6_server_index_by_rx_fib_index)) |
| 624 | server_idx = dm->dhcp6_server_index_by_rx_fib_index[client_fib_idx]; |
| 625 | else |
| 626 | server_idx = 0; |
| 627 | |
| 628 | if (PREDICT_FALSE (pool_is_free_index (dm->dhcp6_servers, server_idx))) |
| 629 | { |
| 630 | error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION; |
| 631 | goto drop_packet; |
| 632 | } |
| 633 | |
| 634 | server = pool_elt_at_index (dm->dhcp6_servers, server_idx); |
| 635 | if (server->valid == 0) |
| 636 | { |
| 637 | error0 = DHCPV6_PROXY_ERROR_NO_SERVER; |
| 638 | goto drop_packet; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | server_fib_idx = im->fib_index_by_sw_if_index |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 643 | [vnet_buffer(b0)->sw_if_index[VLIB_RX]]; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 644 | svr_fib = ip6_fib_get (server_fib_idx); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | svr_fib_id = svr_fib->table_id; |
| 646 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 647 | if (svr_fib_id != server->server_fib6_index || |
| 648 | ip0->src_address.as_u64[0] != server->dhcp6_server.as_u64[0] || |
| 649 | ip0->src_address.as_u64[1] != server->dhcp6_server.as_u64[1]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | { |
| 651 | //drop packet if not from server with configured address or FIB |
| 652 | error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS; |
| 653 | goto drop_packet; |
| 654 | } |
| 655 | |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 656 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 657 | = sw_if_index; |
| 658 | |
| 659 | swif = vnet_get_sw_interface (vnm, original_sw_if_index); |
| 660 | if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) |
| 661 | sw_if_index = swif->unnumbered_sw_if_index; |
| 662 | |
Wojciech Dec | 96e6f95 | 2016-10-20 11:25:37 +0200 | [diff] [blame] | 663 | |
| 664 | /* |
| 665 | * udp_local hands us the DHCPV6 header, need udp hdr, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 666 | * ip hdr to relay to client |
| 667 | */ |
| 668 | vlib_buffer_advance (b0, -(sizeof(*u1))); |
| 669 | u1 = vlib_buffer_get_current (b0); |
| 670 | |
| 671 | vlib_buffer_advance (b0, -(sizeof(*ip1))); |
| 672 | ip1 = vlib_buffer_get_current (b0); |
| 673 | |
| 674 | copy_ip6_address(&client_address, &h0->peer_addr); |
| 675 | |
| 676 | ia0 = ip6_interface_first_address (&ip6_main, sw_if_index); |
| 677 | if (ia0 == 0) |
| 678 | { |
| 679 | error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS; |
| 680 | goto drop_packet; |
| 681 | } |
| 682 | |
| 683 | len = clib_net_to_host_u16(r0->length); |
| 684 | memset(ip1, 0, sizeof(*ip1)); |
| 685 | copy_ip6_address(&ip1->dst_address, &client_address); |
| 686 | u1->checksum = 0; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 687 | u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 688 | u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client); |
| 689 | u1->length = clib_host_to_net_u16 (len + sizeof(udp_header_t)); |
| 690 | |
| 691 | ip1->ip_version_traffic_class_and_flow_label = |
| 692 | ip0->ip_version_traffic_class_and_flow_label & |
| 693 | 0x00000fff; |
| 694 | ip1->payload_length = u1->length; |
| 695 | ip1->protocol = PROTO_UDP; |
| 696 | ip1->hop_limit = HOP_COUNT_LIMIT; |
| 697 | copy_ip6_address(&ip1->src_address, ia0); |
| 698 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 699 | u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 700 | &bogus_length); |
| 701 | ASSERT(bogus_length == 0); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 702 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 703 | vlib_buffer_advance (b0, -(sizeof(ethernet_header_t))); |
| 704 | si0 = vnet_get_sw_interface (vnm, original_sw_if_index); |
| 705 | if (si0->type == VNET_SW_INTERFACE_TYPE_SUB) |
| 706 | vlib_buffer_advance (b0, -4 /* space for VLAN tag */); |
| 707 | |
| 708 | mac0 = vlib_buffer_get_current (b0); |
| 709 | |
| 710 | hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index); |
| 711 | ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 712 | clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 713 | memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address)); |
| 714 | mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ? |
| 715 | clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x86dd); |
| 716 | |
| 717 | if (si0->type == VNET_SW_INTERFACE_TYPE_SUB) |
| 718 | { |
| 719 | u32 * vlan_tag = (u32 *)(mac0+1); |
| 720 | u32 tmp; |
| 721 | tmp = (si0->sub.id << 16) | 0x0800; |
| 722 | *vlan_tag = clib_host_to_net_u32 (tmp); |
| 723 | } |
| 724 | |
| 725 | /* $$$ consider adding a dynamic next to the graph node, for performance */ |
| 726 | f0 = vlib_get_frame_to_node (vm, hi0->output_node_index); |
| 727 | to_next0 = vlib_frame_vector_args (f0); |
| 728 | to_next0[0] = bi0; |
| 729 | f0->n_vectors = 1; |
| 730 | vlib_put_frame_to_node (vm, hi0->output_node_index, f0); |
| 731 | |
| 732 | do_trace: |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 733 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 734 | { |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 735 | dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 736 | b0, sizeof (*tr)); |
| 737 | tr->which = 1; /* to client */ |
| 738 | if (ia0) |
| 739 | copy_ip6_address((ip6_address_t*)tr->packet_data, ia0); |
| 740 | tr->error = error0; |
| 741 | tr->original_sw_if_index = original_sw_if_index; |
| 742 | tr->sw_if_index = sw_if_index; |
| 743 | } |
| 744 | } |
| 745 | return from_frame->n_vectors; |
| 746 | |
| 747 | } |
| 748 | |
| 749 | VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node) = { |
| 750 | .function = dhcpv6_proxy_to_client_input, |
| 751 | .name = "dhcpv6-proxy-to-client", |
| 752 | /* Takes a vector of packets. */ |
| 753 | .vector_size = sizeof (u32), |
| 754 | |
| 755 | .n_errors = DHCPV6_PROXY_N_ERROR, |
| 756 | .error_strings = dhcpv6_proxy_error_strings, |
| 757 | .format_buffer = format_dhcpv6_proxy_header_with_length, |
| 758 | .format_trace = format_dhcpv6_proxy_trace, |
| 759 | #if 0 |
| 760 | .unformat_buffer = unformat_dhcpv6_proxy_header, |
| 761 | #endif |
| 762 | }; |
| 763 | |
| 764 | clib_error_t * dhcpv6_proxy_init (vlib_main_t * vm) |
| 765 | { |
| 766 | dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main; |
| 767 | vlib_node_t * error_drop_node; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 768 | dhcpv6_server_t * server; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 769 | |
| 770 | dm->vlib_main = vm; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 771 | dm->vnet_main = vnet_get_main(); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 772 | error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); |
| 773 | dm->error_drop_node_index = error_drop_node->index; |
| 774 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 775 | dm->vss_index_by_vrf_id = hash_create (0, sizeof (uword)); |
| 776 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 777 | /* RFC says this is the dhcpv6 server address */ |
| 778 | dm->all_dhcpv6_server_address.as_u64[0] = clib_host_to_net_u64 (0xFF05000000000000); |
| 779 | dm->all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003); |
| 780 | |
| 781 | /* RFC says this is the server and agent address */ |
| 782 | dm->all_dhcpv6_server_relay_agent_address.as_u64[0] = clib_host_to_net_u64 (0xFF02000000000000); |
| 783 | dm->all_dhcpv6_server_relay_agent_address.as_u64[1] = clib_host_to_net_u64 (0x00010002); |
| 784 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 785 | udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client, |
| 786 | dhcpv6_proxy_to_client_node.index, 0 /* is_ip6 */); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 787 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 788 | udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | dhcpv6_proxy_to_server_node.index, 0 /* is_ip6 */); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 790 | |
| 791 | /* Create the default server, don't mark it valid */ |
| 792 | pool_get (dm->dhcp6_servers, server); |
| 793 | memset (server, 0, sizeof (*server)); |
| 794 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | VLIB_INIT_FUNCTION (dhcpv6_proxy_init); |
| 799 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 800 | /* Old API, manipulates a single server (only) shared by all Rx VRFs */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | int dhcpv6_proxy_set_server (ip6_address_t *addr, ip6_address_t *src_address, |
| 802 | u32 fib_id, int insert_vss, int is_del) |
| 803 | { |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 804 | return dhcpv6_proxy_set_server_2 (addr, src_address, |
| 805 | 0, fib_id, |
| 806 | insert_vss, is_del); |
| 807 | } |
| 808 | |
| 809 | int dhcpv6_proxy_set_server_2 (ip6_address_t *addr, ip6_address_t *src_address, |
| 810 | u32 rx_fib_id, u32 server_fib_id, |
| 811 | int insert_vss, int is_del) |
| 812 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 813 | dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 814 | dhcpv6_server_t * server = 0; |
| 815 | u32 server_fib_index = 0; |
| 816 | u32 rx_fib_index = 0; |
| 817 | |
| 818 | rx_fib_index = ip6_fib_table_find_or_create_and_lock(rx_fib_id); |
| 819 | server_fib_index = ip6_fib_table_find_or_create_and_lock(server_fib_id); |
| 820 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 821 | if (is_del) |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 822 | { |
| 823 | |
| 824 | if (rx_fib_index >= vec_len(dm->dhcp6_server_index_by_rx_fib_index)) |
| 825 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 826 | |
| 827 | server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index]; |
| 828 | |
| 829 | dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] = 0; |
| 830 | server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index); |
| 831 | memset (server, 0, sizeof (*server)); |
| 832 | pool_put (dm->dhcp6_servers, server); |
| 833 | return 0; |
| 834 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 835 | |
| 836 | if (addr->as_u64[0] == 0 && |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 837 | addr->as_u64[1] == 0 ) |
| 838 | return VNET_API_ERROR_INVALID_DST_ADDRESS; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 839 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 840 | if (src_address->as_u64[0] == 0 && |
| 841 | src_address->as_u64[1] == 0) |
| 842 | return VNET_API_ERROR_INVALID_SRC_ADDRESS; |
| 843 | |
| 844 | if (rx_fib_id == 0) |
| 845 | { |
| 846 | server = pool_elt_at_index (dm->dhcp6_servers, 0); |
| 847 | |
| 848 | goto initialize_it; |
| 849 | } |
| 850 | |
| 851 | if (rx_fib_index < vec_len(dm->dhcp6_server_index_by_rx_fib_index)) |
| 852 | { |
| 853 | server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index]; |
| 854 | if (server_fib_index != 0) |
| 855 | { |
| 856 | server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index); |
| 857 | goto initialize_it; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | /*Allocate a new server*/ |
| 862 | pool_get (dm->dhcp6_servers, server); |
| 863 | |
| 864 | initialize_it: |
| 865 | |
| 866 | copy_ip6_address(&server->dhcp6_server, addr); |
| 867 | copy_ip6_address(&server->dhcp6_src_address, src_address); |
| 868 | server->server_fib6_index = server_fib_index; |
| 869 | server->valid = 1; |
| 870 | server->insert_vss = insert_vss; |
| 871 | |
| 872 | vec_validate (dm->dhcp6_server_index_by_rx_fib_index, rx_fib_index); |
| 873 | dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] = |
| 874 | server - dm->dhcp6_servers; |
| 875 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | static clib_error_t * |
| 880 | dhcpv6_proxy_set_command_fn (vlib_main_t * vm, |
| 881 | unformat_input_t * input, |
| 882 | vlib_cli_command_t * cmd) |
| 883 | { |
| 884 | ip6_address_t addr, src_addr; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 885 | int set_server = 0, set_src_address = 0; |
| 886 | u32 rx_fib_id = 0, server_fib_id = 0; |
| 887 | int is_del = 0, add_vss = 0; |
| 888 | |
| 889 | while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 890 | { |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 891 | if (unformat (input, "server %U", |
| 892 | unformat_ip6_address, &addr)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 893 | set_server = 1; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 894 | else if (unformat(input, "src-address %U", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 895 | unformat_ip6_address, &src_addr)) |
| 896 | set_src_address =1; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 897 | else if (unformat (input, "server-fib-id %d", &server_fib_id)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 898 | ; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 899 | else if (unformat (input, "rx-fib-id %d", &rx_fib_id)) |
| 900 | ; |
| 901 | else if (unformat (input, "add-vss-option") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 902 | || unformat (input, "insert-option")) |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 903 | add_vss = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 904 | else if (unformat (input, "delete") || |
| 905 | unformat (input, "del")) |
| 906 | is_del = 1; |
| 907 | else |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | if (is_del || (set_server && set_src_address)) |
| 912 | { |
| 913 | int rv; |
| 914 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 915 | rv = dhcpv6_proxy_set_server_2 (&addr, &src_addr, rx_fib_id, |
| 916 | server_fib_id, add_vss, is_del); |
| 917 | |
| 918 | //TODO: Complete the errors |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 919 | switch (rv) |
| 920 | { |
| 921 | case 0: |
| 922 | return 0; |
| 923 | |
| 924 | case -1: |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 925 | return clib_error_return (0, "FIB id %d does not exist", server_fib_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 926 | |
| 927 | default: |
| 928 | return clib_error_return (0, "BUG: rv %d", rv); |
| 929 | } |
| 930 | } |
| 931 | else |
| 932 | return clib_error_return (0, "parse error`%U'", |
| 933 | format_unformat_error, input); |
| 934 | } |
| 935 | |
| 936 | VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = { |
| 937 | .path = "set dhcpv6 proxy", |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 938 | .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> " |
| 939 | "[add-vss-option] [server-fib-id <fib-id>] [rx-fib-id <fib-id>] ", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 940 | .function = dhcpv6_proxy_set_command_fn, |
| 941 | }; |
| 942 | |
| 943 | u8 * format_dhcpv6_proxy_server (u8 * s, va_list * args) |
| 944 | { |
| 945 | dhcpv6_proxy_main_t * dm = va_arg (*args, dhcpv6_proxy_main_t *); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 946 | dhcpv6_server_t * server = va_arg (*args, dhcpv6_server_t *); |
| 947 | u32 rx_fib_index = va_arg (*args, u32); |
| 948 | ip6_fib_t * rx_fib, * server_fib; |
| 949 | u32 server_fib_id = (u32)~0, rx_fib_id = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 950 | |
| 951 | if (dm == 0) |
| 952 | { |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 953 | s = format (s, "%=40s%=40s%=14s%=14s%=20s", "Server Address", "Source Address", |
| 954 | "Server FIB", "RX FIB", "Insert VSS Option"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 955 | return s; |
| 956 | } |
| 957 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 958 | server_fib = ip6_fib_get(server->server_fib6_index); |
| 959 | if (server_fib) |
| 960 | server_fib_id= server_fib->table_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 961 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 962 | rx_fib= ip6_fib_get(rx_fib_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 963 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 964 | if (rx_fib) |
| 965 | rx_fib_id = rx_fib->table_id; |
| 966 | |
| 967 | s = format (s, "%=40U%=40U%=14u%=14u%=20s", |
| 968 | format_ip6_address, &server->dhcp6_server, |
| 969 | format_ip6_address, &server->dhcp6_src_address, |
| 970 | server_fib_id, rx_fib_id, |
| 971 | server->insert_vss ? "yes" : "no"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 972 | return s; |
| 973 | } |
| 974 | |
| 975 | static clib_error_t * |
| 976 | dhcpv6_proxy_show_command_fn (vlib_main_t * vm, |
| 977 | unformat_input_t * input, |
| 978 | vlib_cli_command_t * cmd) |
| 979 | { |
| 980 | dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 981 | ip6_main_t * im = &ip6_main; |
| 982 | int i; |
| 983 | u32 server_index; |
| 984 | dhcpv6_server_t * server; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 985 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 986 | vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, 0 /* header line */, |
| 987 | 0, 0); |
| 988 | for (i = 0; i < vec_len (im->fibs); i++) |
| 989 | { |
| 990 | if (i < vec_len(dm->dhcp6_server_index_by_rx_fib_index)) |
| 991 | server_index = dm->dhcp6_server_index_by_rx_fib_index[i]; |
| 992 | else |
| 993 | server_index = 0; |
| 994 | server = pool_elt_at_index (dm->dhcp6_servers, server_index); |
| 995 | if (server->valid) |
| 996 | vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, dm, |
| 997 | server, i); |
| 998 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = { |
| 1003 | .path = "show dhcpv6 proxy", |
| 1004 | .short_help = "Display dhcpv6 proxy info", |
| 1005 | .function = dhcpv6_proxy_show_command_fn, |
| 1006 | }; |
| 1007 | |
| 1008 | int dhcpv6_proxy_set_vss(u32 tbl_id, |
| 1009 | u32 oui, |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1010 | u32 fib_id, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1011 | int is_del) |
| 1012 | { |
| 1013 | dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main; |
| 1014 | u32 old_oui, old_fib_id; |
| 1015 | uword *p; |
| 1016 | dhcpv6_vss_info *v; |
| 1017 | |
| 1018 | p = hash_get (dm->vss_index_by_vrf_id, tbl_id); |
| 1019 | |
| 1020 | if (p) { |
| 1021 | v = pool_elt_at_index (dm->vss, p[0]); |
| 1022 | if (!v) |
| 1023 | return VNET_API_ERROR_NO_SUCH_FIB; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1024 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1025 | old_oui = v->vpn_id.oui; |
| 1026 | old_fib_id = v->vpn_id.fib_id; |
| 1027 | |
| 1028 | if (is_del) |
| 1029 | { |
| 1030 | if (old_oui == oui && |
| 1031 | old_fib_id == fib_id ) |
| 1032 | { |
| 1033 | pool_put(dm->vss, v); |
| 1034 | hash_unset (dm->vss_index_by_vrf_id, tbl_id); |
| 1035 | return 0; |
| 1036 | } |
| 1037 | else |
| 1038 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 1039 | } |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1040 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1041 | pool_put(dm->vss, v); |
| 1042 | hash_unset (dm->vss_index_by_vrf_id, tbl_id); |
| 1043 | } else if (is_del) |
| 1044 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1045 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1046 | pool_get (dm->vss, v); |
| 1047 | memset (v, ~0, sizeof (*v)); |
| 1048 | v->vpn_id.fib_id = fib_id; |
| 1049 | v->vpn_id.oui = oui; |
| 1050 | hash_set (dm->vss_index_by_vrf_id, tbl_id, v - dm->vss); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1051 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | static clib_error_t * |
| 1057 | dhcpv6_vss_command_fn (vlib_main_t * vm, |
| 1058 | unformat_input_t * input, |
| 1059 | vlib_cli_command_t * cmd) |
| 1060 | { |
| 1061 | int is_del = 0, got_new_vss=0; |
| 1062 | u32 oui=0; |
| 1063 | u32 fib_id=0, tbl_id=~0; |
| 1064 | |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1065 | while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1066 | { |
| 1067 | if (unformat (input, "oui %d", &oui)) |
| 1068 | got_new_vss = 1; |
| 1069 | else if (unformat (input, "vpn-id %d", &fib_id)) |
| 1070 | got_new_vss = 1; |
| 1071 | else if (unformat (input, "table %d", &tbl_id)) |
| 1072 | got_new_vss = 1; |
| 1073 | else if (unformat(input, "delete") || unformat(input, "del")) |
| 1074 | is_del = 1; |
| 1075 | else |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | if (tbl_id ==~0) |
| 1080 | return clib_error_return (0, "no table ID specified."); |
| 1081 | |
| 1082 | if (is_del || got_new_vss) |
| 1083 | { |
| 1084 | int rv; |
| 1085 | |
| 1086 | rv = dhcpv6_proxy_set_vss(tbl_id, oui, fib_id, is_del); |
| 1087 | switch (rv) |
| 1088 | { |
| 1089 | case 0: |
| 1090 | return 0; |
| 1091 | |
| 1092 | case VNET_API_ERROR_NO_SUCH_FIB: |
| 1093 | return clib_error_return (0, "vss info (oui:%d, vpn-id:%d) not found in table %d.", |
| 1094 | oui, fib_id, tbl_id); |
| 1095 | |
| 1096 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 1097 | return clib_error_return (0, "vss for table %d not found in pool.", |
| 1098 | tbl_id); |
| 1099 | |
| 1100 | default: |
| 1101 | return clib_error_return (0, "BUG: rv %d", rv); |
| 1102 | } |
| 1103 | } |
| 1104 | else |
| 1105 | return clib_error_return (0, "parse error`%U'", |
| 1106 | format_unformat_error, input); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1107 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = { |
| 1111 | .path = "set dhcpv6 vss", |
| 1112 | .short_help = "set dhcpv6 vss table <table-id> oui <oui> vpn-idx <vpn-idx>", |
| 1113 | .function = dhcpv6_vss_command_fn, |
| 1114 | }; |
| 1115 | |
| 1116 | static clib_error_t * |
| 1117 | dhcpv6_vss_show_command_fn (vlib_main_t * vm, |
| 1118 | unformat_input_t * input, |
| 1119 | vlib_cli_command_t * cmd) |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1120 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1121 | { |
| 1122 | dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main; |
| 1123 | dhcpv6_vss_info *v; |
| 1124 | u32 oui; |
| 1125 | u32 fib_id; |
| 1126 | u32 tbl_id; |
| 1127 | uword index; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1128 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1129 | vlib_cli_output (vm, "%=6s%=6s%=12s","Table", "OUI", "VPN ID"); |
| 1130 | hash_foreach (tbl_id, index, dm->vss_index_by_vrf_id, |
| 1131 | ({ |
| 1132 | v = pool_elt_at_index (dm->vss, index); |
| 1133 | oui = v->vpn_id.oui; |
| 1134 | fib_id = v->vpn_id.fib_id; |
| 1135 | vlib_cli_output (vm, "%=6d%=6d%=12d", |
| 1136 | tbl_id, oui, fib_id); |
| 1137 | })); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1138 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = { |
| 1143 | .path = "show dhcpv6 vss", |
| 1144 | .short_help = "show dhcpv6 VSS", |
| 1145 | .function = dhcpv6_vss_show_command_fn, |
| 1146 | }; |
| 1147 | |
| 1148 | static clib_error_t * |
| 1149 | dhcpv6_link_address_show_command_fn (vlib_main_t * vm, |
| 1150 | unformat_input_t * input, |
| 1151 | vlib_cli_command_t * cmd) |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1152 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1153 | { |
| 1154 | dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1155 | vnet_main_t *vnm = vnet_get_main(); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1156 | u32 sw_if_index0=0, sw_if_index; |
| 1157 | ip6_address_t *ia0; |
| 1158 | vnet_sw_interface_t *swif; |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1159 | |
| 1160 | while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1161 | { |
| 1162 | |
| 1163 | if (unformat(input, "%U", |
| 1164 | unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0)) |
| 1165 | { |
| 1166 | swif = vnet_get_sw_interface (vnm, sw_if_index0); |
| 1167 | sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ? |
| 1168 | swif->unnumbered_sw_if_index : sw_if_index0; |
| 1169 | ia0 = ip6_interface_first_address(&ip6_main, sw_if_index); |
| 1170 | if (ia0) |
| 1171 | { |
| 1172 | vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address"); |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1173 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1174 | vlib_cli_output (vm, "%=20U%=48U", |
| 1175 | format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index0, |
| 1176 | format_ip6_address, ia0); |
| 1177 | } else |
| 1178 | vlib_cli_output (vm, "%=34s%=20U", "No IPv6 address configured on", |
Wojciech Dec | 558d5eb | 2016-11-01 16:44:06 +0100 | [diff] [blame] | 1179 | format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1180 | } else |
| 1181 | break; |
| 1182 | } |
| 1183 | |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = { |
| 1188 | .path = "show dhcpv6 link-address interface", |
| 1189 | .short_help = "show dhcpv6 link-address interface <interface>", |
| 1190 | .function = dhcpv6_link_address_show_command_fn, |
| 1191 | }; |