Initial commit of vpp code.
[vpp.git] / vnet / vnet / dhcpv6 / proxy_node.c
1 /*
2  * proxy_node.c: dhcpv6 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/dhcpv6/proxy.h>
21
22 static char * dhcpv6_proxy_error_strings[] = {
23 #define dhcpv6_proxy_error(n,s) s,
24 #include "proxy_error.def"
25 #undef dhcpv6_proxy_error
26 };
27
28 #define foreach_dhcpv6_proxy_to_server_input_next \
29   _ (DROP, "error-drop")                        \
30   _ (LOOKUP, "ip6-lookup")                      \
31   _ (SEND_TO_CLIENT, "dhcpv6-proxy-to-client")
32   
33
34 typedef enum {
35 #define _(s,n) DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s,
36   foreach_dhcpv6_proxy_to_server_input_next
37 #undef _
38   DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
39 } dhcpv6_proxy_to_server_input_next_t;
40
41 typedef struct {
42   /* 0 => to server, 1 => to client */
43   int which; 
44   u8 packet_data[64]; 
45   u32 error;
46   u32 sw_if_index;
47   u32 original_sw_if_index;
48 } dhcpv6_proxy_trace_t;
49
50 vlib_node_registration_t dhcpv6_proxy_to_server_node;
51 vlib_node_registration_t dhcpv6_proxy_to_client_node;
52
53
54 u8 * format_dhcpv6_proxy_trace (u8 * s, va_list * args)
55 {
56   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
57   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
58   dhcpv6_proxy_trace_t * t = va_arg (*args, dhcpv6_proxy_trace_t *);
59     
60   if (t->which == 0)
61     s = format (s, "DHCPV6 proxy: sent to server %U",
62                 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
63   else
64     s = format (s, "DHCPV6 proxy: sent to client from %U",
65                 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
66   if (t->error != (u32)~0)
67     s = format (s, " error: %s\n", dhcpv6_proxy_error_strings[t->error]);
68
69   s = format (s, "  original_sw_if_index: %d, sw_if_index: %d\n",
70               t->original_sw_if_index, t->sw_if_index);
71   
72   return s;
73 }
74
75 u8 * format_dhcpv6_proxy_header_with_length (u8 * s, va_list * args)
76 {
77   dhcpv6_header_t * h = va_arg (*args, dhcpv6_header_t *);
78   u32 max_header_bytes = va_arg (*args, u32);
79   u32 header_bytes;
80
81   header_bytes = sizeof (h[0]);
82   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
83     return format (s, "dhcpv6 header truncated");
84
85   s = format (s, "DHCPV6 Proxy");
86
87   return s;
88 }
89 /* get first interface address */
90 static ip6_address_t *
91 ip6_interface_first_global_or_site_address (ip6_main_t * im, u32 sw_if_index)
92 {
93   ip_lookup_main_t * lm = &im->lookup_main;
94   ip_interface_address_t * ia = 0;
95   ip6_address_t * result = 0;
96
97   foreach_ip_interface_address (lm, ia, sw_if_index,
98                                 1 /* honor unnumbered */,
99   ({
100     ip6_address_t * a = ip_interface_address_get_address (lm, ia);
101     if ((a->as_u8[0] & 0xe0) == 0x20 ||
102         (a->as_u8[0] & 0xfe) == 0xfc)  {
103         result = a;
104         break;
105     }
106   }));
107   return result;
108 }
109
110 /* get first interface address */
111 static ip6_address_t *
112 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
113 {
114   ip_lookup_main_t * lm = &im->lookup_main;
115   ip_interface_address_t * ia = 0;
116   ip6_address_t * result = 0;
117
118   foreach_ip_interface_address (lm, ia, sw_if_index,
119                                 1 /* honor unnumbered */,
120   ({
121     ip6_address_t * a = ip_interface_address_get_address (lm, ia);
122     result = a;
123     break;
124   }));
125   return result;
126 }
127
128 static inline void copy_ip6_address (ip6_address_t *dst, ip6_address_t *src) 
129 {
130
131   dst->as_u64[0] = src->as_u64[0];
132   dst->as_u64[1] = src->as_u64[1];
133
134
135 static uword
136 dhcpv6_proxy_to_server_input (vlib_main_t * vm,
137                             vlib_node_runtime_t * node,
138                             vlib_frame_t * from_frame)
139 {
140   u32 n_left_from, next_index, * from, * to_next;
141   dhcpv6_proxy_main_t * dpm = &dhcpv6_proxy_main;
142   from = vlib_frame_vector_args (from_frame);
143   n_left_from = from_frame->n_vectors;
144   u32 pkts_to_server=0, pkts_to_client=0;
145   u32 pkts_no_interface_address=0, pkts_no_exceeding_max_hop=0;
146   u32 pkts_no_src_address=0;
147   u32 pkts_wrong_msg_type=0;
148   u32 pkts_too_big=0;
149   ip6_main_t * im = &ip6_main;
150   u32 fib_index=0, fib_id=0;
151   ip6_fib_t * fib;
152   ip6_address_t * src;
153   int bogus_length;
154
155   next_index = node->cached_next_index;
156
157   while (n_left_from > 0)
158     {
159       u32 n_left_to_next;
160
161       vlib_get_next_frame (vm, node, next_index,
162                            to_next, n_left_to_next);
163
164       while (n_left_from > 0 && n_left_to_next > 0)
165         {
166           vnet_main_t *vnm = vnet_get_main();
167           u32 sw_if_index = 0;
168           u32 original_sw_if_index = 0;
169           vnet_sw_interface_t *swif;
170           u32 bi0;
171           vlib_buffer_t * b0;
172           udp_header_t * u0, *u1;
173           dhcpv6_header_t * h0;  // client msg hdr
174           ip6_header_t * ip0, *ip1;
175           ip6_address_t _ia0, *ia0=&_ia0;
176           u32 next0;
177           u32 error0 = (u32) ~0;
178           dhcpv6_option_t *fwd_opt; 
179           dhcpv6_relay_hdr_t *r1;
180           u16 len;
181           dhcpv6_int_id_t *id1;
182           dhcpv6_vss_t *vss1;
183           dhcpv6_client_mac_t *cmac; // client mac
184           ethernet_header_t * e_h0;
185           u8 client_src_mac[6];
186           vlib_buffer_free_list_t *fl;
187           
188           uword *p_vss;
189           u32  oui1=0;
190           u32  fib_id1;
191           dhcpv6_vss_info *vss;
192           
193           fib_id1 = 0;
194           bi0 = from[0];
195           to_next[0] = bi0;
196           from += 1;
197           to_next += 1;
198           n_left_from -= 1;
199           n_left_to_next -= 1;
200
201           b0 = vlib_get_buffer (vm, bi0);
202        
203           h0 = vlib_buffer_get_current (b0);
204           e_h0 = (ethernet_header_t *)b0->data;
205           memcpy(client_src_mac, e_h0->src_address, 6);
206           /* Send to DHCPV6 server via the configured FIB */
207           vnet_buffer(b0)->sw_if_index[VLIB_TX] =
208               dpm->server_fib_index;
209
210           /* 
211            * udp_local hands us the DHCPV6 header.
212            */
213           u0 = (void *)h0 -(sizeof(*u0));
214           ip0 = (void *)u0 -(sizeof(*ip0));
215
216           switch (h0->u.msg_type) {
217             case DHCPV6_MSG_SOLICIT:
218             case DHCPV6_MSG_REQUEST:
219             case DHCPV6_MSG_CONFIRM:
220             case DHCPV6_MSG_RENEW:
221             case DHCPV6_MSG_REBIND:
222             case DHCPV6_MSG_RELEASE:
223             case DHCPV6_MSG_DECLINE:
224             case DHCPV6_MSG_INFORMATION_REQUEST:
225             case DHCPV6_MSG_RELAY_FORW:
226                 /* send to server */
227                 break;
228             case DHCPV6_MSG_RELAY_REPL:
229                 /* send to client */
230                 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
231                 error0 = 0;
232                 pkts_to_client++;
233                 goto do_enqueue;
234             default:
235                 /* drop the packet */
236                 pkts_wrong_msg_type++;
237                 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
238                 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
239                 goto do_trace;
240                 
241           }
242           /* relay-option header pointer */
243           vlib_buffer_advance(b0, -(sizeof(*fwd_opt)));
244           fwd_opt = vlib_buffer_get_current(b0);
245           /* relay message header pointer */
246           vlib_buffer_advance(b0, -(sizeof(*r1)));
247           r1 = vlib_buffer_get_current(b0);
248
249           vlib_buffer_advance(b0, -(sizeof(*u1)));
250           u1 = vlib_buffer_get_current(b0);
251
252           vlib_buffer_advance(b0, -(sizeof(*ip1)));
253           ip1 = vlib_buffer_get_current(b0);
254
255           /* fill in all that rubbish... */
256           len = clib_net_to_host_u16(u0->length) - sizeof(udp_header_t);
257           copy_ip6_address(&r1->peer_addr, &ip0->src_address);
258
259           r1->msg_type = DHCPV6_MSG_RELAY_FORW;
260           fwd_opt->length = clib_host_to_net_u16(len);
261           fwd_opt->option = clib_host_to_net_u16(DHCPV6_OPTION_RELAY_MSG);
262
263           r1->hop_count++;
264           r1->hop_count = (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
265
266           if (PREDICT_FALSE(r1->hop_count >= HOP_COUNT_LIMIT))
267             {
268               error0 =  DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
269               next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
270               pkts_no_exceeding_max_hop++;
271               goto do_trace;
272             }
273               
274
275           /* If relay-fwd and src address is site or global unicast address  */
276           if (h0->u.msg_type == DHCPV6_MSG_RELAY_FORW &&
277               ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
278                (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
279             {
280               /* Set link address to zero */
281               r1->link_addr.as_u64[0] = 0;
282               r1->link_addr.as_u64[1] = 0;
283               goto link_address_set;
284             }
285           
286           /* if receiving interface is unnumbered, use receiving interface
287            * IP address as link address, otherwise use the loopback interface
288            * IP address as link address.
289            */
290           original_sw_if_index = sw_if_index =
291               vnet_buffer(b0)->sw_if_index[VLIB_RX];
292           swif = vnet_get_sw_interface (vnm, sw_if_index);
293           if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
294               sw_if_index = swif->unnumbered_sw_if_index;
295           
296           ia0 = ip6_interface_first_global_or_site_address(&ip6_main, sw_if_index);
297           if (ia0 == 0)
298             {
299               error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
300               next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
301               pkts_no_interface_address++;
302               goto do_trace;
303             }
304           
305           copy_ip6_address(&r1->link_addr, ia0);
306           
307         link_address_set:         
308           fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
309               
310           if ((b0->current_length+sizeof(*id1)+sizeof(*vss1)+sizeof(*cmac))
311               > fl->n_data_bytes)
312             {
313               error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
314               next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
315               pkts_too_big++;
316               goto do_trace;
317             }
318
319           id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
320           b0->current_length += (sizeof (*id1));
321
322           fib_index = im->fib_index_by_sw_if_index 
323               [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
324           fib = vec_elt_at_index (im->fibs, fib_index);
325           fib_id = fib->table_id;
326
327           p_vss = hash_get (dpm->vss_index_by_vrf_id,
328                             fib_id);
329           if (p_vss) 
330             {
331               vss = pool_elt_at_index (dpm->vss, p_vss[0]);
332               oui1 =  vss->vpn_id.oui;
333               fib_id1 =  vss->vpn_id.fib_id;
334             }
335
336           id1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_INTERFACE_ID);
337           id1->opt.length = clib_host_to_net_u16(sizeof(original_sw_if_index));
338           id1->int_idx = clib_host_to_net_u32(original_sw_if_index);
339               
340           u1->length =0;
341           if (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW)
342             {
343                cmac = (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
344                b0->current_length += (sizeof (*cmac));
345                cmac->opt.length =clib_host_to_net_u16(sizeof(*cmac) - 
346                                                       sizeof(cmac->opt));
347                cmac->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
348                cmac->link_type = clib_host_to_net_u16(1); // ethernet
349                memcpy(cmac->data, client_src_mac, 6);
350                u1->length += sizeof(*cmac);
351             }
352           if (oui1 || fib_id1) {
353               vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
354               b0->current_length += (sizeof (*vss1));
355               vss1->opt.length =clib_host_to_net_u16(sizeof(*vss1) - 
356                                                      sizeof(vss1->opt));
357               vss1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_VSS);
358               vss1->data[0] = 1;   // type
359               vss1->data[1] = oui1>>16 & 0xff;
360               vss1->data[2] = oui1>>8  & 0xff;
361               vss1->data[3] = oui1 & 0xff;
362               vss1->data[4] = fib_id1>>24 & 0xff;
363               vss1->data[5] = fib_id1>>16 & 0xff;
364               vss1->data[6] = fib_id1>>8 & 0xff;
365               vss1->data[7] = fib_id1 & 0xff;
366               u1->length += sizeof(*vss1);
367           }
368
369           pkts_to_server++;
370           u1->checksum = 0;
371           u1->src_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_client);
372           u1->dst_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_server);
373
374           u1->length =
375               clib_host_to_net_u16( clib_net_to_host_u16(fwd_opt->length) +
376                                     sizeof(*r1) + sizeof(*fwd_opt) +
377                                     sizeof(*u1) + sizeof(*id1) + u1->length);
378           
379           memset(ip1, 0, sizeof(*ip1));
380           ip1->ip_version_traffic_class_and_flow_label = 0x60;
381           ip1->payload_length =  u1->length;
382           ip1->protocol = PROTO_UDP;
383           ip1->hop_limit = HOP_COUNT_LIMIT;
384           src = (dpm->dhcpv6_server.as_u64[0] || dpm->dhcpv6_server.as_u64[1]) ?
385             &dpm->dhcpv6_server : &dpm->all_dhcpv6_server_address;
386           copy_ip6_address(&ip1->dst_address, src);
387           
388
389           ia0 = ip6_interface_first_global_or_site_address
390               (&ip6_main, vnet_buffer(b0)->sw_if_index[VLIB_RX]);
391
392           src = (dpm->dhcpv6_src_address.as_u64[0] || dpm->dhcpv6_src_address.as_u64[1]) ?
393             &dpm->dhcpv6_src_address : ia0;
394           if (ia0 == 0)
395             {
396               error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS;
397               next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
398               pkts_no_src_address++;
399               goto do_trace;
400             }
401  
402           copy_ip6_address (&ip1->src_address, src);
403           
404
405           u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1, 
406                                                            &bogus_length);
407           ASSERT(bogus_length == 0);
408
409           next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
410
411         do_trace:
412           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
413             {
414                dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node, 
415                                                           b0, sizeof (*tr));
416                tr->which = 0; /* to server */
417                tr->error = error0;
418                tr->original_sw_if_index = original_sw_if_index;
419                tr->sw_if_index = sw_if_index;
420                if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)  
421                  copy_ip6_address((ip6_address_t *)&tr->packet_data[0], &dpm->dhcpv6_server);
422             }
423
424         do_enqueue:
425           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
426                                            to_next, n_left_to_next,
427                                            bi0, next0);
428         }
429
430       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
431     }
432
433   vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
434                                DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
435                                pkts_to_client);
436   vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
437                                DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
438                                pkts_to_server);
439   vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
440                                DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
441                                pkts_no_interface_address);
442   vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
443                                DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
444                                pkts_wrong_msg_type);
445   vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
446                                DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
447                                pkts_no_src_address);
448   vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
449                                DHCPV6_PROXY_ERROR_PKT_TOO_BIG,
450                                pkts_too_big);
451   return from_frame->n_vectors;
452 }
453
454 VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node) = {
455   .function = dhcpv6_proxy_to_server_input,
456   .name = "dhcpv6-proxy-to-server",
457   /* Takes a vector of packets. */
458   .vector_size = sizeof (u32),
459
460   .n_errors = DHCPV6_PROXY_N_ERROR,
461   .error_strings = dhcpv6_proxy_error_strings,
462
463   .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
464   .next_nodes = {
465 #define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
466     foreach_dhcpv6_proxy_to_server_input_next
467 #undef _
468   },
469
470   .format_buffer = format_dhcpv6_proxy_header_with_length,
471   .format_trace = format_dhcpv6_proxy_trace,
472 #if 0
473   .unformat_buffer = unformat_dhcpv6_proxy_header,
474 #endif
475 };
476
477 static uword
478 dhcpv6_proxy_to_client_input (vlib_main_t * vm,
479                             vlib_node_runtime_t * node,
480                             vlib_frame_t * from_frame)
481 {
482
483   u32 n_left_from, * from;
484   ethernet_main_t *em = ethernet_get_main (vm);
485   dhcpv6_proxy_main_t * dpm = &dhcpv6_proxy_main;
486   vnet_main_t * vnm = vnet_get_main(); 
487   int bogus_length;
488   
489   from = vlib_frame_vector_args (from_frame);
490   n_left_from = from_frame->n_vectors;
491
492   while (n_left_from > 0)
493     {
494       u32 bi0;
495       vlib_buffer_t * b0;
496       udp_header_t * u0, *u1=0;
497       dhcpv6_relay_hdr_t * h0;
498       ip6_header_t * ip1 = 0, *ip0;
499       ip6_address_t _ia0, * ia0 = &_ia0;
500       ip6_address_t client_address;
501       ethernet_interface_t *ei0;
502       ethernet_header_t *mac0;
503       vnet_hw_interface_t *hi0;
504       vlib_frame_t *f0;
505       u32 * to_next0;
506       u32 sw_if_index = ~0;
507       u32 original_sw_if_index = ~0;
508       vnet_sw_interface_t *si0;
509       u32 error0 = (u32)~0;
510       vnet_sw_interface_t *swif;
511       dhcpv6_option_t *r0, *o; 
512       u16 len = 0;
513       u32 svr_fib_index, svr_fib_id;
514       ip6_fib_t * svr_fib;
515       ip6_main_t * im = &ip6_main;
516
517       bi0 = from[0];
518       from += 1;
519       n_left_from -= 1;
520       
521       b0 = vlib_get_buffer (vm, bi0);
522       h0 = vlib_buffer_get_current (b0);
523
524       if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
525         {
526           error0 =  DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
527
528         drop_packet:
529           vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
530                                        error0, 1);
531
532           f0 = vlib_get_frame_to_node (vm, dpm->error_drop_node_index);
533           to_next0 = vlib_frame_vector_args (f0);
534           to_next0[0] = bi0;
535           f0->n_vectors = 1;
536           vlib_put_frame_to_node (vm, dpm->error_drop_node_index, f0);
537           goto do_trace;
538         }
539       /* hop count seems not need to be checked */
540       if (HOP_COUNT_LIMIT < h0->hop_count)
541         {
542           error0 =  DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
543           goto drop_packet;
544         }
545       u0 = (void *)h0 -(sizeof(*u0));
546       ip0 = (void *)u0 -(sizeof(*ip0));
547       
548       vlib_buffer_advance (b0, sizeof(*h0));
549       o = r0 = vlib_buffer_get_current (b0);
550       
551       /* Parse through TLVs looking for option 9 (DHCPV6_OPTION_INTERFACE_ID).
552          The interface-ID is the FIB number we need
553          to track down the client-facing interface */
554           
555       while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
556         {
557            if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16(o->option))
558              {
559                 if (clib_net_to_host_u16(o->length) == sizeof(sw_if_index))
560                     sw_if_index = clib_net_to_host_u32(((dhcpv6_int_id_t*)o)->int_idx);
561                 break;
562              } 
563            o = (dhcpv6_option_t *) (((uword) o) + clib_net_to_host_u16(o->length) + sizeof(*o));
564         }
565       
566       if ((u32)~0 == sw_if_index)
567         {
568           error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
569           goto drop_packet;
570         }
571
572       svr_fib_index = im->fib_index_by_sw_if_index
573           [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
574       svr_fib = vec_elt_at_index (im->fibs, svr_fib_index);
575       svr_fib_id = svr_fib->table_id;
576
577       if (svr_fib_id != dpm->server_fib_index ||
578           ip0->src_address.as_u64[0] != dpm->dhcpv6_server.as_u64[0] ||
579           ip0->src_address.as_u64[1] != dpm->dhcpv6_server.as_u64[1])
580         {
581           //drop packet if not from server with configured address or FIB
582           error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
583           goto drop_packet;
584         }
585
586       vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index 
587           = sw_if_index;
588
589       swif = vnet_get_sw_interface (vnm, original_sw_if_index);
590       if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
591           sw_if_index = swif->unnumbered_sw_if_index;
592
593       vlib_buffer_advance (b0, sizeof(*r0));
594       /* 
595        * udp_local hands us the DHCPV6 header, need udp hdr, 
596        * ip hdr to relay to client
597        */
598       vlib_buffer_advance (b0, -(sizeof(*u1)));
599       u1 = vlib_buffer_get_current (b0);
600
601       vlib_buffer_advance (b0, -(sizeof(*ip1)));
602       ip1 = vlib_buffer_get_current (b0);
603
604       copy_ip6_address(&client_address, &h0->peer_addr);
605
606       ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
607       if (ia0 == 0)
608         {
609           error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
610           goto drop_packet;
611         }
612
613       len =  clib_net_to_host_u16(r0->length);
614       memset(ip1, 0, sizeof(*ip1));
615       copy_ip6_address(&ip1->dst_address, &client_address);
616       u1->checksum = 0;
617       u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);     
618       u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
619       u1->length = clib_host_to_net_u16 (len + sizeof(udp_header_t));
620
621       ip1->ip_version_traffic_class_and_flow_label =
622           ip0->ip_version_traffic_class_and_flow_label &
623           0x00000fff;
624       ip1->payload_length =  u1->length;
625       ip1->protocol = PROTO_UDP;
626       ip1->hop_limit = HOP_COUNT_LIMIT;
627       copy_ip6_address(&ip1->src_address, ia0);
628
629       u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1, 
630                                                        &bogus_length);
631       ASSERT(bogus_length == 0);
632              
633       vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
634       si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
635       if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
636           vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
637
638       mac0 = vlib_buffer_get_current (b0);
639
640       hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
641       ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
642       memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
643       memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
644       mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
645         clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x86dd);
646
647       if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
648         {
649           u32 * vlan_tag = (u32 *)(mac0+1);
650           u32 tmp;
651           tmp = (si0->sub.id << 16) | 0x0800;
652           *vlan_tag = clib_host_to_net_u32 (tmp);
653         }
654
655       /* $$$ consider adding a dynamic next to the graph node, for performance */
656       f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
657       to_next0 = vlib_frame_vector_args (f0);
658       to_next0[0] = bi0;
659       f0->n_vectors = 1;
660       vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
661
662     do_trace:
663       if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
664         {
665           dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node, 
666                                                      b0, sizeof (*tr));
667           tr->which = 1; /* to client */
668           if (ia0)
669               copy_ip6_address((ip6_address_t*)tr->packet_data, ia0);
670           tr->error = error0;
671           tr->original_sw_if_index = original_sw_if_index;
672           tr->sw_if_index = sw_if_index;
673         }
674     }
675   return from_frame->n_vectors;
676
677 }
678
679 VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node) = {
680   .function = dhcpv6_proxy_to_client_input,
681   .name = "dhcpv6-proxy-to-client",
682   /* Takes a vector of packets. */
683   .vector_size = sizeof (u32),
684
685   .n_errors = DHCPV6_PROXY_N_ERROR,
686   .error_strings = dhcpv6_proxy_error_strings,
687   .format_buffer = format_dhcpv6_proxy_header_with_length,
688   .format_trace = format_dhcpv6_proxy_trace,
689 #if 0
690   .unformat_buffer = unformat_dhcpv6_proxy_header,
691 #endif
692 };
693
694 clib_error_t * dhcpv6_proxy_init (vlib_main_t * vm)
695 {
696   dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
697   vlib_node_t * error_drop_node;
698
699   dm->vlib_main = vm;
700   dm->vnet_main = vnet_get_main(); 
701   error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
702   dm->error_drop_node_index = error_drop_node->index;
703
704   /* RFC says this is the dhcpv6 server address  */
705   dm->all_dhcpv6_server_address.as_u64[0] = clib_host_to_net_u64 (0xFF05000000000000);
706   dm->all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
707
708   /* RFC says this is the server and agent address */
709   dm->all_dhcpv6_server_relay_agent_address.as_u64[0] = clib_host_to_net_u64 (0xFF02000000000000);
710   dm->all_dhcpv6_server_relay_agent_address.as_u64[1] = clib_host_to_net_u64 (0x00010002);
711
712   udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client, 
713                          dhcpv6_proxy_to_client_node.index, 0 /* is_ip4 */);
714
715   udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server, 
716                          dhcpv6_proxy_to_server_node.index, 0 /* is_ip6 */);
717   return 0;
718 }
719
720 VLIB_INIT_FUNCTION (dhcpv6_proxy_init);
721
722 int dhcpv6_proxy_set_server (ip6_address_t *addr, ip6_address_t *src_address,
723                              u32 fib_id, int insert_vss, int is_del)
724 {
725   dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
726   ip6_main_t * im = &ip6_main;
727   uword * p;
728   
729   
730   if (is_del)
731     {
732       dm->dhcpv6_server.as_u64[0] = 0;
733       dm->dhcpv6_server.as_u64[1] = 0;
734       dm->server_fib_index = 0;
735       dm->dhcpv6_src_address.as_u64[0] = 0;
736       dm->dhcpv6_src_address.as_u64[1] = 0;
737       dm->insert_option = 0;
738       return 0;
739     }
740
741   if (addr->as_u64[0] == 0 &&
742       addr->as_u64[1] == 0 )
743     return VNET_API_ERROR_INVALID_DST_ADDRESS;
744   
745   if (src_address->as_u64[0] == 0 &&
746       src_address->as_u64[1] == 0)
747     return VNET_API_ERROR_INVALID_SRC_ADDRESS;
748
749   p = hash_get (im->fib_index_by_table_id, fib_id);
750   if (p == 0)
751     return VNET_API_ERROR_NO_SUCH_FIB;
752   
753   copy_ip6_address(&dm->dhcpv6_server, addr);
754   dm->server_fib_index = p[0];
755   copy_ip6_address(&dm->dhcpv6_src_address, src_address);
756   dm->insert_option = insert_vss;
757   return 0;
758 }
759
760 static clib_error_t *
761 dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
762                            unformat_input_t * input,
763                            vlib_cli_command_t * cmd)
764 {
765   ip6_address_t addr, src_addr;
766   int set_server = 0, set_src_address = 0, add_opt = 0;
767   u32 fib_id = 0;
768   int is_del = 0;
769   
770   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) 
771     {
772       if (unformat (input, "server %U", 
773                     unformat_ip6_address, &addr)) 
774          set_server = 1;
775       else if (unformat(input, "src-address %U", 
776                         unformat_ip6_address, &src_addr))
777           set_src_address =1;
778        else if (unformat (input, "fib-id %d", &fib_id))
779         ;
780        else if (unformat (input, "add-option")
781                || unformat (input, "insert-option"))
782           add_opt = 1;
783        else if (unformat (input, "delete") ||
784                 unformat (input, "del"))
785            is_del = 1;
786       else
787         break;
788     }
789
790   if (is_del || (set_server && set_src_address))
791   {
792       int rv;
793
794       rv = dhcpv6_proxy_set_server (&addr, &src_addr, fib_id, 
795                                     add_opt, is_del);
796       switch (rv)
797         {
798         case 0:
799           return 0;
800
801         case -1:
802           return clib_error_return (0, "FIB id %d does not exist", fib_id);
803
804         default:
805           return clib_error_return (0, "BUG: rv %d", rv);
806         }
807   }
808   else
809     return clib_error_return (0, "parse error`%U'",
810                               format_unformat_error, input);
811 }
812
813 VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
814   .path = "set dhcpv6 proxy",
815   .short_help = "set dhcpv6 proxy server <ipv6-addr> fib-id <fib-id> src-address <ipv6-addr>",
816   .function = dhcpv6_proxy_set_command_fn,
817 };
818
819 u8 * format_dhcpv6_proxy_server (u8 * s, va_list * args)
820 {
821   dhcpv6_proxy_main_t * dm = va_arg (*args, dhcpv6_proxy_main_t *);
822   ip6_fib_t * f;
823   u32 fib_id = (u32)~0;
824
825   if (dm == 0)
826     {
827       s = format (s, "%=40s%=40s%=14s", "Server Address", "Source Address", 
828                   "Server FIB");
829       return s;
830     }
831
832   f = find_ip6_fib_by_table_index_or_id (&ip6_main, dm->server_fib_index,
833                                          IP6_ROUTE_FLAG_FIB_INDEX);
834   if (f)
835     fib_id = f->table_id;
836
837
838   s = format (s, "%=40U%=40U%=14u",
839               format_ip6_address, &dm->dhcpv6_server,
840               format_ip6_address, &dm->dhcpv6_src_address,
841               fib_id);
842   return s;
843 }
844
845 static clib_error_t *
846 dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
847                             unformat_input_t * input,
848                             vlib_cli_command_t * cmd)
849 {
850   dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
851
852   vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, 0 /* header line */);
853   vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, dm);
854   return 0;
855 }
856
857 VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
858   .path = "show dhcpv6 proxy",
859   .short_help = "Display dhcpv6 proxy info",
860   .function = dhcpv6_proxy_show_command_fn,
861 };
862
863 int dhcpv6_proxy_set_vss(u32 tbl_id,
864                          u32 oui,
865                          u32 fib_id, 
866                          int is_del)
867 {
868   dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
869   u32 old_oui, old_fib_id;
870   uword *p;
871   dhcpv6_vss_info *v;
872
873   p = hash_get (dm->vss_index_by_vrf_id, tbl_id);
874
875   if (p) {
876       v = pool_elt_at_index (dm->vss, p[0]);
877       if (!v)
878         return VNET_API_ERROR_NO_SUCH_FIB;
879       
880       old_oui = v->vpn_id.oui;
881       old_fib_id = v->vpn_id.fib_id;
882
883       if (is_del)
884       {
885           if (old_oui == oui &&
886               old_fib_id == fib_id )
887           {
888               pool_put(dm->vss, v);
889               hash_unset (dm->vss_index_by_vrf_id, tbl_id);
890               return 0;
891           }
892           else
893             return VNET_API_ERROR_NO_SUCH_ENTRY;
894       }
895       
896       pool_put(dm->vss, v);
897       hash_unset (dm->vss_index_by_vrf_id, tbl_id);
898   } else if (is_del)
899     return VNET_API_ERROR_NO_SUCH_ENTRY;
900   
901   pool_get (dm->vss, v);
902   memset (v, ~0, sizeof (*v));
903   v->vpn_id.fib_id = fib_id;
904   v->vpn_id.oui = oui;
905   hash_set (dm->vss_index_by_vrf_id, tbl_id, v - dm->vss);
906   
907   return 0;
908 }
909
910
911 static clib_error_t *
912 dhcpv6_vss_command_fn (vlib_main_t * vm,
913                            unformat_input_t * input,
914                            vlib_cli_command_t * cmd)
915 {
916   int is_del = 0, got_new_vss=0;
917   u32 oui=0;
918   u32 fib_id=0, tbl_id=~0;
919
920   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) 
921     {
922       if (unformat (input, "oui %d", &oui))
923           got_new_vss = 1;
924       else if (unformat (input, "vpn-id %d", &fib_id))
925           got_new_vss = 1;
926       else if (unformat (input, "table %d", &tbl_id))
927           got_new_vss = 1;
928       else if (unformat(input, "delete") || unformat(input, "del"))
929           is_del = 1;
930       else
931           break;
932     }
933
934   if (tbl_id ==~0)
935       return clib_error_return (0, "no table ID specified.");
936
937   if (is_del || got_new_vss)
938     {
939       int rv;
940
941       rv = dhcpv6_proxy_set_vss(tbl_id, oui, fib_id, is_del);
942       switch (rv)
943         {
944         case 0:
945           return 0;
946
947         case VNET_API_ERROR_NO_SUCH_FIB:
948             return clib_error_return (0, "vss info (oui:%d, vpn-id:%d)  not found in table %d.",
949                                       oui, fib_id, tbl_id);
950
951         case VNET_API_ERROR_NO_SUCH_ENTRY:
952             return clib_error_return (0, "vss for table %d not found in pool.",
953                                       tbl_id);
954
955         default:
956           return clib_error_return (0, "BUG: rv %d", rv);
957         }
958     }
959   else
960       return clib_error_return (0, "parse error`%U'",
961                                 format_unformat_error, input);
962   
963 }
964
965 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
966   .path = "set dhcpv6 vss",
967   .short_help = "set dhcpv6 vss table <table-id> oui <oui> vpn-idx <vpn-idx>",
968   .function = dhcpv6_vss_command_fn,
969 };
970
971 static clib_error_t *
972 dhcpv6_vss_show_command_fn (vlib_main_t * vm,
973                             unformat_input_t * input,
974                             vlib_cli_command_t * cmd)
975   
976 {
977   dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
978   dhcpv6_vss_info *v;
979   u32 oui;
980   u32 fib_id;
981   u32 tbl_id;
982   uword index;
983   
984   vlib_cli_output (vm, "%=6s%=6s%=12s","Table", "OUI", "VPN ID");
985   hash_foreach (tbl_id, index, dm->vss_index_by_vrf_id,
986   ({
987      v = pool_elt_at_index (dm->vss, index);
988      oui = v->vpn_id.oui;
989      fib_id = v->vpn_id.fib_id;
990      vlib_cli_output (vm, "%=6d%=6d%=12d",
991                       tbl_id, oui, fib_id);
992   }));
993   
994   return 0;
995 }
996
997 VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
998   .path = "show dhcpv6 vss",
999   .short_help = "show dhcpv6 VSS",
1000   .function = dhcpv6_vss_show_command_fn,
1001 };
1002
1003 static clib_error_t *
1004 dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1005                                 unformat_input_t * input,
1006                                 vlib_cli_command_t * cmd)
1007   
1008 {
1009   dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1010   vnet_main_t *vnm = vnet_get_main();                                     
1011   u32 sw_if_index0=0, sw_if_index;
1012   ip6_address_t *ia0;
1013   vnet_sw_interface_t *swif;
1014   
1015   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) 
1016     {
1017
1018       if (unformat(input, "%U",
1019                    unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1020         {
1021             swif = vnet_get_sw_interface (vnm, sw_if_index0);
1022             sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1023                 swif->unnumbered_sw_if_index : sw_if_index0;
1024             ia0 = ip6_interface_first_address(&ip6_main, sw_if_index);
1025             if (ia0)
1026               {
1027                   vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
1028             
1029                   vlib_cli_output (vm, "%=20U%=48U",
1030                                    format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index0,
1031                                    format_ip6_address, ia0);
1032               } else
1033                 vlib_cli_output (vm, "%=34s%=20U", "No IPv6 address configured on",
1034                                  format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index); 
1035         } else
1036           break;
1037     }
1038
1039   return 0;
1040 }
1041
1042 VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1043   .path = "show dhcpv6 link-address interface",
1044   .short_help = "show dhcpv6 link-address interface <interface>",
1045   .function = dhcpv6_link_address_show_command_fn,
1046 };