pki: Use correct enum value to initialize ID type in --keyid command
[strongswan.git] / src / libstrongswan / ipsec / ipsec_types.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "ipsec_types.h"
17
18 ENUM(ipsec_mode_names, MODE_TRANSPORT, MODE_DROP,
19 "TRANSPORT",
20 "TUNNEL",
21 "BEET",
22 "PASS",
23 "DROP"
24 );
25
26 ENUM(policy_dir_names, POLICY_IN, POLICY_FWD,
27 "in",
28 "out",
29 "fwd"
30 );
31
32 ENUM(ipcomp_transform_names, IPCOMP_NONE, IPCOMP_LZJH,
33 "IPCOMP_NONE",
34 "IPCOMP_OUI",
35 "IPCOMP_DEFLATE",
36 "IPCOMP_LZS",
37 "IPCOMP_LZJH"
38 );
39
40 ENUM(hw_offload_names, HW_OFFLOAD_NO, HW_OFFLOAD_AUTO,
41 "no",
42 "yes",
43 "auto",
44 );
45
46 ENUM(dscp_copy_names, DSCP_COPY_OUT_ONLY, DSCP_COPY_NO,
47 "out",
48 "in",
49 "yes",
50 "no",
51 );
52
53 /*
54 * See header
55 */
56 bool ipsec_sa_cfg_equals(ipsec_sa_cfg_t *a, ipsec_sa_cfg_t *b)
57 {
58 return a->mode == b->mode &&
59 a->reqid == b->reqid &&
60 a->policy_count == b->policy_count &&
61 a->esp.use == b->esp.use &&
62 a->esp.spi == b->esp.spi &&
63 a->ah.use == b->ah.use &&
64 a->ah.spi == b->ah.spi &&
65 a->ipcomp.transform == b->ipcomp.transform &&
66 a->ipcomp.cpi == b->ipcomp.cpi;
67 }
68
69 /*
70 * See header
71 */
72 bool mark_from_string(const char *value, mark_op_t ops, mark_t *mark)
73 {
74 char *endptr;
75
76 if (!value)
77 {
78 return FALSE;
79 }
80 if (strcasepfx(value, "%unique"))
81 {
82 if (!(ops & MARK_OP_UNIQUE))
83 {
84 DBG1(DBG_APP, "unexpected use of %%unique mark", value);
85 return FALSE;
86 }
87 endptr = (char*)value + strlen("%unique");
88 if (strcasepfx(endptr, "-dir"))
89 {
90 mark->value = MARK_UNIQUE_DIR;
91 endptr += strlen("-dir");
92 }
93 else if (!*endptr || *endptr == '/')
94 {
95 mark->value = MARK_UNIQUE;
96 }
97 else
98 {
99 DBG1(DBG_APP, "invalid mark value: %s", value);
100 return FALSE;
101 }
102 }
103 else if (strcasepfx(value, "%same"))
104 {
105 if (!(ops & MARK_OP_SAME))
106 {
107 DBG1(DBG_APP, "unexpected use of %%same mark", value);
108 return FALSE;
109 }
110 endptr = (char*)value + strlen("%same");
111 if (!*endptr || *endptr == '/')
112 {
113 mark->value = MARK_SAME;
114 }
115 else
116 {
117 DBG1(DBG_APP, "invalid mark value: %s", value);
118 return FALSE;
119 }
120 }
121 else
122 {
123 mark->value = strtoul(value, &endptr, 0);
124 }
125 if (*endptr)
126 {
127 if (*endptr != '/')
128 {
129 DBG1(DBG_APP, "invalid mark value: %s", value);
130 return FALSE;
131 }
132 mark->mask = strtoul(endptr+1, &endptr, 0);
133 if (*endptr)
134 {
135 DBG1(DBG_LIB, "invalid mark mask: %s", endptr);
136 return FALSE;
137 }
138 }
139 else
140 {
141 mark->mask = 0xffffffff;
142 }
143 if (!MARK_IS_UNIQUE(mark->value))
144 {
145 /* apply the mask to ensure the value is in range */
146 mark->value &= mark->mask;
147 }
148 return TRUE;
149 }
150
151 /*
152 * Described in header
153 */
154 bool if_id_from_string(const char *value, uint32_t *if_id)
155 {
156 char *endptr;
157
158 if (!value)
159 {
160 return FALSE;
161 }
162 if (strcasepfx(value, "%unique"))
163 {
164 endptr = (char*)value + strlen("%unique");
165 if (strcasepfx(endptr, "-dir"))
166 {
167 *if_id = IF_ID_UNIQUE_DIR;
168 endptr += strlen("-dir");
169 }
170 else if (!*endptr)
171 {
172 *if_id = IF_ID_UNIQUE;
173 }
174 else
175 {
176 DBG1(DBG_APP, "invalid interface ID: %s", value);
177 return FALSE;
178 }
179 }
180 else
181 {
182 *if_id = strtoul(value, &endptr, 0);
183 }
184 if (*endptr)
185 {
186 DBG1(DBG_APP, "invalid interface ID: %s", value);
187 return FALSE;
188 }
189 return TRUE;
190 }
191
192 /*
193 * Described in header
194 */
195 void allocate_unique_if_ids(uint32_t *in, uint32_t *out)
196 {
197 static refcount_t unique_if_id = 0;
198
199 if (IF_ID_IS_UNIQUE(*in) || IF_ID_IS_UNIQUE(*out))
200 {
201 refcount_t if_id = 0;
202 bool unique_dir = *in == IF_ID_UNIQUE_DIR ||
203 *out == IF_ID_UNIQUE_DIR;
204
205 if (!unique_dir)
206 {
207 if_id = ref_get(&unique_if_id);
208 }
209 if (IF_ID_IS_UNIQUE(*in))
210 {
211 *in = unique_dir ? ref_get(&unique_if_id) : if_id;
212 }
213 if (IF_ID_IS_UNIQUE(*out))
214 {
215 *out = unique_dir ? ref_get(&unique_if_id) : if_id;
216 }
217 }
218 }