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 <dhcp/dhcp_proxy.h>
21 #include <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 <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")
37 #define _(s,n) DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s,
38 foreach_dhcp_proxy_to_server_input_next
40 DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
41 } dhcp_proxy_to_server_input_next_t;
45 /* 0 => to server, 1 => to client */
47 ip4_address_t trace_ip4_address;
50 u32 original_sw_if_index;
52 /* enough space for the DHCP header plus some options */
53 u8 packet_data[2 * sizeof (dhcp_header_t)];
57 #define VPP_DHCP_OPTION82_SUB1_SIZE 6
58 #define VPP_DHCP_OPTION82_SUB5_SIZE 6
59 #define VPP_DHCP_OPTION82_VSS_SIZE 12
60 #define VPP_DHCP_OPTION82_SIZE (VPP_DHCP_OPTION82_SUB1_SIZE + \
61 VPP_DHCP_OPTION82_SUB5_SIZE + \
62 VPP_DHCP_OPTION82_VSS_SIZE +3)
64 static vlib_node_registration_t dhcp_proxy_to_server_node;
65 static vlib_node_registration_t dhcp_proxy_to_client_node;
68 format_dhcp_proxy_trace (u8 * s, va_list * args)
70 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
72 dhcp_proxy_trace_t *t = va_arg (*args, dhcp_proxy_trace_t *);
75 s = format (s, "DHCP proxy: sent to server %U\n",
76 format_ip4_address, &t->trace_ip4_address, t->error);
78 s = format (s, "DHCP proxy: broadcast to client from %U\n",
79 format_ip4_address, &t->trace_ip4_address);
81 if (t->error != (u32) ~ 0)
82 s = format (s, " error: %s\n", dhcp_proxy_error_strings[t->error]);
84 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
85 t->original_sw_if_index, t->sw_if_index);
87 format_dhcp_header, t->packet_data, sizeof (t->packet_data));
93 format_dhcp_proxy_header_with_length (u8 * s, va_list * args)
95 dhcp_header_t *h = va_arg (*args, dhcp_header_t *);
96 u32 max_header_bytes = va_arg (*args, u32);
99 header_bytes = sizeof (h[0]);
100 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
101 return format (s, "dhcp header truncated");
103 s = format (s, "DHCP Proxy");
109 dhcp_proxy_to_server_input (vlib_main_t * vm,
110 vlib_node_runtime_t * node,
111 vlib_frame_t * from_frame)
113 u32 n_left_from, next_index, *from, *to_next;
114 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
115 from = vlib_frame_vector_args (from_frame);
116 n_left_from = from_frame->n_vectors;
117 u32 pkts_to_server = 0, pkts_to_client = 0, pkts_no_server = 0;
118 u32 pkts_no_interface_address = 0;
119 u32 pkts_too_big = 0;
120 ip4_main_t *im = &ip4_main;
122 next_index = node->cached_next_index;
124 while (n_left_from > 0)
128 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
130 while (n_left_from > 0 && n_left_to_next > 0)
140 u32 error0 = (u32) ~ 0;
142 u32 original_sw_if_index = 0;
145 dhcp_server_t *server;
147 dhcp_option_t *o, *end;
156 b0 = vlib_get_buffer (vm, bi0);
158 h0 = vlib_buffer_get_current (b0);
161 * udp_local hands us the DHCP header, need udp hdr,
162 * ip hdr to relay to server
164 vlib_buffer_advance (b0, -(sizeof (*u0)));
165 u0 = vlib_buffer_get_current (b0);
167 /* This blows. Return traffic has src_port = 67, dst_port = 67 */
169 clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_server))
171 vlib_buffer_advance (b0, sizeof (*u0));
172 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
178 rx_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
179 fib_index = im->fib_index_by_sw_if_index[rx_sw_if_index];
180 proxy = dhcp_get_proxy (dpm, fib_index, FIB_PROTOCOL_IP4);
182 if (PREDICT_FALSE (NULL == proxy))
184 error0 = DHCP_PROXY_ERROR_NO_SERVER;
185 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
190 if (!vlib_buffer_chain_linearize (vm, b0))
192 error0 = DHCP_PROXY_ERROR_PKT_TOO_BIG;
193 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
197 space_left = vlib_buffer_space_left_at_end (vm, b0);
198 /* cant parse chains...
199 * and we need some space for option 82*/
200 if ((b0->flags & VLIB_BUFFER_NEXT_PRESENT) != 0 ||
201 space_left < VPP_DHCP_OPTION82_SIZE)
203 error0 = DHCP_PROXY_ERROR_PKT_TOO_BIG;
204 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
209 server = &proxy->dhcp_servers[0];
210 vlib_buffer_advance (b0, -(sizeof (*ip0)));
211 ip0 = vlib_buffer_get_current (b0);
213 /* disable UDP checksum */
215 sum0 = ip0->checksum;
216 old0 = ip0->dst_address.as_u32;
217 new0 = server->dhcp_server.ip4.as_u32;
218 ip0->dst_address.as_u32 = server->dhcp_server.ip4.as_u32;
219 sum0 = ip_csum_update (sum0, old0, new0,
220 ip4_header_t /* structure */ ,
221 dst_address /* changed member */ );
222 ip0->checksum = ip_csum_fold (sum0);
224 sum0 = ip0->checksum;
225 old0 = ip0->src_address.as_u32;
226 new0 = proxy->dhcp_src_address.ip4.as_u32;
227 ip0->src_address.as_u32 = new0;
228 sum0 = ip_csum_update (sum0, old0, new0,
229 ip4_header_t /* structure */ ,
230 src_address /* changed member */ );
231 ip0->checksum = ip_csum_fold (sum0);
233 /* Send to DHCP server via the configured FIB */
234 vnet_buffer (b0)->sw_if_index[VLIB_TX] = server->server_fib_index;
236 h0->gateway_ip_address = proxy->dhcp_src_address.ip4;
240 end = (void *) vlib_buffer_get_tail (b0);
242 /* TLVs are not performance-friendly... */
243 while (o->option != DHCP_PACKET_OPTION_END && o < end)
245 if (DHCP_PACKET_OPTION_MSG_TYPE == o->option)
247 if (DHCP_PACKET_DISCOVER == o->data[0])
252 o = (dhcp_option_t *) (o->data + o->length);
255 if (o->option == DHCP_PACKET_OPTION_END && o <= end)
257 vnet_main_t *vnm = vnet_get_main ();
259 ip4_address_t _ia0, *ia0 = &_ia0;
261 vnet_sw_interface_t *swif;
263 original_sw_if_index = sw_if_index =
264 vnet_buffer (b0)->sw_if_index[VLIB_RX];
265 swif = vnet_get_sw_interface (vnm, sw_if_index);
266 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
267 sw_if_index = swif->unnumbered_sw_if_index;
270 * Get the first ip4 address on the [client-side]
271 * RX interface, if not unnumbered. otherwise use
272 * the loopback interface's ip address.
274 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
278 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
279 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
280 pkts_no_interface_address++;
285 o->option = 82; /* option 82 */
286 o->length = 12; /* 12 octets to follow */
287 o->data[0] = 1; /* suboption 1, circuit ID (=FIB id) */
288 o->data[1] = 4; /* length of suboption */
289 u32 *o_ifid = (u32 *) & o->data[2];
290 *o_ifid = clib_host_to_net_u32 (original_sw_if_index);
291 o->data[6] = 5; /* suboption 5 (client RX intfc address) */
292 o->data[7] = 4; /* length 4 */
293 u32 *o_addr = (u32 *) & o->data[8];
294 *o_addr = ia0->as_u32;
295 o->data[12] = DHCP_PACKET_OPTION_END;
297 vss = dhcp_get_vss_info (dpm, fib_index, FIB_PROTOCOL_IP4);
300 u32 id_len; /* length of VPN ID */
302 if (vss->vss_type == VSS_TYPE_VPN_ID)
304 id_len = sizeof (vss->vpn_id); /* vpn_id is 7 bytes */
305 memcpy (&o->data[15], vss->vpn_id, id_len);
307 else if (vss->vss_type == VSS_TYPE_ASCII)
309 id_len = vec_len (vss->vpn_ascii_id);
310 memcpy (&o->data[15], vss->vpn_ascii_id, id_len);
312 else /* must be VSS_TYPE_DEFAULT, no VPN ID */
315 o->data[12] = 151; /* vss suboption */
316 o->data[13] = id_len + 1; /* length: vss_type + id_len */
317 o->data[14] = vss->vss_type; /* vss option type */
318 o->data[15 + id_len] = 152; /* vss control suboption */
319 o->data[16 + id_len] = 0; /* length */
320 o->data[17 + id_len] = DHCP_PACKET_OPTION_END; /* "end-of-options" (0xFF) */
321 /* 5 bytes for suboption headers 151+len, 152+len and 0xFF */
322 o->length += id_len + 5;
326 b0->current_length += len;
327 /* Fix IP header length and checksum */
328 old_l0 = ip0->length;
329 new_l0 = clib_net_to_host_u16 (old_l0);
331 new_l0 = clib_host_to_net_u16 (new_l0);
332 ip0->length = new_l0;
333 sum0 = ip0->checksum;
334 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
335 length /* changed member */ );
336 ip0->checksum = ip_csum_fold (sum0);
339 new_l0 = clib_net_to_host_u16 (u0->length);
341 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 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (c0);
368 ci0 = vlib_get_buffer_index (vm, c0);
369 server = &proxy->dhcp_servers[ii];
371 ip0 = vlib_buffer_get_current (c0);
373 sum0 = ip0->checksum;
374 old0 = ip0->dst_address.as_u32;
375 new0 = server->dhcp_server.ip4.as_u32;
376 ip0->dst_address.as_u32 = server->dhcp_server.ip4.as_u32;
377 sum0 = ip_csum_update (sum0, old0, new0,
378 ip4_header_t /* structure */ ,
379 dst_address /* changed member */ );
380 ip0->checksum = ip_csum_fold (sum0);
386 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
387 to_next, n_left_to_next,
390 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
392 dhcp_proxy_trace_t *tr;
394 tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
395 tr->which = 0; /* to server */
397 tr->original_sw_if_index = original_sw_if_index;
398 tr->sw_if_index = sw_if_index;
399 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
400 tr->trace_ip4_address.as_u32 =
401 server->dhcp_server.ip4.as_u32;
403 clib_memcpy_fast (tr->packet_data, h0,
404 sizeof (tr->packet_data));
408 if (PREDICT_FALSE (0 == n_left_to_next))
410 vlib_put_next_frame (vm, node, next_index,
412 vlib_get_next_frame (vm, node, next_index,
413 to_next, n_left_to_next);
418 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
420 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
422 tr->which = 0; /* to server */
424 tr->original_sw_if_index = original_sw_if_index;
425 tr->sw_if_index = sw_if_index;
426 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
427 tr->trace_ip4_address.as_u32 =
428 proxy->dhcp_servers[0].dhcp_server.ip4.as_u32;
429 clib_memcpy_fast (tr->packet_data, h0,
430 sizeof (tr->packet_data));
438 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
439 to_next, n_left_to_next,
443 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
445 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
446 DHCP_PROXY_ERROR_RELAY_TO_CLIENT,
448 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
449 DHCP_PROXY_ERROR_RELAY_TO_SERVER,
451 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
452 DHCP_PROXY_ERROR_NO_SERVER, pkts_no_server);
453 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
454 DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS,
455 pkts_no_interface_address);
456 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
457 DHCP_PROXY_ERROR_PKT_TOO_BIG, pkts_too_big);
458 return from_frame->n_vectors;
462 VLIB_REGISTER_NODE (dhcp_proxy_to_server_node, static) = {
463 .function = dhcp_proxy_to_server_input,
464 .name = "dhcp-proxy-to-server",
465 /* Takes a vector of packets. */
466 .vector_size = sizeof (u32),
468 .n_errors = DHCP_PROXY_N_ERROR,
469 .error_strings = dhcp_proxy_error_strings,
471 .n_next_nodes = DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
473 #define _(s,n) [DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
474 foreach_dhcp_proxy_to_server_input_next
478 .format_buffer = format_dhcp_proxy_header_with_length,
479 .format_trace = format_dhcp_proxy_trace,
481 .unformat_buffer = unformat_dhcp_proxy_header,
488 DHCP4_PROXY_NEXT_DROP,
494 dhcp_proxy_to_client_input (vlib_main_t * vm,
495 vlib_node_runtime_t * node,
496 vlib_frame_t * from_frame)
498 u32 n_left_from, *from, *to_next, n_left_to_next;
499 ethernet_main_t *em = vnet_get_ethernet_main ();
500 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
501 vnet_main_t *vnm = vnet_get_main ();
502 ip4_main_t *im = &ip4_main;
505 from = vlib_frame_vector_args (from_frame);
506 n_left_from = from_frame->n_vectors;
507 next_index = node->cached_next_index;
509 while (n_left_from > 0)
511 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
513 while (n_left_from > 0 && n_left_to_next > 0)
519 ip4_header_t *ip0 = 0;
520 ip4_address_t *ia0 = 0;
523 ethernet_interface_t *ei0;
524 ethernet_header_t *mac0;
525 vnet_hw_interface_t *hi0;
526 u32 sw_if_index = ~0;
527 vnet_sw_interface_t *si0;
528 u32 error0 = (u32) ~ 0;
529 vnet_sw_interface_t *swif;
532 dhcp_server_t *server;
533 u32 original_sw_if_index = (u32) ~ 0;
534 dhcp4_next_t next0 = DHCP4_PROXY_NEXT_TX;
535 ip4_address_t relay_addr = {
539 bi0 = to_next[0] = from[0];
545 b0 = vlib_get_buffer (vm, bi0);
546 h0 = vlib_buffer_get_current (b0);
549 * udp_local hands us the DHCP header, need udp hdr,
550 * ip hdr to relay to client
552 vlib_buffer_advance (b0, -(sizeof (*u0)));
553 u0 = vlib_buffer_get_current (b0);
555 vlib_buffer_advance (b0, -(sizeof (*ip0)));
556 ip0 = vlib_buffer_get_current (b0);
558 /* Consumed by dhcp client code? */
559 if (dhcp_client_for_us (bi0, b0, ip0, u0, h0))
561 error0 = DHCP_PROXY_ERROR_FOR_US;
565 // if (1 /* dpm->insert_option_82 */ )
566 /* linearize needed to "unclone" and scan options */
567 int rv = vlib_buffer_chain_linearize (vm, b0);
568 if ((b0->flags & VLIB_BUFFER_NEXT_PRESENT) != 0 || !rv)
570 error0 = DHCP_PROXY_ERROR_PKT_TOO_BIG;
574 dhcp_option_t *o = h0->options, *end =
575 (void *) vlib_buffer_get_tail (b0);
577 /* Parse through TLVs looking for option 82.
578 The circuit-ID is the FIB number we need
579 to track down the client-facing interface */
581 while (o->option != DHCP_PACKET_OPTION_END && o < end)
587 dhcp_option_t *sub = (dhcp_option_t *) & o->data[0];
588 dhcp_option_t *subend =
589 (dhcp_option_t *) (o->data + o->length);
590 while (sub->option != DHCP_PACKET_OPTION_END
593 /* If this is one of ours, it will have
594 total length 12, circuit-id suboption type,
595 and the sw_if_index */
596 if (sub->option == 1 && sub->length == 4)
598 sw_if_index = ((sub->data[0] << 24) |
599 (sub->data[1] << 16) |
600 (sub->data[2] << 8) |
603 else if (sub->option == 5 && sub->length == 4)
605 relay_addr.as_u8[0] = sub->data[0];
606 relay_addr.as_u8[1] = sub->data[1];
607 relay_addr.as_u8[2] = sub->data[2];
608 relay_addr.as_u8[3] = sub->data[3];
610 else if (sub->option == 151 &&
611 sub->length == 7 && sub->data[0] == 1)
613 else if (sub->option == 152 && sub->length == 0)
615 sub = (dhcp_option_t *) (sub->data + sub->length);
617 if (vss_ctrl && vss_exist)
618 vlib_node_increment_counter
619 (vm, dhcp_proxy_to_client_node.index,
620 DHCP_PROXY_ERROR_OPTION_82_VSS_NOT_PROCESSED, 1);
623 o = (dhcp_option_t *) (o->data + o->length);
626 if (sw_if_index == (u32) ~ 0)
628 error0 = DHCP_PROXY_ERROR_NO_OPTION_82;
631 vlib_node_increment_counter (vm,
632 dhcp_proxy_to_client_node.index,
634 b0->error = node->errors[error0];
635 next0 = DHCP4_PROXY_NEXT_DROP;
639 if (relay_addr.as_u32 == 0)
641 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82_ADDR;
645 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
647 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82_ITF;
651 fib_index = im->fib_index_by_sw_if_index[sw_if_index];
652 proxy = dhcp_get_proxy (dpm, fib_index, FIB_PROTOCOL_IP4);
654 if (PREDICT_FALSE (NULL == proxy))
656 error0 = DHCP_PROXY_ERROR_NO_SERVER;
660 vec_foreach (server, proxy->dhcp_servers)
662 if (ip0->src_address.as_u32 == server->dhcp_server.ip4.as_u32)
668 error0 = DHCP_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
672 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
674 swif = vnet_get_sw_interface (vnm, sw_if_index);
675 original_sw_if_index = sw_if_index;
676 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
677 sw_if_index = swif->unnumbered_sw_if_index;
679 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
682 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
686 if (relay_addr.as_u32 != ia0->as_u32)
688 error0 = DHCP_PROXY_ERROR_BAD_YIADDR;
693 u0->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client);
694 sum0 = ip0->checksum;
695 old0 = ip0->dst_address.as_u32;
697 ip0->dst_address.as_u32 = new0;
699 ip_csum_update (sum0, old0, new0, ip4_header_t /* structure */ ,
700 dst_address /* offset of changed member */ );
701 ip0->checksum = ip_csum_fold (sum0);
703 sum0 = ip0->checksum;
704 old0 = ip0->src_address.as_u32;
706 ip0->src_address.as_u32 = new0;
708 ip_csum_update (sum0, old0, new0, ip4_header_t /* structure */ ,
709 src_address /* offset of changed member */ );
710 ip0->checksum = ip_csum_fold (sum0);
712 vlib_buffer_advance (b0, -(sizeof (ethernet_header_t)));
713 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
714 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
715 vlib_buffer_advance (b0, -4 /* space for VLAN tag */ );
717 mac0 = vlib_buffer_get_current (b0);
719 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
720 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
721 clib_memcpy (mac0->src_address, ei0->address,
722 sizeof (ei0->address));
723 clib_memset (mac0->dst_address, 0xff, sizeof (mac0->dst_address));
724 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
725 clib_net_to_host_u16 (0x8100) : clib_net_to_host_u16 (0x0800);
727 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
729 u32 *vlan_tag = (u32 *) (mac0 + 1);
731 tmp = (si0->sub.id << 16) | 0x0800;
732 *vlan_tag = clib_host_to_net_u32 (tmp);
736 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
738 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
740 tr->which = 1; /* to client */
741 tr->trace_ip4_address.as_u32 = ia0 ? ia0->as_u32 : 0;
743 tr->original_sw_if_index = original_sw_if_index;
744 tr->sw_if_index = sw_if_index;
745 clib_memcpy_fast (tr->packet_data, h0,
746 sizeof (tr->packet_data));
749 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
750 to_next, n_left_to_next,
753 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
756 return from_frame->n_vectors;
760 VLIB_REGISTER_NODE (dhcp_proxy_to_client_node, static) = {
761 .function = dhcp_proxy_to_client_input,
762 .name = "dhcp-proxy-to-client",
763 /* Takes a vector of packets. */
764 .vector_size = sizeof (u32),
766 .n_errors = DHCP_PROXY_N_ERROR,
767 .error_strings = dhcp_proxy_error_strings,
768 .format_buffer = format_dhcp_proxy_header_with_length,
769 .format_trace = format_dhcp_proxy_trace,
771 .unformat_buffer = unformat_dhcp_proxy_header,
773 .n_next_nodes = DHCP4_PROXY_N_NEXT,
775 [DHCP4_PROXY_NEXT_DROP] = "error-drop",
776 [DHCP4_PROXY_NEXT_TX] = "interface-output",
782 dhcp_maybe_register_udp_ports (dhcp_port_reg_flags_t ports)
784 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
785 vlib_main_t *vm = dm->vlib_main;
786 int port_regs_diff = dm->udp_ports_registered ^ ports;
791 if ((port_regs_diff & DHCP_PORT_REG_CLIENT) & ports)
792 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
793 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */ );
795 if ((port_regs_diff & DHCP_PORT_REG_SERVER) & ports)
796 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
797 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */ );
799 dm->udp_ports_registered |= ports;
802 static clib_error_t *
803 dhcp4_proxy_init (vlib_main_t * vm)
805 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
806 vlib_node_t *error_drop_node;
808 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
809 dm->error_drop_node_index = error_drop_node->index;
816 VLIB_INIT_FUNCTION (dhcp4_proxy_init);
819 dhcp4_proxy_set_server (ip46_address_t * addr,
820 ip46_address_t * src_addr,
821 u32 rx_table_id, u32 server_table_id, int is_del)
823 u32 rx_fib_index = 0;
826 const fib_prefix_t all_1s = {
828 .fp_addr.ip4.as_u32 = 0xffffffff,
829 .fp_proto = FIB_PROTOCOL_IP4,
832 if (ip46_address_is_zero (addr))
833 return VNET_API_ERROR_INVALID_DST_ADDRESS;
835 if (ip46_address_is_zero (src_addr))
836 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
838 dhcp_maybe_register_udp_ports (DHCP_PORT_REG_CLIENT | DHCP_PORT_REG_SERVER);
840 rx_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
846 if (dhcp_proxy_server_del (FIB_PROTOCOL_IP4, rx_fib_index,
847 addr, server_table_id))
849 fib_table_entry_special_remove (rx_fib_index,
850 &all_1s, FIB_SOURCE_DHCP);
851 fib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
856 if (dhcp_proxy_server_add (FIB_PROTOCOL_IP4,
858 rx_fib_index, server_table_id))
860 fib_table_entry_special_add (rx_fib_index,
862 FIB_SOURCE_DHCP, FIB_ENTRY_FLAG_LOCAL);
863 fib_table_lock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
866 fib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
871 static clib_error_t *
872 dhcp4_proxy_set_command_fn (vlib_main_t * vm,
873 unformat_input_t * input,
874 vlib_cli_command_t * cmd)
876 ip46_address_t server_addr, src_addr;
877 u32 server_table_id = 0, rx_table_id = 0;
879 int set_src = 0, set_server = 0;
881 clib_memset (&server_addr, 0, sizeof (server_addr));
882 clib_memset (&src_addr, 0, sizeof (src_addr));
884 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
886 if (unformat (input, "server %U",
887 unformat_ip4_address, &server_addr.ip4))
889 else if (unformat (input, "server-fib-id %d", &server_table_id))
891 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
893 else if (unformat (input, "src-address %U",
894 unformat_ip4_address, &src_addr.ip4))
896 else if (unformat (input, "delete") || unformat (input, "del"))
902 if (is_del || (set_server && set_src))
906 rv = dhcp4_proxy_set_server (&server_addr, &src_addr, rx_table_id,
907 server_table_id, is_del);
913 case VNET_API_ERROR_INVALID_DST_ADDRESS:
914 return clib_error_return (0, "Invalid server address");
916 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
917 return clib_error_return (0, "Invalid src address");
919 case VNET_API_ERROR_NO_SUCH_ENTRY:
920 return clib_error_return
921 (0, "Fib id %d: no per-fib DHCP server configured", rx_table_id);
924 return clib_error_return (0, "BUG: rv %d", rv);
928 return clib_error_return (0, "parse error`%U'",
929 format_unformat_error, input);
933 VLIB_CLI_COMMAND (dhcp_proxy_set_command, static) = {
934 .path = "set dhcp proxy",
935 .short_help = "set dhcp proxy [del] server <ip-addr> src-address <ip-addr> [server-fib-id <n>] [rx-fib-id <n>]",
936 .function = dhcp4_proxy_set_command_fn,
941 format_dhcp4_proxy_server (u8 * s, va_list * args)
943 dhcp_proxy_t *proxy = va_arg (*args, dhcp_proxy_t *);
944 ip4_fib_t *rx_fib, *server_fib;
945 dhcp_server_t *server;
949 s = format (s, "%=14s%=16s%s", "RX FIB", "Src Address",
950 "Servers FIB,Address");
954 rx_fib = ip4_fib_get (proxy->rx_fib_index);
956 s = format (s, "%=14u%=16U",
958 format_ip46_address, &proxy->dhcp_src_address, IP46_TYPE_ANY);
960 vec_foreach (server, proxy->dhcp_servers)
962 server_fib = ip4_fib_get (server->server_fib_index);
963 s = format (s, "%u,%U ",
964 server_fib->table_id,
965 format_ip46_address, &server->dhcp_server, IP46_TYPE_ANY);
971 dhcp4_proxy_show_walk (dhcp_proxy_t * server, void *ctx)
973 vlib_main_t *vm = ctx;
975 vlib_cli_output (vm, "%U", format_dhcp4_proxy_server, server);
980 static clib_error_t *
981 dhcp4_proxy_show_command_fn (vlib_main_t * vm,
982 unformat_input_t * input,
983 vlib_cli_command_t * cmd)
985 vlib_cli_output (vm, "%U", format_dhcp4_proxy_server,
986 NULL /* header line */ );
988 dhcp_proxy_walk (FIB_PROTOCOL_IP4, dhcp4_proxy_show_walk, vm);
994 VLIB_CLI_COMMAND (dhcp_proxy_show_command, static) = {
995 .path = "show dhcp proxy",
996 .short_help = "Display dhcp proxy server info",
997 .function = dhcp4_proxy_show_command_fn,
1001 static clib_error_t *
1002 dhcp_option_82_vss_fn (vlib_main_t * vm,
1003 unformat_input_t * input, vlib_cli_command_t * cmd)
1005 u8 is_del = 0, vss_type = VSS_TYPE_DEFAULT;
1006 u32 oui = 0, fib_id = 0, tbl_id = ~0;
1007 u8 *vpn_ascii_id = 0;
1009 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1011 if (unformat (input, "table %d", &tbl_id))
1013 else if (unformat (input, "oui %d", &oui))
1014 vss_type = VSS_TYPE_VPN_ID;
1015 else if (unformat (input, "vpn-id %d", &fib_id))
1016 vss_type = VSS_TYPE_VPN_ID;
1017 else if (unformat (input, "vpn-ascii-id %s", &vpn_ascii_id))
1018 vss_type = VSS_TYPE_ASCII;
1019 else if (unformat (input, "delete") || unformat (input, "del"))
1026 return clib_error_return (0, "no table ID specified.");
1028 int rv = dhcp_proxy_set_vss (FIB_PROTOCOL_IP4, tbl_id, vss_type,
1029 vpn_ascii_id, oui, fib_id, is_del);
1034 case VNET_API_ERROR_NO_SUCH_ENTRY:
1035 return clib_error_return (0,
1036 "option 82 vss for table %d not found in in pool.",
1039 return clib_error_return (0, "BUG: rv %d", rv);
1045 VLIB_CLI_COMMAND (dhcp_proxy_vss_command,static) = {
1046 .path = "set dhcp option-82 vss",
1047 .short_help = "set dhcp option-82 vss [del] table <table id> [oui <n> vpn-id <n> | vpn-ascii-id <text>]",
1048 .function = dhcp_option_82_vss_fn,
1052 static clib_error_t *
1053 dhcp_vss_show_command_fn (vlib_main_t * vm,
1054 unformat_input_t * input, vlib_cli_command_t * cmd)
1056 dhcp_vss_walk (FIB_PROTOCOL_IP4, dhcp_vss_show_walk, vm);
1062 VLIB_CLI_COMMAND (dhcp_proxy_vss_show_command, static) = {
1063 .path = "show dhcp vss",
1064 .short_help = "show dhcp VSS",
1065 .function = dhcp_vss_show_command_fn,
1069 static clib_error_t *
1070 dhcp_option_82_address_show_command_fn (vlib_main_t * vm,
1071 unformat_input_t * input,
1072 vlib_cli_command_t * cmd)
1074 vnet_main_t *vnm = vnet_get_main ();
1075 u32 sw_if_index0 = 0, sw_if_index;
1076 vnet_sw_interface_t *swif;
1079 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1082 if (unformat (input, "%U",
1083 unformat_vnet_sw_interface, vnm, &sw_if_index0))
1085 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1086 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1087 swif->unnumbered_sw_if_index : sw_if_index0;
1088 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
1091 vlib_cli_output (vm, "%=20s%=20s", "interface",
1092 "source IP address");
1094 vlib_cli_output (vm, "%=20U%=20U",
1095 format_vnet_sw_if_index_name,
1096 vnm, sw_if_index0, format_ip4_address, ia0);
1099 vlib_cli_output (vm, "%=34s %=20U",
1100 "No IPv4 address configured on",
1101 format_vnet_sw_if_index_name, vnm, sw_if_index);
1111 VLIB_CLI_COMMAND (dhcp_proxy_address_show_command,static) = {
1112 .path = "show dhcp option-82-address interface",
1113 .short_help = "show dhcp option-82-address interface <interface>",
1114 .function = dhcp_option_82_address_show_command_fn,
1119 * fd.io coding-style-patch-verification: ON
1122 * eval: (c-set-style "gnu")