docs: Use newer Ubuntu LTS in tutorial
[vpp.git] / src / plugins / dhcp / dhcp6_client_common_dp.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/ethernet/ethernet.h>
17 #include <dhcp/dhcp6_packet.h>
18 #include <dhcp/dhcp6_client_common_dp.h>
19 #include <dhcp/dhcp6_ia_na_client_dp.h>
20 #include <dhcp/dhcp6_pd_client_dp.h>
21 #include <dhcp/dhcp6_packet.h>
22 #include <vnet/udp/udp_local.h>
23 #include <vnet/udp/udp_packet.h>
24
25 dhcp6_client_common_main_t dhcp6_client_common_main;
26 dhcpv6_duid_ll_string_t client_duid;
27
28 u32
29 server_index_get_or_create (u8 * data, u16 len)
30 {
31   dhcp6_client_common_main_t *ccm = &dhcp6_client_common_main;
32   u32 i;
33   server_id_t *se;
34   server_id_t new_se;
35
36   for (i = 0; i < vec_len (ccm->server_ids); i++)
37     {
38       se = &ccm->server_ids[i];
39       if (se->len == len && 0 == memcmp (se->data, data, len))
40         return i;
41     }
42
43   new_se.len = len;
44   new_se.data = 0;
45   vec_validate (new_se.data, len - 1);
46   memcpy (new_se.data, data, len);
47
48   vec_add1 (ccm->server_ids, new_se);
49
50   return vec_len (ccm->server_ids) - 1;
51 }
52
53 static void
54 generate_client_duid (void)
55 {
56   client_duid.duid_type = clib_host_to_net_u16 (DHCPV6_DUID_LL);
57   client_duid.hardware_type = clib_host_to_net_u16 (1);
58
59   vnet_main_t *vnm = vnet_get_main ();
60   vnet_interface_main_t *im = &vnm->interface_main;
61   vnet_hw_interface_t *hi;
62   ethernet_interface_t *eth_if = 0;
63
64   pool_foreach (hi, im->hw_interfaces)
65    {
66     eth_if = ethernet_get_interface (&ethernet_main, hi->hw_if_index);
67     if (eth_if)
68       break;
69   }
70
71   if (eth_if)
72     clib_memcpy (client_duid.lla, &eth_if->address, 6);
73   else
74     {
75       clib_warning ("Failed to find any Ethernet interface, "
76                     "setting DHCPv6 DUID link-layer address to random value");
77       u32 seed = random_default_seed ();
78       random_u32 (&seed);
79       client_duid.lla[0] = 0xc2;        /* locally administered unicast */
80       client_duid.lla[1] = 0x18;
81       client_duid.lla[2] = 0x44;
82       client_duid.lla[3] = random_u32 (&seed);
83       client_duid.lla[4] = random_u32 (&seed);
84       client_duid.lla[5] = random_u32 (&seed);
85     }
86 }
87
88 #define foreach_dhcpv6_client \
89   _(DROP, "error-drop")       \
90   _(LOOKUP, "ip6-lookup")
91
92 typedef enum
93 {
94 #define _(sym,str) DHCPV6_CLIENT_NEXT_##sym,
95   foreach_dhcpv6_client
96 #undef _
97     DHCPV6_CLIENT_N_NEXT,
98 } dhcpv6_client_next_t;
99
100 /**
101  * per-packet trace data
102  */
103 typedef struct dhcpv6_client_trace_t_
104 {
105 } dhcpv6_client_trace_t;
106
107 static u8 *
108 format_dhcpv6_client_trace (u8 * s, va_list * args)
109 {
110   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
111   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
112   //dhcpv6_client_trace_t *t = va_arg (*args, dhcpv6_client_trace_t *);
113
114   s = format (s, "nothing");
115
116   return s;
117 }
118
119 static uword
120 dhcpv6_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
121                        vlib_frame_t * frame)
122 {
123   dhcp6_ia_na_client_main_t *icm = &dhcp6_ia_na_client_main;
124   dhcp6_pd_client_main_t *pcm = &dhcp6_pd_client_main;
125
126   dhcpv6_client_next_t next_index;
127   u32 n_left_from, *from, *to_next;
128   next_index = 0;
129   n_left_from = frame->n_vectors;
130   from = vlib_frame_vector_args (frame);
131
132   while (n_left_from > 0)
133     {
134       u32 n_left_to_next;
135
136       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
137
138       while (n_left_from > 0 && n_left_to_next > 0)
139         {
140           ip6_header_t *ip0;
141           u32 options_length;
142           dhcpv6_header_t *dhcpv60;
143           dhcpv6_option_t *option;
144           vlib_buffer_t *b0;
145           dhcp6_report_common_t report;
146           dhcp6_address_info_t *addresses = 0;
147           dhcp6_prefix_info_t *prefixes = 0;
148           u32 next0 = DHCPV6_CLIENT_NEXT_DROP;
149           u32 bi0;
150           u32 xid;
151           u32 sw_if_index;
152           u32 iaid;
153           u8 client_id_present = 0;
154           u8 discard = 0;
155           u8 is_pd_packet = 0;
156
157           dhcp6_ia_na_client_state_t *ia_na_client_state = NULL;
158           dhcp6_pd_client_state_t *pd_client_state = NULL;
159
160           bi0 = from[0];
161           to_next[0] = bi0;
162           from += 1;
163           to_next += 1;
164           n_left_from -= 1;
165           n_left_to_next -= 1;
166
167           b0 = vlib_get_buffer (vm, bi0);
168
169           dhcpv60 = vlib_buffer_get_current (b0);
170           ip0 = (void *) (b0->data + vnet_buffer (b0)->l3_hdr_offset);
171           u32 dhcpv6_ip6_payload_offset =
172             (u8 *) dhcpv60 - ((u8 *) ip0 + sizeof (*ip0));
173           options_length =
174             clib_net_to_host_u16 (ip0->payload_length) -
175             dhcpv6_ip6_payload_offset - sizeof (*dhcpv60);
176
177           clib_memset (&report, 0, sizeof (report));
178
179           sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
180           if (sw_if_index >= vec_len (icm->client_state_by_sw_if_index))
181             ia_na_client_state = 0;
182           else
183             ia_na_client_state =
184               &icm->client_state_by_sw_if_index[sw_if_index];
185           if (sw_if_index >= vec_len (pcm->client_state_by_sw_if_index))
186             pd_client_state = 0;
187           else
188             pd_client_state = &pcm->client_state_by_sw_if_index[sw_if_index];
189
190           xid =
191             (dhcpv60->xid[0] << 16) + (dhcpv60->xid[1] << 8) +
192             dhcpv60->xid[2];
193           if (ia_na_client_state && ia_na_client_state->transaction_id == xid)
194             is_pd_packet = 0;
195           else if (pd_client_state && pd_client_state->transaction_id == xid)
196             is_pd_packet = 1;
197           else
198             {
199               clib_warning
200                 ("Received DHCPv6 message with wrong Transaction ID");
201               discard = 1;
202             }
203
204           report.sw_if_index = sw_if_index;
205           report.msg_type = dhcpv60->msg_type;
206           report.server_index = ~0;
207
208           switch (dhcpv60->msg_type)
209             {
210             case DHCPV6_MSG_ADVERTISE:
211             case DHCPV6_MSG_REPLY:
212               option = (dhcpv6_option_t *) (dhcpv60 + 1);
213               while (options_length > 0)
214                 {
215                   if (options_length <
216                       clib_net_to_host_u16 (option->length) +
217                       sizeof (*option))
218                     {
219                       clib_warning
220                         ("remaining payload length < option length (%d < %d)",
221                          options_length,
222                          clib_net_to_host_u16 (option->length) +
223                          sizeof (*option));
224                       break;
225                     }
226                   u16 oo = clib_net_to_host_u16 (option->option);
227                   if (oo == DHCPV6_OPTION_IA_NA || oo == DHCPV6_OPTION_IA_PD)
228                     {
229                       u8 discard_option = 0;
230                       dhcpv6_ia_header_t *ia_header = (void *) option;
231                       iaid = clib_net_to_host_u32 (ia_header->iaid);
232                       u32 T1 = clib_net_to_host_u32 (ia_header->t1);
233                       u32 T2 = clib_net_to_host_u32 (ia_header->t2);
234                       if (iaid != DHCPV6_CLIENT_IAID)
235                         discard_option = 1;
236                       if (T1 != 0 && T2 != 0 && T1 > T2)
237                         discard_option = 1;
238                       if (!discard_option)
239                         {
240                           report.T1 = T1;
241                           report.T2 = T2;
242                         }
243                       dhcpv6_option_t *inner_option =
244                         (void *) ia_header->data;
245                       u16 inner_options_length =
246                         clib_net_to_host_u16 (option->length) -
247                         (sizeof (*ia_header) - sizeof (dhcpv6_option_t));
248                       while (inner_options_length > 0)
249                         {
250                           u16 inner_oo =
251                             clib_net_to_host_u16 (inner_option->option);
252                           if (discard_option)
253                             ;
254                           else if (inner_oo == DHCPV6_OPTION_IAADDR)
255                             {
256                               dhcpv6_ia_opt_addr_t *iaaddr =
257                                 (void *) inner_option;
258                               u32 n_addresses = vec_len (addresses);
259                               vec_validate (addresses, n_addresses);
260                               dhcp6_address_info_t *address_info =
261                                 &addresses[n_addresses];
262                               address_info->preferred_time =
263                                 clib_net_to_host_u32 (iaaddr->preferred);
264                               address_info->valid_time =
265                                 clib_net_to_host_u32 (iaaddr->valid);
266                               address_info->address = iaaddr->addr;
267                             }
268                           else if (inner_oo == DHCPV6_OPTION_IAPREFIX)
269                             {
270                               dhcpv6_ia_opt_pd_t *iaprefix =
271                                 (void *) inner_option;
272                               u32 n_prefixes = vec_len (prefixes);
273                               vec_validate (prefixes, n_prefixes);
274                               dhcp6_prefix_info_t *prefix_info =
275                                 &prefixes[n_prefixes];
276                               prefix_info->preferred_time =
277                                 clib_net_to_host_u32 (iaprefix->preferred);
278                               prefix_info->valid_time =
279                                 clib_net_to_host_u32 (iaprefix->valid);
280                               prefix_info->prefix_length = iaprefix->prefix;
281                               prefix_info->prefix = iaprefix->addr;
282                             }
283                           else if (inner_oo == DHCPV6_OPTION_STATUS_CODE)
284                             {
285                               dhcpv6_status_code_t *sc =
286                                 (void *) inner_option;
287                               report.inner_status_code =
288                                 clib_net_to_host_u16 (sc->status_code);
289                             }
290                           inner_options_length -=
291                             sizeof (*inner_option) +
292                             clib_net_to_host_u16 (inner_option->length);
293                           inner_option =
294                             (void *) ((u8 *) inner_option +
295                                       sizeof (*inner_option) +
296                                       clib_net_to_host_u16
297                                       (inner_option->length));
298                         }
299                     }
300                   else if (oo == DHCPV6_OPTION_CLIENTID)
301                     {
302                       if (client_id_present)
303                         {
304                           clib_warning
305                             ("Duplicate Client ID in received DHVPv6 message");
306                           discard = 1;
307                         }
308                       else
309                         {
310                           u16 len = clib_net_to_host_u16 (option->length);
311                           client_id_present = 1;
312                           if (len != CLIENT_DUID_LENGTH ||
313                               0 != memcmp (option->data,
314                                            client_duid.bin_string,
315                                            CLIENT_DUID_LENGTH))
316                             {
317                               clib_warning
318                                 ("Unrecognized client DUID inside received DHVPv6 message");
319                               discard = 1;
320                             }
321                         }
322                     }
323                   else if (oo == DHCPV6_OPTION_SERVERID)
324                     {
325                       if (report.server_index != ~0)
326                         {
327                           clib_warning
328                             ("Duplicate Server ID in received DHVPv6 message");
329                           discard = 1;
330                         }
331                       else
332                         {
333                           u16 ol = clib_net_to_host_u16 (option->length);
334                           if (ol - 2 /* 2 byte DUID type code */  > 128)
335                             {
336                               clib_warning
337                                 ("Server DUID (without type code) is longer than 128 octets");
338                               discard = 1;
339                             }
340                           else
341                             {
342                               report.server_index =
343                                 server_index_get_or_create (option->data, ol);
344                             }
345                         }
346                     }
347                   else if (oo == DHCPV6_OPTION_PREFERENCE)
348                     {
349                       report.preference = option->data[0];
350                     }
351                   else if (oo == DHCPV6_OPTION_STATUS_CODE)
352                     {
353                       dhcpv6_status_code_t *sc = (void *) option;
354                       report.status_code =
355                         clib_net_to_host_u16 (sc->status_code);
356                     }
357                   options_length -=
358                     sizeof (*option) + clib_net_to_host_u16 (option->length);
359                   option =
360                     (void *) ((u8 *) option + sizeof (*option) +
361                               clib_net_to_host_u16 (option->length));
362                 }
363
364               if (!client_id_present)
365                 {
366                   clib_warning
367                     ("Missing Client ID in received DHVPv6 message");
368                   discard = 1;
369                 }
370               if (report.server_index == ~0)
371                 {
372                   clib_warning
373                     ("Missing Server ID in received DHVPv6 message");
374                   discard = 1;
375                 }
376
377               if (!discard)
378                 {
379                   if (!is_pd_packet)
380                     {
381                       address_report_t r;
382                       r.body = report;
383                       r.n_addresses = vec_len (addresses);
384                       r.addresses = addresses;
385                       dhcp6_publish_report (&r);
386                       /* We just gave addresses to another process! */
387                       addresses = 0;
388                     }
389                   else
390                     {
391                       prefix_report_t r;
392                       r.body = report;
393                       r.n_prefixes = vec_len (prefixes);
394                       r.prefixes = prefixes;
395                       dhcp6_pd_publish_report (&r);
396                       /* We just gave prefixes to another process! */
397                       prefixes = 0;
398                     }
399                 }
400               vec_free (addresses);
401               vec_free (prefixes);
402
403               break;
404             default:
405               break;
406             }
407
408           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
409             {
410               dhcpv6_client_trace_t *t =
411                 vlib_add_trace (vm, node, b0, sizeof (*t));
412             }
413
414           /* verify speculative enqueue, maybe switch current next frame */
415           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
416                                            to_next, n_left_to_next,
417                                            bi0, next0);
418         }
419
420       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
421     }
422
423   return frame->n_vectors;
424 }
425
426 VLIB_REGISTER_NODE (dhcpv6_client_node, static) = {
427     .function = dhcpv6_client_node_fn,
428     .name = "dhcpv6-client",
429     .vector_size = sizeof (u32),
430
431     .n_errors = 0,
432
433     .n_next_nodes = DHCPV6_CLIENT_N_NEXT,
434     .next_nodes = {
435   #define _(s,n) [DHCPV6_CLIENT_NEXT_##s] = n,
436       foreach_dhcpv6_client
437   #undef _
438     },
439
440     .format_trace = format_dhcpv6_client_trace,
441 };
442
443 void
444 dhcp6_clients_enable_disable (u8 enable)
445 {
446   vlib_main_t *vm = vlib_get_main ();
447
448   if (enable)
449     {
450       if (client_duid.duid_type == 0)
451         generate_client_duid ();
452       udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
453                              dhcpv6_client_node.index, 0 /* is_ip6 */ );
454     }
455   else
456     udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
457                              0 /* is_ip6 */ );
458 }
459
460 /*
461  * fd.io coding-style-patch-verification: ON
462  *
463  * Local Variables:
464  * eval: (c-set-style "gnu")
465  * End:
466  */