FD.io VPP  v17.07.01-10-g3be13f0
Vector Packet Processing
ipsec_input.c
Go to the documentation of this file.
1 /*
2  * decap.c : IPSec tunnel decapsulation
3  *
4  * Copyright (c) 2015 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 <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/feature/feature.h>
22 
23 #include <vnet/ipsec/ipsec.h>
24 #include <vnet/ipsec/esp.h>
25 
26 #define foreach_ipsec_input_error \
27  _(RX_PKTS, "IPSEC pkts received") \
28  _(DECRYPTION_FAILED, "IPSEC decryption failed")
29 
30 typedef enum
31 {
32 #define _(sym,str) IPSEC_INPUT_ERROR_##sym,
34 #undef _
37 
38 static char *ipsec_input_error_strings[] = {
39 #define _(sym,string) string,
41 #undef _
42 };
43 
44 typedef struct
45 {
50 
51 /* packet trace format function */
52 static u8 *
53 format_ipsec_input_trace (u8 * s, va_list * args)
54 {
55  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
56  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
57  ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
58 
59  if (t->spi == 0 && t->seq == 0)
60  {
61  s = format (s, "esp: no esp packet");
62  return s;
63  }
64 
65  if (t->sa_id != 0)
66  {
67  s = format (s, "esp: sa_id %u spi %u seq %u", t->sa_id, t->spi, t->seq);
68  }
69  else
70  {
71  s = format (s, "esp: no sa spi %u seq %u", t->spi, t->seq);
72  }
73  return s;
74 }
75 
78 {
79  ipsec_main_t *im = &ipsec_main;
80  ipsec_policy_t *p;
81  ipsec_sa_t *s;
82  u32 *i;
83 
85  {
86  p = pool_elt_at_index (spd->policies, *i);
87  s = pool_elt_at_index (im->sad, p->sa_index);
88 
89  if (spi != s->spi)
90  continue;
91 
92  if (s->is_tunnel)
93  {
94  if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32))
95  continue;
96 
97  if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32))
98  continue;
99 
100  return p;
101  }
102 
103  if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
104  continue;
105 
106  if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
107  continue;
108 
109  if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
110  continue;
111 
112  if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
113  continue;
114 
115  return p;
116  }
117  return 0;
118 }
119 
122  ip6_address_t * ua)
123 {
124  if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
125  (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
126  return 1;
127  return 0;
128 }
129 
132  ip6_address_t * sa,
133  ip6_address_t * da, u32 spi)
134 {
135  ipsec_main_t *im = &ipsec_main;
136  ipsec_policy_t *p;
137  ipsec_sa_t *s;
138  u32 *i;
139 
141  {
142  p = pool_elt_at_index (spd->policies, *i);
143  s = pool_elt_at_index (im->sad, p->sa_index);
144 
145  if (spi != s->spi)
146  continue;
147 
148  if (s->is_tunnel)
149  {
150  if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6))
151  continue;
152 
153  if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6))
154  continue;
155 
156  return p;
157  }
158 
159  if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
160  continue;
161 
162  if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
163  continue;
164 
165  return p;
166  }
167  return 0;
168 }
169 
171 
172 static uword
174  vlib_node_runtime_t * node,
175  vlib_frame_t * from_frame)
176 {
177  u32 n_left_from, *from, next_index, *to_next;
178  ipsec_main_t *im = &ipsec_main;
179 
180  from = vlib_frame_vector_args (from_frame);
181  n_left_from = from_frame->n_vectors;
182 
183  next_index = node->cached_next_index;
184 
185  while (n_left_from > 0)
186  {
187  u32 n_left_to_next;
188 
189  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
190 
191  while (n_left_from > 0 && n_left_to_next > 0)
192  {
193  u32 bi0, next0;
194  vlib_buffer_t *b0;
195  ip4_header_t *ip0;
196  esp_header_t *esp0;
197  ip4_ipsec_config_t *c0;
198  ipsec_spd_t *spd0;
199  ipsec_policy_t *p0 = 0;
200 
201  bi0 = to_next[0] = from[0];
202  from += 1;
203  n_left_from -= 1;
204  to_next += 1;
205  n_left_to_next -= 1;
206 
207  b0 = vlib_get_buffer (vm, bi0);
208  c0 =
209  vnet_feature_next_with_data (vnet_buffer (b0)->sw_if_index
210  [VLIB_RX], &next0, b0,
211  sizeof (c0[0]));
212 
213  spd0 = pool_elt_at_index (im->spds, c0->spd_index);
214 
215  ip0 = vlib_buffer_get_current (b0);
216  esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
217 
218  if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
219  {
220 #if 0
222  ("packet received from %U to %U spi %u size %u spd_id %u",
225  clib_net_to_host_u32 (esp0->spi),
226  clib_net_to_host_u16 (ip0->length), spd0->id);
227 #endif
228 
230  clib_net_to_host_u32
231  (ip0->src_address.
232  as_u32),
233  clib_net_to_host_u32
234  (ip0->dst_address.
235  as_u32),
236  clib_net_to_host_u32
237  (esp0->spi));
238 
239  if (PREDICT_TRUE (p0 != 0))
240  {
241  p0->counter.packets++;
242  p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
243  vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
244  vnet_buffer (b0)->ipsec.flags = 0;
245  next0 = im->esp_decrypt_next_index;
247  goto trace0;
248  }
249  }
250 
251  /* FIXME bypass and discard */
252 
253  trace0:
255  {
256  ipsec_input_trace_t *tr =
257  vlib_add_trace (vm, node, b0, sizeof (*tr));
258  if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
259  {
260  if (p0)
261  tr->sa_id = p0->sa_id;
262  tr->spi = clib_host_to_net_u32 (esp0->spi);
263  tr->seq = clib_host_to_net_u32 (esp0->seq);
264  }
265  }
266 
267  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
268  to_next, n_left_to_next, bi0,
269  next0);
270  }
271  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
272  }
274  IPSEC_INPUT_ERROR_RX_PKTS,
275  from_frame->n_vectors);
276 
277  return from_frame->n_vectors;
278 }
279 
280 
281 /* *INDENT-OFF* */
283  .function = ipsec_input_ip4_node_fn,
284  .name = "ipsec-input-ip4",
285  .vector_size = sizeof (u32),
286  .format_trace = format_ipsec_input_trace,
287  .type = VLIB_NODE_TYPE_INTERNAL,
288 
290  .error_strings = ipsec_input_error_strings,
291 
292  .n_next_nodes = IPSEC_INPUT_N_NEXT,
293  .next_nodes = {
294 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
296 #undef _
297  },
298 };
299 /* *INDENT-ON* */
300 
303 
304  static uword
306  vlib_node_runtime_t * node,
307  vlib_frame_t * from_frame)
308 {
309  u32 n_left_from, *from, next_index, *to_next;
310  ipsec_main_t *im = &ipsec_main;
311 
312  from = vlib_frame_vector_args (from_frame);
313  n_left_from = from_frame->n_vectors;
314 
315  next_index = node->cached_next_index;
316 
317  while (n_left_from > 0)
318  {
319  u32 n_left_to_next;
320 
321  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
322 
323  while (n_left_from > 0 && n_left_to_next > 0)
324  {
325  u32 bi0, next0;
326  vlib_buffer_t *b0;
327  ip6_header_t *ip0;
328  esp_header_t *esp0;
329  ip4_ipsec_config_t *c0;
330  ipsec_spd_t *spd0;
331  ipsec_policy_t *p0 = 0;
332  u32 header_size = sizeof (ip0[0]);
333 
334  bi0 = to_next[0] = from[0];
335  from += 1;
336  n_left_from -= 1;
337  to_next += 1;
338  n_left_to_next -= 1;
339 
340  b0 = vlib_get_buffer (vm, bi0);
341  c0 =
342  vnet_feature_next_with_data (vnet_buffer (b0)->sw_if_index
343  [VLIB_RX], &next0, b0,
344  sizeof (c0[0]));
345 
346  spd0 = pool_elt_at_index (im->spds, c0->spd_index);
347 
348  ip0 = vlib_buffer_get_current (b0);
349  esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
350 
351  if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
352  {
353 #if 0
355  ("packet received from %U to %U spi %u size %u spd_id %u",
357  &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
358  clib_net_to_host_u16 (ip0->payload_length) + header_size,
359  spd0->id);
360 #endif
362  &ip0->src_address,
363  &ip0->dst_address,
364  clib_net_to_host_u32
365  (esp0->spi));
366 
367  if (PREDICT_TRUE (p0 != 0))
368  {
369  p0->counter.packets++;
370  p0->counter.bytes +=
371  clib_net_to_host_u16 (ip0->payload_length);
372  p0->counter.bytes += header_size;
373  vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
374  vnet_buffer (b0)->ipsec.flags = 0;
375  next0 = im->esp_decrypt_next_index;
376  vlib_buffer_advance (b0, header_size);
377  goto trace0;
378  }
379  }
380 
381  trace0:
383  {
384  ipsec_input_trace_t *tr =
385  vlib_add_trace (vm, node, b0, sizeof (*tr));
386  if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
387  {
388  if (p0)
389  tr->sa_id = p0->sa_id;
390  tr->spi = clib_host_to_net_u32 (esp0->spi);
391  tr->seq = clib_host_to_net_u32 (esp0->seq);
392  }
393  }
394 
395  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
396  n_left_to_next, bi0, next0);
397  }
398  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
399  }
401  IPSEC_INPUT_ERROR_RX_PKTS,
402  from_frame->n_vectors);
403 
404  return from_frame->n_vectors;
405 }
406 
407 
408 /* *INDENT-OFF* */
410  .function = ipsec_input_ip6_node_fn,
411  .name = "ipsec-input-ip6",
412  .vector_size = sizeof (u32),
413  .format_trace = format_ipsec_input_trace,
414  .type = VLIB_NODE_TYPE_INTERNAL,
415 
417  .error_strings = ipsec_input_error_strings,
418 
419  .sibling_of = "ipsec-input-ip4",
420 };
421 /* *INDENT-ON* */
422 
424 /*
425  * fd.io coding-style-patch-verification: ON
426  *
427  * Local Variables:
428  * eval: (c-set-style "gnu")
429  * End:
430  */
ip46_address_t stop
Definition: ipsec.h:137
static char * ipsec_input_error_strings[]
Definition: ipsec_input.c:38
u32 * ipv6_inbound_protect_policy_indices
Definition: ipsec.h:216
ipsec_spd_t * spds
Definition: ipsec.h:246
u32 * ipv4_inbound_protect_policy_indices
Definition: ipsec.h:214
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define CLIB_UNUSED(x)
Definition: clib.h:79
ip46_address_t tunnel_src_addr
Definition: ipsec.h:119
a
Definition: bitmap.h:516
ip4_address_t src_address
Definition: ip4_packet.h:164
static uword ipsec_input_ip6_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: ipsec_input.c:305
#define PREDICT_TRUE(x)
Definition: clib.h:98
u64 as_u64[2]
Definition: ip6_packet.h:51
static int ip4_header_bytes(ip4_header_t *i)
Definition: ip4_packet.h:227
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:459
static ipsec_policy_t * ipsec_input_protect_policy_match(ipsec_spd_t *spd, u32 sa, u32 da, u32 spi)
Definition: ipsec_input.c:77
u8 is_tunnel
Definition: ipsec.h:117
struct _vlib_node_registration vlib_node_registration_t
static uword ip6_addr_match_range(ip6_address_t *a, ip6_address_t *la, ip6_address_t *ua)
Definition: ipsec_input.c:121
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static u8 * format_ipsec_input_trace(u8 *s, va_list *args)
Definition: ipsec_input.c:53
ip6_address_t src_address
Definition: ip6_packet.h:341
u32 spi
Definition: ipsec.h:103
format_function_t format_ip4_address
Definition: format.h:79
static vlib_node_registration_t ipsec_input_ip6_node
(constructor) VLIB_REGISTER_NODE (ipsec_input_ip6_node)
Definition: ipsec_input.c:302
#define always_inline
Definition: clib.h:84
ip4_address_t dst_address
Definition: ip4_packet.h:164
ipsec_main_t ipsec_main
Definition: ipsec.h:282
static uword ip6_address_is_equal(ip6_address_t *a, ip6_address_t *b)
Definition: ip6_packet.h:208
#define foreach_ipsec_input_next
Definition: ipsec.h:34
unsigned long u64
Definition: types.h:89
ipsec_policy_t * policies
Definition: ipsec.h:210
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
counter_t packets
packet counter
Definition: counter.h:141
ipsec_input_error_t
Definition: ipsec_input.c:30
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:188
#define PREDICT_FALSE(x)
Definition: clib.h:97
ip46_address_range_t laddr
Definition: ipsec.h:191
#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:216
#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:366
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1131
ip46_address_t tunnel_dst_addr
Definition: ipsec.h:120
u16 n_vectors
Definition: node.h:345
format_function_t format_ip6_address
Definition: format.h:95
u32 sa_index
Definition: ipsec.h:200
u32 esp_decrypt_next_index
Definition: ipsec.h:276
ip46_address_t start
Definition: ipsec.h:137
#define clib_warning(format, args...)
Definition: error.h:59
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:85
#define ARRAY_LEN(x)
Definition: clib.h:59
ip46_address_range_t raddr
Definition: ipsec.h:192
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:460
unsigned int u32
Definition: types.h:88
static vlib_node_registration_t ipsec_input_ip4_node
(constructor) VLIB_REGISTER_NODE (ipsec_input_ip4_node)
Definition: ipsec_input.c:170
ipsec_sa_t * sad
Definition: ipsec.h:247
u32 seq
Definition: esp.h:25
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:201
static uword ipsec_input_ip4_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: ipsec_input.c:173
u32 spi
Definition: esp.h:24
static ipsec_policy_t * ipsec_input_ip6_protect_policy_match(ipsec_spd_t *spd, ip6_address_t *sa, ip6_address_t *da, u32 spi)
Definition: ipsec_input.c:131
u64 uword
Definition: types.h:112
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
counter_t bytes
byte counter
Definition: counter.h:142
#define foreach_ipsec_input_error
Definition: ipsec_input.c:26
u16 payload_length
Definition: ip6_packet.h:332
unsigned char u8
Definition: types.h:56
vlib_counter_t counter
Definition: ipsec.h:203
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:269
static_always_inline void * vnet_feature_next_with_data(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0, u32 n_data_bytes)
Definition: feature.h:208
u32 id
Definition: ipsec.h:208
#define vnet_buffer(b)
Definition: buffer.h:304
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:159
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:144
#define vec_foreach(var, vec)
Vector iterator.
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:74
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
Definition: defs.h:46
ip6_address_t dst_address
Definition: ip6_packet.h:341