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 <dhcp/dhcp_proxy.h>
20 #include <dhcp/dhcp6_packet.h>
21 #include <vnet/mfib/mfib_table.h>
22 #include <vnet/mfib/ip6_mfib.h>
23 #include <vnet/fib/fib.h>
25 static char *dhcpv6_proxy_error_strings[] = {
26 #define dhcpv6_proxy_error(n,s) s,
27 #include <dhcp/dhcp6_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")
39 #define _(s,n) DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s,
40 foreach_dhcpv6_proxy_to_server_input_next
42 DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
43 } dhcpv6_proxy_to_server_input_next_t;
47 /* 0 => to server, 1 => to client */
52 u32 original_sw_if_index;
53 } dhcpv6_proxy_trace_t;
55 static vlib_node_registration_t dhcpv6_proxy_to_server_node;
56 static vlib_node_registration_t dhcpv6_proxy_to_client_node;
58 /* all DHCP servers address */
59 static ip6_address_t all_dhcpv6_server_address;
60 static ip6_address_t all_dhcpv6_server_relay_agent_address;
63 format_dhcpv6_proxy_trace (u8 * s, va_list * args)
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67 dhcpv6_proxy_trace_t *t = va_arg (*args, dhcpv6_proxy_trace_t *);
70 s = format (s, "DHCPV6 proxy: sent to server %U",
71 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
73 s = format (s, "DHCPV6 proxy: sent to client from %U",
74 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
75 if (t->error != (u32) ~ 0)
76 s = format (s, " error: %s\n", dhcpv6_proxy_error_strings[t->error]);
78 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
79 t->original_sw_if_index, t->sw_if_index);
85 format_dhcpv6_proxy_header_with_length (u8 * s, va_list * args)
87 dhcpv6_header_t *h = va_arg (*args, dhcpv6_header_t *);
88 u32 max_header_bytes = va_arg (*args, u32);
91 header_bytes = sizeof (h[0]);
92 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
93 return format (s, "dhcpv6 header truncated");
95 s = format (s, "DHCPV6 Proxy");
100 /* get first interface address */
101 static ip6_address_t *
102 ip6_interface_first_global_or_site_address (ip6_main_t * im, u32 sw_if_index)
104 ip_lookup_main_t *lm = &im->lookup_main;
105 ip_interface_address_t *ia = 0;
106 ip6_address_t *result = 0;
109 foreach_ip_interface_address (lm, ia, sw_if_index,
110 1 /* honor unnumbered */,
112 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
113 if ((a->as_u8[0] & 0xe0) == 0x20 ||
114 (a->as_u8[0] & 0xfe) == 0xfc) {
124 copy_ip6_address (ip6_address_t * dst, ip6_address_t * src)
126 dst->as_u64[0] = src->as_u64[0];
127 dst->as_u64[1] = src->as_u64[1];
131 dhcpv6_proxy_to_server_input (vlib_main_t * vm,
132 vlib_node_runtime_t * node,
133 vlib_frame_t * from_frame)
135 u32 n_left_from, next_index, *from, *to_next;
136 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
137 from = vlib_frame_vector_args (from_frame);
138 n_left_from = from_frame->n_vectors;
139 u32 pkts_to_server = 0, pkts_to_client = 0, pkts_no_server = 0;
140 u32 pkts_no_interface_address = 0, pkts_no_exceeding_max_hop = 0;
141 u32 pkts_no_src_address = 0;
142 u32 pkts_wrong_msg_type = 0;
143 u32 pkts_too_big = 0;
144 ip6_main_t *im = &ip6_main;
148 dhcp_server_t *server;
149 u32 rx_fib_idx = 0, server_fib_idx = 0;
151 next_index = node->cached_next_index;
153 while (n_left_from > 0)
157 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
159 while (n_left_from > 0 && n_left_to_next > 0)
161 vnet_main_t *vnm = vnet_get_main ();
163 u32 rx_sw_if_index = 0;
164 vnet_sw_interface_t *swif;
167 udp_header_t *u0, *u1;
168 dhcpv6_header_t *h0; // client msg hdr
169 ip6_header_t *ip0, *ip1;
170 ip6_address_t _ia0, *ia0 = &_ia0;
172 u32 error0 = (u32) ~ 0;
173 dhcpv6_option_t *fwd_opt;
174 dhcpv6_relay_hdr_t *r1;
176 dhcpv6_int_id_t *id1;
178 dhcpv6_client_mac_t *cmac; // client mac
179 ethernet_header_t *e_h0;
180 u8 client_src_mac[6];
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->msg_type)
203 case DHCPV6_MSG_SOLICIT:
204 case DHCPV6_MSG_REQUEST:
205 case DHCPV6_MSG_CONFIRM:
206 case DHCPV6_MSG_RENEW:
207 case DHCPV6_MSG_REBIND:
208 case DHCPV6_MSG_RELEASE:
209 case DHCPV6_MSG_DECLINE:
210 case DHCPV6_MSG_INFORMATION_REQUEST:
211 case DHCPV6_MSG_RELAY_FORW:
214 case DHCPV6_MSG_RELAY_REPL:
216 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
221 /* drop the packet */
222 pkts_wrong_msg_type++;
223 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
224 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
229 /* Send to DHCPV6 server via the configured FIB */
230 rx_sw_if_index = sw_if_index =
231 vnet_buffer (b0)->sw_if_index[VLIB_RX];
232 rx_fib_idx = im->mfib_index_by_sw_if_index[rx_sw_if_index];
233 proxy = dhcp_get_proxy (dpm, rx_fib_idx, FIB_PROTOCOL_IP6);
235 if (PREDICT_FALSE (NULL == proxy))
237 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
238 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
243 server = &proxy->dhcp_servers[0];
244 server_fib_idx = server->server_fib_index;
245 vnet_buffer (b0)->sw_if_index[VLIB_TX] = server_fib_idx;
248 /* relay-option header pointer */
249 vlib_buffer_advance (b0, -(sizeof (*fwd_opt)));
250 fwd_opt = vlib_buffer_get_current (b0);
251 /* relay message header pointer */
252 vlib_buffer_advance (b0, -(sizeof (*r1)));
253 r1 = vlib_buffer_get_current (b0);
255 vlib_buffer_advance (b0, -(sizeof (*u1)));
256 u1 = vlib_buffer_get_current (b0);
258 vlib_buffer_advance (b0, -(sizeof (*ip1)));
259 ip1 = vlib_buffer_get_current (b0);
261 /* fill in all that rubbish... */
262 len = clib_net_to_host_u16 (u0->length) - sizeof (udp_header_t);
263 copy_ip6_address (&r1->peer_addr, &ip0->src_address);
265 r1->msg_type = DHCPV6_MSG_RELAY_FORW;
266 fwd_opt->length = clib_host_to_net_u16 (len);
267 fwd_opt->option = clib_host_to_net_u16 (DHCPV6_OPTION_RELAY_MSG);
271 (h0->msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
273 if (PREDICT_FALSE (r1->hop_count >= HOP_COUNT_LIMIT))
275 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
276 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
277 pkts_no_exceeding_max_hop++;
282 /* If relay-fwd and src address is site or global unicast address */
283 if (h0->msg_type == DHCPV6_MSG_RELAY_FORW &&
284 ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
285 (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
287 /* Set link address to zero */
288 r1->link_addr.as_u64[0] = 0;
289 r1->link_addr.as_u64[1] = 0;
290 goto link_address_set;
293 /* if receiving interface is unnumbered, use receiving interface
294 * IP address as link address, otherwise use the loopback interface
295 * IP address as link address.
298 swif = vnet_get_sw_interface (vnm, rx_sw_if_index);
299 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
300 sw_if_index = swif->unnumbered_sw_if_index;
303 ip6_interface_first_global_or_site_address (&ip6_main,
307 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
308 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
309 pkts_no_interface_address++;
313 copy_ip6_address (&r1->link_addr, ia0);
317 if ((b0->current_data + b0->current_length + sizeof (*id1) +
318 sizeof (*vss1) + sizeof (*cmac)) >
319 vlib_buffer_get_default_data_size (vm))
321 error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
322 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
327 id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
328 b0->current_length += (sizeof (*id1));
330 id1->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_INTERFACE_ID);
331 id1->opt.length = clib_host_to_net_u16 (sizeof (rx_sw_if_index));
332 id1->int_idx = clib_host_to_net_u32 (rx_sw_if_index);
335 if (h0->msg_type != DHCPV6_MSG_RELAY_FORW)
338 (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
339 b0->current_length += (sizeof (*cmac));
340 cmac->opt.length = clib_host_to_net_u16 (sizeof (*cmac) -
344 (DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
345 cmac->link_type = clib_host_to_net_u16 (1); /* ethernet */
346 clib_memcpy (cmac->data, client_src_mac, 6);
347 u1->length += sizeof (*cmac);
350 vss = dhcp_get_vss_info (dpm, rx_fib_idx, FIB_PROTOCOL_IP6);
354 u16 id_len; /* length of VPN ID */
355 u16 type_len = sizeof (vss1->vss_type);
357 vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
358 vss1->vss_type = vss->vss_type;
359 if (vss->vss_type == VSS_TYPE_VPN_ID)
361 id_len = sizeof (vss->vpn_id); /* vpn_id is 7 bytes */
362 memcpy (vss1->data, vss->vpn_id, id_len);
364 else if (vss->vss_type == VSS_TYPE_ASCII)
366 id_len = vec_len (vss->vpn_ascii_id);
367 memcpy (vss1->data, vss->vpn_ascii_id, id_len);
369 else /* must be VSS_TYPE_DEFAULT, no VPN ID */
372 vss1->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_VSS);
373 vss1->opt.length = clib_host_to_net_u16 (type_len + id_len);
374 u1->length += type_len + id_len + sizeof (vss1->opt);
375 b0->current_length += type_len + id_len + sizeof (vss1->opt);
380 u1->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcpv6_to_client);
381 u1->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcpv6_to_server);
384 clib_host_to_net_u16 (clib_net_to_host_u16 (fwd_opt->length) +
385 sizeof (*r1) + sizeof (*fwd_opt) +
386 sizeof (*u1) + sizeof (*id1) + u1->length);
388 clib_memset (ip1, 0, sizeof (*ip1));
389 ip1->ip_version_traffic_class_and_flow_label = 0x60;
390 ip1->payload_length = u1->length;
391 ip1->protocol = PROTO_UDP;
392 ip1->hop_limit = HOP_COUNT_LIMIT;
393 src = ((server->dhcp_server.ip6.as_u64[0] ||
394 server->dhcp_server.ip6.as_u64[1]) ?
395 &server->dhcp_server.ip6 : &all_dhcpv6_server_address);
396 copy_ip6_address (&ip1->dst_address, src);
399 ia0 = ip6_interface_first_global_or_site_address
400 (&ip6_main, vnet_buffer (b0)->sw_if_index[VLIB_RX]);
402 src = (proxy->dhcp_src_address.ip6.as_u64[0] ||
403 proxy->dhcp_src_address.ip6.as_u64[1]) ?
404 &proxy->dhcp_src_address.ip6 : 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;
422 is_solicit = (DHCPV6_MSG_SOLICIT == h0->msg_type);
425 * If we have multiple servers configured and this is the
426 * client's discover message, then send copies to each of
429 if (is_solicit && vec_len (proxy->dhcp_servers) > 1)
433 for (ii = 1; ii < vec_len (proxy->dhcp_servers); ii++)
438 c0 = vlib_buffer_copy (vm, b0);
441 vlib_node_increment_counter
442 (vm, dhcpv6_proxy_to_server_node.index,
443 DHCPV6_PROXY_ERROR_ALLOC_FAIL, 1);
446 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (c0);
447 ci0 = vlib_get_buffer_index (vm, c0);
448 server = &proxy->dhcp_servers[ii];
450 ip0 = vlib_buffer_get_current (c0);
452 src = ((server->dhcp_server.ip6.as_u64[0] ||
453 server->dhcp_server.ip6.as_u64[1]) ?
454 &server->dhcp_server.ip6 :
455 &all_dhcpv6_server_address);
456 copy_ip6_address (&ip1->dst_address, src);
462 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
463 to_next, n_left_to_next,
466 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
468 dhcpv6_proxy_trace_t *tr;
470 tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
471 tr->which = 0; /* to server */
473 tr->original_sw_if_index = rx_sw_if_index;
474 tr->sw_if_index = sw_if_index;
475 if (next0 == DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
476 copy_ip6_address ((ip6_address_t *) &
478 &server->dhcp_server.ip6);
481 if (PREDICT_FALSE (0 == n_left_to_next))
483 vlib_put_next_frame (vm, node, next_index,
485 vlib_get_next_frame (vm, node, next_index,
486 to_next, n_left_to_next);
492 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
494 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
496 tr->which = 0; /* to server */
498 tr->original_sw_if_index = rx_sw_if_index;
499 tr->sw_if_index = sw_if_index;
500 if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)
501 copy_ip6_address ((ip6_address_t *) & tr->packet_data[0],
502 &server->dhcp_server.ip6);
510 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
511 to_next, n_left_to_next,
515 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
518 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
519 DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
521 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
522 DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
524 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
525 DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
526 pkts_no_interface_address);
527 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
528 DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
529 pkts_wrong_msg_type);
530 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
531 DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
532 pkts_no_src_address);
533 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
534 DHCPV6_PROXY_ERROR_PKT_TOO_BIG, pkts_too_big);
535 return from_frame->n_vectors;
539 VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node, static) = {
540 .function = dhcpv6_proxy_to_server_input,
541 .name = "dhcpv6-proxy-to-server",
542 /* Takes a vector of packets. */
543 .vector_size = sizeof (u32),
545 .n_errors = DHCPV6_PROXY_N_ERROR,
546 .error_strings = dhcpv6_proxy_error_strings,
548 .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
550 #define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
551 foreach_dhcpv6_proxy_to_server_input_next
555 .format_buffer = format_dhcpv6_proxy_header_with_length,
556 .format_trace = format_dhcpv6_proxy_trace,
558 .unformat_buffer = unformat_dhcpv6_proxy_header,
564 dhcpv6_proxy_to_client_input (vlib_main_t * vm,
565 vlib_node_runtime_t * node,
566 vlib_frame_t * from_frame)
569 u32 n_left_from, *from;
570 ethernet_main_t *em = vnet_get_ethernet_main ();
571 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
573 dhcp_server_t *server;
574 vnet_main_t *vnm = vnet_get_main ();
577 from = vlib_frame_vector_args (from_frame);
578 n_left_from = from_frame->n_vectors;
580 while (n_left_from > 0)
584 udp_header_t *u0, *u1 = 0;
585 dhcpv6_relay_hdr_t *h0;
586 ip6_header_t *ip1 = 0, *ip0;
587 ip6_address_t _ia0, *ia0 = &_ia0;
588 ip6_address_t client_address;
589 ethernet_interface_t *ei0;
590 ethernet_header_t *mac0;
591 vnet_hw_interface_t *hi0;
594 u32 sw_if_index = ~0;
595 u32 original_sw_if_index = ~0;
596 vnet_sw_interface_t *si0;
597 u32 inner_vlan = (u32) ~ 0;
598 u32 outer_vlan = (u32) ~ 0;
599 u32 error0 = (u32) ~ 0;
600 vnet_sw_interface_t *swif;
601 dhcpv6_option_t *r0 = 0, *o;
603 u8 interface_opt_flag = 0;
604 u8 relay_msg_opt_flag = 0;
605 ip6_main_t *im = &ip6_main;
606 u32 server_fib_idx, client_fib_idx;
612 b0 = vlib_get_buffer (vm, bi0);
613 h0 = vlib_buffer_get_current (b0);
615 if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
617 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
620 vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
623 f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index);
624 to_next0 = vlib_frame_vector_args (f0);
627 vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0);
630 /* hop count seems not need to be checked */
631 if (HOP_COUNT_LIMIT < h0->hop_count)
633 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
636 u0 = (void *) h0 - (sizeof (*u0));
637 ip0 = (void *) u0 - (sizeof (*ip0));
639 vlib_buffer_advance (b0, sizeof (*h0));
640 o = vlib_buffer_get_current (b0);
642 /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID)
643 _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there.
644 Currently assuming no other options need to be processed
645 The interface-ID is the FIB number we need
646 to track down the client-facing interface */
648 while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
650 if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16 (o->option))
652 interface_opt_flag = 1;
653 if (clib_net_to_host_u16 (o->length) == sizeof (sw_if_index))
655 clib_net_to_host_u32 (((dhcpv6_int_id_t *) o)->int_idx);
656 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
658 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
662 if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16 (o->option))
664 relay_msg_opt_flag = 1;
665 r0 = vlib_buffer_get_current (b0);
667 if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1))
669 vlib_buffer_advance (b0,
671 clib_net_to_host_u16 (o->length));
673 (dhcpv6_option_t *) (((uword) o) +
674 clib_net_to_host_u16 (o->length) +
678 if ((relay_msg_opt_flag == 0) || (r0 == 0))
680 error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION;
684 if ((u32) ~ 0 == sw_if_index)
686 error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
690 //Advance buffer to start of encapsulated DHCPv6 message
691 vlib_buffer_advance (b0, sizeof (*r0));
693 client_fib_idx = im->mfib_index_by_sw_if_index[sw_if_index];
694 proxy = dhcp_get_proxy (dm, client_fib_idx, FIB_PROTOCOL_IP6);
698 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
702 server_fib_idx = im->fib_index_by_sw_if_index
703 [vnet_buffer (b0)->sw_if_index[VLIB_RX]];
705 vec_foreach (server, proxy->dhcp_servers)
707 if (server_fib_idx == server->server_fib_index &&
708 ip0->src_address.as_u64[0] == server->dhcp_server.ip6.as_u64[0] &&
709 ip0->src_address.as_u64[1] == server->dhcp_server.ip6.as_u64[1])
715 //drop packet if not from server with configured address or FIB
716 error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
720 vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index
723 swif = vnet_get_sw_interface (vnm, original_sw_if_index);
724 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
725 sw_if_index = swif->unnumbered_sw_if_index;
729 * udp_local hands us the DHCPV6 header, need udp hdr,
730 * ip hdr to relay to client
732 vlib_buffer_advance (b0, -(sizeof (*u1)));
733 u1 = vlib_buffer_get_current (b0);
735 vlib_buffer_advance (b0, -(sizeof (*ip1)));
736 ip1 = vlib_buffer_get_current (b0);
738 copy_ip6_address (&client_address, &h0->peer_addr);
740 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
743 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
747 len = clib_net_to_host_u16 (r0->length);
748 clib_memset (ip1, 0, sizeof (*ip1));
749 copy_ip6_address (&ip1->dst_address, &client_address);
751 u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);
752 u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
753 u1->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
755 ip1->ip_version_traffic_class_and_flow_label =
756 ip0->ip_version_traffic_class_and_flow_label & 0x00000fff;
757 ip1->payload_length = u1->length;
758 ip1->protocol = PROTO_UDP;
759 ip1->hop_limit = HOP_COUNT_LIMIT;
760 copy_ip6_address (&ip1->src_address, ia0);
762 u1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip1,
764 ASSERT (bogus_length == 0);
766 vlib_buffer_advance (b0, -(sizeof (ethernet_header_t)));
767 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
768 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
770 if (si0->sub.eth.flags.one_tag == 1)
772 vlib_buffer_advance (b0, -4 /* space for 1 VLAN tag */ );
773 outer_vlan = (si0->sub.eth.outer_vlan_id << 16) | 0x86dd;
775 else if (si0->sub.eth.flags.two_tags == 1)
777 vlib_buffer_advance (b0, -8 /* space for 2 VLAN tag */ );
778 outer_vlan = (si0->sub.eth.outer_vlan_id << 16) | 0x8100;
779 inner_vlan = (si0->sub.eth.inner_vlan_id << 16) | 0x86dd;
783 mac0 = vlib_buffer_get_current (b0);
785 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
786 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
787 clib_memcpy (mac0->src_address, &ei0->address,
788 sizeof (mac0->src_address));
789 clib_memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
791 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB && outer_vlan != (u32) ~ 0)
793 mac0->type = (si0->sub.eth.flags.dot1ad == 1) ?
794 clib_net_to_host_u16 (0x88a8) : clib_net_to_host_u16 (0x8100);
795 u32 *vlan_tag = (u32 *) (mac0 + 1);
796 *vlan_tag = clib_host_to_net_u32 (outer_vlan);
797 if (inner_vlan != (u32) ~ 0)
799 u32 *inner_vlan_tag = (u32 *) (vlan_tag + 1);
800 *inner_vlan_tag = clib_host_to_net_u32 (inner_vlan);
805 mac0->type = clib_net_to_host_u16 (0x86dd);
808 /* $$$ consider adding a dynamic next to the graph node, for performance */
809 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
810 to_next0 = vlib_frame_vector_args (f0);
813 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
816 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
818 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
820 tr->which = 1; /* to client */
822 copy_ip6_address ((ip6_address_t *) tr->packet_data, ia0);
824 tr->original_sw_if_index = original_sw_if_index;
825 tr->sw_if_index = sw_if_index;
828 return from_frame->n_vectors;
833 VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node, static) = {
834 .function = dhcpv6_proxy_to_client_input,
835 .name = "dhcpv6-proxy-to-client",
836 /* Takes a vector of packets. */
837 .vector_size = sizeof (u32),
839 .n_errors = DHCPV6_PROXY_N_ERROR,
840 .error_strings = dhcpv6_proxy_error_strings,
841 .format_buffer = format_dhcpv6_proxy_header_with_length,
842 .format_trace = format_dhcpv6_proxy_trace,
844 .unformat_buffer = unformat_dhcpv6_proxy_header,
849 static clib_error_t *
850 dhcp6_proxy_init (vlib_main_t * vm)
852 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
853 vlib_node_t *error_drop_node;
855 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
856 dm->error_drop_node_index = error_drop_node->index;
858 /* RFC says this is the dhcpv6 server address */
859 all_dhcpv6_server_address.as_u64[0] =
860 clib_host_to_net_u64 (0xFF05000000000000);
861 all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
863 /* RFC says this is the server and agent address */
864 all_dhcpv6_server_relay_agent_address.as_u64[0] =
865 clib_host_to_net_u64 (0xFF02000000000000);
866 all_dhcpv6_server_relay_agent_address.as_u64[1] =
867 clib_host_to_net_u64 (0x00010002);
872 VLIB_INIT_FUNCTION (dhcp6_proxy_init);
875 dhcp6_proxy_set_server (ip46_address_t * addr,
876 ip46_address_t * src_addr,
877 u32 rx_table_id, u32 server_table_id, int is_del)
879 vlib_main_t *vm = vlib_get_main ();
880 u32 rx_fib_index = 0;
883 const mfib_prefix_t all_dhcp_servers = {
885 .fp_proto = FIB_PROTOCOL_IP6,
887 .ip6 = all_dhcpv6_server_relay_agent_address,
891 if (ip46_address_is_zero (addr))
892 return VNET_API_ERROR_INVALID_DST_ADDRESS;
894 if (ip46_address_is_zero (src_addr))
895 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
897 rx_fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
903 if (dhcp_proxy_server_del (FIB_PROTOCOL_IP6, rx_fib_index,
904 addr, server_table_id))
906 mfib_table_entry_delete (rx_fib_index,
907 &all_dhcp_servers, MFIB_SOURCE_DHCP);
908 mfib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP6,
911 udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
913 udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
919 const fib_route_path_t path_for_us = {
920 .frp_proto = DPO_PROTO_IP6,
921 .frp_addr = zero_addr,
922 .frp_sw_if_index = 0xffffffff,
925 .frp_flags = FIB_ROUTE_PATH_LOCAL,
926 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
928 if (dhcp_proxy_server_add (FIB_PROTOCOL_IP6, addr, src_addr,
929 rx_fib_index, server_table_id))
931 mfib_table_entry_path_update (rx_fib_index,
933 MFIB_SOURCE_DHCP, &path_for_us);
935 * Each interface that is enabled in this table, needs to be added
936 * as an accepting interface, but this is not easily doable in VPP.
937 * So we cheat. Add a flag to the entry that indicates accept form
939 * We will still only accept on v6 enabled interfaces, since the
940 * input feature ensures this.
942 mfib_table_entry_update (rx_fib_index,
946 MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF);
947 mfib_table_lock (rx_fib_index, FIB_PROTOCOL_IP6, MFIB_SOURCE_DHCP);
949 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
950 dhcpv6_proxy_to_client_node.index,
952 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
953 dhcpv6_proxy_to_server_node.index,
958 mfib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP6, MFIB_SOURCE_DHCP);
963 static clib_error_t *
964 dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
965 unformat_input_t * input,
966 vlib_cli_command_t * cmd)
968 ip46_address_t addr, src_addr;
969 int set_server = 0, set_src_address = 0;
970 u32 rx_table_id = 0, server_table_id = 0;
973 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
975 if (unformat (input, "server %U", unformat_ip6_address, &addr.ip6))
977 else if (unformat (input, "src-address %U",
978 unformat_ip6_address, &src_addr.ip6))
980 else if (unformat (input, "server-fib-id %d", &server_table_id))
982 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
984 else if (unformat (input, "delete") || unformat (input, "del"))
990 if (is_del || (set_server && set_src_address))
994 rv = dhcp6_proxy_set_server (&addr, &src_addr, rx_table_id,
995 server_table_id, is_del);
997 //TODO: Complete the errors
1003 case VNET_API_ERROR_INVALID_DST_ADDRESS:
1004 return clib_error_return (0, "Invalid server address");
1006 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
1007 return clib_error_return (0, "Invalid src address");
1009 case VNET_API_ERROR_NO_SUCH_ENTRY:
1010 return clib_error_return
1011 (0, "Fib id %d: no per-fib DHCP server configured", rx_table_id);
1014 return clib_error_return (0, "BUG: rv %d", rv);
1018 return clib_error_return (0, "parse error`%U'",
1019 format_unformat_error, input);
1023 VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
1024 .path = "set dhcpv6 proxy",
1025 .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> "
1026 "[server-fib-id <fib-id>] [rx-fib-id <fib-id>] ",
1027 .function = dhcpv6_proxy_set_command_fn,
1032 format_dhcp6_proxy_server (u8 * s, va_list * args)
1034 dhcp_proxy_t *proxy = va_arg (*args, dhcp_proxy_t *);
1035 fib_table_t *server_fib;
1036 dhcp_server_t *server;
1041 s = format (s, "%=14s%=16s%s", "RX FIB", "Src Address",
1042 "Servers FIB,Address");
1046 rx_fib = ip6_mfib_get (proxy->rx_fib_index);
1048 s = format (s, "%=14u%=16U",
1050 format_ip46_address, &proxy->dhcp_src_address, IP46_TYPE_ANY);
1052 vec_foreach (server, proxy->dhcp_servers)
1054 server_fib = fib_table_get (server->server_fib_index, FIB_PROTOCOL_IP6);
1055 s = format (s, "%u,%U ",
1056 server_fib->ft_table_id,
1057 format_ip46_address, &server->dhcp_server, IP46_TYPE_ANY);
1064 dhcp6_proxy_show_walk (dhcp_proxy_t * proxy, void *ctx)
1066 vlib_main_t *vm = ctx;
1068 vlib_cli_output (vm, "%U", format_dhcp6_proxy_server, proxy);
1073 static clib_error_t *
1074 dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
1075 unformat_input_t * input,
1076 vlib_cli_command_t * cmd)
1078 vlib_cli_output (vm, "%U", format_dhcp6_proxy_server,
1079 NULL /* header line */ );
1081 dhcp_proxy_walk (FIB_PROTOCOL_IP6, dhcp6_proxy_show_walk, vm);
1087 VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
1088 .path = "show dhcpv6 proxy",
1089 .short_help = "Display dhcpv6 proxy info",
1090 .function = dhcpv6_proxy_show_command_fn,
1094 static clib_error_t *
1095 dhcpv6_vss_command_fn (vlib_main_t * vm,
1096 unformat_input_t * input, vlib_cli_command_t * cmd)
1098 u8 is_del = 0, vss_type = VSS_TYPE_DEFAULT;
1099 u8 *vpn_ascii_id = 0;
1100 u32 oui = 0, fib_id = 0, tbl_id = ~0;
1102 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1104 if (unformat (input, "table %d", &tbl_id))
1106 else if (unformat (input, "oui %d", &oui))
1107 vss_type = VSS_TYPE_VPN_ID;
1108 else if (unformat (input, "vpn-id %d", &fib_id))
1109 vss_type = VSS_TYPE_VPN_ID;
1110 else if (unformat (input, "vpn-ascii-id %s", &vpn_ascii_id))
1111 vss_type = VSS_TYPE_ASCII;
1112 else if (unformat (input, "delete") || unformat (input, "del"))
1119 return clib_error_return (0, "no table ID specified.");
1121 int rv = dhcp_proxy_set_vss (FIB_PROTOCOL_IP6, tbl_id, vss_type,
1122 vpn_ascii_id, oui, fib_id, is_del);
1127 case VNET_API_ERROR_NO_SUCH_ENTRY:
1128 return clib_error_return (0, "vss for table %d not found in pool.",
1131 return clib_error_return (0, "BUG: rv %d", rv);
1136 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
1137 .path = "set dhcpv6 vss",
1138 .short_help = "set dhcpv6 vss table <table-id> [oui <n> vpn-id <n> | vpn-ascii-id <text>]",
1139 .function = dhcpv6_vss_command_fn,
1143 static clib_error_t *
1144 dhcpv6_vss_show_command_fn (vlib_main_t * vm,
1145 unformat_input_t * input,
1146 vlib_cli_command_t * cmd)
1148 dhcp_vss_walk (FIB_PROTOCOL_IP6, dhcp_vss_show_walk, vm);
1154 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
1155 .path = "show dhcpv6 vss",
1156 .short_help = "show dhcpv6 VSS",
1157 .function = dhcpv6_vss_show_command_fn,
1161 static clib_error_t *
1162 dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1163 unformat_input_t * input,
1164 vlib_cli_command_t * cmd)
1166 vnet_main_t *vnm = vnet_get_main ();
1167 u32 sw_if_index0 = 0, sw_if_index;
1168 vnet_sw_interface_t *swif;
1171 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1174 if (unformat (input, "%U",
1175 unformat_vnet_sw_interface, vnm, &sw_if_index0))
1177 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1178 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1179 swif->unnumbered_sw_if_index : sw_if_index0;
1180 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
1183 vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
1185 vlib_cli_output (vm, "%=20U%=48U",
1186 format_vnet_sw_if_index_name, vnm,
1187 sw_if_index0, format_ip6_address, ia0);
1190 vlib_cli_output (vm, "%=34s%=20U",
1191 "No IPv6 address configured on",
1192 format_vnet_sw_if_index_name, vnm, sw_if_index);
1202 VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1203 .path = "show dhcpv6 link-address interface",
1204 .short_help = "show dhcpv6 link-address interface <interface>",
1205 .function = dhcpv6_link_address_show_command_fn,
1210 * fd.io coding-style-patch-verification: ON
1213 * eval: (c-set-style "gnu")