7 #ifndef ODP_IPSEC_MISC_H_
8 #define ODP_IPSEC_MISC_H_
15 #include <odp/helper/odph_api.h>
25 #define MAX_LOOPBACK 10
28 #define KEY_BITS_3DES 192
29 #define KEY_BITS_MD5_96 128
30 #define KEY_BITS_SHA1_96 160
31 #define KEY_BITS_SHA256_128 256
34 #define KEY_STR_BITS(str) (4 * strlen(str))
37 #define ipv4_data_len(ip) (odp_be_to_cpu_16(ip->tot_len) - sizeof(odph_ipv4hdr_t))
38 #define ipv4_data_p(ip) ((uint8_t *)((odph_ipv4hdr_t *)ip + 1))
41 #define ESP_ENCODE_LEN(x, b) ((((x) + (b - 1)) / b) * b)
44 #define NO_PATH(file_name) (strrchr((file_name), '/') ? \
45 strrchr((file_name), '/') + 1 : (file_name))
69 typedef struct ip_addr_range_s {
84 int parse_key_string(
char *keystring,
89 int key_bits_in = KEY_STR_BITS(keystring);
97 (KEY_BITS_3DES == key_bits_in))
98 key->length = key_bits_in / 8;
102 (KEY_BITS_MD5_96 == key_bits_in))
103 key->length = key_bits_in / 8;
105 (KEY_BITS_SHA1_96 == key_bits_in))
106 key->length = key_bits_in / 8;
108 (KEY_BITS_SHA256_128 == key_bits_in))
109 key->length = key_bits_in / 8;
112 for (idx = 0; idx < key->length; idx++) {
113 temp[0] = *keystring++;
114 temp[1] = *keystring++;
116 key->data[idx] = strtol(temp, NULL, 16);
119 return key->length ? 0 : -1;
131 int match_ip_range(uint32_t addr, ip_addr_range_t *range)
133 return (range->addr == (addr & range->mask));
145 char *ipv4_addr_str(
char *b, uint32_t addr)
147 sprintf(b,
"%03d.%03d.%03d.%03d",
148 0xFF & ((addr) >> 24),
149 0xFF & ((addr) >> 16),
150 0xFF & ((addr) >> 8),
151 0xFF & ((addr) >> 0));
168 int parse_ipv4_string(
char *ipaddress, uint32_t *addr, uint32_t *mask)
174 if (strchr(ipaddress,
'/')) {
175 converted = sscanf(ipaddress,
"%u.%u.%u.%u/%d",
176 &b[3], &b[2], &b[1], &b[0],
181 converted = sscanf(ipaddress,
"%u.%u.%u.%u",
182 &b[3], &b[2], &b[1], &b[0]);
187 if ((b[0] > 255) || (b[1] > 255) || (b[2] > 255) || (b[3] > 255))
189 if (!qualifier || (qualifier > 32))
192 *addr = (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
194 *mask = ~(0xFFFFFFFF & ((1ULL << (32 - qualifier)) - 1));
209 char *ipv4_subnet_str(
char *b, ip_addr_range_t *range)
214 for (idx = 0; idx < 32; idx++)
215 if (range->mask & (1 << idx))
219 sprintf(b,
"%03d.%03d.%03d.%03d/%d",
220 0xFF & ((range->addr) >> 24),
221 0xFF & ((range->addr) >> 16),
222 0xFF & ((range->addr) >> 8),
223 0xFF & ((range->addr) >> 0),
237 char *mac_addr_str(
char *b, uint8_t *mac)
239 sprintf(b,
"%02X:%02X:%02X:%02X:%02X:%02X",
240 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
255 int parse_mac_string(
char *macaddress, uint8_t *mac)
257 unsigned int macwords[ODPH_ETHADDR_LEN];
260 converted = sscanf(macaddress,
262 &macwords[0], &macwords[1], &macwords[2],
263 &macwords[3], &macwords[4], &macwords[5]);
267 mac[0] = macwords[0];
268 mac[1] = macwords[1];
269 mac[2] = macwords[2];
270 mac[3] = macwords[3];
271 mac[4] = macwords[4];
272 mac[5] = macwords[5];
287 int locate_ipsec_headers(odph_ipv4hdr_t *ip,
289 odph_esphdr_t **esp_p)
291 uint8_t *in = ipv4_data_p(ip);
292 odph_ahhdr_t *ah = NULL;
293 odph_esphdr_t *esp = NULL;
295 if (ODPH_IPPROTO_AH == ip->proto) {
296 ah = (odph_ahhdr_t *)in;
297 in += ((ah)->ah_len + 2) * 4;
298 if (ODPH_IPPROTO_ESP == ah->next_header) {
299 esp = (odph_esphdr_t *)in;
300 in +=
sizeof(odph_esphdr_t);
302 }
else if (ODPH_IPPROTO_ESP == ip->proto) {
303 esp = (odph_esphdr_t *)in;
304 in +=
sizeof(odph_esphdr_t);
309 return in - (ipv4_data_p(ip));
319 void ipv4_adjust_len(odph_ipv4hdr_t *ip,
int adj)
odp_u16be_t odp_cpu_to_be_16(uint16_t cpu16)
Convert cpu native uint16_t to 16bit big endian.
uint16_t odp_be_to_cpu_16(odp_u16be_t be16)
Convert 16bit big endian to cpu native uint16_t.
odp_cipher_alg_t
Crypto API cipher algorithm.
odp_auth_alg_t
Crypto API authentication algorithm.
@ ODP_CIPHER_ALG_3DES_CBC
Triple DES with cipher block chaining.
@ ODP_AUTH_ALG_MD5_HMAC
HMAC-MD5.
@ ODP_AUTH_ALG_SHA1_HMAC
HMAC-SHA-1.
@ ODP_AUTH_ALG_SHA256_HMAC
HMAC-SHA-256.
bool odp_bool_t
Boolean type.