2 * proxy_node.c: dhcpv6 proxy node processing
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/dhcpv6/proxy.h>
21 #include <vnet/fib/ip6_fib.h>
22 #include <vnet/mfib/mfib_table.h>
23 #include <vnet/mfib/ip6_mfib.h>
25 static char * dhcpv6_proxy_error_strings[] = {
26 #define dhcpv6_proxy_error(n,s) s,
27 #include "proxy_error.def"
28 #undef dhcpv6_proxy_error
31 #define foreach_dhcpv6_proxy_to_server_input_next \
32 _ (DROP, "error-drop") \
33 _ (LOOKUP, "ip6-lookup") \
34 _ (SEND_TO_CLIENT, "dhcpv6-proxy-to-client")
38 #define _(s,n) DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s,
39 foreach_dhcpv6_proxy_to_server_input_next
41 DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
42 } dhcpv6_proxy_to_server_input_next_t;
45 /* 0 => to server, 1 => to client */
50 u32 original_sw_if_index;
51 } dhcpv6_proxy_trace_t;
53 vlib_node_registration_t dhcpv6_proxy_to_server_node;
54 vlib_node_registration_t dhcpv6_proxy_to_client_node;
57 u8 * format_dhcpv6_proxy_trace (u8 * s, va_list * args)
59 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61 dhcpv6_proxy_trace_t * t = va_arg (*args, dhcpv6_proxy_trace_t *);
64 s = format (s, "DHCPV6 proxy: sent to server %U",
65 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
67 s = format (s, "DHCPV6 proxy: sent to client from %U",
68 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
69 if (t->error != (u32)~0)
70 s = format (s, " error: %s\n", dhcpv6_proxy_error_strings[t->error]);
72 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
73 t->original_sw_if_index, t->sw_if_index);
78 u8 * format_dhcpv6_proxy_header_with_length (u8 * s, va_list * args)
80 dhcpv6_header_t * h = va_arg (*args, dhcpv6_header_t *);
81 u32 max_header_bytes = va_arg (*args, u32);
84 header_bytes = sizeof (h[0]);
85 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
86 return format (s, "dhcpv6 header truncated");
88 s = format (s, "DHCPV6 Proxy");
92 /* get first interface address */
93 static ip6_address_t *
94 ip6_interface_first_global_or_site_address (ip6_main_t * im, u32 sw_if_index)
96 ip_lookup_main_t * lm = &im->lookup_main;
97 ip_interface_address_t * ia = 0;
98 ip6_address_t * result = 0;
100 foreach_ip_interface_address (lm, ia, sw_if_index,
101 1 /* honor unnumbered */,
103 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
104 if ((a->as_u8[0] & 0xe0) == 0x20 ||
105 (a->as_u8[0] & 0xfe) == 0xfc) {
113 /* get first interface address */
114 static ip6_address_t *
115 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
117 ip_lookup_main_t * lm = &im->lookup_main;
118 ip_interface_address_t * ia = 0;
119 ip6_address_t * result = 0;
121 foreach_ip_interface_address (lm, ia, sw_if_index,
122 1 /* honor unnumbered */,
124 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
131 static inline void copy_ip6_address (ip6_address_t *dst, ip6_address_t *src)
134 dst->as_u64[0] = src->as_u64[0];
135 dst->as_u64[1] = src->as_u64[1];
139 dhcpv6_proxy_to_server_input (vlib_main_t * vm,
140 vlib_node_runtime_t * node,
141 vlib_frame_t * from_frame)
143 u32 n_left_from, next_index, * from, * to_next;
144 dhcpv6_proxy_main_t * dpm = &dhcpv6_proxy_main;
145 from = vlib_frame_vector_args (from_frame);
146 n_left_from = from_frame->n_vectors;
147 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
148 u32 pkts_no_interface_address=0, pkts_no_exceeding_max_hop=0;
149 u32 pkts_no_src_address=0;
150 u32 pkts_wrong_msg_type=0;
152 ip6_main_t * im = &ip6_main;
156 dhcpv6_server_t * server;
157 u32 rx_fib_idx = 0, server_fib_idx = 0;
161 next_index = node->cached_next_index;
163 while (n_left_from > 0)
167 vlib_get_next_frame (vm, node, next_index,
168 to_next, n_left_to_next);
170 while (n_left_from > 0 && n_left_to_next > 0)
172 vnet_main_t *vnm = vnet_get_main();
174 u32 rx_sw_if_index = 0;
175 vnet_sw_interface_t *swif;
178 udp_header_t * u0, *u1;
179 dhcpv6_header_t * h0; // client msg hdr
180 ip6_header_t * ip0, *ip1;
181 ip6_address_t _ia0, *ia0=&_ia0;
183 u32 error0 = (u32) ~0;
184 dhcpv6_option_t *fwd_opt;
185 dhcpv6_relay_hdr_t *r1;
187 dhcpv6_int_id_t *id1;
189 dhcpv6_client_mac_t *cmac; // client mac
190 ethernet_header_t * e_h0;
191 u8 client_src_mac[6];
192 vlib_buffer_free_list_t *fl;
196 dhcpv6_vss_info *vss;
206 b0 = vlib_get_buffer (vm, bi0);
208 h0 = vlib_buffer_get_current (b0);
211 * udp_local hands us the DHCPV6 header.
213 u0 = (void *)h0 -(sizeof(*u0));
214 ip0 = (void *)u0 -(sizeof(*ip0));
215 e_h0 = (void *)ip0 - ethernet_buffer_header_size(b0);
217 clib_memcpy(client_src_mac, e_h0->src_address, 6);
219 switch (h0->u.msg_type) {
220 case DHCPV6_MSG_SOLICIT:
221 case DHCPV6_MSG_REQUEST:
222 case DHCPV6_MSG_CONFIRM:
223 case DHCPV6_MSG_RENEW:
224 case DHCPV6_MSG_REBIND:
225 case DHCPV6_MSG_RELEASE:
226 case DHCPV6_MSG_DECLINE:
227 case DHCPV6_MSG_INFORMATION_REQUEST:
228 case DHCPV6_MSG_RELAY_FORW:
231 case DHCPV6_MSG_RELAY_REPL:
233 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
238 /* drop the packet */
239 pkts_wrong_msg_type++;
240 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
241 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
246 /* Send to DHCPV6 server via the configured FIB */
247 rx_sw_if_index = sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
248 rx_fib_idx = im->fib_index_by_sw_if_index [rx_sw_if_index];
250 if (vec_len(dpm->dhcp6_server_index_by_rx_fib_index) <= rx_fib_idx)
253 server_idx = dpm->dhcp6_server_index_by_rx_fib_index[rx_fib_idx];
255 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp6_servers,
259 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
260 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
265 server = pool_elt_at_index(dpm->dhcp6_servers, server_idx);
266 if (server->valid == 0)
269 server_fib_idx = server->server_fib6_index;
270 vnet_buffer(b0)->sw_if_index[VLIB_TX] = server_fib_idx;
273 /* relay-option header pointer */
274 vlib_buffer_advance(b0, -(sizeof(*fwd_opt)));
275 fwd_opt = vlib_buffer_get_current(b0);
276 /* relay message header pointer */
277 vlib_buffer_advance(b0, -(sizeof(*r1)));
278 r1 = vlib_buffer_get_current(b0);
280 vlib_buffer_advance(b0, -(sizeof(*u1)));
281 u1 = vlib_buffer_get_current(b0);
283 vlib_buffer_advance(b0, -(sizeof(*ip1)));
284 ip1 = vlib_buffer_get_current(b0);
286 /* fill in all that rubbish... */
287 len = clib_net_to_host_u16(u0->length) - sizeof(udp_header_t);
288 copy_ip6_address(&r1->peer_addr, &ip0->src_address);
290 r1->msg_type = DHCPV6_MSG_RELAY_FORW;
291 fwd_opt->length = clib_host_to_net_u16(len);
292 fwd_opt->option = clib_host_to_net_u16(DHCPV6_OPTION_RELAY_MSG);
295 r1->hop_count = (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
297 if (PREDICT_FALSE(r1->hop_count >= HOP_COUNT_LIMIT))
299 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
300 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
301 pkts_no_exceeding_max_hop++;
306 /* If relay-fwd and src address is site or global unicast address */
307 if (h0->u.msg_type == DHCPV6_MSG_RELAY_FORW &&
308 ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
309 (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
311 /* Set link address to zero */
312 r1->link_addr.as_u64[0] = 0;
313 r1->link_addr.as_u64[1] = 0;
314 goto link_address_set;
317 /* if receiving interface is unnumbered, use receiving interface
318 * IP address as link address, otherwise use the loopback interface
319 * IP address as link address.
322 swif = vnet_get_sw_interface (vnm, rx_sw_if_index);
323 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
324 sw_if_index = swif->unnumbered_sw_if_index;
326 ia0 = ip6_interface_first_global_or_site_address(&ip6_main, sw_if_index);
329 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
330 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
331 pkts_no_interface_address++;
335 copy_ip6_address(&r1->link_addr, ia0);
338 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
340 if ((b0->current_length+sizeof(*id1)+sizeof(*vss1)+sizeof(*cmac))
343 error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
344 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
349 id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
350 b0->current_length += (sizeof (*id1));
353 fib = ip6_fib_get (rx_fib_idx);
355 //TODO: Revisit if hash makes sense here
356 p_vss = hash_get (dpm->vss_index_by_vrf_id,
360 vss = pool_elt_at_index (dpm->vss, p_vss[0]);
361 oui1 = vss->vpn_id.oui;
362 fib_id1 = vss->vpn_id.fib_id;
365 id1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_INTERFACE_ID);
366 id1->opt.length = clib_host_to_net_u16(sizeof(rx_sw_if_index));
367 id1->int_idx = clib_host_to_net_u32(rx_sw_if_index);
370 if (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW)
372 cmac = (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
373 b0->current_length += (sizeof (*cmac));
374 cmac->opt.length =clib_host_to_net_u16(sizeof(*cmac) -
376 cmac->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
377 cmac->link_type = clib_host_to_net_u16(1); // ethernet
378 clib_memcpy(cmac->data, client_src_mac, 6);
379 u1->length += sizeof(*cmac);
381 if (server->insert_vss !=0 ) {
382 vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
383 b0->current_length += (sizeof (*vss1));
384 vss1->opt.length =clib_host_to_net_u16(sizeof(*vss1) -
386 vss1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_VSS);
387 vss1->data[0] = 1; // type
388 vss1->data[1] = oui1>>16 & 0xff;
389 vss1->data[2] = oui1>>8 & 0xff;
390 vss1->data[3] = oui1 & 0xff;
391 vss1->data[4] = fib_id1>>24 & 0xff;
392 vss1->data[5] = fib_id1>>16 & 0xff;
393 vss1->data[6] = fib_id1>>8 & 0xff;
394 vss1->data[7] = fib_id1 & 0xff;
395 u1->length += sizeof(*vss1);
400 u1->src_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_client);
401 u1->dst_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_server);
404 clib_host_to_net_u16( clib_net_to_host_u16(fwd_opt->length) +
405 sizeof(*r1) + sizeof(*fwd_opt) +
406 sizeof(*u1) + sizeof(*id1) + u1->length);
408 memset(ip1, 0, sizeof(*ip1));
409 ip1->ip_version_traffic_class_and_flow_label = 0x60;
410 ip1->payload_length = u1->length;
411 ip1->protocol = PROTO_UDP;
412 ip1->hop_limit = HOP_COUNT_LIMIT;
413 src = (server->dhcp6_server.as_u64[0] || server->dhcp6_server.as_u64[1]) ?
414 &server->dhcp6_server : &dpm->all_dhcpv6_server_address;
415 copy_ip6_address(&ip1->dst_address, src);
418 ia0 = ip6_interface_first_global_or_site_address
419 (&ip6_main, vnet_buffer(b0)->sw_if_index[VLIB_RX]);
421 src = (server->dhcp6_src_address.as_u64[0] || server->dhcp6_src_address.as_u64[1]) ?
422 &server->dhcp6_src_address : ia0;
425 error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS;
426 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
427 pkts_no_src_address++;
431 copy_ip6_address (&ip1->src_address, src);
434 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
436 ASSERT(bogus_length == 0);
438 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
441 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
443 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
445 tr->which = 0; /* to server */
447 tr->original_sw_if_index = rx_sw_if_index;
448 tr->sw_if_index = sw_if_index;
449 if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)
450 copy_ip6_address((ip6_address_t *)&tr->packet_data[0], &server->dhcp6_server);
454 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
455 to_next, n_left_to_next,
459 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
462 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
463 DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
465 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
466 DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
468 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
469 DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
470 pkts_no_interface_address);
471 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
472 DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
473 pkts_wrong_msg_type);
474 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
475 DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
476 pkts_no_src_address);
477 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
478 DHCPV6_PROXY_ERROR_PKT_TOO_BIG,
480 return from_frame->n_vectors;
483 VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node) = {
484 .function = dhcpv6_proxy_to_server_input,
485 .name = "dhcpv6-proxy-to-server",
486 /* Takes a vector of packets. */
487 .vector_size = sizeof (u32),
489 .n_errors = DHCPV6_PROXY_N_ERROR,
490 .error_strings = dhcpv6_proxy_error_strings,
492 .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
494 #define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
495 foreach_dhcpv6_proxy_to_server_input_next
499 .format_buffer = format_dhcpv6_proxy_header_with_length,
500 .format_trace = format_dhcpv6_proxy_trace,
502 .unformat_buffer = unformat_dhcpv6_proxy_header,
507 dhcpv6_proxy_to_client_input (vlib_main_t * vm,
508 vlib_node_runtime_t * node,
509 vlib_frame_t * from_frame)
512 u32 n_left_from, * from;
513 ethernet_main_t *em = ethernet_get_main (vm);
514 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
515 dhcpv6_server_t * server;
516 vnet_main_t * vnm = vnet_get_main();
519 from = vlib_frame_vector_args (from_frame);
520 n_left_from = from_frame->n_vectors;
522 while (n_left_from > 0)
526 udp_header_t * u0, *u1=0;
527 dhcpv6_relay_hdr_t * h0;
528 ip6_header_t * ip1 = 0, *ip0;
529 ip6_address_t _ia0, * ia0 = &_ia0;
530 ip6_address_t client_address;
531 ethernet_interface_t *ei0;
532 ethernet_header_t *mac0;
533 vnet_hw_interface_t *hi0;
536 u32 sw_if_index = ~0;
537 u32 original_sw_if_index = ~0;
538 vnet_sw_interface_t *si0;
539 u32 error0 = (u32)~0;
540 vnet_sw_interface_t *swif;
541 dhcpv6_option_t *r0 = 0, *o;
543 u8 interface_opt_flag = 0;
544 u8 relay_msg_opt_flag = 0;
546 ip6_main_t * im = &ip6_main;
547 u32 server_fib_idx, svr_fib_id, client_fib_idx, server_idx;
553 b0 = vlib_get_buffer (vm, bi0);
554 h0 = vlib_buffer_get_current (b0);
556 if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
558 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
561 vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
564 f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index);
565 to_next0 = vlib_frame_vector_args (f0);
568 vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0);
571 /* hop count seems not need to be checked */
572 if (HOP_COUNT_LIMIT < h0->hop_count)
574 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
577 u0 = (void *)h0 -(sizeof(*u0));
578 ip0 = (void *)u0 -(sizeof(*ip0));
580 vlib_buffer_advance (b0, sizeof(*h0));
581 o = vlib_buffer_get_current (b0);
583 /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID)
584 _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there.
585 Currently assuming no other options need to be processed
586 The interface-ID is the FIB number we need
587 to track down the client-facing interface */
589 while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
591 if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16(o->option))
593 interface_opt_flag = 1;
594 if (clib_net_to_host_u16(o->length) == sizeof(sw_if_index))
595 sw_if_index = clib_net_to_host_u32(((dhcpv6_int_id_t*)o)->int_idx);
596 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
598 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
602 if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16(o->option))
604 relay_msg_opt_flag = 1;
605 r0 = vlib_buffer_get_current (b0);
607 if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1))
609 vlib_buffer_advance (b0, sizeof(*o) + clib_net_to_host_u16(o->length));
610 o = (dhcpv6_option_t *) (((uword) o) + clib_net_to_host_u16(o->length) + sizeof(*o));
613 if ((relay_msg_opt_flag == 0) || (r0 == 0))
615 error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION;
619 if ((u32)~0 == sw_if_index)
621 error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
625 //Advance buffer to start of encapsulated DHCPv6 message
626 vlib_buffer_advance (b0, sizeof(*r0));
628 client_fib_idx = im->fib_index_by_sw_if_index[sw_if_index];
629 if (client_fib_idx < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
630 server_idx = dm->dhcp6_server_index_by_rx_fib_index[client_fib_idx];
634 if (PREDICT_FALSE (pool_is_free_index (dm->dhcp6_servers, server_idx)))
636 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
640 server = pool_elt_at_index (dm->dhcp6_servers, server_idx);
641 if (server->valid == 0)
643 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
648 server_fib_idx = im->fib_index_by_sw_if_index
649 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
650 svr_fib = ip6_fib_get (server_fib_idx);
651 svr_fib_id = svr_fib->table_id;
653 if (svr_fib_id != server->server_fib6_index ||
654 ip0->src_address.as_u64[0] != server->dhcp6_server.as_u64[0] ||
655 ip0->src_address.as_u64[1] != server->dhcp6_server.as_u64[1])
657 //drop packet if not from server with configured address or FIB
658 error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
662 vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index
665 swif = vnet_get_sw_interface (vnm, original_sw_if_index);
666 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
667 sw_if_index = swif->unnumbered_sw_if_index;
671 * udp_local hands us the DHCPV6 header, need udp hdr,
672 * ip hdr to relay to client
674 vlib_buffer_advance (b0, -(sizeof(*u1)));
675 u1 = vlib_buffer_get_current (b0);
677 vlib_buffer_advance (b0, -(sizeof(*ip1)));
678 ip1 = vlib_buffer_get_current (b0);
680 copy_ip6_address(&client_address, &h0->peer_addr);
682 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
685 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
689 len = clib_net_to_host_u16(r0->length);
690 memset(ip1, 0, sizeof(*ip1));
691 copy_ip6_address(&ip1->dst_address, &client_address);
693 u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);
694 u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
695 u1->length = clib_host_to_net_u16 (len + sizeof(udp_header_t));
697 ip1->ip_version_traffic_class_and_flow_label =
698 ip0->ip_version_traffic_class_and_flow_label &
700 ip1->payload_length = u1->length;
701 ip1->protocol = PROTO_UDP;
702 ip1->hop_limit = HOP_COUNT_LIMIT;
703 copy_ip6_address(&ip1->src_address, ia0);
705 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
707 ASSERT(bogus_length == 0);
709 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
710 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
711 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
712 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
714 mac0 = vlib_buffer_get_current (b0);
716 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
717 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
718 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
719 memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
720 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
721 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x86dd);
723 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
725 u32 * vlan_tag = (u32 *)(mac0+1);
727 tmp = (si0->sub.id << 16) | 0x0800;
728 *vlan_tag = clib_host_to_net_u32 (tmp);
731 /* $$$ consider adding a dynamic next to the graph node, for performance */
732 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
733 to_next0 = vlib_frame_vector_args (f0);
736 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
739 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
741 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
743 tr->which = 1; /* to client */
745 copy_ip6_address((ip6_address_t*)tr->packet_data, ia0);
747 tr->original_sw_if_index = original_sw_if_index;
748 tr->sw_if_index = sw_if_index;
751 return from_frame->n_vectors;
755 VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node) = {
756 .function = dhcpv6_proxy_to_client_input,
757 .name = "dhcpv6-proxy-to-client",
758 /* Takes a vector of packets. */
759 .vector_size = sizeof (u32),
761 .n_errors = DHCPV6_PROXY_N_ERROR,
762 .error_strings = dhcpv6_proxy_error_strings,
763 .format_buffer = format_dhcpv6_proxy_header_with_length,
764 .format_trace = format_dhcpv6_proxy_trace,
766 .unformat_buffer = unformat_dhcpv6_proxy_header,
770 clib_error_t * dhcpv6_proxy_init (vlib_main_t * vm)
772 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
773 vlib_node_t * error_drop_node;
774 dhcpv6_server_t * server;
777 dm->vnet_main = vnet_get_main();
778 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
779 dm->error_drop_node_index = error_drop_node->index;
781 dm->vss_index_by_vrf_id = hash_create (0, sizeof (uword));
783 /* RFC says this is the dhcpv6 server address */
784 dm->all_dhcpv6_server_address.as_u64[0] = clib_host_to_net_u64 (0xFF05000000000000);
785 dm->all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
787 /* RFC says this is the server and agent address */
788 dm->all_dhcpv6_server_relay_agent_address.as_u64[0] = clib_host_to_net_u64 (0xFF02000000000000);
789 dm->all_dhcpv6_server_relay_agent_address.as_u64[1] = clib_host_to_net_u64 (0x00010002);
791 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
792 dhcpv6_proxy_to_client_node.index, 0 /* is_ip6 */);
794 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
795 dhcpv6_proxy_to_server_node.index, 0 /* is_ip6 */);
797 /* Create the default server, don't mark it valid */
798 pool_get (dm->dhcp6_servers, server);
799 memset (server, 0, sizeof (*server));
804 VLIB_INIT_FUNCTION (dhcpv6_proxy_init);
806 /* Old API, manipulates a single server (only) shared by all Rx VRFs */
807 int dhcpv6_proxy_set_server (ip6_address_t *addr, ip6_address_t *src_address,
808 u32 fib_id, int insert_vss, int is_del)
810 return dhcpv6_proxy_set_server_2 (addr, src_address,
815 int dhcpv6_proxy_set_server_2 (ip6_address_t *addr, ip6_address_t *src_address,
816 u32 rx_fib_id, u32 server_fib_id,
817 int insert_vss, int is_del)
819 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
820 dhcpv6_server_t * server = 0;
821 u32 server_fib_index = 0;
822 u32 rx_fib_index = 0;
824 rx_fib_index = ip6_mfib_table_find_or_create_and_lock(rx_fib_id);
825 server_fib_index = ip6_fib_table_find_or_create_and_lock(server_fib_id);
830 if (rx_fib_index >= vec_len(dm->dhcp6_server_index_by_rx_fib_index))
831 return VNET_API_ERROR_NO_SUCH_ENTRY;
833 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
835 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] = 0;
836 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
837 memset (server, 0, sizeof (*server));
838 pool_put (dm->dhcp6_servers, server);
842 if (addr->as_u64[0] == 0 &&
843 addr->as_u64[1] == 0 )
844 return VNET_API_ERROR_INVALID_DST_ADDRESS;
846 if (src_address->as_u64[0] == 0 &&
847 src_address->as_u64[1] == 0)
848 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
852 server = pool_elt_at_index (dm->dhcp6_servers, 0);
859 if (rx_fib_index < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
861 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
862 if (server_fib_index != 0)
864 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
869 /*Allocate a new server*/
870 pool_get (dm->dhcp6_servers, server);
874 const mfib_prefix_t all_dhcp_servers = {
876 .fp_proto = FIB_PROTOCOL_IP6,
878 .ip6 = dm->all_dhcpv6_server_relay_agent_address,
881 const fib_route_path_t path_for_us = {
882 .frp_proto = FIB_PROTOCOL_IP6,
883 .frp_addr = zero_addr,
884 .frp_sw_if_index = 0xffffffff,
887 .frp_flags = FIB_ROUTE_PATH_LOCAL,
889 mfib_table_entry_path_update(rx_fib_index,
893 MFIB_ITF_FLAG_FORWARD);
895 * Each interface that is enabled in this table, needs to be added
896 * as an accepting interface, but this is not easily doable in VPP.
897 * So we cheat. Add a flag to the entry that indicates accept form
899 * We will still only accept on v6 enabled interfaces, since the input
900 * feature ensures this.
902 mfib_table_entry_update(rx_fib_index,
905 MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF);
910 copy_ip6_address(&server->dhcp6_server, addr);
911 copy_ip6_address(&server->dhcp6_src_address, src_address);
912 server->server_fib6_index = server_fib_index;
914 server->insert_vss = insert_vss;
916 vec_validate (dm->dhcp6_server_index_by_rx_fib_index, rx_fib_index);
917 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] =
918 server - dm->dhcp6_servers;
923 static clib_error_t *
924 dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
925 unformat_input_t * input,
926 vlib_cli_command_t * cmd)
928 ip6_address_t addr, src_addr;
929 int set_server = 0, set_src_address = 0;
930 u32 rx_fib_id = 0, server_fib_id = 0;
931 int is_del = 0, add_vss = 0;
933 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
935 if (unformat (input, "server %U",
936 unformat_ip6_address, &addr))
938 else if (unformat(input, "src-address %U",
939 unformat_ip6_address, &src_addr))
941 else if (unformat (input, "server-fib-id %d", &server_fib_id))
943 else if (unformat (input, "rx-fib-id %d", &rx_fib_id))
945 else if (unformat (input, "add-vss-option")
946 || unformat (input, "insert-option"))
948 else if (unformat (input, "delete") ||
949 unformat (input, "del"))
955 if (is_del || (set_server && set_src_address))
959 rv = dhcpv6_proxy_set_server_2 (&addr, &src_addr, rx_fib_id,
960 server_fib_id, add_vss, is_del);
962 //TODO: Complete the errors
969 return clib_error_return (0, "FIB id %d does not exist", server_fib_id);
972 return clib_error_return (0, "BUG: rv %d", rv);
976 return clib_error_return (0, "parse error`%U'",
977 format_unformat_error, input);
980 VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
981 .path = "set dhcpv6 proxy",
982 .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> "
983 "[add-vss-option] [server-fib-id <fib-id>] [rx-fib-id <fib-id>] ",
984 .function = dhcpv6_proxy_set_command_fn,
987 u8 * format_dhcpv6_proxy_server (u8 * s, va_list * args)
989 dhcpv6_proxy_main_t * dm = va_arg (*args, dhcpv6_proxy_main_t *);
990 dhcpv6_server_t * server = va_arg (*args, dhcpv6_server_t *);
991 u32 rx_fib_index = va_arg (*args, u32);
992 ip6_fib_t * rx_fib, * server_fib;
993 u32 server_fib_id = (u32)~0, rx_fib_id = ~0;
997 s = format (s, "%=40s%=40s%=14s%=14s%=20s", "Server Address", "Source Address",
998 "Server FIB", "RX FIB", "Insert VSS Option");
1002 server_fib = ip6_fib_get(server->server_fib6_index);
1004 server_fib_id= server_fib->table_id;
1006 rx_fib= ip6_fib_get(rx_fib_index);
1009 rx_fib_id = rx_fib->table_id;
1011 s = format (s, "%=40U%=40U%=14u%=14u%=20s",
1012 format_ip6_address, &server->dhcp6_server,
1013 format_ip6_address, &server->dhcp6_src_address,
1014 server_fib_id, rx_fib_id,
1015 server->insert_vss ? "yes" : "no");
1019 static clib_error_t *
1020 dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
1021 unformat_input_t * input,
1022 vlib_cli_command_t * cmd)
1024 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
1025 ip6_main_t * im = &ip6_main;
1028 dhcpv6_server_t * server;
1030 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, 0 /* header line */,
1032 for (i = 0; i < vec_len (im->fibs); i++)
1034 if (i < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
1035 server_index = dm->dhcp6_server_index_by_rx_fib_index[i];
1038 server = pool_elt_at_index (dm->dhcp6_servers, server_index);
1040 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, dm,
1046 VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
1047 .path = "show dhcpv6 proxy",
1048 .short_help = "Display dhcpv6 proxy info",
1049 .function = dhcpv6_proxy_show_command_fn,
1052 int dhcpv6_proxy_set_vss(u32 tbl_id,
1057 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1058 u32 old_oui, old_fib_id;
1062 p = hash_get (dm->vss_index_by_vrf_id, tbl_id);
1065 v = pool_elt_at_index (dm->vss, p[0]);
1067 return VNET_API_ERROR_NO_SUCH_FIB;
1069 old_oui = v->vpn_id.oui;
1070 old_fib_id = v->vpn_id.fib_id;
1074 if (old_oui == oui &&
1075 old_fib_id == fib_id )
1077 pool_put(dm->vss, v);
1078 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1082 return VNET_API_ERROR_NO_SUCH_ENTRY;
1085 pool_put(dm->vss, v);
1086 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1088 return VNET_API_ERROR_NO_SUCH_ENTRY;
1090 pool_get (dm->vss, v);
1091 memset (v, ~0, sizeof (*v));
1092 v->vpn_id.fib_id = fib_id;
1093 v->vpn_id.oui = oui;
1094 hash_set (dm->vss_index_by_vrf_id, tbl_id, v - dm->vss);
1100 static clib_error_t *
1101 dhcpv6_vss_command_fn (vlib_main_t * vm,
1102 unformat_input_t * input,
1103 vlib_cli_command_t * cmd)
1105 int is_del = 0, got_new_vss=0;
1107 u32 fib_id=0, tbl_id=~0;
1109 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1111 if (unformat (input, "oui %d", &oui))
1113 else if (unformat (input, "vpn-id %d", &fib_id))
1115 else if (unformat (input, "table %d", &tbl_id))
1117 else if (unformat(input, "delete") || unformat(input, "del"))
1124 return clib_error_return (0, "no table ID specified.");
1126 if (is_del || got_new_vss)
1130 rv = dhcpv6_proxy_set_vss(tbl_id, oui, fib_id, is_del);
1136 case VNET_API_ERROR_NO_SUCH_FIB:
1137 return clib_error_return (0, "vss info (oui:%d, vpn-id:%d) not found in table %d.",
1138 oui, fib_id, tbl_id);
1140 case VNET_API_ERROR_NO_SUCH_ENTRY:
1141 return clib_error_return (0, "vss for table %d not found in pool.",
1145 return clib_error_return (0, "BUG: rv %d", rv);
1149 return clib_error_return (0, "parse error`%U'",
1150 format_unformat_error, input);
1154 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
1155 .path = "set dhcpv6 vss",
1156 .short_help = "set dhcpv6 vss table <table-id> oui <oui> vpn-idx <vpn-idx>",
1157 .function = dhcpv6_vss_command_fn,
1160 static clib_error_t *
1161 dhcpv6_vss_show_command_fn (vlib_main_t * vm,
1162 unformat_input_t * input,
1163 vlib_cli_command_t * cmd)
1166 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
1173 vlib_cli_output (vm, "%=6s%=6s%=12s","Table", "OUI", "VPN ID");
1174 hash_foreach (tbl_id, index, dm->vss_index_by_vrf_id,
1176 v = pool_elt_at_index (dm->vss, index);
1177 oui = v->vpn_id.oui;
1178 fib_id = v->vpn_id.fib_id;
1179 vlib_cli_output (vm, "%=6d%=6d%=12d",
1180 tbl_id, oui, fib_id);
1186 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
1187 .path = "show dhcpv6 vss",
1188 .short_help = "show dhcpv6 VSS",
1189 .function = dhcpv6_vss_show_command_fn,
1192 static clib_error_t *
1193 dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1194 unformat_input_t * input,
1195 vlib_cli_command_t * cmd)
1198 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1199 vnet_main_t *vnm = vnet_get_main();
1200 u32 sw_if_index0=0, sw_if_index;
1202 vnet_sw_interface_t *swif;
1204 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1207 if (unformat(input, "%U",
1208 unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1210 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1211 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1212 swif->unnumbered_sw_if_index : sw_if_index0;
1213 ia0 = ip6_interface_first_address(&ip6_main, sw_if_index);
1216 vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
1218 vlib_cli_output (vm, "%=20U%=48U",
1219 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index0,
1220 format_ip6_address, ia0);
1222 vlib_cli_output (vm, "%=34s%=20U", "No IPv6 address configured on",
1223 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index);
1231 VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1232 .path = "show dhcpv6 link-address interface",
1233 .short_help = "show dhcpv6 link-address interface <interface>",
1234 .function = dhcpv6_link_address_show_command_fn,