16 #include <odp/helper/odph_api.h>
18 #include <odp_ipsec_sa_db.h>
20 odp_bool_t sa_config_supported(
const sa_db_entry_t *sa,
int *sa_flags);
23 static sa_db_t *sa_db;
26 static tun_db_t *tun_db;
38 ODPH_ERR(
"Error: shared mem reserve failed.\n");
45 ODPH_ERR(
"Error: shared mem alloc failed.\n");
48 memset(sa_db, 0,
sizeof(*sa_db));
51 void init_tun_db(
void)
61 ODPH_ERR(
"Error: shared mem reserve failed.\n");
68 ODPH_ERR(
"Error: shared mem alloc failed.\n");
71 memset(tun_db, 0,
sizeof(*tun_db));
74 int create_sa_db_entry(
char *input,
odp_bool_t cipher)
81 sa_db_entry_t *entry = &sa_db->array[sa_db->index];
84 if (MAX_DB <= sa_db->index)
88 local = malloc(strlen(input) + 1);
94 entry->alg.cipher = cipher;
101 while (NULL != (token = strtok_r(str,
",", &save))) {
107 parse_ipv4_string(token, &entry->src_ip, NULL);
110 parse_ipv4_string(token, &entry->dst_ip, NULL);
114 if (0 == strcmp(token,
"3des")) {
115 entry->alg.u.cipher =
117 entry->block_len = 8;
120 entry->alg.u.cipher =
124 if (0 == strcmp(token,
"md5")) {
128 }
else if (!strcmp(token,
"sha1")) {
132 }
else if (!strcmp(token,
"sha256")) {
142 entry->spi = strtol(token, NULL, 16);
145 parse_key_string(token,
150 printf(
"ERROR: extra token \"%s\" at position %d\n",
161 printf(
"ERROR: \"%s\" contains %d tokens, expected 5\n",
168 if (!sa_config_supported(entry, &entry->flags)) {
175 entry->next = sa_db->list;
182 int create_tun_db_entry(
char *input)
189 tun_db_entry_t *entry = &tun_db->array[tun_db->index];
192 if (MAX_DB <= tun_db->index)
196 local = malloc(strlen(input) + 1);
199 strcpy(local, input);
206 while (NULL != (token = strtok_r(str,
",", &save))) {
212 parse_ipv4_string(token, &entry->src_ip, NULL);
215 parse_ipv4_string(token, &entry->dst_ip, NULL);
218 parse_ipv4_string(token, &entry->tun_src_ip, NULL);
221 parse_ipv4_string(token, &entry->tun_dst_ip, NULL);
224 printf(
"ERROR: extra token \"%s\" at position %d\n",
233 printf(
"ERROR: \"%s\" contains %d tokens, expected 4\n",
242 entry->next = tun_db->list;
243 tun_db->list = entry;
249 tun_db_entry_t *find_tun_db_entry(uint32_t ip_src,
252 tun_db_entry_t *entry = NULL;
255 for (entry = tun_db->list; NULL != entry; entry = entry->next) {
256 if (entry->src_ip != ip_src)
258 if (entry->dst_ip != ip_dst)
265 void dump_sa_db(
void)
267 sa_db_entry_t *entry;
270 "Security association table\n"
271 "--------------------------\n");
273 for (entry = sa_db->list; NULL != entry; entry = entry->next) {
275 char src_ip_str[MAX_STRING];
276 char dst_ip_str[MAX_STRING];
277 uint8_t *p = entry->key.data;
280 printf(
" %s %s %s %X %d ",
281 entry->alg.cipher ?
"esp" :
"ah ",
282 ipv4_addr_str(src_ip_str, entry->src_ip),
283 ipv4_addr_str(dst_ip_str, entry->dst_ip),
286 (
int)entry->alg.u.cipher :
287 (
int)entry->alg.u.auth);
290 for (idx = 0; idx < entry->key.length; idx++)
291 printf(
"%02X", *p++);
297 sa_db_entry_t *find_sa_db_entry(ip_addr_range_t *src,
298 ip_addr_range_t *dst,
301 sa_db_entry_t *entry = NULL;
304 for (entry = sa_db->list; NULL != entry; entry = entry->next) {
305 if (cipher != entry->alg.cipher)
307 if (!match_ip_range(entry->src_ip, src))
309 if (!match_ip_range(entry->dst_ip, dst))
316 void dump_tun_db(
void)
318 tun_db_entry_t *entry;
322 "--------------------------\n");
324 for (entry = tun_db->list; NULL != entry; entry = entry->next) {
325 char src_ip_str[MAX_STRING];
326 char dst_ip_str[MAX_STRING];
327 char tun_src_ip_str[MAX_STRING];
328 char tun_dst_ip_str[MAX_STRING];
330 printf(
" %s:%s %s:%s ",
331 ipv4_addr_str(src_ip_str, entry->src_ip),
332 ipv4_addr_str(dst_ip_str, entry->dst_ip),
333 ipv4_addr_str(tun_src_ip_str, entry->tun_src_ip),
334 ipv4_addr_str(tun_dst_ip_str, entry->tun_dst_ip)
@ ODP_CIPHER_ALG_3DES_CBC
Triple DES with cipher block chaining.
@ ODP_CIPHER_ALG_NULL
No cipher algorithm specified.
@ ODP_AUTH_ALG_NULL
No authentication algorithm specified.
@ ODP_AUTH_ALG_MD5_HMAC
HMAC-MD5.
@ ODP_AUTH_ALG_SHA1_HMAC
HMAC-SHA-1.
@ ODP_AUTH_ALG_SHA256_HMAC
HMAC-SHA-256.
#define ODP_SHM_INVALID
Invalid shared memory block.
void * odp_shm_addr(odp_shm_t shm)
Shared memory block address.
odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, uint32_t flags)
Reserve a contiguous block of shared memory.
bool odp_bool_t
Boolean type.