FD.io VPP  v16.09
Vector Packet Processing
ipsec_output.c
Go to the documentation of this file.
1 /*
2  * ipsec_output.c : IPSec output node
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 
24 #if IPSEC > 0
25 
26 #define foreach_ipsec_output_next \
27 _(DROP, "error-drop") \
28 _(ESP_ENCRYPT, "esp-encrypt")
29 
30 #define _(v, s) IPSEC_OUTPUT_NEXT_##v,
31 typedef enum
32 {
33  foreach_intf_output_feat foreach_ipsec_output_next
34 #undef _
35  IPSEC_OUTPUT_N_NEXT,
36 } ipsec_output_next_t;
37 
38 
39 #define foreach_ipsec_output_error \
40  _(RX_PKTS, "IPSec pkts received") \
41  _(POLICY_DISCARD, "IPSec policy discard") \
42  _(POLICY_NO_MATCH, "IPSec policy (no match)") \
43  _(POLICY_PROTECT, "IPSec policy protect") \
44  _(POLICY_BYPASS, "IPSec policy bypass") \
45  _(ENCAPS_FAILED, "IPSec encapsulation failed")
46 
47 
48 typedef enum
49 {
50 #define _(sym,str) IPSEC_OUTPUT_ERROR_##sym,
51  foreach_ipsec_output_error
52 #undef _
53  IPSEC_DECAP_N_ERROR,
54 } ipsec_output_error_t;
55 
56 static char *ipsec_output_error_strings[] = {
57 #define _(sym,string) string,
58  foreach_ipsec_output_error
59 #undef _
60 };
61 
63 
64 typedef struct
65 {
66  u32 spd_id;
67 } ipsec_output_trace_t;
68 
69 /* packet trace format function */
70 static u8 *
71 format_ipsec_output_trace (u8 * s, va_list * args)
72 {
73  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75  ipsec_output_trace_t *t = va_arg (*args, ipsec_output_trace_t *);
76 
77  if (t->spd_id != ~0)
78  {
79  s = format (s, "spd %u ", t->spd_id);
80  }
81  else
82  {
83  s = format (s, "no spd");
84  }
85  return s;
86 }
87 
88 always_inline intf_output_feat_t __attribute__ ((unused))
89 get_next_intf_output_feature_and_reset_bit (vlib_buffer_t * b)
90 {
91  u32 next_feature;
92  count_trailing_zeros (next_feature,
93  vnet_buffer (b)->output_features.bitmap);
94  if (next_feature != INTF_OUTPUT_FEAT_DONE)
95  vnet_buffer (b)->output_features.bitmap &= ~(1 << next_feature);
96  return next_feature;
97 }
98 
100 ipsec_output_policy_match (ipsec_spd_t * spd, u8 pr, u32 la, u32 ra, u16 lp,
101  u16 rp)
102 {
103  ipsec_policy_t *p;
104  u32 *i;
105 
106  if (!spd)
107  return 0;
108 
110  {
111  p = pool_elt_at_index (spd->policies, *i);
112  if (PREDICT_FALSE (p->protocol && (p->protocol != pr)))
113  continue;
114 
115  if (la < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
116  continue;
117 
118  if (la > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
119  continue;
120 
121  if (ra < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
122  continue;
123 
124  if (ra > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
125  continue;
126 
127  if (PREDICT_FALSE ((pr != IP_PROTOCOL_TCP) && (pr != IP_PROTOCOL_UDP)))
128  return p;
129 
130  if (lp < p->lport.start)
131  continue;
132 
133  if (lp > p->lport.stop)
134  continue;
135 
136  if (rp < p->rport.start)
137  continue;
138 
139  if (rp > p->rport.stop)
140  continue;
141 
142  return p;
143  }
144  return 0;
145 }
146 
149  ip6_address_t * ua)
150 {
151  if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
152  (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
153  return 1;
154  return 0;
155 }
156 
158 ipsec_output_ip6_policy_match (ipsec_spd_t * spd,
159  ip6_address_t * la,
160  ip6_address_t * ra, u16 lp, u16 rp, u8 pr)
161 {
162  ipsec_policy_t *p;
163  u32 *i;
164 
165  if (!spd)
166  return 0;
167 
169  {
170  p = pool_elt_at_index (spd->policies, *i);
171  if (PREDICT_FALSE (p->protocol && (p->protocol != pr)))
172  continue;
173 
174  if (!ip6_addr_match_range (ra, &p->raddr.start.ip6, &p->raddr.stop.ip6))
175  continue;
176 
177  if (!ip6_addr_match_range (la, &p->laddr.start.ip6, &p->laddr.stop.ip6))
178  continue;
179 
180  if (PREDICT_FALSE ((pr != IP_PROTOCOL_TCP) && (pr != IP_PROTOCOL_UDP)))
181  return p;
182 
183  if (lp < p->lport.start)
184  continue;
185 
186  if (lp > p->lport.stop)
187  continue;
188 
189  if (rp < p->rport.start)
190  continue;
191 
192  if (rp > p->rport.stop)
193  continue;
194 
195  return p;
196  }
197 
198  return 0;
199 }
200 
201 static uword
203  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
204 {
205  ipsec_main_t *im = &ipsec_main;
206  vnet_main_t *vnm = im->vnet_main;
207 
208  u32 *from, *to_next = 0;
209  u32 n_left_from, sw_if_index0, last_sw_if_index = (u32) ~ 0;
210  u32 next_node_index = (u32) ~ 0, last_next_node_index = (u32) ~ 0;
211  vlib_frame_t *f = 0;
212  u32 spd_index0 = ~0;
213  ipsec_spd_t *spd0 = 0;
214  u64 nc_protect = 0, nc_bypass = 0, nc_discard = 0, nc_nomatch = 0;
215 
216  from = vlib_frame_vector_args (from_frame);
217  n_left_from = from_frame->n_vectors;
218 
219  while (n_left_from > 0)
220  {
221  u32 bi0;
222  vlib_buffer_t *b0;
223  ipsec_policy_t *p0;
224  ip4_header_t *ip0 = 0;
225  ip6_header_t *ip6_0 = 0;
226  udp_header_t *udp0;
227  u8 is_ipv6 = 0, is_ipv4 = 0;
228  u32 iph_offset = 0;
229  u32 adj_index0;
230  ip_adjacency_t *adj0 = 0;
231 
232  bi0 = from[0];
233  b0 = vlib_get_buffer (vm, bi0);
234  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
235  adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
236 
237  /* Assume ipv4 */
238  if (PREDICT_TRUE
239  (adj_index0 < vec_len (ip4_main.lookup_main.adjacency_heap)))
240  {
241  adj0 = ip_get_adjacency (&ip4_main.lookup_main, adj_index0);
242  iph_offset = adj0->rewrite_header.data_bytes;
243  ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0)
244  + iph_offset);
245  is_ipv4 = ((ip0->ip_version_and_header_length & 0xF0) == 0x40);
246  }
247 
248  /* Test ipv4 assumption. If true, just forward packets */
249  if (PREDICT_FALSE (!is_ipv4))
250  {
251  /* Packet is not ipv4, try ipv6 */
252  if (PREDICT_TRUE
253  (adj_index0 < vec_len (ip6_main.lookup_main.adjacency_heap)))
254  {
255  adj0 = ip_get_adjacency (&ip6_main.lookup_main, adj_index0);
256  iph_offset = adj0->rewrite_header.data_bytes;
257  ip6_0 = (ip6_header_t *) ((u8 *) vlib_buffer_get_current (b0)
258  + iph_offset);
259  is_ipv6 =
260  ((ip6_0->ip_version_traffic_class_and_flow_label & 0xF0) ==
261  0x60);
262  }
263 
264  /* If not ipv6, we're done, go to next output feature */
265  if (PREDICT_FALSE (!is_ipv6))
266  {
267  next_node_index = get_next_output_feature_node_index (vnm, b0);
268  goto dispatch0;
269  }
270  }
271 
272  /* lookup for SPD only if sw_if_index is changed */
273  if (PREDICT_FALSE (last_sw_if_index != sw_if_index0))
274  {
275  uword *p = hash_get (im->spd_index_by_sw_if_index, sw_if_index0);
276  ASSERT (p);
277  spd_index0 = p[0];
278  spd0 = pool_elt_at_index (im->spds, spd_index0);
279  last_sw_if_index = sw_if_index0;
280  }
281 
282  if (is_ipv6)
283  {
284  udp0 = ip6_next_header (ip6_0);
285 #if 0
287  ("packet received from %U port %u to %U port %u spd_id %u",
289  clib_net_to_host_u16 (udp0->src_port), format_ip6_address,
290  &ip6_0->dst_address, clib_net_to_host_u16 (udp0->dst_port),
291  spd0->id);
292 #endif
293 
294  p0 = ipsec_output_ip6_policy_match (spd0,
295  &ip6_0->src_address,
296  &ip6_0->dst_address,
297  clib_net_to_host_u16
298  (udp0->src_port),
299  clib_net_to_host_u16
300  (udp0->dst_port),
301  ip6_0->protocol);
302  }
303  else
304  {
305  udp0 = (udp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
306 
307 #if 0
308  clib_warning ("packet received from %U to %U port %u",
311  clib_net_to_host_u16 (udp0->dst_port));
312  clib_warning ("sw_if_index0 %u spd_index0 %u spd_id %u",
313  sw_if_index0, spd_index0, spd0->id);
314 #endif
315 
316  p0 = ipsec_output_policy_match (spd0, ip0->protocol,
317  clib_net_to_host_u32
318  (ip0->src_address.as_u32),
319  clib_net_to_host_u32
320  (ip0->dst_address.as_u32),
321  clib_net_to_host_u16
322  (udp0->src_port),
323  clib_net_to_host_u16
324  (udp0->dst_port));
325  }
326 
327  if (PREDICT_TRUE (p0 != NULL))
328  {
329  if (p0->policy == IPSEC_POLICY_ACTION_PROTECT)
330  {
331  nc_protect++;
332  next_node_index = im->esp_encrypt_node_index;
333  vnet_buffer (b0)->output_features.ipsec_sad_index =
334  p0->sa_index;
335  vlib_buffer_advance (b0, iph_offset);
336  p0->counter.packets++;
337  if (is_ipv6)
338  {
339  p0->counter.bytes +=
340  clib_net_to_host_u16 (ip6_0->payload_length);
341  p0->counter.bytes += sizeof (ip6_header_t);
342  }
343  else
344  {
345  p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
346  }
347  }
348  else if (p0->policy == IPSEC_POLICY_ACTION_BYPASS)
349  {
350  nc_bypass++;
351  next_node_index = get_next_output_feature_node_index (vnm, b0);
352  p0->counter.packets++;
353  if (is_ipv6)
354  {
355  p0->counter.bytes +=
356  clib_net_to_host_u16 (ip6_0->payload_length);
357  p0->counter.bytes += sizeof (ip6_header_t);
358  }
359  else
360  {
361  p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
362  }
363  }
364  else
365  {
366  nc_discard++;
367  p0->counter.packets++;
368  if (is_ipv6)
369  {
370  p0->counter.bytes +=
371  clib_net_to_host_u16 (ip6_0->payload_length);
372  p0->counter.bytes += sizeof (ip6_header_t);
373  }
374  else
375  {
376  p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
377  }
378  next_node_index = im->error_drop_node_index;
379  }
380  }
381  else
382  {
383  nc_nomatch++;
384  next_node_index = im->error_drop_node_index;
385  }
386 
387  dispatch0:
388  from += 1;
389  n_left_from -= 1;
390 
391  if (PREDICT_FALSE ((last_next_node_index != next_node_index) || f == 0))
392  {
393  /* if this is not 1st frame */
394  if (f)
395  vlib_put_frame_to_node (vm, last_next_node_index, f);
396 
397  last_next_node_index = next_node_index;
398 
399  f = vlib_get_frame_to_node (vm, next_node_index);
400  to_next = vlib_frame_vector_args (f);
401  }
402 
403  to_next[0] = bi0;
404  to_next += 1;
405  f->n_vectors++;
406 
408  {
409  ipsec_output_trace_t *tr =
410  vlib_add_trace (vm, node, b0, sizeof (*tr));
411  if (spd0)
412  tr->spd_id = spd0->id;
413  }
414  }
415 
416  vlib_put_frame_to_node (vm, next_node_index, f);
417  vlib_node_increment_counter (vm, ipsec_output_node.index,
418  IPSEC_OUTPUT_ERROR_POLICY_PROTECT, nc_protect);
419  vlib_node_increment_counter (vm, ipsec_output_node.index,
420  IPSEC_OUTPUT_ERROR_POLICY_BYPASS, nc_bypass);
421  vlib_node_increment_counter (vm, ipsec_output_node.index,
422  IPSEC_OUTPUT_ERROR_POLICY_DISCARD, nc_discard);
423  vlib_node_increment_counter (vm, ipsec_output_node.index,
424  IPSEC_OUTPUT_ERROR_POLICY_NO_MATCH,
425  nc_nomatch);
426  return from_frame->n_vectors;
427 }
428 
429 /* *INDENT-OFF* */
430 VLIB_REGISTER_NODE (ipsec_output_node,static) = {
431  .function = ipsec_output_node_fn,
432  .name = "ipsec-output",
433  .vector_size = sizeof (u32),
434  .format_trace = format_ipsec_output_trace,
436 
437  .n_errors = ARRAY_LEN(ipsec_output_error_strings),
438  .error_strings = ipsec_output_error_strings,
439 
440  .n_next_nodes = IPSEC_OUTPUT_N_NEXT,
441  .next_nodes = {
442 #define _(s,n) [IPSEC_OUTPUT_NEXT_##s] = n,
444  foreach_ipsec_output_next
445 #undef _
446  },
447 };
448 /* *INDENT-ON* */
449 
451 #else /* IPSEC > 1 */
452 
453 /* Dummy ipsec output node, in case when IPSec is disabled */
454 
455 static uword
457  vlib_node_runtime_t * node, vlib_frame_t * frame)
458 {
459  clib_warning ("IPSec disabled");
460  return 0;
461 }
462 
463 /* *INDENT-OFF* */
464 VLIB_REGISTER_NODE (ipsec_output_node) = {
465  .vector_size = sizeof (u32),
466  .function = ipsec_output_node_fn,
467  .name = "ipsec-output",
468 };
469 /* *INDENT-ON* */
470 #endif
471 
472 /*
473  * fd.io coding-style-patch-verification: ON
474  *
475  * Local Variables:
476  * eval: (c-set-style "gnu")
477  * End:
478  */
ip46_address_t stop
Definition: ipsec.h:104
ipsec_spd_t * spds
Definition: ipsec.h:207
u64 packets
packet counter
Definition: counter.h:166
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define CLIB_UNUSED(x)
Definition: clib.h:79
u16 stop
Definition: ipsec.h:109
ip_adjacency_t * adjacency_heap
Adjacency heap.
Definition: lookup.h:401
#define INTF_OUTPUT_FEAT_DONE
Definition: interface.h:552
format_function_t format_ip6_address
Definition: format.h:87
ip4_address_t src_address
Definition: ip4_packet.h:138
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
#define PREDICT_TRUE(x)
Definition: clib.h:98
u64 as_u64[2]
Definition: ip6_packet.h:50
IP unicast adjacency.
Definition: lookup.h:164
static int ip4_header_bytes(ip4_header_t *i)
Definition: ip4_packet.h:186
#define NULL
Definition: clib.h:55
ipsec_main_t ipsec_main
Definition: ipsec.h:238
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:130
intf_output_feat_t
Definition: interface.h:543
u32 * ipv4_outbound_policies
Definition: ipsec.h:179
ip_lookup_main_t lookup_main
Definition: ip4.h:115
format_function_t format_ip4_address
Definition: format.h:71
ip6_address_t src_address
Definition: ip6_packet.h:298
port_range_t lport
Definition: ipsec.h:161
uword * spd_index_by_sw_if_index
Definition: ipsec.h:227
#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:187
ip4_address_t dst_address
Definition: ip4_packet.h:138
#define clib_warning(format, args...)
Definition: error.h:59
unsigned long u64
Definition: types.h:89
ipsec_policy_t * policies
Definition: ipsec.h:177
static_always_inline u32 get_next_output_feature_node_index(vnet_main_t *vnm, vlib_buffer_t *b)
Definition: ipsec.h:311
u32 error_drop_node_index
Definition: ipsec.h:232
#define hash_get(h, key)
Definition: hash.h:248
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
vnet_main_t * vnet_main
Definition: ipsec.h:220
#define PREDICT_FALSE(x)
Definition: clib.h:97
ip46_address_range_t laddr
Definition: ipsec.h:158
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:194
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1111
vlib_node_registration_t ipsec_output_node
(constructor) VLIB_REGISTER_NODE (ipsec_output_node)
Definition: ipsec_output.c:464
u16 n_vectors
Definition: node.h:344
u32 esp_encrypt_node_index
Definition: ipsec.h:234
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:200
#define ARRAY_LEN(x)
Definition: clib.h:59
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:302
port_range_t rport
Definition: ipsec.h:162
ip46_address_range_t raddr
Definition: ipsec.h:159
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:335
ip6_main_t ip6_main
Definition: ip6_forward.c:2955
ip_lookup_main_t lookup_main
Definition: ip6.h:110
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
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
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:285
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
u16 payload_length
Definition: ip6_packet.h:289
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
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:251
static uword ipsec_output_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ipsec_output.c:456
u32 * ipv6_outbound_policies
Definition: ipsec.h:180
u32 id
Definition: ipsec.h:175
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
#define foreach_intf_output_feat
Definition: interface.h:539
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1578
#define vec_foreach(var, vec)
Vector iterator.
u8 ip_version_and_header_length
Definition: ip4_packet.h:108
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:185
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:298
static ip_adjacency_t * ip_get_adjacency(ip_lookup_main_t *lm, u32 adj_index)
Definition: lookup.h:480