FD.io VPP  v20.05.1-5-g09f167997
Vector Packet Processing
dhcp6_client_common_dp.c
Go to the documentation of this file.
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>
21 #include <dhcp/dhcp6_packet.h>
22 #include <vnet/udp/udp.h>
23 
26 
27 u32
29 {
31  u32 i;
32  server_id_t *se;
33  server_id_t new_se;
34 
35  for (i = 0; i < vec_len (ccm->server_ids); i++)
36  {
37  se = &ccm->server_ids[i];
38  if (se->len == len && 0 == memcmp (se->data, data, len))
39  return i;
40  }
41 
42  new_se.len = len;
43  new_se.data = 0;
44  vec_validate (new_se.data, len - 1);
45  memcpy (new_se.data, data, len);
46 
47  vec_add1 (ccm->server_ids, new_se);
48 
49  return vec_len (ccm->server_ids) - 1;
50 }
51 
52 static void
54 {
55  client_duid.duid_type = clib_host_to_net_u16 (DHCPV6_DUID_LL);
56  client_duid.hardware_type = clib_host_to_net_u16 (1);
57 
58  vnet_main_t *vnm = vnet_get_main ();
61  ethernet_interface_t *eth_if = 0;
62 
63  /* *INDENT-OFF* */
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  /* *INDENT-ON* */
71 
72  if (eth_if)
73  clib_memcpy (client_duid.lla, eth_if->address, 6);
74  else
75  {
76  clib_warning ("Failed to find any Ethernet interface, "
77  "setting DHCPv6 DUID link-layer address to random value");
78  u32 seed = random_default_seed ();
79  random_u32 (&seed);
80  client_duid.lla[0] = 0xc2; /* locally administered unicast */
81  client_duid.lla[1] = 0x18;
82  client_duid.lla[2] = 0x44;
83  client_duid.lla[3] = random_u32 (&seed);
84  client_duid.lla[4] = random_u32 (&seed);
85  client_duid.lla[5] = random_u32 (&seed);
86  }
87 }
88 
89 #define foreach_dhcpv6_client \
90  _(DROP, "error-drop") \
91  _(LOOKUP, "ip6-lookup")
92 
93 typedef enum
94 {
95 #define _(sym,str) DHCPV6_CLIENT_NEXT_##sym,
97 #undef _
100 
101 /**
102  * per-packet trace data
103  */
105 {
107 
108 static u8 *
109 format_dhcpv6_client_trace (u8 * s, va_list * args)
110 {
111  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
112  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
113  //dhcpv6_client_trace_t *t = va_arg (*args, dhcpv6_client_trace_t *);
114 
115  s = format (s, "nothing");
116 
117  return s;
118 }
119 
120 static uword
123 {
126 
127  dhcpv6_client_next_t next_index;
128  u32 n_left_from, *from, *to_next;
129  next_index = 0;
130  n_left_from = frame->n_vectors;
131  from = vlib_frame_vector_args (frame);
132 
133  while (n_left_from > 0)
134  {
135  u32 n_left_to_next;
136 
137  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
138 
139  while (n_left_from > 0 && n_left_to_next > 0)
140  {
141  ip6_header_t *ip0;
142  u32 options_length;
143  dhcpv6_header_t *dhcpv60;
144  dhcpv6_option_t *option;
145  vlib_buffer_t *b0;
146  dhcp6_report_common_t report;
147  dhcp6_address_info_t *addresses = 0;
148  dhcp6_prefix_info_t *prefixes = 0;
149  u32 next0 = DHCPV6_CLIENT_NEXT_DROP;
150  u32 bi0;
151  u32 xid;
153  u32 iaid;
154  u8 client_id_present = 0;
155  u8 discard = 0;
156  u8 is_pd_packet = 0;
157 
158  dhcp6_ia_na_client_state_t *ia_na_client_state = NULL;
159  dhcp6_pd_client_state_t *pd_client_state = NULL;
160 
161  bi0 = from[0];
162  to_next[0] = bi0;
163  from += 1;
164  to_next += 1;
165  n_left_from -= 1;
166  n_left_to_next -= 1;
167 
168  b0 = vlib_get_buffer (vm, bi0);
169 
170  dhcpv60 = vlib_buffer_get_current (b0);
171  ip0 = (void *) (b0->data + vnet_buffer (b0)->l3_hdr_offset);
172  u32 dhcpv6_ip6_payload_offset =
173  (u8 *) dhcpv60 - ((u8 *) ip0 + sizeof (*ip0));
174  options_length =
175  clib_net_to_host_u16 (ip0->payload_length) -
176  dhcpv6_ip6_payload_offset - sizeof (*dhcpv60);
177 
178  clib_memset (&report, 0, sizeof (report));
179 
180  sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
181  if (sw_if_index >= vec_len (icm->client_state_by_sw_if_index))
182  ia_na_client_state = 0;
183  else
184  ia_na_client_state =
186  if (sw_if_index >= vec_len (pcm->client_state_by_sw_if_index))
187  pd_client_state = 0;
188  else
189  pd_client_state = &pcm->client_state_by_sw_if_index[sw_if_index];
190 
191  xid =
192  (dhcpv60->xid[0] << 16) + (dhcpv60->xid[1] << 8) +
193  dhcpv60->xid[2];
194  if (ia_na_client_state && ia_na_client_state->transaction_id == xid)
195  is_pd_packet = 0;
196  else if (pd_client_state && pd_client_state->transaction_id == xid)
197  is_pd_packet = 1;
198  else
199  {
201  ("Received DHCPv6 message with wrong Transaction ID");
202  discard = 1;
203  }
204 
205  report.sw_if_index = sw_if_index;
206  report.msg_type = dhcpv60->msg_type;
207  report.server_index = ~0;
208 
209  switch (dhcpv60->msg_type)
210  {
212  case DHCPV6_MSG_REPLY:
213  option = (dhcpv6_option_t *) (dhcpv60 + 1);
214  while (options_length > 0)
215  {
216  if (options_length <
217  clib_net_to_host_u16 (option->length) +
218  sizeof (*option))
219  {
221  ("remaining payload length < option length (%d < %d)",
222  options_length,
223  clib_net_to_host_u16 (option->length) +
224  sizeof (*option));
225  break;
226  }
227  u16 oo = clib_net_to_host_u16 (option->option);
228  if (oo == DHCPV6_OPTION_IA_NA || oo == DHCPV6_OPTION_IA_PD)
229  {
230  u8 discard_option = 0;
231  dhcpv6_ia_header_t *ia_header = (void *) option;
232  iaid = clib_net_to_host_u32 (ia_header->iaid);
233  u32 T1 = clib_net_to_host_u32 (ia_header->t1);
234  u32 T2 = clib_net_to_host_u32 (ia_header->t2);
235  if (iaid != DHCPV6_CLIENT_IAID)
236  discard_option = 1;
237  if (T1 != 0 && T2 != 0 && T1 > T2)
238  discard_option = 1;
239  if (!discard_option)
240  {
241  report.T1 = T1;
242  report.T2 = T2;
243  }
244  dhcpv6_option_t *inner_option =
245  (void *) ia_header->data;
246  u16 inner_options_length =
247  clib_net_to_host_u16 (option->length) -
248  (sizeof (*ia_header) - sizeof (dhcpv6_option_t));
249  while (inner_options_length > 0)
250  {
251  u16 inner_oo =
252  clib_net_to_host_u16 (inner_option->option);
253  if (discard_option)
254  ;
255  else if (inner_oo == DHCPV6_OPTION_IAADDR)
256  {
257  dhcpv6_ia_opt_addr_t *iaaddr =
258  (void *) inner_option;
259  u32 n_addresses = vec_len (addresses);
260  vec_validate (addresses, n_addresses);
261  dhcp6_address_info_t *address_info =
262  &addresses[n_addresses];
263  address_info->preferred_time =
264  clib_net_to_host_u32 (iaaddr->preferred);
265  address_info->valid_time =
266  clib_net_to_host_u32 (iaaddr->valid);
267  address_info->address = iaaddr->addr;
268  }
269  else if (inner_oo == DHCPV6_OPTION_IAPREFIX)
270  {
271  dhcpv6_ia_opt_pd_t *iaprefix =
272  (void *) inner_option;
273  u32 n_prefixes = vec_len (prefixes);
274  vec_validate (prefixes, n_prefixes);
275  dhcp6_prefix_info_t *prefix_info =
276  &prefixes[n_prefixes];
277  prefix_info->preferred_time =
278  clib_net_to_host_u32 (iaprefix->preferred);
279  prefix_info->valid_time =
280  clib_net_to_host_u32 (iaprefix->valid);
281  prefix_info->prefix_length = iaprefix->prefix;
282  prefix_info->prefix = iaprefix->addr;
283  }
284  else if (inner_oo == DHCPV6_OPTION_STATUS_CODE)
285  {
286  dhcpv6_status_code_t *sc =
287  (void *) inner_option;
288  report.inner_status_code =
289  clib_net_to_host_u16 (sc->status_code);
290  }
291  inner_options_length -=
292  sizeof (*inner_option) +
293  clib_net_to_host_u16 (inner_option->length);
294  inner_option =
295  (void *) ((u8 *) inner_option +
296  sizeof (*inner_option) +
297  clib_net_to_host_u16
298  (inner_option->length));
299  }
300  }
301  else if (oo == DHCPV6_OPTION_CLIENTID)
302  {
303  if (client_id_present)
304  {
306  ("Duplicate Client ID in received DHVPv6 message");
307  discard = 1;
308  }
309  else
310  {
311  u16 len = clib_net_to_host_u16 (option->length);
312  client_id_present = 1;
313  if (len != CLIENT_DUID_LENGTH ||
314  0 != memcmp (option->data,
315  client_duid.bin_string,
317  {
319  ("Unrecognized client DUID inside received DHVPv6 message");
320  discard = 1;
321  }
322  }
323  }
324  else if (oo == DHCPV6_OPTION_SERVERID)
325  {
326  if (report.server_index != ~0)
327  {
329  ("Duplicate Server ID in received DHVPv6 message");
330  discard = 1;
331  }
332  else
333  {
334  u16 ol = clib_net_to_host_u16 (option->length);
335  if (ol - 2 /* 2 byte DUID type code */ > 128)
336  {
338  ("Server DUID (without type code) is longer than 128 octets");
339  discard = 1;
340  }
341  else
342  {
343  report.server_index =
344  server_index_get_or_create (option->data, ol);
345  }
346  }
347  }
348  else if (oo == DHCPV6_OPTION_PREFERENCE)
349  {
350  report.preference = option->data[0];
351  }
352  else if (oo == DHCPV6_OPTION_STATUS_CODE)
353  {
354  dhcpv6_status_code_t *sc = (void *) option;
355  report.status_code =
356  clib_net_to_host_u16 (sc->status_code);
357  }
358  options_length -=
359  sizeof (*option) + clib_net_to_host_u16 (option->length);
360  option =
361  (void *) ((u8 *) option + sizeof (*option) +
362  clib_net_to_host_u16 (option->length));
363  }
364 
365  if (!client_id_present)
366  {
368  ("Missing Client ID in received DHVPv6 message");
369  discard = 1;
370  }
371  if (report.server_index == ~0)
372  {
374  ("Missing Server ID in received DHVPv6 message");
375  discard = 1;
376  }
377 
378  if (!discard)
379  {
380  if (!is_pd_packet)
381  {
383  r.body = report;
384  r.n_addresses = vec_len (addresses);
385  r.addresses = addresses;
387  /* We just gave addresses to another process! */
388  addresses = 0;
389  }
390  else
391  {
392  prefix_report_t r;
393  r.body = report;
394  r.n_prefixes = vec_len (prefixes);
395  r.prefixes = prefixes;
397  /* We just gave prefixes to another process! */
398  prefixes = 0;
399  }
400  }
401  vec_free (addresses);
402  vec_free (prefixes);
403 
404  break;
405  default:
406  break;
407  }
408 
409  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
410  {
412  vlib_add_trace (vm, node, b0, sizeof (*t));
413  }
414 
415  /* verify speculative enqueue, maybe switch current next frame */
416  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
417  to_next, n_left_to_next,
418  bi0, next0);
419  }
420 
421  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
422  }
423 
424  return frame->n_vectors;
425 }
426 
427 /* *INDENT-OFF* */
429  .function = dhcpv6_client_node_fn,
430  .name = "dhcpv6-client",
431  .vector_size = sizeof (u32),
432 
433  .n_errors = 0,
434 
435  .n_next_nodes = DHCPV6_CLIENT_N_NEXT,
436  .next_nodes = {
437  #define _(s,n) [DHCPV6_CLIENT_NEXT_##s] = n,
439  #undef _
440  },
441 
442  .format_trace = format_dhcpv6_client_trace,
443 };
444 /* *INDENT-ON* */
445 
446 void
448 {
450 
451  if (enable)
452  {
453  if (client_duid.duid_type == 0)
455  udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
456  dhcpv6_client_node.index, 0 /* is_ip6 */ );
457  }
458  else
459  udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
460  0 /* is_ip6 */ );
461 }
462 
463 /*
464  * fd.io coding-style-patch-verification: ON
465  *
466  * Local Variables:
467  * eval: (c-set-style "gnu")
468  * End:
469  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:507
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
static vlib_node_registration_t dhcpv6_client_node
(constructor) VLIB_REGISTER_NODE (dhcpv6_client_node)
#define CLIB_UNUSED(x)
Definition: clib.h:86
static uword random_default_seed(void)
Default random seed (unix/linux user-mode)
Definition: random.h:91
dhcp6_pd_client_main_t dhcp6_pd_client_main
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_interface_main_t interface_main
Definition: vnet.h:56
dhcp6_pd_client_state_t * client_state_by_sw_if_index
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void generate_client_duid(void)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:590
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
int dhcp6_pd_publish_report(prefix_report_t *r)
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
vl_api_interface_index_t sw_if_index
Definition: gre.api:53
#define foreach_dhcpv6_client
unsigned int u32
Definition: types.h:88
vnet_hw_interface_t * hw_interfaces
Definition: interface.h:844
dhcp6_ia_na_client_main_t dhcp6_ia_na_client_main
u32 server_index_get_or_create(u8 *data, u16 len)
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define PREDICT_FALSE(x)
Definition: clib.h:118
dhcp6_ia_na_client_state_t * client_state_by_sw_if_index
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
vlib_main_t * vm
Definition: in2out_ed.c:1599
per-packet trace data
dhcpv6_duid_ll_string_t client_duid
u8 len
Definition: ip_types.api:92
#define CLIENT_DUID_LENGTH
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:399
#define DHCPV6_CLIENT_IAID
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
void udp_unregister_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp_local.c:506
#define clib_warning(format, args...)
Definition: error.h:59
u8 data[]
Packet data.
Definition: buffer.h:181
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:483
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1599
struct dhcpv6_client_trace_t_ dhcpv6_client_trace_t
per-packet trace data
u8 data[128]
Definition: ipsec_types.api:89
dhcp6_report_common_t body
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
dhcp6_client_common_main_t dhcp6_client_common_main
vl_api_ip4_address_t hi
Definition: arp.api:37
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
u16 payload_length
Definition: ip6_packet.h:301
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
int dhcp6_publish_report(address_report_t *r)
dhcpv6_client_next_t
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
dhcp6_address_info_t * addresses
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
void dhcp6_clients_enable_disable(u8 enable)
#define vnet_buffer(b)
Definition: buffer.h:417
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1600
void udp_register_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u32 node_index, u8 is_ip4)
Definition: udp_local.c:468
dhcp6_report_common_t body
static uword dhcpv6_client_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
dhcp6_prefix_info_t * prefixes
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
Definition: defs.h:46
static u8 * format_dhcpv6_client_trace(u8 *s, va_list *args)