typedef struct
{
- u32 spi;
+ union
+ {
+ u32 spi;
+ u8 spi_bytes[4];
+ };
u32 seq;
u8 data[0];
} esp_header_t;
}) ip6_and_esp_header_t;
/* *INDENT-ON* */
-#define ESP_WINDOW_SIZE (64)
-#define ESP_SEQ_MAX (4294967295UL)
-
-u8 *format_esp_header (u8 * s, va_list * args);
-
-always_inline int
-esp_replay_check (ipsec_sa_t * sa, u32 seq)
+/**
+ * AES GCM Additional Authentication data
+ */
+typedef struct esp_aead_t_
{
- u32 diff;
-
- if (PREDICT_TRUE (seq > sa->last_seq))
- return 0;
-
- diff = sa->last_seq - seq;
-
- if (ESP_WINDOW_SIZE > diff)
- return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
- else
- return 1;
+ /**
+ * for GCM: when using ESN it's:
+ * SPI, seq-hi, seg-low
+ * else
+ * SPI, seq-low
+ */
+ u32 data[3];
+} __clib_packed esp_aead_t;
+
+#define ESP_SEQ_MAX (4294967295UL)
+#define ESP_MAX_BLOCK_SIZE (16)
+#define ESP_MAX_IV_SIZE (16)
+#define ESP_MAX_ICV_SIZE (32)
- return 0;
-}
+u8 *format_esp_header (u8 * s, va_list * args);
+/* TODO seq increment should be atomic to be accessed by multiple workers */
always_inline int
-esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
+esp_seq_advance (ipsec_sa_t * sa)
{
- u32 tl = sa->last_seq;
- u32 th = sa->last_seq_hi;
- u32 diff = tl - seq;
-
- if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
+ if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
{
- if (seq >= (tl - ESP_WINDOW_SIZE + 1))
- {
- sa->seq_hi = th;
- if (seq <= tl)
- return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
- else
- return 0;
- }
- else
+ if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
{
- sa->seq_hi = th + 1;
- return 0;
+ if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
+ sa->seq_hi == ESP_SEQ_MAX))
+ return 1;
+ sa->seq_hi++;
}
+ sa->seq++;
}
else
{
- if (seq >= (tl - ESP_WINDOW_SIZE + 1))
- {
- sa->seq_hi = th - 1;
- return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
- }
- else
- {
- sa->seq_hi = th;
- if (seq <= tl)
- return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
- else
- return 0;
- }
+ if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
+ sa->seq == ESP_SEQ_MAX))
+ return 1;
+ sa->seq++;
}
return 0;
}
-/* TODO seq increment should be atomic to be accessed by multiple workers */
-always_inline void
-esp_replay_advance (ipsec_sa_t * sa, u32 seq)
+always_inline u16
+esp_aad_fill (u8 * data, const esp_header_t * esp, const ipsec_sa_t * sa)
{
- u32 pos;
+ esp_aead_t *aad;
+
+ aad = (esp_aead_t *) data;
+ aad->data[0] = esp->spi;
- if (seq > sa->last_seq)
+ if (ipsec_sa_is_set_USE_ESN (sa))
{
- pos = seq - sa->last_seq;
- if (pos < ESP_WINDOW_SIZE)
- sa->replay_window = ((sa->replay_window) << pos) | 1;
- else
- sa->replay_window = 1;
- sa->last_seq = seq;
+ /* SPI, seq-hi, seq-low */
+ aad->data[1] = (u32) clib_host_to_net_u32 (sa->seq_hi);
+ aad->data[2] = esp->seq;
+ return 12;
}
else
{
- pos = sa->last_seq - seq;
- sa->replay_window |= (1ULL << pos);
+ /* SPI, seq-low */
+ aad->data[1] = esp->seq;
+ return 8;
}
}
+/* Special case to drop or hand off packets for sync/async modes.
+ *
+ * Different than sync mode, async mode only enqueue drop or hand-off packets
+ * to next nodes.
+ */
always_inline void
-esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
+esp_set_next_index (int is_async, u32 * from, u16 * nexts, u32 bi,
+ u16 * drop_index, u16 drop_next, u16 * next)
{
- int wrap = sa->seq_hi - sa->last_seq_hi;
- u32 pos;
-
- if (wrap == 0 && seq > sa->last_seq)
- {
- pos = seq - sa->last_seq;
- if (pos < ESP_WINDOW_SIZE)
- sa->replay_window = ((sa->replay_window) << pos) | 1;
- else
- sa->replay_window = 1;
- sa->last_seq = seq;
- }
- else if (wrap > 0)
- {
- pos = ~seq + sa->last_seq + 1;
- if (pos < ESP_WINDOW_SIZE)
- sa->replay_window = ((sa->replay_window) << pos) | 1;
- else
- sa->replay_window = 1;
- sa->last_seq = seq;
- sa->last_seq_hi = sa->seq_hi;
- }
- else if (wrap < 0)
+ if (is_async)
{
- pos = ~seq + sa->last_seq + 1;
- sa->replay_window |= (1ULL << pos);
+ from[*drop_index] = bi;
+ nexts[*drop_index] = drop_next;
+ *drop_index += 1;
}
else
- {
- pos = sa->last_seq - seq;
- sa->replay_window |= (1ULL << pos);
- }
+ next[0] = drop_next;
}
-always_inline int
-esp_seq_advance (ipsec_sa_t * sa)
+/* when submitting a frame is failed, drop all buffers in the frame */
+always_inline void
+esp_async_recycle_failed_submit (vnet_crypto_async_frame_t * f,
+ vlib_buffer_t ** b, u32 * from, u16 * nexts,
+ u16 * n_dropped, u16 drop_next_index,
+ vlib_error_t err)
{
- if (PREDICT_TRUE (sa->use_esn))
+ u32 n_drop = f->n_elts;
+ u32 *bi = f->buffer_indices;
+ b -= n_drop;
+ while (n_drop--)
{
- if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
- {
- if (PREDICT_FALSE
- (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
- return 1;
- sa->seq_hi++;
- }
- sa->seq++;
+ b[0]->error = err;
+ esp_set_next_index (1, from, nexts, bi[0], n_dropped, drop_next_index,
+ NULL);
+ bi++;
+ b++;
}
- else
+ vnet_crypto_async_reset_frame (f);
+}
+
+/**
+ * The post data structure to for esp_encrypt/decrypt_inline to write to
+ * vib_buffer_t opaque unused field, and for post nodes to pick up after
+ * dequeue.
+ **/
+typedef struct
+{
+ union
+ {
+ struct
{
- if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
- return 1;
- sa->seq++;
- }
+ u8 icv_sz;
+ u8 iv_sz;
+ ipsec_sa_flags_t flags;
+ u32 sa_index;
+ };
+ u64 sa_data;
+ };
- return 0;
-}
+ u32 seq;
+ i16 current_data;
+ i16 current_length;
+ u16 hdr_sz;
+ u16 is_chain;
+ u32 protect_index;
+} esp_decrypt_packet_data_t;
+STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 3 * sizeof (u64));
-always_inline unsigned int
-hmac_calc (vlib_main_t * vm, ipsec_integ_alg_t alg, u8 * key, int key_len,
- u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
+/* we are forced to store the decrypt post data into 2 separate places -
+ vlib_opaque and opaque2. */
+typedef struct
{
- ipsec_main_t *im = &ipsec_main;
- vnet_crypto_op_t _op, *op = &_op;
- ASSERT (alg < IPSEC_INTEG_N_ALG);
+ vlib_buffer_t *lb;
+ u32 free_buffer_index;
+ u8 icv_removed;
+} esp_decrypt_packet_data2_t;
- if (PREDICT_FALSE (im->integ_algs[alg].op_type == 0))
- return 0;
+typedef union
+{
+ u16 next_index;
+ esp_decrypt_packet_data_t decrypt_data;
+} esp_post_data_t;
- op->op = im->integ_algs[alg].op_type;
- op->key = key;
- op->key_len = key_len;
- op->src = data;
- op->len = data_len;
- op->dst = signature;
-#if 0
+STATIC_ASSERT (sizeof (esp_post_data_t) <=
+ STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
+ "Custom meta-data too large for vnet_buffer_opaque_t");
- HMAC_Init_ex (ctx, key, key_len, md, NULL);
+#define esp_post_data(b) \
+ ((esp_post_data_t *)((u8 *)((b)->opaque) \
+ + STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))
- HMAC_Update (ctx, data, data_len);
+STATIC_ASSERT (sizeof (esp_decrypt_packet_data2_t) <=
+ STRUCT_SIZE_OF (vnet_buffer_opaque2_t, unused),
+ "Custom meta-data too large for vnet_buffer_opaque2_t");
- if (PREDICT_TRUE (use_esn))
- HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
- HMAC_Final (ctx, signature, &len);
+#define esp_post_data2(b) \
+ ((esp_decrypt_packet_data2_t *)((u8 *)((b)->opaque2) \
+ + STRUCT_OFFSET_OF (vnet_buffer_opaque2_t, unused)))
-#endif
- vnet_crypto_process_ops (vm, op, 1);
- return im->integ_algs[alg].trunc_size;
-}
+typedef struct
+{
+ /* esp post node index for async crypto */
+ u32 esp4_post_next;
+ u32 esp6_post_next;
+ u32 esp4_tun_post_next;
+ u32 esp6_tun_post_next;
+ u32 esp_mpls_tun_post_next;
+} esp_async_post_next_t;
+
+extern esp_async_post_next_t esp_encrypt_async_next;
+extern esp_async_post_next_t esp_decrypt_async_next;
#endif /* __ESP_H__ */