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 <vnet/dhcp/dhcp_proxy.h>
21 #include <vnet/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 <vnet/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")
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;
46 /* 0 => to server, 1 => to client */
51 u32 original_sw_if_index;
52 } dhcpv6_proxy_trace_t;
54 static vlib_node_registration_t dhcpv6_proxy_to_server_node;
55 static vlib_node_registration_t dhcpv6_proxy_to_client_node;
57 /* all DHCP servers address */
58 static ip6_address_t all_dhcpv6_server_address;
59 static ip6_address_t all_dhcpv6_server_relay_agent_address;
62 format_dhcpv6_proxy_trace (u8 * s, va_list * args)
64 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
65 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
66 dhcpv6_proxy_trace_t * t = va_arg (*args, dhcpv6_proxy_trace_t *);
69 s = format (s, "DHCPV6 proxy: sent to server %U",
70 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
72 s = format (s, "DHCPV6 proxy: sent to client from %U",
73 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
74 if (t->error != (u32)~0)
75 s = format (s, " error: %s\n", dhcpv6_proxy_error_strings[t->error]);
77 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
78 t->original_sw_if_index, t->sw_if_index);
84 format_dhcpv6_proxy_header_with_length (u8 * s, va_list * args)
86 dhcpv6_header_t * h = va_arg (*args, dhcpv6_header_t *);
87 u32 max_header_bytes = va_arg (*args, u32);
90 header_bytes = sizeof (h[0]);
91 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
92 return format (s, "dhcpv6 header truncated");
94 s = format (s, "DHCPV6 Proxy");
98 /* get first interface address */
99 static ip6_address_t *
100 ip6_interface_first_global_or_site_address (ip6_main_t * im, u32 sw_if_index)
102 ip_lookup_main_t * lm = &im->lookup_main;
103 ip_interface_address_t * ia = 0;
104 ip6_address_t * result = 0;
106 foreach_ip_interface_address (lm, ia, sw_if_index,
107 1 /* honor unnumbered */,
109 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
110 if ((a->as_u8[0] & 0xe0) == 0x20 ||
111 (a->as_u8[0] & 0xfe) == 0xfc) {
119 static inline void copy_ip6_address (ip6_address_t *dst,
122 dst->as_u64[0] = src->as_u64[0];
123 dst->as_u64[1] = src->as_u64[1];
127 dhcpv6_proxy_to_server_input (vlib_main_t * vm,
128 vlib_node_runtime_t * node,
129 vlib_frame_t * from_frame)
131 u32 n_left_from, next_index, * from, * to_next;
132 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
133 from = vlib_frame_vector_args (from_frame);
134 n_left_from = from_frame->n_vectors;
135 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
136 u32 pkts_no_interface_address=0, pkts_no_exceeding_max_hop=0;
137 u32 pkts_no_src_address=0;
138 u32 pkts_wrong_msg_type=0;
140 ip6_main_t * im = &ip6_main;
144 dhcp_server_t *server;
145 u32 rx_fib_idx = 0, server_fib_idx = 0;
147 next_index = node->cached_next_index;
149 while (n_left_from > 0)
153 vlib_get_next_frame (vm, node, next_index,
154 to_next, n_left_to_next);
156 while (n_left_from > 0 && n_left_to_next > 0)
158 vnet_main_t *vnm = vnet_get_main();
160 u32 rx_sw_if_index = 0;
161 vnet_sw_interface_t *swif;
164 udp_header_t * u0, *u1;
165 dhcpv6_header_t * h0; // client msg hdr
166 ip6_header_t * ip0, *ip1;
167 ip6_address_t _ia0, *ia0=&_ia0;
169 u32 error0 = (u32) ~0;
170 dhcpv6_option_t *fwd_opt;
171 dhcpv6_relay_hdr_t *r1;
173 dhcpv6_int_id_t *id1;
175 dhcpv6_client_mac_t *cmac; // client mac
176 ethernet_header_t * e_h0;
177 u8 client_src_mac[6];
178 vlib_buffer_free_list_t *fl;
186 b0 = vlib_get_buffer (vm, bi0);
188 h0 = vlib_buffer_get_current (b0);
191 * udp_local hands us the DHCPV6 header.
193 u0 = (void *)h0 -(sizeof(*u0));
194 ip0 = (void *)u0 -(sizeof(*ip0));
195 e_h0 = (void *)ip0 - ethernet_buffer_header_size(b0);
197 clib_memcpy(client_src_mac, e_h0->src_address, 6);
199 switch (h0->u.msg_type) {
200 case DHCPV6_MSG_SOLICIT:
201 case DHCPV6_MSG_REQUEST:
202 case DHCPV6_MSG_CONFIRM:
203 case DHCPV6_MSG_RENEW:
204 case DHCPV6_MSG_REBIND:
205 case DHCPV6_MSG_RELEASE:
206 case DHCPV6_MSG_DECLINE:
207 case DHCPV6_MSG_INFORMATION_REQUEST:
208 case DHCPV6_MSG_RELAY_FORW:
211 case DHCPV6_MSG_RELAY_REPL:
213 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
218 /* drop the packet */
219 pkts_wrong_msg_type++;
220 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
221 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
226 /* Send to DHCPV6 server via the configured FIB */
227 rx_sw_if_index = sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
228 rx_fib_idx = im->mfib_index_by_sw_if_index [rx_sw_if_index];
229 proxy = dhcp_get_proxy(dpm, rx_fib_idx, FIB_PROTOCOL_IP6);
231 if (PREDICT_FALSE (NULL == proxy))
233 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
234 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
239 server = &proxy->dhcp_servers[0];
240 server_fib_idx = server->server_fib_index;
241 vnet_buffer(b0)->sw_if_index[VLIB_TX] = server_fib_idx;
244 /* relay-option header pointer */
245 vlib_buffer_advance(b0, -(sizeof(*fwd_opt)));
246 fwd_opt = vlib_buffer_get_current(b0);
247 /* relay message header pointer */
248 vlib_buffer_advance(b0, -(sizeof(*r1)));
249 r1 = vlib_buffer_get_current(b0);
251 vlib_buffer_advance(b0, -(sizeof(*u1)));
252 u1 = vlib_buffer_get_current(b0);
254 vlib_buffer_advance(b0, -(sizeof(*ip1)));
255 ip1 = vlib_buffer_get_current(b0);
257 /* fill in all that rubbish... */
258 len = clib_net_to_host_u16(u0->length) - sizeof(udp_header_t);
259 copy_ip6_address(&r1->peer_addr, &ip0->src_address);
261 r1->msg_type = DHCPV6_MSG_RELAY_FORW;
262 fwd_opt->length = clib_host_to_net_u16(len);
263 fwd_opt->option = clib_host_to_net_u16(DHCPV6_OPTION_RELAY_MSG);
266 r1->hop_count = (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
268 if (PREDICT_FALSE(r1->hop_count >= HOP_COUNT_LIMIT))
270 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
271 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
272 pkts_no_exceeding_max_hop++;
277 /* If relay-fwd and src address is site or global unicast address */
278 if (h0->u.msg_type == DHCPV6_MSG_RELAY_FORW &&
279 ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
280 (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
282 /* Set link address to zero */
283 r1->link_addr.as_u64[0] = 0;
284 r1->link_addr.as_u64[1] = 0;
285 goto link_address_set;
288 /* if receiving interface is unnumbered, use receiving interface
289 * IP address as link address, otherwise use the loopback interface
290 * IP address as link address.
293 swif = vnet_get_sw_interface (vnm, rx_sw_if_index);
294 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
295 sw_if_index = swif->unnumbered_sw_if_index;
297 ia0 = ip6_interface_first_global_or_site_address(&ip6_main, sw_if_index);
300 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
301 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
302 pkts_no_interface_address++;
306 copy_ip6_address(&r1->link_addr, ia0);
309 fl = vlib_buffer_get_free_list (vm, vlib_buffer_get_free_list_index (b0));
311 if ((b0->current_length+sizeof(*id1)+sizeof(*vss1)+sizeof(*cmac))
314 error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
315 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
320 id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
321 b0->current_length += (sizeof (*id1));
323 id1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_INTERFACE_ID);
324 id1->opt.length = clib_host_to_net_u16(sizeof(rx_sw_if_index));
325 id1->int_idx = clib_host_to_net_u32(rx_sw_if_index);
328 if (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW)
330 cmac = (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
331 b0->current_length += (sizeof (*cmac));
332 cmac->opt.length =clib_host_to_net_u16(sizeof(*cmac) -
334 cmac->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
335 cmac->link_type = clib_host_to_net_u16(1); // ethernet
336 clib_memcpy(cmac->data, client_src_mac, 6);
337 u1->length += sizeof(*cmac);
340 vss = dhcp_get_vss_info(dpm, rx_fib_idx, FIB_PROTOCOL_IP6);
343 vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
344 b0->current_length += (sizeof (*vss1));
345 vss1->opt.length =clib_host_to_net_u16(sizeof(*vss1) -
347 vss1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_VSS);
348 vss1->data[0] = 1; // type
349 vss1->data[1] = vss->oui >>16 & 0xff;
350 vss1->data[2] = vss->oui >>8 & 0xff;
351 vss1->data[3] = vss->oui & 0xff;
352 vss1->data[4] = vss->fib_id >> 24 & 0xff;
353 vss1->data[5] = vss->fib_id >> 16 & 0xff;
354 vss1->data[6] = vss->fib_id >> 8 & 0xff;
355 vss1->data[7] = vss->fib_id & 0xff;
356 u1->length += sizeof(*vss1);
361 u1->src_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_client);
362 u1->dst_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_server);
365 clib_host_to_net_u16( clib_net_to_host_u16(fwd_opt->length) +
366 sizeof(*r1) + sizeof(*fwd_opt) +
367 sizeof(*u1) + sizeof(*id1) + u1->length);
369 memset(ip1, 0, sizeof(*ip1));
370 ip1->ip_version_traffic_class_and_flow_label = 0x60;
371 ip1->payload_length = u1->length;
372 ip1->protocol = PROTO_UDP;
373 ip1->hop_limit = HOP_COUNT_LIMIT;
374 src = ((server->dhcp_server.ip6.as_u64[0] ||
375 server->dhcp_server.ip6.as_u64[1]) ?
376 &server->dhcp_server.ip6 :
377 &all_dhcpv6_server_address);
378 copy_ip6_address(&ip1->dst_address, src);
381 ia0 = ip6_interface_first_global_or_site_address
382 (&ip6_main, vnet_buffer(b0)->sw_if_index[VLIB_RX]);
384 src = (proxy->dhcp_src_address.ip6.as_u64[0] ||
385 proxy->dhcp_src_address.ip6.as_u64[1]) ?
386 &proxy->dhcp_src_address.ip6 : ia0;
389 error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS;
390 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
391 pkts_no_src_address++;
395 copy_ip6_address (&ip1->src_address, src);
398 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
400 ASSERT(bogus_length == 0);
402 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
404 is_solicit = (DHCPV6_MSG_SOLICIT == h0->u.msg_type);
407 * If we have multiple servers configured and this is the
408 * client's discover message, then send copies to each of
411 if (is_solicit && vec_len(proxy->dhcp_servers) > 1)
415 for (ii = 1; ii < vec_len(proxy->dhcp_servers); ii++)
420 c0 = vlib_buffer_copy(vm, b0);
421 ci0 = vlib_get_buffer_index(vm, c0);
422 server = &proxy->dhcp_servers[ii];
424 ip0 = vlib_buffer_get_current (c0);
426 src = ((server->dhcp_server.ip6.as_u64[0] ||
427 server->dhcp_server.ip6.as_u64[1]) ?
428 &server->dhcp_server.ip6 :
429 &all_dhcpv6_server_address);
430 copy_ip6_address(&ip1->dst_address, src);
436 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
437 to_next, n_left_to_next,
440 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
442 dhcpv6_proxy_trace_t *tr;
444 tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
445 tr->which = 0; /* to server */
447 tr->original_sw_if_index = rx_sw_if_index;
448 tr->sw_if_index = sw_if_index;
449 if (next0 == DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
450 copy_ip6_address((ip6_address_t *)&tr->packet_data[0],
451 &server->dhcp_server.ip6);
454 if (PREDICT_FALSE(0 == n_left_to_next))
456 vlib_put_next_frame (vm, node, next_index,
458 vlib_get_next_frame (vm, node, next_index,
459 to_next, n_left_to_next);
465 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
467 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
469 tr->which = 0; /* to server */
471 tr->original_sw_if_index = rx_sw_if_index;
472 tr->sw_if_index = sw_if_index;
473 if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)
474 copy_ip6_address((ip6_address_t *)&tr->packet_data[0], &server->dhcp_server.ip6);
482 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
483 to_next, n_left_to_next,
487 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
490 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
491 DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
493 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
494 DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
496 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
497 DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
498 pkts_no_interface_address);
499 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
500 DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
501 pkts_wrong_msg_type);
502 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
503 DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
504 pkts_no_src_address);
505 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
506 DHCPV6_PROXY_ERROR_PKT_TOO_BIG,
508 return from_frame->n_vectors;
511 VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node, static) = {
512 .function = dhcpv6_proxy_to_server_input,
513 .name = "dhcpv6-proxy-to-server",
514 /* Takes a vector of packets. */
515 .vector_size = sizeof (u32),
517 .n_errors = DHCPV6_PROXY_N_ERROR,
518 .error_strings = dhcpv6_proxy_error_strings,
520 .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
522 #define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
523 foreach_dhcpv6_proxy_to_server_input_next
527 .format_buffer = format_dhcpv6_proxy_header_with_length,
528 .format_trace = format_dhcpv6_proxy_trace,
530 .unformat_buffer = unformat_dhcpv6_proxy_header,
535 dhcpv6_proxy_to_client_input (vlib_main_t * vm,
536 vlib_node_runtime_t * node,
537 vlib_frame_t * from_frame)
540 u32 n_left_from, * from;
541 ethernet_main_t *em = ethernet_get_main (vm);
542 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
544 dhcp_server_t *server;
545 vnet_main_t * vnm = vnet_get_main();
548 from = vlib_frame_vector_args (from_frame);
549 n_left_from = from_frame->n_vectors;
551 while (n_left_from > 0)
555 udp_header_t * u0, *u1=0;
556 dhcpv6_relay_hdr_t * h0;
557 ip6_header_t * ip1 = 0, *ip0;
558 ip6_address_t _ia0, * ia0 = &_ia0;
559 ip6_address_t client_address;
560 ethernet_interface_t *ei0;
561 ethernet_header_t *mac0;
562 vnet_hw_interface_t *hi0;
565 u32 sw_if_index = ~0;
566 u32 original_sw_if_index = ~0;
567 vnet_sw_interface_t *si0;
568 u32 error0 = (u32)~0;
569 vnet_sw_interface_t *swif;
570 dhcpv6_option_t *r0 = 0, *o;
572 u8 interface_opt_flag = 0;
573 u8 relay_msg_opt_flag = 0;
574 ip6_main_t * im = &ip6_main;
575 u32 server_fib_idx, client_fib_idx;
581 b0 = vlib_get_buffer (vm, bi0);
582 h0 = vlib_buffer_get_current (b0);
584 if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
586 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
589 vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
592 f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index);
593 to_next0 = vlib_frame_vector_args (f0);
596 vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0);
599 /* hop count seems not need to be checked */
600 if (HOP_COUNT_LIMIT < h0->hop_count)
602 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
605 u0 = (void *)h0 -(sizeof(*u0));
606 ip0 = (void *)u0 -(sizeof(*ip0));
608 vlib_buffer_advance (b0, sizeof(*h0));
609 o = vlib_buffer_get_current (b0);
611 /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID)
612 _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there.
613 Currently assuming no other options need to be processed
614 The interface-ID is the FIB number we need
615 to track down the client-facing interface */
617 while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
619 if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16(o->option))
621 interface_opt_flag = 1;
622 if (clib_net_to_host_u16(o->length) == sizeof(sw_if_index))
623 sw_if_index = clib_net_to_host_u32(((dhcpv6_int_id_t*)o)->int_idx);
624 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
626 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
630 if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16(o->option))
632 relay_msg_opt_flag = 1;
633 r0 = vlib_buffer_get_current (b0);
635 if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1))
637 vlib_buffer_advance (b0, sizeof(*o) + clib_net_to_host_u16(o->length));
638 o = (dhcpv6_option_t *) (((uword) o) + clib_net_to_host_u16(o->length) + sizeof(*o));
641 if ((relay_msg_opt_flag == 0) || (r0 == 0))
643 error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION;
647 if ((u32)~0 == sw_if_index)
649 error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
653 //Advance buffer to start of encapsulated DHCPv6 message
654 vlib_buffer_advance (b0, sizeof(*r0));
656 client_fib_idx = im->mfib_index_by_sw_if_index[sw_if_index];
657 proxy = dhcp_get_proxy(dm, client_fib_idx, FIB_PROTOCOL_IP6);
661 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
665 server_fib_idx = im->fib_index_by_sw_if_index
666 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
668 vec_foreach(server, proxy->dhcp_servers)
670 if (server_fib_idx == server->server_fib_index &&
671 ip0->src_address.as_u64[0] == server->dhcp_server.ip6.as_u64[0] &&
672 ip0->src_address.as_u64[1] == server->dhcp_server.ip6.as_u64[1])
678 //drop packet if not from server with configured address or FIB
679 error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
683 vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index
686 swif = vnet_get_sw_interface (vnm, original_sw_if_index);
687 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
688 sw_if_index = swif->unnumbered_sw_if_index;
692 * udp_local hands us the DHCPV6 header, need udp hdr,
693 * ip hdr to relay to client
695 vlib_buffer_advance (b0, -(sizeof(*u1)));
696 u1 = vlib_buffer_get_current (b0);
698 vlib_buffer_advance (b0, -(sizeof(*ip1)));
699 ip1 = vlib_buffer_get_current (b0);
701 copy_ip6_address(&client_address, &h0->peer_addr);
703 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
706 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
710 len = clib_net_to_host_u16(r0->length);
711 memset(ip1, 0, sizeof(*ip1));
712 copy_ip6_address(&ip1->dst_address, &client_address);
714 u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);
715 u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
716 u1->length = clib_host_to_net_u16 (len + sizeof(udp_header_t));
718 ip1->ip_version_traffic_class_and_flow_label =
719 ip0->ip_version_traffic_class_and_flow_label &
721 ip1->payload_length = u1->length;
722 ip1->protocol = PROTO_UDP;
723 ip1->hop_limit = HOP_COUNT_LIMIT;
724 copy_ip6_address(&ip1->src_address, ia0);
726 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
728 ASSERT(bogus_length == 0);
730 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
731 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
732 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
733 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
735 mac0 = vlib_buffer_get_current (b0);
737 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
738 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
739 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
740 memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
741 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
742 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x86dd);
744 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
746 u32 * vlan_tag = (u32 *)(mac0+1);
748 tmp = (si0->sub.id << 16) | 0x0800;
749 *vlan_tag = clib_host_to_net_u32 (tmp);
752 /* $$$ consider adding a dynamic next to the graph node, for performance */
753 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
754 to_next0 = vlib_frame_vector_args (f0);
757 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
760 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
762 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
764 tr->which = 1; /* to client */
766 copy_ip6_address((ip6_address_t*)tr->packet_data, ia0);
768 tr->original_sw_if_index = original_sw_if_index;
769 tr->sw_if_index = sw_if_index;
772 return from_frame->n_vectors;
776 VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node, static) = {
777 .function = dhcpv6_proxy_to_client_input,
778 .name = "dhcpv6-proxy-to-client",
779 /* Takes a vector of packets. */
780 .vector_size = sizeof (u32),
782 .n_errors = DHCPV6_PROXY_N_ERROR,
783 .error_strings = dhcpv6_proxy_error_strings,
784 .format_buffer = format_dhcpv6_proxy_header_with_length,
785 .format_trace = format_dhcpv6_proxy_trace,
787 .unformat_buffer = unformat_dhcpv6_proxy_header,
791 static clib_error_t *
792 dhcp6_proxy_init (vlib_main_t * vm)
794 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
795 vlib_node_t * error_drop_node;
797 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
798 dm->error_drop_node_index = error_drop_node->index;
800 /* RFC says this is the dhcpv6 server address */
801 all_dhcpv6_server_address.as_u64[0] = clib_host_to_net_u64 (0xFF05000000000000);
802 all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
804 /* RFC says this is the server and agent address */
805 all_dhcpv6_server_relay_agent_address.as_u64[0] = clib_host_to_net_u64 (0xFF02000000000000);
806 all_dhcpv6_server_relay_agent_address.as_u64[1] = clib_host_to_net_u64 (0x00010002);
808 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
809 dhcpv6_proxy_to_client_node.index, 0 /* is_ip6 */);
811 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
812 dhcpv6_proxy_to_server_node.index, 0 /* is_ip6 */);
817 VLIB_INIT_FUNCTION (dhcp6_proxy_init);
820 dhcp6_proxy_set_server (ip46_address_t *addr,
821 ip46_address_t *src_addr,
826 u32 rx_fib_index = 0;
829 const mfib_prefix_t all_dhcp_servers = {
831 .fp_proto = FIB_PROTOCOL_IP6,
833 .ip6 = all_dhcpv6_server_relay_agent_address,
837 if (ip46_address_is_zero(addr))
838 return VNET_API_ERROR_INVALID_DST_ADDRESS;
840 if (ip46_address_is_zero(src_addr))
841 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
843 rx_fib_index = mfib_table_find_or_create_and_lock(FIB_PROTOCOL_IP6,
849 if (dhcp_proxy_server_del (FIB_PROTOCOL_IP6, rx_fib_index,
850 addr, server_table_id))
852 mfib_table_entry_delete(rx_fib_index,
855 mfib_table_unlock(rx_fib_index, FIB_PROTOCOL_IP6, MFIB_SOURCE_DHCP);
860 const fib_route_path_t path_for_us = {
861 .frp_proto = DPO_PROTO_IP6,
862 .frp_addr = zero_addr,
863 .frp_sw_if_index = 0xffffffff,
866 .frp_flags = FIB_ROUTE_PATH_LOCAL,
868 if (dhcp_proxy_server_add (FIB_PROTOCOL_IP6, addr, src_addr,
869 rx_fib_index, server_table_id))
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
882 * input feature ensures this.
884 mfib_table_entry_update(rx_fib_index,
888 MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF);
889 mfib_table_lock(rx_fib_index, FIB_PROTOCOL_IP6, MFIB_SOURCE_DHCP);
893 mfib_table_unlock(rx_fib_index, FIB_PROTOCOL_IP6, MFIB_SOURCE_DHCP);
898 static clib_error_t *
899 dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
900 unformat_input_t * input,
901 vlib_cli_command_t * cmd)
903 ip46_address_t addr, src_addr;
904 int set_server = 0, set_src_address = 0;
905 u32 rx_table_id = 0, server_table_id = 0;
908 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
910 if (unformat (input, "server %U",
911 unformat_ip6_address, &addr.ip6))
913 else if (unformat(input, "src-address %U",
914 unformat_ip6_address, &src_addr.ip6))
916 else if (unformat (input, "server-fib-id %d", &server_table_id))
918 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
920 else if (unformat (input, "delete") ||
921 unformat (input, "del"))
927 if (is_del || (set_server && set_src_address))
931 rv = dhcp6_proxy_set_server (&addr, &src_addr, rx_table_id,
932 server_table_id, is_del);
934 //TODO: Complete the errors
940 case VNET_API_ERROR_INVALID_DST_ADDRESS:
941 return clib_error_return (0, "Invalid server address");
943 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
944 return clib_error_return (0, "Invalid src address");
946 case VNET_API_ERROR_NO_SUCH_ENTRY:
947 return clib_error_return
948 (0, "Fib id %d: no per-fib DHCP server configured", rx_table_id);
951 return clib_error_return (0, "BUG: rv %d", rv);
955 return clib_error_return (0, "parse error`%U'",
956 format_unformat_error, input);
959 VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
960 .path = "set dhcpv6 proxy",
961 .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> "
962 "[server-fib-id <fib-id>] [rx-fib-id <fib-id>] ",
963 .function = dhcpv6_proxy_set_command_fn,
967 format_dhcp6_proxy_server (u8 * s, va_list * args)
969 dhcp_proxy_t * proxy = va_arg (*args, dhcp_proxy_t *);
970 fib_table_t *server_fib;
971 dhcp_server_t *server;
976 s = format (s, "%=14s%=16s%s", "RX FIB", "Src Address",
977 "Servers FIB,Address");
981 rx_fib = ip6_mfib_get(proxy->rx_fib_index);
983 s = format (s, "%=14u%=16U",
985 format_ip46_address, &proxy->dhcp_src_address, IP46_TYPE_ANY);
987 vec_foreach(server, proxy->dhcp_servers)
989 server_fib = fib_table_get(server->server_fib_index,
991 s = format (s, "%u,%U ",
992 server_fib->ft_table_id,
993 format_ip46_address, &server->dhcp_server, IP46_TYPE_ANY);
1000 dhcp6_proxy_show_walk (dhcp_proxy_t *proxy,
1003 vlib_main_t * vm = ctx;
1005 vlib_cli_output (vm, "%U", format_dhcp6_proxy_server, proxy);
1010 static clib_error_t *
1011 dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
1012 unformat_input_t * input,
1013 vlib_cli_command_t * cmd)
1015 vlib_cli_output (vm, "%U", format_dhcp6_proxy_server, NULL /* header line */);
1017 dhcp_proxy_walk(FIB_PROTOCOL_IP6, dhcp6_proxy_show_walk, vm);
1022 VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
1023 .path = "show dhcpv6 proxy",
1024 .short_help = "Display dhcpv6 proxy info",
1025 .function = dhcpv6_proxy_show_command_fn,
1028 static clib_error_t *
1029 dhcpv6_vss_command_fn (vlib_main_t * vm,
1030 unformat_input_t * input,
1031 vlib_cli_command_t * cmd)
1033 int is_del = 0, got_new_vss=0;
1035 u32 fib_id=0, tbl_id=~0;
1037 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1039 if (unformat (input, "oui %d", &oui))
1041 else if (unformat (input, "vpn-id %d", &fib_id))
1043 else if (unformat (input, "table %d", &tbl_id))
1045 else if (unformat(input, "delete") || unformat(input, "del"))
1052 return clib_error_return (0, "no table ID specified.");
1054 if (is_del || got_new_vss)
1058 rv = dhcp_proxy_set_vss(FIB_PROTOCOL_IP6, tbl_id, oui, fib_id, is_del);
1064 case VNET_API_ERROR_NO_SUCH_FIB:
1065 return clib_error_return (0, "vss info (oui:%d, vpn-id:%d) not found in table %d.",
1066 oui, fib_id, tbl_id);
1068 case VNET_API_ERROR_NO_SUCH_ENTRY:
1069 return clib_error_return (0, "vss for table %d not found in pool.",
1073 return clib_error_return (0, "BUG: rv %d", rv);
1077 return clib_error_return (0, "parse error`%U'",
1078 format_unformat_error, input);
1082 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
1083 .path = "set dhcpv6 vss",
1084 .short_help = "set dhcpv6 vss table <table-id> oui <oui> vpn-idx <vpn-idx>",
1085 .function = dhcpv6_vss_command_fn,
1088 static clib_error_t *
1089 dhcpv6_vss_show_command_fn (vlib_main_t * vm,
1090 unformat_input_t * input,
1091 vlib_cli_command_t * cmd)
1094 dhcp_vss_walk(FIB_PROTOCOL_IP6, dhcp_vss_show_walk, vm);
1099 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
1100 .path = "show dhcpv6 vss",
1101 .short_help = "show dhcpv6 VSS",
1102 .function = dhcpv6_vss_show_command_fn,
1105 static clib_error_t *
1106 dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1107 unformat_input_t * input,
1108 vlib_cli_command_t * cmd)
1111 vnet_main_t *vnm = vnet_get_main();
1112 u32 sw_if_index0=0, sw_if_index;
1113 vnet_sw_interface_t *swif;
1116 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1119 if (unformat(input, "%U",
1120 unformat_vnet_sw_interface, vnm, &sw_if_index0))
1122 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1123 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1124 swif->unnumbered_sw_if_index : sw_if_index0;
1125 ia0 = ip6_interface_first_address(&ip6_main, sw_if_index);
1128 vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
1130 vlib_cli_output (vm, "%=20U%=48U",
1131 format_vnet_sw_if_index_name, vnm, sw_if_index0,
1132 format_ip6_address, ia0);
1134 vlib_cli_output (vm, "%=34s%=20U", "No IPv6 address configured on",
1135 format_vnet_sw_if_index_name, vnm, sw_if_index);
1143 VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1144 .path = "show dhcpv6 link-address interface",
1145 .short_help = "show dhcpv6 link-address interface <interface>",
1146 .function = dhcpv6_link_address_show_command_fn,