28 #define foreach_esp_encrypt_next \ 29 _(DROP, "error-drop") \ 30 _(IP4_LOOKUP, "ip4-lookup") \ 31 _(IP6_LOOKUP, "ip6-lookup") \ 32 _(INTERFACE_OUTPUT, "interface-output") 34 #define _(v, s) ESP_ENCRYPT_NEXT_##v, 42 #define foreach_esp_encrypt_error \ 43 _(RX_PKTS, "ESP pkts received") \ 44 _(NO_BUFFER, "No buffer (packet dropped)") \ 45 _(DECRYPTION_FAILED, "ESP encryption failed") \ 46 _(SEQ_CYCLED, "sequence number cycled") 51 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym, 58 #define _(sym,string) string, 80 s =
format (s,
"esp: spi %u seq %u crypto %U integrity %U%s",
84 t->
udp_encap ?
" udp-encap-enabled" :
"");
90 u8 * in,
u8 * out,
size_t in_len,
u8 * key,
u8 * iv)
94 #if OPENSSL_VERSION_NUMBER >= 0x10100000L 99 const EVP_CIPHER *cipher =
NULL;
115 EVP_EncryptInit_ex (ctx, cipher,
NULL, key, iv);
117 EVP_EncryptUpdate (ctx, out, &out_len, in, in_len);
118 EVP_EncryptFinal_ex (ctx, out + out_len, &out_len);
126 u32 n_left_from, *from, *to_next = 0, next_index;
141 ESP_ENCRYPT_ERROR_NO_BUFFER, n_left_from);
142 clib_warning (
"not enough empty buffers. discarding frame");
143 goto free_buffers_and_exit;
148 while (n_left_from > 0)
154 while (n_left_from > 0 && n_left_to_next > 0)
156 u32 i_bi0, o_bi0, next0;
160 ip4_and_esp_header_t *oh0 = 0;
161 ip6_and_esp_header_t *ih6_0, *oh6_0 = 0;
162 ip4_and_udp_and_esp_header_t *iuh0, *ouh0 = 0;
163 uword last_empty_buffer;
169 u8 transport_mode = 0;
176 next0 = ESP_ENCRYPT_NEXT_DROP;
184 clib_warning (
"sequence number counter has cycled SPI %u",
187 ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
198 last_empty_buffer =
vec_len (empty_buffers) - 1;
199 o_bi0 = empty_buffers[last_empty_buffer];
201 o_b0->
flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
205 empty_buffers[last_empty_buffer -
207 _vec_len (empty_buffers) = last_empty_buffer;
217 next_hdr_type = IP_PROTOCOL_IPV6;
220 oh6_0->ip6.ip_version_traffic_class_and_flow_label =
221 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
222 oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
225 oh6_0->ip6.hop_limit = 254;
226 oh6_0->ip6.src_address.as_u64[0] =
227 ih6_0->ip6.src_address.as_u64[0];
228 oh6_0->ip6.src_address.as_u64[1] =
229 ih6_0->ip6.src_address.as_u64[1];
230 oh6_0->ip6.dst_address.as_u64[0] =
231 ih6_0->ip6.dst_address.as_u64[0];
232 oh6_0->ip6.dst_address.as_u64[1] =
233 ih6_0->ip6.dst_address.as_u64[1];
234 o_esp0->
spi = clib_net_to_host_u32 (sa0->
spi);
235 o_esp0->
seq = clib_net_to_host_u32 (sa0->
seq);
236 ip_proto = ih6_0->ip6.protocol;
238 next0 = ESP_ENCRYPT_NEXT_IP6_LOOKUP;
242 next_hdr_type = IP_PROTOCOL_IP_IN_IP;
246 oh0->ip4.ip_version_and_header_length = 0x45;
247 oh0->ip4.tos = iuh0->ip4.tos;
248 oh0->ip4.fragment_id = 0;
249 oh0->ip4.flags_and_fragment_offset = 0;
254 clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
256 clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
257 ouh0->udp.checksum = 0;
258 ouh0->ip4.protocol = IP_PROTOCOL_UDP;
264 oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
268 oh0->ip4.src_address.as_u32 = iuh0->ip4.src_address.as_u32;
269 oh0->ip4.dst_address.as_u32 = iuh0->ip4.dst_address.as_u32;
270 o_esp0->
spi = clib_net_to_host_u32 (sa0->
spi);
271 o_esp0->
seq = clib_net_to_host_u32 (sa0->
seq);
272 ip_proto = iuh0->ip4.protocol;
274 next0 = ESP_ENCRYPT_NEXT_IP4_LOOKUP;
286 oh6_0->ip6.src_address.as_u64[0] =
288 oh6_0->ip6.src_address.as_u64[1] =
290 oh6_0->ip6.dst_address.as_u64[0] =
292 oh6_0->ip6.dst_address.as_u64[1] =
299 next_hdr_type = ip_proto;
310 next0 = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
330 const int BLOCK_SIZE =
342 for (i = 0; i < pad_bytes; ++
i)
351 BLOCK_SIZE * blocks + IV_SIZE;
357 ipsec_proto_main_crypto_algs[sa0->
crypto_alg].iv_size];
358 RAND_bytes (iv,
sizeof (iv));
369 IV_SIZE, BLOCK_SIZE * blocks,
385 oh6_0->ip6.payload_length =
397 clib_host_to_net_u16 (clib_net_to_host_u16
411 o_b0->
flags |= VLIB_BUFFER_IS_TRACED;
424 to_next, n_left_to_next, o_bi0,
430 ESP_ENCRYPT_ERROR_RX_PKTS,
433 free_buffers_and_exit:
449 .name =
"esp4-encrypt",
450 .vector_size =
sizeof (
u32),
459 #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n, 475 .name =
"esp6-encrypt",
476 .vector_size =
sizeof (
u32),
485 #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n,
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
ip46_address_t tunnel_src_addr
static void vlib_buffer_reset(vlib_buffer_t *b)
Reset current header & length to state they were in when packet was received.
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
#define clib_memcpy_fast(a, b, c)
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)
ipsec_integ_alg_t integ_alg
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
static u8 * format_esp_encrypt_trace(u8 *s, va_list *args)
ipsec_integ_alg_t integ_alg
ipsec_crypto_alg_t crypto_alg
#define VLIB_NODE_FN(node)
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
ipsec_proto_main_crypto_alg_t * ipsec_proto_main_crypto_algs
ipsec_proto_main_t ipsec_proto_main
static int esp_seq_advance(ipsec_sa_t *sa)
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
#define foreach_esp_encrypt_next
#define vlib_prefetch_buffer_with_index(vm, bi, type)
Prefetch buffer metadata by buffer index The first 64 bytes of buffer contains most header informatio...
u8 * format_ipsec_crypto_alg(u8 *s, va_list *args)
vlib_node_registration_t esp6_encrypt_node
(constructor) VLIB_REGISTER_NODE (esp6_encrypt_node)
ipsec_proto_main_per_thread_data_t * per_thread_data
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
u16 current_length
Nbytes between current data and the end of this buffer.
static uword esp_encrypt_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, int is_ip6)
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
ipsec_crypto_alg_t last_encrypt_alg
u32 node_index
Node index.
#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.
#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).
static char * esp_encrypt_error_strings[]
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
ip46_address_t tunnel_dst_addr
#define VLIB_REGISTER_NODE(x,...)
#define vec_free(V)
Free vector's memory (no header).
vlib_node_registration_t esp4_encrypt_node
(constructor) VLIB_REGISTER_NODE (esp4_encrypt_node)
#define clib_warning(format, args...)
static void ipsec_alloc_empty_buffers(vlib_main_t *vm, ipsec_main_t *im)
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.
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
static void esp_encrypt_cbc(vlib_main_t *vm, ipsec_crypto_alg_t alg, u8 *in, u8 *out, size_t in_len, u8 *key, u8 *iv)
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
EVP_CIPHER_CTX encrypt_ctx
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
u8 * format_ipsec_integ_alg(u8 *s, va_list *args)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
ipsec_crypto_alg_t crypto_alg
static int ip4_header_bytes(const ip4_header_t *i)
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
#define foreach_esp_encrypt_error
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
u32 trace_index
Specifies index into trace buffer if VLIB_PACKET_IS_TRACED flag is set.
static u16 ip4_header_checksum(ip4_header_t *i)