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