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