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