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 <dhcp/dhcp_proxy.h>
20 #include <dhcp/client.h>
21 #include <vnet/fib/ip4_fib.h>
23 static char *dhcp_proxy_error_strings[] = {
24 #define dhcp_proxy_error(n,s) s,
25 #include <dhcp/dhcp4_proxy_error.def>
26 #undef dhcp_proxy_error
29 #define foreach_dhcp_proxy_to_server_input_next \
30 _ (DROP, "error-drop") \
31 _ (LOOKUP, "ip4-lookup") \
32 _ (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;
44 /* 0 => to server, 1 => to client */
46 ip4_address_t trace_ip4_address;
49 u32 original_sw_if_index;
51 /* enough space for the DHCP header plus some options */
52 u8 packet_data[2 * sizeof (dhcp_header_t)];
56 #define VPP_DHCP_OPTION82_SUB1_SIZE 6
57 #define VPP_DHCP_OPTION82_SUB5_SIZE 6
58 #define VPP_DHCP_OPTION82_VSS_SIZE 12
59 #define VPP_DHCP_OPTION82_SIZE (VPP_DHCP_OPTION82_SUB1_SIZE + \
60 VPP_DHCP_OPTION82_SUB5_SIZE + \
61 VPP_DHCP_OPTION82_VSS_SIZE +3)
63 static vlib_node_registration_t dhcp_proxy_to_server_node;
64 static vlib_node_registration_t dhcp_proxy_to_client_node;
67 format_dhcp_proxy_trace (u8 * s, va_list * args)
69 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71 dhcp_proxy_trace_t *t = va_arg (*args, dhcp_proxy_trace_t *);
74 s = format (s, "DHCP proxy: sent to server %U\n",
75 format_ip4_address, &t->trace_ip4_address, t->error);
77 s = format (s, "DHCP proxy: broadcast to client from %U\n",
78 format_ip4_address, &t->trace_ip4_address);
80 if (t->error != (u32) ~ 0)
81 s = format (s, " error: %s\n", dhcp_proxy_error_strings[t->error]);
83 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
84 t->original_sw_if_index, t->sw_if_index);
86 format_dhcp_header, t->packet_data, sizeof (t->packet_data));
92 format_dhcp_proxy_header_with_length (u8 * s, va_list * args)
94 dhcp_header_t *h = va_arg (*args, dhcp_header_t *);
95 u32 max_header_bytes = va_arg (*args, u32);
98 header_bytes = sizeof (h[0]);
99 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
100 return format (s, "dhcp header truncated");
102 s = format (s, "DHCP Proxy");
108 dhcp_proxy_to_server_input (vlib_main_t * vm,
109 vlib_node_runtime_t * node,
110 vlib_frame_t * from_frame)
112 u32 n_left_from, next_index, *from, *to_next;
113 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
114 from = vlib_frame_vector_args (from_frame);
115 n_left_from = from_frame->n_vectors;
116 u32 pkts_to_server = 0, pkts_to_client = 0, pkts_no_server = 0;
117 u32 pkts_no_interface_address = 0;
118 u32 pkts_too_big = 0;
119 ip4_main_t *im = &ip4_main;
121 next_index = node->cached_next_index;
123 while (n_left_from > 0)
127 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
129 while (n_left_from > 0 && n_left_to_next > 0)
139 u32 error0 = (u32) ~ 0;
141 u32 original_sw_if_index = 0;
144 dhcp_server_t *server;
146 dhcp_option_t *o, *end;
155 b0 = vlib_get_buffer (vm, bi0);
157 h0 = vlib_buffer_get_current (b0);
160 * udp_local hands us the DHCP header, need udp hdr,
161 * ip hdr to relay to server
163 vlib_buffer_advance (b0, -(sizeof (*u0)));
164 u0 = vlib_buffer_get_current (b0);
166 /* This blows. Return traffic has src_port = 67, dst_port = 67 */
168 clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_server))
170 vlib_buffer_advance (b0, sizeof (*u0));
171 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
177 rx_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
178 fib_index = im->fib_index_by_sw_if_index[rx_sw_if_index];
179 proxy = dhcp_get_proxy (dpm, fib_index, FIB_PROTOCOL_IP4);
181 if (PREDICT_FALSE (NULL == proxy))
183 error0 = DHCP_PROXY_ERROR_NO_SERVER;
184 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
189 if (!vlib_buffer_chain_linearize (vm, b0))
191 error0 = DHCP_PROXY_ERROR_PKT_TOO_BIG;
192 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
196 space_left = vlib_buffer_space_left_at_end (vm, b0);
197 /* cant parse chains...
198 * and we need some space for option 82*/
199 if ((b0->flags & VLIB_BUFFER_NEXT_PRESENT) != 0 ||
200 space_left < VPP_DHCP_OPTION82_SIZE)
202 error0 = DHCP_PROXY_ERROR_PKT_TOO_BIG;
203 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
208 server = &proxy->dhcp_servers[0];
209 vlib_buffer_advance (b0, -(sizeof (*ip0)));
210 ip0 = vlib_buffer_get_current (b0);
212 /* disable UDP checksum */
214 sum0 = ip0->checksum;
215 old0 = ip0->dst_address.as_u32;
216 new0 = server->dhcp_server.ip4.as_u32;
217 ip0->dst_address.as_u32 = server->dhcp_server.ip4.as_u32;
218 sum0 = ip_csum_update (sum0, old0, new0,
219 ip4_header_t /* structure */ ,
220 dst_address /* changed member */ );
221 ip0->checksum = ip_csum_fold (sum0);
223 sum0 = ip0->checksum;
224 old0 = ip0->src_address.as_u32;
225 new0 = proxy->dhcp_src_address.ip4.as_u32;
226 ip0->src_address.as_u32 = new0;
227 sum0 = ip_csum_update (sum0, old0, new0,
228 ip4_header_t /* structure */ ,
229 src_address /* changed member */ );
230 ip0->checksum = ip_csum_fold (sum0);
232 /* Send to DHCP server via the configured FIB */
233 vnet_buffer (b0)->sw_if_index[VLIB_TX] = server->server_fib_index;
235 h0->gateway_ip_address = proxy->dhcp_src_address.ip4;
239 end = (void *) vlib_buffer_get_tail (b0);
241 /* TLVs are not performance-friendly... */
242 while (o->option != DHCP_PACKET_OPTION_END && o < end)
244 if (DHCP_PACKET_OPTION_MSG_TYPE == o->option)
246 if (DHCP_PACKET_DISCOVER == o->data[0])
251 o = (dhcp_option_t *) (o->data + o->length);
254 if (o->option == DHCP_PACKET_OPTION_END && o <= end)
256 vnet_main_t *vnm = vnet_get_main ();
258 ip4_address_t _ia0, *ia0 = &_ia0;
260 vnet_sw_interface_t *swif;
262 original_sw_if_index = sw_if_index =
263 vnet_buffer (b0)->sw_if_index[VLIB_RX];
264 swif = vnet_get_sw_interface (vnm, sw_if_index);
265 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
266 sw_if_index = swif->unnumbered_sw_if_index;
269 * Get the first ip4 address on the [client-side]
270 * RX interface, if not unnumbered. otherwise use
271 * the loopback interface's ip address.
273 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
277 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
278 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
279 pkts_no_interface_address++;
284 o->option = 82; /* option 82 */
285 o->length = 12; /* 12 octets to follow */
286 o->data[0] = 1; /* suboption 1, circuit ID (=FIB id) */
287 o->data[1] = 4; /* length of suboption */
288 u32 *o_ifid = (u32 *) & o->data[2];
289 *o_ifid = clib_host_to_net_u32 (original_sw_if_index);
290 o->data[6] = 5; /* suboption 5 (client RX intfc address) */
291 o->data[7] = 4; /* length 4 */
292 u32 *o_addr = (u32 *) & o->data[8];
293 *o_addr = ia0->as_u32;
294 o->data[12] = DHCP_PACKET_OPTION_END;
296 vss = dhcp_get_vss_info (dpm, fib_index, FIB_PROTOCOL_IP4);
299 u32 id_len; /* length of VPN ID */
301 if (vss->vss_type == VSS_TYPE_VPN_ID)
303 id_len = sizeof (vss->vpn_id); /* vpn_id is 7 bytes */
304 memcpy (&o->data[15], vss->vpn_id, id_len);
306 else if (vss->vss_type == VSS_TYPE_ASCII)
308 id_len = vec_len (vss->vpn_ascii_id);
309 memcpy (&o->data[15], vss->vpn_ascii_id, id_len);
311 else /* must be VSS_TYPE_DEFAULT, no VPN ID */
314 o->data[12] = 151; /* vss suboption */
315 o->data[13] = id_len + 1; /* length: vss_type + id_len */
316 o->data[14] = vss->vss_type; /* vss option type */
317 o->data[15 + id_len] = 152; /* vss control suboption */
318 o->data[16 + id_len] = 0; /* length */
319 o->data[17 + id_len] = DHCP_PACKET_OPTION_END; /* "end-of-options" (0xFF) */
320 /* 5 bytes for suboption headers 151+len, 152+len and 0xFF */
321 o->length += id_len + 5;
325 b0->current_length += len;
326 /* Fix IP header length and checksum */
327 old_l0 = ip0->length;
328 new_l0 = clib_net_to_host_u16 (old_l0);
330 new_l0 = clib_host_to_net_u16 (new_l0);
331 ip0->length = new_l0;
332 sum0 = ip0->checksum;
333 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
334 length /* changed member */ );
335 ip0->checksum = ip_csum_fold (sum0);
338 new_l0 = clib_net_to_host_u16 (u0->length);
340 u0->length = clib_host_to_net_u16 (new_l0);
344 vlib_node_increment_counter
345 (vm, dhcp_proxy_to_server_node.index,
346 DHCP_PROXY_ERROR_OPTION_82_ERROR, 1);
349 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
352 * If we have multiple servers configured and this is the
353 * client's discover message, then send copies to each of
356 if (is_discover && vec_len (proxy->dhcp_servers) > 1)
360 for (ii = 1; ii < vec_len (proxy->dhcp_servers); ii++)
365 c0 = vlib_buffer_copy (vm, b0);
368 vlib_node_increment_counter
369 (vm, dhcp_proxy_to_server_node.index,
370 DHCP_PROXY_ERROR_ALLOC_FAIL, 1);
373 ci0 = vlib_get_buffer_index (vm, c0);
374 server = &proxy->dhcp_servers[ii];
376 ip0 = vlib_buffer_get_current (c0);
378 sum0 = ip0->checksum;
379 old0 = ip0->dst_address.as_u32;
380 new0 = server->dhcp_server.ip4.as_u32;
381 ip0->dst_address.as_u32 = server->dhcp_server.ip4.as_u32;
382 sum0 = ip_csum_update (sum0, old0, new0,
383 ip4_header_t /* structure */ ,
384 dst_address /* changed member */ );
385 ip0->checksum = ip_csum_fold (sum0);
391 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
392 to_next, n_left_to_next,
395 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
397 dhcp_proxy_trace_t *tr;
399 tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
400 tr->which = 0; /* to server */
402 tr->original_sw_if_index = original_sw_if_index;
403 tr->sw_if_index = sw_if_index;
404 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
405 tr->trace_ip4_address.as_u32 =
406 server->dhcp_server.ip4.as_u32;
408 clib_memcpy_fast (tr->packet_data, h0,
409 sizeof (tr->packet_data));
413 if (PREDICT_FALSE (0 == n_left_to_next))
415 vlib_put_next_frame (vm, node, next_index,
417 vlib_get_next_frame (vm, node, next_index,
418 to_next, n_left_to_next);
423 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
425 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
427 tr->which = 0; /* to server */
429 tr->original_sw_if_index = original_sw_if_index;
430 tr->sw_if_index = sw_if_index;
431 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
432 tr->trace_ip4_address.as_u32 =
433 proxy->dhcp_servers[0].dhcp_server.ip4.as_u32;
434 clib_memcpy_fast (tr->packet_data, h0,
435 sizeof (tr->packet_data));
443 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
444 to_next, n_left_to_next,
448 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
450 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
451 DHCP_PROXY_ERROR_RELAY_TO_CLIENT,
453 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
454 DHCP_PROXY_ERROR_RELAY_TO_SERVER,
456 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
457 DHCP_PROXY_ERROR_NO_SERVER, pkts_no_server);
458 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
459 DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS,
460 pkts_no_interface_address);
461 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
462 DHCP_PROXY_ERROR_PKT_TOO_BIG, pkts_too_big);
463 return from_frame->n_vectors;
467 VLIB_REGISTER_NODE (dhcp_proxy_to_server_node, static) = {
468 .function = dhcp_proxy_to_server_input,
469 .name = "dhcp-proxy-to-server",
470 /* Takes a vector of packets. */
471 .vector_size = sizeof (u32),
473 .n_errors = DHCP_PROXY_N_ERROR,
474 .error_strings = dhcp_proxy_error_strings,
476 .n_next_nodes = DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
478 #define _(s,n) [DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
479 foreach_dhcp_proxy_to_server_input_next
483 .format_buffer = format_dhcp_proxy_header_with_length,
484 .format_trace = format_dhcp_proxy_trace,
486 .unformat_buffer = unformat_dhcp_proxy_header,
493 DHCP4_PROXY_NEXT_DROP,
499 dhcp_proxy_to_client_input (vlib_main_t * vm,
500 vlib_node_runtime_t * node,
501 vlib_frame_t * from_frame)
503 u32 n_left_from, *from, *to_next, n_left_to_next;
504 ethernet_main_t *em = vnet_get_ethernet_main ();
505 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
506 vnet_main_t *vnm = vnet_get_main ();
507 ip4_main_t *im = &ip4_main;
510 from = vlib_frame_vector_args (from_frame);
511 n_left_from = from_frame->n_vectors;
512 next_index = node->cached_next_index;
514 while (n_left_from > 0)
516 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
518 while (n_left_from > 0 && n_left_to_next > 0)
524 ip4_header_t *ip0 = 0;
525 ip4_address_t *ia0 = 0;
528 ethernet_interface_t *ei0;
529 ethernet_header_t *mac0;
530 vnet_hw_interface_t *hi0;
531 u32 sw_if_index = ~0;
532 vnet_sw_interface_t *si0;
533 u32 inner_vlan = (u32) ~ 0;
534 u32 outer_vlan = (u32) ~ 0;
535 u32 error0 = (u32) ~ 0;
536 vnet_sw_interface_t *swif;
539 dhcp_server_t *server;
540 u32 original_sw_if_index = (u32) ~ 0;
541 dhcp4_next_t next0 = DHCP4_PROXY_NEXT_TX;
542 ip4_address_t relay_addr = {
546 bi0 = to_next[0] = from[0];
552 b0 = vlib_get_buffer (vm, bi0);
553 h0 = vlib_buffer_get_current (b0);
556 * udp_local hands us the DHCP header, need udp hdr,
557 * ip hdr to relay to client
559 vlib_buffer_advance (b0, -(sizeof (*u0)));
560 u0 = vlib_buffer_get_current (b0);
562 vlib_buffer_advance (b0, -(sizeof (*ip0)));
563 ip0 = vlib_buffer_get_current (b0);
565 /* Consumed by dhcp client code? */
566 if (dhcp_client_for_us (bi0, b0, ip0, u0, h0))
568 error0 = DHCP_PROXY_ERROR_FOR_US;
572 // if (1 /* dpm->insert_option_82 */ )
573 /* linearize needed to "unclone" and scan options */
574 int rv = vlib_buffer_chain_linearize (vm, b0);
575 if ((b0->flags & VLIB_BUFFER_NEXT_PRESENT) != 0 || !rv)
577 error0 = DHCP_PROXY_ERROR_PKT_TOO_BIG;
581 dhcp_option_t *o = h0->options, *end =
582 (void *) vlib_buffer_get_tail (b0);
584 /* Parse through TLVs looking for option 82.
585 The circuit-ID is the FIB number we need
586 to track down the client-facing interface */
588 while (o->option != DHCP_PACKET_OPTION_END && o < end)
594 dhcp_option_t *sub = (dhcp_option_t *) & o->data[0];
595 dhcp_option_t *subend =
596 (dhcp_option_t *) (o->data + o->length);
597 while (sub->option != DHCP_PACKET_OPTION_END
600 /* If this is one of ours, it will have
601 total length 12, circuit-id suboption type,
602 and the sw_if_index */
603 if (sub->option == 1 && sub->length == 4)
605 sw_if_index = ((sub->data[0] << 24) |
606 (sub->data[1] << 16) |
607 (sub->data[2] << 8) |
610 else if (sub->option == 5 && sub->length == 4)
612 relay_addr.as_u8[0] = sub->data[0];
613 relay_addr.as_u8[1] = sub->data[1];
614 relay_addr.as_u8[2] = sub->data[2];
615 relay_addr.as_u8[3] = sub->data[3];
617 else if (sub->option == 151 &&
618 sub->length == 7 && sub->data[0] == 1)
620 else if (sub->option == 152 && sub->length == 0)
622 sub = (dhcp_option_t *) (sub->data + sub->length);
624 if (vss_ctrl && vss_exist)
625 vlib_node_increment_counter
626 (vm, dhcp_proxy_to_client_node.index,
627 DHCP_PROXY_ERROR_OPTION_82_VSS_NOT_PROCESSED, 1);
630 o = (dhcp_option_t *) (o->data + o->length);
633 if (sw_if_index == (u32) ~ 0)
635 error0 = DHCP_PROXY_ERROR_NO_OPTION_82;
638 vlib_node_increment_counter (vm,
639 dhcp_proxy_to_client_node.index,
641 b0->error = node->errors[error0];
642 next0 = DHCP4_PROXY_NEXT_DROP;
646 if (relay_addr.as_u32 == 0)
648 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82_ADDR;
652 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
654 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82_ITF;
658 fib_index = im->fib_index_by_sw_if_index[sw_if_index];
659 proxy = dhcp_get_proxy (dpm, fib_index, FIB_PROTOCOL_IP4);
661 if (PREDICT_FALSE (NULL == proxy))
663 error0 = DHCP_PROXY_ERROR_NO_SERVER;
667 vec_foreach (server, proxy->dhcp_servers)
669 if (ip0->src_address.as_u32 == server->dhcp_server.ip4.as_u32)
675 error0 = DHCP_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
679 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
681 swif = vnet_get_sw_interface (vnm, sw_if_index);
682 original_sw_if_index = sw_if_index;
683 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
684 sw_if_index = swif->unnumbered_sw_if_index;
686 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
689 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
693 if (relay_addr.as_u32 != ia0->as_u32)
695 error0 = DHCP_PROXY_ERROR_BAD_YIADDR;
700 u0->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client);
701 sum0 = ip0->checksum;
702 old0 = ip0->dst_address.as_u32;
704 ip0->dst_address.as_u32 = new0;
706 ip_csum_update (sum0, old0, new0, ip4_header_t /* structure */ ,
707 dst_address /* offset of changed member */ );
708 ip0->checksum = ip_csum_fold (sum0);
710 sum0 = ip0->checksum;
711 old0 = ip0->src_address.as_u32;
713 ip0->src_address.as_u32 = new0;
715 ip_csum_update (sum0, old0, new0, ip4_header_t /* structure */ ,
716 src_address /* offset of changed member */ );
717 ip0->checksum = ip_csum_fold (sum0);
719 vlib_buffer_advance (b0, -(sizeof (ethernet_header_t)));
720 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
721 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
723 if (si0->sub.eth.flags.one_tag == 1)
725 vlib_buffer_advance (b0, -4 /* space for 1 VLAN tag */ );
726 outer_vlan = (si0->sub.eth.outer_vlan_id << 16) | 0x0800;
728 else if (si0->sub.eth.flags.two_tags == 1)
730 vlib_buffer_advance (b0, -8 /* space for 2 VLAN tag */ );
731 outer_vlan = (si0->sub.eth.outer_vlan_id << 16) | 0x8100;
732 inner_vlan = (si0->sub.eth.inner_vlan_id << 16) | 0x0800;
736 mac0 = vlib_buffer_get_current (b0);
738 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
739 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
740 clib_memcpy (mac0->src_address, &ei0->address,
741 sizeof (mac0->src_address));
742 clib_memset (mac0->dst_address, 0xff, sizeof (mac0->dst_address));
744 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB
745 && outer_vlan != (u32) ~ 0)
747 mac0->type = (si0->sub.eth.flags.dot1ad == 1) ?
748 clib_net_to_host_u16 (0x88a8) : clib_net_to_host_u16 (0x8100);
749 u32 *vlan_tag = (u32 *) (mac0 + 1);
750 *vlan_tag = clib_host_to_net_u32 (outer_vlan);
751 if (inner_vlan != (u32) ~ 0)
753 u32 *inner_vlan_tag = (u32 *) (vlan_tag + 1);
754 *inner_vlan_tag = clib_host_to_net_u32 (inner_vlan);
759 mac0->type = clib_net_to_host_u16 (0x0800);
763 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
765 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
767 tr->which = 1; /* to client */
768 tr->trace_ip4_address.as_u32 = ia0 ? ia0->as_u32 : 0;
770 tr->original_sw_if_index = original_sw_if_index;
771 tr->sw_if_index = sw_if_index;
772 clib_memcpy_fast (tr->packet_data, h0,
773 sizeof (tr->packet_data));
776 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
777 to_next, n_left_to_next,
780 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
783 return from_frame->n_vectors;
787 VLIB_REGISTER_NODE (dhcp_proxy_to_client_node, static) = {
788 .function = dhcp_proxy_to_client_input,
789 .name = "dhcp-proxy-to-client",
790 /* Takes a vector of packets. */
791 .vector_size = sizeof (u32),
793 .n_errors = DHCP_PROXY_N_ERROR,
794 .error_strings = dhcp_proxy_error_strings,
795 .format_buffer = format_dhcp_proxy_header_with_length,
796 .format_trace = format_dhcp_proxy_trace,
798 .unformat_buffer = unformat_dhcp_proxy_header,
800 .n_next_nodes = DHCP4_PROXY_N_NEXT,
802 [DHCP4_PROXY_NEXT_DROP] = "error-drop",
803 [DHCP4_PROXY_NEXT_TX] = "interface-output",
809 dhcp_maybe_register_udp_ports (dhcp_port_reg_flags_t ports)
811 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
812 vlib_main_t *vm = dm->vlib_main;
813 int port_regs_diff = dm->udp_ports_registered ^ ports;
818 if ((port_regs_diff & DHCP_PORT_REG_CLIENT) & ports)
819 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
820 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */ );
822 if ((port_regs_diff & DHCP_PORT_REG_SERVER) & ports)
823 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
824 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */ );
826 dm->udp_ports_registered |= ports;
829 static clib_error_t *
830 dhcp4_proxy_init (vlib_main_t * vm)
832 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
833 vlib_node_t *error_drop_node;
835 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
836 dm->error_drop_node_index = error_drop_node->index;
843 VLIB_INIT_FUNCTION (dhcp4_proxy_init);
846 dhcp4_proxy_set_server (ip46_address_t * addr,
847 ip46_address_t * src_addr,
848 u32 rx_table_id, u32 server_table_id, int is_del)
850 u32 rx_fib_index = 0;
853 const fib_prefix_t all_1s = {
855 .fp_addr.ip4.as_u32 = 0xffffffff,
856 .fp_proto = FIB_PROTOCOL_IP4,
859 if (ip46_address_is_zero (addr))
860 return VNET_API_ERROR_INVALID_DST_ADDRESS;
862 if (ip46_address_is_zero (src_addr))
863 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
865 dhcp_maybe_register_udp_ports (DHCP_PORT_REG_CLIENT | DHCP_PORT_REG_SERVER);
867 rx_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
873 if (dhcp_proxy_server_del (FIB_PROTOCOL_IP4, rx_fib_index,
874 addr, server_table_id))
876 fib_table_entry_special_remove (rx_fib_index,
877 &all_1s, FIB_SOURCE_DHCP);
878 fib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
883 if (dhcp_proxy_server_add (FIB_PROTOCOL_IP4,
885 rx_fib_index, server_table_id))
887 fib_table_entry_special_add (rx_fib_index,
889 FIB_SOURCE_DHCP, FIB_ENTRY_FLAG_LOCAL);
890 fib_table_lock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
893 fib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_DHCP);
898 static clib_error_t *
899 dhcp4_proxy_set_command_fn (vlib_main_t * vm,
900 unformat_input_t * input,
901 vlib_cli_command_t * cmd)
903 ip46_address_t server_addr, src_addr;
904 u32 server_table_id = 0, rx_table_id = 0;
906 int set_src = 0, set_server = 0;
908 clib_memset (&server_addr, 0, sizeof (server_addr));
909 clib_memset (&src_addr, 0, sizeof (src_addr));
911 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
913 if (unformat (input, "server %U",
914 unformat_ip4_address, &server_addr.ip4))
916 else if (unformat (input, "server-fib-id %d", &server_table_id))
918 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
920 else if (unformat (input, "src-address %U",
921 unformat_ip4_address, &src_addr.ip4))
923 else if (unformat (input, "delete") || unformat (input, "del"))
929 if (is_del || (set_server && set_src))
933 rv = dhcp4_proxy_set_server (&server_addr, &src_addr, rx_table_id,
934 server_table_id, is_del);
940 case VNET_API_ERROR_INVALID_DST_ADDRESS:
941 return clib_error_return (0, "Invalid server address");
943 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
944 return clib_error_return (0, "Invalid src address");
946 case VNET_API_ERROR_NO_SUCH_ENTRY:
947 return clib_error_return
948 (0, "Fib id %d: no per-fib DHCP server configured", rx_table_id);
951 return clib_error_return (0, "BUG: rv %d", rv);
955 return clib_error_return (0, "parse error`%U'",
956 format_unformat_error, input);
960 VLIB_CLI_COMMAND (dhcp_proxy_set_command, static) = {
961 .path = "set dhcp proxy",
962 .short_help = "set dhcp proxy [del] server <ip-addr> src-address <ip-addr> [server-fib-id <n>] [rx-fib-id <n>]",
963 .function = dhcp4_proxy_set_command_fn,
968 format_dhcp4_proxy_server (u8 * s, va_list * args)
970 dhcp_proxy_t *proxy = va_arg (*args, dhcp_proxy_t *);
971 ip4_fib_t *rx_fib, *server_fib;
972 dhcp_server_t *server;
976 s = format (s, "%=14s%=16s%s", "RX FIB", "Src Address",
977 "Servers FIB,Address");
981 rx_fib = ip4_fib_get (proxy->rx_fib_index);
983 s = format (s, "%=14u%=16U",
985 format_ip46_address, &proxy->dhcp_src_address, IP46_TYPE_ANY);
987 vec_foreach (server, proxy->dhcp_servers)
989 server_fib = ip4_fib_get (server->server_fib_index);
990 s = format (s, "%u,%U ",
991 server_fib->table_id,
992 format_ip46_address, &server->dhcp_server, IP46_TYPE_ANY);
998 dhcp4_proxy_show_walk (dhcp_proxy_t * server, void *ctx)
1000 vlib_main_t *vm = ctx;
1002 vlib_cli_output (vm, "%U", format_dhcp4_proxy_server, server);
1007 static clib_error_t *
1008 dhcp4_proxy_show_command_fn (vlib_main_t * vm,
1009 unformat_input_t * input,
1010 vlib_cli_command_t * cmd)
1012 vlib_cli_output (vm, "%U", format_dhcp4_proxy_server,
1013 NULL /* header line */ );
1015 dhcp_proxy_walk (FIB_PROTOCOL_IP4, dhcp4_proxy_show_walk, vm);
1021 VLIB_CLI_COMMAND (dhcp_proxy_show_command, static) = {
1022 .path = "show dhcp proxy",
1023 .short_help = "Display dhcp proxy server info",
1024 .function = dhcp4_proxy_show_command_fn,
1028 static clib_error_t *
1029 dhcp_option_82_vss_fn (vlib_main_t * vm,
1030 unformat_input_t * input, vlib_cli_command_t * cmd)
1032 u8 is_del = 0, vss_type = VSS_TYPE_DEFAULT;
1033 u32 oui = 0, fib_id = 0, tbl_id = ~0;
1034 u8 *vpn_ascii_id = 0;
1036 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1038 if (unformat (input, "table %d", &tbl_id))
1040 else if (unformat (input, "oui %d", &oui))
1041 vss_type = VSS_TYPE_VPN_ID;
1042 else if (unformat (input, "vpn-id %d", &fib_id))
1043 vss_type = VSS_TYPE_VPN_ID;
1044 else if (unformat (input, "vpn-ascii-id %s", &vpn_ascii_id))
1045 vss_type = VSS_TYPE_ASCII;
1046 else if (unformat (input, "delete") || unformat (input, "del"))
1053 return clib_error_return (0, "no table ID specified.");
1055 int rv = dhcp_proxy_set_vss (FIB_PROTOCOL_IP4, tbl_id, vss_type,
1056 vpn_ascii_id, oui, fib_id, is_del);
1061 case VNET_API_ERROR_NO_SUCH_ENTRY:
1062 return clib_error_return (0,
1063 "option 82 vss for table %d not found in in pool.",
1066 return clib_error_return (0, "BUG: rv %d", rv);
1072 VLIB_CLI_COMMAND (dhcp_proxy_vss_command,static) = {
1073 .path = "set dhcp option-82 vss",
1074 .short_help = "set dhcp option-82 vss [del] table <table id> [oui <n> vpn-id <n> | vpn-ascii-id <text>]",
1075 .function = dhcp_option_82_vss_fn,
1079 static clib_error_t *
1080 dhcp_vss_show_command_fn (vlib_main_t * vm,
1081 unformat_input_t * input, vlib_cli_command_t * cmd)
1083 dhcp_vss_walk (FIB_PROTOCOL_IP4, dhcp_vss_show_walk, vm);
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,
1096 static clib_error_t *
1097 dhcp_option_82_address_show_command_fn (vlib_main_t * vm,
1098 unformat_input_t * input,
1099 vlib_cli_command_t * cmd)
1101 vnet_main_t *vnm = vnet_get_main ();
1102 u32 sw_if_index0 = 0, sw_if_index;
1103 vnet_sw_interface_t *swif;
1106 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1109 if (unformat (input, "%U",
1110 unformat_vnet_sw_interface, vnm, &sw_if_index0))
1112 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1113 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1114 swif->unnumbered_sw_if_index : sw_if_index0;
1115 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
1118 vlib_cli_output (vm, "%=20s%=20s", "interface",
1119 "source IP address");
1121 vlib_cli_output (vm, "%=20U%=20U",
1122 format_vnet_sw_if_index_name,
1123 vnm, sw_if_index0, format_ip4_address, ia0);
1126 vlib_cli_output (vm, "%=34s %=20U",
1127 "No IPv4 address configured on",
1128 format_vnet_sw_if_index_name, vnm, sw_if_index);
1138 VLIB_CLI_COMMAND (dhcp_proxy_address_show_command,static) = {
1139 .path = "show dhcp option-82-address interface",
1140 .short_help = "show dhcp option-82-address interface <interface>",
1141 .function = dhcp_option_82_address_show_command_fn,
1146 * fd.io coding-style-patch-verification: ON
1149 * eval: (c-set-style "gnu")