blob: 0f75d5459ad4431acb35b998a46168e580da9023 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 */
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000015#ifndef __ESP_H__
16#define __ESP_H__
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010018#include <vnet/ip/ip.h>
Damjan Marion91f17dc2019-03-18 18:59:25 +010019#include <vnet/crypto/crypto.h>
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010020#include <vnet/ipsec/ipsec.h>
21
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070022typedef struct
23{
Ed Warnickecb9cada2015-12-08 15:45:58 -070024 u32 spi;
25 u32 seq;
26 u8 data[0];
27} esp_header_t;
28
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070029typedef struct
30{
Ed Warnickecb9cada2015-12-08 15:45:58 -070031 u8 pad_length;
32 u8 next_header;
33} esp_footer_t;
34
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070035/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070036typedef CLIB_PACKED (struct {
37 ip4_header_t ip4;
38 esp_header_t esp;
39}) ip4_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070041
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070042/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070043typedef CLIB_PACKED (struct {
Klement Sekera4b089f22018-04-17 18:04:57 +020044 ip4_header_t ip4;
45 udp_header_t udp;
46 esp_header_t esp;
47}) ip4_and_udp_and_esp_header_t;
48/* *INDENT-ON* */
49
50/* *INDENT-OFF* */
51typedef CLIB_PACKED (struct {
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 ip6_header_t ip6;
53 esp_header_t esp;
54}) ip6_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070055/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Damjan Marionc59b9a22019-03-19 15:38:40 +010057#define ESP_SEQ_MAX (4294967295UL)
58#define ESP_MAX_BLOCK_SIZE (16)
Damjan Marionb4fff3a2019-03-25 15:54:40 +010059#define ESP_MAX_IV_SIZE (16)
Damjan Marionc59b9a22019-03-19 15:38:40 +010060#define ESP_MAX_ICV_SIZE (16)
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000061
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010062u8 *format_esp_header (u8 * s, va_list * args);
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000063
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000064/* TODO seq increment should be atomic to be accessed by multiple workers */
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000065always_inline int
66esp_seq_advance (ipsec_sa_t * sa)
67{
Damjan Marion1e3aa5e2019-03-28 10:58:59 +010068 if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000069 {
70 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
71 {
Damjan Mariond709cbc2019-03-26 13:16:42 +010072 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
73 sa->seq_hi == ESP_SEQ_MAX))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000074 return 1;
75 sa->seq_hi++;
76 }
77 sa->seq++;
78 }
79 else
80 {
Damjan Mariond709cbc2019-03-26 13:16:42 +010081 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
82 sa->seq == ESP_SEQ_MAX))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000083 return 1;
84 sa->seq++;
85 }
86
87 return 0;
88}
89
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
91always_inline unsigned int
Damjan Marionb966e8b2019-03-20 16:07:09 +010092hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len,
93 u8 * signature)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
Damjan Marion91f17dc2019-03-18 18:59:25 +010095 vnet_crypto_op_t _op, *op = &_op;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
Damjan Marionb966e8b2019-03-20 16:07:09 +010097 if (PREDICT_FALSE (sa->integ_op_type == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 return 0;
99
Damjan Mariona03d1822019-03-28 21:40:48 +0100100 vnet_crypto_op_init (op, sa->integ_op_type);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100101 op->key = sa->integ_key.data;
102 op->key_len = sa->integ_key.len;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100103 op->src = data;
104 op->len = data_len;
105 op->dst = signature;
Damjan Marion88631232019-03-20 16:30:54 +0100106 op->hmac_trunc_len = sa->integ_trunc_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100108 if (ipsec_sa_is_set_USE_ESN (sa))
Damjan Marionaf73eda2019-03-20 16:30:54 +0100109 {
Neale Ranns3833ffd2019-03-21 14:34:09 +0000110 u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
111
Damjan Marionaf73eda2019-03-20 16:30:54 +0100112 op->len += 4;
Neale Ranns3833ffd2019-03-21 14:34:09 +0000113 clib_memcpy (data + data_len, &seq_hi, 4);
Damjan Marionaf73eda2019-03-20 16:30:54 +0100114 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Damjan Marion91f17dc2019-03-18 18:59:25 +0100116 vnet_crypto_process_ops (vm, op, 1);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100117 return sa->integ_trunc_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118}
119
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000120#endif /* __ESP_H__ */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700121
122/*
123 * fd.io coding-style-patch-verification: ON
124 *
125 * Local Variables:
126 * eval: (c-set-style "gnu")
127 * End:
128 */