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/dhcp_proxy.h>
21 #include <vnet/dhcp/client.h>
22 #include <vnet/fib/ip4_fib.h>
24 static char * dhcp_proxy_error_strings[] = {
25 #define dhcp_proxy_error(n,s) s,
26 #include <vnet/dhcp/dhcp4_proxy_error.def>
27 #undef dhcp_proxy_error
30 #define foreach_dhcp_proxy_to_server_input_next \
31 _ (DROP, "error-drop") \
32 _ (LOOKUP, "ip4-lookup") \
33 _ (SEND_TO_CLIENT, "dhcp-proxy-to-client")
36 #define _(s,n) DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s,
37 foreach_dhcp_proxy_to_server_input_next
39 DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
40 } dhcp_proxy_to_server_input_next_t;
43 /* 0 => to server, 1 => to client */
45 ip4_address_t trace_ip4_address;
48 u32 original_sw_if_index;
51 #define VPP_DHCP_OPTION82_SUB1_SIZE 6
52 #define VPP_DHCP_OPTION82_SUB5_SIZE 6
53 #define VPP_DHCP_OPTION82_VSS_SIZE 12
54 #define VPP_DHCP_OPTION82_SIZE (VPP_DHCP_OPTION82_SUB1_SIZE + \
55 VPP_DHCP_OPTION82_SUB5_SIZE + \
56 VPP_DHCP_OPTION82_VSS_SIZE +3)
58 static vlib_node_registration_t dhcp_proxy_to_server_node;
59 static vlib_node_registration_t dhcp_proxy_to_client_node;
62 format_dhcp_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 dhcp_proxy_trace_t * t = va_arg (*args, dhcp_proxy_trace_t *);
69 s = format (s, "DHCP proxy: sent to server %U\n",
70 format_ip4_address, &t->trace_ip4_address, t->error);
72 s = format (s, "DHCP proxy: broadcast to client from %U\n",
73 format_ip4_address, &t->trace_ip4_address);
75 if (t->error != (u32)~0)
76 s = format (s, " error: %s\n", dhcp_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_dhcp_proxy_header_with_length (u8 * s, va_list * args)
87 dhcp_header_t * h = va_arg (*args, dhcp_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, "dhcp header truncated");
95 s = format (s, "DHCP Proxy");
101 dhcp_proxy_to_server_input (vlib_main_t * vm,
102 vlib_node_runtime_t * node,
103 vlib_frame_t * from_frame)
105 u32 n_left_from, next_index, * from, * to_next;
106 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
107 from = vlib_frame_vector_args (from_frame);
108 n_left_from = from_frame->n_vectors;
109 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
110 u32 pkts_no_interface_address=0;
112 ip4_main_t * im = &ip4_main;
114 next_index = node->cached_next_index;
116 while (n_left_from > 0)
120 vlib_get_next_frame (vm, node, next_index,
121 to_next, n_left_to_next);
123 while (n_left_from > 0 && n_left_to_next > 0)
133 u32 error0 = (u32) ~0;
135 u32 original_sw_if_index = 0;
139 dhcp_server_t *server;
143 vlib_buffer_free_list_t *fl;
150 b0 = vlib_get_buffer (vm, bi0);
152 h0 = vlib_buffer_get_current (b0);
155 * udp_local hands us the DHCP header, need udp hdr,
156 * ip hdr to relay to server
158 vlib_buffer_advance (b0, -(sizeof(*u0)));
159 u0 = vlib_buffer_get_current (b0);
161 /* This blows. Return traffic has src_port = 67, dst_port = 67 */
162 if (u0->src_port == clib_net_to_host_u16(UDP_DST_PORT_dhcp_to_server))
164 vlib_buffer_advance (b0, sizeof(*u0));
165 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
171 rx_sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
173 fib_index = im->fib_index_by_sw_if_index [rx_sw_if_index];
174 proxy = dhcp_get_proxy(dpm, fib_index, FIB_PROTOCOL_IP4);
176 if (PREDICT_FALSE (NULL == proxy))
178 error0 = DHCP_PROXY_ERROR_NO_SERVER;
179 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
184 server = &proxy->dhcp_servers[0];
185 vlib_buffer_advance (b0, -(sizeof(*ip0)));
186 ip0 = vlib_buffer_get_current (b0);
188 /* disable UDP checksum */
190 sum0 = ip0->checksum;
191 old0 = ip0->dst_address.as_u32;
192 new0 = server->dhcp_server.ip4.as_u32;
193 ip0->dst_address.as_u32 = server->dhcp_server.ip4.as_u32;
194 sum0 = ip_csum_update (sum0, old0, new0,
195 ip4_header_t /* structure */,
196 dst_address /* changed member */);
197 ip0->checksum = ip_csum_fold (sum0);
199 sum0 = ip0->checksum;
200 old0 = ip0->src_address.as_u32;
201 new0 = proxy->dhcp_src_address.ip4.as_u32;
202 ip0->src_address.as_u32 = new0;
203 sum0 = ip_csum_update (sum0, old0, new0,
204 ip4_header_t /* structure */,
205 src_address /* changed member */);
206 ip0->checksum = ip_csum_fold (sum0);
208 /* Send to DHCP server via the configured FIB */
209 vnet_buffer(b0)->sw_if_index[VLIB_TX] =
210 server->server_fib_index;
212 h0->gateway_ip_address.as_u32 = proxy->dhcp_src_address.ip4.as_u32;
215 o = (dhcp_option_t *) h0->options;
217 fib_index = im->fib_index_by_sw_if_index
218 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
220 end = b0->data + b0->current_data + b0->current_length;
221 /* TLVs are not performance-friendly... */
222 while (o->option != 0xFF /* end of options */ && (u8 *)o < end)
224 if (DHCP_PACKET_OPTION_MSG_TYPE == o->option)
226 if (DHCP_PACKET_DISCOVER == o->data[0])
231 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
234 fl = vlib_buffer_get_free_list (vm, vlib_buffer_get_free_list_index (b0));
235 // start write at (option*)o, some packets have padding
236 if (((u8 *)o - (u8 *)b0->data + VPP_DHCP_OPTION82_SIZE) > fl->n_data_bytes)
238 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
243 if ((o->option == 0xFF) && ((u8 *)o <= end))
245 vnet_main_t *vnm = vnet_get_main();
247 ip4_address_t _ia0, * ia0 = &_ia0;
249 vnet_sw_interface_t *swif;
251 original_sw_if_index = 0;
253 original_sw_if_index = sw_if_index =
254 vnet_buffer(b0)->sw_if_index[VLIB_RX];
255 swif = vnet_get_sw_interface (vnm, sw_if_index);
256 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
257 sw_if_index = swif->unnumbered_sw_if_index;
260 * Get the first ip4 address on the [client-side]
261 * RX interface, if not unnumbered. otherwise use
262 * the loopback interface's ip address.
264 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0);
268 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
269 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
270 pkts_no_interface_address++;
275 o->option = 82; /* option 82 */
276 o->length = 12; /* 12 octets to follow */
277 o->data[0] = 1; /* suboption 1, circuit ID (=FIB id) */
278 o->data[1] = 4; /* length of suboption */
279 o->data[2] = (original_sw_if_index >> 24) & 0xFF;
280 o->data[3] = (original_sw_if_index >> 16) & 0xFF;
281 o->data[4] = (original_sw_if_index >> 8) & 0xFF;
282 o->data[5] = (original_sw_if_index >> 0) & 0xFF;
283 o->data[6] = 5; /* suboption 5 (client RX intfc address) */
284 o->data[7] = 4; /* length 4 */
285 o->data[8] = ia0->as_u8[0];
286 o->data[9] = ia0->as_u8[1];
287 o->data[10] = ia0->as_u8[2];
288 o->data[11] = ia0->as_u8[3];
291 vss = dhcp_get_vss_info (dpm, fib_index, FIB_PROTOCOL_IP4);
294 u32 opt82_fib_id=0, opt82_oui=0;
296 opt82_oui = vss->oui;
297 opt82_fib_id = vss->fib_id;
299 o->data[12] = 151; /* vss suboption */
300 if (255 == opt82_fib_id) {
301 o->data[13] = 1; /* length */
302 o->data[14] = 255; /* vss option type */
303 o->data[15] = 152; /* vss control suboption */
304 o->data[16] = 0; /* length */
305 /* and a new "end-of-options" option (0xff) */
309 o->data[13] = 8; /* length */
310 o->data[14] = 1; /* vss option type */
311 o->data[15] = (opt82_oui >> 16) & 0xff;
312 o->data[16] = (opt82_oui >> 8) & 0xff;
313 o->data[17] = (opt82_oui ) & 0xff;
314 o->data[18] = (opt82_fib_id >> 24) & 0xff;
315 o->data[19] = (opt82_fib_id >> 16) & 0xff;
316 o->data[20] = (opt82_fib_id >> 8) & 0xff;
317 o->data[21] = (opt82_fib_id) & 0xff;
318 o->data[22] = 152; /* vss control suboption */
319 o->data[23] = 0; /* length */
321 /* and a new "end-of-options" option (0xff) */
328 b0->current_length += len;
329 /* Fix IP header length and checksum */
330 old_l0 = ip0->length;
331 new_l0 = clib_net_to_host_u16 (old_l0);
333 new_l0 = clib_host_to_net_u16 (new_l0);
334 ip0->length = new_l0;
335 sum0 = ip0->checksum;
336 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
337 length /* changed member */);
338 ip0->checksum = ip_csum_fold (sum0);
341 new_l0 = clib_net_to_host_u16 (u0->length);
343 u0->length = clib_host_to_net_u16 (new_l0);
345 vlib_node_increment_counter
346 (vm, dhcp_proxy_to_server_node.index,
347 DHCP_PROXY_ERROR_OPTION_82_ERROR, 1);
350 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
353 * If we have multiple servers configured and this is the
354 * client's discover message, then send copies to each of
357 if (is_discover && vec_len(proxy->dhcp_servers) > 1)
361 for (ii = 1; ii < vec_len(proxy->dhcp_servers); ii++)
366 c0 = vlib_buffer_copy(vm, b0);
367 ci0 = vlib_get_buffer_index(vm, c0);
368 server = &proxy->dhcp_servers[ii];
370 ip0 = vlib_buffer_get_current (c0);
372 sum0 = ip0->checksum;
373 old0 = ip0->dst_address.as_u32;
374 new0 = server->dhcp_server.ip4.as_u32;
375 ip0->dst_address.as_u32 = server->dhcp_server.ip4.as_u32;
376 sum0 = ip_csum_update (sum0, old0, new0,
377 ip4_header_t /* structure */,
378 dst_address /* changed member */);
379 ip0->checksum = ip_csum_fold (sum0);
385 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
386 to_next, n_left_to_next,
389 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
391 dhcp_proxy_trace_t *tr;
393 tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
394 tr->which = 0; /* to server */
396 tr->original_sw_if_index = original_sw_if_index;
397 tr->sw_if_index = sw_if_index;
398 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
399 tr->trace_ip4_address.as_u32 = server->dhcp_server.ip4.as_u32;
402 if (PREDICT_FALSE(0 == n_left_to_next))
404 vlib_put_next_frame (vm, node, next_index,
406 vlib_get_next_frame (vm, node, next_index,
407 to_next, n_left_to_next);
412 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
414 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
416 tr->which = 0; /* to server */
418 tr->original_sw_if_index = original_sw_if_index;
419 tr->sw_if_index = sw_if_index;
420 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
421 tr->trace_ip4_address.as_u32 =
422 proxy->dhcp_servers[0].dhcp_server.ip4.as_u32;
430 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
431 to_next, n_left_to_next,
435 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
438 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
439 DHCP_PROXY_ERROR_RELAY_TO_CLIENT,
441 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
442 DHCP_PROXY_ERROR_RELAY_TO_SERVER,
444 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
445 DHCP_PROXY_ERROR_NO_SERVER,
447 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
448 DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS,
449 pkts_no_interface_address);
450 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
451 DHCP_PROXY_ERROR_PKT_TOO_BIG,
453 return from_frame->n_vectors;
456 VLIB_REGISTER_NODE (dhcp_proxy_to_server_node, static) = {
457 .function = dhcp_proxy_to_server_input,
458 .name = "dhcp-proxy-to-server",
459 /* Takes a vector of packets. */
460 .vector_size = sizeof (u32),
462 .n_errors = DHCP_PROXY_N_ERROR,
463 .error_strings = dhcp_proxy_error_strings,
465 .n_next_nodes = DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
467 #define _(s,n) [DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
468 foreach_dhcp_proxy_to_server_input_next
472 .format_buffer = format_dhcp_proxy_header_with_length,
473 .format_trace = format_dhcp_proxy_trace,
475 .unformat_buffer = unformat_dhcp_proxy_header,
480 dhcp_proxy_to_client_input (vlib_main_t * vm,
481 vlib_node_runtime_t * node,
482 vlib_frame_t * from_frame)
484 u32 n_left_from, * from;
485 ethernet_main_t *em = ethernet_get_main (vm);
486 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
487 vnet_main_t * vnm = vnet_get_main();
488 ip4_main_t * im = &ip4_main;
490 from = vlib_frame_vector_args (from_frame);
491 n_left_from = from_frame->n_vectors;
493 while (n_left_from > 0)
499 ip4_header_t * ip0 = 0;
500 ip4_address_t * ia0 = 0;
503 ethernet_interface_t *ei0;
504 ethernet_header_t *mac0;
505 vnet_hw_interface_t *hi0;
508 u32 sw_if_index = ~0;
509 vnet_sw_interface_t *si0;
510 u32 error0 = (u32)~0;
511 vnet_sw_interface_t *swif;
514 dhcp_server_t *server;
515 u32 original_sw_if_index = (u32) ~0;
516 ip4_address_t relay_addr = {
524 b0 = vlib_get_buffer (vm, bi0);
525 h0 = vlib_buffer_get_current (b0);
528 * udp_local hands us the DHCP header, need udp hdr,
529 * ip hdr to relay to client
531 vlib_buffer_advance (b0, -(sizeof(*u0)));
532 u0 = vlib_buffer_get_current (b0);
534 vlib_buffer_advance (b0, -(sizeof(*ip0)));
535 ip0 = vlib_buffer_get_current (b0);
537 /* Consumed by dhcp client code? */
538 if (dhcp_client_for_us (bi0, b0, ip0, u0, h0))
541 if (1 /* dpm->insert_option_82 */)
543 dhcp_option_t *o = (dhcp_option_t *) h0->options;
546 /* Parse through TLVs looking for option 82.
547 The circuit-ID is the FIB number we need
548 to track down the client-facing interface */
550 while (o->option != 0xFF /* end of options */ &&
551 (u8 *) o < (b0->data + b0->current_data + b0->current_length))
557 sub = (dhcp_option_t *) &o->data[0];
558 while (sub->option != 0xFF /* end of options */ &&
559 (u8 *) sub < (u8 *)(o + o->length)) {
560 /* If this is one of ours, it will have
561 total length 12, circuit-id suboption type,
562 and the sw_if_index */
563 if (sub->option == 1 && sub->length == 4)
565 sw_if_index = ((sub->data[0] << 24) |
566 (sub->data[1] << 16) |
567 (sub->data[2] << 8) |
570 else if (sub->option == 5 && sub->length == 4)
572 relay_addr.as_u8[0] = sub->data[0];
573 relay_addr.as_u8[1] = sub->data[1];
574 relay_addr.as_u8[2] = sub->data[2];
575 relay_addr.as_u8[3] = sub->data[3];
577 else if (sub->option == 151 &&
581 else if (sub->option == 152 && sub->length == 0)
583 sub = (dhcp_option_t *)
584 (((uword) sub) + (sub->length + 2));
586 if (vss_ctrl && vss_exist)
587 vlib_node_increment_counter
588 (vm, dhcp_proxy_to_client_node.index,
589 DHCP_PROXY_ERROR_OPTION_82_VSS_NOT_PROCESSED, 1);
592 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
596 if (sw_if_index == (u32)~0)
598 error0 = DHCP_PROXY_ERROR_NO_OPTION_82;
601 vlib_node_increment_counter (vm, dhcp_proxy_to_client_node.index,
603 f0 = vlib_get_frame_to_node (vm, dpm->error_drop_node_index);
604 to_next0 = vlib_frame_vector_args (f0);
607 vlib_put_frame_to_node (vm, dpm->error_drop_node_index, f0);
611 if (relay_addr.as_u32 == 0)
613 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82_ADDR;
617 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
619 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82_ITF;
623 fib_index = im->fib_index_by_sw_if_index [sw_if_index];
624 proxy = dhcp_get_proxy(dpm, fib_index, FIB_PROTOCOL_IP4);
626 if (PREDICT_FALSE (NULL == proxy))
628 error0 = DHCP_PROXY_ERROR_NO_SERVER;
632 vec_foreach(server, proxy->dhcp_servers)
634 if (ip0->src_address.as_u32 == server->dhcp_server.ip4.as_u32)
640 error0 = DHCP_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
644 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
646 swif = vnet_get_sw_interface (vnm, sw_if_index);
647 original_sw_if_index = sw_if_index;
648 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
649 sw_if_index = swif->unnumbered_sw_if_index;
651 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
654 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
658 if (relay_addr.as_u32 != ia0->as_u32)
660 error0 = DHCP_PROXY_ERROR_BAD_YIADDR;
665 u0->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client);
666 sum0 = ip0->checksum;
667 old0 = ip0->dst_address.as_u32;
669 ip0->dst_address.as_u32 = new0;
670 sum0 = ip_csum_update (sum0, old0, new0,
671 ip4_header_t /* structure */,
672 dst_address /* offset of changed member */);
673 ip0->checksum = ip_csum_fold (sum0);
675 sum0 = ip0->checksum;
676 old0 = ip0->src_address.as_u32;
678 ip0->src_address.as_u32 = new0;
679 sum0 = ip_csum_update (sum0, old0, new0,
680 ip4_header_t /* structure */,
681 src_address /* offset of changed member */);
682 ip0->checksum = ip_csum_fold (sum0);
684 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
685 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
686 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
687 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
689 mac0 = vlib_buffer_get_current (b0);
691 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
692 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
693 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
694 memset (mac0->dst_address, 0xff, sizeof (mac0->dst_address));
695 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
696 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x0800);
698 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
700 u32 * vlan_tag = (u32 *)(mac0+1);
702 tmp = (si0->sub.id << 16) | 0x0800;
703 *vlan_tag = clib_host_to_net_u32 (tmp);
706 /* $$$ This needs to be rewritten, for sure */
707 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
708 to_next0 = vlib_frame_vector_args (f0);
711 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
714 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
716 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
718 tr->which = 1; /* to client */
719 tr->trace_ip4_address.as_u32 = ia0 ? ia0->as_u32 : 0;
721 tr->original_sw_if_index = original_sw_if_index;
722 tr->sw_if_index = sw_if_index;
725 return from_frame->n_vectors;
728 VLIB_REGISTER_NODE (dhcp_proxy_to_client_node, static) = {
729 .function = dhcp_proxy_to_client_input,
730 .name = "dhcp-proxy-to-client",
731 /* Takes a vector of packets. */
732 .vector_size = sizeof (u32),
734 .n_errors = DHCP_PROXY_N_ERROR,
735 .error_strings = dhcp_proxy_error_strings,
736 .format_buffer = format_dhcp_proxy_header_with_length,
737 .format_trace = format_dhcp_proxy_trace,
739 .unformat_buffer = unformat_dhcp_proxy_header,
743 static clib_error_t *
744 dhcp4_proxy_init (vlib_main_t * vm)
746 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
747 vlib_node_t * error_drop_node;
749 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
750 dm->error_drop_node_index = error_drop_node->index;
752 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
753 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */);
755 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
756 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */);
762 VLIB_INIT_FUNCTION (dhcp4_proxy_init);
765 dhcp4_proxy_set_server (ip46_address_t *addr,
766 ip46_address_t *src_addr,
771 u32 rx_fib_index = 0;
774 const fib_prefix_t all_1s =
777 .fp_addr.ip4.as_u32 = 0xffffffff,
778 .fp_proto = FIB_PROTOCOL_IP4,
781 if (ip46_address_is_zero(addr))
782 return VNET_API_ERROR_INVALID_DST_ADDRESS;
784 if (ip46_address_is_zero(src_addr))
785 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
787 rx_fib_index = fib_table_find_or_create_and_lock(FIB_PROTOCOL_IP4,
793 if (dhcp_proxy_server_del (FIB_PROTOCOL_IP4, rx_fib_index,
794 addr, server_table_id))
796 fib_table_entry_special_remove(rx_fib_index,
799 fib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
804 if (dhcp_proxy_server_add (FIB_PROTOCOL_IP4,
806 rx_fib_index, server_table_id))
808 fib_table_entry_special_add(rx_fib_index,
811 FIB_ENTRY_FLAG_LOCAL);
812 fib_table_lock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
815 fib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
820 static clib_error_t *
821 dhcp4_proxy_set_command_fn (vlib_main_t * vm,
822 unformat_input_t * input,
823 vlib_cli_command_t * cmd)
825 ip46_address_t server_addr, src_addr;
826 u32 server_table_id = 0, rx_table_id = 0;
828 int set_src = 0, set_server = 0;
830 memset(&server_addr, 0, sizeof(server_addr));
831 memset(&src_addr, 0, sizeof(src_addr));
833 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
835 if (unformat (input, "server %U",
836 unformat_ip4_address, &server_addr.ip4))
838 else if (unformat (input, "server-fib-id %d", &server_table_id))
840 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
842 else if (unformat(input, "src-address %U",
843 unformat_ip4_address, &src_addr.ip4))
845 else if (unformat (input, "delete") ||
846 unformat (input, "del"))
852 if (is_del || (set_server && set_src))
856 rv = dhcp4_proxy_set_server (&server_addr, &src_addr, rx_table_id,
857 server_table_id, is_del);
863 case VNET_API_ERROR_INVALID_DST_ADDRESS:
864 return clib_error_return (0, "Invalid server address");
866 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
867 return clib_error_return (0, "Invalid src address");
869 case VNET_API_ERROR_NO_SUCH_ENTRY:
870 return clib_error_return
871 (0, "Fib id %d: no per-fib DHCP server configured", rx_table_id);
874 return clib_error_return (0, "BUG: rv %d", rv);
878 return clib_error_return (0, "parse error`%U'",
879 format_unformat_error, input);
882 VLIB_CLI_COMMAND (dhcp_proxy_set_command, static) = {
883 .path = "set dhcp proxy",
884 .short_help = "set dhcp proxy [del] server <ip-addr> src-address <ip-addr> [server-fib-id <n>] [rx-fib-id <n>]",
885 .function = dhcp4_proxy_set_command_fn,
889 format_dhcp4_proxy_server (u8 * s, va_list * args)
891 dhcp_proxy_t *proxy = va_arg (*args, dhcp_proxy_t *);
892 ip4_fib_t * rx_fib, * server_fib;
893 dhcp_server_t *server;
897 s = format (s, "%=14s%=16s%s", "RX FIB", "Src Address",
898 "Servers FIB,Address");
902 rx_fib = ip4_fib_get(proxy->rx_fib_index);
904 s = format (s, "%=14u%=16U",
906 format_ip46_address, &proxy->dhcp_src_address, IP46_TYPE_ANY);
908 vec_foreach(server, proxy->dhcp_servers)
910 server_fib = ip4_fib_get(server->server_fib_index);
911 s = format (s, "%u,%U ",
912 server_fib->table_id,
913 format_ip46_address, &server->dhcp_server, IP46_TYPE_ANY);
919 dhcp4_proxy_show_walk (dhcp_proxy_t *server,
922 vlib_main_t * vm = ctx;
924 vlib_cli_output (vm, "%U", format_dhcp4_proxy_server, server);
929 static clib_error_t *
930 dhcp4_proxy_show_command_fn (vlib_main_t * vm,
931 unformat_input_t * input,
932 vlib_cli_command_t * cmd)
934 vlib_cli_output (vm, "%U", format_dhcp4_proxy_server, NULL /* header line */);
936 dhcp_proxy_walk(FIB_PROTOCOL_IP4, dhcp4_proxy_show_walk, vm);
941 VLIB_CLI_COMMAND (dhcp_proxy_show_command, static) = {
942 .path = "show dhcp proxy",
943 .short_help = "Display dhcp proxy server info",
944 .function = dhcp4_proxy_show_command_fn,
947 static clib_error_t *
948 dhcp_option_82_vss_fn (vlib_main_t * vm,
949 unformat_input_t * input,
950 vlib_cli_command_t * cmd)
952 int is_del = 0, got_new_vpn_id=0;
953 u32 oui=0, fib_id=0, tbl_id=~0;
955 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
958 if (unformat(input, "delete") || unformat(input, "del"))
960 else if (unformat (input, "oui %d", &oui))
962 else if (unformat (input, "vpn-id %d", &fib_id))
964 else if (unformat (input, "table %d", &tbl_id))
970 return clib_error_return (0, "no table ID specified.");
972 if (is_del || got_new_vpn_id)
975 rv = dhcp_proxy_set_vss(FIB_PROTOCOL_IP4, tbl_id, oui, fib_id, is_del);
981 case VNET_API_ERROR_NO_SUCH_FIB:
982 return clib_error_return (0, "option 82 vss(oui:%d, vpn-id:%d) not found in table %d",
983 oui, fib_id, tbl_id);
985 case VNET_API_ERROR_NO_SUCH_ENTRY:
986 return clib_error_return (0, "option 82 vss for table %d not found in in pool.",
989 return clib_error_return (0, "BUG: rv %d", rv);
993 return clib_error_return (0, "parse error`%U'",
994 format_unformat_error, input);
997 VLIB_CLI_COMMAND (dhcp_proxy_vss_command,static) = {
998 .path = "set dhcp option-82 vss",
999 .short_help = "set dhcp option-82 vss [del] table <table id> oui <oui> vpn-id <vpn-id>",
1000 .function = dhcp_option_82_vss_fn,
1003 static clib_error_t *
1004 dhcp_vss_show_command_fn (vlib_main_t * vm,
1005 unformat_input_t * input,
1006 vlib_cli_command_t * cmd)
1009 dhcp_vss_walk(FIB_PROTOCOL_IP4, dhcp_vss_show_walk, vm);
1014 VLIB_CLI_COMMAND (dhcp_proxy_vss_show_command, static) = {
1015 .path = "show dhcp vss",
1016 .short_help = "show dhcp VSS",
1017 .function = dhcp_vss_show_command_fn,
1020 static clib_error_t *
1021 dhcp_option_82_address_show_command_fn (vlib_main_t * vm,
1022 unformat_input_t * input,
1023 vlib_cli_command_t * cmd)
1026 vnet_main_t *vnm = vnet_get_main();
1027 u32 sw_if_index0=0, sw_if_index;
1028 vnet_sw_interface_t *swif;
1031 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1034 if (unformat(input, "%U",
1035 unformat_vnet_sw_interface, vnm, &sw_if_index0))
1037 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1038 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1039 swif->unnumbered_sw_if_index : sw_if_index0;
1040 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0);
1043 vlib_cli_output (vm, "%=20s%=20s", "interface",
1044 "source IP address");
1046 vlib_cli_output (vm, "%=20U%=20U",
1047 format_vnet_sw_if_index_name,
1049 format_ip4_address, ia0);
1052 vlib_cli_output (vm, "%=34s %=20U",
1053 "No IPv4 address configured on",
1054 format_vnet_sw_if_index_name,
1064 VLIB_CLI_COMMAND (dhcp_proxy_address_show_command,static) = {
1065 .path = "show dhcp option-82-address interface",
1066 .short_help = "show dhcp option-82-address interface <interface>",
1067 .function = dhcp_option_82_address_show_command_fn,