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