aboutsummaryrefslogtreecommitdiff
path: root/example/ipsec_offload/odp_ipsec_offload_sp_db.c
blob: a286a1c44760aaa798e1ba6b6e3125721cb350b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2017, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/* enable strtok */
#define _POSIX_C_SOURCE 200112L

#include <stdlib.h>
#include <string.h>

#include <example_debug.h>

#include <odp.h>

#include <odp_ipsec_offload_sp_db.h>

/** Global pointer to sp db */
sp_db_t *sp_db;

void init_sp_db(void)
{
	odp_shm_t shm;

	shm = odp_shm_reserve("shm_sp_db",
			      sizeof(sp_db_t),
			      ODP_CACHE_LINE_SIZE,
			      0);

	sp_db = odp_shm_addr(shm);

	if (sp_db == NULL)
		EXAMPLE_ABORT("Error: shared mem alloc failed.\n");
	memset(sp_db, 0, sizeof(*sp_db));
}

int create_sp_db_entry(char *input, int entries)
{
	int pos = 0, count = 0;
	char *local;
	char *str;
	char *save;
	char *token;
	sp_db_entry_t *entry = &sp_db->array[sp_db->index];

	/* Verify we have a good entry */
	if (MAX_DB <= sp_db->index)
		return -1;

	/* Make a local copy */
	local = malloc(strlen(input) + 1);
	if (NULL == local)
		return -1;
	strcpy(local, input);

	/* Setup for using "strtok_r" to search input string */
	str = local;
	save = NULL;

	/* Parse tokens separated by ':' */
	while (NULL != (token = strtok_r(str, ":", &save))) {
		str = NULL;  /* reset str for subsequent strtok_r calls */

		/* Parse token based on its position */
		switch (pos) {
		case 0:
			parse_ipv4_string(token,
					  &entry->src_subnet.addr,
					  &entry->src_subnet.mask);
			break;
		case 1:
			parse_ipv4_string(token,
					  &entry->dst_subnet.addr,
					  &entry->dst_subnet.mask);
			break;
		case 2:
			if (0 == strcmp(token, "in"))
				entry->input = TRUE;
			else
				entry->input = FALSE;
			break;
		case 3:
			if (0 == strcmp(token, "esp")) {
				entry->esp = TRUE;
			} else if (0 == strcmp(token, "ah")) {
				entry->ah = TRUE;
			} else if (0 == strcmp(token, "both")) {
				entry->esp = TRUE;
				entry->ah = TRUE;
			}
			break;
		default:
			printf("ERROR: extra token \"%s\" at position %d\n",
			       token, pos);
			break;
		}

		/* Advance to next position */
		pos++;
	}

	/* Verify we parsed exactly the number of tokens we expected */
	if (4 != pos) {
		printf("ERROR: \"%s\" contains %d tokens, expected 4\n",
		       input,
		       pos);
		free(local);
		return -1;
	}

	/* Add route to the list */
	sp_db->index++;
	entry->next = sp_db->list;
	sp_db->list = entry;
	count++;
	while (count < entries) {
		sp_db_entry_t *new_entry = &sp_db->array[sp_db->index];

		/* Verify we have a good entry */
		if (MAX_DB <= sp_db->index)
			return -1;

		new_entry->src_subnet.addr = entry->src_subnet.addr + count;
		new_entry->src_subnet.mask = entry->src_subnet.mask;
		new_entry->dst_subnet.addr = entry->dst_subnet.addr + count;
		new_entry->dst_subnet.mask = entry->dst_subnet.mask;
		new_entry->input = entry->input;
		new_entry->esp = entry->esp;
		new_entry->ah = entry->ah;
		/* Add route to the list */
		sp_db->index++;
		new_entry->next = sp_db->list;
		sp_db->list = new_entry;
		count++;
	}

	free(local);
	return 0;
}

void dump_sp_db_entry(sp_db_entry_t *entry)
{
	char src_subnet_str[MAX_STRING];
	char dst_subnet_str[MAX_STRING];

	printf(" %s %s %s %s:%s\n",
	       ipv4_subnet_str(src_subnet_str, &entry->src_subnet),
	       ipv4_subnet_str(dst_subnet_str, &entry->dst_subnet),
	       entry->input ? "in" : "out",
	       entry->esp ? "esp" : "none",
	       entry->ah ? "ah" : "none");
}

void dump_sp_db(void)
{
	sp_db_entry_t *entry;

	printf("\n"
	       "Security policy table\n"
	       "---------------------\n");

	for (entry = sp_db->list; NULL != entry; entry = entry->next)
		dump_sp_db_entry(entry);
}