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>
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
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")
36 #define _(s,n) DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s,
37 foreach_dhcpv6_proxy_to_server_input_next
39 DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
40 } dhcpv6_proxy_to_server_input_next_t;
43 /* 0 => to server, 1 => to client */
48 u32 original_sw_if_index;
49 } dhcpv6_proxy_trace_t;
51 vlib_node_registration_t dhcpv6_proxy_to_server_node;
52 vlib_node_registration_t dhcpv6_proxy_to_client_node;
55 u8 * format_dhcpv6_proxy_trace (u8 * s, va_list * args)
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 *);
62 s = format (s, "DHCPV6 proxy: sent to server %U",
63 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
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]);
70 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
71 t->original_sw_if_index, t->sw_if_index);
76 u8 * format_dhcpv6_proxy_header_with_length (u8 * s, va_list * args)
78 dhcpv6_header_t * h = va_arg (*args, dhcpv6_header_t *);
79 u32 max_header_bytes = va_arg (*args, u32);
82 header_bytes = sizeof (h[0]);
83 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
84 return format (s, "dhcpv6 header truncated");
86 s = format (s, "DHCPV6 Proxy");
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)
94 ip_lookup_main_t * lm = &im->lookup_main;
95 ip_interface_address_t * ia = 0;
96 ip6_address_t * result = 0;
98 foreach_ip_interface_address (lm, ia, sw_if_index,
99 1 /* honor unnumbered */,
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) {
111 /* get first interface address */
112 static ip6_address_t *
113 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
115 ip_lookup_main_t * lm = &im->lookup_main;
116 ip_interface_address_t * ia = 0;
117 ip6_address_t * result = 0;
119 foreach_ip_interface_address (lm, ia, sw_if_index,
120 1 /* honor unnumbered */,
122 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
129 static inline void copy_ip6_address (ip6_address_t *dst, ip6_address_t *src)
132 dst->as_u64[0] = src->as_u64[0];
133 dst->as_u64[1] = src->as_u64[1];
137 dhcpv6_proxy_to_server_input (vlib_main_t * vm,
138 vlib_node_runtime_t * node,
139 vlib_frame_t * from_frame)
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;
145 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
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;
150 ip6_main_t * im = &ip6_main;
154 dhcpv6_server_t * server;
155 u32 rx_fib_idx = 0, server_fib_idx = 0;
159 next_index = node->cached_next_index;
161 while (n_left_from > 0)
165 vlib_get_next_frame (vm, node, next_index,
166 to_next, n_left_to_next);
168 while (n_left_from > 0 && n_left_to_next > 0)
170 vnet_main_t *vnm = vnet_get_main();
172 u32 rx_sw_if_index = 0;
173 vnet_sw_interface_t *swif;
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;
181 u32 error0 = (u32) ~0;
182 dhcpv6_option_t *fwd_opt;
183 dhcpv6_relay_hdr_t *r1;
185 dhcpv6_int_id_t *id1;
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;
194 dhcpv6_vss_info *vss;
204 b0 = vlib_get_buffer (vm, bi0);
206 h0 = vlib_buffer_get_current (b0);
209 * udp_local hands us the DHCPV6 header.
211 u0 = (void *)h0 -(sizeof(*u0));
212 ip0 = (void *)u0 -(sizeof(*ip0));
213 e_h0 = (void *)ip0 - ethernet_buffer_header_size(b0);
215 clib_memcpy(client_src_mac, e_h0->src_address, 6);
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:
229 case DHCPV6_MSG_RELAY_REPL:
231 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
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;
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];
248 if (vec_len(dpm->dhcp6_server_index_by_rx_fib_index) <= rx_fib_idx)
251 server_idx = dpm->dhcp6_server_index_by_rx_fib_index[rx_fib_idx];
253 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp6_servers,
257 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
258 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
263 server = pool_elt_at_index(dpm->dhcp6_servers, server_idx);
264 if (server->valid == 0)
267 server_fib_idx = server->server_fib6_index;
268 vnet_buffer(b0)->sw_if_index[VLIB_TX] = server_fib_idx;
271 /* relay-option header pointer */
272 vlib_buffer_advance(b0, -(sizeof(*fwd_opt)));
273 fwd_opt = vlib_buffer_get_current(b0);
274 /* relay message header pointer */
275 vlib_buffer_advance(b0, -(sizeof(*r1)));
276 r1 = vlib_buffer_get_current(b0);
278 vlib_buffer_advance(b0, -(sizeof(*u1)));
279 u1 = vlib_buffer_get_current(b0);
281 vlib_buffer_advance(b0, -(sizeof(*ip1)));
282 ip1 = vlib_buffer_get_current(b0);
284 /* fill in all that rubbish... */
285 len = clib_net_to_host_u16(u0->length) - sizeof(udp_header_t);
286 copy_ip6_address(&r1->peer_addr, &ip0->src_address);
288 r1->msg_type = DHCPV6_MSG_RELAY_FORW;
289 fwd_opt->length = clib_host_to_net_u16(len);
290 fwd_opt->option = clib_host_to_net_u16(DHCPV6_OPTION_RELAY_MSG);
293 r1->hop_count = (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
295 if (PREDICT_FALSE(r1->hop_count >= HOP_COUNT_LIMIT))
297 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
298 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
299 pkts_no_exceeding_max_hop++;
304 /* If relay-fwd and src address is site or global unicast address */
305 if (h0->u.msg_type == DHCPV6_MSG_RELAY_FORW &&
306 ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
307 (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
309 /* Set link address to zero */
310 r1->link_addr.as_u64[0] = 0;
311 r1->link_addr.as_u64[1] = 0;
312 goto link_address_set;
315 /* if receiving interface is unnumbered, use receiving interface
316 * IP address as link address, otherwise use the loopback interface
317 * IP address as link address.
320 swif = vnet_get_sw_interface (vnm, rx_sw_if_index);
321 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
322 sw_if_index = swif->unnumbered_sw_if_index;
324 ia0 = ip6_interface_first_global_or_site_address(&ip6_main, sw_if_index);
327 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
328 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
329 pkts_no_interface_address++;
333 copy_ip6_address(&r1->link_addr, ia0);
336 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
338 if ((b0->current_length+sizeof(*id1)+sizeof(*vss1)+sizeof(*cmac))
341 error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
342 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
347 id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
348 b0->current_length += (sizeof (*id1));
351 fib = ip6_fib_get (rx_fib_idx);
353 //TODO: Revisit if hash makes sense here
354 p_vss = hash_get (dpm->vss_index_by_vrf_id,
358 vss = pool_elt_at_index (dpm->vss, p_vss[0]);
359 oui1 = vss->vpn_id.oui;
360 fib_id1 = vss->vpn_id.fib_id;
363 id1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_INTERFACE_ID);
364 id1->opt.length = clib_host_to_net_u16(sizeof(rx_sw_if_index));
365 id1->int_idx = clib_host_to_net_u32(rx_sw_if_index);
368 if (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW)
370 cmac = (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
371 b0->current_length += (sizeof (*cmac));
372 cmac->opt.length =clib_host_to_net_u16(sizeof(*cmac) -
374 cmac->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
375 cmac->link_type = clib_host_to_net_u16(1); // ethernet
376 clib_memcpy(cmac->data, client_src_mac, 6);
377 u1->length += sizeof(*cmac);
379 if (server->insert_vss !=0 ) {
380 vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
381 b0->current_length += (sizeof (*vss1));
382 vss1->opt.length =clib_host_to_net_u16(sizeof(*vss1) -
384 vss1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_VSS);
385 vss1->data[0] = 1; // type
386 vss1->data[1] = oui1>>16 & 0xff;
387 vss1->data[2] = oui1>>8 & 0xff;
388 vss1->data[3] = oui1 & 0xff;
389 vss1->data[4] = fib_id1>>24 & 0xff;
390 vss1->data[5] = fib_id1>>16 & 0xff;
391 vss1->data[6] = fib_id1>>8 & 0xff;
392 vss1->data[7] = fib_id1 & 0xff;
393 u1->length += sizeof(*vss1);
398 u1->src_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_client);
399 u1->dst_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_server);
402 clib_host_to_net_u16( clib_net_to_host_u16(fwd_opt->length) +
403 sizeof(*r1) + sizeof(*fwd_opt) +
404 sizeof(*u1) + sizeof(*id1) + u1->length);
406 memset(ip1, 0, sizeof(*ip1));
407 ip1->ip_version_traffic_class_and_flow_label = 0x60;
408 ip1->payload_length = u1->length;
409 ip1->protocol = PROTO_UDP;
410 ip1->hop_limit = HOP_COUNT_LIMIT;
411 src = (server->dhcp6_server.as_u64[0] || server->dhcp6_server.as_u64[1]) ?
412 &server->dhcp6_server : &dpm->all_dhcpv6_server_address;
413 copy_ip6_address(&ip1->dst_address, src);
416 ia0 = ip6_interface_first_global_or_site_address
417 (&ip6_main, vnet_buffer(b0)->sw_if_index[VLIB_RX]);
419 src = (server->dhcp6_src_address.as_u64[0] || server->dhcp6_src_address.as_u64[1]) ?
420 &server->dhcp6_src_address : ia0;
423 error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS;
424 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
425 pkts_no_src_address++;
429 copy_ip6_address (&ip1->src_address, src);
432 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
434 ASSERT(bogus_length == 0);
436 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
439 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
441 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
443 tr->which = 0; /* to server */
445 tr->original_sw_if_index = rx_sw_if_index;
446 tr->sw_if_index = sw_if_index;
447 if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)
448 copy_ip6_address((ip6_address_t *)&tr->packet_data[0], &server->dhcp6_server);
452 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
453 to_next, n_left_to_next,
457 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
460 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
461 DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
463 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
464 DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
466 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
467 DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
468 pkts_no_interface_address);
469 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
470 DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
471 pkts_wrong_msg_type);
472 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
473 DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
474 pkts_no_src_address);
475 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
476 DHCPV6_PROXY_ERROR_PKT_TOO_BIG,
478 return from_frame->n_vectors;
481 VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node) = {
482 .function = dhcpv6_proxy_to_server_input,
483 .name = "dhcpv6-proxy-to-server",
484 /* Takes a vector of packets. */
485 .vector_size = sizeof (u32),
487 .n_errors = DHCPV6_PROXY_N_ERROR,
488 .error_strings = dhcpv6_proxy_error_strings,
490 .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
492 #define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
493 foreach_dhcpv6_proxy_to_server_input_next
497 .format_buffer = format_dhcpv6_proxy_header_with_length,
498 .format_trace = format_dhcpv6_proxy_trace,
500 .unformat_buffer = unformat_dhcpv6_proxy_header,
505 dhcpv6_proxy_to_client_input (vlib_main_t * vm,
506 vlib_node_runtime_t * node,
507 vlib_frame_t * from_frame)
510 u32 n_left_from, * from;
511 ethernet_main_t *em = ethernet_get_main (vm);
512 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
513 dhcpv6_server_t * server;
514 vnet_main_t * vnm = vnet_get_main();
517 from = vlib_frame_vector_args (from_frame);
518 n_left_from = from_frame->n_vectors;
520 while (n_left_from > 0)
524 udp_header_t * u0, *u1=0;
525 dhcpv6_relay_hdr_t * h0;
526 ip6_header_t * ip1 = 0, *ip0;
527 ip6_address_t _ia0, * ia0 = &_ia0;
528 ip6_address_t client_address;
529 ethernet_interface_t *ei0;
530 ethernet_header_t *mac0;
531 vnet_hw_interface_t *hi0;
534 u32 sw_if_index = ~0;
535 u32 original_sw_if_index = ~0;
536 vnet_sw_interface_t *si0;
537 u32 error0 = (u32)~0;
538 vnet_sw_interface_t *swif;
539 dhcpv6_option_t *r0 = 0, *o;
541 u8 interface_opt_flag = 0;
542 u8 relay_msg_opt_flag = 0;
544 ip6_main_t * im = &ip6_main;
545 u32 server_fib_idx, svr_fib_id, client_fib_idx, server_idx;
551 b0 = vlib_get_buffer (vm, bi0);
552 h0 = vlib_buffer_get_current (b0);
554 if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
556 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
559 vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
562 f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index);
563 to_next0 = vlib_frame_vector_args (f0);
566 vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0);
569 /* hop count seems not need to be checked */
570 if (HOP_COUNT_LIMIT < h0->hop_count)
572 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
575 u0 = (void *)h0 -(sizeof(*u0));
576 ip0 = (void *)u0 -(sizeof(*ip0));
578 vlib_buffer_advance (b0, sizeof(*h0));
579 o = vlib_buffer_get_current (b0);
581 /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID)
582 _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there.
583 Currently assuming no other options need to be processed
584 The interface-ID is the FIB number we need
585 to track down the client-facing interface */
587 while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
589 if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16(o->option))
591 interface_opt_flag = 1;
592 if (clib_net_to_host_u16(o->length) == sizeof(sw_if_index))
593 sw_if_index = clib_net_to_host_u32(((dhcpv6_int_id_t*)o)->int_idx);
594 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
596 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
600 if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16(o->option))
602 relay_msg_opt_flag = 1;
603 r0 = vlib_buffer_get_current (b0);
605 if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1))
607 vlib_buffer_advance (b0, sizeof(*o) + clib_net_to_host_u16(o->length));
608 o = (dhcpv6_option_t *) (((uword) o) + clib_net_to_host_u16(o->length) + sizeof(*o));
611 if ((relay_msg_opt_flag == 0) || (r0 == 0))
613 error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION;
617 if ((u32)~0 == sw_if_index)
619 error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
623 //Advance buffer to start of encapsulated DHCPv6 message
624 vlib_buffer_advance (b0, sizeof(*r0));
626 client_fib_idx = im->fib_index_by_sw_if_index[sw_if_index];
627 if (client_fib_idx < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
628 server_idx = dm->dhcp6_server_index_by_rx_fib_index[client_fib_idx];
632 if (PREDICT_FALSE (pool_is_free_index (dm->dhcp6_servers, server_idx)))
634 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
638 server = pool_elt_at_index (dm->dhcp6_servers, server_idx);
639 if (server->valid == 0)
641 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
646 server_fib_idx = im->fib_index_by_sw_if_index
647 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
648 svr_fib = ip6_fib_get (server_fib_idx);
649 svr_fib_id = svr_fib->table_id;
651 if (svr_fib_id != server->server_fib6_index ||
652 ip0->src_address.as_u64[0] != server->dhcp6_server.as_u64[0] ||
653 ip0->src_address.as_u64[1] != server->dhcp6_server.as_u64[1])
655 //drop packet if not from server with configured address or FIB
656 error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
660 vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index
663 swif = vnet_get_sw_interface (vnm, original_sw_if_index);
664 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
665 sw_if_index = swif->unnumbered_sw_if_index;
669 * udp_local hands us the DHCPV6 header, need udp hdr,
670 * ip hdr to relay to client
672 vlib_buffer_advance (b0, -(sizeof(*u1)));
673 u1 = vlib_buffer_get_current (b0);
675 vlib_buffer_advance (b0, -(sizeof(*ip1)));
676 ip1 = vlib_buffer_get_current (b0);
678 copy_ip6_address(&client_address, &h0->peer_addr);
680 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
683 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
687 len = clib_net_to_host_u16(r0->length);
688 memset(ip1, 0, sizeof(*ip1));
689 copy_ip6_address(&ip1->dst_address, &client_address);
691 u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);
692 u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
693 u1->length = clib_host_to_net_u16 (len + sizeof(udp_header_t));
695 ip1->ip_version_traffic_class_and_flow_label =
696 ip0->ip_version_traffic_class_and_flow_label &
698 ip1->payload_length = u1->length;
699 ip1->protocol = PROTO_UDP;
700 ip1->hop_limit = HOP_COUNT_LIMIT;
701 copy_ip6_address(&ip1->src_address, ia0);
703 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
705 ASSERT(bogus_length == 0);
707 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
708 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
709 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
710 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
712 mac0 = vlib_buffer_get_current (b0);
714 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
715 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
716 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
717 memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
718 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
719 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x86dd);
721 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
723 u32 * vlan_tag = (u32 *)(mac0+1);
725 tmp = (si0->sub.id << 16) | 0x0800;
726 *vlan_tag = clib_host_to_net_u32 (tmp);
729 /* $$$ consider adding a dynamic next to the graph node, for performance */
730 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
731 to_next0 = vlib_frame_vector_args (f0);
734 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
737 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
739 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
741 tr->which = 1; /* to client */
743 copy_ip6_address((ip6_address_t*)tr->packet_data, ia0);
745 tr->original_sw_if_index = original_sw_if_index;
746 tr->sw_if_index = sw_if_index;
749 return from_frame->n_vectors;
753 VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node) = {
754 .function = dhcpv6_proxy_to_client_input,
755 .name = "dhcpv6-proxy-to-client",
756 /* Takes a vector of packets. */
757 .vector_size = sizeof (u32),
759 .n_errors = DHCPV6_PROXY_N_ERROR,
760 .error_strings = dhcpv6_proxy_error_strings,
761 .format_buffer = format_dhcpv6_proxy_header_with_length,
762 .format_trace = format_dhcpv6_proxy_trace,
764 .unformat_buffer = unformat_dhcpv6_proxy_header,
768 clib_error_t * dhcpv6_proxy_init (vlib_main_t * vm)
770 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
771 vlib_node_t * error_drop_node;
772 dhcpv6_server_t * server;
775 dm->vnet_main = vnet_get_main();
776 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
777 dm->error_drop_node_index = error_drop_node->index;
779 dm->vss_index_by_vrf_id = hash_create (0, sizeof (uword));
781 /* RFC says this is the dhcpv6 server address */
782 dm->all_dhcpv6_server_address.as_u64[0] = clib_host_to_net_u64 (0xFF05000000000000);
783 dm->all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
785 /* RFC says this is the server and agent address */
786 dm->all_dhcpv6_server_relay_agent_address.as_u64[0] = clib_host_to_net_u64 (0xFF02000000000000);
787 dm->all_dhcpv6_server_relay_agent_address.as_u64[1] = clib_host_to_net_u64 (0x00010002);
789 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
790 dhcpv6_proxy_to_client_node.index, 0 /* is_ip6 */);
792 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
793 dhcpv6_proxy_to_server_node.index, 0 /* is_ip6 */);
795 /* Create the default server, don't mark it valid */
796 pool_get (dm->dhcp6_servers, server);
797 memset (server, 0, sizeof (*server));
802 VLIB_INIT_FUNCTION (dhcpv6_proxy_init);
804 /* Old API, manipulates a single server (only) shared by all Rx VRFs */
805 int dhcpv6_proxy_set_server (ip6_address_t *addr, ip6_address_t *src_address,
806 u32 fib_id, int insert_vss, int is_del)
808 return dhcpv6_proxy_set_server_2 (addr, src_address,
813 int dhcpv6_proxy_set_server_2 (ip6_address_t *addr, ip6_address_t *src_address,
814 u32 rx_fib_id, u32 server_fib_id,
815 int insert_vss, int is_del)
817 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
818 dhcpv6_server_t * server = 0;
819 u32 server_fib_index = 0;
820 u32 rx_fib_index = 0;
822 rx_fib_index = ip6_fib_table_find_or_create_and_lock(rx_fib_id);
823 server_fib_index = ip6_fib_table_find_or_create_and_lock(server_fib_id);
828 if (rx_fib_index >= vec_len(dm->dhcp6_server_index_by_rx_fib_index))
829 return VNET_API_ERROR_NO_SUCH_ENTRY;
831 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
833 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] = 0;
834 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
835 memset (server, 0, sizeof (*server));
836 pool_put (dm->dhcp6_servers, server);
840 if (addr->as_u64[0] == 0 &&
841 addr->as_u64[1] == 0 )
842 return VNET_API_ERROR_INVALID_DST_ADDRESS;
844 if (src_address->as_u64[0] == 0 &&
845 src_address->as_u64[1] == 0)
846 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
850 server = pool_elt_at_index (dm->dhcp6_servers, 0);
855 if (rx_fib_index < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
857 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
858 if (server_fib_index != 0)
860 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
865 /*Allocate a new server*/
866 pool_get (dm->dhcp6_servers, server);
870 copy_ip6_address(&server->dhcp6_server, addr);
871 copy_ip6_address(&server->dhcp6_src_address, src_address);
872 server->server_fib6_index = server_fib_index;
874 server->insert_vss = insert_vss;
876 vec_validate (dm->dhcp6_server_index_by_rx_fib_index, rx_fib_index);
877 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] =
878 server - dm->dhcp6_servers;
883 static clib_error_t *
884 dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
885 unformat_input_t * input,
886 vlib_cli_command_t * cmd)
888 ip6_address_t addr, src_addr;
889 int set_server = 0, set_src_address = 0;
890 u32 rx_fib_id = 0, server_fib_id = 0;
891 int is_del = 0, add_vss = 0;
893 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
895 if (unformat (input, "server %U",
896 unformat_ip6_address, &addr))
898 else if (unformat(input, "src-address %U",
899 unformat_ip6_address, &src_addr))
901 else if (unformat (input, "server-fib-id %d", &server_fib_id))
903 else if (unformat (input, "rx-fib-id %d", &rx_fib_id))
905 else if (unformat (input, "add-vss-option")
906 || unformat (input, "insert-option"))
908 else if (unformat (input, "delete") ||
909 unformat (input, "del"))
915 if (is_del || (set_server && set_src_address))
919 rv = dhcpv6_proxy_set_server_2 (&addr, &src_addr, rx_fib_id,
920 server_fib_id, add_vss, is_del);
922 //TODO: Complete the errors
929 return clib_error_return (0, "FIB id %d does not exist", server_fib_id);
932 return clib_error_return (0, "BUG: rv %d", rv);
936 return clib_error_return (0, "parse error`%U'",
937 format_unformat_error, input);
940 VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
941 .path = "set dhcpv6 proxy",
942 .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> "
943 "[add-vss-option] [server-fib-id <fib-id>] [rx-fib-id <fib-id>] ",
944 .function = dhcpv6_proxy_set_command_fn,
947 u8 * format_dhcpv6_proxy_server (u8 * s, va_list * args)
949 dhcpv6_proxy_main_t * dm = va_arg (*args, dhcpv6_proxy_main_t *);
950 dhcpv6_server_t * server = va_arg (*args, dhcpv6_server_t *);
951 u32 rx_fib_index = va_arg (*args, u32);
952 ip6_fib_t * rx_fib, * server_fib;
953 u32 server_fib_id = (u32)~0, rx_fib_id = ~0;
957 s = format (s, "%=40s%=40s%=14s%=14s%=20s", "Server Address", "Source Address",
958 "Server FIB", "RX FIB", "Insert VSS Option");
962 server_fib = ip6_fib_get(server->server_fib6_index);
964 server_fib_id= server_fib->table_id;
966 rx_fib= ip6_fib_get(rx_fib_index);
969 rx_fib_id = rx_fib->table_id;
971 s = format (s, "%=40U%=40U%=14u%=14u%=20s",
972 format_ip6_address, &server->dhcp6_server,
973 format_ip6_address, &server->dhcp6_src_address,
974 server_fib_id, rx_fib_id,
975 server->insert_vss ? "yes" : "no");
979 static clib_error_t *
980 dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
981 unformat_input_t * input,
982 vlib_cli_command_t * cmd)
984 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
985 ip6_main_t * im = &ip6_main;
988 dhcpv6_server_t * server;
990 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, 0 /* header line */,
992 for (i = 0; i < vec_len (im->fibs); i++)
994 if (i < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
995 server_index = dm->dhcp6_server_index_by_rx_fib_index[i];
998 server = pool_elt_at_index (dm->dhcp6_servers, server_index);
1000 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, dm,
1006 VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
1007 .path = "show dhcpv6 proxy",
1008 .short_help = "Display dhcpv6 proxy info",
1009 .function = dhcpv6_proxy_show_command_fn,
1012 int dhcpv6_proxy_set_vss(u32 tbl_id,
1017 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1018 u32 old_oui, old_fib_id;
1022 p = hash_get (dm->vss_index_by_vrf_id, tbl_id);
1025 v = pool_elt_at_index (dm->vss, p[0]);
1027 return VNET_API_ERROR_NO_SUCH_FIB;
1029 old_oui = v->vpn_id.oui;
1030 old_fib_id = v->vpn_id.fib_id;
1034 if (old_oui == oui &&
1035 old_fib_id == fib_id )
1037 pool_put(dm->vss, v);
1038 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1042 return VNET_API_ERROR_NO_SUCH_ENTRY;
1045 pool_put(dm->vss, v);
1046 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1048 return VNET_API_ERROR_NO_SUCH_ENTRY;
1050 pool_get (dm->vss, v);
1051 memset (v, ~0, sizeof (*v));
1052 v->vpn_id.fib_id = fib_id;
1053 v->vpn_id.oui = oui;
1054 hash_set (dm->vss_index_by_vrf_id, tbl_id, v - dm->vss);
1060 static clib_error_t *
1061 dhcpv6_vss_command_fn (vlib_main_t * vm,
1062 unformat_input_t * input,
1063 vlib_cli_command_t * cmd)
1065 int is_del = 0, got_new_vss=0;
1067 u32 fib_id=0, tbl_id=~0;
1069 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1071 if (unformat (input, "oui %d", &oui))
1073 else if (unformat (input, "vpn-id %d", &fib_id))
1075 else if (unformat (input, "table %d", &tbl_id))
1077 else if (unformat(input, "delete") || unformat(input, "del"))
1084 return clib_error_return (0, "no table ID specified.");
1086 if (is_del || got_new_vss)
1090 rv = dhcpv6_proxy_set_vss(tbl_id, oui, fib_id, is_del);
1096 case VNET_API_ERROR_NO_SUCH_FIB:
1097 return clib_error_return (0, "vss info (oui:%d, vpn-id:%d) not found in table %d.",
1098 oui, fib_id, tbl_id);
1100 case VNET_API_ERROR_NO_SUCH_ENTRY:
1101 return clib_error_return (0, "vss for table %d not found in pool.",
1105 return clib_error_return (0, "BUG: rv %d", rv);
1109 return clib_error_return (0, "parse error`%U'",
1110 format_unformat_error, input);
1114 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
1115 .path = "set dhcpv6 vss",
1116 .short_help = "set dhcpv6 vss table <table-id> oui <oui> vpn-idx <vpn-idx>",
1117 .function = dhcpv6_vss_command_fn,
1120 static clib_error_t *
1121 dhcpv6_vss_show_command_fn (vlib_main_t * vm,
1122 unformat_input_t * input,
1123 vlib_cli_command_t * cmd)
1126 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
1133 vlib_cli_output (vm, "%=6s%=6s%=12s","Table", "OUI", "VPN ID");
1134 hash_foreach (tbl_id, index, dm->vss_index_by_vrf_id,
1136 v = pool_elt_at_index (dm->vss, index);
1137 oui = v->vpn_id.oui;
1138 fib_id = v->vpn_id.fib_id;
1139 vlib_cli_output (vm, "%=6d%=6d%=12d",
1140 tbl_id, oui, fib_id);
1146 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
1147 .path = "show dhcpv6 vss",
1148 .short_help = "show dhcpv6 VSS",
1149 .function = dhcpv6_vss_show_command_fn,
1152 static clib_error_t *
1153 dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1154 unformat_input_t * input,
1155 vlib_cli_command_t * cmd)
1158 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1159 vnet_main_t *vnm = vnet_get_main();
1160 u32 sw_if_index0=0, sw_if_index;
1162 vnet_sw_interface_t *swif;
1164 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1167 if (unformat(input, "%U",
1168 unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1170 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1171 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1172 swif->unnumbered_sw_if_index : sw_if_index0;
1173 ia0 = ip6_interface_first_address(&ip6_main, sw_if_index);
1176 vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
1178 vlib_cli_output (vm, "%=20U%=48U",
1179 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index0,
1180 format_ip6_address, ia0);
1182 vlib_cli_output (vm, "%=34s%=20U", "No IPv6 address configured on",
1183 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index);
1191 VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1192 .path = "show dhcpv6 link-address interface",
1193 .short_help = "show dhcpv6 link-address interface <interface>",
1194 .function = dhcpv6_link_address_show_command_fn,