API Reference Manual  1.46.0
ipsec_api/odp_ipsec_sp_db.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2014-2018 Linaro Limited
3  */
4 
7 /* enable strtok */
8 #ifndef _GNU_SOURCE
9 #define _GNU_SOURCE
10 #endif
11 
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <odp_api.h>
16 #include <odp/helper/odph_api.h>
17 
18 #include <odp_ipsec_sp_db.h>
19 
21 sp_db_t *sp_db;
22 
23 void init_sp_db(void)
24 {
25  odp_shm_t shm;
26 
27  shm = odp_shm_reserve("shm_sp_db",
28  sizeof(sp_db_t),
29  ODP_CACHE_LINE_SIZE,
30  0);
31 
32  if (shm == ODP_SHM_INVALID) {
33  ODPH_ERR("Error: shared mem reserve failed.\n");
34  exit(EXIT_FAILURE);
35  }
36 
37  sp_db = odp_shm_addr(shm);
38 
39  if (sp_db == NULL) {
40  ODPH_ERR("Error: shared mem alloc failed.\n");
41  exit(EXIT_FAILURE);
42  }
43  memset(sp_db, 0, sizeof(*sp_db));
44 }
45 
46 int create_sp_db_entry(char *input, odp_bool_t both_supported)
47 {
48  int pos = 0;
49  char *local;
50  char *str;
51  char *save;
52  char *token;
53  sp_db_entry_t *entry = &sp_db->array[sp_db->index];
54 
55  /* Verify we have a good entry */
56  if (MAX_DB <= sp_db->index)
57  return -1;
58 
59  /* Make a local copy */
60  local = malloc(strlen(input) + 1);
61  if (NULL == local)
62  return -1;
63  strcpy(local, input);
64 
65  /* Setup for using "strtok_r" to search input string */
66  str = local;
67  save = NULL;
68 
69  /* Parse tokens separated by ',' */
70  while (NULL != (token = strtok_r(str, ",", &save))) {
71  str = NULL; /* reset str for subsequent strtok_r calls */
72 
73  /* Parse token based on its position */
74  switch (pos) {
75  case 0:
76  parse_ipv4_string(token,
77  &entry->src_subnet.addr,
78  &entry->src_subnet.mask);
79  break;
80  case 1:
81  parse_ipv4_string(token,
82  &entry->dst_subnet.addr,
83  &entry->dst_subnet.mask);
84  break;
85  case 2:
86  if (0 == strcmp(token, "in"))
87  entry->input = TRUE;
88  else
89  entry->input = FALSE;
90  break;
91  case 3:
92  if (0 == strcmp(token, "esp")) {
93  entry->esp = TRUE;
94  } else if (0 == strcmp(token, "ah")) {
95  entry->ah = TRUE;
96  } else if (0 == strcmp(token, "both")) {
97  entry->esp = TRUE;
98  entry->ah = TRUE;
99  }
100  break;
101  default:
102  printf("ERROR: extra token \"%s\" at position %d\n",
103  token, pos);
104  break;
105  }
106 
107  /* Advance to next position */
108  pos++;
109  }
110 
111  /* Check if enabling both AH and ESP protocols is supported */
112  if (!both_supported && (entry->ah && entry->esp)) {
113  printf("ERROR: enabling both AH and ESP is not supported\n");
114  free(local);
115  return -1;
116  }
117 
118  /* Verify we parsed exactly the number of tokens we expected */
119  if (4 != pos) {
120  printf("ERROR: \"%s\" contains %d tokens, expected 4\n",
121  input,
122  pos);
123  free(local);
124  return -1;
125  }
126 
127  /* Add route to the list */
128  sp_db->index++;
129  entry->next = sp_db->list;
130  sp_db->list = entry;
131 
132  free(local);
133  return 0;
134 }
135 
136 void dump_sp_db_entry(sp_db_entry_t *entry)
137 {
138  char src_subnet_str[MAX_STRING];
139  char dst_subnet_str[MAX_STRING];
140 
141  printf(" %s %s %s %s:%s\n",
142  ipv4_subnet_str(src_subnet_str, &entry->src_subnet),
143  ipv4_subnet_str(dst_subnet_str, &entry->dst_subnet),
144  entry->input ? "in" : "out",
145  entry->esp ? "esp" : "none",
146  entry->ah ? "ah" : "none");
147 }
148 
149 void dump_sp_db(void)
150 {
151  sp_db_entry_t *entry;
152 
153  printf("\n"
154  "Security policy table\n"
155  "---------------------\n");
156 
157  for (entry = sp_db->list; NULL != entry; entry = entry->next)
158  dump_sp_db_entry(entry);
159 }
#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.
The OpenDataPlane API.