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