API Reference Manual  1.46.0
ipsec_crypto/odp_ipsec_misc.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2014-2018 Linaro Limited
3  */
4 
7 #ifndef ODP_IPSEC_MISC_H_
8 #define ODP_IPSEC_MISC_H_
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include <odp_api.h>
15 #include <odp/helper/odph_api.h>
16 
17 #ifndef TRUE
18 #define TRUE 1
19 #endif
20 #ifndef FALSE
21 #define FALSE 0
22 #endif
23 
24 #define MAX_DB 32
25 #define MAX_LOOPBACK 10
26 #define MAX_STRING 32
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))
35 
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))
39 
41 #define ESP_ENCODE_LEN(x, b) ((((x) + (b - 1)) / b) * b)
42 
44 #define NO_PATH(file_name) (strrchr((file_name), '/') ? \
45  strrchr((file_name), '/') + 1 : (file_name))
46 
50 typedef struct {
51  uint8_t data[32];
52  uint8_t length;
53 } ipsec_key_t;
54 
58 typedef struct {
59  odp_bool_t cipher;
60  union {
61  odp_cipher_alg_t cipher;
62  odp_auth_alg_t auth;
63  } u;
64 } ipsec_alg_t;
65 
69 typedef struct ip_addr_range_s {
70  uint32_t addr;
71  uint32_t mask;
72 } ip_addr_range_t;
73 
83 static inline
84 int parse_key_string(char *keystring,
85  ipsec_key_t *key,
86  ipsec_alg_t *alg)
87 {
88  int idx;
89  int key_bits_in = KEY_STR_BITS(keystring);
90  char temp[3];
91 
92  key->length = 0;
93 
94  /* Algorithm is either cipher or authentication */
95  if (alg->cipher) {
96  if ((alg->u.cipher == ODP_CIPHER_ALG_3DES_CBC) &&
97  (KEY_BITS_3DES == key_bits_in))
98  key->length = key_bits_in / 8;
99 
100  } else {
101  if ((alg->u.auth == ODP_AUTH_ALG_MD5_HMAC) &&
102  (KEY_BITS_MD5_96 == key_bits_in))
103  key->length = key_bits_in / 8;
104  else if ((alg->u.auth == ODP_AUTH_ALG_SHA1_HMAC) &&
105  (KEY_BITS_SHA1_96 == key_bits_in))
106  key->length = key_bits_in / 8;
107  else if ((alg->u.auth == ODP_AUTH_ALG_SHA256_HMAC) &&
108  (KEY_BITS_SHA256_128 == key_bits_in))
109  key->length = key_bits_in / 8;
110  }
111 
112  for (idx = 0; idx < key->length; idx++) {
113  temp[0] = *keystring++;
114  temp[1] = *keystring++;
115  temp[2] = 0;
116  key->data[idx] = strtol(temp, NULL, 16);
117  }
118 
119  return key->length ? 0 : -1;
120 }
121 
130 static inline
131 int match_ip_range(uint32_t addr, ip_addr_range_t *range)
132 {
133  return (range->addr == (addr & range->mask));
134 }
135 
144 static inline
145 char *ipv4_addr_str(char *b, uint32_t addr)
146 {
147  sprintf(b, "%03d.%03d.%03d.%03d",
148  0xFF & ((addr) >> 24),
149  0xFF & ((addr) >> 16),
150  0xFF & ((addr) >> 8),
151  0xFF & ((addr) >> 0));
152  return b;
153 }
154 
167 static inline
168 int parse_ipv4_string(char *ipaddress, uint32_t *addr, uint32_t *mask)
169 {
170  unsigned int b[4];
171  int qualifier = 32;
172  int converted;
173 
174  if (strchr(ipaddress, '/')) {
175  converted = sscanf(ipaddress, "%u.%u.%u.%u/%d",
176  &b[3], &b[2], &b[1], &b[0],
177  &qualifier);
178  if (5 != converted)
179  return -1;
180  } else {
181  converted = sscanf(ipaddress, "%u.%u.%u.%u",
182  &b[3], &b[2], &b[1], &b[0]);
183  if (4 != converted)
184  return -1;
185  }
186 
187  if ((b[0] > 255) || (b[1] > 255) || (b[2] > 255) || (b[3] > 255))
188  return -1;
189  if (!qualifier || (qualifier > 32))
190  return -1;
191 
192  *addr = (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
193  if (mask)
194  *mask = ~(0xFFFFFFFF & ((1ULL << (32 - qualifier)) - 1));
195 
196  return 0;
197 }
198 
208 static inline
209 char *ipv4_subnet_str(char *b, ip_addr_range_t *range)
210 {
211  int idx;
212  int len;
213 
214  for (idx = 0; idx < 32; idx++)
215  if (range->mask & (1 << idx))
216  break;
217  len = 32 - idx;
218 
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),
224  len);
225  return b;
226 }
227 
236 static inline
237 char *mac_addr_str(char *b, uint8_t *mac)
238 {
239  sprintf(b, "%02X:%02X:%02X:%02X:%02X:%02X",
240  mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
241  return b;
242 }
243 
254 static inline
255 int parse_mac_string(char *macaddress, uint8_t *mac)
256 {
257  unsigned int macwords[ODPH_ETHADDR_LEN];
258  int converted;
259 
260  converted = sscanf(macaddress,
261  "%x:%x:%x:%x:%x:%x",
262  &macwords[0], &macwords[1], &macwords[2],
263  &macwords[3], &macwords[4], &macwords[5]);
264  if (6 != converted)
265  return -1;
266 
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];
273 
274  return 0;
275 }
276 
286 static inline
287 int locate_ipsec_headers(odph_ipv4hdr_t *ip,
288  odph_ahhdr_t **ah_p,
289  odph_esphdr_t **esp_p)
290 {
291  uint8_t *in = ipv4_data_p(ip);
292  odph_ahhdr_t *ah = NULL;
293  odph_esphdr_t *esp = NULL;
294 
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);
301  }
302  } else if (ODPH_IPPROTO_ESP == ip->proto) {
303  esp = (odph_esphdr_t *)in;
304  in += sizeof(odph_esphdr_t);
305  }
306 
307  *ah_p = ah;
308  *esp_p = esp;
309  return in - (ipv4_data_p(ip));
310 }
311 
318 static inline
319 void ipv4_adjust_len(odph_ipv4hdr_t *ip, int adj)
320 {
321  ip->tot_len = odp_cpu_to_be_16(odp_be_to_cpu_16(ip->tot_len) + adj);
322 }
323 
324 #ifdef __cplusplus
325 }
326 #endif
327 
328 #endif
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.
The OpenDataPlane API.