blob: 6a58fcdb1852c9f3dcb53a0b7b01c27d56aa0fdc [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>
20#include <vnet/dhcp/proxy.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/fib/ip4_fib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
23static char * dhcp_proxy_error_strings[] = {
24#define dhcp_proxy_error(n,s) s,
25#include "proxy_error.def"
26#undef dhcp_proxy_error
27};
28
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")
33
34typedef enum {
35#define _(s,n) DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s,
36 foreach_dhcp_proxy_to_server_input_next
37#undef _
38 DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
39} dhcp_proxy_to_server_input_next_t;
40
41typedef struct {
42 /* 0 => to server, 1 => to client */
43 int which;
44 ip4_address_t trace_ip4_address;
45 u32 error;
46 u32 sw_if_index;
47 u32 original_sw_if_index;
48} dhcp_proxy_trace_t;
49
50#define VPP_DHCP_OPTION82_SUB1_SIZE 6
51#define VPP_DHCP_OPTION82_SUB5_SIZE 6
52#define VPP_DHCP_OPTION82_VSS_SIZE 12
53#define VPP_DHCP_OPTION82_SIZE (VPP_DHCP_OPTION82_SUB1_SIZE + \
54 VPP_DHCP_OPTION82_SUB5_SIZE + \
55 VPP_DHCP_OPTION82_VSS_SIZE +3)
56
57vlib_node_registration_t dhcp_proxy_to_server_node;
58vlib_node_registration_t dhcp_proxy_to_client_node;
59
60u8 * format_dhcp_proxy_trace (u8 * s, va_list * args)
61{
62 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
63 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
64 dhcp_proxy_trace_t * t = va_arg (*args, dhcp_proxy_trace_t *);
65
66 if (t->which == 0)
67 s = format (s, "DHCP proxy: sent to server %U\n",
68 format_ip4_address, &t->trace_ip4_address, t->error);
69 else
70 s = format (s, "DHCP proxy: broadcast to client from %U\n",
71 format_ip4_address, &t->trace_ip4_address);
72
73 if (t->error != (u32)~0)
74 s = format (s, " error: %s\n", dhcp_proxy_error_strings[t->error]);
75
76 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
77 t->original_sw_if_index, t->sw_if_index);
78
79 return s;
80}
81
82u8 * format_dhcp_proxy_header_with_length (u8 * s, va_list * args)
83{
84 dhcp_header_t * h = va_arg (*args, dhcp_header_t *);
85 u32 max_header_bytes = va_arg (*args, u32);
86 u32 header_bytes;
87
88 header_bytes = sizeof (h[0]);
89 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
90 return format (s, "dhcp header truncated");
91
92 s = format (s, "DHCP Proxy");
93
94 return s;
95}
96
Ed Warnickecb9cada2015-12-08 15:45:58 -070097static uword
98dhcp_proxy_to_server_input (vlib_main_t * vm,
99 vlib_node_runtime_t * node,
100 vlib_frame_t * from_frame)
101{
102 u32 n_left_from, next_index, * from, * to_next;
103 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
104 from = vlib_frame_vector_args (from_frame);
105 n_left_from = from_frame->n_vectors;
106 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
107 u32 pkts_no_interface_address=0;
108 u32 pkts_too_big=0;
109 ip4_main_t * im = &ip4_main;
110
111 next_index = node->cached_next_index;
112
113 while (n_left_from > 0)
114 {
115 u32 n_left_to_next;
116
117 vlib_get_next_frame (vm, node, next_index,
118 to_next, n_left_to_next);
119
120 while (n_left_from > 0 && n_left_to_next > 0)
121 {
122 u32 bi0;
123 vlib_buffer_t * b0;
124 udp_header_t * u0;
125 dhcp_header_t * h0;
126 ip4_header_t * ip0;
127 u32 next0;
128 u32 old0, new0;
129 ip_csum_t sum0;
130 u32 error0 = (u32) ~0;
131 u32 sw_if_index = 0;
132 u32 original_sw_if_index = 0;
133 u8 *end = NULL;
134 u32 fib_index, server_index;
135 dhcp_server_t * server;
136 u32 rx_sw_if_index;
137
138 bi0 = from[0];
139 to_next[0] = bi0;
140 from += 1;
141 to_next += 1;
142 n_left_from -= 1;
143 n_left_to_next -= 1;
144
145 b0 = vlib_get_buffer (vm, bi0);
146
147 h0 = vlib_buffer_get_current (b0);
148
149 /*
150 * udp_local hands us the DHCP header, need udp hdr,
151 * ip hdr to relay to server
152 */
153 vlib_buffer_advance (b0, -(sizeof(*u0)));
154 u0 = vlib_buffer_get_current (b0);
155
156 /* This blows. Return traffic has src_port = 67, dst_port = 67 */
157 if (u0->src_port == clib_net_to_host_u16(UDP_DST_PORT_dhcp_to_server))
158 {
159 vlib_buffer_advance (b0, sizeof(*u0));
160 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
161 error0 = 0;
162 pkts_to_client++;
163 goto do_enqueue;
164 }
165
166 rx_sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
167
168 fib_index = im->fib_index_by_sw_if_index [rx_sw_if_index];
169
170 if (fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
171 server_index = dpm->dhcp_server_index_by_rx_fib_index[fib_index];
172 else
173 server_index = 0;
174
175 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp_servers,
176 server_index)))
177 {
178 no_server:
179 error0 = DHCP_PROXY_ERROR_NO_SERVER;
180 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
181 pkts_no_server++;
182 goto do_trace;
183 }
184
185 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
186 if (server->valid == 0)
187 goto no_server;
188
189 vlib_buffer_advance (b0, -(sizeof(*ip0)));
190 ip0 = vlib_buffer_get_current (b0);
191
192 /* disable UDP checksum */
193 u0->checksum = 0;
194 sum0 = ip0->checksum;
195 old0 = ip0->dst_address.as_u32;
196 new0 = server->dhcp_server.as_u32;
197 ip0->dst_address.as_u32 = server->dhcp_server.as_u32;
198 sum0 = ip_csum_update (sum0, old0, new0,
199 ip4_header_t /* structure */,
200 dst_address /* changed member */);
201 ip0->checksum = ip_csum_fold (sum0);
202
203 sum0 = ip0->checksum;
204 old0 = ip0->src_address.as_u32;
205 new0 = server->dhcp_src_address.as_u32;
206 ip0->src_address.as_u32 = new0;
207 sum0 = ip_csum_update (sum0, old0, new0,
208 ip4_header_t /* structure */,
209 src_address /* changed member */);
210 ip0->checksum = ip_csum_fold (sum0);
211
212 /* Send to DHCP server via the configured FIB */
213 vnet_buffer(b0)->sw_if_index[VLIB_TX] =
214 server->server_fib_index;
215
216 h0->gateway_ip_address.as_u32 = server->dhcp_src_address.as_u32;
217 pkts_to_server++;
218
219 if (server->insert_option_82)
220 {
221 u32 fib_index, fib_id, opt82_fib_id=0, opt82_oui=0;
222 ip4_fib_t * fib;
223 dhcp_option_t *o = (dhcp_option_t *) h0->options;
224 u32 len = 0;
225 vlib_buffer_free_list_t *fl;
226
227 fib_index = im->fib_index_by_sw_if_index
228 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100229 fib = ip4_fib_get (fib_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 fib_id = fib->table_id;
231
232 end = b0->data + b0->current_data + b0->current_length;
233 /* TLVs are not performance-friendly... */
234 while (o->option != 0xFF /* end of options */ && (u8 *)o < end)
235 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
236
237 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
238 // start write at (option*)o, some packets have padding
239 if (((u8 *)o - (u8 *)b0->data + VPP_DHCP_OPTION82_SIZE) > fl->n_data_bytes)
240 {
241 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
242 pkts_too_big++;
243 goto do_trace;
244 }
245
246 if ((o->option == 0xFF) && ((u8 *)o <= end))
247 {
248 vnet_main_t *vnm = vnet_get_main();
249 u16 old_l0, new_l0;
250 ip4_address_t _ia0, * ia0 = &_ia0;
251 uword *p_vss;
252 vss_info *vss;
253 vnet_sw_interface_t *swif;
254 sw_if_index = 0;
255 original_sw_if_index = 0;
256
257 original_sw_if_index = sw_if_index =
258 vnet_buffer(b0)->sw_if_index[VLIB_RX];
259 swif = vnet_get_sw_interface (vnm, sw_if_index);
260 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
261 sw_if_index = swif->unnumbered_sw_if_index;
262
263 p_vss = hash_get (dpm->opt82vss_index_by_vrf_id,
264 fib_id);
265 if (p_vss)
266 {
267 vss = pool_elt_at_index (dpm->opt82vss, p_vss[0]);
268 opt82_oui = vss->vpn_id.oui;
269 opt82_fib_id = vss->vpn_id.fib_id;
270 }
271 /*
272 * Get the first ip4 address on the [client-side]
273 * RX interface, if not unnumbered. otherwise use
274 * the loopback interface's ip address.
275 */
Dave Barach203c6322016-06-26 10:29:03 -0400276 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
278 if (ia0 == 0)
279 {
280 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
281 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_DROP;
282 pkts_no_interface_address++;
283 goto do_trace;
284 }
285
286 /* Add option 82 */
287 o->option = 82; /* option 82 */
288 o->length = 12; /* 12 octets to follow */
289 o->data[0] = 1; /* suboption 1, circuit ID (=FIB id) */
290 o->data[1] = 4; /* length of suboption */
291 o->data[2] = (original_sw_if_index >> 24) & 0xFF;
292 o->data[3] = (original_sw_if_index >> 16) & 0xFF;
293 o->data[4] = (original_sw_if_index >> 8) & 0xFF;
294 o->data[5] = (original_sw_if_index >> 0) & 0xFF;
295 o->data[6] = 5; /* suboption 5 (client RX intfc address) */
296 o->data[7] = 4; /* length 4 */
297 o->data[8] = ia0->as_u8[0];
298 o->data[9] = ia0->as_u8[1];
299 o->data[10] = ia0->as_u8[2];
300 o->data[11] = ia0->as_u8[3];
301 o->data[12] = 0xFF;
302 if (opt82_oui !=0 || opt82_fib_id != 0)
303 {
304 o->data[12] = 151; /* vss suboption */
305 if (255 == opt82_fib_id) {
306 o->data[13] = 1; /* length */
307 o->data[14] = 255; /* vss option type */
308 o->data[15] = 152; /* vss control suboption */
309 o->data[16] = 0; /* length */
310 /* and a new "end-of-options" option (0xff) */
311 o->data[17] = 0xFF;
312 o->length += 5;
313 } else {
314 o->data[13] = 8; /* length */
315 o->data[14] = 1; /* vss option type */
316 o->data[15] = (opt82_oui >> 16) & 0xff;
317 o->data[16] = (opt82_oui >> 8) & 0xff;
318 o->data[17] = (opt82_oui ) & 0xff;
319 o->data[18] = (opt82_fib_id >> 24) & 0xff;
320 o->data[19] = (opt82_fib_id >> 16) & 0xff;
321 o->data[20] = (opt82_fib_id >> 8) & 0xff;
322 o->data[21] = (opt82_fib_id) & 0xff;
323 o->data[22] = 152; /* vss control suboption */
324 o->data[23] = 0; /* length */
325
326 /* and a new "end-of-options" option (0xff) */
327 o->data[24] = 0xFF;
328 o->length += 12;
329 }
330 }
331
332 len = o->length + 3;
333 b0->current_length += len;
334 /* Fix IP header length and checksum */
335 old_l0 = ip0->length;
336 new_l0 = clib_net_to_host_u16 (old_l0);
337 new_l0 += len;
338 new_l0 = clib_host_to_net_u16 (new_l0);
339 ip0->length = new_l0;
340 sum0 = ip0->checksum;
341 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
342 length /* changed member */);
343 ip0->checksum = ip_csum_fold (sum0);
344
345 /* Fix UDP length */
346 new_l0 = clib_net_to_host_u16 (u0->length);
347 new_l0 += len;
348 u0->length = clib_host_to_net_u16 (new_l0);
349 } else {
350 vlib_node_increment_counter
351 (vm, dhcp_proxy_to_server_node.index,
352 DHCP_PROXY_ERROR_OPTION_82_ERROR, 1);
353 }
354 }
355
356 next0 = DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
357
358 do_trace:
359 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
360 {
361 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
362 b0, sizeof (*tr));
363 tr->which = 0; /* to server */
364 tr->error = error0;
365 tr->original_sw_if_index = original_sw_if_index;
366 tr->sw_if_index = sw_if_index;
367 if (next0 == DHCP_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP)
368 tr->trace_ip4_address.as_u32 = server->dhcp_server.as_u32;
369 }
370
371 do_enqueue:
372 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
373 to_next, n_left_to_next,
374 bi0, next0);
375 }
376
377 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
378 }
379
380 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
381 DHCP_PROXY_ERROR_RELAY_TO_CLIENT,
382 pkts_to_client);
383 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
384 DHCP_PROXY_ERROR_RELAY_TO_SERVER,
385 pkts_to_server);
386 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
387 DHCP_PROXY_ERROR_NO_SERVER,
388 pkts_no_server);
389 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
390 DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS,
391 pkts_no_interface_address);
392 vlib_node_increment_counter (vm, dhcp_proxy_to_server_node.index,
393 DHCP_PROXY_ERROR_PKT_TOO_BIG,
394 pkts_too_big);
395 return from_frame->n_vectors;
396}
397
398VLIB_REGISTER_NODE (dhcp_proxy_to_server_node) = {
399 .function = dhcp_proxy_to_server_input,
400 .name = "dhcp-proxy-to-server",
401 /* Takes a vector of packets. */
402 .vector_size = sizeof (u32),
403
404 .n_errors = DHCP_PROXY_N_ERROR,
405 .error_strings = dhcp_proxy_error_strings,
406
407 .n_next_nodes = DHCP_PROXY_TO_SERVER_INPUT_N_NEXT,
408 .next_nodes = {
409#define _(s,n) [DHCP_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
410 foreach_dhcp_proxy_to_server_input_next
411#undef _
412 },
413
414 .format_buffer = format_dhcp_proxy_header_with_length,
415 .format_trace = format_dhcp_proxy_trace,
416#if 0
417 .unformat_buffer = unformat_dhcp_proxy_header,
418#endif
419};
420
421static uword
422dhcp_proxy_to_client_input (vlib_main_t * vm,
423 vlib_node_runtime_t * node,
424 vlib_frame_t * from_frame)
425{
426 u32 n_left_from, * from;
427 ethernet_main_t *em = ethernet_get_main (vm);
428 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
429 vnet_main_t * vnm = vnet_get_main();
430 ip4_main_t * im = &ip4_main;
431
432 from = vlib_frame_vector_args (from_frame);
433 n_left_from = from_frame->n_vectors;
434
435 while (n_left_from > 0)
436 {
437 u32 bi0;
438 vlib_buffer_t * b0;
439 udp_header_t * u0;
440 dhcp_header_t * h0;
441 ip4_header_t * ip0 = 0;
442 ip4_address_t * ia0 = 0;
443 u32 old0, new0;
444 ip_csum_t sum0;
445 ethernet_interface_t *ei0;
446 ethernet_header_t *mac0;
447 vnet_hw_interface_t *hi0;
448 vlib_frame_t *f0;
449 u32 * to_next0;
450 u32 sw_if_index = ~0;
451 vnet_sw_interface_t *si0;
452 u32 error0 = (u32)~0;
453 vnet_sw_interface_t *swif;
454 u32 server_index;
455 u32 fib_index;
456 dhcp_server_t * server;
457 u32 original_sw_if_index = (u32) ~0;
458
459 bi0 = from[0];
460 from += 1;
461 n_left_from -= 1;
462
463 b0 = vlib_get_buffer (vm, bi0);
464 h0 = vlib_buffer_get_current (b0);
465
466 /*
467 * udp_local hands us the DHCP header, need udp hdr,
468 * ip hdr to relay to client
469 */
470 vlib_buffer_advance (b0, -(sizeof(*u0)));
471 u0 = vlib_buffer_get_current (b0);
472
473 vlib_buffer_advance (b0, -(sizeof(*ip0)));
474 ip0 = vlib_buffer_get_current (b0);
475
476 /* Consumed by dhcp client code? */
477 if (dhcp_client_for_us (bi0, b0, ip0, u0, h0))
478 continue;
479
480 if (1 /* dpm->insert_option_82 */)
481 {
482 dhcp_option_t *o = (dhcp_option_t *) h0->options;
483 dhcp_option_t *sub;
484
485 /* Parse through TLVs looking for option 82.
486 The circuit-ID is the FIB number we need
487 to track down the client-facing interface */
488
489 while (o->option != 0xFF /* end of options */ &&
490 (u8 *) o < (b0->data + b0->current_data + b0->current_length))
491 {
492 if (o->option == 82)
493 {
494 u32 vss_exist = 0;
495 u32 vss_ctrl = 0;
496 sub = (dhcp_option_t *) &o->data[0];
497 while (sub->option != 0xFF /* end of options */ &&
498 (u8 *) sub < (u8 *)(o + o->length)) {
499 /* If this is one of ours, it will have
500 total length 12, circuit-id suboption type,
501 and the sw_if_index */
502 if (sub->option == 1 && sub->length == 4)
503 {
504 sw_if_index = (o->data[2] << 24)
505 | (o->data[3] << 16)
506 | (o->data[4] << 8)
507 | (o->data[5]);
508 } else if (sub->option == 151 &&
509 sub->length == 7 &&
510 sub->data[0] == 1)
511 vss_exist = 1;
512 else if (sub->option == 152 && sub->length == 0)
513 vss_ctrl = 1;
514 sub = (dhcp_option_t *)
515 (((uword) sub) + (sub->length + 2));
516 }
517 if (vss_ctrl && vss_exist)
518 vlib_node_increment_counter
519 (vm, dhcp_proxy_to_client_node.index,
520 DHCP_PROXY_ERROR_OPTION_82_VSS_NOT_PROCESSED, 1);
521
522 }
523 o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
524 }
525 }
526
527 if (sw_if_index == (u32)~0)
528 {
529 error0 = DHCP_PROXY_ERROR_NO_OPTION_82;
530
531 drop_packet:
532 vlib_node_increment_counter (vm, dhcp_proxy_to_client_node.index,
533 error0, 1);
534 f0 = vlib_get_frame_to_node (vm, dpm->error_drop_node_index);
535 to_next0 = vlib_frame_vector_args (f0);
536 to_next0[0] = bi0;
537 f0->n_vectors = 1;
538 vlib_put_frame_to_node (vm, dpm->error_drop_node_index, f0);
539 goto do_trace;
540 }
541
542
543 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
544 {
545 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82;
546 goto drop_packet;
547 }
548
549 fib_index = im->fib_index_by_sw_if_index [sw_if_index];
550
551 if (fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
552 server_index = dpm->dhcp_server_index_by_rx_fib_index[fib_index];
553 else
554 server_index = 0;
555
556 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp_servers,
557 server_index)))
558 {
559 error0 = DHCP_PROXY_ERROR_BAD_OPTION_82;
560 goto drop_packet;
561 }
562
563 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
564 if (server->valid == 0)
565 {
566 error0 = DHCP_PROXY_ERROR_NO_SERVER;
567 goto drop_packet;
568 }
569
570 if (ip0->src_address.as_u32 != server->dhcp_server.as_u32)
571 {
572 error0 = DHCP_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
573 goto drop_packet;
574 }
575
576 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
577
578 swif = vnet_get_sw_interface (vnm, sw_if_index);
579 original_sw_if_index = sw_if_index;
580 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
581 sw_if_index = swif->unnumbered_sw_if_index;
582
Dave Barach203c6322016-06-26 10:29:03 -0400583 ia0 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584 if (ia0 == 0)
585 {
586 error0 = DHCP_PROXY_ERROR_NO_INTERFACE_ADDRESS;
587 goto drop_packet;
588 }
589
590 u0->checksum = 0;
591 u0->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client);
592 sum0 = ip0->checksum;
593 old0 = ip0->dst_address.as_u32;
594 new0 = 0xFFFFFFFF;
595 ip0->dst_address.as_u32 = new0;
596 sum0 = ip_csum_update (sum0, old0, new0,
597 ip4_header_t /* structure */,
598 dst_address /* offset of changed member */);
599 ip0->checksum = ip_csum_fold (sum0);
600
601 sum0 = ip0->checksum;
602 old0 = ip0->src_address.as_u32;
603 new0 = ia0->as_u32;
604 ip0->src_address.as_u32 = new0;
605 sum0 = ip_csum_update (sum0, old0, new0,
606 ip4_header_t /* structure */,
607 src_address /* offset of changed member */);
608 ip0->checksum = ip_csum_fold (sum0);
609
610 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
611 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
612 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
613 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
614
615 mac0 = vlib_buffer_get_current (b0);
616
617 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
618 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
Damjan Marionf1213b82016-03-13 02:22:06 +0100619 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620 memset (mac0->dst_address, 0xff, sizeof (mac0->dst_address));
621 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
622 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x0800);
623
624 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
625 {
626 u32 * vlan_tag = (u32 *)(mac0+1);
627 u32 tmp;
628 tmp = (si0->sub.id << 16) | 0x0800;
629 *vlan_tag = clib_host_to_net_u32 (tmp);
630 }
631
632 /* $$$ This needs to be rewritten, for sure */
633 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
634 to_next0 = vlib_frame_vector_args (f0);
635 to_next0[0] = bi0;
636 f0->n_vectors = 1;
637 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
638
639 do_trace:
640 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
641 {
642 dhcp_proxy_trace_t *tr = vlib_add_trace (vm, node,
643 b0, sizeof (*tr));
644 tr->which = 1; /* to client */
645 tr->trace_ip4_address.as_u32 = ia0 ? ia0->as_u32 : 0;
646 tr->error = error0;
647 tr->original_sw_if_index = original_sw_if_index;
648 tr->sw_if_index = sw_if_index;
649 }
650 }
651 return from_frame->n_vectors;
652}
653
654VLIB_REGISTER_NODE (dhcp_proxy_to_client_node) = {
655 .function = dhcp_proxy_to_client_input,
656 .name = "dhcp-proxy-to-client",
657 /* Takes a vector of packets. */
658 .vector_size = sizeof (u32),
659
660 .n_errors = DHCP_PROXY_N_ERROR,
661 .error_strings = dhcp_proxy_error_strings,
662 .format_buffer = format_dhcp_proxy_header_with_length,
663 .format_trace = format_dhcp_proxy_trace,
664#if 0
665 .unformat_buffer = unformat_dhcp_proxy_header,
666#endif
667};
668
669clib_error_t * dhcp_proxy_init (vlib_main_t * vm)
670{
671 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
672 vlib_node_t * error_drop_node;
673 dhcp_server_t * server;
674
675 dm->vlib_main = vm;
676 dm->vnet_main = vnet_get_main();
677 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
678 dm->error_drop_node_index = error_drop_node->index;
679
680 dm->opt82vss_index_by_vrf_id = hash_create (0, sizeof (uword));
681
682 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
683 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */);
684
685 udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
686 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */);
687
688 /* Create the default server, don't mark it valid */
689 pool_get (dm->dhcp_servers, server);
690 memset (server, 0, sizeof (*server));
691
692 return 0;
693}
694
695VLIB_INIT_FUNCTION (dhcp_proxy_init);
696
697int dhcp_proxy_set_server_2 (ip4_address_t *addr, ip4_address_t *src_address,
698 u32 rx_fib_id,
699 u32 server_fib_id,
700 int insert_option_82, int is_del)
701{
702 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703 dhcp_server_t * server = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 u32 server_index = 0;
705 u32 rx_fib_index = 0;
Neale Rannsfca0c242017-01-13 07:57:46 -0800706 const fib_prefix_t all_1s =
707 {
708 .fp_len = 32,
709 .fp_addr.ip4.as_u32 = 0xffffffff,
710 .fp_proto = FIB_PROTOCOL_IP4,
711 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712
713 if (addr->as_u32 == 0)
714 return VNET_API_ERROR_INVALID_DST_ADDRESS;
715
716 if (src_address->as_u32 == 0)
717 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
718
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100719 rx_fib_index = fib_table_find_or_create_and_lock(FIB_PROTOCOL_IP4,
720 rx_fib_id);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100721
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722 if (rx_fib_id == 0)
723 {
724 server = pool_elt_at_index (dpm->dhcp_servers, 0);
725
726 if (is_del)
727 {
728 memset (server, 0, sizeof (*server));
Neale Rannsfca0c242017-01-13 07:57:46 -0800729 fib_table_entry_special_remove(rx_fib_index,
730 &all_1s,
731 FIB_SOURCE_DHCP);
732 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 }
Neale Rannsfca0c242017-01-13 07:57:46 -0800734 if (!server->valid)
735 fib_table_entry_special_add(rx_fib_index,
736 &all_1s,
737 FIB_SOURCE_DHCP,
738 FIB_ENTRY_FLAG_LOCAL,
739 ADJ_INDEX_INVALID);
740
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741 goto initialize_it;
742 }
743
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 if (is_del)
745 {
746 if (rx_fib_index >= vec_len(dpm->dhcp_server_index_by_rx_fib_index))
747 return VNET_API_ERROR_NO_SUCH_ENTRY;
748
749 server_index = dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index];
750 ASSERT(server_index > 0);
751
752 /* Use the default server again. */
753 dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index] = 0;
754 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
755 memset (server, 0, sizeof (*server));
756 pool_put (dpm->dhcp_servers, server);
Neale Rannsfca0c242017-01-13 07:57:46 -0800757
758 fib_table_entry_special_remove(rx_fib_index,
759 &all_1s,
760 FIB_SOURCE_DHCP);
761
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762 return 0;
763 }
764
765 if (rx_fib_index < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
766 {
767 server_index = dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index];
768 if (server_index != 0)
769 {
770 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
771 goto initialize_it;
772 }
773 }
774
775 pool_get (dpm->dhcp_servers, server);
Neale Rannsfca0c242017-01-13 07:57:46 -0800776
777 fib_table_entry_special_add(rx_fib_index,
778 &all_1s,
779 FIB_SOURCE_DHCP,
780 FIB_ENTRY_FLAG_LOCAL,
781 ADJ_INDEX_INVALID);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782
783 initialize_it:
784
Neale Rannsfca0c242017-01-13 07:57:46 -0800785
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786 server->dhcp_server.as_u32 = addr->as_u32;
Igor Mikhailov (imichail)2138b352016-11-09 12:38:33 -0800787 server->server_fib_index =
788 fib_table_find_or_create_and_lock(FIB_PROTOCOL_IP4,
789 server_fib_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 server->dhcp_src_address.as_u32 = src_address->as_u32;
791 server->insert_option_82 = insert_option_82;
792 server->valid = 1;
793 if (rx_fib_index)
794 {
795 vec_validate (dpm->dhcp_server_index_by_rx_fib_index, rx_fib_index);
796 dpm->dhcp_server_index_by_rx_fib_index[rx_fib_index] =
797 server - dpm->dhcp_servers;
798 }
799
800 return 0;
801}
802
803/* Old API, manipulates the default server (only) */
804int dhcp_proxy_set_server (ip4_address_t *addr, ip4_address_t *src_address,
805 u32 fib_id, int insert_option_82, int is_del)
806{
807 return dhcp_proxy_set_server_2 (addr, src_address, 0 /* rx_fib_id */,
808 fib_id /* server_fib_id */,
809 insert_option_82, is_del);
810}
811
812
813static clib_error_t *
814dhcp_proxy_set_command_fn (vlib_main_t * vm,
815 unformat_input_t * input,
816 vlib_cli_command_t * cmd)
817{
818 ip4_address_t server_addr, src_addr;
819 u32 server_fib_id = 0, rx_fib_id = 0;
820 int is_del = 0;
821 int add_option_82 = 0;
822 int set_src = 0, set_server = 0;
823
824 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
825 {
826 if (unformat (input, "server %U",
827 unformat_ip4_address, &server_addr))
828 set_server = 1;
829 else if (unformat (input, "server-fib-id %d", &server_fib_id))
830 ;
831 else if (unformat (input, "rx-fib-id %d", &rx_fib_id))
832 ;
833 else if (unformat(input, "src-address %U",
834 unformat_ip4_address, &src_addr))
835 set_src = 1;
836 else if (unformat (input, "add-option-82")
837 || unformat (input, "insert-option-82"))
838 add_option_82 = 1;
839 else if (unformat (input, "delete") ||
840 unformat (input, "del"))
841 is_del = 1;
842 else
843 break;
844 }
845
846 if (is_del || (set_server && set_src))
847 {
848 int rv;
849
850 rv = dhcp_proxy_set_server_2 (&server_addr, &src_addr, rx_fib_id,
851 server_fib_id, add_option_82, is_del);
852 switch (rv)
853 {
854 case 0:
855 return 0;
856
857 case VNET_API_ERROR_INVALID_DST_ADDRESS:
858 return clib_error_return (0, "Invalid server address");
859
860 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
861 return clib_error_return (0, "Invalid src address");
862
863 case VNET_API_ERROR_NO_SUCH_INNER_FIB:
864 return clib_error_return (0, "No such rx fib id %d", rx_fib_id);
865
866 case VNET_API_ERROR_NO_SUCH_FIB:
867 return clib_error_return (0, "No such server fib id %d",
868 server_fib_id);
869
870 case VNET_API_ERROR_NO_SUCH_ENTRY:
871 return clib_error_return
872 (0, "Fib id %d: no per-fib DHCP server configured", rx_fib_id);
873
874 default:
875 return clib_error_return (0, "BUG: rv %d", rv);
876 }
877 }
878 else
879 return clib_error_return (0, "parse error`%U'",
880 format_unformat_error, input);
881}
882
883VLIB_CLI_COMMAND (dhcp_proxy_set_command, static) = {
884 .path = "set dhcp proxy",
885 .short_help = "set dhcp proxy [del] server <ip-addr> src-address <ip-addr> [add-option-82] [server-fib-id <n>] [rx-fib-id <n>]",
886 .function = dhcp_proxy_set_command_fn,
887};
888
889u8 * format_dhcp_proxy_server (u8 * s, va_list * args)
890{
891 dhcp_proxy_main_t * dm = va_arg (*args, dhcp_proxy_main_t *);
892 dhcp_server_t * server = va_arg (*args, dhcp_server_t *);
893 u32 rx_fib_index = va_arg (*args, u32);
894 ip4_fib_t * rx_fib, * server_fib;
895 u32 server_fib_id = ~0, rx_fib_id = ~0;
896
897 if (dm == 0)
898 {
899 s = format (s, "%=16s%=16s%=14s%=14s%=20s", "Server", "Src Address",
900 "Server FIB", "RX FIB", "Insert Option 82");
901 return s;
902 }
903
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100904 server_fib = ip4_fib_get(server->server_fib_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905
906 if (server_fib)
907 server_fib_id = server_fib->table_id;
908
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100909 rx_fib = ip4_fib_get(rx_fib_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910
911 if (rx_fib)
912 rx_fib_id = rx_fib->table_id;
913
914 s = format (s, "%=16U%=16U%=14u%=14u%=20s",
915 format_ip4_address, &server->dhcp_server,
916 format_ip4_address, &server->dhcp_src_address,
917 server_fib_id, rx_fib_id,
918 server->insert_option_82 ? "yes" : "no");
919 return s;
920}
921
922static clib_error_t *
923dhcp_proxy_show_command_fn (vlib_main_t * vm,
924 unformat_input_t * input,
925 vlib_cli_command_t * cmd)
926{
927 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
928 ip4_main_t * im = &ip4_main;
929 dhcp_server_t * server;
930 u32 server_index;
931 int i;
932
933 vlib_cli_output (vm, "%U", format_dhcp_proxy_server, 0 /* header line */,
934 0, 0);
935
936 for (i = 0; i < vec_len (im->fibs); i++)
937 {
938 if (i < vec_len(dpm->dhcp_server_index_by_rx_fib_index))
939 server_index = dpm->dhcp_server_index_by_rx_fib_index[i];
940 else
941 server_index = 0;
942 server = pool_elt_at_index (dpm->dhcp_servers, server_index);
943 if (server->valid)
944 vlib_cli_output (vm, "%U", format_dhcp_proxy_server, dpm,
945 server, i);
946 }
947
948 return 0;
949}
950
951VLIB_CLI_COMMAND (dhcp_proxy_show_command, static) = {
952 .path = "show dhcp proxy",
953 .short_help = "Display dhcp proxy server info",
954 .function = dhcp_proxy_show_command_fn,
955};
956
957
958int dhcp_proxy_set_option82_vss( u32 vrf_id,
959 u32 oui,
960 u32 fib_id,
961 int is_del)
962{
963 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
964 uword *p;
965 vss_info *a;
966 u32 old_oui=0, old_fib_id=0;
967
968 p = hash_get (dm->opt82vss_index_by_vrf_id, vrf_id);
969
970 if (p)
971 {
972 a = pool_elt_at_index (dm->opt82vss, p[0]);
973 if (!a)
974 return VNET_API_ERROR_NO_SUCH_FIB;
975 old_oui = a->vpn_id.oui;
976 old_fib_id = a->vpn_id.fib_id;
977
978 if (is_del)
979 {
980 if (old_oui == oui &&
981 old_fib_id == fib_id)
982 {
983 pool_put(dm->opt82vss, a);
984 hash_unset (dm->opt82vss_index_by_vrf_id, vrf_id);
985 return 0;
986 }
987 else
988 return VNET_API_ERROR_NO_SUCH_ENTRY;
989 }
990 pool_put(dm->opt82vss, a);
991 hash_unset (dm->opt82vss_index_by_vrf_id, vrf_id);
992 } else if (is_del)
993 return VNET_API_ERROR_NO_SUCH_ENTRY;
994 pool_get (dm->opt82vss, a);
995 memset (a, ~0, sizeof (a[0]));
996 a->vpn_id.oui = oui;
997 a->vpn_id.fib_id = fib_id;
998 hash_set (dm->opt82vss_index_by_vrf_id, vrf_id, a - dm->opt82vss);
999
1000 return 0;
1001}
1002
1003static clib_error_t *
1004dhcp_option_82_vss_fn (vlib_main_t * vm,
1005 unformat_input_t * input,
1006 vlib_cli_command_t * cmd)
1007{
1008 int is_del = 0, got_new_vpn_id=0;
1009 u32 oui=0, fib_id=0, tbl_id=~0;
1010
1011
1012 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1013 {
1014
1015 if (unformat(input, "delete") || unformat(input, "del"))
1016 is_del = 1;
1017 else if (unformat (input, "oui %d", &oui))
1018 got_new_vpn_id = 1;
1019 else if (unformat (input, "vpn-id %d", &fib_id))
1020 got_new_vpn_id = 1;
1021 else if (unformat (input, "table %d", &tbl_id))
1022 got_new_vpn_id = 1;
1023 else
1024 break;
1025 }
1026 if (tbl_id == ~0)
1027 return clib_error_return (0, "no table ID specified.");
1028
1029 if (is_del || got_new_vpn_id)
1030 {
1031 int rv;
1032 rv = dhcp_proxy_set_option82_vss(tbl_id, oui, fib_id, is_del);
1033 switch (rv)
1034 {
1035 case 0:
1036 return 0;
1037
1038 case VNET_API_ERROR_NO_SUCH_FIB:
1039 return clib_error_return (0, "option 82 vss(oui:%d, vpn-id:%d) not found in table %d",
1040 oui, fib_id, tbl_id);
1041
1042 case VNET_API_ERROR_NO_SUCH_ENTRY:
1043 return clib_error_return (0, "option 82 vss for table %d not found in in pool.",
1044 tbl_id);
1045 default:
1046 return clib_error_return (0, "BUG: rv %d", rv);
1047 }
1048 }
1049 else
1050 return clib_error_return (0, "parse error`%U'",
1051 format_unformat_error, input);
1052}
1053
1054VLIB_CLI_COMMAND (dhcp_proxy_vss_command,static) = {
1055 .path = "set dhcp option-82 vss",
1056 .short_help = "set dhcp option-82 vss [del] table <table id> oui <oui> vpn-id <vpn-id>",
1057 .function = dhcp_option_82_vss_fn,
1058};
1059
1060
1061static clib_error_t *
1062dhcp_vss_show_command_fn (vlib_main_t * vm,
1063 unformat_input_t * input,
1064 vlib_cli_command_t * cmd)
1065
1066{
1067 dhcp_proxy_main_t * dm = &dhcp_proxy_main;
1068 vss_info *v;
1069 u32 oui;
1070 u32 fib_id;
1071 u32 tbl_id;
1072 uword index;
1073
1074 vlib_cli_output (vm, "%=9s%=11s%=12s","Table", "OUI", "VPN-ID");
1075 hash_foreach (tbl_id, index, dm->opt82vss_index_by_vrf_id,
1076 ({
1077 v = pool_elt_at_index (dm->opt82vss, index);
1078 oui = v->vpn_id.oui;
1079 fib_id = v->vpn_id.fib_id;
1080 vlib_cli_output (vm, "%=9d 0x%08x%=12d",
1081 tbl_id, oui, fib_id);
1082 }));
1083
1084 return 0;
1085}
1086
1087VLIB_CLI_COMMAND (dhcp_proxy_vss_show_command, static) = {
1088 .path = "show dhcp vss",
1089 .short_help = "show dhcp VSS",
1090 .function = dhcp_vss_show_command_fn,
1091};
1092
1093static clib_error_t *
1094dhcp_option_82_address_show_command_fn (vlib_main_t * vm,
1095 unformat_input_t * input,
1096 vlib_cli_command_t * cmd)
1097
1098{
1099 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
1100 vnet_main_t *vnm = vnet_get_main();
1101 u32 sw_if_index0=0, sw_if_index;
1102 ip4_address_t *ia0;
1103 vnet_sw_interface_t *swif;
1104
1105 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
1106 {
1107
1108 if (unformat(input, "%U",
1109 unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1110 {
1111 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1112 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1113 swif->unnumbered_sw_if_index : sw_if_index0;
Dave Barach203c6322016-06-26 10:29:03 -04001114 ia0 = ip4_interface_first_address(&ip4_main, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115 if (ia0)
1116 {
1117 vlib_cli_output (vm, "%=20s%=20s", "interface",
1118 "source IP address");
1119
1120 vlib_cli_output (vm, "%=20U%=20U",
1121 format_vnet_sw_if_index_name,
1122 dm->vnet_main, sw_if_index0,
1123 format_ip4_address, ia0);
1124 }
1125 else
1126 vlib_cli_output (vm, "%=34s %=20U",
1127 "No IPv4 address configured on",
1128 format_vnet_sw_if_index_name,
1129 dm->vnet_main, sw_if_index);
1130 }
1131 else
1132 break;
1133 }
1134
1135 return 0;
1136}
1137
1138VLIB_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,
1142};