2 * dhcp6_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 <dhcp/dhcp_proxy.h>
21 #include <dhcp/dhcp6_packet.h>
22 #include <vnet/mfib/mfib_table.h>
23 #include <vnet/mfib/ip6_mfib.h>
24 #include <vnet/fib/fib.h>
26 static char *dhcpv6_proxy_error_strings[] = {
27 #define dhcpv6_proxy_error(n,s) s,
28 #include <dhcp/dhcp6_proxy_error.def>
29 #undef dhcpv6_proxy_error
32 #define foreach_dhcpv6_proxy_to_server_input_next \
33 _ (DROP, "error-drop") \
34 _ (LOOKUP, "ip6-lookup") \
35 _ (SEND_TO_CLIENT, "dhcpv6-proxy-to-client")
40 #define _(s,n) DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s,
41 foreach_dhcpv6_proxy_to_server_input_next
43 DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
44 } dhcpv6_proxy_to_server_input_next_t;
48 /* 0 => to server, 1 => to client */
53 u32 original_sw_if_index;
54 } dhcpv6_proxy_trace_t;
56 static vlib_node_registration_t dhcpv6_proxy_to_server_node;
57 static vlib_node_registration_t dhcpv6_proxy_to_client_node;
59 /* all DHCP servers address */
60 static ip6_address_t all_dhcpv6_server_address;
61 static ip6_address_t all_dhcpv6_server_relay_agent_address;
64 format_dhcpv6_proxy_trace (u8 * s, va_list * args)
66 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
67 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
68 dhcpv6_proxy_trace_t *t = va_arg (*args, dhcpv6_proxy_trace_t *);
71 s = format (s, "DHCPV6 proxy: sent to server %U",
72 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
74 s = format (s, "DHCPV6 proxy: sent to client from %U",
75 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
76 if (t->error != (u32) ~ 0)
77 s = format (s, " error: %s\n", dhcpv6_proxy_error_strings[t->error]);
79 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
80 t->original_sw_if_index, t->sw_if_index);
86 format_dhcpv6_proxy_header_with_length (u8 * s, va_list * args)
88 dhcpv6_header_t *h = va_arg (*args, dhcpv6_header_t *);
89 u32 max_header_bytes = va_arg (*args, u32);
92 header_bytes = sizeof (h[0]);
93 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
94 return format (s, "dhcpv6 header truncated");
96 s = format (s, "DHCPV6 Proxy");
101 /* get first interface address */
102 static ip6_address_t *
103 ip6_interface_first_global_or_site_address (ip6_main_t * im, u32 sw_if_index)
105 ip_lookup_main_t *lm = &im->lookup_main;
106 ip_interface_address_t *ia = 0;
107 ip6_address_t *result = 0;
110 foreach_ip_interface_address (lm, ia, sw_if_index,
111 1 /* honor unnumbered */,
113 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
114 if ((a->as_u8[0] & 0xe0) == 0x20 ||
115 (a->as_u8[0] & 0xfe) == 0xfc) {
125 copy_ip6_address (ip6_address_t * dst, ip6_address_t * src)
127 dst->as_u64[0] = src->as_u64[0];
128 dst->as_u64[1] = src->as_u64[1];
132 dhcpv6_proxy_to_server_input (vlib_main_t * vm,
133 vlib_node_runtime_t * node,
134 vlib_frame_t * from_frame)
136 u32 n_left_from, next_index, *from, *to_next;
137 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
138 from = vlib_frame_vector_args (from_frame);
139 n_left_from = from_frame->n_vectors;
140 u32 pkts_to_server = 0, pkts_to_client = 0, pkts_no_server = 0;
141 u32 pkts_no_interface_address = 0, pkts_no_exceeding_max_hop = 0;
142 u32 pkts_no_src_address = 0;
143 u32 pkts_wrong_msg_type = 0;
144 u32 pkts_too_big = 0;
145 ip6_main_t *im = &ip6_main;
149 dhcp_server_t *server;
150 u32 rx_fib_idx = 0, server_fib_idx = 0;
152 next_index = node->cached_next_index;
154 while (n_left_from > 0)
158 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
160 while (n_left_from > 0 && n_left_to_next > 0)
162 vnet_main_t *vnm = vnet_get_main ();
164 u32 rx_sw_if_index = 0;
165 vnet_sw_interface_t *swif;
168 udp_header_t *u0, *u1;
169 dhcpv6_header_t *h0; // client msg hdr
170 ip6_header_t *ip0, *ip1;
171 ip6_address_t _ia0, *ia0 = &_ia0;
173 u32 error0 = (u32) ~ 0;
174 dhcpv6_option_t *fwd_opt;
175 dhcpv6_relay_hdr_t *r1;
177 dhcpv6_int_id_t *id1;
179 dhcpv6_client_mac_t *cmac; // client mac
180 ethernet_header_t *e_h0;
181 u8 client_src_mac[6];
189 b0 = vlib_get_buffer (vm, bi0);
191 h0 = vlib_buffer_get_current (b0);
194 * udp_local hands us the DHCPV6 header.
196 u0 = (void *) h0 - (sizeof (*u0));
197 ip0 = (void *) u0 - (sizeof (*ip0));
198 e_h0 = (void *) ip0 - ethernet_buffer_header_size (b0);
200 clib_memcpy (client_src_mac, e_h0->src_address, 6);
202 switch (h0->msg_type)
204 case DHCPV6_MSG_SOLICIT:
205 case DHCPV6_MSG_REQUEST:
206 case DHCPV6_MSG_CONFIRM:
207 case DHCPV6_MSG_RENEW:
208 case DHCPV6_MSG_REBIND:
209 case DHCPV6_MSG_RELEASE:
210 case DHCPV6_MSG_DECLINE:
211 case DHCPV6_MSG_INFORMATION_REQUEST:
212 case DHCPV6_MSG_RELAY_FORW:
215 case DHCPV6_MSG_RELAY_REPL:
217 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
222 /* drop the packet */
223 pkts_wrong_msg_type++;
224 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
225 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
230 /* Send to DHCPV6 server via the configured FIB */
231 rx_sw_if_index = sw_if_index =
232 vnet_buffer (b0)->sw_if_index[VLIB_RX];
233 rx_fib_idx = im->mfib_index_by_sw_if_index[rx_sw_if_index];
234 proxy = dhcp_get_proxy (dpm, rx_fib_idx, FIB_PROTOCOL_IP6);
236 if (PREDICT_FALSE (NULL == proxy))
238 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
239 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
244 server = &proxy->dhcp_servers[0];
245 server_fib_idx = server->server_fib_index;
246 vnet_buffer (b0)->sw_if_index[VLIB_TX] = server_fib_idx;
249 /* relay-option header pointer */
250 vlib_buffer_advance (b0, -(sizeof (*fwd_opt)));
251 fwd_opt = vlib_buffer_get_current (b0);
252 /* relay message header pointer */
253 vlib_buffer_advance (b0, -(sizeof (*r1)));
254 r1 = vlib_buffer_get_current (b0);
256 vlib_buffer_advance (b0, -(sizeof (*u1)));
257 u1 = vlib_buffer_get_current (b0);
259 vlib_buffer_advance (b0, -(sizeof (*ip1)));
260 ip1 = vlib_buffer_get_current (b0);
262 /* fill in all that rubbish... */
263 len = clib_net_to_host_u16 (u0->length) - sizeof (udp_header_t);
264 copy_ip6_address (&r1->peer_addr, &ip0->src_address);
266 r1->msg_type = DHCPV6_MSG_RELAY_FORW;
267 fwd_opt->length = clib_host_to_net_u16 (len);
268 fwd_opt->option = clib_host_to_net_u16 (DHCPV6_OPTION_RELAY_MSG);
272 (h0->msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
274 if (PREDICT_FALSE (r1->hop_count >= HOP_COUNT_LIMIT))
276 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
277 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
278 pkts_no_exceeding_max_hop++;
283 /* If relay-fwd and src address is site or global unicast address */
284 if (h0->msg_type == DHCPV6_MSG_RELAY_FORW &&
285 ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
286 (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
288 /* Set link address to zero */
289 r1->link_addr.as_u64[0] = 0;
290 r1->link_addr.as_u64[1] = 0;
291 goto link_address_set;
294 /* if receiving interface is unnumbered, use receiving interface
295 * IP address as link address, otherwise use the loopback interface
296 * IP address as link address.
299 swif = vnet_get_sw_interface (vnm, rx_sw_if_index);
300 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
301 sw_if_index = swif->unnumbered_sw_if_index;
304 ip6_interface_first_global_or_site_address (&ip6_main,
308 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
309 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
310 pkts_no_interface_address++;
314 copy_ip6_address (&r1->link_addr, ia0);
318 if ((b0->current_data + b0->current_length + sizeof (*id1) +
319 sizeof (*vss1) + sizeof (*cmac)) >
320 vlib_buffer_get_default_data_size (vm))
322 error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
323 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
328 id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
329 b0->current_length += (sizeof (*id1));
331 id1->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_INTERFACE_ID);
332 id1->opt.length = clib_host_to_net_u16 (sizeof (rx_sw_if_index));
333 id1->int_idx = clib_host_to_net_u32 (rx_sw_if_index);
336 if (h0->msg_type != DHCPV6_MSG_RELAY_FORW)
339 (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
340 b0->current_length += (sizeof (*cmac));
341 cmac->opt.length = clib_host_to_net_u16 (sizeof (*cmac) -
345 (DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
346 cmac->link_type = clib_host_to_net_u16 (1); /* ethernet */
347 clib_memcpy (cmac->data, client_src_mac, 6);
348 u1->length += sizeof (*cmac);
351 vss = dhcp_get_vss_info (dpm, rx_fib_idx, FIB_PROTOCOL_IP6);
355 u16 id_len; /* length of VPN ID */
356 u16 type_len = sizeof (vss1->vss_type);
358 vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
359 vss1->vss_type = vss->vss_type;
360 if (vss->vss_type == VSS_TYPE_VPN_ID)
362 id_len = sizeof (vss->vpn_id); /* vpn_id is 7 bytes */
363 memcpy (vss1->data, vss->vpn_id, id_len);
365 else if (vss->vss_type == VSS_TYPE_ASCII)
367 id_len = vec_len (vss->vpn_ascii_id);
368 memcpy (vss1->data, vss->vpn_ascii_id, id_len);
370 else /* must be VSS_TYPE_DEFAULT, no VPN ID */
373 vss1->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_VSS);
374 vss1->opt.length = clib_host_to_net_u16 (type_len + id_len);
375 u1->length += type_len + id_len + sizeof (vss1->opt);
376 b0->current_length += type_len + id_len + sizeof (vss1->opt);
381 u1->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcpv6_to_client);
382 u1->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcpv6_to_server);
385 clib_host_to_net_u16 (clib_net_to_host_u16 (fwd_opt->length) +
386 sizeof (*r1) + sizeof (*fwd_opt) +
387 sizeof (*u1) + sizeof (*id1) + u1->length);
389 clib_memset (ip1, 0, sizeof (*ip1));
390 ip1->ip_version_traffic_class_and_flow_label = 0x60;
391 ip1->payload_length = u1->length;
392 ip1->protocol = PROTO_UDP;
393 ip1->hop_limit = HOP_COUNT_LIMIT;
394 src = ((server->dhcp_server.ip6.as_u64[0] ||
395 server->dhcp_server.ip6.as_u64[1]) ?
396 &server->dhcp_server.ip6 : &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 = (proxy->dhcp_src_address.ip6.as_u64[0] ||
404 proxy->dhcp_src_address.ip6.as_u64[1]) ?
405 &proxy->dhcp_src_address.ip6 : ia0;
408 error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS;
409 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
410 pkts_no_src_address++;
414 copy_ip6_address (&ip1->src_address, src);
417 u1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip1,
419 ASSERT (bogus_length == 0);
421 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
423 is_solicit = (DHCPV6_MSG_SOLICIT == h0->msg_type);
426 * If we have multiple servers configured and this is the
427 * client's discover message, then send copies to each of
430 if (is_solicit && vec_len (proxy->dhcp_servers) > 1)
434 for (ii = 1; ii < vec_len (proxy->dhcp_servers); ii++)
439 c0 = vlib_buffer_copy (vm, b0);
442 vlib_node_increment_counter
443 (vm, dhcpv6_proxy_to_server_node.index,
444 DHCPV6_PROXY_ERROR_ALLOC_FAIL, 1);
447 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (c0);
448 ci0 = vlib_get_buffer_index (vm, c0);
449 server = &proxy->dhcp_servers[ii];
451 ip0 = vlib_buffer_get_current (c0);
453 src = ((server->dhcp_server.ip6.as_u64[0] ||
454 server->dhcp_server.ip6.as_u64[1]) ?
455 &server->dhcp_server.ip6 :
456 &all_dhcpv6_server_address);
457 copy_ip6_address (&ip1->dst_address, src);
463 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
464 to_next, n_left_to_next,
467 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
469 dhcpv6_proxy_trace_t *tr;
471 tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
472 tr->which = 0; /* to server */
474 tr->original_sw_if_index = rx_sw_if_index;
475 tr->sw_if_index = sw_if_index;
476 if (next0 == DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
477 copy_ip6_address ((ip6_address_t *) &
479 &server->dhcp_server.ip6);
482 if (PREDICT_FALSE (0 == n_left_to_next))
484 vlib_put_next_frame (vm, node, next_index,
486 vlib_get_next_frame (vm, node, next_index,
487 to_next, n_left_to_next);
493 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
495 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
497 tr->which = 0; /* to server */
499 tr->original_sw_if_index = rx_sw_if_index;
500 tr->sw_if_index = sw_if_index;
501 if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)
502 copy_ip6_address ((ip6_address_t *) & tr->packet_data[0],
503 &server->dhcp_server.ip6);
511 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
512 to_next, n_left_to_next,
516 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
519 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
520 DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
522 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
523 DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
525 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
526 DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
527 pkts_no_interface_address);
528 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
529 DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
530 pkts_wrong_msg_type);
531 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
532 DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
533 pkts_no_src_address);
534 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
535 DHCPV6_PROXY_ERROR_PKT_TOO_BIG, pkts_too_big);
536 return from_frame->n_vectors;
540 VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node, static) = {
541 .function = dhcpv6_proxy_to_server_input,
542 .name = "dhcpv6-proxy-to-server",
543 /* Takes a vector of packets. */
544 .vector_size = sizeof (u32),
546 .n_errors = DHCPV6_PROXY_N_ERROR,
547 .error_strings = dhcpv6_proxy_error_strings,
549 .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
551 #define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
552 foreach_dhcpv6_proxy_to_server_input_next
556 .format_buffer = format_dhcpv6_proxy_header_with_length,
557 .format_trace = format_dhcpv6_proxy_trace,
559 .unformat_buffer = unformat_dhcpv6_proxy_header,
565 dhcpv6_proxy_to_client_input (vlib_main_t * vm,
566 vlib_node_runtime_t * node,
567 vlib_frame_t * from_frame)
570 u32 n_left_from, *from;
571 ethernet_main_t *em = vnet_get_ethernet_main ();
572 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
574 dhcp_server_t *server;
575 vnet_main_t *vnm = vnet_get_main ();
578 from = vlib_frame_vector_args (from_frame);
579 n_left_from = from_frame->n_vectors;
581 while (n_left_from > 0)
585 udp_header_t *u0, *u1 = 0;
586 dhcpv6_relay_hdr_t *h0;
587 ip6_header_t *ip1 = 0, *ip0;
588 ip6_address_t _ia0, *ia0 = &_ia0;
589 ip6_address_t client_address;
590 ethernet_interface_t *ei0;
591 ethernet_header_t *mac0;
592 vnet_hw_interface_t *hi0;
595 u32 sw_if_index = ~0;
596 u32 original_sw_if_index = ~0;
597 vnet_sw_interface_t *si0;
598 u32 error0 = (u32) ~ 0;
599 vnet_sw_interface_t *swif;
600 dhcpv6_option_t *r0 = 0, *o;
602 u8 interface_opt_flag = 0;
603 u8 relay_msg_opt_flag = 0;
604 ip6_main_t *im = &ip6_main;
605 u32 server_fib_idx, client_fib_idx;
611 b0 = vlib_get_buffer (vm, bi0);
612 h0 = vlib_buffer_get_current (b0);
614 if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
616 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
619 vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
622 f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index);
623 to_next0 = vlib_frame_vector_args (f0);
626 vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0);
629 /* hop count seems not need to be checked */
630 if (HOP_COUNT_LIMIT < h0->hop_count)
632 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
635 u0 = (void *) h0 - (sizeof (*u0));
636 ip0 = (void *) u0 - (sizeof (*ip0));
638 vlib_buffer_advance (b0, sizeof (*h0));
639 o = vlib_buffer_get_current (b0);
641 /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID)
642 _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there.
643 Currently assuming no other options need to be processed
644 The interface-ID is the FIB number we need
645 to track down the client-facing interface */
647 while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
649 if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16 (o->option))
651 interface_opt_flag = 1;
652 if (clib_net_to_host_u16 (o->length) == sizeof (sw_if_index))
654 clib_net_to_host_u32 (((dhcpv6_int_id_t *) o)->int_idx);
655 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
657 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
661 if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16 (o->option))
663 relay_msg_opt_flag = 1;
664 r0 = vlib_buffer_get_current (b0);
666 if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1))
668 vlib_buffer_advance (b0,
670 clib_net_to_host_u16 (o->length));
672 (dhcpv6_option_t *) (((uword) o) +
673 clib_net_to_host_u16 (o->length) +
677 if ((relay_msg_opt_flag == 0) || (r0 == 0))
679 error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION;
683 if ((u32) ~ 0 == sw_if_index)
685 error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
689 //Advance buffer to start of encapsulated DHCPv6 message
690 vlib_buffer_advance (b0, sizeof (*r0));
692 client_fib_idx = im->mfib_index_by_sw_if_index[sw_if_index];
693 proxy = dhcp_get_proxy (dm, client_fib_idx, FIB_PROTOCOL_IP6);
697 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
701 server_fib_idx = im->fib_index_by_sw_if_index
702 [vnet_buffer (b0)->sw_if_index[VLIB_RX]];
704 vec_foreach (server, proxy->dhcp_servers)
706 if (server_fib_idx == server->server_fib_index &&
707 ip0->src_address.as_u64[0] == server->dhcp_server.ip6.as_u64[0] &&
708 ip0->src_address.as_u64[1] == server->dhcp_server.ip6.as_u64[1])
714 //drop packet if not from server with configured address or FIB
715 error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
719 vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index
722 swif = vnet_get_sw_interface (vnm, original_sw_if_index);
723 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
724 sw_if_index = swif->unnumbered_sw_if_index;
728 * udp_local hands us the DHCPV6 header, need udp hdr,
729 * ip hdr to relay to client
731 vlib_buffer_advance (b0, -(sizeof (*u1)));
732 u1 = vlib_buffer_get_current (b0);
734 vlib_buffer_advance (b0, -(sizeof (*ip1)));
735 ip1 = vlib_buffer_get_current (b0);
737 copy_ip6_address (&client_address, &h0->peer_addr);
739 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
742 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
746 len = clib_net_to_host_u16 (r0->length);
747 clib_memset (ip1, 0, sizeof (*ip1));
748 copy_ip6_address (&ip1->dst_address, &client_address);
750 u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);
751 u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
752 u1->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
754 ip1->ip_version_traffic_class_and_flow_label =
755 ip0->ip_version_traffic_class_and_flow_label & 0x00000fff;
756 ip1->payload_length = u1->length;
757 ip1->protocol = PROTO_UDP;
758 ip1->hop_limit = HOP_COUNT_LIMIT;
759 copy_ip6_address (&ip1->src_address, ia0);
761 u1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip1,
763 ASSERT (bogus_length == 0);
765 vlib_buffer_advance (b0, -(sizeof (ethernet_header_t)));
766 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
767 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
768 vlib_buffer_advance (b0, -4 /* space for VLAN tag */ );
770 mac0 = vlib_buffer_get_current (b0);
772 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
773 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
774 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
775 clib_memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
776 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
777 clib_net_to_host_u16 (0x8100) : clib_net_to_host_u16 (0x86dd);
779 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
781 u32 *vlan_tag = (u32 *) (mac0 + 1);
783 tmp = (si0->sub.id << 16) | 0x0800;
784 *vlan_tag = clib_host_to_net_u32 (tmp);
787 /* $$$ consider adding a dynamic next to the graph node, for performance */
788 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
789 to_next0 = vlib_frame_vector_args (f0);
792 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
795 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
797 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
799 tr->which = 1; /* to client */
801 copy_ip6_address ((ip6_address_t *) tr->packet_data, ia0);
803 tr->original_sw_if_index = original_sw_if_index;
804 tr->sw_if_index = sw_if_index;
807 return from_frame->n_vectors;
812 VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node, static) = {
813 .function = dhcpv6_proxy_to_client_input,
814 .name = "dhcpv6-proxy-to-client",
815 /* Takes a vector of packets. */
816 .vector_size = sizeof (u32),
818 .n_errors = DHCPV6_PROXY_N_ERROR,
819 .error_strings = dhcpv6_proxy_error_strings,
820 .format_buffer = format_dhcpv6_proxy_header_with_length,
821 .format_trace = format_dhcpv6_proxy_trace,
823 .unformat_buffer = unformat_dhcpv6_proxy_header,
828 static clib_error_t *
829 dhcp6_proxy_init (vlib_main_t * vm)
831 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
832 vlib_node_t *error_drop_node;
834 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
835 dm->error_drop_node_index = error_drop_node->index;
837 /* RFC says this is the dhcpv6 server address */
838 all_dhcpv6_server_address.as_u64[0] =
839 clib_host_to_net_u64 (0xFF05000000000000);
840 all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
842 /* RFC says this is the server and agent address */
843 all_dhcpv6_server_relay_agent_address.as_u64[0] =
844 clib_host_to_net_u64 (0xFF02000000000000);
845 all_dhcpv6_server_relay_agent_address.as_u64[1] =
846 clib_host_to_net_u64 (0x00010002);
851 VLIB_INIT_FUNCTION (dhcp6_proxy_init);
854 dhcp6_proxy_set_server (ip46_address_t * addr,
855 ip46_address_t * src_addr,
856 u32 rx_table_id, u32 server_table_id, int is_del)
858 vlib_main_t *vm = vlib_get_main ();
859 u32 rx_fib_index = 0;
862 const mfib_prefix_t all_dhcp_servers = {
864 .fp_proto = FIB_PROTOCOL_IP6,
866 .ip6 = all_dhcpv6_server_relay_agent_address,
870 if (ip46_address_is_zero (addr))
871 return VNET_API_ERROR_INVALID_DST_ADDRESS;
873 if (ip46_address_is_zero (src_addr))
874 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
876 rx_fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
882 if (dhcp_proxy_server_del (FIB_PROTOCOL_IP6, rx_fib_index,
883 addr, server_table_id))
885 mfib_table_entry_delete (rx_fib_index,
886 &all_dhcp_servers, MFIB_SOURCE_DHCP);
887 mfib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP6,
890 udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
892 udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
898 const fib_route_path_t path_for_us = {
899 .frp_proto = DPO_PROTO_IP6,
900 .frp_addr = zero_addr,
901 .frp_sw_if_index = 0xffffffff,
904 .frp_flags = FIB_ROUTE_PATH_LOCAL,
905 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
907 if (dhcp_proxy_server_add (FIB_PROTOCOL_IP6, addr, src_addr,
908 rx_fib_index, server_table_id))
910 mfib_table_entry_path_update (rx_fib_index,
912 MFIB_SOURCE_DHCP, &path_for_us);
914 * Each interface that is enabled in this table, needs to be added
915 * as an accepting interface, but this is not easily doable in VPP.
916 * So we cheat. Add a flag to the entry that indicates accept form
918 * We will still only accept on v6 enabled interfaces, since the
919 * input feature ensures this.
921 mfib_table_entry_update (rx_fib_index,
925 MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF);
926 mfib_table_lock (rx_fib_index, FIB_PROTOCOL_IP6, MFIB_SOURCE_DHCP);
928 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
929 dhcpv6_proxy_to_client_node.index,
931 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
932 dhcpv6_proxy_to_server_node.index,
937 mfib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP6, MFIB_SOURCE_DHCP);
942 static clib_error_t *
943 dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
944 unformat_input_t * input,
945 vlib_cli_command_t * cmd)
947 ip46_address_t addr, src_addr;
948 int set_server = 0, set_src_address = 0;
949 u32 rx_table_id = 0, server_table_id = 0;
952 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
954 if (unformat (input, "server %U", unformat_ip6_address, &addr.ip6))
956 else if (unformat (input, "src-address %U",
957 unformat_ip6_address, &src_addr.ip6))
959 else if (unformat (input, "server-fib-id %d", &server_table_id))
961 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
963 else if (unformat (input, "delete") || unformat (input, "del"))
969 if (is_del || (set_server && set_src_address))
973 rv = dhcp6_proxy_set_server (&addr, &src_addr, rx_table_id,
974 server_table_id, is_del);
976 //TODO: Complete the errors
982 case VNET_API_ERROR_INVALID_DST_ADDRESS:
983 return clib_error_return (0, "Invalid server address");
985 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
986 return clib_error_return (0, "Invalid src address");
988 case VNET_API_ERROR_NO_SUCH_ENTRY:
989 return clib_error_return
990 (0, "Fib id %d: no per-fib DHCP server configured", rx_table_id);
993 return clib_error_return (0, "BUG: rv %d", rv);
997 return clib_error_return (0, "parse error`%U'",
998 format_unformat_error, input);
1002 VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
1003 .path = "set dhcpv6 proxy",
1004 .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> "
1005 "[server-fib-id <fib-id>] [rx-fib-id <fib-id>] ",
1006 .function = dhcpv6_proxy_set_command_fn,
1011 format_dhcp6_proxy_server (u8 * s, va_list * args)
1013 dhcp_proxy_t *proxy = va_arg (*args, dhcp_proxy_t *);
1014 fib_table_t *server_fib;
1015 dhcp_server_t *server;
1020 s = format (s, "%=14s%=16s%s", "RX FIB", "Src Address",
1021 "Servers FIB,Address");
1025 rx_fib = ip6_mfib_get (proxy->rx_fib_index);
1027 s = format (s, "%=14u%=16U",
1029 format_ip46_address, &proxy->dhcp_src_address, IP46_TYPE_ANY);
1031 vec_foreach (server, proxy->dhcp_servers)
1033 server_fib = fib_table_get (server->server_fib_index, FIB_PROTOCOL_IP6);
1034 s = format (s, "%u,%U ",
1035 server_fib->ft_table_id,
1036 format_ip46_address, &server->dhcp_server, IP46_TYPE_ANY);
1043 dhcp6_proxy_show_walk (dhcp_proxy_t * proxy, void *ctx)
1045 vlib_main_t *vm = ctx;
1047 vlib_cli_output (vm, "%U", format_dhcp6_proxy_server, proxy);
1052 static clib_error_t *
1053 dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
1054 unformat_input_t * input,
1055 vlib_cli_command_t * cmd)
1057 vlib_cli_output (vm, "%U", format_dhcp6_proxy_server,
1058 NULL /* header line */ );
1060 dhcp_proxy_walk (FIB_PROTOCOL_IP6, dhcp6_proxy_show_walk, vm);
1066 VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
1067 .path = "show dhcpv6 proxy",
1068 .short_help = "Display dhcpv6 proxy info",
1069 .function = dhcpv6_proxy_show_command_fn,
1073 static clib_error_t *
1074 dhcpv6_vss_command_fn (vlib_main_t * vm,
1075 unformat_input_t * input, vlib_cli_command_t * cmd)
1077 u8 is_del = 0, vss_type = VSS_TYPE_DEFAULT;
1078 u8 *vpn_ascii_id = 0;
1079 u32 oui = 0, fib_id = 0, tbl_id = ~0;
1081 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1083 if (unformat (input, "table %d", &tbl_id))
1085 else if (unformat (input, "oui %d", &oui))
1086 vss_type = VSS_TYPE_VPN_ID;
1087 else if (unformat (input, "vpn-id %d", &fib_id))
1088 vss_type = VSS_TYPE_VPN_ID;
1089 else if (unformat (input, "vpn-ascii-id %s", &vpn_ascii_id))
1090 vss_type = VSS_TYPE_ASCII;
1091 else if (unformat (input, "delete") || unformat (input, "del"))
1098 return clib_error_return (0, "no table ID specified.");
1100 int rv = dhcp_proxy_set_vss (FIB_PROTOCOL_IP6, tbl_id, vss_type,
1101 vpn_ascii_id, oui, fib_id, is_del);
1106 case VNET_API_ERROR_NO_SUCH_ENTRY:
1107 return clib_error_return (0, "vss for table %d not found in pool.",
1110 return clib_error_return (0, "BUG: rv %d", rv);
1115 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
1116 .path = "set dhcpv6 vss",
1117 .short_help = "set dhcpv6 vss table <table-id> [oui <n> vpn-id <n> | vpn-ascii-id <text>]",
1118 .function = dhcpv6_vss_command_fn,
1122 static clib_error_t *
1123 dhcpv6_vss_show_command_fn (vlib_main_t * vm,
1124 unformat_input_t * input,
1125 vlib_cli_command_t * cmd)
1127 dhcp_vss_walk (FIB_PROTOCOL_IP6, dhcp_vss_show_walk, vm);
1133 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
1134 .path = "show dhcpv6 vss",
1135 .short_help = "show dhcpv6 VSS",
1136 .function = dhcpv6_vss_show_command_fn,
1140 static clib_error_t *
1141 dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1142 unformat_input_t * input,
1143 vlib_cli_command_t * cmd)
1145 vnet_main_t *vnm = vnet_get_main ();
1146 u32 sw_if_index0 = 0, sw_if_index;
1147 vnet_sw_interface_t *swif;
1150 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1153 if (unformat (input, "%U",
1154 unformat_vnet_sw_interface, vnm, &sw_if_index0))
1156 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1157 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1158 swif->unnumbered_sw_if_index : sw_if_index0;
1159 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
1162 vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
1164 vlib_cli_output (vm, "%=20U%=48U",
1165 format_vnet_sw_if_index_name, vnm,
1166 sw_if_index0, format_ip6_address, ia0);
1169 vlib_cli_output (vm, "%=34s%=20U",
1170 "No IPv6 address configured on",
1171 format_vnet_sw_if_index_name, vnm, sw_if_index);
1181 VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1182 .path = "show dhcpv6 link-address interface",
1183 .short_help = "show dhcpv6 link-address interface <interface>",
1184 .function = dhcpv6_link_address_show_command_fn,
1189 * fd.io coding-style-patch-verification: ON
1192 * eval: (c-set-style "gnu")