blob: 26e1e65cd4cba9bc017a22ed855a29e7cd0674f8 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * proxy_node.c: dhcp proxy node processing
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vlib/vlib.h>
19#include <vnet/pg/pg.h>
Neale Ranns2dd68522017-02-16 03:38:59 -080020#include <vnet/dhcp/dhcp_proxy.h>
Neale Ranns20a175a2017-02-14 07:28:41 -080021#include <vnet/dhcp/client.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022#include <vnet/fib/ip4_fib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24static char * dhcp_proxy_error_strings[] = {
25#define dhcp_proxy_error(n,s) s,
Neale Ranns2dd68522017-02-16 03:38:59 -080026#include <vnet/dhcp/dhcp4_proxy_error.def>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027#undef dhcp_proxy_error
28};
29
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")
34
35typedef enum {
36#define _(s,n) DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s,
37 foreach_dhcp_proxy_to_server_input_next
38#undef _
39 DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
40} dhcp_proxy_to_server_input_next_t;
41
42typedef struct {
43 /* 0 => to server, 1 => to client */
44 int which;
45 ip4_address_t trace_ip4_address;
46 u32 error;
47 u32 sw_if_index;
48 u32 original_sw_if_index;
49} dhcp_proxy_trace_t;
50
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)
57
Neale Ranns2dd68522017-02-16 03:38:59 -080058static vlib_node_registration_t dhcp_proxy_to_server_node;
59static vlib_node_registration_t dhcp_proxy_to_client_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Neale Ranns2dd68522017-02-16 03:38:59 -080061static u8 *
62format_dhcp_proxy_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070063{
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 *);
67
68 if (t->which == 0)
69 s = format (s, "DHCP proxy: sent to server %U\n",
70 format_ip4_address, &t->trace_ip4_address, t->error);
71 else
72 s = format (s, "DHCP proxy: broadcast to client from %U\n",
73 format_ip4_address, &t->trace_ip4_address);
74
75 if (t->error != (u32)~0)
76 s = format (s, " error: %s\n", dhcp_proxy_error_strings[t->error]);
77
78 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
79 t->original_sw_if_index, t->sw_if_index);
80
81 return s;
82}
83
Neale Ranns2dd68522017-02-16 03:38:59 -080084static u8 *
85format_dhcp_proxy_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
87 dhcp_header_t * h = va_arg (*args, dhcp_header_t *);
88 u32 max_header_bytes = va_arg (*args, u32);
89 u32 header_bytes;
90
91 header_bytes = sizeof (h[0]);
92 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
93 return format (s, "dhcp header truncated");
94
95 s = format (s, "DHCP Proxy");
96
97 return s;
98}
99
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100static uword
101dhcp_proxy_to_server_input (vlib_main_t * vm,
102 vlib_node_runtime_t * node,
103 vlib_frame_t * from_frame)
104{
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;
111 u32 pkts_too_big=0;
112 ip4_main_t * im = &ip4_main;
113
114 next_index = node->cached_next_index;
115
116 while (n_left_from > 0)
117 {
118 u32 n_left_to_next;
119
120 vlib_get_next_frame (vm, node, next_index,
121 to_next, n_left_to_next);
122
123 while (n_left_from > 0 && n_left_to_next > 0)
124 {
125 u32 bi0;
126 vlib_buffer_t * b0;
127 udp_header_t * u0;
128 dhcp_header_t * h0;
129 ip4_header_t * ip0;
130 u32 next0;
131 u32 old0, new0;
132 ip_csum_t sum0;
133 u32 error0 = (u32) ~0;
134 u32 sw_if_index = 0;
135 u32 original_sw_if_index = 0;
136 u8 *end = NULL;
Neale Ranns20a175a2017-02-14 07:28:41 -0800137 u32 fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -0800138 dhcp_proxy_t *proxy;
139 dhcp_server_t *server;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 u32 rx_sw_if_index;
Neale Ranns20a175a2017-02-14 07:28:41 -0800141 dhcp_option_t *o;
142 u32 len = 0;
143 vlib_buffer_free_list_t *fl;
Neale Ranns3466c302017-02-16 07:45:03 -0800144 u8 is_discover = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
146 bi0 = from[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 from += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
150 b0 = vlib_get_buffer (vm, bi0);
151
152 h0 = vlib_buffer_get_current (b0);
153
154 /*
155 * udp_local hands us the DHCP header, need udp hdr,
156 * ip hdr to relay to server
157 */
158 vlib_buffer_advance (b0, -(sizeof(*u0)));
159 u0 = vlib_buffer_get_current (b0);
160
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))
163 {
164 vlib_buffer_advance (b0, sizeof(*u0));
165 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
166 error0 = 0;
167 pkts_to_client++;
168 goto do_enqueue;
169 }
170
171 rx_sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
172
173 fib_index = im->fib_index_by_sw_if_index [rx_sw_if_index];
Neale Ranns3466c302017-02-16 07:45:03 -0800174 proxy = dhcp_get_proxy(dpm, fib_index, FIB_PROTOCOL_IP4);
175
176 if (PREDICT_FALSE (NULL == proxy))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 error0 = DHCP_PROXY_ERROR_NO_SERVER;
179 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
180 pkts_no_server++;
181 goto do_trace;
182 }
Neale Ranns3466c302017-02-16 07:45:03 -0800183
184 server = &proxy->dhcp_servers[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 vlib_buffer_advance (b0, -(sizeof(*ip0)));
186 ip0 = vlib_buffer_get_current (b0);
187
188 /* disable UDP checksum */
189 u0->checksum = 0;
190 sum0 = ip0->checksum;
191 old0 = ip0->dst_address.as_u32;
Neale Ranns2dd68522017-02-16 03:38:59 -0800192 new0 = server->dhcp_server.ip4.as_u32;
193 ip0->dst_address.as_u32 = server->dhcp_server.ip4.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 sum0 = ip_csum_update (sum0, old0, new0,
195 ip4_header_t /* structure */,
196 dst_address /* changed member */);
197 ip0->checksum = ip_csum_fold (sum0);
198
199 sum0 = ip0->checksum;
200 old0 = ip0->src_address.as_u32;
Neale Ranns3466c302017-02-16 07:45:03 -0800201 new0 = proxy->dhcp_src_address.ip4.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 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);
207
208 /* Send to DHCP server via the configured FIB */
209 vnet_buffer(b0)->sw_if_index[VLIB_TX] =
210 server->server_fib_index;
211
Neale Ranns3466c302017-02-16 07:45:03 -0800212 h0->gateway_ip_address.as_u32 = proxy->dhcp_src_address.ip4.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213 pkts_to_server++;
214
Neale Ranns20a175a2017-02-14 07:28:41 -0800215 o = (dhcp_option_t *) h0->options;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Neale Ranns20a175a2017-02-14 07:28:41 -0800217 fib_index = im->fib_index_by_sw_if_index
218 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
Neale Ranns20a175a2017-02-14 07:28:41 -0800220 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)
Neale Ranns3466c302017-02-16 07:45:03 -0800223 {
224 if (DHCP_PACKET_OPTION_MSG_TYPE == o->option)
225 {
226 if (DHCP_PACKET_DISCOVER == o->data[0])
227 {
228 is_discover = 1;
229 }
230 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800231 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
Neale Ranns3466c302017-02-16 07:45:03 -0800232 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
Neale Ranns20a175a2017-02-14 07:28:41 -0800234 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
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)
237 {
238 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
239 pkts_too_big++;
240 goto do_trace;
241 }
242
243 if ((o->option == 0xFF) && ((u8 *)o <= end))
244 {
245 vnet_main_t *vnm = vnet_get_main();
246 u16 old_l0, new_l0;
247 ip4_address_t _ia0, * ia0 = &_ia0;
Neale Ranns2dd68522017-02-16 03:38:59 -0800248 dhcp_vss_t *vss;
Neale Ranns20a175a2017-02-14 07:28:41 -0800249 vnet_sw_interface_t *swif;
250 sw_if_index = 0;
251 original_sw_if_index = 0;
252
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;
258
259 /*
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.
263 */
264 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0);
265
266 if (ia0 == 0)
267 {
268 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
Neale Ranns20a175a2017-02-14 07:28:41 -0800270 pkts_no_interface_address++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 goto do_trace;
Neale Ranns20a175a2017-02-14 07:28:41 -0800272 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
Neale Ranns20a175a2017-02-14 07:28:41 -0800274 /* Add option 82 */
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];
289 o->data[12] = 0xFF;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Neale Ranns2dd68522017-02-16 03:38:59 -0800291 vss = dhcp_get_vss_info (dpm, fib_index, FIB_PROTOCOL_IP4);
Neale Ranns20a175a2017-02-14 07:28:41 -0800292 if (NULL != vss)
293 {
294 u32 opt82_fib_id=0, opt82_oui=0;
295
Neale Ranns2dd68522017-02-16 03:38:59 -0800296 opt82_oui = vss->oui;
297 opt82_fib_id = vss->fib_id;
Neale Ranns20a175a2017-02-14 07:28:41 -0800298
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) */
306 o->data[17] = 0xFF;
307 o->length += 5;
308 } else {
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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Neale Ranns20a175a2017-02-14 07:28:41 -0800321 /* and a new "end-of-options" option (0xff) */
322 o->data[24] = 0xFF;
323 o->length += 12;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800325 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Neale Ranns20a175a2017-02-14 07:28:41 -0800327 len = o->length + 3;
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);
332 new_l0 += len;
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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339
Neale Ranns20a175a2017-02-14 07:28:41 -0800340 /* Fix UDP length */
341 new_l0 = clib_net_to_host_u16 (u0->length);
342 new_l0 += len;
343 u0->length = clib_host_to_net_u16 (new_l0);
344 } else {
345 vlib_node_increment_counter
346 (vm, dhcp_proxy_to_server_node.index,
347 DHCP_PROXY_ERROR_OPTION_82_ERROR, 1);
348 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
350 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
351
Neale Ranns3466c302017-02-16 07:45:03 -0800352 /*
353 * If we have multiple servers configured and this is the
354 * client's discover message, then send copies to each of
355 * those servers
356 */
357 if (is_discover && vec_len(proxy->dhcp_servers) > 1)
358 {
359 u32 ii;
360
361 for (ii = 1; ii < vec_len(proxy->dhcp_servers); ii++)
362 {
363 vlib_buffer_t *c0;
364 u32 ci0;
365
366 c0 = vlib_buffer_copy(vm, b0);
367 ci0 = vlib_get_buffer_index(vm, c0);
368 server = &proxy->dhcp_servers[ii];
369
370 ip0 = vlib_buffer_get_current (c0);
371
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);
380
381 to_next[0] = ci0;
382 to_next += 1;
383 n_left_to_next -= 1;
384
385 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
386 to_next, n_left_to_next,
387 ci0, next0);
388
389 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
390 {
391 dhcp_proxy_trace_t *tr;
392
393 tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
394 tr->which = 0; /* to server */
395 tr->error = error0;
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;
400 }
401
402 if (PREDICT_FALSE(0 == n_left_to_next))
403 {
404 vlib_put_next_frame (vm, node, next_index,
405 n_left_to_next);
406 vlib_get_next_frame (vm, node, next_index,
407 to_next, n_left_to_next);
408 }
409 }
410 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 do_trace:
412 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
413 {
414 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
415 b0, sizeof (*tr));
416 tr->which = 0; /* to server */
417 tr->error = error0;
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)
Neale Ranns3466c302017-02-16 07:45:03 -0800421 tr->trace_ip4_address.as_u32 =
422 proxy->dhcp_servers[0].dhcp_server.ip4.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 }
424
425 do_enqueue:
Neale Ranns3466c302017-02-16 07:45:03 -0800426 to_next[0] = bi0;
427 to_next += 1;
428 n_left_to_next -= 1;
429
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
431 to_next, n_left_to_next,
432 bi0, next0);
433 }
434
435 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
436 }
437
438 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
439 DHCP_PROXY_ERROR_RELAY_TO_CLIENT,
440 pkts_to_client);
441 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
442 DHCP_PROXY_ERROR_RELAY_TO_SERVER,
443 pkts_to_server);
444 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
445 DHCP_PROXY_ERROR_NO_SERVER,
446 pkts_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,
452 pkts_too_big);
453 return from_frame->n_vectors;
454}
455
Neale Ranns2dd68522017-02-16 03:38:59 -0800456VLIB_REGISTER_NODE (dhcp_proxy_to_server_node, static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 .function = dhcp_proxy_to_server_input,
458 .name = "dhcp-proxy-to-server",
459 /* Takes a vector of packets. */
460 .vector_size = sizeof (u32),
461
462 .n_errors = DHCP_PROXY_N_ERROR,
463 .error_strings = dhcp_proxy_error_strings,
464
465 .n_next_nodes = DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
466 .next_nodes = {
467#define _(s,n) [DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
468 foreach_dhcp_proxy_to_server_input_next
469#undef _
470 },
471
472 .format_buffer = format_dhcp_proxy_header_with_length,
473 .format_trace = format_dhcp_proxy_trace,
474#if 0
475 .unformat_buffer = unformat_dhcp_proxy_header,
476#endif
477};
478
479static uword
480dhcp_proxy_to_client_input (vlib_main_t * vm,
481 vlib_node_runtime_t * node,
482 vlib_frame_t * from_frame)
483{
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;
489
490 from = vlib_frame_vector_args (from_frame);
491 n_left_from = from_frame->n_vectors;
492
493 while (n_left_from > 0)
494 {
495 u32 bi0;
496 vlib_buffer_t * b0;
497 udp_header_t * u0;
498 dhcp_header_t * h0;
499 ip4_header_t * ip0 = 0;
500 ip4_address_t * ia0 = 0;
501 u32 old0, new0;
502 ip_csum_t sum0;
503 ethernet_interface_t *ei0;
504 ethernet_header_t *mac0;
505 vnet_hw_interface_t *hi0;
506 vlib_frame_t *f0;
507 u32 * to_next0;
508 u32 sw_if_index = ~0;
509 vnet_sw_interface_t *si0;
510 u32 error0 = (u32)~0;
511 vnet_sw_interface_t *swif;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512 u32 fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -0800513 dhcp_proxy_t *proxy;
514 dhcp_server_t *server;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 u32 original_sw_if_index = (u32) ~0;
Neale Ranns20a175a2017-02-14 07:28:41 -0800516 ip4_address_t relay_addr = {
517 .as_u32 = 0,
518 };
519
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 bi0 = from[0];
521 from += 1;
522 n_left_from -= 1;
523
524 b0 = vlib_get_buffer (vm, bi0);
525 h0 = vlib_buffer_get_current (b0);
526
527 /*
528 * udp_local hands us the DHCP header, need udp hdr,
529 * ip hdr to relay to client
530 */
531 vlib_buffer_advance (b0, -(sizeof(*u0)));
532 u0 = vlib_buffer_get_current (b0);
533
534 vlib_buffer_advance (b0, -(sizeof(*ip0)));
535 ip0 = vlib_buffer_get_current (b0);
536
537 /* Consumed by dhcp client code? */
538 if (dhcp_client_for_us (bi0, b0, ip0, u0, h0))
539 continue;
540
541 if (1 /* dpm->insert_option_82 */)
542 {
543 dhcp_option_t *o = (dhcp_option_t *) h0->options;
544 dhcp_option_t *sub;
545
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 */
549
550 while (o->option != 0xFF /* end of options */ &&
551 (u8 *) o < (b0->data + b0->current_data + b0->current_length))
552 {
553 if (o->option == 82)
554 {
555 u32 vss_exist = 0;
556 u32 vss_ctrl = 0;
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)
564 {
Neale Ranns20a175a2017-02-14 07:28:41 -0800565 sw_if_index = ((sub->data[0] << 24) |
566 (sub->data[1] << 16) |
567 (sub->data[2] << 8) |
568 (sub->data[3]));
569 }
570 else if (sub->option == 5 && sub->length == 4)
571 {
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];
576 }
577 else if (sub->option == 151 &&
578 sub->length == 7 &&
579 sub->data[0] == 1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580 vss_exist = 1;
581 else if (sub->option == 152 && sub->length == 0)
582 vss_ctrl = 1;
583 sub = (dhcp_option_t *)
584 (((uword) sub) + (sub->length + 2));
585 }
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);
590
591 }
592 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
593 }
594 }
595
596 if (sw_if_index == (u32)~0)
597 {
598 error0 = DHCP_PROXY_ERROR_NO_OPTION_82;
599
600 drop_packet:
601 vlib_node_increment_counter (vm, dhcp_proxy_to_client_node.index,
602 error0, 1);
603 f0 = vlib_get_frame_to_node (vm, dpm->error_drop_node_index);
604 to_next0 = vlib_frame_vector_args (f0);
605 to_next0[0] = bi0;
606 f0->n_vectors = 1;
607 vlib_put_frame_to_node (vm, dpm->error_drop_node_index, f0);
608 goto do_trace;
609 }
610
Neale Ranns20a175a2017-02-14 07:28:41 -0800611 if (relay_addr.as_u32 == 0)
612 {
613 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82_ADDR;
614 goto drop_packet;
615 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616
617 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
618 {
Neale Ranns20a175a2017-02-14 07:28:41 -0800619 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82_ITF;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620 goto drop_packet;
621 }
622
623 fib_index = im->fib_index_by_sw_if_index [sw_if_index];
Neale Ranns3466c302017-02-16 07:45:03 -0800624 proxy = dhcp_get_proxy(dpm, fib_index, FIB_PROTOCOL_IP4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625
Neale Ranns3466c302017-02-16 07:45:03 -0800626 if (PREDICT_FALSE (NULL == proxy))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627 {
628 error0 = DHCP_PROXY_ERROR_NO_SERVER;
629 goto drop_packet;
630 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800631
Neale Ranns3466c302017-02-16 07:45:03 -0800632 vec_foreach(server, proxy->dhcp_servers)
633 {
634 if (ip0->src_address.as_u32 == server->dhcp_server.ip4.as_u32)
635 {
636 goto server_found;
637 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638 }
639
Neale Ranns3466c302017-02-16 07:45:03 -0800640 error0 = DHCP_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
641 goto drop_packet;
642
643 server_found:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
645
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;
650
Dave Barach203c6322016-06-26 10:29:03 -0400651 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652 if (ia0 == 0)
653 {
654 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
655 goto drop_packet;
656 }
657
Neale Ranns20a175a2017-02-14 07:28:41 -0800658 if (relay_addr.as_u32 != ia0->as_u32)
659 {
660 error0 = DHCP_PROXY_ERROR_BAD_YIADDR;
661 goto drop_packet;
662 }
663
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 u0->checksum = 0;
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;
668 new0 = 0xFFFFFFFF;
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);
674
675 sum0 = ip0->checksum;
676 old0 = ip0->src_address.as_u32;
677 new0 = ia0->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);
683
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 */);
688
689 mac0 = vlib_buffer_get_current (b0);
690
691 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
692 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
Damjan Marionf1213b82016-03-13 02:22:06 +0100693 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 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);
697
698 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
699 {
700 u32 * vlan_tag = (u32 *)(mac0+1);
701 u32 tmp;
702 tmp = (si0->sub.id << 16) | 0x0800;
703 *vlan_tag = clib_host_to_net_u32 (tmp);
704 }
705
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);
709 to_next0[0] = bi0;
710 f0->n_vectors = 1;
711 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
712
713 do_trace:
714 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
715 {
716 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
717 b0, sizeof (*tr));
718 tr->which = 1; /* to client */
719 tr->trace_ip4_address.as_u32 = ia0 ? ia0->as_u32 : 0;
720 tr->error = error0;
721 tr->original_sw_if_index = original_sw_if_index;
722 tr->sw_if_index = sw_if_index;
723 }
724 }
725 return from_frame->n_vectors;
726}
727
Neale Ranns2dd68522017-02-16 03:38:59 -0800728VLIB_REGISTER_NODE (dhcp_proxy_to_client_node, static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 .function = dhcp_proxy_to_client_input,
730 .name = "dhcp-proxy-to-client",
731 /* Takes a vector of packets. */
732 .vector_size = sizeof (u32),
733
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,
738#if 0
739 .unformat_buffer = unformat_dhcp_proxy_header,
740#endif
741};
742
Neale Ranns2dd68522017-02-16 03:38:59 -0800743static clib_error_t *
744dhcp4_proxy_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745{
746 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
747 vlib_node_t * error_drop_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
750 dm->error_drop_node_index = error_drop_node->index;
751
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
753 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */);
754
755 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
756 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */);
757
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758 return 0;
759}
760
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Neale Ranns2dd68522017-02-16 03:38:59 -0800762VLIB_INIT_FUNCTION (dhcp4_proxy_init);
763
764int
765dhcp4_proxy_set_server (ip46_address_t *addr,
766 ip46_address_t *src_addr,
767 u32 rx_table_id,
768 u32 server_table_id,
769 int is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771 u32 rx_fib_index = 0;
Neale Ranns2dd68522017-02-16 03:38:59 -0800772 int rc = 0;
Neale Ranns20a175a2017-02-14 07:28:41 -0800773
Neale Rannsfca0c242017-01-13 07:57:46 -0800774 const fib_prefix_t all_1s =
775 {
776 .fp_len = 32,
777 .fp_addr.ip4.as_u32 = 0xffffffff,
778 .fp_proto = FIB_PROTOCOL_IP4,
779 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780
Neale Ranns2dd68522017-02-16 03:38:59 -0800781 if (ip46_address_is_zero(addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 return VNET_API_ERROR_INVALID_DST_ADDRESS;
783
Neale Ranns2dd68522017-02-16 03:38:59 -0800784 if (ip46_address_is_zero(src_addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
786
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100787 rx_fib_index = fib_table_find_or_create_and_lock(FIB_PROTOCOL_IP4,
Neale Ranns2dd68522017-02-16 03:38:59 -0800788 rx_table_id);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100789
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 if (is_del)
791 {
Neale Ranns3466c302017-02-16 07:45:03 -0800792 if (dhcp_proxy_server_del (FIB_PROTOCOL_IP4, rx_fib_index,
793 addr, server_table_id))
Neale Ranns2dd68522017-02-16 03:38:59 -0800794 {
795 fib_table_entry_special_remove(rx_fib_index,
796 &all_1s,
797 FIB_SOURCE_DHCP);
798 fib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP4);
799 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800801 else
802 {
Neale Ranns2dd68522017-02-16 03:38:59 -0800803 if (dhcp_proxy_server_add (FIB_PROTOCOL_IP4,
804 addr, src_addr,
805 rx_fib_index, server_table_id))
806 {
807 fib_table_entry_special_add(rx_fib_index,
808 &all_1s,
809 FIB_SOURCE_DHCP,
Neale Rannsa0558302017-04-13 00:44:52 -0700810 FIB_ENTRY_FLAG_LOCAL);
Neale Ranns2dd68522017-02-16 03:38:59 -0800811 fib_table_lock (rx_fib_index, FIB_PROTOCOL_IP4);
812 }
Neale Ranns20a175a2017-02-14 07:28:41 -0800813 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800814 fib_table_unlock (rx_fib_index, FIB_PROTOCOL_IP4);
Neale Ranns20a175a2017-02-14 07:28:41 -0800815
Neale Ranns2dd68522017-02-16 03:38:59 -0800816 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817}
818
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819static clib_error_t *
Neale Ranns2dd68522017-02-16 03:38:59 -0800820dhcp4_proxy_set_command_fn (vlib_main_t * vm,
821 unformat_input_t * input,
822 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823{
Neale Ranns2dd68522017-02-16 03:38:59 -0800824 ip46_address_t server_addr, src_addr;
825 u32 server_table_id = 0, rx_table_id = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826 int is_del = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827 int set_src = 0, set_server = 0;
Neale Ranns2dd68522017-02-16 03:38:59 -0800828
829 memset(&server_addr, 0, sizeof(server_addr));
830 memset(&src_addr, 0, sizeof(src_addr));
831
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
833 {
834 if (unformat (input, "server %U",
Neale Ranns2dd68522017-02-16 03:38:59 -0800835 unformat_ip4_address, &server_addr.ip4))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836 set_server = 1;
Neale Ranns2dd68522017-02-16 03:38:59 -0800837 else if (unformat (input, "server-fib-id %d", &server_table_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838 ;
Neale Ranns2dd68522017-02-16 03:38:59 -0800839 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840 ;
841 else if (unformat(input, "src-address %U",
Neale Ranns2dd68522017-02-16 03:38:59 -0800842 unformat_ip4_address, &src_addr.ip4))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843 set_src = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844 else if (unformat (input, "delete") ||
845 unformat (input, "del"))
846 is_del = 1;
847 else
848 break;
849 }
850
851 if (is_del || (set_server && set_src))
852 {
853 int rv;
854
Neale Ranns2dd68522017-02-16 03:38:59 -0800855 rv = dhcp4_proxy_set_server (&server_addr, &src_addr, rx_table_id,
856 server_table_id, is_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857 switch (rv)
858 {
859 case 0:
860 return 0;
861
862 case VNET_API_ERROR_INVALID_DST_ADDRESS:
863 return clib_error_return (0, "Invalid server address");
864
865 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
866 return clib_error_return (0, "Invalid src address");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
868 case VNET_API_ERROR_NO_SUCH_ENTRY:
869 return clib_error_return
Neale Ranns2dd68522017-02-16 03:38:59 -0800870 (0, "Fib id %d: no per-fib DHCP server configured", rx_table_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871
872 default:
873 return clib_error_return (0, "BUG: rv %d", rv);
874 }
875 }
876 else
877 return clib_error_return (0, "parse error`%U'",
878 format_unformat_error, input);
879}
880
881VLIB_CLI_COMMAND (dhcp_proxy_set_command, static) = {
882 .path = "set dhcp proxy",
Neale Ranns20a175a2017-02-14 07:28:41 -0800883 .short_help = "set dhcp proxy [del] server <ip-addr> src-address <ip-addr> [server-fib-id <n>] [rx-fib-id <n>]",
Neale Ranns2dd68522017-02-16 03:38:59 -0800884 .function = dhcp4_proxy_set_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885};
886
Neale Ranns2dd68522017-02-16 03:38:59 -0800887static u8 *
888format_dhcp4_proxy_server (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889{
Neale Ranns3466c302017-02-16 07:45:03 -0800890 dhcp_proxy_t *proxy = va_arg (*args, dhcp_proxy_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891 ip4_fib_t * rx_fib, * server_fib;
Neale Ranns3466c302017-02-16 07:45:03 -0800892 dhcp_server_t *server;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893
Neale Ranns3466c302017-02-16 07:45:03 -0800894 if (proxy == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 {
Neale Ranns3466c302017-02-16 07:45:03 -0800896 s = format (s, "%=14s%=16s%s", "RX FIB", "Src Address",
897 "Servers FIB,Address");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 return s;
899 }
900
Neale Ranns3466c302017-02-16 07:45:03 -0800901 rx_fib = ip4_fib_get(proxy->rx_fib_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902
Neale Ranns3466c302017-02-16 07:45:03 -0800903 s = format (s, "%=14u%=16U",
904 rx_fib->table_id,
905 format_ip46_address, &proxy->dhcp_src_address, IP46_TYPE_ANY);
906
907 vec_foreach(server, proxy->dhcp_servers)
908 {
909 server_fib = ip4_fib_get(server->server_fib_index);
910 s = format (s, "%u,%U ",
911 server_fib->table_id,
912 format_ip46_address, &server->dhcp_server, IP46_TYPE_ANY);
913 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914 return s;
915}
916
Neale Ranns2dd68522017-02-16 03:38:59 -0800917static int
Neale Ranns3466c302017-02-16 07:45:03 -0800918dhcp4_proxy_show_walk (dhcp_proxy_t *server,
Neale Ranns2dd68522017-02-16 03:38:59 -0800919 void *ctx)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920{
Neale Ranns2dd68522017-02-16 03:38:59 -0800921 vlib_main_t * vm = ctx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922
Neale Ranns2dd68522017-02-16 03:38:59 -0800923 vlib_cli_output (vm, "%U", format_dhcp4_proxy_server, server);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924
Neale Ranns2dd68522017-02-16 03:38:59 -0800925 return (1);
926}
Neale Ranns20a175a2017-02-14 07:28:41 -0800927
Neale Ranns2dd68522017-02-16 03:38:59 -0800928static clib_error_t *
929dhcp4_proxy_show_command_fn (vlib_main_t * vm,
930 unformat_input_t * input,
931 vlib_cli_command_t * cmd)
932{
933 vlib_cli_output (vm, "%U", format_dhcp4_proxy_server, NULL /* header line */);
Neale Ranns20a175a2017-02-14 07:28:41 -0800934
Neale Ranns2dd68522017-02-16 03:38:59 -0800935 dhcp_proxy_walk(FIB_PROTOCOL_IP4, dhcp4_proxy_show_walk, vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936
Neale Ranns2dd68522017-02-16 03:38:59 -0800937 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938}
939
940VLIB_CLI_COMMAND (dhcp_proxy_show_command, static) = {
941 .path = "show dhcp proxy",
942 .short_help = "Display dhcp proxy server info",
Neale Ranns2dd68522017-02-16 03:38:59 -0800943 .function = dhcp4_proxy_show_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944};
945
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946static clib_error_t *
947dhcp_option_82_vss_fn (vlib_main_t * vm,
948 unformat_input_t * input,
949 vlib_cli_command_t * cmd)
950{
951 int is_del = 0, got_new_vpn_id=0;
952 u32 oui=0, fib_id=0, tbl_id=~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953
954 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
955 {
956
957 if (unformat(input, "delete") || unformat(input, "del"))
958 is_del = 1;
959 else if (unformat (input, "oui %d", &oui))
960 got_new_vpn_id = 1;
961 else if (unformat (input, "vpn-id %d", &fib_id))
962 got_new_vpn_id = 1;
963 else if (unformat (input, "table %d", &tbl_id))
964 got_new_vpn_id = 1;
965 else
966 break;
967 }
968 if (tbl_id == ~0)
969 return clib_error_return (0, "no table ID specified.");
970
971 if (is_del || got_new_vpn_id)
972 {
973 int rv;
Neale Ranns2dd68522017-02-16 03:38:59 -0800974 rv = dhcp_proxy_set_vss(FIB_PROTOCOL_IP4, tbl_id, oui, fib_id, is_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 switch (rv)
976 {
977 case 0:
978 return 0;
979
980 case VNET_API_ERROR_NO_SUCH_FIB:
981 return clib_error_return (0, "option 82 vss(oui:%d, vpn-id:%d) not found in table %d",
982 oui, fib_id, tbl_id);
983
984 case VNET_API_ERROR_NO_SUCH_ENTRY:
985 return clib_error_return (0, "option 82 vss for table %d not found in in pool.",
986 tbl_id);
987 default:
988 return clib_error_return (0, "BUG: rv %d", rv);
989 }
990 }
991 else
992 return clib_error_return (0, "parse error`%U'",
993 format_unformat_error, input);
994}
995
996VLIB_CLI_COMMAND (dhcp_proxy_vss_command,static) = {
997 .path = "set dhcp option-82 vss",
998 .short_help = "set dhcp option-82 vss [del] table <table id> oui <oui> vpn-id <vpn-id>",
999 .function = dhcp_option_82_vss_fn,
1000};
1001
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002static clib_error_t *
1003dhcp_vss_show_command_fn (vlib_main_t * vm,
1004 unformat_input_t * input,
1005 vlib_cli_command_t * cmd)
1006
1007{
Neale Ranns2dd68522017-02-16 03:38:59 -08001008 dhcp_vss_walk(FIB_PROTOCOL_IP4, dhcp_vss_show_walk, vm);
Neale Ranns20a175a2017-02-14 07:28:41 -08001009
Neale Ranns2dd68522017-02-16 03:38:59 -08001010 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011}
1012
1013VLIB_CLI_COMMAND (dhcp_proxy_vss_show_command, static) = {
1014 .path = "show dhcp vss",
1015 .short_help = "show dhcp VSS",
1016 .function = dhcp_vss_show_command_fn,
1017};
1018
1019static clib_error_t *
1020dhcp_option_82_address_show_command_fn (vlib_main_t * vm,
1021 unformat_input_t * input,
1022 vlib_cli_command_t * cmd)
1023
1024{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 vnet_main_t *vnm = vnet_get_main();
1026 u32 sw_if_index0=0, sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027 vnet_sw_interface_t *swif;
Neale Ranns2dd68522017-02-16 03:38:59 -08001028 ip4_address_t *ia0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029
1030 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1031 {
1032
1033 if (unformat(input, "%U",
Neale Ranns2dd68522017-02-16 03:38:59 -08001034 unformat_vnet_sw_interface, vnm, &sw_if_index0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035 {
1036 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1037 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1038 swif->unnumbered_sw_if_index : sw_if_index0;
Dave Barach203c6322016-06-26 10:29:03 -04001039 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040 if (ia0)
1041 {
1042 vlib_cli_output (vm, "%=20s%=20s", "interface",
1043 "source IP address");
1044
1045 vlib_cli_output (vm, "%=20U%=20U",
1046 format_vnet_sw_if_index_name,
Neale Ranns2dd68522017-02-16 03:38:59 -08001047 vnm, sw_if_index0,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048 format_ip4_address, ia0);
1049 }
1050 else
1051 vlib_cli_output (vm, "%=34s %=20U",
1052 "No IPv4 address configured on",
1053 format_vnet_sw_if_index_name,
Neale Ranns2dd68522017-02-16 03:38:59 -08001054 vnm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055 }
1056 else
1057 break;
1058 }
1059
1060 return 0;
1061}
1062
1063VLIB_CLI_COMMAND (dhcp_proxy_address_show_command,static) = {
1064 .path = "show dhcp option-82-address interface",
1065 .short_help = "show dhcp option-82-address interface <interface>",
1066 .function = dhcp_option_82_address_show_command_fn,
1067};