FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
esp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __ESP_H__
16 #define __ESP_H__
17 
18 #include <vnet/ip/ip.h>
19 #include <vnet/crypto/crypto.h>
20 #include <vnet/ipsec/ipsec.h>
21 
22 typedef struct
23 {
24  union
25  {
27  u8 spi_bytes[4];
28  };
30  u8 data[0];
31 } esp_header_t;
32 
33 typedef struct
34 {
37 } esp_footer_t;
38 
39 /* *INDENT-OFF* */
40 typedef CLIB_PACKED (struct {
41  ip4_header_t ip4;
42  esp_header_t esp;
43 }) ip4_and_esp_header_t;
44 /* *INDENT-ON* */
45 
46 /* *INDENT-OFF* */
47 typedef CLIB_PACKED (struct {
48  ip4_header_t ip4;
49  udp_header_t udp;
50  esp_header_t esp;
51 }) ip4_and_udp_and_esp_header_t;
52 /* *INDENT-ON* */
53 
54 /* *INDENT-OFF* */
55 typedef CLIB_PACKED (struct {
56  ip6_header_t ip6;
57  esp_header_t esp;
58 }) ip6_and_esp_header_t;
59 /* *INDENT-ON* */
60 
61 /**
62  * AES GCM Additional Authentication data
63  */
64 typedef struct esp_aead_t_
65 {
66  /**
67  * for GCM: when using ESN it's:
68  * SPI, seq-hi, seg-low
69  * else
70  * SPI, seq-low
71  */
72  u32 data[3];
73 } __clib_packed esp_aead_t;
74 
75 #define ESP_SEQ_MAX (4294967295UL)
76 #define ESP_MAX_BLOCK_SIZE (16)
77 #define ESP_MAX_IV_SIZE (16)
78 #define ESP_MAX_ICV_SIZE (32)
79 
80 u8 *format_esp_header (u8 * s, va_list * args);
81 
82 /* TODO seq increment should be atomic to be accessed by multiple workers */
83 always_inline int
85 {
86  if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
87  {
88  if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
89  {
90  if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
91  sa->seq_hi == ESP_SEQ_MAX))
92  return 1;
93  sa->seq_hi++;
94  }
95  sa->seq++;
96  }
97  else
98  {
99  if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
100  sa->seq == ESP_SEQ_MAX))
101  return 1;
102  sa->seq++;
103  }
104 
105  return 0;
106 }
107 
108 
109 always_inline unsigned int
110 hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len,
111  u8 * signature)
112 {
113  vnet_crypto_op_t _op, *op = &_op;
114 
115  if (PREDICT_FALSE (sa->integ_op_id == 0))
116  return 0;
117 
119  op->key_index = sa->integ_key_index;
120  op->src = data;
121  op->len = data_len;
122  op->digest = signature;
123  op->digest_len = sa->integ_icv_size;
124 
125  if (ipsec_sa_is_set_USE_ESN (sa))
126  {
127  u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
128 
129  op->len += 4;
130  clib_memcpy (data + data_len, &seq_hi, 4);
131  }
132 
133  vnet_crypto_process_ops (vm, op, 1);
134  return sa->integ_icv_size;
135 }
136 
137 always_inline void
139  const esp_header_t * esp, const ipsec_sa_t * sa)
140 {
141  esp_aead_t *aad;
142 
143  aad = (esp_aead_t *) op->aad;
144  aad->data[0] = esp->spi;
145 
146  if (ipsec_sa_is_set_USE_ESN (sa))
147  {
148  /* SPI, seq-hi, seq-low */
149  aad->data[1] = clib_host_to_net_u32 (sa->seq_hi);
150  aad->data[2] = esp->seq;
151  op->aad_len = 12;
152  }
153  else
154  {
155  /* SPI, seq-low */
156  aad->data[1] = esp->seq;
157  op->aad_len = 8;
158  }
159 }
160 #endif /* __ESP_H__ */
161 
162 /*
163  * fd.io coding-style-patch-verification: ON
164  *
165  * Local Variables:
166  * eval: (c-set-style "gnu")
167  * End:
168  */
u32 vnet_crypto_process_ops(vlib_main_t *vm, vnet_crypto_op_t ops[], u32 n_ops)
Definition: crypto.c:46
#define PREDICT_TRUE(x)
Definition: clib.h:113
struct esp_aead_t_ esp_aead_t
AES GCM Additional Authentication data.
AES GCM Additional Authentication data.
Definition: esp.h:64
u8 data[128]
Definition: ipsec.api:251
vnet_crypto_op_id_t integ_op_id
Definition: ipsec_sa.h:129
unsigned char u8
Definition: types.h:56
u32 seq_hi
Definition: ipsec_sa.h:119
#define clib_memcpy(d, s, n)
Definition: string.h:180
static int esp_seq_advance(ipsec_sa_t *sa)
Definition: esp.h:84
#define always_inline
Definition: clib.h:99
u32 data[3]
for GCM: when using ESN it&#39;s: SPI, seq-hi, seg-low else SPI, seq-low
Definition: esp.h:72
static_always_inline void vnet_crypto_op_init(vnet_crypto_op_t *op, vnet_crypto_op_id_t type)
Definition: crypto.h:221
unsigned int u32
Definition: types.h:88
static void esp_aad_fill(vnet_crypto_op_t *op, const esp_header_t *esp, const ipsec_sa_t *sa)
Definition: esp.h:138
static unsigned int hmac_calc(vlib_main_t *vm, ipsec_sa_t *sa, u8 *data, int data_len, u8 *signature)
Definition: esp.h:110
typedef CLIB_PACKED(struct { ip4_header_t ip4;esp_header_t esp;}) ip4_and_esp_header_t
#define PREDICT_FALSE(x)
Definition: clib.h:112
vlib_main_t * vm
Definition: buffer.c:323
u8 * format_esp_header(u8 *s, va_list *args)
Definition: esp_format.c:23
u32 seq
Definition: esp.h:29
#define ESP_SEQ_MAX
Definition: esp.h:75
u32 spi
Definition: esp.h:26
vnet_crypto_key_index_t integ_key_index
Definition: ipsec_sa.h:126
u8 integ_icv_size
Definition: ipsec_sa.h:116