FD.io VPP  v16.12-rc0-308-g931be3a
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 
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 
25 #define foreach_ipsec_input_next \
26 _(DROP, "error-drop") \
27 _(ESP_DECRYPT, "esp-decrypt")
28 
29 #define _(v, s) IPSEC_INPUT_NEXT_##v,
30 typedef enum
31 {
33 #undef _
36 
37 
38 #define foreach_ipsec_input_error \
39  _(RX_PKTS, "IPSEC pkts received") \
40  _(DECRYPTION_FAILED, "IPSEC decryption failed")
41 
42 
43 typedef enum
44 {
45 #define _(sym,str) IPSEC_INPUT_ERROR_##sym,
47 #undef _
50 
51 static char *ipsec_input_error_strings[] = {
52 #define _(sym,string) string,
54 #undef _
55 };
56 
57 typedef struct
58 {
63 
64 /* packet trace format function */
65 static u8 *
66 format_ipsec_input_trace (u8 * s, va_list * args)
67 {
68  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
69  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
70  ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
71 
72  if (t->spi == 0 && t->seq == 0)
73  {
74  s = format (s, "esp: no esp packet");
75  return s;
76  }
77 
78  if (t->sa_id != 0)
79  {
80  s = format (s, "esp: sa_id %u spi %u seq %u", t->sa_id, t->spi, t->seq);
81  }
82  else
83  {
84  s = format (s, "esp: no sa spi %u seq %u", t->spi, t->seq);
85  }
86  return s;
87 }
88 
91 {
92  ipsec_main_t *im = &ipsec_main;
93  ipsec_policy_t *p;
94  ipsec_sa_t *s;
95  u32 *i;
96 
98  {
99  p = pool_elt_at_index (spd->policies, *i);
100  s = pool_elt_at_index (im->sad, p->sa_index);
101 
102  if (spi != s->spi)
103  continue;
104 
105  if (s->is_tunnel)
106  {
107  if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32))
108  continue;
109 
110  if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32))
111  continue;
112 
113  return p;
114  }
115 
116  if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
117  continue;
118 
119  if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
120  continue;
121 
122  if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
123  continue;
124 
125  if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
126  continue;
127 
128  return p;
129  }
130  return 0;
131 }
132 
135  ip6_address_t * ua)
136 {
137  if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
138  (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
139  return 1;
140  return 0;
141 }
142 
145  ip6_address_t * sa,
146  ip6_address_t * da, u32 spi)
147 {
148  ipsec_main_t *im = &ipsec_main;
149  ipsec_policy_t *p;
150  ipsec_sa_t *s;
151  u32 *i;
152 
154  {
155  p = pool_elt_at_index (spd->policies, *i);
156  s = pool_elt_at_index (im->sad, p->sa_index);
157 
158  if (spi != s->spi)
159  continue;
160 
161  if (s->is_tunnel)
162  {
163  if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6))
164  continue;
165 
166  if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6))
167  continue;
168 
169  return p;
170  }
171 
172  if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
173  continue;
174 
175  if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
176  continue;
177 
178  return p;
179  }
180  return 0;
181 }
182 
184 
185 static uword
187  vlib_node_runtime_t * node,
188  vlib_frame_t * from_frame)
189 {
190  ip4_main_t *i4m = &ip4_main;
191  ip_lookup_main_t *lm = &i4m->lookup_main;
194  u32 n_left_from, *from, next_index, *to_next;
195  ipsec_main_t *im = &ipsec_main;
196 
197  from = vlib_frame_vector_args (from_frame);
198  n_left_from = from_frame->n_vectors;
199 
200  next_index = node->cached_next_index;
201 
202  while (n_left_from > 0)
203  {
204  u32 n_left_to_next;
205 
206  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
207 
208  while (n_left_from > 0 && n_left_to_next > 0)
209  {
210  u32 bi0, next0;
211  vlib_buffer_t *b0;
212  ip4_header_t *ip0;
213  esp_header_t *esp0;
214  ip4_ipsec_config_t *c0;
215  ipsec_spd_t *spd0;
216  ipsec_policy_t *p0 = 0;
217 
218  bi0 = to_next[0] = from[0];
219  from += 1;
220  n_left_from -= 1;
221  to_next += 1;
222  n_left_to_next -= 1;
223 
224  b0 = vlib_get_buffer (vm, bi0);
227  &next0, sizeof (c0[0]));
228 
229  spd0 = pool_elt_at_index (im->spds, c0->spd_index);
230 
231  ip0 = vlib_buffer_get_current (b0);
232  esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
233 
234  if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
235  {
236 #if 0
238  ("packet received from %U to %U spi %u size %u spd_id %u",
241  clib_net_to_host_u32 (esp0->spi),
242  clib_net_to_host_u16 (ip0->length), spd0->id);
243 #endif
244 
246  clib_net_to_host_u32
247  (ip0->src_address.
248  as_u32),
249  clib_net_to_host_u32
250  (ip0->dst_address.
251  as_u32),
252  clib_net_to_host_u32
253  (esp0->spi));
254 
255  if (PREDICT_TRUE (p0 != 0))
256  {
257  p0->counter.packets++;
258  p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
259  vnet_buffer (b0)->output_features.ipsec_sad_index =
260  p0->sa_index;
261  vnet_buffer (b0)->output_features.ipsec_flags = 0;
262  next0 = IPSEC_INPUT_NEXT_ESP_DECRYPT;
264  goto trace0;
265  }
266  }
267 
268  /* FIXME bypass and discard */
269 
270  trace0:
272  {
273  ipsec_input_trace_t *tr =
274  vlib_add_trace (vm, node, b0, sizeof (*tr));
275  if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
276  {
277  if (p0)
278  tr->sa_id = p0->sa_id;
279  tr->spi = clib_host_to_net_u32 (esp0->spi);
280  tr->seq = clib_host_to_net_u32 (esp0->seq);
281  }
282  }
283 
284  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
285  to_next, n_left_to_next, bi0,
286  next0);
287  }
288  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
289  }
291  IPSEC_INPUT_ERROR_RX_PKTS,
292  from_frame->n_vectors);
293 
294  return from_frame->n_vectors;
295 }
296 
297 
298 /* *INDENT-OFF* */
300  .function = ipsec_input_ip4_node_fn,
301  .name = "ipsec-input-ip4",
302  .vector_size = sizeof (u32),
303  .format_trace = format_ipsec_input_trace,
305 
307  .error_strings = ipsec_input_error_strings,
308 
309  .n_next_nodes = IPSEC_INPUT_N_NEXT,
310  .next_nodes = {
311 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
313 #undef _
314  },
315 };
316 /* *INDENT-ON* */
317 
320 
321  static uword
323  vlib_node_runtime_t * node,
324  vlib_frame_t * from_frame)
325 {
326  ip6_main_t *i6m = &ip6_main;
327  ip_lookup_main_t *lm = &i6m->lookup_main;
330  u32 n_left_from, *from, next_index, *to_next;
331  ipsec_main_t *im = &ipsec_main;
332 
333  from = vlib_frame_vector_args (from_frame);
334  n_left_from = from_frame->n_vectors;
335 
336  next_index = node->cached_next_index;
337 
338  while (n_left_from > 0)
339  {
340  u32 n_left_to_next;
341 
342  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
343 
344  while (n_left_from > 0 && n_left_to_next > 0)
345  {
346  u32 bi0, next0;
347  vlib_buffer_t *b0;
348  ip6_header_t *ip0;
349  esp_header_t *esp0;
350  ip4_ipsec_config_t *c0;
351  ipsec_spd_t *spd0;
352  ipsec_policy_t *p0 = 0;
353  u32 header_size = sizeof (ip0[0]);
354 
355  bi0 = to_next[0] = from[0];
356  from += 1;
357  n_left_from -= 1;
358  to_next += 1;
359  n_left_to_next -= 1;
360 
361  b0 = vlib_get_buffer (vm, bi0);
364  &next0, sizeof (c0[0]));
365 
366  spd0 = pool_elt_at_index (im->spds, c0->spd_index);
367 
368  ip0 = vlib_buffer_get_current (b0);
369  esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
370 
371  if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
372  {
373 #if 0
375  ("packet received from %U to %U spi %u size %u spd_id %u",
377  &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
378  clib_net_to_host_u16 (ip0->payload_length) + header_size,
379  spd0->id);
380 #endif
382  &ip0->src_address,
383  &ip0->dst_address,
384  clib_net_to_host_u32
385  (esp0->spi));
386 
387  if (PREDICT_TRUE (p0 != 0))
388  {
389  p0->counter.packets++;
390  p0->counter.bytes +=
391  clib_net_to_host_u16 (ip0->payload_length);
392  p0->counter.bytes += header_size;
393  vnet_buffer (b0)->output_features.ipsec_sad_index =
394  p0->sa_index;
395  vnet_buffer (b0)->output_features.ipsec_flags = 0;
396  next0 = IPSEC_INPUT_NEXT_ESP_DECRYPT;
397  vlib_buffer_advance (b0, header_size);
398  goto trace0;
399  }
400  }
401 
402  trace0:
404  {
405  ipsec_input_trace_t *tr =
406  vlib_add_trace (vm, node, b0, sizeof (*tr));
407  if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
408  {
409  if (p0)
410  tr->sa_id = p0->sa_id;
411  tr->spi = clib_host_to_net_u32 (esp0->spi);
412  tr->seq = clib_host_to_net_u32 (esp0->seq);
413  }
414  }
415 
416  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
417  n_left_to_next, bi0, next0);
418  }
419  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
420  }
422  IPSEC_INPUT_ERROR_RX_PKTS,
423  from_frame->n_vectors);
424 
425  return from_frame->n_vectors;
426 }
427 
428 
429 /* *INDENT-OFF* */
431  .function = ipsec_input_ip6_node_fn,
432  .name = "ipsec-input-ip6",
433  .vector_size = sizeof (u32),
434  .format_trace = format_ipsec_input_trace,
436 
438  .error_strings = ipsec_input_error_strings,
439 
440  .n_next_nodes = IPSEC_INPUT_N_NEXT,
441  .next_nodes = {
442 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
444 #undef _
445  },
446 };
447 /* *INDENT-ON* */
448 
450 /*
451  * fd.io coding-style-patch-verification: ON
452  *
453  * Local Variables:
454  * eval: (c-set-style "gnu")
455  * End:
456  */
vnet_config_main_t config_main
Definition: feature.h:55
ip46_address_t stop
Definition: ipsec.h:104
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:457
static char * ipsec_input_error_strings[]
Definition: ipsec_input.c:51
u32 * ipv6_inbound_protect_policy_indices
Definition: ipsec.h:183
ipsec_spd_t * spds
Definition: ipsec.h:207
u64 packets
packet counter
Definition: counter.h:166
u32 * ipv4_inbound_protect_policy_indices
Definition: ipsec.h:181
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define CLIB_UNUSED(x)
Definition: clib.h:79
ip46_address_t tunnel_src_addr
Definition: ipsec.h:91
a
Definition: bitmap.h:516
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:124
format_function_t format_ip6_address
Definition: format.h:94
ip4_address_t src_address
Definition: ip4_packet.h:138
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
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:322
#define PREDICT_TRUE(x)
Definition: clib.h:98
u64 as_u64[2]
Definition: ip6_packet.h:50
static int ip4_header_bytes(ip4_header_t *i)
Definition: ip4_packet.h:186
static ipsec_policy_t * ipsec_input_protect_policy_match(ipsec_spd_t *spd, u32 sa, u32 da, u32 spi)
Definition: ipsec_input.c:90
ipsec_main_t ipsec_main
Definition: ipsec.h:238
u8 is_tunnel
Definition: ipsec.h:89
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:134
ip_lookup_main_t lookup_main
Definition: ip4.h:96
static u8 * format_ipsec_input_trace(u8 *s, va_list *args)
Definition: ipsec_input.c:66
format_function_t format_ip4_address
Definition: format.h:78
ip6_address_t src_address
Definition: ip6_packet.h:300
u32 spi
Definition: ipsec.h:75
static vlib_node_registration_t ipsec_input_ip6_node
(constructor) VLIB_REGISTER_NODE (ipsec_input_ip6_node)
Definition: ipsec_input.c:319
#define always_inline
Definition: clib.h:84
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:190
ip4_address_t dst_address
Definition: ip4_packet.h:138
static uword ip6_address_is_equal(ip6_address_t *a, ip6_address_t *b)
Definition: ip6_packet.h:176
#define clib_warning(format, args...)
Definition: error.h:59
unsigned long u64
Definition: types.h:89
ipsec_policy_t * policies
Definition: ipsec.h:177
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
ipsec_input_error_t
Definition: ipsec_input.c:43
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:122
#define PREDICT_FALSE(x)
Definition: clib.h:97
ip46_address_range_t laddr
Definition: ipsec.h:158
#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:350
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1113
ip46_address_t tunnel_dst_addr
Definition: ipsec.h:92
vnet_feature_config_main_t feature_config_mains[VNET_N_IP_FEAT]
rx unicast, multicast, tx interface/feature configuration.
Definition: lookup.h:360
u16 n_vectors
Definition: node.h:344
ipsec_input_next_t
Definition: ipsec_input.c:30
u32 sa_index
Definition: ipsec.h:167
u64 bytes
byte counter
Definition: counter.h:167
ip46_address_t start
Definition: ipsec.h:104
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:203
#define ARRAY_LEN(x)
Definition: clib.h:59
ip46_address_range_t raddr
Definition: ipsec.h:159
u16 cached_next_index
Definition: node.h:463
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:183
#define vnet_buffer(b)
Definition: buffer.h:333
ip6_main_t ip6_main
Definition: ip6_forward.c:2655
ip_lookup_main_t lookup_main
Definition: ip6.h:132
ipsec_sa_t * sad
Definition: ipsec.h:208
IPv4 main type.
Definition: ip4.h:95
u32 seq
Definition: esp.h:26
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:186
u32 spi
Definition: esp.h:25
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:144
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:95
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
#define foreach_ipsec_input_error
Definition: ipsec_input.c:38
u16 payload_length
Definition: ip6_packet.h:291
unsigned char u8
Definition: types.h:56
vlib_counter_t counter
Definition: ipsec.h:170
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
u32 id
Definition: ipsec.h:175
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1060
#define vec_foreach(var, vec)
Vector iterator.
#define foreach_ipsec_input_next
Definition: ipsec_input.c:25
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
ip6_address_t dst_address
Definition: ip6_packet.h:300