2 * proxy_node.c: dhcp 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/proxy.h>
22 static char * dhcp_proxy_error_strings[] = {
23 #define dhcp_proxy_error(n,s) s,
24 #include "proxy_error.def"
25 #undef dhcp_proxy_error
28 #define foreach_dhcp_proxy_to_server_input_next \
29 _ (DROP, "error-drop") \
30 _ (LOOKUP, "ip4-lookup") \
31 _ (SEND_TO_CLIENT, "dhcp-proxy-to-client")
34 #define _(s,n) DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s,
35 foreach_dhcp_proxy_to_server_input_next
37 DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
38 } dhcp_proxy_to_server_input_next_t;
41 /* 0 => to server, 1 => to client */
43 ip4_address_t trace_ip4_address;
46 u32 original_sw_if_index;
49 #define VPP_DHCP_OPTION82_SUB1_SIZE 6
50 #define VPP_DHCP_OPTION82_SUB5_SIZE 6
51 #define VPP_DHCP_OPTION82_VSS_SIZE 12
52 #define VPP_DHCP_OPTION82_SIZE (VPP_DHCP_OPTION82_SUB1_SIZE + \
53 VPP_DHCP_OPTION82_SUB5_SIZE + \
54 VPP_DHCP_OPTION82_VSS_SIZE +3)
56 vlib_node_registration_t dhcp_proxy_to_server_node;
57 vlib_node_registration_t dhcp_proxy_to_client_node;
59 u8 * format_dhcp_proxy_trace (u8 * s, va_list * args)
61 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
63 dhcp_proxy_trace_t * t = va_arg (*args, dhcp_proxy_trace_t *);
66 s = format (s, "DHCP proxy: sent to server %U\n",
67 format_ip4_address, &t->trace_ip4_address, t->error);
69 s = format (s, "DHCP proxy: broadcast to client from %U\n",
70 format_ip4_address, &t->trace_ip4_address);
72 if (t->error != (u32)~0)
73 s = format (s, " error: %s\n", dhcp_proxy_error_strings[t->error]);
75 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
76 t->original_sw_if_index, t->sw_if_index);
81 u8 * format_dhcp_proxy_header_with_length (u8 * s, va_list * args)
83 dhcp_header_t * h = va_arg (*args, dhcp_header_t *);
84 u32 max_header_bytes = va_arg (*args, u32);
87 header_bytes = sizeof (h[0]);
88 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
89 return format (s, "dhcp header truncated");
91 s = format (s, "DHCP Proxy");
97 dhcp_proxy_to_server_input (vlib_main_t * vm,
98 vlib_node_runtime_t * node,
99 vlib_frame_t * from_frame)
101 u32 n_left_from, next_index, * from, * to_next;
102 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
103 from = vlib_frame_vector_args (from_frame);
104 n_left_from = from_frame->n_vectors;
105 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
106 u32 pkts_no_interface_address=0;
108 ip4_main_t * im = &ip4_main;
110 next_index = node->cached_next_index;
112 while (n_left_from > 0)
116 vlib_get_next_frame (vm, node, next_index,
117 to_next, n_left_to_next);
119 while (n_left_from > 0 && n_left_to_next > 0)
129 u32 error0 = (u32) ~0;
131 u32 original_sw_if_index = 0;
133 u32 fib_index, server_index;
134 dhcp_server_t * server;
144 b0 = vlib_get_buffer (vm, bi0);
146 h0 = vlib_buffer_get_current (b0);
149 * udp_local hands us the DHCP header, need udp hdr,
150 * ip hdr to relay to server
152 vlib_buffer_advance (b0, -(sizeof(*u0)));
153 u0 = vlib_buffer_get_current (b0);
155 /* This blows. Return traffic has src_port = 67, dst_port = 67 */
156 if (u0->src_port == clib_net_to_host_u16(UDP_DST_PORT_dhcp_to_server))
158 vlib_buffer_advance (b0, sizeof(*u0));
159 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
165 rx_sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
167 fib_index = im->fib_index_by_sw_if_index [rx_sw_if_index];
169 if (fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
170 server_index = dpm->dhcp_server_index_by_rx_fib_index[fib_index];
174 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp_servers,
178 error0 = DHCP_PROXY_ERROR_NO_SERVER;
179 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
184 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
185 if (server->valid == 0)
188 vlib_buffer_advance (b0, -(sizeof(*ip0)));
189 ip0 = vlib_buffer_get_current (b0);
191 /* disable UDP checksum */
193 sum0 = ip0->checksum;
194 old0 = ip0->dst_address.as_u32;
195 new0 = server->dhcp_server.as_u32;
196 ip0->dst_address.as_u32 = server->dhcp_server.as_u32;
197 sum0 = ip_csum_update (sum0, old0, new0,
198 ip4_header_t /* structure */,
199 dst_address /* changed member */);
200 ip0->checksum = ip_csum_fold (sum0);
202 sum0 = ip0->checksum;
203 old0 = ip0->src_address.as_u32;
204 new0 = server->dhcp_src_address.as_u32;
205 ip0->src_address.as_u32 = new0;
206 sum0 = ip_csum_update (sum0, old0, new0,
207 ip4_header_t /* structure */,
208 src_address /* changed member */);
209 ip0->checksum = ip_csum_fold (sum0);
211 /* Send to DHCP server via the configured FIB */
212 vnet_buffer(b0)->sw_if_index[VLIB_TX] =
213 server->server_fib_index;
215 h0->gateway_ip_address.as_u32 = server->dhcp_src_address.as_u32;
218 if (server->insert_option_82)
220 u32 fib_index, fib_id, opt82_fib_id=0, opt82_oui=0;
222 dhcp_option_t *o = (dhcp_option_t *) h0->options;
224 vlib_buffer_free_list_t *fl;
226 fib_index = im->fib_index_by_sw_if_index
227 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
228 fib = vec_elt_at_index (im->fibs, fib_index);
229 fib_id = fib->table_id;
231 end = b0->data + b0->current_data + b0->current_length;
232 /* TLVs are not performance-friendly... */
233 while (o->option != 0xFF /* end of options */ && (u8 *)o < end)
234 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
236 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
237 // start write at (option*)o, some packets have padding
238 if (((u8 *)o - (u8 *)b0->data + VPP_DHCP_OPTION82_SIZE) > fl->n_data_bytes)
240 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
245 if ((o->option == 0xFF) && ((u8 *)o <= end))
247 vnet_main_t *vnm = vnet_get_main();
249 ip4_address_t _ia0, * ia0 = &_ia0;
252 vnet_sw_interface_t *swif;
254 original_sw_if_index = 0;
256 original_sw_if_index = sw_if_index =
257 vnet_buffer(b0)->sw_if_index[VLIB_RX];
258 swif = vnet_get_sw_interface (vnm, sw_if_index);
259 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
260 sw_if_index = swif->unnumbered_sw_if_index;
262 p_vss = hash_get (dpm->opt82vss_index_by_vrf_id,
266 vss = pool_elt_at_index (dpm->opt82vss, p_vss[0]);
267 opt82_oui = vss->vpn_id.oui;
268 opt82_fib_id = vss->vpn_id.fib_id;
271 * Get the first ip4 address on the [client-side]
272 * RX interface, if not unnumbered. otherwise use
273 * the loopback interface's ip address.
275 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0);
279 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
280 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
281 pkts_no_interface_address++;
286 o->option = 82; /* option 82 */
287 o->length = 12; /* 12 octets to follow */
288 o->data[0] = 1; /* suboption 1, circuit ID (=FIB id) */
289 o->data[1] = 4; /* length of suboption */
290 o->data[2] = (original_sw_if_index >> 24) & 0xFF;
291 o->data[3] = (original_sw_if_index >> 16) & 0xFF;
292 o->data[4] = (original_sw_if_index >> 8) & 0xFF;
293 o->data[5] = (original_sw_if_index >> 0) & 0xFF;
294 o->data[6] = 5; /* suboption 5 (client RX intfc address) */
295 o->data[7] = 4; /* length 4 */
296 o->data[8] = ia0->as_u8[0];
297 o->data[9] = ia0->as_u8[1];
298 o->data[10] = ia0->as_u8[2];
299 o->data[11] = ia0->as_u8[3];
301 if (opt82_oui !=0 || opt82_fib_id != 0)
303 o->data[12] = 151; /* vss suboption */
304 if (255 == opt82_fib_id) {
305 o->data[13] = 1; /* length */
306 o->data[14] = 255; /* vss option type */
307 o->data[15] = 152; /* vss control suboption */
308 o->data[16] = 0; /* length */
309 /* and a new "end-of-options" option (0xff) */
313 o->data[13] = 8; /* length */
314 o->data[14] = 1; /* vss option type */
315 o->data[15] = (opt82_oui >> 16) & 0xff;
316 o->data[16] = (opt82_oui >> 8) & 0xff;
317 o->data[17] = (opt82_oui ) & 0xff;
318 o->data[18] = (opt82_fib_id >> 24) & 0xff;
319 o->data[19] = (opt82_fib_id >> 16) & 0xff;
320 o->data[20] = (opt82_fib_id >> 8) & 0xff;
321 o->data[21] = (opt82_fib_id) & 0xff;
322 o->data[22] = 152; /* vss control suboption */
323 o->data[23] = 0; /* length */
325 /* and a new "end-of-options" option (0xff) */
332 b0->current_length += len;
333 /* Fix IP header length and checksum */
334 old_l0 = ip0->length;
335 new_l0 = clib_net_to_host_u16 (old_l0);
337 new_l0 = clib_host_to_net_u16 (new_l0);
338 ip0->length = new_l0;
339 sum0 = ip0->checksum;
340 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
341 length /* changed member */);
342 ip0->checksum = ip_csum_fold (sum0);
345 new_l0 = clib_net_to_host_u16 (u0->length);
347 u0->length = clib_host_to_net_u16 (new_l0);
349 vlib_node_increment_counter
350 (vm, dhcp_proxy_to_server_node.index,
351 DHCP_PROXY_ERROR_OPTION_82_ERROR, 1);
355 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
358 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
360 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
362 tr->which = 0; /* to server */
364 tr->original_sw_if_index = original_sw_if_index;
365 tr->sw_if_index = sw_if_index;
366 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
367 tr->trace_ip4_address.as_u32 = server->dhcp_server.as_u32;
371 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
372 to_next, n_left_to_next,
376 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
379 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
380 DHCP_PROXY_ERROR_RELAY_TO_CLIENT,
382 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
383 DHCP_PROXY_ERROR_RELAY_TO_SERVER,
385 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
386 DHCP_PROXY_ERROR_NO_SERVER,
388 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
389 DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS,
390 pkts_no_interface_address);
391 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
392 DHCP_PROXY_ERROR_PKT_TOO_BIG,
394 return from_frame->n_vectors;
397 VLIB_REGISTER_NODE (dhcp_proxy_to_server_node) = {
398 .function = dhcp_proxy_to_server_input,
399 .name = "dhcp-proxy-to-server",
400 /* Takes a vector of packets. */
401 .vector_size = sizeof (u32),
403 .n_errors = DHCP_PROXY_N_ERROR,
404 .error_strings = dhcp_proxy_error_strings,
406 .n_next_nodes = DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
408 #define _(s,n) [DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
409 foreach_dhcp_proxy_to_server_input_next
413 .format_buffer = format_dhcp_proxy_header_with_length,
414 .format_trace = format_dhcp_proxy_trace,
416 .unformat_buffer = unformat_dhcp_proxy_header,
421 dhcp_proxy_to_client_input (vlib_main_t * vm,
422 vlib_node_runtime_t * node,
423 vlib_frame_t * from_frame)
425 u32 n_left_from, * from;
426 ethernet_main_t *em = ethernet_get_main (vm);
427 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
428 vnet_main_t * vnm = vnet_get_main();
429 ip4_main_t * im = &ip4_main;
431 from = vlib_frame_vector_args (from_frame);
432 n_left_from = from_frame->n_vectors;
434 while (n_left_from > 0)
440 ip4_header_t * ip0 = 0;
441 ip4_address_t * ia0 = 0;
444 ethernet_interface_t *ei0;
445 ethernet_header_t *mac0;
446 vnet_hw_interface_t *hi0;
449 u32 sw_if_index = ~0;
450 vnet_sw_interface_t *si0;
451 u32 error0 = (u32)~0;
452 vnet_sw_interface_t *swif;
455 dhcp_server_t * server;
456 u32 original_sw_if_index = (u32) ~0;
462 b0 = vlib_get_buffer (vm, bi0);
463 h0 = vlib_buffer_get_current (b0);
466 * udp_local hands us the DHCP header, need udp hdr,
467 * ip hdr to relay to client
469 vlib_buffer_advance (b0, -(sizeof(*u0)));
470 u0 = vlib_buffer_get_current (b0);
472 vlib_buffer_advance (b0, -(sizeof(*ip0)));
473 ip0 = vlib_buffer_get_current (b0);
475 /* Consumed by dhcp client code? */
476 if (dhcp_client_for_us (bi0, b0, ip0, u0, h0))
479 if (1 /* dpm->insert_option_82 */)
481 dhcp_option_t *o = (dhcp_option_t *) h0->options;
484 /* Parse through TLVs looking for option 82.
485 The circuit-ID is the FIB number we need
486 to track down the client-facing interface */
488 while (o->option != 0xFF /* end of options */ &&
489 (u8 *) o < (b0->data + b0->current_data + b0->current_length))
495 sub = (dhcp_option_t *) &o->data[0];
496 while (sub->option != 0xFF /* end of options */ &&
497 (u8 *) sub < (u8 *)(o + o->length)) {
498 /* If this is one of ours, it will have
499 total length 12, circuit-id suboption type,
500 and the sw_if_index */
501 if (sub->option == 1 && sub->length == 4)
503 sw_if_index = (o->data[2] << 24)
507 } else if (sub->option == 151 &&
511 else if (sub->option == 152 && sub->length == 0)
513 sub = (dhcp_option_t *)
514 (((uword) sub) + (sub->length + 2));
516 if (vss_ctrl && vss_exist)
517 vlib_node_increment_counter
518 (vm, dhcp_proxy_to_client_node.index,
519 DHCP_PROXY_ERROR_OPTION_82_VSS_NOT_PROCESSED, 1);
522 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
526 if (sw_if_index == (u32)~0)
528 error0 = DHCP_PROXY_ERROR_NO_OPTION_82;
531 vlib_node_increment_counter (vm, dhcp_proxy_to_client_node.index,
533 f0 = vlib_get_frame_to_node (vm, dpm->error_drop_node_index);
534 to_next0 = vlib_frame_vector_args (f0);
537 vlib_put_frame_to_node (vm, dpm->error_drop_node_index, f0);
542 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
544 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82;
548 fib_index = im->fib_index_by_sw_if_index [sw_if_index];
550 if (fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
551 server_index = dpm->dhcp_server_index_by_rx_fib_index[fib_index];
555 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp_servers,
558 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82;
562 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
563 if (server->valid == 0)
565 error0 = DHCP_PROXY_ERROR_NO_SERVER;
569 if (ip0->src_address.as_u32 != server->dhcp_server.as_u32)
571 error0 = DHCP_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
575 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
577 swif = vnet_get_sw_interface (vnm, sw_if_index);
578 original_sw_if_index = sw_if_index;
579 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
580 sw_if_index = swif->unnumbered_sw_if_index;
582 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
585 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
590 u0->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client);
591 sum0 = ip0->checksum;
592 old0 = ip0->dst_address.as_u32;
594 ip0->dst_address.as_u32 = new0;
595 sum0 = ip_csum_update (sum0, old0, new0,
596 ip4_header_t /* structure */,
597 dst_address /* offset of changed member */);
598 ip0->checksum = ip_csum_fold (sum0);
600 sum0 = ip0->checksum;
601 old0 = ip0->src_address.as_u32;
603 ip0->src_address.as_u32 = new0;
604 sum0 = ip_csum_update (sum0, old0, new0,
605 ip4_header_t /* structure */,
606 src_address /* offset of changed member */);
607 ip0->checksum = ip_csum_fold (sum0);
609 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
610 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
611 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
612 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
614 mac0 = vlib_buffer_get_current (b0);
616 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
617 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
618 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
619 memset (mac0->dst_address, 0xff, sizeof (mac0->dst_address));
620 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
621 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x0800);
623 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
625 u32 * vlan_tag = (u32 *)(mac0+1);
627 tmp = (si0->sub.id << 16) | 0x0800;
628 *vlan_tag = clib_host_to_net_u32 (tmp);
631 /* $$$ This needs to be rewritten, for sure */
632 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
633 to_next0 = vlib_frame_vector_args (f0);
636 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
639 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
641 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
643 tr->which = 1; /* to client */
644 tr->trace_ip4_address.as_u32 = ia0 ? ia0->as_u32 : 0;
646 tr->original_sw_if_index = original_sw_if_index;
647 tr->sw_if_index = sw_if_index;
650 return from_frame->n_vectors;
653 VLIB_REGISTER_NODE (dhcp_proxy_to_client_node) = {
654 .function = dhcp_proxy_to_client_input,
655 .name = "dhcp-proxy-to-client",
656 /* Takes a vector of packets. */
657 .vector_size = sizeof (u32),
659 .n_errors = DHCP_PROXY_N_ERROR,
660 .error_strings = dhcp_proxy_error_strings,
661 .format_buffer = format_dhcp_proxy_header_with_length,
662 .format_trace = format_dhcp_proxy_trace,
664 .unformat_buffer = unformat_dhcp_proxy_header,
668 clib_error_t * dhcp_proxy_init (vlib_main_t * vm)
670 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
671 vlib_node_t * error_drop_node;
672 dhcp_server_t * server;
675 dm->vnet_main = vnet_get_main();
676 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
677 dm->error_drop_node_index = error_drop_node->index;
679 dm->opt82vss_index_by_vrf_id = hash_create (0, sizeof (uword));
681 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
682 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */);
684 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
685 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */);
687 /* Create the default server, don't mark it valid */
688 pool_get (dm->dhcp_servers, server);
689 memset (server, 0, sizeof (*server));
694 VLIB_INIT_FUNCTION (dhcp_proxy_init);
696 int dhcp_proxy_set_server_2 (ip4_address_t *addr, ip4_address_t *src_address,
699 int insert_option_82, int is_del)
701 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
702 ip4_main_t * im = &ip4_main;
703 dhcp_server_t * server = 0;
704 ip4_fib_t *rx_fib, *server_fib;
705 u32 server_index = 0;
706 u32 rx_fib_index = 0;
708 if (addr->as_u32 == 0)
709 return VNET_API_ERROR_INVALID_DST_ADDRESS;
711 if (src_address->as_u32 == 0)
712 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
714 rx_fib = find_ip4_fib_by_table_index_or_id
715 (&ip4_main, rx_fib_id, IP4_ROUTE_FLAG_TABLE_ID);
718 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
720 server_fib = find_ip4_fib_by_table_index_or_id
721 (&ip4_main, server_fib_id, IP4_ROUTE_FLAG_TABLE_ID);
724 return VNET_API_ERROR_NO_SUCH_FIB;
728 server = pool_elt_at_index (dpm->dhcp_servers, 0);
732 memset (server, 0, sizeof (*server));
738 rx_fib_index = rx_fib - im->fibs;
742 if (rx_fib_index >= vec_len(dpm->dhcp_server_index_by_rx_fib_index))
743 return VNET_API_ERROR_NO_SUCH_ENTRY;
745 server_index = dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index];
746 ASSERT(server_index > 0);
748 /* Use the default server again. */
749 dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index] = 0;
750 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
751 memset (server, 0, sizeof (*server));
752 pool_put (dpm->dhcp_servers, server);
756 if (rx_fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
758 server_index = dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index];
759 if (server_index != 0)
761 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
766 pool_get (dpm->dhcp_servers, server);
770 server->dhcp_server.as_u32 = addr->as_u32;
771 server->server_fib_index = server_fib - im->fibs;
772 server->dhcp_src_address.as_u32 = src_address->as_u32;
773 server->insert_option_82 = insert_option_82;
777 vec_validate (dpm->dhcp_server_index_by_rx_fib_index, rx_fib_index);
778 dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index] =
779 server - dpm->dhcp_servers;
785 /* Old API, manipulates the default server (only) */
786 int dhcp_proxy_set_server (ip4_address_t *addr, ip4_address_t *src_address,
787 u32 fib_id, int insert_option_82, int is_del)
789 return dhcp_proxy_set_server_2 (addr, src_address, 0 /* rx_fib_id */,
790 fib_id /* server_fib_id */,
791 insert_option_82, is_del);
795 static clib_error_t *
796 dhcp_proxy_set_command_fn (vlib_main_t * vm,
797 unformat_input_t * input,
798 vlib_cli_command_t * cmd)
800 ip4_address_t server_addr, src_addr;
801 u32 server_fib_id = 0, rx_fib_id = 0;
803 int add_option_82 = 0;
804 int set_src = 0, set_server = 0;
806 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
808 if (unformat (input, "server %U",
809 unformat_ip4_address, &server_addr))
811 else if (unformat (input, "server-fib-id %d", &server_fib_id))
813 else if (unformat (input, "rx-fib-id %d", &rx_fib_id))
815 else if (unformat(input, "src-address %U",
816 unformat_ip4_address, &src_addr))
818 else if (unformat (input, "add-option-82")
819 || unformat (input, "insert-option-82"))
821 else if (unformat (input, "delete") ||
822 unformat (input, "del"))
828 if (is_del || (set_server && set_src))
832 rv = dhcp_proxy_set_server_2 (&server_addr, &src_addr, rx_fib_id,
833 server_fib_id, add_option_82, is_del);
839 case VNET_API_ERROR_INVALID_DST_ADDRESS:
840 return clib_error_return (0, "Invalid server address");
842 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
843 return clib_error_return (0, "Invalid src address");
845 case VNET_API_ERROR_NO_SUCH_INNER_FIB:
846 return clib_error_return (0, "No such rx fib id %d", rx_fib_id);
848 case VNET_API_ERROR_NO_SUCH_FIB:
849 return clib_error_return (0, "No such server fib id %d",
852 case VNET_API_ERROR_NO_SUCH_ENTRY:
853 return clib_error_return
854 (0, "Fib id %d: no per-fib DHCP server configured", rx_fib_id);
857 return clib_error_return (0, "BUG: rv %d", rv);
861 return clib_error_return (0, "parse error`%U'",
862 format_unformat_error, input);
865 VLIB_CLI_COMMAND (dhcp_proxy_set_command, static) = {
866 .path = "set dhcp proxy",
867 .short_help = "set dhcp proxy [del] server <ip-addr> src-address <ip-addr> [add-option-82] [server-fib-id <n>] [rx-fib-id <n>]",
868 .function = dhcp_proxy_set_command_fn,
871 u8 * format_dhcp_proxy_server (u8 * s, va_list * args)
873 dhcp_proxy_main_t * dm = va_arg (*args, dhcp_proxy_main_t *);
874 dhcp_server_t * server = va_arg (*args, dhcp_server_t *);
875 u32 rx_fib_index = va_arg (*args, u32);
876 ip4_fib_t * rx_fib, * server_fib;
877 u32 server_fib_id = ~0, rx_fib_id = ~0;
881 s = format (s, "%=16s%=16s%=14s%=14s%=20s", "Server", "Src Address",
882 "Server FIB", "RX FIB", "Insert Option 82");
886 server_fib = find_ip4_fib_by_table_index_or_id
887 (&ip4_main, server->server_fib_index, IP4_ROUTE_FLAG_FIB_INDEX);
890 server_fib_id = server_fib->table_id;
892 rx_fib = find_ip4_fib_by_table_index_or_id
893 (&ip4_main, rx_fib_index, IP4_ROUTE_FLAG_FIB_INDEX);
896 rx_fib_id = rx_fib->table_id;
898 s = format (s, "%=16U%=16U%=14u%=14u%=20s",
899 format_ip4_address, &server->dhcp_server,
900 format_ip4_address, &server->dhcp_src_address,
901 server_fib_id, rx_fib_id,
902 server->insert_option_82 ? "yes" : "no");
906 static clib_error_t *
907 dhcp_proxy_show_command_fn (vlib_main_t * vm,
908 unformat_input_t * input,
909 vlib_cli_command_t * cmd)
911 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
912 ip4_main_t * im = &ip4_main;
913 dhcp_server_t * server;
917 vlib_cli_output (vm, "%U", format_dhcp_proxy_server, 0 /* header line */,
920 for (i = 0; i < vec_len (im->fibs); i++)
922 if (i < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
923 server_index = dpm->dhcp_server_index_by_rx_fib_index[i];
926 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
928 vlib_cli_output (vm, "%U", format_dhcp_proxy_server, dpm,
935 VLIB_CLI_COMMAND (dhcp_proxy_show_command, static) = {
936 .path = "show dhcp proxy",
937 .short_help = "Display dhcp proxy server info",
938 .function = dhcp_proxy_show_command_fn,
942 int dhcp_proxy_set_option82_vss( u32 vrf_id,
947 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
950 u32 old_oui=0, old_fib_id=0;
952 p = hash_get (dm->opt82vss_index_by_vrf_id, vrf_id);
956 a = pool_elt_at_index (dm->opt82vss, p[0]);
958 return VNET_API_ERROR_NO_SUCH_FIB;
959 old_oui = a->vpn_id.oui;
960 old_fib_id = a->vpn_id.fib_id;
964 if (old_oui == oui &&
965 old_fib_id == fib_id)
967 pool_put(dm->opt82vss, a);
968 hash_unset (dm->opt82vss_index_by_vrf_id, vrf_id);
972 return VNET_API_ERROR_NO_SUCH_ENTRY;
974 pool_put(dm->opt82vss, a);
975 hash_unset (dm->opt82vss_index_by_vrf_id, vrf_id);
977 return VNET_API_ERROR_NO_SUCH_ENTRY;
978 pool_get (dm->opt82vss, a);
979 memset (a, ~0, sizeof (a[0]));
981 a->vpn_id.fib_id = fib_id;
982 hash_set (dm->opt82vss_index_by_vrf_id, vrf_id, a - dm->opt82vss);
987 static clib_error_t *
988 dhcp_option_82_vss_fn (vlib_main_t * vm,
989 unformat_input_t * input,
990 vlib_cli_command_t * cmd)
992 int is_del = 0, got_new_vpn_id=0;
993 u32 oui=0, fib_id=0, tbl_id=~0;
996 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
999 if (unformat(input, "delete") || unformat(input, "del"))
1001 else if (unformat (input, "oui %d", &oui))
1003 else if (unformat (input, "vpn-id %d", &fib_id))
1005 else if (unformat (input, "table %d", &tbl_id))
1011 return clib_error_return (0, "no table ID specified.");
1013 if (is_del || got_new_vpn_id)
1016 rv = dhcp_proxy_set_option82_vss(tbl_id, oui, fib_id, is_del);
1022 case VNET_API_ERROR_NO_SUCH_FIB:
1023 return clib_error_return (0, "option 82 vss(oui:%d, vpn-id:%d) not found in table %d",
1024 oui, fib_id, tbl_id);
1026 case VNET_API_ERROR_NO_SUCH_ENTRY:
1027 return clib_error_return (0, "option 82 vss for table %d not found in in pool.",
1030 return clib_error_return (0, "BUG: rv %d", rv);
1034 return clib_error_return (0, "parse error`%U'",
1035 format_unformat_error, input);
1038 VLIB_CLI_COMMAND (dhcp_proxy_vss_command,static) = {
1039 .path = "set dhcp option-82 vss",
1040 .short_help = "set dhcp option-82 vss [del] table <table id> oui <oui> vpn-id <vpn-id>",
1041 .function = dhcp_option_82_vss_fn,
1045 static clib_error_t *
1046 dhcp_vss_show_command_fn (vlib_main_t * vm,
1047 unformat_input_t * input,
1048 vlib_cli_command_t * cmd)
1051 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
1058 vlib_cli_output (vm, "%=9s%=11s%=12s","Table", "OUI", "VPN-ID");
1059 hash_foreach (tbl_id, index, dm->opt82vss_index_by_vrf_id,
1061 v = pool_elt_at_index (dm->opt82vss, index);
1062 oui = v->vpn_id.oui;
1063 fib_id = v->vpn_id.fib_id;
1064 vlib_cli_output (vm, "%=9d 0x%08x%=12d",
1065 tbl_id, oui, fib_id);
1071 VLIB_CLI_COMMAND (dhcp_proxy_vss_show_command, static) = {
1072 .path = "show dhcp vss",
1073 .short_help = "show dhcp VSS",
1074 .function = dhcp_vss_show_command_fn,
1077 static clib_error_t *
1078 dhcp_option_82_address_show_command_fn (vlib_main_t * vm,
1079 unformat_input_t * input,
1080 vlib_cli_command_t * cmd)
1083 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
1084 vnet_main_t *vnm = vnet_get_main();
1085 u32 sw_if_index0=0, sw_if_index;
1087 vnet_sw_interface_t *swif;
1089 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1092 if (unformat(input, "%U",
1093 unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1095 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1096 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1097 swif->unnumbered_sw_if_index : sw_if_index0;
1098 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0);
1101 vlib_cli_output (vm, "%=20s%=20s", "interface",
1102 "source IP address");
1104 vlib_cli_output (vm, "%=20U%=20U",
1105 format_vnet_sw_if_index_name,
1106 dm->vnet_main, sw_if_index0,
1107 format_ip4_address, ia0);
1110 vlib_cli_output (vm, "%=34s %=20U",
1111 "No IPv4 address configured on",
1112 format_vnet_sw_if_index_name,
1113 dm->vnet_main, sw_if_index);
1122 VLIB_CLI_COMMAND (dhcp_proxy_address_show_command,static) = {
1123 .path = "show dhcp option-82-address interface",
1124 .short_help = "show dhcp option-82-address interface <interface>",
1125 .function = dhcp_option_82_address_show_command_fn,