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");
96 /* get first interface address */
97 static ip4_address_t *
98 ip4_interface_first_address (ip4_main_t * im, u32 sw_if_index)
100 ip_lookup_main_t * lm = &im->lookup_main;
101 ip_interface_address_t * ia = 0;
102 ip4_address_t * result = 0;
104 foreach_ip_interface_address (lm, ia, sw_if_index,
105 1 /* honor unnumbered */,
107 ip4_address_t * a = ip_interface_address_get_address (lm, ia);
115 dhcp_proxy_to_server_input (vlib_main_t * vm,
116 vlib_node_runtime_t * node,
117 vlib_frame_t * from_frame)
119 u32 n_left_from, next_index, * from, * to_next;
120 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
121 from = vlib_frame_vector_args (from_frame);
122 n_left_from = from_frame->n_vectors;
123 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
124 u32 pkts_no_interface_address=0;
126 ip4_main_t * im = &ip4_main;
128 next_index = node->cached_next_index;
130 while (n_left_from > 0)
134 vlib_get_next_frame (vm, node, next_index,
135 to_next, n_left_to_next);
137 while (n_left_from > 0 && n_left_to_next > 0)
147 u32 error0 = (u32) ~0;
149 u32 original_sw_if_index = 0;
151 u32 fib_index, server_index;
152 dhcp_server_t * server;
162 b0 = vlib_get_buffer (vm, bi0);
164 h0 = vlib_buffer_get_current (b0);
167 * udp_local hands us the DHCP header, need udp hdr,
168 * ip hdr to relay to server
170 vlib_buffer_advance (b0, -(sizeof(*u0)));
171 u0 = vlib_buffer_get_current (b0);
173 /* This blows. Return traffic has src_port = 67, dst_port = 67 */
174 if (u0->src_port == clib_net_to_host_u16(UDP_DST_PORT_dhcp_to_server))
176 vlib_buffer_advance (b0, sizeof(*u0));
177 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
183 rx_sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
185 fib_index = im->fib_index_by_sw_if_index [rx_sw_if_index];
187 if (fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
188 server_index = dpm->dhcp_server_index_by_rx_fib_index[fib_index];
192 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp_servers,
196 error0 = DHCP_PROXY_ERROR_NO_SERVER;
197 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
202 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
203 if (server->valid == 0)
206 vlib_buffer_advance (b0, -(sizeof(*ip0)));
207 ip0 = vlib_buffer_get_current (b0);
209 /* disable UDP checksum */
211 sum0 = ip0->checksum;
212 old0 = ip0->dst_address.as_u32;
213 new0 = server->dhcp_server.as_u32;
214 ip0->dst_address.as_u32 = server->dhcp_server.as_u32;
215 sum0 = ip_csum_update (sum0, old0, new0,
216 ip4_header_t /* structure */,
217 dst_address /* changed member */);
218 ip0->checksum = ip_csum_fold (sum0);
220 sum0 = ip0->checksum;
221 old0 = ip0->src_address.as_u32;
222 new0 = server->dhcp_src_address.as_u32;
223 ip0->src_address.as_u32 = new0;
224 sum0 = ip_csum_update (sum0, old0, new0,
225 ip4_header_t /* structure */,
226 src_address /* changed member */);
227 ip0->checksum = ip_csum_fold (sum0);
229 /* Send to DHCP server via the configured FIB */
230 vnet_buffer(b0)->sw_if_index[VLIB_TX] =
231 server->server_fib_index;
233 h0->gateway_ip_address.as_u32 = server->dhcp_src_address.as_u32;
236 if (server->insert_option_82)
238 u32 fib_index, fib_id, opt82_fib_id=0, opt82_oui=0;
240 dhcp_option_t *o = (dhcp_option_t *) h0->options;
242 vlib_buffer_free_list_t *fl;
244 fib_index = im->fib_index_by_sw_if_index
245 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
246 fib = vec_elt_at_index (im->fibs, fib_index);
247 fib_id = fib->table_id;
249 end = b0->data + b0->current_data + b0->current_length;
250 /* TLVs are not performance-friendly... */
251 while (o->option != 0xFF /* end of options */ && (u8 *)o < end)
252 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
254 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
255 // start write at (option*)o, some packets have padding
256 if (((u8 *)o - (u8 *)b0->data + VPP_DHCP_OPTION82_SIZE) > fl->n_data_bytes)
258 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
263 if ((o->option == 0xFF) && ((u8 *)o <= end))
265 vnet_main_t *vnm = vnet_get_main();
267 ip4_address_t _ia0, * ia0 = &_ia0;
270 vnet_sw_interface_t *swif;
272 original_sw_if_index = 0;
274 original_sw_if_index = sw_if_index =
275 vnet_buffer(b0)->sw_if_index[VLIB_RX];
276 swif = vnet_get_sw_interface (vnm, sw_if_index);
277 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
278 sw_if_index = swif->unnumbered_sw_if_index;
280 p_vss = hash_get (dpm->opt82vss_index_by_vrf_id,
284 vss = pool_elt_at_index (dpm->opt82vss, p_vss[0]);
285 opt82_oui = vss->vpn_id.oui;
286 opt82_fib_id = vss->vpn_id.fib_id;
289 * Get the first ip4 address on the [client-side]
290 * RX interface, if not unnumbered. otherwise use
291 * the loopback interface's ip address.
293 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index);
297 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
298 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
299 pkts_no_interface_address++;
304 o->option = 82; /* option 82 */
305 o->length = 12; /* 12 octets to follow */
306 o->data[0] = 1; /* suboption 1, circuit ID (=FIB id) */
307 o->data[1] = 4; /* length of suboption */
308 o->data[2] = (original_sw_if_index >> 24) & 0xFF;
309 o->data[3] = (original_sw_if_index >> 16) & 0xFF;
310 o->data[4] = (original_sw_if_index >> 8) & 0xFF;
311 o->data[5] = (original_sw_if_index >> 0) & 0xFF;
312 o->data[6] = 5; /* suboption 5 (client RX intfc address) */
313 o->data[7] = 4; /* length 4 */
314 o->data[8] = ia0->as_u8[0];
315 o->data[9] = ia0->as_u8[1];
316 o->data[10] = ia0->as_u8[2];
317 o->data[11] = ia0->as_u8[3];
319 if (opt82_oui !=0 || opt82_fib_id != 0)
321 o->data[12] = 151; /* vss suboption */
322 if (255 == opt82_fib_id) {
323 o->data[13] = 1; /* length */
324 o->data[14] = 255; /* vss option type */
325 o->data[15] = 152; /* vss control suboption */
326 o->data[16] = 0; /* length */
327 /* and a new "end-of-options" option (0xff) */
331 o->data[13] = 8; /* length */
332 o->data[14] = 1; /* vss option type */
333 o->data[15] = (opt82_oui >> 16) & 0xff;
334 o->data[16] = (opt82_oui >> 8) & 0xff;
335 o->data[17] = (opt82_oui ) & 0xff;
336 o->data[18] = (opt82_fib_id >> 24) & 0xff;
337 o->data[19] = (opt82_fib_id >> 16) & 0xff;
338 o->data[20] = (opt82_fib_id >> 8) & 0xff;
339 o->data[21] = (opt82_fib_id) & 0xff;
340 o->data[22] = 152; /* vss control suboption */
341 o->data[23] = 0; /* length */
343 /* and a new "end-of-options" option (0xff) */
350 b0->current_length += len;
351 /* Fix IP header length and checksum */
352 old_l0 = ip0->length;
353 new_l0 = clib_net_to_host_u16 (old_l0);
355 new_l0 = clib_host_to_net_u16 (new_l0);
356 ip0->length = new_l0;
357 sum0 = ip0->checksum;
358 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
359 length /* changed member */);
360 ip0->checksum = ip_csum_fold (sum0);
363 new_l0 = clib_net_to_host_u16 (u0->length);
365 u0->length = clib_host_to_net_u16 (new_l0);
367 vlib_node_increment_counter
368 (vm, dhcp_proxy_to_server_node.index,
369 DHCP_PROXY_ERROR_OPTION_82_ERROR, 1);
373 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
376 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
378 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
380 tr->which = 0; /* to server */
382 tr->original_sw_if_index = original_sw_if_index;
383 tr->sw_if_index = sw_if_index;
384 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
385 tr->trace_ip4_address.as_u32 = server->dhcp_server.as_u32;
389 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
390 to_next, n_left_to_next,
394 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
397 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
398 DHCP_PROXY_ERROR_RELAY_TO_CLIENT,
400 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
401 DHCP_PROXY_ERROR_RELAY_TO_SERVER,
403 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
404 DHCP_PROXY_ERROR_NO_SERVER,
406 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
407 DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS,
408 pkts_no_interface_address);
409 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
410 DHCP_PROXY_ERROR_PKT_TOO_BIG,
412 return from_frame->n_vectors;
415 VLIB_REGISTER_NODE (dhcp_proxy_to_server_node) = {
416 .function = dhcp_proxy_to_server_input,
417 .name = "dhcp-proxy-to-server",
418 /* Takes a vector of packets. */
419 .vector_size = sizeof (u32),
421 .n_errors = DHCP_PROXY_N_ERROR,
422 .error_strings = dhcp_proxy_error_strings,
424 .n_next_nodes = DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
426 #define _(s,n) [DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
427 foreach_dhcp_proxy_to_server_input_next
431 .format_buffer = format_dhcp_proxy_header_with_length,
432 .format_trace = format_dhcp_proxy_trace,
434 .unformat_buffer = unformat_dhcp_proxy_header,
439 dhcp_proxy_to_client_input (vlib_main_t * vm,
440 vlib_node_runtime_t * node,
441 vlib_frame_t * from_frame)
443 u32 n_left_from, * from;
444 ethernet_main_t *em = ethernet_get_main (vm);
445 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
446 vnet_main_t * vnm = vnet_get_main();
447 ip4_main_t * im = &ip4_main;
449 from = vlib_frame_vector_args (from_frame);
450 n_left_from = from_frame->n_vectors;
452 while (n_left_from > 0)
458 ip4_header_t * ip0 = 0;
459 ip4_address_t * ia0 = 0;
462 ethernet_interface_t *ei0;
463 ethernet_header_t *mac0;
464 vnet_hw_interface_t *hi0;
467 u32 sw_if_index = ~0;
468 vnet_sw_interface_t *si0;
469 u32 error0 = (u32)~0;
470 vnet_sw_interface_t *swif;
473 dhcp_server_t * server;
474 u32 original_sw_if_index = (u32) ~0;
480 b0 = vlib_get_buffer (vm, bi0);
481 h0 = vlib_buffer_get_current (b0);
484 * udp_local hands us the DHCP header, need udp hdr,
485 * ip hdr to relay to client
487 vlib_buffer_advance (b0, -(sizeof(*u0)));
488 u0 = vlib_buffer_get_current (b0);
490 vlib_buffer_advance (b0, -(sizeof(*ip0)));
491 ip0 = vlib_buffer_get_current (b0);
493 /* Consumed by dhcp client code? */
494 if (dhcp_client_for_us (bi0, b0, ip0, u0, h0))
497 if (1 /* dpm->insert_option_82 */)
499 dhcp_option_t *o = (dhcp_option_t *) h0->options;
502 /* Parse through TLVs looking for option 82.
503 The circuit-ID is the FIB number we need
504 to track down the client-facing interface */
506 while (o->option != 0xFF /* end of options */ &&
507 (u8 *) o < (b0->data + b0->current_data + b0->current_length))
513 sub = (dhcp_option_t *) &o->data[0];
514 while (sub->option != 0xFF /* end of options */ &&
515 (u8 *) sub < (u8 *)(o + o->length)) {
516 /* If this is one of ours, it will have
517 total length 12, circuit-id suboption type,
518 and the sw_if_index */
519 if (sub->option == 1 && sub->length == 4)
521 sw_if_index = (o->data[2] << 24)
525 } else if (sub->option == 151 &&
529 else if (sub->option == 152 && sub->length == 0)
531 sub = (dhcp_option_t *)
532 (((uword) sub) + (sub->length + 2));
534 if (vss_ctrl && vss_exist)
535 vlib_node_increment_counter
536 (vm, dhcp_proxy_to_client_node.index,
537 DHCP_PROXY_ERROR_OPTION_82_VSS_NOT_PROCESSED, 1);
540 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
544 if (sw_if_index == (u32)~0)
546 error0 = DHCP_PROXY_ERROR_NO_OPTION_82;
549 vlib_node_increment_counter (vm, dhcp_proxy_to_client_node.index,
551 f0 = vlib_get_frame_to_node (vm, dpm->error_drop_node_index);
552 to_next0 = vlib_frame_vector_args (f0);
555 vlib_put_frame_to_node (vm, dpm->error_drop_node_index, f0);
560 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
562 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82;
566 fib_index = im->fib_index_by_sw_if_index [sw_if_index];
568 if (fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
569 server_index = dpm->dhcp_server_index_by_rx_fib_index[fib_index];
573 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp_servers,
576 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82;
580 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
581 if (server->valid == 0)
583 error0 = DHCP_PROXY_ERROR_NO_SERVER;
587 if (ip0->src_address.as_u32 != server->dhcp_server.as_u32)
589 error0 = DHCP_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
593 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
595 swif = vnet_get_sw_interface (vnm, sw_if_index);
596 original_sw_if_index = sw_if_index;
597 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
598 sw_if_index = swif->unnumbered_sw_if_index;
600 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index);
603 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
608 u0->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client);
609 sum0 = ip0->checksum;
610 old0 = ip0->dst_address.as_u32;
612 ip0->dst_address.as_u32 = new0;
613 sum0 = ip_csum_update (sum0, old0, new0,
614 ip4_header_t /* structure */,
615 dst_address /* offset of changed member */);
616 ip0->checksum = ip_csum_fold (sum0);
618 sum0 = ip0->checksum;
619 old0 = ip0->src_address.as_u32;
621 ip0->src_address.as_u32 = new0;
622 sum0 = ip_csum_update (sum0, old0, new0,
623 ip4_header_t /* structure */,
624 src_address /* offset of changed member */);
625 ip0->checksum = ip_csum_fold (sum0);
627 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
628 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
629 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
630 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
632 mac0 = vlib_buffer_get_current (b0);
634 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
635 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
636 memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
637 memset (mac0->dst_address, 0xff, sizeof (mac0->dst_address));
638 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
639 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x0800);
641 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
643 u32 * vlan_tag = (u32 *)(mac0+1);
645 tmp = (si0->sub.id << 16) | 0x0800;
646 *vlan_tag = clib_host_to_net_u32 (tmp);
649 /* $$$ This needs to be rewritten, for sure */
650 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
651 to_next0 = vlib_frame_vector_args (f0);
654 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
657 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
659 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
661 tr->which = 1; /* to client */
662 tr->trace_ip4_address.as_u32 = ia0 ? ia0->as_u32 : 0;
664 tr->original_sw_if_index = original_sw_if_index;
665 tr->sw_if_index = sw_if_index;
668 return from_frame->n_vectors;
671 VLIB_REGISTER_NODE (dhcp_proxy_to_client_node) = {
672 .function = dhcp_proxy_to_client_input,
673 .name = "dhcp-proxy-to-client",
674 /* Takes a vector of packets. */
675 .vector_size = sizeof (u32),
677 .n_errors = DHCP_PROXY_N_ERROR,
678 .error_strings = dhcp_proxy_error_strings,
679 .format_buffer = format_dhcp_proxy_header_with_length,
680 .format_trace = format_dhcp_proxy_trace,
682 .unformat_buffer = unformat_dhcp_proxy_header,
686 clib_error_t * dhcp_proxy_init (vlib_main_t * vm)
688 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
689 vlib_node_t * error_drop_node;
690 dhcp_server_t * server;
693 dm->vnet_main = vnet_get_main();
694 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
695 dm->error_drop_node_index = error_drop_node->index;
697 dm->opt82vss_index_by_vrf_id = hash_create (0, sizeof (uword));
699 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
700 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */);
702 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
703 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */);
705 /* Create the default server, don't mark it valid */
706 pool_get (dm->dhcp_servers, server);
707 memset (server, 0, sizeof (*server));
712 VLIB_INIT_FUNCTION (dhcp_proxy_init);
714 int dhcp_proxy_set_server_2 (ip4_address_t *addr, ip4_address_t *src_address,
717 int insert_option_82, int is_del)
719 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
720 ip4_main_t * im = &ip4_main;
721 dhcp_server_t * server = 0;
722 ip4_fib_t *rx_fib, *server_fib;
723 u32 server_index = 0;
724 u32 rx_fib_index = 0;
726 if (addr->as_u32 == 0)
727 return VNET_API_ERROR_INVALID_DST_ADDRESS;
729 if (src_address->as_u32 == 0)
730 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
732 rx_fib = find_ip4_fib_by_table_index_or_id
733 (&ip4_main, rx_fib_id, IP4_ROUTE_FLAG_TABLE_ID);
736 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
738 server_fib = find_ip4_fib_by_table_index_or_id
739 (&ip4_main, server_fib_id, IP4_ROUTE_FLAG_TABLE_ID);
742 return VNET_API_ERROR_NO_SUCH_FIB;
746 server = pool_elt_at_index (dpm->dhcp_servers, 0);
750 memset (server, 0, sizeof (*server));
756 rx_fib_index = rx_fib - im->fibs;
760 if (rx_fib_index >= vec_len(dpm->dhcp_server_index_by_rx_fib_index))
761 return VNET_API_ERROR_NO_SUCH_ENTRY;
763 server_index = dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index];
764 ASSERT(server_index > 0);
766 /* Use the default server again. */
767 dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index] = 0;
768 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
769 memset (server, 0, sizeof (*server));
770 pool_put (dpm->dhcp_servers, server);
774 if (rx_fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
776 server_index = dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index];
777 if (server_index != 0)
779 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
784 pool_get (dpm->dhcp_servers, server);
788 server->dhcp_server.as_u32 = addr->as_u32;
789 server->server_fib_index = server_fib - im->fibs;
790 server->dhcp_src_address.as_u32 = src_address->as_u32;
791 server->insert_option_82 = insert_option_82;
795 vec_validate (dpm->dhcp_server_index_by_rx_fib_index, rx_fib_index);
796 dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index] =
797 server - dpm->dhcp_servers;
803 /* Old API, manipulates the default server (only) */
804 int dhcp_proxy_set_server (ip4_address_t *addr, ip4_address_t *src_address,
805 u32 fib_id, int insert_option_82, int is_del)
807 return dhcp_proxy_set_server_2 (addr, src_address, 0 /* rx_fib_id */,
808 fib_id /* server_fib_id */,
809 insert_option_82, is_del);
813 static clib_error_t *
814 dhcp_proxy_set_command_fn (vlib_main_t * vm,
815 unformat_input_t * input,
816 vlib_cli_command_t * cmd)
818 ip4_address_t server_addr, src_addr;
819 u32 server_fib_id = 0, rx_fib_id = 0;
821 int add_option_82 = 0;
822 int set_src = 0, set_server = 0;
824 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
826 if (unformat (input, "server %U",
827 unformat_ip4_address, &server_addr))
829 else if (unformat (input, "server-fib-id %d", &server_fib_id))
831 else if (unformat (input, "rx-fib-id %d", &rx_fib_id))
833 else if (unformat(input, "src-address %U",
834 unformat_ip4_address, &src_addr))
836 else if (unformat (input, "add-option-82")
837 || unformat (input, "insert-option-82"))
839 else if (unformat (input, "delete") ||
840 unformat (input, "del"))
846 if (is_del || (set_server && set_src))
850 rv = dhcp_proxy_set_server_2 (&server_addr, &src_addr, rx_fib_id,
851 server_fib_id, add_option_82, is_del);
857 case VNET_API_ERROR_INVALID_DST_ADDRESS:
858 return clib_error_return (0, "Invalid server address");
860 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
861 return clib_error_return (0, "Invalid src address");
863 case VNET_API_ERROR_NO_SUCH_INNER_FIB:
864 return clib_error_return (0, "No such rx fib id %d", rx_fib_id);
866 case VNET_API_ERROR_NO_SUCH_FIB:
867 return clib_error_return (0, "No such server fib id %d",
870 case VNET_API_ERROR_NO_SUCH_ENTRY:
871 return clib_error_return
872 (0, "Fib id %d: no per-fib DHCP server configured", rx_fib_id);
875 return clib_error_return (0, "BUG: rv %d", rv);
879 return clib_error_return (0, "parse error`%U'",
880 format_unformat_error, input);
883 VLIB_CLI_COMMAND (dhcp_proxy_set_command, static) = {
884 .path = "set dhcp proxy",
885 .short_help = "set dhcp proxy [del] server <ip-addr> src-address <ip-addr> [add-option-82] [server-fib-id <n>] [rx-fib-id <n>]",
886 .function = dhcp_proxy_set_command_fn,
889 u8 * format_dhcp_proxy_server (u8 * s, va_list * args)
891 dhcp_proxy_main_t * dm = va_arg (*args, dhcp_proxy_main_t *);
892 dhcp_server_t * server = va_arg (*args, dhcp_server_t *);
893 u32 rx_fib_index = va_arg (*args, u32);
894 ip4_fib_t * rx_fib, * server_fib;
895 u32 server_fib_id = ~0, rx_fib_id = ~0;
899 s = format (s, "%=16s%=16s%=14s%=14s%=20s", "Server", "Src Address",
900 "Server FIB", "RX FIB", "Insert Option 82");
904 server_fib = find_ip4_fib_by_table_index_or_id
905 (&ip4_main, server->server_fib_index, IP4_ROUTE_FLAG_FIB_INDEX);
908 server_fib_id = server_fib->table_id;
910 rx_fib = find_ip4_fib_by_table_index_or_id
911 (&ip4_main, rx_fib_index, IP4_ROUTE_FLAG_FIB_INDEX);
914 rx_fib_id = rx_fib->table_id;
916 s = format (s, "%=16U%=16U%=14u%=14u%=20s",
917 format_ip4_address, &server->dhcp_server,
918 format_ip4_address, &server->dhcp_src_address,
919 server_fib_id, rx_fib_id,
920 server->insert_option_82 ? "yes" : "no");
924 static clib_error_t *
925 dhcp_proxy_show_command_fn (vlib_main_t * vm,
926 unformat_input_t * input,
927 vlib_cli_command_t * cmd)
929 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
930 ip4_main_t * im = &ip4_main;
931 dhcp_server_t * server;
935 vlib_cli_output (vm, "%U", format_dhcp_proxy_server, 0 /* header line */,
938 for (i = 0; i < vec_len (im->fibs); i++)
940 if (i < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
941 server_index = dpm->dhcp_server_index_by_rx_fib_index[i];
944 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
946 vlib_cli_output (vm, "%U", format_dhcp_proxy_server, dpm,
953 VLIB_CLI_COMMAND (dhcp_proxy_show_command, static) = {
954 .path = "show dhcp proxy",
955 .short_help = "Display dhcp proxy server info",
956 .function = dhcp_proxy_show_command_fn,
960 int dhcp_proxy_set_option82_vss( u32 vrf_id,
965 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
968 u32 old_oui=0, old_fib_id=0;
970 p = hash_get (dm->opt82vss_index_by_vrf_id, vrf_id);
974 a = pool_elt_at_index (dm->opt82vss, p[0]);
976 return VNET_API_ERROR_NO_SUCH_FIB;
977 old_oui = a->vpn_id.oui;
978 old_fib_id = a->vpn_id.fib_id;
982 if (old_oui == oui &&
983 old_fib_id == fib_id)
985 pool_put(dm->opt82vss, a);
986 hash_unset (dm->opt82vss_index_by_vrf_id, vrf_id);
990 return VNET_API_ERROR_NO_SUCH_ENTRY;
992 pool_put(dm->opt82vss, a);
993 hash_unset (dm->opt82vss_index_by_vrf_id, vrf_id);
995 return VNET_API_ERROR_NO_SUCH_ENTRY;
996 pool_get (dm->opt82vss, a);
997 memset (a, ~0, sizeof (a[0]));
999 a->vpn_id.fib_id = fib_id;
1000 hash_set (dm->opt82vss_index_by_vrf_id, vrf_id, a - dm->opt82vss);
1005 static clib_error_t *
1006 dhcp_option_82_vss_fn (vlib_main_t * vm,
1007 unformat_input_t * input,
1008 vlib_cli_command_t * cmd)
1010 int is_del = 0, got_new_vpn_id=0;
1011 u32 oui=0, fib_id=0, tbl_id=~0;
1014 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1017 if (unformat(input, "delete") || unformat(input, "del"))
1019 else if (unformat (input, "oui %d", &oui))
1021 else if (unformat (input, "vpn-id %d", &fib_id))
1023 else if (unformat (input, "table %d", &tbl_id))
1029 return clib_error_return (0, "no table ID specified.");
1031 if (is_del || got_new_vpn_id)
1034 rv = dhcp_proxy_set_option82_vss(tbl_id, oui, fib_id, is_del);
1040 case VNET_API_ERROR_NO_SUCH_FIB:
1041 return clib_error_return (0, "option 82 vss(oui:%d, vpn-id:%d) not found in table %d",
1042 oui, fib_id, tbl_id);
1044 case VNET_API_ERROR_NO_SUCH_ENTRY:
1045 return clib_error_return (0, "option 82 vss for table %d not found in in pool.",
1048 return clib_error_return (0, "BUG: rv %d", rv);
1052 return clib_error_return (0, "parse error`%U'",
1053 format_unformat_error, input);
1056 VLIB_CLI_COMMAND (dhcp_proxy_vss_command,static) = {
1057 .path = "set dhcp option-82 vss",
1058 .short_help = "set dhcp option-82 vss [del] table <table id> oui <oui> vpn-id <vpn-id>",
1059 .function = dhcp_option_82_vss_fn,
1063 static clib_error_t *
1064 dhcp_vss_show_command_fn (vlib_main_t * vm,
1065 unformat_input_t * input,
1066 vlib_cli_command_t * cmd)
1069 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
1076 vlib_cli_output (vm, "%=9s%=11s%=12s","Table", "OUI", "VPN-ID");
1077 hash_foreach (tbl_id, index, dm->opt82vss_index_by_vrf_id,
1079 v = pool_elt_at_index (dm->opt82vss, index);
1080 oui = v->vpn_id.oui;
1081 fib_id = v->vpn_id.fib_id;
1082 vlib_cli_output (vm, "%=9d 0x%08x%=12d",
1083 tbl_id, oui, fib_id);
1089 VLIB_CLI_COMMAND (dhcp_proxy_vss_show_command, static) = {
1090 .path = "show dhcp vss",
1091 .short_help = "show dhcp VSS",
1092 .function = dhcp_vss_show_command_fn,
1095 static clib_error_t *
1096 dhcp_option_82_address_show_command_fn (vlib_main_t * vm,
1097 unformat_input_t * input,
1098 vlib_cli_command_t * cmd)
1101 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
1102 vnet_main_t *vnm = vnet_get_main();
1103 u32 sw_if_index0=0, sw_if_index;
1105 vnet_sw_interface_t *swif;
1107 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1110 if (unformat(input, "%U",
1111 unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1113 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1114 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1115 swif->unnumbered_sw_if_index : sw_if_index0;
1116 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index);
1119 vlib_cli_output (vm, "%=20s%=20s", "interface",
1120 "source IP address");
1122 vlib_cli_output (vm, "%=20U%=20U",
1123 format_vnet_sw_if_index_name,
1124 dm->vnet_main, sw_if_index0,
1125 format_ip4_address, ia0);
1128 vlib_cli_output (vm, "%=34s %=20U",
1129 "No IPv4 address configured on",
1130 format_vnet_sw_if_index_name,
1131 dm->vnet_main, sw_if_index);
1140 VLIB_CLI_COMMAND (dhcp_proxy_address_show_command,static) = {
1141 .path = "show dhcp option-82-address interface",
1142 .short_help = "show dhcp option-82-address interface <interface>",
1143 .function = dhcp_option_82_address_show_command_fn,