| /* |
| * Copyright (c) 2015 Cisco and/or its affiliates. |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at: |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| #ifndef __ESP_H__ |
| #define __ESP_H__ |
| |
| #include <vnet/ip/ip.h> |
| #include <vnet/crypto/crypto.h> |
| #include <vnet/ipsec/ipsec.h> |
| |
| typedef struct |
| { |
| u32 spi; |
| u32 seq; |
| u8 data[0]; |
| } esp_header_t; |
| |
| typedef struct |
| { |
| u8 pad_length; |
| u8 next_header; |
| } esp_footer_t; |
| |
| /* *INDENT-OFF* */ |
| typedef CLIB_PACKED (struct { |
| ip4_header_t ip4; |
| esp_header_t esp; |
| }) ip4_and_esp_header_t; |
| /* *INDENT-ON* */ |
| |
| /* *INDENT-OFF* */ |
| typedef CLIB_PACKED (struct { |
| ip4_header_t ip4; |
| udp_header_t udp; |
| esp_header_t esp; |
| }) ip4_and_udp_and_esp_header_t; |
| /* *INDENT-ON* */ |
| |
| /* *INDENT-OFF* */ |
| typedef CLIB_PACKED (struct { |
| ip6_header_t ip6; |
| esp_header_t esp; |
| }) ip6_and_esp_header_t; |
| /* *INDENT-ON* */ |
| |
| #define ESP_WINDOW_SIZE (64) |
| #define ESP_SEQ_MAX (4294967295UL) |
| #define ESP_MAX_BLOCK_SIZE (16) |
| #define ESP_MAX_ICV_SIZE (16) |
| |
| u8 *format_esp_header (u8 * s, va_list * args); |
| |
| always_inline int |
| esp_replay_check (ipsec_sa_t * sa, u32 seq) |
| { |
| 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; |
| |
| return 0; |
| } |
| |
| always_inline int |
| esp_replay_check_esn (ipsec_sa_t * sa, u32 seq) |
| { |
| u32 tl = sa->last_seq; |
| u32 th = sa->last_seq_hi; |
| u32 diff = tl - seq; |
| |
| if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1))) |
| { |
| 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 |
| { |
| sa->seq_hi = th + 1; |
| return 0; |
| } |
| } |
| 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; |
| } |
| } |
| |
| 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) |
| { |
| u32 pos; |
| |
| if (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 |
| { |
| pos = sa->last_seq - seq; |
| sa->replay_window |= (1ULL << pos); |
| } |
| } |
| |
| always_inline void |
| esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq) |
| { |
| 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) |
| { |
| pos = ~seq + sa->last_seq + 1; |
| sa->replay_window |= (1ULL << pos); |
| } |
| else |
| { |
| pos = sa->last_seq - seq; |
| sa->replay_window |= (1ULL << pos); |
| } |
| } |
| |
| always_inline int |
| esp_seq_advance (ipsec_sa_t * sa) |
| { |
| if (PREDICT_TRUE (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa))) |
| { |
| if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX)) |
| { |
| 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 (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) && |
| sa->seq == ESP_SEQ_MAX)) |
| return 1; |
| sa->seq++; |
| } |
| |
| return 0; |
| } |
| |
| |
| always_inline unsigned int |
| hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len, |
| u8 * signature) |
| { |
| vnet_crypto_op_t _op, *op = &_op; |
| |
| if (PREDICT_FALSE (sa->integ_op_type == 0)) |
| return 0; |
| |
| op->op = sa->integ_op_type; |
| op->key = sa->integ_key.data; |
| op->key_len = sa->integ_key.len; |
| op->src = data; |
| op->len = data_len; |
| op->dst = signature; |
| op->hmac_trunc_len = sa->integ_trunc_size; |
| |
| if (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa)) |
| { |
| u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi); |
| |
| op->len += 4; |
| clib_memcpy (data + data_len, &seq_hi, 4); |
| } |
| |
| vnet_crypto_process_ops (vm, op, 1); |
| return sa->integ_trunc_size; |
| } |
| |
| #endif /* __ESP_H__ */ |
| |
| /* |
| * fd.io coding-style-patch-verification: ON |
| * |
| * Local Variables: |
| * eval: (c-set-style "gnu") |
| * End: |
| */ |