FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
ah_encrypt.c
Go to the documentation of this file.
1 /*
2  * ah_encrypt.c : IPSec AH encrypt 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 #include <vnet/ipsec/esp.h>
24 #include <vnet/ipsec/ah.h>
25 
26 #define foreach_ah_encrypt_next \
27  _ (DROP, "error-drop") \
28  _ (IP4_LOOKUP, "ip4-lookup") \
29  _ (IP6_LOOKUP, "ip6-lookup") \
30  _ (INTERFACE_OUTPUT, "interface-output")
31 
32 
33 #define _(v, s) AH_ENCRYPT_NEXT_##v,
34 typedef enum
35 {
37 #undef _
40 
41 #define foreach_ah_encrypt_error \
42  _(RX_PKTS, "AH pkts received") \
43  _(SEQ_CYCLED, "sequence number cycled")
44 
45 
46 typedef enum
47 {
48 #define _(sym,str) AH_ENCRYPT_ERROR_##sym,
50 #undef _
53 
54 static char *ah_encrypt_error_strings[] = {
55 #define _(sym,string) string,
57 #undef _
58 };
59 
60 typedef struct
61 {
66 
67 /* packet trace format function */
68 static u8 *
69 format_ah_encrypt_trace (u8 * s, va_list * args)
70 {
71  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
72  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
73  ah_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *);
74 
75  s = format (s, "ah: spi %u seq %u integrity %U",
77  return s;
78 }
79 
82  vlib_node_runtime_t * node, vlib_frame_t * from_frame,
83  int is_ip6)
84 {
85  u32 n_left_from, *from, *to_next = 0, next_index;
86  int icv_size = 0;
87  from = vlib_frame_vector_args (from_frame);
88  n_left_from = from_frame->n_vectors;
89  ipsec_main_t *im = &ipsec_main;
91  next_index = node->cached_next_index;
92 
93  while (n_left_from > 0)
94  {
95  u32 n_left_to_next;
96 
97  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
98 
99  while (n_left_from > 0 && n_left_to_next > 0)
100  {
101  u32 i_bi0, next0;
102  vlib_buffer_t *i_b0 = 0;
103  u32 sa_index0;
104  ipsec_sa_t *sa0;
105  ip4_and_ah_header_t *ih0, *oh0 = 0;
106  ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
107  u8 ip_hdr_size;
108  u8 next_hdr_type;
109  u8 tos = 0;
110  u8 ttl = 0;
111  u8 hop_limit = 0;
112  u32 ip_version_traffic_class_and_flow_label = 0;
113 
114  i_bi0 = from[0];
115  from += 1;
116  n_left_from -= 1;
117  n_left_to_next -= 1;
118  next0 = AH_ENCRYPT_NEXT_DROP;
119 
120  i_b0 = vlib_get_buffer (vm, i_bi0);
121  to_next[0] = i_bi0;
122  to_next += 1;
123  sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
124  sa0 = pool_elt_at_index (im->sad, sa_index0);
125 
126  if (PREDICT_FALSE (esp_seq_advance (sa0)))
127  {
128  clib_warning ("sequence number counter has cycled SPI %u",
129  sa0->spi);
131  AH_ENCRYPT_ERROR_SEQ_CYCLED, 1);
132  //TODO need to confirm if below is needed
133  to_next[0] = i_bi0;
134  to_next += 1;
135  goto trace;
136  }
137 
138 
139  sa0->total_data_size += i_b0->current_length;
140 
141  ssize_t adv;
142  ih0 = vlib_buffer_get_current (i_b0);
143  ttl = ih0->ip4.ttl;
144  tos = ih0->ip4.tos;
145 
146  if (PREDICT_TRUE (sa0->is_tunnel))
147  {
148  if (is_ip6)
149  adv = -sizeof (ip6_and_ah_header_t);
150  else
151  adv = -sizeof (ip4_and_ah_header_t);
152  }
153  else
154  {
155  adv = -sizeof (ah_header_t);
156  }
157 
158  icv_size =
160  const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
161  adv -= padding_len;
162  /* transport mode save the eth header before it is overwritten */
163  if (PREDICT_FALSE (!sa0->is_tunnel))
164  {
166  ((u8 *) vlib_buffer_get_current (i_b0) -
167  sizeof (ethernet_header_t));
168  ethernet_header_t *oeh0 =
169  (ethernet_header_t *) ((u8 *) ieh0 + (adv - icv_size));
170  clib_memcpy_fast (oeh0, ieh0, sizeof (ethernet_header_t));
171  }
172 
173  vlib_buffer_advance (i_b0, adv - icv_size);
174 
175  if (is_ip6)
176  {
177  ih6_0 = (ip6_and_ah_header_t *) ih0;
178  ip_hdr_size = sizeof (ip6_header_t);
179  oh6_0 = vlib_buffer_get_current (i_b0);
180 
181  hop_limit = ih6_0->ip6.hop_limit;
182  ip_version_traffic_class_and_flow_label =
183  ih6_0->ip6.ip_version_traffic_class_and_flow_label;
184  if (PREDICT_TRUE (sa0->is_tunnel))
185  {
186  next_hdr_type = IP_PROTOCOL_IPV6;
187  }
188  else
189  {
190  next_hdr_type = ih6_0->ip6.protocol;
191  memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
192  }
193 
194  oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_AH;
195  oh6_0->ip6.hop_limit = 0;
196  oh6_0->ip6.ip_version_traffic_class_and_flow_label = 0x60;
197  oh6_0->ah.reserved = 0;
198  oh6_0->ah.nexthdr = next_hdr_type;
199  oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi);
200  oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
201  oh6_0->ip6.payload_length =
202  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0) -
203  sizeof (ip6_header_t));
204  oh6_0->ah.hdrlen =
205  (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
206  }
207  else
208  {
209  ip_hdr_size = sizeof (ip4_header_t);
210  oh0 = vlib_buffer_get_current (i_b0);
211  clib_memset (oh0, 0, sizeof (ip4_and_ah_header_t));
212 
213  if (PREDICT_TRUE (sa0->is_tunnel))
214  {
215  next_hdr_type = IP_PROTOCOL_IP_IN_IP;
216  }
217  else
218  {
219  next_hdr_type = ih0->ip4.protocol;
220  memmove (oh0, ih0, sizeof (ip4_header_t));
221  }
222 
223  oh0->ip4.length =
224  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0));
225  oh0->ip4.ip_version_and_header_length = 0x45;
226  oh0->ip4.fragment_id = 0;
227  oh0->ip4.flags_and_fragment_offset = 0;
228  oh0->ip4.ttl = 0;
229  oh0->ip4.tos = 0;
230  oh0->ip4.protocol = IP_PROTOCOL_IPSEC_AH;
231  oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
232  oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
233  oh0->ip4.checksum = 0;
234  oh0->ah.nexthdr = next_hdr_type;
235  oh0->ah.hdrlen =
236  (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
237  }
238 
239 
240  if (PREDICT_TRUE (!is_ip6 && sa0->is_tunnel && !sa0->is_tunnel_ip6))
241  {
242  oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32;
243  oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32;
244 
245  next0 = AH_ENCRYPT_NEXT_IP4_LOOKUP;
246  vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
247  }
248  else if (is_ip6 && sa0->is_tunnel && sa0->is_tunnel_ip6)
249  {
250  oh6_0->ip6.src_address.as_u64[0] =
251  sa0->tunnel_src_addr.ip6.as_u64[0];
252  oh6_0->ip6.src_address.as_u64[1] =
253  sa0->tunnel_src_addr.ip6.as_u64[1];
254  oh6_0->ip6.dst_address.as_u64[0] =
255  sa0->tunnel_dst_addr.ip6.as_u64[0];
256  oh6_0->ip6.dst_address.as_u64[1] =
257  sa0->tunnel_dst_addr.ip6.as_u64[1];
258 
259  next0 = AH_ENCRYPT_NEXT_IP6_LOOKUP;
260  vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
261  }
262 
263  u8 sig[64];
264  clib_memset (sig, 0, sizeof (sig));
265  u8 *digest =
266  vlib_buffer_get_current (i_b0) + ip_hdr_size +
267  sizeof (ah_header_t);
268  clib_memset (digest, 0, icv_size);
269 
270  unsigned size = hmac_calc (sa0->integ_alg, sa0->integ_key,
271  sa0->integ_key_len,
273  i_b0->current_length, sig, sa0->use_esn,
274  sa0->seq_hi);
275 
276  memcpy (digest, sig, size);
277  if (is_ip6)
278  {
279  oh6_0->ip6.hop_limit = hop_limit;
280  oh6_0->ip6.ip_version_traffic_class_and_flow_label =
281  ip_version_traffic_class_and_flow_label;
282  }
283  else
284  {
285  oh0->ip4.ttl = ttl;
286  oh0->ip4.tos = tos;
287  oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
288  }
289 
290  if (!sa0->is_tunnel)
291  {
292  next0 = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
293  vlib_buffer_advance (i_b0, -sizeof (ethernet_header_t));
294  }
295 
296  trace:
297  if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
298  {
299  i_b0->flags |= VLIB_BUFFER_IS_TRACED;
300  ah_encrypt_trace_t *tr =
301  vlib_add_trace (vm, node, i_b0, sizeof (*tr));
302  tr->spi = sa0->spi;
303  tr->seq = sa0->seq - 1;
304  tr->integ_alg = sa0->integ_alg;
305  }
306 
307  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
308  to_next, n_left_to_next, i_bi0,
309  next0);
310  }
311  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
312  }
314  AH_ENCRYPT_ERROR_RX_PKTS,
315  from_frame->n_vectors);
316 
317  return from_frame->n_vectors;
318 }
319 
321  vlib_node_runtime_t * node,
322  vlib_frame_t * from_frame)
323 {
324  return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
325 }
326 
327 /* *INDENT-OFF* */
329  .name = "ah4-encrypt",
330  .vector_size = sizeof (u32),
331  .format_trace = format_ah_encrypt_trace,
332  .type = VLIB_NODE_TYPE_INTERNAL,
333 
334  .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
335  .error_strings = ah_encrypt_error_strings,
336 
337  .n_next_nodes = AH_ENCRYPT_N_NEXT,
338  .next_nodes = {
339 #define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
341 #undef _
342  },
343 };
344 /* *INDENT-ON* */
345 
347  vlib_node_runtime_t * node,
348  vlib_frame_t * from_frame)
349 {
350  return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
351 }
352 
353 /* *INDENT-OFF* */
355  .name = "ah6-encrypt",
356  .vector_size = sizeof (u32),
357  .format_trace = format_ah_encrypt_trace,
358  .type = VLIB_NODE_TYPE_INTERNAL,
359 
360  .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
361  .error_strings = ah_encrypt_error_strings,
362 
363  .n_next_nodes = AH_ENCRYPT_N_NEXT,
364  .next_nodes = {
365 #define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
367 #undef _
368  },
369 };
370 /* *INDENT-ON* */
371 
372 /*
373  * fd.io coding-style-patch-verification: ON
374  *
375  * Local Variables:
376  * eval: (c-set-style "gnu")
377  * End:
378  */
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:862
#define CLIB_UNUSED(x)
Definition: clib.h:82
ip46_address_t tunnel_src_addr
Definition: ipsec.h:139
ipsec_proto_main_integ_alg_t * ipsec_proto_main_integ_algs
Definition: ipsec.h:343
#define PREDICT_TRUE(x)
Definition: clib.h:112
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static unsigned int hmac_calc(ipsec_integ_alg_t alg, u8 *key, int key_len, u8 *data, int data_len, u8 *signature, u8 use_esn, u32 seq_hi)
Definition: esp.h:280
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:129
vlib_node_registration_t ah6_encrypt_node
(constructor) VLIB_REGISTER_NODE (ah6_encrypt_node)
Definition: ah_encrypt.c:354
u8 is_tunnel
Definition: ipsec.h:136
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define VLIB_NODE_FN(node)
Definition: node.h:201
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:267
unsigned char u8
Definition: types.h:56
u32 spi
Definition: ipsec.h:122
ipsec_proto_main_t ipsec_proto_main
Definition: esp_encrypt.c:26
u32 seq_hi
Definition: ipsec.h:147
static int esp_seq_advance(ipsec_sa_t *sa)
Definition: esp.h:182
static uword ah_encrypt_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, int is_ip6)
Definition: ah_encrypt.c:81
u8 integ_key[128]
Definition: ipsec.h:131
ipsec_main_t ipsec_main
Definition: ipsec.c:30
#define always_inline
Definition: clib.h:98
u8 use_esn
Definition: ipsec.h:133
unsigned int u32
Definition: types.h:88
ipsec_integ_alg_t integ_alg
Definition: ah_encrypt.c:64
static u8 ah_calc_icv_padding_len(u8 icv_size, int is_ipv6)
Definition: ah.h:53
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:511
uword size
ipsec_integ_alg_t
Definition: ipsec.h:105
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:114
u8 is_tunnel_ip6
Definition: ipsec.h:137
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:214
#define PREDICT_FALSE(x)
Definition: clib.h:111
ah_encrypt_next_t
Definition: ah_encrypt.c:34
vlib_node_registration_t ah4_encrypt_node
(constructor) VLIB_REGISTER_NODE (ah4_encrypt_node)
Definition: ah_encrypt.c:328
u32 node_index
Node index.
Definition: node.h:518
#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:218
#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
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
ip46_address_t tunnel_dst_addr
Definition: ipsec.h:140
static u8 * format_ah_encrypt_trace(u8 *s, va_list *args)
Definition: ah_encrypt.c:69
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:420
vlib_main_t * vm
Definition: buffer.c:301
static char * ah_encrypt_error_strings[]
Definition: ah_encrypt.c:54
#define clib_warning(format, args...)
Definition: error.h:59
#define ARRAY_LEN(x)
Definition: clib.h:62
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:452
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:537
#define foreach_ah_encrypt_error
Definition: ah_encrypt.c:41
ipsec_sa_t * sad
Definition: ipsec.h:353
u64 total_data_size
Definition: ipsec.h:153
u8 integ_key_len
Definition: ipsec.h:130
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:233
u32 seq
Definition: ipsec.h:146
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:57
u8 * format_ipsec_integ_alg(u8 *s, va_list *args)
Definition: ipsec_format.c:90
Definition: defs.h:47
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
#define vnet_buffer(b)
Definition: buffer.h:368
Definition: ah.h:27
ah_encrypt_error_t
Definition: ah_encrypt.c:46
#define foreach_ah_encrypt_next
Definition: ah_encrypt.c:26
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:117
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:62
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:247