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 static inline void copy_ip6_address (ip6_address_t *dst, ip6_address_t *src)
116 dst->as_u64[0] = src->as_u64[0];
117 dst->as_u64[1] = src->as_u64[1];
121 dhcpv6_proxy_to_server_input (vlib_main_t * vm,
122 vlib_node_runtime_t * node,
123 vlib_frame_t * from_frame)
125 u32 n_left_from, next_index, * from, * to_next;
126 dhcpv6_proxy_main_t * dpm = &dhcpv6_proxy_main;
127 from = vlib_frame_vector_args (from_frame);
128 n_left_from = from_frame->n_vectors;
129 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
130 u32 pkts_no_interface_address=0, pkts_no_exceeding_max_hop=0;
131 u32 pkts_no_src_address=0;
132 u32 pkts_wrong_msg_type=0;
134 ip6_main_t * im = &ip6_main;
138 dhcpv6_server_t * server;
139 u32 rx_fib_idx = 0, server_fib_idx = 0;
143 next_index = node->cached_next_index;
145 while (n_left_from > 0)
149 vlib_get_next_frame (vm, node, next_index,
150 to_next, n_left_to_next);
152 while (n_left_from > 0 && n_left_to_next > 0)
154 vnet_main_t *vnm = vnet_get_main();
156 u32 rx_sw_if_index = 0;
157 vnet_sw_interface_t *swif;
160 udp_header_t * u0, *u1;
161 dhcpv6_header_t * h0; // client msg hdr
162 ip6_header_t * ip0, *ip1;
163 ip6_address_t _ia0, *ia0=&_ia0;
165 u32 error0 = (u32) ~0;
166 dhcpv6_option_t *fwd_opt;
167 dhcpv6_relay_hdr_t *r1;
169 dhcpv6_int_id_t *id1;
171 dhcpv6_client_mac_t *cmac; // client mac
172 ethernet_header_t * e_h0;
173 u8 client_src_mac[6];
174 vlib_buffer_free_list_t *fl;
178 dhcpv6_vss_info *vss;
188 b0 = vlib_get_buffer (vm, bi0);
190 h0 = vlib_buffer_get_current (b0);
193 * udp_local hands us the DHCPV6 header.
195 u0 = (void *)h0 -(sizeof(*u0));
196 ip0 = (void *)u0 -(sizeof(*ip0));
197 e_h0 = (void *)ip0 - ethernet_buffer_header_size(b0);
199 clib_memcpy(client_src_mac, e_h0->src_address, 6);
201 switch (h0->u.msg_type) {
202 case DHCPV6_MSG_SOLICIT:
203 case DHCPV6_MSG_REQUEST:
204 case DHCPV6_MSG_CONFIRM:
205 case DHCPV6_MSG_RENEW:
206 case DHCPV6_MSG_REBIND:
207 case DHCPV6_MSG_RELEASE:
208 case DHCPV6_MSG_DECLINE:
209 case DHCPV6_MSG_INFORMATION_REQUEST:
210 case DHCPV6_MSG_RELAY_FORW:
213 case DHCPV6_MSG_RELAY_REPL:
215 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
220 /* drop the packet */
221 pkts_wrong_msg_type++;
222 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
223 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
228 /* Send to DHCPV6 server via the configured FIB */
229 rx_sw_if_index = sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
230 rx_fib_idx = im->fib_index_by_sw_if_index [rx_sw_if_index];
232 if (vec_len(dpm->dhcp6_server_index_by_rx_fib_index) <= rx_fib_idx)
235 server_idx = dpm->dhcp6_server_index_by_rx_fib_index[rx_fib_idx];
237 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp6_servers,
241 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
242 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
247 server = pool_elt_at_index(dpm->dhcp6_servers, server_idx);
248 if (server->valid == 0)
251 server_fib_idx = server->server_fib6_index;
252 vnet_buffer(b0)->sw_if_index[VLIB_TX] = server_fib_idx;
255 /* relay-option header pointer */
256 vlib_buffer_advance(b0, -(sizeof(*fwd_opt)));
257 fwd_opt = vlib_buffer_get_current(b0);
258 /* relay message header pointer */
259 vlib_buffer_advance(b0, -(sizeof(*r1)));
260 r1 = vlib_buffer_get_current(b0);
262 vlib_buffer_advance(b0, -(sizeof(*u1)));
263 u1 = vlib_buffer_get_current(b0);
265 vlib_buffer_advance(b0, -(sizeof(*ip1)));
266 ip1 = vlib_buffer_get_current(b0);
268 /* fill in all that rubbish... */
269 len = clib_net_to_host_u16(u0->length) - sizeof(udp_header_t);
270 copy_ip6_address(&r1->peer_addr, &ip0->src_address);
272 r1->msg_type = DHCPV6_MSG_RELAY_FORW;
273 fwd_opt->length = clib_host_to_net_u16(len);
274 fwd_opt->option = clib_host_to_net_u16(DHCPV6_OPTION_RELAY_MSG);
277 r1->hop_count = (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
279 if (PREDICT_FALSE(r1->hop_count >= HOP_COUNT_LIMIT))
281 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
282 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
283 pkts_no_exceeding_max_hop++;
288 /* If relay-fwd and src address is site or global unicast address */
289 if (h0->u.msg_type == DHCPV6_MSG_RELAY_FORW &&
290 ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
291 (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
293 /* Set link address to zero */
294 r1->link_addr.as_u64[0] = 0;
295 r1->link_addr.as_u64[1] = 0;
296 goto link_address_set;
299 /* if receiving interface is unnumbered, use receiving interface
300 * IP address as link address, otherwise use the loopback interface
301 * IP address as link address.
304 swif = vnet_get_sw_interface (vnm, rx_sw_if_index);
305 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
306 sw_if_index = swif->unnumbered_sw_if_index;
308 ia0 = ip6_interface_first_global_or_site_address(&ip6_main, sw_if_index);
311 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
312 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
313 pkts_no_interface_address++;
317 copy_ip6_address(&r1->link_addr, ia0);
320 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
322 if ((b0->current_length+sizeof(*id1)+sizeof(*vss1)+sizeof(*cmac))
325 error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
326 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
331 id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
332 b0->current_length += (sizeof (*id1));
335 fib = ip6_fib_get (rx_fib_idx);
337 //TODO: Revisit if hash makes sense here
338 p_vss = hash_get (dpm->vss_index_by_vrf_id,
342 vss = pool_elt_at_index (dpm->vss, p_vss[0]);
343 oui1 = vss->vpn_id.oui;
344 fib_id1 = vss->vpn_id.fib_id;
347 id1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_INTERFACE_ID);
348 id1->opt.length = clib_host_to_net_u16(sizeof(rx_sw_if_index));
349 id1->int_idx = clib_host_to_net_u32(rx_sw_if_index);
352 if (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW)
354 cmac = (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
355 b0->current_length += (sizeof (*cmac));
356 cmac->opt.length =clib_host_to_net_u16(sizeof(*cmac) -
358 cmac->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
359 cmac->link_type = clib_host_to_net_u16(1); // ethernet
360 clib_memcpy(cmac->data, client_src_mac, 6);
361 u1->length += sizeof(*cmac);
363 if (server->insert_vss !=0 ) {
364 vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
365 b0->current_length += (sizeof (*vss1));
366 vss1->opt.length =clib_host_to_net_u16(sizeof(*vss1) -
368 vss1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_VSS);
369 vss1->data[0] = 1; // type
370 vss1->data[1] = oui1>>16 & 0xff;
371 vss1->data[2] = oui1>>8 & 0xff;
372 vss1->data[3] = oui1 & 0xff;
373 vss1->data[4] = fib_id1>>24 & 0xff;
374 vss1->data[5] = fib_id1>>16 & 0xff;
375 vss1->data[6] = fib_id1>>8 & 0xff;
376 vss1->data[7] = fib_id1 & 0xff;
377 u1->length += sizeof(*vss1);
382 u1->src_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_client);
383 u1->dst_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_server);
386 clib_host_to_net_u16( clib_net_to_host_u16(fwd_opt->length) +
387 sizeof(*r1) + sizeof(*fwd_opt) +
388 sizeof(*u1) + sizeof(*id1) + u1->length);
390 memset(ip1, 0, sizeof(*ip1));
391 ip1->ip_version_traffic_class_and_flow_label = 0x60;
392 ip1->payload_length = u1->length;
393 ip1->protocol = PROTO_UDP;
394 ip1->hop_limit = HOP_COUNT_LIMIT;
395 src = (server->dhcp6_server.as_u64[0] || server->dhcp6_server.as_u64[1]) ?
396 &server->dhcp6_server : &dpm->all_dhcpv6_server_address;
397 copy_ip6_address(&ip1->dst_address, src);
400 ia0 = ip6_interface_first_global_or_site_address
401 (&ip6_main, vnet_buffer(b0)->sw_if_index[VLIB_RX]);
403 src = (server->dhcp6_src_address.as_u64[0] || server->dhcp6_src_address.as_u64[1]) ?
404 &server->dhcp6_src_address : ia0;
407 error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS;
408 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
409 pkts_no_src_address++;
413 copy_ip6_address (&ip1->src_address, src);
416 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
418 ASSERT(bogus_length == 0);
420 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
423 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
425 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
427 tr->which = 0; /* to server */
429 tr->original_sw_if_index = rx_sw_if_index;
430 tr->sw_if_index = sw_if_index;
431 if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)
432 copy_ip6_address((ip6_address_t *)&tr->packet_data[0], &server->dhcp6_server);
436 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
437 to_next, n_left_to_next,
441 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
444 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
445 DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
447 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
448 DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
450 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
451 DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
452 pkts_no_interface_address);
453 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
454 DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
455 pkts_wrong_msg_type);
456 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
457 DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
458 pkts_no_src_address);
459 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
460 DHCPV6_PROXY_ERROR_PKT_TOO_BIG,
462 return from_frame->n_vectors;
465 VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node) = {
466 .function = dhcpv6_proxy_to_server_input,
467 .name = "dhcpv6-proxy-to-server",
468 /* Takes a vector of packets. */
469 .vector_size = sizeof (u32),
471 .n_errors = DHCPV6_PROXY_N_ERROR,
472 .error_strings = dhcpv6_proxy_error_strings,
474 .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
476 #define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
477 foreach_dhcpv6_proxy_to_server_input_next
481 .format_buffer = format_dhcpv6_proxy_header_with_length,
482 .format_trace = format_dhcpv6_proxy_trace,
484 .unformat_buffer = unformat_dhcpv6_proxy_header,
489 dhcpv6_proxy_to_client_input (vlib_main_t * vm,
490 vlib_node_runtime_t * node,
491 vlib_frame_t * from_frame)
494 u32 n_left_from, * from;
495 ethernet_main_t *em = ethernet_get_main (vm);
496 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
497 dhcpv6_server_t * server;
498 vnet_main_t * vnm = vnet_get_main();
501 from = vlib_frame_vector_args (from_frame);
502 n_left_from = from_frame->n_vectors;
504 while (n_left_from > 0)
508 udp_header_t * u0, *u1=0;
509 dhcpv6_relay_hdr_t * h0;
510 ip6_header_t * ip1 = 0, *ip0;
511 ip6_address_t _ia0, * ia0 = &_ia0;
512 ip6_address_t client_address;
513 ethernet_interface_t *ei0;
514 ethernet_header_t *mac0;
515 vnet_hw_interface_t *hi0;
518 u32 sw_if_index = ~0;
519 u32 original_sw_if_index = ~0;
520 vnet_sw_interface_t *si0;
521 u32 error0 = (u32)~0;
522 vnet_sw_interface_t *swif;
523 dhcpv6_option_t *r0 = 0, *o;
525 u8 interface_opt_flag = 0;
526 u8 relay_msg_opt_flag = 0;
528 ip6_main_t * im = &ip6_main;
529 u32 server_fib_idx, svr_fib_id, client_fib_idx, server_idx;
535 b0 = vlib_get_buffer (vm, bi0);
536 h0 = vlib_buffer_get_current (b0);
538 if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
540 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
543 vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
546 f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index);
547 to_next0 = vlib_frame_vector_args (f0);
550 vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0);
553 /* hop count seems not need to be checked */
554 if (HOP_COUNT_LIMIT < h0->hop_count)
556 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
559 u0 = (void *)h0 -(sizeof(*u0));
560 ip0 = (void *)u0 -(sizeof(*ip0));
562 vlib_buffer_advance (b0, sizeof(*h0));
563 o = vlib_buffer_get_current (b0);
565 /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID)
566 _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there.
567 Currently assuming no other options need to be processed
568 The interface-ID is the FIB number we need
569 to track down the client-facing interface */
571 while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
573 if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16(o->option))
575 interface_opt_flag = 1;
576 if (clib_net_to_host_u16(o->length) == sizeof(sw_if_index))
577 sw_if_index = clib_net_to_host_u32(((dhcpv6_int_id_t*)o)->int_idx);
578 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
580 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
584 if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16(o->option))
586 relay_msg_opt_flag = 1;
587 r0 = vlib_buffer_get_current (b0);
589 if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1))
591 vlib_buffer_advance (b0, sizeof(*o) + clib_net_to_host_u16(o->length));
592 o = (dhcpv6_option_t *) (((uword) o) + clib_net_to_host_u16(o->length) + sizeof(*o));
595 if ((relay_msg_opt_flag == 0) || (r0 == 0))
597 error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION;
601 if ((u32)~0 == sw_if_index)
603 error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
607 //Advance buffer to start of encapsulated DHCPv6 message
608 vlib_buffer_advance (b0, sizeof(*r0));
610 client_fib_idx = im->fib_index_by_sw_if_index[sw_if_index];
611 if (client_fib_idx < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
612 server_idx = dm->dhcp6_server_index_by_rx_fib_index[client_fib_idx];
616 if (PREDICT_FALSE (pool_is_free_index (dm->dhcp6_servers, server_idx)))
618 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
622 server = pool_elt_at_index (dm->dhcp6_servers, server_idx);
623 if (server->valid == 0)
625 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
630 server_fib_idx = im->fib_index_by_sw_if_index
631 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
632 svr_fib = ip6_fib_get (server_fib_idx);
633 svr_fib_id = svr_fib->table_id;
635 if (svr_fib_id != server->server_fib6_index ||
636 ip0->src_address.as_u64[0] != server->dhcp6_server.as_u64[0] ||
637 ip0->src_address.as_u64[1] != server->dhcp6_server.as_u64[1])
639 //drop packet if not from server with configured address or FIB
640 error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
644 vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index
647 swif = vnet_get_sw_interface (vnm, original_sw_if_index);
648 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
649 sw_if_index = swif->unnumbered_sw_if_index;
653 * udp_local hands us the DHCPV6 header, need udp hdr,
654 * ip hdr to relay to client
656 vlib_buffer_advance (b0, -(sizeof(*u1)));
657 u1 = vlib_buffer_get_current (b0);
659 vlib_buffer_advance (b0, -(sizeof(*ip1)));
660 ip1 = vlib_buffer_get_current (b0);
662 copy_ip6_address(&client_address, &h0->peer_addr);
664 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
667 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
671 len = clib_net_to_host_u16(r0->length);
672 memset(ip1, 0, sizeof(*ip1));
673 copy_ip6_address(&ip1->dst_address, &client_address);
675 u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);
676 u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
677 u1->length = clib_host_to_net_u16 (len + sizeof(udp_header_t));
679 ip1->ip_version_traffic_class_and_flow_label =
680 ip0->ip_version_traffic_class_and_flow_label &
682 ip1->payload_length = u1->length;
683 ip1->protocol = PROTO_UDP;
684 ip1->hop_limit = HOP_COUNT_LIMIT;
685 copy_ip6_address(&ip1->src_address, ia0);
687 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
689 ASSERT(bogus_length == 0);
691 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
692 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
693 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
694 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
696 mac0 = vlib_buffer_get_current (b0);
698 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
699 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
700 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
701 memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
702 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
703 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x86dd);
705 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
707 u32 * vlan_tag = (u32 *)(mac0+1);
709 tmp = (si0->sub.id << 16) | 0x0800;
710 *vlan_tag = clib_host_to_net_u32 (tmp);
713 /* $$$ consider adding a dynamic next to the graph node, for performance */
714 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
715 to_next0 = vlib_frame_vector_args (f0);
718 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
721 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
723 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
725 tr->which = 1; /* to client */
727 copy_ip6_address((ip6_address_t*)tr->packet_data, ia0);
729 tr->original_sw_if_index = original_sw_if_index;
730 tr->sw_if_index = sw_if_index;
733 return from_frame->n_vectors;
737 VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node) = {
738 .function = dhcpv6_proxy_to_client_input,
739 .name = "dhcpv6-proxy-to-client",
740 /* Takes a vector of packets. */
741 .vector_size = sizeof (u32),
743 .n_errors = DHCPV6_PROXY_N_ERROR,
744 .error_strings = dhcpv6_proxy_error_strings,
745 .format_buffer = format_dhcpv6_proxy_header_with_length,
746 .format_trace = format_dhcpv6_proxy_trace,
748 .unformat_buffer = unformat_dhcpv6_proxy_header,
752 clib_error_t * dhcpv6_proxy_init (vlib_main_t * vm)
754 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
755 vlib_node_t * error_drop_node;
756 dhcpv6_server_t * server;
759 dm->vnet_main = vnet_get_main();
760 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
761 dm->error_drop_node_index = error_drop_node->index;
763 dm->vss_index_by_vrf_id = hash_create (0, sizeof (uword));
765 /* RFC says this is the dhcpv6 server address */
766 dm->all_dhcpv6_server_address.as_u64[0] = clib_host_to_net_u64 (0xFF05000000000000);
767 dm->all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
769 /* RFC says this is the server and agent address */
770 dm->all_dhcpv6_server_relay_agent_address.as_u64[0] = clib_host_to_net_u64 (0xFF02000000000000);
771 dm->all_dhcpv6_server_relay_agent_address.as_u64[1] = clib_host_to_net_u64 (0x00010002);
773 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
774 dhcpv6_proxy_to_client_node.index, 0 /* is_ip6 */);
776 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
777 dhcpv6_proxy_to_server_node.index, 0 /* is_ip6 */);
779 /* Create the default server, don't mark it valid */
780 pool_get (dm->dhcp6_servers, server);
781 memset (server, 0, sizeof (*server));
786 VLIB_INIT_FUNCTION (dhcpv6_proxy_init);
788 /* Old API, manipulates a single server (only) shared by all Rx VRFs */
789 int dhcpv6_proxy_set_server (ip6_address_t *addr, ip6_address_t *src_address,
790 u32 fib_id, int insert_vss, int is_del)
792 return dhcpv6_proxy_set_server_2 (addr, src_address,
797 int dhcpv6_proxy_set_server_2 (ip6_address_t *addr, ip6_address_t *src_address,
798 u32 rx_fib_id, u32 server_fib_id,
799 int insert_vss, int is_del)
801 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
802 dhcpv6_server_t * server = 0;
803 u32 server_fib_index = 0;
804 u32 rx_fib_index = 0;
806 rx_fib_index = ip6_mfib_table_find_or_create_and_lock(rx_fib_id);
807 server_fib_index = ip6_fib_table_find_or_create_and_lock(server_fib_id);
812 if (rx_fib_index >= vec_len(dm->dhcp6_server_index_by_rx_fib_index))
813 return VNET_API_ERROR_NO_SUCH_ENTRY;
815 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
817 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] = 0;
818 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
819 memset (server, 0, sizeof (*server));
820 pool_put (dm->dhcp6_servers, server);
824 if (addr->as_u64[0] == 0 &&
825 addr->as_u64[1] == 0 )
826 return VNET_API_ERROR_INVALID_DST_ADDRESS;
828 if (src_address->as_u64[0] == 0 &&
829 src_address->as_u64[1] == 0)
830 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
834 server = pool_elt_at_index (dm->dhcp6_servers, 0);
841 if (rx_fib_index < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
843 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
844 if (server_fib_index != 0)
846 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
851 /*Allocate a new server*/
852 pool_get (dm->dhcp6_servers, server);
856 const mfib_prefix_t all_dhcp_servers = {
858 .fp_proto = FIB_PROTOCOL_IP6,
860 .ip6 = dm->all_dhcpv6_server_relay_agent_address,
863 const fib_route_path_t path_for_us = {
864 .frp_proto = FIB_PROTOCOL_IP6,
865 .frp_addr = zero_addr,
866 .frp_sw_if_index = 0xffffffff,
869 .frp_flags = FIB_ROUTE_PATH_LOCAL,
871 mfib_table_entry_path_update(rx_fib_index,
875 MFIB_ITF_FLAG_FORWARD);
877 * Each interface that is enabled in this table, needs to be added
878 * as an accepting interface, but this is not easily doable in VPP.
879 * So we cheat. Add a flag to the entry that indicates accept form
881 * We will still only accept on v6 enabled interfaces, since the input
882 * feature ensures this.
884 mfib_table_entry_update(rx_fib_index,
887 MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF);
892 copy_ip6_address(&server->dhcp6_server, addr);
893 copy_ip6_address(&server->dhcp6_src_address, src_address);
894 server->server_fib6_index = server_fib_index;
896 server->insert_vss = insert_vss;
898 vec_validate (dm->dhcp6_server_index_by_rx_fib_index, rx_fib_index);
899 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] =
900 server - dm->dhcp6_servers;
905 static clib_error_t *
906 dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
907 unformat_input_t * input,
908 vlib_cli_command_t * cmd)
910 ip6_address_t addr, src_addr;
911 int set_server = 0, set_src_address = 0;
912 u32 rx_fib_id = 0, server_fib_id = 0;
913 int is_del = 0, add_vss = 0;
915 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
917 if (unformat (input, "server %U",
918 unformat_ip6_address, &addr))
920 else if (unformat(input, "src-address %U",
921 unformat_ip6_address, &src_addr))
923 else if (unformat (input, "server-fib-id %d", &server_fib_id))
925 else if (unformat (input, "rx-fib-id %d", &rx_fib_id))
927 else if (unformat (input, "add-vss-option")
928 || unformat (input, "insert-option"))
930 else if (unformat (input, "delete") ||
931 unformat (input, "del"))
937 if (is_del || (set_server && set_src_address))
941 rv = dhcpv6_proxy_set_server_2 (&addr, &src_addr, rx_fib_id,
942 server_fib_id, add_vss, is_del);
944 //TODO: Complete the errors
951 return clib_error_return (0, "FIB id %d does not exist", server_fib_id);
954 return clib_error_return (0, "BUG: rv %d", rv);
958 return clib_error_return (0, "parse error`%U'",
959 format_unformat_error, input);
962 VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
963 .path = "set dhcpv6 proxy",
964 .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> "
965 "[add-vss-option] [server-fib-id <fib-id>] [rx-fib-id <fib-id>] ",
966 .function = dhcpv6_proxy_set_command_fn,
969 u8 * format_dhcpv6_proxy_server (u8 * s, va_list * args)
971 dhcpv6_proxy_main_t * dm = va_arg (*args, dhcpv6_proxy_main_t *);
972 dhcpv6_server_t * server = va_arg (*args, dhcpv6_server_t *);
973 u32 rx_fib_index = va_arg (*args, u32);
974 ip6_fib_t * rx_fib, * server_fib;
975 u32 server_fib_id = (u32)~0, rx_fib_id = ~0;
979 s = format (s, "%=40s%=40s%=14s%=14s%=20s", "Server Address", "Source Address",
980 "Server FIB", "RX FIB", "Insert VSS Option");
984 server_fib = ip6_fib_get(server->server_fib6_index);
986 server_fib_id= server_fib->table_id;
988 rx_fib= ip6_fib_get(rx_fib_index);
991 rx_fib_id = rx_fib->table_id;
993 s = format (s, "%=40U%=40U%=14u%=14u%=20s",
994 format_ip6_address, &server->dhcp6_server,
995 format_ip6_address, &server->dhcp6_src_address,
996 server_fib_id, rx_fib_id,
997 server->insert_vss ? "yes" : "no");
1001 static clib_error_t *
1002 dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
1003 unformat_input_t * input,
1004 vlib_cli_command_t * cmd)
1006 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
1007 ip6_main_t * im = &ip6_main;
1010 dhcpv6_server_t * server;
1012 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, 0 /* header line */,
1014 for (i = 0; i < vec_len (im->fibs); i++)
1016 if (i < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
1017 server_index = dm->dhcp6_server_index_by_rx_fib_index[i];
1020 server = pool_elt_at_index (dm->dhcp6_servers, server_index);
1022 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, dm,
1028 VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
1029 .path = "show dhcpv6 proxy",
1030 .short_help = "Display dhcpv6 proxy info",
1031 .function = dhcpv6_proxy_show_command_fn,
1034 int dhcpv6_proxy_set_vss(u32 tbl_id,
1039 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1040 u32 old_oui, old_fib_id;
1044 p = hash_get (dm->vss_index_by_vrf_id, tbl_id);
1047 v = pool_elt_at_index (dm->vss, p[0]);
1049 return VNET_API_ERROR_NO_SUCH_FIB;
1051 old_oui = v->vpn_id.oui;
1052 old_fib_id = v->vpn_id.fib_id;
1056 if (old_oui == oui &&
1057 old_fib_id == fib_id )
1059 pool_put(dm->vss, v);
1060 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1064 return VNET_API_ERROR_NO_SUCH_ENTRY;
1067 pool_put(dm->vss, v);
1068 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1070 return VNET_API_ERROR_NO_SUCH_ENTRY;
1072 pool_get (dm->vss, v);
1073 memset (v, ~0, sizeof (*v));
1074 v->vpn_id.fib_id = fib_id;
1075 v->vpn_id.oui = oui;
1076 hash_set (dm->vss_index_by_vrf_id, tbl_id, v - dm->vss);
1082 static clib_error_t *
1083 dhcpv6_vss_command_fn (vlib_main_t * vm,
1084 unformat_input_t * input,
1085 vlib_cli_command_t * cmd)
1087 int is_del = 0, got_new_vss=0;
1089 u32 fib_id=0, tbl_id=~0;
1091 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1093 if (unformat (input, "oui %d", &oui))
1095 else if (unformat (input, "vpn-id %d", &fib_id))
1097 else if (unformat (input, "table %d", &tbl_id))
1099 else if (unformat(input, "delete") || unformat(input, "del"))
1106 return clib_error_return (0, "no table ID specified.");
1108 if (is_del || got_new_vss)
1112 rv = dhcpv6_proxy_set_vss(tbl_id, oui, fib_id, is_del);
1118 case VNET_API_ERROR_NO_SUCH_FIB:
1119 return clib_error_return (0, "vss info (oui:%d, vpn-id:%d) not found in table %d.",
1120 oui, fib_id, tbl_id);
1122 case VNET_API_ERROR_NO_SUCH_ENTRY:
1123 return clib_error_return (0, "vss for table %d not found in pool.",
1127 return clib_error_return (0, "BUG: rv %d", rv);
1131 return clib_error_return (0, "parse error`%U'",
1132 format_unformat_error, input);
1136 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
1137 .path = "set dhcpv6 vss",
1138 .short_help = "set dhcpv6 vss table <table-id> oui <oui> vpn-idx <vpn-idx>",
1139 .function = dhcpv6_vss_command_fn,
1142 static clib_error_t *
1143 dhcpv6_vss_show_command_fn (vlib_main_t * vm,
1144 unformat_input_t * input,
1145 vlib_cli_command_t * cmd)
1148 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
1155 vlib_cli_output (vm, "%=6s%=6s%=12s","Table", "OUI", "VPN ID");
1156 hash_foreach (tbl_id, index, dm->vss_index_by_vrf_id,
1158 v = pool_elt_at_index (dm->vss, index);
1159 oui = v->vpn_id.oui;
1160 fib_id = v->vpn_id.fib_id;
1161 vlib_cli_output (vm, "%=6d%=6d%=12d",
1162 tbl_id, oui, fib_id);
1168 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
1169 .path = "show dhcpv6 vss",
1170 .short_help = "show dhcpv6 VSS",
1171 .function = dhcpv6_vss_show_command_fn,
1174 static clib_error_t *
1175 dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1176 unformat_input_t * input,
1177 vlib_cli_command_t * cmd)
1180 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1181 vnet_main_t *vnm = vnet_get_main();
1182 u32 sw_if_index0=0, sw_if_index;
1184 vnet_sw_interface_t *swif;
1186 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1189 if (unformat(input, "%U",
1190 unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1192 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1193 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1194 swif->unnumbered_sw_if_index : sw_if_index0;
1195 ia0 = ip6_interface_first_address(&ip6_main, sw_if_index);
1198 vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
1200 vlib_cli_output (vm, "%=20U%=48U",
1201 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index0,
1202 format_ip6_address, ia0);
1204 vlib_cli_output (vm, "%=34s%=20U", "No IPv6 address configured on",
1205 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index);
1213 VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1214 .path = "show dhcpv6 link-address interface",
1215 .short_help = "show dhcpv6 link-address interface <interface>",
1216 .function = dhcpv6_link_address_show_command_fn,