FD.io VPP  v16.06
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 {
33  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 #define _(sym,str) IPSEC_OUTPUT_ERROR_##sym,
50  foreach_ipsec_output_error
51 #undef _
52  IPSEC_DECAP_N_ERROR,
53 } ipsec_output_error_t;
54 
55 static char * ipsec_output_error_strings[] = {
56 #define _(sym,string) string,
57  foreach_ipsec_output_error
58 #undef _
59 };
60 
62 
63 typedef struct {
64  u32 spd_id;
65 } ipsec_output_trace_t;
66 
67 /* packet trace format function */
68 static u8 * format_ipsec_output_trace (u8 * s, va_list * args)
69 {
70  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
72  ipsec_output_trace_t * t = va_arg (*args, ipsec_output_trace_t *);
73 
74  if (t->spd_id != ~0)
75  {
76  s = format (s, "spd %u ", t->spd_id);
77  }
78  else
79  {
80  s = format (s, "no spd");
81  }
82  return s;
83 }
84 
85 always_inline intf_output_feat_t __attribute__((unused))
86 get_next_intf_output_feature_and_reset_bit(vlib_buffer_t *b)
87 {
88  u32 next_feature;
89  count_trailing_zeros(next_feature, vnet_buffer(b)->output_features.bitmap);
90  if (next_feature != INTF_OUTPUT_FEAT_DONE)
91  vnet_buffer(b)->output_features.bitmap &= ~(1 << next_feature);
92  return next_feature;
93 }
94 
96 ipsec_output_policy_match(ipsec_spd_t * spd, u8 pr, u32 la, u32 ra, u16 lp, u16 rp)
97 {
98  ipsec_policy_t * p;
99  u32 * i;
100 
102  {
103  p = pool_elt_at_index(spd->policies, *i);
104  if (PREDICT_FALSE(p->protocol && (p->protocol != pr)))
105  continue;
106 
107  if (la < clib_net_to_host_u32(p->laddr.start.ip4.as_u32))
108  continue;
109 
110  if (la > clib_net_to_host_u32(p->laddr.stop.ip4.as_u32))
111  continue;
112 
113  if (ra < clib_net_to_host_u32(p->raddr.start.ip4.as_u32))
114  continue;
115 
116  if (ra > clib_net_to_host_u32(p->raddr.stop.ip4.as_u32))
117  continue;
118 
119  if (PREDICT_FALSE((pr != IP_PROTOCOL_TCP) && (pr != IP_PROTOCOL_UDP)))
120  return p;
121 
122  if (lp < p->lport.start)
123  continue;
124 
125  if (lp > p->lport.stop)
126  continue;
127 
128  if (rp < p->rport.start)
129  continue;
130 
131  if (rp > p->rport.stop)
132  continue;
133 
134  return p;
135  }
136  return 0;
137 }
138 
141 {
142  if ((memcmp(a->as_u64, la->as_u64, 2 * sizeof(u64)) >= 0) &&
143  (memcmp(a->as_u64, ua->as_u64, 2 * sizeof(u64)) <= 0))
144  return 1;
145  return 0;
146 }
147 
149 ipsec_output_ip6_policy_match (ipsec_spd_t * spd,
150  ip6_address_t * sa,
151  ip6_address_t * da,
152  u16 lp,
153  u16 rp,
154  u8 pr)
155 {
156  ipsec_policy_t * p;
157  u32 * i;
158 
160  {
161  p = pool_elt_at_index(spd->policies, *i);
162  if (PREDICT_FALSE(p->protocol && (p->protocol != pr)))
163  continue;
164 
165  if (!ip6_addr_match_range(sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
166  continue;
167 
168  if (!ip6_addr_match_range(da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
169  continue;
170 
171  if (PREDICT_FALSE((pr != IP_PROTOCOL_TCP) && (pr != IP_PROTOCOL_UDP)))
172  return p;
173 
174  if (lp < p->lport.start)
175  continue;
176 
177  if (lp > p->lport.stop)
178  continue;
179 
180  if (rp < p->rport.start)
181  continue;
182 
183  if (rp > p->rport.stop)
184  continue;
185 
186  return p;
187  }
188 
189  return 0;
190 }
191 static uword
193  vlib_node_runtime_t * node,
194  vlib_frame_t * from_frame)
195 {
196  ipsec_main_t *im = &ipsec_main;
197  vnet_main_t * vnm = im->vnet_main;
198 
199  u32 * from, * to_next = 0;
200  u32 n_left_from, sw_if_index0, last_sw_if_index = (u32) ~0;
201  u32 next_node_index = (u32)~0, last_next_node_index = (u32) ~0;
202  vlib_frame_t *f = 0;
203  u32 spd_index0 = ~0;
204  ipsec_spd_t * spd0 = 0;
205  u64 nc_protect = 0, nc_bypass = 0, nc_discard = 0, nc_nomatch = 0;
206 
207  from = vlib_frame_vector_args (from_frame);
208  n_left_from = from_frame->n_vectors;
209 
210  while (n_left_from > 0)
211  {
212  u32 bi0;
213  vlib_buffer_t * b0;
214  ipsec_policy_t * p0;
215  ip4_header_t * ip0;
216  ip6_header_t * ip6_0 = 0;
217  udp_header_t * udp0;
218  u8 is_ipv6 = 0;
219 
220  bi0 = from[0];
221  b0 = vlib_get_buffer (vm, bi0);
222  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
223 
224 
225  ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
226  sizeof(ethernet_header_t));
227 
228  /* just forward non ipv4 packets */
229  if (PREDICT_FALSE((ip0->ip_version_and_header_length & 0xF0 ) != 0x40))
230  {
231  /* ipv6 packets */
232  if (PREDICT_TRUE((ip0->ip_version_and_header_length & 0xF0 ) == 0x60))
233  {
234  is_ipv6 = 1;
235  ip6_0 = (ip6_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
236  sizeof(ethernet_header_t));
237  }
238  else
239  {
240  next_node_index = get_next_output_feature_node_index(vnm, b0);
241  goto dispatch0;
242  }
243  }
244 
245  /* lookup for SPD only if sw_if_index is changed */
246  if (PREDICT_FALSE(last_sw_if_index != sw_if_index0))
247  {
248  uword * p = hash_get (im->spd_index_by_sw_if_index, sw_if_index0);
249  ASSERT(p);
250  spd_index0 = p[0];
251  spd0 = pool_elt_at_index(im->spds, spd_index0);
252  last_sw_if_index = sw_if_index0;
253  }
254 
255  if (is_ipv6)
256  {
257  udp0 = ip6_next_header(ip6_0);
258 #if 0
259  clib_warning("packet received from %U port %u to %U port %u spd_id %u",
261  clib_net_to_host_u16(udp0->src_port),
263  clib_net_to_host_u16(udp0->dst_port),
264  spd0->id);
265 #endif
266 
267  p0 = ipsec_output_ip6_policy_match(spd0,
268  &ip6_0->src_address,
269  &ip6_0->dst_address,
270  clib_net_to_host_u16(udp0->src_port),
271  clib_net_to_host_u16(udp0->dst_port),
272  ip6_0->protocol);
273  }
274  else
275  {
276  udp0 = (udp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
277 
278 #if 0
279  clib_warning("packet received from %U to %U port %u",
282  clib_net_to_host_u16(udp0->dst_port));
283  clib_warning("sw_if_index0 %u spd_index0 %u spd_id %u",
284  sw_if_index0, spd_index0, spd0->id);
285 #endif
286 
287  p0 = ipsec_output_policy_match(spd0, ip0->protocol,
288  clib_net_to_host_u32(ip0->src_address.as_u32),
289  clib_net_to_host_u32(ip0->dst_address.as_u32),
290  clib_net_to_host_u16(udp0->src_port),
291  clib_net_to_host_u16(udp0->dst_port));
292  }
293 
294  if (PREDICT_TRUE(p0 != NULL))
295  {
296  if (p0->policy == IPSEC_POLICY_ACTION_PROTECT)
297  {
298  nc_protect++;
299  next_node_index = im->esp_encrypt_node_index;
300  vnet_buffer(b0)->output_features.ipsec_sad_index = p0->sa_index;
302  p0->counter.packets++;
303  if (is_ipv6)
304  {
305  p0->counter.bytes += clib_net_to_host_u16(ip6_0->payload_length);
306  p0->counter.bytes += sizeof(ip6_header_t);
307  }
308  else
309  {
310  p0->counter.bytes += clib_net_to_host_u16(ip0->length);
311  }
312  }
313  else if (p0->policy == IPSEC_POLICY_ACTION_BYPASS)
314  {
315  nc_bypass++;
316  next_node_index = get_next_output_feature_node_index(vnm, b0);
317  p0->counter.packets++;
318  if (is_ipv6)
319  {
320  p0->counter.bytes += clib_net_to_host_u16(ip6_0->payload_length);
321  p0->counter.bytes += sizeof(ip6_header_t);
322  }
323  else
324  {
325  p0->counter.bytes += clib_net_to_host_u16(ip0->length);
326  }
327  }
328  else
329  {
330  nc_discard++;
331  p0->counter.packets++;
332  if (is_ipv6)
333  {
334  p0->counter.bytes += clib_net_to_host_u16(ip6_0->payload_length);
335  p0->counter.bytes += sizeof(ip6_header_t);
336  }
337  else
338  {
339  p0->counter.bytes += clib_net_to_host_u16(ip0->length);
340  }
341  next_node_index = im->error_drop_node_index;
342  }
343  }
344  else
345  {
346  nc_nomatch++;
347  next_node_index = im->error_drop_node_index;
348  }
349 
350 dispatch0:
351  from += 1;
352  n_left_from -= 1;
353 
354  if (PREDICT_FALSE((last_next_node_index != next_node_index)))
355  {
356  /* if this is not 1st frame */
357  if (f)
358  vlib_put_frame_to_node (vm, last_next_node_index, f);
359 
360  last_next_node_index = next_node_index;
361 
362  f = vlib_get_frame_to_node(vm, next_node_index);
363  to_next = vlib_frame_vector_args (f);
364  }
365 
366  to_next[0] = bi0;
367  to_next+=1;
368  f->n_vectors++;
369 
371  ipsec_output_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
372  if (spd0)
373  tr->spd_id = spd0->id;
374  }
375  }
376 
377  vlib_put_frame_to_node (vm, next_node_index, f);
378  vlib_node_increment_counter (vm, ipsec_output_node.index,
379  IPSEC_OUTPUT_ERROR_POLICY_PROTECT, nc_protect);
380  vlib_node_increment_counter (vm, ipsec_output_node.index,
381  IPSEC_OUTPUT_ERROR_POLICY_BYPASS, nc_bypass);
382  vlib_node_increment_counter (vm, ipsec_output_node.index,
383  IPSEC_OUTPUT_ERROR_POLICY_DISCARD, nc_discard);
384  vlib_node_increment_counter (vm, ipsec_output_node.index,
385  IPSEC_OUTPUT_ERROR_POLICY_NO_MATCH, nc_nomatch);
386  return from_frame->n_vectors;
387 }
388 
389 VLIB_REGISTER_NODE (ipsec_output_node,static) = {
390  .function = ipsec_output_node_fn,
391  .name = "ipsec-output",
392  .vector_size = sizeof (u32),
393  .format_trace = format_ipsec_output_trace,
395 
396  .n_errors = ARRAY_LEN(ipsec_output_error_strings),
397  .error_strings = ipsec_output_error_strings,
398 
399  .n_next_nodes = IPSEC_OUTPUT_N_NEXT,
400  .next_nodes = {
401 #define _(s,n) [IPSEC_OUTPUT_NEXT_##s] = n,
403  foreach_ipsec_output_next
404 #undef _
405  },
406 };
407 
408 #else /* IPSEC > 1 */
409 
410 /* Dummy ipsec output node, in case when IPSec is disabled */
411 
412 static uword
414  vlib_node_runtime_t * node,
415  vlib_frame_t * frame)
416 {
417  clib_warning ("IPSec disabled");
418  return 0;
419 }
420 
421 VLIB_REGISTER_NODE (ipsec_output_node) = {
422  .vector_size = sizeof (u32),
423  .function = ipsec_output_node_fn,
424  .name = "ipsec-output",
425 };
426 #endif
ip46_address_t stop
Definition: ipsec.h:96
ipsec_spd_t * spds
Definition: ipsec.h:171
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
#define CLIB_UNUSED(x)
Definition: clib.h:79
u16 stop
Definition: ipsec.h:100
#define INTF_OUTPUT_FEAT_DONE
Definition: interface.h:505
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
#define NULL
Definition: clib.h:55
ipsec_main_t ipsec_main
Definition: ipsec.h:202
struct _vlib_node_registration vlib_node_registration_t
intf_output_feat_t
Definition: interface.h:497
u32 * ipv4_outbound_policies
Definition: ipsec.h:147
always_inline void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:297
format_function_t format_ip4_address
Definition: format.h:71
ip6_address_t src_address
Definition: ip6_packet.h:293
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
port_range_t lport
Definition: ipsec.h:130
uword * spd_index_by_sw_if_index
Definition: ipsec.h:191
#define always_inline
Definition: clib.h:84
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
always_inline int ip4_header_bytes(ip4_header_t *i)
Definition: ip4_packet.h:186
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
ipsec_policy_t * policies
Definition: ipsec.h:145
static_always_inline u32 get_next_output_feature_node_index(vnet_main_t *vnm, vlib_buffer_t *b)
Definition: ipsec.h:265
u32 error_drop_node_index
Definition: ipsec.h:196
#define hash_get(h, key)
Definition: hash.h:231
#define pool_elt_at_index(p, i)
Definition: pool.h:346
vnet_main_t * vnet_main
Definition: ipsec.h:184
#define PREDICT_FALSE(x)
Definition: clib.h:97
always_inline void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:970
ip46_address_range_t laddr
Definition: ipsec.h:127
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:191
vlib_node_registration_t ipsec_output_node
(constructor) VLIB_REGISTER_NODE (ipsec_output_node)
Definition: ipsec_output.c:421
u16 n_vectors
Definition: node.h:307
u32 esp_encrypt_node_index
Definition: ipsec.h:198
u32 sa_index
Definition: ipsec.h:136
ip46_address_t start
Definition: ipsec.h:96
#define ARRAY_LEN(x)
Definition: clib.h:59
port_range_t rport
Definition: ipsec.h:131
ip46_address_range_t raddr
Definition: ipsec.h:128
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
u64 uword
Definition: types.h:112
Definition: defs.h:46
unsigned short u16
Definition: types.h:57
u16 payload_length
Definition: ip6_packet.h:284
always_inline void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:197
unsigned char u8
Definition: types.h:56
vlib_counter_t counter
Definition: ipsec.h:139
always_inline 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
static uword ipsec_output_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ipsec_output.c:413
u32 * ipv6_outbound_policies
Definition: ipsec.h:148
u32 id
Definition: ipsec.h:143
#define foreach_intf_output_feat
Definition: interface.h:493
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
#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:184
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline 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:293
always_inline uword ip6_addr_match_range(ip6_address_t *a, ip6_address_t *la, ip6_address_t *ua)
Definition: ipsec_input.c:124